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