6 #include "repository.h"
9 #include "object-store.h"
12 #include "string-list.h"
14 #include "transport.h"
15 #include "run-command.h"
16 #include "parse-options.h"
18 #include "submodule-config.h"
19 #include "submodule.h"
20 #include "connected.h"
21 #include "argv-array.h"
24 #include "list-objects-filter-options.h"
25 #include "commit-reach.h"
27 static const char * const builtin_fetch_usage[] = {
28 N_("git fetch [<options>] [<repository> [<refspec>...]]"),
29 N_("git fetch [<options>] <group>"),
30 N_("git fetch --multiple [<options>] [(<repository> | <group>)...]"),
31 N_("git fetch --all [<options>]"),
41 static int fetch_prune_config = -1; /* unspecified */
42 static int prune = -1; /* unspecified */
43 #define PRUNE_BY_DEFAULT 0 /* do we prune by default? */
45 static int fetch_prune_tags_config = -1; /* unspecified */
46 static int prune_tags = -1; /* unspecified */
47 #define PRUNE_TAGS_BY_DEFAULT 0 /* do we prune tags by default? */
49 static int all, append, dry_run, force, keep, multiple, update_head_ok, verbosity, deepen_relative;
50 static int progress = -1;
51 static int tags = TAGS_DEFAULT, unshallow, update_shallow, deepen;
52 static int max_children = 1;
53 static enum transport_family family;
54 static const char *depth;
55 static const char *deepen_since;
56 static const char *upload_pack;
57 static struct string_list deepen_not = STRING_LIST_INIT_NODUP;
58 static struct strbuf default_rla = STRBUF_INIT;
59 static struct transport *gtransport;
60 static struct transport *gsecondary;
61 static const char *submodule_prefix = "";
62 static int recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
63 static int recurse_submodules_default = RECURSE_SUBMODULES_ON_DEMAND;
64 static int shown_url = 0;
65 static struct refspec refmap = REFSPEC_INIT_FETCH;
66 static struct list_objects_filter_options filter_options;
67 static struct string_list server_options = STRING_LIST_INIT_DUP;
69 static int git_fetch_config(const char *k, const char *v, void *cb)
71 if (!strcmp(k, "fetch.prune")) {
72 fetch_prune_config = git_config_bool(k, v);
76 if (!strcmp(k, "fetch.prunetags")) {
77 fetch_prune_tags_config = git_config_bool(k, v);
81 if (!strcmp(k, "submodule.recurse")) {
82 int r = git_config_bool(k, v) ?
83 RECURSE_SUBMODULES_ON : RECURSE_SUBMODULES_OFF;
84 recurse_submodules = r;
87 if (!strcmp(k, "submodule.fetchjobs")) {
88 max_children = parse_submodule_fetchjobs(k, v);
90 } else if (!strcmp(k, "fetch.recursesubmodules")) {
91 recurse_submodules = parse_fetch_recurse_submodules_arg(k, v);
95 return git_default_config(k, v, cb);
98 static int gitmodules_fetch_config(const char *var, const char *value, void *cb)
100 if (!strcmp(var, "submodule.fetchjobs")) {
101 max_children = parse_submodule_fetchjobs(var, value);
103 } else if (!strcmp(var, "fetch.recursesubmodules")) {
104 recurse_submodules = parse_fetch_recurse_submodules_arg(var, value);
111 static int parse_refmap_arg(const struct option *opt, const char *arg, int unset)
114 * "git fetch --refmap='' origin foo"
115 * can be used to tell the command not to store anywhere
117 refspec_append(&refmap, arg);
122 static struct option builtin_fetch_options[] = {
123 OPT__VERBOSITY(&verbosity),
124 OPT_BOOL(0, "all", &all,
125 N_("fetch from all remotes")),
126 OPT_BOOL('a', "append", &append,
127 N_("append to .git/FETCH_HEAD instead of overwriting")),
128 OPT_STRING(0, "upload-pack", &upload_pack, N_("path"),
129 N_("path to upload pack on remote end")),
130 OPT__FORCE(&force, N_("force overwrite of local branch"), 0),
131 OPT_BOOL('m', "multiple", &multiple,
132 N_("fetch from multiple remotes")),
133 OPT_SET_INT('t', "tags", &tags,
134 N_("fetch all tags and associated objects"), TAGS_SET),
135 OPT_SET_INT('n', NULL, &tags,
136 N_("do not fetch all tags (--no-tags)"), TAGS_UNSET),
137 OPT_INTEGER('j', "jobs", &max_children,
138 N_("number of submodules fetched in parallel")),
139 OPT_BOOL('p', "prune", &prune,
140 N_("prune remote-tracking branches no longer on remote")),
141 OPT_BOOL('P', "prune-tags", &prune_tags,
142 N_("prune local tags no longer on remote and clobber changed tags")),
143 { OPTION_CALLBACK, 0, "recurse-submodules", &recurse_submodules, N_("on-demand"),
144 N_("control recursive fetching of submodules"),
145 PARSE_OPT_OPTARG, option_fetch_parse_recurse_submodules },
146 OPT_BOOL(0, "dry-run", &dry_run,
148 OPT_BOOL('k', "keep", &keep, N_("keep downloaded pack")),
149 OPT_BOOL('u', "update-head-ok", &update_head_ok,
150 N_("allow updating of HEAD ref")),
151 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
152 OPT_STRING(0, "depth", &depth, N_("depth"),
153 N_("deepen history of shallow clone")),
154 OPT_STRING(0, "shallow-since", &deepen_since, N_("time"),
155 N_("deepen history of shallow repository based on time")),
156 OPT_STRING_LIST(0, "shallow-exclude", &deepen_not, N_("revision"),
157 N_("deepen history of shallow clone, excluding rev")),
158 OPT_INTEGER(0, "deepen", &deepen_relative,
159 N_("deepen history of shallow clone")),
160 OPT_SET_INT_F(0, "unshallow", &unshallow,
161 N_("convert to a complete repository"),
163 { OPTION_STRING, 0, "submodule-prefix", &submodule_prefix, N_("dir"),
164 N_("prepend this to submodule path output"), PARSE_OPT_HIDDEN },
165 { OPTION_CALLBACK, 0, "recurse-submodules-default",
166 &recurse_submodules_default, N_("on-demand"),
167 N_("default for recursive fetching of submodules "
168 "(lower priority than config files)"),
169 PARSE_OPT_HIDDEN, option_fetch_parse_recurse_submodules },
170 OPT_BOOL(0, "update-shallow", &update_shallow,
171 N_("accept refs that update .git/shallow")),
172 { OPTION_CALLBACK, 0, "refmap", NULL, N_("refmap"),
173 N_("specify fetch refmap"), PARSE_OPT_NONEG, parse_refmap_arg },
174 OPT_STRING_LIST('o', "server-option", &server_options, N_("server-specific"), N_("option to transmit")),
175 OPT_SET_INT('4', "ipv4", &family, N_("use IPv4 addresses only"),
176 TRANSPORT_FAMILY_IPV4),
177 OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"),
178 TRANSPORT_FAMILY_IPV6),
179 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
183 static void unlock_pack(void)
186 transport_unlock_pack(gtransport);
188 transport_unlock_pack(gsecondary);
191 static void unlock_pack_on_signal(int signo)
198 static void add_merge_config(struct ref **head,
199 const struct ref *remote_refs,
200 struct branch *branch,
205 for (i = 0; i < branch->merge_nr; i++) {
206 struct ref *rm, **old_tail = *tail;
207 struct refspec_item refspec;
209 for (rm = *head; rm; rm = rm->next) {
210 if (branch_merge_matches(branch, i, rm->name)) {
211 rm->fetch_head_status = FETCH_HEAD_MERGE;
219 * Not fetched to a remote-tracking branch? We need to fetch
220 * it anyway to allow this branch's "branch.$name.merge"
221 * to be honored by 'git pull', but we do not have to
222 * fail if branch.$name.merge is misconfigured to point
223 * at a nonexisting branch. If we were indeed called by
224 * 'git pull', it will notice the misconfiguration because
225 * there is no entry in the resulting FETCH_HEAD marked
228 memset(&refspec, 0, sizeof(refspec));
229 refspec.src = branch->merge[i]->src;
230 get_fetch_map(remote_refs, &refspec, tail, 1);
231 for (rm = *old_tail; rm; rm = rm->next)
232 rm->fetch_head_status = FETCH_HEAD_MERGE;
236 static int add_existing(const char *refname, const struct object_id *oid,
237 int flag, void *cbdata)
239 struct string_list *list = (struct string_list *)cbdata;
240 struct string_list_item *item = string_list_insert(list, refname);
241 struct object_id *old_oid = xmalloc(sizeof(*old_oid));
243 oidcpy(old_oid, oid);
244 item->util = old_oid;
248 static int will_fetch(struct ref **head, const unsigned char *sha1)
250 struct ref *rm = *head;
252 if (!hashcmp(rm->old_oid.hash, sha1))
259 static void find_non_local_tags(struct transport *transport,
263 struct string_list existing_refs = STRING_LIST_INIT_DUP;
264 struct string_list remote_refs = STRING_LIST_INIT_NODUP;
265 const struct ref *ref;
266 struct string_list_item *item = NULL;
268 for_each_ref(add_existing, &existing_refs);
269 for (ref = transport_get_remote_refs(transport, NULL); ref; ref = ref->next) {
270 if (!starts_with(ref->name, "refs/tags/"))
274 * The peeled ref always follows the matching base
275 * ref, so if we see a peeled ref that we don't want
276 * to fetch then we can mark the ref entry in the list
277 * as one to ignore by setting util to NULL.
279 if (ends_with(ref->name, "^{}")) {
281 !has_object_file_with_flags(&ref->old_oid,
282 OBJECT_INFO_QUICK) &&
283 !will_fetch(head, ref->old_oid.hash) &&
284 !has_sha1_file_with_flags(item->util,
285 OBJECT_INFO_QUICK) &&
286 !will_fetch(head, item->util))
293 * If item is non-NULL here, then we previously saw a
294 * ref not followed by a peeled reference, so we need
295 * to check if it is a lightweight tag that we want to
299 !has_sha1_file_with_flags(item->util, OBJECT_INFO_QUICK) &&
300 !will_fetch(head, item->util))
305 /* skip duplicates and refs that we already have */
306 if (string_list_has_string(&remote_refs, ref->name) ||
307 string_list_has_string(&existing_refs, ref->name))
310 item = string_list_insert(&remote_refs, ref->name);
311 item->util = (void *)&ref->old_oid;
313 string_list_clear(&existing_refs, 1);
316 * We may have a final lightweight tag that needs to be
317 * checked to see if it needs fetching.
320 !has_sha1_file_with_flags(item->util, OBJECT_INFO_QUICK) &&
321 !will_fetch(head, item->util))
325 * For all the tags in the remote_refs string list,
326 * add them to the list of refs to be fetched
328 for_each_string_list_item(item, &remote_refs) {
329 /* Unless we have already decided to ignore this item... */
332 struct ref *rm = alloc_ref(item->string);
333 rm->peer_ref = alloc_ref(item->string);
334 oidcpy(&rm->old_oid, item->util);
340 string_list_clear(&remote_refs, 0);
343 static struct ref *get_ref_map(struct transport *transport,
345 int tags, int *autotags)
349 struct ref *ref_map = NULL;
350 struct ref **tail = &ref_map;
351 struct argv_array ref_prefixes = ARGV_ARRAY_INIT;
353 /* opportunistically-updated references: */
354 struct ref *orefs = NULL, **oref_tail = &orefs;
356 const struct ref *remote_refs;
359 refspec_ref_prefixes(rs, &ref_prefixes);
360 else if (transport->remote && transport->remote->fetch.nr)
361 refspec_ref_prefixes(&transport->remote->fetch, &ref_prefixes);
363 if (ref_prefixes.argc &&
364 (tags == TAGS_SET || (tags == TAGS_DEFAULT && !rs->nr))) {
365 argv_array_push(&ref_prefixes, "refs/tags/");
368 remote_refs = transport_get_remote_refs(transport, &ref_prefixes);
370 argv_array_clear(&ref_prefixes);
373 struct refspec *fetch_refspec;
375 for (i = 0; i < rs->nr; i++) {
376 get_fetch_map(remote_refs, &rs->items[i], &tail, 0);
377 if (rs->items[i].dst && rs->items[i].dst[0])
380 /* Merge everything on the command line (but not --tags) */
381 for (rm = ref_map; rm; rm = rm->next)
382 rm->fetch_head_status = FETCH_HEAD_MERGE;
385 * For any refs that we happen to be fetching via
386 * command-line arguments, the destination ref might
387 * have been missing or have been different than the
388 * remote-tracking ref that would be derived from the
389 * configured refspec. In these cases, we want to
390 * take the opportunity to update their configured
391 * remote-tracking reference. However, we do not want
392 * to mention these entries in FETCH_HEAD at all, as
393 * they would simply be duplicates of existing
394 * entries, so we set them FETCH_HEAD_IGNORE below.
396 * We compute these entries now, based only on the
397 * refspecs specified on the command line. But we add
398 * them to the list following the refspecs resulting
399 * from the tags option so that one of the latter,
400 * which has FETCH_HEAD_NOT_FOR_MERGE, is not removed
401 * by ref_remove_duplicates() in favor of one of these
402 * opportunistic entries with FETCH_HEAD_IGNORE.
405 fetch_refspec = &refmap;
407 fetch_refspec = &transport->remote->fetch;
409 for (i = 0; i < fetch_refspec->nr; i++)
410 get_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1);
411 } else if (refmap.nr) {
412 die("--refmap option is only meaningful with command-line refspec(s).");
414 /* Use the defaults */
415 struct remote *remote = transport->remote;
416 struct branch *branch = branch_get(NULL);
417 int has_merge = branch_has_merge_config(branch);
420 /* Note: has_merge implies non-NULL branch->remote_name */
421 (has_merge && !strcmp(branch->remote_name, remote->name)))) {
422 for (i = 0; i < remote->fetch.nr; i++) {
423 get_fetch_map(remote_refs, &remote->fetch.items[i], &tail, 0);
424 if (remote->fetch.items[i].dst &&
425 remote->fetch.items[i].dst[0])
427 if (!i && !has_merge && ref_map &&
428 !remote->fetch.items[0].pattern)
429 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
432 * if the remote we're fetching from is the same
433 * as given in branch.<name>.remote, we add the
434 * ref given in branch.<name>.merge, too.
436 * Note: has_merge implies non-NULL branch->remote_name
439 !strcmp(branch->remote_name, remote->name))
440 add_merge_config(&ref_map, remote_refs, branch, &tail);
442 ref_map = get_remote_ref(remote_refs, "HEAD");
444 die(_("Couldn't find remote ref HEAD"));
445 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
446 tail = &ref_map->next;
450 if (tags == TAGS_SET)
451 /* also fetch all tags */
452 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
453 else if (tags == TAGS_DEFAULT && *autotags)
454 find_non_local_tags(transport, &ref_map, &tail);
456 /* Now append any refs to be updated opportunistically: */
458 for (rm = orefs; rm; rm = rm->next) {
459 rm->fetch_head_status = FETCH_HEAD_IGNORE;
463 return ref_remove_duplicates(ref_map);
466 #define STORE_REF_ERROR_OTHER 1
467 #define STORE_REF_ERROR_DF_CONFLICT 2
469 static int s_update_ref(const char *action,
474 char *rla = getenv("GIT_REFLOG_ACTION");
475 struct ref_transaction *transaction;
476 struct strbuf err = STRBUF_INIT;
477 int ret, df_conflict = 0;
482 rla = default_rla.buf;
483 msg = xstrfmt("%s: %s", rla, action);
485 transaction = ref_transaction_begin(&err);
487 ref_transaction_update(transaction, ref->name,
489 check_old ? &ref->old_oid : NULL,
493 ret = ref_transaction_commit(transaction, &err);
495 df_conflict = (ret == TRANSACTION_NAME_CONFLICT);
499 ref_transaction_free(transaction);
500 strbuf_release(&err);
504 ref_transaction_free(transaction);
505 error("%s", err.buf);
506 strbuf_release(&err);
508 return df_conflict ? STORE_REF_ERROR_DF_CONFLICT
509 : STORE_REF_ERROR_OTHER;
512 static int refcol_width = 10;
513 static int compact_format;
515 static void adjust_refcol_width(const struct ref *ref)
517 int max, rlen, llen, len;
519 /* uptodate lines are only shown on high verbosity level */
520 if (!verbosity && !oidcmp(&ref->peer_ref->old_oid, &ref->old_oid))
523 max = term_columns();
524 rlen = utf8_strwidth(prettify_refname(ref->name));
526 llen = utf8_strwidth(prettify_refname(ref->peer_ref->name));
529 * rough estimation to see if the output line is too long and
530 * should not be counted (we can't do precise calculation
531 * anyway because we don't know if the error explanation part
532 * will be printed in update_local_ref)
534 if (compact_format) {
538 len = 21 /* flag and summary */ + rlen + 4 /* -> */ + llen;
543 * Not precise calculation for compact mode because '*' can
544 * appear on the left hand side of '->' and shrink the column
547 if (refcol_width < rlen)
551 static void prepare_format_display(struct ref *ref_map)
554 const char *format = "full";
556 git_config_get_string_const("fetch.output", &format);
557 if (!strcasecmp(format, "full"))
559 else if (!strcasecmp(format, "compact"))
562 die(_("configuration fetch.output contains invalid value %s"),
565 for (rm = ref_map; rm; rm = rm->next) {
566 if (rm->status == REF_STATUS_REJECT_SHALLOW ||
568 !strcmp(rm->name, "HEAD"))
571 adjust_refcol_width(rm);
575 static void print_remote_to_local(struct strbuf *display,
576 const char *remote, const char *local)
578 strbuf_addf(display, "%-*s -> %s", refcol_width, remote, local);
581 static int find_and_replace(struct strbuf *haystack,
583 const char *placeholder)
585 const char *p = strstr(haystack->buf, needle);
591 if (p > haystack->buf && p[-1] != '/')
595 nlen = strlen(needle);
596 if (plen > nlen && p[nlen] != '/')
599 strbuf_splice(haystack, p - haystack->buf, nlen,
600 placeholder, strlen(placeholder));
604 static void print_compact(struct strbuf *display,
605 const char *remote, const char *local)
607 struct strbuf r = STRBUF_INIT;
608 struct strbuf l = STRBUF_INIT;
610 if (!strcmp(remote, local)) {
611 strbuf_addf(display, "%-*s -> *", refcol_width, remote);
615 strbuf_addstr(&r, remote);
616 strbuf_addstr(&l, local);
618 if (!find_and_replace(&r, local, "*"))
619 find_and_replace(&l, remote, "*");
620 print_remote_to_local(display, r.buf, l.buf);
626 static void format_display(struct strbuf *display, char code,
627 const char *summary, const char *error,
628 const char *remote, const char *local,
631 int width = (summary_width + strlen(summary) - gettext_width(summary));
633 strbuf_addf(display, "%c %-*s ", code, width, summary);
635 print_remote_to_local(display, remote, local);
637 print_compact(display, remote, local);
639 strbuf_addf(display, " (%s)", error);
642 static int update_local_ref(struct ref *ref,
644 const struct ref *remote_ref,
645 struct strbuf *display,
648 struct commit *current = NULL, *updated;
649 enum object_type type;
650 struct branch *current_branch = branch_get(NULL);
651 const char *pretty_ref = prettify_refname(ref->name);
653 type = oid_object_info(the_repository, &ref->new_oid, NULL);
655 die(_("object %s not found"), oid_to_hex(&ref->new_oid));
657 if (!oidcmp(&ref->old_oid, &ref->new_oid)) {
659 format_display(display, '=', _("[up to date]"), NULL,
660 remote, pretty_ref, summary_width);
664 if (current_branch &&
665 !strcmp(ref->name, current_branch->name) &&
666 !(update_head_ok || is_bare_repository()) &&
667 !is_null_oid(&ref->old_oid)) {
669 * If this is the head, and it's not okay to update
670 * the head, and the old value of the head isn't empty...
672 format_display(display, '!', _("[rejected]"),
673 _("can't fetch in current branch"),
674 remote, pretty_ref, summary_width);
678 if (!is_null_oid(&ref->old_oid) &&
679 starts_with(ref->name, "refs/tags/")) {
681 r = s_update_ref("updating tag", ref, 0);
682 format_display(display, r ? '!' : 't', _("[tag update]"),
683 r ? _("unable to update local ref") : NULL,
684 remote, pretty_ref, summary_width);
688 current = lookup_commit_reference_gently(the_repository,
690 updated = lookup_commit_reference_gently(the_repository,
692 if (!current || !updated) {
697 * Nicely describe the new ref we're fetching.
698 * Base this on the remote's ref name, as it's
699 * more likely to follow a standard layout.
701 const char *name = remote_ref ? remote_ref->name : "";
702 if (starts_with(name, "refs/tags/")) {
704 what = _("[new tag]");
705 } else if (starts_with(name, "refs/heads/")) {
706 msg = "storing head";
707 what = _("[new branch]");
710 what = _("[new ref]");
713 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
714 (recurse_submodules != RECURSE_SUBMODULES_ON))
715 check_for_new_submodule_commits(&ref->new_oid);
716 r = s_update_ref(msg, ref, 0);
717 format_display(display, r ? '!' : '*', what,
718 r ? _("unable to update local ref") : NULL,
719 remote, pretty_ref, summary_width);
723 if (in_merge_bases(current, updated)) {
724 struct strbuf quickref = STRBUF_INIT;
726 strbuf_add_unique_abbrev(&quickref, ¤t->object.oid, DEFAULT_ABBREV);
727 strbuf_addstr(&quickref, "..");
728 strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV);
729 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
730 (recurse_submodules != RECURSE_SUBMODULES_ON))
731 check_for_new_submodule_commits(&ref->new_oid);
732 r = s_update_ref("fast-forward", ref, 1);
733 format_display(display, r ? '!' : ' ', quickref.buf,
734 r ? _("unable to update local ref") : NULL,
735 remote, pretty_ref, summary_width);
736 strbuf_release(&quickref);
738 } else if (force || ref->force) {
739 struct strbuf quickref = STRBUF_INIT;
741 strbuf_add_unique_abbrev(&quickref, ¤t->object.oid, DEFAULT_ABBREV);
742 strbuf_addstr(&quickref, "...");
743 strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV);
744 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
745 (recurse_submodules != RECURSE_SUBMODULES_ON))
746 check_for_new_submodule_commits(&ref->new_oid);
747 r = s_update_ref("forced-update", ref, 1);
748 format_display(display, r ? '!' : '+', quickref.buf,
749 r ? _("unable to update local ref") : _("forced update"),
750 remote, pretty_ref, summary_width);
751 strbuf_release(&quickref);
754 format_display(display, '!', _("[rejected]"), _("non-fast-forward"),
755 remote, pretty_ref, summary_width);
760 static int iterate_ref_map(void *cb_data, struct object_id *oid)
762 struct ref **rm = cb_data;
763 struct ref *ref = *rm;
765 while (ref && ref->status == REF_STATUS_REJECT_SHALLOW)
768 return -1; /* end of the list */
770 oidcpy(oid, &ref->old_oid);
774 static int store_updated_refs(const char *raw_url, const char *remote_name,
778 struct commit *commit;
779 int url_len, i, rc = 0;
780 struct strbuf note = STRBUF_INIT;
781 const char *what, *kind;
784 const char *filename = dry_run ? "/dev/null" : git_path_fetch_head(the_repository);
786 int summary_width = transport_summary_width(ref_map);
788 fp = fopen(filename, "a");
790 return error_errno(_("cannot open %s"), filename);
793 url = transport_anonymize_url(raw_url);
795 url = xstrdup("foreign");
798 if (check_connected(iterate_ref_map, &rm, NULL)) {
799 rc = error(_("%s did not send all necessary objects\n"), url);
803 prepare_format_display(ref_map);
806 * We do a pass for each fetch_head_status type in their enum order, so
807 * merged entries are written before not-for-merge. That lets readers
808 * use FETCH_HEAD as a refname to refer to the ref to be merged.
810 for (want_status = FETCH_HEAD_MERGE;
811 want_status <= FETCH_HEAD_IGNORE;
813 for (rm = ref_map; rm; rm = rm->next) {
814 struct ref *ref = NULL;
815 const char *merge_status_marker = "";
817 if (rm->status == REF_STATUS_REJECT_SHALLOW) {
818 if (want_status == FETCH_HEAD_MERGE)
819 warning(_("reject %s because shallow roots are not allowed to be updated"),
820 rm->peer_ref ? rm->peer_ref->name : rm->name);
824 commit = lookup_commit_reference_gently(the_repository,
828 rm->fetch_head_status = FETCH_HEAD_NOT_FOR_MERGE;
830 if (rm->fetch_head_status != want_status)
834 ref = alloc_ref(rm->peer_ref->name);
835 oidcpy(&ref->old_oid, &rm->peer_ref->old_oid);
836 oidcpy(&ref->new_oid, &rm->old_oid);
837 ref->force = rm->peer_ref->force;
841 if (!strcmp(rm->name, "HEAD")) {
845 else if (starts_with(rm->name, "refs/heads/")) {
847 what = rm->name + 11;
849 else if (starts_with(rm->name, "refs/tags/")) {
851 what = rm->name + 10;
853 else if (starts_with(rm->name, "refs/remotes/")) {
854 kind = "remote-tracking branch";
855 what = rm->name + 13;
862 url_len = strlen(url);
863 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
866 if (4 < i && !strncmp(".git", url + i - 3, 4))
872 strbuf_addf(¬e, "%s ", kind);
873 strbuf_addf(¬e, "'%s' of ", what);
875 switch (rm->fetch_head_status) {
876 case FETCH_HEAD_NOT_FOR_MERGE:
877 merge_status_marker = "not-for-merge";
879 case FETCH_HEAD_MERGE:
880 fprintf(fp, "%s\t%s\t%s",
881 oid_to_hex(&rm->old_oid),
884 for (i = 0; i < url_len; ++i)
892 /* do not write anything to FETCH_HEAD */
898 rc |= update_local_ref(ref, what, rm, ¬e,
902 format_display(¬e, '*',
903 *kind ? kind : "branch", NULL,
904 *what ? what : "HEAD",
905 "FETCH_HEAD", summary_width);
907 if (verbosity >= 0 && !shown_url) {
908 fprintf(stderr, _("From %.*s\n"),
913 fprintf(stderr, " %s\n", note.buf);
918 if (rc & STORE_REF_ERROR_DF_CONFLICT)
919 error(_("some local refs could not be updated; try running\n"
920 " 'git remote prune %s' to remove any old, conflicting "
921 "branches"), remote_name);
924 strbuf_release(¬e);
931 * We would want to bypass the object transfer altogether if
932 * everything we are going to fetch already exists and is connected
935 static int quickfetch(struct ref *ref_map)
937 struct ref *rm = ref_map;
938 struct check_connected_options opt = CHECK_CONNECTED_INIT;
941 * If we are deepening a shallow clone we already have these
942 * objects reachable. Running rev-list here will return with
943 * a good (0) exit status and we'll bypass the fetch that we
944 * really need to perform. Claiming failure now will ensure
945 * we perform the network exchange to deepen our history.
950 return check_connected(iterate_ref_map, &rm, &opt);
953 static int fetch_refs(struct transport *transport, struct ref *ref_map)
955 int ret = quickfetch(ref_map);
957 ret = transport_fetch_refs(transport, ref_map);
959 ret |= store_updated_refs(transport->url,
960 transport->remote->name,
962 transport_unlock_pack(transport);
966 static int prune_refs(struct refspec *rs, struct ref *ref_map,
969 int url_len, i, result = 0;
970 struct ref *ref, *stale_refs = get_stale_heads(rs, ref_map);
972 int summary_width = transport_summary_width(stale_refs);
973 const char *dangling_msg = dry_run
974 ? _(" (%s will become dangling)")
975 : _(" (%s has become dangling)");
978 url = transport_anonymize_url(raw_url);
980 url = xstrdup("foreign");
982 url_len = strlen(url);
983 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
987 if (4 < i && !strncmp(".git", url + i - 3, 4))
991 struct string_list refnames = STRING_LIST_INIT_NODUP;
993 for (ref = stale_refs; ref; ref = ref->next)
994 string_list_append(&refnames, ref->name);
996 result = delete_refs("fetch: prune", &refnames, 0);
997 string_list_clear(&refnames, 0);
1000 if (verbosity >= 0) {
1001 for (ref = stale_refs; ref; ref = ref->next) {
1002 struct strbuf sb = STRBUF_INIT;
1004 fprintf(stderr, _("From %.*s\n"), url_len, url);
1007 format_display(&sb, '-', _("[deleted]"), NULL,
1008 _("(none)"), prettify_refname(ref->name),
1010 fprintf(stderr, " %s\n",sb.buf);
1011 strbuf_release(&sb);
1012 warn_dangling_symref(stderr, dangling_msg, ref->name);
1017 free_refs(stale_refs);
1021 static void check_not_current_branch(struct ref *ref_map)
1023 struct branch *current_branch = branch_get(NULL);
1025 if (is_bare_repository() || !current_branch)
1028 for (; ref_map; ref_map = ref_map->next)
1029 if (ref_map->peer_ref && !strcmp(current_branch->refname,
1030 ref_map->peer_ref->name))
1031 die(_("Refusing to fetch into current branch %s "
1032 "of non-bare repository"), current_branch->refname);
1035 static int truncate_fetch_head(void)
1037 const char *filename = git_path_fetch_head(the_repository);
1038 FILE *fp = fopen_for_writing(filename);
1041 return error_errno(_("cannot open %s"), filename);
1046 static void set_option(struct transport *transport, const char *name, const char *value)
1048 int r = transport_set_option(transport, name, value);
1050 die(_("Option \"%s\" value \"%s\" is not valid for %s"),
1051 name, value, transport->url);
1053 warning(_("Option \"%s\" is ignored for %s\n"),
1054 name, transport->url);
1057 static struct transport *prepare_transport(struct remote *remote, int deepen)
1059 struct transport *transport;
1060 transport = transport_get(remote, NULL);
1061 transport_set_verbosity(transport, verbosity, progress);
1062 transport->family = family;
1064 set_option(transport, TRANS_OPT_UPLOADPACK, upload_pack);
1066 set_option(transport, TRANS_OPT_KEEP, "yes");
1068 set_option(transport, TRANS_OPT_DEPTH, depth);
1069 if (deepen && deepen_since)
1070 set_option(transport, TRANS_OPT_DEEPEN_SINCE, deepen_since);
1071 if (deepen && deepen_not.nr)
1072 set_option(transport, TRANS_OPT_DEEPEN_NOT,
1073 (const char *)&deepen_not);
1074 if (deepen_relative)
1075 set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, "yes");
1077 set_option(transport, TRANS_OPT_UPDATE_SHALLOW, "yes");
1078 if (filter_options.choice) {
1079 set_option(transport, TRANS_OPT_LIST_OBJECTS_FILTER,
1080 filter_options.filter_spec);
1081 set_option(transport, TRANS_OPT_FROM_PROMISOR, "1");
1086 static void backfill_tags(struct transport *transport, struct ref *ref_map)
1091 * Once we have set TRANS_OPT_DEEPEN_SINCE, we can't unset it
1092 * when remote helper is used (setting it to an empty string
1093 * is not unsetting). We could extend the remote helper
1094 * protocol for that, but for now, just force a new connection
1095 * without deepen-since. Similar story for deepen-not.
1097 cannot_reuse = transport->cannot_reuse ||
1098 deepen_since || deepen_not.nr;
1100 gsecondary = prepare_transport(transport->remote, 0);
1101 transport = gsecondary;
1104 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);
1105 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
1106 transport_set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, NULL);
1107 fetch_refs(transport, ref_map);
1110 transport_disconnect(gsecondary);
1115 static int do_fetch(struct transport *transport,
1118 struct string_list existing_refs = STRING_LIST_INIT_DUP;
1119 struct ref *ref_map;
1121 int autotags = (transport->remote->fetch_tags == 1);
1124 for_each_ref(add_existing, &existing_refs);
1126 if (tags == TAGS_DEFAULT) {
1127 if (transport->remote->fetch_tags == 2)
1129 if (transport->remote->fetch_tags == -1)
1133 /* if not appending, truncate FETCH_HEAD */
1134 if (!append && !dry_run) {
1135 retcode = truncate_fetch_head();
1140 ref_map = get_ref_map(transport, rs, tags, &autotags);
1141 if (!update_head_ok)
1142 check_not_current_branch(ref_map);
1144 for (rm = ref_map; rm; rm = rm->next) {
1146 struct string_list_item *peer_item =
1147 string_list_lookup(&existing_refs,
1148 rm->peer_ref->name);
1150 struct object_id *old_oid = peer_item->util;
1151 oidcpy(&rm->peer_ref->old_oid, old_oid);
1156 if (tags == TAGS_DEFAULT && autotags)
1157 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
1160 * We only prune based on refspecs specified
1161 * explicitly (via command line or configuration); we
1162 * don't care whether --tags was specified.
1165 prune_refs(rs, ref_map, transport->url);
1167 prune_refs(&transport->remote->fetch,
1172 if (fetch_refs(transport, ref_map)) {
1179 /* if neither --no-tags nor --tags was specified, do automated tag
1181 if (tags == TAGS_DEFAULT && autotags) {
1182 struct ref **tail = &ref_map;
1184 find_non_local_tags(transport, &ref_map, &tail);
1186 backfill_tags(transport, ref_map);
1191 string_list_clear(&existing_refs, 1);
1195 static int get_one_remote_for_fetch(struct remote *remote, void *priv)
1197 struct string_list *list = priv;
1198 if (!remote->skip_default_update)
1199 string_list_append(list, remote->name);
1203 struct remote_group_data {
1205 struct string_list *list;
1208 static int get_remote_group(const char *key, const char *value, void *priv)
1210 struct remote_group_data *g = priv;
1212 if (skip_prefix(key, "remotes.", &key) && !strcmp(key, g->name)) {
1213 /* split list by white space */
1215 size_t wordlen = strcspn(value, " \t\n");
1218 string_list_append_nodup(g->list,
1219 xstrndup(value, wordlen));
1220 value += wordlen + (value[wordlen] != '\0');
1227 static int add_remote_or_group(const char *name, struct string_list *list)
1229 int prev_nr = list->nr;
1230 struct remote_group_data g;
1231 g.name = name; g.list = list;
1233 git_config(get_remote_group, &g);
1234 if (list->nr == prev_nr) {
1235 struct remote *remote = remote_get(name);
1236 if (!remote_is_configured(remote, 0))
1238 string_list_append(list, remote->name);
1243 static void add_options_to_argv(struct argv_array *argv)
1246 argv_array_push(argv, "--dry-run");
1248 argv_array_push(argv, prune ? "--prune" : "--no-prune");
1249 if (prune_tags != -1)
1250 argv_array_push(argv, prune_tags ? "--prune-tags" : "--no-prune-tags");
1252 argv_array_push(argv, "--update-head-ok");
1254 argv_array_push(argv, "--force");
1256 argv_array_push(argv, "--keep");
1257 if (recurse_submodules == RECURSE_SUBMODULES_ON)
1258 argv_array_push(argv, "--recurse-submodules");
1259 else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
1260 argv_array_push(argv, "--recurse-submodules=on-demand");
1261 if (tags == TAGS_SET)
1262 argv_array_push(argv, "--tags");
1263 else if (tags == TAGS_UNSET)
1264 argv_array_push(argv, "--no-tags");
1266 argv_array_push(argv, "-v");
1268 argv_array_push(argv, "-v");
1269 else if (verbosity < 0)
1270 argv_array_push(argv, "-q");
1274 static int fetch_multiple(struct string_list *list)
1277 struct argv_array argv = ARGV_ARRAY_INIT;
1279 if (!append && !dry_run) {
1280 int errcode = truncate_fetch_head();
1285 argv_array_pushl(&argv, "fetch", "--append", NULL);
1286 add_options_to_argv(&argv);
1288 for (i = 0; i < list->nr; i++) {
1289 const char *name = list->items[i].string;
1290 argv_array_push(&argv, name);
1292 printf(_("Fetching %s\n"), name);
1293 if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) {
1294 error(_("Could not fetch %s"), name);
1297 argv_array_pop(&argv);
1300 argv_array_clear(&argv);
1305 * Fetching from the promisor remote should use the given filter-spec
1306 * or inherit the default filter-spec from the config.
1308 static inline void fetch_one_setup_partial(struct remote *remote)
1311 * Explicit --no-filter argument overrides everything, regardless
1312 * of any prior partial clones and fetches.
1314 if (filter_options.no_filter)
1318 * If no prior partial clone/fetch and the current fetch DID NOT
1319 * request a partial-fetch, do a normal fetch.
1321 if (!repository_format_partial_clone && !filter_options.choice)
1325 * If this is the FIRST partial-fetch request, we enable partial
1326 * on this repo and remember the given filter-spec as the default
1327 * for subsequent fetches to this remote.
1329 if (!repository_format_partial_clone && filter_options.choice) {
1330 partial_clone_register(remote->name, &filter_options);
1335 * We are currently limited to only ONE promisor remote and only
1336 * allow partial-fetches from the promisor remote.
1338 if (strcmp(remote->name, repository_format_partial_clone)) {
1339 if (filter_options.choice)
1340 die(_("--filter can only be used with the remote configured in core.partialClone"));
1345 * Do a partial-fetch from the promisor remote using either the
1346 * explicitly given filter-spec or inherit the filter-spec from
1349 if (!filter_options.choice)
1350 partial_clone_get_default_filter_spec(&filter_options);
1354 static int fetch_one(struct remote *remote, int argc, const char **argv, int prune_tags_ok)
1356 struct refspec rs = REFSPEC_INIT_FETCH;
1359 int maybe_prune_tags;
1360 int remote_via_config = remote_is_configured(remote, 0);
1363 die(_("No remote repository specified. Please, specify either a URL or a\n"
1364 "remote name from which new revisions should be fetched."));
1366 gtransport = prepare_transport(remote, 1);
1369 /* no command line request */
1370 if (0 <= remote->prune)
1371 prune = remote->prune;
1372 else if (0 <= fetch_prune_config)
1373 prune = fetch_prune_config;
1375 prune = PRUNE_BY_DEFAULT;
1378 if (prune_tags < 0) {
1379 /* no command line request */
1380 if (0 <= remote->prune_tags)
1381 prune_tags = remote->prune_tags;
1382 else if (0 <= fetch_prune_tags_config)
1383 prune_tags = fetch_prune_tags_config;
1385 prune_tags = PRUNE_TAGS_BY_DEFAULT;
1388 maybe_prune_tags = prune_tags_ok && prune_tags;
1389 if (maybe_prune_tags && remote_via_config)
1390 refspec_append(&remote->fetch, TAG_REFSPEC);
1392 if (maybe_prune_tags && (argc || !remote_via_config))
1393 refspec_append(&rs, TAG_REFSPEC);
1395 for (i = 0; i < argc; i++) {
1396 if (!strcmp(argv[i], "tag")) {
1400 die(_("You need to specify a tag name."));
1402 tag = xstrfmt("refs/tags/%s:refs/tags/%s",
1404 refspec_append(&rs, tag);
1407 refspec_append(&rs, argv[i]);
1411 if (server_options.nr)
1412 gtransport->server_options = &server_options;
1414 sigchain_push_common(unlock_pack_on_signal);
1415 atexit(unlock_pack);
1416 exit_code = do_fetch(gtransport, &rs);
1418 transport_disconnect(gtransport);
1423 int cmd_fetch(int argc, const char **argv, const char *prefix)
1426 struct string_list list = STRING_LIST_INIT_DUP;
1427 struct remote *remote = NULL;
1429 int prune_tags_ok = 1;
1430 struct argv_array argv_gc_auto = ARGV_ARRAY_INIT;
1432 packet_trace_identity("fetch");
1434 fetch_if_missing = 0;
1436 /* Record the command line for the reflog */
1437 strbuf_addstr(&default_rla, "fetch");
1438 for (i = 1; i < argc; i++)
1439 strbuf_addf(&default_rla, " %s", argv[i]);
1441 config_from_gitmodules(gitmodules_fetch_config, NULL);
1442 git_config(git_fetch_config, NULL);
1444 argc = parse_options(argc, argv, prefix,
1445 builtin_fetch_options, builtin_fetch_usage, 0);
1447 if (deepen_relative) {
1448 if (deepen_relative < 0)
1449 die(_("Negative depth in --deepen is not supported"));
1451 die(_("--deepen and --depth are mutually exclusive"));
1452 depth = xstrfmt("%d", deepen_relative);
1456 die(_("--depth and --unshallow cannot be used together"));
1457 else if (!is_repository_shallow(the_repository))
1458 die(_("--unshallow on a complete repository does not make sense"));
1460 depth = xstrfmt("%d", INFINITE_DEPTH);
1463 /* no need to be strict, transport_set_option() will validate it again */
1464 if (depth && atoi(depth) < 1)
1465 die(_("depth %s is not a positive number"), depth);
1466 if (depth || deepen_since || deepen_not.nr)
1469 if (filter_options.choice && !repository_format_partial_clone)
1470 die("--filter can only be used when extensions.partialClone is set");
1474 die(_("fetch --all does not take a repository argument"));
1476 die(_("fetch --all does not make sense with refspecs"));
1477 (void) for_each_remote(get_one_remote_for_fetch, &list);
1478 } else if (argc == 0) {
1479 /* No arguments -- use default remote */
1480 remote = remote_get(NULL);
1481 } else if (multiple) {
1482 /* All arguments are assumed to be remotes or groups */
1483 for (i = 0; i < argc; i++)
1484 if (!add_remote_or_group(argv[i], &list))
1485 die(_("No such remote or remote group: %s"), argv[i]);
1487 /* Single remote or group */
1488 (void) add_remote_or_group(argv[0], &list);
1490 /* More than one remote */
1492 die(_("Fetching a group and specifying refspecs does not make sense"));
1494 /* Zero or one remotes */
1495 remote = remote_get(argv[0]);
1496 prune_tags_ok = (argc == 1);
1503 if (filter_options.choice || repository_format_partial_clone)
1504 fetch_one_setup_partial(remote);
1505 result = fetch_one(remote, argc, argv, prune_tags_ok);
1507 if (filter_options.choice)
1508 die(_("--filter can only be used with the remote configured in core.partialClone"));
1509 /* TODO should this also die if we have a previous partial-clone? */
1510 result = fetch_multiple(&list);
1513 if (!result && (recurse_submodules != RECURSE_SUBMODULES_OFF)) {
1514 struct argv_array options = ARGV_ARRAY_INIT;
1516 add_options_to_argv(&options);
1517 result = fetch_populated_submodules(the_repository,
1521 recurse_submodules_default,
1524 argv_array_clear(&options);
1527 string_list_clear(&list, 0);
1529 close_all_packs(the_repository->objects);
1531 argv_array_pushl(&argv_gc_auto, "gc", "--auto", NULL);
1533 argv_array_push(&argv_gc_auto, "--quiet");
1534 run_command_v_opt(argv_gc_auto.argv, RUN_GIT_CMD);
1535 argv_array_clear(&argv_gc_auto);