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