fetch: Give remote_ref to update_local_ref() as well
[alt-git.git] / builtin / fetch.c
blob06d71e4e4c795a96c4554c44178545b0f146e6a3
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
26 enum {
27 TAGS_UNSET = 0,
28 TAGS_DEFAULT = 1,
29 TAGS_SET = 2
32 static int all, append, dry_run, force, keep, multiple, prune, update_head_ok, verbosity;
33 static int progress = -1, 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)
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;
53 return 0;
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_BOOL(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()
92 static void unlock_pack(void)
94 if (transport)
95 transport_unlock_pack(transport);
98 static void unlock_pack_on_signal(int signo)
100 unlock_pack();
101 sigchain_pop(signo);
102 raise(signo);
105 static void add_merge_config(struct ref **head,
106 const struct ref *remote_refs,
107 struct branch *branch,
108 struct ref ***tail)
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;
122 if (rm)
123 continue;
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.
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;
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)
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;
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;
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.
192 * Note: has_merge implies non-NULL branch->remote_name
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;
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;
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)
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;
239 #define REFCOL_WIDTH 10
241 static int update_local_ref(struct ref *ref,
242 const char *remote,
243 const struct ref *remote_ref,
244 struct strbuf *display)
246 struct commit *current = NULL, *updated;
247 enum object_type type;
248 struct branch *current_branch = branch_get(NULL);
249 const char *pretty_ref = prettify_refname(ref->name);
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 strbuf_addf(display, "= %-*s %-*s -> %s",
258 TRANSPORT_SUMMARY_WIDTH,
259 _("[up to date]"), REFCOL_WIDTH,
260 remote, pretty_ref);
261 return 0;
264 if (current_branch &&
265 !strcmp(ref->name, current_branch->name) &&
266 !(update_head_ok || is_bare_repository()) &&
267 !is_null_sha1(ref->old_sha1)) {
269 * If this is the head, and it's not okay to update
270 * the head, and the old value of the head isn't empty...
272 strbuf_addf(display,
273 _("! %-*s %-*s -> %s (can't fetch in current branch)"),
274 TRANSPORT_SUMMARY_WIDTH, _("[rejected]"),
275 REFCOL_WIDTH, remote, pretty_ref);
276 return 1;
279 if (!is_null_sha1(ref->old_sha1) &&
280 !prefixcmp(ref->name, "refs/tags/")) {
281 int r;
282 r = s_update_ref("updating tag", ref, 0);
283 strbuf_addf(display, "%c %-*s %-*s -> %s%s",
284 r ? '!' : '-',
285 TRANSPORT_SUMMARY_WIDTH, _("[tag update]"),
286 REFCOL_WIDTH, remote, pretty_ref,
287 r ? _(" (unable to update local ref)") : "");
288 return r;
291 current = lookup_commit_reference_gently(ref->old_sha1, 1);
292 updated = lookup_commit_reference_gently(ref->new_sha1, 1);
293 if (!current || !updated) {
294 const char *msg;
295 const char *what;
296 int r;
297 if (!strncmp(ref->name, "refs/tags/", 10)) {
298 msg = "storing tag";
299 what = _("[new tag]");
301 else {
302 msg = "storing head";
303 what = _("[new branch]");
304 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
305 (recurse_submodules != RECURSE_SUBMODULES_ON))
306 check_for_new_submodule_commits(ref->new_sha1);
309 r = s_update_ref(msg, ref, 0);
310 strbuf_addf(display, "%c %-*s %-*s -> %s%s",
311 r ? '!' : '*',
312 TRANSPORT_SUMMARY_WIDTH, what,
313 REFCOL_WIDTH, remote, pretty_ref,
314 r ? _(" (unable to update local ref)") : "");
315 return r;
318 if (in_merge_bases(current, &updated, 1)) {
319 char quickref[83];
320 int r;
321 strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
322 strcat(quickref, "..");
323 strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
324 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
325 (recurse_submodules != RECURSE_SUBMODULES_ON))
326 check_for_new_submodule_commits(ref->new_sha1);
327 r = s_update_ref("fast-forward", ref, 1);
328 strbuf_addf(display, "%c %-*s %-*s -> %s%s",
329 r ? '!' : ' ',
330 TRANSPORT_SUMMARY_WIDTH, quickref,
331 REFCOL_WIDTH, remote, pretty_ref,
332 r ? _(" (unable to update local ref)") : "");
333 return r;
334 } else if (force || ref->force) {
335 char quickref[84];
336 int r;
337 strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
338 strcat(quickref, "...");
339 strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
340 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
341 (recurse_submodules != RECURSE_SUBMODULES_ON))
342 check_for_new_submodule_commits(ref->new_sha1);
343 r = s_update_ref("forced-update", ref, 1);
344 strbuf_addf(display, "%c %-*s %-*s -> %s (%s)",
345 r ? '!' : '+',
346 TRANSPORT_SUMMARY_WIDTH, quickref,
347 REFCOL_WIDTH, remote, pretty_ref,
348 r ? _("unable to update local ref") : _("forced update"));
349 return r;
350 } else {
351 strbuf_addf(display, "! %-*s %-*s -> %s %s",
352 TRANSPORT_SUMMARY_WIDTH, _("[rejected]"),
353 REFCOL_WIDTH, remote, pretty_ref,
354 _("(non-fast-forward)"));
355 return 1;
359 static int iterate_ref_map(void *cb_data, unsigned char sha1[20])
361 struct ref **rm = cb_data;
362 struct ref *ref = *rm;
364 if (!ref)
365 return -1; /* end of the list */
366 *rm = ref->next;
367 hashcpy(sha1, ref->old_sha1);
368 return 0;
371 static int store_updated_refs(const char *raw_url, const char *remote_name,
372 struct ref *ref_map)
374 FILE *fp;
375 struct commit *commit;
376 int url_len, i, shown_url = 0, rc = 0;
377 struct strbuf note = STRBUF_INIT;
378 const char *what, *kind;
379 struct ref *rm;
380 char *url, *filename = dry_run ? "/dev/null" : git_path("FETCH_HEAD");
381 int want_merge;
383 fp = fopen(filename, "a");
384 if (!fp)
385 return error(_("cannot open %s: %s\n"), filename, strerror(errno));
387 if (raw_url)
388 url = transport_anonymize_url(raw_url);
389 else
390 url = xstrdup("foreign");
392 rm = ref_map;
393 if (check_everything_connected(iterate_ref_map, 0, &rm)) {
394 rc = error(_("%s did not send all necessary objects\n"), url);
395 goto abort;
399 * The first pass writes objects to be merged and then the
400 * second pass writes the rest, in order to allow using
401 * FETCH_HEAD as a refname to refer to the ref to be merged.
403 for (want_merge = 1; 0 <= want_merge; want_merge--) {
404 for (rm = ref_map; rm; rm = rm->next) {
405 struct ref *ref = NULL;
407 commit = lookup_commit_reference_gently(rm->old_sha1, 1);
408 if (!commit)
409 rm->merge = 0;
411 if (rm->merge != want_merge)
412 continue;
414 if (rm->peer_ref) {
415 ref = xcalloc(1, sizeof(*ref) + strlen(rm->peer_ref->name) + 1);
416 strcpy(ref->name, rm->peer_ref->name);
417 hashcpy(ref->old_sha1, rm->peer_ref->old_sha1);
418 hashcpy(ref->new_sha1, rm->old_sha1);
419 ref->force = rm->peer_ref->force;
423 if (!strcmp(rm->name, "HEAD")) {
424 kind = "";
425 what = "";
427 else if (!prefixcmp(rm->name, "refs/heads/")) {
428 kind = "branch";
429 what = rm->name + 11;
431 else if (!prefixcmp(rm->name, "refs/tags/")) {
432 kind = "tag";
433 what = rm->name + 10;
435 else if (!prefixcmp(rm->name, "refs/remotes/")) {
436 kind = "remote-tracking branch";
437 what = rm->name + 13;
439 else {
440 kind = "";
441 what = rm->name;
444 url_len = strlen(url);
445 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
447 url_len = i + 1;
448 if (4 < i && !strncmp(".git", url + i - 3, 4))
449 url_len = i - 3;
451 strbuf_reset(&note);
452 if (*what) {
453 if (*kind)
454 strbuf_addf(&note, "%s ", kind);
455 strbuf_addf(&note, "'%s' of ", what);
457 fprintf(fp, "%s\t%s\t%s",
458 sha1_to_hex(rm->old_sha1),
459 rm->merge ? "" : "not-for-merge",
460 note.buf);
461 for (i = 0; i < url_len; ++i)
462 if ('\n' == url[i])
463 fputs("\\n", fp);
464 else
465 fputc(url[i], fp);
466 fputc('\n', fp);
468 strbuf_reset(&note);
469 if (ref) {
470 rc |= update_local_ref(ref, what, rm, &note);
471 free(ref);
472 } else
473 strbuf_addf(&note, "* %-*s %-*s -> FETCH_HEAD",
474 TRANSPORT_SUMMARY_WIDTH,
475 *kind ? kind : "branch",
476 REFCOL_WIDTH,
477 *what ? what : "HEAD");
478 if (note.len) {
479 if (verbosity >= 0 && !shown_url) {
480 fprintf(stderr, _("From %.*s\n"),
481 url_len, url);
482 shown_url = 1;
484 if (verbosity >= 0)
485 fprintf(stderr, " %s\n", note.buf);
490 if (rc & STORE_REF_ERROR_DF_CONFLICT)
491 error(_("some local refs could not be updated; try running\n"
492 " 'git remote prune %s' to remove any old, conflicting "
493 "branches"), remote_name);
495 abort:
496 strbuf_release(&note);
497 free(url);
498 fclose(fp);
499 return rc;
503 * We would want to bypass the object transfer altogether if
504 * everything we are going to fetch already exists and is connected
505 * locally.
507 static int quickfetch(struct ref *ref_map)
509 struct ref *rm = ref_map;
512 * If we are deepening a shallow clone we already have these
513 * objects reachable. Running rev-list here will return with
514 * a good (0) exit status and we'll bypass the fetch that we
515 * really need to perform. Claiming failure now will ensure
516 * we perform the network exchange to deepen our history.
518 if (depth)
519 return -1;
520 return check_everything_connected(iterate_ref_map, 1, &rm);
523 static int fetch_refs(struct transport *transport, struct ref *ref_map)
525 int ret = quickfetch(ref_map);
526 if (ret)
527 ret = transport_fetch_refs(transport, ref_map);
528 if (!ret)
529 ret |= store_updated_refs(transport->url,
530 transport->remote->name,
531 ref_map);
532 transport_unlock_pack(transport);
533 return ret;
536 static int prune_refs(struct refspec *refs, int ref_count, struct ref *ref_map)
538 int result = 0;
539 struct ref *ref, *stale_refs = get_stale_heads(refs, ref_count, ref_map);
540 const char *dangling_msg = dry_run
541 ? _(" (%s will become dangling)\n")
542 : _(" (%s has become dangling)\n");
544 for (ref = stale_refs; ref; ref = ref->next) {
545 if (!dry_run)
546 result |= delete_ref(ref->name, NULL, 0);
547 if (verbosity >= 0) {
548 fprintf(stderr, " x %-*s %-*s -> %s\n",
549 TRANSPORT_SUMMARY_WIDTH, _("[deleted]"),
550 REFCOL_WIDTH, _("(none)"), prettify_refname(ref->name));
551 warn_dangling_symref(stderr, dangling_msg, ref->name);
554 free_refs(stale_refs);
555 return result;
558 static int add_existing(const char *refname, const unsigned char *sha1,
559 int flag, void *cbdata)
561 struct string_list *list = (struct string_list *)cbdata;
562 struct string_list_item *item = string_list_insert(list, refname);
563 item->util = (void *)sha1;
564 return 0;
567 static int will_fetch(struct ref **head, const unsigned char *sha1)
569 struct ref *rm = *head;
570 while (rm) {
571 if (!hashcmp(rm->old_sha1, sha1))
572 return 1;
573 rm = rm->next;
575 return 0;
578 static void find_non_local_tags(struct transport *transport,
579 struct ref **head,
580 struct ref ***tail)
582 struct string_list existing_refs = STRING_LIST_INIT_NODUP;
583 struct string_list remote_refs = STRING_LIST_INIT_NODUP;
584 const struct ref *ref;
585 struct string_list_item *item = NULL;
587 for_each_ref(add_existing, &existing_refs);
588 for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
589 if (prefixcmp(ref->name, "refs/tags"))
590 continue;
593 * The peeled ref always follows the matching base
594 * ref, so if we see a peeled ref that we don't want
595 * to fetch then we can mark the ref entry in the list
596 * as one to ignore by setting util to NULL.
598 if (!suffixcmp(ref->name, "^{}")) {
599 if (item && !has_sha1_file(ref->old_sha1) &&
600 !will_fetch(head, ref->old_sha1) &&
601 !has_sha1_file(item->util) &&
602 !will_fetch(head, item->util))
603 item->util = NULL;
604 item = NULL;
605 continue;
609 * If item is non-NULL here, then we previously saw a
610 * ref not followed by a peeled reference, so we need
611 * to check if it is a lightweight tag that we want to
612 * fetch.
614 if (item && !has_sha1_file(item->util) &&
615 !will_fetch(head, item->util))
616 item->util = NULL;
618 item = NULL;
620 /* skip duplicates and refs that we already have */
621 if (string_list_has_string(&remote_refs, ref->name) ||
622 string_list_has_string(&existing_refs, ref->name))
623 continue;
625 item = string_list_insert(&remote_refs, ref->name);
626 item->util = (void *)ref->old_sha1;
628 string_list_clear(&existing_refs, 0);
631 * We may have a final lightweight tag that needs to be
632 * checked to see if it needs fetching.
634 if (item && !has_sha1_file(item->util) &&
635 !will_fetch(head, item->util))
636 item->util = NULL;
639 * For all the tags in the remote_refs string list,
640 * add them to the list of refs to be fetched
642 for_each_string_list_item(item, &remote_refs) {
643 /* Unless we have already decided to ignore this item... */
644 if (item->util)
646 struct ref *rm = alloc_ref(item->string);
647 rm->peer_ref = alloc_ref(item->string);
648 hashcpy(rm->old_sha1, item->util);
649 **tail = rm;
650 *tail = &rm->next;
654 string_list_clear(&remote_refs, 0);
657 static void check_not_current_branch(struct ref *ref_map)
659 struct branch *current_branch = branch_get(NULL);
661 if (is_bare_repository() || !current_branch)
662 return;
664 for (; ref_map; ref_map = ref_map->next)
665 if (ref_map->peer_ref && !strcmp(current_branch->refname,
666 ref_map->peer_ref->name))
667 die(_("Refusing to fetch into current branch %s "
668 "of non-bare repository"), current_branch->refname);
671 static int truncate_fetch_head(void)
673 char *filename = git_path("FETCH_HEAD");
674 FILE *fp = fopen(filename, "w");
676 if (!fp)
677 return error(_("cannot open %s: %s\n"), filename, strerror(errno));
678 fclose(fp);
679 return 0;
682 static int do_fetch(struct transport *transport,
683 struct refspec *refs, int ref_count)
685 struct string_list existing_refs = STRING_LIST_INIT_NODUP;
686 struct string_list_item *peer_item = NULL;
687 struct ref *ref_map;
688 struct ref *rm;
689 int autotags = (transport->remote->fetch_tags == 1);
691 for_each_ref(add_existing, &existing_refs);
693 if (tags == TAGS_DEFAULT) {
694 if (transport->remote->fetch_tags == 2)
695 tags = TAGS_SET;
696 if (transport->remote->fetch_tags == -1)
697 tags = TAGS_UNSET;
700 if (!transport->get_refs_list || !transport->fetch)
701 die(_("Don't know how to fetch from %s"), transport->url);
703 /* if not appending, truncate FETCH_HEAD */
704 if (!append && !dry_run) {
705 int errcode = truncate_fetch_head();
706 if (errcode)
707 return errcode;
710 ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
711 if (!update_head_ok)
712 check_not_current_branch(ref_map);
714 for (rm = ref_map; rm; rm = rm->next) {
715 if (rm->peer_ref) {
716 peer_item = string_list_lookup(&existing_refs,
717 rm->peer_ref->name);
718 if (peer_item)
719 hashcpy(rm->peer_ref->old_sha1,
720 peer_item->util);
724 if (tags == TAGS_DEFAULT && autotags)
725 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
726 if (fetch_refs(transport, ref_map)) {
727 free_refs(ref_map);
728 return 1;
730 if (prune) {
731 /* If --tags was specified, pretend the user gave us the canonical tags refspec */
732 if (tags == TAGS_SET) {
733 const char *tags_str = "refs/tags/*:refs/tags/*";
734 struct refspec *tags_refspec, *refspec;
736 /* Copy the refspec and add the tags to it */
737 refspec = xcalloc(ref_count + 1, sizeof(struct refspec));
738 tags_refspec = parse_fetch_refspec(1, &tags_str);
739 memcpy(refspec, refs, ref_count * sizeof(struct refspec));
740 memcpy(&refspec[ref_count], tags_refspec, sizeof(struct refspec));
741 ref_count++;
743 prune_refs(refspec, ref_count, ref_map);
745 ref_count--;
746 /* The rest of the strings belong to fetch_one */
747 free_refspec(1, tags_refspec);
748 free(refspec);
749 } else if (ref_count) {
750 prune_refs(refs, ref_count, ref_map);
751 } else {
752 prune_refs(transport->remote->fetch, transport->remote->fetch_refspec_nr, ref_map);
755 free_refs(ref_map);
757 /* if neither --no-tags nor --tags was specified, do automated tag
758 * following ... */
759 if (tags == TAGS_DEFAULT && autotags) {
760 struct ref **tail = &ref_map;
761 ref_map = NULL;
762 find_non_local_tags(transport, &ref_map, &tail);
763 if (ref_map) {
764 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);
765 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
766 fetch_refs(transport, ref_map);
768 free_refs(ref_map);
771 return 0;
774 static void set_option(const char *name, const char *value)
776 int r = transport_set_option(transport, name, value);
777 if (r < 0)
778 die(_("Option \"%s\" value \"%s\" is not valid for %s"),
779 name, value, transport->url);
780 if (r > 0)
781 warning(_("Option \"%s\" is ignored for %s\n"),
782 name, transport->url);
785 static int get_one_remote_for_fetch(struct remote *remote, void *priv)
787 struct string_list *list = priv;
788 if (!remote->skip_default_update)
789 string_list_append(list, remote->name);
790 return 0;
793 struct remote_group_data {
794 const char *name;
795 struct string_list *list;
798 static int get_remote_group(const char *key, const char *value, void *priv)
800 struct remote_group_data *g = priv;
802 if (!prefixcmp(key, "remotes.") &&
803 !strcmp(key + 8, g->name)) {
804 /* split list by white space */
805 int space = strcspn(value, " \t\n");
806 while (*value) {
807 if (space > 1) {
808 string_list_append(g->list,
809 xstrndup(value, space));
811 value += space + (value[space] != '\0');
812 space = strcspn(value, " \t\n");
816 return 0;
819 static int add_remote_or_group(const char *name, struct string_list *list)
821 int prev_nr = list->nr;
822 struct remote_group_data g;
823 g.name = name; g.list = list;
825 git_config(get_remote_group, &g);
826 if (list->nr == prev_nr) {
827 struct remote *remote;
828 if (!remote_is_configured(name))
829 return 0;
830 remote = remote_get(name);
831 string_list_append(list, remote->name);
833 return 1;
836 static void add_options_to_argv(int *argc, const char **argv)
838 if (dry_run)
839 argv[(*argc)++] = "--dry-run";
840 if (prune)
841 argv[(*argc)++] = "--prune";
842 if (update_head_ok)
843 argv[(*argc)++] = "--update-head-ok";
844 if (force)
845 argv[(*argc)++] = "--force";
846 if (keep)
847 argv[(*argc)++] = "--keep";
848 if (recurse_submodules == RECURSE_SUBMODULES_ON)
849 argv[(*argc)++] = "--recurse-submodules";
850 else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
851 argv[(*argc)++] = "--recurse-submodules=on-demand";
852 if (verbosity >= 2)
853 argv[(*argc)++] = "-v";
854 if (verbosity >= 1)
855 argv[(*argc)++] = "-v";
856 else if (verbosity < 0)
857 argv[(*argc)++] = "-q";
861 static int fetch_multiple(struct string_list *list)
863 int i, result = 0;
864 const char *argv[12] = { "fetch", "--append" };
865 int argc = 2;
867 add_options_to_argv(&argc, argv);
869 if (!append && !dry_run) {
870 int errcode = truncate_fetch_head();
871 if (errcode)
872 return errcode;
875 for (i = 0; i < list->nr; i++) {
876 const char *name = list->items[i].string;
877 argv[argc] = name;
878 argv[argc + 1] = NULL;
879 if (verbosity >= 0)
880 printf(_("Fetching %s\n"), name);
881 if (run_command_v_opt(argv, RUN_GIT_CMD)) {
882 error(_("Could not fetch %s"), name);
883 result = 1;
887 return result;
890 static int fetch_one(struct remote *remote, int argc, const char **argv)
892 int i;
893 static const char **refs = NULL;
894 struct refspec *refspec;
895 int ref_nr = 0;
896 int exit_code;
898 if (!remote)
899 die(_("No remote repository specified. Please, specify either a URL or a\n"
900 "remote name from which new revisions should be fetched."));
902 transport = transport_get(remote, NULL);
903 transport_set_verbosity(transport, verbosity, progress);
904 if (upload_pack)
905 set_option(TRANS_OPT_UPLOADPACK, upload_pack);
906 if (keep)
907 set_option(TRANS_OPT_KEEP, "yes");
908 if (depth)
909 set_option(TRANS_OPT_DEPTH, depth);
911 if (argc > 0) {
912 int j = 0;
913 refs = xcalloc(argc + 1, sizeof(const char *));
914 for (i = 0; i < argc; i++) {
915 if (!strcmp(argv[i], "tag")) {
916 char *ref;
917 i++;
918 if (i >= argc)
919 die(_("You need to specify a tag name."));
920 ref = xmalloc(strlen(argv[i]) * 2 + 22);
921 strcpy(ref, "refs/tags/");
922 strcat(ref, argv[i]);
923 strcat(ref, ":refs/tags/");
924 strcat(ref, argv[i]);
925 refs[j++] = ref;
926 } else
927 refs[j++] = argv[i];
929 refs[j] = NULL;
930 ref_nr = j;
933 sigchain_push_common(unlock_pack_on_signal);
934 atexit(unlock_pack);
935 refspec = parse_fetch_refspec(ref_nr, refs);
936 exit_code = do_fetch(transport, refspec, ref_nr);
937 free_refspec(ref_nr, refspec);
938 transport_disconnect(transport);
939 transport = NULL;
940 return exit_code;
943 int cmd_fetch(int argc, const char **argv, const char *prefix)
945 int i;
946 struct string_list list = STRING_LIST_INIT_NODUP;
947 struct remote *remote;
948 int result = 0;
950 packet_trace_identity("fetch");
952 /* Record the command line for the reflog */
953 strbuf_addstr(&default_rla, "fetch");
954 for (i = 1; i < argc; i++)
955 strbuf_addf(&default_rla, " %s", argv[i]);
957 argc = parse_options(argc, argv, prefix,
958 builtin_fetch_options, builtin_fetch_usage, 0);
960 if (recurse_submodules != RECURSE_SUBMODULES_OFF) {
961 if (recurse_submodules_default) {
962 int arg = parse_fetch_recurse_submodules_arg("--recurse-submodules-default", recurse_submodules_default);
963 set_config_fetch_recurse_submodules(arg);
965 gitmodules_config();
966 git_config(submodule_config, NULL);
969 if (all) {
970 if (argc == 1)
971 die(_("fetch --all does not take a repository argument"));
972 else if (argc > 1)
973 die(_("fetch --all does not make sense with refspecs"));
974 (void) for_each_remote(get_one_remote_for_fetch, &list);
975 result = fetch_multiple(&list);
976 } else if (argc == 0) {
977 /* No arguments -- use default remote */
978 remote = remote_get(NULL);
979 result = fetch_one(remote, argc, argv);
980 } else if (multiple) {
981 /* All arguments are assumed to be remotes or groups */
982 for (i = 0; i < argc; i++)
983 if (!add_remote_or_group(argv[i], &list))
984 die(_("No such remote or remote group: %s"), argv[i]);
985 result = fetch_multiple(&list);
986 } else {
987 /* Single remote or group */
988 (void) add_remote_or_group(argv[0], &list);
989 if (list.nr > 1) {
990 /* More than one remote */
991 if (argc > 1)
992 die(_("Fetching a group and specifying refspecs does not make sense"));
993 result = fetch_multiple(&list);
994 } else {
995 /* Zero or one remotes */
996 remote = remote_get(argv[0]);
997 result = fetch_one(remote, argc-1, argv+1);
1001 if (!result && (recurse_submodules != RECURSE_SUBMODULES_OFF)) {
1002 const char *options[10];
1003 int num_options = 0;
1004 add_options_to_argv(&num_options, options);
1005 result = fetch_populated_submodules(num_options, options,
1006 submodule_prefix,
1007 recurse_submodules,
1008 verbosity < 0);
1011 /* All names were strdup()ed or strndup()ed */
1012 list.strdup_strings = 1;
1013 string_list_clear(&list, 0);
1015 return result;