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;
66 static int git_fetch_config(const char *k, const char *v, void *cb)
68 if (!strcmp(k, "fetch.prune")) {
69 fetch_prune_config = git_config_bool(k, v);
73 if (!strcmp(k, "fetch.prunetags")) {
74 fetch_prune_tags_config = git_config_bool(k, v);
78 if (!strcmp(k, "submodule.recurse")) {
79 int r = git_config_bool(k, v) ?
80 RECURSE_SUBMODULES_ON : RECURSE_SUBMODULES_OFF;
81 recurse_submodules = r;
84 if (!strcmp(k, "submodule.fetchjobs")) {
85 max_children = parse_submodule_fetchjobs(k, v);
87 } else if (!strcmp(k, "fetch.recursesubmodules")) {
88 recurse_submodules = parse_fetch_recurse_submodules_arg(k, v);
92 return git_default_config(k, v, cb);
95 static int gitmodules_fetch_config(const char *var, const char *value, void *cb)
97 if (!strcmp(var, "submodule.fetchjobs")) {
98 max_children = parse_submodule_fetchjobs(var, value);
100 } else if (!strcmp(var, "fetch.recursesubmodules")) {
101 recurse_submodules = parse_fetch_recurse_submodules_arg(var, value);
108 static int parse_refmap_arg(const struct option *opt, const char *arg, int unset)
111 * "git fetch --refmap='' origin foo"
112 * can be used to tell the command not to store anywhere
114 refspec_append(&refmap, arg);
119 static struct option builtin_fetch_options[] = {
120 OPT__VERBOSITY(&verbosity),
121 OPT_BOOL(0, "all", &all,
122 N_("fetch from all remotes")),
123 OPT_BOOL('a', "append", &append,
124 N_("append to .git/FETCH_HEAD instead of overwriting")),
125 OPT_STRING(0, "upload-pack", &upload_pack, N_("path"),
126 N_("path to upload pack on remote end")),
127 OPT__FORCE(&force, N_("force overwrite of local branch"), 0),
128 OPT_BOOL('m', "multiple", &multiple,
129 N_("fetch from multiple remotes")),
130 OPT_SET_INT('t', "tags", &tags,
131 N_("fetch all tags and associated objects"), TAGS_SET),
132 OPT_SET_INT('n', NULL, &tags,
133 N_("do not fetch all tags (--no-tags)"), TAGS_UNSET),
134 OPT_INTEGER('j', "jobs", &max_children,
135 N_("number of submodules fetched in parallel")),
136 OPT_BOOL('p', "prune", &prune,
137 N_("prune remote-tracking branches no longer on remote")),
138 OPT_BOOL('P', "prune-tags", &prune_tags,
139 N_("prune local tags no longer on remote and clobber changed tags")),
140 { OPTION_CALLBACK, 0, "recurse-submodules", &recurse_submodules, N_("on-demand"),
141 N_("control recursive fetching of submodules"),
142 PARSE_OPT_OPTARG, option_fetch_parse_recurse_submodules },
143 OPT_BOOL(0, "dry-run", &dry_run,
145 OPT_BOOL('k', "keep", &keep, N_("keep downloaded pack")),
146 OPT_BOOL('u', "update-head-ok", &update_head_ok,
147 N_("allow updating of HEAD ref")),
148 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
149 OPT_STRING(0, "depth", &depth, N_("depth"),
150 N_("deepen history of shallow clone")),
151 OPT_STRING(0, "shallow-since", &deepen_since, N_("time"),
152 N_("deepen history of shallow repository based on time")),
153 OPT_STRING_LIST(0, "shallow-exclude", &deepen_not, N_("revision"),
154 N_("deepen history of shallow clone, excluding rev")),
155 OPT_INTEGER(0, "deepen", &deepen_relative,
156 N_("deepen history of shallow clone")),
157 { OPTION_SET_INT, 0, "unshallow", &unshallow, NULL,
158 N_("convert to a complete repository"),
159 PARSE_OPT_NONEG | PARSE_OPT_NOARG, NULL, 1 },
160 { OPTION_STRING, 0, "submodule-prefix", &submodule_prefix, N_("dir"),
161 N_("prepend this to submodule path output"), PARSE_OPT_HIDDEN },
162 { OPTION_CALLBACK, 0, "recurse-submodules-default",
163 &recurse_submodules_default, N_("on-demand"),
164 N_("default for recursive fetching of submodules "
165 "(lower priority than config files)"),
166 PARSE_OPT_HIDDEN, option_fetch_parse_recurse_submodules },
167 OPT_BOOL(0, "update-shallow", &update_shallow,
168 N_("accept refs that update .git/shallow")),
169 { OPTION_CALLBACK, 0, "refmap", NULL, N_("refmap"),
170 N_("specify fetch refmap"), PARSE_OPT_NONEG, parse_refmap_arg },
171 OPT_SET_INT('4', "ipv4", &family, N_("use IPv4 addresses only"),
172 TRANSPORT_FAMILY_IPV4),
173 OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"),
174 TRANSPORT_FAMILY_IPV6),
175 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
179 static void unlock_pack(void)
182 transport_unlock_pack(gtransport);
184 transport_unlock_pack(gsecondary);
187 static void unlock_pack_on_signal(int signo)
194 static void add_merge_config(struct ref **head,
195 const struct ref *remote_refs,
196 struct branch *branch,
201 for (i = 0; i < branch->merge_nr; i++) {
202 struct ref *rm, **old_tail = *tail;
203 struct refspec_item refspec;
205 for (rm = *head; rm; rm = rm->next) {
206 if (branch_merge_matches(branch, i, rm->name)) {
207 rm->fetch_head_status = FETCH_HEAD_MERGE;
215 * Not fetched to a remote-tracking branch? We need to fetch
216 * it anyway to allow this branch's "branch.$name.merge"
217 * to be honored by 'git pull', but we do not have to
218 * fail if branch.$name.merge is misconfigured to point
219 * at a nonexisting branch. If we were indeed called by
220 * 'git pull', it will notice the misconfiguration because
221 * there is no entry in the resulting FETCH_HEAD marked
224 memset(&refspec, 0, sizeof(refspec));
225 refspec.src = branch->merge[i]->src;
226 get_fetch_map(remote_refs, &refspec, tail, 1);
227 for (rm = *old_tail; rm; rm = rm->next)
228 rm->fetch_head_status = FETCH_HEAD_MERGE;
232 static int add_existing(const char *refname, const struct object_id *oid,
233 int flag, void *cbdata)
235 struct string_list *list = (struct string_list *)cbdata;
236 struct string_list_item *item = string_list_insert(list, refname);
237 struct object_id *old_oid = xmalloc(sizeof(*old_oid));
239 oidcpy(old_oid, oid);
240 item->util = old_oid;
244 static int will_fetch(struct ref **head, const unsigned char *sha1)
246 struct ref *rm = *head;
248 if (!hashcmp(rm->old_oid.hash, sha1))
255 static void find_non_local_tags(struct transport *transport,
259 struct string_list existing_refs = STRING_LIST_INIT_DUP;
260 struct string_list remote_refs = STRING_LIST_INIT_NODUP;
261 const struct ref *ref;
262 struct string_list_item *item = NULL;
264 for_each_ref(add_existing, &existing_refs);
265 for (ref = transport_get_remote_refs(transport, NULL); ref; ref = ref->next) {
266 if (!starts_with(ref->name, "refs/tags/"))
270 * The peeled ref always follows the matching base
271 * ref, so if we see a peeled ref that we don't want
272 * to fetch then we can mark the ref entry in the list
273 * as one to ignore by setting util to NULL.
275 if (ends_with(ref->name, "^{}")) {
277 !has_object_file_with_flags(&ref->old_oid,
278 OBJECT_INFO_QUICK) &&
279 !will_fetch(head, ref->old_oid.hash) &&
280 !has_sha1_file_with_flags(item->util,
281 OBJECT_INFO_QUICK) &&
282 !will_fetch(head, item->util))
289 * If item is non-NULL here, then we previously saw a
290 * ref not followed by a peeled reference, so we need
291 * to check if it is a lightweight tag that we want to
295 !has_sha1_file_with_flags(item->util, OBJECT_INFO_QUICK) &&
296 !will_fetch(head, item->util))
301 /* skip duplicates and refs that we already have */
302 if (string_list_has_string(&remote_refs, ref->name) ||
303 string_list_has_string(&existing_refs, ref->name))
306 item = string_list_insert(&remote_refs, ref->name);
307 item->util = (void *)&ref->old_oid;
309 string_list_clear(&existing_refs, 1);
312 * We may have a final lightweight tag that needs to be
313 * checked to see if it needs fetching.
316 !has_sha1_file_with_flags(item->util, OBJECT_INFO_QUICK) &&
317 !will_fetch(head, item->util))
321 * For all the tags in the remote_refs string list,
322 * add them to the list of refs to be fetched
324 for_each_string_list_item(item, &remote_refs) {
325 /* Unless we have already decided to ignore this item... */
328 struct ref *rm = alloc_ref(item->string);
329 rm->peer_ref = alloc_ref(item->string);
330 oidcpy(&rm->old_oid, item->util);
336 string_list_clear(&remote_refs, 0);
339 static struct ref *get_ref_map(struct transport *transport,
341 int tags, int *autotags)
345 struct ref *ref_map = NULL;
346 struct ref **tail = &ref_map;
347 struct argv_array ref_prefixes = ARGV_ARRAY_INIT;
349 /* opportunistically-updated references: */
350 struct ref *orefs = NULL, **oref_tail = &orefs;
352 const struct ref *remote_refs;
354 refspec_ref_prefixes(rs, &ref_prefixes);
356 remote_refs = transport_get_remote_refs(transport, &ref_prefixes);
358 argv_array_clear(&ref_prefixes);
361 struct refspec *fetch_refspec;
363 for (i = 0; i < rs->nr; i++) {
364 get_fetch_map(remote_refs, &rs->items[i], &tail, 0);
365 if (rs->items[i].dst && rs->items[i].dst[0])
368 /* Merge everything on the command line (but not --tags) */
369 for (rm = ref_map; rm; rm = rm->next)
370 rm->fetch_head_status = FETCH_HEAD_MERGE;
373 * For any refs that we happen to be fetching via
374 * command-line arguments, the destination ref might
375 * have been missing or have been different than the
376 * remote-tracking ref that would be derived from the
377 * configured refspec. In these cases, we want to
378 * take the opportunity to update their configured
379 * remote-tracking reference. However, we do not want
380 * to mention these entries in FETCH_HEAD at all, as
381 * they would simply be duplicates of existing
382 * entries, so we set them FETCH_HEAD_IGNORE below.
384 * We compute these entries now, based only on the
385 * refspecs specified on the command line. But we add
386 * them to the list following the refspecs resulting
387 * from the tags option so that one of the latter,
388 * which has FETCH_HEAD_NOT_FOR_MERGE, is not removed
389 * by ref_remove_duplicates() in favor of one of these
390 * opportunistic entries with FETCH_HEAD_IGNORE.
393 fetch_refspec = &refmap;
395 fetch_refspec = &transport->remote->fetch;
397 for (i = 0; i < fetch_refspec->nr; i++)
398 get_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1);
399 } else if (refmap.nr) {
400 die("--refmap option is only meaningful with command-line refspec(s).");
402 /* Use the defaults */
403 struct remote *remote = transport->remote;
404 struct branch *branch = branch_get(NULL);
405 int has_merge = branch_has_merge_config(branch);
408 /* Note: has_merge implies non-NULL branch->remote_name */
409 (has_merge && !strcmp(branch->remote_name, remote->name)))) {
410 for (i = 0; i < remote->fetch.nr; i++) {
411 get_fetch_map(remote_refs, &remote->fetch.items[i], &tail, 0);
412 if (remote->fetch.items[i].dst &&
413 remote->fetch.items[i].dst[0])
415 if (!i && !has_merge && ref_map &&
416 !remote->fetch.items[0].pattern)
417 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
420 * if the remote we're fetching from is the same
421 * as given in branch.<name>.remote, we add the
422 * ref given in branch.<name>.merge, too.
424 * Note: has_merge implies non-NULL branch->remote_name
427 !strcmp(branch->remote_name, remote->name))
428 add_merge_config(&ref_map, remote_refs, branch, &tail);
430 ref_map = get_remote_ref(remote_refs, "HEAD");
432 die(_("Couldn't find remote ref HEAD"));
433 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
434 tail = &ref_map->next;
438 if (tags == TAGS_SET)
439 /* also fetch all tags */
440 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
441 else if (tags == TAGS_DEFAULT && *autotags)
442 find_non_local_tags(transport, &ref_map, &tail);
444 /* Now append any refs to be updated opportunistically: */
446 for (rm = orefs; rm; rm = rm->next) {
447 rm->fetch_head_status = FETCH_HEAD_IGNORE;
451 return ref_remove_duplicates(ref_map);
454 #define STORE_REF_ERROR_OTHER 1
455 #define STORE_REF_ERROR_DF_CONFLICT 2
457 static int s_update_ref(const char *action,
462 char *rla = getenv("GIT_REFLOG_ACTION");
463 struct ref_transaction *transaction;
464 struct strbuf err = STRBUF_INIT;
465 int ret, df_conflict = 0;
470 rla = default_rla.buf;
471 msg = xstrfmt("%s: %s", rla, action);
473 transaction = ref_transaction_begin(&err);
475 ref_transaction_update(transaction, ref->name,
477 check_old ? &ref->old_oid : NULL,
481 ret = ref_transaction_commit(transaction, &err);
483 df_conflict = (ret == TRANSACTION_NAME_CONFLICT);
487 ref_transaction_free(transaction);
488 strbuf_release(&err);
492 ref_transaction_free(transaction);
493 error("%s", err.buf);
494 strbuf_release(&err);
496 return df_conflict ? STORE_REF_ERROR_DF_CONFLICT
497 : STORE_REF_ERROR_OTHER;
500 static int refcol_width = 10;
501 static int compact_format;
503 static void adjust_refcol_width(const struct ref *ref)
505 int max, rlen, llen, len;
507 /* uptodate lines are only shown on high verbosity level */
508 if (!verbosity && !oidcmp(&ref->peer_ref->old_oid, &ref->old_oid))
511 max = term_columns();
512 rlen = utf8_strwidth(prettify_refname(ref->name));
514 llen = utf8_strwidth(prettify_refname(ref->peer_ref->name));
517 * rough estimation to see if the output line is too long and
518 * should not be counted (we can't do precise calculation
519 * anyway because we don't know if the error explanation part
520 * will be printed in update_local_ref)
522 if (compact_format) {
526 len = 21 /* flag and summary */ + rlen + 4 /* -> */ + llen;
531 * Not precise calculation for compact mode because '*' can
532 * appear on the left hand side of '->' and shrink the column
535 if (refcol_width < rlen)
539 static void prepare_format_display(struct ref *ref_map)
542 const char *format = "full";
544 git_config_get_string_const("fetch.output", &format);
545 if (!strcasecmp(format, "full"))
547 else if (!strcasecmp(format, "compact"))
550 die(_("configuration fetch.output contains invalid value %s"),
553 for (rm = ref_map; rm; rm = rm->next) {
554 if (rm->status == REF_STATUS_REJECT_SHALLOW ||
556 !strcmp(rm->name, "HEAD"))
559 adjust_refcol_width(rm);
563 static void print_remote_to_local(struct strbuf *display,
564 const char *remote, const char *local)
566 strbuf_addf(display, "%-*s -> %s", refcol_width, remote, local);
569 static int find_and_replace(struct strbuf *haystack,
571 const char *placeholder)
573 const char *p = strstr(haystack->buf, needle);
579 if (p > haystack->buf && p[-1] != '/')
583 nlen = strlen(needle);
584 if (plen > nlen && p[nlen] != '/')
587 strbuf_splice(haystack, p - haystack->buf, nlen,
588 placeholder, strlen(placeholder));
592 static void print_compact(struct strbuf *display,
593 const char *remote, const char *local)
595 struct strbuf r = STRBUF_INIT;
596 struct strbuf l = STRBUF_INIT;
598 if (!strcmp(remote, local)) {
599 strbuf_addf(display, "%-*s -> *", refcol_width, remote);
603 strbuf_addstr(&r, remote);
604 strbuf_addstr(&l, local);
606 if (!find_and_replace(&r, local, "*"))
607 find_and_replace(&l, remote, "*");
608 print_remote_to_local(display, r.buf, l.buf);
614 static void format_display(struct strbuf *display, char code,
615 const char *summary, const char *error,
616 const char *remote, const char *local,
619 int width = (summary_width + strlen(summary) - gettext_width(summary));
621 strbuf_addf(display, "%c %-*s ", code, width, summary);
623 print_remote_to_local(display, remote, local);
625 print_compact(display, remote, local);
627 strbuf_addf(display, " (%s)", error);
630 static int update_local_ref(struct ref *ref,
632 const struct ref *remote_ref,
633 struct strbuf *display,
636 struct commit *current = NULL, *updated;
637 enum object_type type;
638 struct branch *current_branch = branch_get(NULL);
639 const char *pretty_ref = prettify_refname(ref->name);
641 type = oid_object_info(&ref->new_oid, NULL);
643 die(_("object %s not found"), oid_to_hex(&ref->new_oid));
645 if (!oidcmp(&ref->old_oid, &ref->new_oid)) {
647 format_display(display, '=', _("[up to date]"), NULL,
648 remote, pretty_ref, summary_width);
652 if (current_branch &&
653 !strcmp(ref->name, current_branch->name) &&
654 !(update_head_ok || is_bare_repository()) &&
655 !is_null_oid(&ref->old_oid)) {
657 * If this is the head, and it's not okay to update
658 * the head, and the old value of the head isn't empty...
660 format_display(display, '!', _("[rejected]"),
661 _("can't fetch in current branch"),
662 remote, pretty_ref, summary_width);
666 if (!is_null_oid(&ref->old_oid) &&
667 starts_with(ref->name, "refs/tags/")) {
669 r = s_update_ref("updating tag", ref, 0);
670 format_display(display, r ? '!' : 't', _("[tag update]"),
671 r ? _("unable to update local ref") : NULL,
672 remote, pretty_ref, summary_width);
676 current = lookup_commit_reference_gently(&ref->old_oid, 1);
677 updated = lookup_commit_reference_gently(&ref->new_oid, 1);
678 if (!current || !updated) {
683 * Nicely describe the new ref we're fetching.
684 * Base this on the remote's ref name, as it's
685 * more likely to follow a standard layout.
687 const char *name = remote_ref ? remote_ref->name : "";
688 if (starts_with(name, "refs/tags/")) {
690 what = _("[new tag]");
691 } else if (starts_with(name, "refs/heads/")) {
692 msg = "storing head";
693 what = _("[new branch]");
696 what = _("[new ref]");
699 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
700 (recurse_submodules != RECURSE_SUBMODULES_ON))
701 check_for_new_submodule_commits(&ref->new_oid);
702 r = s_update_ref(msg, ref, 0);
703 format_display(display, r ? '!' : '*', what,
704 r ? _("unable to update local ref") : NULL,
705 remote, pretty_ref, summary_width);
709 if (in_merge_bases(current, updated)) {
710 struct strbuf quickref = STRBUF_INIT;
712 strbuf_add_unique_abbrev(&quickref, ¤t->object.oid, DEFAULT_ABBREV);
713 strbuf_addstr(&quickref, "..");
714 strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV);
715 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
716 (recurse_submodules != RECURSE_SUBMODULES_ON))
717 check_for_new_submodule_commits(&ref->new_oid);
718 r = s_update_ref("fast-forward", ref, 1);
719 format_display(display, r ? '!' : ' ', quickref.buf,
720 r ? _("unable to update local ref") : NULL,
721 remote, pretty_ref, summary_width);
722 strbuf_release(&quickref);
724 } else if (force || ref->force) {
725 struct strbuf quickref = STRBUF_INIT;
727 strbuf_add_unique_abbrev(&quickref, ¤t->object.oid, DEFAULT_ABBREV);
728 strbuf_addstr(&quickref, "...");
729 strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV);
730 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
731 (recurse_submodules != RECURSE_SUBMODULES_ON))
732 check_for_new_submodule_commits(&ref->new_oid);
733 r = s_update_ref("forced-update", ref, 1);
734 format_display(display, r ? '!' : '+', quickref.buf,
735 r ? _("unable to update local ref") : _("forced update"),
736 remote, pretty_ref, summary_width);
737 strbuf_release(&quickref);
740 format_display(display, '!', _("[rejected]"), _("non-fast-forward"),
741 remote, pretty_ref, summary_width);
746 static int iterate_ref_map(void *cb_data, struct object_id *oid)
748 struct ref **rm = cb_data;
749 struct ref *ref = *rm;
751 while (ref && ref->status == REF_STATUS_REJECT_SHALLOW)
754 return -1; /* end of the list */
756 oidcpy(oid, &ref->old_oid);
760 static int store_updated_refs(const char *raw_url, const char *remote_name,
764 struct commit *commit;
765 int url_len, i, rc = 0;
766 struct strbuf note = STRBUF_INIT;
767 const char *what, *kind;
770 const char *filename = dry_run ? "/dev/null" : git_path_fetch_head();
772 int summary_width = transport_summary_width(ref_map);
774 fp = fopen(filename, "a");
776 return error_errno(_("cannot open %s"), filename);
779 url = transport_anonymize_url(raw_url);
781 url = xstrdup("foreign");
784 if (check_connected(iterate_ref_map, &rm, NULL)) {
785 rc = error(_("%s did not send all necessary objects\n"), url);
789 prepare_format_display(ref_map);
792 * We do a pass for each fetch_head_status type in their enum order, so
793 * merged entries are written before not-for-merge. That lets readers
794 * use FETCH_HEAD as a refname to refer to the ref to be merged.
796 for (want_status = FETCH_HEAD_MERGE;
797 want_status <= FETCH_HEAD_IGNORE;
799 for (rm = ref_map; rm; rm = rm->next) {
800 struct ref *ref = NULL;
801 const char *merge_status_marker = "";
803 if (rm->status == REF_STATUS_REJECT_SHALLOW) {
804 if (want_status == FETCH_HEAD_MERGE)
805 warning(_("reject %s because shallow roots are not allowed to be updated"),
806 rm->peer_ref ? rm->peer_ref->name : rm->name);
810 commit = lookup_commit_reference_gently(&rm->old_oid,
813 rm->fetch_head_status = FETCH_HEAD_NOT_FOR_MERGE;
815 if (rm->fetch_head_status != want_status)
819 ref = alloc_ref(rm->peer_ref->name);
820 oidcpy(&ref->old_oid, &rm->peer_ref->old_oid);
821 oidcpy(&ref->new_oid, &rm->old_oid);
822 ref->force = rm->peer_ref->force;
826 if (!strcmp(rm->name, "HEAD")) {
830 else if (starts_with(rm->name, "refs/heads/")) {
832 what = rm->name + 11;
834 else if (starts_with(rm->name, "refs/tags/")) {
836 what = rm->name + 10;
838 else if (starts_with(rm->name, "refs/remotes/")) {
839 kind = "remote-tracking branch";
840 what = rm->name + 13;
847 url_len = strlen(url);
848 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
851 if (4 < i && !strncmp(".git", url + i - 3, 4))
857 strbuf_addf(¬e, "%s ", kind);
858 strbuf_addf(¬e, "'%s' of ", what);
860 switch (rm->fetch_head_status) {
861 case FETCH_HEAD_NOT_FOR_MERGE:
862 merge_status_marker = "not-for-merge";
864 case FETCH_HEAD_MERGE:
865 fprintf(fp, "%s\t%s\t%s",
866 oid_to_hex(&rm->old_oid),
869 for (i = 0; i < url_len; ++i)
877 /* do not write anything to FETCH_HEAD */
883 rc |= update_local_ref(ref, what, rm, ¬e,
887 format_display(¬e, '*',
888 *kind ? kind : "branch", NULL,
889 *what ? what : "HEAD",
890 "FETCH_HEAD", summary_width);
892 if (verbosity >= 0 && !shown_url) {
893 fprintf(stderr, _("From %.*s\n"),
898 fprintf(stderr, " %s\n", note.buf);
903 if (rc & STORE_REF_ERROR_DF_CONFLICT)
904 error(_("some local refs could not be updated; try running\n"
905 " 'git remote prune %s' to remove any old, conflicting "
906 "branches"), remote_name);
909 strbuf_release(¬e);
916 * We would want to bypass the object transfer altogether if
917 * everything we are going to fetch already exists and is connected
920 static int quickfetch(struct ref *ref_map)
922 struct ref *rm = ref_map;
923 struct check_connected_options opt = CHECK_CONNECTED_INIT;
926 * If we are deepening a shallow clone we already have these
927 * objects reachable. Running rev-list here will return with
928 * a good (0) exit status and we'll bypass the fetch that we
929 * really need to perform. Claiming failure now will ensure
930 * we perform the network exchange to deepen our history.
935 return check_connected(iterate_ref_map, &rm, &opt);
938 static int fetch_refs(struct transport *transport, struct ref *ref_map)
940 int ret = quickfetch(ref_map);
942 ret = transport_fetch_refs(transport, ref_map);
944 ret |= store_updated_refs(transport->url,
945 transport->remote->name,
947 transport_unlock_pack(transport);
951 static int prune_refs(struct refspec *rs, struct ref *ref_map,
954 int url_len, i, result = 0;
955 struct ref *ref, *stale_refs = get_stale_heads(rs, ref_map);
957 int summary_width = transport_summary_width(stale_refs);
958 const char *dangling_msg = dry_run
959 ? _(" (%s will become dangling)")
960 : _(" (%s has become dangling)");
963 url = transport_anonymize_url(raw_url);
965 url = xstrdup("foreign");
967 url_len = strlen(url);
968 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
972 if (4 < i && !strncmp(".git", url + i - 3, 4))
976 struct string_list refnames = STRING_LIST_INIT_NODUP;
978 for (ref = stale_refs; ref; ref = ref->next)
979 string_list_append(&refnames, ref->name);
981 result = delete_refs("fetch: prune", &refnames, 0);
982 string_list_clear(&refnames, 0);
985 if (verbosity >= 0) {
986 for (ref = stale_refs; ref; ref = ref->next) {
987 struct strbuf sb = STRBUF_INIT;
989 fprintf(stderr, _("From %.*s\n"), url_len, url);
992 format_display(&sb, '-', _("[deleted]"), NULL,
993 _("(none)"), prettify_refname(ref->name),
995 fprintf(stderr, " %s\n",sb.buf);
997 warn_dangling_symref(stderr, dangling_msg, ref->name);
1002 free_refs(stale_refs);
1006 static void check_not_current_branch(struct ref *ref_map)
1008 struct branch *current_branch = branch_get(NULL);
1010 if (is_bare_repository() || !current_branch)
1013 for (; ref_map; ref_map = ref_map->next)
1014 if (ref_map->peer_ref && !strcmp(current_branch->refname,
1015 ref_map->peer_ref->name))
1016 die(_("Refusing to fetch into current branch %s "
1017 "of non-bare repository"), current_branch->refname);
1020 static int truncate_fetch_head(void)
1022 const char *filename = git_path_fetch_head();
1023 FILE *fp = fopen_for_writing(filename);
1026 return error_errno(_("cannot open %s"), filename);
1031 static void set_option(struct transport *transport, const char *name, const char *value)
1033 int r = transport_set_option(transport, name, value);
1035 die(_("Option \"%s\" value \"%s\" is not valid for %s"),
1036 name, value, transport->url);
1038 warning(_("Option \"%s\" is ignored for %s\n"),
1039 name, transport->url);
1042 static struct transport *prepare_transport(struct remote *remote, int deepen)
1044 struct transport *transport;
1045 transport = transport_get(remote, NULL);
1046 transport_set_verbosity(transport, verbosity, progress);
1047 transport->family = family;
1049 set_option(transport, TRANS_OPT_UPLOADPACK, upload_pack);
1051 set_option(transport, TRANS_OPT_KEEP, "yes");
1053 set_option(transport, TRANS_OPT_DEPTH, depth);
1054 if (deepen && deepen_since)
1055 set_option(transport, TRANS_OPT_DEEPEN_SINCE, deepen_since);
1056 if (deepen && deepen_not.nr)
1057 set_option(transport, TRANS_OPT_DEEPEN_NOT,
1058 (const char *)&deepen_not);
1059 if (deepen_relative)
1060 set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, "yes");
1062 set_option(transport, TRANS_OPT_UPDATE_SHALLOW, "yes");
1063 if (filter_options.choice) {
1064 set_option(transport, TRANS_OPT_LIST_OBJECTS_FILTER,
1065 filter_options.filter_spec);
1066 set_option(transport, TRANS_OPT_FROM_PROMISOR, "1");
1071 static void backfill_tags(struct transport *transport, struct ref *ref_map)
1076 * Once we have set TRANS_OPT_DEEPEN_SINCE, we can't unset it
1077 * when remote helper is used (setting it to an empty string
1078 * is not unsetting). We could extend the remote helper
1079 * protocol for that, but for now, just force a new connection
1080 * without deepen-since. Similar story for deepen-not.
1082 cannot_reuse = transport->cannot_reuse ||
1083 deepen_since || deepen_not.nr;
1085 gsecondary = prepare_transport(transport->remote, 0);
1086 transport = gsecondary;
1089 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);
1090 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
1091 transport_set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, NULL);
1092 fetch_refs(transport, ref_map);
1095 transport_disconnect(gsecondary);
1100 static int do_fetch(struct transport *transport,
1103 struct string_list existing_refs = STRING_LIST_INIT_DUP;
1104 struct ref *ref_map;
1106 int autotags = (transport->remote->fetch_tags == 1);
1109 for_each_ref(add_existing, &existing_refs);
1111 if (tags == TAGS_DEFAULT) {
1112 if (transport->remote->fetch_tags == 2)
1114 if (transport->remote->fetch_tags == -1)
1118 /* if not appending, truncate FETCH_HEAD */
1119 if (!append && !dry_run) {
1120 retcode = truncate_fetch_head();
1125 ref_map = get_ref_map(transport, rs, tags, &autotags);
1126 if (!update_head_ok)
1127 check_not_current_branch(ref_map);
1129 for (rm = ref_map; rm; rm = rm->next) {
1131 struct string_list_item *peer_item =
1132 string_list_lookup(&existing_refs,
1133 rm->peer_ref->name);
1135 struct object_id *old_oid = peer_item->util;
1136 oidcpy(&rm->peer_ref->old_oid, old_oid);
1141 if (tags == TAGS_DEFAULT && autotags)
1142 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
1145 * We only prune based on refspecs specified
1146 * explicitly (via command line or configuration); we
1147 * don't care whether --tags was specified.
1150 prune_refs(rs, ref_map, transport->url);
1152 prune_refs(&transport->remote->fetch,
1157 if (fetch_refs(transport, ref_map)) {
1164 /* if neither --no-tags nor --tags was specified, do automated tag
1166 if (tags == TAGS_DEFAULT && autotags) {
1167 struct ref **tail = &ref_map;
1169 find_non_local_tags(transport, &ref_map, &tail);
1171 backfill_tags(transport, ref_map);
1176 string_list_clear(&existing_refs, 1);
1180 static int get_one_remote_for_fetch(struct remote *remote, void *priv)
1182 struct string_list *list = priv;
1183 if (!remote->skip_default_update)
1184 string_list_append(list, remote->name);
1188 struct remote_group_data {
1190 struct string_list *list;
1193 static int get_remote_group(const char *key, const char *value, void *priv)
1195 struct remote_group_data *g = priv;
1197 if (skip_prefix(key, "remotes.", &key) && !strcmp(key, g->name)) {
1198 /* split list by white space */
1200 size_t wordlen = strcspn(value, " \t\n");
1203 string_list_append_nodup(g->list,
1204 xstrndup(value, wordlen));
1205 value += wordlen + (value[wordlen] != '\0');
1212 static int add_remote_or_group(const char *name, struct string_list *list)
1214 int prev_nr = list->nr;
1215 struct remote_group_data g;
1216 g.name = name; g.list = list;
1218 git_config(get_remote_group, &g);
1219 if (list->nr == prev_nr) {
1220 struct remote *remote = remote_get(name);
1221 if (!remote_is_configured(remote, 0))
1223 string_list_append(list, remote->name);
1228 static void add_options_to_argv(struct argv_array *argv)
1231 argv_array_push(argv, "--dry-run");
1233 argv_array_push(argv, prune ? "--prune" : "--no-prune");
1234 if (prune_tags != -1)
1235 argv_array_push(argv, prune_tags ? "--prune-tags" : "--no-prune-tags");
1237 argv_array_push(argv, "--update-head-ok");
1239 argv_array_push(argv, "--force");
1241 argv_array_push(argv, "--keep");
1242 if (recurse_submodules == RECURSE_SUBMODULES_ON)
1243 argv_array_push(argv, "--recurse-submodules");
1244 else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
1245 argv_array_push(argv, "--recurse-submodules=on-demand");
1246 if (tags == TAGS_SET)
1247 argv_array_push(argv, "--tags");
1248 else if (tags == TAGS_UNSET)
1249 argv_array_push(argv, "--no-tags");
1251 argv_array_push(argv, "-v");
1253 argv_array_push(argv, "-v");
1254 else if (verbosity < 0)
1255 argv_array_push(argv, "-q");
1259 static int fetch_multiple(struct string_list *list)
1262 struct argv_array argv = ARGV_ARRAY_INIT;
1264 if (!append && !dry_run) {
1265 int errcode = truncate_fetch_head();
1270 argv_array_pushl(&argv, "fetch", "--append", NULL);
1271 add_options_to_argv(&argv);
1273 for (i = 0; i < list->nr; i++) {
1274 const char *name = list->items[i].string;
1275 argv_array_push(&argv, name);
1277 printf(_("Fetching %s\n"), name);
1278 if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) {
1279 error(_("Could not fetch %s"), name);
1282 argv_array_pop(&argv);
1285 argv_array_clear(&argv);
1290 * Fetching from the promisor remote should use the given filter-spec
1291 * or inherit the default filter-spec from the config.
1293 static inline void fetch_one_setup_partial(struct remote *remote)
1296 * Explicit --no-filter argument overrides everything, regardless
1297 * of any prior partial clones and fetches.
1299 if (filter_options.no_filter)
1303 * If no prior partial clone/fetch and the current fetch DID NOT
1304 * request a partial-fetch, do a normal fetch.
1306 if (!repository_format_partial_clone && !filter_options.choice)
1310 * If this is the FIRST partial-fetch request, we enable partial
1311 * on this repo and remember the given filter-spec as the default
1312 * for subsequent fetches to this remote.
1314 if (!repository_format_partial_clone && filter_options.choice) {
1315 partial_clone_register(remote->name, &filter_options);
1320 * We are currently limited to only ONE promisor remote and only
1321 * allow partial-fetches from the promisor remote.
1323 if (strcmp(remote->name, repository_format_partial_clone)) {
1324 if (filter_options.choice)
1325 die(_("--filter can only be used with the remote configured in core.partialClone"));
1330 * Do a partial-fetch from the promisor remote using either the
1331 * explicitly given filter-spec or inherit the filter-spec from
1334 if (!filter_options.choice)
1335 partial_clone_get_default_filter_spec(&filter_options);
1339 static int fetch_one(struct remote *remote, int argc, const char **argv, int prune_tags_ok)
1341 struct refspec rs = REFSPEC_INIT_FETCH;
1344 int maybe_prune_tags;
1345 int remote_via_config = remote_is_configured(remote, 0);
1348 die(_("No remote repository specified. Please, specify either a URL or a\n"
1349 "remote name from which new revisions should be fetched."));
1351 gtransport = prepare_transport(remote, 1);
1354 /* no command line request */
1355 if (0 <= remote->prune)
1356 prune = remote->prune;
1357 else if (0 <= fetch_prune_config)
1358 prune = fetch_prune_config;
1360 prune = PRUNE_BY_DEFAULT;
1363 if (prune_tags < 0) {
1364 /* no command line request */
1365 if (0 <= remote->prune_tags)
1366 prune_tags = remote->prune_tags;
1367 else if (0 <= fetch_prune_tags_config)
1368 prune_tags = fetch_prune_tags_config;
1370 prune_tags = PRUNE_TAGS_BY_DEFAULT;
1373 maybe_prune_tags = prune_tags_ok && prune_tags;
1374 if (maybe_prune_tags && remote_via_config)
1375 refspec_append(&remote->fetch, TAG_REFSPEC);
1377 if (maybe_prune_tags && (argc || !remote_via_config))
1378 refspec_append(&rs, TAG_REFSPEC);
1380 for (i = 0; i < argc; i++) {
1381 if (!strcmp(argv[i], "tag")) {
1385 die(_("You need to specify a tag name."));
1387 tag = xstrfmt("refs/tags/%s:refs/tags/%s",
1389 refspec_append(&rs, tag);
1392 refspec_append(&rs, argv[i]);
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 config_from_gitmodules(gitmodules_fetch_config, NULL);
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);