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