6 #include "repository.h"
9 #include "object-store.h"
13 #include "string-list.h"
15 #include "transport.h"
16 #include "run-command.h"
17 #include "parse-options.h"
19 #include "submodule-config.h"
20 #include "submodule.h"
21 #include "connected.h"
22 #include "argv-array.h"
25 #include "list-objects-filter-options.h"
26 #include "commit-reach.h"
28 #include "promisor-remote.h"
29 #include "commit-graph.h"
32 #define FORCED_UPDATES_DELAY_WARNING_IN_MS (10 * 1000)
34 static const char * const builtin_fetch_usage[] = {
35 N_("git fetch [<options>] [<repository> [<refspec>...]]"),
36 N_("git fetch [<options>] <group>"),
37 N_("git fetch --multiple [<options>] [(<repository> | <group>)...]"),
38 N_("git fetch --all [<options>]"),
48 static int fetch_prune_config = -1; /* unspecified */
49 static int fetch_show_forced_updates = 1;
50 static uint64_t forced_updates_ms = 0;
51 static int prune = -1; /* unspecified */
52 #define PRUNE_BY_DEFAULT 0 /* do we prune by default? */
54 static int fetch_prune_tags_config = -1; /* unspecified */
55 static int prune_tags = -1; /* unspecified */
56 #define PRUNE_TAGS_BY_DEFAULT 0 /* do we prune tags by default? */
58 static int all, append, dry_run, force, keep, multiple, update_head_ok;
59 static int verbosity, deepen_relative, set_upstream;
60 static int progress = -1;
61 static int enable_auto_gc = 1;
62 static int tags = TAGS_DEFAULT, unshallow, update_shallow, deepen;
63 static int max_jobs = -1, submodule_fetch_jobs_config = -1;
64 static int fetch_parallel_config = 1;
65 static enum transport_family family;
66 static const char *depth;
67 static const char *deepen_since;
68 static const char *upload_pack;
69 static struct string_list deepen_not = STRING_LIST_INIT_NODUP;
70 static struct strbuf default_rla = STRBUF_INIT;
71 static struct transport *gtransport;
72 static struct transport *gsecondary;
73 static const char *submodule_prefix = "";
74 static int recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
75 static int recurse_submodules_default = RECURSE_SUBMODULES_ON_DEMAND;
76 static int shown_url = 0;
77 static struct refspec refmap = REFSPEC_INIT_FETCH;
78 static struct list_objects_filter_options filter_options;
79 static struct string_list server_options = STRING_LIST_INIT_DUP;
80 static struct string_list negotiation_tip = STRING_LIST_INIT_NODUP;
81 static int fetch_write_commit_graph = -1;
83 static int git_fetch_config(const char *k, const char *v, void *cb)
85 if (!strcmp(k, "fetch.prune")) {
86 fetch_prune_config = git_config_bool(k, v);
90 if (!strcmp(k, "fetch.prunetags")) {
91 fetch_prune_tags_config = git_config_bool(k, v);
95 if (!strcmp(k, "fetch.showforcedupdates")) {
96 fetch_show_forced_updates = git_config_bool(k, v);
100 if (!strcmp(k, "submodule.recurse")) {
101 int r = git_config_bool(k, v) ?
102 RECURSE_SUBMODULES_ON : RECURSE_SUBMODULES_OFF;
103 recurse_submodules = r;
106 if (!strcmp(k, "submodule.fetchjobs")) {
107 submodule_fetch_jobs_config = parse_submodule_fetchjobs(k, v);
109 } else if (!strcmp(k, "fetch.recursesubmodules")) {
110 recurse_submodules = parse_fetch_recurse_submodules_arg(k, v);
114 if (!strcmp(k, "fetch.parallel")) {
115 fetch_parallel_config = git_config_int(k, v);
116 if (fetch_parallel_config < 0)
117 die(_("fetch.parallel cannot be negative"));
121 return git_default_config(k, v, cb);
124 static int parse_refmap_arg(const struct option *opt, const char *arg, int unset)
126 BUG_ON_OPT_NEG(unset);
129 * "git fetch --refmap='' origin foo"
130 * can be used to tell the command not to store anywhere
132 refspec_append(&refmap, arg);
137 static struct option builtin_fetch_options[] = {
138 OPT__VERBOSITY(&verbosity),
139 OPT_BOOL(0, "all", &all,
140 N_("fetch from all remotes")),
141 OPT_BOOL(0, "set-upstream", &set_upstream,
142 N_("set upstream for git pull/fetch")),
143 OPT_BOOL('a', "append", &append,
144 N_("append to .git/FETCH_HEAD instead of overwriting")),
145 OPT_STRING(0, "upload-pack", &upload_pack, N_("path"),
146 N_("path to upload pack on remote end")),
147 OPT__FORCE(&force, N_("force overwrite of local reference"), 0),
148 OPT_BOOL('m', "multiple", &multiple,
149 N_("fetch from multiple remotes")),
150 OPT_SET_INT('t', "tags", &tags,
151 N_("fetch all tags and associated objects"), TAGS_SET),
152 OPT_SET_INT('n', NULL, &tags,
153 N_("do not fetch all tags (--no-tags)"), TAGS_UNSET),
154 OPT_INTEGER('j', "jobs", &max_jobs,
155 N_("number of submodules fetched in parallel")),
156 OPT_BOOL('p', "prune", &prune,
157 N_("prune remote-tracking branches no longer on remote")),
158 OPT_BOOL('P', "prune-tags", &prune_tags,
159 N_("prune local tags no longer on remote and clobber changed tags")),
160 OPT_CALLBACK_F(0, "recurse-submodules", &recurse_submodules, N_("on-demand"),
161 N_("control recursive fetching of submodules"),
162 PARSE_OPT_OPTARG, option_fetch_parse_recurse_submodules),
163 OPT_BOOL(0, "dry-run", &dry_run,
165 OPT_BOOL('k', "keep", &keep, N_("keep downloaded pack")),
166 OPT_BOOL('u', "update-head-ok", &update_head_ok,
167 N_("allow updating of HEAD ref")),
168 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
169 OPT_STRING(0, "depth", &depth, N_("depth"),
170 N_("deepen history of shallow clone")),
171 OPT_STRING(0, "shallow-since", &deepen_since, N_("time"),
172 N_("deepen history of shallow repository based on time")),
173 OPT_STRING_LIST(0, "shallow-exclude", &deepen_not, N_("revision"),
174 N_("deepen history of shallow clone, excluding rev")),
175 OPT_INTEGER(0, "deepen", &deepen_relative,
176 N_("deepen history of shallow clone")),
177 OPT_SET_INT_F(0, "unshallow", &unshallow,
178 N_("convert to a complete repository"),
180 { OPTION_STRING, 0, "submodule-prefix", &submodule_prefix, N_("dir"),
181 N_("prepend this to submodule path output"), PARSE_OPT_HIDDEN },
182 OPT_CALLBACK_F(0, "recurse-submodules-default",
183 &recurse_submodules_default, N_("on-demand"),
184 N_("default for recursive fetching of submodules "
185 "(lower priority than config files)"),
186 PARSE_OPT_HIDDEN, option_fetch_parse_recurse_submodules),
187 OPT_BOOL(0, "update-shallow", &update_shallow,
188 N_("accept refs that update .git/shallow")),
189 OPT_CALLBACK_F(0, "refmap", NULL, N_("refmap"),
190 N_("specify fetch refmap"), PARSE_OPT_NONEG, parse_refmap_arg),
191 OPT_STRING_LIST('o', "server-option", &server_options, N_("server-specific"), N_("option to transmit")),
192 OPT_SET_INT('4', "ipv4", &family, N_("use IPv4 addresses only"),
193 TRANSPORT_FAMILY_IPV4),
194 OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"),
195 TRANSPORT_FAMILY_IPV6),
196 OPT_STRING_LIST(0, "negotiation-tip", &negotiation_tip, N_("revision"),
197 N_("report that we have only objects reachable from this object")),
198 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
199 OPT_BOOL(0, "auto-gc", &enable_auto_gc,
200 N_("run 'gc --auto' after fetching")),
201 OPT_BOOL(0, "show-forced-updates", &fetch_show_forced_updates,
202 N_("check for forced-updates on all updated branches")),
203 OPT_BOOL(0, "write-commit-graph", &fetch_write_commit_graph,
204 N_("write the commit-graph after fetching")),
208 static void unlock_pack(void)
211 transport_unlock_pack(gtransport);
213 transport_unlock_pack(gsecondary);
216 static void unlock_pack_on_signal(int signo)
223 static void add_merge_config(struct ref **head,
224 const struct ref *remote_refs,
225 struct branch *branch,
230 for (i = 0; i < branch->merge_nr; i++) {
231 struct ref *rm, **old_tail = *tail;
232 struct refspec_item refspec;
234 for (rm = *head; rm; rm = rm->next) {
235 if (branch_merge_matches(branch, i, rm->name)) {
236 rm->fetch_head_status = FETCH_HEAD_MERGE;
244 * Not fetched to a remote-tracking branch? We need to fetch
245 * it anyway to allow this branch's "branch.$name.merge"
246 * to be honored by 'git pull', but we do not have to
247 * fail if branch.$name.merge is misconfigured to point
248 * at a nonexisting branch. If we were indeed called by
249 * 'git pull', it will notice the misconfiguration because
250 * there is no entry in the resulting FETCH_HEAD marked
253 memset(&refspec, 0, sizeof(refspec));
254 refspec.src = branch->merge[i]->src;
255 get_fetch_map(remote_refs, &refspec, tail, 1);
256 for (rm = *old_tail; rm; rm = rm->next)
257 rm->fetch_head_status = FETCH_HEAD_MERGE;
261 static void create_fetch_oidset(struct ref **head, struct oidset *out)
263 struct ref *rm = *head;
265 oidset_insert(out, &rm->old_oid);
270 struct refname_hash_entry {
271 struct hashmap_entry ent;
272 struct object_id oid;
274 char refname[FLEX_ARRAY];
277 static int refname_hash_entry_cmp(const void *hashmap_cmp_fn_data,
278 const struct hashmap_entry *eptr,
279 const struct hashmap_entry *entry_or_key,
282 const struct refname_hash_entry *e1, *e2;
284 e1 = container_of(eptr, const struct refname_hash_entry, ent);
285 e2 = container_of(entry_or_key, const struct refname_hash_entry, ent);
286 return strcmp(e1->refname, keydata ? keydata : e2->refname);
289 static struct refname_hash_entry *refname_hash_add(struct hashmap *map,
291 const struct object_id *oid)
293 struct refname_hash_entry *ent;
294 size_t len = strlen(refname);
296 FLEX_ALLOC_MEM(ent, refname, refname, len);
297 hashmap_entry_init(&ent->ent, strhash(refname));
298 oidcpy(&ent->oid, oid);
299 hashmap_add(map, &ent->ent);
303 static int add_one_refname(const char *refname,
304 const struct object_id *oid,
305 int flag, void *cbdata)
307 struct hashmap *refname_map = cbdata;
309 (void) refname_hash_add(refname_map, refname, oid);
313 static void refname_hash_init(struct hashmap *map)
315 hashmap_init(map, refname_hash_entry_cmp, NULL, 0);
318 static int refname_hash_exists(struct hashmap *map, const char *refname)
320 return !!hashmap_get_from_hash(map, strhash(refname), refname);
323 static void clear_item(struct refname_hash_entry *item)
328 static void find_non_local_tags(const struct ref *refs,
332 struct hashmap existing_refs;
333 struct hashmap remote_refs;
334 struct oidset fetch_oids = OIDSET_INIT;
335 struct string_list remote_refs_list = STRING_LIST_INIT_NODUP;
336 struct string_list_item *remote_ref_item;
337 const struct ref *ref;
338 struct refname_hash_entry *item = NULL;
339 const int quick_flags = OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT;
341 refname_hash_init(&existing_refs);
342 refname_hash_init(&remote_refs);
343 create_fetch_oidset(head, &fetch_oids);
345 for_each_ref(add_one_refname, &existing_refs);
346 for (ref = refs; ref; ref = ref->next) {
347 if (!starts_with(ref->name, "refs/tags/"))
351 * The peeled ref always follows the matching base
352 * ref, so if we see a peeled ref that we don't want
353 * to fetch then we can mark the ref entry in the list
354 * as one to ignore by setting util to NULL.
356 if (ends_with(ref->name, "^{}")) {
358 !has_object_file_with_flags(&ref->old_oid, quick_flags) &&
359 !oidset_contains(&fetch_oids, &ref->old_oid) &&
360 !has_object_file_with_flags(&item->oid, quick_flags) &&
361 !oidset_contains(&fetch_oids, &item->oid))
368 * If item is non-NULL here, then we previously saw a
369 * ref not followed by a peeled reference, so we need
370 * to check if it is a lightweight tag that we want to
374 !has_object_file_with_flags(&item->oid, quick_flags) &&
375 !oidset_contains(&fetch_oids, &item->oid))
380 /* skip duplicates and refs that we already have */
381 if (refname_hash_exists(&remote_refs, ref->name) ||
382 refname_hash_exists(&existing_refs, ref->name))
385 item = refname_hash_add(&remote_refs, ref->name, &ref->old_oid);
386 string_list_insert(&remote_refs_list, ref->name);
388 hashmap_free_entries(&existing_refs, struct refname_hash_entry, ent);
391 * We may have a final lightweight tag that needs to be
392 * checked to see if it needs fetching.
395 !has_object_file_with_flags(&item->oid, quick_flags) &&
396 !oidset_contains(&fetch_oids, &item->oid))
400 * For all the tags in the remote_refs_list,
401 * add them to the list of refs to be fetched
403 for_each_string_list_item(remote_ref_item, &remote_refs_list) {
404 const char *refname = remote_ref_item->string;
406 unsigned int hash = strhash(refname);
408 item = hashmap_get_entry_from_hash(&remote_refs, hash, refname,
409 struct refname_hash_entry, ent);
411 BUG("unseen remote ref?");
413 /* Unless we have already decided to ignore this item... */
417 rm = alloc_ref(item->refname);
418 rm->peer_ref = alloc_ref(item->refname);
419 oidcpy(&rm->old_oid, &item->oid);
423 hashmap_free_entries(&remote_refs, struct refname_hash_entry, ent);
424 string_list_clear(&remote_refs_list, 0);
425 oidset_clear(&fetch_oids);
428 static struct ref *get_ref_map(struct remote *remote,
429 const struct ref *remote_refs,
431 int tags, int *autotags)
435 struct ref *ref_map = NULL;
436 struct ref **tail = &ref_map;
438 /* opportunistically-updated references: */
439 struct ref *orefs = NULL, **oref_tail = &orefs;
441 struct hashmap existing_refs;
444 struct refspec *fetch_refspec;
446 for (i = 0; i < rs->nr; i++) {
447 get_fetch_map(remote_refs, &rs->items[i], &tail, 0);
448 if (rs->items[i].dst && rs->items[i].dst[0])
451 /* Merge everything on the command line (but not --tags) */
452 for (rm = ref_map; rm; rm = rm->next)
453 rm->fetch_head_status = FETCH_HEAD_MERGE;
456 * For any refs that we happen to be fetching via
457 * command-line arguments, the destination ref might
458 * have been missing or have been different than the
459 * remote-tracking ref that would be derived from the
460 * configured refspec. In these cases, we want to
461 * take the opportunity to update their configured
462 * remote-tracking reference. However, we do not want
463 * to mention these entries in FETCH_HEAD at all, as
464 * they would simply be duplicates of existing
465 * entries, so we set them FETCH_HEAD_IGNORE below.
467 * We compute these entries now, based only on the
468 * refspecs specified on the command line. But we add
469 * them to the list following the refspecs resulting
470 * from the tags option so that one of the latter,
471 * which has FETCH_HEAD_NOT_FOR_MERGE, is not removed
472 * by ref_remove_duplicates() in favor of one of these
473 * opportunistic entries with FETCH_HEAD_IGNORE.
476 fetch_refspec = &refmap;
478 fetch_refspec = &remote->fetch;
480 for (i = 0; i < fetch_refspec->nr; i++)
481 get_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1);
482 } else if (refmap.nr) {
483 die("--refmap option is only meaningful with command-line refspec(s).");
485 /* Use the defaults */
486 struct branch *branch = branch_get(NULL);
487 int has_merge = branch_has_merge_config(branch);
490 /* Note: has_merge implies non-NULL branch->remote_name */
491 (has_merge && !strcmp(branch->remote_name, remote->name)))) {
492 for (i = 0; i < remote->fetch.nr; i++) {
493 get_fetch_map(remote_refs, &remote->fetch.items[i], &tail, 0);
494 if (remote->fetch.items[i].dst &&
495 remote->fetch.items[i].dst[0])
497 if (!i && !has_merge && ref_map &&
498 !remote->fetch.items[0].pattern)
499 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
502 * if the remote we're fetching from is the same
503 * as given in branch.<name>.remote, we add the
504 * ref given in branch.<name>.merge, too.
506 * Note: has_merge implies non-NULL branch->remote_name
509 !strcmp(branch->remote_name, remote->name))
510 add_merge_config(&ref_map, remote_refs, branch, &tail);
512 ref_map = get_remote_ref(remote_refs, "HEAD");
514 die(_("Couldn't find remote ref HEAD"));
515 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
516 tail = &ref_map->next;
520 if (tags == TAGS_SET)
521 /* also fetch all tags */
522 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
523 else if (tags == TAGS_DEFAULT && *autotags)
524 find_non_local_tags(remote_refs, &ref_map, &tail);
526 /* Now append any refs to be updated opportunistically: */
528 for (rm = orefs; rm; rm = rm->next) {
529 rm->fetch_head_status = FETCH_HEAD_IGNORE;
533 ref_map = ref_remove_duplicates(ref_map);
535 refname_hash_init(&existing_refs);
536 for_each_ref(add_one_refname, &existing_refs);
538 for (rm = ref_map; rm; rm = rm->next) {
540 const char *refname = rm->peer_ref->name;
541 struct refname_hash_entry *peer_item;
542 unsigned int hash = strhash(refname);
544 peer_item = hashmap_get_entry_from_hash(&existing_refs,
546 struct refname_hash_entry, ent);
548 struct object_id *old_oid = &peer_item->oid;
549 oidcpy(&rm->peer_ref->old_oid, old_oid);
553 hashmap_free_entries(&existing_refs, struct refname_hash_entry, ent);
558 #define STORE_REF_ERROR_OTHER 1
559 #define STORE_REF_ERROR_DF_CONFLICT 2
561 static int s_update_ref(const char *action,
566 char *rla = getenv("GIT_REFLOG_ACTION");
567 struct ref_transaction *transaction;
568 struct strbuf err = STRBUF_INIT;
569 int ret, df_conflict = 0;
574 rla = default_rla.buf;
575 msg = xstrfmt("%s: %s", rla, action);
577 transaction = ref_transaction_begin(&err);
579 ref_transaction_update(transaction, ref->name,
581 check_old ? &ref->old_oid : NULL,
585 ret = ref_transaction_commit(transaction, &err);
587 df_conflict = (ret == TRANSACTION_NAME_CONFLICT);
591 ref_transaction_free(transaction);
592 strbuf_release(&err);
596 ref_transaction_free(transaction);
597 error("%s", err.buf);
598 strbuf_release(&err);
600 return df_conflict ? STORE_REF_ERROR_DF_CONFLICT
601 : STORE_REF_ERROR_OTHER;
604 static int refcol_width = 10;
605 static int compact_format;
607 static void adjust_refcol_width(const struct ref *ref)
609 int max, rlen, llen, len;
611 /* uptodate lines are only shown on high verbosity level */
612 if (!verbosity && oideq(&ref->peer_ref->old_oid, &ref->old_oid))
615 max = term_columns();
616 rlen = utf8_strwidth(prettify_refname(ref->name));
618 llen = utf8_strwidth(prettify_refname(ref->peer_ref->name));
621 * rough estimation to see if the output line is too long and
622 * should not be counted (we can't do precise calculation
623 * anyway because we don't know if the error explanation part
624 * will be printed in update_local_ref)
626 if (compact_format) {
630 len = 21 /* flag and summary */ + rlen + 4 /* -> */ + llen;
635 * Not precise calculation for compact mode because '*' can
636 * appear on the left hand side of '->' and shrink the column
639 if (refcol_width < rlen)
643 static void prepare_format_display(struct ref *ref_map)
646 const char *format = "full";
648 git_config_get_string_const("fetch.output", &format);
649 if (!strcasecmp(format, "full"))
651 else if (!strcasecmp(format, "compact"))
654 die(_("configuration fetch.output contains invalid value %s"),
657 for (rm = ref_map; rm; rm = rm->next) {
658 if (rm->status == REF_STATUS_REJECT_SHALLOW ||
660 !strcmp(rm->name, "HEAD"))
663 adjust_refcol_width(rm);
667 static void print_remote_to_local(struct strbuf *display,
668 const char *remote, const char *local)
670 strbuf_addf(display, "%-*s -> %s", refcol_width, remote, local);
673 static int find_and_replace(struct strbuf *haystack,
675 const char *placeholder)
677 const char *p = NULL;
680 nlen = strlen(needle);
681 if (ends_with(haystack->buf, needle))
682 p = haystack->buf + haystack->len - nlen;
684 p = strstr(haystack->buf, needle);
688 if (p > haystack->buf && p[-1] != '/')
692 if (plen > nlen && p[nlen] != '/')
695 strbuf_splice(haystack, p - haystack->buf, nlen,
696 placeholder, strlen(placeholder));
700 static void print_compact(struct strbuf *display,
701 const char *remote, const char *local)
703 struct strbuf r = STRBUF_INIT;
704 struct strbuf l = STRBUF_INIT;
706 if (!strcmp(remote, local)) {
707 strbuf_addf(display, "%-*s -> *", refcol_width, remote);
711 strbuf_addstr(&r, remote);
712 strbuf_addstr(&l, local);
714 if (!find_and_replace(&r, local, "*"))
715 find_and_replace(&l, remote, "*");
716 print_remote_to_local(display, r.buf, l.buf);
722 static void format_display(struct strbuf *display, char code,
723 const char *summary, const char *error,
724 const char *remote, const char *local,
727 int width = (summary_width + strlen(summary) - gettext_width(summary));
729 strbuf_addf(display, "%c %-*s ", code, width, summary);
731 print_remote_to_local(display, remote, local);
733 print_compact(display, remote, local);
735 strbuf_addf(display, " (%s)", error);
738 static int update_local_ref(struct ref *ref,
740 const struct ref *remote_ref,
741 struct strbuf *display,
744 struct commit *current = NULL, *updated;
745 enum object_type type;
746 struct branch *current_branch = branch_get(NULL);
747 const char *pretty_ref = prettify_refname(ref->name);
748 int fast_forward = 0;
750 type = oid_object_info(the_repository, &ref->new_oid, NULL);
752 die(_("object %s not found"), oid_to_hex(&ref->new_oid));
754 if (oideq(&ref->old_oid, &ref->new_oid)) {
756 format_display(display, '=', _("[up to date]"), NULL,
757 remote, pretty_ref, summary_width);
761 if (current_branch &&
762 !strcmp(ref->name, current_branch->name) &&
763 !(update_head_ok || is_bare_repository()) &&
764 !is_null_oid(&ref->old_oid)) {
766 * If this is the head, and it's not okay to update
767 * the head, and the old value of the head isn't empty...
769 format_display(display, '!', _("[rejected]"),
770 _("can't fetch in current branch"),
771 remote, pretty_ref, summary_width);
775 if (!is_null_oid(&ref->old_oid) &&
776 starts_with(ref->name, "refs/tags/")) {
777 if (force || ref->force) {
779 r = s_update_ref("updating tag", ref, 0);
780 format_display(display, r ? '!' : 't', _("[tag update]"),
781 r ? _("unable to update local ref") : NULL,
782 remote, pretty_ref, summary_width);
785 format_display(display, '!', _("[rejected]"), _("would clobber existing tag"),
786 remote, pretty_ref, summary_width);
791 current = lookup_commit_reference_gently(the_repository,
793 updated = lookup_commit_reference_gently(the_repository,
795 if (!current || !updated) {
800 * Nicely describe the new ref we're fetching.
801 * Base this on the remote's ref name, as it's
802 * more likely to follow a standard layout.
804 const char *name = remote_ref ? remote_ref->name : "";
805 if (starts_with(name, "refs/tags/")) {
807 what = _("[new tag]");
808 } else if (starts_with(name, "refs/heads/")) {
809 msg = "storing head";
810 what = _("[new branch]");
813 what = _("[new ref]");
816 r = s_update_ref(msg, ref, 0);
817 format_display(display, r ? '!' : '*', what,
818 r ? _("unable to update local ref") : NULL,
819 remote, pretty_ref, summary_width);
823 if (fetch_show_forced_updates) {
824 uint64_t t_before = getnanotime();
825 fast_forward = in_merge_bases(current, updated);
826 forced_updates_ms += (getnanotime() - t_before) / 1000000;
832 struct strbuf quickref = STRBUF_INIT;
835 strbuf_add_unique_abbrev(&quickref, ¤t->object.oid, DEFAULT_ABBREV);
836 strbuf_addstr(&quickref, "..");
837 strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV);
838 r = s_update_ref("fast-forward", ref, 1);
839 format_display(display, r ? '!' : ' ', quickref.buf,
840 r ? _("unable to update local ref") : NULL,
841 remote, pretty_ref, summary_width);
842 strbuf_release(&quickref);
844 } else if (force || ref->force) {
845 struct strbuf quickref = STRBUF_INIT;
847 strbuf_add_unique_abbrev(&quickref, ¤t->object.oid, DEFAULT_ABBREV);
848 strbuf_addstr(&quickref, "...");
849 strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV);
850 r = s_update_ref("forced-update", ref, 1);
851 format_display(display, r ? '!' : '+', quickref.buf,
852 r ? _("unable to update local ref") : _("forced update"),
853 remote, pretty_ref, summary_width);
854 strbuf_release(&quickref);
857 format_display(display, '!', _("[rejected]"), _("non-fast-forward"),
858 remote, pretty_ref, summary_width);
863 static int iterate_ref_map(void *cb_data, struct object_id *oid)
865 struct ref **rm = cb_data;
866 struct ref *ref = *rm;
868 while (ref && ref->status == REF_STATUS_REJECT_SHALLOW)
871 return -1; /* end of the list */
873 oidcpy(oid, &ref->old_oid);
877 static const char warn_show_forced_updates[] =
878 N_("Fetch normally indicates which branches had a forced update,\n"
879 "but that check has been disabled. To re-enable, use '--show-forced-updates'\n"
880 "flag or run 'git config fetch.showForcedUpdates true'.");
881 static const char warn_time_show_forced_updates[] =
882 N_("It took %.2f seconds to check forced updates. You can use\n"
883 "'--no-show-forced-updates' or run 'git config fetch.showForcedUpdates false'\n"
884 " to avoid this check.\n");
886 static int store_updated_refs(const char *raw_url, const char *remote_name,
887 int connectivity_checked, struct ref *ref_map)
890 struct commit *commit;
891 int url_len, i, rc = 0;
892 struct strbuf note = STRBUF_INIT;
893 const char *what, *kind;
896 const char *filename = dry_run ? "/dev/null" : git_path_fetch_head(the_repository);
898 int summary_width = transport_summary_width(ref_map);
900 fp = fopen(filename, "a");
902 return error_errno(_("cannot open %s"), filename);
905 url = transport_anonymize_url(raw_url);
907 url = xstrdup("foreign");
909 if (!connectivity_checked) {
910 struct check_connected_options opt = CHECK_CONNECTED_INIT;
913 if (check_connected(iterate_ref_map, &rm, &opt)) {
914 rc = error(_("%s did not send all necessary objects\n"), url);
919 prepare_format_display(ref_map);
922 * We do a pass for each fetch_head_status type in their enum order, so
923 * merged entries are written before not-for-merge. That lets readers
924 * use FETCH_HEAD as a refname to refer to the ref to be merged.
926 for (want_status = FETCH_HEAD_MERGE;
927 want_status <= FETCH_HEAD_IGNORE;
929 for (rm = ref_map; rm; rm = rm->next) {
930 struct ref *ref = NULL;
931 const char *merge_status_marker = "";
933 if (rm->status == REF_STATUS_REJECT_SHALLOW) {
934 if (want_status == FETCH_HEAD_MERGE)
935 warning(_("reject %s because shallow roots are not allowed to be updated"),
936 rm->peer_ref ? rm->peer_ref->name : rm->name);
940 commit = lookup_commit_reference_gently(the_repository,
944 rm->fetch_head_status = FETCH_HEAD_NOT_FOR_MERGE;
946 if (rm->fetch_head_status != want_status)
950 ref = alloc_ref(rm->peer_ref->name);
951 oidcpy(&ref->old_oid, &rm->peer_ref->old_oid);
952 oidcpy(&ref->new_oid, &rm->old_oid);
953 ref->force = rm->peer_ref->force;
956 if (recurse_submodules != RECURSE_SUBMODULES_OFF)
957 check_for_new_submodule_commits(&rm->old_oid);
959 if (!strcmp(rm->name, "HEAD")) {
963 else if (skip_prefix(rm->name, "refs/heads/", &what))
965 else if (skip_prefix(rm->name, "refs/tags/", &what))
967 else if (skip_prefix(rm->name, "refs/remotes/", &what))
968 kind = "remote-tracking branch";
974 url_len = strlen(url);
975 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
978 if (4 < i && !strncmp(".git", url + i - 3, 4))
984 strbuf_addf(¬e, "%s ", kind);
985 strbuf_addf(¬e, "'%s' of ", what);
987 switch (rm->fetch_head_status) {
988 case FETCH_HEAD_NOT_FOR_MERGE:
989 merge_status_marker = "not-for-merge";
991 case FETCH_HEAD_MERGE:
992 fprintf(fp, "%s\t%s\t%s",
993 oid_to_hex(&rm->old_oid),
996 for (i = 0; i < url_len; ++i)
1004 /* do not write anything to FETCH_HEAD */
1008 strbuf_reset(¬e);
1010 rc |= update_local_ref(ref, what, rm, ¬e,
1014 format_display(¬e, '*',
1015 *kind ? kind : "branch", NULL,
1016 *what ? what : "HEAD",
1017 "FETCH_HEAD", summary_width);
1019 if (verbosity >= 0 && !shown_url) {
1020 fprintf(stderr, _("From %.*s\n"),
1025 fprintf(stderr, " %s\n", note.buf);
1030 if (rc & STORE_REF_ERROR_DF_CONFLICT)
1031 error(_("some local refs could not be updated; try running\n"
1032 " 'git remote prune %s' to remove any old, conflicting "
1033 "branches"), remote_name);
1035 if (advice_fetch_show_forced_updates) {
1036 if (!fetch_show_forced_updates) {
1037 warning(_(warn_show_forced_updates));
1038 } else if (forced_updates_ms > FORCED_UPDATES_DELAY_WARNING_IN_MS) {
1039 warning(_(warn_time_show_forced_updates),
1040 forced_updates_ms / 1000.0);
1045 strbuf_release(¬e);
1052 * We would want to bypass the object transfer altogether if
1053 * everything we are going to fetch already exists and is connected
1056 static int check_exist_and_connected(struct ref *ref_map)
1058 struct ref *rm = ref_map;
1059 struct check_connected_options opt = CHECK_CONNECTED_INIT;
1063 * If we are deepening a shallow clone we already have these
1064 * objects reachable. Running rev-list here will return with
1065 * a good (0) exit status and we'll bypass the fetch that we
1066 * really need to perform. Claiming failure now will ensure
1067 * we perform the network exchange to deepen our history.
1073 * check_connected() allows objects to merely be promised, but
1074 * we need all direct targets to exist.
1076 for (r = rm; r; r = r->next) {
1077 if (!has_object_file_with_flags(&r->old_oid,
1078 OBJECT_INFO_SKIP_FETCH_OBJECT))
1083 return check_connected(iterate_ref_map, &rm, &opt);
1086 static int fetch_refs(struct transport *transport, struct ref *ref_map)
1088 int ret = check_exist_and_connected(ref_map);
1090 trace2_region_enter("fetch", "fetch_refs", the_repository);
1091 ret = transport_fetch_refs(transport, ref_map);
1092 trace2_region_leave("fetch", "fetch_refs", the_repository);
1096 * Keep the new pack's ".keep" file around to allow the caller
1097 * time to update refs to reference the new objects.
1100 transport_unlock_pack(transport);
1104 /* Update local refs based on the ref values fetched from a remote */
1105 static int consume_refs(struct transport *transport, struct ref *ref_map)
1107 int connectivity_checked = transport->smart_options
1108 ? transport->smart_options->connectivity_checked : 0;
1110 trace2_region_enter("fetch", "consume_refs", the_repository);
1111 ret = store_updated_refs(transport->url,
1112 transport->remote->name,
1113 connectivity_checked,
1115 transport_unlock_pack(transport);
1116 trace2_region_leave("fetch", "consume_refs", the_repository);
1120 static int prune_refs(struct refspec *rs, struct ref *ref_map,
1121 const char *raw_url)
1123 int url_len, i, result = 0;
1124 struct ref *ref, *stale_refs = get_stale_heads(rs, ref_map);
1126 int summary_width = transport_summary_width(stale_refs);
1127 const char *dangling_msg = dry_run
1128 ? _(" (%s will become dangling)")
1129 : _(" (%s has become dangling)");
1132 url = transport_anonymize_url(raw_url);
1134 url = xstrdup("foreign");
1136 url_len = strlen(url);
1137 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
1141 if (4 < i && !strncmp(".git", url + i - 3, 4))
1145 struct string_list refnames = STRING_LIST_INIT_NODUP;
1147 for (ref = stale_refs; ref; ref = ref->next)
1148 string_list_append(&refnames, ref->name);
1150 result = delete_refs("fetch: prune", &refnames, 0);
1151 string_list_clear(&refnames, 0);
1154 if (verbosity >= 0) {
1155 for (ref = stale_refs; ref; ref = ref->next) {
1156 struct strbuf sb = STRBUF_INIT;
1158 fprintf(stderr, _("From %.*s\n"), url_len, url);
1161 format_display(&sb, '-', _("[deleted]"), NULL,
1162 _("(none)"), prettify_refname(ref->name),
1164 fprintf(stderr, " %s\n",sb.buf);
1165 strbuf_release(&sb);
1166 warn_dangling_symref(stderr, dangling_msg, ref->name);
1171 free_refs(stale_refs);
1175 static void check_not_current_branch(struct ref *ref_map)
1177 struct branch *current_branch = branch_get(NULL);
1179 if (is_bare_repository() || !current_branch)
1182 for (; ref_map; ref_map = ref_map->next)
1183 if (ref_map->peer_ref && !strcmp(current_branch->refname,
1184 ref_map->peer_ref->name))
1185 die(_("Refusing to fetch into current branch %s "
1186 "of non-bare repository"), current_branch->refname);
1189 static int truncate_fetch_head(void)
1191 const char *filename = git_path_fetch_head(the_repository);
1192 FILE *fp = fopen_for_writing(filename);
1195 return error_errno(_("cannot open %s"), filename);
1200 static void set_option(struct transport *transport, const char *name, const char *value)
1202 int r = transport_set_option(transport, name, value);
1204 die(_("Option \"%s\" value \"%s\" is not valid for %s"),
1205 name, value, transport->url);
1207 warning(_("Option \"%s\" is ignored for %s\n"),
1208 name, transport->url);
1212 static int add_oid(const char *refname, const struct object_id *oid, int flags,
1215 struct oid_array *oids = cb_data;
1217 oid_array_append(oids, oid);
1221 static void add_negotiation_tips(struct git_transport_options *smart_options)
1223 struct oid_array *oids = xcalloc(1, sizeof(*oids));
1226 for (i = 0; i < negotiation_tip.nr; i++) {
1227 const char *s = negotiation_tip.items[i].string;
1229 if (!has_glob_specials(s)) {
1230 struct object_id oid;
1231 if (get_oid(s, &oid))
1232 die("%s is not a valid object", s);
1233 oid_array_append(oids, &oid);
1237 for_each_glob_ref(add_oid, s, oids);
1238 if (old_nr == oids->nr)
1239 warning("Ignoring --negotiation-tip=%s because it does not match any refs",
1242 smart_options->negotiation_tips = oids;
1245 static struct transport *prepare_transport(struct remote *remote, int deepen)
1247 struct transport *transport;
1249 transport = transport_get(remote, NULL);
1250 transport_set_verbosity(transport, verbosity, progress);
1251 transport->family = family;
1253 set_option(transport, TRANS_OPT_UPLOADPACK, upload_pack);
1255 set_option(transport, TRANS_OPT_KEEP, "yes");
1257 set_option(transport, TRANS_OPT_DEPTH, depth);
1258 if (deepen && deepen_since)
1259 set_option(transport, TRANS_OPT_DEEPEN_SINCE, deepen_since);
1260 if (deepen && deepen_not.nr)
1261 set_option(transport, TRANS_OPT_DEEPEN_NOT,
1262 (const char *)&deepen_not);
1263 if (deepen_relative)
1264 set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, "yes");
1266 set_option(transport, TRANS_OPT_UPDATE_SHALLOW, "yes");
1267 if (filter_options.choice) {
1269 expand_list_objects_filter_spec(&filter_options);
1270 set_option(transport, TRANS_OPT_LIST_OBJECTS_FILTER, spec);
1271 set_option(transport, TRANS_OPT_FROM_PROMISOR, "1");
1273 if (negotiation_tip.nr) {
1274 if (transport->smart_options)
1275 add_negotiation_tips(transport->smart_options);
1277 warning("Ignoring --negotiation-tip because the protocol does not support it.");
1282 static void backfill_tags(struct transport *transport, struct ref *ref_map)
1287 * Once we have set TRANS_OPT_DEEPEN_SINCE, we can't unset it
1288 * when remote helper is used (setting it to an empty string
1289 * is not unsetting). We could extend the remote helper
1290 * protocol for that, but for now, just force a new connection
1291 * without deepen-since. Similar story for deepen-not.
1293 cannot_reuse = transport->cannot_reuse ||
1294 deepen_since || deepen_not.nr;
1296 gsecondary = prepare_transport(transport->remote, 0);
1297 transport = gsecondary;
1300 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);
1301 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
1302 transport_set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, NULL);
1303 if (!fetch_refs(transport, ref_map))
1304 consume_refs(transport, ref_map);
1307 transport_disconnect(gsecondary);
1312 static int do_fetch(struct transport *transport,
1315 struct ref *ref_map;
1316 int autotags = (transport->remote->fetch_tags == 1);
1318 const struct ref *remote_refs;
1319 struct argv_array ref_prefixes = ARGV_ARRAY_INIT;
1320 int must_list_refs = 1;
1322 if (tags == TAGS_DEFAULT) {
1323 if (transport->remote->fetch_tags == 2)
1325 if (transport->remote->fetch_tags == -1)
1329 /* if not appending, truncate FETCH_HEAD */
1330 if (!append && !dry_run) {
1331 retcode = truncate_fetch_head();
1339 refspec_ref_prefixes(rs, &ref_prefixes);
1342 * We can avoid listing refs if all of them are exact
1346 for (i = 0; i < rs->nr; i++) {
1347 if (!rs->items[i].exact_sha1) {
1352 } else if (transport->remote && transport->remote->fetch.nr)
1353 refspec_ref_prefixes(&transport->remote->fetch, &ref_prefixes);
1355 if (tags == TAGS_SET || tags == TAGS_DEFAULT) {
1357 if (ref_prefixes.argc)
1358 argv_array_push(&ref_prefixes, "refs/tags/");
1361 if (must_list_refs) {
1362 trace2_region_enter("fetch", "remote_refs", the_repository);
1363 remote_refs = transport_get_remote_refs(transport, &ref_prefixes);
1364 trace2_region_leave("fetch", "remote_refs", the_repository);
1368 argv_array_clear(&ref_prefixes);
1370 ref_map = get_ref_map(transport->remote, remote_refs, rs,
1372 if (!update_head_ok)
1373 check_not_current_branch(ref_map);
1375 if (tags == TAGS_DEFAULT && autotags)
1376 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
1379 * We only prune based on refspecs specified
1380 * explicitly (via command line or configuration); we
1381 * don't care whether --tags was specified.
1384 prune_refs(rs, ref_map, transport->url);
1386 prune_refs(&transport->remote->fetch,
1391 if (fetch_refs(transport, ref_map) || consume_refs(transport, ref_map)) {
1398 struct branch *branch = branch_get("HEAD");
1400 struct ref *source_ref = NULL;
1403 * We're setting the upstream configuration for the
1404 * current branch. The relevant upstream is the
1405 * fetched branch that is meant to be merged with the
1406 * current one, i.e. the one fetched to FETCH_HEAD.
1408 * When there are several such branches, consider the
1409 * request ambiguous and err on the safe side by doing
1410 * nothing and just emit a warning.
1412 for (rm = ref_map; rm; rm = rm->next) {
1413 if (!rm->peer_ref) {
1415 warning(_("multiple branches detected, incompatible with --set-upstream"));
1423 if (!strcmp(source_ref->name, "HEAD") ||
1424 starts_with(source_ref->name, "refs/heads/"))
1425 install_branch_config(0,
1427 transport->remote->name,
1429 else if (starts_with(source_ref->name, "refs/remotes/"))
1430 warning(_("not setting upstream for a remote remote-tracking branch"));
1431 else if (starts_with(source_ref->name, "refs/tags/"))
1432 warning(_("not setting upstream for a remote tag"));
1434 warning(_("unknown branch type"));
1436 warning(_("no source branch found.\n"
1437 "you need to specify exactly one branch with the --set-upstream option."));
1443 /* if neither --no-tags nor --tags was specified, do automated tag
1445 if (tags == TAGS_DEFAULT && autotags) {
1446 struct ref **tail = &ref_map;
1448 find_non_local_tags(remote_refs, &ref_map, &tail);
1450 backfill_tags(transport, ref_map);
1458 static int get_one_remote_for_fetch(struct remote *remote, void *priv)
1460 struct string_list *list = priv;
1461 if (!remote->skip_default_update)
1462 string_list_append(list, remote->name);
1466 struct remote_group_data {
1468 struct string_list *list;
1471 static int get_remote_group(const char *key, const char *value, void *priv)
1473 struct remote_group_data *g = priv;
1475 if (skip_prefix(key, "remotes.", &key) && !strcmp(key, g->name)) {
1476 /* split list by white space */
1478 size_t wordlen = strcspn(value, " \t\n");
1481 string_list_append_nodup(g->list,
1482 xstrndup(value, wordlen));
1483 value += wordlen + (value[wordlen] != '\0');
1490 static int add_remote_or_group(const char *name, struct string_list *list)
1492 int prev_nr = list->nr;
1493 struct remote_group_data g;
1494 g.name = name; g.list = list;
1496 git_config(get_remote_group, &g);
1497 if (list->nr == prev_nr) {
1498 struct remote *remote = remote_get(name);
1499 if (!remote_is_configured(remote, 0))
1501 string_list_append(list, remote->name);
1506 static void add_options_to_argv(struct argv_array *argv)
1509 argv_array_push(argv, "--dry-run");
1511 argv_array_push(argv, prune ? "--prune" : "--no-prune");
1512 if (prune_tags != -1)
1513 argv_array_push(argv, prune_tags ? "--prune-tags" : "--no-prune-tags");
1515 argv_array_push(argv, "--update-head-ok");
1517 argv_array_push(argv, "--force");
1519 argv_array_push(argv, "--keep");
1520 if (recurse_submodules == RECURSE_SUBMODULES_ON)
1521 argv_array_push(argv, "--recurse-submodules");
1522 else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
1523 argv_array_push(argv, "--recurse-submodules=on-demand");
1524 if (tags == TAGS_SET)
1525 argv_array_push(argv, "--tags");
1526 else if (tags == TAGS_UNSET)
1527 argv_array_push(argv, "--no-tags");
1529 argv_array_push(argv, "-v");
1531 argv_array_push(argv, "-v");
1532 else if (verbosity < 0)
1533 argv_array_push(argv, "-q");
1537 /* Fetch multiple remotes in parallel */
1539 struct parallel_fetch_state {
1541 struct string_list *remotes;
1545 static int fetch_next_remote(struct child_process *cp, struct strbuf *out,
1546 void *cb, void **task_cb)
1548 struct parallel_fetch_state *state = cb;
1551 if (state->next < 0 || state->next >= state->remotes->nr)
1554 remote = state->remotes->items[state->next++].string;
1557 argv_array_pushv(&cp->args, state->argv);
1558 argv_array_push(&cp->args, remote);
1562 printf(_("Fetching %s\n"), remote);
1567 static int fetch_failed_to_start(struct strbuf *out, void *cb, void *task_cb)
1569 struct parallel_fetch_state *state = cb;
1570 const char *remote = task_cb;
1572 state->result = error(_("Could not fetch %s"), remote);
1577 static int fetch_finished(int result, struct strbuf *out,
1578 void *cb, void *task_cb)
1580 struct parallel_fetch_state *state = cb;
1581 const char *remote = task_cb;
1584 strbuf_addf(out, _("could not fetch '%s' (exit code: %d)\n"),
1592 static int fetch_multiple(struct string_list *list, int max_children)
1595 struct argv_array argv = ARGV_ARRAY_INIT;
1597 if (!append && !dry_run) {
1598 int errcode = truncate_fetch_head();
1603 argv_array_pushl(&argv, "fetch", "--append", "--no-auto-gc",
1604 "--no-write-commit-graph", NULL);
1605 add_options_to_argv(&argv);
1607 if (max_children != 1 && list->nr != 1) {
1608 struct parallel_fetch_state state = { argv.argv, list, 0, 0 };
1610 argv_array_push(&argv, "--end-of-options");
1611 result = run_processes_parallel_tr2(max_children,
1613 &fetch_failed_to_start,
1616 "fetch", "parallel/fetch");
1619 result = state.result;
1621 for (i = 0; i < list->nr; i++) {
1622 const char *name = list->items[i].string;
1623 argv_array_push(&argv, name);
1625 printf(_("Fetching %s\n"), name);
1626 if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) {
1627 error(_("Could not fetch %s"), name);
1630 argv_array_pop(&argv);
1633 argv_array_clear(&argv);
1638 * Fetching from the promisor remote should use the given filter-spec
1639 * or inherit the default filter-spec from the config.
1641 static inline void fetch_one_setup_partial(struct remote *remote)
1644 * Explicit --no-filter argument overrides everything, regardless
1645 * of any prior partial clones and fetches.
1647 if (filter_options.no_filter)
1651 * If no prior partial clone/fetch and the current fetch DID NOT
1652 * request a partial-fetch, do a normal fetch.
1654 if (!has_promisor_remote() && !filter_options.choice)
1658 * If this is a partial-fetch request, we enable partial on
1659 * this repo if not already enabled and remember the given
1660 * filter-spec as the default for subsequent fetches to this
1663 if (filter_options.choice) {
1664 partial_clone_register(remote->name, &filter_options);
1669 * Do a partial-fetch from the promisor remote using either the
1670 * explicitly given filter-spec or inherit the filter-spec from
1673 if (!filter_options.choice)
1674 partial_clone_get_default_filter_spec(&filter_options, remote->name);
1678 static int fetch_one(struct remote *remote, int argc, const char **argv, int prune_tags_ok)
1680 struct refspec rs = REFSPEC_INIT_FETCH;
1683 int maybe_prune_tags;
1684 int remote_via_config = remote_is_configured(remote, 0);
1687 die(_("No remote repository specified. Please, specify either a URL or a\n"
1688 "remote name from which new revisions should be fetched."));
1690 gtransport = prepare_transport(remote, 1);
1693 /* no command line request */
1694 if (0 <= remote->prune)
1695 prune = remote->prune;
1696 else if (0 <= fetch_prune_config)
1697 prune = fetch_prune_config;
1699 prune = PRUNE_BY_DEFAULT;
1702 if (prune_tags < 0) {
1703 /* no command line request */
1704 if (0 <= remote->prune_tags)
1705 prune_tags = remote->prune_tags;
1706 else if (0 <= fetch_prune_tags_config)
1707 prune_tags = fetch_prune_tags_config;
1709 prune_tags = PRUNE_TAGS_BY_DEFAULT;
1712 maybe_prune_tags = prune_tags_ok && prune_tags;
1713 if (maybe_prune_tags && remote_via_config)
1714 refspec_append(&remote->fetch, TAG_REFSPEC);
1716 if (maybe_prune_tags && (argc || !remote_via_config))
1717 refspec_append(&rs, TAG_REFSPEC);
1719 for (i = 0; i < argc; i++) {
1720 if (!strcmp(argv[i], "tag")) {
1724 die(_("You need to specify a tag name."));
1726 tag = xstrfmt("refs/tags/%s:refs/tags/%s",
1728 refspec_append(&rs, tag);
1731 refspec_append(&rs, argv[i]);
1735 if (server_options.nr)
1736 gtransport->server_options = &server_options;
1738 sigchain_push_common(unlock_pack_on_signal);
1739 atexit(unlock_pack);
1740 sigchain_push(SIGPIPE, SIG_IGN);
1741 exit_code = do_fetch(gtransport, &rs);
1742 sigchain_pop(SIGPIPE);
1744 transport_disconnect(gtransport);
1749 int cmd_fetch(int argc, const char **argv, const char *prefix)
1752 struct string_list list = STRING_LIST_INIT_DUP;
1753 struct remote *remote = NULL;
1755 int prune_tags_ok = 1;
1757 packet_trace_identity("fetch");
1759 /* Record the command line for the reflog */
1760 strbuf_addstr(&default_rla, "fetch");
1761 for (i = 1; i < argc; i++) {
1762 /* This handles non-URLs gracefully */
1763 char *anon = transport_anonymize_url(argv[i]);
1765 strbuf_addf(&default_rla, " %s", anon);
1769 fetch_config_from_gitmodules(&submodule_fetch_jobs_config,
1770 &recurse_submodules);
1771 git_config(git_fetch_config, NULL);
1773 argc = parse_options(argc, argv, prefix,
1774 builtin_fetch_options, builtin_fetch_usage, 0);
1776 if (deepen_relative) {
1777 if (deepen_relative < 0)
1778 die(_("Negative depth in --deepen is not supported"));
1780 die(_("--deepen and --depth are mutually exclusive"));
1781 depth = xstrfmt("%d", deepen_relative);
1785 die(_("--depth and --unshallow cannot be used together"));
1786 else if (!is_repository_shallow(the_repository))
1787 die(_("--unshallow on a complete repository does not make sense"));
1789 depth = xstrfmt("%d", INFINITE_DEPTH);
1792 /* no need to be strict, transport_set_option() will validate it again */
1793 if (depth && atoi(depth) < 1)
1794 die(_("depth %s is not a positive number"), depth);
1795 if (depth || deepen_since || deepen_not.nr)
1798 if (filter_options.choice && !has_promisor_remote())
1799 die("--filter can only be used when extensions.partialClone is set");
1803 die(_("fetch --all does not take a repository argument"));
1805 die(_("fetch --all does not make sense with refspecs"));
1806 (void) for_each_remote(get_one_remote_for_fetch, &list);
1807 } else if (argc == 0) {
1808 /* No arguments -- use default remote */
1809 remote = remote_get(NULL);
1810 } else if (multiple) {
1811 /* All arguments are assumed to be remotes or groups */
1812 for (i = 0; i < argc; i++)
1813 if (!add_remote_or_group(argv[i], &list))
1814 die(_("No such remote or remote group: %s"), argv[i]);
1816 /* Single remote or group */
1817 (void) add_remote_or_group(argv[0], &list);
1819 /* More than one remote */
1821 die(_("Fetching a group and specifying refspecs does not make sense"));
1823 /* Zero or one remotes */
1824 remote = remote_get(argv[0]);
1825 prune_tags_ok = (argc == 1);
1832 if (filter_options.choice || has_promisor_remote())
1833 fetch_one_setup_partial(remote);
1834 result = fetch_one(remote, argc, argv, prune_tags_ok);
1836 int max_children = max_jobs;
1838 if (filter_options.choice)
1839 die(_("--filter can only be used with the remote "
1840 "configured in extensions.partialclone"));
1842 if (max_children < 0)
1843 max_children = fetch_parallel_config;
1845 /* TODO should this also die if we have a previous partial-clone? */
1846 result = fetch_multiple(&list, max_children);
1849 if (!result && (recurse_submodules != RECURSE_SUBMODULES_OFF)) {
1850 struct argv_array options = ARGV_ARRAY_INIT;
1851 int max_children = max_jobs;
1853 if (max_children < 0)
1854 max_children = submodule_fetch_jobs_config;
1855 if (max_children < 0)
1856 max_children = fetch_parallel_config;
1858 add_options_to_argv(&options);
1859 result = fetch_populated_submodules(the_repository,
1863 recurse_submodules_default,
1866 argv_array_clear(&options);
1869 string_list_clear(&list, 0);
1871 prepare_repo_settings(the_repository);
1872 if (fetch_write_commit_graph > 0 ||
1873 (fetch_write_commit_graph < 0 &&
1874 the_repository->settings.fetch_write_commit_graph)) {
1875 int commit_graph_flags = COMMIT_GRAPH_WRITE_SPLIT;
1878 commit_graph_flags |= COMMIT_GRAPH_WRITE_PROGRESS;
1880 write_commit_graph_reachable(the_repository->objects->odb,
1885 close_object_store(the_repository->objects);
1888 run_auto_gc(verbosity < 0);