make display of total transferred fully accurate
[git/dscho.git] / builtin-fetch.c
blobe65690f25cdadb7b75aad40718510b1b9bdb8800
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 int s_update_ref(const char *action,
135 struct ref *ref,
136 int check_old)
138 char msg[1024];
139 char *rla = getenv("GIT_REFLOG_ACTION");
140 static struct ref_lock *lock;
142 if (!rla)
143 rla = default_rla;
144 snprintf(msg, sizeof(msg), "%s: %s", rla, action);
145 lock = lock_any_ref_for_update(ref->name,
146 check_old ? ref->old_sha1 : NULL, 0);
147 if (!lock)
148 return 1;
149 if (write_ref_sha1(lock, ref->new_sha1, msg) < 0)
150 return 1;
151 return 0;
154 #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
155 #define REFCOL_WIDTH 10
157 static int update_local_ref(struct ref *ref,
158 const char *remote,
159 int verbose,
160 char *display)
162 struct commit *current = NULL, *updated;
163 enum object_type type;
164 struct branch *current_branch = branch_get(NULL);
165 const char *pretty_ref = ref->name + (
166 !prefixcmp(ref->name, "refs/heads/") ? 11 :
167 !prefixcmp(ref->name, "refs/tags/") ? 10 :
168 !prefixcmp(ref->name, "refs/remotes/") ? 13 :
171 *display = 0;
172 type = sha1_object_info(ref->new_sha1, NULL);
173 if (type < 0)
174 die("object %s not found", sha1_to_hex(ref->new_sha1));
176 if (!*ref->name) {
177 /* Not storing */
178 if (verbose)
179 sprintf(display, "* branch %s -> FETCH_HEAD", remote);
180 return 0;
183 if (!hashcmp(ref->old_sha1, ref->new_sha1)) {
184 if (verbose)
185 sprintf(display, "= %-*s %-*s -> %s", SUMMARY_WIDTH,
186 "[up to date]", REFCOL_WIDTH, remote,
187 pretty_ref);
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 sprintf(display, "! %-*s %-*s -> %s (can't fetch in current branch)",
200 SUMMARY_WIDTH, "[rejected]", REFCOL_WIDTH, remote,
201 pretty_ref);
202 return 1;
205 if (!is_null_sha1(ref->old_sha1) &&
206 !prefixcmp(ref->name, "refs/tags/")) {
207 sprintf(display, "- %-*s %-*s -> %s",
208 SUMMARY_WIDTH, "[tag update]", REFCOL_WIDTH, remote,
209 pretty_ref);
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 const char *msg;
217 const char *what;
218 if (!strncmp(ref->name, "refs/tags/", 10)) {
219 msg = "storing tag";
220 what = "[new tag]";
222 else {
223 msg = "storing head";
224 what = "[new branch]";
227 sprintf(display, "* %-*s %-*s -> %s", SUMMARY_WIDTH, what,
228 REFCOL_WIDTH, remote, pretty_ref);
229 return s_update_ref(msg, ref, 0);
232 if (in_merge_bases(current, &updated, 1)) {
233 char quickref[83];
234 strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
235 strcat(quickref, "..");
236 strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
237 sprintf(display, " %-*s %-*s -> %s", SUMMARY_WIDTH, quickref,
238 REFCOL_WIDTH, remote, pretty_ref);
239 return s_update_ref("fast forward", ref, 1);
240 } else if (force || ref->force) {
241 char quickref[84];
242 strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
243 strcat(quickref, "...");
244 strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
245 sprintf(display, "+ %-*s %-*s -> %s (forced update)",
246 SUMMARY_WIDTH, quickref, REFCOL_WIDTH, remote, pretty_ref);
247 return s_update_ref("forced-update", ref, 1);
248 } else {
249 sprintf(display, "! %-*s %-*s -> %s (non fast forward)",
250 SUMMARY_WIDTH, "[rejected]", REFCOL_WIDTH, remote,
251 pretty_ref);
252 return 1;
256 static void store_updated_refs(const char *url, struct ref *ref_map)
258 FILE *fp;
259 struct commit *commit;
260 int url_len, i, note_len, shown_url = 0;
261 char note[1024];
262 const char *what, *kind;
263 struct ref *rm;
265 fp = fopen(git_path("FETCH_HEAD"), "a");
266 for (rm = ref_map; rm; rm = rm->next) {
267 struct ref *ref = NULL;
269 if (rm->peer_ref) {
270 ref = xcalloc(1, sizeof(*ref) + strlen(rm->peer_ref->name) + 1);
271 strcpy(ref->name, rm->peer_ref->name);
272 hashcpy(ref->old_sha1, rm->peer_ref->old_sha1);
273 hashcpy(ref->new_sha1, rm->old_sha1);
274 ref->force = rm->peer_ref->force;
277 commit = lookup_commit_reference_gently(rm->old_sha1, 1);
278 if (!commit)
279 rm->merge = 0;
281 if (!strcmp(rm->name, "HEAD")) {
282 kind = "";
283 what = "";
285 else if (!prefixcmp(rm->name, "refs/heads/")) {
286 kind = "branch";
287 what = rm->name + 11;
289 else if (!prefixcmp(rm->name, "refs/tags/")) {
290 kind = "tag";
291 what = rm->name + 10;
293 else if (!prefixcmp(rm->name, "refs/remotes/")) {
294 kind = "remote branch";
295 what = rm->name + 13;
297 else {
298 kind = "";
299 what = rm->name;
302 url_len = strlen(url);
303 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
305 url_len = i + 1;
306 if (4 < i && !strncmp(".git", url + i - 3, 4))
307 url_len = i - 3;
309 note_len = 0;
310 if (*what) {
311 if (*kind)
312 note_len += sprintf(note + note_len, "%s ",
313 kind);
314 note_len += sprintf(note + note_len, "'%s' of ", what);
316 note_len += sprintf(note + note_len, "%.*s", url_len, url);
317 fprintf(fp, "%s\t%s\t%s\n",
318 sha1_to_hex(commit ? commit->object.sha1 :
319 rm->old_sha1),
320 rm->merge ? "" : "not-for-merge",
321 note);
323 if (ref) {
324 update_local_ref(ref, what, verbose, note);
325 if (*note) {
326 if (!shown_url) {
327 fprintf(stderr, "From %.*s\n",
328 url_len, url);
329 shown_url = 1;
331 fprintf(stderr, " %s\n", note);
335 fclose(fp);
338 static int fetch_refs(struct transport *transport, struct ref *ref_map)
340 int ret = transport_fetch_refs(transport, ref_map);
341 if (!ret)
342 store_updated_refs(transport->url, ref_map);
343 transport_unlock_pack(transport);
344 return ret;
347 static int add_existing(const char *refname, const unsigned char *sha1,
348 int flag, void *cbdata)
350 struct path_list *list = (struct path_list *)cbdata;
351 path_list_insert(refname, list);
352 return 0;
355 static struct ref *find_non_local_tags(struct transport *transport,
356 struct ref *fetch_map)
358 static struct path_list existing_refs = { NULL, 0, 0, 0 };
359 struct path_list new_refs = { NULL, 0, 0, 1 };
360 char *ref_name;
361 int ref_name_len;
362 unsigned char *ref_sha1;
363 struct ref *tag_ref;
364 struct ref *rm = NULL;
365 struct ref *ref_map = NULL;
366 struct ref **tail = &ref_map;
367 struct ref *ref;
369 for_each_ref(add_existing, &existing_refs);
370 for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
371 if (prefixcmp(ref->name, "refs/tags"))
372 continue;
374 ref_name = xstrdup(ref->name);
375 ref_name_len = strlen(ref_name);
376 ref_sha1 = ref->old_sha1;
378 if (!strcmp(ref_name + ref_name_len - 3, "^{}")) {
379 ref_name[ref_name_len - 3] = 0;
380 tag_ref = transport_get_remote_refs(transport);
381 while (tag_ref) {
382 if (!strcmp(tag_ref->name, ref_name)) {
383 ref_sha1 = tag_ref->old_sha1;
384 break;
386 tag_ref = tag_ref->next;
390 if (!path_list_has_path(&existing_refs, ref_name) &&
391 !path_list_has_path(&new_refs, ref_name) &&
392 lookup_object(ref->old_sha1)) {
393 path_list_insert(ref_name, &new_refs);
395 rm = alloc_ref(strlen(ref_name) + 1);
396 strcpy(rm->name, ref_name);
397 rm->peer_ref = alloc_ref(strlen(ref_name) + 1);
398 strcpy(rm->peer_ref->name, ref_name);
399 hashcpy(rm->old_sha1, ref_sha1);
401 *tail = rm;
402 tail = &rm->next;
404 free(ref_name);
407 return ref_map;
410 static int do_fetch(struct transport *transport,
411 struct refspec *refs, int ref_count)
413 struct ref *ref_map, *fetch_map;
414 struct ref *rm;
415 int autotags = (transport->remote->fetch_tags == 1);
416 if (transport->remote->fetch_tags == 2 && !no_tags)
417 tags = 1;
418 if (transport->remote->fetch_tags == -1)
419 no_tags = 1;
421 if (!transport->get_refs_list || !transport->fetch)
422 die("Don't know how to fetch from %s", transport->url);
424 /* if not appending, truncate FETCH_HEAD */
425 if (!append)
426 fclose(fopen(git_path("FETCH_HEAD"), "w"));
428 ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
430 for (rm = ref_map; rm; rm = rm->next) {
431 if (rm->peer_ref)
432 read_ref(rm->peer_ref->name, rm->peer_ref->old_sha1);
435 if (fetch_refs(transport, ref_map)) {
436 free_refs(ref_map);
437 return 1;
440 fetch_map = ref_map;
442 /* if neither --no-tags nor --tags was specified, do automated tag
443 * following ... */
444 if (!(tags || no_tags) && autotags) {
445 ref_map = find_non_local_tags(transport, fetch_map);
446 if (ref_map) {
447 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
448 fetch_refs(transport, ref_map);
450 free_refs(ref_map);
453 free_refs(fetch_map);
455 return 0;
458 static void set_option(const char *name, const char *value)
460 int r = transport_set_option(transport, name, value);
461 if (r < 0)
462 die("Option \"%s\" value \"%s\" is not valid for %s\n",
463 name, value, transport->url);
464 if (r > 0)
465 warning("Option \"%s\" is ignored for %s\n",
466 name, transport->url);
469 int cmd_fetch(int argc, const char **argv, const char *prefix)
471 struct remote *remote;
472 int i, j, rla_offset;
473 static const char **refs = NULL;
474 int ref_nr = 0;
475 int cmd_len = 0;
476 const char *depth = NULL, *upload_pack = NULL;
477 int keep = 0;
479 for (i = 1; i < argc; i++) {
480 const char *arg = argv[i];
481 cmd_len += strlen(arg);
483 if (arg[0] != '-')
484 break;
485 if (!strcmp(arg, "--append") || !strcmp(arg, "-a")) {
486 append = 1;
487 continue;
489 if (!prefixcmp(arg, "--upload-pack=")) {
490 upload_pack = arg + 14;
491 continue;
493 if (!strcmp(arg, "--upload-pack")) {
494 i++;
495 if (i == argc)
496 usage(fetch_usage);
497 upload_pack = argv[i];
498 continue;
500 if (!strcmp(arg, "--force") || !strcmp(arg, "-f")) {
501 force = 1;
502 continue;
504 if (!strcmp(arg, "--no-tags")) {
505 no_tags = 1;
506 continue;
508 if (!strcmp(arg, "--tags") || !strcmp(arg, "-t")) {
509 tags = 1;
510 continue;
512 if (!strcmp(arg, "--keep") || !strcmp(arg, "-k")) {
513 keep = 1;
514 continue;
516 if (!strcmp(arg, "--update-head-ok") || !strcmp(arg, "-u")) {
517 update_head_ok = 1;
518 continue;
520 if (!prefixcmp(arg, "--depth=")) {
521 depth = arg + 8;
522 continue;
524 if (!strcmp(arg, "--depth")) {
525 i++;
526 if (i == argc)
527 usage(fetch_usage);
528 depth = argv[i];
529 continue;
531 if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) {
532 quiet = 1;
533 continue;
535 if (!strcmp(arg, "--verbose") || !strcmp(arg, "-v")) {
536 verbose++;
537 continue;
539 usage(fetch_usage);
542 for (j = i; j < argc; j++)
543 cmd_len += strlen(argv[j]);
545 default_rla = xmalloc(cmd_len + 5 + argc + 1);
546 sprintf(default_rla, "fetch");
547 rla_offset = strlen(default_rla);
548 for (j = 1; j < argc; j++) {
549 sprintf(default_rla + rla_offset, " %s", argv[j]);
550 rla_offset += strlen(argv[j]) + 1;
553 if (i == argc)
554 remote = remote_get(NULL);
555 else
556 remote = remote_get(argv[i++]);
558 transport = transport_get(remote, remote->url[0]);
559 if (verbose >= 2)
560 transport->verbose = 1;
561 if (quiet)
562 transport->verbose = -1;
563 if (upload_pack)
564 set_option(TRANS_OPT_UPLOADPACK, upload_pack);
565 if (keep)
566 set_option(TRANS_OPT_KEEP, "yes");
567 if (depth)
568 set_option(TRANS_OPT_DEPTH, depth);
570 if (!transport->url)
571 die("Where do you want to fetch from today?");
573 if (i < argc) {
574 int j = 0;
575 refs = xcalloc(argc - i + 1, sizeof(const char *));
576 while (i < argc) {
577 if (!strcmp(argv[i], "tag")) {
578 char *ref;
579 i++;
580 ref = xmalloc(strlen(argv[i]) * 2 + 22);
581 strcpy(ref, "refs/tags/");
582 strcat(ref, argv[i]);
583 strcat(ref, ":refs/tags/");
584 strcat(ref, argv[i]);
585 refs[j++] = ref;
586 } else
587 refs[j++] = argv[i];
588 i++;
590 refs[j] = NULL;
591 ref_nr = j;
594 signal(SIGINT, unlock_pack_on_signal);
595 atexit(unlock_pack);
596 return do_fetch(transport, parse_ref_spec(ref_nr, refs), ref_nr);