8 #include "string-list.h"
10 #include "transport.h"
11 #include "run-command.h"
12 #include "parse-options.h"
15 static const char * const builtin_fetch_usage[] = {
16 "git fetch [options] [<repository> <refspec>...]",
17 "git fetch [options] <group>",
18 "git fetch --all [options]",
28 static int all, append, force, keep, update_head_ok, verbosity;
29 static int tags = TAGS_DEFAULT;
30 static const char *depth;
31 static const char *upload_pack;
32 static struct strbuf default_rla = STRBUF_INIT;
33 static struct transport *transport;
35 static struct option builtin_fetch_options[] = {
36 OPT__VERBOSITY(&verbosity),
37 OPT_BOOLEAN(0, "all", &all,
38 "fetch from all remotes"),
39 OPT_BOOLEAN('a', "append", &append,
40 "append to .git/FETCH_HEAD instead of overwriting"),
41 OPT_STRING(0, "upload-pack", &upload_pack, "PATH",
42 "path to upload pack on remote end"),
43 OPT_BOOLEAN('f', "force", &force,
44 "force overwrite of local branch"),
45 OPT_SET_INT('t', "tags", &tags,
46 "fetch all tags and associated objects", TAGS_SET),
47 OPT_SET_INT('n', NULL, &tags,
48 "do not fetch all tags (--no-tags)", TAGS_UNSET),
49 OPT_BOOLEAN('k', "keep", &keep, "keep downloaded pack"),
50 OPT_BOOLEAN('u', "update-head-ok", &update_head_ok,
51 "allow updating of HEAD ref"),
52 OPT_STRING(0, "depth", &depth, "DEPTH",
53 "deepen history of shallow clone"),
57 static void unlock_pack(void)
60 transport_unlock_pack(transport);
63 static void unlock_pack_on_signal(int signo)
70 static void add_merge_config(struct ref **head,
71 const struct ref *remote_refs,
72 struct branch *branch,
77 for (i = 0; i < branch->merge_nr; i++) {
78 struct ref *rm, **old_tail = *tail;
79 struct refspec refspec;
81 for (rm = *head; rm; rm = rm->next) {
82 if (branch_merge_matches(branch, i, rm->name)) {
91 * Not fetched to a tracking branch? We need to fetch
92 * it anyway to allow this branch's "branch.$name.merge"
93 * to be honored by 'git pull', but we do not have to
94 * fail if branch.$name.merge is misconfigured to point
95 * at a nonexisting branch. If we were indeed called by
96 * 'git pull', it will notice the misconfiguration because
97 * there is no entry in the resulting FETCH_HEAD marked
100 refspec.src = branch->merge[i]->src;
104 get_fetch_map(remote_refs, &refspec, tail, 1);
105 for (rm = *old_tail; rm; rm = rm->next)
110 static void find_non_local_tags(struct transport *transport,
114 static struct ref *get_ref_map(struct transport *transport,
115 struct refspec *refs, int ref_count, int tags,
120 struct ref *ref_map = NULL;
121 struct ref **tail = &ref_map;
123 const struct ref *remote_refs = transport_get_remote_refs(transport);
125 if (ref_count || tags == TAGS_SET) {
126 for (i = 0; i < ref_count; i++) {
127 get_fetch_map(remote_refs, &refs[i], &tail, 0);
128 if (refs[i].dst && refs[i].dst[0])
131 /* Merge everything on the command line, but not --tags */
132 for (rm = ref_map; rm; rm = rm->next)
134 if (tags == TAGS_SET)
135 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
137 /* Use the defaults */
138 struct remote *remote = transport->remote;
139 struct branch *branch = branch_get(NULL);
140 int has_merge = branch_has_merge_config(branch);
141 if (remote && (remote->fetch_refspec_nr || has_merge)) {
142 for (i = 0; i < remote->fetch_refspec_nr; i++) {
143 get_fetch_map(remote_refs, &remote->fetch[i], &tail, 0);
144 if (remote->fetch[i].dst &&
145 remote->fetch[i].dst[0])
147 if (!i && !has_merge && ref_map &&
148 !remote->fetch[0].pattern)
152 * if the remote we're fetching from is the same
153 * as given in branch.<name>.remote, we add the
154 * ref given in branch.<name>.merge, too.
157 !strcmp(branch->remote_name, remote->name))
158 add_merge_config(&ref_map, remote_refs, branch, &tail);
160 ref_map = get_remote_ref(remote_refs, "HEAD");
162 die("Couldn't find remote ref HEAD");
164 tail = &ref_map->next;
167 if (tags == TAGS_DEFAULT && *autotags)
168 find_non_local_tags(transport, &ref_map, &tail);
169 ref_remove_duplicates(ref_map);
174 #define STORE_REF_ERROR_OTHER 1
175 #define STORE_REF_ERROR_DF_CONFLICT 2
177 static int s_update_ref(const char *action,
182 char *rla = getenv("GIT_REFLOG_ACTION");
183 static struct ref_lock *lock;
186 rla = default_rla.buf;
187 snprintf(msg, sizeof(msg), "%s: %s", rla, action);
188 lock = lock_any_ref_for_update(ref->name,
189 check_old ? ref->old_sha1 : NULL, 0);
191 return errno == ENOTDIR ? STORE_REF_ERROR_DF_CONFLICT :
192 STORE_REF_ERROR_OTHER;
193 if (write_ref_sha1(lock, ref->new_sha1, msg) < 0)
194 return errno == ENOTDIR ? STORE_REF_ERROR_DF_CONFLICT :
195 STORE_REF_ERROR_OTHER;
199 #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
200 #define REFCOL_WIDTH 10
202 static int update_local_ref(struct ref *ref,
206 struct commit *current = NULL, *updated;
207 enum object_type type;
208 struct branch *current_branch = branch_get(NULL);
209 const char *pretty_ref = prettify_refname(ref->name);
212 type = sha1_object_info(ref->new_sha1, NULL);
214 die("object %s not found", sha1_to_hex(ref->new_sha1));
216 if (!hashcmp(ref->old_sha1, ref->new_sha1)) {
218 sprintf(display, "= %-*s %-*s -> %s", SUMMARY_WIDTH,
219 "[up to date]", REFCOL_WIDTH, remote,
224 if (current_branch &&
225 !strcmp(ref->name, current_branch->name) &&
226 !(update_head_ok || is_bare_repository()) &&
227 !is_null_sha1(ref->old_sha1)) {
229 * If this is the head, and it's not okay to update
230 * the head, and the old value of the head isn't empty...
232 sprintf(display, "! %-*s %-*s -> %s (can't fetch in current branch)",
233 SUMMARY_WIDTH, "[rejected]", REFCOL_WIDTH, remote,
238 if (!is_null_sha1(ref->old_sha1) &&
239 !prefixcmp(ref->name, "refs/tags/")) {
241 r = s_update_ref("updating tag", ref, 0);
242 sprintf(display, "%c %-*s %-*s -> %s%s", r ? '!' : '-',
243 SUMMARY_WIDTH, "[tag update]", REFCOL_WIDTH, remote,
244 pretty_ref, r ? " (unable to update local ref)" : "");
248 current = lookup_commit_reference_gently(ref->old_sha1, 1);
249 updated = lookup_commit_reference_gently(ref->new_sha1, 1);
250 if (!current || !updated) {
254 if (!strncmp(ref->name, "refs/tags/", 10)) {
259 msg = "storing head";
260 what = "[new branch]";
263 r = s_update_ref(msg, ref, 0);
264 sprintf(display, "%c %-*s %-*s -> %s%s", r ? '!' : '*',
265 SUMMARY_WIDTH, what, REFCOL_WIDTH, remote, pretty_ref,
266 r ? " (unable to update local ref)" : "");
270 if (in_merge_bases(current, &updated, 1)) {
273 strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
274 strcat(quickref, "..");
275 strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
276 r = s_update_ref("fast forward", ref, 1);
277 sprintf(display, "%c %-*s %-*s -> %s%s", r ? '!' : ' ',
278 SUMMARY_WIDTH, quickref, REFCOL_WIDTH, remote,
279 pretty_ref, r ? " (unable to update local ref)" : "");
281 } else if (force || ref->force) {
284 strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
285 strcat(quickref, "...");
286 strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
287 r = s_update_ref("forced-update", ref, 1);
288 sprintf(display, "%c %-*s %-*s -> %s (%s)", r ? '!' : '+',
289 SUMMARY_WIDTH, quickref, REFCOL_WIDTH, remote,
291 r ? "unable to update local ref" : "forced update");
294 sprintf(display, "! %-*s %-*s -> %s (non fast forward)",
295 SUMMARY_WIDTH, "[rejected]", REFCOL_WIDTH, remote,
301 static int store_updated_refs(const char *raw_url, const char *remote_name,
305 struct commit *commit;
306 int url_len, i, note_len, shown_url = 0, rc = 0;
308 const char *what, *kind;
310 char *url, *filename = git_path("FETCH_HEAD");
312 fp = fopen(filename, "a");
314 return error("cannot open %s: %s\n", filename, strerror(errno));
316 url = transport_anonymize_url(raw_url);
317 for (rm = ref_map; rm; rm = rm->next) {
318 struct ref *ref = NULL;
321 ref = xcalloc(1, sizeof(*ref) + strlen(rm->peer_ref->name) + 1);
322 strcpy(ref->name, rm->peer_ref->name);
323 hashcpy(ref->old_sha1, rm->peer_ref->old_sha1);
324 hashcpy(ref->new_sha1, rm->old_sha1);
325 ref->force = rm->peer_ref->force;
328 commit = lookup_commit_reference_gently(rm->old_sha1, 1);
332 if (!strcmp(rm->name, "HEAD")) {
336 else if (!prefixcmp(rm->name, "refs/heads/")) {
338 what = rm->name + 11;
340 else if (!prefixcmp(rm->name, "refs/tags/")) {
342 what = rm->name + 10;
344 else if (!prefixcmp(rm->name, "refs/remotes/")) {
345 kind = "remote branch";
346 what = rm->name + 13;
353 url_len = strlen(url);
354 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
357 if (4 < i && !strncmp(".git", url + i - 3, 4))
363 note_len += sprintf(note + note_len, "%s ",
365 note_len += sprintf(note + note_len, "'%s' of ", what);
367 note[note_len] = '\0';
368 fprintf(fp, "%s\t%s\t%s",
369 sha1_to_hex(commit ? commit->object.sha1 :
371 rm->merge ? "" : "not-for-merge",
373 for (i = 0; i < url_len; ++i)
381 rc |= update_local_ref(ref, what, note);
383 sprintf(note, "* %-*s %-*s -> FETCH_HEAD",
384 SUMMARY_WIDTH, *kind ? kind : "branch",
385 REFCOL_WIDTH, *what ? what : "HEAD");
387 if (verbosity >= 0 && !shown_url) {
388 fprintf(stderr, "From %.*s\n",
393 fprintf(stderr, " %s\n", note);
398 if (rc & STORE_REF_ERROR_DF_CONFLICT)
399 error("some local refs could not be updated; try running\n"
400 " 'git remote prune %s' to remove any old, conflicting "
401 "branches", remote_name);
406 * We would want to bypass the object transfer altogether if
407 * everything we are going to fetch already exists and is connected
410 * The refs we are going to fetch are in ref_map. If running
412 * $ git rev-list --objects --stdin --not --all
414 * (feeding all the refs in ref_map on its standard input)
415 * does not error out, that means everything reachable from the
416 * refs we are going to fetch exists and is connected to some of
419 static int quickfetch(struct ref *ref_map)
421 struct child_process revlist;
424 const char *argv[] = {"rev-list",
425 "--quiet", "--objects", "--stdin", "--not", "--all", NULL};
428 * If we are deepening a shallow clone we already have these
429 * objects reachable. Running rev-list here will return with
430 * a good (0) exit status and we'll bypass the fetch that we
431 * really need to perform. Claiming failure now will ensure
432 * we perform the network exchange to deepen our history.
440 memset(&revlist, 0, sizeof(revlist));
443 revlist.no_stdout = 1;
444 revlist.no_stderr = 1;
447 err = start_command(&revlist);
449 error("could not run rev-list");
454 * If rev-list --stdin encounters an unknown commit, it terminates,
455 * which will cause SIGPIPE in the write loop below.
457 sigchain_push(SIGPIPE, SIG_IGN);
459 for (ref = ref_map; ref; ref = ref->next) {
460 if (write_in_full(revlist.in, sha1_to_hex(ref->old_sha1), 40) < 0 ||
461 write_str_in_full(revlist.in, "\n") < 0) {
462 if (errno != EPIPE && errno != EINVAL)
463 error("failed write to rev-list: %s", strerror(errno));
469 if (close(revlist.in)) {
470 error("failed to close rev-list's stdin: %s", strerror(errno));
474 sigchain_pop(SIGPIPE);
476 return finish_command(&revlist) || err;
479 static int fetch_refs(struct transport *transport, struct ref *ref_map)
481 int ret = quickfetch(ref_map);
483 ret = transport_fetch_refs(transport, ref_map);
485 ret |= store_updated_refs(transport->url,
486 transport->remote->name,
488 transport_unlock_pack(transport);
492 static int add_existing(const char *refname, const unsigned char *sha1,
493 int flag, void *cbdata)
495 struct string_list *list = (struct string_list *)cbdata;
496 string_list_insert(refname, list);
500 static int will_fetch(struct ref **head, const unsigned char *sha1)
502 struct ref *rm = *head;
504 if (!hashcmp(rm->old_sha1, sha1))
511 static void find_non_local_tags(struct transport *transport,
515 struct string_list existing_refs = { NULL, 0, 0, 0 };
516 struct string_list new_refs = { NULL, 0, 0, 1 };
519 const unsigned char *ref_sha1;
520 const struct ref *tag_ref;
521 struct ref *rm = NULL;
522 const struct ref *ref;
524 for_each_ref(add_existing, &existing_refs);
525 for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
526 if (prefixcmp(ref->name, "refs/tags"))
529 ref_name = xstrdup(ref->name);
530 ref_name_len = strlen(ref_name);
531 ref_sha1 = ref->old_sha1;
533 if (!strcmp(ref_name + ref_name_len - 3, "^{}")) {
534 ref_name[ref_name_len - 3] = 0;
535 tag_ref = transport_get_remote_refs(transport);
537 if (!strcmp(tag_ref->name, ref_name)) {
538 ref_sha1 = tag_ref->old_sha1;
541 tag_ref = tag_ref->next;
545 if (!string_list_has_string(&existing_refs, ref_name) &&
546 !string_list_has_string(&new_refs, ref_name) &&
547 (has_sha1_file(ref->old_sha1) ||
548 will_fetch(head, ref->old_sha1))) {
549 string_list_insert(ref_name, &new_refs);
551 rm = alloc_ref(ref_name);
552 rm->peer_ref = alloc_ref(ref_name);
553 hashcpy(rm->old_sha1, ref_sha1);
560 string_list_clear(&existing_refs, 0);
561 string_list_clear(&new_refs, 0);
564 static void check_not_current_branch(struct ref *ref_map)
566 struct branch *current_branch = branch_get(NULL);
568 if (is_bare_repository() || !current_branch)
571 for (; ref_map; ref_map = ref_map->next)
572 if (ref_map->peer_ref && !strcmp(current_branch->refname,
573 ref_map->peer_ref->name))
574 die("Refusing to fetch into current branch %s "
575 "of non-bare repository", current_branch->refname);
578 static int do_fetch(struct transport *transport,
579 struct refspec *refs, int ref_count)
583 int autotags = (transport->remote->fetch_tags == 1);
584 if (transport->remote->fetch_tags == 2 && tags != TAGS_UNSET)
586 if (transport->remote->fetch_tags == -1)
589 if (!transport->get_refs_list || !transport->fetch)
590 die("Don't know how to fetch from %s", transport->url);
592 /* if not appending, truncate FETCH_HEAD */
594 char *filename = git_path("FETCH_HEAD");
595 FILE *fp = fopen(filename, "w");
597 return error("cannot open %s: %s\n", filename, strerror(errno));
601 ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
603 check_not_current_branch(ref_map);
605 for (rm = ref_map; rm; rm = rm->next) {
607 read_ref(rm->peer_ref->name, rm->peer_ref->old_sha1);
610 if (tags == TAGS_DEFAULT && autotags)
611 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
612 if (fetch_refs(transport, ref_map)) {
618 /* if neither --no-tags nor --tags was specified, do automated tag
620 if (tags == TAGS_DEFAULT && autotags) {
621 struct ref **tail = &ref_map;
623 find_non_local_tags(transport, &ref_map, &tail);
625 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);
626 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
627 fetch_refs(transport, ref_map);
635 static void set_option(const char *name, const char *value)
637 int r = transport_set_option(transport, name, value);
639 die("Option \"%s\" value \"%s\" is not valid for %s",
640 name, value, transport->url);
642 warning("Option \"%s\" is ignored for %s\n",
643 name, transport->url);
646 static int get_one_remote_for_fetch(struct remote *remote, void *priv)
648 struct string_list *list = priv;
649 string_list_append(remote->name, list);
653 struct remote_group_data {
655 struct string_list *list;
658 static int get_remote_group(const char *key, const char *value, void *priv)
660 struct remote_group_data *g = priv;
662 if (!prefixcmp(key, "remotes.") &&
663 !strcmp(key + 8, g->name)) {
664 /* split list by white space */
665 int space = strcspn(value, " \t\n");
668 string_list_append(xstrndup(value, space),
671 value += space + (value[space] != '\0');
672 space = strcspn(value, " \t\n");
679 static int add_remote_or_group(const char *name, struct string_list *list)
681 int prev_nr = list->nr;
682 struct remote_group_data g = { name, list };
684 git_config(get_remote_group, &g);
685 if (list->nr == prev_nr) {
686 struct remote *remote;
687 if (!remote_is_configured(name))
689 remote = remote_get(name);
690 string_list_append(remote->name, list);
695 static int fetch_multiple(struct string_list *list)
698 const char *argv[] = { "fetch", NULL, NULL, NULL, NULL };
705 else if (verbosity < 0)
708 for (i = 0; i < list->nr; i++) {
709 const char *name = list->items[i].string;
712 printf("Fetching %s\n", name);
713 if (run_command_v_opt(argv, RUN_GIT_CMD)) {
714 error("Could not fetch %s", name);
722 static int fetch_one(struct remote *remote, int argc, const char **argv)
725 static const char **refs = NULL;
730 die("Where do you want to fetch from today?");
732 transport = transport_get(remote, remote->url[0]);
734 transport->verbose = 1;
736 transport->verbose = -1;
738 set_option(TRANS_OPT_UPLOADPACK, upload_pack);
740 set_option(TRANS_OPT_KEEP, "yes");
742 set_option(TRANS_OPT_DEPTH, depth);
746 refs = xcalloc(argc + 1, sizeof(const char *));
747 for (i = 0; i < argc; i++) {
748 if (!strcmp(argv[i], "tag")) {
752 die("You need to specify a tag name.");
753 ref = xmalloc(strlen(argv[i]) * 2 + 22);
754 strcpy(ref, "refs/tags/");
755 strcat(ref, argv[i]);
756 strcat(ref, ":refs/tags/");
757 strcat(ref, argv[i]);
766 sigchain_push_common(unlock_pack_on_signal);
768 exit_code = do_fetch(transport,
769 parse_fetch_refspec(ref_nr, refs), ref_nr);
770 transport_disconnect(transport);
775 int cmd_fetch(int argc, const char **argv, const char *prefix)
778 struct string_list list = { NULL, 0, 0, 0 };
779 struct remote *remote;
782 /* Record the command line for the reflog */
783 strbuf_addstr(&default_rla, "fetch");
784 for (i = 1; i < argc; i++)
785 strbuf_addf(&default_rla, " %s", argv[i]);
787 argc = parse_options(argc, argv, prefix,
788 builtin_fetch_options, builtin_fetch_usage, 0);
792 die("fetch --all does not take a repository argument");
794 die("fetch --all does not make sense with refspecs");
795 (void) for_each_remote(get_one_remote_for_fetch, &list);
796 result = fetch_multiple(&list);
797 } else if (argc == 0) {
798 /* No arguments -- use default remote */
799 remote = remote_get(NULL);
800 result = fetch_one(remote, argc, argv);
802 /* Single remote or group */
803 (void) add_remote_or_group(argv[0], &list);
805 /* More than one remote */
807 die("Fetching a group and specifying refspecs does not make sense");
808 result = fetch_multiple(&list);
810 /* Zero or one remotes */
811 remote = remote_get(argv[0]);
812 result = fetch_one(remote, argc-1, argv+1);
816 /* All names were strdup()ed or strndup()ed */
817 list.strdup_strings = 1;
818 string_list_clear(&list, 0);