fetch: let --jobs=<n> parallelize --multiple, too
[git/raj.git] / builtin / fetch.c
blobe2d374724d754d4f9cf543d3d735b39fccbe180c
1 /*
2 * "git fetch"
3 */
4 #include "cache.h"
5 #include "config.h"
6 #include "repository.h"
7 #include "refs.h"
8 #include "refspec.h"
9 #include "object-store.h"
10 #include "commit.h"
11 #include "builtin.h"
12 #include "string-list.h"
13 #include "remote.h"
14 #include "transport.h"
15 #include "run-command.h"
16 #include "parse-options.h"
17 #include "sigchain.h"
18 #include "submodule-config.h"
19 #include "submodule.h"
20 #include "connected.h"
21 #include "argv-array.h"
22 #include "utf8.h"
23 #include "packfile.h"
24 #include "list-objects-filter-options.h"
25 #include "commit-reach.h"
27 #define FORCED_UPDATES_DELAY_WARNING_IN_MS (10 * 1000)
29 static const char * const builtin_fetch_usage[] = {
30 N_("git fetch [<options>] [<repository> [<refspec>...]]"),
31 N_("git fetch [<options>] <group>"),
32 N_("git fetch --multiple [<options>] [(<repository> | <group>)...]"),
33 N_("git fetch --all [<options>]"),
34 NULL
37 enum {
38 TAGS_UNSET = 0,
39 TAGS_DEFAULT = 1,
40 TAGS_SET = 2
43 static int fetch_prune_config = -1; /* unspecified */
44 static int fetch_show_forced_updates = 1;
45 static uint64_t forced_updates_ms = 0;
46 static int prune = -1; /* unspecified */
47 #define PRUNE_BY_DEFAULT 0 /* do we prune by default? */
49 static int fetch_prune_tags_config = -1; /* unspecified */
50 static int prune_tags = -1; /* unspecified */
51 #define PRUNE_TAGS_BY_DEFAULT 0 /* do we prune tags by default? */
53 static int all, append, dry_run, force, keep, multiple, update_head_ok, verbosity, deepen_relative;
54 static int progress = -1;
55 static int enable_auto_gc = 1;
56 static int tags = TAGS_DEFAULT, unshallow, update_shallow, deepen;
57 static int max_jobs = -1, submodule_fetch_jobs_config = -1;
58 static int fetch_parallel_config = 1;
59 static enum transport_family family;
60 static const char *depth;
61 static const char *deepen_since;
62 static const char *upload_pack;
63 static struct string_list deepen_not = STRING_LIST_INIT_NODUP;
64 static struct strbuf default_rla = STRBUF_INIT;
65 static struct transport *gtransport;
66 static struct transport *gsecondary;
67 static const char *submodule_prefix = "";
68 static int recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
69 static int recurse_submodules_default = RECURSE_SUBMODULES_ON_DEMAND;
70 static int shown_url = 0;
71 static struct refspec refmap = REFSPEC_INIT_FETCH;
72 static struct list_objects_filter_options filter_options;
73 static struct string_list server_options = STRING_LIST_INIT_DUP;
74 static struct string_list negotiation_tip = STRING_LIST_INIT_NODUP;
76 static int git_fetch_config(const char *k, const char *v, void *cb)
78 if (!strcmp(k, "fetch.prune")) {
79 fetch_prune_config = git_config_bool(k, v);
80 return 0;
83 if (!strcmp(k, "fetch.prunetags")) {
84 fetch_prune_tags_config = git_config_bool(k, v);
85 return 0;
88 if (!strcmp(k, "fetch.showforcedupdates")) {
89 fetch_show_forced_updates = git_config_bool(k, v);
90 return 0;
93 if (!strcmp(k, "submodule.recurse")) {
94 int r = git_config_bool(k, v) ?
95 RECURSE_SUBMODULES_ON : RECURSE_SUBMODULES_OFF;
96 recurse_submodules = r;
99 if (!strcmp(k, "submodule.fetchjobs")) {
100 submodule_fetch_jobs_config = parse_submodule_fetchjobs(k, v);
101 return 0;
102 } else if (!strcmp(k, "fetch.recursesubmodules")) {
103 recurse_submodules = parse_fetch_recurse_submodules_arg(k, v);
104 return 0;
107 if (!strcmp(k, "fetch.parallel")) {
108 fetch_parallel_config = git_config_int(k, v);
109 if (fetch_parallel_config < 0)
110 die(_("fetch.parallel cannot be negative"));
111 return 0;
114 return git_default_config(k, v, cb);
117 static int parse_refmap_arg(const struct option *opt, const char *arg, int unset)
119 BUG_ON_OPT_NEG(unset);
122 * "git fetch --refmap='' origin foo"
123 * can be used to tell the command not to store anywhere
125 refspec_append(&refmap, arg);
127 return 0;
130 static struct option builtin_fetch_options[] = {
131 OPT__VERBOSITY(&verbosity),
132 OPT_BOOL(0, "all", &all,
133 N_("fetch from all remotes")),
134 OPT_BOOL('a', "append", &append,
135 N_("append to .git/FETCH_HEAD instead of overwriting")),
136 OPT_STRING(0, "upload-pack", &upload_pack, N_("path"),
137 N_("path to upload pack on remote end")),
138 OPT__FORCE(&force, N_("force overwrite of local reference"), 0),
139 OPT_BOOL('m', "multiple", &multiple,
140 N_("fetch from multiple remotes")),
141 OPT_SET_INT('t', "tags", &tags,
142 N_("fetch all tags and associated objects"), TAGS_SET),
143 OPT_SET_INT('n', NULL, &tags,
144 N_("do not fetch all tags (--no-tags)"), TAGS_UNSET),
145 OPT_INTEGER('j', "jobs", &max_jobs,
146 N_("number of submodules fetched in parallel")),
147 OPT_BOOL('p', "prune", &prune,
148 N_("prune remote-tracking branches no longer on remote")),
149 OPT_BOOL('P', "prune-tags", &prune_tags,
150 N_("prune local tags no longer on remote and clobber changed tags")),
151 { OPTION_CALLBACK, 0, "recurse-submodules", &recurse_submodules, N_("on-demand"),
152 N_("control recursive fetching of submodules"),
153 PARSE_OPT_OPTARG, option_fetch_parse_recurse_submodules },
154 OPT_BOOL(0, "dry-run", &dry_run,
155 N_("dry run")),
156 OPT_BOOL('k', "keep", &keep, N_("keep downloaded pack")),
157 OPT_BOOL('u', "update-head-ok", &update_head_ok,
158 N_("allow updating of HEAD ref")),
159 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
160 OPT_STRING(0, "depth", &depth, N_("depth"),
161 N_("deepen history of shallow clone")),
162 OPT_STRING(0, "shallow-since", &deepen_since, N_("time"),
163 N_("deepen history of shallow repository based on time")),
164 OPT_STRING_LIST(0, "shallow-exclude", &deepen_not, N_("revision"),
165 N_("deepen history of shallow clone, excluding rev")),
166 OPT_INTEGER(0, "deepen", &deepen_relative,
167 N_("deepen history of shallow clone")),
168 OPT_SET_INT_F(0, "unshallow", &unshallow,
169 N_("convert to a complete repository"),
170 1, PARSE_OPT_NONEG),
171 { OPTION_STRING, 0, "submodule-prefix", &submodule_prefix, N_("dir"),
172 N_("prepend this to submodule path output"), PARSE_OPT_HIDDEN },
173 { OPTION_CALLBACK, 0, "recurse-submodules-default",
174 &recurse_submodules_default, N_("on-demand"),
175 N_("default for recursive fetching of submodules "
176 "(lower priority than config files)"),
177 PARSE_OPT_HIDDEN, option_fetch_parse_recurse_submodules },
178 OPT_BOOL(0, "update-shallow", &update_shallow,
179 N_("accept refs that update .git/shallow")),
180 { OPTION_CALLBACK, 0, "refmap", NULL, N_("refmap"),
181 N_("specify fetch refmap"), PARSE_OPT_NONEG, parse_refmap_arg },
182 OPT_STRING_LIST('o', "server-option", &server_options, N_("server-specific"), N_("option to transmit")),
183 OPT_SET_INT('4', "ipv4", &family, N_("use IPv4 addresses only"),
184 TRANSPORT_FAMILY_IPV4),
185 OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"),
186 TRANSPORT_FAMILY_IPV6),
187 OPT_STRING_LIST(0, "negotiation-tip", &negotiation_tip, N_("revision"),
188 N_("report that we have only objects reachable from this object")),
189 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
190 OPT_BOOL(0, "auto-gc", &enable_auto_gc,
191 N_("run 'gc --auto' after fetching")),
192 OPT_BOOL(0, "show-forced-updates", &fetch_show_forced_updates,
193 N_("check for forced-updates on all updated branches")),
194 OPT_END()
197 static void unlock_pack(void)
199 if (gtransport)
200 transport_unlock_pack(gtransport);
201 if (gsecondary)
202 transport_unlock_pack(gsecondary);
205 static void unlock_pack_on_signal(int signo)
207 unlock_pack();
208 sigchain_pop(signo);
209 raise(signo);
212 static void add_merge_config(struct ref **head,
213 const struct ref *remote_refs,
214 struct branch *branch,
215 struct ref ***tail)
217 int i;
219 for (i = 0; i < branch->merge_nr; i++) {
220 struct ref *rm, **old_tail = *tail;
221 struct refspec_item refspec;
223 for (rm = *head; rm; rm = rm->next) {
224 if (branch_merge_matches(branch, i, rm->name)) {
225 rm->fetch_head_status = FETCH_HEAD_MERGE;
226 break;
229 if (rm)
230 continue;
233 * Not fetched to a remote-tracking branch? We need to fetch
234 * it anyway to allow this branch's "branch.$name.merge"
235 * to be honored by 'git pull', but we do not have to
236 * fail if branch.$name.merge is misconfigured to point
237 * at a nonexisting branch. If we were indeed called by
238 * 'git pull', it will notice the misconfiguration because
239 * there is no entry in the resulting FETCH_HEAD marked
240 * for merging.
242 memset(&refspec, 0, sizeof(refspec));
243 refspec.src = branch->merge[i]->src;
244 get_fetch_map(remote_refs, &refspec, tail, 1);
245 for (rm = *old_tail; rm; rm = rm->next)
246 rm->fetch_head_status = FETCH_HEAD_MERGE;
250 static int will_fetch(struct ref **head, const unsigned char *sha1)
252 struct ref *rm = *head;
253 while (rm) {
254 if (hasheq(rm->old_oid.hash, sha1))
255 return 1;
256 rm = rm->next;
258 return 0;
261 struct refname_hash_entry {
262 struct hashmap_entry ent; /* must be the first member */
263 struct object_id oid;
264 int ignore;
265 char refname[FLEX_ARRAY];
268 static int refname_hash_entry_cmp(const void *hashmap_cmp_fn_data,
269 const void *e1_,
270 const void *e2_,
271 const void *keydata)
273 const struct refname_hash_entry *e1 = e1_;
274 const struct refname_hash_entry *e2 = e2_;
276 return strcmp(e1->refname, keydata ? keydata : e2->refname);
279 static struct refname_hash_entry *refname_hash_add(struct hashmap *map,
280 const char *refname,
281 const struct object_id *oid)
283 struct refname_hash_entry *ent;
284 size_t len = strlen(refname);
286 FLEX_ALLOC_MEM(ent, refname, refname, len);
287 hashmap_entry_init(ent, strhash(refname));
288 oidcpy(&ent->oid, oid);
289 hashmap_add(map, ent);
290 return ent;
293 static int add_one_refname(const char *refname,
294 const struct object_id *oid,
295 int flag, void *cbdata)
297 struct hashmap *refname_map = cbdata;
299 (void) refname_hash_add(refname_map, refname, oid);
300 return 0;
303 static void refname_hash_init(struct hashmap *map)
305 hashmap_init(map, refname_hash_entry_cmp, NULL, 0);
308 static int refname_hash_exists(struct hashmap *map, const char *refname)
310 return !!hashmap_get_from_hash(map, strhash(refname), refname);
313 static void clear_item(struct refname_hash_entry *item)
315 item->ignore = 1;
318 static void find_non_local_tags(const struct ref *refs,
319 struct ref **head,
320 struct ref ***tail)
322 struct hashmap existing_refs;
323 struct hashmap remote_refs;
324 struct string_list remote_refs_list = STRING_LIST_INIT_NODUP;
325 struct string_list_item *remote_ref_item;
326 const struct ref *ref;
327 struct refname_hash_entry *item = NULL;
329 refname_hash_init(&existing_refs);
330 refname_hash_init(&remote_refs);
332 for_each_ref(add_one_refname, &existing_refs);
333 for (ref = refs; ref; ref = ref->next) {
334 if (!starts_with(ref->name, "refs/tags/"))
335 continue;
338 * The peeled ref always follows the matching base
339 * ref, so if we see a peeled ref that we don't want
340 * to fetch then we can mark the ref entry in the list
341 * as one to ignore by setting util to NULL.
343 if (ends_with(ref->name, "^{}")) {
344 if (item &&
345 !has_object_file_with_flags(&ref->old_oid,
346 OBJECT_INFO_QUICK) &&
347 !will_fetch(head, ref->old_oid.hash) &&
348 !has_object_file_with_flags(&item->oid, OBJECT_INFO_QUICK) &&
349 !will_fetch(head, item->oid.hash))
350 clear_item(item);
351 item = NULL;
352 continue;
356 * If item is non-NULL here, then we previously saw a
357 * ref not followed by a peeled reference, so we need
358 * to check if it is a lightweight tag that we want to
359 * fetch.
361 if (item &&
362 !has_object_file_with_flags(&item->oid, OBJECT_INFO_QUICK) &&
363 !will_fetch(head, item->oid.hash))
364 clear_item(item);
366 item = NULL;
368 /* skip duplicates and refs that we already have */
369 if (refname_hash_exists(&remote_refs, ref->name) ||
370 refname_hash_exists(&existing_refs, ref->name))
371 continue;
373 item = refname_hash_add(&remote_refs, ref->name, &ref->old_oid);
374 string_list_insert(&remote_refs_list, ref->name);
376 hashmap_free(&existing_refs, 1);
379 * We may have a final lightweight tag that needs to be
380 * checked to see if it needs fetching.
382 if (item &&
383 !has_object_file_with_flags(&item->oid, OBJECT_INFO_QUICK) &&
384 !will_fetch(head, item->oid.hash))
385 clear_item(item);
388 * For all the tags in the remote_refs_list,
389 * add them to the list of refs to be fetched
391 for_each_string_list_item(remote_ref_item, &remote_refs_list) {
392 const char *refname = remote_ref_item->string;
393 struct ref *rm;
395 item = hashmap_get_from_hash(&remote_refs, strhash(refname), refname);
396 if (!item)
397 BUG("unseen remote ref?");
399 /* Unless we have already decided to ignore this item... */
400 if (item->ignore)
401 continue;
403 rm = alloc_ref(item->refname);
404 rm->peer_ref = alloc_ref(item->refname);
405 oidcpy(&rm->old_oid, &item->oid);
406 **tail = rm;
407 *tail = &rm->next;
409 hashmap_free(&remote_refs, 1);
410 string_list_clear(&remote_refs_list, 0);
413 static struct ref *get_ref_map(struct remote *remote,
414 const struct ref *remote_refs,
415 struct refspec *rs,
416 int tags, int *autotags)
418 int i;
419 struct ref *rm;
420 struct ref *ref_map = NULL;
421 struct ref **tail = &ref_map;
423 /* opportunistically-updated references: */
424 struct ref *orefs = NULL, **oref_tail = &orefs;
426 struct hashmap existing_refs;
428 if (rs->nr) {
429 struct refspec *fetch_refspec;
431 for (i = 0; i < rs->nr; i++) {
432 get_fetch_map(remote_refs, &rs->items[i], &tail, 0);
433 if (rs->items[i].dst && rs->items[i].dst[0])
434 *autotags = 1;
436 /* Merge everything on the command line (but not --tags) */
437 for (rm = ref_map; rm; rm = rm->next)
438 rm->fetch_head_status = FETCH_HEAD_MERGE;
441 * For any refs that we happen to be fetching via
442 * command-line arguments, the destination ref might
443 * have been missing or have been different than the
444 * remote-tracking ref that would be derived from the
445 * configured refspec. In these cases, we want to
446 * take the opportunity to update their configured
447 * remote-tracking reference. However, we do not want
448 * to mention these entries in FETCH_HEAD at all, as
449 * they would simply be duplicates of existing
450 * entries, so we set them FETCH_HEAD_IGNORE below.
452 * We compute these entries now, based only on the
453 * refspecs specified on the command line. But we add
454 * them to the list following the refspecs resulting
455 * from the tags option so that one of the latter,
456 * which has FETCH_HEAD_NOT_FOR_MERGE, is not removed
457 * by ref_remove_duplicates() in favor of one of these
458 * opportunistic entries with FETCH_HEAD_IGNORE.
460 if (refmap.nr)
461 fetch_refspec = &refmap;
462 else
463 fetch_refspec = &remote->fetch;
465 for (i = 0; i < fetch_refspec->nr; i++)
466 get_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1);
467 } else if (refmap.nr) {
468 die("--refmap option is only meaningful with command-line refspec(s).");
469 } else {
470 /* Use the defaults */
471 struct branch *branch = branch_get(NULL);
472 int has_merge = branch_has_merge_config(branch);
473 if (remote &&
474 (remote->fetch.nr ||
475 /* Note: has_merge implies non-NULL branch->remote_name */
476 (has_merge && !strcmp(branch->remote_name, remote->name)))) {
477 for (i = 0; i < remote->fetch.nr; i++) {
478 get_fetch_map(remote_refs, &remote->fetch.items[i], &tail, 0);
479 if (remote->fetch.items[i].dst &&
480 remote->fetch.items[i].dst[0])
481 *autotags = 1;
482 if (!i && !has_merge && ref_map &&
483 !remote->fetch.items[0].pattern)
484 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
487 * if the remote we're fetching from is the same
488 * as given in branch.<name>.remote, we add the
489 * ref given in branch.<name>.merge, too.
491 * Note: has_merge implies non-NULL branch->remote_name
493 if (has_merge &&
494 !strcmp(branch->remote_name, remote->name))
495 add_merge_config(&ref_map, remote_refs, branch, &tail);
496 } else {
497 ref_map = get_remote_ref(remote_refs, "HEAD");
498 if (!ref_map)
499 die(_("Couldn't find remote ref HEAD"));
500 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
501 tail = &ref_map->next;
505 if (tags == TAGS_SET)
506 /* also fetch all tags */
507 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
508 else if (tags == TAGS_DEFAULT && *autotags)
509 find_non_local_tags(remote_refs, &ref_map, &tail);
511 /* Now append any refs to be updated opportunistically: */
512 *tail = orefs;
513 for (rm = orefs; rm; rm = rm->next) {
514 rm->fetch_head_status = FETCH_HEAD_IGNORE;
515 tail = &rm->next;
518 ref_map = ref_remove_duplicates(ref_map);
520 refname_hash_init(&existing_refs);
521 for_each_ref(add_one_refname, &existing_refs);
523 for (rm = ref_map; rm; rm = rm->next) {
524 if (rm->peer_ref) {
525 const char *refname = rm->peer_ref->name;
526 struct refname_hash_entry *peer_item;
528 peer_item = hashmap_get_from_hash(&existing_refs,
529 strhash(refname),
530 refname);
531 if (peer_item) {
532 struct object_id *old_oid = &peer_item->oid;
533 oidcpy(&rm->peer_ref->old_oid, old_oid);
537 hashmap_free(&existing_refs, 1);
539 return ref_map;
542 #define STORE_REF_ERROR_OTHER 1
543 #define STORE_REF_ERROR_DF_CONFLICT 2
545 static int s_update_ref(const char *action,
546 struct ref *ref,
547 int check_old)
549 char *msg;
550 char *rla = getenv("GIT_REFLOG_ACTION");
551 struct ref_transaction *transaction;
552 struct strbuf err = STRBUF_INIT;
553 int ret, df_conflict = 0;
555 if (dry_run)
556 return 0;
557 if (!rla)
558 rla = default_rla.buf;
559 msg = xstrfmt("%s: %s", rla, action);
561 transaction = ref_transaction_begin(&err);
562 if (!transaction ||
563 ref_transaction_update(transaction, ref->name,
564 &ref->new_oid,
565 check_old ? &ref->old_oid : NULL,
566 0, msg, &err))
567 goto fail;
569 ret = ref_transaction_commit(transaction, &err);
570 if (ret) {
571 df_conflict = (ret == TRANSACTION_NAME_CONFLICT);
572 goto fail;
575 ref_transaction_free(transaction);
576 strbuf_release(&err);
577 free(msg);
578 return 0;
579 fail:
580 ref_transaction_free(transaction);
581 error("%s", err.buf);
582 strbuf_release(&err);
583 free(msg);
584 return df_conflict ? STORE_REF_ERROR_DF_CONFLICT
585 : STORE_REF_ERROR_OTHER;
588 static int refcol_width = 10;
589 static int compact_format;
591 static void adjust_refcol_width(const struct ref *ref)
593 int max, rlen, llen, len;
595 /* uptodate lines are only shown on high verbosity level */
596 if (!verbosity && oideq(&ref->peer_ref->old_oid, &ref->old_oid))
597 return;
599 max = term_columns();
600 rlen = utf8_strwidth(prettify_refname(ref->name));
602 llen = utf8_strwidth(prettify_refname(ref->peer_ref->name));
605 * rough estimation to see if the output line is too long and
606 * should not be counted (we can't do precise calculation
607 * anyway because we don't know if the error explanation part
608 * will be printed in update_local_ref)
610 if (compact_format) {
611 llen = 0;
612 max = max * 2 / 3;
614 len = 21 /* flag and summary */ + rlen + 4 /* -> */ + llen;
615 if (len >= max)
616 return;
619 * Not precise calculation for compact mode because '*' can
620 * appear on the left hand side of '->' and shrink the column
621 * back.
623 if (refcol_width < rlen)
624 refcol_width = rlen;
627 static void prepare_format_display(struct ref *ref_map)
629 struct ref *rm;
630 const char *format = "full";
632 git_config_get_string_const("fetch.output", &format);
633 if (!strcasecmp(format, "full"))
634 compact_format = 0;
635 else if (!strcasecmp(format, "compact"))
636 compact_format = 1;
637 else
638 die(_("configuration fetch.output contains invalid value %s"),
639 format);
641 for (rm = ref_map; rm; rm = rm->next) {
642 if (rm->status == REF_STATUS_REJECT_SHALLOW ||
643 !rm->peer_ref ||
644 !strcmp(rm->name, "HEAD"))
645 continue;
647 adjust_refcol_width(rm);
651 static void print_remote_to_local(struct strbuf *display,
652 const char *remote, const char *local)
654 strbuf_addf(display, "%-*s -> %s", refcol_width, remote, local);
657 static int find_and_replace(struct strbuf *haystack,
658 const char *needle,
659 const char *placeholder)
661 const char *p = NULL;
662 int plen, nlen;
664 nlen = strlen(needle);
665 if (ends_with(haystack->buf, needle))
666 p = haystack->buf + haystack->len - nlen;
667 else
668 p = strstr(haystack->buf, needle);
669 if (!p)
670 return 0;
672 if (p > haystack->buf && p[-1] != '/')
673 return 0;
675 plen = strlen(p);
676 if (plen > nlen && p[nlen] != '/')
677 return 0;
679 strbuf_splice(haystack, p - haystack->buf, nlen,
680 placeholder, strlen(placeholder));
681 return 1;
684 static void print_compact(struct strbuf *display,
685 const char *remote, const char *local)
687 struct strbuf r = STRBUF_INIT;
688 struct strbuf l = STRBUF_INIT;
690 if (!strcmp(remote, local)) {
691 strbuf_addf(display, "%-*s -> *", refcol_width, remote);
692 return;
695 strbuf_addstr(&r, remote);
696 strbuf_addstr(&l, local);
698 if (!find_and_replace(&r, local, "*"))
699 find_and_replace(&l, remote, "*");
700 print_remote_to_local(display, r.buf, l.buf);
702 strbuf_release(&r);
703 strbuf_release(&l);
706 static void format_display(struct strbuf *display, char code,
707 const char *summary, const char *error,
708 const char *remote, const char *local,
709 int summary_width)
711 int width = (summary_width + strlen(summary) - gettext_width(summary));
713 strbuf_addf(display, "%c %-*s ", code, width, summary);
714 if (!compact_format)
715 print_remote_to_local(display, remote, local);
716 else
717 print_compact(display, remote, local);
718 if (error)
719 strbuf_addf(display, " (%s)", error);
722 static int update_local_ref(struct ref *ref,
723 const char *remote,
724 const struct ref *remote_ref,
725 struct strbuf *display,
726 int summary_width)
728 struct commit *current = NULL, *updated;
729 enum object_type type;
730 struct branch *current_branch = branch_get(NULL);
731 const char *pretty_ref = prettify_refname(ref->name);
732 int fast_forward = 0;
734 type = oid_object_info(the_repository, &ref->new_oid, NULL);
735 if (type < 0)
736 die(_("object %s not found"), oid_to_hex(&ref->new_oid));
738 if (oideq(&ref->old_oid, &ref->new_oid)) {
739 if (verbosity > 0)
740 format_display(display, '=', _("[up to date]"), NULL,
741 remote, pretty_ref, summary_width);
742 return 0;
745 if (current_branch &&
746 !strcmp(ref->name, current_branch->name) &&
747 !(update_head_ok || is_bare_repository()) &&
748 !is_null_oid(&ref->old_oid)) {
750 * If this is the head, and it's not okay to update
751 * the head, and the old value of the head isn't empty...
753 format_display(display, '!', _("[rejected]"),
754 _("can't fetch in current branch"),
755 remote, pretty_ref, summary_width);
756 return 1;
759 if (!is_null_oid(&ref->old_oid) &&
760 starts_with(ref->name, "refs/tags/")) {
761 if (force || ref->force) {
762 int r;
763 r = s_update_ref("updating tag", ref, 0);
764 format_display(display, r ? '!' : 't', _("[tag update]"),
765 r ? _("unable to update local ref") : NULL,
766 remote, pretty_ref, summary_width);
767 return r;
768 } else {
769 format_display(display, '!', _("[rejected]"), _("would clobber existing tag"),
770 remote, pretty_ref, summary_width);
771 return 1;
775 current = lookup_commit_reference_gently(the_repository,
776 &ref->old_oid, 1);
777 updated = lookup_commit_reference_gently(the_repository,
778 &ref->new_oid, 1);
779 if (!current || !updated) {
780 const char *msg;
781 const char *what;
782 int r;
784 * Nicely describe the new ref we're fetching.
785 * Base this on the remote's ref name, as it's
786 * more likely to follow a standard layout.
788 const char *name = remote_ref ? remote_ref->name : "";
789 if (starts_with(name, "refs/tags/")) {
790 msg = "storing tag";
791 what = _("[new tag]");
792 } else if (starts_with(name, "refs/heads/")) {
793 msg = "storing head";
794 what = _("[new branch]");
795 } else {
796 msg = "storing ref";
797 what = _("[new ref]");
800 r = s_update_ref(msg, ref, 0);
801 format_display(display, r ? '!' : '*', what,
802 r ? _("unable to update local ref") : NULL,
803 remote, pretty_ref, summary_width);
804 return r;
807 if (fetch_show_forced_updates) {
808 uint64_t t_before = getnanotime();
809 fast_forward = in_merge_bases(current, updated);
810 forced_updates_ms += (getnanotime() - t_before) / 1000000;
811 } else {
812 fast_forward = 1;
815 if (fast_forward) {
816 struct strbuf quickref = STRBUF_INIT;
817 int r;
819 strbuf_add_unique_abbrev(&quickref, &current->object.oid, DEFAULT_ABBREV);
820 strbuf_addstr(&quickref, "..");
821 strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV);
822 r = s_update_ref("fast-forward", ref, 1);
823 format_display(display, r ? '!' : ' ', quickref.buf,
824 r ? _("unable to update local ref") : NULL,
825 remote, pretty_ref, summary_width);
826 strbuf_release(&quickref);
827 return r;
828 } else if (force || ref->force) {
829 struct strbuf quickref = STRBUF_INIT;
830 int r;
831 strbuf_add_unique_abbrev(&quickref, &current->object.oid, DEFAULT_ABBREV);
832 strbuf_addstr(&quickref, "...");
833 strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV);
834 r = s_update_ref("forced-update", ref, 1);
835 format_display(display, r ? '!' : '+', quickref.buf,
836 r ? _("unable to update local ref") : _("forced update"),
837 remote, pretty_ref, summary_width);
838 strbuf_release(&quickref);
839 return r;
840 } else {
841 format_display(display, '!', _("[rejected]"), _("non-fast-forward"),
842 remote, pretty_ref, summary_width);
843 return 1;
847 static int iterate_ref_map(void *cb_data, struct object_id *oid)
849 struct ref **rm = cb_data;
850 struct ref *ref = *rm;
852 while (ref && ref->status == REF_STATUS_REJECT_SHALLOW)
853 ref = ref->next;
854 if (!ref)
855 return -1; /* end of the list */
856 *rm = ref->next;
857 oidcpy(oid, &ref->old_oid);
858 return 0;
861 static int store_updated_refs(const char *raw_url, const char *remote_name,
862 int connectivity_checked, struct ref *ref_map)
864 FILE *fp;
865 struct commit *commit;
866 int url_len, i, rc = 0;
867 struct strbuf note = STRBUF_INIT;
868 const char *what, *kind;
869 struct ref *rm;
870 char *url;
871 const char *filename = dry_run ? "/dev/null" : git_path_fetch_head(the_repository);
872 int want_status;
873 int summary_width = transport_summary_width(ref_map);
875 fp = fopen(filename, "a");
876 if (!fp)
877 return error_errno(_("cannot open %s"), filename);
879 if (raw_url)
880 url = transport_anonymize_url(raw_url);
881 else
882 url = xstrdup("foreign");
884 if (!connectivity_checked) {
885 rm = ref_map;
886 if (check_connected(iterate_ref_map, &rm, NULL)) {
887 rc = error(_("%s did not send all necessary objects\n"), url);
888 goto abort;
892 prepare_format_display(ref_map);
895 * We do a pass for each fetch_head_status type in their enum order, so
896 * merged entries are written before not-for-merge. That lets readers
897 * use FETCH_HEAD as a refname to refer to the ref to be merged.
899 for (want_status = FETCH_HEAD_MERGE;
900 want_status <= FETCH_HEAD_IGNORE;
901 want_status++) {
902 for (rm = ref_map; rm; rm = rm->next) {
903 struct ref *ref = NULL;
904 const char *merge_status_marker = "";
906 if (rm->status == REF_STATUS_REJECT_SHALLOW) {
907 if (want_status == FETCH_HEAD_MERGE)
908 warning(_("reject %s because shallow roots are not allowed to be updated"),
909 rm->peer_ref ? rm->peer_ref->name : rm->name);
910 continue;
913 commit = lookup_commit_reference_gently(the_repository,
914 &rm->old_oid,
916 if (!commit)
917 rm->fetch_head_status = FETCH_HEAD_NOT_FOR_MERGE;
919 if (rm->fetch_head_status != want_status)
920 continue;
922 if (rm->peer_ref) {
923 ref = alloc_ref(rm->peer_ref->name);
924 oidcpy(&ref->old_oid, &rm->peer_ref->old_oid);
925 oidcpy(&ref->new_oid, &rm->old_oid);
926 ref->force = rm->peer_ref->force;
929 if (recurse_submodules != RECURSE_SUBMODULES_OFF)
930 check_for_new_submodule_commits(&rm->old_oid);
932 if (!strcmp(rm->name, "HEAD")) {
933 kind = "";
934 what = "";
936 else if (starts_with(rm->name, "refs/heads/")) {
937 kind = "branch";
938 what = rm->name + 11;
940 else if (starts_with(rm->name, "refs/tags/")) {
941 kind = "tag";
942 what = rm->name + 10;
944 else if (starts_with(rm->name, "refs/remotes/")) {
945 kind = "remote-tracking branch";
946 what = rm->name + 13;
948 else {
949 kind = "";
950 what = rm->name;
953 url_len = strlen(url);
954 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
956 url_len = i + 1;
957 if (4 < i && !strncmp(".git", url + i - 3, 4))
958 url_len = i - 3;
960 strbuf_reset(&note);
961 if (*what) {
962 if (*kind)
963 strbuf_addf(&note, "%s ", kind);
964 strbuf_addf(&note, "'%s' of ", what);
966 switch (rm->fetch_head_status) {
967 case FETCH_HEAD_NOT_FOR_MERGE:
968 merge_status_marker = "not-for-merge";
969 /* fall-through */
970 case FETCH_HEAD_MERGE:
971 fprintf(fp, "%s\t%s\t%s",
972 oid_to_hex(&rm->old_oid),
973 merge_status_marker,
974 note.buf);
975 for (i = 0; i < url_len; ++i)
976 if ('\n' == url[i])
977 fputs("\\n", fp);
978 else
979 fputc(url[i], fp);
980 fputc('\n', fp);
981 break;
982 default:
983 /* do not write anything to FETCH_HEAD */
984 break;
987 strbuf_reset(&note);
988 if (ref) {
989 rc |= update_local_ref(ref, what, rm, &note,
990 summary_width);
991 free(ref);
992 } else
993 format_display(&note, '*',
994 *kind ? kind : "branch", NULL,
995 *what ? what : "HEAD",
996 "FETCH_HEAD", summary_width);
997 if (note.len) {
998 if (verbosity >= 0 && !shown_url) {
999 fprintf(stderr, _("From %.*s\n"),
1000 url_len, url);
1001 shown_url = 1;
1003 if (verbosity >= 0)
1004 fprintf(stderr, " %s\n", note.buf);
1009 if (rc & STORE_REF_ERROR_DF_CONFLICT)
1010 error(_("some local refs could not be updated; try running\n"
1011 " 'git remote prune %s' to remove any old, conflicting "
1012 "branches"), remote_name);
1014 if (advice_fetch_show_forced_updates) {
1015 if (!fetch_show_forced_updates) {
1016 warning(_("Fetch normally indicates which branches had a forced update, but that check has been disabled."));
1017 warning(_("To re-enable, use '--show-forced-updates' flag or run 'git config fetch.showForcedUpdates true'."));
1018 } else if (forced_updates_ms > FORCED_UPDATES_DELAY_WARNING_IN_MS) {
1019 warning(_("It took %.2f seconds to check forced updates. You can use '--no-show-forced-updates'\n"),
1020 forced_updates_ms / 1000.0);
1021 warning(_("or run 'git config fetch.showForcedUpdates false' to avoid this check.\n"));
1025 abort:
1026 strbuf_release(&note);
1027 free(url);
1028 fclose(fp);
1029 return rc;
1033 * We would want to bypass the object transfer altogether if
1034 * everything we are going to fetch already exists and is connected
1035 * locally.
1037 static int check_exist_and_connected(struct ref *ref_map)
1039 struct ref *rm = ref_map;
1040 struct check_connected_options opt = CHECK_CONNECTED_INIT;
1041 struct ref *r;
1044 * If we are deepening a shallow clone we already have these
1045 * objects reachable. Running rev-list here will return with
1046 * a good (0) exit status and we'll bypass the fetch that we
1047 * really need to perform. Claiming failure now will ensure
1048 * we perform the network exchange to deepen our history.
1050 if (deepen)
1051 return -1;
1054 * check_connected() allows objects to merely be promised, but
1055 * we need all direct targets to exist.
1057 for (r = rm; r; r = r->next) {
1058 if (!has_object_file(&r->old_oid))
1059 return -1;
1062 opt.quiet = 1;
1063 return check_connected(iterate_ref_map, &rm, &opt);
1066 static int fetch_refs(struct transport *transport, struct ref *ref_map)
1068 int ret = check_exist_and_connected(ref_map);
1069 if (ret)
1070 ret = transport_fetch_refs(transport, ref_map);
1071 if (!ret)
1073 * Keep the new pack's ".keep" file around to allow the caller
1074 * time to update refs to reference the new objects.
1076 return 0;
1077 transport_unlock_pack(transport);
1078 return ret;
1081 /* Update local refs based on the ref values fetched from a remote */
1082 static int consume_refs(struct transport *transport, struct ref *ref_map)
1084 int connectivity_checked = transport->smart_options
1085 ? transport->smart_options->connectivity_checked : 0;
1086 int ret = store_updated_refs(transport->url,
1087 transport->remote->name,
1088 connectivity_checked,
1089 ref_map);
1090 transport_unlock_pack(transport);
1091 return ret;
1094 static int prune_refs(struct refspec *rs, struct ref *ref_map,
1095 const char *raw_url)
1097 int url_len, i, result = 0;
1098 struct ref *ref, *stale_refs = get_stale_heads(rs, ref_map);
1099 char *url;
1100 int summary_width = transport_summary_width(stale_refs);
1101 const char *dangling_msg = dry_run
1102 ? _(" (%s will become dangling)")
1103 : _(" (%s has become dangling)");
1105 if (raw_url)
1106 url = transport_anonymize_url(raw_url);
1107 else
1108 url = xstrdup("foreign");
1110 url_len = strlen(url);
1111 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
1114 url_len = i + 1;
1115 if (4 < i && !strncmp(".git", url + i - 3, 4))
1116 url_len = i - 3;
1118 if (!dry_run) {
1119 struct string_list refnames = STRING_LIST_INIT_NODUP;
1121 for (ref = stale_refs; ref; ref = ref->next)
1122 string_list_append(&refnames, ref->name);
1124 result = delete_refs("fetch: prune", &refnames, 0);
1125 string_list_clear(&refnames, 0);
1128 if (verbosity >= 0) {
1129 for (ref = stale_refs; ref; ref = ref->next) {
1130 struct strbuf sb = STRBUF_INIT;
1131 if (!shown_url) {
1132 fprintf(stderr, _("From %.*s\n"), url_len, url);
1133 shown_url = 1;
1135 format_display(&sb, '-', _("[deleted]"), NULL,
1136 _("(none)"), prettify_refname(ref->name),
1137 summary_width);
1138 fprintf(stderr, " %s\n",sb.buf);
1139 strbuf_release(&sb);
1140 warn_dangling_symref(stderr, dangling_msg, ref->name);
1144 free(url);
1145 free_refs(stale_refs);
1146 return result;
1149 static void check_not_current_branch(struct ref *ref_map)
1151 struct branch *current_branch = branch_get(NULL);
1153 if (is_bare_repository() || !current_branch)
1154 return;
1156 for (; ref_map; ref_map = ref_map->next)
1157 if (ref_map->peer_ref && !strcmp(current_branch->refname,
1158 ref_map->peer_ref->name))
1159 die(_("Refusing to fetch into current branch %s "
1160 "of non-bare repository"), current_branch->refname);
1163 static int truncate_fetch_head(void)
1165 const char *filename = git_path_fetch_head(the_repository);
1166 FILE *fp = fopen_for_writing(filename);
1168 if (!fp)
1169 return error_errno(_("cannot open %s"), filename);
1170 fclose(fp);
1171 return 0;
1174 static void set_option(struct transport *transport, const char *name, const char *value)
1176 int r = transport_set_option(transport, name, value);
1177 if (r < 0)
1178 die(_("Option \"%s\" value \"%s\" is not valid for %s"),
1179 name, value, transport->url);
1180 if (r > 0)
1181 warning(_("Option \"%s\" is ignored for %s\n"),
1182 name, transport->url);
1186 static int add_oid(const char *refname, const struct object_id *oid, int flags,
1187 void *cb_data)
1189 struct oid_array *oids = cb_data;
1191 oid_array_append(oids, oid);
1192 return 0;
1195 static void add_negotiation_tips(struct git_transport_options *smart_options)
1197 struct oid_array *oids = xcalloc(1, sizeof(*oids));
1198 int i;
1200 for (i = 0; i < negotiation_tip.nr; i++) {
1201 const char *s = negotiation_tip.items[i].string;
1202 int old_nr;
1203 if (!has_glob_specials(s)) {
1204 struct object_id oid;
1205 if (get_oid(s, &oid))
1206 die("%s is not a valid object", s);
1207 oid_array_append(oids, &oid);
1208 continue;
1210 old_nr = oids->nr;
1211 for_each_glob_ref(add_oid, s, oids);
1212 if (old_nr == oids->nr)
1213 warning("Ignoring --negotiation-tip=%s because it does not match any refs",
1216 smart_options->negotiation_tips = oids;
1219 static struct transport *prepare_transport(struct remote *remote, int deepen)
1221 struct transport *transport;
1223 transport = transport_get(remote, NULL);
1224 transport_set_verbosity(transport, verbosity, progress);
1225 transport->family = family;
1226 if (upload_pack)
1227 set_option(transport, TRANS_OPT_UPLOADPACK, upload_pack);
1228 if (keep)
1229 set_option(transport, TRANS_OPT_KEEP, "yes");
1230 if (depth)
1231 set_option(transport, TRANS_OPT_DEPTH, depth);
1232 if (deepen && deepen_since)
1233 set_option(transport, TRANS_OPT_DEEPEN_SINCE, deepen_since);
1234 if (deepen && deepen_not.nr)
1235 set_option(transport, TRANS_OPT_DEEPEN_NOT,
1236 (const char *)&deepen_not);
1237 if (deepen_relative)
1238 set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, "yes");
1239 if (update_shallow)
1240 set_option(transport, TRANS_OPT_UPDATE_SHALLOW, "yes");
1241 if (filter_options.choice) {
1242 struct strbuf expanded_filter_spec = STRBUF_INIT;
1243 expand_list_objects_filter_spec(&filter_options,
1244 &expanded_filter_spec);
1245 set_option(transport, TRANS_OPT_LIST_OBJECTS_FILTER,
1246 expanded_filter_spec.buf);
1247 set_option(transport, TRANS_OPT_FROM_PROMISOR, "1");
1248 strbuf_release(&expanded_filter_spec);
1250 if (negotiation_tip.nr) {
1251 if (transport->smart_options)
1252 add_negotiation_tips(transport->smart_options);
1253 else
1254 warning("Ignoring --negotiation-tip because the protocol does not support it.");
1256 return transport;
1259 static void backfill_tags(struct transport *transport, struct ref *ref_map)
1261 int cannot_reuse;
1264 * Once we have set TRANS_OPT_DEEPEN_SINCE, we can't unset it
1265 * when remote helper is used (setting it to an empty string
1266 * is not unsetting). We could extend the remote helper
1267 * protocol for that, but for now, just force a new connection
1268 * without deepen-since. Similar story for deepen-not.
1270 cannot_reuse = transport->cannot_reuse ||
1271 deepen_since || deepen_not.nr;
1272 if (cannot_reuse) {
1273 gsecondary = prepare_transport(transport->remote, 0);
1274 transport = gsecondary;
1277 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);
1278 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
1279 transport_set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, NULL);
1280 if (!fetch_refs(transport, ref_map))
1281 consume_refs(transport, ref_map);
1283 if (gsecondary) {
1284 transport_disconnect(gsecondary);
1285 gsecondary = NULL;
1289 static int do_fetch(struct transport *transport,
1290 struct refspec *rs)
1292 struct ref *ref_map;
1293 int autotags = (transport->remote->fetch_tags == 1);
1294 int retcode = 0;
1295 const struct ref *remote_refs;
1296 struct argv_array ref_prefixes = ARGV_ARRAY_INIT;
1297 int must_list_refs = 1;
1299 if (tags == TAGS_DEFAULT) {
1300 if (transport->remote->fetch_tags == 2)
1301 tags = TAGS_SET;
1302 if (transport->remote->fetch_tags == -1)
1303 tags = TAGS_UNSET;
1306 /* if not appending, truncate FETCH_HEAD */
1307 if (!append && !dry_run) {
1308 retcode = truncate_fetch_head();
1309 if (retcode)
1310 goto cleanup;
1313 if (rs->nr) {
1314 int i;
1316 refspec_ref_prefixes(rs, &ref_prefixes);
1319 * We can avoid listing refs if all of them are exact
1320 * OIDs
1322 must_list_refs = 0;
1323 for (i = 0; i < rs->nr; i++) {
1324 if (!rs->items[i].exact_sha1) {
1325 must_list_refs = 1;
1326 break;
1329 } else if (transport->remote && transport->remote->fetch.nr)
1330 refspec_ref_prefixes(&transport->remote->fetch, &ref_prefixes);
1332 if (tags == TAGS_SET || tags == TAGS_DEFAULT) {
1333 must_list_refs = 1;
1334 if (ref_prefixes.argc)
1335 argv_array_push(&ref_prefixes, "refs/tags/");
1338 if (must_list_refs)
1339 remote_refs = transport_get_remote_refs(transport, &ref_prefixes);
1340 else
1341 remote_refs = NULL;
1343 argv_array_clear(&ref_prefixes);
1345 ref_map = get_ref_map(transport->remote, remote_refs, rs,
1346 tags, &autotags);
1347 if (!update_head_ok)
1348 check_not_current_branch(ref_map);
1350 if (tags == TAGS_DEFAULT && autotags)
1351 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
1352 if (prune) {
1354 * We only prune based on refspecs specified
1355 * explicitly (via command line or configuration); we
1356 * don't care whether --tags was specified.
1358 if (rs->nr) {
1359 prune_refs(rs, ref_map, transport->url);
1360 } else {
1361 prune_refs(&transport->remote->fetch,
1362 ref_map,
1363 transport->url);
1366 if (fetch_refs(transport, ref_map) || consume_refs(transport, ref_map)) {
1367 free_refs(ref_map);
1368 retcode = 1;
1369 goto cleanup;
1371 free_refs(ref_map);
1373 /* if neither --no-tags nor --tags was specified, do automated tag
1374 * following ... */
1375 if (tags == TAGS_DEFAULT && autotags) {
1376 struct ref **tail = &ref_map;
1377 ref_map = NULL;
1378 find_non_local_tags(remote_refs, &ref_map, &tail);
1379 if (ref_map)
1380 backfill_tags(transport, ref_map);
1381 free_refs(ref_map);
1384 cleanup:
1385 return retcode;
1388 static int get_one_remote_for_fetch(struct remote *remote, void *priv)
1390 struct string_list *list = priv;
1391 if (!remote->skip_default_update)
1392 string_list_append(list, remote->name);
1393 return 0;
1396 struct remote_group_data {
1397 const char *name;
1398 struct string_list *list;
1401 static int get_remote_group(const char *key, const char *value, void *priv)
1403 struct remote_group_data *g = priv;
1405 if (skip_prefix(key, "remotes.", &key) && !strcmp(key, g->name)) {
1406 /* split list by white space */
1407 while (*value) {
1408 size_t wordlen = strcspn(value, " \t\n");
1410 if (wordlen >= 1)
1411 string_list_append_nodup(g->list,
1412 xstrndup(value, wordlen));
1413 value += wordlen + (value[wordlen] != '\0');
1417 return 0;
1420 static int add_remote_or_group(const char *name, struct string_list *list)
1422 int prev_nr = list->nr;
1423 struct remote_group_data g;
1424 g.name = name; g.list = list;
1426 git_config(get_remote_group, &g);
1427 if (list->nr == prev_nr) {
1428 struct remote *remote = remote_get(name);
1429 if (!remote_is_configured(remote, 0))
1430 return 0;
1431 string_list_append(list, remote->name);
1433 return 1;
1436 static void add_options_to_argv(struct argv_array *argv)
1438 if (dry_run)
1439 argv_array_push(argv, "--dry-run");
1440 if (prune != -1)
1441 argv_array_push(argv, prune ? "--prune" : "--no-prune");
1442 if (prune_tags != -1)
1443 argv_array_push(argv, prune_tags ? "--prune-tags" : "--no-prune-tags");
1444 if (update_head_ok)
1445 argv_array_push(argv, "--update-head-ok");
1446 if (force)
1447 argv_array_push(argv, "--force");
1448 if (keep)
1449 argv_array_push(argv, "--keep");
1450 if (recurse_submodules == RECURSE_SUBMODULES_ON)
1451 argv_array_push(argv, "--recurse-submodules");
1452 else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
1453 argv_array_push(argv, "--recurse-submodules=on-demand");
1454 if (tags == TAGS_SET)
1455 argv_array_push(argv, "--tags");
1456 else if (tags == TAGS_UNSET)
1457 argv_array_push(argv, "--no-tags");
1458 if (verbosity >= 2)
1459 argv_array_push(argv, "-v");
1460 if (verbosity >= 1)
1461 argv_array_push(argv, "-v");
1462 else if (verbosity < 0)
1463 argv_array_push(argv, "-q");
1467 /* Fetch multiple remotes in parallel */
1469 struct parallel_fetch_state {
1470 const char **argv;
1471 struct string_list *remotes;
1472 int next, result;
1475 static int fetch_next_remote(struct child_process *cp, struct strbuf *out,
1476 void *cb, void **task_cb)
1478 struct parallel_fetch_state *state = cb;
1479 char *remote;
1481 if (state->next < 0 || state->next >= state->remotes->nr)
1482 return 0;
1484 remote = state->remotes->items[state->next++].string;
1485 *task_cb = remote;
1487 argv_array_pushv(&cp->args, state->argv);
1488 argv_array_push(&cp->args, remote);
1489 cp->git_cmd = 1;
1491 if (verbosity >= 0)
1492 printf(_("Fetching %s\n"), remote);
1494 return 1;
1497 static int fetch_failed_to_start(struct strbuf *out, void *cb, void *task_cb)
1499 struct parallel_fetch_state *state = cb;
1500 const char *remote = task_cb;
1502 state->result = error(_("Could not fetch %s"), remote);
1504 return 0;
1507 static int fetch_finished(int result, struct strbuf *out,
1508 void *cb, void *task_cb)
1510 struct parallel_fetch_state *state = cb;
1511 const char *remote = task_cb;
1513 if (result) {
1514 strbuf_addf(out, _("could not fetch '%s' (exit code: %d)\n"),
1515 remote, result);
1516 state->result = -1;
1519 return 0;
1522 static int fetch_multiple(struct string_list *list, int max_children)
1524 int i, result = 0;
1525 struct argv_array argv = ARGV_ARRAY_INIT;
1527 if (!append && !dry_run) {
1528 int errcode = truncate_fetch_head();
1529 if (errcode)
1530 return errcode;
1533 argv_array_pushl(&argv, "fetch", "--append", "--no-auto-gc", NULL);
1534 add_options_to_argv(&argv);
1536 if (max_children != 1 && list->nr != 1) {
1537 struct parallel_fetch_state state = { argv.argv, list, 0, 0 };
1539 argv_array_push(&argv, "--end-of-options");
1540 result = run_processes_parallel_tr2(max_children,
1541 &fetch_next_remote,
1542 &fetch_failed_to_start,
1543 &fetch_finished,
1544 &state,
1545 "fetch", "parallel/fetch");
1547 if (!result)
1548 result = state.result;
1549 } else
1550 for (i = 0; i < list->nr; i++) {
1551 const char *name = list->items[i].string;
1552 argv_array_push(&argv, name);
1553 if (verbosity >= 0)
1554 printf(_("Fetching %s\n"), name);
1555 if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) {
1556 error(_("Could not fetch %s"), name);
1557 result = 1;
1559 argv_array_pop(&argv);
1562 argv_array_clear(&argv);
1563 return !!result;
1567 * Fetching from the promisor remote should use the given filter-spec
1568 * or inherit the default filter-spec from the config.
1570 static inline void fetch_one_setup_partial(struct remote *remote)
1573 * Explicit --no-filter argument overrides everything, regardless
1574 * of any prior partial clones and fetches.
1576 if (filter_options.no_filter)
1577 return;
1580 * If no prior partial clone/fetch and the current fetch DID NOT
1581 * request a partial-fetch, do a normal fetch.
1583 if (!repository_format_partial_clone && !filter_options.choice)
1584 return;
1587 * If this is the FIRST partial-fetch request, we enable partial
1588 * on this repo and remember the given filter-spec as the default
1589 * for subsequent fetches to this remote.
1591 if (!repository_format_partial_clone && filter_options.choice) {
1592 partial_clone_register(remote->name, &filter_options);
1593 return;
1597 * We are currently limited to only ONE promisor remote and only
1598 * allow partial-fetches from the promisor remote.
1600 if (strcmp(remote->name, repository_format_partial_clone)) {
1601 if (filter_options.choice)
1602 die(_("--filter can only be used with the remote "
1603 "configured in extensions.partialClone"));
1604 return;
1608 * Do a partial-fetch from the promisor remote using either the
1609 * explicitly given filter-spec or inherit the filter-spec from
1610 * the config.
1612 if (!filter_options.choice)
1613 partial_clone_get_default_filter_spec(&filter_options);
1614 return;
1617 static int fetch_one(struct remote *remote, int argc, const char **argv, int prune_tags_ok)
1619 struct refspec rs = REFSPEC_INIT_FETCH;
1620 int i;
1621 int exit_code;
1622 int maybe_prune_tags;
1623 int remote_via_config = remote_is_configured(remote, 0);
1625 if (!remote)
1626 die(_("No remote repository specified. Please, specify either a URL or a\n"
1627 "remote name from which new revisions should be fetched."));
1629 gtransport = prepare_transport(remote, 1);
1631 if (prune < 0) {
1632 /* no command line request */
1633 if (0 <= remote->prune)
1634 prune = remote->prune;
1635 else if (0 <= fetch_prune_config)
1636 prune = fetch_prune_config;
1637 else
1638 prune = PRUNE_BY_DEFAULT;
1641 if (prune_tags < 0) {
1642 /* no command line request */
1643 if (0 <= remote->prune_tags)
1644 prune_tags = remote->prune_tags;
1645 else if (0 <= fetch_prune_tags_config)
1646 prune_tags = fetch_prune_tags_config;
1647 else
1648 prune_tags = PRUNE_TAGS_BY_DEFAULT;
1651 maybe_prune_tags = prune_tags_ok && prune_tags;
1652 if (maybe_prune_tags && remote_via_config)
1653 refspec_append(&remote->fetch, TAG_REFSPEC);
1655 if (maybe_prune_tags && (argc || !remote_via_config))
1656 refspec_append(&rs, TAG_REFSPEC);
1658 for (i = 0; i < argc; i++) {
1659 if (!strcmp(argv[i], "tag")) {
1660 char *tag;
1661 i++;
1662 if (i >= argc)
1663 die(_("You need to specify a tag name."));
1665 tag = xstrfmt("refs/tags/%s:refs/tags/%s",
1666 argv[i], argv[i]);
1667 refspec_append(&rs, tag);
1668 free(tag);
1669 } else {
1670 refspec_append(&rs, argv[i]);
1674 if (server_options.nr)
1675 gtransport->server_options = &server_options;
1677 sigchain_push_common(unlock_pack_on_signal);
1678 atexit(unlock_pack);
1679 sigchain_push(SIGPIPE, SIG_IGN);
1680 exit_code = do_fetch(gtransport, &rs);
1681 sigchain_pop(SIGPIPE);
1682 refspec_clear(&rs);
1683 transport_disconnect(gtransport);
1684 gtransport = NULL;
1685 return exit_code;
1688 int cmd_fetch(int argc, const char **argv, const char *prefix)
1690 int i;
1691 struct string_list list = STRING_LIST_INIT_DUP;
1692 struct remote *remote = NULL;
1693 int result = 0;
1694 int prune_tags_ok = 1;
1695 struct argv_array argv_gc_auto = ARGV_ARRAY_INIT;
1697 packet_trace_identity("fetch");
1699 fetch_if_missing = 0;
1701 /* Record the command line for the reflog */
1702 strbuf_addstr(&default_rla, "fetch");
1703 for (i = 1; i < argc; i++)
1704 strbuf_addf(&default_rla, " %s", argv[i]);
1706 fetch_config_from_gitmodules(&submodule_fetch_jobs_config,
1707 &recurse_submodules);
1708 git_config(git_fetch_config, NULL);
1710 argc = parse_options(argc, argv, prefix,
1711 builtin_fetch_options, builtin_fetch_usage, 0);
1713 if (deepen_relative) {
1714 if (deepen_relative < 0)
1715 die(_("Negative depth in --deepen is not supported"));
1716 if (depth)
1717 die(_("--deepen and --depth are mutually exclusive"));
1718 depth = xstrfmt("%d", deepen_relative);
1720 if (unshallow) {
1721 if (depth)
1722 die(_("--depth and --unshallow cannot be used together"));
1723 else if (!is_repository_shallow(the_repository))
1724 die(_("--unshallow on a complete repository does not make sense"));
1725 else
1726 depth = xstrfmt("%d", INFINITE_DEPTH);
1729 /* no need to be strict, transport_set_option() will validate it again */
1730 if (depth && atoi(depth) < 1)
1731 die(_("depth %s is not a positive number"), depth);
1732 if (depth || deepen_since || deepen_not.nr)
1733 deepen = 1;
1735 if (filter_options.choice && !repository_format_partial_clone)
1736 die("--filter can only be used when extensions.partialClone is set");
1738 if (all) {
1739 if (argc == 1)
1740 die(_("fetch --all does not take a repository argument"));
1741 else if (argc > 1)
1742 die(_("fetch --all does not make sense with refspecs"));
1743 (void) for_each_remote(get_one_remote_for_fetch, &list);
1744 } else if (argc == 0) {
1745 /* No arguments -- use default remote */
1746 remote = remote_get(NULL);
1747 } else if (multiple) {
1748 /* All arguments are assumed to be remotes or groups */
1749 for (i = 0; i < argc; i++)
1750 if (!add_remote_or_group(argv[i], &list))
1751 die(_("No such remote or remote group: %s"), argv[i]);
1752 } else {
1753 /* Single remote or group */
1754 (void) add_remote_or_group(argv[0], &list);
1755 if (list.nr > 1) {
1756 /* More than one remote */
1757 if (argc > 1)
1758 die(_("Fetching a group and specifying refspecs does not make sense"));
1759 } else {
1760 /* Zero or one remotes */
1761 remote = remote_get(argv[0]);
1762 prune_tags_ok = (argc == 1);
1763 argc--;
1764 argv++;
1768 if (remote) {
1769 if (filter_options.choice || repository_format_partial_clone)
1770 fetch_one_setup_partial(remote);
1771 result = fetch_one(remote, argc, argv, prune_tags_ok);
1772 } else {
1773 int max_children = max_jobs;
1775 if (filter_options.choice)
1776 die(_("--filter can only be used with the remote "
1777 "configured in extensions.partialclone"));
1779 if (max_children < 0)
1780 max_children = fetch_parallel_config;
1782 /* TODO should this also die if we have a previous partial-clone? */
1783 result = fetch_multiple(&list, max_children);
1786 if (!result && (recurse_submodules != RECURSE_SUBMODULES_OFF)) {
1787 struct argv_array options = ARGV_ARRAY_INIT;
1788 int max_children = max_jobs;
1790 if (max_children < 0)
1791 max_children = submodule_fetch_jobs_config;
1792 if (max_children < 0)
1793 max_children = fetch_parallel_config;
1795 add_options_to_argv(&options);
1796 result = fetch_populated_submodules(the_repository,
1797 &options,
1798 submodule_prefix,
1799 recurse_submodules,
1800 recurse_submodules_default,
1801 verbosity < 0,
1802 max_children);
1803 argv_array_clear(&options);
1806 string_list_clear(&list, 0);
1808 close_object_store(the_repository->objects);
1810 if (enable_auto_gc) {
1811 argv_array_pushl(&argv_gc_auto, "gc", "--auto", NULL);
1812 if (verbosity < 0)
1813 argv_array_push(&argv_gc_auto, "--quiet");
1814 run_command_v_opt(argv_gc_auto.argv, RUN_GIT_CMD);
1815 argv_array_clear(&argv_gc_auto);
1818 return result;