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_BOOLEAN(0, "all", &all,
73 N_("fetch from all remotes")),
74 OPT_BOOLEAN('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_BOOLEAN('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_BOOLEAN(0, "dry-run", &dry_run,
92 OPT_BOOLEAN('k', "keep", &keep, N_("keep downloaded pack")),
93 OPT_BOOLEAN('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)) {
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)
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 for (i = 0; i < ref_count; i++) {
177 get_fetch_map(remote_refs, &refs[i], &tail, 0);
178 if (refs[i].dst && refs[i].dst[0])
181 /* Merge everything on the command line, but not --tags */
182 for (rm = ref_map; rm; rm = rm->next)
184 if (tags == TAGS_SET)
185 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
187 /* Use the defaults */
188 struct remote *remote = transport->remote;
189 struct branch *branch = branch_get(NULL);
190 int has_merge = branch_has_merge_config(branch);
192 (remote->fetch_refspec_nr ||
193 /* Note: has_merge implies non-NULL branch->remote_name */
194 (has_merge && !strcmp(branch->remote_name, remote->name)))) {
195 for (i = 0; i < remote->fetch_refspec_nr; i++) {
196 get_fetch_map(remote_refs, &remote->fetch[i], &tail, 0);
197 if (remote->fetch[i].dst &&
198 remote->fetch[i].dst[0])
200 if (!i && !has_merge && ref_map &&
201 !remote->fetch[0].pattern)
205 * if the remote we're fetching from is the same
206 * as given in branch.<name>.remote, we add the
207 * ref given in branch.<name>.merge, too.
209 * Note: has_merge implies non-NULL branch->remote_name
212 !strcmp(branch->remote_name, remote->name))
213 add_merge_config(&ref_map, remote_refs, branch, &tail);
215 ref_map = get_remote_ref(remote_refs, "HEAD");
217 die(_("Couldn't find remote ref HEAD"));
219 tail = &ref_map->next;
222 if (tags == TAGS_DEFAULT && *autotags)
223 find_non_local_tags(transport, &ref_map, &tail);
224 ref_remove_duplicates(ref_map);
229 #define STORE_REF_ERROR_OTHER 1
230 #define STORE_REF_ERROR_DF_CONFLICT 2
232 static int s_update_ref(const char *action,
237 char *rla = getenv("GIT_REFLOG_ACTION");
238 static struct ref_lock *lock;
243 rla = default_rla.buf;
244 snprintf(msg, sizeof(msg), "%s: %s", rla, action);
245 lock = lock_any_ref_for_update(ref->name,
246 check_old ? ref->old_sha1 : NULL, 0);
248 return errno == ENOTDIR ? STORE_REF_ERROR_DF_CONFLICT :
249 STORE_REF_ERROR_OTHER;
250 if (write_ref_sha1(lock, ref->new_sha1, msg) < 0)
251 return errno == ENOTDIR ? STORE_REF_ERROR_DF_CONFLICT :
252 STORE_REF_ERROR_OTHER;
256 #define REFCOL_WIDTH 10
258 static int update_local_ref(struct ref *ref,
260 const struct ref *remote_ref,
261 struct strbuf *display)
263 struct commit *current = NULL, *updated;
264 enum object_type type;
265 struct branch *current_branch = branch_get(NULL);
266 const char *pretty_ref = prettify_refname(ref->name);
268 type = sha1_object_info(ref->new_sha1, NULL);
270 die(_("object %s not found"), sha1_to_hex(ref->new_sha1));
272 if (!hashcmp(ref->old_sha1, ref->new_sha1)) {
274 strbuf_addf(display, "= %-*s %-*s -> %s",
275 TRANSPORT_SUMMARY(_("[up to date]")),
276 REFCOL_WIDTH, remote, pretty_ref);
280 if (current_branch &&
281 !strcmp(ref->name, current_branch->name) &&
282 !(update_head_ok || is_bare_repository()) &&
283 !is_null_sha1(ref->old_sha1)) {
285 * If this is the head, and it's not okay to update
286 * the head, and the old value of the head isn't empty...
289 _("! %-*s %-*s -> %s (can't fetch in current branch)"),
290 TRANSPORT_SUMMARY(_("[rejected]")),
291 REFCOL_WIDTH, remote, pretty_ref);
295 if (!is_null_sha1(ref->old_sha1) &&
296 !prefixcmp(ref->name, "refs/tags/")) {
298 r = s_update_ref("updating tag", ref, 0);
299 strbuf_addf(display, "%c %-*s %-*s -> %s%s",
301 TRANSPORT_SUMMARY(_("[tag update]")),
302 REFCOL_WIDTH, remote, pretty_ref,
303 r ? _(" (unable to update local ref)") : "");
307 current = lookup_commit_reference_gently(ref->old_sha1, 1);
308 updated = lookup_commit_reference_gently(ref->new_sha1, 1);
309 if (!current || !updated) {
314 * Nicely describe the new ref we're fetching.
315 * Base this on the remote's ref name, as it's
316 * more likely to follow a standard layout.
318 const char *name = remote_ref ? remote_ref->name : "";
319 if (!prefixcmp(name, "refs/tags/")) {
321 what = _("[new tag]");
322 } else if (!prefixcmp(name, "refs/heads/")) {
323 msg = "storing head";
324 what = _("[new branch]");
327 what = _("[new ref]");
330 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
331 (recurse_submodules != RECURSE_SUBMODULES_ON))
332 check_for_new_submodule_commits(ref->new_sha1);
333 r = s_update_ref(msg, ref, 0);
334 strbuf_addf(display, "%c %-*s %-*s -> %s%s",
336 TRANSPORT_SUMMARY(what),
337 REFCOL_WIDTH, remote, pretty_ref,
338 r ? _(" (unable to update local ref)") : "");
342 if (in_merge_bases(current, updated)) {
345 strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
346 strcat(quickref, "..");
347 strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
348 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
349 (recurse_submodules != RECURSE_SUBMODULES_ON))
350 check_for_new_submodule_commits(ref->new_sha1);
351 r = s_update_ref("fast-forward", ref, 1);
352 strbuf_addf(display, "%c %-*s %-*s -> %s%s",
354 TRANSPORT_SUMMARY_WIDTH, quickref,
355 REFCOL_WIDTH, remote, pretty_ref,
356 r ? _(" (unable to update local ref)") : "");
358 } else if (force || ref->force) {
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("forced-update", 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") : _("forced update"));
375 strbuf_addf(display, "! %-*s %-*s -> %s %s",
376 TRANSPORT_SUMMARY(_("[rejected]")),
377 REFCOL_WIDTH, remote, pretty_ref,
378 _("(non-fast-forward)"));
383 static int iterate_ref_map(void *cb_data, unsigned char sha1[20])
385 struct ref **rm = cb_data;
386 struct ref *ref = *rm;
389 return -1; /* end of the list */
391 hashcpy(sha1, ref->old_sha1);
395 static int store_updated_refs(const char *raw_url, const char *remote_name,
399 struct commit *commit;
400 int url_len, i, shown_url = 0, rc = 0;
401 struct strbuf note = STRBUF_INIT;
402 const char *what, *kind;
404 char *url, *filename = dry_run ? "/dev/null" : git_path("FETCH_HEAD");
407 fp = fopen(filename, "a");
409 return error(_("cannot open %s: %s\n"), filename, strerror(errno));
412 url = transport_anonymize_url(raw_url);
414 url = xstrdup("foreign");
417 if (check_everything_connected(iterate_ref_map, 0, &rm)) {
418 rc = error(_("%s did not send all necessary objects\n"), url);
423 * The first pass writes objects to be merged and then the
424 * second pass writes the rest, in order to allow using
425 * FETCH_HEAD as a refname to refer to the ref to be merged.
427 for (want_merge = 1; 0 <= want_merge; want_merge--) {
428 for (rm = ref_map; rm; rm = rm->next) {
429 struct ref *ref = NULL;
431 commit = lookup_commit_reference_gently(rm->old_sha1, 1);
435 if (rm->merge != want_merge)
439 ref = xcalloc(1, sizeof(*ref) + strlen(rm->peer_ref->name) + 1);
440 strcpy(ref->name, rm->peer_ref->name);
441 hashcpy(ref->old_sha1, rm->peer_ref->old_sha1);
442 hashcpy(ref->new_sha1, rm->old_sha1);
443 ref->force = rm->peer_ref->force;
447 if (!strcmp(rm->name, "HEAD")) {
451 else if (!prefixcmp(rm->name, "refs/heads/")) {
453 what = rm->name + 11;
455 else if (!prefixcmp(rm->name, "refs/tags/")) {
457 what = rm->name + 10;
459 else if (!prefixcmp(rm->name, "refs/remotes/")) {
460 kind = "remote-tracking branch";
461 what = rm->name + 13;
468 url_len = strlen(url);
469 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
472 if (4 < i && !strncmp(".git", url + i - 3, 4))
478 strbuf_addf(¬e, "%s ", kind);
479 strbuf_addf(¬e, "'%s' of ", what);
481 fprintf(fp, "%s\t%s\t%s",
482 sha1_to_hex(rm->old_sha1),
483 rm->merge ? "" : "not-for-merge",
485 for (i = 0; i < url_len; ++i)
494 rc |= update_local_ref(ref, what, rm, ¬e);
497 strbuf_addf(¬e, "* %-*s %-*s -> FETCH_HEAD",
498 TRANSPORT_SUMMARY_WIDTH,
499 *kind ? kind : "branch",
501 *what ? what : "HEAD");
503 if (verbosity >= 0 && !shown_url) {
504 fprintf(stderr, _("From %.*s\n"),
509 fprintf(stderr, " %s\n", note.buf);
514 if (rc & STORE_REF_ERROR_DF_CONFLICT)
515 error(_("some local refs could not be updated; try running\n"
516 " 'git remote prune %s' to remove any old, conflicting "
517 "branches"), remote_name);
520 strbuf_release(¬e);
527 * We would want to bypass the object transfer altogether if
528 * everything we are going to fetch already exists and is connected
531 static int quickfetch(struct ref *ref_map)
533 struct ref *rm = ref_map;
536 * If we are deepening a shallow clone we already have these
537 * objects reachable. Running rev-list here will return with
538 * a good (0) exit status and we'll bypass the fetch that we
539 * really need to perform. Claiming failure now will ensure
540 * we perform the network exchange to deepen our history.
544 return check_everything_connected(iterate_ref_map, 1, &rm);
547 static int fetch_refs(struct transport *transport, struct ref *ref_map)
549 int ret = quickfetch(ref_map);
551 ret = transport_fetch_refs(transport, ref_map);
553 ret |= store_updated_refs(transport->url,
554 transport->remote->name,
556 transport_unlock_pack(transport);
560 static int prune_refs(struct refspec *refs, int ref_count, struct ref *ref_map)
563 struct ref *ref, *stale_refs = get_stale_heads(refs, ref_count, ref_map);
564 const char *dangling_msg = dry_run
565 ? _(" (%s will become dangling)")
566 : _(" (%s has become dangling)");
568 for (ref = stale_refs; ref; ref = ref->next) {
570 result |= delete_ref(ref->name, NULL, 0);
571 if (verbosity >= 0) {
572 fprintf(stderr, " x %-*s %-*s -> %s\n",
573 TRANSPORT_SUMMARY(_("[deleted]")),
574 REFCOL_WIDTH, _("(none)"), prettify_refname(ref->name));
575 warn_dangling_symref(stderr, dangling_msg, ref->name);
578 free_refs(stale_refs);
582 static int add_existing(const char *refname, const unsigned char *sha1,
583 int flag, void *cbdata)
585 struct string_list *list = (struct string_list *)cbdata;
586 struct string_list_item *item = string_list_insert(list, refname);
587 item->util = (void *)sha1;
591 static int will_fetch(struct ref **head, const unsigned char *sha1)
593 struct ref *rm = *head;
595 if (!hashcmp(rm->old_sha1, sha1))
602 static void find_non_local_tags(struct transport *transport,
606 struct string_list existing_refs = STRING_LIST_INIT_NODUP;
607 struct string_list remote_refs = STRING_LIST_INIT_NODUP;
608 const struct ref *ref;
609 struct string_list_item *item = NULL;
611 for_each_ref(add_existing, &existing_refs);
612 for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
613 if (prefixcmp(ref->name, "refs/tags/"))
617 * The peeled ref always follows the matching base
618 * ref, so if we see a peeled ref that we don't want
619 * to fetch then we can mark the ref entry in the list
620 * as one to ignore by setting util to NULL.
622 if (!suffixcmp(ref->name, "^{}")) {
623 if (item && !has_sha1_file(ref->old_sha1) &&
624 !will_fetch(head, ref->old_sha1) &&
625 !has_sha1_file(item->util) &&
626 !will_fetch(head, item->util))
633 * If item is non-NULL here, then we previously saw a
634 * ref not followed by a peeled reference, so we need
635 * to check if it is a lightweight tag that we want to
638 if (item && !has_sha1_file(item->util) &&
639 !will_fetch(head, item->util))
644 /* skip duplicates and refs that we already have */
645 if (string_list_has_string(&remote_refs, ref->name) ||
646 string_list_has_string(&existing_refs, ref->name))
649 item = string_list_insert(&remote_refs, ref->name);
650 item->util = (void *)ref->old_sha1;
652 string_list_clear(&existing_refs, 0);
655 * We may have a final lightweight tag that needs to be
656 * checked to see if it needs fetching.
658 if (item && !has_sha1_file(item->util) &&
659 !will_fetch(head, item->util))
663 * For all the tags in the remote_refs string list,
664 * add them to the list of refs to be fetched
666 for_each_string_list_item(item, &remote_refs) {
667 /* Unless we have already decided to ignore this item... */
670 struct ref *rm = alloc_ref(item->string);
671 rm->peer_ref = alloc_ref(item->string);
672 hashcpy(rm->old_sha1, item->util);
678 string_list_clear(&remote_refs, 0);
681 static void check_not_current_branch(struct ref *ref_map)
683 struct branch *current_branch = branch_get(NULL);
685 if (is_bare_repository() || !current_branch)
688 for (; ref_map; ref_map = ref_map->next)
689 if (ref_map->peer_ref && !strcmp(current_branch->refname,
690 ref_map->peer_ref->name))
691 die(_("Refusing to fetch into current branch %s "
692 "of non-bare repository"), current_branch->refname);
695 static int truncate_fetch_head(void)
697 char *filename = git_path("FETCH_HEAD");
698 FILE *fp = fopen(filename, "w");
701 return error(_("cannot open %s: %s\n"), filename, strerror(errno));
706 static int do_fetch(struct transport *transport,
707 struct refspec *refs, int ref_count)
709 struct string_list existing_refs = STRING_LIST_INIT_NODUP;
710 struct string_list_item *peer_item = NULL;
713 int autotags = (transport->remote->fetch_tags == 1);
715 for_each_ref(add_existing, &existing_refs);
717 if (tags == TAGS_DEFAULT) {
718 if (transport->remote->fetch_tags == 2)
720 if (transport->remote->fetch_tags == -1)
724 if (!transport->get_refs_list || !transport->fetch)
725 die(_("Don't know how to fetch from %s"), transport->url);
727 /* if not appending, truncate FETCH_HEAD */
728 if (!append && !dry_run) {
729 int errcode = truncate_fetch_head();
734 ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
736 check_not_current_branch(ref_map);
738 for (rm = ref_map; rm; rm = rm->next) {
740 peer_item = string_list_lookup(&existing_refs,
743 hashcpy(rm->peer_ref->old_sha1,
748 if (tags == TAGS_DEFAULT && autotags)
749 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
750 if (fetch_refs(transport, ref_map)) {
756 * If --tags was specified, pretend that the user gave us
757 * the canonical tags refspec
759 if (tags == TAGS_SET) {
760 const char *tags_str = "refs/tags/*:refs/tags/*";
761 struct refspec *tags_refspec, *refspec;
763 /* Copy the refspec and add the tags to it */
764 refspec = xcalloc(ref_count + 1, sizeof(struct refspec));
765 tags_refspec = parse_fetch_refspec(1, &tags_str);
766 memcpy(refspec, refs, ref_count * sizeof(struct refspec));
767 memcpy(&refspec[ref_count], tags_refspec, sizeof(struct refspec));
770 prune_refs(refspec, ref_count, ref_map);
773 /* The rest of the strings belong to fetch_one */
774 free_refspec(1, tags_refspec);
776 } else if (ref_count) {
777 prune_refs(refs, ref_count, ref_map);
779 prune_refs(transport->remote->fetch, transport->remote->fetch_refspec_nr, ref_map);
784 /* if neither --no-tags nor --tags was specified, do automated tag
786 if (tags == TAGS_DEFAULT && autotags) {
787 struct ref **tail = &ref_map;
789 find_non_local_tags(transport, &ref_map, &tail);
791 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);
792 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
793 fetch_refs(transport, ref_map);
801 static void set_option(const char *name, const char *value)
803 int r = transport_set_option(transport, name, value);
805 die(_("Option \"%s\" value \"%s\" is not valid for %s"),
806 name, value, transport->url);
808 warning(_("Option \"%s\" is ignored for %s\n"),
809 name, transport->url);
812 static int get_one_remote_for_fetch(struct remote *remote, void *priv)
814 struct string_list *list = priv;
815 if (!remote->skip_default_update)
816 string_list_append(list, remote->name);
820 struct remote_group_data {
822 struct string_list *list;
825 static int get_remote_group(const char *key, const char *value, void *priv)
827 struct remote_group_data *g = priv;
829 if (!prefixcmp(key, "remotes.") &&
830 !strcmp(key + 8, g->name)) {
831 /* split list by white space */
832 int space = strcspn(value, " \t\n");
835 string_list_append(g->list,
836 xstrndup(value, space));
838 value += space + (value[space] != '\0');
839 space = strcspn(value, " \t\n");
846 static int add_remote_or_group(const char *name, struct string_list *list)
848 int prev_nr = list->nr;
849 struct remote_group_data g;
850 g.name = name; g.list = list;
852 git_config(get_remote_group, &g);
853 if (list->nr == prev_nr) {
854 struct remote *remote;
855 if (!remote_is_configured(name))
857 remote = remote_get(name);
858 string_list_append(list, remote->name);
863 static void add_options_to_argv(struct argv_array *argv)
866 argv_array_push(argv, "--dry-run");
868 argv_array_push(argv, "--prune");
870 argv_array_push(argv, "--update-head-ok");
872 argv_array_push(argv, "--force");
874 argv_array_push(argv, "--keep");
875 if (recurse_submodules == RECURSE_SUBMODULES_ON)
876 argv_array_push(argv, "--recurse-submodules");
877 else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
878 argv_array_push(argv, "--recurse-submodules=on-demand");
879 if (tags == TAGS_SET)
880 argv_array_push(argv, "--tags");
881 else if (tags == TAGS_UNSET)
882 argv_array_push(argv, "--no-tags");
884 argv_array_push(argv, "-v");
886 argv_array_push(argv, "-v");
887 else if (verbosity < 0)
888 argv_array_push(argv, "-q");
892 static int fetch_multiple(struct string_list *list)
895 struct argv_array argv = ARGV_ARRAY_INIT;
897 if (!append && !dry_run) {
898 int errcode = truncate_fetch_head();
903 argv_array_pushl(&argv, "fetch", "--append", NULL);
904 add_options_to_argv(&argv);
906 for (i = 0; i < list->nr; i++) {
907 const char *name = list->items[i].string;
908 argv_array_push(&argv, name);
910 printf(_("Fetching %s\n"), name);
911 if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) {
912 error(_("Could not fetch %s"), name);
915 argv_array_pop(&argv);
918 argv_array_clear(&argv);
922 static int fetch_one(struct remote *remote, int argc, const char **argv)
925 static const char **refs = NULL;
926 struct refspec *refspec;
931 die(_("No remote repository specified. Please, specify either a URL or a\n"
932 "remote name from which new revisions should be fetched."));
934 transport = transport_get(remote, NULL);
937 /* no command line request */
938 if (0 <= transport->remote->prune)
939 prune = transport->remote->prune;
940 else if (0 <= fetch_prune_config)
941 prune = fetch_prune_config;
943 prune = PRUNE_BY_DEFAULT;
946 transport_set_verbosity(transport, verbosity, progress);
948 set_option(TRANS_OPT_UPLOADPACK, upload_pack);
950 set_option(TRANS_OPT_KEEP, "yes");
952 set_option(TRANS_OPT_DEPTH, depth);
956 refs = xcalloc(argc + 1, sizeof(const char *));
957 for (i = 0; i < argc; i++) {
958 if (!strcmp(argv[i], "tag")) {
962 die(_("You need to specify a tag name."));
963 ref = xmalloc(strlen(argv[i]) * 2 + 22);
964 strcpy(ref, "refs/tags/");
965 strcat(ref, argv[i]);
966 strcat(ref, ":refs/tags/");
967 strcat(ref, argv[i]);
976 sigchain_push_common(unlock_pack_on_signal);
978 refspec = parse_fetch_refspec(ref_nr, refs);
979 exit_code = do_fetch(transport, refspec, ref_nr);
980 free_refspec(ref_nr, refspec);
981 transport_disconnect(transport);
986 int cmd_fetch(int argc, const char **argv, const char *prefix)
989 struct string_list list = STRING_LIST_INIT_NODUP;
990 struct remote *remote;
992 static const char *argv_gc_auto[] = {
993 "gc", "--auto", NULL,
996 packet_trace_identity("fetch");
998 /* Record the command line for the reflog */
999 strbuf_addstr(&default_rla, "fetch");
1000 for (i = 1; i < argc; i++)
1001 strbuf_addf(&default_rla, " %s", argv[i]);
1003 git_config(git_fetch_config, NULL);
1005 argc = parse_options(argc, argv, prefix,
1006 builtin_fetch_options, builtin_fetch_usage, 0);
1010 die(_("--depth and --unshallow cannot be used together"));
1011 else if (!is_repository_shallow())
1012 die(_("--unshallow on a complete repository does not make sense"));
1014 static char inf_depth[12];
1015 sprintf(inf_depth, "%d", INFINITE_DEPTH);
1020 if (recurse_submodules != RECURSE_SUBMODULES_OFF) {
1021 if (recurse_submodules_default) {
1022 int arg = parse_fetch_recurse_submodules_arg("--recurse-submodules-default", recurse_submodules_default);
1023 set_config_fetch_recurse_submodules(arg);
1025 gitmodules_config();
1026 git_config(submodule_config, NULL);
1031 die(_("fetch --all does not take a repository argument"));
1033 die(_("fetch --all does not make sense with refspecs"));
1034 (void) for_each_remote(get_one_remote_for_fetch, &list);
1035 result = fetch_multiple(&list);
1036 } else if (argc == 0) {
1037 /* No arguments -- use default remote */
1038 remote = remote_get(NULL);
1039 result = fetch_one(remote, argc, argv);
1040 } else if (multiple) {
1041 /* All arguments are assumed to be remotes or groups */
1042 for (i = 0; i < argc; i++)
1043 if (!add_remote_or_group(argv[i], &list))
1044 die(_("No such remote or remote group: %s"), argv[i]);
1045 result = fetch_multiple(&list);
1047 /* Single remote or group */
1048 (void) add_remote_or_group(argv[0], &list);
1050 /* More than one remote */
1052 die(_("Fetching a group and specifying refspecs does not make sense"));
1053 result = fetch_multiple(&list);
1055 /* Zero or one remotes */
1056 remote = remote_get(argv[0]);
1057 result = fetch_one(remote, argc-1, argv+1);
1061 if (!result && (recurse_submodules != RECURSE_SUBMODULES_OFF)) {
1062 struct argv_array options = ARGV_ARRAY_INIT;
1064 add_options_to_argv(&options);
1065 result = fetch_populated_submodules(&options,
1069 argv_array_clear(&options);
1072 /* All names were strdup()ed or strndup()ed */
1073 list.strdup_strings = 1;
1074 string_list_clear(&list, 0);
1076 run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);