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