criss cross rename failure workaround
[git/dscho.git] / builtin / fetch.c
blob757478a7f8adfa19e9fd1855764799e99a46882a
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 RECURSIVE_UNSET = 0,
33 RECURSIVE_DEFAULT = 1,
34 RECURSIVE_SET = 2
37 static int all, append, dry_run, force, keep, multiple, prune, recursive = RECURSIVE_DEFAULT, update_head_ok, verbosity;
38 static int progress;
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;
45 static struct option builtin_fetch_options[] = {
46 OPT__VERBOSITY(&verbosity),
47 OPT_BOOLEAN(0, "all", &all,
48 "fetch from all remotes"),
49 OPT_BOOLEAN('a', "append", &append,
50 "append to .git/FETCH_HEAD instead of overwriting"),
51 OPT_STRING(0, "upload-pack", &upload_pack, "PATH",
52 "path to upload pack on remote end"),
53 OPT_BOOLEAN('f', "force", &force,
54 "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 tracking branches no longer on remote"),
63 OPT_SET_INT(0, "recursive", &recursive,
64 "control recursive fetching of submodules", RECURSIVE_SET),
65 OPT_BOOLEAN(0, "dry-run", &dry_run,
66 "dry run"),
67 OPT_BOOLEAN('k', "keep", &keep, "keep downloaded pack"),
68 OPT_BOOLEAN('u', "update-head-ok", &update_head_ok,
69 "allow updating of HEAD ref"),
70 OPT_BOOLEAN(0, "progress", &progress, "force progress reporting"),
71 OPT_STRING(0, "depth", &depth, "DEPTH",
72 "deepen history of shallow clone"),
73 OPT_END()
76 static void unlock_pack(void)
78 if (transport)
79 transport_unlock_pack(transport);
82 static void unlock_pack_on_signal(int signo)
84 unlock_pack();
85 sigchain_pop(signo);
86 raise(signo);
89 static void add_merge_config(struct ref **head,
90 const struct ref *remote_refs,
91 struct branch *branch,
92 struct ref ***tail)
94 int i;
96 for (i = 0; i < branch->merge_nr; i++) {
97 struct ref *rm, **old_tail = *tail;
98 struct refspec refspec;
100 for (rm = *head; rm; rm = rm->next) {
101 if (branch_merge_matches(branch, i, rm->name)) {
102 rm->merge = 1;
103 break;
106 if (rm)
107 continue;
110 * Not fetched to a tracking branch? We need to fetch
111 * it anyway to allow this branch's "branch.$name.merge"
112 * to be honored by 'git pull', but we do not have to
113 * fail if branch.$name.merge is misconfigured to point
114 * at a nonexisting branch. If we were indeed called by
115 * 'git pull', it will notice the misconfiguration because
116 * there is no entry in the resulting FETCH_HEAD marked
117 * for merging.
119 memset(&refspec, 0, sizeof(refspec));
120 refspec.src = branch->merge[i]->src;
121 get_fetch_map(remote_refs, &refspec, tail, 1);
122 for (rm = *old_tail; rm; rm = rm->next)
123 rm->merge = 1;
127 static void find_non_local_tags(struct transport *transport,
128 struct ref **head,
129 struct ref ***tail);
131 static struct ref *get_ref_map(struct transport *transport,
132 struct refspec *refs, int ref_count, int tags,
133 int *autotags)
135 int i;
136 struct ref *rm;
137 struct ref *ref_map = NULL;
138 struct ref **tail = &ref_map;
140 const struct ref *remote_refs = transport_get_remote_refs(transport);
142 if (ref_count || tags == TAGS_SET) {
143 for (i = 0; i < ref_count; i++) {
144 get_fetch_map(remote_refs, &refs[i], &tail, 0);
145 if (refs[i].dst && refs[i].dst[0])
146 *autotags = 1;
148 /* Merge everything on the command line, but not --tags */
149 for (rm = ref_map; rm; rm = rm->next)
150 rm->merge = 1;
151 if (tags == TAGS_SET)
152 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
153 } else {
154 /* Use the defaults */
155 struct remote *remote = transport->remote;
156 struct branch *branch = branch_get(NULL);
157 int has_merge = branch_has_merge_config(branch);
158 if (remote &&
159 (remote->fetch_refspec_nr ||
160 /* Note: has_merge implies non-NULL branch->remote_name */
161 (has_merge && !strcmp(branch->remote_name, remote->name)))) {
162 for (i = 0; i < remote->fetch_refspec_nr; i++) {
163 get_fetch_map(remote_refs, &remote->fetch[i], &tail, 0);
164 if (remote->fetch[i].dst &&
165 remote->fetch[i].dst[0])
166 *autotags = 1;
167 if (!i && !has_merge && ref_map &&
168 !remote->fetch[0].pattern)
169 ref_map->merge = 1;
172 * if the remote we're fetching from is the same
173 * as given in branch.<name>.remote, we add the
174 * ref given in branch.<name>.merge, too.
176 * Note: has_merge implies non-NULL branch->remote_name
178 if (has_merge &&
179 !strcmp(branch->remote_name, remote->name))
180 add_merge_config(&ref_map, remote_refs, branch, &tail);
181 } else {
182 ref_map = get_remote_ref(remote_refs, "HEAD");
183 if (!ref_map)
184 die("Couldn't find remote ref HEAD");
185 ref_map->merge = 1;
186 tail = &ref_map->next;
189 if (tags == TAGS_DEFAULT && *autotags)
190 find_non_local_tags(transport, &ref_map, &tail);
191 ref_remove_duplicates(ref_map);
193 return ref_map;
196 #define STORE_REF_ERROR_OTHER 1
197 #define STORE_REF_ERROR_DF_CONFLICT 2
199 static int s_update_ref(const char *action,
200 struct ref *ref,
201 int check_old)
203 char msg[1024];
204 char *rla = getenv("GIT_REFLOG_ACTION");
205 static struct ref_lock *lock;
207 if (dry_run)
208 return 0;
209 if (!rla)
210 rla = default_rla.buf;
211 snprintf(msg, sizeof(msg), "%s: %s", rla, action);
212 lock = lock_any_ref_for_update(ref->name,
213 check_old ? ref->old_sha1 : NULL, 0);
214 if (!lock)
215 return errno == ENOTDIR ? STORE_REF_ERROR_DF_CONFLICT :
216 STORE_REF_ERROR_OTHER;
217 if (write_ref_sha1(lock, ref->new_sha1, msg) < 0)
218 return errno == ENOTDIR ? STORE_REF_ERROR_DF_CONFLICT :
219 STORE_REF_ERROR_OTHER;
220 return 0;
223 #define REFCOL_WIDTH 10
225 static int update_local_ref(struct ref *ref,
226 const char *remote,
227 char *display)
229 struct commit *current = NULL, *updated;
230 enum object_type type;
231 struct branch *current_branch = branch_get(NULL);
232 const char *pretty_ref = prettify_refname(ref->name);
234 *display = 0;
235 type = sha1_object_info(ref->new_sha1, NULL);
236 if (type < 0)
237 die("object %s not found", sha1_to_hex(ref->new_sha1));
239 if (!hashcmp(ref->old_sha1, ref->new_sha1)) {
240 if (verbosity > 0)
241 sprintf(display, "= %-*s %-*s -> %s", TRANSPORT_SUMMARY_WIDTH,
242 "[up to date]", REFCOL_WIDTH, remote,
243 pretty_ref);
244 return 0;
247 if (current_branch &&
248 !strcmp(ref->name, current_branch->name) &&
249 !(update_head_ok || is_bare_repository()) &&
250 !is_null_sha1(ref->old_sha1)) {
252 * If this is the head, and it's not okay to update
253 * the head, and the old value of the head isn't empty...
255 sprintf(display, "! %-*s %-*s -> %s (can't fetch in current branch)",
256 TRANSPORT_SUMMARY_WIDTH, "[rejected]", REFCOL_WIDTH, remote,
257 pretty_ref);
258 return 1;
261 if (!is_null_sha1(ref->old_sha1) &&
262 !prefixcmp(ref->name, "refs/tags/")) {
263 int r;
264 r = s_update_ref("updating tag", ref, 0);
265 sprintf(display, "%c %-*s %-*s -> %s%s", r ? '!' : '-',
266 TRANSPORT_SUMMARY_WIDTH, "[tag update]", REFCOL_WIDTH, remote,
267 pretty_ref, r ? " (unable to update local ref)" : "");
268 return r;
271 current = lookup_commit_reference_gently(ref->old_sha1, 1);
272 updated = lookup_commit_reference_gently(ref->new_sha1, 1);
273 if (!current || !updated) {
274 const char *msg;
275 const char *what;
276 int r;
277 if (!strncmp(ref->name, "refs/tags/", 10)) {
278 msg = "storing tag";
279 what = "[new tag]";
281 else {
282 msg = "storing head";
283 what = "[new branch]";
286 r = s_update_ref(msg, ref, 0);
287 sprintf(display, "%c %-*s %-*s -> %s%s", r ? '!' : '*',
288 TRANSPORT_SUMMARY_WIDTH, what, REFCOL_WIDTH, remote, pretty_ref,
289 r ? " (unable to update local ref)" : "");
290 return r;
293 if (in_merge_bases(current, &updated, 1)) {
294 char quickref[83];
295 int r;
296 strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
297 strcat(quickref, "..");
298 strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
299 r = s_update_ref("fast-forward", ref, 1);
300 sprintf(display, "%c %-*s %-*s -> %s%s", r ? '!' : ' ',
301 TRANSPORT_SUMMARY_WIDTH, quickref, REFCOL_WIDTH, remote,
302 pretty_ref, r ? " (unable to update local ref)" : "");
303 return r;
304 } else if (force || ref->force) {
305 char quickref[84];
306 int r;
307 strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
308 strcat(quickref, "...");
309 strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
310 r = s_update_ref("forced-update", ref, 1);
311 sprintf(display, "%c %-*s %-*s -> %s (%s)", r ? '!' : '+',
312 TRANSPORT_SUMMARY_WIDTH, quickref, REFCOL_WIDTH, remote,
313 pretty_ref,
314 r ? "unable to update local ref" : "forced update");
315 return r;
316 } else {
317 sprintf(display, "! %-*s %-*s -> %s (non-fast-forward)",
318 TRANSPORT_SUMMARY_WIDTH, "[rejected]", REFCOL_WIDTH, remote,
319 pretty_ref);
320 return 1;
324 static int store_updated_refs(const char *raw_url, const char *remote_name,
325 struct ref *ref_map)
327 FILE *fp;
328 struct commit *commit;
329 int url_len, i, note_len, shown_url = 0, rc = 0;
330 char note[1024];
331 const char *what, *kind;
332 struct ref *rm;
333 char *url, *filename = dry_run ? "/dev/null" : git_path("FETCH_HEAD");
335 fp = fopen(filename, "a");
336 if (!fp)
337 return error("cannot open %s: %s\n", filename, strerror(errno));
339 if (raw_url)
340 url = transport_anonymize_url(raw_url);
341 else
342 url = xstrdup("foreign");
343 for (rm = ref_map; rm; rm = rm->next) {
344 struct ref *ref = NULL;
346 if (rm->peer_ref) {
347 ref = xcalloc(1, sizeof(*ref) + strlen(rm->peer_ref->name) + 1);
348 strcpy(ref->name, rm->peer_ref->name);
349 hashcpy(ref->old_sha1, rm->peer_ref->old_sha1);
350 hashcpy(ref->new_sha1, rm->old_sha1);
351 ref->force = rm->peer_ref->force;
354 commit = lookup_commit_reference_gently(rm->old_sha1, 1);
355 if (!commit)
356 rm->merge = 0;
358 if (!strcmp(rm->name, "HEAD")) {
359 kind = "";
360 what = "";
362 else if (!prefixcmp(rm->name, "refs/heads/")) {
363 kind = "branch";
364 what = rm->name + 11;
366 else if (!prefixcmp(rm->name, "refs/tags/")) {
367 kind = "tag";
368 what = rm->name + 10;
370 else if (!prefixcmp(rm->name, "refs/remotes/")) {
371 kind = "remote branch";
372 what = rm->name + 13;
374 else {
375 kind = "";
376 what = rm->name;
379 url_len = strlen(url);
380 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
382 url_len = i + 1;
383 if (4 < i && !strncmp(".git", url + i - 3, 4))
384 url_len = i - 3;
386 note_len = 0;
387 if (*what) {
388 if (*kind)
389 note_len += sprintf(note + note_len, "%s ",
390 kind);
391 note_len += sprintf(note + note_len, "'%s' of ", what);
393 note[note_len] = '\0';
394 fprintf(fp, "%s\t%s\t%s",
395 sha1_to_hex(commit ? commit->object.sha1 :
396 rm->old_sha1),
397 rm->merge ? "" : "not-for-merge",
398 note);
399 for (i = 0; i < url_len; ++i)
400 if ('\n' == url[i])
401 fputs("\\n", fp);
402 else
403 fputc(url[i], fp);
404 fputc('\n', fp);
406 if (ref) {
407 rc |= update_local_ref(ref, what, note);
408 free(ref);
409 } else
410 sprintf(note, "* %-*s %-*s -> FETCH_HEAD",
411 TRANSPORT_SUMMARY_WIDTH, *kind ? kind : "branch",
412 REFCOL_WIDTH, *what ? what : "HEAD");
413 if (*note) {
414 if (verbosity >= 0 && !shown_url) {
415 fprintf(stderr, "From %.*s\n",
416 url_len, url);
417 shown_url = 1;
419 if (verbosity >= 0)
420 fprintf(stderr, " %s\n", note);
423 free(url);
424 fclose(fp);
425 if (rc & STORE_REF_ERROR_DF_CONFLICT)
426 error("some local refs could not be updated; try running\n"
427 " 'git remote prune %s' to remove any old, conflicting "
428 "branches", remote_name);
429 return rc;
433 * We would want to bypass the object transfer altogether if
434 * everything we are going to fetch already exists and is connected
435 * locally.
437 * The refs we are going to fetch are in ref_map. If running
439 * $ git rev-list --objects --stdin --not --all
441 * (feeding all the refs in ref_map on its standard input)
442 * does not error out, that means everything reachable from the
443 * refs we are going to fetch exists and is connected to some of
444 * our existing refs.
446 static int quickfetch(struct ref *ref_map)
448 struct child_process revlist;
449 struct ref *ref;
450 int err;
451 const char *argv[] = {"rev-list",
452 "--quiet", "--objects", "--stdin", "--not", "--all", NULL};
455 * If we are deepening a shallow clone we already have these
456 * objects reachable. Running rev-list here will return with
457 * a good (0) exit status and we'll bypass the fetch that we
458 * really need to perform. Claiming failure now will ensure
459 * we perform the network exchange to deepen our history.
461 if (depth)
462 return -1;
464 if (!ref_map)
465 return 0;
467 memset(&revlist, 0, sizeof(revlist));
468 revlist.argv = argv;
469 revlist.git_cmd = 1;
470 revlist.no_stdout = 1;
471 revlist.no_stderr = 1;
472 revlist.in = -1;
474 err = start_command(&revlist);
475 if (err) {
476 error("could not run rev-list");
477 return err;
481 * If rev-list --stdin encounters an unknown commit, it terminates,
482 * which will cause SIGPIPE in the write loop below.
484 sigchain_push(SIGPIPE, SIG_IGN);
486 for (ref = ref_map; ref; ref = ref->next) {
487 if (write_in_full(revlist.in, sha1_to_hex(ref->old_sha1), 40) < 0 ||
488 write_str_in_full(revlist.in, "\n") < 0) {
489 if (errno != EPIPE && errno != EINVAL)
490 error("failed write to rev-list: %s", strerror(errno));
491 err = -1;
492 break;
496 if (close(revlist.in)) {
497 error("failed to close rev-list's stdin: %s", strerror(errno));
498 err = -1;
501 sigchain_pop(SIGPIPE);
503 return finish_command(&revlist) || err;
506 static int fetch_refs(struct transport *transport, struct ref *ref_map)
508 int ret = quickfetch(ref_map);
509 if (ret)
510 ret = transport_fetch_refs(transport, ref_map);
511 if (!ret)
512 ret |= store_updated_refs(transport->url,
513 transport->remote->name,
514 ref_map);
515 transport_unlock_pack(transport);
516 return ret;
519 static int prune_refs(struct transport *transport, struct ref *ref_map)
521 int result = 0;
522 struct ref *ref, *stale_refs = get_stale_heads(transport->remote, ref_map);
523 const char *dangling_msg = dry_run
524 ? " (%s will become dangling)\n"
525 : " (%s has become dangling)\n";
527 for (ref = stale_refs; ref; ref = ref->next) {
528 if (!dry_run)
529 result |= delete_ref(ref->name, NULL, 0);
530 if (verbosity >= 0) {
531 fprintf(stderr, " x %-*s %-*s -> %s\n",
532 TRANSPORT_SUMMARY_WIDTH, "[deleted]",
533 REFCOL_WIDTH, "(none)", prettify_refname(ref->name));
534 warn_dangling_symref(stderr, dangling_msg, ref->name);
537 free_refs(stale_refs);
538 return result;
541 static int add_existing(const char *refname, const unsigned char *sha1,
542 int flag, void *cbdata)
544 struct string_list *list = (struct string_list *)cbdata;
545 struct string_list_item *item = string_list_insert(list, refname);
546 item->util = (void *)sha1;
547 return 0;
550 static int will_fetch(struct ref **head, const unsigned char *sha1)
552 struct ref *rm = *head;
553 while (rm) {
554 if (!hashcmp(rm->old_sha1, sha1))
555 return 1;
556 rm = rm->next;
558 return 0;
561 static void find_non_local_tags(struct transport *transport,
562 struct ref **head,
563 struct ref ***tail)
565 struct string_list existing_refs = STRING_LIST_INIT_NODUP;
566 struct string_list remote_refs = STRING_LIST_INIT_NODUP;
567 const struct ref *ref;
568 struct string_list_item *item = NULL;
570 for_each_ref(add_existing, &existing_refs);
571 for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
572 if (prefixcmp(ref->name, "refs/tags"))
573 continue;
576 * The peeled ref always follows the matching base
577 * ref, so if we see a peeled ref that we don't want
578 * to fetch then we can mark the ref entry in the list
579 * as one to ignore by setting util to NULL.
581 if (!suffixcmp(ref->name, "^{}")) {
582 if (item && !has_sha1_file(ref->old_sha1) &&
583 !will_fetch(head, ref->old_sha1) &&
584 !has_sha1_file(item->util) &&
585 !will_fetch(head, item->util))
586 item->util = NULL;
587 item = NULL;
588 continue;
592 * If item is non-NULL here, then we previously saw a
593 * ref not followed by a peeled reference, so we need
594 * to check if it is a lightweight tag that we want to
595 * fetch.
597 if (item && !has_sha1_file(item->util) &&
598 !will_fetch(head, item->util))
599 item->util = NULL;
601 item = NULL;
603 /* skip duplicates and refs that we already have */
604 if (string_list_has_string(&remote_refs, ref->name) ||
605 string_list_has_string(&existing_refs, ref->name))
606 continue;
608 item = string_list_insert(&remote_refs, ref->name);
609 item->util = (void *)ref->old_sha1;
611 string_list_clear(&existing_refs, 0);
614 * We may have a final lightweight tag that needs to be
615 * checked to see if it needs fetching.
617 if (item && !has_sha1_file(item->util) &&
618 !will_fetch(head, item->util))
619 item->util = NULL;
622 * For all the tags in the remote_refs string list,
623 * add them to the list of refs to be fetched
625 for_each_string_list_item(item, &remote_refs) {
626 /* Unless we have already decided to ignore this item... */
627 if (item->util)
629 struct ref *rm = alloc_ref(item->string);
630 rm->peer_ref = alloc_ref(item->string);
631 hashcpy(rm->old_sha1, item->util);
632 **tail = rm;
633 *tail = &rm->next;
637 string_list_clear(&remote_refs, 0);
640 static void check_not_current_branch(struct ref *ref_map)
642 struct branch *current_branch = branch_get(NULL);
644 if (is_bare_repository() || !current_branch)
645 return;
647 for (; ref_map; ref_map = ref_map->next)
648 if (ref_map->peer_ref && !strcmp(current_branch->refname,
649 ref_map->peer_ref->name))
650 die("Refusing to fetch into current branch %s "
651 "of non-bare repository", current_branch->refname);
654 static int truncate_fetch_head(void)
656 char *filename = git_path("FETCH_HEAD");
657 FILE *fp = fopen(filename, "w");
659 if (!fp)
660 return error("cannot open %s: %s\n", filename, strerror(errno));
661 fclose(fp);
662 return 0;
665 static int do_fetch(struct transport *transport,
666 struct refspec *refs, int ref_count)
668 struct string_list existing_refs = STRING_LIST_INIT_NODUP;
669 struct string_list_item *peer_item = NULL;
670 struct ref *ref_map;
671 struct ref *rm;
672 int autotags = (transport->remote->fetch_tags == 1);
674 for_each_ref(add_existing, &existing_refs);
676 if (tags == TAGS_DEFAULT) {
677 if (transport->remote->fetch_tags == 2)
678 tags = TAGS_SET;
679 if (transport->remote->fetch_tags == -1)
680 tags = TAGS_UNSET;
683 if (!transport->get_refs_list || !transport->fetch)
684 die("Don't know how to fetch from %s", transport->url);
686 /* if not appending, truncate FETCH_HEAD */
687 if (!append && !dry_run) {
688 int errcode = truncate_fetch_head();
689 if (errcode)
690 return errcode;
693 ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
694 if (!update_head_ok)
695 check_not_current_branch(ref_map);
697 for (rm = ref_map; rm; rm = rm->next) {
698 if (rm->peer_ref) {
699 peer_item = string_list_lookup(&existing_refs,
700 rm->peer_ref->name);
701 if (peer_item)
702 hashcpy(rm->peer_ref->old_sha1,
703 peer_item->util);
707 if (tags == TAGS_DEFAULT && autotags)
708 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
709 if (fetch_refs(transport, ref_map)) {
710 free_refs(ref_map);
711 return 1;
713 if (prune)
714 prune_refs(transport, ref_map);
715 free_refs(ref_map);
717 /* if neither --no-tags nor --tags was specified, do automated tag
718 * following ... */
719 if (tags == TAGS_DEFAULT && autotags) {
720 struct ref **tail = &ref_map;
721 ref_map = NULL;
722 find_non_local_tags(transport, &ref_map, &tail);
723 if (ref_map) {
724 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);
725 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
726 fetch_refs(transport, ref_map);
728 free_refs(ref_map);
731 return 0;
734 static void set_option(const char *name, const char *value)
736 int r = transport_set_option(transport, name, value);
737 if (r < 0)
738 die("Option \"%s\" value \"%s\" is not valid for %s",
739 name, value, transport->url);
740 if (r > 0)
741 warning("Option \"%s\" is ignored for %s\n",
742 name, transport->url);
745 static int get_one_remote_for_fetch(struct remote *remote, void *priv)
747 struct string_list *list = priv;
748 if (!remote->skip_default_update)
749 string_list_append(list, remote->name);
750 return 0;
753 struct remote_group_data {
754 const char *name;
755 struct string_list *list;
758 static int get_remote_group(const char *key, const char *value, void *priv)
760 struct remote_group_data *g = priv;
762 if (!prefixcmp(key, "remotes.") &&
763 !strcmp(key + 8, g->name)) {
764 /* split list by white space */
765 int space = strcspn(value, " \t\n");
766 while (*value) {
767 if (space > 1) {
768 string_list_append(g->list,
769 xstrndup(value, space));
771 value += space + (value[space] != '\0');
772 space = strcspn(value, " \t\n");
776 return 0;
779 static int add_remote_or_group(const char *name, struct string_list *list)
781 int prev_nr = list->nr;
782 struct remote_group_data g;
783 g.name = name; g.list = list;
785 git_config(get_remote_group, &g);
786 if (list->nr == prev_nr) {
787 struct remote *remote;
788 if (!remote_is_configured(name))
789 return 0;
790 remote = remote_get(name);
791 string_list_append(list, remote->name);
793 return 1;
796 static int fetch_multiple(struct string_list *list)
798 int i, result = 0;
799 const char *argv[11] = { "fetch", "--append" };
800 int argc = 2;
802 if (dry_run)
803 argv[argc++] = "--dry-run";
804 if (prune)
805 argv[argc++] = "--prune";
806 if (update_head_ok)
807 argv[argc++] = "--update-head-ok";
808 if (force)
809 argv[argc++] = "--force";
810 if (keep)
811 argv[argc++] = "--keep";
812 if (verbosity >= 2)
813 argv[argc++] = "-v";
814 if (verbosity >= 1)
815 argv[argc++] = "-v";
816 else if (verbosity < 0)
817 argv[argc++] = "-q";
819 if (!append && !dry_run) {
820 int errcode = truncate_fetch_head();
821 if (errcode)
822 return errcode;
825 for (i = 0; i < list->nr; i++) {
826 const char *name = list->items[i].string;
827 argv[argc] = name;
828 argv[argc + 1] = NULL;
829 if (verbosity >= 0)
830 printf("Fetching %s\n", name);
831 if (run_command_v_opt(argv, RUN_GIT_CMD)) {
832 error("Could not fetch %s", name);
833 result = 1;
837 return result;
840 static int fetch_one(struct remote *remote, int argc, const char **argv)
842 int i;
843 static const char **refs = NULL;
844 int ref_nr = 0;
845 int exit_code;
847 if (!remote)
848 die("No remote repository specified. Please, specify either a URL or a\n"
849 "remote name from which new revisions should be fetched.");
851 transport = transport_get(remote, NULL);
852 transport_set_verbosity(transport, verbosity, progress);
853 if (upload_pack)
854 set_option(TRANS_OPT_UPLOADPACK, upload_pack);
855 if (keep)
856 set_option(TRANS_OPT_KEEP, "yes");
857 if (depth)
858 set_option(TRANS_OPT_DEPTH, depth);
860 if (argc > 0) {
861 int j = 0;
862 refs = xcalloc(argc + 1, sizeof(const char *));
863 for (i = 0; i < argc; i++) {
864 if (!strcmp(argv[i], "tag")) {
865 char *ref;
866 i++;
867 if (i >= argc)
868 die("You need to specify a tag name.");
869 ref = xmalloc(strlen(argv[i]) * 2 + 22);
870 strcpy(ref, "refs/tags/");
871 strcat(ref, argv[i]);
872 strcat(ref, ":refs/tags/");
873 strcat(ref, argv[i]);
874 refs[j++] = ref;
875 } else
876 refs[j++] = argv[i];
878 refs[j] = NULL;
879 ref_nr = j;
882 sigchain_push_common(unlock_pack_on_signal);
883 atexit(unlock_pack);
884 exit_code = do_fetch(transport,
885 parse_fetch_refspec(ref_nr, refs), ref_nr);
886 transport_disconnect(transport);
887 transport = NULL;
888 return exit_code;
891 int cmd_fetch(int argc, const char **argv, const char *prefix)
893 int i;
894 struct string_list list = STRING_LIST_INIT_NODUP;
895 struct remote *remote;
896 int result = 0;
898 /* Record the command line for the reflog */
899 strbuf_addstr(&default_rla, "fetch");
900 for (i = 1; i < argc; i++)
901 strbuf_addf(&default_rla, " %s", argv[i]);
903 argc = parse_options(argc, argv, prefix,
904 builtin_fetch_options, builtin_fetch_usage, 0);
906 if (all) {
907 if (argc == 1)
908 die("fetch --all does not take a repository argument");
909 else if (argc > 1)
910 die("fetch --all does not make sense with refspecs");
911 (void) for_each_remote(get_one_remote_for_fetch, &list);
912 result = fetch_multiple(&list);
913 } else if (argc == 0) {
914 /* No arguments -- use default remote */
915 remote = remote_get(NULL);
916 result = fetch_one(remote, argc, argv);
917 } else if (multiple) {
918 /* All arguments are assumed to be remotes or groups */
919 for (i = 0; i < argc; i++)
920 if (!add_remote_or_group(argv[i], &list))
921 die("No such remote or remote group: %s", argv[i]);
922 result = fetch_multiple(&list);
923 } else {
924 /* Single remote or group */
925 (void) add_remote_or_group(argv[0], &list);
926 if (list.nr > 1) {
927 /* More than one remote */
928 if (argc > 1)
929 die("Fetching a group and specifying refspecs does not make sense");
930 result = fetch_multiple(&list);
931 } else {
932 /* Zero or one remotes */
933 remote = remote_get(argv[0]);
934 result = fetch_one(remote, argc-1, argv+1);
938 if (!result && recursive) {
939 gitmodules_config();
940 git_config(submodule_config, NULL);
941 result = fetch_populated_submodules(recursive == RECURSIVE_SET);
944 /* All names were strdup()ed or strndup()ed */
945 list.strdup_strings = 1;
946 string_list_clear(&list, 0);
948 return result;