6 #include "repository.h"
11 #include "string-list.h"
13 #include "transport.h"
14 #include "run-command.h"
15 #include "parse-options.h"
17 #include "submodule-config.h"
18 #include "submodule.h"
19 #include "connected.h"
20 #include "argv-array.h"
23 #include "list-objects-filter-options.h"
25 static const char * const builtin_fetch_usage[] = {
26 N_("git fetch [<options>] [<repository> [<refspec>...]]"),
27 N_("git fetch [<options>] <group>"),
28 N_("git fetch --multiple [<options>] [(<repository> | <group>)...]"),
29 N_("git fetch --all [<options>]"),
39 static int fetch_prune_config = -1; /* unspecified */
40 static int prune = -1; /* unspecified */
41 #define PRUNE_BY_DEFAULT 0 /* do we prune by default? */
43 static int fetch_prune_tags_config = -1; /* unspecified */
44 static int prune_tags = -1; /* unspecified */
45 #define PRUNE_TAGS_BY_DEFAULT 0 /* do we prune tags by default? */
47 static int all, append, dry_run, force, keep, multiple, update_head_ok, verbosity, deepen_relative;
48 static int progress = -1;
49 static int tags = TAGS_DEFAULT, unshallow, update_shallow, deepen;
50 static int max_children = 1;
51 static enum transport_family family;
52 static const char *depth;
53 static const char *deepen_since;
54 static const char *upload_pack;
55 static struct string_list deepen_not = STRING_LIST_INIT_NODUP;
56 static struct strbuf default_rla = STRBUF_INIT;
57 static struct transport *gtransport;
58 static struct transport *gsecondary;
59 static const char *submodule_prefix = "";
60 static int recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
61 static int recurse_submodules_default = RECURSE_SUBMODULES_ON_DEMAND;
62 static int shown_url = 0;
63 static struct refspec refmap = REFSPEC_INIT_FETCH;
64 static struct list_objects_filter_options filter_options;
65 static struct string_list server_options = STRING_LIST_INIT_DUP;
67 static int git_fetch_config(const char *k, const char *v, void *cb)
69 if (!strcmp(k, "fetch.prune")) {
70 fetch_prune_config = git_config_bool(k, v);
74 if (!strcmp(k, "fetch.prunetags")) {
75 fetch_prune_tags_config = git_config_bool(k, v);
79 if (!strcmp(k, "submodule.recurse")) {
80 int r = git_config_bool(k, v) ?
81 RECURSE_SUBMODULES_ON : RECURSE_SUBMODULES_OFF;
82 recurse_submodules = r;
85 if (!strcmp(k, "submodule.fetchjobs")) {
86 max_children = parse_submodule_fetchjobs(k, v);
88 } else if (!strcmp(k, "fetch.recursesubmodules")) {
89 recurse_submodules = parse_fetch_recurse_submodules_arg(k, v);
93 return git_default_config(k, v, cb);
96 static int parse_refmap_arg(const struct option *opt, const char *arg, int unset)
99 * "git fetch --refmap='' origin foo"
100 * can be used to tell the command not to store anywhere
102 refspec_append(&refmap, arg);
107 static struct option builtin_fetch_options[] = {
108 OPT__VERBOSITY(&verbosity),
109 OPT_BOOL(0, "all", &all,
110 N_("fetch from all remotes")),
111 OPT_BOOL('a', "append", &append,
112 N_("append to .git/FETCH_HEAD instead of overwriting")),
113 OPT_STRING(0, "upload-pack", &upload_pack, N_("path"),
114 N_("path to upload pack on remote end")),
115 OPT__FORCE(&force, N_("force overwrite of local branch"), 0),
116 OPT_BOOL('m', "multiple", &multiple,
117 N_("fetch from multiple remotes")),
118 OPT_SET_INT('t', "tags", &tags,
119 N_("fetch all tags and associated objects"), TAGS_SET),
120 OPT_SET_INT('n', NULL, &tags,
121 N_("do not fetch all tags (--no-tags)"), TAGS_UNSET),
122 OPT_INTEGER('j', "jobs", &max_children,
123 N_("number of submodules fetched in parallel")),
124 OPT_BOOL('p', "prune", &prune,
125 N_("prune remote-tracking branches no longer on remote")),
126 OPT_BOOL('P', "prune-tags", &prune_tags,
127 N_("prune local tags no longer on remote and clobber changed tags")),
128 { OPTION_CALLBACK, 0, "recurse-submodules", &recurse_submodules, N_("on-demand"),
129 N_("control recursive fetching of submodules"),
130 PARSE_OPT_OPTARG, option_fetch_parse_recurse_submodules },
131 OPT_BOOL(0, "dry-run", &dry_run,
133 OPT_BOOL('k', "keep", &keep, N_("keep downloaded pack")),
134 OPT_BOOL('u', "update-head-ok", &update_head_ok,
135 N_("allow updating of HEAD ref")),
136 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
137 OPT_STRING(0, "depth", &depth, N_("depth"),
138 N_("deepen history of shallow clone")),
139 OPT_STRING(0, "shallow-since", &deepen_since, N_("time"),
140 N_("deepen history of shallow repository based on time")),
141 OPT_STRING_LIST(0, "shallow-exclude", &deepen_not, N_("revision"),
142 N_("deepen history of shallow clone, excluding rev")),
143 OPT_INTEGER(0, "deepen", &deepen_relative,
144 N_("deepen history of shallow clone")),
145 OPT_SET_INT_F(0, "unshallow", &unshallow,
146 N_("convert to a complete repository"),
148 { OPTION_STRING, 0, "submodule-prefix", &submodule_prefix, N_("dir"),
149 N_("prepend this to submodule path output"), PARSE_OPT_HIDDEN },
150 { OPTION_CALLBACK, 0, "recurse-submodules-default",
151 &recurse_submodules_default, N_("on-demand"),
152 N_("default for recursive fetching of submodules "
153 "(lower priority than config files)"),
154 PARSE_OPT_HIDDEN, option_fetch_parse_recurse_submodules },
155 OPT_BOOL(0, "update-shallow", &update_shallow,
156 N_("accept refs that update .git/shallow")),
157 { OPTION_CALLBACK, 0, "refmap", NULL, N_("refmap"),
158 N_("specify fetch refmap"), PARSE_OPT_NONEG, parse_refmap_arg },
159 OPT_STRING_LIST('o', "server-option", &server_options, N_("server-specific"), N_("option to transmit")),
160 OPT_SET_INT('4', "ipv4", &family, N_("use IPv4 addresses only"),
161 TRANSPORT_FAMILY_IPV4),
162 OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"),
163 TRANSPORT_FAMILY_IPV6),
164 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
168 static void unlock_pack(void)
171 transport_unlock_pack(gtransport);
173 transport_unlock_pack(gsecondary);
176 static void unlock_pack_on_signal(int signo)
183 static void add_merge_config(struct ref **head,
184 const struct ref *remote_refs,
185 struct branch *branch,
190 for (i = 0; i < branch->merge_nr; i++) {
191 struct ref *rm, **old_tail = *tail;
192 struct refspec_item refspec;
194 for (rm = *head; rm; rm = rm->next) {
195 if (branch_merge_matches(branch, i, rm->name)) {
196 rm->fetch_head_status = FETCH_HEAD_MERGE;
204 * Not fetched to a remote-tracking branch? We need to fetch
205 * it anyway to allow this branch's "branch.$name.merge"
206 * to be honored by 'git pull', but we do not have to
207 * fail if branch.$name.merge is misconfigured to point
208 * at a nonexisting branch. If we were indeed called by
209 * 'git pull', it will notice the misconfiguration because
210 * there is no entry in the resulting FETCH_HEAD marked
213 memset(&refspec, 0, sizeof(refspec));
214 refspec.src = branch->merge[i]->src;
215 get_fetch_map(remote_refs, &refspec, tail, 1);
216 for (rm = *old_tail; rm; rm = rm->next)
217 rm->fetch_head_status = FETCH_HEAD_MERGE;
221 static int add_existing(const char *refname, const struct object_id *oid,
222 int flag, void *cbdata)
224 struct string_list *list = (struct string_list *)cbdata;
225 struct string_list_item *item = string_list_insert(list, refname);
226 struct object_id *old_oid = xmalloc(sizeof(*old_oid));
228 oidcpy(old_oid, oid);
229 item->util = old_oid;
233 static int will_fetch(struct ref **head, const unsigned char *sha1)
235 struct ref *rm = *head;
237 if (!hashcmp(rm->old_oid.hash, sha1))
244 static void find_non_local_tags(struct transport *transport,
248 struct string_list existing_refs = STRING_LIST_INIT_DUP;
249 struct string_list remote_refs = STRING_LIST_INIT_NODUP;
250 const struct ref *ref;
251 struct string_list_item *item = NULL;
253 for_each_ref(add_existing, &existing_refs);
254 for (ref = transport_get_remote_refs(transport, NULL); ref; ref = ref->next) {
255 if (!starts_with(ref->name, "refs/tags/"))
259 * The peeled ref always follows the matching base
260 * ref, so if we see a peeled ref that we don't want
261 * to fetch then we can mark the ref entry in the list
262 * as one to ignore by setting util to NULL.
264 if (ends_with(ref->name, "^{}")) {
266 !has_object_file_with_flags(&ref->old_oid,
267 OBJECT_INFO_QUICK) &&
268 !will_fetch(head, ref->old_oid.hash) &&
269 !has_sha1_file_with_flags(item->util,
270 OBJECT_INFO_QUICK) &&
271 !will_fetch(head, item->util))
278 * If item is non-NULL here, then we previously saw a
279 * ref not followed by a peeled reference, so we need
280 * to check if it is a lightweight tag that we want to
284 !has_sha1_file_with_flags(item->util, OBJECT_INFO_QUICK) &&
285 !will_fetch(head, item->util))
290 /* skip duplicates and refs that we already have */
291 if (string_list_has_string(&remote_refs, ref->name) ||
292 string_list_has_string(&existing_refs, ref->name))
295 item = string_list_insert(&remote_refs, ref->name);
296 item->util = (void *)&ref->old_oid;
298 string_list_clear(&existing_refs, 1);
301 * We may have a final lightweight tag that needs to be
302 * checked to see if it needs fetching.
305 !has_sha1_file_with_flags(item->util, OBJECT_INFO_QUICK) &&
306 !will_fetch(head, item->util))
310 * For all the tags in the remote_refs string list,
311 * add them to the list of refs to be fetched
313 for_each_string_list_item(item, &remote_refs) {
314 /* Unless we have already decided to ignore this item... */
317 struct ref *rm = alloc_ref(item->string);
318 rm->peer_ref = alloc_ref(item->string);
319 oidcpy(&rm->old_oid, item->util);
325 string_list_clear(&remote_refs, 0);
328 static struct ref *get_ref_map(struct transport *transport,
330 int tags, int *autotags)
334 struct ref *ref_map = NULL;
335 struct ref **tail = &ref_map;
336 struct argv_array ref_prefixes = ARGV_ARRAY_INIT;
338 /* opportunistically-updated references: */
339 struct ref *orefs = NULL, **oref_tail = &orefs;
341 const struct ref *remote_refs;
344 refspec_ref_prefixes(rs, &ref_prefixes);
345 else if (transport->remote && transport->remote->fetch.nr)
346 refspec_ref_prefixes(&transport->remote->fetch, &ref_prefixes);
348 if (ref_prefixes.argc &&
349 (tags == TAGS_SET || (tags == TAGS_DEFAULT && !rs->nr))) {
350 argv_array_push(&ref_prefixes, "refs/tags/");
353 remote_refs = transport_get_remote_refs(transport, &ref_prefixes);
355 argv_array_clear(&ref_prefixes);
358 struct refspec *fetch_refspec;
360 for (i = 0; i < rs->nr; i++) {
361 get_fetch_map(remote_refs, &rs->items[i], &tail, 0);
362 if (rs->items[i].dst && rs->items[i].dst[0])
365 /* Merge everything on the command line (but not --tags) */
366 for (rm = ref_map; rm; rm = rm->next)
367 rm->fetch_head_status = FETCH_HEAD_MERGE;
370 * For any refs that we happen to be fetching via
371 * command-line arguments, the destination ref might
372 * have been missing or have been different than the
373 * remote-tracking ref that would be derived from the
374 * configured refspec. In these cases, we want to
375 * take the opportunity to update their configured
376 * remote-tracking reference. However, we do not want
377 * to mention these entries in FETCH_HEAD at all, as
378 * they would simply be duplicates of existing
379 * entries, so we set them FETCH_HEAD_IGNORE below.
381 * We compute these entries now, based only on the
382 * refspecs specified on the command line. But we add
383 * them to the list following the refspecs resulting
384 * from the tags option so that one of the latter,
385 * which has FETCH_HEAD_NOT_FOR_MERGE, is not removed
386 * by ref_remove_duplicates() in favor of one of these
387 * opportunistic entries with FETCH_HEAD_IGNORE.
390 fetch_refspec = &refmap;
392 fetch_refspec = &transport->remote->fetch;
394 for (i = 0; i < fetch_refspec->nr; i++)
395 get_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1);
396 } else if (refmap.nr) {
397 die("--refmap option is only meaningful with command-line refspec(s).");
399 /* Use the defaults */
400 struct remote *remote = transport->remote;
401 struct branch *branch = branch_get(NULL);
402 int has_merge = branch_has_merge_config(branch);
405 /* Note: has_merge implies non-NULL branch->remote_name */
406 (has_merge && !strcmp(branch->remote_name, remote->name)))) {
407 for (i = 0; i < remote->fetch.nr; i++) {
408 get_fetch_map(remote_refs, &remote->fetch.items[i], &tail, 0);
409 if (remote->fetch.items[i].dst &&
410 remote->fetch.items[i].dst[0])
412 if (!i && !has_merge && ref_map &&
413 !remote->fetch.items[0].pattern)
414 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
417 * if the remote we're fetching from is the same
418 * as given in branch.<name>.remote, we add the
419 * ref given in branch.<name>.merge, too.
421 * Note: has_merge implies non-NULL branch->remote_name
424 !strcmp(branch->remote_name, remote->name))
425 add_merge_config(&ref_map, remote_refs, branch, &tail);
427 ref_map = get_remote_ref(remote_refs, "HEAD");
429 die(_("Couldn't find remote ref HEAD"));
430 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
431 tail = &ref_map->next;
435 if (tags == TAGS_SET)
436 /* also fetch all tags */
437 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
438 else if (tags == TAGS_DEFAULT && *autotags)
439 find_non_local_tags(transport, &ref_map, &tail);
441 /* Now append any refs to be updated opportunistically: */
443 for (rm = orefs; rm; rm = rm->next) {
444 rm->fetch_head_status = FETCH_HEAD_IGNORE;
448 return ref_remove_duplicates(ref_map);
451 #define STORE_REF_ERROR_OTHER 1
452 #define STORE_REF_ERROR_DF_CONFLICT 2
454 static int s_update_ref(const char *action,
459 char *rla = getenv("GIT_REFLOG_ACTION");
460 struct ref_transaction *transaction;
461 struct strbuf err = STRBUF_INIT;
462 int ret, df_conflict = 0;
467 rla = default_rla.buf;
468 msg = xstrfmt("%s: %s", rla, action);
470 transaction = ref_transaction_begin(&err);
472 ref_transaction_update(transaction, ref->name,
474 check_old ? &ref->old_oid : NULL,
478 ret = ref_transaction_commit(transaction, &err);
480 df_conflict = (ret == TRANSACTION_NAME_CONFLICT);
484 ref_transaction_free(transaction);
485 strbuf_release(&err);
489 ref_transaction_free(transaction);
490 error("%s", err.buf);
491 strbuf_release(&err);
493 return df_conflict ? STORE_REF_ERROR_DF_CONFLICT
494 : STORE_REF_ERROR_OTHER;
497 static int refcol_width = 10;
498 static int compact_format;
500 static void adjust_refcol_width(const struct ref *ref)
502 int max, rlen, llen, len;
504 /* uptodate lines are only shown on high verbosity level */
505 if (!verbosity && !oidcmp(&ref->peer_ref->old_oid, &ref->old_oid))
508 max = term_columns();
509 rlen = utf8_strwidth(prettify_refname(ref->name));
511 llen = utf8_strwidth(prettify_refname(ref->peer_ref->name));
514 * rough estimation to see if the output line is too long and
515 * should not be counted (we can't do precise calculation
516 * anyway because we don't know if the error explanation part
517 * will be printed in update_local_ref)
519 if (compact_format) {
523 len = 21 /* flag and summary */ + rlen + 4 /* -> */ + llen;
528 * Not precise calculation for compact mode because '*' can
529 * appear on the left hand side of '->' and shrink the column
532 if (refcol_width < rlen)
536 static void prepare_format_display(struct ref *ref_map)
539 const char *format = "full";
541 git_config_get_string_const("fetch.output", &format);
542 if (!strcasecmp(format, "full"))
544 else if (!strcasecmp(format, "compact"))
547 die(_("configuration fetch.output contains invalid value %s"),
550 for (rm = ref_map; rm; rm = rm->next) {
551 if (rm->status == REF_STATUS_REJECT_SHALLOW ||
553 !strcmp(rm->name, "HEAD"))
556 adjust_refcol_width(rm);
560 static void print_remote_to_local(struct strbuf *display,
561 const char *remote, const char *local)
563 strbuf_addf(display, "%-*s -> %s", refcol_width, remote, local);
566 static int find_and_replace(struct strbuf *haystack,
568 const char *placeholder)
570 const char *p = strstr(haystack->buf, needle);
576 if (p > haystack->buf && p[-1] != '/')
580 nlen = strlen(needle);
581 if (plen > nlen && p[nlen] != '/')
584 strbuf_splice(haystack, p - haystack->buf, nlen,
585 placeholder, strlen(placeholder));
589 static void print_compact(struct strbuf *display,
590 const char *remote, const char *local)
592 struct strbuf r = STRBUF_INIT;
593 struct strbuf l = STRBUF_INIT;
595 if (!strcmp(remote, local)) {
596 strbuf_addf(display, "%-*s -> *", refcol_width, remote);
600 strbuf_addstr(&r, remote);
601 strbuf_addstr(&l, local);
603 if (!find_and_replace(&r, local, "*"))
604 find_and_replace(&l, remote, "*");
605 print_remote_to_local(display, r.buf, l.buf);
611 static void format_display(struct strbuf *display, char code,
612 const char *summary, const char *error,
613 const char *remote, const char *local,
616 int width = (summary_width + strlen(summary) - gettext_width(summary));
618 strbuf_addf(display, "%c %-*s ", code, width, summary);
620 print_remote_to_local(display, remote, local);
622 print_compact(display, remote, local);
624 strbuf_addf(display, " (%s)", error);
627 static int update_local_ref(struct ref *ref,
629 const struct ref *remote_ref,
630 struct strbuf *display,
633 struct commit *current = NULL, *updated;
634 enum object_type type;
635 struct branch *current_branch = branch_get(NULL);
636 const char *pretty_ref = prettify_refname(ref->name);
638 type = oid_object_info(the_repository, &ref->new_oid, NULL);
640 die(_("object %s not found"), oid_to_hex(&ref->new_oid));
642 if (!oidcmp(&ref->old_oid, &ref->new_oid)) {
644 format_display(display, '=', _("[up to date]"), NULL,
645 remote, pretty_ref, summary_width);
649 if (current_branch &&
650 !strcmp(ref->name, current_branch->name) &&
651 !(update_head_ok || is_bare_repository()) &&
652 !is_null_oid(&ref->old_oid)) {
654 * If this is the head, and it's not okay to update
655 * the head, and the old value of the head isn't empty...
657 format_display(display, '!', _("[rejected]"),
658 _("can't fetch in current branch"),
659 remote, pretty_ref, summary_width);
663 if (!is_null_oid(&ref->old_oid) &&
664 starts_with(ref->name, "refs/tags/")) {
666 r = s_update_ref("updating tag", ref, 0);
667 format_display(display, r ? '!' : 't', _("[tag update]"),
668 r ? _("unable to update local ref") : NULL,
669 remote, pretty_ref, summary_width);
673 current = lookup_commit_reference_gently(&ref->old_oid, 1);
674 updated = lookup_commit_reference_gently(&ref->new_oid, 1);
675 if (!current || !updated) {
680 * Nicely describe the new ref we're fetching.
681 * Base this on the remote's ref name, as it's
682 * more likely to follow a standard layout.
684 const char *name = remote_ref ? remote_ref->name : "";
685 if (starts_with(name, "refs/tags/")) {
687 what = _("[new tag]");
688 } else if (starts_with(name, "refs/heads/")) {
689 msg = "storing head";
690 what = _("[new branch]");
693 what = _("[new ref]");
696 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
697 (recurse_submodules != RECURSE_SUBMODULES_ON))
698 check_for_new_submodule_commits(&ref->new_oid);
699 r = s_update_ref(msg, ref, 0);
700 format_display(display, r ? '!' : '*', what,
701 r ? _("unable to update local ref") : NULL,
702 remote, pretty_ref, summary_width);
706 if (in_merge_bases(current, updated)) {
707 struct strbuf quickref = STRBUF_INIT;
709 strbuf_add_unique_abbrev(&quickref, ¤t->object.oid, DEFAULT_ABBREV);
710 strbuf_addstr(&quickref, "..");
711 strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV);
712 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
713 (recurse_submodules != RECURSE_SUBMODULES_ON))
714 check_for_new_submodule_commits(&ref->new_oid);
715 r = s_update_ref("fast-forward", ref, 1);
716 format_display(display, r ? '!' : ' ', quickref.buf,
717 r ? _("unable to update local ref") : NULL,
718 remote, pretty_ref, summary_width);
719 strbuf_release(&quickref);
721 } else if (force || ref->force) {
722 struct strbuf quickref = STRBUF_INIT;
724 strbuf_add_unique_abbrev(&quickref, ¤t->object.oid, DEFAULT_ABBREV);
725 strbuf_addstr(&quickref, "...");
726 strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV);
727 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
728 (recurse_submodules != RECURSE_SUBMODULES_ON))
729 check_for_new_submodule_commits(&ref->new_oid);
730 r = s_update_ref("forced-update", ref, 1);
731 format_display(display, r ? '!' : '+', quickref.buf,
732 r ? _("unable to update local ref") : _("forced update"),
733 remote, pretty_ref, summary_width);
734 strbuf_release(&quickref);
737 format_display(display, '!', _("[rejected]"), _("non-fast-forward"),
738 remote, pretty_ref, summary_width);
743 static int iterate_ref_map(void *cb_data, struct object_id *oid)
745 struct ref **rm = cb_data;
746 struct ref *ref = *rm;
748 while (ref && ref->status == REF_STATUS_REJECT_SHALLOW)
751 return -1; /* end of the list */
753 oidcpy(oid, &ref->old_oid);
757 static int store_updated_refs(const char *raw_url, const char *remote_name,
761 struct commit *commit;
762 int url_len, i, rc = 0;
763 struct strbuf note = STRBUF_INIT;
764 const char *what, *kind;
767 const char *filename = dry_run ? "/dev/null" : git_path_fetch_head();
769 int summary_width = transport_summary_width(ref_map);
771 fp = fopen(filename, "a");
773 return error_errno(_("cannot open %s"), filename);
776 url = transport_anonymize_url(raw_url);
778 url = xstrdup("foreign");
781 if (check_connected(iterate_ref_map, &rm, NULL)) {
782 rc = error(_("%s did not send all necessary objects\n"), url);
786 prepare_format_display(ref_map);
789 * We do a pass for each fetch_head_status type in their enum order, so
790 * merged entries are written before not-for-merge. That lets readers
791 * use FETCH_HEAD as a refname to refer to the ref to be merged.
793 for (want_status = FETCH_HEAD_MERGE;
794 want_status <= FETCH_HEAD_IGNORE;
796 for (rm = ref_map; rm; rm = rm->next) {
797 struct ref *ref = NULL;
798 const char *merge_status_marker = "";
800 if (rm->status == REF_STATUS_REJECT_SHALLOW) {
801 if (want_status == FETCH_HEAD_MERGE)
802 warning(_("reject %s because shallow roots are not allowed to be updated"),
803 rm->peer_ref ? rm->peer_ref->name : rm->name);
807 commit = lookup_commit_reference_gently(&rm->old_oid,
810 rm->fetch_head_status = FETCH_HEAD_NOT_FOR_MERGE;
812 if (rm->fetch_head_status != want_status)
816 ref = alloc_ref(rm->peer_ref->name);
817 oidcpy(&ref->old_oid, &rm->peer_ref->old_oid);
818 oidcpy(&ref->new_oid, &rm->old_oid);
819 ref->force = rm->peer_ref->force;
823 if (!strcmp(rm->name, "HEAD")) {
827 else if (starts_with(rm->name, "refs/heads/")) {
829 what = rm->name + 11;
831 else if (starts_with(rm->name, "refs/tags/")) {
833 what = rm->name + 10;
835 else if (starts_with(rm->name, "refs/remotes/")) {
836 kind = "remote-tracking branch";
837 what = rm->name + 13;
844 url_len = strlen(url);
845 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
848 if (4 < i && !strncmp(".git", url + i - 3, 4))
854 strbuf_addf(¬e, "%s ", kind);
855 strbuf_addf(¬e, "'%s' of ", what);
857 switch (rm->fetch_head_status) {
858 case FETCH_HEAD_NOT_FOR_MERGE:
859 merge_status_marker = "not-for-merge";
861 case FETCH_HEAD_MERGE:
862 fprintf(fp, "%s\t%s\t%s",
863 oid_to_hex(&rm->old_oid),
866 for (i = 0; i < url_len; ++i)
874 /* do not write anything to FETCH_HEAD */
880 rc |= update_local_ref(ref, what, rm, ¬e,
884 format_display(¬e, '*',
885 *kind ? kind : "branch", NULL,
886 *what ? what : "HEAD",
887 "FETCH_HEAD", summary_width);
889 if (verbosity >= 0 && !shown_url) {
890 fprintf(stderr, _("From %.*s\n"),
895 fprintf(stderr, " %s\n", note.buf);
900 if (rc & STORE_REF_ERROR_DF_CONFLICT)
901 error(_("some local refs could not be updated; try running\n"
902 " 'git remote prune %s' to remove any old, conflicting "
903 "branches"), remote_name);
906 strbuf_release(¬e);
913 * We would want to bypass the object transfer altogether if
914 * everything we are going to fetch already exists and is connected
917 static int quickfetch(struct ref *ref_map)
919 struct ref *rm = ref_map;
920 struct check_connected_options opt = CHECK_CONNECTED_INIT;
923 * If we are deepening a shallow clone we already have these
924 * objects reachable. Running rev-list here will return with
925 * a good (0) exit status and we'll bypass the fetch that we
926 * really need to perform. Claiming failure now will ensure
927 * we perform the network exchange to deepen our history.
932 return check_connected(iterate_ref_map, &rm, &opt);
935 static int fetch_refs(struct transport *transport, struct ref *ref_map)
937 int ret = quickfetch(ref_map);
939 ret = transport_fetch_refs(transport, ref_map);
941 ret |= store_updated_refs(transport->url,
942 transport->remote->name,
944 transport_unlock_pack(transport);
948 static int prune_refs(struct refspec *rs, struct ref *ref_map,
951 int url_len, i, result = 0;
952 struct ref *ref, *stale_refs = get_stale_heads(rs, ref_map);
954 int summary_width = transport_summary_width(stale_refs);
955 const char *dangling_msg = dry_run
956 ? _(" (%s will become dangling)")
957 : _(" (%s has become dangling)");
960 url = transport_anonymize_url(raw_url);
962 url = xstrdup("foreign");
964 url_len = strlen(url);
965 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
969 if (4 < i && !strncmp(".git", url + i - 3, 4))
973 struct string_list refnames = STRING_LIST_INIT_NODUP;
975 for (ref = stale_refs; ref; ref = ref->next)
976 string_list_append(&refnames, ref->name);
978 result = delete_refs("fetch: prune", &refnames, 0);
979 string_list_clear(&refnames, 0);
982 if (verbosity >= 0) {
983 for (ref = stale_refs; ref; ref = ref->next) {
984 struct strbuf sb = STRBUF_INIT;
986 fprintf(stderr, _("From %.*s\n"), url_len, url);
989 format_display(&sb, '-', _("[deleted]"), NULL,
990 _("(none)"), prettify_refname(ref->name),
992 fprintf(stderr, " %s\n",sb.buf);
994 warn_dangling_symref(stderr, dangling_msg, ref->name);
999 free_refs(stale_refs);
1003 static void check_not_current_branch(struct ref *ref_map)
1005 struct branch *current_branch = branch_get(NULL);
1007 if (is_bare_repository() || !current_branch)
1010 for (; ref_map; ref_map = ref_map->next)
1011 if (ref_map->peer_ref && !strcmp(current_branch->refname,
1012 ref_map->peer_ref->name))
1013 die(_("Refusing to fetch into current branch %s "
1014 "of non-bare repository"), current_branch->refname);
1017 static int truncate_fetch_head(void)
1019 const char *filename = git_path_fetch_head();
1020 FILE *fp = fopen_for_writing(filename);
1023 return error_errno(_("cannot open %s"), filename);
1028 static void set_option(struct transport *transport, const char *name, const char *value)
1030 int r = transport_set_option(transport, name, value);
1032 die(_("Option \"%s\" value \"%s\" is not valid for %s"),
1033 name, value, transport->url);
1035 warning(_("Option \"%s\" is ignored for %s\n"),
1036 name, transport->url);
1039 static struct transport *prepare_transport(struct remote *remote, int deepen)
1041 struct transport *transport;
1042 transport = transport_get(remote, NULL);
1043 transport_set_verbosity(transport, verbosity, progress);
1044 transport->family = family;
1046 set_option(transport, TRANS_OPT_UPLOADPACK, upload_pack);
1048 set_option(transport, TRANS_OPT_KEEP, "yes");
1050 set_option(transport, TRANS_OPT_DEPTH, depth);
1051 if (deepen && deepen_since)
1052 set_option(transport, TRANS_OPT_DEEPEN_SINCE, deepen_since);
1053 if (deepen && deepen_not.nr)
1054 set_option(transport, TRANS_OPT_DEEPEN_NOT,
1055 (const char *)&deepen_not);
1056 if (deepen_relative)
1057 set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, "yes");
1059 set_option(transport, TRANS_OPT_UPDATE_SHALLOW, "yes");
1060 if (filter_options.choice) {
1061 set_option(transport, TRANS_OPT_LIST_OBJECTS_FILTER,
1062 filter_options.filter_spec);
1063 set_option(transport, TRANS_OPT_FROM_PROMISOR, "1");
1068 static void backfill_tags(struct transport *transport, struct ref *ref_map)
1073 * Once we have set TRANS_OPT_DEEPEN_SINCE, we can't unset it
1074 * when remote helper is used (setting it to an empty string
1075 * is not unsetting). We could extend the remote helper
1076 * protocol for that, but for now, just force a new connection
1077 * without deepen-since. Similar story for deepen-not.
1079 cannot_reuse = transport->cannot_reuse ||
1080 deepen_since || deepen_not.nr;
1082 gsecondary = prepare_transport(transport->remote, 0);
1083 transport = gsecondary;
1086 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);
1087 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
1088 transport_set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, NULL);
1089 fetch_refs(transport, ref_map);
1092 transport_disconnect(gsecondary);
1097 static int do_fetch(struct transport *transport,
1100 struct string_list existing_refs = STRING_LIST_INIT_DUP;
1101 struct ref *ref_map;
1103 int autotags = (transport->remote->fetch_tags == 1);
1106 for_each_ref(add_existing, &existing_refs);
1108 if (tags == TAGS_DEFAULT) {
1109 if (transport->remote->fetch_tags == 2)
1111 if (transport->remote->fetch_tags == -1)
1115 /* if not appending, truncate FETCH_HEAD */
1116 if (!append && !dry_run) {
1117 retcode = truncate_fetch_head();
1122 ref_map = get_ref_map(transport, rs, tags, &autotags);
1123 if (!update_head_ok)
1124 check_not_current_branch(ref_map);
1126 for (rm = ref_map; rm; rm = rm->next) {
1128 struct string_list_item *peer_item =
1129 string_list_lookup(&existing_refs,
1130 rm->peer_ref->name);
1132 struct object_id *old_oid = peer_item->util;
1133 oidcpy(&rm->peer_ref->old_oid, old_oid);
1138 if (tags == TAGS_DEFAULT && autotags)
1139 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
1142 * We only prune based on refspecs specified
1143 * explicitly (via command line or configuration); we
1144 * don't care whether --tags was specified.
1147 prune_refs(rs, ref_map, transport->url);
1149 prune_refs(&transport->remote->fetch,
1154 if (fetch_refs(transport, ref_map)) {
1161 /* if neither --no-tags nor --tags was specified, do automated tag
1163 if (tags == TAGS_DEFAULT && autotags) {
1164 struct ref **tail = &ref_map;
1166 find_non_local_tags(transport, &ref_map, &tail);
1168 backfill_tags(transport, ref_map);
1173 string_list_clear(&existing_refs, 1);
1177 static int get_one_remote_for_fetch(struct remote *remote, void *priv)
1179 struct string_list *list = priv;
1180 if (!remote->skip_default_update)
1181 string_list_append(list, remote->name);
1185 struct remote_group_data {
1187 struct string_list *list;
1190 static int get_remote_group(const char *key, const char *value, void *priv)
1192 struct remote_group_data *g = priv;
1194 if (skip_prefix(key, "remotes.", &key) && !strcmp(key, g->name)) {
1195 /* split list by white space */
1197 size_t wordlen = strcspn(value, " \t\n");
1200 string_list_append_nodup(g->list,
1201 xstrndup(value, wordlen));
1202 value += wordlen + (value[wordlen] != '\0');
1209 static int add_remote_or_group(const char *name, struct string_list *list)
1211 int prev_nr = list->nr;
1212 struct remote_group_data g;
1213 g.name = name; g.list = list;
1215 git_config(get_remote_group, &g);
1216 if (list->nr == prev_nr) {
1217 struct remote *remote = remote_get(name);
1218 if (!remote_is_configured(remote, 0))
1220 string_list_append(list, remote->name);
1225 static void add_options_to_argv(struct argv_array *argv)
1228 argv_array_push(argv, "--dry-run");
1230 argv_array_push(argv, prune ? "--prune" : "--no-prune");
1231 if (prune_tags != -1)
1232 argv_array_push(argv, prune_tags ? "--prune-tags" : "--no-prune-tags");
1234 argv_array_push(argv, "--update-head-ok");
1236 argv_array_push(argv, "--force");
1238 argv_array_push(argv, "--keep");
1239 if (recurse_submodules == RECURSE_SUBMODULES_ON)
1240 argv_array_push(argv, "--recurse-submodules");
1241 else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
1242 argv_array_push(argv, "--recurse-submodules=on-demand");
1243 if (tags == TAGS_SET)
1244 argv_array_push(argv, "--tags");
1245 else if (tags == TAGS_UNSET)
1246 argv_array_push(argv, "--no-tags");
1248 argv_array_push(argv, "-v");
1250 argv_array_push(argv, "-v");
1251 else if (verbosity < 0)
1252 argv_array_push(argv, "-q");
1256 static int fetch_multiple(struct string_list *list)
1259 struct argv_array argv = ARGV_ARRAY_INIT;
1261 if (!append && !dry_run) {
1262 int errcode = truncate_fetch_head();
1267 argv_array_pushl(&argv, "fetch", "--append", NULL);
1268 add_options_to_argv(&argv);
1270 for (i = 0; i < list->nr; i++) {
1271 const char *name = list->items[i].string;
1272 argv_array_push(&argv, name);
1274 printf(_("Fetching %s\n"), name);
1275 if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) {
1276 error(_("Could not fetch %s"), name);
1279 argv_array_pop(&argv);
1282 argv_array_clear(&argv);
1287 * Fetching from the promisor remote should use the given filter-spec
1288 * or inherit the default filter-spec from the config.
1290 static inline void fetch_one_setup_partial(struct remote *remote)
1293 * Explicit --no-filter argument overrides everything, regardless
1294 * of any prior partial clones and fetches.
1296 if (filter_options.no_filter)
1300 * If no prior partial clone/fetch and the current fetch DID NOT
1301 * request a partial-fetch, do a normal fetch.
1303 if (!repository_format_partial_clone && !filter_options.choice)
1307 * If this is the FIRST partial-fetch request, we enable partial
1308 * on this repo and remember the given filter-spec as the default
1309 * for subsequent fetches to this remote.
1311 if (!repository_format_partial_clone && filter_options.choice) {
1312 partial_clone_register(remote->name, &filter_options);
1317 * We are currently limited to only ONE promisor remote and only
1318 * allow partial-fetches from the promisor remote.
1320 if (strcmp(remote->name, repository_format_partial_clone)) {
1321 if (filter_options.choice)
1322 die(_("--filter can only be used with the remote configured in core.partialClone"));
1327 * Do a partial-fetch from the promisor remote using either the
1328 * explicitly given filter-spec or inherit the filter-spec from
1331 if (!filter_options.choice)
1332 partial_clone_get_default_filter_spec(&filter_options);
1336 static int fetch_one(struct remote *remote, int argc, const char **argv, int prune_tags_ok)
1338 struct refspec rs = REFSPEC_INIT_FETCH;
1341 int maybe_prune_tags;
1342 int remote_via_config = remote_is_configured(remote, 0);
1345 die(_("No remote repository specified. Please, specify either a URL or a\n"
1346 "remote name from which new revisions should be fetched."));
1348 gtransport = prepare_transport(remote, 1);
1351 /* no command line request */
1352 if (0 <= remote->prune)
1353 prune = remote->prune;
1354 else if (0 <= fetch_prune_config)
1355 prune = fetch_prune_config;
1357 prune = PRUNE_BY_DEFAULT;
1360 if (prune_tags < 0) {
1361 /* no command line request */
1362 if (0 <= remote->prune_tags)
1363 prune_tags = remote->prune_tags;
1364 else if (0 <= fetch_prune_tags_config)
1365 prune_tags = fetch_prune_tags_config;
1367 prune_tags = PRUNE_TAGS_BY_DEFAULT;
1370 maybe_prune_tags = prune_tags_ok && prune_tags;
1371 if (maybe_prune_tags && remote_via_config)
1372 refspec_append(&remote->fetch, TAG_REFSPEC);
1374 if (maybe_prune_tags && (argc || !remote_via_config))
1375 refspec_append(&rs, TAG_REFSPEC);
1377 for (i = 0; i < argc; i++) {
1378 if (!strcmp(argv[i], "tag")) {
1382 die(_("You need to specify a tag name."));
1384 tag = xstrfmt("refs/tags/%s:refs/tags/%s",
1386 refspec_append(&rs, tag);
1389 refspec_append(&rs, argv[i]);
1393 if (server_options.nr)
1394 gtransport->server_options = &server_options;
1396 sigchain_push_common(unlock_pack_on_signal);
1397 atexit(unlock_pack);
1398 exit_code = do_fetch(gtransport, &rs);
1400 transport_disconnect(gtransport);
1405 int cmd_fetch(int argc, const char **argv, const char *prefix)
1408 struct string_list list = STRING_LIST_INIT_DUP;
1409 struct remote *remote = NULL;
1411 int prune_tags_ok = 1;
1412 struct argv_array argv_gc_auto = ARGV_ARRAY_INIT;
1414 packet_trace_identity("fetch");
1416 fetch_if_missing = 0;
1418 /* Record the command line for the reflog */
1419 strbuf_addstr(&default_rla, "fetch");
1420 for (i = 1; i < argc; i++)
1421 strbuf_addf(&default_rla, " %s", argv[i]);
1423 fetch_config_from_gitmodules(&max_children, &recurse_submodules);
1424 git_config(git_fetch_config, NULL);
1426 argc = parse_options(argc, argv, prefix,
1427 builtin_fetch_options, builtin_fetch_usage, 0);
1429 if (deepen_relative) {
1430 if (deepen_relative < 0)
1431 die(_("Negative depth in --deepen is not supported"));
1433 die(_("--deepen and --depth are mutually exclusive"));
1434 depth = xstrfmt("%d", deepen_relative);
1438 die(_("--depth and --unshallow cannot be used together"));
1439 else if (!is_repository_shallow())
1440 die(_("--unshallow on a complete repository does not make sense"));
1442 depth = xstrfmt("%d", INFINITE_DEPTH);
1445 /* no need to be strict, transport_set_option() will validate it again */
1446 if (depth && atoi(depth) < 1)
1447 die(_("depth %s is not a positive number"), depth);
1448 if (depth || deepen_since || deepen_not.nr)
1451 if (filter_options.choice && !repository_format_partial_clone)
1452 die("--filter can only be used when extensions.partialClone is set");
1456 die(_("fetch --all does not take a repository argument"));
1458 die(_("fetch --all does not make sense with refspecs"));
1459 (void) for_each_remote(get_one_remote_for_fetch, &list);
1460 } else if (argc == 0) {
1461 /* No arguments -- use default remote */
1462 remote = remote_get(NULL);
1463 } else if (multiple) {
1464 /* All arguments are assumed to be remotes or groups */
1465 for (i = 0; i < argc; i++)
1466 if (!add_remote_or_group(argv[i], &list))
1467 die(_("No such remote or remote group: %s"), argv[i]);
1469 /* Single remote or group */
1470 (void) add_remote_or_group(argv[0], &list);
1472 /* More than one remote */
1474 die(_("Fetching a group and specifying refspecs does not make sense"));
1476 /* Zero or one remotes */
1477 remote = remote_get(argv[0]);
1478 prune_tags_ok = (argc == 1);
1485 if (filter_options.choice || repository_format_partial_clone)
1486 fetch_one_setup_partial(remote);
1487 result = fetch_one(remote, argc, argv, prune_tags_ok);
1489 if (filter_options.choice)
1490 die(_("--filter can only be used with the remote configured in core.partialClone"));
1491 /* TODO should this also die if we have a previous partial-clone? */
1492 result = fetch_multiple(&list);
1495 if (!result && (recurse_submodules != RECURSE_SUBMODULES_OFF)) {
1496 struct argv_array options = ARGV_ARRAY_INIT;
1498 add_options_to_argv(&options);
1499 result = fetch_populated_submodules(the_repository,
1503 recurse_submodules_default,
1506 argv_array_clear(&options);
1509 string_list_clear(&list, 0);
1511 close_all_packs(the_repository->objects);
1513 argv_array_pushl(&argv_gc_auto, "gc", "--auto", NULL);
1515 argv_array_push(&argv_gc_auto, "--quiet");
1516 run_command_v_opt(argv_gc_auto.argv, RUN_GIT_CMD);
1517 argv_array_clear(&argv_gc_auto);