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