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