Make fetch a builtin
[git/gitweb.git] / builtin-fetch.c
blob64392f317d416e54554f634ece64c380c485c776
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;
16 static int unpacklimit;
18 static char *default_rla = NULL;
20 static void find_merge_config(struct ref *ref_map, struct remote *remote)
22 struct ref *rm = ref_map;
23 struct branch *branch = branch_get(NULL);
25 for (rm = ref_map; rm; rm = rm->next) {
26 if (!branch_has_merge_config(branch)) {
27 if (remote && remote->fetch &&
28 !strcmp(remote->fetch[0].src, rm->name))
29 rm->merge = 1;
30 } else {
31 if (branch_merges(branch, rm->name))
32 rm->merge = 1;
37 static struct ref *get_ref_map(struct transport *transport,
38 struct refspec *refs, int ref_count, int tags,
39 int *autotags)
41 int i;
42 struct ref *rm;
43 struct ref *ref_map = NULL;
44 struct ref **tail = &ref_map;
46 struct ref *remote_refs = transport_get_remote_refs(transport);
48 if (ref_count || tags) {
49 for (i = 0; i < ref_count; i++) {
50 get_fetch_map(remote_refs, &refs[i], &tail);
51 if (refs[i].dst && refs[i].dst[0])
52 *autotags = 1;
54 /* Merge everything on the command line, but not --tags */
55 for (rm = ref_map; rm; rm = rm->next)
56 rm->merge = 1;
57 if (tags) {
58 struct refspec refspec;
59 refspec.src = "refs/tags/";
60 refspec.dst = "refs/tags/";
61 refspec.pattern = 1;
62 refspec.force = 0;
63 get_fetch_map(remote_refs, &refspec, &tail);
65 } else {
66 /* Use the defaults */
67 struct remote *remote = transport->remote;
68 if (remote->fetch_refspec_nr) {
69 for (i = 0; i < remote->fetch_refspec_nr; i++) {
70 get_fetch_map(remote_refs, &remote->fetch[i], &tail);
71 if (remote->fetch[i].dst &&
72 remote->fetch[i].dst[0])
73 *autotags = 1;
75 find_merge_config(ref_map, remote);
76 } else {
77 ref_map = get_remote_ref(remote_refs, "HEAD");
79 ref_map->merge = 1;
83 return ref_map;
86 static void show_new(enum object_type type, unsigned char *sha1_new)
88 fprintf(stderr, " %s: %s\n", typename(type),
89 find_unique_abbrev(sha1_new, DEFAULT_ABBREV));
92 static int s_update_ref(const char *action,
93 struct ref *ref,
94 int check_old)
96 char msg[1024];
97 char *rla = getenv("GIT_REFLOG_ACTION");
98 static struct ref_lock *lock;
100 if (!rla)
101 rla = default_rla;
102 snprintf(msg, sizeof(msg), "%s: %s", rla, action);
103 lock = lock_any_ref_for_update(ref->name,
104 check_old ? ref->old_sha1 : NULL, 0);
105 if (!lock)
106 return 1;
107 if (write_ref_sha1(lock, ref->new_sha1, msg) < 0)
108 return 1;
109 return 0;
112 static int update_local_ref(struct ref *ref,
113 const char *note,
114 int verbose)
116 char oldh[41], newh[41];
117 struct commit *current = NULL, *updated;
118 enum object_type type;
119 struct branch *current_branch = branch_get(NULL);
121 type = sha1_object_info(ref->new_sha1, NULL);
122 if (type < 0)
123 die("object %s not found", sha1_to_hex(ref->new_sha1));
125 if (!*ref->name) {
126 /* Not storing */
127 if (verbose) {
128 fprintf(stderr, "* fetched %s\n", note);
129 show_new(type, ref->new_sha1);
131 return 0;
134 if (!hashcmp(ref->old_sha1, ref->new_sha1)) {
135 if (verbose) {
136 fprintf(stderr, "* %s: same as %s\n",
137 ref->name, note);
138 show_new(type, ref->new_sha1);
140 return 0;
143 if (!strcmp(ref->name, current_branch->name) &&
144 !(update_head_ok || is_bare_repository()) &&
145 !is_null_sha1(ref->old_sha1)) {
147 * If this is the head, and it's not okay to update
148 * the head, and the old value of the head isn't empty...
150 fprintf(stderr,
151 " * %s: Cannot fetch into the current branch.\n",
152 ref->name);
153 return 1;
156 if (!is_null_sha1(ref->old_sha1) &&
157 !prefixcmp(ref->name, "refs/tags/")) {
158 fprintf(stderr, "* %s: updating with %s\n",
159 ref->name, note);
160 show_new(type, ref->new_sha1);
161 return s_update_ref("updating tag", ref, 0);
164 current = lookup_commit_reference(ref->old_sha1);
165 updated = lookup_commit_reference(ref->new_sha1);
166 if (!current || !updated) {
167 char *msg;
168 if (!strncmp(ref->name, "refs/tags/", 10))
169 msg = "storing tag";
170 else
171 msg = "storing head";
172 fprintf(stderr, "* %s: storing %s\n",
173 ref->name, note);
174 show_new(type, ref->new_sha1);
175 return s_update_ref(msg, ref, 0);
178 strcpy(oldh, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
179 strcpy(newh, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
181 if (in_merge_bases(current, &updated, 1)) {
182 fprintf(stderr, "* %s: fast forward to %s\n",
183 ref->name, note);
184 fprintf(stderr, " old..new: %s..%s\n", oldh, newh);
185 return s_update_ref("fast forward", ref, 1);
187 if (!force && !ref->force) {
188 fprintf(stderr,
189 "* %s: not updating to non-fast forward %s\n",
190 ref->name, note);
191 fprintf(stderr,
192 " old...new: %s...%s\n", oldh, newh);
193 return 1;
195 fprintf(stderr,
196 "* %s: forcing update to non-fast forward %s\n",
197 ref->name, note);
198 fprintf(stderr, " old...new: %s...%s\n", oldh, newh);
199 return s_update_ref("forced-update", ref, 1);
202 static void store_updated_refs(const char *url, struct ref *ref_map)
204 FILE *fp;
205 struct commit *commit;
206 int url_len, i, note_len;
207 char note[1024];
208 const char *what, *kind;
209 struct ref *rm;
211 fp = fopen(git_path("FETCH_HEAD"), "a");
212 for (rm = ref_map; rm; rm = rm->next) {
213 struct ref *ref = NULL;
215 if (rm->peer_ref) {
216 ref = xcalloc(1, sizeof(*ref) + strlen(rm->peer_ref->name) + 1);
217 strcpy(ref->name, rm->peer_ref->name);
218 hashcpy(ref->old_sha1, rm->peer_ref->old_sha1);
219 hashcpy(ref->new_sha1, rm->old_sha1);
220 ref->force = rm->force;
223 commit = lookup_commit_reference(rm->old_sha1);
224 if (!commit)
225 rm->merge = 0;
227 if (!strcmp(rm->name, "HEAD")) {
228 kind = "";
229 what = "";
231 else if (!prefixcmp(rm->name, "refs/heads/")) {
232 kind = "branch";
233 what = rm->name + 11;
235 else if (!prefixcmp(rm->name, "refs/tags/")) {
236 kind = "tag";
237 what = rm->name + 10;
239 else if (!prefixcmp(rm->name, "refs/remotes/")) {
240 kind = "remote branch";
241 what = rm->name + 13;
243 else {
244 kind = "";
245 what = rm->name;
248 url_len = strlen(url);
249 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
251 url_len = i + 1;
252 if (4 < i && !strncmp(".git", url + i - 3, 4))
253 url_len = i - 3;
255 note_len = 0;
256 if (*what) {
257 if (*kind)
258 note_len += sprintf(note + note_len, "%s ",
259 kind);
260 note_len += sprintf(note + note_len, "'%s' of ", what);
262 note_len += sprintf(note + note_len, "%.*s", url_len, url);
263 fprintf(fp, "%s\t%s\t%s\n",
264 sha1_to_hex(commit ? commit->object.sha1 :
265 rm->old_sha1),
266 rm->merge ? "" : "not-for-merge",
267 note);
269 if (ref)
270 update_local_ref(ref, note, verbose);
272 fclose(fp);
275 static int fetch_refs(struct transport *transport, struct ref *ref_map)
277 int ret = transport_fetch_refs(transport, ref_map);
278 if (!ret)
279 store_updated_refs(transport->url, ref_map);
280 return ret;
283 static int add_existing(const char *refname, const unsigned char *sha1,
284 int flag, void *cbdata)
286 struct path_list *list = (struct path_list *)cbdata;
287 path_list_insert(refname, list);
288 return 0;
291 static struct ref *find_non_local_tags(struct transport *transport,
292 struct ref *fetch_map)
294 static struct path_list existing_refs = { NULL, 0, 0, 0 };
295 struct path_list new_refs = { NULL, 0, 0, 1 };
296 char *ref_name;
297 int ref_name_len;
298 unsigned char *ref_sha1;
299 struct ref *tag_ref;
300 struct ref *rm = NULL;
301 struct ref *ref_map = NULL;
302 struct ref **tail = &ref_map;
303 struct ref *ref;
305 for_each_ref(add_existing, &existing_refs);
306 for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
307 if (prefixcmp(ref->name, "refs/tags"))
308 continue;
310 ref_name = xstrdup(ref->name);
311 ref_name_len = strlen(ref_name);
312 ref_sha1 = ref->old_sha1;
314 if (!strcmp(ref_name + ref_name_len - 3, "^{}")) {
315 ref_name[ref_name_len - 3] = 0;
316 tag_ref = transport_get_remote_refs(transport);
317 while (tag_ref) {
318 if (!strcmp(tag_ref->name, ref_name)) {
319 ref_sha1 = tag_ref->old_sha1;
320 break;
322 tag_ref = tag_ref->next;
326 if (!path_list_has_path(&existing_refs, ref_name) &&
327 !path_list_has_path(&new_refs, ref_name) &&
328 lookup_object(ref->old_sha1)) {
329 fprintf(stderr, "Auto-following %s\n",
330 ref_name);
332 path_list_insert(ref_name, &new_refs);
334 rm = alloc_ref(strlen(ref_name) + 1);
335 strcpy(rm->name, ref_name);
336 rm->peer_ref = alloc_ref(strlen(ref_name) + 1);
337 strcpy(rm->peer_ref->name, ref_name);
338 hashcpy(rm->old_sha1, ref_sha1);
340 *tail = rm;
341 tail = &rm->next;
343 free(ref_name);
346 return ref_map;
349 static int do_fetch(struct transport *transport,
350 struct refspec *refs, int ref_count)
352 struct ref *ref_map, *fetch_map;
353 struct ref *rm;
354 int autotags = (transport->remote->fetch_tags == 1);
355 if (transport->remote->fetch_tags == 2 && !no_tags)
356 tags = 1;
357 if (transport->remote->fetch_tags == -1)
358 no_tags = 1;
360 if (!transport->ops || !transport->ops->get_refs_list ||
361 !(transport->ops->fetch_refs || transport->ops->fetch_objs))
362 die("Don't know how to fetch from %s", transport->url);
364 /* if not appending, truncate FETCH_HEAD */
365 if (!append)
366 fclose(fopen(git_path("FETCH_HEAD"), "w"));
368 ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
370 for (rm = ref_map; rm; rm = rm->next) {
371 if (rm->peer_ref)
372 read_ref(rm->peer_ref->name, rm->peer_ref->old_sha1);
374 printf("%s : %s\n", rm->name, rm->peer_ref ? rm->peer_ref->name : NULL);
375 printf(" < %s\n", sha1_to_hex(rm->old_sha1));
376 if (rm->peer_ref)
377 printf(" > %s\n", sha1_to_hex(rm->peer_ref->old_sha1));
378 if (!rm->peer_ref ||
379 hashcmp(rm->old_sha1, rm->peer_ref->old_sha1)) {
380 printf("%s needs update.\n", rm->name);
384 if (fetch_refs(transport, ref_map)) {
385 free_refs(ref_map);
386 return 1;
389 fetch_map = ref_map;
391 /* if neither --no-tags nor --tags was specified, do automated tag
392 * following ... */
393 if (!(tags || no_tags) && autotags) {
394 ref_map = find_non_local_tags(transport, fetch_map);
395 if (ref_map) {
396 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
397 fetch_refs(transport, ref_map);
399 free_refs(ref_map);
402 free_refs(fetch_map);
404 return 0;
407 static int fetch_config(const char *var, const char *value)
409 if (strcmp(var, "fetch.unpacklimit") == 0) {
410 unpacklimit = git_config_int(var, value);
411 return 0;
414 if (strcmp(var, "transfer.unpacklimit") == 0) {
415 unpacklimit = git_config_int(var, value);
416 return 0;
419 return git_default_config(var, value);
422 int cmd_fetch(int argc, const char **argv, const char *prefix)
424 struct remote *remote;
425 struct transport *transport;
426 int i, j, rla_offset;
427 static const char **refs = NULL;
428 int ref_nr = 0;
429 int cmd_len = 0;
430 const char *depth = NULL, *upload_pack = NULL;
431 int keep = 0;
433 git_config(fetch_config);
435 for (i = 1; i < argc; i++) {
436 const char *arg = argv[i];
437 cmd_len += strlen(arg);
439 if (arg[0] != '-')
440 break;
441 if (!strcmp(arg, "--append") || !strcmp(arg, "-a")) {
442 append = 1;
443 continue;
445 if (!prefixcmp(arg, "--upload-pack=")) {
446 upload_pack = arg + 14;
447 continue;
449 if (!strcmp(arg, "--upload-pack")) {
450 i++;
451 if (i == argc)
452 usage(fetch_usage);
453 upload_pack = argv[i];
454 continue;
456 if (!strcmp(arg, "--force") || !strcmp(arg, "-f")) {
457 force = 1;
458 continue;
460 if (!strcmp(arg, "--no-tags")) {
461 no_tags = 1;
462 continue;
464 if (!strcmp(arg, "--tags") || !strcmp(arg, "-t")) {
465 tags = 1;
466 continue;
468 if (!strcmp(arg, "--keep") || !strcmp(arg, "-k")) {
469 keep = 1;
470 continue;
472 if (!strcmp(arg, "--update-head-ok") || !strcmp(arg, "-u")) {
473 update_head_ok = 1;
474 continue;
476 if (!prefixcmp(arg, "--depth=")) {
477 depth = arg + 8;
478 continue;
480 if (!strcmp(arg, "--depth")) {
481 i++;
482 if (i == argc)
483 usage(fetch_usage);
484 depth = argv[i];
485 continue;
487 if (!strcmp(arg, "--quiet")) {
488 quiet = 1;
489 continue;
491 if (!strcmp(arg, "--verbose") || !strcmp(arg, "-v")) {
492 verbose++;
493 continue;
495 usage(fetch_usage);
498 for (j = i; j < argc; j++)
499 cmd_len += strlen(argv[j]);
501 default_rla = xmalloc(cmd_len + 5 + argc + 1);
502 sprintf(default_rla, "fetch");
503 rla_offset = strlen(default_rla);
504 for (j = 1; j < argc; j++) {
505 sprintf(default_rla + rla_offset, " %s", argv[j]);
506 rla_offset += strlen(argv[j]);
509 if (i == argc)
510 remote = remote_get(NULL);
511 else
512 remote = remote_get(argv[i++]);
514 transport = transport_get(remote, remote->uri[0], 1);
515 if (verbose >= 2)
516 transport->verbose = 1;
517 if (quiet)
518 transport->verbose = 0;
519 if (upload_pack)
520 transport_set_option(transport, TRANS_OPT_UPLOADPACK, upload_pack);
521 if (keep)
522 transport_set_option(transport, TRANS_OPT_KEEP, "yes");
523 transport_set_option(transport, TRANS_OPT_DEPTH, depth);
525 if (!transport->url)
526 die("Where do you want to fetch from today?");
528 if (i < argc) {
529 int j = 0;
530 refs = xcalloc(argc - i + 1, sizeof(const char *));
531 while (i < argc) {
532 if (!strcmp(argv[i], "tag")) {
533 char *ref;
534 i++;
535 ref = xmalloc(strlen(argv[i]) * 2 + 22);
536 strcpy(ref, "refs/tags/");
537 strcat(ref, argv[i]);
538 strcat(ref, ":refs/tags/");
539 strcat(ref, argv[i]);
540 refs[j++] = ref;
541 } else
542 refs[j++] = argv[i];
543 i++;
545 refs[j] = NULL;
546 ref_nr = j;
547 for (j = 0; refs[j]; j++)
548 printf("ref: %s\n", refs[j]);
551 return do_fetch(transport, parse_ref_spec(ref_nr, refs), ref_nr);