teach warn_dangling_symref to take a FILE argument
[git/dkf.git] / builtin-fetch.c
blob4cbd69075f718ce97abefddbf99c1f0ccbf67774
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"
15 static const char * const builtin_fetch_usage[] = {
16 "git fetch [options] [<repository> <refspec>...]",
17 "git fetch [options] <group>",
18 "git fetch --multiple [options] [<repository> | <group>]...",
19 "git fetch --all [options]",
20 NULL
23 enum {
24 TAGS_UNSET = 0,
25 TAGS_DEFAULT = 1,
26 TAGS_SET = 2
29 static int all, append, force, keep, multiple, update_head_ok, verbosity;
30 static int tags = TAGS_DEFAULT;
31 static const char *depth;
32 static const char *upload_pack;
33 static struct strbuf default_rla = STRBUF_INIT;
34 static struct transport *transport;
36 static struct option builtin_fetch_options[] = {
37 OPT__VERBOSITY(&verbosity),
38 OPT_BOOLEAN(0, "all", &all,
39 "fetch from all remotes"),
40 OPT_BOOLEAN('a', "append", &append,
41 "append to .git/FETCH_HEAD instead of overwriting"),
42 OPT_STRING(0, "upload-pack", &upload_pack, "PATH",
43 "path to upload pack on remote end"),
44 OPT_BOOLEAN('f', "force", &force,
45 "force overwrite of local branch"),
46 OPT_BOOLEAN('m', "multiple", &multiple,
47 "fetch from multiple remotes"),
48 OPT_SET_INT('t', "tags", &tags,
49 "fetch all tags and associated objects", TAGS_SET),
50 OPT_SET_INT('n', NULL, &tags,
51 "do not fetch all tags (--no-tags)", TAGS_UNSET),
52 OPT_BOOLEAN('k', "keep", &keep, "keep downloaded pack"),
53 OPT_BOOLEAN('u', "update-head-ok", &update_head_ok,
54 "allow updating of HEAD ref"),
55 OPT_STRING(0, "depth", &depth, "DEPTH",
56 "deepen history of shallow clone"),
57 OPT_END()
60 static void unlock_pack(void)
62 if (transport)
63 transport_unlock_pack(transport);
66 static void unlock_pack_on_signal(int signo)
68 unlock_pack();
69 sigchain_pop(signo);
70 raise(signo);
73 static void add_merge_config(struct ref **head,
74 const struct ref *remote_refs,
75 struct branch *branch,
76 struct ref ***tail)
78 int i;
80 for (i = 0; i < branch->merge_nr; i++) {
81 struct ref *rm, **old_tail = *tail;
82 struct refspec refspec;
84 for (rm = *head; rm; rm = rm->next) {
85 if (branch_merge_matches(branch, i, rm->name)) {
86 rm->merge = 1;
87 break;
90 if (rm)
91 continue;
94 * Not fetched to a tracking branch? We need to fetch
95 * it anyway to allow this branch's "branch.$name.merge"
96 * to be honored by 'git pull', but we do not have to
97 * fail if branch.$name.merge is misconfigured to point
98 * at a nonexisting branch. If we were indeed called by
99 * 'git pull', it will notice the misconfiguration because
100 * there is no entry in the resulting FETCH_HEAD marked
101 * for merging.
103 refspec.src = branch->merge[i]->src;
104 refspec.dst = NULL;
105 refspec.pattern = 0;
106 refspec.force = 0;
107 get_fetch_map(remote_refs, &refspec, tail, 1);
108 for (rm = *old_tail; rm; rm = rm->next)
109 rm->merge = 1;
113 static void find_non_local_tags(struct transport *transport,
114 struct ref **head,
115 struct ref ***tail);
117 static struct ref *get_ref_map(struct transport *transport,
118 struct refspec *refs, int ref_count, int tags,
119 int *autotags)
121 int i;
122 struct ref *rm;
123 struct ref *ref_map = NULL;
124 struct ref **tail = &ref_map;
126 const struct ref *remote_refs = transport_get_remote_refs(transport);
128 if (ref_count || tags == TAGS_SET) {
129 for (i = 0; i < ref_count; i++) {
130 get_fetch_map(remote_refs, &refs[i], &tail, 0);
131 if (refs[i].dst && refs[i].dst[0])
132 *autotags = 1;
134 /* Merge everything on the command line, but not --tags */
135 for (rm = ref_map; rm; rm = rm->next)
136 rm->merge = 1;
137 if (tags == TAGS_SET)
138 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
139 } else {
140 /* Use the defaults */
141 struct remote *remote = transport->remote;
142 struct branch *branch = branch_get(NULL);
143 int has_merge = branch_has_merge_config(branch);
144 if (remote && (remote->fetch_refspec_nr || has_merge)) {
145 for (i = 0; i < remote->fetch_refspec_nr; i++) {
146 get_fetch_map(remote_refs, &remote->fetch[i], &tail, 0);
147 if (remote->fetch[i].dst &&
148 remote->fetch[i].dst[0])
149 *autotags = 1;
150 if (!i && !has_merge && ref_map &&
151 !remote->fetch[0].pattern)
152 ref_map->merge = 1;
155 * if the remote we're fetching from is the same
156 * as given in branch.<name>.remote, we add the
157 * ref given in branch.<name>.merge, too.
159 if (has_merge &&
160 !strcmp(branch->remote_name, remote->name))
161 add_merge_config(&ref_map, remote_refs, branch, &tail);
162 } else {
163 ref_map = get_remote_ref(remote_refs, "HEAD");
164 if (!ref_map)
165 die("Couldn't find remote ref HEAD");
166 ref_map->merge = 1;
167 tail = &ref_map->next;
170 if (tags == TAGS_DEFAULT && *autotags)
171 find_non_local_tags(transport, &ref_map, &tail);
172 ref_remove_duplicates(ref_map);
174 return ref_map;
177 #define STORE_REF_ERROR_OTHER 1
178 #define STORE_REF_ERROR_DF_CONFLICT 2
180 static int s_update_ref(const char *action,
181 struct ref *ref,
182 int check_old)
184 char msg[1024];
185 char *rla = getenv("GIT_REFLOG_ACTION");
186 static struct ref_lock *lock;
188 if (!rla)
189 rla = default_rla.buf;
190 snprintf(msg, sizeof(msg), "%s: %s", rla, action);
191 lock = lock_any_ref_for_update(ref->name,
192 check_old ? ref->old_sha1 : NULL, 0);
193 if (!lock)
194 return errno == ENOTDIR ? STORE_REF_ERROR_DF_CONFLICT :
195 STORE_REF_ERROR_OTHER;
196 if (write_ref_sha1(lock, ref->new_sha1, msg) < 0)
197 return errno == ENOTDIR ? STORE_REF_ERROR_DF_CONFLICT :
198 STORE_REF_ERROR_OTHER;
199 return 0;
202 #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
203 #define REFCOL_WIDTH 10
205 static int update_local_ref(struct ref *ref,
206 const char *remote,
207 char *display)
209 struct commit *current = NULL, *updated;
210 enum object_type type;
211 struct branch *current_branch = branch_get(NULL);
212 const char *pretty_ref = prettify_refname(ref->name);
214 *display = 0;
215 type = sha1_object_info(ref->new_sha1, NULL);
216 if (type < 0)
217 die("object %s not found", sha1_to_hex(ref->new_sha1));
219 if (!hashcmp(ref->old_sha1, ref->new_sha1)) {
220 if (verbosity > 0)
221 sprintf(display, "= %-*s %-*s -> %s", SUMMARY_WIDTH,
222 "[up to date]", REFCOL_WIDTH, remote,
223 pretty_ref);
224 return 0;
227 if (current_branch &&
228 !strcmp(ref->name, current_branch->name) &&
229 !(update_head_ok || is_bare_repository()) &&
230 !is_null_sha1(ref->old_sha1)) {
232 * If this is the head, and it's not okay to update
233 * the head, and the old value of the head isn't empty...
235 sprintf(display, "! %-*s %-*s -> %s (can't fetch in current branch)",
236 SUMMARY_WIDTH, "[rejected]", REFCOL_WIDTH, remote,
237 pretty_ref);
238 return 1;
241 if (!is_null_sha1(ref->old_sha1) &&
242 !prefixcmp(ref->name, "refs/tags/")) {
243 int r;
244 r = s_update_ref("updating tag", ref, 0);
245 sprintf(display, "%c %-*s %-*s -> %s%s", r ? '!' : '-',
246 SUMMARY_WIDTH, "[tag update]", REFCOL_WIDTH, remote,
247 pretty_ref, r ? " (unable to update local ref)" : "");
248 return r;
251 current = lookup_commit_reference_gently(ref->old_sha1, 1);
252 updated = lookup_commit_reference_gently(ref->new_sha1, 1);
253 if (!current || !updated) {
254 const char *msg;
255 const char *what;
256 int r;
257 if (!strncmp(ref->name, "refs/tags/", 10)) {
258 msg = "storing tag";
259 what = "[new tag]";
261 else {
262 msg = "storing head";
263 what = "[new branch]";
266 r = s_update_ref(msg, ref, 0);
267 sprintf(display, "%c %-*s %-*s -> %s%s", r ? '!' : '*',
268 SUMMARY_WIDTH, what, REFCOL_WIDTH, remote, pretty_ref,
269 r ? " (unable to update local ref)" : "");
270 return r;
273 if (in_merge_bases(current, &updated, 1)) {
274 char quickref[83];
275 int r;
276 strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
277 strcat(quickref, "..");
278 strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
279 r = s_update_ref("fast forward", ref, 1);
280 sprintf(display, "%c %-*s %-*s -> %s%s", r ? '!' : ' ',
281 SUMMARY_WIDTH, quickref, REFCOL_WIDTH, remote,
282 pretty_ref, r ? " (unable to update local ref)" : "");
283 return r;
284 } else if (force || ref->force) {
285 char quickref[84];
286 int r;
287 strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
288 strcat(quickref, "...");
289 strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
290 r = s_update_ref("forced-update", ref, 1);
291 sprintf(display, "%c %-*s %-*s -> %s (%s)", r ? '!' : '+',
292 SUMMARY_WIDTH, quickref, REFCOL_WIDTH, remote,
293 pretty_ref,
294 r ? "unable to update local ref" : "forced update");
295 return r;
296 } else {
297 sprintf(display, "! %-*s %-*s -> %s (non fast forward)",
298 SUMMARY_WIDTH, "[rejected]", REFCOL_WIDTH, remote,
299 pretty_ref);
300 return 1;
304 static int store_updated_refs(const char *raw_url, const char *remote_name,
305 struct ref *ref_map)
307 FILE *fp;
308 struct commit *commit;
309 int url_len, i, note_len, shown_url = 0, rc = 0;
310 char note[1024];
311 const char *what, *kind;
312 struct ref *rm;
313 char *url, *filename = git_path("FETCH_HEAD");
315 fp = fopen(filename, "a");
316 if (!fp)
317 return error("cannot open %s: %s\n", filename, strerror(errno));
319 url = transport_anonymize_url(raw_url);
320 for (rm = ref_map; rm; rm = rm->next) {
321 struct ref *ref = NULL;
323 if (rm->peer_ref) {
324 ref = xcalloc(1, sizeof(*ref) + strlen(rm->peer_ref->name) + 1);
325 strcpy(ref->name, rm->peer_ref->name);
326 hashcpy(ref->old_sha1, rm->peer_ref->old_sha1);
327 hashcpy(ref->new_sha1, rm->old_sha1);
328 ref->force = rm->peer_ref->force;
331 commit = lookup_commit_reference_gently(rm->old_sha1, 1);
332 if (!commit)
333 rm->merge = 0;
335 if (!strcmp(rm->name, "HEAD")) {
336 kind = "";
337 what = "";
339 else if (!prefixcmp(rm->name, "refs/heads/")) {
340 kind = "branch";
341 what = rm->name + 11;
343 else if (!prefixcmp(rm->name, "refs/tags/")) {
344 kind = "tag";
345 what = rm->name + 10;
347 else if (!prefixcmp(rm->name, "refs/remotes/")) {
348 kind = "remote branch";
349 what = rm->name + 13;
351 else {
352 kind = "";
353 what = rm->name;
356 url_len = strlen(url);
357 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
359 url_len = i + 1;
360 if (4 < i && !strncmp(".git", url + i - 3, 4))
361 url_len = i - 3;
363 note_len = 0;
364 if (*what) {
365 if (*kind)
366 note_len += sprintf(note + note_len, "%s ",
367 kind);
368 note_len += sprintf(note + note_len, "'%s' of ", what);
370 note[note_len] = '\0';
371 fprintf(fp, "%s\t%s\t%s",
372 sha1_to_hex(commit ? commit->object.sha1 :
373 rm->old_sha1),
374 rm->merge ? "" : "not-for-merge",
375 note);
376 for (i = 0; i < url_len; ++i)
377 if ('\n' == url[i])
378 fputs("\\n", fp);
379 else
380 fputc(url[i], fp);
381 fputc('\n', fp);
383 if (ref)
384 rc |= update_local_ref(ref, what, note);
385 else
386 sprintf(note, "* %-*s %-*s -> FETCH_HEAD",
387 SUMMARY_WIDTH, *kind ? kind : "branch",
388 REFCOL_WIDTH, *what ? what : "HEAD");
389 if (*note) {
390 if (verbosity >= 0 && !shown_url) {
391 fprintf(stderr, "From %.*s\n",
392 url_len, url);
393 shown_url = 1;
395 if (verbosity >= 0)
396 fprintf(stderr, " %s\n", note);
399 free(url);
400 fclose(fp);
401 if (rc & STORE_REF_ERROR_DF_CONFLICT)
402 error("some local refs could not be updated; try running\n"
403 " 'git remote prune %s' to remove any old, conflicting "
404 "branches", remote_name);
405 return rc;
409 * We would want to bypass the object transfer altogether if
410 * everything we are going to fetch already exists and is connected
411 * locally.
413 * The refs we are going to fetch are in ref_map. If running
415 * $ git rev-list --objects --stdin --not --all
417 * (feeding all the refs in ref_map on its standard input)
418 * does not error out, that means everything reachable from the
419 * refs we are going to fetch exists and is connected to some of
420 * our existing refs.
422 static int quickfetch(struct ref *ref_map)
424 struct child_process revlist;
425 struct ref *ref;
426 int err;
427 const char *argv[] = {"rev-list",
428 "--quiet", "--objects", "--stdin", "--not", "--all", NULL};
431 * If we are deepening a shallow clone we already have these
432 * objects reachable. Running rev-list here will return with
433 * a good (0) exit status and we'll bypass the fetch that we
434 * really need to perform. Claiming failure now will ensure
435 * we perform the network exchange to deepen our history.
437 if (depth)
438 return -1;
440 if (!ref_map)
441 return 0;
443 memset(&revlist, 0, sizeof(revlist));
444 revlist.argv = argv;
445 revlist.git_cmd = 1;
446 revlist.no_stdout = 1;
447 revlist.no_stderr = 1;
448 revlist.in = -1;
450 err = start_command(&revlist);
451 if (err) {
452 error("could not run rev-list");
453 return err;
457 * If rev-list --stdin encounters an unknown commit, it terminates,
458 * which will cause SIGPIPE in the write loop below.
460 sigchain_push(SIGPIPE, SIG_IGN);
462 for (ref = ref_map; ref; ref = ref->next) {
463 if (write_in_full(revlist.in, sha1_to_hex(ref->old_sha1), 40) < 0 ||
464 write_str_in_full(revlist.in, "\n") < 0) {
465 if (errno != EPIPE && errno != EINVAL)
466 error("failed write to rev-list: %s", strerror(errno));
467 err = -1;
468 break;
472 if (close(revlist.in)) {
473 error("failed to close rev-list's stdin: %s", strerror(errno));
474 err = -1;
477 sigchain_pop(SIGPIPE);
479 return finish_command(&revlist) || err;
482 static int fetch_refs(struct transport *transport, struct ref *ref_map)
484 int ret = quickfetch(ref_map);
485 if (ret)
486 ret = transport_fetch_refs(transport, ref_map);
487 if (!ret)
488 ret |= store_updated_refs(transport->url,
489 transport->remote->name,
490 ref_map);
491 transport_unlock_pack(transport);
492 return ret;
495 static int add_existing(const char *refname, const unsigned char *sha1,
496 int flag, void *cbdata)
498 struct string_list *list = (struct string_list *)cbdata;
499 string_list_insert(refname, list);
500 return 0;
503 static int will_fetch(struct ref **head, const unsigned char *sha1)
505 struct ref *rm = *head;
506 while (rm) {
507 if (!hashcmp(rm->old_sha1, sha1))
508 return 1;
509 rm = rm->next;
511 return 0;
514 static void find_non_local_tags(struct transport *transport,
515 struct ref **head,
516 struct ref ***tail)
518 struct string_list existing_refs = { NULL, 0, 0, 0 };
519 struct string_list new_refs = { NULL, 0, 0, 1 };
520 char *ref_name;
521 int ref_name_len;
522 const unsigned char *ref_sha1;
523 const struct ref *tag_ref;
524 struct ref *rm = NULL;
525 const struct ref *ref;
527 for_each_ref(add_existing, &existing_refs);
528 for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
529 if (prefixcmp(ref->name, "refs/tags"))
530 continue;
532 ref_name = xstrdup(ref->name);
533 ref_name_len = strlen(ref_name);
534 ref_sha1 = ref->old_sha1;
536 if (!strcmp(ref_name + ref_name_len - 3, "^{}")) {
537 ref_name[ref_name_len - 3] = 0;
538 tag_ref = transport_get_remote_refs(transport);
539 while (tag_ref) {
540 if (!strcmp(tag_ref->name, ref_name)) {
541 ref_sha1 = tag_ref->old_sha1;
542 break;
544 tag_ref = tag_ref->next;
548 if (!string_list_has_string(&existing_refs, ref_name) &&
549 !string_list_has_string(&new_refs, ref_name) &&
550 (has_sha1_file(ref->old_sha1) ||
551 will_fetch(head, ref->old_sha1))) {
552 string_list_insert(ref_name, &new_refs);
554 rm = alloc_ref(ref_name);
555 rm->peer_ref = alloc_ref(ref_name);
556 hashcpy(rm->old_sha1, ref_sha1);
558 **tail = rm;
559 *tail = &rm->next;
561 free(ref_name);
563 string_list_clear(&existing_refs, 0);
564 string_list_clear(&new_refs, 0);
567 static void check_not_current_branch(struct ref *ref_map)
569 struct branch *current_branch = branch_get(NULL);
571 if (is_bare_repository() || !current_branch)
572 return;
574 for (; ref_map; ref_map = ref_map->next)
575 if (ref_map->peer_ref && !strcmp(current_branch->refname,
576 ref_map->peer_ref->name))
577 die("Refusing to fetch into current branch %s "
578 "of non-bare repository", current_branch->refname);
581 static int do_fetch(struct transport *transport,
582 struct refspec *refs, int ref_count)
584 struct ref *ref_map;
585 struct ref *rm;
586 int autotags = (transport->remote->fetch_tags == 1);
587 if (transport->remote->fetch_tags == 2 && tags != TAGS_UNSET)
588 tags = TAGS_SET;
589 if (transport->remote->fetch_tags == -1)
590 tags = TAGS_UNSET;
592 if (!transport->get_refs_list || !transport->fetch)
593 die("Don't know how to fetch from %s", transport->url);
595 /* if not appending, truncate FETCH_HEAD */
596 if (!append) {
597 char *filename = git_path("FETCH_HEAD");
598 FILE *fp = fopen(filename, "w");
599 if (!fp)
600 return error("cannot open %s: %s\n", filename, strerror(errno));
601 fclose(fp);
604 ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
605 if (!update_head_ok)
606 check_not_current_branch(ref_map);
608 for (rm = ref_map; rm; rm = rm->next) {
609 if (rm->peer_ref)
610 read_ref(rm->peer_ref->name, rm->peer_ref->old_sha1);
613 if (tags == TAGS_DEFAULT && autotags)
614 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
615 if (fetch_refs(transport, ref_map)) {
616 free_refs(ref_map);
617 return 1;
619 free_refs(ref_map);
621 /* if neither --no-tags nor --tags was specified, do automated tag
622 * following ... */
623 if (tags == TAGS_DEFAULT && autotags) {
624 struct ref **tail = &ref_map;
625 ref_map = NULL;
626 find_non_local_tags(transport, &ref_map, &tail);
627 if (ref_map) {
628 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);
629 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
630 fetch_refs(transport, ref_map);
632 free_refs(ref_map);
635 return 0;
638 static void set_option(const char *name, const char *value)
640 int r = transport_set_option(transport, name, value);
641 if (r < 0)
642 die("Option \"%s\" value \"%s\" is not valid for %s",
643 name, value, transport->url);
644 if (r > 0)
645 warning("Option \"%s\" is ignored for %s\n",
646 name, transport->url);
649 static int get_one_remote_for_fetch(struct remote *remote, void *priv)
651 struct string_list *list = priv;
652 if (!remote->skip_default_update)
653 string_list_append(remote->name, list);
654 return 0;
657 struct remote_group_data {
658 const char *name;
659 struct string_list *list;
662 static int get_remote_group(const char *key, const char *value, void *priv)
664 struct remote_group_data *g = priv;
666 if (!prefixcmp(key, "remotes.") &&
667 !strcmp(key + 8, g->name)) {
668 /* split list by white space */
669 int space = strcspn(value, " \t\n");
670 while (*value) {
671 if (space > 1) {
672 string_list_append(xstrndup(value, space),
673 g->list);
675 value += space + (value[space] != '\0');
676 space = strcspn(value, " \t\n");
680 return 0;
683 static int add_remote_or_group(const char *name, struct string_list *list)
685 int prev_nr = list->nr;
686 struct remote_group_data g = { name, list };
688 git_config(get_remote_group, &g);
689 if (list->nr == prev_nr) {
690 struct remote *remote;
691 if (!remote_is_configured(name))
692 return 0;
693 remote = remote_get(name);
694 string_list_append(remote->name, list);
696 return 1;
699 static int fetch_multiple(struct string_list *list)
701 int i, result = 0;
702 const char *argv[] = { "fetch", NULL, NULL, NULL, NULL };
703 int argc = 1;
705 if (verbosity >= 2)
706 argv[argc++] = "-v";
707 if (verbosity >= 1)
708 argv[argc++] = "-v";
709 else if (verbosity < 0)
710 argv[argc++] = "-q";
712 for (i = 0; i < list->nr; i++) {
713 const char *name = list->items[i].string;
714 argv[argc] = name;
715 if (verbosity >= 0)
716 printf("Fetching %s\n", name);
717 if (run_command_v_opt(argv, RUN_GIT_CMD)) {
718 error("Could not fetch %s", name);
719 result = 1;
723 return result;
726 static int fetch_one(struct remote *remote, int argc, const char **argv)
728 int i;
729 static const char **refs = NULL;
730 int ref_nr = 0;
731 int exit_code;
733 if (!remote)
734 die("Where do you want to fetch from today?");
736 transport = transport_get(remote, remote->url[0]);
737 if (verbosity >= 2)
738 transport->verbose = 1;
739 if (verbosity < 0)
740 transport->verbose = -1;
741 if (upload_pack)
742 set_option(TRANS_OPT_UPLOADPACK, upload_pack);
743 if (keep)
744 set_option(TRANS_OPT_KEEP, "yes");
745 if (depth)
746 set_option(TRANS_OPT_DEPTH, depth);
748 if (argc > 0) {
749 int j = 0;
750 refs = xcalloc(argc + 1, sizeof(const char *));
751 for (i = 0; i < argc; i++) {
752 if (!strcmp(argv[i], "tag")) {
753 char *ref;
754 i++;
755 if (i >= argc)
756 die("You need to specify a tag name.");
757 ref = xmalloc(strlen(argv[i]) * 2 + 22);
758 strcpy(ref, "refs/tags/");
759 strcat(ref, argv[i]);
760 strcat(ref, ":refs/tags/");
761 strcat(ref, argv[i]);
762 refs[j++] = ref;
763 } else
764 refs[j++] = argv[i];
766 refs[j] = NULL;
767 ref_nr = j;
770 sigchain_push_common(unlock_pack_on_signal);
771 atexit(unlock_pack);
772 exit_code = do_fetch(transport,
773 parse_fetch_refspec(ref_nr, refs), ref_nr);
774 transport_disconnect(transport);
775 transport = NULL;
776 return exit_code;
779 int cmd_fetch(int argc, const char **argv, const char *prefix)
781 int i;
782 struct string_list list = { NULL, 0, 0, 0 };
783 struct remote *remote;
784 int result = 0;
786 /* Record the command line for the reflog */
787 strbuf_addstr(&default_rla, "fetch");
788 for (i = 1; i < argc; i++)
789 strbuf_addf(&default_rla, " %s", argv[i]);
791 argc = parse_options(argc, argv, prefix,
792 builtin_fetch_options, builtin_fetch_usage, 0);
794 if (all) {
795 if (argc == 1)
796 die("fetch --all does not take a repository argument");
797 else if (argc > 1)
798 die("fetch --all does not make sense with refspecs");
799 (void) for_each_remote(get_one_remote_for_fetch, &list);
800 result = fetch_multiple(&list);
801 } else if (argc == 0) {
802 /* No arguments -- use default remote */
803 remote = remote_get(NULL);
804 result = fetch_one(remote, argc, argv);
805 } else if (multiple) {
806 /* All arguments are assumed to be remotes or groups */
807 for (i = 0; i < argc; i++)
808 if (!add_remote_or_group(argv[i], &list))
809 die("No such remote or remote group: %s", argv[i]);
810 result = fetch_multiple(&list);
811 } else {
812 /* Single remote or group */
813 (void) add_remote_or_group(argv[0], &list);
814 if (list.nr > 1) {
815 /* More than one remote */
816 if (argc > 1)
817 die("Fetching a group and specifying refspecs does not make sense");
818 result = fetch_multiple(&list);
819 } else {
820 /* Zero or one remotes */
821 remote = remote_get(argv[0]);
822 result = fetch_one(remote, argc-1, argv+1);
826 /* All names were strdup()ed or strndup()ed */
827 list.strdup_strings = 1;
828 string_list_clear(&list, 0);
830 return result;