Migrate git-quiltimport.sh to use git-rev-parse --parseopt
[git/dscho.git] / builtin-fetch.c
blob5f5b59bfdb5056dfedf692215db2f9e4207f8580
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)
156 static int update_local_ref(struct ref *ref,
157 const char *remote,
158 int verbose,
159 char *display)
161 struct commit *current = NULL, *updated;
162 enum object_type type;
163 struct branch *current_branch = branch_get(NULL);
164 const char *pretty_ref = ref->name + (
165 !prefixcmp(ref->name, "refs/heads/") ? 11 :
166 !prefixcmp(ref->name, "refs/tags/") ? 10 :
167 !prefixcmp(ref->name, "refs/remotes/") ? 13 :
170 *display = 0;
171 type = sha1_object_info(ref->new_sha1, NULL);
172 if (type < 0)
173 die("object %s not found", sha1_to_hex(ref->new_sha1));
175 if (!*ref->name) {
176 /* Not storing */
177 if (verbose)
178 sprintf(display, "* branch %s -> FETCH_HEAD", remote);
179 return 0;
182 if (!hashcmp(ref->old_sha1, ref->new_sha1)) {
183 if (verbose)
184 sprintf(display, "= %-*s %s -> %s", SUMMARY_WIDTH,
185 "[up to date]", remote, pretty_ref);
186 return 0;
189 if (current_branch &&
190 !strcmp(ref->name, current_branch->name) &&
191 !(update_head_ok || is_bare_repository()) &&
192 !is_null_sha1(ref->old_sha1)) {
194 * If this is the head, and it's not okay to update
195 * the head, and the old value of the head isn't empty...
197 sprintf(display, "! %-*s %s -> %s (can't fetch in current branch)",
198 SUMMARY_WIDTH, "[rejected]", remote, pretty_ref);
199 return 1;
202 if (!is_null_sha1(ref->old_sha1) &&
203 !prefixcmp(ref->name, "refs/tags/")) {
204 sprintf(display, "- %-*s %s -> %s",
205 SUMMARY_WIDTH, "[tag update]", remote, pretty_ref);
206 return s_update_ref("updating tag", ref, 0);
209 current = lookup_commit_reference_gently(ref->old_sha1, 1);
210 updated = lookup_commit_reference_gently(ref->new_sha1, 1);
211 if (!current || !updated) {
212 const char *msg;
213 const char *what;
214 if (!strncmp(ref->name, "refs/tags/", 10)) {
215 msg = "storing tag";
216 what = "[new tag]";
218 else {
219 msg = "storing head";
220 what = "[new branch]";
223 sprintf(display, "* %-*s %s -> %s",
224 SUMMARY_WIDTH, what, remote, pretty_ref);
225 return s_update_ref(msg, ref, 0);
228 if (in_merge_bases(current, &updated, 1)) {
229 char quickref[83];
230 strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
231 strcat(quickref, "..");
232 strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
233 sprintf(display, " %-*s %s -> %s (fast forward)",
234 SUMMARY_WIDTH, quickref, remote, pretty_ref);
235 return s_update_ref("fast forward", ref, 1);
236 } else if (force || ref->force) {
237 char quickref[84];
238 strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
239 strcat(quickref, "...");
240 strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
241 sprintf(display, "+ %-*s %s -> %s (forced update)",
242 SUMMARY_WIDTH, quickref, remote, pretty_ref);
243 return s_update_ref("forced-update", ref, 1);
244 } else {
245 sprintf(display, "! %-*s %s -> %s (non fast forward)",
246 SUMMARY_WIDTH, "[rejected]", remote, pretty_ref);
247 return 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, shown_url = 0;
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, what, verbose, note);
320 if (*note) {
321 if (!shown_url) {
322 fprintf(stderr, "From %.*s\n",
323 url_len, url);
324 shown_url = 1;
326 fprintf(stderr, " %s\n", note);
330 fclose(fp);
333 static int fetch_refs(struct transport *transport, struct ref *ref_map)
335 int ret = transport_fetch_refs(transport, ref_map);
336 if (!ret)
337 store_updated_refs(transport->url, ref_map);
338 transport_unlock_pack(transport);
339 return ret;
342 static int add_existing(const char *refname, const unsigned char *sha1,
343 int flag, void *cbdata)
345 struct path_list *list = (struct path_list *)cbdata;
346 path_list_insert(refname, list);
347 return 0;
350 static struct ref *find_non_local_tags(struct transport *transport,
351 struct ref *fetch_map)
353 static struct path_list existing_refs = { NULL, 0, 0, 0 };
354 struct path_list new_refs = { NULL, 0, 0, 1 };
355 char *ref_name;
356 int ref_name_len;
357 unsigned char *ref_sha1;
358 struct ref *tag_ref;
359 struct ref *rm = NULL;
360 struct ref *ref_map = NULL;
361 struct ref **tail = &ref_map;
362 struct ref *ref;
364 for_each_ref(add_existing, &existing_refs);
365 for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
366 if (prefixcmp(ref->name, "refs/tags"))
367 continue;
369 ref_name = xstrdup(ref->name);
370 ref_name_len = strlen(ref_name);
371 ref_sha1 = ref->old_sha1;
373 if (!strcmp(ref_name + ref_name_len - 3, "^{}")) {
374 ref_name[ref_name_len - 3] = 0;
375 tag_ref = transport_get_remote_refs(transport);
376 while (tag_ref) {
377 if (!strcmp(tag_ref->name, ref_name)) {
378 ref_sha1 = tag_ref->old_sha1;
379 break;
381 tag_ref = tag_ref->next;
385 if (!path_list_has_path(&existing_refs, ref_name) &&
386 !path_list_has_path(&new_refs, ref_name) &&
387 lookup_object(ref->old_sha1)) {
388 path_list_insert(ref_name, &new_refs);
390 rm = alloc_ref(strlen(ref_name) + 1);
391 strcpy(rm->name, ref_name);
392 rm->peer_ref = alloc_ref(strlen(ref_name) + 1);
393 strcpy(rm->peer_ref->name, ref_name);
394 hashcpy(rm->old_sha1, ref_sha1);
396 *tail = rm;
397 tail = &rm->next;
399 free(ref_name);
402 return ref_map;
405 static int do_fetch(struct transport *transport,
406 struct refspec *refs, int ref_count)
408 struct ref *ref_map, *fetch_map;
409 struct ref *rm;
410 int autotags = (transport->remote->fetch_tags == 1);
411 if (transport->remote->fetch_tags == 2 && !no_tags)
412 tags = 1;
413 if (transport->remote->fetch_tags == -1)
414 no_tags = 1;
416 if (!transport->get_refs_list || !transport->fetch)
417 die("Don't know how to fetch from %s", transport->url);
419 /* if not appending, truncate FETCH_HEAD */
420 if (!append)
421 fclose(fopen(git_path("FETCH_HEAD"), "w"));
423 ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
425 for (rm = ref_map; rm; rm = rm->next) {
426 if (rm->peer_ref)
427 read_ref(rm->peer_ref->name, rm->peer_ref->old_sha1);
430 if (fetch_refs(transport, ref_map)) {
431 free_refs(ref_map);
432 return 1;
435 fetch_map = ref_map;
437 /* if neither --no-tags nor --tags was specified, do automated tag
438 * following ... */
439 if (!(tags || no_tags) && autotags) {
440 ref_map = find_non_local_tags(transport, fetch_map);
441 if (ref_map) {
442 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
443 fetch_refs(transport, ref_map);
445 free_refs(ref_map);
448 free_refs(fetch_map);
450 return 0;
453 static void set_option(const char *name, const char *value)
455 int r = transport_set_option(transport, name, value);
456 if (r < 0)
457 die("Option \"%s\" value \"%s\" is not valid for %s\n",
458 name, value, transport->url);
459 if (r > 0)
460 warning("Option \"%s\" is ignored for %s\n",
461 name, transport->url);
464 int cmd_fetch(int argc, const char **argv, const char *prefix)
466 struct remote *remote;
467 int i, j, rla_offset;
468 static const char **refs = NULL;
469 int ref_nr = 0;
470 int cmd_len = 0;
471 const char *depth = NULL, *upload_pack = NULL;
472 int keep = 0;
474 for (i = 1; i < argc; i++) {
475 const char *arg = argv[i];
476 cmd_len += strlen(arg);
478 if (arg[0] != '-')
479 break;
480 if (!strcmp(arg, "--append") || !strcmp(arg, "-a")) {
481 append = 1;
482 continue;
484 if (!prefixcmp(arg, "--upload-pack=")) {
485 upload_pack = arg + 14;
486 continue;
488 if (!strcmp(arg, "--upload-pack")) {
489 i++;
490 if (i == argc)
491 usage(fetch_usage);
492 upload_pack = argv[i];
493 continue;
495 if (!strcmp(arg, "--force") || !strcmp(arg, "-f")) {
496 force = 1;
497 continue;
499 if (!strcmp(arg, "--no-tags")) {
500 no_tags = 1;
501 continue;
503 if (!strcmp(arg, "--tags") || !strcmp(arg, "-t")) {
504 tags = 1;
505 continue;
507 if (!strcmp(arg, "--keep") || !strcmp(arg, "-k")) {
508 keep = 1;
509 continue;
511 if (!strcmp(arg, "--update-head-ok") || !strcmp(arg, "-u")) {
512 update_head_ok = 1;
513 continue;
515 if (!prefixcmp(arg, "--depth=")) {
516 depth = arg + 8;
517 continue;
519 if (!strcmp(arg, "--depth")) {
520 i++;
521 if (i == argc)
522 usage(fetch_usage);
523 depth = argv[i];
524 continue;
526 if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) {
527 quiet = 1;
528 continue;
530 if (!strcmp(arg, "--verbose") || !strcmp(arg, "-v")) {
531 verbose++;
532 continue;
534 usage(fetch_usage);
537 for (j = i; j < argc; j++)
538 cmd_len += strlen(argv[j]);
540 default_rla = xmalloc(cmd_len + 5 + argc + 1);
541 sprintf(default_rla, "fetch");
542 rla_offset = strlen(default_rla);
543 for (j = 1; j < argc; j++) {
544 sprintf(default_rla + rla_offset, " %s", argv[j]);
545 rla_offset += strlen(argv[j]) + 1;
548 if (i == argc)
549 remote = remote_get(NULL);
550 else
551 remote = remote_get(argv[i++]);
553 transport = transport_get(remote, remote->url[0]);
554 if (verbose >= 2)
555 transport->verbose = 1;
556 if (quiet)
557 transport->verbose = -1;
558 if (upload_pack)
559 set_option(TRANS_OPT_UPLOADPACK, upload_pack);
560 if (keep)
561 set_option(TRANS_OPT_KEEP, "yes");
562 if (depth)
563 set_option(TRANS_OPT_DEPTH, depth);
565 if (!transport->url)
566 die("Where do you want to fetch from today?");
568 if (i < argc) {
569 int j = 0;
570 refs = xcalloc(argc - i + 1, sizeof(const char *));
571 while (i < argc) {
572 if (!strcmp(argv[i], "tag")) {
573 char *ref;
574 i++;
575 ref = xmalloc(strlen(argv[i]) * 2 + 22);
576 strcpy(ref, "refs/tags/");
577 strcat(ref, argv[i]);
578 strcat(ref, ":refs/tags/");
579 strcat(ref, argv[i]);
580 refs[j++] = ref;
581 } else
582 refs[j++] = argv[i];
583 i++;
585 refs[j] = NULL;
586 ref_nr = j;
589 signal(SIGINT, unlock_pack_on_signal);
590 atexit(unlock_pack);
591 return do_fetch(transport, parse_ref_spec(ref_nr, refs), ref_nr);