Correct handling of branch.$name.merge in builtin-fetch
[git/git.git] / builtin-fetch.c
CommitLineData
b888d61c
DB
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"
11
12static 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>...]";
13
14static int append, force, tags, no_tags, update_head_ok, verbose, quiet;
b888d61c 15static char *default_rla = NULL;
e4022ed2
SP
16static struct transport *transport;
17
18static void unlock_pack(void)
19{
20 if (transport)
21 transport_unlock_pack(transport);
22}
23
24static void unlock_pack_on_signal(int signo)
25{
26 unlock_pack();
27 signal(SIGINT, SIG_DFL);
28 raise(signo);
29}
b888d61c 30
85682c19
SP
31static void add_merge_config(struct ref **head,
32 struct ref *remote_refs,
33 struct branch *branch,
34 struct ref ***tail)
b888d61c 35{
85682c19 36 int i;
b888d61c 37
85682c19
SP
38 for (i = 0; i < branch->merge_nr; i++) {
39 struct ref *rm, **old_tail = *tail;
40 struct refspec refspec;
41
42 for (rm = *head; rm; rm = rm->next) {
43 if (branch_merge_matches(branch, i, rm->name)) {
b888d61c 44 rm->merge = 1;
85682c19
SP
45 break;
46 }
b888d61c 47 }
85682c19
SP
48 if (rm)
49 continue;
50
51 /* Not fetched to a tracking branch? We need to fetch
52 * it anyway to allow this branch's "branch.$name.merge"
53 * to be honored by git-pull.
54 */
55 refspec.src = branch->merge[i]->src;
56 refspec.dst = NULL;
57 refspec.pattern = 0;
58 refspec.force = 0;
59 get_fetch_map(remote_refs, &refspec, tail);
60 for (rm = *old_tail; rm; rm = rm->next)
61 rm->merge = 1;
b888d61c
DB
62 }
63}
64
65static struct ref *get_ref_map(struct transport *transport,
66 struct refspec *refs, int ref_count, int tags,
67 int *autotags)
68{
69 int i;
70 struct ref *rm;
71 struct ref *ref_map = NULL;
72 struct ref **tail = &ref_map;
73
74 struct ref *remote_refs = transport_get_remote_refs(transport);
75
76 if (ref_count || tags) {
77 for (i = 0; i < ref_count; i++) {
78 get_fetch_map(remote_refs, &refs[i], &tail);
79 if (refs[i].dst && refs[i].dst[0])
80 *autotags = 1;
81 }
82 /* Merge everything on the command line, but not --tags */
83 for (rm = ref_map; rm; rm = rm->next)
84 rm->merge = 1;
85 if (tags) {
86 struct refspec refspec;
87 refspec.src = "refs/tags/";
88 refspec.dst = "refs/tags/";
89 refspec.pattern = 1;
90 refspec.force = 0;
91 get_fetch_map(remote_refs, &refspec, &tail);
92 }
93 } else {
94 /* Use the defaults */
95 struct remote *remote = transport->remote;
85682c19
SP
96 struct branch *branch = branch_get(NULL);
97 int has_merge = branch_has_merge_config(branch);
98 if (remote && (remote->fetch_refspec_nr || has_merge)) {
b888d61c
DB
99 for (i = 0; i < remote->fetch_refspec_nr; i++) {
100 get_fetch_map(remote_refs, &remote->fetch[i], &tail);
101 if (remote->fetch[i].dst &&
102 remote->fetch[i].dst[0])
103 *autotags = 1;
85682c19
SP
104 if (!i && !has_merge && ref_map &&
105 !strcmp(remote->fetch[0].src, ref_map->name))
106 ref_map->merge = 1;
b888d61c 107 }
85682c19
SP
108 if (has_merge)
109 add_merge_config(&ref_map, remote_refs, branch, &tail);
b888d61c
DB
110 } else {
111 ref_map = get_remote_ref(remote_refs, "HEAD");
b888d61c
DB
112 ref_map->merge = 1;
113 }
114 }
115
116 return ref_map;
117}
118
119static void show_new(enum object_type type, unsigned char *sha1_new)
120{
121 fprintf(stderr, " %s: %s\n", typename(type),
122 find_unique_abbrev(sha1_new, DEFAULT_ABBREV));
123}
124
125static int s_update_ref(const char *action,
126 struct ref *ref,
127 int check_old)
128{
129 char msg[1024];
130 char *rla = getenv("GIT_REFLOG_ACTION");
131 static struct ref_lock *lock;
132
133 if (!rla)
134 rla = default_rla;
135 snprintf(msg, sizeof(msg), "%s: %s", rla, action);
136 lock = lock_any_ref_for_update(ref->name,
137 check_old ? ref->old_sha1 : NULL, 0);
138 if (!lock)
139 return 1;
140 if (write_ref_sha1(lock, ref->new_sha1, msg) < 0)
141 return 1;
142 return 0;
143}
144
145static int update_local_ref(struct ref *ref,
146 const char *note,
147 int verbose)
148{
149 char oldh[41], newh[41];
150 struct commit *current = NULL, *updated;
151 enum object_type type;
152 struct branch *current_branch = branch_get(NULL);
153
154 type = sha1_object_info(ref->new_sha1, NULL);
155 if (type < 0)
156 die("object %s not found", sha1_to_hex(ref->new_sha1));
157
158 if (!*ref->name) {
159 /* Not storing */
160 if (verbose) {
161 fprintf(stderr, "* fetched %s\n", note);
162 show_new(type, ref->new_sha1);
163 }
164 return 0;
165 }
166
167 if (!hashcmp(ref->old_sha1, ref->new_sha1)) {
168 if (verbose) {
169 fprintf(stderr, "* %s: same as %s\n",
170 ref->name, note);
171 show_new(type, ref->new_sha1);
172 }
173 return 0;
174 }
175
b3abdd9d
SP
176 if (current_branch &&
177 !strcmp(ref->name, current_branch->name) &&
b888d61c
DB
178 !(update_head_ok || is_bare_repository()) &&
179 !is_null_sha1(ref->old_sha1)) {
180 /*
181 * If this is the head, and it's not okay to update
182 * the head, and the old value of the head isn't empty...
183 */
184 fprintf(stderr,
185 " * %s: Cannot fetch into the current branch.\n",
186 ref->name);
187 return 1;
188 }
189
190 if (!is_null_sha1(ref->old_sha1) &&
191 !prefixcmp(ref->name, "refs/tags/")) {
192 fprintf(stderr, "* %s: updating with %s\n",
193 ref->name, note);
194 show_new(type, ref->new_sha1);
195 return s_update_ref("updating tag", ref, 0);
196 }
197
198 current = lookup_commit_reference(ref->old_sha1);
199 updated = lookup_commit_reference(ref->new_sha1);
200 if (!current || !updated) {
201 char *msg;
202 if (!strncmp(ref->name, "refs/tags/", 10))
203 msg = "storing tag";
204 else
205 msg = "storing head";
206 fprintf(stderr, "* %s: storing %s\n",
207 ref->name, note);
208 show_new(type, ref->new_sha1);
209 return s_update_ref(msg, ref, 0);
210 }
211
212 strcpy(oldh, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
213 strcpy(newh, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
214
215 if (in_merge_bases(current, &updated, 1)) {
216 fprintf(stderr, "* %s: fast forward to %s\n",
217 ref->name, note);
218 fprintf(stderr, " old..new: %s..%s\n", oldh, newh);
219 return s_update_ref("fast forward", ref, 1);
220 }
221 if (!force && !ref->force) {
222 fprintf(stderr,
223 "* %s: not updating to non-fast forward %s\n",
224 ref->name, note);
225 fprintf(stderr,
226 " old...new: %s...%s\n", oldh, newh);
227 return 1;
228 }
229 fprintf(stderr,
230 "* %s: forcing update to non-fast forward %s\n",
231 ref->name, note);
232 fprintf(stderr, " old...new: %s...%s\n", oldh, newh);
233 return s_update_ref("forced-update", ref, 1);
234}
235
236static void store_updated_refs(const char *url, struct ref *ref_map)
237{
238 FILE *fp;
239 struct commit *commit;
240 int url_len, i, note_len;
241 char note[1024];
242 const char *what, *kind;
243 struct ref *rm;
244
245 fp = fopen(git_path("FETCH_HEAD"), "a");
246 for (rm = ref_map; rm; rm = rm->next) {
247 struct ref *ref = NULL;
248
249 if (rm->peer_ref) {
250 ref = xcalloc(1, sizeof(*ref) + strlen(rm->peer_ref->name) + 1);
251 strcpy(ref->name, rm->peer_ref->name);
252 hashcpy(ref->old_sha1, rm->peer_ref->old_sha1);
253 hashcpy(ref->new_sha1, rm->old_sha1);
1aad91f5 254 ref->force = rm->peer_ref->force;
b888d61c
DB
255 }
256
257 commit = lookup_commit_reference(rm->old_sha1);
258 if (!commit)
259 rm->merge = 0;
260
261 if (!strcmp(rm->name, "HEAD")) {
262 kind = "";
263 what = "";
264 }
265 else if (!prefixcmp(rm->name, "refs/heads/")) {
266 kind = "branch";
267 what = rm->name + 11;
268 }
269 else if (!prefixcmp(rm->name, "refs/tags/")) {
270 kind = "tag";
271 what = rm->name + 10;
272 }
273 else if (!prefixcmp(rm->name, "refs/remotes/")) {
274 kind = "remote branch";
275 what = rm->name + 13;
276 }
277 else {
278 kind = "";
279 what = rm->name;
280 }
281
282 url_len = strlen(url);
283 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
284 ;
285 url_len = i + 1;
286 if (4 < i && !strncmp(".git", url + i - 3, 4))
287 url_len = i - 3;
288
289 note_len = 0;
290 if (*what) {
291 if (*kind)
292 note_len += sprintf(note + note_len, "%s ",
293 kind);
294 note_len += sprintf(note + note_len, "'%s' of ", what);
295 }
296 note_len += sprintf(note + note_len, "%.*s", url_len, url);
297 fprintf(fp, "%s\t%s\t%s\n",
298 sha1_to_hex(commit ? commit->object.sha1 :
299 rm->old_sha1),
300 rm->merge ? "" : "not-for-merge",
301 note);
302
303 if (ref)
304 update_local_ref(ref, note, verbose);
305 }
306 fclose(fp);
307}
308
309static int fetch_refs(struct transport *transport, struct ref *ref_map)
310{
311 int ret = transport_fetch_refs(transport, ref_map);
312 if (!ret)
313 store_updated_refs(transport->url, ref_map);
1788c39c 314 transport_unlock_pack(transport);
b888d61c
DB
315 return ret;
316}
317
318static int add_existing(const char *refname, const unsigned char *sha1,
319 int flag, void *cbdata)
320{
321 struct path_list *list = (struct path_list *)cbdata;
322 path_list_insert(refname, list);
323 return 0;
324}
325
326static struct ref *find_non_local_tags(struct transport *transport,
327 struct ref *fetch_map)
328{
329 static struct path_list existing_refs = { NULL, 0, 0, 0 };
330 struct path_list new_refs = { NULL, 0, 0, 1 };
331 char *ref_name;
332 int ref_name_len;
333 unsigned char *ref_sha1;
334 struct ref *tag_ref;
335 struct ref *rm = NULL;
336 struct ref *ref_map = NULL;
337 struct ref **tail = &ref_map;
338 struct ref *ref;
339
340 for_each_ref(add_existing, &existing_refs);
341 for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
342 if (prefixcmp(ref->name, "refs/tags"))
343 continue;
344
345 ref_name = xstrdup(ref->name);
346 ref_name_len = strlen(ref_name);
347 ref_sha1 = ref->old_sha1;
348
349 if (!strcmp(ref_name + ref_name_len - 3, "^{}")) {
350 ref_name[ref_name_len - 3] = 0;
351 tag_ref = transport_get_remote_refs(transport);
352 while (tag_ref) {
353 if (!strcmp(tag_ref->name, ref_name)) {
354 ref_sha1 = tag_ref->old_sha1;
355 break;
356 }
357 tag_ref = tag_ref->next;
358 }
359 }
360
361 if (!path_list_has_path(&existing_refs, ref_name) &&
362 !path_list_has_path(&new_refs, ref_name) &&
363 lookup_object(ref->old_sha1)) {
364 fprintf(stderr, "Auto-following %s\n",
365 ref_name);
366
367 path_list_insert(ref_name, &new_refs);
368
369 rm = alloc_ref(strlen(ref_name) + 1);
370 strcpy(rm->name, ref_name);
371 rm->peer_ref = alloc_ref(strlen(ref_name) + 1);
372 strcpy(rm->peer_ref->name, ref_name);
373 hashcpy(rm->old_sha1, ref_sha1);
374
375 *tail = rm;
376 tail = &rm->next;
377 }
378 free(ref_name);
379 }
380
381 return ref_map;
382}
383
384static int do_fetch(struct transport *transport,
385 struct refspec *refs, int ref_count)
386{
387 struct ref *ref_map, *fetch_map;
388 struct ref *rm;
389 int autotags = (transport->remote->fetch_tags == 1);
390 if (transport->remote->fetch_tags == 2 && !no_tags)
391 tags = 1;
392 if (transport->remote->fetch_tags == -1)
393 no_tags = 1;
394
395 if (!transport->ops || !transport->ops->get_refs_list ||
425b1393 396 !transport->ops->fetch)
b888d61c
DB
397 die("Don't know how to fetch from %s", transport->url);
398
399 /* if not appending, truncate FETCH_HEAD */
400 if (!append)
401 fclose(fopen(git_path("FETCH_HEAD"), "w"));
402
403 ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
404
405 for (rm = ref_map; rm; rm = rm->next) {
406 if (rm->peer_ref)
407 read_ref(rm->peer_ref->name, rm->peer_ref->old_sha1);
b888d61c
DB
408 }
409
410 if (fetch_refs(transport, ref_map)) {
411 free_refs(ref_map);
412 return 1;
413 }
414
415 fetch_map = ref_map;
416
417 /* if neither --no-tags nor --tags was specified, do automated tag
418 * following ... */
419 if (!(tags || no_tags) && autotags) {
420 ref_map = find_non_local_tags(transport, fetch_map);
421 if (ref_map) {
422 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
423 fetch_refs(transport, ref_map);
424 }
425 free_refs(ref_map);
426 }
427
428 free_refs(fetch_map);
429
430 return 0;
431}
432
b888d61c
DB
433int cmd_fetch(int argc, const char **argv, const char *prefix)
434{
435 struct remote *remote;
b888d61c
DB
436 int i, j, rla_offset;
437 static const char **refs = NULL;
438 int ref_nr = 0;
439 int cmd_len = 0;
440 const char *depth = NULL, *upload_pack = NULL;
441 int keep = 0;
442
b888d61c
DB
443 for (i = 1; i < argc; i++) {
444 const char *arg = argv[i];
445 cmd_len += strlen(arg);
446
447 if (arg[0] != '-')
448 break;
449 if (!strcmp(arg, "--append") || !strcmp(arg, "-a")) {
450 append = 1;
451 continue;
452 }
453 if (!prefixcmp(arg, "--upload-pack=")) {
454 upload_pack = arg + 14;
455 continue;
456 }
457 if (!strcmp(arg, "--upload-pack")) {
458 i++;
459 if (i == argc)
460 usage(fetch_usage);
461 upload_pack = argv[i];
462 continue;
463 }
464 if (!strcmp(arg, "--force") || !strcmp(arg, "-f")) {
465 force = 1;
466 continue;
467 }
468 if (!strcmp(arg, "--no-tags")) {
469 no_tags = 1;
470 continue;
471 }
472 if (!strcmp(arg, "--tags") || !strcmp(arg, "-t")) {
473 tags = 1;
474 continue;
475 }
476 if (!strcmp(arg, "--keep") || !strcmp(arg, "-k")) {
477 keep = 1;
478 continue;
479 }
480 if (!strcmp(arg, "--update-head-ok") || !strcmp(arg, "-u")) {
481 update_head_ok = 1;
482 continue;
483 }
484 if (!prefixcmp(arg, "--depth=")) {
485 depth = arg + 8;
486 continue;
487 }
488 if (!strcmp(arg, "--depth")) {
489 i++;
490 if (i == argc)
491 usage(fetch_usage);
492 depth = argv[i];
493 continue;
494 }
495 if (!strcmp(arg, "--quiet")) {
496 quiet = 1;
497 continue;
498 }
499 if (!strcmp(arg, "--verbose") || !strcmp(arg, "-v")) {
500 verbose++;
501 continue;
502 }
503 usage(fetch_usage);
504 }
505
506 for (j = i; j < argc; j++)
507 cmd_len += strlen(argv[j]);
508
509 default_rla = xmalloc(cmd_len + 5 + argc + 1);
510 sprintf(default_rla, "fetch");
511 rla_offset = strlen(default_rla);
512 for (j = 1; j < argc; j++) {
513 sprintf(default_rla + rla_offset, " %s", argv[j]);
4ad1eada 514 rla_offset += strlen(argv[j]) + 1;
b888d61c
DB
515 }
516
517 if (i == argc)
518 remote = remote_get(NULL);
519 else
520 remote = remote_get(argv[i++]);
521
e5f4e214 522 transport = transport_get(remote, remote->uri[0]);
b888d61c
DB
523 if (verbose >= 2)
524 transport->verbose = 1;
525 if (quiet)
526 transport->verbose = 0;
527 if (upload_pack)
528 transport_set_option(transport, TRANS_OPT_UPLOADPACK, upload_pack);
529 if (keep)
530 transport_set_option(transport, TRANS_OPT_KEEP, "yes");
531 transport_set_option(transport, TRANS_OPT_DEPTH, depth);
532
533 if (!transport->url)
534 die("Where do you want to fetch from today?");
535
536 if (i < argc) {
537 int j = 0;
538 refs = xcalloc(argc - i + 1, sizeof(const char *));
539 while (i < argc) {
540 if (!strcmp(argv[i], "tag")) {
541 char *ref;
542 i++;
543 ref = xmalloc(strlen(argv[i]) * 2 + 22);
544 strcpy(ref, "refs/tags/");
545 strcat(ref, argv[i]);
546 strcat(ref, ":refs/tags/");
547 strcat(ref, argv[i]);
548 refs[j++] = ref;
549 } else
550 refs[j++] = argv[i];
551 i++;
552 }
553 refs[j] = NULL;
554 ref_nr = j;
b888d61c
DB
555 }
556
e4022ed2
SP
557 signal(SIGINT, unlock_pack_on_signal);
558 atexit(unlock_pack);
b888d61c
DB
559 return do_fetch(transport, parse_ref_spec(ref_nr, refs), ref_nr);
560}