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