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"
18 static const char * const builtin_fetch_usage[] = {
19 "git fetch [<options>] [<repository> [<refspec>...]]",
20 "git fetch [<options>] <group>",
21 "git fetch --multiple [<options>] [(<repository> | <group>)...]",
22 "git fetch --all [<options>]",
32 static int all, append, dry_run, force, keep, multiple, prune, update_head_ok, verbosity;
33 static int progress = -1, recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
34 static int tags = TAGS_DEFAULT;
35 static const char *depth;
36 static const char *upload_pack;
37 static struct strbuf default_rla = STRBUF_INIT;
38 static struct transport *transport;
39 static const char *submodule_prefix = "";
40 static const char *recurse_submodules_default;
42 static int option_parse_recurse_submodules(const struct option *opt,
43 const char *arg, int unset)
46 recurse_submodules = RECURSE_SUBMODULES_OFF;
49 recurse_submodules = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
51 recurse_submodules = RECURSE_SUBMODULES_ON;
56 static struct option builtin_fetch_options[] = {
57 OPT__VERBOSITY(&verbosity),
58 OPT_BOOLEAN(0, "all", &all,
59 "fetch from all remotes"),
60 OPT_BOOLEAN('a', "append", &append,
61 "append to .git/FETCH_HEAD instead of overwriting"),
62 OPT_STRING(0, "upload-pack", &upload_pack, "path",
63 "path to upload pack on remote end"),
64 OPT__FORCE(&force, "force overwrite of local branch"),
65 OPT_BOOLEAN('m', "multiple", &multiple,
66 "fetch from multiple remotes"),
67 OPT_SET_INT('t', "tags", &tags,
68 "fetch all tags and associated objects", TAGS_SET),
69 OPT_SET_INT('n', NULL, &tags,
70 "do not fetch all tags (--no-tags)", TAGS_UNSET),
71 OPT_BOOLEAN('p', "prune", &prune,
72 "prune remote-tracking branches no longer on remote"),
73 { OPTION_CALLBACK, 0, "recurse-submodules", NULL, "on-demand",
74 "control recursive fetching of submodules",
75 PARSE_OPT_OPTARG, option_parse_recurse_submodules },
76 OPT_BOOLEAN(0, "dry-run", &dry_run,
78 OPT_BOOLEAN('k', "keep", &keep, "keep downloaded pack"),
79 OPT_BOOLEAN('u', "update-head-ok", &update_head_ok,
80 "allow updating of HEAD ref"),
81 OPT_BOOL(0, "progress", &progress, "force progress reporting"),
82 OPT_STRING(0, "depth", &depth, "depth",
83 "deepen history of shallow clone"),
84 { OPTION_STRING, 0, "submodule-prefix", &submodule_prefix, "dir",
85 "prepend this to submodule path output", PARSE_OPT_HIDDEN },
86 { OPTION_STRING, 0, "recurse-submodules-default",
87 &recurse_submodules_default, NULL,
88 "default mode for recursion", PARSE_OPT_HIDDEN },
92 static void unlock_pack(void)
95 transport_unlock_pack(transport);
98 static void unlock_pack_on_signal(int signo)
105 static void add_merge_config(struct ref **head,
106 const struct ref *remote_refs,
107 struct branch *branch,
112 for (i = 0; i < branch->merge_nr; i++) {
113 struct ref *rm, **old_tail = *tail;
114 struct refspec refspec;
116 for (rm = *head; rm; rm = rm->next) {
117 if (branch_merge_matches(branch, i, rm->name)) {
126 * Not fetched to a remote-tracking branch? We need to fetch
127 * it anyway to allow this branch's "branch.$name.merge"
128 * to be honored by 'git pull', but we do not have to
129 * fail if branch.$name.merge is misconfigured to point
130 * at a nonexisting branch. If we were indeed called by
131 * 'git pull', it will notice the misconfiguration because
132 * there is no entry in the resulting FETCH_HEAD marked
135 memset(&refspec, 0, sizeof(refspec));
136 refspec.src = branch->merge[i]->src;
137 get_fetch_map(remote_refs, &refspec, tail, 1);
138 for (rm = *old_tail; rm; rm = rm->next)
143 static void find_non_local_tags(struct transport *transport,
147 static struct ref *get_ref_map(struct transport *transport,
148 struct refspec *refs, int ref_count, int tags,
153 struct ref *ref_map = NULL;
154 struct ref **tail = &ref_map;
156 const struct ref *remote_refs = transport_get_remote_refs(transport);
158 if (ref_count || tags == TAGS_SET) {
159 for (i = 0; i < ref_count; i++) {
160 get_fetch_map(remote_refs, &refs[i], &tail, 0);
161 if (refs[i].dst && refs[i].dst[0])
164 /* Merge everything on the command line, but not --tags */
165 for (rm = ref_map; rm; rm = rm->next)
167 if (tags == TAGS_SET)
168 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
170 /* Use the defaults */
171 struct remote *remote = transport->remote;
172 struct branch *branch = branch_get(NULL);
173 int has_merge = branch_has_merge_config(branch);
175 (remote->fetch_refspec_nr ||
176 /* Note: has_merge implies non-NULL branch->remote_name */
177 (has_merge && !strcmp(branch->remote_name, remote->name)))) {
178 for (i = 0; i < remote->fetch_refspec_nr; i++) {
179 get_fetch_map(remote_refs, &remote->fetch[i], &tail, 0);
180 if (remote->fetch[i].dst &&
181 remote->fetch[i].dst[0])
183 if (!i && !has_merge && ref_map &&
184 !remote->fetch[0].pattern)
188 * if the remote we're fetching from is the same
189 * as given in branch.<name>.remote, we add the
190 * ref given in branch.<name>.merge, too.
192 * Note: has_merge implies non-NULL branch->remote_name
195 !strcmp(branch->remote_name, remote->name))
196 add_merge_config(&ref_map, remote_refs, branch, &tail);
198 ref_map = get_remote_ref(remote_refs, "HEAD");
200 die(_("Couldn't find remote ref HEAD"));
202 tail = &ref_map->next;
205 if (tags == TAGS_DEFAULT && *autotags)
206 find_non_local_tags(transport, &ref_map, &tail);
207 ref_remove_duplicates(ref_map);
212 #define STORE_REF_ERROR_OTHER 1
213 #define STORE_REF_ERROR_DF_CONFLICT 2
215 static int s_update_ref(const char *action,
220 char *rla = getenv("GIT_REFLOG_ACTION");
221 static struct ref_lock *lock;
226 rla = default_rla.buf;
227 snprintf(msg, sizeof(msg), "%s: %s", rla, action);
228 lock = lock_any_ref_for_update(ref->name,
229 check_old ? ref->old_sha1 : NULL, 0);
231 return errno == ENOTDIR ? STORE_REF_ERROR_DF_CONFLICT :
232 STORE_REF_ERROR_OTHER;
233 if (write_ref_sha1(lock, ref->new_sha1, msg) < 0)
234 return errno == ENOTDIR ? STORE_REF_ERROR_DF_CONFLICT :
235 STORE_REF_ERROR_OTHER;
239 #define REFCOL_WIDTH 10
241 static int update_local_ref(struct ref *ref,
243 const struct ref *remote_ref,
244 struct strbuf *display)
246 struct commit *current = NULL, *updated;
247 enum object_type type;
248 struct branch *current_branch = branch_get(NULL);
249 const char *pretty_ref = prettify_refname(ref->name);
251 type = sha1_object_info(ref->new_sha1, NULL);
253 die(_("object %s not found"), sha1_to_hex(ref->new_sha1));
255 if (!hashcmp(ref->old_sha1, ref->new_sha1)) {
257 strbuf_addf(display, "= %-*s %-*s -> %s",
258 TRANSPORT_SUMMARY(_("[up to date]")),
259 REFCOL_WIDTH, remote, pretty_ref);
263 if (current_branch &&
264 !strcmp(ref->name, current_branch->name) &&
265 !(update_head_ok || is_bare_repository()) &&
266 !is_null_sha1(ref->old_sha1)) {
268 * If this is the head, and it's not okay to update
269 * the head, and the old value of the head isn't empty...
272 _("! %-*s %-*s -> %s (can't fetch in current branch)"),
273 TRANSPORT_SUMMARY(_("[rejected]")),
274 REFCOL_WIDTH, remote, pretty_ref);
278 if (!is_null_sha1(ref->old_sha1) &&
279 !prefixcmp(ref->name, "refs/tags/")) {
281 r = s_update_ref("updating tag", ref, 0);
282 strbuf_addf(display, "%c %-*s %-*s -> %s%s",
284 TRANSPORT_SUMMARY(_("[tag update]")),
285 REFCOL_WIDTH, remote, pretty_ref,
286 r ? _(" (unable to update local ref)") : "");
290 current = lookup_commit_reference_gently(ref->old_sha1, 1);
291 updated = lookup_commit_reference_gently(ref->new_sha1, 1);
292 if (!current || !updated) {
297 * Nicely describe the new ref we're fetching.
298 * Base this on the remote's ref name, as it's
299 * more likely to follow a standard layout.
301 const char *name = remote_ref ? remote_ref->name : "";
302 if (!prefixcmp(name, "refs/tags/")) {
304 what = _("[new tag]");
305 } else if (!prefixcmp(name, "refs/heads/")) {
306 msg = "storing head";
307 what = _("[new branch]");
310 what = _("[new ref]");
313 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
314 (recurse_submodules != RECURSE_SUBMODULES_ON))
315 check_for_new_submodule_commits(ref->new_sha1);
316 r = s_update_ref(msg, ref, 0);
317 strbuf_addf(display, "%c %-*s %-*s -> %s%s",
319 TRANSPORT_SUMMARY(what),
320 REFCOL_WIDTH, remote, pretty_ref,
321 r ? _(" (unable to update local ref)") : "");
325 if (in_merge_bases(current, &updated, 1)) {
328 strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
329 strcat(quickref, "..");
330 strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
331 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
332 (recurse_submodules != RECURSE_SUBMODULES_ON))
333 check_for_new_submodule_commits(ref->new_sha1);
334 r = s_update_ref("fast-forward", ref, 1);
335 strbuf_addf(display, "%c %-*s %-*s -> %s%s",
337 TRANSPORT_SUMMARY_WIDTH, quickref,
338 REFCOL_WIDTH, remote, pretty_ref,
339 r ? _(" (unable to update local ref)") : "");
341 } else if (force || ref->force) {
344 strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
345 strcat(quickref, "...");
346 strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
347 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
348 (recurse_submodules != RECURSE_SUBMODULES_ON))
349 check_for_new_submodule_commits(ref->new_sha1);
350 r = s_update_ref("forced-update", ref, 1);
351 strbuf_addf(display, "%c %-*s %-*s -> %s (%s)",
353 TRANSPORT_SUMMARY_WIDTH, quickref,
354 REFCOL_WIDTH, remote, pretty_ref,
355 r ? _("unable to update local ref") : _("forced update"));
358 strbuf_addf(display, "! %-*s %-*s -> %s %s",
359 TRANSPORT_SUMMARY(_("[rejected]")),
360 REFCOL_WIDTH, remote, pretty_ref,
361 _("(non-fast-forward)"));
366 static int iterate_ref_map(void *cb_data, unsigned char sha1[20])
368 struct ref **rm = cb_data;
369 struct ref *ref = *rm;
372 return -1; /* end of the list */
374 hashcpy(sha1, ref->old_sha1);
378 static int store_updated_refs(const char *raw_url, const char *remote_name,
382 struct commit *commit;
383 int url_len, i, shown_url = 0, rc = 0;
384 struct strbuf note = STRBUF_INIT;
385 const char *what, *kind;
387 char *url, *filename = dry_run ? "/dev/null" : git_path("FETCH_HEAD");
390 fp = fopen(filename, "a");
392 return error(_("cannot open %s: %s\n"), filename, strerror(errno));
395 url = transport_anonymize_url(raw_url);
397 url = xstrdup("foreign");
400 if (check_everything_connected(iterate_ref_map, 0, &rm)) {
401 rc = error(_("%s did not send all necessary objects\n"), url);
406 * The first pass writes objects to be merged and then the
407 * second pass writes the rest, in order to allow using
408 * FETCH_HEAD as a refname to refer to the ref to be merged.
410 for (want_merge = 1; 0 <= want_merge; want_merge--) {
411 for (rm = ref_map; rm; rm = rm->next) {
412 struct ref *ref = NULL;
414 commit = lookup_commit_reference_gently(rm->old_sha1, 1);
418 if (rm->merge != want_merge)
422 ref = xcalloc(1, sizeof(*ref) + strlen(rm->peer_ref->name) + 1);
423 strcpy(ref->name, rm->peer_ref->name);
424 hashcpy(ref->old_sha1, rm->peer_ref->old_sha1);
425 hashcpy(ref->new_sha1, rm->old_sha1);
426 ref->force = rm->peer_ref->force;
430 if (!strcmp(rm->name, "HEAD")) {
434 else if (!prefixcmp(rm->name, "refs/heads/")) {
436 what = rm->name + 11;
438 else if (!prefixcmp(rm->name, "refs/tags/")) {
440 what = rm->name + 10;
442 else if (!prefixcmp(rm->name, "refs/remotes/")) {
443 kind = "remote-tracking branch";
444 what = rm->name + 13;
451 url_len = strlen(url);
452 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
455 if (4 < i && !strncmp(".git", url + i - 3, 4))
461 strbuf_addf(¬e, "%s ", kind);
462 strbuf_addf(¬e, "'%s' of ", what);
464 fprintf(fp, "%s\t%s\t%s",
465 sha1_to_hex(rm->old_sha1),
466 rm->merge ? "" : "not-for-merge",
468 for (i = 0; i < url_len; ++i)
477 rc |= update_local_ref(ref, what, rm, ¬e);
480 strbuf_addf(¬e, "* %-*s %-*s -> FETCH_HEAD",
481 TRANSPORT_SUMMARY_WIDTH,
482 *kind ? kind : "branch",
484 *what ? what : "HEAD");
486 if (verbosity >= 0 && !shown_url) {
487 fprintf(stderr, _("From %.*s\n"),
492 fprintf(stderr, " %s\n", note.buf);
497 if (rc & STORE_REF_ERROR_DF_CONFLICT)
498 error(_("some local refs could not be updated; try running\n"
499 " 'git remote prune %s' to remove any old, conflicting "
500 "branches"), remote_name);
503 strbuf_release(¬e);
510 * We would want to bypass the object transfer altogether if
511 * everything we are going to fetch already exists and is connected
514 static int quickfetch(struct ref *ref_map)
516 struct ref *rm = ref_map;
519 * If we are deepening a shallow clone we already have these
520 * objects reachable. Running rev-list here will return with
521 * a good (0) exit status and we'll bypass the fetch that we
522 * really need to perform. Claiming failure now will ensure
523 * we perform the network exchange to deepen our history.
527 return check_everything_connected(iterate_ref_map, 1, &rm);
530 static int fetch_refs(struct transport *transport, struct ref *ref_map)
532 int ret = quickfetch(ref_map);
534 ret = transport_fetch_refs(transport, ref_map);
536 ret |= store_updated_refs(transport->url,
537 transport->remote->name,
539 transport_unlock_pack(transport);
543 static int prune_refs(struct refspec *refs, int ref_count, struct ref *ref_map)
546 struct ref *ref, *stale_refs = get_stale_heads(refs, ref_count, ref_map);
547 const char *dangling_msg = dry_run
548 ? _(" (%s will become dangling)")
549 : _(" (%s has become dangling)");
551 for (ref = stale_refs; ref; ref = ref->next) {
553 result |= delete_ref(ref->name, NULL, 0);
554 if (verbosity >= 0) {
555 fprintf(stderr, " x %-*s %-*s -> %s\n",
556 TRANSPORT_SUMMARY(_("[deleted]")),
557 REFCOL_WIDTH, _("(none)"), prettify_refname(ref->name));
558 warn_dangling_symref(stderr, dangling_msg, ref->name);
561 free_refs(stale_refs);
565 static int add_existing(const char *refname, const unsigned char *sha1,
566 int flag, void *cbdata)
568 struct string_list *list = (struct string_list *)cbdata;
569 struct string_list_item *item = string_list_insert(list, refname);
570 item->util = (void *)sha1;
574 static int will_fetch(struct ref **head, const unsigned char *sha1)
576 struct ref *rm = *head;
578 if (!hashcmp(rm->old_sha1, sha1))
585 static void find_non_local_tags(struct transport *transport,
589 struct string_list existing_refs = STRING_LIST_INIT_NODUP;
590 struct string_list remote_refs = STRING_LIST_INIT_NODUP;
591 const struct ref *ref;
592 struct string_list_item *item = NULL;
594 for_each_ref(add_existing, &existing_refs);
595 for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
596 if (prefixcmp(ref->name, "refs/tags/"))
600 * The peeled ref always follows the matching base
601 * ref, so if we see a peeled ref that we don't want
602 * to fetch then we can mark the ref entry in the list
603 * as one to ignore by setting util to NULL.
605 if (!suffixcmp(ref->name, "^{}")) {
606 if (item && !has_sha1_file(ref->old_sha1) &&
607 !will_fetch(head, ref->old_sha1) &&
608 !has_sha1_file(item->util) &&
609 !will_fetch(head, item->util))
616 * If item is non-NULL here, then we previously saw a
617 * ref not followed by a peeled reference, so we need
618 * to check if it is a lightweight tag that we want to
621 if (item && !has_sha1_file(item->util) &&
622 !will_fetch(head, item->util))
627 /* skip duplicates and refs that we already have */
628 if (string_list_has_string(&remote_refs, ref->name) ||
629 string_list_has_string(&existing_refs, ref->name))
632 item = string_list_insert(&remote_refs, ref->name);
633 item->util = (void *)ref->old_sha1;
635 string_list_clear(&existing_refs, 0);
638 * We may have a final lightweight tag that needs to be
639 * checked to see if it needs fetching.
641 if (item && !has_sha1_file(item->util) &&
642 !will_fetch(head, item->util))
646 * For all the tags in the remote_refs string list,
647 * add them to the list of refs to be fetched
649 for_each_string_list_item(item, &remote_refs) {
650 /* Unless we have already decided to ignore this item... */
653 struct ref *rm = alloc_ref(item->string);
654 rm->peer_ref = alloc_ref(item->string);
655 hashcpy(rm->old_sha1, item->util);
661 string_list_clear(&remote_refs, 0);
664 static void check_not_current_branch(struct ref *ref_map)
666 struct branch *current_branch = branch_get(NULL);
668 if (is_bare_repository() || !current_branch)
671 for (; ref_map; ref_map = ref_map->next)
672 if (ref_map->peer_ref && !strcmp(current_branch->refname,
673 ref_map->peer_ref->name))
674 die(_("Refusing to fetch into current branch %s "
675 "of non-bare repository"), current_branch->refname);
678 static int truncate_fetch_head(void)
680 char *filename = git_path("FETCH_HEAD");
681 FILE *fp = fopen(filename, "w");
684 return error(_("cannot open %s: %s\n"), filename, strerror(errno));
689 static int do_fetch(struct transport *transport,
690 struct refspec *refs, int ref_count)
692 struct string_list existing_refs = STRING_LIST_INIT_NODUP;
693 struct string_list_item *peer_item = NULL;
696 int autotags = (transport->remote->fetch_tags == 1);
698 for_each_ref(add_existing, &existing_refs);
700 if (tags == TAGS_DEFAULT) {
701 if (transport->remote->fetch_tags == 2)
703 if (transport->remote->fetch_tags == -1)
707 if (!transport->get_refs_list || !transport->fetch)
708 die(_("Don't know how to fetch from %s"), transport->url);
710 /* if not appending, truncate FETCH_HEAD */
711 if (!append && !dry_run) {
712 int errcode = truncate_fetch_head();
717 ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
719 check_not_current_branch(ref_map);
721 for (rm = ref_map; rm; rm = rm->next) {
723 peer_item = string_list_lookup(&existing_refs,
726 hashcpy(rm->peer_ref->old_sha1,
731 if (tags == TAGS_DEFAULT && autotags)
732 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
733 if (fetch_refs(transport, ref_map)) {
738 /* If --tags was specified, pretend the user gave us the canonical tags refspec */
739 if (tags == TAGS_SET) {
740 const char *tags_str = "refs/tags/*:refs/tags/*";
741 struct refspec *tags_refspec, *refspec;
743 /* Copy the refspec and add the tags to it */
744 refspec = xcalloc(ref_count + 1, sizeof(struct refspec));
745 tags_refspec = parse_fetch_refspec(1, &tags_str);
746 memcpy(refspec, refs, ref_count * sizeof(struct refspec));
747 memcpy(&refspec[ref_count], tags_refspec, sizeof(struct refspec));
750 prune_refs(refspec, ref_count, ref_map);
753 /* The rest of the strings belong to fetch_one */
754 free_refspec(1, tags_refspec);
756 } else if (ref_count) {
757 prune_refs(refs, ref_count, ref_map);
759 prune_refs(transport->remote->fetch, transport->remote->fetch_refspec_nr, ref_map);
764 /* if neither --no-tags nor --tags was specified, do automated tag
766 if (tags == TAGS_DEFAULT && autotags) {
767 struct ref **tail = &ref_map;
769 find_non_local_tags(transport, &ref_map, &tail);
771 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);
772 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
773 fetch_refs(transport, ref_map);
781 static void set_option(const char *name, const char *value)
783 int r = transport_set_option(transport, name, value);
785 die(_("Option \"%s\" value \"%s\" is not valid for %s"),
786 name, value, transport->url);
788 warning(_("Option \"%s\" is ignored for %s\n"),
789 name, transport->url);
792 static int get_one_remote_for_fetch(struct remote *remote, void *priv)
794 struct string_list *list = priv;
795 if (!remote->skip_default_update)
796 string_list_append(list, remote->name);
800 struct remote_group_data {
802 struct string_list *list;
805 static int get_remote_group(const char *key, const char *value, void *priv)
807 struct remote_group_data *g = priv;
809 if (!prefixcmp(key, "remotes.") &&
810 !strcmp(key + 8, g->name)) {
811 /* split list by white space */
812 int space = strcspn(value, " \t\n");
815 string_list_append(g->list,
816 xstrndup(value, space));
818 value += space + (value[space] != '\0');
819 space = strcspn(value, " \t\n");
826 static int add_remote_or_group(const char *name, struct string_list *list)
828 int prev_nr = list->nr;
829 struct remote_group_data g;
830 g.name = name; g.list = list;
832 git_config(get_remote_group, &g);
833 if (list->nr == prev_nr) {
834 struct remote *remote;
835 if (!remote_is_configured(name))
837 remote = remote_get(name);
838 string_list_append(list, remote->name);
843 static void add_options_to_argv(int *argc, const char **argv)
846 argv[(*argc)++] = "--dry-run";
848 argv[(*argc)++] = "--prune";
850 argv[(*argc)++] = "--update-head-ok";
852 argv[(*argc)++] = "--force";
854 argv[(*argc)++] = "--keep";
855 if (recurse_submodules == RECURSE_SUBMODULES_ON)
856 argv[(*argc)++] = "--recurse-submodules";
857 else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
858 argv[(*argc)++] = "--recurse-submodules=on-demand";
860 argv[(*argc)++] = "-v";
862 argv[(*argc)++] = "-v";
863 else if (verbosity < 0)
864 argv[(*argc)++] = "-q";
868 static int fetch_multiple(struct string_list *list)
871 const char *argv[12] = { "fetch", "--append" };
874 add_options_to_argv(&argc, argv);
876 if (!append && !dry_run) {
877 int errcode = truncate_fetch_head();
882 for (i = 0; i < list->nr; i++) {
883 const char *name = list->items[i].string;
885 argv[argc + 1] = NULL;
887 printf(_("Fetching %s\n"), name);
888 if (run_command_v_opt(argv, RUN_GIT_CMD)) {
889 error(_("Could not fetch %s"), name);
897 static int fetch_one(struct remote *remote, int argc, const char **argv)
900 static const char **refs = NULL;
901 struct refspec *refspec;
906 die(_("No remote repository specified. Please, specify either a URL or a\n"
907 "remote name from which new revisions should be fetched."));
909 transport = transport_get(remote, NULL);
910 transport_set_verbosity(transport, verbosity, progress);
912 set_option(TRANS_OPT_UPLOADPACK, upload_pack);
914 set_option(TRANS_OPT_KEEP, "yes");
916 set_option(TRANS_OPT_DEPTH, depth);
920 refs = xcalloc(argc + 1, sizeof(const char *));
921 for (i = 0; i < argc; i++) {
922 if (!strcmp(argv[i], "tag")) {
926 die(_("You need to specify a tag name."));
927 ref = xmalloc(strlen(argv[i]) * 2 + 22);
928 strcpy(ref, "refs/tags/");
929 strcat(ref, argv[i]);
930 strcat(ref, ":refs/tags/");
931 strcat(ref, argv[i]);
940 sigchain_push_common(unlock_pack_on_signal);
942 refspec = parse_fetch_refspec(ref_nr, refs);
943 exit_code = do_fetch(transport, refspec, ref_nr);
944 free_refspec(ref_nr, refspec);
945 transport_disconnect(transport);
950 int cmd_fetch(int argc, const char **argv, const char *prefix)
953 struct string_list list = STRING_LIST_INIT_NODUP;
954 struct remote *remote;
957 packet_trace_identity("fetch");
959 /* Record the command line for the reflog */
960 strbuf_addstr(&default_rla, "fetch");
961 for (i = 1; i < argc; i++)
962 strbuf_addf(&default_rla, " %s", argv[i]);
964 argc = parse_options(argc, argv, prefix,
965 builtin_fetch_options, builtin_fetch_usage, 0);
967 if (recurse_submodules != RECURSE_SUBMODULES_OFF) {
968 if (recurse_submodules_default) {
969 int arg = parse_fetch_recurse_submodules_arg("--recurse-submodules-default", recurse_submodules_default);
970 set_config_fetch_recurse_submodules(arg);
973 git_config(submodule_config, NULL);
978 die(_("fetch --all does not take a repository argument"));
980 die(_("fetch --all does not make sense with refspecs"));
981 (void) for_each_remote(get_one_remote_for_fetch, &list);
982 result = fetch_multiple(&list);
983 } else if (argc == 0) {
984 /* No arguments -- use default remote */
985 remote = remote_get(NULL);
986 result = fetch_one(remote, argc, argv);
987 } else if (multiple) {
988 /* All arguments are assumed to be remotes or groups */
989 for (i = 0; i < argc; i++)
990 if (!add_remote_or_group(argv[i], &list))
991 die(_("No such remote or remote group: %s"), argv[i]);
992 result = fetch_multiple(&list);
994 /* Single remote or group */
995 (void) add_remote_or_group(argv[0], &list);
997 /* More than one remote */
999 die(_("Fetching a group and specifying refspecs does not make sense"));
1000 result = fetch_multiple(&list);
1002 /* Zero or one remotes */
1003 remote = remote_get(argv[0]);
1004 result = fetch_one(remote, argc-1, argv+1);
1008 if (!result && (recurse_submodules != RECURSE_SUBMODULES_OFF)) {
1009 const char *options[10];
1010 int num_options = 0;
1011 add_options_to_argv(&num_options, options);
1012 result = fetch_populated_submodules(num_options, options,
1018 /* All names were strdup()ed or strndup()ed */
1019 list.strdup_strings = 1;
1020 string_list_clear(&list, 0);