]> git.ipfire.org Git - thirdparty/git.git/blame - merge-recursive.c
Use xmemdupz() in many places.
[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{
182af834 435 int ret;
3af244ca 436 char *slash, *dirs;
6d297f81
JS
437
438 ret = unlink(name);
3af244ca 439 if (ret)
6d297f81 440 return ret;
182af834 441 dirs = xstrdup(name);
3af244ca 442 while ((slash = strrchr(name, '/'))) {
6d297f81 443 *slash = '\0';
3af244ca 444 if (rmdir(name) != 0)
6d297f81
JS
445 break;
446 }
447 free(dirs);
448 return ret;
449}
450
65ac6e9c 451static int remove_file(int clean, const char *path, int no_wd)
6d297f81 452{
5a753613 453 int update_cache = index_only || clean;
65ac6e9c 454 int update_working_directory = !index_only && !no_wd;
6d297f81 455
5a753613 456 if (update_cache) {
6d297f81
JS
457 if (remove_file_from_cache(path))
458 return -1;
459 }
65ac6e9c 460 if (update_working_directory) {
6d297f81 461 unlink(path);
3af244ca 462 if (errno != ENOENT || errno != EISDIR)
6d297f81
JS
463 return -1;
464 remove_path(path);
465 }
466 return 0;
467}
468
469static char *unique_path(const char *path, const char *branch)
470{
471 char *newpath = xmalloc(strlen(path) + 1 + strlen(branch) + 8 + 1);
3af244ca
JS
472 int suffix = 0;
473 struct stat st;
f59aac47 474 char *p = newpath + strlen(path);
6d297f81 475 strcpy(newpath, path);
f59aac47 476 *(p++) = '~';
6d297f81 477 strcpy(p, branch);
3af244ca
JS
478 for (; *p; ++p)
479 if ('/' == *p)
6d297f81 480 *p = '_';
5a753613
JS
481 while (path_list_has_path(&current_file_set, newpath) ||
482 path_list_has_path(&current_directory_set, newpath) ||
3af244ca 483 lstat(newpath, &st) == 0)
6d297f81 484 sprintf(p, "_%d", suffix++);
3af244ca 485
5a753613 486 path_list_insert(newpath, &current_file_set);
6d297f81
JS
487 return newpath;
488}
489
3af244ca 490static int mkdir_p(const char *path, unsigned long mode)
6d297f81 491{
9befac47
SP
492 /* path points to cache entries, so xstrdup before messing with it */
493 char *buf = xstrdup(path);
3af244ca 494 int result = safe_create_leading_directories(buf);
6d297f81 495 free(buf);
3af244ca 496 return result;
6d297f81
JS
497}
498
499static void flush_buffer(int fd, const char *buf, unsigned long size)
500{
501 while (size > 0) {
93822c22 502 long ret = write_in_full(fd, buf, size);
6d297f81
JS
503 if (ret < 0) {
504 /* Ignore epipe */
505 if (errno == EPIPE)
506 break;
507 die("merge-recursive: %s", strerror(errno));
508 } else if (!ret) {
509 die("merge-recursive: disk full?");
510 }
511 size -= ret;
512 buf += ret;
513 }
514}
515
851c603e
LT
516static int make_room_for_path(const char *path)
517{
518 int status;
519 const char *msg = "failed to create path '%s'%s";
520
521 status = mkdir_p(path, 0777);
522 if (status) {
523 if (status == -3) {
524 /* something else exists */
525 error(msg, path, ": perhaps a D/F conflict?");
526 return -1;
527 }
528 die(msg, path, "");
529 }
530
531 /* Successful unlink is good.. */
532 if (!unlink(path))
533 return 0;
534 /* .. and so is no existing file */
535 if (errno == ENOENT)
536 return 0;
537 /* .. but not some other error (who really cares what?) */
538 return error(msg, path, ": perhaps a D/F conflict?");
539}
540
9fe0d87d
JH
541static void update_file_flags(const unsigned char *sha,
542 unsigned mode,
543 const char *path,
544 int update_cache,
545 int update_wd)
6d297f81 546{
3af244ca
JS
547 if (index_only)
548 update_wd = 0;
6d297f81 549
3af244ca 550 if (update_wd) {
21666f1a 551 enum object_type type;
6d297f81
JS
552 void *buf;
553 unsigned long size;
554
21666f1a 555 buf = read_sha1_file(sha, &type, &size);
6d297f81
JS
556 if (!buf)
557 die("cannot read object %s '%s'", sha1_to_hex(sha), path);
21666f1a 558 if (type != OBJ_BLOB)
6d297f81
JS
559 die("blob expected for %s '%s'", sha1_to_hex(sha), path);
560
851c603e
LT
561 if (make_room_for_path(path) < 0) {
562 update_wd = 0;
563 goto update_index;
564 }
723024d6 565 if (S_ISREG(mode) || (!has_symlinks && S_ISLNK(mode))) {
3af244ca 566 int fd;
3af244ca 567 if (mode & 0100)
6d297f81
JS
568 mode = 0777;
569 else
570 mode = 0666;
3af244ca
JS
571 fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);
572 if (fd < 0)
6d297f81
JS
573 die("failed to open %s: %s", path, strerror(errno));
574 flush_buffer(fd, buf, size);
575 close(fd);
3af244ca 576 } else if (S_ISLNK(mode)) {
182af834 577 char *lnk = xmemdupz(buf, size);
3af244ca 578 mkdir_p(path, 0777);
17cd29b2 579 unlink(path);
3af244ca 580 symlink(lnk, path);
723024d6 581 free(lnk);
6d297f81
JS
582 } else
583 die("do not know what to do with %06o %s '%s'",
584 mode, sha1_to_hex(sha), path);
585 }
4d50895a 586 update_index:
3af244ca
JS
587 if (update_cache)
588 add_cacheinfo(mode, sha, path, 0, update_wd, ADD_CACHE_OK_TO_ADD);
6d297f81
JS
589}
590
9fe0d87d
JH
591static void update_file(int clean,
592 const unsigned char *sha,
593 unsigned mode,
594 const char *path)
6d297f81
JS
595{
596 update_file_flags(sha, mode, path, index_only || clean, !index_only);
597}
598
599/* Low level file merging, update and removal */
600
601struct merge_file_info
602{
603 unsigned char sha[20];
604 unsigned mode;
605 unsigned clean:1,
606 merge:1;
607};
608
c2b4faea 609static void fill_mm(const unsigned char *sha1, mmfile_t *mm)
6d297f81 610{
6d297f81 611 unsigned long size;
21666f1a 612 enum object_type type;
6d297f81 613
f953831e
JS
614 if (!hashcmp(sha1, null_sha1)) {
615 mm->ptr = xstrdup("");
616 mm->size = 0;
617 return;
618 }
6d297f81 619
21666f1a
NP
620 mm->ptr = read_sha1_file(sha1, &type, &size);
621 if (!mm->ptr || type != OBJ_BLOB)
6d297f81 622 die("unable to read blob object %s", sha1_to_hex(sha1));
c2b4faea 623 mm->size = size;
6d297f81
JS
624}
625
153920da
JH
626/*
627 * Customizable low-level merge drivers support.
628 */
629
630struct ll_merge_driver;
631typedef int (*ll_merge_fn)(const struct ll_merge_driver *,
632 const char *path,
f3ef6b6b 633 mmfile_t *orig,
a129d96f
JH
634 mmfile_t *src1, const char *name1,
635 mmfile_t *src2, const char *name2,
636 mmbuffer_t *result);
637
153920da
JH
638struct ll_merge_driver {
639 const char *name;
640 const char *description;
641 ll_merge_fn fn;
3086486d 642 const char *recursive;
153920da
JH
643 struct ll_merge_driver *next;
644 char *cmdline;
645};
646
647/*
648 * Built-in low-levels
649 */
b798671f
JH
650static int ll_binary_merge(const struct ll_merge_driver *drv_unused,
651 const char *path_unused,
652 mmfile_t *orig,
653 mmfile_t *src1, const char *name1,
654 mmfile_t *src2, const char *name2,
655 mmbuffer_t *result)
656{
657 /*
658 * The tentative merge result is "ours" for the final round,
659 * or common ancestor for an internal merge. Still return
660 * "conflicted merge" status.
661 */
662 mmfile_t *stolen = index_only ? orig : src1;
663
664 result->ptr = stolen->ptr;
665 result->size = stolen->size;
666 stolen->ptr = NULL;
667 return 1;
668}
669
153920da
JH
670static int ll_xdl_merge(const struct ll_merge_driver *drv_unused,
671 const char *path_unused,
f3ef6b6b 672 mmfile_t *orig,
a129d96f
JH
673 mmfile_t *src1, const char *name1,
674 mmfile_t *src2, const char *name2,
675 mmbuffer_t *result)
676{
677 xpparam_t xpp;
678
9f30855d 679 if (buffer_is_binary(orig->ptr, orig->size) ||
b798671f
JH
680 buffer_is_binary(src1->ptr, src1->size) ||
681 buffer_is_binary(src2->ptr, src2->size)) {
682 warning("Cannot merge binary files: %s vs. %s\n",
9f30855d 683 name1, name2);
b798671f
JH
684 return ll_binary_merge(drv_unused, path_unused,
685 orig, src1, name1,
686 src2, name2,
687 result);
688 }
9f30855d 689
a129d96f
JH
690 memset(&xpp, 0, sizeof(xpp));
691 return xdl_merge(orig,
692 src1, name1,
693 src2, name2,
694 &xpp, XDL_MERGE_ZEALOUS,
695 result);
696}
697
153920da
JH
698static int ll_union_merge(const struct ll_merge_driver *drv_unused,
699 const char *path_unused,
f3ef6b6b 700 mmfile_t *orig,
a129d96f
JH
701 mmfile_t *src1, const char *name1,
702 mmfile_t *src2, const char *name2,
703 mmbuffer_t *result)
704{
705 char *src, *dst;
706 long size;
707 const int marker_size = 7;
708
153920da
JH
709 int status = ll_xdl_merge(drv_unused, path_unused,
710 orig, src1, NULL, src2, NULL, result);
a129d96f
JH
711 if (status <= 0)
712 return status;
713 size = result->size;
714 src = dst = result->ptr;
715 while (size) {
716 char ch;
717 if ((marker_size < size) &&
718 (*src == '<' || *src == '=' || *src == '>')) {
719 int i;
720 ch = *src;
721 for (i = 0; i < marker_size; i++)
722 if (src[i] != ch)
723 goto not_a_marker;
724 if (src[marker_size] != '\n')
725 goto not_a_marker;
726 src += marker_size + 1;
727 size -= marker_size + 1;
728 continue;
729 }
730 not_a_marker:
731 do {
732 ch = *src++;
733 *dst++ = ch;
734 size--;
735 } while (ch != '\n' && size);
736 }
737 result->size = dst - result->ptr;
738 return 0;
739}
740
153920da
JH
741#define LL_BINARY_MERGE 0
742#define LL_TEXT_MERGE 1
743#define LL_UNION_MERGE 2
744static struct ll_merge_driver ll_merge_drv[] = {
745 { "binary", "built-in binary merge", ll_binary_merge },
746 { "text", "built-in 3-way text merge", ll_xdl_merge },
747 { "union", "built-in union merge", ll_union_merge },
a129d96f
JH
748};
749
f3ef6b6b
JH
750static void create_temp(mmfile_t *src, char *path)
751{
752 int fd;
753
754 strcpy(path, ".merge_file_XXXXXX");
7647b17f 755 fd = xmkstemp(path);
f3ef6b6b
JH
756 if (write_in_full(fd, src->ptr, src->size) != src->size)
757 die("unable to write temp-file");
758 close(fd);
759}
760
153920da
JH
761/*
762 * User defined low-level merge driver support.
763 */
764static int ll_ext_merge(const struct ll_merge_driver *fn,
765 const char *path,
f3ef6b6b
JH
766 mmfile_t *orig,
767 mmfile_t *src1, const char *name1,
768 mmfile_t *src2, const char *name2,
769 mmbuffer_t *result)
770{
771 char temp[3][50];
772 char cmdbuf[2048];
773 struct interp table[] = {
774 { "%O" },
775 { "%A" },
776 { "%B" },
777 };
778 struct child_process child;
779 const char *args[20];
780 int status, fd, i;
781 struct stat st;
782
15ba3af2
JH
783 if (fn->cmdline == NULL)
784 die("custom merge driver %s lacks command line.", fn->name);
785
f3ef6b6b
JH
786 result->ptr = NULL;
787 result->size = 0;
788 create_temp(orig, temp[0]);
789 create_temp(src1, temp[1]);
790 create_temp(src2, temp[2]);
791
792 interp_set_entry(table, 0, temp[0]);
793 interp_set_entry(table, 1, temp[1]);
794 interp_set_entry(table, 2, temp[2]);
795
153920da
JH
796 output(1, "merging %s using %s", path,
797 fn->description ? fn->description : fn->name);
798
799 interpolate(cmdbuf, sizeof(cmdbuf), fn->cmdline, table, 3);
f3ef6b6b
JH
800
801 memset(&child, 0, sizeof(child));
802 child.argv = args;
803 args[0] = "sh";
804 args[1] = "-c";
805 args[2] = cmdbuf;
806 args[3] = NULL;
807
808 status = run_command(&child);
809 if (status < -ERR_RUN_COMMAND_FORK)
810 ; /* failure in run-command */
811 else
812 status = -status;
813 fd = open(temp[1], O_RDONLY);
814 if (fd < 0)
815 goto bad;
816 if (fstat(fd, &st))
817 goto close_bad;
818 result->size = st.st_size;
819 result->ptr = xmalloc(result->size + 1);
820 if (read_in_full(fd, result->ptr, result->size) != result->size) {
821 free(result->ptr);
822 result->ptr = NULL;
823 result->size = 0;
824 }
825 close_bad:
826 close(fd);
827 bad:
828 for (i = 0; i < 3; i++)
829 unlink(temp[i]);
830 return status;
831}
832
833/*
834 * merge.default and merge.driver configuration items
835 */
153920da 836static struct ll_merge_driver *ll_user_merge, **ll_user_merge_tail;
be89cb23 837static const char *default_ll_merge;
f3ef6b6b
JH
838
839static int read_merge_config(const char *var, const char *value)
840{
153920da
JH
841 struct ll_merge_driver *fn;
842 const char *ep, *name;
843 int namelen;
f3ef6b6b 844
be89cb23 845 if (!strcmp(var, "merge.default")) {
153920da
JH
846 if (value)
847 default_ll_merge = strdup(value);
be89cb23
JH
848 return 0;
849 }
850
153920da
JH
851 /*
852 * We are not interested in anything but "merge.<name>.variable";
853 * especially, we do not want to look at variables such as
854 * "merge.summary", "merge.tool", and "merge.verbosity".
855 */
15ba3af2 856 if (prefixcmp(var, "merge.") || (ep = strrchr(var, '.')) == var + 5)
f3ef6b6b 857 return 0;
153920da 858
f3ef6b6b 859 /*
153920da
JH
860 * Find existing one as we might be processing merge.<name>.var2
861 * after seeing merge.<name>.var1.
f3ef6b6b 862 */
153920da
JH
863 name = var + 6;
864 namelen = ep - name;
865 for (fn = ll_user_merge; fn; fn = fn->next)
866 if (!strncmp(fn->name, name, namelen) && !fn->name[namelen])
867 break;
868 if (!fn) {
153920da 869 fn = xcalloc(1, sizeof(struct ll_merge_driver));
182af834 870 fn->name = xmemdupz(name, namelen);
153920da 871 fn->fn = ll_ext_merge;
153920da 872 *ll_user_merge_tail = fn;
e87b1c94 873 ll_user_merge_tail = &(fn->next);
153920da
JH
874 }
875
876 ep++;
877
878 if (!strcmp("name", ep)) {
879 if (!value)
880 return error("%s: lacks value", var);
881 fn->description = strdup(value);
882 return 0;
883 }
884
885 if (!strcmp("driver", ep)) {
886 if (!value)
887 return error("%s: lacks value", var);
888 /*
889 * merge.<name>.driver specifies the command line:
890 *
891 * command-line
892 *
893 * The command-line will be interpolated with the following
894 * tokens and is given to the shell:
895 *
896 * %O - temporary file name for the merge base.
897 * %A - temporary file name for our version.
898 * %B - temporary file name for the other branches' version.
899 *
900 * The external merge driver should write the results in the
901 * file named by %A, and signal that it has done with zero exit
902 * status.
903 */
904 fn->cmdline = strdup(value);
905 return 0;
906 }
907
3086486d
JH
908 if (!strcmp("recursive", ep)) {
909 if (!value)
910 return error("%s: lacks value", var);
911 fn->recursive = strdup(value);
912 return 0;
913 }
914
f3ef6b6b
JH
915 return 0;
916}
917
918static void initialize_ll_merge(void)
a129d96f 919{
153920da 920 if (ll_user_merge_tail)
f3ef6b6b 921 return;
153920da 922 ll_user_merge_tail = &ll_user_merge;
f3ef6b6b
JH
923 git_config(read_merge_config);
924}
925
a5e92abd 926static const struct ll_merge_driver *find_ll_merge_driver(const char *merge_attr)
f3ef6b6b 927{
153920da 928 struct ll_merge_driver *fn;
a129d96f
JH
929 const char *name;
930 int i;
931
f3ef6b6b
JH
932 initialize_ll_merge();
933
934 if (ATTR_TRUE(merge_attr))
153920da 935 return &ll_merge_drv[LL_TEXT_MERGE];
a129d96f 936 else if (ATTR_FALSE(merge_attr))
153920da 937 return &ll_merge_drv[LL_BINARY_MERGE];
be89cb23
JH
938 else if (ATTR_UNSET(merge_attr)) {
939 if (!default_ll_merge)
153920da 940 return &ll_merge_drv[LL_TEXT_MERGE];
be89cb23
JH
941 else
942 name = default_ll_merge;
943 }
f3ef6b6b
JH
944 else
945 name = merge_attr;
946
153920da
JH
947 for (fn = ll_user_merge; fn; fn = fn->next)
948 if (!strcmp(fn->name, name))
949 return fn;
a129d96f 950
153920da
JH
951 for (i = 0; i < ARRAY_SIZE(ll_merge_drv); i++)
952 if (!strcmp(ll_merge_drv[i].name, name))
953 return &ll_merge_drv[i];
a129d96f
JH
954
955 /* default to the 3-way */
153920da 956 return &ll_merge_drv[LL_TEXT_MERGE];
a129d96f
JH
957}
958
a5e92abd 959static const char *git_path_check_merge(const char *path)
a129d96f
JH
960{
961 static struct git_attr_check attr_merge_check;
962
963 if (!attr_merge_check.attr)
964 attr_merge_check.attr = git_attr("merge", 5);
965
966 if (git_checkattr(path, 1, &attr_merge_check))
a5e92abd 967 return NULL;
a129d96f
JH
968 return attr_merge_check.value;
969}
970
3e5261a2
JH
971static int ll_merge(mmbuffer_t *result_buf,
972 struct diff_filespec *o,
973 struct diff_filespec *a,
974 struct diff_filespec *b,
975 const char *branch1,
976 const char *branch2)
977{
978 mmfile_t orig, src1, src2;
3e5261a2
JH
979 char *name1, *name2;
980 int merge_status;
a5e92abd 981 const char *ll_driver_name;
153920da 982 const struct ll_merge_driver *driver;
3e5261a2
JH
983
984 name1 = xstrdup(mkpath("%s:%s", branch1, a->path));
985 name2 = xstrdup(mkpath("%s:%s", branch2, b->path));
986
987 fill_mm(o->sha1, &orig);
988 fill_mm(a->sha1, &src1);
989 fill_mm(b->sha1, &src2);
990
a5e92abd
JH
991 ll_driver_name = git_path_check_merge(a->path);
992 driver = find_ll_merge_driver(ll_driver_name);
a129d96f 993
d56dbd67
JH
994 if (index_only && driver->recursive)
995 driver = find_ll_merge_driver(driver->recursive);
153920da
JH
996 merge_status = driver->fn(driver, a->path,
997 &orig, &src1, name1, &src2, name2,
998 result_buf);
a129d96f 999
3e5261a2
JH
1000 free(name1);
1001 free(name2);
1002 free(orig.ptr);
1003 free(src1.ptr);
1004 free(src2.ptr);
1005 return merge_status;
1006}
1007
3af244ca
JS
1008static struct merge_file_info merge_file(struct diff_filespec *o,
1009 struct diff_filespec *a, struct diff_filespec *b,
c1d20846 1010 const char *branch1, const char *branch2)
6d297f81
JS
1011{
1012 struct merge_file_info result;
1013 result.merge = 0;
1014 result.clean = 1;
1015
3af244ca 1016 if ((S_IFMT & a->mode) != (S_IFMT & b->mode)) {
6d297f81 1017 result.clean = 0;
3af244ca
JS
1018 if (S_ISREG(a->mode)) {
1019 result.mode = a->mode;
8da71493 1020 hashcpy(result.sha, a->sha1);
6d297f81 1021 } else {
3af244ca 1022 result.mode = b->mode;
8da71493 1023 hashcpy(result.sha, b->sha1);
6d297f81
JS
1024 }
1025 } else {
3af244ca 1026 if (!sha_eq(a->sha1, o->sha1) && !sha_eq(b->sha1, o->sha1))
6d297f81
JS
1027 result.merge = 1;
1028
3af244ca 1029 result.mode = a->mode == o->mode ? b->mode: a->mode;
6d297f81 1030
3af244ca 1031 if (sha_eq(a->sha1, o->sha1))
8da71493 1032 hashcpy(result.sha, b->sha1);
3af244ca 1033 else if (sha_eq(b->sha1, o->sha1))
8da71493 1034 hashcpy(result.sha, a->sha1);
3af244ca 1035 else if (S_ISREG(a->mode)) {
c2b4faea 1036 mmbuffer_t result_buf;
c2b4faea
JH
1037 int merge_status;
1038
3e5261a2
JH
1039 merge_status = ll_merge(&result_buf, o, a, b,
1040 branch1, branch2);
c2b4faea
JH
1041
1042 if ((merge_status < 0) || !result_buf.ptr)
1043 die("Failed to execute internal merge");
1044
1045 if (write_sha1_file(result_buf.ptr, result_buf.size,
1046 blob_type, result.sha))
1047 die("Unable to add %s to database",
1048 a->path);
1049
1050 free(result_buf.ptr);
1051 result.clean = (merge_status == 0);
6d297f81 1052 } else {
3af244ca 1053 if (!(S_ISLNK(a->mode) || S_ISLNK(b->mode)))
6d297f81
JS
1054 die("cannot merge modes?");
1055
8da71493 1056 hashcpy(result.sha, a->sha1);
6d297f81 1057
3af244ca 1058 if (!sha_eq(a->sha1, b->sha1))
6d297f81
JS
1059 result.clean = 0;
1060 }
1061 }
1062
1063 return result;
1064}
1065
1066static void conflict_rename_rename(struct rename *ren1,
1067 const char *branch1,
1068 struct rename *ren2,
1069 const char *branch2)
1070{
1071 char *del[2];
1072 int delp = 0;
1073 const char *ren1_dst = ren1->pair->two->path;
1074 const char *ren2_dst = ren2->pair->two->path;
5a753613
JS
1075 const char *dst_name1 = ren1_dst;
1076 const char *dst_name2 = ren2_dst;
1077 if (path_list_has_path(&current_directory_set, ren1_dst)) {
1078 dst_name1 = del[delp++] = unique_path(ren1_dst, branch1);
89f40be2 1079 output(1, "%s is a directory in %s added as %s instead",
5a753613 1080 ren1_dst, branch2, dst_name1);
65ac6e9c 1081 remove_file(0, ren1_dst, 0);
6d297f81 1082 }
5a753613
JS
1083 if (path_list_has_path(&current_directory_set, ren2_dst)) {
1084 dst_name2 = del[delp++] = unique_path(ren2_dst, branch2);
89f40be2 1085 output(1, "%s is a directory in %s added as %s instead",
5a753613 1086 ren2_dst, branch1, dst_name2);
65ac6e9c 1087 remove_file(0, ren2_dst, 0);
6d297f81 1088 }
a97e4075
AR
1089 if (index_only) {
1090 remove_file_from_cache(dst_name1);
1091 remove_file_from_cache(dst_name2);
1092 /*
1093 * Uncomment to leave the conflicting names in the resulting tree
1094 *
1095 * update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, dst_name1);
1096 * update_file(0, ren2->pair->two->sha1, ren2->pair->two->mode, dst_name2);
1097 */
1098 } else {
1099 update_stages(dst_name1, NULL, ren1->pair->two, NULL, 1);
1100 update_stages(dst_name2, NULL, NULL, ren2->pair->two, 1);
1101 }
3af244ca 1102 while (delp--)
6d297f81
JS
1103 free(del[delp]);
1104}
1105
1106static void conflict_rename_dir(struct rename *ren1,
1107 const char *branch1)
1108{
5a753613 1109 char *new_path = unique_path(ren1->pair->two->path, branch1);
89f40be2 1110 output(1, "Renamed %s to %s instead", ren1->pair->one->path, new_path);
65ac6e9c 1111 remove_file(0, ren1->pair->two->path, 0);
5a753613
JS
1112 update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path);
1113 free(new_path);
6d297f81
JS
1114}
1115
1116static void conflict_rename_rename_2(struct rename *ren1,
1117 const char *branch1,
1118 struct rename *ren2,
1119 const char *branch2)
1120{
5a753613
JS
1121 char *new_path1 = unique_path(ren1->pair->two->path, branch1);
1122 char *new_path2 = unique_path(ren2->pair->two->path, branch2);
89f40be2 1123 output(1, "Renamed %s to %s and %s to %s instead",
5a753613
JS
1124 ren1->pair->one->path, new_path1,
1125 ren2->pair->one->path, new_path2);
65ac6e9c 1126 remove_file(0, ren1->pair->two->path, 0);
5a753613
JS
1127 update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, new_path1);
1128 update_file(0, ren2->pair->two->sha1, ren2->pair->two->mode, new_path2);
1129 free(new_path2);
1130 free(new_path1);
6d297f81
JS
1131}
1132
5a753613
JS
1133static int process_renames(struct path_list *a_renames,
1134 struct path_list *b_renames,
1135 const char *a_branch,
1136 const char *b_branch)
6d297f81 1137{
5a753613
JS
1138 int clean_merge = 1, i, j;
1139 struct path_list a_by_dst = {NULL, 0, 0, 0}, b_by_dst = {NULL, 0, 0, 0};
6d297f81
JS
1140 const struct rename *sre;
1141
5a753613
JS
1142 for (i = 0; i < a_renames->nr; i++) {
1143 sre = a_renames->items[i].util;
1144 path_list_insert(sre->pair->two->path, &a_by_dst)->util
6d297f81
JS
1145 = sre->dst_entry;
1146 }
5a753613
JS
1147 for (i = 0; i < b_renames->nr; i++) {
1148 sre = b_renames->items[i].util;
1149 path_list_insert(sre->pair->two->path, &b_by_dst)->util
6d297f81
JS
1150 = sre->dst_entry;
1151 }
1152
5a753613 1153 for (i = 0, j = 0; i < a_renames->nr || j < b_renames->nr;) {
3af244ca
JS
1154 int compare;
1155 char *src;
6d297f81 1156 struct path_list *renames1, *renames2, *renames2Dst;
3af244ca 1157 struct rename *ren1 = NULL, *ren2 = NULL;
5a753613 1158 const char *branch1, *branch2;
3af244ca
JS
1159 const char *ren1_src, *ren1_dst;
1160
5a753613 1161 if (i >= a_renames->nr) {
3af244ca 1162 compare = 1;
5a753613
JS
1163 ren2 = b_renames->items[j++].util;
1164 } else if (j >= b_renames->nr) {
3af244ca 1165 compare = -1;
5a753613 1166 ren1 = a_renames->items[i++].util;
3af244ca 1167 } else {
5a753613
JS
1168 compare = strcmp(a_renames->items[i].path,
1169 b_renames->items[j].path);
3d234d0a
JS
1170 if (compare <= 0)
1171 ren1 = a_renames->items[i++].util;
1172 if (compare >= 0)
1173 ren2 = b_renames->items[j++].util;
3af244ca
JS
1174 }
1175
6d297f81 1176 /* TODO: refactor, so that 1/2 are not needed */
3af244ca 1177 if (ren1) {
5a753613
JS
1178 renames1 = a_renames;
1179 renames2 = b_renames;
1180 renames2Dst = &b_by_dst;
1181 branch1 = a_branch;
1182 branch2 = b_branch;
6d297f81 1183 } else {
3af244ca 1184 struct rename *tmp;
5a753613
JS
1185 renames1 = b_renames;
1186 renames2 = a_renames;
1187 renames2Dst = &a_by_dst;
1188 branch1 = b_branch;
1189 branch2 = a_branch;
3af244ca 1190 tmp = ren2;
6d297f81
JS
1191 ren2 = ren1;
1192 ren1 = tmp;
1193 }
3af244ca 1194 src = ren1->pair->one->path;
6d297f81
JS
1195
1196 ren1->dst_entry->processed = 1;
1197 ren1->src_entry->processed = 1;
1198
3af244ca 1199 if (ren1->processed)
6d297f81
JS
1200 continue;
1201 ren1->processed = 1;
1202
3af244ca
JS
1203 ren1_src = ren1->pair->one->path;
1204 ren1_dst = ren1->pair->two->path;
6d297f81 1205
3af244ca 1206 if (ren2) {
6d297f81
JS
1207 const char *ren2_src = ren2->pair->one->path;
1208 const char *ren2_dst = ren2->pair->two->path;
1209 /* Renamed in 1 and renamed in 2 */
1210 if (strcmp(ren1_src, ren2_src) != 0)
1211 die("ren1.src != ren2.src");
1212 ren2->dst_entry->processed = 1;
1213 ren2->processed = 1;
1214 if (strcmp(ren1_dst, ren2_dst) != 0) {
5a753613 1215 clean_merge = 0;
8c3275ab 1216 output(1, "CONFLICT (rename/rename): "
a97e4075
AR
1217 "Rename \"%s\"->\"%s\" in branch \"%s\" "
1218 "rename \"%s\"->\"%s\" in \"%s\"%s",
5a753613 1219 src, ren1_dst, branch1,
a97e4075
AR
1220 src, ren2_dst, branch2,
1221 index_only ? " (left unresolved)": "");
1222 if (index_only) {
1223 remove_file_from_cache(src);
1224 update_file(0, ren1->pair->one->sha1,
1225 ren1->pair->one->mode, src);
1226 }
5a753613 1227 conflict_rename_rename(ren1, branch1, ren2, branch2);
6d297f81 1228 } else {
6d297f81 1229 struct merge_file_info mfi;
65ac6e9c 1230 remove_file(1, ren1_src, 1);
3af244ca
JS
1231 mfi = merge_file(ren1->pair->one,
1232 ren1->pair->two,
1233 ren2->pair->two,
5a753613
JS
1234 branch1,
1235 branch2);
3af244ca 1236 if (mfi.merge || !mfi.clean)
89f40be2 1237 output(1, "Renamed %s->%s", src, ren1_dst);
6d297f81 1238
3af244ca 1239 if (mfi.merge)
89f40be2 1240 output(2, "Auto-merged %s", ren1_dst);
6d297f81 1241
3af244ca 1242 if (!mfi.clean) {
8c3275ab 1243 output(1, "CONFLICT (content): merge conflict in %s",
6d297f81 1244 ren1_dst);
5a753613 1245 clean_merge = 0;
6d297f81 1246
3af244ca 1247 if (!index_only)
6d297f81 1248 update_stages(ren1_dst,
3af244ca
JS
1249 ren1->pair->one,
1250 ren1->pair->two,
1251 ren2->pair->two,
6d297f81
JS
1252 1 /* clear */);
1253 }
1254 update_file(mfi.clean, mfi.sha, mfi.mode, ren1_dst);
1255 }
1256 } else {
1257 /* Renamed in 1, maybe changed in 2 */
3af244ca
JS
1258 struct path_list_item *item;
1259 /* we only use sha1 and mode of these */
1260 struct diff_filespec src_other, dst_other;
5a753613 1261 int try_merge, stage = a_renames == renames1 ? 3: 2;
6d297f81 1262
183d7972 1263 remove_file(1, ren1_src, index_only || stage == 3);
6d297f81 1264
87cb004e 1265 hashcpy(src_other.sha1, ren1->src_entry->stages[stage].sha);
3af244ca 1266 src_other.mode = ren1->src_entry->stages[stage].mode;
87cb004e 1267 hashcpy(dst_other.sha1, ren1->dst_entry->stages[stage].sha);
3af244ca 1268 dst_other.mode = ren1->dst_entry->stages[stage].mode;
6d297f81 1269
5a753613 1270 try_merge = 0;
6d297f81 1271
5a753613
JS
1272 if (path_list_has_path(&current_directory_set, ren1_dst)) {
1273 clean_merge = 0;
89f40be2 1274 output(1, "CONFLICT (rename/directory): Renamed %s->%s in %s "
6d297f81 1275 " directory %s added in %s",
5a753613
JS
1276 ren1_src, ren1_dst, branch1,
1277 ren1_dst, branch2);
1278 conflict_rename_dir(ren1, branch1);
3af244ca 1279 } else if (sha_eq(src_other.sha1, null_sha1)) {
5a753613 1280 clean_merge = 0;
89f40be2 1281 output(1, "CONFLICT (rename/delete): Renamed %s->%s in %s "
6d297f81 1282 "and deleted in %s",
5a753613
JS
1283 ren1_src, ren1_dst, branch1,
1284 branch2);
6d297f81 1285 update_file(0, ren1->pair->two->sha1, ren1->pair->two->mode, ren1_dst);
3af244ca 1286 } else if (!sha_eq(dst_other.sha1, null_sha1)) {
5a753613
JS
1287 const char *new_path;
1288 clean_merge = 0;
1289 try_merge = 1;
89f40be2 1290 output(1, "CONFLICT (rename/add): Renamed %s->%s in %s. "
6d297f81 1291 "%s added in %s",
5a753613
JS
1292 ren1_src, ren1_dst, branch1,
1293 ren1_dst, branch2);
1294 new_path = unique_path(ren1_dst, branch2);
89f40be2 1295 output(1, "Added as %s instead", new_path);
5a753613 1296 update_file(0, dst_other.sha1, dst_other.mode, new_path);
3af244ca
JS
1297 } else if ((item = path_list_lookup(ren1_dst, renames2Dst))) {
1298 ren2 = item->util;
5a753613 1299 clean_merge = 0;
6d297f81 1300 ren2->processed = 1;
89f40be2
SP
1301 output(1, "CONFLICT (rename/rename): Renamed %s->%s in %s. "
1302 "Renamed %s->%s in %s",
5a753613
JS
1303 ren1_src, ren1_dst, branch1,
1304 ren2->pair->one->path, ren2->pair->two->path, branch2);
1305 conflict_rename_rename_2(ren1, branch1, ren2, branch2);
6d297f81 1306 } else
5a753613 1307 try_merge = 1;
6d297f81 1308
5a753613 1309 if (try_merge) {
3af244ca 1310 struct diff_filespec *o, *a, *b;
6d297f81 1311 struct merge_file_info mfi;
3af244ca
JS
1312 src_other.path = (char *)ren1_src;
1313
1314 o = ren1->pair->one;
5a753613 1315 if (a_renames == renames1) {
3af244ca
JS
1316 a = ren1->pair->two;
1317 b = &src_other;
1318 } else {
1319 b = ren1->pair->two;
1320 a = &src_other;
1321 }
1322 mfi = merge_file(o, a, b,
5a753613 1323 a_branch, b_branch);
6d297f81 1324
0d5e6c97 1325 if (mfi.clean &&
c135ee88
AR
1326 sha_eq(mfi.sha, ren1->pair->two->sha1) &&
1327 mfi.mode == ren1->pair->two->mode)
8a359819
AR
1328 /*
1329 * This messaged is part of
1330 * t6022 test. If you change
1331 * it update the test too.
1332 */
c135ee88
AR
1333 output(3, "Skipped %s (merged same as existing)", ren1_dst);
1334 else {
1335 if (mfi.merge || !mfi.clean)
1336 output(1, "Renamed %s => %s", ren1_src, ren1_dst);
1337 if (mfi.merge)
1338 output(2, "Auto-merged %s", ren1_dst);
1339 if (!mfi.clean) {
1340 output(1, "CONFLICT (rename/modify): Merge conflict in %s",
1341 ren1_dst);
1342 clean_merge = 0;
1343
1344 if (!index_only)
1345 update_stages(ren1_dst,
1346 o, a, b, 1);
1347 }
1348 update_file(mfi.clean, mfi.sha, mfi.mode, ren1_dst);
6d297f81 1349 }
6d297f81
JS
1350 }
1351 }
1352 }
5a753613
JS
1353 path_list_clear(&a_by_dst, 0);
1354 path_list_clear(&b_by_dst, 0);
6d297f81 1355
5a753613 1356 return clean_merge;
6d297f81
JS
1357}
1358
ac7f0f43 1359static unsigned char *stage_sha(const unsigned char *sha, unsigned mode)
6d297f81 1360{
ac7f0f43 1361 return (is_null_sha1(sha) || mode == 0) ? NULL: (unsigned char *)sha;
6d297f81
JS
1362}
1363
1364/* Per entry merge function */
1365static int process_entry(const char *path, struct stage_data *entry,
c1d20846
JS
1366 const char *branch1,
1367 const char *branch2)
6d297f81
JS
1368{
1369 /*
1370 printf("processing entry, clean cache: %s\n", index_only ? "yes": "no");
1371 print_index_entry("\tpath: ", entry);
1372 */
5a753613 1373 int clean_merge = 1;
5a753613
JS
1374 unsigned o_mode = entry->stages[1].mode;
1375 unsigned a_mode = entry->stages[2].mode;
1376 unsigned b_mode = entry->stages[3].mode;
ac7f0f43
JH
1377 unsigned char *o_sha = stage_sha(entry->stages[1].sha, o_mode);
1378 unsigned char *a_sha = stage_sha(entry->stages[2].sha, a_mode);
1379 unsigned char *b_sha = stage_sha(entry->stages[3].sha, b_mode);
5a753613
JS
1380
1381 if (o_sha && (!a_sha || !b_sha)) {
6d297f81 1382 /* Case A: Deleted in one */
5a753613
JS
1383 if ((!a_sha && !b_sha) ||
1384 (sha_eq(a_sha, o_sha) && !b_sha) ||
1385 (!a_sha && sha_eq(b_sha, o_sha))) {
6d297f81
JS
1386 /* Deleted in both or deleted in one and
1387 * unchanged in the other */
5a753613 1388 if (a_sha)
89f40be2 1389 output(2, "Removed %s", path);
65ac6e9c
JH
1390 /* do not touch working file if it did not exist */
1391 remove_file(1, path, !a_sha);
6d297f81
JS
1392 } else {
1393 /* Deleted in one and changed in the other */
5a753613
JS
1394 clean_merge = 0;
1395 if (!a_sha) {
8c3275ab 1396 output(1, "CONFLICT (delete/modify): %s deleted in %s "
6d297f81 1397 "and modified in %s. Version %s of %s left in tree.",
c1d20846
JS
1398 path, branch1,
1399 branch2, branch2, path);
5a753613 1400 update_file(0, b_sha, b_mode, path);
6d297f81 1401 } else {
8c3275ab 1402 output(1, "CONFLICT (delete/modify): %s deleted in %s "
6d297f81 1403 "and modified in %s. Version %s of %s left in tree.",
c1d20846
JS
1404 path, branch2,
1405 branch1, branch1, path);
5a753613 1406 update_file(0, a_sha, a_mode, path);
6d297f81
JS
1407 }
1408 }
1409
5a753613
JS
1410 } else if ((!o_sha && a_sha && !b_sha) ||
1411 (!o_sha && !a_sha && b_sha)) {
6d297f81 1412 /* Case B: Added in one. */
5a753613
JS
1413 const char *add_branch;
1414 const char *other_branch;
6d297f81
JS
1415 unsigned mode;
1416 const unsigned char *sha;
1417 const char *conf;
1418
5a753613 1419 if (a_sha) {
c1d20846
JS
1420 add_branch = branch1;
1421 other_branch = branch2;
5a753613
JS
1422 mode = a_mode;
1423 sha = a_sha;
6d297f81
JS
1424 conf = "file/directory";
1425 } else {
c1d20846
JS
1426 add_branch = branch2;
1427 other_branch = branch1;
5a753613
JS
1428 mode = b_mode;
1429 sha = b_sha;
6d297f81
JS
1430 conf = "directory/file";
1431 }
5a753613
JS
1432 if (path_list_has_path(&current_directory_set, path)) {
1433 const char *new_path = unique_path(path, add_branch);
1434 clean_merge = 0;
8c3275ab 1435 output(1, "CONFLICT (%s): There is a directory with name %s in %s. "
89f40be2 1436 "Added %s as %s",
5a753613 1437 conf, path, other_branch, path, new_path);
65ac6e9c 1438 remove_file(0, path, 0);
5a753613 1439 update_file(0, sha, mode, new_path);
6d297f81 1440 } else {
89f40be2 1441 output(2, "Added %s", path);
6d297f81
JS
1442 update_file(1, sha, mode, path);
1443 }
f953831e
JS
1444 } else if (a_sha && b_sha) {
1445 /* Case C: Added in both (check for same permissions) and */
6d297f81 1446 /* case D: Modified in both, but differently. */
f953831e 1447 const char *reason = "content";
6d297f81 1448 struct merge_file_info mfi;
3af244ca
JS
1449 struct diff_filespec o, a, b;
1450
f953831e
JS
1451 if (!o_sha) {
1452 reason = "add/add";
1453 o_sha = (unsigned char *)null_sha1;
1454 }
89f40be2 1455 output(2, "Auto-merged %s", path);
3af244ca 1456 o.path = a.path = b.path = (char *)path;
8da71493 1457 hashcpy(o.sha1, o_sha);
5a753613 1458 o.mode = o_mode;
8da71493 1459 hashcpy(a.sha1, a_sha);
5a753613 1460 a.mode = a_mode;
8da71493 1461 hashcpy(b.sha1, b_sha);
5a753613 1462 b.mode = b_mode;
3af244ca
JS
1463
1464 mfi = merge_file(&o, &a, &b,
c1d20846 1465 branch1, branch2);
6d297f81 1466
3af244ca 1467 if (mfi.clean)
6d297f81
JS
1468 update_file(1, mfi.sha, mfi.mode, path);
1469 else {
5a753613 1470 clean_merge = 0;
8c3275ab 1471 output(1, "CONFLICT (%s): Merge conflict in %s",
f953831e 1472 reason, path);
6d297f81 1473
3af244ca 1474 if (index_only)
6d297f81
JS
1475 update_file(0, mfi.sha, mfi.mode, path);
1476 else
1477 update_file_flags(mfi.sha, mfi.mode, path,
5a753613 1478 0 /* update_cache */, 1 /* update_working_directory */);
6d297f81 1479 }
ac7f0f43
JH
1480 } else if (!o_sha && !a_sha && !b_sha) {
1481 /*
1482 * this entry was deleted altogether. a_mode == 0 means
1483 * we had that path and want to actively remove it.
1484 */
1485 remove_file(1, path, !a_mode);
6d297f81
JS
1486 } else
1487 die("Fatal merge failure, shouldn't happen.");
1488
5a753613 1489 return clean_merge;
6d297f81
JS
1490}
1491
3af244ca
JS
1492static int merge_trees(struct tree *head,
1493 struct tree *merge,
1494 struct tree *common,
c1d20846
JS
1495 const char *branch1,
1496 const char *branch2,
3af244ca 1497 struct tree **result)
6d297f81 1498{
3af244ca 1499 int code, clean;
68faf689
JH
1500
1501 if (subtree_merge) {
1502 merge = shift_tree_object(head, merge);
1503 common = shift_tree_object(head, common);
1504 }
1505
3af244ca 1506 if (sha_eq(common->object.sha1, merge->object.sha1)) {
8c3275ab 1507 output(0, "Already uptodate!");
3af244ca
JS
1508 *result = head;
1509 return 1;
6d297f81
JS
1510 }
1511
7a85b848 1512 code = git_merge_trees(index_only, common, head, merge);
6d297f81 1513
3af244ca 1514 if (code != 0)
6d297f81
JS
1515 die("merging of trees %s and %s failed",
1516 sha1_to_hex(head->object.sha1),
1517 sha1_to_hex(merge->object.sha1));
1518
8b944b56 1519 if (unmerged_index()) {
3af244ca
JS
1520 struct path_list *entries, *re_head, *re_merge;
1521 int i;
5a753613
JS
1522 path_list_clear(&current_file_set, 1);
1523 path_list_clear(&current_directory_set, 1);
3af244ca
JS
1524 get_files_dirs(head);
1525 get_files_dirs(merge);
6d297f81 1526
3af244ca 1527 entries = get_unmerged();
6d297f81
JS
1528 re_head = get_renames(head, common, head, merge, entries);
1529 re_merge = get_renames(merge, common, head, merge, entries);
3af244ca 1530 clean = process_renames(re_head, re_merge,
c1d20846 1531 branch1, branch2);
ad57cbca 1532 for (i = 0; i < entries->nr; i++) {
6d297f81
JS
1533 const char *path = entries->items[i].path;
1534 struct stage_data *e = entries->items[i].util;
3f6ee2d1
SP
1535 if (!e->processed
1536 && !process_entry(path, e, branch1, branch2))
3af244ca 1537 clean = 0;
6d297f81
JS
1538 }
1539
3af244ca
JS
1540 path_list_clear(re_merge, 0);
1541 path_list_clear(re_head, 0);
1542 path_list_clear(entries, 1);
6d297f81 1543
6d297f81 1544 }
1cf716a2
JH
1545 else
1546 clean = 1;
1547
8b944b56
JH
1548 if (index_only)
1549 *result = git_write_tree();
6d297f81 1550
3af244ca 1551 return clean;
6d297f81
JS
1552}
1553
8918b0c9
JS
1554static struct commit_list *reverse_commit_list(struct commit_list *list)
1555{
1556 struct commit_list *next = NULL, *current, *backup;
1557 for (current = list; current; current = backup) {
1558 backup = current->next;
1559 current->next = next;
1560 next = current;
1561 }
1562 return next;
1563}
1564
6d297f81
JS
1565/*
1566 * Merge the commits h1 and h2, return the resulting virtual
3dff5379 1567 * commit object and a flag indicating the cleanness of the merge.
6d297f81 1568 */
65ac6e9c
JH
1569static int merge(struct commit *h1,
1570 struct commit *h2,
1571 const char *branch1,
1572 const char *branch2,
8b944b56 1573 struct commit_list *ca,
65ac6e9c 1574 struct commit **result)
6d297f81 1575{
8b944b56 1576 struct commit_list *iter;
5a753613 1577 struct commit *merged_common_ancestors;
3af244ca
JS
1578 struct tree *mrtree;
1579 int clean;
6d297f81 1580
8c3275ab
SP
1581 if (show(4)) {
1582 output(4, "Merging:");
1583 output_commit_title(h1);
1584 output_commit_title(h2);
1585 }
6d297f81 1586
8b944b56
JH
1587 if (!ca) {
1588 ca = get_merge_bases(h1, h2, 1);
1589 ca = reverse_commit_list(ca);
1590 }
6d297f81 1591
8c3275ab
SP
1592 if (show(5)) {
1593 output(5, "found %u common ancestor(s):", commit_list_count(ca));
1594 for (iter = ca; iter; iter = iter->next)
1595 output_commit_title(iter->item);
1596 }
6d297f81 1597
5a753613 1598 merged_common_ancestors = pop_commit(&ca);
934d9a24
JS
1599 if (merged_common_ancestors == NULL) {
1600 /* if there is no common ancestor, make an empty tree */
1601 struct tree *tree = xcalloc(1, sizeof(struct tree));
934d9a24
JS
1602
1603 tree->object.parsed = 1;
1604 tree->object.type = OBJ_TREE;
21666f1a 1605 pretend_sha1_file(NULL, 0, OBJ_TREE, tree->object.sha1);
934d9a24
JS
1606 merged_common_ancestors = make_virtual_commit(tree, "ancestor");
1607 }
6d297f81 1608
6d297f81 1609 for (iter = ca; iter; iter = iter->next) {
63889639 1610 call_depth++;
3af244ca
JS
1611 /*
1612 * When the merge fails, the result contains files
1613 * with conflict markers. The cleanness flag is
3dff5379
PR
1614 * ignored, it was never actually used, as result of
1615 * merge_trees has always overwritten it: the committed
3af244ca
JS
1616 * "conflicts" were already resolved.
1617 */
8b944b56 1618 discard_cache();
5a753613 1619 merge(merged_common_ancestors, iter->item,
3af244ca
JS
1620 "Temporary merge branch 1",
1621 "Temporary merge branch 2",
3af244ca 1622 NULL,
5a753613 1623 &merged_common_ancestors);
63889639 1624 call_depth--;
6d297f81 1625
5a753613 1626 if (!merged_common_ancestors)
6d297f81
JS
1627 die("merge returned no commit");
1628 }
1629
8b944b56 1630 discard_cache();
63889639 1631 if (!call_depth) {
8b944b56 1632 read_cache();
6d297f81 1633 index_only = 0;
8b944b56 1634 } else
6d297f81 1635 index_only = 1;
6d297f81 1636
5a753613 1637 clean = merge_trees(h1->tree, h2->tree, merged_common_ancestors->tree,
c1d20846 1638 branch1, branch2, &mrtree);
6d297f81 1639
8b944b56 1640 if (index_only) {
3af244ca
JS
1641 *result = make_virtual_commit(mrtree, "merged tree");
1642 commit_list_insert(h1, &(*result)->parents);
1643 commit_list_insert(h2, &(*result)->parents->next);
8b944b56 1644 }
66a155bc 1645 flush_output();
3af244ca 1646 return clean;
6d297f81
JS
1647}
1648
7ba3c078
SP
1649static const char *better_branch_name(const char *branch)
1650{
1651 static char githead_env[8 + 40 + 1];
1652 char *name;
1653
1654 if (strlen(branch) != 40)
1655 return branch;
1656 sprintf(githead_env, "GITHEAD_%s", branch);
1657 name = getenv(githead_env);
1658 return name ? name : branch;
1659}
1660
6d297f81
JS
1661static struct commit *get_ref(const char *ref)
1662{
1663 unsigned char sha1[20];
1664 struct object *object;
1665
1666 if (get_sha1(ref, sha1))
1667 die("Could not resolve ref '%s'", ref);
1668 object = deref_tag(parse_object(sha1), ref, strlen(ref));
a970e84e
SP
1669 if (object->type == OBJ_TREE)
1670 return make_virtual_commit((struct tree*)object,
1671 better_branch_name(ref));
bf6d324e 1672 if (object->type != OBJ_COMMIT)
6d297f81
JS
1673 return NULL;
1674 if (parse_commit((struct commit *)object))
1675 die("Could not parse commit '%s'", sha1_to_hex(object->sha1));
1676 return (struct commit *)object;
1677}
1678
8c3275ab
SP
1679static int merge_config(const char *var, const char *value)
1680{
1681 if (!strcasecmp(var, "merge.verbosity")) {
1682 verbosity = git_config_int(var, value);
1683 return 0;
1684 }
1685 return git_default_config(var, value);
1686}
1687
6d297f81
JS
1688int main(int argc, char *argv[])
1689{
8b944b56 1690 static const char *bases[20];
6d297f81 1691 static unsigned bases_count = 0;
3af244ca
JS
1692 int i, clean;
1693 const char *branch1, *branch2;
1694 struct commit *result, *h1, *h2;
8b944b56
JH
1695 struct commit_list *ca = NULL;
1696 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
1697 int index_fd;
6d297f81 1698
68faf689
JH
1699 if (argv[0]) {
1700 int namelen = strlen(argv[0]);
1701 if (8 < namelen &&
1702 !strcmp(argv[0] + namelen - 8, "-subtree"))
1703 subtree_merge = 1;
1704 }
1705
8c3275ab
SP
1706 git_config(merge_config);
1707 if (getenv("GIT_MERGE_VERBOSITY"))
1708 verbosity = strtol(getenv("GIT_MERGE_VERBOSITY"), NULL, 10);
6d297f81
JS
1709
1710 if (argc < 4)
1711 die("Usage: %s <base>... -- <head> <remote> ...\n", argv[0]);
1712
6d297f81
JS
1713 for (i = 1; i < argc; ++i) {
1714 if (!strcmp(argv[i], "--"))
1715 break;
1716 if (bases_count < sizeof(bases)/sizeof(*bases))
1717 bases[bases_count++] = argv[i];
1718 }
1719 if (argc - i != 3) /* "--" "<head>" "<remote>" */
1720 die("Not handling anything other than two heads merge.");
ad57cbca 1721 if (verbosity >= 5)
3f6ee2d1 1722 buffer_output = 0;
6d297f81 1723
6d297f81
JS
1724 branch1 = argv[++i];
1725 branch2 = argv[++i];
6d297f81 1726
3af244ca
JS
1727 h1 = get_ref(branch1);
1728 h2 = get_ref(branch2);
6d297f81 1729
e0ec1819
SP
1730 branch1 = better_branch_name(branch1);
1731 branch2 = better_branch_name(branch2);
3f6ee2d1 1732
8c3275ab
SP
1733 if (show(3))
1734 printf("Merging %s with %s\n", branch1, branch2);
e0ec1819 1735
30ca07a2 1736 index_fd = hold_locked_index(lock, 1);
6d297f81 1737
8b944b56
JH
1738 for (i = 0; i < bases_count; i++) {
1739 struct commit *ancestor = get_ref(bases[i]);
1740 ca = commit_list_insert(ancestor, &ca);
1741 }
63889639 1742 clean = merge(h1, h2, branch1, branch2, ca, &result);
8b944b56
JH
1743
1744 if (active_cache_changed &&
1745 (write_cache(index_fd, active_cache, active_nr) ||
30ca07a2 1746 close(index_fd) || commit_locked_index(lock)))
8b944b56 1747 die ("unable to write %s", get_index_file());
6d297f81 1748
3af244ca 1749 return clean ? 0: 1;
6d297f81 1750}