]> git.ipfire.org Git - thirdparty/git.git/blame - merge-recursive.c
Remove duplicate ref matches in fetch
[thirdparty/git.git] / merge-recursive.c
CommitLineData
6d297f81
JS
1/*
2 * Recursive Merge algorithm stolen from git-merge-recursive.py by
3 * Fredrik Kuivinen.
4 * The thieves were Alex Riesen and Johannes Schindelin, in June/July 2006
5 */
6d297f81
JS
6#include "cache.h"
7#include "cache-tree.h"
8#include "commit.h"
9#include "blob.h"
10#include "tree-walk.h"
11#include "diff.h"
12#include "diffcore.h"
13#include "run-command.h"
14#include "tag.h"
7a85b848 15#include "unpack-trees.h"
6d297f81 16#include "path-list.h"
c2b4faea 17#include "xdiff-interface.h"
f3ef6b6b 18#include "interpolate.h"
a129d96f 19#include "attr.h"
6d297f81 20
68faf689
JH
21static int subtree_merge;
22
23static struct tree *shift_tree_object(struct tree *one, struct tree *two)
24{
25 unsigned char shifted[20];
26
27 /*
28 * NEEDSWORK: this limits the recursion depth to hardcoded
29 * value '2' to avoid excessive overhead.
30 */
31 shift_tree(one->object.sha1, two->object.sha1, shifted, 2);
32 if (!hashcmp(two->object.sha1, shifted))
33 return two;
34 return lookup_tree(shifted);
35}
36
6d297f81
JS
37/*
38 * A virtual commit has
39 * - (const char *)commit->util set to the name, and
40 * - *(int *)commit->object.sha1 set to the virtual id.
41 */
6d297f81
JS
42
43static unsigned commit_list_count(const struct commit_list *l)
44{
45 unsigned c = 0;
46 for (; l; l = l->next )
47 c++;
48 return c;
49}
50
51static struct commit *make_virtual_commit(struct tree *tree, const char *comment)
52{
53 struct commit *commit = xcalloc(1, sizeof(struct commit));
54 static unsigned virtual_id = 1;
55 commit->tree = tree;
56 commit->util = (void*)comment;
57 *(int*)commit->object.sha1 = virtual_id++;
c1f3089e
JS
58 /* avoid warnings */
59 commit->object.parsed = 1;
6d297f81
JS
60 return commit;
61}
62
63/*
3058e933
JS
64 * Since we use get_tree_entry(), which does not put the read object into
65 * the object pool, we cannot rely on a == b.
6d297f81
JS
66 */
67static int sha_eq(const unsigned char *a, const unsigned char *b)
68{
3af244ca 69 if (!a && !b)
6d297f81 70 return 2;
87cb004e 71 return a && b && hashcmp(a, b) == 0;
6d297f81
JS
72}
73
6d297f81 74/*
3058e933
JS
75 * Since we want to write the index eventually, we cannot reuse the index
76 * for these (temporary) data.
6d297f81
JS
77 */
78struct stage_data
79{
80 struct
81 {
82 unsigned mode;
83 unsigned char sha[20];
84 } stages[4];
85 unsigned processed:1;
86};
87
66a155bc
SP
88struct output_buffer
89{
90 struct output_buffer *next;
91 char *str;
92};
93
5a753613
JS
94static struct path_list current_file_set = {NULL, 0, 0, 1};
95static struct path_list current_directory_set = {NULL, 0, 0, 1};
6d297f81 96
63889639 97static int call_depth = 0;
8c3275ab 98static int verbosity = 2;
66a155bc
SP
99static int buffer_output = 1;
100static struct output_buffer *output_list, *output_end;
6d297f81 101
8c3275ab
SP
102static int show (int v)
103{
104 return (!call_depth && verbosity >= v) || verbosity >= 5;
105}
106
107static void output(int v, const char *fmt, ...)
6d297f81
JS
108{
109 va_list args;
6d297f81 110 va_start(args, fmt);
66a155bc
SP
111 if (buffer_output && show(v)) {
112 struct output_buffer *b = xmalloc(sizeof(*b));
113 nfvasprintf(&b->str, fmt, args);
114 b->next = NULL;
115 if (output_end)
116 output_end->next = b;
117 else
118 output_list = b;
119 output_end = b;
120 } else if (show(v)) {
8c3275ab
SP
121 int i;
122 for (i = call_depth; i--;)
123 fputs(" ", stdout);
124 vfprintf(stdout, fmt, args);
125 fputc('\n', stdout);
126 }
6d297f81 127 va_end(args);
6d297f81
JS
128}
129
b79d18c9 130static void flush_output(void)
66a155bc
SP
131{
132 struct output_buffer *b, *n;
133 for (b = output_list; b; b = n) {
134 int i;
135 for (i = call_depth; i--;)
136 fputs(" ", stdout);
137 fputs(b->str, stdout);
138 fputc('\n', stdout);
139 n = b->next;
140 free(b->str);
141 free(b);
142 }
143 output_list = NULL;
144 output_end = NULL;
145}
146
3af244ca
JS
147static void output_commit_title(struct commit *commit)
148{
149 int i;
66a155bc 150 flush_output();
63889639 151 for (i = call_depth; i--;)
3af244ca
JS
152 fputs(" ", stdout);
153 if (commit->util)
154 printf("virtual %s\n", (char *)commit->util);
155 else {
9926ba98 156 printf("%s ", find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV));
3af244ca
JS
157 if (parse_commit(commit) != 0)
158 printf("(bad commit)\n");
159 else {
160 const char *s;
161 int len;
162 for (s = commit->buffer; *s; s++)
163 if (*s == '\n' && s[1] == '\n') {
164 s += 2;
165 break;
166 }
167 for (len = 0; s[len] && '\n' != s[len]; len++)
168 ; /* do nothing */
169 printf("%.*s\n", len, s);
170 }
171 }
172}
173
6d297f81
JS
174static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
175 const char *path, int stage, int refresh, int options)
176{
177 struct cache_entry *ce;
6d297f81
JS
178 ce = make_cache_entry(mode, sha1 ? sha1 : null_sha1, path, stage, refresh);
179 if (!ce)
0424138d 180 return error("addinfo_cache failed for path '%s'", path);
6d297f81
JS
181 return add_cache_entry(ce, options);
182}
183
184/*
185 * This is a global variable which is used in a number of places but
186 * only written to in the 'merge' function.
187 *
188 * index_only == 1 => Don't leave any non-stage 0 entries in the cache and
189 * don't update the working directory.
190 * 0 => Leave unmerged entries in the cache and update
191 * the working directory.
192 */
193static int index_only = 0;
194
933bf40a
LT
195static void init_tree_desc_from_tree(struct tree_desc *desc, struct tree *tree)
196{
197 parse_tree(tree);
198 init_tree_desc(desc, tree->buffer, tree->size);
199}
200
7a85b848 201static int git_merge_trees(int index_only,
6d297f81
JS
202 struct tree *common,
203 struct tree *head,
204 struct tree *merge)
205{
3af244ca 206 int rc;
933bf40a 207 struct tree_desc t[3];
7a85b848
JS
208 struct unpack_trees_options opts;
209
7a85b848
JS
210 memset(&opts, 0, sizeof(opts));
211 if (index_only)
212 opts.index_only = 1;
213 else
214 opts.update = 1;
215 opts.merge = 1;
216 opts.head_idx = 2;
217 opts.fn = threeway_merge;
218
933bf40a
LT
219 init_tree_desc_from_tree(t+0, common);
220 init_tree_desc_from_tree(t+1, head);
221 init_tree_desc_from_tree(t+2, merge);
7a85b848 222
933bf40a 223 rc = unpack_trees(3, t, &opts);
7a85b848 224 cache_tree_free(&active_cache_tree);
7a85b848 225 return rc;
6d297f81
JS
226}
227
8b944b56
JH
228static int unmerged_index(void)
229{
230 int i;
231 for (i = 0; i < active_nr; i++) {
232 struct cache_entry *ce = active_cache[i];
233 if (ce_stage(ce))
234 return 1;
235 }
236 return 0;
237}
238
3af244ca 239static struct tree *git_write_tree(void)
6d297f81 240{
5b982f84
JS
241 struct tree *result = NULL;
242
a97e4075
AR
243 if (unmerged_index()) {
244 int i;
245 output(0, "There are unmerged index entries:");
246 for (i = 0; i < active_nr; i++) {
247 struct cache_entry *ce = active_cache[i];
248 if (ce_stage(ce))
249 output(0, "%d %.*s", ce_stage(ce), ce_namelen(ce), ce->name);
250 }
8b944b56 251 return NULL;
a97e4075 252 }
5b982f84
JS
253
254 if (!active_cache_tree)
255 active_cache_tree = cache_tree();
256
257 if (!cache_tree_fully_valid(active_cache_tree) &&
8b944b56
JH
258 cache_tree_update(active_cache_tree,
259 active_cache, active_nr, 0, 0) < 0)
5b982f84
JS
260 die("error building trees");
261
262 result = lookup_tree(active_cache_tree->sha1);
263
5b982f84 264 return result;
6d297f81
JS
265}
266
6d297f81
JS
267static int save_files_dirs(const unsigned char *sha1,
268 const char *base, int baselen, const char *path,
269 unsigned int mode, int stage)
270{
271 int len = strlen(path);
2d7320d0 272 char *newpath = xmalloc(baselen + len + 1);
6d297f81
JS
273 memcpy(newpath, base, baselen);
274 memcpy(newpath + baselen, path, len);
275 newpath[baselen + len] = '\0';
276
277 if (S_ISDIR(mode))
5a753613 278 path_list_insert(newpath, &current_directory_set);
6d297f81 279 else
5a753613 280 path_list_insert(newpath, &current_file_set);
6d297f81
JS
281 free(newpath);
282
283 return READ_TREE_RECURSIVE;
284}
285
3af244ca 286static int get_files_dirs(struct tree *tree)
6d297f81
JS
287{
288 int n;
bd669986 289 if (read_tree_recursive(tree, "", 0, 0, NULL, save_files_dirs) != 0)
6d297f81 290 return 0;
5a753613 291 n = current_file_set.nr + current_directory_set.nr;
6d297f81
JS
292 return n;
293}
294
6d297f81
JS
295/*
296 * Returns a index_entry instance which doesn't have to correspond to
297 * a real cache entry in Git's index.
298 */
3af244ca
JS
299static struct stage_data *insert_stage_data(const char *path,
300 struct tree *o, struct tree *a, struct tree *b,
301 struct path_list *entries)
6d297f81 302{
3af244ca 303 struct path_list_item *item;
6d297f81
JS
304 struct stage_data *e = xcalloc(1, sizeof(struct stage_data));
305 get_tree_entry(o->object.sha1, path,
306 e->stages[1].sha, &e->stages[1].mode);
307 get_tree_entry(a->object.sha1, path,
308 e->stages[2].sha, &e->stages[2].mode);
309 get_tree_entry(b->object.sha1, path,
310 e->stages[3].sha, &e->stages[3].mode);
3af244ca
JS
311 item = path_list_insert(path, entries);
312 item->util = e;
6d297f81
JS
313 return e;
314}
315
6d297f81 316/*
5a753613 317 * Create a dictionary mapping file names to stage_data objects. The
6d297f81
JS
318 * dictionary contains one entry for every path with a non-zero stage entry.
319 */
3af244ca 320static struct path_list *get_unmerged(void)
6d297f81
JS
321{
322 struct path_list *unmerged = xcalloc(1, sizeof(struct path_list));
323 int i;
324
325 unmerged->strdup_paths = 1;
8b944b56 326
ad57cbca 327 for (i = 0; i < active_nr; i++) {
3af244ca
JS
328 struct path_list_item *item;
329 struct stage_data *e;
6d297f81
JS
330 struct cache_entry *ce = active_cache[i];
331 if (!ce_stage(ce))
332 continue;
333
3af244ca
JS
334 item = path_list_lookup(ce->name, unmerged);
335 if (!item) {
336 item = path_list_insert(ce->name, unmerged);
337 item->util = xcalloc(1, sizeof(struct stage_data));
338 }
339 e = item->util;
6d297f81 340 e->stages[ce_stage(ce)].mode = ntohl(ce->ce_mode);
8da71493 341 hashcpy(e->stages[ce_stage(ce)].sha, ce->sha1);
6d297f81
JS
342 }
343
6d297f81
JS
344 return unmerged;
345}
346
347struct rename
348{
349 struct diff_filepair *pair;
350 struct stage_data *src_entry;
351 struct stage_data *dst_entry;
352 unsigned processed:1;
353};
354
6d297f81 355/*
3dff5379 356 * Get information of all renames which occurred between 'o_tree' and
5a753613
JS
357 * 'tree'. We need the three trees in the merge ('o_tree', 'a_tree' and
358 * 'b_tree') to be able to associate the correct cache entries with
359 * the rename information. 'tree' is always equal to either a_tree or b_tree.
6d297f81
JS
360 */
361static struct path_list *get_renames(struct tree *tree,
5a753613
JS
362 struct tree *o_tree,
363 struct tree *a_tree,
364 struct tree *b_tree,
6d297f81
JS
365 struct path_list *entries)
366{
3af244ca
JS
367 int i;
368 struct path_list *renames;
369 struct diff_options opts;
3af244ca
JS
370
371 renames = xcalloc(1, sizeof(struct path_list));
6d297f81
JS
372 diff_setup(&opts);
373 opts.recursive = 1;
374 opts.detect_rename = DIFF_DETECT_RENAME;
375 opts.output_format = DIFF_FORMAT_NO_OUTPUT;
376 if (diff_setup_done(&opts) < 0)
377 die("diff setup failed");
5a753613 378 diff_tree_sha1(o_tree->object.sha1, tree->object.sha1, "", &opts);
6d297f81
JS
379 diffcore_std(&opts);
380 for (i = 0; i < diff_queued_diff.nr; ++i) {
3af244ca 381 struct path_list_item *item;
6d297f81
JS
382 struct rename *re;
383 struct diff_filepair *pair = diff_queued_diff.queue[i];
384 if (pair->status != 'R') {
385 diff_free_filepair(pair);
386 continue;
387 }
388 re = xmalloc(sizeof(*re));
389 re->processed = 0;
390 re->pair = pair;
3af244ca
JS
391 item = path_list_lookup(re->pair->one->path, entries);
392 if (!item)
393 re->src_entry = insert_stage_data(re->pair->one->path,
5a753613 394 o_tree, a_tree, b_tree, entries);
3af244ca
JS
395 else
396 re->src_entry = item->util;
397
398 item = path_list_lookup(re->pair->two->path, entries);
399 if (!item)
400 re->dst_entry = insert_stage_data(re->pair->two->path,
5a753613 401 o_tree, a_tree, b_tree, entries);
3af244ca
JS
402 else
403 re->dst_entry = item->util;
404 item = path_list_insert(pair->one->path, renames);
6d297f81
JS
405 item->util = re;
406 }
407 opts.output_format = DIFF_FORMAT_NO_OUTPUT;
408 diff_queued_diff.nr = 0;
409 diff_flush(&opts);
6d297f81
JS
410 return renames;
411}
412
9fe0d87d
JH
413static int update_stages(const char *path, struct diff_filespec *o,
414 struct diff_filespec *a, struct diff_filespec *b,
415 int clear)
6d297f81
JS
416{
417 int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE;
3af244ca
JS
418 if (clear)
419 if (remove_file_from_cache(path))
6d297f81 420 return -1;
3af244ca
JS
421 if (o)
422 if (add_cacheinfo(o->mode, o->sha1, path, 1, 0, options))
6d297f81 423 return -1;
3af244ca
JS
424 if (a)
425 if (add_cacheinfo(a->mode, a->sha1, path, 2, 0, options))
6d297f81 426 return -1;
3af244ca
JS
427 if (b)
428 if (add_cacheinfo(b->mode, b->sha1, path, 3, 0, options))
6d297f81
JS
429 return -1;
430 return 0;
431}
432
6d297f81
JS
433static int remove_path(const char *name)
434{
3af244ca
JS
435 int ret, len;
436 char *slash, *dirs;
6d297f81
JS
437
438 ret = unlink(name);
3af244ca 439 if (ret)
6d297f81 440 return ret;
3af244ca 441 len = strlen(name);
2d7320d0 442 dirs = xmalloc(len+1);
6d297f81
JS
443 memcpy(dirs, name, len);
444 dirs[len] = '\0';
3af244ca 445 while ((slash = strrchr(name, '/'))) {
6d297f81
JS
446 *slash = '\0';
447 len = slash - name;
3af244ca 448 if (rmdir(name) != 0)
6d297f81
JS
449 break;
450 }
451 free(dirs);
452 return ret;
453}
454
65ac6e9c 455static int remove_file(int clean, const char *path, int no_wd)
6d297f81 456{
5a753613 457 int update_cache = index_only || clean;
65ac6e9c 458 int update_working_directory = !index_only && !no_wd;
6d297f81 459
5a753613 460 if (update_cache) {
6d297f81
JS
461 if (remove_file_from_cache(path))
462 return -1;
463 }
65ac6e9c 464 if (update_working_directory) {
6d297f81 465 unlink(path);
3af244ca 466 if (errno != ENOENT || errno != EISDIR)
6d297f81
JS
467 return -1;
468 remove_path(path);
469 }
470 return 0;
471}
472
473static char *unique_path(const char *path, const char *branch)
474{
475 char *newpath = xmalloc(strlen(path) + 1 + strlen(branch) + 8 + 1);
3af244ca
JS
476 int suffix = 0;
477 struct stat st;
f59aac47 478 char *p = newpath + strlen(path);
6d297f81 479 strcpy(newpath, path);
f59aac47 480 *(p++) = '~';
6d297f81 481 strcpy(p, branch);
3af244ca
JS
482 for (; *p; ++p)
483 if ('/' == *p)
6d297f81 484 *p = '_';
5a753613
JS
485 while (path_list_has_path(&current_file_set, newpath) ||
486 path_list_has_path(&current_directory_set, newpath) ||
3af244ca 487 lstat(newpath, &st) == 0)
6d297f81 488 sprintf(p, "_%d", suffix++);
3af244ca 489
5a753613 490 path_list_insert(newpath, &current_file_set);
6d297f81
JS
491 return newpath;
492}
493
3af244ca 494static int mkdir_p(const char *path, unsigned long mode)
6d297f81 495{
9befac47
SP
496 /* path points to cache entries, so xstrdup before messing with it */
497 char *buf = xstrdup(path);
3af244ca 498 int result = safe_create_leading_directories(buf);
6d297f81 499 free(buf);
3af244ca 500 return result;
6d297f81
JS
501}
502
503static void flush_buffer(int fd, const char *buf, unsigned long size)
504{
505 while (size > 0) {
93822c22 506 long ret = write_in_full(fd, buf, size);
6d297f81
JS
507 if (ret < 0) {
508 /* Ignore epipe */
509 if (errno == EPIPE)
510 break;
511 die("merge-recursive: %s", strerror(errno));
512 } else if (!ret) {
513 die("merge-recursive: disk full?");
514 }
515 size -= ret;
516 buf += ret;
517 }
518}
519
851c603e
LT
520static int make_room_for_path(const char *path)
521{
522 int status;
523 const char *msg = "failed to create path '%s'%s";
524
525 status = mkdir_p(path, 0777);
526 if (status) {
527 if (status == -3) {
528 /* something else exists */
529 error(msg, path, ": perhaps a D/F conflict?");
530 return -1;
531 }
532 die(msg, path, "");
533 }
534
535 /* Successful unlink is good.. */
536 if (!unlink(path))
537 return 0;
538 /* .. and so is no existing file */
539 if (errno == ENOENT)
540 return 0;
541 /* .. but not some other error (who really cares what?) */
542 return error(msg, path, ": perhaps a D/F conflict?");
543}
544
9fe0d87d
JH
545static void update_file_flags(const unsigned char *sha,
546 unsigned mode,
547 const char *path,
548 int update_cache,
549 int update_wd)
6d297f81 550{
3af244ca
JS
551 if (index_only)
552 update_wd = 0;
6d297f81 553
3af244ca 554 if (update_wd) {
21666f1a 555 enum object_type type;
6d297f81
JS
556 void *buf;
557 unsigned long size;
558
21666f1a 559 buf = read_sha1_file(sha, &type, &size);
6d297f81
JS
560 if (!buf)
561 die("cannot read object %s '%s'", sha1_to_hex(sha), path);
21666f1a 562 if (type != OBJ_BLOB)
6d297f81
JS
563 die("blob expected for %s '%s'", sha1_to_hex(sha), path);
564
851c603e
LT
565 if (make_room_for_path(path) < 0) {
566 update_wd = 0;
567 goto update_index;
568 }
723024d6 569 if (S_ISREG(mode) || (!has_symlinks && S_ISLNK(mode))) {
3af244ca 570 int fd;
3af244ca 571 if (mode & 0100)
6d297f81
JS
572 mode = 0777;
573 else
574 mode = 0666;
3af244ca
JS
575 fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);
576 if (fd < 0)
6d297f81
JS
577 die("failed to open %s: %s", path, strerror(errno));
578 flush_buffer(fd, buf, size);
579 close(fd);
3af244ca 580 } else if (S_ISLNK(mode)) {
2d7320d0 581 char *lnk = xmalloc(size + 1);
3af244ca
JS
582 memcpy(lnk, buf, size);
583 lnk[size] = '\0';
584 mkdir_p(path, 0777);
17cd29b2 585 unlink(path);
3af244ca 586 symlink(lnk, path);
723024d6 587 free(lnk);
6d297f81
JS
588 } else
589 die("do not know what to do with %06o %s '%s'",
590 mode, sha1_to_hex(sha), path);
591 }
4d50895a 592 update_index:
3af244ca
JS
593 if (update_cache)
594 add_cacheinfo(mode, sha, path, 0, update_wd, ADD_CACHE_OK_TO_ADD);
6d297f81
JS
595}
596
9fe0d87d
JH
597static void update_file(int clean,
598 const unsigned char *sha,
599 unsigned mode,
600 const char *path)
6d297f81
JS
601{
602 update_file_flags(sha, mode, path, index_only || clean, !index_only);
603}
604
605/* Low level file merging, update and removal */
606
607struct merge_file_info
608{
609 unsigned char sha[20];
610 unsigned mode;
611 unsigned clean:1,
612 merge:1;
613};
614
c2b4faea 615static void fill_mm(const unsigned char *sha1, mmfile_t *mm)
6d297f81 616{
6d297f81 617 unsigned long size;
21666f1a 618 enum object_type type;
6d297f81 619
f953831e
JS
620 if (!hashcmp(sha1, null_sha1)) {
621 mm->ptr = xstrdup("");
622 mm->size = 0;
623 return;
624 }
6d297f81 625
21666f1a
NP
626 mm->ptr = read_sha1_file(sha1, &type, &size);
627 if (!mm->ptr || type != OBJ_BLOB)
6d297f81 628 die("unable to read blob object %s", sha1_to_hex(sha1));
c2b4faea 629 mm->size = size;
6d297f81
JS
630}
631
153920da
JH
632/*
633 * Customizable low-level merge drivers support.
634 */
635
636struct ll_merge_driver;
637typedef int (*ll_merge_fn)(const struct ll_merge_driver *,
638 const char *path,
f3ef6b6b 639 mmfile_t *orig,
a129d96f
JH
640 mmfile_t *src1, const char *name1,
641 mmfile_t *src2, const char *name2,
642 mmbuffer_t *result);
643
153920da
JH
644struct ll_merge_driver {
645 const char *name;
646 const char *description;
647 ll_merge_fn fn;
3086486d 648 const char *recursive;
153920da
JH
649 struct ll_merge_driver *next;
650 char *cmdline;
651};
652
653/*
654 * Built-in low-levels
655 */
b798671f
JH
656static int ll_binary_merge(const struct ll_merge_driver *drv_unused,
657 const char *path_unused,
658 mmfile_t *orig,
659 mmfile_t *src1, const char *name1,
660 mmfile_t *src2, const char *name2,
661 mmbuffer_t *result)
662{
663 /*
664 * The tentative merge result is "ours" for the final round,
665 * or common ancestor for an internal merge. Still return
666 * "conflicted merge" status.
667 */
668 mmfile_t *stolen = index_only ? orig : src1;
669
670 result->ptr = stolen->ptr;
671 result->size = stolen->size;
672 stolen->ptr = NULL;
673 return 1;
674}
675
153920da
JH
676static int ll_xdl_merge(const struct ll_merge_driver *drv_unused,
677 const char *path_unused,
f3ef6b6b 678 mmfile_t *orig,
a129d96f
JH
679 mmfile_t *src1, const char *name1,
680 mmfile_t *src2, const char *name2,
681 mmbuffer_t *result)
682{
683 xpparam_t xpp;
684
9f30855d 685 if (buffer_is_binary(orig->ptr, orig->size) ||
b798671f
JH
686 buffer_is_binary(src1->ptr, src1->size) ||
687 buffer_is_binary(src2->ptr, src2->size)) {
688 warning("Cannot merge binary files: %s vs. %s\n",
9f30855d 689 name1, name2);
b798671f
JH
690 return ll_binary_merge(drv_unused, path_unused,
691 orig, src1, name1,
692 src2, name2,
693 result);
694 }
9f30855d 695
a129d96f
JH
696 memset(&xpp, 0, sizeof(xpp));
697 return xdl_merge(orig,
698 src1, name1,
699 src2, name2,
700 &xpp, XDL_MERGE_ZEALOUS,
701 result);
702}
703
153920da
JH
704static int ll_union_merge(const struct ll_merge_driver *drv_unused,
705 const char *path_unused,
f3ef6b6b 706 mmfile_t *orig,
a129d96f
JH
707 mmfile_t *src1, const char *name1,
708 mmfile_t *src2, const char *name2,
709 mmbuffer_t *result)
710{
711 char *src, *dst;
712 long size;
713 const int marker_size = 7;
714
153920da
JH
715 int status = ll_xdl_merge(drv_unused, path_unused,
716 orig, src1, NULL, src2, NULL, result);
a129d96f
JH
717 if (status <= 0)
718 return status;
719 size = result->size;
720 src = dst = result->ptr;
721 while (size) {
722 char ch;
723 if ((marker_size < size) &&
724 (*src == '<' || *src == '=' || *src == '>')) {
725 int i;
726 ch = *src;
727 for (i = 0; i < marker_size; i++)
728 if (src[i] != ch)
729 goto not_a_marker;
730 if (src[marker_size] != '\n')
731 goto not_a_marker;
732 src += marker_size + 1;
733 size -= marker_size + 1;
734 continue;
735 }
736 not_a_marker:
737 do {
738 ch = *src++;
739 *dst++ = ch;
740 size--;
741 } while (ch != '\n' && size);
742 }
743 result->size = dst - result->ptr;
744 return 0;
745}
746
153920da
JH
747#define LL_BINARY_MERGE 0
748#define LL_TEXT_MERGE 1
749#define LL_UNION_MERGE 2
750static struct ll_merge_driver ll_merge_drv[] = {
751 { "binary", "built-in binary merge", ll_binary_merge },
752 { "text", "built-in 3-way text merge", ll_xdl_merge },
753 { "union", "built-in union merge", ll_union_merge },
a129d96f
JH
754};
755
f3ef6b6b
JH
756static void create_temp(mmfile_t *src, char *path)
757{
758 int fd;
759
760 strcpy(path, ".merge_file_XXXXXX");
7647b17f 761 fd = xmkstemp(path);
f3ef6b6b
JH
762 if (write_in_full(fd, src->ptr, src->size) != src->size)
763 die("unable to write temp-file");
764 close(fd);
765}
766
153920da
JH
767/*
768 * User defined low-level merge driver support.
769 */
770static int ll_ext_merge(const struct ll_merge_driver *fn,
771 const char *path,
f3ef6b6b
JH
772 mmfile_t *orig,
773 mmfile_t *src1, const char *name1,
774 mmfile_t *src2, const char *name2,
775 mmbuffer_t *result)
776{
777 char temp[3][50];
778 char cmdbuf[2048];
779 struct interp table[] = {
780 { "%O" },
781 { "%A" },
782 { "%B" },
783 };
784 struct child_process child;
785 const char *args[20];
786 int status, fd, i;
787 struct stat st;
788
15ba3af2
JH
789 if (fn->cmdline == NULL)
790 die("custom merge driver %s lacks command line.", fn->name);
791
f3ef6b6b
JH
792 result->ptr = NULL;
793 result->size = 0;
794 create_temp(orig, temp[0]);
795 create_temp(src1, temp[1]);
796 create_temp(src2, temp[2]);
797
798 interp_set_entry(table, 0, temp[0]);
799 interp_set_entry(table, 1, temp[1]);
800 interp_set_entry(table, 2, temp[2]);
801
153920da
JH
802 output(1, "merging %s using %s", path,
803 fn->description ? fn->description : fn->name);
804
805 interpolate(cmdbuf, sizeof(cmdbuf), fn->cmdline, table, 3);
f3ef6b6b
JH
806
807 memset(&child, 0, sizeof(child));
808 child.argv = args;
809 args[0] = "sh";
810 args[1] = "-c";
811 args[2] = cmdbuf;
812 args[3] = NULL;
813
814 status = run_command(&child);
815 if (status < -ERR_RUN_COMMAND_FORK)
816 ; /* failure in run-command */
817 else
818 status = -status;
819 fd = open(temp[1], O_RDONLY);
820 if (fd < 0)
821 goto bad;
822 if (fstat(fd, &st))
823 goto close_bad;
824 result->size = st.st_size;
825 result->ptr = xmalloc(result->size + 1);
826 if (read_in_full(fd, result->ptr, result->size) != result->size) {
827 free(result->ptr);
828 result->ptr = NULL;
829 result->size = 0;
830 }
831 close_bad:
832 close(fd);
833 bad:
834 for (i = 0; i < 3; i++)
835 unlink(temp[i]);
836 return status;
837}
838
839/*
840 * merge.default and merge.driver configuration items
841 */
153920da 842static struct ll_merge_driver *ll_user_merge, **ll_user_merge_tail;
be89cb23 843static const char *default_ll_merge;
f3ef6b6b
JH
844
845static int read_merge_config(const char *var, const char *value)
846{
153920da
JH
847 struct ll_merge_driver *fn;
848 const char *ep, *name;
849 int namelen;
f3ef6b6b 850
be89cb23 851 if (!strcmp(var, "merge.default")) {
153920da
JH
852 if (value)
853 default_ll_merge = strdup(value);
be89cb23
JH
854 return 0;
855 }
856
153920da
JH
857 /*
858 * We are not interested in anything but "merge.<name>.variable";
859 * especially, we do not want to look at variables such as
860 * "merge.summary", "merge.tool", and "merge.verbosity".
861 */
15ba3af2 862 if (prefixcmp(var, "merge.") || (ep = strrchr(var, '.')) == var + 5)
f3ef6b6b 863 return 0;
153920da 864
f3ef6b6b 865 /*
153920da
JH
866 * Find existing one as we might be processing merge.<name>.var2
867 * after seeing merge.<name>.var1.
f3ef6b6b 868 */
153920da
JH
869 name = var + 6;
870 namelen = ep - name;
871 for (fn = ll_user_merge; fn; fn = fn->next)
872 if (!strncmp(fn->name, name, namelen) && !fn->name[namelen])
873 break;
874 if (!fn) {
875 char *namebuf;
876 fn = xcalloc(1, sizeof(struct ll_merge_driver));
877 namebuf = xmalloc(namelen + 1);
878 memcpy(namebuf, name, namelen);
879 namebuf[namelen] = 0;
880 fn->name = namebuf;
881 fn->fn = ll_ext_merge;
e87b1c94 882 fn->next = NULL;
153920da 883 *ll_user_merge_tail = fn;
e87b1c94 884 ll_user_merge_tail = &(fn->next);
153920da
JH
885 }
886
887 ep++;
888
889 if (!strcmp("name", ep)) {
890 if (!value)
891 return error("%s: lacks value", var);
892 fn->description = strdup(value);
893 return 0;
894 }
895
896 if (!strcmp("driver", ep)) {
897 if (!value)
898 return error("%s: lacks value", var);
899 /*
900 * merge.<name>.driver specifies the command line:
901 *
902 * command-line
903 *
904 * The command-line will be interpolated with the following
905 * tokens and is given to the shell:
906 *
907 * %O - temporary file name for the merge base.
908 * %A - temporary file name for our version.
909 * %B - temporary file name for the other branches' version.
910 *
911 * The external merge driver should write the results in the
912 * file named by %A, and signal that it has done with zero exit
913 * status.
914 */
915 fn->cmdline = strdup(value);
916 return 0;
917 }
918
3086486d
JH
919 if (!strcmp("recursive", ep)) {
920 if (!value)
921 return error("%s: lacks value", var);
922 fn->recursive = strdup(value);
923 return 0;
924 }
925
f3ef6b6b
JH
926 return 0;
927}
928
929static void initialize_ll_merge(void)
a129d96f 930{
153920da 931 if (ll_user_merge_tail)
f3ef6b6b 932 return;
153920da 933 ll_user_merge_tail = &ll_user_merge;
f3ef6b6b
JH
934 git_config(read_merge_config);
935}
936
a5e92abd 937static const struct ll_merge_driver *find_ll_merge_driver(const char *merge_attr)
f3ef6b6b 938{
153920da 939 struct ll_merge_driver *fn;
a129d96f
JH
940 const char *name;
941 int i;
942
f3ef6b6b
JH
943 initialize_ll_merge();
944
945 if (ATTR_TRUE(merge_attr))
153920da 946 return &ll_merge_drv[LL_TEXT_MERGE];
a129d96f 947 else if (ATTR_FALSE(merge_attr))
153920da 948 return &ll_merge_drv[LL_BINARY_MERGE];
be89cb23
JH
949 else if (ATTR_UNSET(merge_attr)) {
950 if (!default_ll_merge)
153920da 951 return &ll_merge_drv[LL_TEXT_MERGE];
be89cb23
JH
952 else
953 name = default_ll_merge;
954 }
f3ef6b6b
JH
955 else
956 name = merge_attr;
957
153920da
JH
958 for (fn = ll_user_merge; fn; fn = fn->next)
959 if (!strcmp(fn->name, name))
960 return fn;
a129d96f 961
153920da
JH
962 for (i = 0; i < ARRAY_SIZE(ll_merge_drv); i++)
963 if (!strcmp(ll_merge_drv[i].name, name))
964 return &ll_merge_drv[i];
a129d96f
JH
965
966 /* default to the 3-way */
153920da 967 return &ll_merge_drv[LL_TEXT_MERGE];
a129d96f
JH
968}
969
a5e92abd 970static const char *git_path_check_merge(const char *path)
a129d96f
JH
971{
972 static struct git_attr_check attr_merge_check;
973
974 if (!attr_merge_check.attr)
975 attr_merge_check.attr = git_attr("merge", 5);
976
977 if (git_checkattr(path, 1, &attr_merge_check))
a5e92abd 978 return NULL;
a129d96f
JH
979 return attr_merge_check.value;
980}
981
3e5261a2
JH
982static int ll_merge(mmbuffer_t *result_buf,
983 struct diff_filespec *o,
984 struct diff_filespec *a,
985 struct diff_filespec *b,
986 const char *branch1,
987 const char *branch2)
988{
989 mmfile_t orig, src1, src2;
3e5261a2
JH
990 char *name1, *name2;
991 int merge_status;
a5e92abd 992 const char *ll_driver_name;
153920da 993 const struct ll_merge_driver *driver;
3e5261a2
JH
994
995 name1 = xstrdup(mkpath("%s:%s", branch1, a->path));
996 name2 = xstrdup(mkpath("%s:%s", branch2, b->path));
997
998 fill_mm(o->sha1, &orig);
999 fill_mm(a->sha1, &src1);
1000 fill_mm(b->sha1, &src2);
1001
a5e92abd
JH
1002 ll_driver_name = git_path_check_merge(a->path);
1003 driver = find_ll_merge_driver(ll_driver_name);
a129d96f 1004
d56dbd67
JH
1005 if (index_only && driver->recursive)
1006 driver = find_ll_merge_driver(driver->recursive);
153920da
JH
1007 merge_status = driver->fn(driver, a->path,
1008 &orig, &src1, name1, &src2, name2,
1009 result_buf);
a129d96f 1010
3e5261a2
JH
1011 free(name1);
1012 free(name2);
1013 free(orig.ptr);
1014 free(src1.ptr);
1015 free(src2.ptr);
1016 return merge_status;
1017}
1018
3af244ca
JS
1019static struct merge_file_info merge_file(struct diff_filespec *o,
1020 struct diff_filespec *a, struct diff_filespec *b,
c1d20846 1021 const char *branch1, const char *branch2)
6d297f81
JS
1022{
1023 struct merge_file_info result;
1024 result.merge = 0;
1025 result.clean = 1;
1026
3af244ca 1027 if ((S_IFMT & a->mode) != (S_IFMT & b->mode)) {
6d297f81 1028 result.clean = 0;
3af244ca
JS
1029 if (S_ISREG(a->mode)) {
1030 result.mode = a->mode;
8da71493 1031 hashcpy(result.sha, a->sha1);
6d297f81 1032 } else {
3af244ca 1033 result.mode = b->mode;
8da71493 1034 hashcpy(result.sha, b->sha1);
6d297f81
JS
1035 }
1036 } else {
3af244ca 1037 if (!sha_eq(a->sha1, o->sha1) && !sha_eq(b->sha1, o->sha1))
6d297f81
JS
1038 result.merge = 1;
1039
3af244ca 1040 result.mode = a->mode == o->mode ? b->mode: a->mode;
6d297f81 1041
3af244ca 1042 if (sha_eq(a->sha1, o->sha1))
8da71493 1043 hashcpy(result.sha, b->sha1);
3af244ca 1044 else if (sha_eq(b->sha1, o->sha1))
8da71493 1045 hashcpy(result.sha, a->sha1);
3af244ca 1046 else if (S_ISREG(a->mode)) {
c2b4faea 1047 mmbuffer_t result_buf;
c2b4faea
JH
1048 int merge_status;
1049
3e5261a2
JH
1050 merge_status = ll_merge(&result_buf, o, a, b,
1051 branch1, branch2);
c2b4faea
JH
1052
1053 if ((merge_status < 0) || !result_buf.ptr)
1054 die("Failed to execute internal merge");
1055
1056 if (write_sha1_file(result_buf.ptr, result_buf.size,
1057 blob_type, result.sha))
1058 die("Unable to add %s to database",
1059 a->path);
1060
1061 free(result_buf.ptr);
1062 result.clean = (merge_status == 0);
6d297f81 1063 } else {
3af244ca 1064 if (!(S_ISLNK(a->mode) || S_ISLNK(b->mode)))
6d297f81
JS
1065 die("cannot merge modes?");
1066
8da71493 1067 hashcpy(result.sha, a->sha1);
6d297f81 1068
3af244ca 1069 if (!sha_eq(a->sha1, b->sha1))
6d297f81
JS
1070 result.clean = 0;
1071 }
1072 }
1073
1074 return result;
1075}
1076
1077static void conflict_rename_rename(struct rename *ren1,
1078 const char *branch1,
1079 struct rename *ren2,
1080 const char *branch2)
1081{
1082 char *del[2];
1083 int delp = 0;
1084 const char *ren1_dst = ren1->pair->two->path;
1085 const char *ren2_dst = ren2->pair->two->path;
5a753613
JS
1086 const char *dst_name1 = ren1_dst;
1087 const char *dst_name2 = ren2_dst;
1088 if (path_list_has_path(&current_directory_set, ren1_dst)) {
1089 dst_name1 = del[delp++] = unique_path(ren1_dst, branch1);
89f40be2 1090 output(1, "%s is a directory in %s added as %s instead",
5a753613 1091 ren1_dst, branch2, dst_name1);
65ac6e9c 1092 remove_file(0, ren1_dst, 0);
6d297f81 1093 }
5a753613
JS
1094 if (path_list_has_path(&current_directory_set, ren2_dst)) {
1095 dst_name2 = del[delp++] = unique_path(ren2_dst, branch2);
89f40be2 1096 output(1, "%s is a directory in %s added as %s instead",
5a753613 1097 ren2_dst, branch1, dst_name2);
65ac6e9c 1098 remove_file(0, ren2_dst, 0);
6d297f81 1099 }
a97e4075
AR
1100 if (index_only) {
1101 remove_file_from_cache(dst_name1);
1102 remove_file_from_cache(dst_name2);
1103 /*
1104 * Uncomment to leave the conflicting names in the resulting tree
1105 *
1106 * update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, dst_name1);
1107 * update_file(0, ren2->pair->two->sha1, ren2->pair->two->mode, dst_name2);
1108 */
1109 } else {
1110 update_stages(dst_name1, NULL, ren1->pair->two, NULL, 1);
1111 update_stages(dst_name2, NULL, NULL, ren2->pair->two, 1);
1112 }
3af244ca 1113 while (delp--)
6d297f81
JS
1114 free(del[delp]);
1115}
1116
1117static void conflict_rename_dir(struct rename *ren1,
1118 const char *branch1)
1119{
5a753613 1120 char *new_path = unique_path(ren1->pair->two->path, branch1);
89f40be2 1121 output(1, "Renamed %s to %s instead", ren1->pair->one->path, new_path);
65ac6e9c 1122 remove_file(0, ren1->pair->two->path, 0);
5a753613
JS
1123 update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path);
1124 free(new_path);
6d297f81
JS
1125}
1126
1127static void conflict_rename_rename_2(struct rename *ren1,
1128 const char *branch1,
1129 struct rename *ren2,
1130 const char *branch2)
1131{
5a753613
JS
1132 char *new_path1 = unique_path(ren1->pair->two->path, branch1);
1133 char *new_path2 = unique_path(ren2->pair->two->path, branch2);
89f40be2 1134 output(1, "Renamed %s to %s and %s to %s instead",
5a753613
JS
1135 ren1->pair->one->path, new_path1,
1136 ren2->pair->one->path, new_path2);
65ac6e9c 1137 remove_file(0, ren1->pair->two->path, 0);
5a753613
JS
1138 update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path1);
1139 update_file(0, ren2->pair->two->sha1, ren2->pair->two->mode, new_path2);
1140 free(new_path2);
1141 free(new_path1);
6d297f81
JS
1142}
1143
5a753613
JS
1144static int process_renames(struct path_list *a_renames,
1145 struct path_list *b_renames,
1146 const char *a_branch,
1147 const char *b_branch)
6d297f81 1148{
5a753613
JS
1149 int clean_merge = 1, i, j;
1150 struct path_list a_by_dst = {NULL, 0, 0, 0}, b_by_dst = {NULL, 0, 0, 0};
6d297f81
JS
1151 const struct rename *sre;
1152
5a753613
JS
1153 for (i = 0; i < a_renames->nr; i++) {
1154 sre = a_renames->items[i].util;
1155 path_list_insert(sre->pair->two->path, &a_by_dst)->util
6d297f81
JS
1156 = sre->dst_entry;
1157 }
5a753613
JS
1158 for (i = 0; i < b_renames->nr; i++) {
1159 sre = b_renames->items[i].util;
1160 path_list_insert(sre->pair->two->path, &b_by_dst)->util
6d297f81
JS
1161 = sre->dst_entry;
1162 }
1163
5a753613 1164 for (i = 0, j = 0; i < a_renames->nr || j < b_renames->nr;) {
3af244ca
JS
1165 int compare;
1166 char *src;
6d297f81 1167 struct path_list *renames1, *renames2, *renames2Dst;
3af244ca 1168 struct rename *ren1 = NULL, *ren2 = NULL;
5a753613 1169 const char *branch1, *branch2;
3af244ca
JS
1170 const char *ren1_src, *ren1_dst;
1171
5a753613 1172 if (i >= a_renames->nr) {
3af244ca 1173 compare = 1;
5a753613
JS
1174 ren2 = b_renames->items[j++].util;
1175 } else if (j >= b_renames->nr) {
3af244ca 1176 compare = -1;
5a753613 1177 ren1 = a_renames->items[i++].util;
3af244ca 1178 } else {
5a753613
JS
1179 compare = strcmp(a_renames->items[i].path,
1180 b_renames->items[j].path);
3d234d0a
JS
1181 if (compare <= 0)
1182 ren1 = a_renames->items[i++].util;
1183 if (compare >= 0)
1184 ren2 = b_renames->items[j++].util;
3af244ca
JS
1185 }
1186
6d297f81 1187 /* TODO: refactor, so that 1/2 are not needed */
3af244ca 1188 if (ren1) {
5a753613
JS
1189 renames1 = a_renames;
1190 renames2 = b_renames;
1191 renames2Dst = &b_by_dst;
1192 branch1 = a_branch;
1193 branch2 = b_branch;
6d297f81 1194 } else {
3af244ca 1195 struct rename *tmp;
5a753613
JS
1196 renames1 = b_renames;
1197 renames2 = a_renames;
1198 renames2Dst = &a_by_dst;
1199 branch1 = b_branch;
1200 branch2 = a_branch;
3af244ca 1201 tmp = ren2;
6d297f81
JS
1202 ren2 = ren1;
1203 ren1 = tmp;
1204 }
3af244ca 1205 src = ren1->pair->one->path;
6d297f81
JS
1206
1207 ren1->dst_entry->processed = 1;
1208 ren1->src_entry->processed = 1;
1209
3af244ca 1210 if (ren1->processed)
6d297f81
JS
1211 continue;
1212 ren1->processed = 1;
1213
3af244ca
JS
1214 ren1_src = ren1->pair->one->path;
1215 ren1_dst = ren1->pair->two->path;
6d297f81 1216
3af244ca 1217 if (ren2) {
6d297f81
JS
1218 const char *ren2_src = ren2->pair->one->path;
1219 const char *ren2_dst = ren2->pair->two->path;
1220 /* Renamed in 1 and renamed in 2 */
1221 if (strcmp(ren1_src, ren2_src) != 0)
1222 die("ren1.src != ren2.src");
1223 ren2->dst_entry->processed = 1;
1224 ren2->processed = 1;
1225 if (strcmp(ren1_dst, ren2_dst) != 0) {
5a753613 1226 clean_merge = 0;
8c3275ab 1227 output(1, "CONFLICT (rename/rename): "
a97e4075
AR
1228 "Rename \"%s\"->\"%s\" in branch \"%s\" "
1229 "rename \"%s\"->\"%s\" in \"%s\"%s",
5a753613 1230 src, ren1_dst, branch1,
a97e4075
AR
1231 src, ren2_dst, branch2,
1232 index_only ? " (left unresolved)": "");
1233 if (index_only) {
1234 remove_file_from_cache(src);
1235 update_file(0, ren1->pair->one->sha1,
1236 ren1->pair->one->mode, src);
1237 }
5a753613 1238 conflict_rename_rename(ren1, branch1, ren2, branch2);
6d297f81 1239 } else {
6d297f81 1240 struct merge_file_info mfi;
65ac6e9c 1241 remove_file(1, ren1_src, 1);
3af244ca
JS
1242 mfi = merge_file(ren1->pair->one,
1243 ren1->pair->two,
1244 ren2->pair->two,
5a753613
JS
1245 branch1,
1246 branch2);
3af244ca 1247 if (mfi.merge || !mfi.clean)
89f40be2 1248 output(1, "Renamed %s->%s", src, ren1_dst);
6d297f81 1249
3af244ca 1250 if (mfi.merge)
89f40be2 1251 output(2, "Auto-merged %s", ren1_dst);
6d297f81 1252
3af244ca 1253 if (!mfi.clean) {
8c3275ab 1254 output(1, "CONFLICT (content): merge conflict in %s",
6d297f81 1255 ren1_dst);
5a753613 1256 clean_merge = 0;
6d297f81 1257
3af244ca 1258 if (!index_only)
6d297f81 1259 update_stages(ren1_dst,
3af244ca
JS
1260 ren1->pair->one,
1261 ren1->pair->two,
1262 ren2->pair->two,
6d297f81
JS
1263 1 /* clear */);
1264 }
1265 update_file(mfi.clean, mfi.sha, mfi.mode, ren1_dst);
1266 }
1267 } else {
1268 /* Renamed in 1, maybe changed in 2 */
3af244ca
JS
1269 struct path_list_item *item;
1270 /* we only use sha1 and mode of these */
1271 struct diff_filespec src_other, dst_other;
5a753613 1272 int try_merge, stage = a_renames == renames1 ? 3: 2;
6d297f81 1273
183d7972 1274 remove_file(1, ren1_src, index_only || stage == 3);
6d297f81 1275
87cb004e 1276 hashcpy(src_other.sha1, ren1->src_entry->stages[stage].sha);
3af244ca 1277 src_other.mode = ren1->src_entry->stages[stage].mode;
87cb004e 1278 hashcpy(dst_other.sha1, ren1->dst_entry->stages[stage].sha);
3af244ca 1279 dst_other.mode = ren1->dst_entry->stages[stage].mode;
6d297f81 1280
5a753613 1281 try_merge = 0;
6d297f81 1282
5a753613
JS
1283 if (path_list_has_path(&current_directory_set, ren1_dst)) {
1284 clean_merge = 0;
89f40be2 1285 output(1, "CONFLICT (rename/directory): Renamed %s->%s in %s "
6d297f81 1286 " directory %s added in %s",
5a753613
JS
1287 ren1_src, ren1_dst, branch1,
1288 ren1_dst, branch2);
1289 conflict_rename_dir(ren1, branch1);
3af244ca 1290 } else if (sha_eq(src_other.sha1, null_sha1)) {
5a753613 1291 clean_merge = 0;
89f40be2 1292 output(1, "CONFLICT (rename/delete): Renamed %s->%s in %s "
6d297f81 1293 "and deleted in %s",
5a753613
JS
1294 ren1_src, ren1_dst, branch1,
1295 branch2);
6d297f81 1296 update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, ren1_dst);
3af244ca 1297 } else if (!sha_eq(dst_other.sha1, null_sha1)) {
5a753613
JS
1298 const char *new_path;
1299 clean_merge = 0;
1300 try_merge = 1;
89f40be2 1301 output(1, "CONFLICT (rename/add): Renamed %s->%s in %s. "
6d297f81 1302 "%s added in %s",
5a753613
JS
1303 ren1_src, ren1_dst, branch1,
1304 ren1_dst, branch2);
1305 new_path = unique_path(ren1_dst, branch2);
89f40be2 1306 output(1, "Added as %s instead", new_path);
5a753613 1307 update_file(0, dst_other.sha1, dst_other.mode, new_path);
3af244ca
JS
1308 } else if ((item = path_list_lookup(ren1_dst, renames2Dst))) {
1309 ren2 = item->util;
5a753613 1310 clean_merge = 0;
6d297f81 1311 ren2->processed = 1;
89f40be2
SP
1312 output(1, "CONFLICT (rename/rename): Renamed %s->%s in %s. "
1313 "Renamed %s->%s in %s",
5a753613
JS
1314 ren1_src, ren1_dst, branch1,
1315 ren2->pair->one->path, ren2->pair->two->path, branch2);
1316 conflict_rename_rename_2(ren1, branch1, ren2, branch2);
6d297f81 1317 } else
5a753613 1318 try_merge = 1;
6d297f81 1319
5a753613 1320 if (try_merge) {
3af244ca 1321 struct diff_filespec *o, *a, *b;
6d297f81 1322 struct merge_file_info mfi;
3af244ca
JS
1323 src_other.path = (char *)ren1_src;
1324
1325 o = ren1->pair->one;
5a753613 1326 if (a_renames == renames1) {
3af244ca
JS
1327 a = ren1->pair->two;
1328 b = &src_other;
1329 } else {
1330 b = ren1->pair->two;
1331 a = &src_other;
1332 }
1333 mfi = merge_file(o, a, b,
5a753613 1334 a_branch, b_branch);
6d297f81 1335
0d5e6c97 1336 if (mfi.clean &&
c135ee88
AR
1337 sha_eq(mfi.sha, ren1->pair->two->sha1) &&
1338 mfi.mode == ren1->pair->two->mode)
8a359819
AR
1339 /*
1340 * This messaged is part of
1341 * t6022 test. If you change
1342 * it update the test too.
1343 */
c135ee88
AR
1344 output(3, "Skipped %s (merged same as existing)", ren1_dst);
1345 else {
1346 if (mfi.merge || !mfi.clean)
1347 output(1, "Renamed %s => %s", ren1_src, ren1_dst);
1348 if (mfi.merge)
1349 output(2, "Auto-merged %s", ren1_dst);
1350 if (!mfi.clean) {
1351 output(1, "CONFLICT (rename/modify): Merge conflict in %s",
1352 ren1_dst);
1353 clean_merge = 0;
1354
1355 if (!index_only)
1356 update_stages(ren1_dst,
1357 o, a, b, 1);
1358 }
1359 update_file(mfi.clean, mfi.sha, mfi.mode, ren1_dst);
6d297f81 1360 }
6d297f81
JS
1361 }
1362 }
1363 }
5a753613
JS
1364 path_list_clear(&a_by_dst, 0);
1365 path_list_clear(&b_by_dst, 0);
6d297f81 1366
5a753613 1367 return clean_merge;
6d297f81
JS
1368}
1369
ac7f0f43 1370static unsigned char *stage_sha(const unsigned char *sha, unsigned mode)
6d297f81 1371{
ac7f0f43 1372 return (is_null_sha1(sha) || mode == 0) ? NULL: (unsigned char *)sha;
6d297f81
JS
1373}
1374
1375/* Per entry merge function */
1376static int process_entry(const char *path, struct stage_data *entry,
c1d20846
JS
1377 const char *branch1,
1378 const char *branch2)
6d297f81
JS
1379{
1380 /*
1381 printf("processing entry, clean cache: %s\n", index_only ? "yes": "no");
1382 print_index_entry("\tpath: ", entry);
1383 */
5a753613 1384 int clean_merge = 1;
5a753613
JS
1385 unsigned o_mode = entry->stages[1].mode;
1386 unsigned a_mode = entry->stages[2].mode;
1387 unsigned b_mode = entry->stages[3].mode;
ac7f0f43
JH
1388 unsigned char *o_sha = stage_sha(entry->stages[1].sha, o_mode);
1389 unsigned char *a_sha = stage_sha(entry->stages[2].sha, a_mode);
1390 unsigned char *b_sha = stage_sha(entry->stages[3].sha, b_mode);
5a753613
JS
1391
1392 if (o_sha && (!a_sha || !b_sha)) {
6d297f81 1393 /* Case A: Deleted in one */
5a753613
JS
1394 if ((!a_sha && !b_sha) ||
1395 (sha_eq(a_sha, o_sha) && !b_sha) ||
1396 (!a_sha && sha_eq(b_sha, o_sha))) {
6d297f81
JS
1397 /* Deleted in both or deleted in one and
1398 * unchanged in the other */
5a753613 1399 if (a_sha)
89f40be2 1400 output(2, "Removed %s", path);
65ac6e9c
JH
1401 /* do not touch working file if it did not exist */
1402 remove_file(1, path, !a_sha);
6d297f81
JS
1403 } else {
1404 /* Deleted in one and changed in the other */
5a753613
JS
1405 clean_merge = 0;
1406 if (!a_sha) {
8c3275ab 1407 output(1, "CONFLICT (delete/modify): %s deleted in %s "
6d297f81 1408 "and modified in %s. Version %s of %s left in tree.",
c1d20846
JS
1409 path, branch1,
1410 branch2, branch2, path);
5a753613 1411 update_file(0, b_sha, b_mode, path);
6d297f81 1412 } else {
8c3275ab 1413 output(1, "CONFLICT (delete/modify): %s deleted in %s "
6d297f81 1414 "and modified in %s. Version %s of %s left in tree.",
c1d20846
JS
1415 path, branch2,
1416 branch1, branch1, path);
5a753613 1417 update_file(0, a_sha, a_mode, path);
6d297f81
JS
1418 }
1419 }
1420
5a753613
JS
1421 } else if ((!o_sha && a_sha && !b_sha) ||
1422 (!o_sha && !a_sha && b_sha)) {
6d297f81 1423 /* Case B: Added in one. */
5a753613
JS
1424 const char *add_branch;
1425 const char *other_branch;
6d297f81
JS
1426 unsigned mode;
1427 const unsigned char *sha;
1428 const char *conf;
1429
5a753613 1430 if (a_sha) {
c1d20846
JS
1431 add_branch = branch1;
1432 other_branch = branch2;
5a753613
JS
1433 mode = a_mode;
1434 sha = a_sha;
6d297f81
JS
1435 conf = "file/directory";
1436 } else {
c1d20846
JS
1437 add_branch = branch2;
1438 other_branch = branch1;
5a753613
JS
1439 mode = b_mode;
1440 sha = b_sha;
6d297f81
JS
1441 conf = "directory/file";
1442 }
5a753613
JS
1443 if (path_list_has_path(&current_directory_set, path)) {
1444 const char *new_path = unique_path(path, add_branch);
1445 clean_merge = 0;
8c3275ab 1446 output(1, "CONFLICT (%s): There is a directory with name %s in %s. "
89f40be2 1447 "Added %s as %s",
5a753613 1448 conf, path, other_branch, path, new_path);
65ac6e9c 1449 remove_file(0, path, 0);
5a753613 1450 update_file(0, sha, mode, new_path);
6d297f81 1451 } else {
89f40be2 1452 output(2, "Added %s", path);
6d297f81
JS
1453 update_file(1, sha, mode, path);
1454 }
f953831e
JS
1455 } else if (a_sha && b_sha) {
1456 /* Case C: Added in both (check for same permissions) and */
6d297f81 1457 /* case D: Modified in both, but differently. */
f953831e 1458 const char *reason = "content";
6d297f81 1459 struct merge_file_info mfi;
3af244ca
JS
1460 struct diff_filespec o, a, b;
1461
f953831e
JS
1462 if (!o_sha) {
1463 reason = "add/add";
1464 o_sha = (unsigned char *)null_sha1;
1465 }
89f40be2 1466 output(2, "Auto-merged %s", path);
3af244ca 1467 o.path = a.path = b.path = (char *)path;
8da71493 1468 hashcpy(o.sha1, o_sha);
5a753613 1469 o.mode = o_mode;
8da71493 1470 hashcpy(a.sha1, a_sha);
5a753613 1471 a.mode = a_mode;
8da71493 1472 hashcpy(b.sha1, b_sha);
5a753613 1473 b.mode = b_mode;
3af244ca
JS
1474
1475 mfi = merge_file(&o, &a, &b,
c1d20846 1476 branch1, branch2);
6d297f81 1477
3af244ca 1478 if (mfi.clean)
6d297f81
JS
1479 update_file(1, mfi.sha, mfi.mode, path);
1480 else {
5a753613 1481 clean_merge = 0;
8c3275ab 1482 output(1, "CONFLICT (%s): Merge conflict in %s",
f953831e 1483 reason, path);
6d297f81 1484
3af244ca 1485 if (index_only)
6d297f81
JS
1486 update_file(0, mfi.sha, mfi.mode, path);
1487 else
1488 update_file_flags(mfi.sha, mfi.mode, path,
5a753613 1489 0 /* update_cache */, 1 /* update_working_directory */);
6d297f81 1490 }
ac7f0f43
JH
1491 } else if (!o_sha && !a_sha && !b_sha) {
1492 /*
1493 * this entry was deleted altogether. a_mode == 0 means
1494 * we had that path and want to actively remove it.
1495 */
1496 remove_file(1, path, !a_mode);
6d297f81
JS
1497 } else
1498 die("Fatal merge failure, shouldn't happen.");
1499
5a753613 1500 return clean_merge;
6d297f81
JS
1501}
1502
3af244ca
JS
1503static int merge_trees(struct tree *head,
1504 struct tree *merge,
1505 struct tree *common,
c1d20846
JS
1506 const char *branch1,
1507 const char *branch2,
3af244ca 1508 struct tree **result)
6d297f81 1509{
3af244ca 1510 int code, clean;
68faf689
JH
1511
1512 if (subtree_merge) {
1513 merge = shift_tree_object(head, merge);
1514 common = shift_tree_object(head, common);
1515 }
1516
3af244ca 1517 if (sha_eq(common->object.sha1, merge->object.sha1)) {
8c3275ab 1518 output(0, "Already uptodate!");
3af244ca
JS
1519 *result = head;
1520 return 1;
6d297f81
JS
1521 }
1522
7a85b848 1523 code = git_merge_trees(index_only, common, head, merge);
6d297f81 1524
3af244ca 1525 if (code != 0)
6d297f81
JS
1526 die("merging of trees %s and %s failed",
1527 sha1_to_hex(head->object.sha1),
1528 sha1_to_hex(merge->object.sha1));
1529
8b944b56 1530 if (unmerged_index()) {
3af244ca
JS
1531 struct path_list *entries, *re_head, *re_merge;
1532 int i;
5a753613
JS
1533 path_list_clear(&current_file_set, 1);
1534 path_list_clear(&current_directory_set, 1);
3af244ca
JS
1535 get_files_dirs(head);
1536 get_files_dirs(merge);
6d297f81 1537
3af244ca 1538 entries = get_unmerged();
6d297f81
JS
1539 re_head = get_renames(head, common, head, merge, entries);
1540 re_merge = get_renames(merge, common, head, merge, entries);
3af244ca 1541 clean = process_renames(re_head, re_merge,
c1d20846 1542 branch1, branch2);
ad57cbca 1543 for (i = 0; i < entries->nr; i++) {
6d297f81
JS
1544 const char *path = entries->items[i].path;
1545 struct stage_data *e = entries->items[i].util;
3f6ee2d1
SP
1546 if (!e->processed
1547 && !process_entry(path, e, branch1, branch2))
3af244ca 1548 clean = 0;
6d297f81
JS
1549 }
1550
3af244ca
JS
1551 path_list_clear(re_merge, 0);
1552 path_list_clear(re_head, 0);
1553 path_list_clear(entries, 1);
6d297f81 1554
6d297f81 1555 }
1cf716a2
JH
1556 else
1557 clean = 1;
1558
8b944b56
JH
1559 if (index_only)
1560 *result = git_write_tree();
6d297f81 1561
3af244ca 1562 return clean;
6d297f81
JS
1563}
1564
8918b0c9
JS
1565static struct commit_list *reverse_commit_list(struct commit_list *list)
1566{
1567 struct commit_list *next = NULL, *current, *backup;
1568 for (current = list; current; current = backup) {
1569 backup = current->next;
1570 current->next = next;
1571 next = current;
1572 }
1573 return next;
1574}
1575
6d297f81
JS
1576/*
1577 * Merge the commits h1 and h2, return the resulting virtual
3dff5379 1578 * commit object and a flag indicating the cleanness of the merge.
6d297f81 1579 */
65ac6e9c
JH
1580static int merge(struct commit *h1,
1581 struct commit *h2,
1582 const char *branch1,
1583 const char *branch2,
8b944b56 1584 struct commit_list *ca,
65ac6e9c 1585 struct commit **result)
6d297f81 1586{
8b944b56 1587 struct commit_list *iter;
5a753613 1588 struct commit *merged_common_ancestors;
3af244ca
JS
1589 struct tree *mrtree;
1590 int clean;
6d297f81 1591
8c3275ab
SP
1592 if (show(4)) {
1593 output(4, "Merging:");
1594 output_commit_title(h1);
1595 output_commit_title(h2);
1596 }
6d297f81 1597
8b944b56
JH
1598 if (!ca) {
1599 ca = get_merge_bases(h1, h2, 1);
1600 ca = reverse_commit_list(ca);
1601 }
6d297f81 1602
8c3275ab
SP
1603 if (show(5)) {
1604 output(5, "found %u common ancestor(s):", commit_list_count(ca));
1605 for (iter = ca; iter; iter = iter->next)
1606 output_commit_title(iter->item);
1607 }
6d297f81 1608
5a753613 1609 merged_common_ancestors = pop_commit(&ca);
934d9a24
JS
1610 if (merged_common_ancestors == NULL) {
1611 /* if there is no common ancestor, make an empty tree */
1612 struct tree *tree = xcalloc(1, sizeof(struct tree));
934d9a24
JS
1613
1614 tree->object.parsed = 1;
1615 tree->object.type = OBJ_TREE;
21666f1a 1616 pretend_sha1_file(NULL, 0, OBJ_TREE, tree->object.sha1);
934d9a24
JS
1617 merged_common_ancestors = make_virtual_commit(tree, "ancestor");
1618 }
6d297f81 1619
6d297f81 1620 for (iter = ca; iter; iter = iter->next) {
63889639 1621 call_depth++;
3af244ca
JS
1622 /*
1623 * When the merge fails, the result contains files
1624 * with conflict markers. The cleanness flag is
3dff5379
PR
1625 * ignored, it was never actually used, as result of
1626 * merge_trees has always overwritten it: the committed
3af244ca
JS
1627 * "conflicts" were already resolved.
1628 */
8b944b56 1629 discard_cache();
5a753613 1630 merge(merged_common_ancestors, iter->item,
3af244ca
JS
1631 "Temporary merge branch 1",
1632 "Temporary merge branch 2",
3af244ca 1633 NULL,
5a753613 1634 &merged_common_ancestors);
63889639 1635 call_depth--;
6d297f81 1636
5a753613 1637 if (!merged_common_ancestors)
6d297f81
JS
1638 die("merge returned no commit");
1639 }
1640
8b944b56 1641 discard_cache();
63889639 1642 if (!call_depth) {
8b944b56 1643 read_cache();
6d297f81 1644 index_only = 0;
8b944b56 1645 } else
6d297f81 1646 index_only = 1;
6d297f81 1647
5a753613 1648 clean = merge_trees(h1->tree, h2->tree, merged_common_ancestors->tree,
c1d20846 1649 branch1, branch2, &mrtree);
6d297f81 1650
8b944b56 1651 if (index_only) {
3af244ca
JS
1652 *result = make_virtual_commit(mrtree, "merged tree");
1653 commit_list_insert(h1, &(*result)->parents);
1654 commit_list_insert(h2, &(*result)->parents->next);
8b944b56 1655 }
66a155bc 1656 flush_output();
3af244ca 1657 return clean;
6d297f81
JS
1658}
1659
7ba3c078
SP
1660static const char *better_branch_name(const char *branch)
1661{
1662 static char githead_env[8 + 40 + 1];
1663 char *name;
1664
1665 if (strlen(branch) != 40)
1666 return branch;
1667 sprintf(githead_env, "GITHEAD_%s", branch);
1668 name = getenv(githead_env);
1669 return name ? name : branch;
1670}
1671
6d297f81
JS
1672static struct commit *get_ref(const char *ref)
1673{
1674 unsigned char sha1[20];
1675 struct object *object;
1676
1677 if (get_sha1(ref, sha1))
1678 die("Could not resolve ref '%s'", ref);
1679 object = deref_tag(parse_object(sha1), ref, strlen(ref));
a970e84e
SP
1680 if (object->type == OBJ_TREE)
1681 return make_virtual_commit((struct tree*)object,
1682 better_branch_name(ref));
bf6d324e 1683 if (object->type != OBJ_COMMIT)
6d297f81
JS
1684 return NULL;
1685 if (parse_commit((struct commit *)object))
1686 die("Could not parse commit '%s'", sha1_to_hex(object->sha1));
1687 return (struct commit *)object;
1688}
1689
8c3275ab
SP
1690static int merge_config(const char *var, const char *value)
1691{
1692 if (!strcasecmp(var, "merge.verbosity")) {
1693 verbosity = git_config_int(var, value);
1694 return 0;
1695 }
1696 return git_default_config(var, value);
1697}
1698
6d297f81
JS
1699int main(int argc, char *argv[])
1700{
8b944b56 1701 static const char *bases[20];
6d297f81 1702 static unsigned bases_count = 0;
3af244ca
JS
1703 int i, clean;
1704 const char *branch1, *branch2;
1705 struct commit *result, *h1, *h2;
8b944b56
JH
1706 struct commit_list *ca = NULL;
1707 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
1708 int index_fd;
6d297f81 1709
68faf689
JH
1710 if (argv[0]) {
1711 int namelen = strlen(argv[0]);
1712 if (8 < namelen &&
1713 !strcmp(argv[0] + namelen - 8, "-subtree"))
1714 subtree_merge = 1;
1715 }
1716
8c3275ab
SP
1717 git_config(merge_config);
1718 if (getenv("GIT_MERGE_VERBOSITY"))
1719 verbosity = strtol(getenv("GIT_MERGE_VERBOSITY"), NULL, 10);
6d297f81
JS
1720
1721 if (argc < 4)
1722 die("Usage: %s <base>... -- <head> <remote> ...\n", argv[0]);
1723
6d297f81
JS
1724 for (i = 1; i < argc; ++i) {
1725 if (!strcmp(argv[i], "--"))
1726 break;
1727 if (bases_count < sizeof(bases)/sizeof(*bases))
1728 bases[bases_count++] = argv[i];
1729 }
1730 if (argc - i != 3) /* "--" "<head>" "<remote>" */
1731 die("Not handling anything other than two heads merge.");
ad57cbca 1732 if (verbosity >= 5)
3f6ee2d1 1733 buffer_output = 0;
6d297f81 1734
6d297f81
JS
1735 branch1 = argv[++i];
1736 branch2 = argv[++i];
6d297f81 1737
3af244ca
JS
1738 h1 = get_ref(branch1);
1739 h2 = get_ref(branch2);
6d297f81 1740
e0ec1819
SP
1741 branch1 = better_branch_name(branch1);
1742 branch2 = better_branch_name(branch2);
3f6ee2d1 1743
8c3275ab
SP
1744 if (show(3))
1745 printf("Merging %s with %s\n", branch1, branch2);
e0ec1819 1746
30ca07a2 1747 index_fd = hold_locked_index(lock, 1);
6d297f81 1748
8b944b56
JH
1749 for (i = 0; i < bases_count; i++) {
1750 struct commit *ancestor = get_ref(bases[i]);
1751 ca = commit_list_insert(ancestor, &ca);
1752 }
63889639 1753 clean = merge(h1, h2, branch1, branch2, ca, &result);
8b944b56
JH
1754
1755 if (active_cache_changed &&
1756 (write_cache(index_fd, active_cache, active_nr) ||
30ca07a2 1757 close(index_fd) || commit_locked_index(lock)))
8b944b56 1758 die ("unable to write %s", get_index_file());
6d297f81 1759
3af244ca 1760 return clean ? 0: 1;
6d297f81 1761}