6 #include "repository.h"
10 #include "string-list.h"
12 #include "transport.h"
13 #include "run-command.h"
14 #include "parse-options.h"
16 #include "submodule-config.h"
17 #include "submodule.h"
18 #include "connected.h"
19 #include "argv-array.h"
23 static const char * const builtin_fetch_usage[] = {
24 N_("git fetch [<options>] [<repository> [<refspec>...]]"),
25 N_("git fetch [<options>] <group>"),
26 N_("git fetch --multiple [<options>] [(<repository> | <group>)...]"),
27 N_("git fetch --all [<options>]"),
37 static int fetch_prune_config = -1; /* unspecified */
38 static int prune = -1; /* unspecified */
39 #define PRUNE_BY_DEFAULT 0 /* do we prune by default? */
41 static int fetch_prune_tags_config = -1; /* unspecified */
42 static int prune_tags = -1; /* unspecified */
43 #define PRUNE_TAGS_BY_DEFAULT 0 /* do we prune tags by default? */
45 static int all, append, dry_run, force, keep, multiple, update_head_ok, verbosity, deepen_relative;
46 static int progress = -1;
47 static int tags = TAGS_DEFAULT, unshallow, update_shallow, deepen;
48 static int max_children = 1;
49 static enum transport_family family;
50 static const char *depth;
51 static const char *deepen_since;
52 static const char *upload_pack;
53 static struct string_list deepen_not = STRING_LIST_INIT_NODUP;
54 static struct strbuf default_rla = STRBUF_INIT;
55 static struct transport *gtransport;
56 static struct transport *gsecondary;
57 static const char *submodule_prefix = "";
58 static int recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
59 static int recurse_submodules_default = RECURSE_SUBMODULES_ON_DEMAND;
60 static int shown_url = 0;
61 static int refmap_alloc, refmap_nr;
62 static const char **refmap_array;
64 static int git_fetch_config(const char *k, const char *v, void *cb)
66 if (!strcmp(k, "fetch.prune")) {
67 fetch_prune_config = git_config_bool(k, v);
71 if (!strcmp(k, "fetch.prunetags")) {
72 fetch_prune_tags_config = git_config_bool(k, v);
76 if (!strcmp(k, "submodule.recurse")) {
77 int r = git_config_bool(k, v) ?
78 RECURSE_SUBMODULES_ON : RECURSE_SUBMODULES_OFF;
79 recurse_submodules = r;
82 if (!strcmp(k, "submodule.fetchjobs")) {
83 max_children = parse_submodule_fetchjobs(k, v);
85 } else if (!strcmp(k, "fetch.recursesubmodules")) {
86 recurse_submodules = parse_fetch_recurse_submodules_arg(k, v);
90 return git_default_config(k, v, cb);
93 static int gitmodules_fetch_config(const char *var, const char *value, void *cb)
95 if (!strcmp(var, "submodule.fetchjobs")) {
96 max_children = parse_submodule_fetchjobs(var, value);
98 } else if (!strcmp(var, "fetch.recursesubmodules")) {
99 recurse_submodules = parse_fetch_recurse_submodules_arg(var, value);
106 static int parse_refmap_arg(const struct option *opt, const char *arg, int unset)
108 ALLOC_GROW(refmap_array, refmap_nr + 1, refmap_alloc);
111 * "git fetch --refmap='' origin foo"
112 * can be used to tell the command not to store anywhere
115 refmap_array[refmap_nr++] = 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")),
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),
178 static void unlock_pack(void)
181 transport_unlock_pack(gtransport);
183 transport_unlock_pack(gsecondary);
186 static void unlock_pack_on_signal(int signo)
193 static void add_merge_config(struct ref **head,
194 const struct ref *remote_refs,
195 struct branch *branch,
200 for (i = 0; i < branch->merge_nr; i++) {
201 struct ref *rm, **old_tail = *tail;
202 struct refspec refspec;
204 for (rm = *head; rm; rm = rm->next) {
205 if (branch_merge_matches(branch, i, rm->name)) {
206 rm->fetch_head_status = FETCH_HEAD_MERGE;
214 * Not fetched to a remote-tracking branch? We need to fetch
215 * it anyway to allow this branch's "branch.$name.merge"
216 * to be honored by 'git pull', but we do not have to
217 * fail if branch.$name.merge is misconfigured to point
218 * at a nonexisting branch. If we were indeed called by
219 * 'git pull', it will notice the misconfiguration because
220 * there is no entry in the resulting FETCH_HEAD marked
223 memset(&refspec, 0, sizeof(refspec));
224 refspec.src = branch->merge[i]->src;
225 get_fetch_map(remote_refs, &refspec, tail, 1);
226 for (rm = *old_tail; rm; rm = rm->next)
227 rm->fetch_head_status = FETCH_HEAD_MERGE;
231 static int add_existing(const char *refname, const struct object_id *oid,
232 int flag, void *cbdata)
234 struct string_list *list = (struct string_list *)cbdata;
235 struct string_list_item *item = string_list_insert(list, refname);
236 struct object_id *old_oid = xmalloc(sizeof(*old_oid));
238 oidcpy(old_oid, oid);
239 item->util = old_oid;
243 static int will_fetch(struct ref **head, const unsigned char *sha1)
245 struct ref *rm = *head;
247 if (!hashcmp(rm->old_oid.hash, sha1))
254 static void find_non_local_tags(struct transport *transport,
258 struct string_list existing_refs = STRING_LIST_INIT_DUP;
259 struct string_list remote_refs = STRING_LIST_INIT_NODUP;
260 const struct ref *ref;
261 struct string_list_item *item = NULL;
263 for_each_ref(add_existing, &existing_refs);
264 for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
265 if (!starts_with(ref->name, "refs/tags/"))
269 * The peeled ref always follows the matching base
270 * ref, so if we see a peeled ref that we don't want
271 * to fetch then we can mark the ref entry in the list
272 * as one to ignore by setting util to NULL.
274 if (ends_with(ref->name, "^{}")) {
276 !has_object_file_with_flags(&ref->old_oid,
277 OBJECT_INFO_QUICK) &&
278 !will_fetch(head, ref->old_oid.hash) &&
279 !has_sha1_file_with_flags(item->util,
280 OBJECT_INFO_QUICK) &&
281 !will_fetch(head, item->util))
288 * If item is non-NULL here, then we previously saw a
289 * ref not followed by a peeled reference, so we need
290 * to check if it is a lightweight tag that we want to
294 !has_sha1_file_with_flags(item->util, OBJECT_INFO_QUICK) &&
295 !will_fetch(head, item->util))
300 /* skip duplicates and refs that we already have */
301 if (string_list_has_string(&remote_refs, ref->name) ||
302 string_list_has_string(&existing_refs, ref->name))
305 item = string_list_insert(&remote_refs, ref->name);
306 item->util = (void *)&ref->old_oid;
308 string_list_clear(&existing_refs, 1);
311 * We may have a final lightweight tag that needs to be
312 * checked to see if it needs fetching.
315 !has_sha1_file_with_flags(item->util, OBJECT_INFO_QUICK) &&
316 !will_fetch(head, item->util))
320 * For all the tags in the remote_refs string list,
321 * add them to the list of refs to be fetched
323 for_each_string_list_item(item, &remote_refs) {
324 /* Unless we have already decided to ignore this item... */
327 struct ref *rm = alloc_ref(item->string);
328 rm->peer_ref = alloc_ref(item->string);
329 oidcpy(&rm->old_oid, item->util);
335 string_list_clear(&remote_refs, 0);
338 static struct ref *get_ref_map(struct transport *transport,
339 struct refspec *refspecs, int refspec_count,
340 int tags, int *autotags)
344 struct ref *ref_map = NULL;
345 struct ref **tail = &ref_map;
347 /* opportunistically-updated references: */
348 struct ref *orefs = NULL, **oref_tail = &orefs;
350 const struct ref *remote_refs = transport_get_remote_refs(transport);
353 struct refspec *fetch_refspec;
354 int fetch_refspec_nr;
356 for (i = 0; i < refspec_count; i++) {
357 get_fetch_map(remote_refs, &refspecs[i], &tail, 0);
358 if (refspecs[i].dst && refspecs[i].dst[0])
361 /* Merge everything on the command line (but not --tags) */
362 for (rm = ref_map; rm; rm = rm->next)
363 rm->fetch_head_status = FETCH_HEAD_MERGE;
366 * For any refs that we happen to be fetching via
367 * command-line arguments, the destination ref might
368 * have been missing or have been different than the
369 * remote-tracking ref that would be derived from the
370 * configured refspec. In these cases, we want to
371 * take the opportunity to update their configured
372 * remote-tracking reference. However, we do not want
373 * to mention these entries in FETCH_HEAD at all, as
374 * they would simply be duplicates of existing
375 * entries, so we set them FETCH_HEAD_IGNORE below.
377 * We compute these entries now, based only on the
378 * refspecs specified on the command line. But we add
379 * them to the list following the refspecs resulting
380 * from the tags option so that one of the latter,
381 * which has FETCH_HEAD_NOT_FOR_MERGE, is not removed
382 * by ref_remove_duplicates() in favor of one of these
383 * opportunistic entries with FETCH_HEAD_IGNORE.
386 fetch_refspec = parse_fetch_refspec(refmap_nr, refmap_array);
387 fetch_refspec_nr = refmap_nr;
389 fetch_refspec = transport->remote->fetch;
390 fetch_refspec_nr = transport->remote->fetch_refspec_nr;
393 for (i = 0; i < fetch_refspec_nr; i++)
394 get_fetch_map(ref_map, &fetch_refspec[i], &oref_tail, 1);
395 } else if (refmap_array) {
396 die("--refmap option is only meaningful with command-line refspec(s).");
398 /* Use the defaults */
399 struct remote *remote = transport->remote;
400 struct branch *branch = branch_get(NULL);
401 int has_merge = branch_has_merge_config(branch);
403 (remote->fetch_refspec_nr ||
404 /* Note: has_merge implies non-NULL branch->remote_name */
405 (has_merge && !strcmp(branch->remote_name, remote->name)))) {
406 for (i = 0; i < remote->fetch_refspec_nr; i++) {
407 get_fetch_map(remote_refs, &remote->fetch[i], &tail, 0);
408 if (remote->fetch[i].dst &&
409 remote->fetch[i].dst[0])
411 if (!i && !has_merge && ref_map &&
412 !remote->fetch[0].pattern)
413 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
416 * if the remote we're fetching from is the same
417 * as given in branch.<name>.remote, we add the
418 * ref given in branch.<name>.merge, too.
420 * Note: has_merge implies non-NULL branch->remote_name
423 !strcmp(branch->remote_name, remote->name))
424 add_merge_config(&ref_map, remote_refs, branch, &tail);
426 ref_map = get_remote_ref(remote_refs, "HEAD");
428 die(_("Couldn't find remote ref HEAD"));
429 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
430 tail = &ref_map->next;
434 if (tags == TAGS_SET)
435 /* also fetch all tags */
436 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
437 else if (tags == TAGS_DEFAULT && *autotags)
438 find_non_local_tags(transport, &ref_map, &tail);
440 /* Now append any refs to be updated opportunistically: */
442 for (rm = orefs; rm; rm = rm->next) {
443 rm->fetch_head_status = FETCH_HEAD_IGNORE;
447 return ref_remove_duplicates(ref_map);
450 #define STORE_REF_ERROR_OTHER 1
451 #define STORE_REF_ERROR_DF_CONFLICT 2
453 static int s_update_ref(const char *action,
458 char *rla = getenv("GIT_REFLOG_ACTION");
459 struct ref_transaction *transaction;
460 struct strbuf err = STRBUF_INIT;
461 int ret, df_conflict = 0;
466 rla = default_rla.buf;
467 msg = xstrfmt("%s: %s", rla, action);
469 transaction = ref_transaction_begin(&err);
471 ref_transaction_update(transaction, ref->name,
473 check_old ? &ref->old_oid : NULL,
477 ret = ref_transaction_commit(transaction, &err);
479 df_conflict = (ret == TRANSACTION_NAME_CONFLICT);
483 ref_transaction_free(transaction);
484 strbuf_release(&err);
488 ref_transaction_free(transaction);
489 error("%s", err.buf);
490 strbuf_release(&err);
492 return df_conflict ? STORE_REF_ERROR_DF_CONFLICT
493 : STORE_REF_ERROR_OTHER;
496 static int refcol_width = 10;
497 static int compact_format;
499 static void adjust_refcol_width(const struct ref *ref)
501 int max, rlen, llen, len;
503 /* uptodate lines are only shown on high verbosity level */
504 if (!verbosity && !oidcmp(&ref->peer_ref->old_oid, &ref->old_oid))
507 max = term_columns();
508 rlen = utf8_strwidth(prettify_refname(ref->name));
510 llen = utf8_strwidth(prettify_refname(ref->peer_ref->name));
513 * rough estimation to see if the output line is too long and
514 * should not be counted (we can't do precise calculation
515 * anyway because we don't know if the error explanation part
516 * will be printed in update_local_ref)
518 if (compact_format) {
522 len = 21 /* flag and summary */ + rlen + 4 /* -> */ + llen;
527 * Not precise calculation for compact mode because '*' can
528 * appear on the left hand side of '->' and shrink the column
531 if (refcol_width < rlen)
535 static void prepare_format_display(struct ref *ref_map)
538 const char *format = "full";
540 git_config_get_string_const("fetch.output", &format);
541 if (!strcasecmp(format, "full"))
543 else if (!strcasecmp(format, "compact"))
546 die(_("configuration fetch.output contains invalid value %s"),
549 for (rm = ref_map; rm; rm = rm->next) {
550 if (rm->status == REF_STATUS_REJECT_SHALLOW ||
552 !strcmp(rm->name, "HEAD"))
555 adjust_refcol_width(rm);
559 static void print_remote_to_local(struct strbuf *display,
560 const char *remote, const char *local)
562 strbuf_addf(display, "%-*s -> %s", refcol_width, remote, local);
565 static int find_and_replace(struct strbuf *haystack,
567 const char *placeholder)
569 const char *p = strstr(haystack->buf, needle);
575 if (p > haystack->buf && p[-1] != '/')
579 nlen = strlen(needle);
580 if (plen > nlen && p[nlen] != '/')
583 strbuf_splice(haystack, p - haystack->buf, nlen,
584 placeholder, strlen(placeholder));
588 static void print_compact(struct strbuf *display,
589 const char *remote, const char *local)
591 struct strbuf r = STRBUF_INIT;
592 struct strbuf l = STRBUF_INIT;
594 if (!strcmp(remote, local)) {
595 strbuf_addf(display, "%-*s -> *", refcol_width, remote);
599 strbuf_addstr(&r, remote);
600 strbuf_addstr(&l, local);
602 if (!find_and_replace(&r, local, "*"))
603 find_and_replace(&l, remote, "*");
604 print_remote_to_local(display, r.buf, l.buf);
610 static void format_display(struct strbuf *display, char code,
611 const char *summary, const char *error,
612 const char *remote, const char *local,
615 int width = (summary_width + strlen(summary) - gettext_width(summary));
617 strbuf_addf(display, "%c %-*s ", code, width, summary);
619 print_remote_to_local(display, remote, local);
621 print_compact(display, remote, local);
623 strbuf_addf(display, " (%s)", error);
626 static int update_local_ref(struct ref *ref,
628 const struct ref *remote_ref,
629 struct strbuf *display,
632 struct commit *current = NULL, *updated;
633 enum object_type type;
634 struct branch *current_branch = branch_get(NULL);
635 const char *pretty_ref = prettify_refname(ref->name);
637 type = sha1_object_info(ref->new_oid.hash, NULL);
639 die(_("object %s not found"), oid_to_hex(&ref->new_oid));
641 if (!oidcmp(&ref->old_oid, &ref->new_oid)) {
643 format_display(display, '=', _("[up to date]"), NULL,
644 remote, pretty_ref, summary_width);
648 if (current_branch &&
649 !strcmp(ref->name, current_branch->name) &&
650 !(update_head_ok || is_bare_repository()) &&
651 !is_null_oid(&ref->old_oid)) {
653 * If this is the head, and it's not okay to update
654 * the head, and the old value of the head isn't empty...
656 format_display(display, '!', _("[rejected]"),
657 _("can't fetch in current branch"),
658 remote, pretty_ref, summary_width);
662 if (!is_null_oid(&ref->old_oid) &&
663 starts_with(ref->name, "refs/tags/")) {
665 r = s_update_ref("updating tag", ref, 0);
666 format_display(display, r ? '!' : 't', _("[tag update]"),
667 r ? _("unable to update local ref") : NULL,
668 remote, pretty_ref, summary_width);
672 current = lookup_commit_reference_gently(&ref->old_oid, 1);
673 updated = lookup_commit_reference_gently(&ref->new_oid, 1);
674 if (!current || !updated) {
679 * Nicely describe the new ref we're fetching.
680 * Base this on the remote's ref name, as it's
681 * more likely to follow a standard layout.
683 const char *name = remote_ref ? remote_ref->name : "";
684 if (starts_with(name, "refs/tags/")) {
686 what = _("[new tag]");
687 } else if (starts_with(name, "refs/heads/")) {
688 msg = "storing head";
689 what = _("[new branch]");
692 what = _("[new ref]");
695 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
696 (recurse_submodules != RECURSE_SUBMODULES_ON))
697 check_for_new_submodule_commits(&ref->new_oid);
698 r = s_update_ref(msg, ref, 0);
699 format_display(display, r ? '!' : '*', what,
700 r ? _("unable to update local ref") : NULL,
701 remote, pretty_ref, summary_width);
705 if (in_merge_bases(current, updated)) {
706 struct strbuf quickref = STRBUF_INIT;
708 strbuf_add_unique_abbrev(&quickref, current->object.oid.hash, DEFAULT_ABBREV);
709 strbuf_addstr(&quickref, "..");
710 strbuf_add_unique_abbrev(&quickref, ref->new_oid.hash, DEFAULT_ABBREV);
711 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
712 (recurse_submodules != RECURSE_SUBMODULES_ON))
713 check_for_new_submodule_commits(&ref->new_oid);
714 r = s_update_ref("fast-forward", ref, 1);
715 format_display(display, r ? '!' : ' ', quickref.buf,
716 r ? _("unable to update local ref") : NULL,
717 remote, pretty_ref, summary_width);
718 strbuf_release(&quickref);
720 } else if (force || ref->force) {
721 struct strbuf quickref = STRBUF_INIT;
723 strbuf_add_unique_abbrev(&quickref, current->object.oid.hash, DEFAULT_ABBREV);
724 strbuf_addstr(&quickref, "...");
725 strbuf_add_unique_abbrev(&quickref, ref->new_oid.hash, DEFAULT_ABBREV);
726 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
727 (recurse_submodules != RECURSE_SUBMODULES_ON))
728 check_for_new_submodule_commits(&ref->new_oid);
729 r = s_update_ref("forced-update", ref, 1);
730 format_display(display, r ? '!' : '+', quickref.buf,
731 r ? _("unable to update local ref") : _("forced update"),
732 remote, pretty_ref, summary_width);
733 strbuf_release(&quickref);
736 format_display(display, '!', _("[rejected]"), _("non-fast-forward"),
737 remote, pretty_ref, summary_width);
742 static int iterate_ref_map(void *cb_data, struct object_id *oid)
744 struct ref **rm = cb_data;
745 struct ref *ref = *rm;
747 while (ref && ref->status == REF_STATUS_REJECT_SHALLOW)
750 return -1; /* end of the list */
752 oidcpy(oid, &ref->old_oid);
756 static int store_updated_refs(const char *raw_url, const char *remote_name,
760 struct commit *commit;
761 int url_len, i, rc = 0;
762 struct strbuf note = STRBUF_INIT;
763 const char *what, *kind;
766 const char *filename = dry_run ? "/dev/null" : git_path_fetch_head();
768 int summary_width = transport_summary_width(ref_map);
770 fp = fopen(filename, "a");
772 return error_errno(_("cannot open %s"), filename);
775 url = transport_anonymize_url(raw_url);
777 url = xstrdup("foreign");
780 if (check_connected(iterate_ref_map, &rm, NULL)) {
781 rc = error(_("%s did not send all necessary objects\n"), url);
785 prepare_format_display(ref_map);
788 * We do a pass for each fetch_head_status type in their enum order, so
789 * merged entries are written before not-for-merge. That lets readers
790 * use FETCH_HEAD as a refname to refer to the ref to be merged.
792 for (want_status = FETCH_HEAD_MERGE;
793 want_status <= FETCH_HEAD_IGNORE;
795 for (rm = ref_map; rm; rm = rm->next) {
796 struct ref *ref = NULL;
797 const char *merge_status_marker = "";
799 if (rm->status == REF_STATUS_REJECT_SHALLOW) {
800 if (want_status == FETCH_HEAD_MERGE)
801 warning(_("reject %s because shallow roots are not allowed to be updated"),
802 rm->peer_ref ? rm->peer_ref->name : rm->name);
806 commit = lookup_commit_reference_gently(&rm->old_oid,
809 rm->fetch_head_status = FETCH_HEAD_NOT_FOR_MERGE;
811 if (rm->fetch_head_status != want_status)
815 ref = alloc_ref(rm->peer_ref->name);
816 oidcpy(&ref->old_oid, &rm->peer_ref->old_oid);
817 oidcpy(&ref->new_oid, &rm->old_oid);
818 ref->force = rm->peer_ref->force;
822 if (!strcmp(rm->name, "HEAD")) {
826 else if (starts_with(rm->name, "refs/heads/")) {
828 what = rm->name + 11;
830 else if (starts_with(rm->name, "refs/tags/")) {
832 what = rm->name + 10;
834 else if (starts_with(rm->name, "refs/remotes/")) {
835 kind = "remote-tracking branch";
836 what = rm->name + 13;
843 url_len = strlen(url);
844 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
847 if (4 < i && !strncmp(".git", url + i - 3, 4))
853 strbuf_addf(¬e, "%s ", kind);
854 strbuf_addf(¬e, "'%s' of ", what);
856 switch (rm->fetch_head_status) {
857 case FETCH_HEAD_NOT_FOR_MERGE:
858 merge_status_marker = "not-for-merge";
860 case FETCH_HEAD_MERGE:
861 fprintf(fp, "%s\t%s\t%s",
862 oid_to_hex(&rm->old_oid),
865 for (i = 0; i < url_len; ++i)
873 /* do not write anything to FETCH_HEAD */
879 rc |= update_local_ref(ref, what, rm, ¬e,
883 format_display(¬e, '*',
884 *kind ? kind : "branch", NULL,
885 *what ? what : "HEAD",
886 "FETCH_HEAD", summary_width);
888 if (verbosity >= 0 && !shown_url) {
889 fprintf(stderr, _("From %.*s\n"),
894 fprintf(stderr, " %s\n", note.buf);
899 if (rc & STORE_REF_ERROR_DF_CONFLICT)
900 error(_("some local refs could not be updated; try running\n"
901 " 'git remote prune %s' to remove any old, conflicting "
902 "branches"), remote_name);
905 strbuf_release(¬e);
912 * We would want to bypass the object transfer altogether if
913 * everything we are going to fetch already exists and is connected
916 static int quickfetch(struct ref *ref_map)
918 struct ref *rm = ref_map;
919 struct check_connected_options opt = CHECK_CONNECTED_INIT;
922 * If we are deepening a shallow clone we already have these
923 * objects reachable. Running rev-list here will return with
924 * a good (0) exit status and we'll bypass the fetch that we
925 * really need to perform. Claiming failure now will ensure
926 * we perform the network exchange to deepen our history.
931 return check_connected(iterate_ref_map, &rm, &opt);
934 static int fetch_refs(struct transport *transport, struct ref *ref_map)
936 int ret = quickfetch(ref_map);
938 ret = transport_fetch_refs(transport, ref_map);
940 ret |= store_updated_refs(transport->url,
941 transport->remote->name,
943 transport_unlock_pack(transport);
947 static int prune_refs(struct refspec *refs, int ref_count, struct ref *ref_map,
950 int url_len, i, result = 0;
951 struct ref *ref, *stale_refs = get_stale_heads(refs, ref_count, ref_map);
953 int summary_width = transport_summary_width(stale_refs);
954 const char *dangling_msg = dry_run
955 ? _(" (%s will become dangling)")
956 : _(" (%s has become dangling)");
959 url = transport_anonymize_url(raw_url);
961 url = xstrdup("foreign");
963 url_len = strlen(url);
964 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
968 if (4 < i && !strncmp(".git", url + i - 3, 4))
972 struct string_list refnames = STRING_LIST_INIT_NODUP;
974 for (ref = stale_refs; ref; ref = ref->next)
975 string_list_append(&refnames, ref->name);
977 result = delete_refs("fetch: prune", &refnames, 0);
978 string_list_clear(&refnames, 0);
981 if (verbosity >= 0) {
982 for (ref = stale_refs; ref; ref = ref->next) {
983 struct strbuf sb = STRBUF_INIT;
985 fprintf(stderr, _("From %.*s\n"), url_len, url);
988 format_display(&sb, '-', _("[deleted]"), NULL,
989 _("(none)"), prettify_refname(ref->name),
991 fprintf(stderr, " %s\n",sb.buf);
993 warn_dangling_symref(stderr, dangling_msg, ref->name);
998 free_refs(stale_refs);
1002 static void check_not_current_branch(struct ref *ref_map)
1004 struct branch *current_branch = branch_get(NULL);
1006 if (is_bare_repository() || !current_branch)
1009 for (; ref_map; ref_map = ref_map->next)
1010 if (ref_map->peer_ref && !strcmp(current_branch->refname,
1011 ref_map->peer_ref->name))
1012 die(_("Refusing to fetch into current branch %s "
1013 "of non-bare repository"), current_branch->refname);
1016 static int truncate_fetch_head(void)
1018 const char *filename = git_path_fetch_head();
1019 FILE *fp = fopen_for_writing(filename);
1022 return error_errno(_("cannot open %s"), filename);
1027 static void set_option(struct transport *transport, const char *name, const char *value)
1029 int r = transport_set_option(transport, name, value);
1031 die(_("Option \"%s\" value \"%s\" is not valid for %s"),
1032 name, value, transport->url);
1034 warning(_("Option \"%s\" is ignored for %s\n"),
1035 name, transport->url);
1038 static struct transport *prepare_transport(struct remote *remote, int deepen)
1040 struct transport *transport;
1041 transport = transport_get(remote, NULL);
1042 transport_set_verbosity(transport, verbosity, progress);
1043 transport->family = family;
1045 set_option(transport, TRANS_OPT_UPLOADPACK, upload_pack);
1047 set_option(transport, TRANS_OPT_KEEP, "yes");
1049 set_option(transport, TRANS_OPT_DEPTH, depth);
1050 if (deepen && deepen_since)
1051 set_option(transport, TRANS_OPT_DEEPEN_SINCE, deepen_since);
1052 if (deepen && deepen_not.nr)
1053 set_option(transport, TRANS_OPT_DEEPEN_NOT,
1054 (const char *)&deepen_not);
1055 if (deepen_relative)
1056 set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, "yes");
1058 set_option(transport, TRANS_OPT_UPDATE_SHALLOW, "yes");
1062 static void backfill_tags(struct transport *transport, struct ref *ref_map)
1067 * Once we have set TRANS_OPT_DEEPEN_SINCE, we can't unset it
1068 * when remote helper is used (setting it to an empty string
1069 * is not unsetting). We could extend the remote helper
1070 * protocol for that, but for now, just force a new connection
1071 * without deepen-since. Similar story for deepen-not.
1073 cannot_reuse = transport->cannot_reuse ||
1074 deepen_since || deepen_not.nr;
1076 gsecondary = prepare_transport(transport->remote, 0);
1077 transport = gsecondary;
1080 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);
1081 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
1082 transport_set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, NULL);
1083 fetch_refs(transport, ref_map);
1086 transport_disconnect(gsecondary);
1091 static int do_fetch(struct transport *transport,
1092 struct refspec *refs, int ref_count)
1094 struct string_list existing_refs = STRING_LIST_INIT_DUP;
1095 struct ref *ref_map;
1097 int autotags = (transport->remote->fetch_tags == 1);
1100 for_each_ref(add_existing, &existing_refs);
1102 if (tags == TAGS_DEFAULT) {
1103 if (transport->remote->fetch_tags == 2)
1105 if (transport->remote->fetch_tags == -1)
1109 /* if not appending, truncate FETCH_HEAD */
1110 if (!append && !dry_run) {
1111 retcode = truncate_fetch_head();
1116 ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
1117 if (!update_head_ok)
1118 check_not_current_branch(ref_map);
1120 for (rm = ref_map; rm; rm = rm->next) {
1122 struct string_list_item *peer_item =
1123 string_list_lookup(&existing_refs,
1124 rm->peer_ref->name);
1126 struct object_id *old_oid = peer_item->util;
1127 oidcpy(&rm->peer_ref->old_oid, old_oid);
1132 if (tags == TAGS_DEFAULT && autotags)
1133 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
1136 * We only prune based on refspecs specified
1137 * explicitly (via command line or configuration); we
1138 * don't care whether --tags was specified.
1141 prune_refs(refs, ref_count, ref_map, transport->url);
1143 prune_refs(transport->remote->fetch,
1144 transport->remote->fetch_refspec_nr,
1149 if (fetch_refs(transport, ref_map)) {
1156 /* if neither --no-tags nor --tags was specified, do automated tag
1158 if (tags == TAGS_DEFAULT && autotags) {
1159 struct ref **tail = &ref_map;
1161 find_non_local_tags(transport, &ref_map, &tail);
1163 backfill_tags(transport, ref_map);
1168 string_list_clear(&existing_refs, 1);
1172 static int get_one_remote_for_fetch(struct remote *remote, void *priv)
1174 struct string_list *list = priv;
1175 if (!remote->skip_default_update)
1176 string_list_append(list, remote->name);
1180 struct remote_group_data {
1182 struct string_list *list;
1185 static int get_remote_group(const char *key, const char *value, void *priv)
1187 struct remote_group_data *g = priv;
1189 if (skip_prefix(key, "remotes.", &key) && !strcmp(key, g->name)) {
1190 /* split list by white space */
1192 size_t wordlen = strcspn(value, " \t\n");
1195 string_list_append_nodup(g->list,
1196 xstrndup(value, wordlen));
1197 value += wordlen + (value[wordlen] != '\0');
1204 static int add_remote_or_group(const char *name, struct string_list *list)
1206 int prev_nr = list->nr;
1207 struct remote_group_data g;
1208 g.name = name; g.list = list;
1210 git_config(get_remote_group, &g);
1211 if (list->nr == prev_nr) {
1212 struct remote *remote = remote_get(name);
1213 if (!remote_is_configured(remote, 0))
1215 string_list_append(list, remote->name);
1220 static void add_options_to_argv(struct argv_array *argv)
1223 argv_array_push(argv, "--dry-run");
1225 argv_array_push(argv, prune ? "--prune" : "--no-prune");
1226 if (prune_tags != -1)
1227 argv_array_push(argv, prune_tags ? "--prune-tags" : "--no-prune-tags");
1229 argv_array_push(argv, "--update-head-ok");
1231 argv_array_push(argv, "--force");
1233 argv_array_push(argv, "--keep");
1234 if (recurse_submodules == RECURSE_SUBMODULES_ON)
1235 argv_array_push(argv, "--recurse-submodules");
1236 else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
1237 argv_array_push(argv, "--recurse-submodules=on-demand");
1238 if (tags == TAGS_SET)
1239 argv_array_push(argv, "--tags");
1240 else if (tags == TAGS_UNSET)
1241 argv_array_push(argv, "--no-tags");
1243 argv_array_push(argv, "-v");
1245 argv_array_push(argv, "-v");
1246 else if (verbosity < 0)
1247 argv_array_push(argv, "-q");
1251 static int fetch_multiple(struct string_list *list)
1254 struct argv_array argv = ARGV_ARRAY_INIT;
1256 if (!append && !dry_run) {
1257 int errcode = truncate_fetch_head();
1262 argv_array_pushl(&argv, "fetch", "--append", NULL);
1263 add_options_to_argv(&argv);
1265 for (i = 0; i < list->nr; i++) {
1266 const char *name = list->items[i].string;
1267 argv_array_push(&argv, name);
1269 printf(_("Fetching %s\n"), name);
1270 if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) {
1271 error(_("Could not fetch %s"), name);
1274 argv_array_pop(&argv);
1277 argv_array_clear(&argv);
1281 static int fetch_one(struct remote *remote, int argc, const char **argv, int prune_tags_ok)
1283 static const char **refs = NULL;
1284 struct refspec *refspec;
1289 die(_("No remote repository specified. Please, specify either a URL or a\n"
1290 "remote name from which new revisions should be fetched."));
1292 gtransport = prepare_transport(remote, 1);
1295 /* no command line request */
1296 if (0 <= remote->prune)
1297 prune = remote->prune;
1298 else if (0 <= fetch_prune_config)
1299 prune = fetch_prune_config;
1301 prune = PRUNE_BY_DEFAULT;
1304 if (prune_tags < 0) {
1305 /* no command line request */
1306 if (0 <= remote->prune_tags)
1307 prune_tags = remote->prune_tags;
1308 else if (0 <= fetch_prune_tags_config)
1309 prune_tags = fetch_prune_tags_config;
1311 prune_tags = PRUNE_TAGS_BY_DEFAULT;
1314 if (prune_tags_ok && prune_tags && remote_is_configured(remote, 0))
1315 add_prune_tags_to_fetch_refspec(remote);
1320 refs = xcalloc(st_add(argc, 1), sizeof(const char *));
1321 for (i = 0; i < argc; i++) {
1322 if (!strcmp(argv[i], "tag")) {
1325 die(_("You need to specify a tag name."));
1326 refs[j++] = xstrfmt("refs/tags/%s:refs/tags/%s",
1329 refs[j++] = argv[i];
1334 sigchain_push_common(unlock_pack_on_signal);
1335 atexit(unlock_pack);
1336 refspec = parse_fetch_refspec(ref_nr, refs);
1337 exit_code = do_fetch(gtransport, refspec, ref_nr);
1338 free_refspec(ref_nr, refspec);
1339 transport_disconnect(gtransport);
1344 int cmd_fetch(int argc, const char **argv, const char *prefix)
1347 struct string_list list = STRING_LIST_INIT_DUP;
1348 struct remote *remote;
1350 struct argv_array argv_gc_auto = ARGV_ARRAY_INIT;
1352 packet_trace_identity("fetch");
1354 /* Record the command line for the reflog */
1355 strbuf_addstr(&default_rla, "fetch");
1356 for (i = 1; i < argc; i++)
1357 strbuf_addf(&default_rla, " %s", argv[i]);
1359 config_from_gitmodules(gitmodules_fetch_config, NULL);
1360 git_config(git_fetch_config, NULL);
1362 argc = parse_options(argc, argv, prefix,
1363 builtin_fetch_options, builtin_fetch_usage, 0);
1365 if (deepen_relative) {
1366 if (deepen_relative < 0)
1367 die(_("Negative depth in --deepen is not supported"));
1369 die(_("--deepen and --depth are mutually exclusive"));
1370 depth = xstrfmt("%d", deepen_relative);
1374 die(_("--depth and --unshallow cannot be used together"));
1375 else if (!is_repository_shallow())
1376 die(_("--unshallow on a complete repository does not make sense"));
1378 depth = xstrfmt("%d", INFINITE_DEPTH);
1381 /* no need to be strict, transport_set_option() will validate it again */
1382 if (depth && atoi(depth) < 1)
1383 die(_("depth %s is not a positive number"), depth);
1384 if (depth || deepen_since || deepen_not.nr)
1389 die(_("fetch --all does not take a repository argument"));
1391 die(_("fetch --all does not make sense with refspecs"));
1392 (void) for_each_remote(get_one_remote_for_fetch, &list);
1393 result = fetch_multiple(&list);
1394 } else if (argc == 0) {
1395 /* No arguments -- use default remote */
1396 remote = remote_get(NULL);
1397 result = fetch_one(remote, argc, argv, 1);
1398 } else if (multiple) {
1399 /* All arguments are assumed to be remotes or groups */
1400 for (i = 0; i < argc; i++)
1401 if (!add_remote_or_group(argv[i], &list))
1402 die(_("No such remote or remote group: %s"), argv[i]);
1403 result = fetch_multiple(&list);
1405 /* Single remote or group */
1406 (void) add_remote_or_group(argv[0], &list);
1408 /* More than one remote */
1410 die(_("Fetching a group and specifying refspecs does not make sense"));
1411 result = fetch_multiple(&list);
1413 /* Zero or one remotes */
1414 remote = remote_get(argv[0]);
1415 result = fetch_one(remote, argc-1, argv+1, argc == 1);
1419 if (!result && (recurse_submodules != RECURSE_SUBMODULES_OFF)) {
1420 struct argv_array options = ARGV_ARRAY_INIT;
1422 add_options_to_argv(&options);
1423 result = fetch_populated_submodules(the_repository,
1427 recurse_submodules_default,
1430 argv_array_clear(&options);
1433 string_list_clear(&list, 0);
1437 argv_array_pushl(&argv_gc_auto, "gc", "--auto", NULL);
1439 argv_array_push(&argv_gc_auto, "--quiet");
1440 run_command_v_opt(argv_gc_auto.argv, RUN_GIT_CMD);
1441 argv_array_clear(&argv_gc_auto);