transport: use strbufs for status table "quickref" strings
[git/git.git] / builtin / fetch.c
CommitLineData
b888d61c
DB
1/*
2 * "git fetch"
3 */
4#include "cache.h"
5#include "refs.h"
6#include "commit.h"
7#include "builtin.h"
c455c87c 8#include "string-list.h"
b888d61c
DB
9#include "remote.h"
10#include "transport.h"
4191c356 11#include "run-command.h"
83201998 12#include "parse-options.h"
4a16d072 13#include "sigchain.h"
027771fc 14#include "submodule-config.h"
7dce19d3 15#include "submodule.h"
f96400cb 16#include "connected.h"
85556d4e 17#include "argv-array.h"
b888d61c 18
83201998 19static const char * const builtin_fetch_usage[] = {
719acedb
NTND
20 N_("git fetch [<options>] [<repository> [<refspec>...]]"),
21 N_("git fetch [<options>] <group>"),
22 N_("git fetch --multiple [<options>] [(<repository> | <group>)...]"),
23 N_("git fetch --all [<options>]"),
83201998
KH
24 NULL
25};
b888d61c 26
83201998
KH
27enum {
28 TAGS_UNSET = 0,
29 TAGS_DEFAULT = 1,
30 TAGS_SET = 2
31};
32
737c5a9c
MS
33static int fetch_prune_config = -1; /* unspecified */
34static int prune = -1; /* unspecified */
35#define PRUNE_BY_DEFAULT 0 /* do we prune by default? */
36
37static int all, append, dry_run, force, keep, multiple, update_head_ok, verbosity;
01fdc21f 38static int progress = -1, recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
48d25cae 39static int tags = TAGS_DEFAULT, unshallow, update_shallow;
4191c356 40static const char *depth;
83201998 41static const char *upload_pack;
2d324efa 42static struct strbuf default_rla = STRBUF_INIT;
af234459 43static struct transport *gtransport;
b26ed430 44static struct transport *gsecondary;
7dce19d3 45static const char *submodule_prefix = "";
88a21979 46static const char *recurse_submodules_default;
4b3b33a7 47static int shown_url = 0;
c5558f80
JH
48static int refmap_alloc, refmap_nr;
49static const char **refmap_array;
e4022ed2 50
8f0700dd
JL
51static int option_parse_recurse_submodules(const struct option *opt,
52 const char *arg, int unset)
53{
54 if (unset) {
55 recurse_submodules = RECURSE_SUBMODULES_OFF;
56 } else {
57 if (arg)
58 recurse_submodules = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
59 else
60 recurse_submodules = RECURSE_SUBMODULES_ON;
61 }
62 return 0;
63}
e4022ed2 64
737c5a9c
MS
65static int git_fetch_config(const char *k, const char *v, void *cb)
66{
67 if (!strcmp(k, "fetch.prune")) {
68 fetch_prune_config = git_config_bool(k, v);
69 return 0;
70 }
72549dfd 71 return git_default_config(k, v, cb);
737c5a9c
MS
72}
73
c5558f80
JH
74static int parse_refmap_arg(const struct option *opt, const char *arg, int unset)
75{
76 ALLOC_GROW(refmap_array, refmap_nr + 1, refmap_alloc);
77
78 /*
79 * "git fetch --refmap='' origin foo"
80 * can be used to tell the command not to store anywhere
81 */
82 if (*arg)
83 refmap_array[refmap_nr++] = arg;
84 return 0;
85}
86
83201998 87static struct option builtin_fetch_options[] = {
7f87aff2 88 OPT__VERBOSITY(&verbosity),
d5d09d47
SB
89 OPT_BOOL(0, "all", &all,
90 N_("fetch from all remotes")),
91 OPT_BOOL('a', "append", &append,
92 N_("append to .git/FETCH_HEAD instead of overwriting")),
719acedb
NTND
93 OPT_STRING(0, "upload-pack", &upload_pack, N_("path"),
94 N_("path to upload pack on remote end")),
95 OPT__FORCE(&force, N_("force overwrite of local branch")),
d5d09d47
SB
96 OPT_BOOL('m', "multiple", &multiple,
97 N_("fetch from multiple remotes")),
83201998 98 OPT_SET_INT('t', "tags", &tags,
719acedb 99 N_("fetch all tags and associated objects"), TAGS_SET),
e7951290 100 OPT_SET_INT('n', NULL, &tags,
719acedb 101 N_("do not fetch all tags (--no-tags)"), TAGS_UNSET),
d5d09d47
SB
102 OPT_BOOL('p', "prune", &prune,
103 N_("prune remote-tracking branches no longer on remote")),
719acedb
NTND
104 { OPTION_CALLBACK, 0, "recurse-submodules", NULL, N_("on-demand"),
105 N_("control recursive fetching of submodules"),
8f0700dd 106 PARSE_OPT_OPTARG, option_parse_recurse_submodules },
d5d09d47
SB
107 OPT_BOOL(0, "dry-run", &dry_run,
108 N_("dry run")),
109 OPT_BOOL('k', "keep", &keep, N_("keep downloaded pack")),
110 OPT_BOOL('u', "update-head-ok", &update_head_ok,
719acedb
NTND
111 N_("allow updating of HEAD ref")),
112 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
113 OPT_STRING(0, "depth", &depth, N_("depth"),
114 N_("deepen history of shallow clone")),
4dcb167f
NTND
115 { OPTION_SET_INT, 0, "unshallow", &unshallow, NULL,
116 N_("convert to a complete repository"),
117 PARSE_OPT_NONEG | PARSE_OPT_NOARG, NULL, 1 },
719acedb
NTND
118 { OPTION_STRING, 0, "submodule-prefix", &submodule_prefix, N_("dir"),
119 N_("prepend this to submodule path output"), PARSE_OPT_HIDDEN },
88a21979
JL
120 { OPTION_STRING, 0, "recurse-submodules-default",
121 &recurse_submodules_default, NULL,
719acedb 122 N_("default mode for recursion"), PARSE_OPT_HIDDEN },
48d25cae
NTND
123 OPT_BOOL(0, "update-shallow", &update_shallow,
124 N_("accept refs that update .git/shallow")),
c5558f80
JH
125 { OPTION_CALLBACK, 0, "refmap", NULL, N_("refmap"),
126 N_("specify fetch refmap"), PARSE_OPT_NONEG, parse_refmap_arg },
83201998
KH
127 OPT_END()
128};
129
e4022ed2
SP
130static void unlock_pack(void)
131{
af234459
JH
132 if (gtransport)
133 transport_unlock_pack(gtransport);
b26ed430
JH
134 if (gsecondary)
135 transport_unlock_pack(gsecondary);
e4022ed2
SP
136}
137
138static void unlock_pack_on_signal(int signo)
139{
140 unlock_pack();
4a16d072 141 sigchain_pop(signo);
e4022ed2
SP
142 raise(signo);
143}
b888d61c 144
85682c19 145static void add_merge_config(struct ref **head,
4577370e 146 const struct ref *remote_refs,
85682c19
SP
147 struct branch *branch,
148 struct ref ***tail)
b888d61c 149{
85682c19 150 int i;
b888d61c 151
85682c19
SP
152 for (i = 0; i < branch->merge_nr; i++) {
153 struct ref *rm, **old_tail = *tail;
154 struct refspec refspec;
155
156 for (rm = *head; rm; rm = rm->next) {
157 if (branch_merge_matches(branch, i, rm->name)) {
900f2814 158 rm->fetch_head_status = FETCH_HEAD_MERGE;
85682c19
SP
159 break;
160 }
b888d61c 161 }
85682c19
SP
162 if (rm)
163 continue;
164
9ad7c5ae 165 /*
8b3f3f84 166 * Not fetched to a remote-tracking branch? We need to fetch
85682c19 167 * it anyway to allow this branch's "branch.$name.merge"
05207a28 168 * to be honored by 'git pull', but we do not have to
9ad7c5ae
JH
169 * fail if branch.$name.merge is misconfigured to point
170 * at a nonexisting branch. If we were indeed called by
05207a28 171 * 'git pull', it will notice the misconfiguration because
9ad7c5ae
JH
172 * there is no entry in the resulting FETCH_HEAD marked
173 * for merging.
85682c19 174 */
8da61a2a 175 memset(&refspec, 0, sizeof(refspec));
85682c19 176 refspec.src = branch->merge[i]->src;
9ad7c5ae 177 get_fetch_map(remote_refs, &refspec, tail, 1);
85682c19 178 for (rm = *old_tail; rm; rm = rm->next)
900f2814 179 rm->fetch_head_status = FETCH_HEAD_MERGE;
b888d61c
DB
180 }
181}
182
0e0b7de4 183static int add_existing(const char *refname, const struct object_id *oid,
a0fbb5a3
MH
184 int flag, void *cbdata)
185{
186 struct string_list *list = (struct string_list *)cbdata;
187 struct string_list_item *item = string_list_insert(list, refname);
0e0b7de4
MH
188 struct object_id *old_oid = xmalloc(sizeof(*old_oid));
189
190 oidcpy(old_oid, oid);
191 item->util = old_oid;
a0fbb5a3
MH
192 return 0;
193}
194
195static int will_fetch(struct ref **head, const unsigned char *sha1)
196{
197 struct ref *rm = *head;
198 while (rm) {
199 if (!hashcmp(rm->old_sha1, sha1))
200 return 1;
201 rm = rm->next;
202 }
203 return 0;
204}
205
767f176a
SP
206static void find_non_local_tags(struct transport *transport,
207 struct ref **head,
a0fbb5a3
MH
208 struct ref ***tail)
209{
210 struct string_list existing_refs = STRING_LIST_INIT_DUP;
211 struct string_list remote_refs = STRING_LIST_INIT_NODUP;
212 const struct ref *ref;
213 struct string_list_item *item = NULL;
214
0e0b7de4 215 for_each_ref(add_existing, &existing_refs);
a0fbb5a3 216 for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
ad704485 217 if (!starts_with(ref->name, "refs/tags/"))
a0fbb5a3
MH
218 continue;
219
220 /*
221 * The peeled ref always follows the matching base
222 * ref, so if we see a peeled ref that we don't want
223 * to fetch then we can mark the ref entry in the list
224 * as one to ignore by setting util to NULL.
225 */
ad704485 226 if (ends_with(ref->name, "^{}")) {
a0fbb5a3
MH
227 if (item && !has_sha1_file(ref->old_sha1) &&
228 !will_fetch(head, ref->old_sha1) &&
229 !has_sha1_file(item->util) &&
230 !will_fetch(head, item->util))
231 item->util = NULL;
232 item = NULL;
233 continue;
234 }
235
236 /*
237 * If item is non-NULL here, then we previously saw a
238 * ref not followed by a peeled reference, so we need
239 * to check if it is a lightweight tag that we want to
240 * fetch.
241 */
242 if (item && !has_sha1_file(item->util) &&
243 !will_fetch(head, item->util))
244 item->util = NULL;
245
246 item = NULL;
247
248 /* skip duplicates and refs that we already have */
249 if (string_list_has_string(&remote_refs, ref->name) ||
250 string_list_has_string(&existing_refs, ref->name))
251 continue;
252
253 item = string_list_insert(&remote_refs, ref->name);
254 item->util = (void *)ref->old_sha1;
255 }
256 string_list_clear(&existing_refs, 1);
257
258 /*
259 * We may have a final lightweight tag that needs to be
260 * checked to see if it needs fetching.
261 */
262 if (item && !has_sha1_file(item->util) &&
263 !will_fetch(head, item->util))
264 item->util = NULL;
265
266 /*
267 * For all the tags in the remote_refs string list,
268 * add them to the list of refs to be fetched
269 */
270 for_each_string_list_item(item, &remote_refs) {
271 /* Unless we have already decided to ignore this item... */
272 if (item->util)
273 {
274 struct ref *rm = alloc_ref(item->string);
275 rm->peer_ref = alloc_ref(item->string);
276 hashcpy(rm->old_sha1, item->util);
277 **tail = rm;
278 *tail = &rm->next;
279 }
280 }
281
282 string_list_clear(&remote_refs, 0);
283}
767f176a 284
b888d61c 285static struct ref *get_ref_map(struct transport *transport,
f137a45e
MH
286 struct refspec *refspecs, int refspec_count,
287 int tags, int *autotags)
b888d61c
DB
288{
289 int i;
290 struct ref *rm;
291 struct ref *ref_map = NULL;
292 struct ref **tail = &ref_map;
293
c5a84e92
MH
294 /* opportunistically-updated references: */
295 struct ref *orefs = NULL, **oref_tail = &orefs;
b888d61c 296
c5a84e92 297 const struct ref *remote_refs = transport_get_remote_refs(transport);
f2690487 298
c5a84e92 299 if (refspec_count) {
c5558f80
JH
300 struct refspec *fetch_refspec;
301 int fetch_refspec_nr;
302
f137a45e
MH
303 for (i = 0; i < refspec_count; i++) {
304 get_fetch_map(remote_refs, &refspecs[i], &tail, 0);
305 if (refspecs[i].dst && refspecs[i].dst[0])
b888d61c
DB
306 *autotags = 1;
307 }
0281c930 308 /* Merge everything on the command line (but not --tags) */
b888d61c 309 for (rm = ref_map; rm; rm = rm->next)
900f2814 310 rm->fetch_head_status = FETCH_HEAD_MERGE;
f2690487
JK
311
312 /*
0281c930
MH
313 * For any refs that we happen to be fetching via
314 * command-line arguments, the destination ref might
315 * have been missing or have been different than the
316 * remote-tracking ref that would be derived from the
317 * configured refspec. In these cases, we want to
318 * take the opportunity to update their configured
319 * remote-tracking reference. However, we do not want
320 * to mention these entries in FETCH_HEAD at all, as
321 * they would simply be duplicates of existing
322 * entries, so we set them FETCH_HEAD_IGNORE below.
323 *
324 * We compute these entries now, based only on the
325 * refspecs specified on the command line. But we add
326 * them to the list following the refspecs resulting
327 * from the tags option so that one of the latter,
328 * which has FETCH_HEAD_NOT_FOR_MERGE, is not removed
329 * by ref_remove_duplicates() in favor of one of these
330 * opportunistic entries with FETCH_HEAD_IGNORE.
f2690487 331 */
c5558f80
JH
332 if (refmap_array) {
333 fetch_refspec = parse_fetch_refspec(refmap_nr, refmap_array);
334 fetch_refspec_nr = refmap_nr;
335 } else {
336 fetch_refspec = transport->remote->fetch;
337 fetch_refspec_nr = transport->remote->fetch_refspec_nr;
338 }
339
340 for (i = 0; i < fetch_refspec_nr; i++)
341 get_fetch_map(ref_map, &fetch_refspec[i], &oref_tail, 1);
0281c930
MH
342
343 if (tags == TAGS_SET)
344 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
c5558f80
JH
345 } else if (refmap_array) {
346 die("--refmap option is only meaningful with command-line refspec(s).");
b888d61c
DB
347 } else {
348 /* Use the defaults */
349 struct remote *remote = transport->remote;
85682c19
SP
350 struct branch *branch = branch_get(NULL);
351 int has_merge = branch_has_merge_config(branch);
3ee1757b
BC
352 if (remote &&
353 (remote->fetch_refspec_nr ||
f31dbdc7 354 /* Note: has_merge implies non-NULL branch->remote_name */
3ee1757b 355 (has_merge && !strcmp(branch->remote_name, remote->name)))) {
b888d61c 356 for (i = 0; i < remote->fetch_refspec_nr; i++) {
9ad7c5ae 357 get_fetch_map(remote_refs, &remote->fetch[i], &tail, 0);
b888d61c
DB
358 if (remote->fetch[i].dst &&
359 remote->fetch[i].dst[0])
360 *autotags = 1;
85682c19 361 if (!i && !has_merge && ref_map &&
cfb8f898 362 !remote->fetch[0].pattern)
900f2814 363 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
b888d61c 364 }
da0204df
JS
365 /*
366 * if the remote we're fetching from is the same
367 * as given in branch.<name>.remote, we add the
368 * ref given in branch.<name>.merge, too.
f31dbdc7
BC
369 *
370 * Note: has_merge implies non-NULL branch->remote_name
da0204df 371 */
9ad7c5ae
JH
372 if (has_merge &&
373 !strcmp(branch->remote_name, remote->name))
85682c19 374 add_merge_config(&ref_map, remote_refs, branch, &tail);
b888d61c
DB
375 } else {
376 ref_map = get_remote_ref(remote_refs, "HEAD");
9ad7c5ae 377 if (!ref_map)
bd4a51fc 378 die(_("Couldn't find remote ref HEAD"));
900f2814 379 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
5aaf7f2a 380 tail = &ref_map->next;
b888d61c
DB
381 }
382 }
0281c930 383
c5a84e92
MH
384 if (tags == TAGS_SET)
385 /* also fetch all tags */
386 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
387 else if (tags == TAGS_DEFAULT && *autotags)
767f176a 388 find_non_local_tags(transport, &ref_map, &tail);
0281c930 389
c5a84e92
MH
390 /* Now append any refs to be updated opportunistically: */
391 *tail = orefs;
392 for (rm = orefs; rm; rm = rm->next) {
393 rm->fetch_head_status = FETCH_HEAD_IGNORE;
394 tail = &rm->next;
395 }
396
b9afe665 397 return ref_remove_duplicates(ref_map);
b888d61c
DB
398}
399
fa250759
JK
400#define STORE_REF_ERROR_OTHER 1
401#define STORE_REF_ERROR_DF_CONFLICT 2
402
b888d61c
DB
403static int s_update_ref(const char *action,
404 struct ref *ref,
405 int check_old)
406{
407 char msg[1024];
408 char *rla = getenv("GIT_REFLOG_ACTION");
cd94f765
RS
409 struct ref_transaction *transaction;
410 struct strbuf err = STRBUF_INIT;
411 int ret, df_conflict = 0;
b888d61c 412
28a15401
JS
413 if (dry_run)
414 return 0;
b888d61c 415 if (!rla)
2d324efa 416 rla = default_rla.buf;
b888d61c 417 snprintf(msg, sizeof(msg), "%s: %s", rla, action);
cd94f765
RS
418
419 transaction = ref_transaction_begin(&err);
420 if (!transaction ||
1d147bdf
MH
421 ref_transaction_update(transaction, ref->name,
422 ref->new_sha1,
423 check_old ? ref->old_sha1 : NULL,
424 0, msg, &err))
cd94f765
RS
425 goto fail;
426
427 ret = ref_transaction_commit(transaction, &err);
428 if (ret) {
429 df_conflict = (ret == TRANSACTION_NAME_CONFLICT);
430 goto fail;
431 }
432
433 ref_transaction_free(transaction);
434 strbuf_release(&err);
b888d61c 435 return 0;
cd94f765
RS
436fail:
437 ref_transaction_free(transaction);
438 error("%s", err.buf);
439 strbuf_release(&err);
440 return df_conflict ? STORE_REF_ERROR_DF_CONFLICT
441 : STORE_REF_ERROR_OTHER;
b888d61c
DB
442}
443
9ef4272b 444#define REFCOL_WIDTH 10
165f3902 445
b888d61c 446static int update_local_ref(struct ref *ref,
165f3902 447 const char *remote,
6da618d5 448 const struct ref *remote_ref,
5914f2d0 449 struct strbuf *display)
b888d61c 450{
b888d61c
DB
451 struct commit *current = NULL, *updated;
452 enum object_type type;
453 struct branch *current_branch = branch_get(NULL);
4577e483 454 const char *pretty_ref = prettify_refname(ref->name);
b888d61c
DB
455
456 type = sha1_object_info(ref->new_sha1, NULL);
457 if (type < 0)
bd4a51fc 458 die(_("object %s not found"), sha1_to_hex(ref->new_sha1));
b888d61c 459
b888d61c 460 if (!hashcmp(ref->old_sha1, ref->new_sha1)) {
7f87aff2 461 if (verbosity > 0)
5914f2d0 462 strbuf_addf(display, "= %-*s %-*s -> %s",
754395d3
NTND
463 TRANSPORT_SUMMARY(_("[up to date]")),
464 REFCOL_WIDTH, remote, pretty_ref);
b888d61c
DB
465 return 0;
466 }
467
b3abdd9d
SP
468 if (current_branch &&
469 !strcmp(ref->name, current_branch->name) &&
b888d61c
DB
470 !(update_head_ok || is_bare_repository()) &&
471 !is_null_sha1(ref->old_sha1)) {
472 /*
473 * If this is the head, and it's not okay to update
474 * the head, and the old value of the head isn't empty...
475 */
5914f2d0
JK
476 strbuf_addf(display,
477 _("! %-*s %-*s -> %s (can't fetch in current branch)"),
754395d3 478 TRANSPORT_SUMMARY(_("[rejected]")),
5914f2d0 479 REFCOL_WIDTH, remote, pretty_ref);
b888d61c
DB
480 return 1;
481 }
482
483 if (!is_null_sha1(ref->old_sha1) &&
59556548 484 starts_with(ref->name, "refs/tags/")) {
6315472e
JK
485 int r;
486 r = s_update_ref("updating tag", ref, 0);
5914f2d0
JK
487 strbuf_addf(display, "%c %-*s %-*s -> %s%s",
488 r ? '!' : '-',
754395d3 489 TRANSPORT_SUMMARY(_("[tag update]")),
5914f2d0
JK
490 REFCOL_WIDTH, remote, pretty_ref,
491 r ? _(" (unable to update local ref)") : "");
6315472e 492 return r;
b888d61c
DB
493 }
494
cfa5b2b7
SP
495 current = lookup_commit_reference_gently(ref->old_sha1, 1);
496 updated = lookup_commit_reference_gently(ref->new_sha1, 1);
b888d61c 497 if (!current || !updated) {
165f3902
NP
498 const char *msg;
499 const char *what;
6315472e 500 int r;
0997adaa
MB
501 /*
502 * Nicely describe the new ref we're fetching.
503 * Base this on the remote's ref name, as it's
504 * more likely to follow a standard layout.
505 */
506 const char *name = remote_ref ? remote_ref->name : "";
59556548 507 if (starts_with(name, "refs/tags/")) {
b888d61c 508 msg = "storing tag";
f7b3742a 509 what = _("[new tag]");
59556548 510 } else if (starts_with(name, "refs/heads/")) {
b888d61c 511 msg = "storing head";
f7b3742a 512 what = _("[new branch]");
0997adaa
MB
513 } else {
514 msg = "storing ref";
515 what = _("[new ref]");
165f3902
NP
516 }
517
a6801adc
JL
518 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
519 (recurse_submodules != RECURSE_SUBMODULES_ON))
520 check_for_new_submodule_commits(ref->new_sha1);
6315472e 521 r = s_update_ref(msg, ref, 0);
5914f2d0
JK
522 strbuf_addf(display, "%c %-*s %-*s -> %s%s",
523 r ? '!' : '*',
754395d3 524 TRANSPORT_SUMMARY(what),
5914f2d0
JK
525 REFCOL_WIDTH, remote, pretty_ref,
526 r ? _(" (unable to update local ref)") : "");
6315472e 527 return r;
b888d61c
DB
528 }
529
a20efee9 530 if (in_merge_bases(current, updated)) {
bd22d4ff 531 struct strbuf quickref = STRBUF_INIT;
6315472e 532 int r;
bd22d4ff
JK
533 strbuf_add_unique_abbrev(&quickref, current->object.sha1, DEFAULT_ABBREV);
534 strbuf_addstr(&quickref, "..");
535 strbuf_add_unique_abbrev(&quickref, ref->new_sha1, DEFAULT_ABBREV);
88a21979
JL
536 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
537 (recurse_submodules != RECURSE_SUBMODULES_ON))
538 check_for_new_submodule_commits(ref->new_sha1);
a75d7b54 539 r = s_update_ref("fast-forward", ref, 1);
5914f2d0
JK
540 strbuf_addf(display, "%c %-*s %-*s -> %s%s",
541 r ? '!' : ' ',
bd22d4ff 542 TRANSPORT_SUMMARY_WIDTH, quickref.buf,
5914f2d0
JK
543 REFCOL_WIDTH, remote, pretty_ref,
544 r ? _(" (unable to update local ref)") : "");
bd22d4ff 545 strbuf_release(&quickref);
6315472e 546 return r;
165f3902 547 } else if (force || ref->force) {
bd22d4ff 548 struct strbuf quickref = STRBUF_INIT;
6315472e 549 int r;
bd22d4ff
JK
550 strbuf_add_unique_abbrev(&quickref, current->object.sha1, DEFAULT_ABBREV);
551 strbuf_addstr(&quickref, "...");
552 strbuf_add_unique_abbrev(&quickref, ref->new_sha1, DEFAULT_ABBREV);
88a21979
JL
553 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
554 (recurse_submodules != RECURSE_SUBMODULES_ON))
555 check_for_new_submodule_commits(ref->new_sha1);
6315472e 556 r = s_update_ref("forced-update", ref, 1);
5914f2d0
JK
557 strbuf_addf(display, "%c %-*s %-*s -> %s (%s)",
558 r ? '!' : '+',
bd22d4ff 559 TRANSPORT_SUMMARY_WIDTH, quickref.buf,
5914f2d0
JK
560 REFCOL_WIDTH, remote, pretty_ref,
561 r ? _("unable to update local ref") : _("forced update"));
bd22d4ff 562 strbuf_release(&quickref);
6315472e 563 return r;
165f3902 564 } else {
5914f2d0 565 strbuf_addf(display, "! %-*s %-*s -> %s %s",
754395d3 566 TRANSPORT_SUMMARY(_("[rejected]")),
5914f2d0
JK
567 REFCOL_WIDTH, remote, pretty_ref,
568 _("(non-fast-forward)"));
b888d61c
DB
569 return 1;
570 }
b888d61c
DB
571}
572
f0e278b1 573static int iterate_ref_map(void *cb_data, unsigned char sha1[20])
6b67e0dc 574{
f0e278b1
JH
575 struct ref **rm = cb_data;
576 struct ref *ref = *rm;
6b67e0dc 577
4820a33b
NTND
578 while (ref && ref->status == REF_STATUS_REJECT_SHALLOW)
579 ref = ref->next;
f0e278b1
JH
580 if (!ref)
581 return -1; /* end of the list */
582 *rm = ref->next;
583 hashcpy(sha1, ref->old_sha1);
584 return 0;
6b67e0dc
JH
585}
586
47abd85b 587static int store_updated_refs(const char *raw_url, const char *remote_name,
f3cb169b 588 struct ref *ref_map)
b888d61c
DB
589{
590 FILE *fp;
591 struct commit *commit;
4b3b33a7 592 int url_len, i, rc = 0;
5914f2d0 593 struct strbuf note = STRBUF_INIT;
b888d61c
DB
594 const char *what, *kind;
595 struct ref *rm;
dcf69262 596 char *url;
f932729c 597 const char *filename = dry_run ? "/dev/null" : git_path_fetch_head();
900f2814 598 int want_status;
b888d61c 599
d6617c7c
AGR
600 fp = fopen(filename, "a");
601 if (!fp)
bd4a51fc 602 return error(_("cannot open %s: %s\n"), filename, strerror(errno));
47abd85b 603
fb0cc87e
DB
604 if (raw_url)
605 url = transport_anonymize_url(raw_url);
606 else
607 url = xstrdup("foreign");
6b67e0dc 608
f0e278b1 609 rm = ref_map;
3f7d11c4 610 if (check_everything_connected(iterate_ref_map, 0, &rm)) {
9516a598
TRC
611 rc = error(_("%s did not send all necessary objects\n"), url);
612 goto abort;
613 }
6b67e0dc 614
96890f4c 615 /*
900f2814
JK
616 * We do a pass for each fetch_head_status type in their enum order, so
617 * merged entries are written before not-for-merge. That lets readers
618 * use FETCH_HEAD as a refname to refer to the ref to be merged.
96890f4c 619 */
900f2814
JK
620 for (want_status = FETCH_HEAD_MERGE;
621 want_status <= FETCH_HEAD_IGNORE;
622 want_status++) {
96890f4c
JH
623 for (rm = ref_map; rm; rm = rm->next) {
624 struct ref *ref = NULL;
900f2814 625 const char *merge_status_marker = "";
96890f4c 626
4820a33b
NTND
627 if (rm->status == REF_STATUS_REJECT_SHALLOW) {
628 if (want_status == FETCH_HEAD_MERGE)
629 warning(_("reject %s because shallow roots are not allowed to be updated"),
630 rm->peer_ref ? rm->peer_ref->name : rm->name);
631 continue;
632 }
633
96890f4c
JH
634 commit = lookup_commit_reference_gently(rm->old_sha1, 1);
635 if (!commit)
900f2814 636 rm->fetch_head_status = FETCH_HEAD_NOT_FOR_MERGE;
96890f4c 637
900f2814 638 if (rm->fetch_head_status != want_status)
96890f4c
JH
639 continue;
640
641 if (rm->peer_ref) {
642 ref = xcalloc(1, sizeof(*ref) + strlen(rm->peer_ref->name) + 1);
643 strcpy(ref->name, rm->peer_ref->name);
644 hashcpy(ref->old_sha1, rm->peer_ref->old_sha1);
645 hashcpy(ref->new_sha1, rm->old_sha1);
646 ref->force = rm->peer_ref->force;
647 }
b888d61c 648
b888d61c 649
96890f4c
JH
650 if (!strcmp(rm->name, "HEAD")) {
651 kind = "";
652 what = "";
653 }
59556548 654 else if (starts_with(rm->name, "refs/heads/")) {
96890f4c
JH
655 kind = "branch";
656 what = rm->name + 11;
657 }
59556548 658 else if (starts_with(rm->name, "refs/tags/")) {
96890f4c
JH
659 kind = "tag";
660 what = rm->name + 10;
661 }
59556548 662 else if (starts_with(rm->name, "refs/remotes/")) {
96890f4c
JH
663 kind = "remote-tracking branch";
664 what = rm->name + 13;
665 }
666 else {
667 kind = "";
668 what = rm->name;
669 }
b888d61c 670
96890f4c
JH
671 url_len = strlen(url);
672 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
673 ;
674 url_len = i + 1;
675 if (4 < i && !strncmp(".git", url + i - 3, 4))
676 url_len = i - 3;
677
678 strbuf_reset(&note);
679 if (*what) {
680 if (*kind)
681 strbuf_addf(&note, "%s ", kind);
682 strbuf_addf(&note, "'%s' of ", what);
683 }
900f2814
JK
684 switch (rm->fetch_head_status) {
685 case FETCH_HEAD_NOT_FOR_MERGE:
686 merge_status_marker = "not-for-merge";
687 /* fall-through */
688 case FETCH_HEAD_MERGE:
689 fprintf(fp, "%s\t%s\t%s",
690 sha1_to_hex(rm->old_sha1),
691 merge_status_marker,
692 note.buf);
693 for (i = 0; i < url_len; ++i)
694 if ('\n' == url[i])
695 fputs("\\n", fp);
696 else
697 fputc(url[i], fp);
698 fputc('\n', fp);
699 break;
700 default:
701 /* do not write anything to FETCH_HEAD */
702 break;
703 }
96890f4c
JH
704
705 strbuf_reset(&note);
706 if (ref) {
6da618d5 707 rc |= update_local_ref(ref, what, rm, &note);
96890f4c
JH
708 free(ref);
709 } else
710 strbuf_addf(&note, "* %-*s %-*s -> FETCH_HEAD",
711 TRANSPORT_SUMMARY_WIDTH,
712 *kind ? kind : "branch",
713 REFCOL_WIDTH,
714 *what ? what : "HEAD");
715 if (note.len) {
716 if (verbosity >= 0 && !shown_url) {
717 fprintf(stderr, _("From %.*s\n"),
718 url_len, url);
719 shown_url = 1;
720 }
721 if (verbosity >= 0)
722 fprintf(stderr, " %s\n", note.buf);
165f3902
NP
723 }
724 }
b888d61c 725 }
9516a598 726
fa250759 727 if (rc & STORE_REF_ERROR_DF_CONFLICT)
bd4a51fc 728 error(_("some local refs could not be updated; try running\n"
f3cb169b 729 " 'git remote prune %s' to remove any old, conflicting "
bd4a51fc 730 "branches"), remote_name);
9516a598
TRC
731
732 abort:
5914f2d0 733 strbuf_release(&note);
9516a598
TRC
734 free(url);
735 fclose(fp);
efb98b44 736 return rc;
b888d61c
DB
737}
738
4191c356
SP
739/*
740 * We would want to bypass the object transfer altogether if
d9eb0205 741 * everything we are going to fetch already exists and is connected
4191c356 742 * locally.
4191c356
SP
743 */
744static int quickfetch(struct ref *ref_map)
745{
f0e278b1
JH
746 struct ref *rm = ref_map;
747
4191c356
SP
748 /*
749 * If we are deepening a shallow clone we already have these
750 * objects reachable. Running rev-list here will return with
751 * a good (0) exit status and we'll bypass the fetch that we
752 * really need to perform. Claiming failure now will ensure
753 * we perform the network exchange to deepen our history.
754 */
755 if (depth)
756 return -1;
f0e278b1 757 return check_everything_connected(iterate_ref_map, 1, &rm);
4191c356
SP
758}
759
b888d61c
DB
760static int fetch_refs(struct transport *transport, struct ref *ref_map)
761{
4191c356
SP
762 int ret = quickfetch(ref_map);
763 if (ret)
764 ret = transport_fetch_refs(transport, ref_map);
b888d61c 765 if (!ret)
f3cb169b
JK
766 ret |= store_updated_refs(transport->url,
767 transport->remote->name,
768 ref_map);
1788c39c 769 transport_unlock_pack(transport);
b888d61c
DB
770 return ret;
771}
772
4b3b33a7
TM
773static int prune_refs(struct refspec *refs, int ref_count, struct ref *ref_map,
774 const char *raw_url)
f360d844 775{
4b3b33a7 776 int url_len, i, result = 0;
ed43de6e 777 struct ref *ref, *stale_refs = get_stale_heads(refs, ref_count, ref_map);
4b3b33a7 778 char *url;
f360d844 779 const char *dangling_msg = dry_run
18986d53
NTND
780 ? _(" (%s will become dangling)")
781 : _(" (%s has become dangling)");
f360d844 782
4b3b33a7
TM
783 if (raw_url)
784 url = transport_anonymize_url(raw_url);
785 else
786 url = xstrdup("foreign");
787
788 url_len = strlen(url);
789 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
790 ;
791
792 url_len = i + 1;
793 if (4 < i && !strncmp(".git", url + i - 3, 4))
794 url_len = i - 3;
795
a087b432
MH
796 if (!dry_run) {
797 struct string_list refnames = STRING_LIST_INIT_NODUP;
798
799 for (ref = stale_refs; ref; ref = ref->next)
800 string_list_append(&refnames, ref->name);
801
802 result = delete_refs(&refnames);
803 string_list_clear(&refnames, 0);
804 }
805
806 if (verbosity >= 0) {
807 for (ref = stale_refs; ref; ref = ref->next) {
808 if (!shown_url) {
809 fprintf(stderr, _("From %.*s\n"), url_len, url);
810 shown_url = 1;
811 }
f360d844 812 fprintf(stderr, " x %-*s %-*s -> %s\n",
754395d3 813 TRANSPORT_SUMMARY(_("[deleted]")),
502681cd 814 REFCOL_WIDTH, _("(none)"), prettify_refname(ref->name));
f360d844
JS
815 warn_dangling_symref(stderr, dangling_msg, ref->name);
816 }
817 }
a087b432 818
4b3b33a7 819 free(url);
f360d844
JS
820 free_refs(stale_refs);
821 return result;
822}
823
8ee5d731
JS
824static void check_not_current_branch(struct ref *ref_map)
825{
826 struct branch *current_branch = branch_get(NULL);
827
828 if (is_bare_repository() || !current_branch)
829 return;
830
831 for (; ref_map; ref_map = ref_map->next)
832 if (ref_map->peer_ref && !strcmp(current_branch->refname,
833 ref_map->peer_ref->name))
bd4a51fc
ÆAB
834 die(_("Refusing to fetch into current branch %s "
835 "of non-bare repository"), current_branch->refname);
8ee5d731
JS
836}
837
e6cc5104
JH
838static int truncate_fetch_head(void)
839{
f932729c 840 const char *filename = git_path_fetch_head();
e6cc5104
JH
841 FILE *fp = fopen(filename, "w");
842
843 if (!fp)
bd4a51fc 844 return error(_("cannot open %s: %s\n"), filename, strerror(errno));
e6cc5104
JH
845 fclose(fp);
846 return 0;
847}
848
db5723c6
JH
849static void set_option(struct transport *transport, const char *name, const char *value)
850{
851 int r = transport_set_option(transport, name, value);
852 if (r < 0)
853 die(_("Option \"%s\" value \"%s\" is not valid for %s"),
854 name, value, transport->url);
855 if (r > 0)
856 warning(_("Option \"%s\" is ignored for %s\n"),
857 name, transport->url);
858}
859
0f73f8bd 860static struct transport *prepare_transport(struct remote *remote)
db5723c6
JH
861{
862 struct transport *transport;
863 transport = transport_get(remote, NULL);
864 transport_set_verbosity(transport, verbosity, progress);
865 if (upload_pack)
866 set_option(transport, TRANS_OPT_UPLOADPACK, upload_pack);
867 if (keep)
868 set_option(transport, TRANS_OPT_KEEP, "yes");
869 if (depth)
870 set_option(transport, TRANS_OPT_DEPTH, depth);
48d25cae
NTND
871 if (update_shallow)
872 set_option(transport, TRANS_OPT_UPDATE_SHALLOW, "yes");
db5723c6
JH
873 return transport;
874}
875
069d5032
JH
876static void backfill_tags(struct transport *transport, struct ref *ref_map)
877{
b26ed430
JH
878 if (transport->cannot_reuse) {
879 gsecondary = prepare_transport(transport->remote);
880 transport = gsecondary;
881 }
882
069d5032
JH
883 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);
884 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
885 fetch_refs(transport, ref_map);
b26ed430
JH
886
887 if (gsecondary) {
888 transport_disconnect(gsecondary);
889 gsecondary = NULL;
890 }
069d5032
JH
891}
892
b888d61c
DB
893static int do_fetch(struct transport *transport,
894 struct refspec *refs, int ref_count)
895{
b87dbcc8 896 struct string_list existing_refs = STRING_LIST_INIT_DUP;
7f98428d 897 struct ref *ref_map;
b888d61c
DB
898 struct ref *rm;
899 int autotags = (transport->remote->fetch_tags == 1);
5b87d8d3 900 int retcode = 0;
b1a01e1c 901
0e0b7de4 902 for_each_ref(add_existing, &existing_refs);
b1a01e1c 903
ed368546
DJ
904 if (tags == TAGS_DEFAULT) {
905 if (transport->remote->fetch_tags == 2)
906 tags = TAGS_SET;
907 if (transport->remote->fetch_tags == -1)
908 tags = TAGS_UNSET;
909 }
b888d61c 910
824d5776 911 if (!transport->get_refs_list || !transport->fetch)
bd4a51fc 912 die(_("Don't know how to fetch from %s"), transport->url);
b888d61c
DB
913
914 /* if not appending, truncate FETCH_HEAD */
28a15401 915 if (!append && !dry_run) {
5b87d8d3
MH
916 retcode = truncate_fetch_head();
917 if (retcode)
918 goto cleanup;
d6617c7c 919 }
b888d61c
DB
920
921 ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
8ee5d731
JS
922 if (!update_head_ok)
923 check_not_current_branch(ref_map);
b888d61c
DB
924
925 for (rm = ref_map; rm; rm = rm->next) {
b1a01e1c 926 if (rm->peer_ref) {
6f64a16f
MH
927 struct string_list_item *peer_item =
928 string_list_lookup(&existing_refs,
929 rm->peer_ref->name);
0e0b7de4
MH
930 if (peer_item) {
931 struct object_id *old_oid = peer_item->util;
932 hashcpy(rm->peer_ref->old_sha1, old_oid->hash);
933 }
b1a01e1c 934 }
b888d61c
DB
935 }
936
41fa7d2e
SP
937 if (tags == TAGS_DEFAULT && autotags)
938 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
ed43de6e 939 if (prune) {
0838bf47
MH
940 /*
941 * We only prune based on refspecs specified
942 * explicitly (via command line or configuration); we
943 * don't care whether --tags was specified.
944 */
c5a84e92 945 if (ref_count) {
4b3b33a7 946 prune_refs(refs, ref_count, ref_map, transport->url);
e8c1e6c7 947 } else {
0838bf47
MH
948 prune_refs(transport->remote->fetch,
949 transport->remote->fetch_refspec_nr,
4b3b33a7
TM
950 ref_map,
951 transport->url);
e8c1e6c7 952 }
ed43de6e 953 }
10a6cc88
TM
954 if (fetch_refs(transport, ref_map)) {
955 free_refs(ref_map);
956 retcode = 1;
957 goto cleanup;
958 }
7f98428d 959 free_refs(ref_map);
b888d61c
DB
960
961 /* if neither --no-tags nor --tags was specified, do automated tag
962 * following ... */
83201998 963 if (tags == TAGS_DEFAULT && autotags) {
c50b2b47
SP
964 struct ref **tail = &ref_map;
965 ref_map = NULL;
966 find_non_local_tags(transport, &ref_map, &tail);
069d5032
JH
967 if (ref_map)
968 backfill_tags(transport, ref_map);
b888d61c
DB
969 free_refs(ref_map);
970 }
971
5b87d8d3 972 cleanup:
f83918ed 973 string_list_clear(&existing_refs, 1);
5b87d8d3 974 return retcode;
b888d61c
DB
975}
976
9c4a036b
BG
977static int get_one_remote_for_fetch(struct remote *remote, void *priv)
978{
979 struct string_list *list = priv;
7cc91a2f 980 if (!remote->skip_default_update)
1d2f80fa 981 string_list_append(list, remote->name);
9c4a036b
BG
982 return 0;
983}
984
985struct remote_group_data {
986 const char *name;
987 struct string_list *list;
988};
989
990static int get_remote_group(const char *key, const char *value, void *priv)
991{
992 struct remote_group_data *g = priv;
993
bc598c32 994 if (skip_prefix(key, "remotes.", &key) && !strcmp(key, g->name)) {
9c4a036b 995 /* split list by white space */
9c4a036b 996 while (*value) {
5f65499f
MH
997 size_t wordlen = strcspn(value, " \t\n");
998
e286542d 999 if (wordlen >= 1)
1d2f80fa 1000 string_list_append(g->list,
e286542d
MH
1001 xstrndup(value, wordlen));
1002 value += wordlen + (value[wordlen] != '\0');
9c4a036b
BG
1003 }
1004 }
1005
1006 return 0;
1007}
1008
1009static int add_remote_or_group(const char *name, struct string_list *list)
1010{
1011 int prev_nr = list->nr;
66dbfd55
GV
1012 struct remote_group_data g;
1013 g.name = name; g.list = list;
9c4a036b
BG
1014
1015 git_config(get_remote_group, &g);
1016 if (list->nr == prev_nr) {
1017 struct remote *remote;
1018 if (!remote_is_configured(name))
1019 return 0;
1020 remote = remote_get(name);
1d2f80fa 1021 string_list_append(list, remote->name);
9c4a036b
BG
1022 }
1023 return 1;
1024}
1025
85556d4e 1026static void add_options_to_argv(struct argv_array *argv)
9c4a036b 1027{
28a15401 1028 if (dry_run)
85556d4e 1029 argv_array_push(argv, "--dry-run");
90765fa3
MH
1030 if (prune != -1)
1031 argv_array_push(argv, prune ? "--prune" : "--no-prune");
bba5322a 1032 if (update_head_ok)
85556d4e 1033 argv_array_push(argv, "--update-head-ok");
bba5322a 1034 if (force)
85556d4e 1035 argv_array_push(argv, "--force");
bba5322a 1036 if (keep)
85556d4e 1037 argv_array_push(argv, "--keep");
be254a0e 1038 if (recurse_submodules == RECURSE_SUBMODULES_ON)
85556d4e 1039 argv_array_push(argv, "--recurse-submodules");
8f0700dd 1040 else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
85556d4e 1041 argv_array_push(argv, "--recurse-submodules=on-demand");
85566460
DJ
1042 if (tags == TAGS_SET)
1043 argv_array_push(argv, "--tags");
1044 else if (tags == TAGS_UNSET)
1045 argv_array_push(argv, "--no-tags");
9c4a036b 1046 if (verbosity >= 2)
85556d4e 1047 argv_array_push(argv, "-v");
9c4a036b 1048 if (verbosity >= 1)
85556d4e 1049 argv_array_push(argv, "-v");
9c4a036b 1050 else if (verbosity < 0)
85556d4e 1051 argv_array_push(argv, "-q");
7dce19d3
JL
1052
1053}
1054
1055static int fetch_multiple(struct string_list *list)
1056{
1057 int i, result = 0;
85556d4e 1058 struct argv_array argv = ARGV_ARRAY_INIT;
9c4a036b 1059
e6cc5104
JH
1060 if (!append && !dry_run) {
1061 int errcode = truncate_fetch_head();
1062 if (errcode)
1063 return errcode;
1064 }
1065
85556d4e
JK
1066 argv_array_pushl(&argv, "fetch", "--append", NULL);
1067 add_options_to_argv(&argv);
1068
9c4a036b
BG
1069 for (i = 0; i < list->nr; i++) {
1070 const char *name = list->items[i].string;
85556d4e 1071 argv_array_push(&argv, name);
9c4a036b 1072 if (verbosity >= 0)
bd4a51fc 1073 printf(_("Fetching %s\n"), name);
85556d4e 1074 if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) {
bd4a51fc 1075 error(_("Could not fetch %s"), name);
9c4a036b
BG
1076 result = 1;
1077 }
85556d4e 1078 argv_array_pop(&argv);
9c4a036b
BG
1079 }
1080
85556d4e 1081 argv_array_clear(&argv);
9c4a036b
BG
1082 return result;
1083}
1084
1085static int fetch_one(struct remote *remote, int argc, const char **argv)
b888d61c 1086{
b888d61c 1087 static const char **refs = NULL;
d8ead159 1088 struct refspec *refspec;
b888d61c 1089 int ref_nr = 0;
7b7f39ea 1090 int exit_code;
b888d61c 1091
fa685bdf 1092 if (!remote)
bd4a51fc
ÆAB
1093 die(_("No remote repository specified. Please, specify either a URL or a\n"
1094 "remote name from which new revisions should be fetched."));
fa685bdf 1095
db5723c6 1096 gtransport = prepare_transport(remote);
737c5a9c
MS
1097
1098 if (prune < 0) {
1099 /* no command line request */
20419de9
JH
1100 if (0 <= gtransport->remote->prune)
1101 prune = gtransport->remote->prune;
737c5a9c
MS
1102 else if (0 <= fetch_prune_config)
1103 prune = fetch_prune_config;
1104 else
1105 prune = PRUNE_BY_DEFAULT;
1106 }
1107
9c4a036b 1108 if (argc > 0) {
b888d61c 1109 int j = 0;
bf7e645c 1110 int i;
83201998 1111 refs = xcalloc(argc + 1, sizeof(const char *));
9c4a036b 1112 for (i = 0; i < argc; i++) {
b888d61c 1113 if (!strcmp(argv[i], "tag")) {
b888d61c 1114 i++;
f53423b0 1115 if (i >= argc)
bd4a51fc 1116 die(_("You need to specify a tag name."));
b2724c87
JK
1117 refs[j++] = xstrfmt("refs/tags/%s:refs/tags/%s",
1118 argv[i], argv[i]);
b888d61c
DB
1119 } else
1120 refs[j++] = argv[i];
b888d61c
DB
1121 }
1122 refs[j] = NULL;
1123 ref_nr = j;
b888d61c
DB
1124 }
1125
57b235a4 1126 sigchain_push_common(unlock_pack_on_signal);
e4022ed2 1127 atexit(unlock_pack);
d8ead159 1128 refspec = parse_fetch_refspec(ref_nr, refs);
af234459 1129 exit_code = do_fetch(gtransport, refspec, ref_nr);
5caf1973 1130 free_refspec(ref_nr, refspec);
af234459
JH
1131 transport_disconnect(gtransport);
1132 gtransport = NULL;
7b7f39ea 1133 return exit_code;
b888d61c 1134}
9c4a036b
BG
1135
1136int cmd_fetch(int argc, const char **argv, const char *prefix)
1137{
1138 int i;
183113a5 1139 struct string_list list = STRING_LIST_INIT_NODUP;
9c4a036b
BG
1140 struct remote *remote;
1141 int result = 0;
1991006c 1142 struct argv_array argv_gc_auto = ARGV_ARRAY_INIT;
9c4a036b 1143
bbc30f99
JK
1144 packet_trace_identity("fetch");
1145
9c4a036b
BG
1146 /* Record the command line for the reflog */
1147 strbuf_addstr(&default_rla, "fetch");
1148 for (i = 1; i < argc; i++)
1149 strbuf_addf(&default_rla, " %s", argv[i]);
1150
737c5a9c
MS
1151 git_config(git_fetch_config, NULL);
1152
9c4a036b
BG
1153 argc = parse_options(argc, argv, prefix,
1154 builtin_fetch_options, builtin_fetch_usage, 0);
1155
4dcb167f
NTND
1156 if (unshallow) {
1157 if (depth)
1158 die(_("--depth and --unshallow cannot be used together"));
1159 else if (!is_repository_shallow())
1160 die(_("--unshallow on a complete repository does not make sense"));
2805bb59
JK
1161 else
1162 depth = xstrfmt("%d", INFINITE_DEPTH);
4dcb167f
NTND
1163 }
1164
5594bcad
NTND
1165 /* no need to be strict, transport_set_option() will validate it again */
1166 if (depth && atoi(depth) < 1)
1167 die(_("depth %s is not a positive number"), depth);
1168
18322bad
JL
1169 if (recurse_submodules != RECURSE_SUBMODULES_OFF) {
1170 if (recurse_submodules_default) {
1171 int arg = parse_fetch_recurse_submodules_arg("--recurse-submodules-default", recurse_submodules_default);
1172 set_config_fetch_recurse_submodules(arg);
1173 }
1174 gitmodules_config();
1175 git_config(submodule_config, NULL);
1176 }
1177
9c4a036b
BG
1178 if (all) {
1179 if (argc == 1)
bd4a51fc 1180 die(_("fetch --all does not take a repository argument"));
9c4a036b 1181 else if (argc > 1)
bd4a51fc 1182 die(_("fetch --all does not make sense with refspecs"));
9c4a036b
BG
1183 (void) for_each_remote(get_one_remote_for_fetch, &list);
1184 result = fetch_multiple(&list);
1185 } else if (argc == 0) {
1186 /* No arguments -- use default remote */
1187 remote = remote_get(NULL);
1188 result = fetch_one(remote, argc, argv);
16679e37
BG
1189 } else if (multiple) {
1190 /* All arguments are assumed to be remotes or groups */
1191 for (i = 0; i < argc; i++)
1192 if (!add_remote_or_group(argv[i], &list))
bd4a51fc 1193 die(_("No such remote or remote group: %s"), argv[i]);
16679e37 1194 result = fetch_multiple(&list);
9c4a036b
BG
1195 } else {
1196 /* Single remote or group */
1197 (void) add_remote_or_group(argv[0], &list);
1198 if (list.nr > 1) {
1199 /* More than one remote */
1200 if (argc > 1)
bd4a51fc 1201 die(_("Fetching a group and specifying refspecs does not make sense"));
9c4a036b
BG
1202 result = fetch_multiple(&list);
1203 } else {
1204 /* Zero or one remotes */
1205 remote = remote_get(argv[0]);
1206 result = fetch_one(remote, argc-1, argv+1);
1207 }
1208 }
1209
be254a0e 1210 if (!result && (recurse_submodules != RECURSE_SUBMODULES_OFF)) {
85556d4e
JK
1211 struct argv_array options = ARGV_ARRAY_INIT;
1212
1213 add_options_to_argv(&options);
50d89ad6 1214 result = fetch_populated_submodules(&options,
7dce19d3 1215 submodule_prefix,
8f0700dd 1216 recurse_submodules,
7dce19d3 1217 verbosity < 0);
85556d4e 1218 argv_array_clear(&options);
7dce19d3
JL
1219 }
1220
9c4a036b
BG
1221 /* All names were strdup()ed or strndup()ed */
1222 list.strdup_strings = 1;
1223 string_list_clear(&list, 0);
1224
1991006c 1225 argv_array_pushl(&argv_gc_auto, "gc", "--auto", NULL);
6fceed3b
NTND
1226 if (verbosity < 0)
1227 argv_array_push(&argv_gc_auto, "--quiet");
1991006c
NTND
1228 run_command_v_opt(argv_gc_auto.argv, RUN_GIT_CMD);
1229 argv_array_clear(&argv_gc_auto);
131b8fcb 1230
9c4a036b
BG
1231 return result;
1232}