t2019-checkout-ambiguous-ref.sh: depend on C_LOCALE_OUTPUT
[git/mingw/j6t.git] / builtin / fetch.c
blob6cbb5f69ecf0946f4028b88563347332d54f65bc
1 /*
2 * "git fetch"
3 */
4 #include "cache.h"
5 #include "refs.h"
6 #include "commit.h"
7 #include "builtin.h"
8 #include "string-list.h"
9 #include "remote.h"
10 #include "transport.h"
11 #include "run-command.h"
12 #include "parse-options.h"
13 #include "sigchain.h"
14 #include "transport.h"
15 #include "submodule.h"
17 static const char * const builtin_fetch_usage[] = {
18 "git fetch [<options>] [<repository> [<refspec>...]]",
19 "git fetch [<options>] <group>",
20 "git fetch --multiple [<options>] [(<repository> | <group>)...]",
21 "git fetch --all [<options>]",
22 NULL
25 enum {
26 TAGS_UNSET = 0,
27 TAGS_DEFAULT = 1,
28 TAGS_SET = 2
31 enum {
32 RECURSE_SUBMODULES_OFF = 0,
33 RECURSE_SUBMODULES_DEFAULT = 1,
34 RECURSE_SUBMODULES_ON = 2
37 static int all, append, dry_run, force, keep, multiple, prune, update_head_ok, verbosity;
38 static int progress, recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
39 static int tags = TAGS_DEFAULT;
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 = "";
46 static struct option builtin_fetch_options[] = {
47 OPT__VERBOSITY(&verbosity),
48 OPT_BOOLEAN(0, "all", &all,
49 "fetch from all remotes"),
50 OPT_BOOLEAN('a', "append", &append,
51 "append to .git/FETCH_HEAD instead of overwriting"),
52 OPT_STRING(0, "upload-pack", &upload_pack, "path",
53 "path to upload pack on remote end"),
54 OPT__FORCE(&force, "force overwrite of local branch"),
55 OPT_BOOLEAN('m', "multiple", &multiple,
56 "fetch from multiple remotes"),
57 OPT_SET_INT('t', "tags", &tags,
58 "fetch all tags and associated objects", TAGS_SET),
59 OPT_SET_INT('n', NULL, &tags,
60 "do not fetch all tags (--no-tags)", TAGS_UNSET),
61 OPT_BOOLEAN('p', "prune", &prune,
62 "prune remote-tracking branches no longer on remote"),
63 OPT_SET_INT(0, "recurse-submodules", &recurse_submodules,
64 "control recursive fetching of submodules",
65 RECURSE_SUBMODULES_ON),
66 OPT_BOOLEAN(0, "dry-run", &dry_run,
67 "dry run"),
68 OPT_BOOLEAN('k', "keep", &keep, "keep downloaded pack"),
69 OPT_BOOLEAN('u', "update-head-ok", &update_head_ok,
70 "allow updating of HEAD ref"),
71 OPT_BOOLEAN(0, "progress", &progress, "force progress reporting"),
72 OPT_STRING(0, "depth", &depth, "depth",
73 "deepen history of shallow clone"),
74 { OPTION_STRING, 0, "submodule-prefix", &submodule_prefix, "dir",
75 "prepend this to submodule path output", PARSE_OPT_HIDDEN },
76 OPT_END()
79 static void unlock_pack(void)
81 if (transport)
82 transport_unlock_pack(transport);
85 static void unlock_pack_on_signal(int signo)
87 unlock_pack();
88 sigchain_pop(signo);
89 raise(signo);
92 static void add_merge_config(struct ref **head,
93 const struct ref *remote_refs,
94 struct branch *branch,
95 struct ref ***tail)
97 int i;
99 for (i = 0; i < branch->merge_nr; i++) {
100 struct ref *rm, **old_tail = *tail;
101 struct refspec refspec;
103 for (rm = *head; rm; rm = rm->next) {
104 if (branch_merge_matches(branch, i, rm->name)) {
105 rm->merge = 1;
106 break;
109 if (rm)
110 continue;
113 * Not fetched to a remote-tracking branch? We need to fetch
114 * it anyway to allow this branch's "branch.$name.merge"
115 * to be honored by 'git pull', but we do not have to
116 * fail if branch.$name.merge is misconfigured to point
117 * at a nonexisting branch. If we were indeed called by
118 * 'git pull', it will notice the misconfiguration because
119 * there is no entry in the resulting FETCH_HEAD marked
120 * for merging.
122 memset(&refspec, 0, sizeof(refspec));
123 refspec.src = branch->merge[i]->src;
124 get_fetch_map(remote_refs, &refspec, tail, 1);
125 for (rm = *old_tail; rm; rm = rm->next)
126 rm->merge = 1;
130 static void find_non_local_tags(struct transport *transport,
131 struct ref **head,
132 struct ref ***tail);
134 static struct ref *get_ref_map(struct transport *transport,
135 struct refspec *refs, int ref_count, int tags,
136 int *autotags)
138 int i;
139 struct ref *rm;
140 struct ref *ref_map = NULL;
141 struct ref **tail = &ref_map;
143 const struct ref *remote_refs = transport_get_remote_refs(transport);
145 if (ref_count || tags == TAGS_SET) {
146 for (i = 0; i < ref_count; i++) {
147 get_fetch_map(remote_refs, &refs[i], &tail, 0);
148 if (refs[i].dst && refs[i].dst[0])
149 *autotags = 1;
151 /* Merge everything on the command line, but not --tags */
152 for (rm = ref_map; rm; rm = rm->next)
153 rm->merge = 1;
154 if (tags == TAGS_SET)
155 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
156 } else {
157 /* Use the defaults */
158 struct remote *remote = transport->remote;
159 struct branch *branch = branch_get(NULL);
160 int has_merge = branch_has_merge_config(branch);
161 if (remote &&
162 (remote->fetch_refspec_nr ||
163 /* Note: has_merge implies non-NULL branch->remote_name */
164 (has_merge && !strcmp(branch->remote_name, remote->name)))) {
165 for (i = 0; i < remote->fetch_refspec_nr; i++) {
166 get_fetch_map(remote_refs, &remote->fetch[i], &tail, 0);
167 if (remote->fetch[i].dst &&
168 remote->fetch[i].dst[0])
169 *autotags = 1;
170 if (!i && !has_merge && ref_map &&
171 !remote->fetch[0].pattern)
172 ref_map->merge = 1;
175 * if the remote we're fetching from is the same
176 * as given in branch.<name>.remote, we add the
177 * ref given in branch.<name>.merge, too.
179 * Note: has_merge implies non-NULL branch->remote_name
181 if (has_merge &&
182 !strcmp(branch->remote_name, remote->name))
183 add_merge_config(&ref_map, remote_refs, branch, &tail);
184 } else {
185 ref_map = get_remote_ref(remote_refs, "HEAD");
186 if (!ref_map)
187 die(_("Couldn't find remote ref HEAD"));
188 ref_map->merge = 1;
189 tail = &ref_map->next;
192 if (tags == TAGS_DEFAULT && *autotags)
193 find_non_local_tags(transport, &ref_map, &tail);
194 ref_remove_duplicates(ref_map);
196 return ref_map;
199 #define STORE_REF_ERROR_OTHER 1
200 #define STORE_REF_ERROR_DF_CONFLICT 2
202 static int s_update_ref(const char *action,
203 struct ref *ref,
204 int check_old)
206 char msg[1024];
207 char *rla = getenv("GIT_REFLOG_ACTION");
208 static struct ref_lock *lock;
210 if (dry_run)
211 return 0;
212 if (!rla)
213 rla = default_rla.buf;
214 snprintf(msg, sizeof(msg), "%s: %s", rla, action);
215 lock = lock_any_ref_for_update(ref->name,
216 check_old ? ref->old_sha1 : NULL, 0);
217 if (!lock)
218 return errno == ENOTDIR ? STORE_REF_ERROR_DF_CONFLICT :
219 STORE_REF_ERROR_OTHER;
220 if (write_ref_sha1(lock, ref->new_sha1, msg) < 0)
221 return errno == ENOTDIR ? STORE_REF_ERROR_DF_CONFLICT :
222 STORE_REF_ERROR_OTHER;
223 return 0;
226 #define REFCOL_WIDTH 10
228 static int update_local_ref(struct ref *ref,
229 const char *remote,
230 char *display)
232 struct commit *current = NULL, *updated;
233 enum object_type type;
234 struct branch *current_branch = branch_get(NULL);
235 const char *pretty_ref = prettify_refname(ref->name);
237 *display = 0;
238 type = sha1_object_info(ref->new_sha1, NULL);
239 if (type < 0)
240 die(_("object %s not found"), sha1_to_hex(ref->new_sha1));
242 if (!hashcmp(ref->old_sha1, ref->new_sha1)) {
243 if (verbosity > 0)
244 sprintf(display, "= %-*s %-*s -> %s", TRANSPORT_SUMMARY_WIDTH,
245 _("[up to date]"), REFCOL_WIDTH, remote,
246 pretty_ref);
247 return 0;
250 if (current_branch &&
251 !strcmp(ref->name, current_branch->name) &&
252 !(update_head_ok || is_bare_repository()) &&
253 !is_null_sha1(ref->old_sha1)) {
255 * If this is the head, and it's not okay to update
256 * the head, and the old value of the head isn't empty...
258 sprintf(display, _("! %-*s %-*s -> %s (can't fetch in current branch)"),
259 TRANSPORT_SUMMARY_WIDTH, _("[rejected]"), REFCOL_WIDTH, remote,
260 pretty_ref);
261 return 1;
264 if (!is_null_sha1(ref->old_sha1) &&
265 !prefixcmp(ref->name, "refs/tags/")) {
266 int r;
267 r = s_update_ref("updating tag", ref, 0);
268 sprintf(display, "%c %-*s %-*s -> %s%s", r ? '!' : '-',
269 TRANSPORT_SUMMARY_WIDTH, _("[tag update]"), REFCOL_WIDTH, remote,
270 pretty_ref, r ? _(" (unable to update local ref)") : "");
271 return r;
274 current = lookup_commit_reference_gently(ref->old_sha1, 1);
275 updated = lookup_commit_reference_gently(ref->new_sha1, 1);
276 if (!current || !updated) {
277 const char *msg;
278 const char *what;
279 int r;
280 if (!strncmp(ref->name, "refs/tags/", 10)) {
281 msg = "storing tag";
282 what = _("[new tag]");
284 else {
285 msg = "storing head";
286 what = _("[new branch]");
289 r = s_update_ref(msg, ref, 0);
290 sprintf(display, "%c %-*s %-*s -> %s%s", r ? '!' : '*',
291 TRANSPORT_SUMMARY_WIDTH, what, REFCOL_WIDTH, remote, pretty_ref,
292 r ? _(" (unable to update local ref)") : "");
293 return r;
296 if (in_merge_bases(current, &updated, 1)) {
297 char quickref[83];
298 int r;
299 strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
300 strcat(quickref, "..");
301 strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
302 r = s_update_ref("fast-forward", ref, 1);
303 sprintf(display, "%c %-*s %-*s -> %s%s", r ? '!' : ' ',
304 TRANSPORT_SUMMARY_WIDTH, quickref, REFCOL_WIDTH, remote,
305 pretty_ref, r ? _(" (unable to update local ref)") : "");
306 return r;
307 } else if (force || ref->force) {
308 char quickref[84];
309 int r;
310 strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
311 strcat(quickref, "...");
312 strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
313 r = s_update_ref("forced-update", ref, 1);
314 sprintf(display, "%c %-*s %-*s -> %s (%s)", r ? '!' : '+',
315 TRANSPORT_SUMMARY_WIDTH, quickref, REFCOL_WIDTH, remote,
316 pretty_ref,
317 r ? _("unable to update local ref") : _("forced update"));
318 return r;
319 } else {
320 sprintf(display, "! %-*s %-*s -> %s %s",
321 TRANSPORT_SUMMARY_WIDTH, _("[rejected]"), REFCOL_WIDTH, remote,
322 pretty_ref, _("(non-fast-forward)"));
323 return 1;
327 static int store_updated_refs(const char *raw_url, const char *remote_name,
328 struct ref *ref_map)
330 FILE *fp;
331 struct commit *commit;
332 int url_len, i, note_len, shown_url = 0, rc = 0;
333 char note[1024];
334 const char *what, *kind;
335 struct ref *rm;
336 char *url, *filename = dry_run ? "/dev/null" : git_path("FETCH_HEAD");
338 fp = fopen(filename, "a");
339 if (!fp)
340 return error(_("cannot open %s: %s\n"), filename, strerror(errno));
342 if (raw_url)
343 url = transport_anonymize_url(raw_url);
344 else
345 url = xstrdup("foreign");
346 for (rm = ref_map; rm; rm = rm->next) {
347 struct ref *ref = NULL;
349 if (rm->peer_ref) {
350 ref = xcalloc(1, sizeof(*ref) + strlen(rm->peer_ref->name) + 1);
351 strcpy(ref->name, rm->peer_ref->name);
352 hashcpy(ref->old_sha1, rm->peer_ref->old_sha1);
353 hashcpy(ref->new_sha1, rm->old_sha1);
354 ref->force = rm->peer_ref->force;
357 commit = lookup_commit_reference_gently(rm->old_sha1, 1);
358 if (!commit)
359 rm->merge = 0;
361 if (!strcmp(rm->name, "HEAD")) {
362 kind = "";
363 what = "";
365 else if (!prefixcmp(rm->name, "refs/heads/")) {
366 kind = "branch";
367 what = rm->name + 11;
369 else if (!prefixcmp(rm->name, "refs/tags/")) {
370 kind = "tag";
371 what = rm->name + 10;
373 else if (!prefixcmp(rm->name, "refs/remotes/")) {
374 kind = "remote-tracking branch";
375 what = rm->name + 13;
377 else {
378 kind = "";
379 what = rm->name;
382 url_len = strlen(url);
383 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
385 url_len = i + 1;
386 if (4 < i && !strncmp(".git", url + i - 3, 4))
387 url_len = i - 3;
389 note_len = 0;
390 if (*what) {
391 if (*kind)
392 note_len += sprintf(note + note_len, "%s ",
393 kind);
394 note_len += sprintf(note + note_len, "'%s' of ", what);
396 note[note_len] = '\0';
397 fprintf(fp, "%s\t%s\t%s",
398 sha1_to_hex(commit ? commit->object.sha1 :
399 rm->old_sha1),
400 rm->merge ? "" : "not-for-merge",
401 note);
402 for (i = 0; i < url_len; ++i)
403 if ('\n' == url[i])
404 fputs("\\n", fp);
405 else
406 fputc(url[i], fp);
407 fputc('\n', fp);
409 if (ref) {
410 rc |= update_local_ref(ref, what, note);
411 free(ref);
412 } else
413 sprintf(note, "* %-*s %-*s -> FETCH_HEAD",
414 TRANSPORT_SUMMARY_WIDTH, *kind ? kind : "branch",
415 REFCOL_WIDTH, *what ? what : "HEAD");
416 if (*note) {
417 if (verbosity >= 0 && !shown_url) {
418 fprintf(stderr, _("From %.*s\n"),
419 url_len, url);
420 shown_url = 1;
422 if (verbosity >= 0)
423 fprintf(stderr, " %s\n", note);
426 free(url);
427 fclose(fp);
428 if (rc & STORE_REF_ERROR_DF_CONFLICT)
429 error(_("some local refs could not be updated; try running\n"
430 " 'git remote prune %s' to remove any old, conflicting "
431 "branches"), remote_name);
432 return rc;
436 * We would want to bypass the object transfer altogether if
437 * everything we are going to fetch already exists and is connected
438 * locally.
440 * The refs we are going to fetch are in ref_map. If running
442 * $ git rev-list --objects --stdin --not --all
444 * (feeding all the refs in ref_map on its standard input)
445 * does not error out, that means everything reachable from the
446 * refs we are going to fetch exists and is connected to some of
447 * our existing refs.
449 static int quickfetch(struct ref *ref_map)
451 struct child_process revlist;
452 struct ref *ref;
453 int err;
454 const char *argv[] = {"rev-list",
455 "--quiet", "--objects", "--stdin", "--not", "--all", NULL};
458 * If we are deepening a shallow clone we already have these
459 * objects reachable. Running rev-list here will return with
460 * a good (0) exit status and we'll bypass the fetch that we
461 * really need to perform. Claiming failure now will ensure
462 * we perform the network exchange to deepen our history.
464 if (depth)
465 return -1;
467 if (!ref_map)
468 return 0;
470 memset(&revlist, 0, sizeof(revlist));
471 revlist.argv = argv;
472 revlist.git_cmd = 1;
473 revlist.no_stdout = 1;
474 revlist.no_stderr = 1;
475 revlist.in = -1;
477 err = start_command(&revlist);
478 if (err) {
479 error(_("could not run rev-list"));
480 return err;
484 * If rev-list --stdin encounters an unknown commit, it terminates,
485 * which will cause SIGPIPE in the write loop below.
487 sigchain_push(SIGPIPE, SIG_IGN);
489 for (ref = ref_map; ref; ref = ref->next) {
490 if (write_in_full(revlist.in, sha1_to_hex(ref->old_sha1), 40) < 0 ||
491 write_str_in_full(revlist.in, "\n") < 0) {
492 if (errno != EPIPE && errno != EINVAL)
493 error(_("failed write to rev-list: %s"), strerror(errno));
494 err = -1;
495 break;
499 if (close(revlist.in)) {
500 error(_("failed to close rev-list's stdin: %s"), strerror(errno));
501 err = -1;
504 sigchain_pop(SIGPIPE);
506 return finish_command(&revlist) || err;
509 static int fetch_refs(struct transport *transport, struct ref *ref_map)
511 int ret = quickfetch(ref_map);
512 if (ret)
513 ret = transport_fetch_refs(transport, ref_map);
514 if (!ret)
515 ret |= store_updated_refs(transport->url,
516 transport->remote->name,
517 ref_map);
518 transport_unlock_pack(transport);
519 return ret;
522 static int prune_refs(struct transport *transport, struct ref *ref_map)
524 int result = 0;
525 struct ref *ref, *stale_refs = get_stale_heads(transport->remote, ref_map);
526 const char *dangling_msg = dry_run
527 ? _(" (%s will become dangling)\n")
528 : _(" (%s has become dangling)\n");
530 for (ref = stale_refs; ref; ref = ref->next) {
531 if (!dry_run)
532 result |= delete_ref(ref->name, NULL, 0);
533 if (verbosity >= 0) {
534 fprintf(stderr, " x %-*s %-*s -> %s\n",
535 TRANSPORT_SUMMARY_WIDTH, _("[deleted]"),
536 REFCOL_WIDTH, _("(none)"), prettify_refname(ref->name));
537 warn_dangling_symref(stderr, dangling_msg, ref->name);
540 free_refs(stale_refs);
541 return result;
544 static int add_existing(const char *refname, const unsigned char *sha1,
545 int flag, void *cbdata)
547 struct string_list *list = (struct string_list *)cbdata;
548 struct string_list_item *item = string_list_insert(list, refname);
549 item->util = (void *)sha1;
550 return 0;
553 static int will_fetch(struct ref **head, const unsigned char *sha1)
555 struct ref *rm = *head;
556 while (rm) {
557 if (!hashcmp(rm->old_sha1, sha1))
558 return 1;
559 rm = rm->next;
561 return 0;
564 static void find_non_local_tags(struct transport *transport,
565 struct ref **head,
566 struct ref ***tail)
568 struct string_list existing_refs = STRING_LIST_INIT_NODUP;
569 struct string_list remote_refs = STRING_LIST_INIT_NODUP;
570 const struct ref *ref;
571 struct string_list_item *item = NULL;
573 for_each_ref(add_existing, &existing_refs);
574 for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
575 if (prefixcmp(ref->name, "refs/tags"))
576 continue;
579 * The peeled ref always follows the matching base
580 * ref, so if we see a peeled ref that we don't want
581 * to fetch then we can mark the ref entry in the list
582 * as one to ignore by setting util to NULL.
584 if (!suffixcmp(ref->name, "^{}")) {
585 if (item && !has_sha1_file(ref->old_sha1) &&
586 !will_fetch(head, ref->old_sha1) &&
587 !has_sha1_file(item->util) &&
588 !will_fetch(head, item->util))
589 item->util = NULL;
590 item = NULL;
591 continue;
595 * If item is non-NULL here, then we previously saw a
596 * ref not followed by a peeled reference, so we need
597 * to check if it is a lightweight tag that we want to
598 * fetch.
600 if (item && !has_sha1_file(item->util) &&
601 !will_fetch(head, item->util))
602 item->util = NULL;
604 item = NULL;
606 /* skip duplicates and refs that we already have */
607 if (string_list_has_string(&remote_refs, ref->name) ||
608 string_list_has_string(&existing_refs, ref->name))
609 continue;
611 item = string_list_insert(&remote_refs, ref->name);
612 item->util = (void *)ref->old_sha1;
614 string_list_clear(&existing_refs, 0);
617 * We may have a final lightweight tag that needs to be
618 * checked to see if it needs fetching.
620 if (item && !has_sha1_file(item->util) &&
621 !will_fetch(head, item->util))
622 item->util = NULL;
625 * For all the tags in the remote_refs string list,
626 * add them to the list of refs to be fetched
628 for_each_string_list_item(item, &remote_refs) {
629 /* Unless we have already decided to ignore this item... */
630 if (item->util)
632 struct ref *rm = alloc_ref(item->string);
633 rm->peer_ref = alloc_ref(item->string);
634 hashcpy(rm->old_sha1, item->util);
635 **tail = rm;
636 *tail = &rm->next;
640 string_list_clear(&remote_refs, 0);
643 static void check_not_current_branch(struct ref *ref_map)
645 struct branch *current_branch = branch_get(NULL);
647 if (is_bare_repository() || !current_branch)
648 return;
650 for (; ref_map; ref_map = ref_map->next)
651 if (ref_map->peer_ref && !strcmp(current_branch->refname,
652 ref_map->peer_ref->name))
653 die(_("Refusing to fetch into current branch %s "
654 "of non-bare repository"), current_branch->refname);
657 static int truncate_fetch_head(void)
659 char *filename = git_path("FETCH_HEAD");
660 FILE *fp = fopen(filename, "w");
662 if (!fp)
663 return error(_("cannot open %s: %s\n"), filename, strerror(errno));
664 fclose(fp);
665 return 0;
668 static int do_fetch(struct transport *transport,
669 struct refspec *refs, int ref_count)
671 struct string_list existing_refs = STRING_LIST_INIT_NODUP;
672 struct string_list_item *peer_item = NULL;
673 struct ref *ref_map;
674 struct ref *rm;
675 int autotags = (transport->remote->fetch_tags == 1);
677 for_each_ref(add_existing, &existing_refs);
679 if (tags == TAGS_DEFAULT) {
680 if (transport->remote->fetch_tags == 2)
681 tags = TAGS_SET;
682 if (transport->remote->fetch_tags == -1)
683 tags = TAGS_UNSET;
686 if (!transport->get_refs_list || !transport->fetch)
687 die(_("Don't know how to fetch from %s"), transport->url);
689 /* if not appending, truncate FETCH_HEAD */
690 if (!append && !dry_run) {
691 int errcode = truncate_fetch_head();
692 if (errcode)
693 return errcode;
696 ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
697 if (!update_head_ok)
698 check_not_current_branch(ref_map);
700 for (rm = ref_map; rm; rm = rm->next) {
701 if (rm->peer_ref) {
702 peer_item = string_list_lookup(&existing_refs,
703 rm->peer_ref->name);
704 if (peer_item)
705 hashcpy(rm->peer_ref->old_sha1,
706 peer_item->util);
710 if (tags == TAGS_DEFAULT && autotags)
711 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
712 if (fetch_refs(transport, ref_map)) {
713 free_refs(ref_map);
714 return 1;
716 if (prune)
717 prune_refs(transport, ref_map);
718 free_refs(ref_map);
720 /* if neither --no-tags nor --tags was specified, do automated tag
721 * following ... */
722 if (tags == TAGS_DEFAULT && autotags) {
723 struct ref **tail = &ref_map;
724 ref_map = NULL;
725 find_non_local_tags(transport, &ref_map, &tail);
726 if (ref_map) {
727 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);
728 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
729 fetch_refs(transport, ref_map);
731 free_refs(ref_map);
734 return 0;
737 static void set_option(const char *name, const char *value)
739 int r = transport_set_option(transport, name, value);
740 if (r < 0)
741 die(_("Option \"%s\" value \"%s\" is not valid for %s"),
742 name, value, transport->url);
743 if (r > 0)
744 warning(_("Option \"%s\" is ignored for %s\n"),
745 name, transport->url);
748 static int get_one_remote_for_fetch(struct remote *remote, void *priv)
750 struct string_list *list = priv;
751 if (!remote->skip_default_update)
752 string_list_append(list, remote->name);
753 return 0;
756 struct remote_group_data {
757 const char *name;
758 struct string_list *list;
761 static int get_remote_group(const char *key, const char *value, void *priv)
763 struct remote_group_data *g = priv;
765 if (!prefixcmp(key, "remotes.") &&
766 !strcmp(key + 8, g->name)) {
767 /* split list by white space */
768 int space = strcspn(value, " \t\n");
769 while (*value) {
770 if (space > 1) {
771 string_list_append(g->list,
772 xstrndup(value, space));
774 value += space + (value[space] != '\0');
775 space = strcspn(value, " \t\n");
779 return 0;
782 static int add_remote_or_group(const char *name, struct string_list *list)
784 int prev_nr = list->nr;
785 struct remote_group_data g;
786 g.name = name; g.list = list;
788 git_config(get_remote_group, &g);
789 if (list->nr == prev_nr) {
790 struct remote *remote;
791 if (!remote_is_configured(name))
792 return 0;
793 remote = remote_get(name);
794 string_list_append(list, remote->name);
796 return 1;
799 static void add_options_to_argv(int *argc, const char **argv)
801 if (dry_run)
802 argv[(*argc)++] = "--dry-run";
803 if (prune)
804 argv[(*argc)++] = "--prune";
805 if (update_head_ok)
806 argv[(*argc)++] = "--update-head-ok";
807 if (force)
808 argv[(*argc)++] = "--force";
809 if (keep)
810 argv[(*argc)++] = "--keep";
811 if (recurse_submodules == RECURSE_SUBMODULES_ON)
812 argv[(*argc)++] = "--recurse-submodules";
813 if (verbosity >= 2)
814 argv[(*argc)++] = "-v";
815 if (verbosity >= 1)
816 argv[(*argc)++] = "-v";
817 else if (verbosity < 0)
818 argv[(*argc)++] = "-q";
822 static int fetch_multiple(struct string_list *list)
824 int i, result = 0;
825 const char *argv[12] = { "fetch", "--append" };
826 int argc = 2;
828 add_options_to_argv(&argc, argv);
830 if (!append && !dry_run) {
831 int errcode = truncate_fetch_head();
832 if (errcode)
833 return errcode;
836 for (i = 0; i < list->nr; i++) {
837 const char *name = list->items[i].string;
838 argv[argc] = name;
839 argv[argc + 1] = NULL;
840 if (verbosity >= 0)
841 printf(_("Fetching %s\n"), name);
842 if (run_command_v_opt(argv, RUN_GIT_CMD)) {
843 error(_("Could not fetch %s"), name);
844 result = 1;
848 return result;
851 static int fetch_one(struct remote *remote, int argc, const char **argv)
853 int i;
854 static const char **refs = NULL;
855 int ref_nr = 0;
856 int exit_code;
858 if (!remote)
859 die(_("No remote repository specified. Please, specify either a URL or a\n"
860 "remote name from which new revisions should be fetched."));
862 transport = transport_get(remote, NULL);
863 transport_set_verbosity(transport, verbosity, progress);
864 if (upload_pack)
865 set_option(TRANS_OPT_UPLOADPACK, upload_pack);
866 if (keep)
867 set_option(TRANS_OPT_KEEP, "yes");
868 if (depth)
869 set_option(TRANS_OPT_DEPTH, depth);
871 if (argc > 0) {
872 int j = 0;
873 refs = xcalloc(argc + 1, sizeof(const char *));
874 for (i = 0; i < argc; i++) {
875 if (!strcmp(argv[i], "tag")) {
876 char *ref;
877 i++;
878 if (i >= argc)
879 die(_("You need to specify a tag name."));
880 ref = xmalloc(strlen(argv[i]) * 2 + 22);
881 strcpy(ref, "refs/tags/");
882 strcat(ref, argv[i]);
883 strcat(ref, ":refs/tags/");
884 strcat(ref, argv[i]);
885 refs[j++] = ref;
886 } else
887 refs[j++] = argv[i];
889 refs[j] = NULL;
890 ref_nr = j;
893 sigchain_push_common(unlock_pack_on_signal);
894 atexit(unlock_pack);
895 exit_code = do_fetch(transport,
896 parse_fetch_refspec(ref_nr, refs), ref_nr);
897 transport_disconnect(transport);
898 transport = NULL;
899 return exit_code;
902 int cmd_fetch(int argc, const char **argv, const char *prefix)
904 int i;
905 struct string_list list = STRING_LIST_INIT_NODUP;
906 struct remote *remote;
907 int result = 0;
909 packet_trace_identity("fetch");
911 /* Record the command line for the reflog */
912 strbuf_addstr(&default_rla, "fetch");
913 for (i = 1; i < argc; i++)
914 strbuf_addf(&default_rla, " %s", argv[i]);
916 argc = parse_options(argc, argv, prefix,
917 builtin_fetch_options, builtin_fetch_usage, 0);
919 if (all) {
920 if (argc == 1)
921 die(_("fetch --all does not take a repository argument"));
922 else if (argc > 1)
923 die(_("fetch --all does not make sense with refspecs"));
924 (void) for_each_remote(get_one_remote_for_fetch, &list);
925 result = fetch_multiple(&list);
926 } else if (argc == 0) {
927 /* No arguments -- use default remote */
928 remote = remote_get(NULL);
929 result = fetch_one(remote, argc, argv);
930 } else if (multiple) {
931 /* All arguments are assumed to be remotes or groups */
932 for (i = 0; i < argc; i++)
933 if (!add_remote_or_group(argv[i], &list))
934 die(_("No such remote or remote group: %s"), argv[i]);
935 result = fetch_multiple(&list);
936 } else {
937 /* Single remote or group */
938 (void) add_remote_or_group(argv[0], &list);
939 if (list.nr > 1) {
940 /* More than one remote */
941 if (argc > 1)
942 die(_("Fetching a group and specifying refspecs does not make sense"));
943 result = fetch_multiple(&list);
944 } else {
945 /* Zero or one remotes */
946 remote = remote_get(argv[0]);
947 result = fetch_one(remote, argc-1, argv+1);
951 if (!result && (recurse_submodules != RECURSE_SUBMODULES_OFF)) {
952 const char *options[10];
953 int num_options = 0;
954 /* Set recursion as default when we already are recursing */
955 if (submodule_prefix[0])
956 set_config_fetch_recurse_submodules(1);
957 gitmodules_config();
958 git_config(submodule_config, NULL);
959 add_options_to_argv(&num_options, options);
960 result = fetch_populated_submodules(num_options, options,
961 submodule_prefix,
962 recurse_submodules == RECURSE_SUBMODULES_ON,
963 verbosity < 0);
966 /* All names were strdup()ed or strndup()ed */
967 list.strdup_strings = 1;
968 string_list_clear(&list, 0);
970 return result;