git-fetch: do not fail when remote branch disappears
[git.git] / builtin-fetch.c
blob003ed76d16236ec6857a19d6223518e6e73bdc93
1 /*
2 * "git fetch"
3 */
4 #include "cache.h"
5 #include "refs.h"
6 #include "commit.h"
7 #include "builtin.h"
8 #include "path-list.h"
9 #include "remote.h"
10 #include "transport.h"
12 static const char fetch_usage[] = "git-fetch [-a | --append] [--upload-pack <upload-pack>] [-f | --force] [--no-tags] [-t | --tags] [-k | --keep] [-u | --update-head-ok] [--depth <depth>] [-v | --verbose] [<repository> <refspec>...]";
14 static int append, force, tags, no_tags, update_head_ok, verbose, quiet;
15 static char *default_rla = NULL;
16 static struct transport *transport;
18 static void unlock_pack(void)
20 if (transport)
21 transport_unlock_pack(transport);
24 static void unlock_pack_on_signal(int signo)
26 unlock_pack();
27 signal(SIGINT, SIG_DFL);
28 raise(signo);
31 static void add_merge_config(struct ref **head,
32 struct ref *remote_refs,
33 struct branch *branch,
34 struct ref ***tail)
36 int i;
38 for (i = 0; i < branch->merge_nr; i++) {
39 struct ref *rm, **old_tail = *tail;
40 struct refspec refspec;
42 for (rm = *head; rm; rm = rm->next) {
43 if (branch_merge_matches(branch, i, rm->name)) {
44 rm->merge = 1;
45 break;
48 if (rm)
49 continue;
52 * Not fetched to a tracking branch? We need to fetch
53 * it anyway to allow this branch's "branch.$name.merge"
54 * to be honored by git-pull, but we do not have to
55 * fail if branch.$name.merge is misconfigured to point
56 * at a nonexisting branch. If we were indeed called by
57 * git-pull, it will notice the misconfiguration because
58 * there is no entry in the resulting FETCH_HEAD marked
59 * for merging.
61 refspec.src = branch->merge[i]->src;
62 refspec.dst = NULL;
63 refspec.pattern = 0;
64 refspec.force = 0;
65 get_fetch_map(remote_refs, &refspec, tail, 1);
66 for (rm = *old_tail; rm; rm = rm->next)
67 rm->merge = 1;
71 static struct ref *get_ref_map(struct transport *transport,
72 struct refspec *refs, int ref_count, int tags,
73 int *autotags)
75 int i;
76 struct ref *rm;
77 struct ref *ref_map = NULL;
78 struct ref **tail = &ref_map;
80 struct ref *remote_refs = transport_get_remote_refs(transport);
82 if (ref_count || tags) {
83 for (i = 0; i < ref_count; i++) {
84 get_fetch_map(remote_refs, &refs[i], &tail, 0);
85 if (refs[i].dst && refs[i].dst[0])
86 *autotags = 1;
88 /* Merge everything on the command line, but not --tags */
89 for (rm = ref_map; rm; rm = rm->next)
90 rm->merge = 1;
91 if (tags) {
92 struct refspec refspec;
93 refspec.src = "refs/tags/";
94 refspec.dst = "refs/tags/";
95 refspec.pattern = 1;
96 refspec.force = 0;
97 get_fetch_map(remote_refs, &refspec, &tail, 0);
99 } else {
100 /* Use the defaults */
101 struct remote *remote = transport->remote;
102 struct branch *branch = branch_get(NULL);
103 int has_merge = branch_has_merge_config(branch);
104 if (remote && (remote->fetch_refspec_nr || has_merge)) {
105 for (i = 0; i < remote->fetch_refspec_nr; i++) {
106 get_fetch_map(remote_refs, &remote->fetch[i], &tail, 0);
107 if (remote->fetch[i].dst &&
108 remote->fetch[i].dst[0])
109 *autotags = 1;
110 if (!i && !has_merge && ref_map &&
111 !remote->fetch[0].pattern)
112 ref_map->merge = 1;
115 * if the remote we're fetching from is the same
116 * as given in branch.<name>.remote, we add the
117 * ref given in branch.<name>.merge, too.
119 if (has_merge &&
120 !strcmp(branch->remote_name, remote->name))
121 add_merge_config(&ref_map, remote_refs, branch, &tail);
122 } else {
123 ref_map = get_remote_ref(remote_refs, "HEAD");
124 if (!ref_map)
125 die("Couldn't find remote ref HEAD");
126 ref_map->merge = 1;
129 ref_remove_duplicates(ref_map);
131 return ref_map;
134 static void show_new(enum object_type type, unsigned char *sha1_new)
136 fprintf(stderr, " %s: %s\n", typename(type),
137 find_unique_abbrev(sha1_new, DEFAULT_ABBREV));
140 static int s_update_ref(const char *action,
141 struct ref *ref,
142 int check_old)
144 char msg[1024];
145 char *rla = getenv("GIT_REFLOG_ACTION");
146 static struct ref_lock *lock;
148 if (!rla)
149 rla = default_rla;
150 snprintf(msg, sizeof(msg), "%s: %s", rla, action);
151 lock = lock_any_ref_for_update(ref->name,
152 check_old ? ref->old_sha1 : NULL, 0);
153 if (!lock)
154 return 1;
155 if (write_ref_sha1(lock, ref->new_sha1, msg) < 0)
156 return 1;
157 return 0;
160 static int update_local_ref(struct ref *ref,
161 const char *note,
162 int verbose)
164 char oldh[41], newh[41];
165 struct commit *current = NULL, *updated;
166 enum object_type type;
167 struct branch *current_branch = branch_get(NULL);
169 type = sha1_object_info(ref->new_sha1, NULL);
170 if (type < 0)
171 die("object %s not found", sha1_to_hex(ref->new_sha1));
173 if (!*ref->name) {
174 /* Not storing */
175 if (verbose) {
176 fprintf(stderr, "* fetched %s\n", note);
177 show_new(type, ref->new_sha1);
179 return 0;
182 if (!hashcmp(ref->old_sha1, ref->new_sha1)) {
183 if (verbose) {
184 fprintf(stderr, "* %s: same as %s\n",
185 ref->name, note);
186 show_new(type, ref->new_sha1);
188 return 0;
191 if (current_branch &&
192 !strcmp(ref->name, current_branch->name) &&
193 !(update_head_ok || is_bare_repository()) &&
194 !is_null_sha1(ref->old_sha1)) {
196 * If this is the head, and it's not okay to update
197 * the head, and the old value of the head isn't empty...
199 fprintf(stderr,
200 " * %s: Cannot fetch into the current branch.\n",
201 ref->name);
202 return 1;
205 if (!is_null_sha1(ref->old_sha1) &&
206 !prefixcmp(ref->name, "refs/tags/")) {
207 fprintf(stderr, "* %s: updating with %s\n",
208 ref->name, note);
209 show_new(type, ref->new_sha1);
210 return s_update_ref("updating tag", ref, 0);
213 current = lookup_commit_reference_gently(ref->old_sha1, 1);
214 updated = lookup_commit_reference_gently(ref->new_sha1, 1);
215 if (!current || !updated) {
216 char *msg;
217 if (!strncmp(ref->name, "refs/tags/", 10))
218 msg = "storing tag";
219 else
220 msg = "storing head";
221 fprintf(stderr, "* %s: storing %s\n",
222 ref->name, note);
223 show_new(type, ref->new_sha1);
224 return s_update_ref(msg, ref, 0);
227 strcpy(oldh, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
228 strcpy(newh, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
230 if (in_merge_bases(current, &updated, 1)) {
231 fprintf(stderr, "* %s: fast forward to %s\n",
232 ref->name, note);
233 fprintf(stderr, " old..new: %s..%s\n", oldh, newh);
234 return s_update_ref("fast forward", ref, 1);
236 if (!force && !ref->force) {
237 fprintf(stderr,
238 "* %s: not updating to non-fast forward %s\n",
239 ref->name, note);
240 fprintf(stderr,
241 " old...new: %s...%s\n", oldh, newh);
242 return 1;
244 fprintf(stderr,
245 "* %s: forcing update to non-fast forward %s\n",
246 ref->name, note);
247 fprintf(stderr, " old...new: %s...%s\n", oldh, newh);
248 return s_update_ref("forced-update", ref, 1);
251 static void store_updated_refs(const char *url, struct ref *ref_map)
253 FILE *fp;
254 struct commit *commit;
255 int url_len, i, note_len;
256 char note[1024];
257 const char *what, *kind;
258 struct ref *rm;
260 fp = fopen(git_path("FETCH_HEAD"), "a");
261 for (rm = ref_map; rm; rm = rm->next) {
262 struct ref *ref = NULL;
264 if (rm->peer_ref) {
265 ref = xcalloc(1, sizeof(*ref) + strlen(rm->peer_ref->name) + 1);
266 strcpy(ref->name, rm->peer_ref->name);
267 hashcpy(ref->old_sha1, rm->peer_ref->old_sha1);
268 hashcpy(ref->new_sha1, rm->old_sha1);
269 ref->force = rm->peer_ref->force;
272 commit = lookup_commit_reference_gently(rm->old_sha1, 1);
273 if (!commit)
274 rm->merge = 0;
276 if (!strcmp(rm->name, "HEAD")) {
277 kind = "";
278 what = "";
280 else if (!prefixcmp(rm->name, "refs/heads/")) {
281 kind = "branch";
282 what = rm->name + 11;
284 else if (!prefixcmp(rm->name, "refs/tags/")) {
285 kind = "tag";
286 what = rm->name + 10;
288 else if (!prefixcmp(rm->name, "refs/remotes/")) {
289 kind = "remote branch";
290 what = rm->name + 13;
292 else {
293 kind = "";
294 what = rm->name;
297 url_len = strlen(url);
298 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
300 url_len = i + 1;
301 if (4 < i && !strncmp(".git", url + i - 3, 4))
302 url_len = i - 3;
304 note_len = 0;
305 if (*what) {
306 if (*kind)
307 note_len += sprintf(note + note_len, "%s ",
308 kind);
309 note_len += sprintf(note + note_len, "'%s' of ", what);
311 note_len += sprintf(note + note_len, "%.*s", url_len, url);
312 fprintf(fp, "%s\t%s\t%s\n",
313 sha1_to_hex(commit ? commit->object.sha1 :
314 rm->old_sha1),
315 rm->merge ? "" : "not-for-merge",
316 note);
318 if (ref)
319 update_local_ref(ref, note, verbose);
321 fclose(fp);
324 static int fetch_refs(struct transport *transport, struct ref *ref_map)
326 int ret = transport_fetch_refs(transport, ref_map);
327 if (!ret)
328 store_updated_refs(transport->url, ref_map);
329 transport_unlock_pack(transport);
330 return ret;
333 static int add_existing(const char *refname, const unsigned char *sha1,
334 int flag, void *cbdata)
336 struct path_list *list = (struct path_list *)cbdata;
337 path_list_insert(refname, list);
338 return 0;
341 static struct ref *find_non_local_tags(struct transport *transport,
342 struct ref *fetch_map)
344 static struct path_list existing_refs = { NULL, 0, 0, 0 };
345 struct path_list new_refs = { NULL, 0, 0, 1 };
346 char *ref_name;
347 int ref_name_len;
348 unsigned char *ref_sha1;
349 struct ref *tag_ref;
350 struct ref *rm = NULL;
351 struct ref *ref_map = NULL;
352 struct ref **tail = &ref_map;
353 struct ref *ref;
355 for_each_ref(add_existing, &existing_refs);
356 for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
357 if (prefixcmp(ref->name, "refs/tags"))
358 continue;
360 ref_name = xstrdup(ref->name);
361 ref_name_len = strlen(ref_name);
362 ref_sha1 = ref->old_sha1;
364 if (!strcmp(ref_name + ref_name_len - 3, "^{}")) {
365 ref_name[ref_name_len - 3] = 0;
366 tag_ref = transport_get_remote_refs(transport);
367 while (tag_ref) {
368 if (!strcmp(tag_ref->name, ref_name)) {
369 ref_sha1 = tag_ref->old_sha1;
370 break;
372 tag_ref = tag_ref->next;
376 if (!path_list_has_path(&existing_refs, ref_name) &&
377 !path_list_has_path(&new_refs, ref_name) &&
378 lookup_object(ref->old_sha1)) {
379 fprintf(stderr, "Auto-following %s\n",
380 ref_name);
382 path_list_insert(ref_name, &new_refs);
384 rm = alloc_ref(strlen(ref_name) + 1);
385 strcpy(rm->name, ref_name);
386 rm->peer_ref = alloc_ref(strlen(ref_name) + 1);
387 strcpy(rm->peer_ref->name, ref_name);
388 hashcpy(rm->old_sha1, ref_sha1);
390 *tail = rm;
391 tail = &rm->next;
393 free(ref_name);
396 return ref_map;
399 static int do_fetch(struct transport *transport,
400 struct refspec *refs, int ref_count)
402 struct ref *ref_map, *fetch_map;
403 struct ref *rm;
404 int autotags = (transport->remote->fetch_tags == 1);
405 if (transport->remote->fetch_tags == 2 && !no_tags)
406 tags = 1;
407 if (transport->remote->fetch_tags == -1)
408 no_tags = 1;
410 if (!transport->get_refs_list || !transport->fetch)
411 die("Don't know how to fetch from %s", transport->url);
413 /* if not appending, truncate FETCH_HEAD */
414 if (!append)
415 fclose(fopen(git_path("FETCH_HEAD"), "w"));
417 ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
419 for (rm = ref_map; rm; rm = rm->next) {
420 if (rm->peer_ref)
421 read_ref(rm->peer_ref->name, rm->peer_ref->old_sha1);
424 if (fetch_refs(transport, ref_map)) {
425 free_refs(ref_map);
426 return 1;
429 fetch_map = ref_map;
431 /* if neither --no-tags nor --tags was specified, do automated tag
432 * following ... */
433 if (!(tags || no_tags) && autotags) {
434 ref_map = find_non_local_tags(transport, fetch_map);
435 if (ref_map) {
436 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
437 fetch_refs(transport, ref_map);
439 free_refs(ref_map);
442 free_refs(fetch_map);
444 return 0;
447 static void set_option(const char *name, const char *value)
449 int r = transport_set_option(transport, name, value);
450 if (r < 0)
451 die("Option \"%s\" value \"%s\" is not valid for %s\n",
452 name, value, transport->url);
453 if (r > 0)
454 warning("Option \"%s\" is ignored for %s\n",
455 name, transport->url);
458 int cmd_fetch(int argc, const char **argv, const char *prefix)
460 struct remote *remote;
461 int i, j, rla_offset;
462 static const char **refs = NULL;
463 int ref_nr = 0;
464 int cmd_len = 0;
465 const char *depth = NULL, *upload_pack = NULL;
466 int keep = 0;
468 for (i = 1; i < argc; i++) {
469 const char *arg = argv[i];
470 cmd_len += strlen(arg);
472 if (arg[0] != '-')
473 break;
474 if (!strcmp(arg, "--append") || !strcmp(arg, "-a")) {
475 append = 1;
476 continue;
478 if (!prefixcmp(arg, "--upload-pack=")) {
479 upload_pack = arg + 14;
480 continue;
482 if (!strcmp(arg, "--upload-pack")) {
483 i++;
484 if (i == argc)
485 usage(fetch_usage);
486 upload_pack = argv[i];
487 continue;
489 if (!strcmp(arg, "--force") || !strcmp(arg, "-f")) {
490 force = 1;
491 continue;
493 if (!strcmp(arg, "--no-tags")) {
494 no_tags = 1;
495 continue;
497 if (!strcmp(arg, "--tags") || !strcmp(arg, "-t")) {
498 tags = 1;
499 continue;
501 if (!strcmp(arg, "--keep") || !strcmp(arg, "-k")) {
502 keep = 1;
503 continue;
505 if (!strcmp(arg, "--update-head-ok") || !strcmp(arg, "-u")) {
506 update_head_ok = 1;
507 continue;
509 if (!prefixcmp(arg, "--depth=")) {
510 depth = arg + 8;
511 continue;
513 if (!strcmp(arg, "--depth")) {
514 i++;
515 if (i == argc)
516 usage(fetch_usage);
517 depth = argv[i];
518 continue;
520 if (!strcmp(arg, "--quiet")) {
521 quiet = 1;
522 continue;
524 if (!strcmp(arg, "--verbose") || !strcmp(arg, "-v")) {
525 verbose++;
526 continue;
528 usage(fetch_usage);
531 for (j = i; j < argc; j++)
532 cmd_len += strlen(argv[j]);
534 default_rla = xmalloc(cmd_len + 5 + argc + 1);
535 sprintf(default_rla, "fetch");
536 rla_offset = strlen(default_rla);
537 for (j = 1; j < argc; j++) {
538 sprintf(default_rla + rla_offset, " %s", argv[j]);
539 rla_offset += strlen(argv[j]) + 1;
542 if (i == argc)
543 remote = remote_get(NULL);
544 else
545 remote = remote_get(argv[i++]);
547 transport = transport_get(remote, remote->url[0]);
548 if (verbose >= 2)
549 transport->verbose = 1;
550 if (quiet)
551 transport->verbose = -1;
552 if (upload_pack)
553 set_option(TRANS_OPT_UPLOADPACK, upload_pack);
554 if (keep)
555 set_option(TRANS_OPT_KEEP, "yes");
556 if (depth)
557 set_option(TRANS_OPT_DEPTH, depth);
559 if (!transport->url)
560 die("Where do you want to fetch from today?");
562 if (i < argc) {
563 int j = 0;
564 refs = xcalloc(argc - i + 1, sizeof(const char *));
565 while (i < argc) {
566 if (!strcmp(argv[i], "tag")) {
567 char *ref;
568 i++;
569 ref = xmalloc(strlen(argv[i]) * 2 + 22);
570 strcpy(ref, "refs/tags/");
571 strcat(ref, argv[i]);
572 strcat(ref, ":refs/tags/");
573 strcat(ref, argv[i]);
574 refs[j++] = ref;
575 } else
576 refs[j++] = argv[i];
577 i++;
579 refs[j] = NULL;
580 ref_nr = j;
583 signal(SIGINT, unlock_pack_on_signal);
584 atexit(unlock_pack);
585 return do_fetch(transport, parse_ref_spec(ref_nr, refs), ref_nr);