8 #include "string-list.h"
10 #include "transport.h"
11 #include "run-command.h"
12 #include "parse-options.h"
14 #include "submodule-config.h"
15 #include "submodule.h"
16 #include "connected.h"
17 #include "argv-array.h"
20 static const char * const builtin_fetch_usage[] = {
21 N_("git fetch [<options>] [<repository> [<refspec>...]]"),
22 N_("git fetch [<options>] <group>"),
23 N_("git fetch --multiple [<options>] [(<repository> | <group>)...]"),
24 N_("git fetch --all [<options>]"),
34 static int fetch_prune_config = -1; /* unspecified */
35 static int prune = -1; /* unspecified */
36 #define PRUNE_BY_DEFAULT 0 /* do we prune by default? */
38 static int all, append, dry_run, force, keep, multiple, update_head_ok, verbosity, deepen_relative;
39 static int progress = -1;
40 static int tags = TAGS_DEFAULT, unshallow, update_shallow, deepen;
41 static int max_children = -1;
42 static enum transport_family family;
43 static const char *depth;
44 static const char *deepen_since;
45 static const char *upload_pack;
46 static struct string_list deepen_not = STRING_LIST_INIT_NODUP;
47 static struct strbuf default_rla = STRBUF_INIT;
48 static struct transport *gtransport;
49 static struct transport *gsecondary;
50 static const char *submodule_prefix = "";
51 static int recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
52 static int recurse_submodules_default = RECURSE_SUBMODULES_DEFAULT;
53 static int shown_url = 0;
54 static int refmap_alloc, refmap_nr;
55 static const char **refmap_array;
57 static int git_fetch_config(const char *k, const char *v, void *cb)
59 if (!strcmp(k, "fetch.prune")) {
60 fetch_prune_config = git_config_bool(k, v);
64 if (!strcmp(k, "submodule.recurse")) {
65 int r = git_config_bool(k, v) ?
66 RECURSE_SUBMODULES_ON : RECURSE_SUBMODULES_OFF;
67 recurse_submodules = r;
70 return git_default_config(k, v, cb);
73 static int parse_refmap_arg(const struct option *opt, const char *arg, int unset)
75 ALLOC_GROW(refmap_array, refmap_nr + 1, refmap_alloc);
78 * "git fetch --refmap='' origin foo"
79 * can be used to tell the command not to store anywhere
82 refmap_array[refmap_nr++] = arg;
86 static struct option builtin_fetch_options[] = {
87 OPT__VERBOSITY(&verbosity),
88 OPT_BOOL(0, "all", &all,
89 N_("fetch from all remotes")),
90 OPT_BOOL('a', "append", &append,
91 N_("append to .git/FETCH_HEAD instead of overwriting")),
92 OPT_STRING(0, "upload-pack", &upload_pack, N_("path"),
93 N_("path to upload pack on remote end")),
94 OPT__FORCE(&force, N_("force overwrite of local branch")),
95 OPT_BOOL('m', "multiple", &multiple,
96 N_("fetch from multiple remotes")),
97 OPT_SET_INT('t', "tags", &tags,
98 N_("fetch all tags and associated objects"), TAGS_SET),
99 OPT_SET_INT('n', NULL, &tags,
100 N_("do not fetch all tags (--no-tags)"), TAGS_UNSET),
101 OPT_INTEGER('j', "jobs", &max_children,
102 N_("number of submodules fetched in parallel")),
103 OPT_BOOL('p', "prune", &prune,
104 N_("prune remote-tracking branches no longer on remote")),
105 { OPTION_CALLBACK, 0, "recurse-submodules", &recurse_submodules, N_("on-demand"),
106 N_("control recursive fetching of submodules"),
107 PARSE_OPT_OPTARG, option_fetch_parse_recurse_submodules },
108 OPT_BOOL(0, "dry-run", &dry_run,
110 OPT_BOOL('k', "keep", &keep, N_("keep downloaded pack")),
111 OPT_BOOL('u', "update-head-ok", &update_head_ok,
112 N_("allow updating of HEAD ref")),
113 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
114 OPT_STRING(0, "depth", &depth, N_("depth"),
115 N_("deepen history of shallow clone")),
116 OPT_STRING(0, "shallow-since", &deepen_since, N_("time"),
117 N_("deepen history of shallow repository based on time")),
118 OPT_STRING_LIST(0, "shallow-exclude", &deepen_not, N_("revision"),
119 N_("deepen history of shallow clone, excluding rev")),
120 OPT_INTEGER(0, "deepen", &deepen_relative,
121 N_("deepen history of shallow clone")),
122 { OPTION_SET_INT, 0, "unshallow", &unshallow, NULL,
123 N_("convert to a complete repository"),
124 PARSE_OPT_NONEG | PARSE_OPT_NOARG, NULL, 1 },
125 { OPTION_STRING, 0, "submodule-prefix", &submodule_prefix, N_("dir"),
126 N_("prepend this to submodule path output"), PARSE_OPT_HIDDEN },
127 { OPTION_CALLBACK, 0, "recurse-submodules-default",
128 &recurse_submodules_default, N_("on-demand"),
129 N_("default for recursive fetching of submodules "
130 "(lower priority than config files)"),
131 PARSE_OPT_HIDDEN, option_fetch_parse_recurse_submodules },
132 OPT_BOOL(0, "update-shallow", &update_shallow,
133 N_("accept refs that update .git/shallow")),
134 { OPTION_CALLBACK, 0, "refmap", NULL, N_("refmap"),
135 N_("specify fetch refmap"), PARSE_OPT_NONEG, parse_refmap_arg },
136 OPT_SET_INT('4', "ipv4", &family, N_("use IPv4 addresses only"),
137 TRANSPORT_FAMILY_IPV4),
138 OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"),
139 TRANSPORT_FAMILY_IPV6),
143 static void unlock_pack(void)
146 transport_unlock_pack(gtransport);
148 transport_unlock_pack(gsecondary);
151 static void unlock_pack_on_signal(int signo)
158 static void add_merge_config(struct ref **head,
159 const struct ref *remote_refs,
160 struct branch *branch,
165 for (i = 0; i < branch->merge_nr; i++) {
166 struct ref *rm, **old_tail = *tail;
167 struct refspec refspec;
169 for (rm = *head; rm; rm = rm->next) {
170 if (branch_merge_matches(branch, i, rm->name)) {
171 rm->fetch_head_status = FETCH_HEAD_MERGE;
179 * Not fetched to a remote-tracking branch? We need to fetch
180 * it anyway to allow this branch's "branch.$name.merge"
181 * to be honored by 'git pull', but we do not have to
182 * fail if branch.$name.merge is misconfigured to point
183 * at a nonexisting branch. If we were indeed called by
184 * 'git pull', it will notice the misconfiguration because
185 * there is no entry in the resulting FETCH_HEAD marked
188 memset(&refspec, 0, sizeof(refspec));
189 refspec.src = branch->merge[i]->src;
190 get_fetch_map(remote_refs, &refspec, tail, 1);
191 for (rm = *old_tail; rm; rm = rm->next)
192 rm->fetch_head_status = FETCH_HEAD_MERGE;
196 static int add_existing(const char *refname, const struct object_id *oid,
197 int flag, void *cbdata)
199 struct string_list *list = (struct string_list *)cbdata;
200 struct string_list_item *item = string_list_insert(list, refname);
201 struct object_id *old_oid = xmalloc(sizeof(*old_oid));
203 oidcpy(old_oid, oid);
204 item->util = old_oid;
208 static int will_fetch(struct ref **head, const unsigned char *sha1)
210 struct ref *rm = *head;
212 if (!hashcmp(rm->old_oid.hash, sha1))
219 static void find_non_local_tags(struct transport *transport,
223 struct string_list existing_refs = STRING_LIST_INIT_DUP;
224 struct string_list remote_refs = STRING_LIST_INIT_NODUP;
225 const struct ref *ref;
226 struct string_list_item *item = NULL;
228 for_each_ref(add_existing, &existing_refs);
229 for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
230 if (!starts_with(ref->name, "refs/tags/"))
234 * The peeled ref always follows the matching base
235 * ref, so if we see a peeled ref that we don't want
236 * to fetch then we can mark the ref entry in the list
237 * as one to ignore by setting util to NULL.
239 if (ends_with(ref->name, "^{}")) {
241 !has_object_file_with_flags(&ref->old_oid, HAS_SHA1_QUICK) &&
242 !will_fetch(head, ref->old_oid.hash) &&
243 !has_sha1_file_with_flags(item->util, HAS_SHA1_QUICK) &&
244 !will_fetch(head, item->util))
251 * If item is non-NULL here, then we previously saw a
252 * ref not followed by a peeled reference, so we need
253 * to check if it is a lightweight tag that we want to
257 !has_sha1_file_with_flags(item->util, HAS_SHA1_QUICK) &&
258 !will_fetch(head, item->util))
263 /* skip duplicates and refs that we already have */
264 if (string_list_has_string(&remote_refs, ref->name) ||
265 string_list_has_string(&existing_refs, ref->name))
268 item = string_list_insert(&remote_refs, ref->name);
269 item->util = (void *)&ref->old_oid;
271 string_list_clear(&existing_refs, 1);
274 * We may have a final lightweight tag that needs to be
275 * checked to see if it needs fetching.
278 !has_sha1_file_with_flags(item->util, HAS_SHA1_QUICK) &&
279 !will_fetch(head, item->util))
283 * For all the tags in the remote_refs string list,
284 * add them to the list of refs to be fetched
286 for_each_string_list_item(item, &remote_refs) {
287 /* Unless we have already decided to ignore this item... */
290 struct ref *rm = alloc_ref(item->string);
291 rm->peer_ref = alloc_ref(item->string);
292 oidcpy(&rm->old_oid, item->util);
298 string_list_clear(&remote_refs, 0);
301 static struct ref *get_ref_map(struct transport *transport,
302 struct refspec *refspecs, int refspec_count,
303 int tags, int *autotags)
307 struct ref *ref_map = NULL;
308 struct ref **tail = &ref_map;
310 /* opportunistically-updated references: */
311 struct ref *orefs = NULL, **oref_tail = &orefs;
313 const struct ref *remote_refs = transport_get_remote_refs(transport);
316 struct refspec *fetch_refspec;
317 int fetch_refspec_nr;
319 for (i = 0; i < refspec_count; i++) {
320 get_fetch_map(remote_refs, &refspecs[i], &tail, 0);
321 if (refspecs[i].dst && refspecs[i].dst[0])
324 /* Merge everything on the command line (but not --tags) */
325 for (rm = ref_map; rm; rm = rm->next)
326 rm->fetch_head_status = FETCH_HEAD_MERGE;
329 * For any refs that we happen to be fetching via
330 * command-line arguments, the destination ref might
331 * have been missing or have been different than the
332 * remote-tracking ref that would be derived from the
333 * configured refspec. In these cases, we want to
334 * take the opportunity to update their configured
335 * remote-tracking reference. However, we do not want
336 * to mention these entries in FETCH_HEAD at all, as
337 * they would simply be duplicates of existing
338 * entries, so we set them FETCH_HEAD_IGNORE below.
340 * We compute these entries now, based only on the
341 * refspecs specified on the command line. But we add
342 * them to the list following the refspecs resulting
343 * from the tags option so that one of the latter,
344 * which has FETCH_HEAD_NOT_FOR_MERGE, is not removed
345 * by ref_remove_duplicates() in favor of one of these
346 * opportunistic entries with FETCH_HEAD_IGNORE.
349 fetch_refspec = parse_fetch_refspec(refmap_nr, refmap_array);
350 fetch_refspec_nr = refmap_nr;
352 fetch_refspec = transport->remote->fetch;
353 fetch_refspec_nr = transport->remote->fetch_refspec_nr;
356 for (i = 0; i < fetch_refspec_nr; i++)
357 get_fetch_map(ref_map, &fetch_refspec[i], &oref_tail, 1);
358 } else if (refmap_array) {
359 die("--refmap option is only meaningful with command-line refspec(s).");
361 /* Use the defaults */
362 struct remote *remote = transport->remote;
363 struct branch *branch = branch_get(NULL);
364 int has_merge = branch_has_merge_config(branch);
366 (remote->fetch_refspec_nr ||
367 /* Note: has_merge implies non-NULL branch->remote_name */
368 (has_merge && !strcmp(branch->remote_name, remote->name)))) {
369 for (i = 0; i < remote->fetch_refspec_nr; i++) {
370 get_fetch_map(remote_refs, &remote->fetch[i], &tail, 0);
371 if (remote->fetch[i].dst &&
372 remote->fetch[i].dst[0])
374 if (!i && !has_merge && ref_map &&
375 !remote->fetch[0].pattern)
376 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
379 * if the remote we're fetching from is the same
380 * as given in branch.<name>.remote, we add the
381 * ref given in branch.<name>.merge, too.
383 * Note: has_merge implies non-NULL branch->remote_name
386 !strcmp(branch->remote_name, remote->name))
387 add_merge_config(&ref_map, remote_refs, branch, &tail);
389 ref_map = get_remote_ref(remote_refs, "HEAD");
391 die(_("Couldn't find remote ref HEAD"));
392 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
393 tail = &ref_map->next;
397 if (tags == TAGS_SET)
398 /* also fetch all tags */
399 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
400 else if (tags == TAGS_DEFAULT && *autotags)
401 find_non_local_tags(transport, &ref_map, &tail);
403 /* Now append any refs to be updated opportunistically: */
405 for (rm = orefs; rm; rm = rm->next) {
406 rm->fetch_head_status = FETCH_HEAD_IGNORE;
410 return ref_remove_duplicates(ref_map);
413 #define STORE_REF_ERROR_OTHER 1
414 #define STORE_REF_ERROR_DF_CONFLICT 2
416 static int s_update_ref(const char *action,
421 char *rla = getenv("GIT_REFLOG_ACTION");
422 struct ref_transaction *transaction;
423 struct strbuf err = STRBUF_INIT;
424 int ret, df_conflict = 0;
429 rla = default_rla.buf;
430 msg = xstrfmt("%s: %s", rla, action);
432 transaction = ref_transaction_begin(&err);
434 ref_transaction_update(transaction, ref->name,
436 check_old ? ref->old_oid.hash : NULL,
440 ret = ref_transaction_commit(transaction, &err);
442 df_conflict = (ret == TRANSACTION_NAME_CONFLICT);
446 ref_transaction_free(transaction);
447 strbuf_release(&err);
451 ref_transaction_free(transaction);
452 error("%s", err.buf);
453 strbuf_release(&err);
455 return df_conflict ? STORE_REF_ERROR_DF_CONFLICT
456 : STORE_REF_ERROR_OTHER;
459 static int refcol_width = 10;
460 static int compact_format;
462 static void adjust_refcol_width(const struct ref *ref)
464 int max, rlen, llen, len;
466 /* uptodate lines are only shown on high verbosity level */
467 if (!verbosity && !oidcmp(&ref->peer_ref->old_oid, &ref->old_oid))
470 max = term_columns();
471 rlen = utf8_strwidth(prettify_refname(ref->name));
473 llen = utf8_strwidth(prettify_refname(ref->peer_ref->name));
476 * rough estimation to see if the output line is too long and
477 * should not be counted (we can't do precise calculation
478 * anyway because we don't know if the error explanation part
479 * will be printed in update_local_ref)
481 if (compact_format) {
485 len = 21 /* flag and summary */ + rlen + 4 /* -> */ + llen;
490 * Not precise calculation for compact mode because '*' can
491 * appear on the left hand side of '->' and shrink the column
494 if (refcol_width < rlen)
498 static void prepare_format_display(struct ref *ref_map)
501 const char *format = "full";
503 git_config_get_string_const("fetch.output", &format);
504 if (!strcasecmp(format, "full"))
506 else if (!strcasecmp(format, "compact"))
509 die(_("configuration fetch.output contains invalid value %s"),
512 for (rm = ref_map; rm; rm = rm->next) {
513 if (rm->status == REF_STATUS_REJECT_SHALLOW ||
515 !strcmp(rm->name, "HEAD"))
518 adjust_refcol_width(rm);
522 static void print_remote_to_local(struct strbuf *display,
523 const char *remote, const char *local)
525 strbuf_addf(display, "%-*s -> %s", refcol_width, remote, local);
528 static int find_and_replace(struct strbuf *haystack,
530 const char *placeholder)
532 const char *p = strstr(haystack->buf, needle);
538 if (p > haystack->buf && p[-1] != '/')
542 nlen = strlen(needle);
543 if (plen > nlen && p[nlen] != '/')
546 strbuf_splice(haystack, p - haystack->buf, nlen,
547 placeholder, strlen(placeholder));
551 static void print_compact(struct strbuf *display,
552 const char *remote, const char *local)
554 struct strbuf r = STRBUF_INIT;
555 struct strbuf l = STRBUF_INIT;
557 if (!strcmp(remote, local)) {
558 strbuf_addf(display, "%-*s -> *", refcol_width, remote);
562 strbuf_addstr(&r, remote);
563 strbuf_addstr(&l, local);
565 if (!find_and_replace(&r, local, "*"))
566 find_and_replace(&l, remote, "*");
567 print_remote_to_local(display, r.buf, l.buf);
573 static void format_display(struct strbuf *display, char code,
574 const char *summary, const char *error,
575 const char *remote, const char *local,
578 int width = (summary_width + strlen(summary) - gettext_width(summary));
580 strbuf_addf(display, "%c %-*s ", code, width, summary);
582 print_remote_to_local(display, remote, local);
584 print_compact(display, remote, local);
586 strbuf_addf(display, " (%s)", error);
589 static int update_local_ref(struct ref *ref,
591 const struct ref *remote_ref,
592 struct strbuf *display,
595 struct commit *current = NULL, *updated;
596 enum object_type type;
597 struct branch *current_branch = branch_get(NULL);
598 const char *pretty_ref = prettify_refname(ref->name);
600 type = sha1_object_info(ref->new_oid.hash, NULL);
602 die(_("object %s not found"), oid_to_hex(&ref->new_oid));
604 if (!oidcmp(&ref->old_oid, &ref->new_oid)) {
606 format_display(display, '=', _("[up to date]"), NULL,
607 remote, pretty_ref, summary_width);
611 if (current_branch &&
612 !strcmp(ref->name, current_branch->name) &&
613 !(update_head_ok || is_bare_repository()) &&
614 !is_null_oid(&ref->old_oid)) {
616 * If this is the head, and it's not okay to update
617 * the head, and the old value of the head isn't empty...
619 format_display(display, '!', _("[rejected]"),
620 _("can't fetch in current branch"),
621 remote, pretty_ref, summary_width);
625 if (!is_null_oid(&ref->old_oid) &&
626 starts_with(ref->name, "refs/tags/")) {
628 r = s_update_ref("updating tag", ref, 0);
629 format_display(display, r ? '!' : 't', _("[tag update]"),
630 r ? _("unable to update local ref") : NULL,
631 remote, pretty_ref, summary_width);
635 current = lookup_commit_reference_gently(&ref->old_oid, 1);
636 updated = lookup_commit_reference_gently(&ref->new_oid, 1);
637 if (!current || !updated) {
642 * Nicely describe the new ref we're fetching.
643 * Base this on the remote's ref name, as it's
644 * more likely to follow a standard layout.
646 const char *name = remote_ref ? remote_ref->name : "";
647 if (starts_with(name, "refs/tags/")) {
649 what = _("[new tag]");
650 } else if (starts_with(name, "refs/heads/")) {
651 msg = "storing head";
652 what = _("[new branch]");
655 what = _("[new ref]");
658 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
659 (recurse_submodules != RECURSE_SUBMODULES_ON))
660 check_for_new_submodule_commits(&ref->new_oid);
661 r = s_update_ref(msg, ref, 0);
662 format_display(display, r ? '!' : '*', what,
663 r ? _("unable to update local ref") : NULL,
664 remote, pretty_ref, summary_width);
668 if (in_merge_bases(current, updated)) {
669 struct strbuf quickref = STRBUF_INIT;
671 strbuf_add_unique_abbrev(&quickref, current->object.oid.hash, DEFAULT_ABBREV);
672 strbuf_addstr(&quickref, "..");
673 strbuf_add_unique_abbrev(&quickref, ref->new_oid.hash, DEFAULT_ABBREV);
674 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
675 (recurse_submodules != RECURSE_SUBMODULES_ON))
676 check_for_new_submodule_commits(&ref->new_oid);
677 r = s_update_ref("fast-forward", ref, 1);
678 format_display(display, r ? '!' : ' ', quickref.buf,
679 r ? _("unable to update local ref") : NULL,
680 remote, pretty_ref, summary_width);
681 strbuf_release(&quickref);
683 } else if (force || ref->force) {
684 struct strbuf quickref = STRBUF_INIT;
686 strbuf_add_unique_abbrev(&quickref, current->object.oid.hash, DEFAULT_ABBREV);
687 strbuf_addstr(&quickref, "...");
688 strbuf_add_unique_abbrev(&quickref, ref->new_oid.hash, DEFAULT_ABBREV);
689 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
690 (recurse_submodules != RECURSE_SUBMODULES_ON))
691 check_for_new_submodule_commits(&ref->new_oid);
692 r = s_update_ref("forced-update", ref, 1);
693 format_display(display, r ? '!' : '+', quickref.buf,
694 r ? _("unable to update local ref") : _("forced update"),
695 remote, pretty_ref, summary_width);
696 strbuf_release(&quickref);
699 format_display(display, '!', _("[rejected]"), _("non-fast-forward"),
700 remote, pretty_ref, summary_width);
705 static int iterate_ref_map(void *cb_data, unsigned char sha1[20])
707 struct ref **rm = cb_data;
708 struct ref *ref = *rm;
710 while (ref && ref->status == REF_STATUS_REJECT_SHALLOW)
713 return -1; /* end of the list */
715 hashcpy(sha1, ref->old_oid.hash);
719 static int store_updated_refs(const char *raw_url, const char *remote_name,
723 struct commit *commit;
724 int url_len, i, rc = 0;
725 struct strbuf note = STRBUF_INIT;
726 const char *what, *kind;
729 const char *filename = dry_run ? "/dev/null" : git_path_fetch_head();
731 int summary_width = transport_summary_width(ref_map);
733 fp = fopen(filename, "a");
735 return error_errno(_("cannot open %s"), filename);
738 url = transport_anonymize_url(raw_url);
740 url = xstrdup("foreign");
743 if (check_connected(iterate_ref_map, &rm, NULL)) {
744 rc = error(_("%s did not send all necessary objects\n"), url);
748 prepare_format_display(ref_map);
751 * We do a pass for each fetch_head_status type in their enum order, so
752 * merged entries are written before not-for-merge. That lets readers
753 * use FETCH_HEAD as a refname to refer to the ref to be merged.
755 for (want_status = FETCH_HEAD_MERGE;
756 want_status <= FETCH_HEAD_IGNORE;
758 for (rm = ref_map; rm; rm = rm->next) {
759 struct ref *ref = NULL;
760 const char *merge_status_marker = "";
762 if (rm->status == REF_STATUS_REJECT_SHALLOW) {
763 if (want_status == FETCH_HEAD_MERGE)
764 warning(_("reject %s because shallow roots are not allowed to be updated"),
765 rm->peer_ref ? rm->peer_ref->name : rm->name);
769 commit = lookup_commit_reference_gently(&rm->old_oid,
772 rm->fetch_head_status = FETCH_HEAD_NOT_FOR_MERGE;
774 if (rm->fetch_head_status != want_status)
778 ref = alloc_ref(rm->peer_ref->name);
779 oidcpy(&ref->old_oid, &rm->peer_ref->old_oid);
780 oidcpy(&ref->new_oid, &rm->old_oid);
781 ref->force = rm->peer_ref->force;
785 if (!strcmp(rm->name, "HEAD")) {
789 else if (starts_with(rm->name, "refs/heads/")) {
791 what = rm->name + 11;
793 else if (starts_with(rm->name, "refs/tags/")) {
795 what = rm->name + 10;
797 else if (starts_with(rm->name, "refs/remotes/")) {
798 kind = "remote-tracking branch";
799 what = rm->name + 13;
806 url_len = strlen(url);
807 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
810 if (4 < i && !strncmp(".git", url + i - 3, 4))
816 strbuf_addf(¬e, "%s ", kind);
817 strbuf_addf(¬e, "'%s' of ", what);
819 switch (rm->fetch_head_status) {
820 case FETCH_HEAD_NOT_FOR_MERGE:
821 merge_status_marker = "not-for-merge";
823 case FETCH_HEAD_MERGE:
824 fprintf(fp, "%s\t%s\t%s",
825 oid_to_hex(&rm->old_oid),
828 for (i = 0; i < url_len; ++i)
836 /* do not write anything to FETCH_HEAD */
842 rc |= update_local_ref(ref, what, rm, ¬e,
846 format_display(¬e, '*',
847 *kind ? kind : "branch", NULL,
848 *what ? what : "HEAD",
849 "FETCH_HEAD", summary_width);
851 if (verbosity >= 0 && !shown_url) {
852 fprintf(stderr, _("From %.*s\n"),
857 fprintf(stderr, " %s\n", note.buf);
862 if (rc & STORE_REF_ERROR_DF_CONFLICT)
863 error(_("some local refs could not be updated; try running\n"
864 " 'git remote prune %s' to remove any old, conflicting "
865 "branches"), remote_name);
868 strbuf_release(¬e);
875 * We would want to bypass the object transfer altogether if
876 * everything we are going to fetch already exists and is connected
879 static int quickfetch(struct ref *ref_map)
881 struct ref *rm = ref_map;
882 struct check_connected_options opt = CHECK_CONNECTED_INIT;
885 * If we are deepening a shallow clone we already have these
886 * objects reachable. Running rev-list here will return with
887 * a good (0) exit status and we'll bypass the fetch that we
888 * really need to perform. Claiming failure now will ensure
889 * we perform the network exchange to deepen our history.
894 return check_connected(iterate_ref_map, &rm, &opt);
897 static int fetch_refs(struct transport *transport, struct ref *ref_map)
899 int ret = quickfetch(ref_map);
901 ret = transport_fetch_refs(transport, ref_map);
903 ret |= store_updated_refs(transport->url,
904 transport->remote->name,
906 transport_unlock_pack(transport);
910 static int prune_refs(struct refspec *refs, int ref_count, struct ref *ref_map,
913 int url_len, i, result = 0;
914 struct ref *ref, *stale_refs = get_stale_heads(refs, ref_count, ref_map);
916 int summary_width = transport_summary_width(stale_refs);
917 const char *dangling_msg = dry_run
918 ? _(" (%s will become dangling)")
919 : _(" (%s has become dangling)");
922 url = transport_anonymize_url(raw_url);
924 url = xstrdup("foreign");
926 url_len = strlen(url);
927 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
931 if (4 < i && !strncmp(".git", url + i - 3, 4))
935 struct string_list refnames = STRING_LIST_INIT_NODUP;
937 for (ref = stale_refs; ref; ref = ref->next)
938 string_list_append(&refnames, ref->name);
940 result = delete_refs("fetch: prune", &refnames, 0);
941 string_list_clear(&refnames, 0);
944 if (verbosity >= 0) {
945 for (ref = stale_refs; ref; ref = ref->next) {
946 struct strbuf sb = STRBUF_INIT;
948 fprintf(stderr, _("From %.*s\n"), url_len, url);
951 format_display(&sb, '-', _("[deleted]"), NULL,
952 _("(none)"), prettify_refname(ref->name),
954 fprintf(stderr, " %s\n",sb.buf);
956 warn_dangling_symref(stderr, dangling_msg, ref->name);
961 free_refs(stale_refs);
965 static void check_not_current_branch(struct ref *ref_map)
967 struct branch *current_branch = branch_get(NULL);
969 if (is_bare_repository() || !current_branch)
972 for (; ref_map; ref_map = ref_map->next)
973 if (ref_map->peer_ref && !strcmp(current_branch->refname,
974 ref_map->peer_ref->name))
975 die(_("Refusing to fetch into current branch %s "
976 "of non-bare repository"), current_branch->refname);
979 static int truncate_fetch_head(void)
981 const char *filename = git_path_fetch_head();
982 FILE *fp = fopen_for_writing(filename);
985 return error_errno(_("cannot open %s"), filename);
990 static void set_option(struct transport *transport, const char *name, const char *value)
992 int r = transport_set_option(transport, name, value);
994 die(_("Option \"%s\" value \"%s\" is not valid for %s"),
995 name, value, transport->url);
997 warning(_("Option \"%s\" is ignored for %s\n"),
998 name, transport->url);
1001 static struct transport *prepare_transport(struct remote *remote, int deepen)
1003 struct transport *transport;
1004 transport = transport_get(remote, NULL);
1005 transport_set_verbosity(transport, verbosity, progress);
1006 transport->family = family;
1008 set_option(transport, TRANS_OPT_UPLOADPACK, upload_pack);
1010 set_option(transport, TRANS_OPT_KEEP, "yes");
1012 set_option(transport, TRANS_OPT_DEPTH, depth);
1013 if (deepen && deepen_since)
1014 set_option(transport, TRANS_OPT_DEEPEN_SINCE, deepen_since);
1015 if (deepen && deepen_not.nr)
1016 set_option(transport, TRANS_OPT_DEEPEN_NOT,
1017 (const char *)&deepen_not);
1018 if (deepen_relative)
1019 set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, "yes");
1021 set_option(transport, TRANS_OPT_UPDATE_SHALLOW, "yes");
1025 static void backfill_tags(struct transport *transport, struct ref *ref_map)
1030 * Once we have set TRANS_OPT_DEEPEN_SINCE, we can't unset it
1031 * when remote helper is used (setting it to an empty string
1032 * is not unsetting). We could extend the remote helper
1033 * protocol for that, but for now, just force a new connection
1034 * without deepen-since. Similar story for deepen-not.
1036 cannot_reuse = transport->cannot_reuse ||
1037 deepen_since || deepen_not.nr;
1039 gsecondary = prepare_transport(transport->remote, 0);
1040 transport = gsecondary;
1043 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);
1044 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
1045 transport_set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, NULL);
1046 fetch_refs(transport, ref_map);
1049 transport_disconnect(gsecondary);
1054 static int do_fetch(struct transport *transport,
1055 struct refspec *refs, int ref_count)
1057 struct string_list existing_refs = STRING_LIST_INIT_DUP;
1058 struct ref *ref_map;
1060 int autotags = (transport->remote->fetch_tags == 1);
1063 for_each_ref(add_existing, &existing_refs);
1065 if (tags == TAGS_DEFAULT) {
1066 if (transport->remote->fetch_tags == 2)
1068 if (transport->remote->fetch_tags == -1)
1072 if (!transport->get_refs_list || !transport->fetch)
1073 die(_("Don't know how to fetch from %s"), transport->url);
1075 /* if not appending, truncate FETCH_HEAD */
1076 if (!append && !dry_run) {
1077 retcode = truncate_fetch_head();
1082 ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
1083 if (!update_head_ok)
1084 check_not_current_branch(ref_map);
1086 for (rm = ref_map; rm; rm = rm->next) {
1088 struct string_list_item *peer_item =
1089 string_list_lookup(&existing_refs,
1090 rm->peer_ref->name);
1092 struct object_id *old_oid = peer_item->util;
1093 oidcpy(&rm->peer_ref->old_oid, old_oid);
1098 if (tags == TAGS_DEFAULT && autotags)
1099 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
1102 * We only prune based on refspecs specified
1103 * explicitly (via command line or configuration); we
1104 * don't care whether --tags was specified.
1107 prune_refs(refs, ref_count, ref_map, transport->url);
1109 prune_refs(transport->remote->fetch,
1110 transport->remote->fetch_refspec_nr,
1115 if (fetch_refs(transport, ref_map)) {
1122 /* if neither --no-tags nor --tags was specified, do automated tag
1124 if (tags == TAGS_DEFAULT && autotags) {
1125 struct ref **tail = &ref_map;
1127 find_non_local_tags(transport, &ref_map, &tail);
1129 backfill_tags(transport, ref_map);
1134 string_list_clear(&existing_refs, 1);
1138 static int get_one_remote_for_fetch(struct remote *remote, void *priv)
1140 struct string_list *list = priv;
1141 if (!remote->skip_default_update)
1142 string_list_append(list, remote->name);
1146 struct remote_group_data {
1148 struct string_list *list;
1151 static int get_remote_group(const char *key, const char *value, void *priv)
1153 struct remote_group_data *g = priv;
1155 if (skip_prefix(key, "remotes.", &key) && !strcmp(key, g->name)) {
1156 /* split list by white space */
1158 size_t wordlen = strcspn(value, " \t\n");
1161 string_list_append_nodup(g->list,
1162 xstrndup(value, wordlen));
1163 value += wordlen + (value[wordlen] != '\0');
1170 static int add_remote_or_group(const char *name, struct string_list *list)
1172 int prev_nr = list->nr;
1173 struct remote_group_data g;
1174 g.name = name; g.list = list;
1176 git_config(get_remote_group, &g);
1177 if (list->nr == prev_nr) {
1178 struct remote *remote = remote_get(name);
1179 if (!remote_is_configured(remote, 0))
1181 string_list_append(list, remote->name);
1186 static void add_options_to_argv(struct argv_array *argv)
1189 argv_array_push(argv, "--dry-run");
1191 argv_array_push(argv, prune ? "--prune" : "--no-prune");
1193 argv_array_push(argv, "--update-head-ok");
1195 argv_array_push(argv, "--force");
1197 argv_array_push(argv, "--keep");
1198 if (recurse_submodules == RECURSE_SUBMODULES_ON)
1199 argv_array_push(argv, "--recurse-submodules");
1200 else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
1201 argv_array_push(argv, "--recurse-submodules=on-demand");
1202 if (tags == TAGS_SET)
1203 argv_array_push(argv, "--tags");
1204 else if (tags == TAGS_UNSET)
1205 argv_array_push(argv, "--no-tags");
1207 argv_array_push(argv, "-v");
1209 argv_array_push(argv, "-v");
1210 else if (verbosity < 0)
1211 argv_array_push(argv, "-q");
1215 static int fetch_multiple(struct string_list *list)
1218 struct argv_array argv = ARGV_ARRAY_INIT;
1220 if (!append && !dry_run) {
1221 int errcode = truncate_fetch_head();
1226 argv_array_pushl(&argv, "fetch", "--append", NULL);
1227 add_options_to_argv(&argv);
1229 for (i = 0; i < list->nr; i++) {
1230 const char *name = list->items[i].string;
1231 argv_array_push(&argv, name);
1233 printf(_("Fetching %s\n"), name);
1234 if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) {
1235 error(_("Could not fetch %s"), name);
1238 argv_array_pop(&argv);
1241 argv_array_clear(&argv);
1245 static int fetch_one(struct remote *remote, int argc, const char **argv)
1247 static const char **refs = NULL;
1248 struct refspec *refspec;
1253 die(_("No remote repository specified. Please, specify either a URL or a\n"
1254 "remote name from which new revisions should be fetched."));
1256 gtransport = prepare_transport(remote, 1);
1259 /* no command line request */
1260 if (0 <= gtransport->remote->prune)
1261 prune = gtransport->remote->prune;
1262 else if (0 <= fetch_prune_config)
1263 prune = fetch_prune_config;
1265 prune = PRUNE_BY_DEFAULT;
1271 refs = xcalloc(st_add(argc, 1), sizeof(const char *));
1272 for (i = 0; i < argc; i++) {
1273 if (!strcmp(argv[i], "tag")) {
1276 die(_("You need to specify a tag name."));
1277 refs[j++] = xstrfmt("refs/tags/%s:refs/tags/%s",
1280 refs[j++] = argv[i];
1286 sigchain_push_common(unlock_pack_on_signal);
1287 atexit(unlock_pack);
1288 refspec = parse_fetch_refspec(ref_nr, refs);
1289 exit_code = do_fetch(gtransport, refspec, ref_nr);
1290 free_refspec(ref_nr, refspec);
1291 transport_disconnect(gtransport);
1296 int cmd_fetch(int argc, const char **argv, const char *prefix)
1299 struct string_list list = STRING_LIST_INIT_DUP;
1300 struct remote *remote;
1302 struct argv_array argv_gc_auto = ARGV_ARRAY_INIT;
1304 packet_trace_identity("fetch");
1306 /* Record the command line for the reflog */
1307 strbuf_addstr(&default_rla, "fetch");
1308 for (i = 1; i < argc; i++)
1309 strbuf_addf(&default_rla, " %s", argv[i]);
1311 git_config(git_fetch_config, NULL);
1313 argc = parse_options(argc, argv, prefix,
1314 builtin_fetch_options, builtin_fetch_usage, 0);
1316 if (deepen_relative) {
1317 if (deepen_relative < 0)
1318 die(_("Negative depth in --deepen is not supported"));
1320 die(_("--deepen and --depth are mutually exclusive"));
1321 depth = xstrfmt("%d", deepen_relative);
1325 die(_("--depth and --unshallow cannot be used together"));
1326 else if (!is_repository_shallow())
1327 die(_("--unshallow on a complete repository does not make sense"));
1329 depth = xstrfmt("%d", INFINITE_DEPTH);
1332 /* no need to be strict, transport_set_option() will validate it again */
1333 if (depth && atoi(depth) < 1)
1334 die(_("depth %s is not a positive number"), depth);
1335 if (depth || deepen_since || deepen_not.nr)
1338 if (recurse_submodules != RECURSE_SUBMODULES_OFF) {
1339 if (recurse_submodules_default != RECURSE_SUBMODULES_DEFAULT)
1340 set_config_fetch_recurse_submodules(recurse_submodules_default);
1341 gitmodules_config();
1342 git_config(submodule_config, NULL);
1347 die(_("fetch --all does not take a repository argument"));
1349 die(_("fetch --all does not make sense with refspecs"));
1350 (void) for_each_remote(get_one_remote_for_fetch, &list);
1351 result = fetch_multiple(&list);
1352 } else if (argc == 0) {
1353 /* No arguments -- use default remote */
1354 remote = remote_get(NULL);
1355 result = fetch_one(remote, argc, argv);
1356 } else if (multiple) {
1357 /* All arguments are assumed to be remotes or groups */
1358 for (i = 0; i < argc; i++)
1359 if (!add_remote_or_group(argv[i], &list))
1360 die(_("No such remote or remote group: %s"), argv[i]);
1361 result = fetch_multiple(&list);
1363 /* Single remote or group */
1364 (void) add_remote_or_group(argv[0], &list);
1366 /* More than one remote */
1368 die(_("Fetching a group and specifying refspecs does not make sense"));
1369 result = fetch_multiple(&list);
1371 /* Zero or one remotes */
1372 remote = remote_get(argv[0]);
1373 result = fetch_one(remote, argc-1, argv+1);
1377 if (!result && (recurse_submodules != RECURSE_SUBMODULES_OFF)) {
1378 struct argv_array options = ARGV_ARRAY_INIT;
1380 add_options_to_argv(&options);
1381 result = fetch_populated_submodules(&options,
1386 argv_array_clear(&options);
1389 string_list_clear(&list, 0);
1393 argv_array_pushl(&argv_gc_auto, "gc", "--auto", NULL);
1395 argv_array_push(&argv_gc_auto, "--quiet");
1396 run_command_v_opt(argv_gc_auto.argv, RUN_GIT_CMD);
1397 argv_array_clear(&argv_gc_auto);