8 #include "string-list.h"
10 #include "transport.h"
11 #include "run-command.h"
12 #include "parse-options.h"
14 #include "transport.h"
15 #include "submodule.h"
16 #include "connected.h"
17 #include "argv-array.h"
19 static const char * const builtin_fetch_usage[] = {
20 N_("git fetch [<options>] [<repository> [<refspec>...]]"),
21 N_("git fetch [<options>] <group>"),
22 N_("git fetch --multiple [<options>] [(<repository> | <group>)...]"),
23 N_("git fetch --all [<options>]"),
33 static int fetch_prune_config = -1; /* unspecified */
34 static int prune = -1; /* unspecified */
35 #define PRUNE_BY_DEFAULT 0 /* do we prune by default? */
37 static int all, append, dry_run, force, keep, multiple, update_head_ok, verbosity;
38 static int progress = -1, recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
39 static int tags = TAGS_DEFAULT, unshallow;
40 static const char *depth;
41 static const char *upload_pack;
42 static struct strbuf default_rla = STRBUF_INIT;
43 static struct transport *transport;
44 static const char *submodule_prefix = "";
45 static const char *recurse_submodules_default;
47 static int option_parse_recurse_submodules(const struct option *opt,
48 const char *arg, int unset)
51 recurse_submodules = RECURSE_SUBMODULES_OFF;
54 recurse_submodules = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
56 recurse_submodules = RECURSE_SUBMODULES_ON;
61 static int git_fetch_config(const char *k, const char *v, void *cb)
63 if (!strcmp(k, "fetch.prune")) {
64 fetch_prune_config = git_config_bool(k, v);
70 static struct option builtin_fetch_options[] = {
71 OPT__VERBOSITY(&verbosity),
72 OPT_BOOL(0, "all", &all,
73 N_("fetch from all remotes")),
74 OPT_BOOL('a', "append", &append,
75 N_("append to .git/FETCH_HEAD instead of overwriting")),
76 OPT_STRING(0, "upload-pack", &upload_pack, N_("path"),
77 N_("path to upload pack on remote end")),
78 OPT__FORCE(&force, N_("force overwrite of local branch")),
79 OPT_BOOL('m', "multiple", &multiple,
80 N_("fetch from multiple remotes")),
81 OPT_SET_INT('t', "tags", &tags,
82 N_("fetch all tags and associated objects"), TAGS_SET),
83 OPT_SET_INT('n', NULL, &tags,
84 N_("do not fetch all tags (--no-tags)"), TAGS_UNSET),
85 OPT_BOOL('p', "prune", &prune,
86 N_("prune remote-tracking branches no longer on remote")),
87 { OPTION_CALLBACK, 0, "recurse-submodules", NULL, N_("on-demand"),
88 N_("control recursive fetching of submodules"),
89 PARSE_OPT_OPTARG, option_parse_recurse_submodules },
90 OPT_BOOL(0, "dry-run", &dry_run,
92 OPT_BOOL('k', "keep", &keep, N_("keep downloaded pack")),
93 OPT_BOOL('u', "update-head-ok", &update_head_ok,
94 N_("allow updating of HEAD ref")),
95 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
96 OPT_STRING(0, "depth", &depth, N_("depth"),
97 N_("deepen history of shallow clone")),
98 { OPTION_SET_INT, 0, "unshallow", &unshallow, NULL,
99 N_("convert to a complete repository"),
100 PARSE_OPT_NONEG | PARSE_OPT_NOARG, NULL, 1 },
101 { OPTION_STRING, 0, "submodule-prefix", &submodule_prefix, N_("dir"),
102 N_("prepend this to submodule path output"), PARSE_OPT_HIDDEN },
103 { OPTION_STRING, 0, "recurse-submodules-default",
104 &recurse_submodules_default, NULL,
105 N_("default mode for recursion"), PARSE_OPT_HIDDEN },
109 static void unlock_pack(void)
112 transport_unlock_pack(transport);
115 static void unlock_pack_on_signal(int signo)
122 static void add_merge_config(struct ref **head,
123 const struct ref *remote_refs,
124 struct branch *branch,
129 for (i = 0; i < branch->merge_nr; i++) {
130 struct ref *rm, **old_tail = *tail;
131 struct refspec refspec;
133 for (rm = *head; rm; rm = rm->next) {
134 if (branch_merge_matches(branch, i, rm->name)) {
135 rm->fetch_head_status = FETCH_HEAD_MERGE;
143 * Not fetched to a remote-tracking branch? We need to fetch
144 * it anyway to allow this branch's "branch.$name.merge"
145 * to be honored by 'git pull', but we do not have to
146 * fail if branch.$name.merge is misconfigured to point
147 * at a nonexisting branch. If we were indeed called by
148 * 'git pull', it will notice the misconfiguration because
149 * there is no entry in the resulting FETCH_HEAD marked
152 memset(&refspec, 0, sizeof(refspec));
153 refspec.src = branch->merge[i]->src;
154 get_fetch_map(remote_refs, &refspec, tail, 1);
155 for (rm = *old_tail; rm; rm = rm->next)
156 rm->fetch_head_status = FETCH_HEAD_MERGE;
160 static void find_non_local_tags(struct transport *transport,
164 static struct ref *get_ref_map(struct transport *transport,
165 struct refspec *refs, int ref_count, int tags,
170 struct ref *ref_map = NULL;
171 struct ref **tail = &ref_map;
173 const struct ref *remote_refs = transport_get_remote_refs(transport);
175 if (ref_count || tags == TAGS_SET) {
176 struct ref **old_tail;
178 for (i = 0; i < ref_count; i++) {
179 get_fetch_map(remote_refs, &refs[i], &tail, 0);
180 if (refs[i].dst && refs[i].dst[0])
183 /* Merge everything on the command line, but not --tags */
184 for (rm = ref_map; rm; rm = rm->next)
185 rm->fetch_head_status = FETCH_HEAD_MERGE;
186 if (tags == TAGS_SET)
187 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
190 * For any refs that we happen to be fetching via command-line
191 * arguments, take the opportunity to update their configured
192 * counterparts. However, we do not want to mention these
193 * entries in FETCH_HEAD at all, as they would simply be
194 * duplicates of existing entries.
197 for (i = 0; i < transport->remote->fetch_refspec_nr; i++)
198 get_fetch_map(ref_map, &transport->remote->fetch[i],
200 for (rm = *old_tail; rm; rm = rm->next)
201 rm->fetch_head_status = FETCH_HEAD_IGNORE;
203 /* Use the defaults */
204 struct remote *remote = transport->remote;
205 struct branch *branch = branch_get(NULL);
206 int has_merge = branch_has_merge_config(branch);
208 (remote->fetch_refspec_nr ||
209 /* Note: has_merge implies non-NULL branch->remote_name */
210 (has_merge && !strcmp(branch->remote_name, remote->name)))) {
211 for (i = 0; i < remote->fetch_refspec_nr; i++) {
212 get_fetch_map(remote_refs, &remote->fetch[i], &tail, 0);
213 if (remote->fetch[i].dst &&
214 remote->fetch[i].dst[0])
216 if (!i && !has_merge && ref_map &&
217 !remote->fetch[0].pattern)
218 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
221 * if the remote we're fetching from is the same
222 * as given in branch.<name>.remote, we add the
223 * ref given in branch.<name>.merge, too.
225 * Note: has_merge implies non-NULL branch->remote_name
228 !strcmp(branch->remote_name, remote->name))
229 add_merge_config(&ref_map, remote_refs, branch, &tail);
231 ref_map = get_remote_ref(remote_refs, "HEAD");
233 die(_("Couldn't find remote ref HEAD"));
234 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
235 tail = &ref_map->next;
238 if (tags == TAGS_DEFAULT && *autotags)
239 find_non_local_tags(transport, &ref_map, &tail);
240 ref_remove_duplicates(ref_map);
245 #define STORE_REF_ERROR_OTHER 1
246 #define STORE_REF_ERROR_DF_CONFLICT 2
248 static int s_update_ref(const char *action,
253 char *rla = getenv("GIT_REFLOG_ACTION");
254 static struct ref_lock *lock;
259 rla = default_rla.buf;
260 snprintf(msg, sizeof(msg), "%s: %s", rla, action);
261 lock = lock_any_ref_for_update(ref->name,
262 check_old ? ref->old_sha1 : NULL, 0);
264 return errno == ENOTDIR ? STORE_REF_ERROR_DF_CONFLICT :
265 STORE_REF_ERROR_OTHER;
266 if (write_ref_sha1(lock, ref->new_sha1, msg) < 0)
267 return errno == ENOTDIR ? STORE_REF_ERROR_DF_CONFLICT :
268 STORE_REF_ERROR_OTHER;
272 #define REFCOL_WIDTH 10
274 static int update_local_ref(struct ref *ref,
276 const struct ref *remote_ref,
277 struct strbuf *display)
279 struct commit *current = NULL, *updated;
280 enum object_type type;
281 struct branch *current_branch = branch_get(NULL);
282 const char *pretty_ref = prettify_refname(ref->name);
284 type = sha1_object_info(ref->new_sha1, NULL);
286 die(_("object %s not found"), sha1_to_hex(ref->new_sha1));
288 if (!hashcmp(ref->old_sha1, ref->new_sha1)) {
290 strbuf_addf(display, "= %-*s %-*s -> %s",
291 TRANSPORT_SUMMARY(_("[up to date]")),
292 REFCOL_WIDTH, remote, pretty_ref);
296 if (current_branch &&
297 !strcmp(ref->name, current_branch->name) &&
298 !(update_head_ok || is_bare_repository()) &&
299 !is_null_sha1(ref->old_sha1)) {
301 * If this is the head, and it's not okay to update
302 * the head, and the old value of the head isn't empty...
305 _("! %-*s %-*s -> %s (can't fetch in current branch)"),
306 TRANSPORT_SUMMARY(_("[rejected]")),
307 REFCOL_WIDTH, remote, pretty_ref);
311 if (!is_null_sha1(ref->old_sha1) &&
312 !prefixcmp(ref->name, "refs/tags/")) {
314 r = s_update_ref("updating tag", ref, 0);
315 strbuf_addf(display, "%c %-*s %-*s -> %s%s",
317 TRANSPORT_SUMMARY(_("[tag update]")),
318 REFCOL_WIDTH, remote, pretty_ref,
319 r ? _(" (unable to update local ref)") : "");
323 current = lookup_commit_reference_gently(ref->old_sha1, 1);
324 updated = lookup_commit_reference_gently(ref->new_sha1, 1);
325 if (!current || !updated) {
330 * Nicely describe the new ref we're fetching.
331 * Base this on the remote's ref name, as it's
332 * more likely to follow a standard layout.
334 const char *name = remote_ref ? remote_ref->name : "";
335 if (!prefixcmp(name, "refs/tags/")) {
337 what = _("[new tag]");
338 } else if (!prefixcmp(name, "refs/heads/")) {
339 msg = "storing head";
340 what = _("[new branch]");
343 what = _("[new ref]");
346 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
347 (recurse_submodules != RECURSE_SUBMODULES_ON))
348 check_for_new_submodule_commits(ref->new_sha1);
349 r = s_update_ref(msg, ref, 0);
350 strbuf_addf(display, "%c %-*s %-*s -> %s%s",
352 TRANSPORT_SUMMARY(what),
353 REFCOL_WIDTH, remote, pretty_ref,
354 r ? _(" (unable to update local ref)") : "");
358 if (in_merge_bases(current, updated)) {
361 strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
362 strcat(quickref, "..");
363 strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
364 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
365 (recurse_submodules != RECURSE_SUBMODULES_ON))
366 check_for_new_submodule_commits(ref->new_sha1);
367 r = s_update_ref("fast-forward", ref, 1);
368 strbuf_addf(display, "%c %-*s %-*s -> %s%s",
370 TRANSPORT_SUMMARY_WIDTH, quickref,
371 REFCOL_WIDTH, remote, pretty_ref,
372 r ? _(" (unable to update local ref)") : "");
374 } else if (force || ref->force) {
377 strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
378 strcat(quickref, "...");
379 strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
380 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
381 (recurse_submodules != RECURSE_SUBMODULES_ON))
382 check_for_new_submodule_commits(ref->new_sha1);
383 r = s_update_ref("forced-update", ref, 1);
384 strbuf_addf(display, "%c %-*s %-*s -> %s (%s)",
386 TRANSPORT_SUMMARY_WIDTH, quickref,
387 REFCOL_WIDTH, remote, pretty_ref,
388 r ? _("unable to update local ref") : _("forced update"));
391 strbuf_addf(display, "! %-*s %-*s -> %s %s",
392 TRANSPORT_SUMMARY(_("[rejected]")),
393 REFCOL_WIDTH, remote, pretty_ref,
394 _("(non-fast-forward)"));
399 static int iterate_ref_map(void *cb_data, unsigned char sha1[20])
401 struct ref **rm = cb_data;
402 struct ref *ref = *rm;
405 return -1; /* end of the list */
407 hashcpy(sha1, ref->old_sha1);
411 static int store_updated_refs(const char *raw_url, const char *remote_name,
415 struct commit *commit;
416 int url_len, i, shown_url = 0, rc = 0;
417 struct strbuf note = STRBUF_INIT;
418 const char *what, *kind;
420 char *url, *filename = dry_run ? "/dev/null" : git_path("FETCH_HEAD");
423 fp = fopen(filename, "a");
425 return error(_("cannot open %s: %s\n"), filename, strerror(errno));
428 url = transport_anonymize_url(raw_url);
430 url = xstrdup("foreign");
433 if (check_everything_connected(iterate_ref_map, 0, &rm)) {
434 rc = error(_("%s did not send all necessary objects\n"), url);
439 * We do a pass for each fetch_head_status type in their enum order, so
440 * merged entries are written before not-for-merge. That lets readers
441 * use FETCH_HEAD as a refname to refer to the ref to be merged.
443 for (want_status = FETCH_HEAD_MERGE;
444 want_status <= FETCH_HEAD_IGNORE;
446 for (rm = ref_map; rm; rm = rm->next) {
447 struct ref *ref = NULL;
448 const char *merge_status_marker = "";
450 commit = lookup_commit_reference_gently(rm->old_sha1, 1);
452 rm->fetch_head_status = FETCH_HEAD_NOT_FOR_MERGE;
454 if (rm->fetch_head_status != want_status)
458 ref = xcalloc(1, sizeof(*ref) + strlen(rm->peer_ref->name) + 1);
459 strcpy(ref->name, rm->peer_ref->name);
460 hashcpy(ref->old_sha1, rm->peer_ref->old_sha1);
461 hashcpy(ref->new_sha1, rm->old_sha1);
462 ref->force = rm->peer_ref->force;
466 if (!strcmp(rm->name, "HEAD")) {
470 else if (!prefixcmp(rm->name, "refs/heads/")) {
472 what = rm->name + 11;
474 else if (!prefixcmp(rm->name, "refs/tags/")) {
476 what = rm->name + 10;
478 else if (!prefixcmp(rm->name, "refs/remotes/")) {
479 kind = "remote-tracking branch";
480 what = rm->name + 13;
487 url_len = strlen(url);
488 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
491 if (4 < i && !strncmp(".git", url + i - 3, 4))
497 strbuf_addf(¬e, "%s ", kind);
498 strbuf_addf(¬e, "'%s' of ", what);
500 switch (rm->fetch_head_status) {
501 case FETCH_HEAD_NOT_FOR_MERGE:
502 merge_status_marker = "not-for-merge";
504 case FETCH_HEAD_MERGE:
505 fprintf(fp, "%s\t%s\t%s",
506 sha1_to_hex(rm->old_sha1),
509 for (i = 0; i < url_len; ++i)
517 /* do not write anything to FETCH_HEAD */
523 rc |= update_local_ref(ref, what, rm, ¬e);
526 strbuf_addf(¬e, "* %-*s %-*s -> FETCH_HEAD",
527 TRANSPORT_SUMMARY_WIDTH,
528 *kind ? kind : "branch",
530 *what ? what : "HEAD");
532 if (verbosity >= 0 && !shown_url) {
533 fprintf(stderr, _("From %.*s\n"),
538 fprintf(stderr, " %s\n", note.buf);
543 if (rc & STORE_REF_ERROR_DF_CONFLICT)
544 error(_("some local refs could not be updated; try running\n"
545 " 'git remote prune %s' to remove any old, conflicting "
546 "branches"), remote_name);
549 strbuf_release(¬e);
556 * We would want to bypass the object transfer altogether if
557 * everything we are going to fetch already exists and is connected
560 static int quickfetch(struct ref *ref_map)
562 struct ref *rm = ref_map;
565 * If we are deepening a shallow clone we already have these
566 * objects reachable. Running rev-list here will return with
567 * a good (0) exit status and we'll bypass the fetch that we
568 * really need to perform. Claiming failure now will ensure
569 * we perform the network exchange to deepen our history.
573 return check_everything_connected(iterate_ref_map, 1, &rm);
576 static int fetch_refs(struct transport *transport, struct ref *ref_map)
578 int ret = quickfetch(ref_map);
580 ret = transport_fetch_refs(transport, ref_map);
582 ret |= store_updated_refs(transport->url,
583 transport->remote->name,
585 transport_unlock_pack(transport);
589 static int prune_refs(struct refspec *refs, int ref_count, struct ref *ref_map)
592 struct ref *ref, *stale_refs = get_stale_heads(refs, ref_count, ref_map);
593 const char *dangling_msg = dry_run
594 ? _(" (%s will become dangling)")
595 : _(" (%s has become dangling)");
597 for (ref = stale_refs; ref; ref = ref->next) {
599 result |= delete_ref(ref->name, NULL, 0);
600 if (verbosity >= 0) {
601 fprintf(stderr, " x %-*s %-*s -> %s\n",
602 TRANSPORT_SUMMARY(_("[deleted]")),
603 REFCOL_WIDTH, _("(none)"), prettify_refname(ref->name));
604 warn_dangling_symref(stderr, dangling_msg, ref->name);
607 free_refs(stale_refs);
611 static int add_existing(const char *refname, const unsigned char *sha1,
612 int flag, void *cbdata)
614 struct string_list *list = (struct string_list *)cbdata;
615 struct string_list_item *item = string_list_insert(list, refname);
616 item->util = xmalloc(20);
617 hashcpy(item->util, sha1);
621 static int will_fetch(struct ref **head, const unsigned char *sha1)
623 struct ref *rm = *head;
625 if (!hashcmp(rm->old_sha1, sha1))
632 static void find_non_local_tags(struct transport *transport,
636 struct string_list existing_refs = STRING_LIST_INIT_DUP;
637 struct string_list remote_refs = STRING_LIST_INIT_NODUP;
638 const struct ref *ref;
639 struct string_list_item *item = NULL;
641 for_each_ref(add_existing, &existing_refs);
642 for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
643 if (prefixcmp(ref->name, "refs/tags/"))
647 * The peeled ref always follows the matching base
648 * ref, so if we see a peeled ref that we don't want
649 * to fetch then we can mark the ref entry in the list
650 * as one to ignore by setting util to NULL.
652 if (!suffixcmp(ref->name, "^{}")) {
653 if (item && !has_sha1_file(ref->old_sha1) &&
654 !will_fetch(head, ref->old_sha1) &&
655 !has_sha1_file(item->util) &&
656 !will_fetch(head, item->util))
663 * If item is non-NULL here, then we previously saw a
664 * ref not followed by a peeled reference, so we need
665 * to check if it is a lightweight tag that we want to
668 if (item && !has_sha1_file(item->util) &&
669 !will_fetch(head, item->util))
674 /* skip duplicates and refs that we already have */
675 if (string_list_has_string(&remote_refs, ref->name) ||
676 string_list_has_string(&existing_refs, ref->name))
679 item = string_list_insert(&remote_refs, ref->name);
680 item->util = (void *)ref->old_sha1;
682 string_list_clear(&existing_refs, 1);
685 * We may have a final lightweight tag that needs to be
686 * checked to see if it needs fetching.
688 if (item && !has_sha1_file(item->util) &&
689 !will_fetch(head, item->util))
693 * For all the tags in the remote_refs string list,
694 * add them to the list of refs to be fetched
696 for_each_string_list_item(item, &remote_refs) {
697 /* Unless we have already decided to ignore this item... */
700 struct ref *rm = alloc_ref(item->string);
701 rm->peer_ref = alloc_ref(item->string);
702 hashcpy(rm->old_sha1, item->util);
708 string_list_clear(&remote_refs, 0);
711 static void check_not_current_branch(struct ref *ref_map)
713 struct branch *current_branch = branch_get(NULL);
715 if (is_bare_repository() || !current_branch)
718 for (; ref_map; ref_map = ref_map->next)
719 if (ref_map->peer_ref && !strcmp(current_branch->refname,
720 ref_map->peer_ref->name))
721 die(_("Refusing to fetch into current branch %s "
722 "of non-bare repository"), current_branch->refname);
725 static int truncate_fetch_head(void)
727 char *filename = git_path("FETCH_HEAD");
728 FILE *fp = fopen(filename, "w");
731 return error(_("cannot open %s: %s\n"), filename, strerror(errno));
736 static int do_fetch(struct transport *transport,
737 struct refspec *refs, int ref_count)
739 struct string_list existing_refs = STRING_LIST_INIT_DUP;
742 int autotags = (transport->remote->fetch_tags == 1);
745 for_each_ref(add_existing, &existing_refs);
747 if (tags == TAGS_DEFAULT) {
748 if (transport->remote->fetch_tags == 2)
750 if (transport->remote->fetch_tags == -1)
754 if (!transport->get_refs_list || !transport->fetch)
755 die(_("Don't know how to fetch from %s"), transport->url);
757 /* if not appending, truncate FETCH_HEAD */
758 if (!append && !dry_run) {
759 retcode = truncate_fetch_head();
764 ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
766 check_not_current_branch(ref_map);
768 for (rm = ref_map; rm; rm = rm->next) {
770 struct string_list_item *peer_item =
771 string_list_lookup(&existing_refs,
774 hashcpy(rm->peer_ref->old_sha1,
779 if (tags == TAGS_DEFAULT && autotags)
780 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
781 if (fetch_refs(transport, ref_map)) {
788 * If --tags was specified, pretend that the user gave us
789 * the canonical tags refspec
791 if (tags == TAGS_SET) {
792 const char *tags_str = "refs/tags/*:refs/tags/*";
793 struct refspec *tags_refspec, *refspec;
795 /* Copy the refspec and add the tags to it */
796 refspec = xcalloc(ref_count + 1, sizeof(struct refspec));
797 tags_refspec = parse_fetch_refspec(1, &tags_str);
798 memcpy(refspec, refs, ref_count * sizeof(struct refspec));
799 memcpy(&refspec[ref_count], tags_refspec, sizeof(struct refspec));
802 prune_refs(refspec, ref_count, ref_map);
805 /* The rest of the strings belong to fetch_one */
806 free_refspec(1, tags_refspec);
808 } else if (ref_count) {
809 prune_refs(refs, ref_count, ref_map);
811 prune_refs(transport->remote->fetch, transport->remote->fetch_refspec_nr, ref_map);
816 /* if neither --no-tags nor --tags was specified, do automated tag
818 if (tags == TAGS_DEFAULT && autotags) {
819 struct ref **tail = &ref_map;
821 find_non_local_tags(transport, &ref_map, &tail);
823 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);
824 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
825 fetch_refs(transport, ref_map);
831 string_list_clear(&existing_refs, 1);
835 static void set_option(const char *name, const char *value)
837 int r = transport_set_option(transport, name, value);
839 die(_("Option \"%s\" value \"%s\" is not valid for %s"),
840 name, value, transport->url);
842 warning(_("Option \"%s\" is ignored for %s\n"),
843 name, transport->url);
846 static int get_one_remote_for_fetch(struct remote *remote, void *priv)
848 struct string_list *list = priv;
849 if (!remote->skip_default_update)
850 string_list_append(list, remote->name);
854 struct remote_group_data {
856 struct string_list *list;
859 static int get_remote_group(const char *key, const char *value, void *priv)
861 struct remote_group_data *g = priv;
863 if (!prefixcmp(key, "remotes.") &&
864 !strcmp(key + 8, g->name)) {
865 /* split list by white space */
866 int space = strcspn(value, " \t\n");
869 string_list_append(g->list,
870 xstrndup(value, space));
872 value += space + (value[space] != '\0');
873 space = strcspn(value, " \t\n");
880 static int add_remote_or_group(const char *name, struct string_list *list)
882 int prev_nr = list->nr;
883 struct remote_group_data g;
884 g.name = name; g.list = list;
886 git_config(get_remote_group, &g);
887 if (list->nr == prev_nr) {
888 struct remote *remote;
889 if (!remote_is_configured(name))
891 remote = remote_get(name);
892 string_list_append(list, remote->name);
897 static void add_options_to_argv(struct argv_array *argv)
900 argv_array_push(argv, "--dry-run");
902 argv_array_push(argv, "--prune");
904 argv_array_push(argv, "--update-head-ok");
906 argv_array_push(argv, "--force");
908 argv_array_push(argv, "--keep");
909 if (recurse_submodules == RECURSE_SUBMODULES_ON)
910 argv_array_push(argv, "--recurse-submodules");
911 else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
912 argv_array_push(argv, "--recurse-submodules=on-demand");
913 if (tags == TAGS_SET)
914 argv_array_push(argv, "--tags");
915 else if (tags == TAGS_UNSET)
916 argv_array_push(argv, "--no-tags");
918 argv_array_push(argv, "-v");
920 argv_array_push(argv, "-v");
921 else if (verbosity < 0)
922 argv_array_push(argv, "-q");
926 static int fetch_multiple(struct string_list *list)
929 struct argv_array argv = ARGV_ARRAY_INIT;
931 if (!append && !dry_run) {
932 int errcode = truncate_fetch_head();
937 argv_array_pushl(&argv, "fetch", "--append", NULL);
938 add_options_to_argv(&argv);
940 for (i = 0; i < list->nr; i++) {
941 const char *name = list->items[i].string;
942 argv_array_push(&argv, name);
944 printf(_("Fetching %s\n"), name);
945 if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) {
946 error(_("Could not fetch %s"), name);
949 argv_array_pop(&argv);
952 argv_array_clear(&argv);
956 static int fetch_one(struct remote *remote, int argc, const char **argv)
959 static const char **refs = NULL;
960 struct refspec *refspec;
965 die(_("No remote repository specified. Please, specify either a URL or a\n"
966 "remote name from which new revisions should be fetched."));
968 transport = transport_get(remote, NULL);
971 /* no command line request */
972 if (0 <= transport->remote->prune)
973 prune = transport->remote->prune;
974 else if (0 <= fetch_prune_config)
975 prune = fetch_prune_config;
977 prune = PRUNE_BY_DEFAULT;
980 transport_set_verbosity(transport, verbosity, progress);
982 set_option(TRANS_OPT_UPLOADPACK, upload_pack);
984 set_option(TRANS_OPT_KEEP, "yes");
986 set_option(TRANS_OPT_DEPTH, depth);
990 refs = xcalloc(argc + 1, sizeof(const char *));
991 for (i = 0; i < argc; i++) {
992 if (!strcmp(argv[i], "tag")) {
996 die(_("You need to specify a tag name."));
997 ref = xmalloc(strlen(argv[i]) * 2 + 22);
998 strcpy(ref, "refs/tags/");
999 strcat(ref, argv[i]);
1000 strcat(ref, ":refs/tags/");
1001 strcat(ref, argv[i]);
1004 refs[j++] = argv[i];
1010 sigchain_push_common(unlock_pack_on_signal);
1011 atexit(unlock_pack);
1012 refspec = parse_fetch_refspec(ref_nr, refs);
1013 exit_code = do_fetch(transport, refspec, ref_nr);
1014 free_refspec(ref_nr, refspec);
1015 transport_disconnect(transport);
1020 int cmd_fetch(int argc, const char **argv, const char *prefix)
1023 struct string_list list = STRING_LIST_INIT_NODUP;
1024 struct remote *remote;
1026 static const char *argv_gc_auto[] = {
1027 "gc", "--auto", NULL,
1030 packet_trace_identity("fetch");
1032 /* Record the command line for the reflog */
1033 strbuf_addstr(&default_rla, "fetch");
1034 for (i = 1; i < argc; i++)
1035 strbuf_addf(&default_rla, " %s", argv[i]);
1037 git_config(git_fetch_config, NULL);
1039 argc = parse_options(argc, argv, prefix,
1040 builtin_fetch_options, builtin_fetch_usage, 0);
1044 die(_("--depth and --unshallow cannot be used together"));
1045 else if (!is_repository_shallow())
1046 die(_("--unshallow on a complete repository does not make sense"));
1048 static char inf_depth[12];
1049 sprintf(inf_depth, "%d", INFINITE_DEPTH);
1054 if (recurse_submodules != RECURSE_SUBMODULES_OFF) {
1055 if (recurse_submodules_default) {
1056 int arg = parse_fetch_recurse_submodules_arg("--recurse-submodules-default", recurse_submodules_default);
1057 set_config_fetch_recurse_submodules(arg);
1059 gitmodules_config();
1060 git_config(submodule_config, NULL);
1065 die(_("fetch --all does not take a repository argument"));
1067 die(_("fetch --all does not make sense with refspecs"));
1068 (void) for_each_remote(get_one_remote_for_fetch, &list);
1069 result = fetch_multiple(&list);
1070 } else if (argc == 0) {
1071 /* No arguments -- use default remote */
1072 remote = remote_get(NULL);
1073 result = fetch_one(remote, argc, argv);
1074 } else if (multiple) {
1075 /* All arguments are assumed to be remotes or groups */
1076 for (i = 0; i < argc; i++)
1077 if (!add_remote_or_group(argv[i], &list))
1078 die(_("No such remote or remote group: %s"), argv[i]);
1079 result = fetch_multiple(&list);
1081 /* Single remote or group */
1082 (void) add_remote_or_group(argv[0], &list);
1084 /* More than one remote */
1086 die(_("Fetching a group and specifying refspecs does not make sense"));
1087 result = fetch_multiple(&list);
1089 /* Zero or one remotes */
1090 remote = remote_get(argv[0]);
1091 result = fetch_one(remote, argc-1, argv+1);
1095 if (!result && (recurse_submodules != RECURSE_SUBMODULES_OFF)) {
1096 struct argv_array options = ARGV_ARRAY_INIT;
1098 add_options_to_argv(&options);
1099 result = fetch_populated_submodules(&options,
1103 argv_array_clear(&options);
1106 /* All names were strdup()ed or strndup()ed */
1107 list.strdup_strings = 1;
1108 string_list_clear(&list, 0);
1110 run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);