1/*
2 * "git fetch"
3 */
4#include "cache.h"
5#include "refs.h"
6#include "commit.h"
7#include "builtin.h"
8#include "string-list.h"
9#include "remote.h"
10#include "transport.h"
11#include "run-command.h"
12#include "parse-options.h"
13#include "sigchain.h"
14#include "submodule-config.h"
15#include "submodule.h"
16#include "connected.h"
17#include "argv-array.h"
18#include "utf8.h"
19
20static const char * const builtin_fetch_usage[] = {
21 N_("git fetch [<options>] [<repository> [<refspec>...]]"),
22 N_("git fetch [<options>] <group>"),
23 N_("git fetch --multiple [<options>] [(<repository> | <group>)...]"),
24 N_("git fetch --all [<options>]"),
25 NULL
26};
27
28enum {
29 TAGS_UNSET = 0,
30 TAGS_DEFAULT = 1,
31 TAGS_SET = 2
32};
33
34static int fetch_prune_config = -1; /* unspecified */
35static int prune = -1; /* unspecified */
36#define PRUNE_BY_DEFAULT 0 /* do we prune by default? */
37
38static int all, append, dry_run, force, keep, multiple, update_head_ok, verbosity, deepen_relative;
39static int progress = -1, recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
40static int tags = TAGS_DEFAULT, unshallow, update_shallow, deepen;
41static int max_children = -1;
42static enum transport_family family;
43static const char *depth;
44static const char *deepen_since;
45static const char *upload_pack;
46static struct string_list deepen_not = STRING_LIST_INIT_NODUP;
47static struct strbuf default_rla = STRBUF_INIT;
48static struct transport *gtransport;
49static struct transport *gsecondary;
50static const char *submodule_prefix = "";
51static const char *recurse_submodules_default;
52static int shown_url = 0;
53static int refmap_alloc, refmap_nr;
54static const char **refmap_array;
55
56static int option_parse_recurse_submodules(const struct option *opt,
57 const char *arg, int unset)
58{
59 if (unset) {
60 recurse_submodules = RECURSE_SUBMODULES_OFF;
61 } else {
62 if (arg)
63 recurse_submodules = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
64 else
65 recurse_submodules = RECURSE_SUBMODULES_ON;
66 }
67 return 0;
68}
69
70static int git_fetch_config(const char *k, const char *v, void *cb)
71{
72 if (!strcmp(k, "fetch.prune")) {
73 fetch_prune_config = git_config_bool(k, v);
74 return 0;
75 }
76
77 if (!strcmp(k, "submodule.recurse")) {
78 int r = git_config_bool(k, v) ?
79 RECURSE_SUBMODULES_ON : RECURSE_SUBMODULES_OFF;
80 recurse_submodules = r;
81 }
82
83 return git_default_config(k, v, cb);
84}
85
86static int parse_refmap_arg(const struct option *opt, const char *arg, int unset)
87{
88 ALLOC_GROW(refmap_array, refmap_nr + 1, refmap_alloc);
89
90 /*
91 * "git fetch --refmap='' origin foo"
92 * can be used to tell the command not to store anywhere
93 */
94 if (*arg)
95 refmap_array[refmap_nr++] = arg;
96 return 0;
97}
98
99static struct option builtin_fetch_options[] = {
100 OPT__VERBOSITY(&verbosity),
101 OPT_BOOL(0, "all", &all,
102 N_("fetch from all remotes")),
103 OPT_BOOL('a', "append", &append,
104 N_("append to .git/FETCH_HEAD instead of overwriting")),
105 OPT_STRING(0, "upload-pack", &upload_pack, N_("path"),
106 N_("path to upload pack on remote end")),
107 OPT__FORCE(&force, N_("force overwrite of local branch")),
108 OPT_BOOL('m', "multiple", &multiple,
109 N_("fetch from multiple remotes")),
110 OPT_SET_INT('t', "tags", &tags,
111 N_("fetch all tags and associated objects"), TAGS_SET),
112 OPT_SET_INT('n', NULL, &tags,
113 N_("do not fetch all tags (--no-tags)"), TAGS_UNSET),
114 OPT_INTEGER('j', "jobs", &max_children,
115 N_("number of submodules fetched in parallel")),
116 OPT_BOOL('p', "prune", &prune,
117 N_("prune remote-tracking branches no longer on remote")),
118 { OPTION_CALLBACK, 0, "recurse-submodules", NULL, N_("on-demand"),
119 N_("control recursive fetching of submodules"),
120 PARSE_OPT_OPTARG, option_parse_recurse_submodules },
121 OPT_BOOL(0, "dry-run", &dry_run,
122 N_("dry run")),
123 OPT_BOOL('k', "keep", &keep, N_("keep downloaded pack")),
124 OPT_BOOL('u', "update-head-ok", &update_head_ok,
125 N_("allow updating of HEAD ref")),
126 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
127 OPT_STRING(0, "depth", &depth, N_("depth"),
128 N_("deepen history of shallow clone")),
129 OPT_STRING(0, "shallow-since", &deepen_since, N_("time"),
130 N_("deepen history of shallow repository based on time")),
131 OPT_STRING_LIST(0, "shallow-exclude", &deepen_not, N_("revision"),
132 N_("deepen history of shallow clone, excluding rev")),
133 OPT_INTEGER(0, "deepen", &deepen_relative,
134 N_("deepen history of shallow clone")),
135 { OPTION_SET_INT, 0, "unshallow", &unshallow, NULL,
136 N_("convert to a complete repository"),
137 PARSE_OPT_NONEG | PARSE_OPT_NOARG, NULL, 1 },
138 { OPTION_STRING, 0, "submodule-prefix", &submodule_prefix, N_("dir"),
139 N_("prepend this to submodule path output"), PARSE_OPT_HIDDEN },
140 { OPTION_STRING, 0, "recurse-submodules-default",
141 &recurse_submodules_default, NULL,
142 N_("default mode for recursion"), PARSE_OPT_HIDDEN },
143 OPT_BOOL(0, "update-shallow", &update_shallow,
144 N_("accept refs that update .git/shallow")),
145 { OPTION_CALLBACK, 0, "refmap", NULL, N_("refmap"),
146 N_("specify fetch refmap"), PARSE_OPT_NONEG, parse_refmap_arg },
147 OPT_SET_INT('4', "ipv4", &family, N_("use IPv4 addresses only"),
148 TRANSPORT_FAMILY_IPV4),
149 OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"),
150 TRANSPORT_FAMILY_IPV6),
151 OPT_END()
152};
153
154static void unlock_pack(void)
155{
156 if (gtransport)
157 transport_unlock_pack(gtransport);
158 if (gsecondary)
159 transport_unlock_pack(gsecondary);
160}
161
162static void unlock_pack_on_signal(int signo)
163{
164 unlock_pack();
165 sigchain_pop(signo);
166 raise(signo);
167}
168
169static void add_merge_config(struct ref **head,
170 const struct ref *remote_refs,
171 struct branch *branch,
172 struct ref ***tail)
173{
174 int i;
175
176 for (i = 0; i < branch->merge_nr; i++) {
177 struct ref *rm, **old_tail = *tail;
178 struct refspec refspec;
179
180 for (rm = *head; rm; rm = rm->next) {
181 if (branch_merge_matches(branch, i, rm->name)) {
182 rm->fetch_head_status = FETCH_HEAD_MERGE;
183 break;
184 }
185 }
186 if (rm)
187 continue;
188
189 /*
190 * Not fetched to a remote-tracking branch? We need to fetch
191 * it anyway to allow this branch's "branch.$name.merge"
192 * to be honored by 'git pull', but we do not have to
193 * fail if branch.$name.merge is misconfigured to point
194 * at a nonexisting branch. If we were indeed called by
195 * 'git pull', it will notice the misconfiguration because
196 * there is no entry in the resulting FETCH_HEAD marked
197 * for merging.
198 */
199 memset(&refspec, 0, sizeof(refspec));
200 refspec.src = branch->merge[i]->src;
201 get_fetch_map(remote_refs, &refspec, tail, 1);
202 for (rm = *old_tail; rm; rm = rm->next)
203 rm->fetch_head_status = FETCH_HEAD_MERGE;
204 }
205}
206
207static int add_existing(const char *refname, const struct object_id *oid,
208 int flag, void *cbdata)
209{
210 struct string_list *list = (struct string_list *)cbdata;
211 struct string_list_item *item = string_list_insert(list, refname);
212 struct object_id *old_oid = xmalloc(sizeof(*old_oid));
213
214 oidcpy(old_oid, oid);
215 item->util = old_oid;
216 return 0;
217}
218
219static int will_fetch(struct ref **head, const unsigned char *sha1)
220{
221 struct ref *rm = *head;
222 while (rm) {
223 if (!hashcmp(rm->old_oid.hash, sha1))
224 return 1;
225 rm = rm->next;
226 }
227 return 0;
228}
229
230static void find_non_local_tags(struct transport *transport,
231 struct ref **head,
232 struct ref ***tail)
233{
234 struct string_list existing_refs = STRING_LIST_INIT_DUP;
235 struct string_list remote_refs = STRING_LIST_INIT_NODUP;
236 const struct ref *ref;
237 struct string_list_item *item = NULL;
238
239 for_each_ref(add_existing, &existing_refs);
240 for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
241 if (!starts_with(ref->name, "refs/tags/"))
242 continue;
243
244 /*
245 * The peeled ref always follows the matching base
246 * ref, so if we see a peeled ref that we don't want
247 * to fetch then we can mark the ref entry in the list
248 * as one to ignore by setting util to NULL.
249 */
250 if (ends_with(ref->name, "^{}")) {
251 if (item &&
252 !has_object_file_with_flags(&ref->old_oid, HAS_SHA1_QUICK) &&
253 !will_fetch(head, ref->old_oid.hash) &&
254 !has_sha1_file_with_flags(item->util, HAS_SHA1_QUICK) &&
255 !will_fetch(head, item->util))
256 item->util = NULL;
257 item = NULL;
258 continue;
259 }
260
261 /*
262 * If item is non-NULL here, then we previously saw a
263 * ref not followed by a peeled reference, so we need
264 * to check if it is a lightweight tag that we want to
265 * fetch.
266 */
267 if (item &&
268 !has_sha1_file_with_flags(item->util, HAS_SHA1_QUICK) &&
269 !will_fetch(head, item->util))
270 item->util = NULL;
271
272 item = NULL;
273
274 /* skip duplicates and refs that we already have */
275 if (string_list_has_string(&remote_refs, ref->name) ||
276 string_list_has_string(&existing_refs, ref->name))
277 continue;
278
279 item = string_list_insert(&remote_refs, ref->name);
280 item->util = (void *)&ref->old_oid;
281 }
282 string_list_clear(&existing_refs, 1);
283
284 /*
285 * We may have a final lightweight tag that needs to be
286 * checked to see if it needs fetching.
287 */
288 if (item &&
289 !has_sha1_file_with_flags(item->util, HAS_SHA1_QUICK) &&
290 !will_fetch(head, item->util))
291 item->util = NULL;
292
293 /*
294 * For all the tags in the remote_refs string list,
295 * add them to the list of refs to be fetched
296 */
297 for_each_string_list_item(item, &remote_refs) {
298 /* Unless we have already decided to ignore this item... */
299 if (item->util)
300 {
301 struct ref *rm = alloc_ref(item->string);
302 rm->peer_ref = alloc_ref(item->string);
303 oidcpy(&rm->old_oid, item->util);
304 **tail = rm;
305 *tail = &rm->next;
306 }
307 }
308
309 string_list_clear(&remote_refs, 0);
310}
311
312static struct ref *get_ref_map(struct transport *transport,
313 struct refspec *refspecs, int refspec_count,
314 int tags, int *autotags)
315{
316 int i;
317 struct ref *rm;
318 struct ref *ref_map = NULL;
319 struct ref **tail = &ref_map;
320
321 /* opportunistically-updated references: */
322 struct ref *orefs = NULL, **oref_tail = &orefs;
323
324 const struct ref *remote_refs = transport_get_remote_refs(transport);
325
326 if (refspec_count) {
327 struct refspec *fetch_refspec;
328 int fetch_refspec_nr;
329
330 for (i = 0; i < refspec_count; i++) {
331 get_fetch_map(remote_refs, &refspecs[i], &tail, 0);
332 if (refspecs[i].dst && refspecs[i].dst[0])
333 *autotags = 1;
334 }
335 /* Merge everything on the command line (but not --tags) */
336 for (rm = ref_map; rm; rm = rm->next)
337 rm->fetch_head_status = FETCH_HEAD_MERGE;
338
339 /*
340 * For any refs that we happen to be fetching via
341 * command-line arguments, the destination ref might
342 * have been missing or have been different than the
343 * remote-tracking ref that would be derived from the
344 * configured refspec. In these cases, we want to
345 * take the opportunity to update their configured
346 * remote-tracking reference. However, we do not want
347 * to mention these entries in FETCH_HEAD at all, as
348 * they would simply be duplicates of existing
349 * entries, so we set them FETCH_HEAD_IGNORE below.
350 *
351 * We compute these entries now, based only on the
352 * refspecs specified on the command line. But we add
353 * them to the list following the refspecs resulting
354 * from the tags option so that one of the latter,
355 * which has FETCH_HEAD_NOT_FOR_MERGE, is not removed
356 * by ref_remove_duplicates() in favor of one of these
357 * opportunistic entries with FETCH_HEAD_IGNORE.
358 */
359 if (refmap_array) {
360 fetch_refspec = parse_fetch_refspec(refmap_nr, refmap_array);
361 fetch_refspec_nr = refmap_nr;
362 } else {
363 fetch_refspec = transport->remote->fetch;
364 fetch_refspec_nr = transport->remote->fetch_refspec_nr;
365 }
366
367 for (i = 0; i < fetch_refspec_nr; i++)
368 get_fetch_map(ref_map, &fetch_refspec[i], &oref_tail, 1);
369 } else if (refmap_array) {
370 die("--refmap option is only meaningful with command-line refspec(s).");
371 } else {
372 /* Use the defaults */
373 struct remote *remote = transport->remote;
374 struct branch *branch = branch_get(NULL);
375 int has_merge = branch_has_merge_config(branch);
376 if (remote &&
377 (remote->fetch_refspec_nr ||
378 /* Note: has_merge implies non-NULL branch->remote_name */
379 (has_merge && !strcmp(branch->remote_name, remote->name)))) {
380 for (i = 0; i < remote->fetch_refspec_nr; i++) {
381 get_fetch_map(remote_refs, &remote->fetch[i], &tail, 0);
382 if (remote->fetch[i].dst &&
383 remote->fetch[i].dst[0])
384 *autotags = 1;
385 if (!i && !has_merge && ref_map &&
386 !remote->fetch[0].pattern)
387 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
388 }
389 /*
390 * if the remote we're fetching from is the same
391 * as given in branch.<name>.remote, we add the
392 * ref given in branch.<name>.merge, too.
393 *
394 * Note: has_merge implies non-NULL branch->remote_name
395 */
396 if (has_merge &&
397 !strcmp(branch->remote_name, remote->name))
398 add_merge_config(&ref_map, remote_refs, branch, &tail);
399 } else {
400 ref_map = get_remote_ref(remote_refs, "HEAD");
401 if (!ref_map)
402 die(_("Couldn't find remote ref HEAD"));
403 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
404 tail = &ref_map->next;
405 }
406 }
407
408 if (tags == TAGS_SET)
409 /* also fetch all tags */
410 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
411 else if (tags == TAGS_DEFAULT && *autotags)
412 find_non_local_tags(transport, &ref_map, &tail);
413
414 /* Now append any refs to be updated opportunistically: */
415 *tail = orefs;
416 for (rm = orefs; rm; rm = rm->next) {
417 rm->fetch_head_status = FETCH_HEAD_IGNORE;
418 tail = &rm->next;
419 }
420
421 return ref_remove_duplicates(ref_map);
422}
423
424#define STORE_REF_ERROR_OTHER 1
425#define STORE_REF_ERROR_DF_CONFLICT 2
426
427static int s_update_ref(const char *action,
428 struct ref *ref,
429 int check_old)
430{
431 char *msg;
432 char *rla = getenv("GIT_REFLOG_ACTION");
433 struct ref_transaction *transaction;
434 struct strbuf err = STRBUF_INIT;
435 int ret, df_conflict = 0;
436
437 if (dry_run)
438 return 0;
439 if (!rla)
440 rla = default_rla.buf;
441 msg = xstrfmt("%s: %s", rla, action);
442
443 transaction = ref_transaction_begin(&err);
444 if (!transaction ||
445 ref_transaction_update(transaction, ref->name,
446 ref->new_oid.hash,
447 check_old ? ref->old_oid.hash : NULL,
448 0, msg, &err))
449 goto fail;
450
451 ret = ref_transaction_commit(transaction, &err);
452 if (ret) {
453 df_conflict = (ret == TRANSACTION_NAME_CONFLICT);
454 goto fail;
455 }
456
457 ref_transaction_free(transaction);
458 strbuf_release(&err);
459 free(msg);
460 return 0;
461fail:
462 ref_transaction_free(transaction);
463 error("%s", err.buf);
464 strbuf_release(&err);
465 free(msg);
466 return df_conflict ? STORE_REF_ERROR_DF_CONFLICT
467 : STORE_REF_ERROR_OTHER;
468}
469
470static int refcol_width = 10;
471static int compact_format;
472
473static void adjust_refcol_width(const struct ref *ref)
474{
475 int max, rlen, llen, len;
476
477 /* uptodate lines are only shown on high verbosity level */
478 if (!verbosity && !oidcmp(&ref->peer_ref->old_oid, &ref->old_oid))
479 return;
480
481 max = term_columns();
482 rlen = utf8_strwidth(prettify_refname(ref->name));
483
484 llen = utf8_strwidth(prettify_refname(ref->peer_ref->name));
485
486 /*
487 * rough estimation to see if the output line is too long and
488 * should not be counted (we can't do precise calculation
489 * anyway because we don't know if the error explanation part
490 * will be printed in update_local_ref)
491 */
492 if (compact_format) {
493 llen = 0;
494 max = max * 2 / 3;
495 }
496 len = 21 /* flag and summary */ + rlen + 4 /* -> */ + llen;
497 if (len >= max)
498 return;
499
500 /*
501 * Not precise calculation for compact mode because '*' can
502 * appear on the left hand side of '->' and shrink the column
503 * back.
504 */
505 if (refcol_width < rlen)
506 refcol_width = rlen;
507}
508
509static void prepare_format_display(struct ref *ref_map)
510{
511 struct ref *rm;
512 const char *format = "full";
513
514 git_config_get_string_const("fetch.output", &format);
515 if (!strcasecmp(format, "full"))
516 compact_format = 0;
517 else if (!strcasecmp(format, "compact"))
518 compact_format = 1;
519 else
520 die(_("configuration fetch.output contains invalid value %s"),
521 format);
522
523 for (rm = ref_map; rm; rm = rm->next) {
524 if (rm->status == REF_STATUS_REJECT_SHALLOW ||
525 !rm->peer_ref ||
526 !strcmp(rm->name, "HEAD"))
527 continue;
528
529 adjust_refcol_width(rm);
530 }
531}
532
533static void print_remote_to_local(struct strbuf *display,
534 const char *remote, const char *local)
535{
536 strbuf_addf(display, "%-*s -> %s", refcol_width, remote, local);
537}
538
539static int find_and_replace(struct strbuf *haystack,
540 const char *needle,
541 const char *placeholder)
542{
543 const char *p = strstr(haystack->buf, needle);
544 int plen, nlen;
545
546 if (!p)
547 return 0;
548
549 if (p > haystack->buf && p[-1] != '/')
550 return 0;
551
552 plen = strlen(p);
553 nlen = strlen(needle);
554 if (plen > nlen && p[nlen] != '/')
555 return 0;
556
557 strbuf_splice(haystack, p - haystack->buf, nlen,
558 placeholder, strlen(placeholder));
559 return 1;
560}
561
562static void print_compact(struct strbuf *display,
563 const char *remote, const char *local)
564{
565 struct strbuf r = STRBUF_INIT;
566 struct strbuf l = STRBUF_INIT;
567
568 if (!strcmp(remote, local)) {
569 strbuf_addf(display, "%-*s -> *", refcol_width, remote);
570 return;
571 }
572
573 strbuf_addstr(&r, remote);
574 strbuf_addstr(&l, local);
575
576 if (!find_and_replace(&r, local, "*"))
577 find_and_replace(&l, remote, "*");
578 print_remote_to_local(display, r.buf, l.buf);
579
580 strbuf_release(&r);
581 strbuf_release(&l);
582}
583
584static void format_display(struct strbuf *display, char code,
585 const char *summary, const char *error,
586 const char *remote, const char *local,
587 int summary_width)
588{
589 int width = (summary_width + strlen(summary) - gettext_width(summary));
590
591 strbuf_addf(display, "%c %-*s ", code, width, summary);
592 if (!compact_format)
593 print_remote_to_local(display, remote, local);
594 else
595 print_compact(display, remote, local);
596 if (error)
597 strbuf_addf(display, " (%s)", error);
598}
599
600static int update_local_ref(struct ref *ref,
601 const char *remote,
602 const struct ref *remote_ref,
603 struct strbuf *display,
604 int summary_width)
605{
606 struct commit *current = NULL, *updated;
607 enum object_type type;
608 struct branch *current_branch = branch_get(NULL);
609 const char *pretty_ref = prettify_refname(ref->name);
610
611 type = sha1_object_info(ref->new_oid.hash, NULL);
612 if (type < 0)
613 die(_("object %s not found"), oid_to_hex(&ref->new_oid));
614
615 if (!oidcmp(&ref->old_oid, &ref->new_oid)) {
616 if (verbosity > 0)
617 format_display(display, '=', _("[up to date]"), NULL,
618 remote, pretty_ref, summary_width);
619 return 0;
620 }
621
622 if (current_branch &&
623 !strcmp(ref->name, current_branch->name) &&
624 !(update_head_ok || is_bare_repository()) &&
625 !is_null_oid(&ref->old_oid)) {
626 /*
627 * If this is the head, and it's not okay to update
628 * the head, and the old value of the head isn't empty...
629 */
630 format_display(display, '!', _("[rejected]"),
631 _("can't fetch in current branch"),
632 remote, pretty_ref, summary_width);
633 return 1;
634 }
635
636 if (!is_null_oid(&ref->old_oid) &&
637 starts_with(ref->name, "refs/tags/")) {
638 int r;
639 r = s_update_ref("updating tag", ref, 0);
640 format_display(display, r ? '!' : 't', _("[tag update]"),
641 r ? _("unable to update local ref") : NULL,
642 remote, pretty_ref, summary_width);
643 return r;
644 }
645
646 current = lookup_commit_reference_gently(ref->old_oid.hash, 1);
647 updated = lookup_commit_reference_gently(ref->new_oid.hash, 1);
648 if (!current || !updated) {
649 const char *msg;
650 const char *what;
651 int r;
652 /*
653 * Nicely describe the new ref we're fetching.
654 * Base this on the remote's ref name, as it's
655 * more likely to follow a standard layout.
656 */
657 const char *name = remote_ref ? remote_ref->name : "";
658 if (starts_with(name, "refs/tags/")) {
659 msg = "storing tag";
660 what = _("[new tag]");
661 } else if (starts_with(name, "refs/heads/")) {
662 msg = "storing head";
663 what = _("[new branch]");
664 } else {
665 msg = "storing ref";
666 what = _("[new ref]");
667 }
668
669 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
670 (recurse_submodules != RECURSE_SUBMODULES_ON))
671 check_for_new_submodule_commits(&ref->new_oid);
672 r = s_update_ref(msg, ref, 0);
673 format_display(display, r ? '!' : '*', what,
674 r ? _("unable to update local ref") : NULL,
675 remote, pretty_ref, summary_width);
676 return r;
677 }
678
679 if (in_merge_bases(current, updated)) {
680 struct strbuf quickref = STRBUF_INIT;
681 int r;
682 strbuf_add_unique_abbrev(&quickref, current->object.oid.hash, DEFAULT_ABBREV);
683 strbuf_addstr(&quickref, "..");
684 strbuf_add_unique_abbrev(&quickref, ref->new_oid.hash, DEFAULT_ABBREV);
685 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
686 (recurse_submodules != RECURSE_SUBMODULES_ON))
687 check_for_new_submodule_commits(&ref->new_oid);
688 r = s_update_ref("fast-forward", ref, 1);
689 format_display(display, r ? '!' : ' ', quickref.buf,
690 r ? _("unable to update local ref") : NULL,
691 remote, pretty_ref, summary_width);
692 strbuf_release(&quickref);
693 return r;
694 } else if (force || ref->force) {
695 struct strbuf quickref = STRBUF_INIT;
696 int r;
697 strbuf_add_unique_abbrev(&quickref, current->object.oid.hash, DEFAULT_ABBREV);
698 strbuf_addstr(&quickref, "...");
699 strbuf_add_unique_abbrev(&quickref, ref->new_oid.hash, DEFAULT_ABBREV);
700 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
701 (recurse_submodules != RECURSE_SUBMODULES_ON))
702 check_for_new_submodule_commits(&ref->new_oid);
703 r = s_update_ref("forced-update", ref, 1);
704 format_display(display, r ? '!' : '+', quickref.buf,
705 r ? _("unable to update local ref") : _("forced update"),
706 remote, pretty_ref, summary_width);
707 strbuf_release(&quickref);
708 return r;
709 } else {
710 format_display(display, '!', _("[rejected]"), _("non-fast-forward"),
711 remote, pretty_ref, summary_width);
712 return 1;
713 }
714}
715
716static int iterate_ref_map(void *cb_data, unsigned char sha1[20])
717{
718 struct ref **rm = cb_data;
719 struct ref *ref = *rm;
720
721 while (ref && ref->status == REF_STATUS_REJECT_SHALLOW)
722 ref = ref->next;
723 if (!ref)
724 return -1; /* end of the list */
725 *rm = ref->next;
726 hashcpy(sha1, ref->old_oid.hash);
727 return 0;
728}
729
730static int store_updated_refs(const char *raw_url, const char *remote_name,
731 struct ref *ref_map)
732{
733 FILE *fp;
734 struct commit *commit;
735 int url_len, i, rc = 0;
736 struct strbuf note = STRBUF_INIT;
737 const char *what, *kind;
738 struct ref *rm;
739 char *url;
740 const char *filename = dry_run ? "/dev/null" : git_path_fetch_head();
741 int want_status;
742 int summary_width = transport_summary_width(ref_map);
743
744 fp = fopen(filename, "a");
745 if (!fp)
746 return error_errno(_("cannot open %s"), filename);
747
748 if (raw_url)
749 url = transport_anonymize_url(raw_url);
750 else
751 url = xstrdup("foreign");
752
753 rm = ref_map;
754 if (check_connected(iterate_ref_map, &rm, NULL)) {
755 rc = error(_("%s did not send all necessary objects\n"), url);
756 goto abort;
757 }
758
759 prepare_format_display(ref_map);
760
761 /*
762 * We do a pass for each fetch_head_status type in their enum order, so
763 * merged entries are written before not-for-merge. That lets readers
764 * use FETCH_HEAD as a refname to refer to the ref to be merged.
765 */
766 for (want_status = FETCH_HEAD_MERGE;
767 want_status <= FETCH_HEAD_IGNORE;
768 want_status++) {
769 for (rm = ref_map; rm; rm = rm->next) {
770 struct ref *ref = NULL;
771 const char *merge_status_marker = "";
772
773 if (rm->status == REF_STATUS_REJECT_SHALLOW) {
774 if (want_status == FETCH_HEAD_MERGE)
775 warning(_("reject %s because shallow roots are not allowed to be updated"),
776 rm->peer_ref ? rm->peer_ref->name : rm->name);
777 continue;
778 }
779
780 commit = lookup_commit_reference_gently(rm->old_oid.hash, 1);
781 if (!commit)
782 rm->fetch_head_status = FETCH_HEAD_NOT_FOR_MERGE;
783
784 if (rm->fetch_head_status != want_status)
785 continue;
786
787 if (rm->peer_ref) {
788 ref = alloc_ref(rm->peer_ref->name);
789 oidcpy(&ref->old_oid, &rm->peer_ref->old_oid);
790 oidcpy(&ref->new_oid, &rm->old_oid);
791 ref->force = rm->peer_ref->force;
792 }
793
794
795 if (!strcmp(rm->name, "HEAD")) {
796 kind = "";
797 what = "";
798 }
799 else if (starts_with(rm->name, "refs/heads/")) {
800 kind = "branch";
801 what = rm->name + 11;
802 }
803 else if (starts_with(rm->name, "refs/tags/")) {
804 kind = "tag";
805 what = rm->name + 10;
806 }
807 else if (starts_with(rm->name, "refs/remotes/")) {
808 kind = "remote-tracking branch";
809 what = rm->name + 13;
810 }
811 else {
812 kind = "";
813 what = rm->name;
814 }
815
816 url_len = strlen(url);
817 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
818 ;
819 url_len = i + 1;
820 if (4 < i && !strncmp(".git", url + i - 3, 4))
821 url_len = i - 3;
822
823 strbuf_reset(¬e);
824 if (*what) {
825 if (*kind)
826 strbuf_addf(¬e, "%s ", kind);
827 strbuf_addf(¬e, "'%s' of ", what);
828 }
829 switch (rm->fetch_head_status) {
830 case FETCH_HEAD_NOT_FOR_MERGE:
831 merge_status_marker = "not-for-merge";
832 /* fall-through */
833 case FETCH_HEAD_MERGE:
834 fprintf(fp, "%s\t%s\t%s",
835 oid_to_hex(&rm->old_oid),
836 merge_status_marker,
837 note.buf);
838 for (i = 0; i < url_len; ++i)
839 if ('\n' == url[i])
840 fputs("\\n", fp);
841 else
842 fputc(url[i], fp);
843 fputc('\n', fp);
844 break;
845 default:
846 /* do not write anything to FETCH_HEAD */
847 break;
848 }
849
850 strbuf_reset(¬e);
851 if (ref) {
852 rc |= update_local_ref(ref, what, rm, ¬e,
853 summary_width);
854 free(ref);
855 } else
856 format_display(¬e, '*',
857 *kind ? kind : "branch", NULL,
858 *what ? what : "HEAD",
859 "FETCH_HEAD", summary_width);
860 if (note.len) {
861 if (verbosity >= 0 && !shown_url) {
862 fprintf(stderr, _("From %.*s\n"),
863 url_len, url);
864 shown_url = 1;
865 }
866 if (verbosity >= 0)
867 fprintf(stderr, " %s\n", note.buf);
868 }
869 }
870 }
871
872 if (rc & STORE_REF_ERROR_DF_CONFLICT)
873 error(_("some local refs could not be updated; try running\n"
874 " 'git remote prune %s' to remove any old, conflicting "
875 "branches"), remote_name);
876
877 abort:
878 strbuf_release(¬e);
879 free(url);
880 fclose(fp);
881 return rc;
882}
883
884/*
885 * We would want to bypass the object transfer altogether if
886 * everything we are going to fetch already exists and is connected
887 * locally.
888 */
889static int quickfetch(struct ref *ref_map)
890{
891 struct ref *rm = ref_map;
892 struct check_connected_options opt = CHECK_CONNECTED_INIT;
893
894 /*
895 * If we are deepening a shallow clone we already have these
896 * objects reachable. Running rev-list here will return with
897 * a good (0) exit status and we'll bypass the fetch that we
898 * really need to perform. Claiming failure now will ensure
899 * we perform the network exchange to deepen our history.
900 */
901 if (deepen)
902 return -1;
903 opt.quiet = 1;
904 return check_connected(iterate_ref_map, &rm, &opt);
905}
906
907static int fetch_refs(struct transport *transport, struct ref *ref_map)
908{
909 int ret = quickfetch(ref_map);
910 if (ret)
911 ret = transport_fetch_refs(transport, ref_map);
912 if (!ret)
913 ret |= store_updated_refs(transport->url,
914 transport->remote->name,
915 ref_map);
916 transport_unlock_pack(transport);
917 return ret;
918}
919
920static int prune_refs(struct refspec *refs, int ref_count, struct ref *ref_map,
921 const char *raw_url)
922{
923 int url_len, i, result = 0;
924 struct ref *ref, *stale_refs = get_stale_heads(refs, ref_count, ref_map);
925 char *url;
926 int summary_width = transport_summary_width(stale_refs);
927 const char *dangling_msg = dry_run
928 ? _(" (%s will become dangling)")
929 : _(" (%s has become dangling)");
930
931 if (raw_url)
932 url = transport_anonymize_url(raw_url);
933 else
934 url = xstrdup("foreign");
935
936 url_len = strlen(url);
937 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
938 ;
939
940 url_len = i + 1;
941 if (4 < i && !strncmp(".git", url + i - 3, 4))
942 url_len = i - 3;
943
944 if (!dry_run) {
945 struct string_list refnames = STRING_LIST_INIT_NODUP;
946
947 for (ref = stale_refs; ref; ref = ref->next)
948 string_list_append(&refnames, ref->name);
949
950 result = delete_refs(&refnames, 0);
951 string_list_clear(&refnames, 0);
952 }
953
954 if (verbosity >= 0) {
955 for (ref = stale_refs; ref; ref = ref->next) {
956 struct strbuf sb = STRBUF_INIT;
957 if (!shown_url) {
958 fprintf(stderr, _("From %.*s\n"), url_len, url);
959 shown_url = 1;
960 }
961 format_display(&sb, '-', _("[deleted]"), NULL,
962 _("(none)"), prettify_refname(ref->name),
963 summary_width);
964 fprintf(stderr, " %s\n",sb.buf);
965 strbuf_release(&sb);
966 warn_dangling_symref(stderr, dangling_msg, ref->name);
967 }
968 }
969
970 free(url);
971 free_refs(stale_refs);
972 return result;
973}
974
975static void check_not_current_branch(struct ref *ref_map)
976{
977 struct branch *current_branch = branch_get(NULL);
978
979 if (is_bare_repository() || !current_branch)
980 return;
981
982 for (; ref_map; ref_map = ref_map->next)
983 if (ref_map->peer_ref && !strcmp(current_branch->refname,
984 ref_map->peer_ref->name))
985 die(_("Refusing to fetch into current branch %s "
986 "of non-bare repository"), current_branch->refname);
987}
988
989static int truncate_fetch_head(void)
990{
991 const char *filename = git_path_fetch_head();
992 FILE *fp = fopen_for_writing(filename);
993
994 if (!fp)
995 return error_errno(_("cannot open %s"), filename);
996 fclose(fp);
997 return 0;
998}
999
1000static void set_option(struct transport *transport, const char *name, const char *value)
1001{
1002 int r = transport_set_option(transport, name, value);
1003 if (r < 0)
1004 die(_("Option \"%s\" value \"%s\" is not valid for %s"),
1005 name, value, transport->url);
1006 if (r > 0)
1007 warning(_("Option \"%s\" is ignored for %s\n"),
1008 name, transport->url);
1009}
1010
1011static struct transport *prepare_transport(struct remote *remote, int deepen)
1012{
1013 struct transport *transport;
1014 transport = transport_get(remote, NULL);
1015 transport_set_verbosity(transport, verbosity, progress);
1016 transport->family = family;
1017 if (upload_pack)
1018 set_option(transport, TRANS_OPT_UPLOADPACK, upload_pack);
1019 if (keep)
1020 set_option(transport, TRANS_OPT_KEEP, "yes");
1021 if (depth)
1022 set_option(transport, TRANS_OPT_DEPTH, depth);
1023 if (deepen && deepen_since)
1024 set_option(transport, TRANS_OPT_DEEPEN_SINCE, deepen_since);
1025 if (deepen && deepen_not.nr)
1026 set_option(transport, TRANS_OPT_DEEPEN_NOT,
1027 (const char *)&deepen_not);
1028 if (deepen_relative)
1029 set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, "yes");
1030 if (update_shallow)
1031 set_option(transport, TRANS_OPT_UPDATE_SHALLOW, "yes");
1032 return transport;
1033}
1034
1035static void backfill_tags(struct transport *transport, struct ref *ref_map)
1036{
1037 int cannot_reuse;
1038
1039 /*
1040 * Once we have set TRANS_OPT_DEEPEN_SINCE, we can't unset it
1041 * when remote helper is used (setting it to an empty string
1042 * is not unsetting). We could extend the remote helper
1043 * protocol for that, but for now, just force a new connection
1044 * without deepen-since. Similar story for deepen-not.
1045 */
1046 cannot_reuse = transport->cannot_reuse ||
1047 deepen_since || deepen_not.nr;
1048 if (cannot_reuse) {
1049 gsecondary = prepare_transport(transport->remote, 0);
1050 transport = gsecondary;
1051 }
1052
1053 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);
1054 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
1055 transport_set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, NULL);
1056 fetch_refs(transport, ref_map);
1057
1058 if (gsecondary) {
1059 transport_disconnect(gsecondary);
1060 gsecondary = NULL;
1061 }
1062}
1063
1064static int do_fetch(struct transport *transport,
1065 struct refspec *refs, int ref_count)
1066{
1067 struct string_list existing_refs = STRING_LIST_INIT_DUP;
1068 struct ref *ref_map;
1069 struct ref *rm;
1070 int autotags = (transport->remote->fetch_tags == 1);
1071 int retcode = 0;
1072
1073 for_each_ref(add_existing, &existing_refs);
1074
1075 if (tags == TAGS_DEFAULT) {
1076 if (transport->remote->fetch_tags == 2)
1077 tags = TAGS_SET;
1078 if (transport->remote->fetch_tags == -1)
1079 tags = TAGS_UNSET;
1080 }
1081
1082 if (!transport->get_refs_list || !transport->fetch)
1083 die(_("Don't know how to fetch from %s"), transport->url);
1084
1085 /* if not appending, truncate FETCH_HEAD */
1086 if (!append && !dry_run) {
1087 retcode = truncate_fetch_head();
1088 if (retcode)
1089 goto cleanup;
1090 }
1091
1092 ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
1093 if (!update_head_ok)
1094 check_not_current_branch(ref_map);
1095
1096 for (rm = ref_map; rm; rm = rm->next) {
1097 if (rm->peer_ref) {
1098 struct string_list_item *peer_item =
1099 string_list_lookup(&existing_refs,
1100 rm->peer_ref->name);
1101 if (peer_item) {
1102 struct object_id *old_oid = peer_item->util;
1103 oidcpy(&rm->peer_ref->old_oid, old_oid);
1104 }
1105 }
1106 }
1107
1108 if (tags == TAGS_DEFAULT && autotags)
1109 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
1110 if (prune) {
1111 /*
1112 * We only prune based on refspecs specified
1113 * explicitly (via command line or configuration); we
1114 * don't care whether --tags was specified.
1115 */
1116 if (ref_count) {
1117 prune_refs(refs, ref_count, ref_map, transport->url);
1118 } else {
1119 prune_refs(transport->remote->fetch,
1120 transport->remote->fetch_refspec_nr,
1121 ref_map,
1122 transport->url);
1123 }
1124 }
1125 if (fetch_refs(transport, ref_map)) {
1126 free_refs(ref_map);
1127 retcode = 1;
1128 goto cleanup;
1129 }
1130 free_refs(ref_map);
1131
1132 /* if neither --no-tags nor --tags was specified, do automated tag
1133 * following ... */
1134 if (tags == TAGS_DEFAULT && autotags) {
1135 struct ref **tail = &ref_map;
1136 ref_map = NULL;
1137 find_non_local_tags(transport, &ref_map, &tail);
1138 if (ref_map)
1139 backfill_tags(transport, ref_map);
1140 free_refs(ref_map);
1141 }
1142
1143 cleanup:
1144 string_list_clear(&existing_refs, 1);
1145 return retcode;
1146}
1147
1148static int get_one_remote_for_fetch(struct remote *remote, void *priv)
1149{
1150 struct string_list *list = priv;
1151 if (!remote->skip_default_update)
1152 string_list_append(list, remote->name);
1153 return 0;
1154}
1155
1156struct remote_group_data {
1157 const char *name;
1158 struct string_list *list;
1159};
1160
1161static int get_remote_group(const char *key, const char *value, void *priv)
1162{
1163 struct remote_group_data *g = priv;
1164
1165 if (skip_prefix(key, "remotes.", &key) && !strcmp(key, g->name)) {
1166 /* split list by white space */
1167 while (*value) {
1168 size_t wordlen = strcspn(value, " \t\n");
1169
1170 if (wordlen >= 1)
1171 string_list_append_nodup(g->list,
1172 xstrndup(value, wordlen));
1173 value += wordlen + (value[wordlen] != '\0');
1174 }
1175 }
1176
1177 return 0;
1178}
1179
1180static int add_remote_or_group(const char *name, struct string_list *list)
1181{
1182 int prev_nr = list->nr;
1183 struct remote_group_data g;
1184 g.name = name; g.list = list;
1185
1186 git_config(get_remote_group, &g);
1187 if (list->nr == prev_nr) {
1188 struct remote *remote = remote_get(name);
1189 if (!remote_is_configured(remote, 0))
1190 return 0;
1191 string_list_append(list, remote->name);
1192 }
1193 return 1;
1194}
1195
1196static void add_options_to_argv(struct argv_array *argv)
1197{
1198 if (dry_run)
1199 argv_array_push(argv, "--dry-run");
1200 if (prune != -1)
1201 argv_array_push(argv, prune ? "--prune" : "--no-prune");
1202 if (update_head_ok)
1203 argv_array_push(argv, "--update-head-ok");
1204 if (force)
1205 argv_array_push(argv, "--force");
1206 if (keep)
1207 argv_array_push(argv, "--keep");
1208 if (recurse_submodules == RECURSE_SUBMODULES_ON)
1209 argv_array_push(argv, "--recurse-submodules");
1210 else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
1211 argv_array_push(argv, "--recurse-submodules=on-demand");
1212 if (tags == TAGS_SET)
1213 argv_array_push(argv, "--tags");
1214 else if (tags == TAGS_UNSET)
1215 argv_array_push(argv, "--no-tags");
1216 if (verbosity >= 2)
1217 argv_array_push(argv, "-v");
1218 if (verbosity >= 1)
1219 argv_array_push(argv, "-v");
1220 else if (verbosity < 0)
1221 argv_array_push(argv, "-q");
1222
1223}
1224
1225static int fetch_multiple(struct string_list *list)
1226{
1227 int i, result = 0;
1228 struct argv_array argv = ARGV_ARRAY_INIT;
1229
1230 if (!append && !dry_run) {
1231 int errcode = truncate_fetch_head();
1232 if (errcode)
1233 return errcode;
1234 }
1235
1236 argv_array_pushl(&argv, "fetch", "--append", NULL);
1237 add_options_to_argv(&argv);
1238
1239 for (i = 0; i < list->nr; i++) {
1240 const char *name = list->items[i].string;
1241 argv_array_push(&argv, name);
1242 if (verbosity >= 0)
1243 printf(_("Fetching %s\n"), name);
1244 if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) {
1245 error(_("Could not fetch %s"), name);
1246 result = 1;
1247 }
1248 argv_array_pop(&argv);
1249 }
1250
1251 argv_array_clear(&argv);
1252 return result;
1253}
1254
1255static int fetch_one(struct remote *remote, int argc, const char **argv)
1256{
1257 static const char **refs = NULL;
1258 struct refspec *refspec;
1259 int ref_nr = 0;
1260 int exit_code;
1261
1262 if (!remote)
1263 die(_("No remote repository specified. Please, specify either a URL or a\n"
1264 "remote name from which new revisions should be fetched."));
1265
1266 gtransport = prepare_transport(remote, 1);
1267
1268 if (prune < 0) {
1269 /* no command line request */
1270 if (0 <= gtransport->remote->prune)
1271 prune = gtransport->remote->prune;
1272 else if (0 <= fetch_prune_config)
1273 prune = fetch_prune_config;
1274 else
1275 prune = PRUNE_BY_DEFAULT;
1276 }
1277
1278 if (argc > 0) {
1279 int j = 0;
1280 int i;
1281 refs = xcalloc(st_add(argc, 1), sizeof(const char *));
1282 for (i = 0; i < argc; i++) {
1283 if (!strcmp(argv[i], "tag")) {
1284 i++;
1285 if (i >= argc)
1286 die(_("You need to specify a tag name."));
1287 refs[j++] = xstrfmt("refs/tags/%s:refs/tags/%s",
1288 argv[i], argv[i]);
1289 } else
1290 refs[j++] = argv[i];
1291 }
1292 refs[j] = NULL;
1293 ref_nr = j;
1294 }
1295
1296 sigchain_push_common(unlock_pack_on_signal);
1297 atexit(unlock_pack);
1298 refspec = parse_fetch_refspec(ref_nr, refs);
1299 exit_code = do_fetch(gtransport, refspec, ref_nr);
1300 free_refspec(ref_nr, refspec);
1301 transport_disconnect(gtransport);
1302 gtransport = NULL;
1303 return exit_code;
1304}
1305
1306int cmd_fetch(int argc, const char **argv, const char *prefix)
1307{
1308 int i;
1309 struct string_list list = STRING_LIST_INIT_DUP;
1310 struct remote *remote;
1311 int result = 0;
1312 struct argv_array argv_gc_auto = ARGV_ARRAY_INIT;
1313
1314 packet_trace_identity("fetch");
1315
1316 /* Record the command line for the reflog */
1317 strbuf_addstr(&default_rla, "fetch");
1318 for (i = 1; i < argc; i++)
1319 strbuf_addf(&default_rla, " %s", argv[i]);
1320
1321 git_config(git_fetch_config, NULL);
1322
1323 argc = parse_options(argc, argv, prefix,
1324 builtin_fetch_options, builtin_fetch_usage, 0);
1325
1326 if (deepen_relative) {
1327 if (deepen_relative < 0)
1328 die(_("Negative depth in --deepen is not supported"));
1329 if (depth)
1330 die(_("--deepen and --depth are mutually exclusive"));
1331 depth = xstrfmt("%d", deepen_relative);
1332 }
1333 if (unshallow) {
1334 if (depth)
1335 die(_("--depth and --unshallow cannot be used together"));
1336 else if (!is_repository_shallow())
1337 die(_("--unshallow on a complete repository does not make sense"));
1338 else
1339 depth = xstrfmt("%d", INFINITE_DEPTH);
1340 }
1341
1342 /* no need to be strict, transport_set_option() will validate it again */
1343 if (depth && atoi(depth) < 1)
1344 die(_("depth %s is not a positive number"), depth);
1345 if (depth || deepen_since || deepen_not.nr)
1346 deepen = 1;
1347
1348 if (recurse_submodules != RECURSE_SUBMODULES_OFF) {
1349 if (recurse_submodules_default) {
1350 int arg = parse_fetch_recurse_submodules_arg("--recurse-submodules-default", recurse_submodules_default);
1351 set_config_fetch_recurse_submodules(arg);
1352 }
1353 gitmodules_config();
1354 git_config(submodule_config, NULL);
1355 }
1356
1357 if (all) {
1358 if (argc == 1)
1359 die(_("fetch --all does not take a repository argument"));
1360 else if (argc > 1)
1361 die(_("fetch --all does not make sense with refspecs"));
1362 (void) for_each_remote(get_one_remote_for_fetch, &list);
1363 result = fetch_multiple(&list);
1364 } else if (argc == 0) {
1365 /* No arguments -- use default remote */
1366 remote = remote_get(NULL);
1367 result = fetch_one(remote, argc, argv);
1368 } else if (multiple) {
1369 /* All arguments are assumed to be remotes or groups */
1370 for (i = 0; i < argc; i++)
1371 if (!add_remote_or_group(argv[i], &list))
1372 die(_("No such remote or remote group: %s"), argv[i]);
1373 result = fetch_multiple(&list);
1374 } else {
1375 /* Single remote or group */
1376 (void) add_remote_or_group(argv[0], &list);
1377 if (list.nr > 1) {
1378 /* More than one remote */
1379 if (argc > 1)
1380 die(_("Fetching a group and specifying refspecs does not make sense"));
1381 result = fetch_multiple(&list);
1382 } else {
1383 /* Zero or one remotes */
1384 remote = remote_get(argv[0]);
1385 result = fetch_one(remote, argc-1, argv+1);
1386 }
1387 }
1388
1389 if (!result && (recurse_submodules != RECURSE_SUBMODULES_OFF)) {
1390 struct argv_array options = ARGV_ARRAY_INIT;
1391
1392 add_options_to_argv(&options);
1393 result = fetch_populated_submodules(&options,
1394 submodule_prefix,
1395 recurse_submodules,
1396 verbosity < 0,
1397 max_children);
1398 argv_array_clear(&options);
1399 }
1400
1401 string_list_clear(&list, 0);
1402
1403 close_all_packs();
1404
1405 argv_array_pushl(&argv_gc_auto, "gc", "--auto", NULL);
1406 if (verbosity < 0)
1407 argv_array_push(&argv_gc_auto, "--quiet");
1408 run_command_v_opt(argv_gc_auto.argv, RUN_GIT_CMD);
1409 argv_array_clear(&argv_gc_auto);
1410
1411 return result;
1412}