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