blob: e34912683ca106524616673f96fff538af8447ed [file] [log] [blame]
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001/*
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 */
6#include "cache.h"
David Aguilar1c4b6602014-09-14 00:40:45 -07007#include "advice.h"
Michael Haggerty697cc8e2014-10-01 12:28:42 +02008#include "lockfile.h"
Miklos Vajna9047ebb2008-08-12 18:45:14 +02009#include "cache-tree.h"
10#include "commit.h"
11#include "blob.h"
12#include "builtin.h"
13#include "tree-walk.h"
14#include "diff.h"
15#include "diffcore.h"
16#include "tag.h"
17#include "unpack-trees.h"
18#include "string-list.h"
19#include "xdiff-interface.h"
20#include "ll-merge.h"
Miklos Vajna9047ebb2008-08-12 18:45:14 +020021#include "attr.h"
22#include "merge-recursive.h"
Shawn O. Pearce9800c0d2008-09-29 11:04:20 -070023#include "dir.h"
Heiko Voigt68d03e42010-07-07 15:39:13 +020024#include "submodule.h"
Miklos Vajna9047ebb2008-08-12 18:45:14 +020025
Johannes Schindelinbc9204d2016-08-01 13:44:37 +020026static void flush_output(struct merge_options *o)
27{
Johannes Schindelinf1e24262016-08-01 13:44:50 +020028 if (o->buffer_output < 2 && o->obuf.len) {
Johannes Schindelinbc9204d2016-08-01 13:44:37 +020029 fputs(o->obuf.buf, stdout);
30 strbuf_reset(&o->obuf);
31 }
32}
33
34static int err(struct merge_options *o, const char *err, ...)
35{
36 va_list params;
37
Johannes Schindelinf1e24262016-08-01 13:44:50 +020038 if (o->buffer_output < 2)
39 flush_output(o);
40 else {
41 strbuf_complete(&o->obuf, '\n');
42 strbuf_addstr(&o->obuf, "error: ");
43 }
Johannes Schindelinbc9204d2016-08-01 13:44:37 +020044 va_start(params, err);
45 strbuf_vaddf(&o->obuf, err, params);
46 va_end(params);
Johannes Schindelinf1e24262016-08-01 13:44:50 +020047 if (o->buffer_output > 1)
48 strbuf_addch(&o->obuf, '\n');
49 else {
50 error("%s", o->obuf.buf);
51 strbuf_reset(&o->obuf);
52 }
Johannes Schindelinbc9204d2016-08-01 13:44:37 +020053
54 return -1;
55}
56
Junio C Hamano85e51b72008-06-30 22:18:57 -070057static struct tree *shift_tree_object(struct tree *one, struct tree *two,
58 const char *subtree_shift)
Miklos Vajna9047ebb2008-08-12 18:45:14 +020059{
brian m. carlsonf2fd0762015-11-10 02:22:28 +000060 struct object_id shifted;
Miklos Vajna9047ebb2008-08-12 18:45:14 +020061
Junio C Hamano85e51b72008-06-30 22:18:57 -070062 if (!*subtree_shift) {
brian m. carlson82db3d42016-04-17 23:10:38 +000063 shift_tree(&one->object.oid, &two->object.oid, &shifted, 0);
Junio C Hamano85e51b72008-06-30 22:18:57 -070064 } else {
brian m. carlson82db3d42016-04-17 23:10:38 +000065 shift_tree_by(&one->object.oid, &two->object.oid, &shifted,
Junio C Hamano85e51b72008-06-30 22:18:57 -070066 subtree_shift);
67 }
brian m. carlsonf2fd0762015-11-10 02:22:28 +000068 if (!oidcmp(&two->object.oid, &shifted))
Miklos Vajna9047ebb2008-08-12 18:45:14 +020069 return two;
brian m. carlsonf2fd0762015-11-10 02:22:28 +000070 return lookup_tree(shifted.hash);
Miklos Vajna9047ebb2008-08-12 18:45:14 +020071}
72
Linus Torvalds2af202b2009-06-18 10:28:43 -070073static struct commit *make_virtual_commit(struct tree *tree, const char *comment)
Miklos Vajna9047ebb2008-08-12 18:45:14 +020074{
Jeff King10322a02014-06-10 17:39:11 -040075 struct commit *commit = alloc_commit_node();
Junio C Hamanoae8e4c92011-11-07 13:26:22 -080076
René Scharfea2571652016-08-13 14:16:04 +020077 set_merge_remote_desc(commit, comment, (struct object *)commit);
Miklos Vajna9047ebb2008-08-12 18:45:14 +020078 commit->tree = tree;
Miklos Vajna9047ebb2008-08-12 18:45:14 +020079 commit->object.parsed = 1;
80 return commit;
81}
82
83/*
84 * Since we use get_tree_entry(), which does not put the read object into
85 * the object pool, we cannot rely on a == b.
86 */
brian m. carlsonb4da9d62016-06-24 23:09:27 +000087static int oid_eq(const struct object_id *a, const struct object_id *b)
Miklos Vajna9047ebb2008-08-12 18:45:14 +020088{
89 if (!a && !b)
90 return 2;
brian m. carlsonb4da9d62016-06-24 23:09:27 +000091 return a && b && oidcmp(a, b) == 0;
Miklos Vajna9047ebb2008-08-12 18:45:14 +020092}
93
Elijah Newren25c39362010-09-20 02:28:53 -060094enum rename_type {
95 RENAME_NORMAL = 0,
96 RENAME_DELETE,
Elijah Newren4f66dad2011-08-11 23:20:08 -060097 RENAME_ONE_FILE_TO_ONE,
Elijah Newren461f5042011-08-11 23:20:15 -060098 RENAME_ONE_FILE_TO_TWO,
99 RENAME_TWO_FILES_TO_ONE
Elijah Newren25c39362010-09-20 02:28:53 -0600100};
101
Elijah Newren4f66dad2011-08-11 23:20:08 -0600102struct rename_conflict_info {
Elijah Newren25c39362010-09-20 02:28:53 -0600103 enum rename_type rename_type;
104 struct diff_filepair *pair1;
105 struct diff_filepair *pair2;
106 const char *branch1;
107 const char *branch2;
108 struct stage_data *dst_entry1;
109 struct stage_data *dst_entry2;
Elijah Newren232c6352011-08-11 23:20:16 -0600110 struct diff_filespec ren1_other;
111 struct diff_filespec ren2_other;
Elijah Newren25c39362010-09-20 02:28:53 -0600112};
113
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200114/*
115 * Since we want to write the index eventually, we cannot reuse the index
116 * for these (temporary) data.
117 */
Jonathan Nieder9cba13c2011-03-16 02:08:34 -0500118struct stage_data {
119 struct {
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200120 unsigned mode;
brian m. carlsonfd429e92016-06-24 23:09:25 +0000121 struct object_id oid;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200122 } stages[4];
Elijah Newren4f66dad2011-08-11 23:20:08 -0600123 struct rename_conflict_info *rename_conflict_info;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200124 unsigned processed:1;
125};
126
Elijah Newren4f66dad2011-08-11 23:20:08 -0600127static inline void setup_rename_conflict_info(enum rename_type rename_type,
128 struct diff_filepair *pair1,
129 struct diff_filepair *pair2,
130 const char *branch1,
131 const char *branch2,
132 struct stage_data *dst_entry1,
Elijah Newren232c6352011-08-11 23:20:16 -0600133 struct stage_data *dst_entry2,
134 struct merge_options *o,
135 struct stage_data *src_entry1,
136 struct stage_data *src_entry2)
Elijah Newren25c39362010-09-20 02:28:53 -0600137{
Elijah Newren4f66dad2011-08-11 23:20:08 -0600138 struct rename_conflict_info *ci = xcalloc(1, sizeof(struct rename_conflict_info));
Elijah Newren25c39362010-09-20 02:28:53 -0600139 ci->rename_type = rename_type;
140 ci->pair1 = pair1;
141 ci->branch1 = branch1;
142 ci->branch2 = branch2;
143
144 ci->dst_entry1 = dst_entry1;
Elijah Newren4f66dad2011-08-11 23:20:08 -0600145 dst_entry1->rename_conflict_info = ci;
Elijah Newren25c39362010-09-20 02:28:53 -0600146 dst_entry1->processed = 0;
147
148 assert(!pair2 == !dst_entry2);
149 if (dst_entry2) {
150 ci->dst_entry2 = dst_entry2;
151 ci->pair2 = pair2;
Elijah Newren4f66dad2011-08-11 23:20:08 -0600152 dst_entry2->rename_conflict_info = ci;
Elijah Newren25c39362010-09-20 02:28:53 -0600153 }
Elijah Newren232c6352011-08-11 23:20:16 -0600154
155 if (rename_type == RENAME_TWO_FILES_TO_ONE) {
156 /*
157 * For each rename, there could have been
158 * modifications on the side of history where that
159 * file was not renamed.
160 */
161 int ostage1 = o->branch1 == branch1 ? 3 : 2;
162 int ostage2 = ostage1 ^ 1;
163
164 ci->ren1_other.path = pair1->one->path;
brian m. carlsonfd429e92016-06-24 23:09:25 +0000165 oidcpy(&ci->ren1_other.oid, &src_entry1->stages[ostage1].oid);
Elijah Newren232c6352011-08-11 23:20:16 -0600166 ci->ren1_other.mode = src_entry1->stages[ostage1].mode;
167
168 ci->ren2_other.path = pair2->one->path;
brian m. carlsonfd429e92016-06-24 23:09:25 +0000169 oidcpy(&ci->ren2_other.oid, &src_entry2->stages[ostage2].oid);
Elijah Newren232c6352011-08-11 23:20:16 -0600170 ci->ren2_other.mode = src_entry2->stages[ostage2].mode;
Elijah Newren25c39362010-09-20 02:28:53 -0600171 }
172}
173
Miklos Vajna8a2fce12008-08-25 16:25:57 +0200174static int show(struct merge_options *o, int v)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200175{
Miklos Vajna50336392008-09-02 23:30:09 +0200176 return (!o->call_depth && o->verbosity >= v) || o->verbosity >= 5;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200177}
178
Tarmigan Casebolt28bea9e2009-11-14 13:33:13 -0800179__attribute__((format (printf, 3, 4)))
Miklos Vajna8a2fce12008-08-25 16:25:57 +0200180static void output(struct merge_options *o, int v, const char *fmt, ...)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200181{
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200182 va_list ap;
183
Miklos Vajna8a2fce12008-08-25 16:25:57 +0200184 if (!show(o, v))
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200185 return;
186
René Scharfe415792e2014-09-07 09:06:42 +0200187 strbuf_addchars(&o->obuf, ' ', o->call_depth * 2);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200188
189 va_start(ap, fmt);
Jeff Kingebeb6092011-02-25 23:08:53 -0600190 strbuf_vaddf(&o->obuf, fmt, ap);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200191 va_end(ap);
192
René Scharfe294b2682014-07-10 10:54:24 +0200193 strbuf_addch(&o->obuf, '\n');
Miklos Vajna8a2fce12008-08-25 16:25:57 +0200194 if (!o->buffer_output)
Miklos Vajnac7d84922008-09-03 02:30:03 +0200195 flush_output(o);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200196}
197
Miklos Vajna50336392008-09-02 23:30:09 +0200198static void output_commit_title(struct merge_options *o, struct commit *commit)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200199{
Johannes Schindelindde75cb2016-08-01 13:44:45 +0200200 strbuf_addchars(&o->obuf, ' ', o->call_depth * 2);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200201 if (commit->util)
Johannes Schindelindde75cb2016-08-01 13:44:45 +0200202 strbuf_addf(&o->obuf, "virtual %s\n",
203 merge_remote_util(commit)->name);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200204 else {
Johannes Schindelindde75cb2016-08-01 13:44:45 +0200205 strbuf_addf(&o->obuf, "%s ",
206 find_unique_abbrev(commit->object.oid.hash,
207 DEFAULT_ABBREV));
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200208 if (parse_commit(commit) != 0)
Johannes Schindelindde75cb2016-08-01 13:44:45 +0200209 strbuf_addf(&o->obuf, _("(bad commit)\n"));
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200210 else {
Christian Couder49b71202010-07-22 15:18:34 +0200211 const char *title;
Jeff King8597ea32014-06-10 17:44:13 -0400212 const char *msg = get_commit_buffer(commit, NULL);
Jeff Kingbc6b8fc2014-06-10 17:41:51 -0400213 int len = find_commit_subject(msg, &title);
Christian Couder49b71202010-07-22 15:18:34 +0200214 if (len)
Johannes Schindelindde75cb2016-08-01 13:44:45 +0200215 strbuf_addf(&o->obuf, "%.*s\n", len, title);
Jeff Kingbc6b8fc2014-06-10 17:41:51 -0400216 unuse_commit_buffer(commit, msg);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200217 }
218 }
Johannes Schindelindde75cb2016-08-01 13:44:45 +0200219 flush_output(o);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200220}
221
Johannes Schindelinbc9204d2016-08-01 13:44:37 +0200222static int add_cacheinfo(struct merge_options *o,
223 unsigned int mode, const struct object_id *oid,
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200224 const char *path, int stage, int refresh, int options)
225{
226 struct cache_entry *ce;
Junio C Hamano1335d762016-07-08 10:59:15 -0700227 int ret;
228
Junio C Hamano21bed622016-07-25 14:13:38 -0700229 ce = make_cache_entry(mode, oid ? oid->hash : null_sha1, path, stage, 0);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200230 if (!ce)
Johannes Schindelinbc9204d2016-08-01 13:44:37 +0200231 return err(o, _("addinfo_cache failed for path '%s'"), path);
Junio C Hamano1335d762016-07-08 10:59:15 -0700232
233 ret = add_cache_entry(ce, options);
234 if (refresh) {
235 struct cache_entry *nce;
236
237 nce = refresh_cache_entry(ce, CE_MATCH_REFRESH | CE_MATCH_IGNORE_MISSING);
238 if (nce != ce)
239 ret = add_cache_entry(nce, options);
240 }
241 return ret;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200242}
243
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200244static void init_tree_desc_from_tree(struct tree_desc *desc, struct tree *tree)
245{
246 parse_tree(tree);
247 init_tree_desc(desc, tree->buffer, tree->size);
248}
249
250static int git_merge_trees(int index_only,
251 struct tree *common,
252 struct tree *head,
253 struct tree *merge)
254{
255 int rc;
256 struct tree_desc t[3];
257 struct unpack_trees_options opts;
258
259 memset(&opts, 0, sizeof(opts));
260 if (index_only)
261 opts.index_only = 1;
262 else
263 opts.update = 1;
264 opts.merge = 1;
265 opts.head_idx = 2;
266 opts.fn = threeway_merge;
267 opts.src_index = &the_index;
268 opts.dst_index = &the_index;
Matthieu Moye2940302010-09-02 13:57:34 +0200269 setup_unpack_trees_porcelain(&opts, "merge");
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200270
271 init_tree_desc_from_tree(t+0, common);
272 init_tree_desc_from_tree(t+1, head);
273 init_tree_desc_from_tree(t+2, merge);
274
275 rc = unpack_trees(3, t, &opts);
276 cache_tree_free(&active_cache_tree);
277 return rc;
278}
279
Miklos Vajna8a2fce12008-08-25 16:25:57 +0200280struct tree *write_tree_from_memory(struct merge_options *o)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200281{
282 struct tree *result = NULL;
283
284 if (unmerged_cache()) {
285 int i;
Junio C Hamano19c6a4f2010-01-21 16:38:56 -0800286 fprintf(stderr, "BUG: There are unmerged index entries:\n");
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200287 for (i = 0; i < active_nr; i++) {
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700288 const struct cache_entry *ce = active_cache[i];
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200289 if (ce_stage(ce))
Elijah Newrenc43ba422011-08-11 23:19:49 -0600290 fprintf(stderr, "BUG: %d %.*s\n", ce_stage(ce),
Junio C Hamano19c6a4f2010-01-21 16:38:56 -0800291 (int)ce_namelen(ce), ce->name);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200292 }
Johannes Schindelinef1177d12016-07-26 18:05:50 +0200293 die("BUG: unmerged index entries in merge-recursive.c");
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200294 }
295
296 if (!active_cache_tree)
297 active_cache_tree = cache_tree();
298
299 if (!cache_tree_fully_valid(active_cache_tree) &&
Johannes Schindelin60033032016-07-26 18:06:26 +0200300 cache_tree_update(&the_index, 0) < 0) {
Johannes Schindelinbc9204d2016-08-01 13:44:37 +0200301 err(o, _("error building trees"));
Johannes Schindelin60033032016-07-26 18:06:26 +0200302 return NULL;
303 }
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200304
305 result = lookup_tree(active_cache_tree->sha1);
306
307 return result;
308}
309
310static int save_files_dirs(const unsigned char *sha1,
Nguyễn Thái Ngọc Duy6a0b0b62014-11-30 16:05:00 +0700311 struct strbuf *base, const char *path,
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200312 unsigned int mode, int stage, void *context)
313{
Nguyễn Thái Ngọc Duy6a0b0b62014-11-30 16:05:00 +0700314 int baselen = base->len;
Miklos Vajna696ee232008-09-03 19:08:56 +0200315 struct merge_options *o = context;
316
Nguyễn Thái Ngọc Duy6a0b0b62014-11-30 16:05:00 +0700317 strbuf_addstr(base, path);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200318
319 if (S_ISDIR(mode))
Nguyễn Thái Ngọc Duy6a0b0b62014-11-30 16:05:00 +0700320 string_list_insert(&o->current_directory_set, base->buf);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200321 else
Nguyễn Thái Ngọc Duy6a0b0b62014-11-30 16:05:00 +0700322 string_list_insert(&o->current_file_set, base->buf);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200323
Nguyễn Thái Ngọc Duy6a0b0b62014-11-30 16:05:00 +0700324 strbuf_setlen(base, baselen);
Lars Hjemlid3bee162009-01-25 01:52:05 +0100325 return (S_ISDIR(mode) ? READ_TREE_RECURSIVE : 0);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200326}
327
Miklos Vajna696ee232008-09-03 19:08:56 +0200328static int get_files_dirs(struct merge_options *o, struct tree *tree)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200329{
330 int n;
Nguyễn Thái Ngọc Duyf0096c02011-03-25 16:34:19 +0700331 struct pathspec match_all;
Nguyễn Thái Ngọc Duy9a087272013-07-14 15:35:59 +0700332 memset(&match_all, 0, sizeof(match_all));
Nguyễn Thái Ngọc Duyf0096c02011-03-25 16:34:19 +0700333 if (read_tree_recursive(tree, "", 0, 0, &match_all, save_files_dirs, o))
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200334 return 0;
Miklos Vajna696ee232008-09-03 19:08:56 +0200335 n = o->current_file_set.nr + o->current_directory_set.nr;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200336 return n;
337}
338
339/*
340 * Returns an index_entry instance which doesn't have to correspond to
341 * a real cache entry in Git's index.
342 */
343static struct stage_data *insert_stage_data(const char *path,
344 struct tree *o, struct tree *a, struct tree *b,
345 struct string_list *entries)
346{
347 struct string_list_item *item;
348 struct stage_data *e = xcalloc(1, sizeof(struct stage_data));
brian m. carlsoned1c9972015-11-10 02:22:29 +0000349 get_tree_entry(o->object.oid.hash, path,
brian m. carlsonfd429e92016-06-24 23:09:25 +0000350 e->stages[1].oid.hash, &e->stages[1].mode);
brian m. carlsoned1c9972015-11-10 02:22:29 +0000351 get_tree_entry(a->object.oid.hash, path,
brian m. carlsonfd429e92016-06-24 23:09:25 +0000352 e->stages[2].oid.hash, &e->stages[2].mode);
brian m. carlsoned1c9972015-11-10 02:22:29 +0000353 get_tree_entry(b->object.oid.hash, path,
brian m. carlsonfd429e92016-06-24 23:09:25 +0000354 e->stages[3].oid.hash, &e->stages[3].mode);
Julian Phillips78a395d2010-06-26 00:41:35 +0100355 item = string_list_insert(entries, path);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200356 item->util = e;
357 return e;
358}
359
360/*
361 * Create a dictionary mapping file names to stage_data objects. The
362 * dictionary contains one entry for every path with a non-zero stage entry.
363 */
364static struct string_list *get_unmerged(void)
365{
366 struct string_list *unmerged = xcalloc(1, sizeof(struct string_list));
367 int i;
368
369 unmerged->strdup_strings = 1;
370
371 for (i = 0; i < active_nr; i++) {
372 struct string_list_item *item;
373 struct stage_data *e;
Nguyễn Thái Ngọc Duy9c5e6c82013-07-09 22:29:00 +0700374 const struct cache_entry *ce = active_cache[i];
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200375 if (!ce_stage(ce))
376 continue;
377
Julian Phillipse8c8b712010-06-26 00:41:37 +0100378 item = string_list_lookup(unmerged, ce->name);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200379 if (!item) {
Julian Phillips78a395d2010-06-26 00:41:35 +0100380 item = string_list_insert(unmerged, ce->name);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200381 item->util = xcalloc(1, sizeof(struct stage_data));
382 }
383 e = item->util;
384 e->stages[ce_stage(ce)].mode = ce->ce_mode;
brian m. carlsonfd429e92016-06-24 23:09:25 +0000385 hashcpy(e->stages[ce_stage(ce)].oid.hash, ce->sha1);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200386 }
387
388 return unmerged;
389}
390
Elijah Newrenf0fd4d02011-08-11 23:19:56 -0600391static int string_list_df_name_compare(const void *a, const void *b)
Elijah Newrenef02b312010-09-20 02:29:09 -0600392{
Elijah Newrenf0fd4d02011-08-11 23:19:56 -0600393 const struct string_list_item *one = a;
394 const struct string_list_item *two = b;
395 int onelen = strlen(one->string);
396 int twolen = strlen(two->string);
397 /*
398 * Here we only care that entries for D/F conflicts are
399 * adjacent, in particular with the file of the D/F conflict
400 * appearing before files below the corresponding directory.
401 * The order of the rest of the list is irrelevant for us.
Elijah Newrenef02b312010-09-20 02:29:09 -0600402 *
Elijah Newrenf0fd4d02011-08-11 23:19:56 -0600403 * To achieve this, we sort with df_name_compare and provide
404 * the mode S_IFDIR so that D/F conflicts will sort correctly.
405 * We use the mode S_IFDIR for everything else for simplicity,
406 * since in other cases any changes in their order due to
407 * sorting cause no problems for us.
Elijah Newrenef02b312010-09-20 02:29:09 -0600408 */
Elijah Newrenf0fd4d02011-08-11 23:19:56 -0600409 int cmp = df_name_compare(one->string, onelen, S_IFDIR,
410 two->string, twolen, S_IFDIR);
411 /*
412 * Now that 'foo' and 'foo/bar' compare equal, we have to make sure
413 * that 'foo' comes before 'foo/bar'.
414 */
415 if (cmp)
416 return cmp;
417 return onelen - twolen;
418}
419
Elijah Newren70cc3d32011-08-11 23:19:58 -0600420static void record_df_conflict_files(struct merge_options *o,
421 struct string_list *entries)
Elijah Newrenef02b312010-09-20 02:29:09 -0600422{
Elijah Newren70cc3d32011-08-11 23:19:58 -0600423 /* If there is a D/F conflict and the file for such a conflict
Carlos Martín Nietof7d650c2011-09-20 22:25:57 +0200424 * currently exist in the working tree, we want to allow it to be
Elijah Newrenedd2faf2011-08-11 23:20:07 -0600425 * removed to make room for the corresponding directory if needed.
426 * The files underneath the directories of such D/F conflicts will
427 * be processed before the corresponding file involved in the D/F
428 * conflict. If the D/F directory ends up being removed by the
429 * merge, then we won't have to touch the D/F file. If the D/F
430 * directory needs to be written to the working copy, then the D/F
431 * file will simply be removed (in make_room_for_path()) to make
432 * room for the necessary paths. Note that if both the directory
433 * and the file need to be present, then the D/F file will be
434 * reinstated with a new unique name at the time it is processed.
Elijah Newrenef02b312010-09-20 02:29:09 -0600435 */
René Scharfeaf4941d2016-08-05 22:42:12 +0200436 struct string_list df_sorted_entries = STRING_LIST_INIT_NODUP;
Elijah Newrenef02b312010-09-20 02:29:09 -0600437 const char *last_file = NULL;
Junio C Hamanoc8516502010-10-21 07:34:33 -0700438 int last_len = 0;
Elijah Newrenef02b312010-09-20 02:29:09 -0600439 int i;
440
Elijah Newren0b30e812011-08-11 23:19:54 -0600441 /*
442 * If we're merging merge-bases, we don't want to bother with
443 * any working directory changes.
444 */
445 if (o->call_depth)
446 return;
447
Elijah Newrenf0fd4d02011-08-11 23:19:56 -0600448 /* Ensure D/F conflicts are adjacent in the entries list. */
Elijah Newrenef02b312010-09-20 02:29:09 -0600449 for (i = 0; i < entries->nr; i++) {
Elijah Newrenf701aae2011-08-12 20:23:51 -0600450 struct string_list_item *next = &entries->items[i];
451 string_list_append(&df_sorted_entries, next->string)->util =
452 next->util;
453 }
454 qsort(df_sorted_entries.items, entries->nr, sizeof(*entries->items),
Elijah Newrenf0fd4d02011-08-11 23:19:56 -0600455 string_list_df_name_compare);
456
Elijah Newren70cc3d32011-08-11 23:19:58 -0600457 string_list_clear(&o->df_conflict_file_set, 1);
Elijah Newrenf701aae2011-08-12 20:23:51 -0600458 for (i = 0; i < df_sorted_entries.nr; i++) {
459 const char *path = df_sorted_entries.items[i].string;
Elijah Newrenef02b312010-09-20 02:29:09 -0600460 int len = strlen(path);
Elijah Newrenf701aae2011-08-12 20:23:51 -0600461 struct stage_data *e = df_sorted_entries.items[i].util;
Elijah Newrenef02b312010-09-20 02:29:09 -0600462
463 /*
464 * Check if last_file & path correspond to a D/F conflict;
465 * i.e. whether path is last_file+'/'+<something>.
Elijah Newren70cc3d32011-08-11 23:19:58 -0600466 * If so, record that it's okay to remove last_file to make
467 * room for path and friends if needed.
Elijah Newrenef02b312010-09-20 02:29:09 -0600468 */
469 if (last_file &&
470 len > last_len &&
471 memcmp(path, last_file, last_len) == 0 &&
472 path[last_len] == '/') {
Elijah Newren70cc3d32011-08-11 23:19:58 -0600473 string_list_insert(&o->df_conflict_file_set, last_file);
Elijah Newrenef02b312010-09-20 02:29:09 -0600474 }
475
476 /*
477 * Determine whether path could exist as a file in the
478 * working directory as a possible D/F conflict. This
479 * will only occur when it exists in stage 2 as a
480 * file.
481 */
482 if (S_ISREG(e->stages[2].mode) || S_ISLNK(e->stages[2].mode)) {
483 last_file = path;
484 last_len = len;
Elijah Newrenef02b312010-09-20 02:29:09 -0600485 } else {
486 last_file = NULL;
487 }
488 }
Elijah Newrenf701aae2011-08-12 20:23:51 -0600489 string_list_clear(&df_sorted_entries, 0);
Elijah Newrenef02b312010-09-20 02:29:09 -0600490}
491
Jonathan Nieder9cba13c2011-03-16 02:08:34 -0500492struct rename {
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200493 struct diff_filepair *pair;
494 struct stage_data *src_entry;
495 struct stage_data *dst_entry;
496 unsigned processed:1;
497};
498
499/*
500 * Get information of all renames which occurred between 'o_tree' and
501 * 'tree'. We need the three trees in the merge ('o_tree', 'a_tree' and
502 * 'b_tree') to be able to associate the correct cache entries with
503 * the rename information. 'tree' is always equal to either a_tree or b_tree.
504 */
Miklos Vajna8a2fce12008-08-25 16:25:57 +0200505static struct string_list *get_renames(struct merge_options *o,
506 struct tree *tree,
507 struct tree *o_tree,
508 struct tree *a_tree,
509 struct tree *b_tree,
510 struct string_list *entries)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200511{
512 int i;
513 struct string_list *renames;
514 struct diff_options opts;
515
516 renames = xcalloc(1, sizeof(struct string_list));
Felipe Gonçalves Assisd2b11ec2016-02-17 01:15:25 -0200517 if (!o->detect_rename)
518 return renames;
519
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200520 diff_setup(&opts);
521 DIFF_OPT_SET(&opts, RECURSIVE);
Jeff King4f7cb992012-03-22 18:52:24 -0400522 DIFF_OPT_CLR(&opts, RENAME_EMPTY);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200523 opts.detect_rename = DIFF_DETECT_RENAME;
Miklos Vajna8a2fce12008-08-25 16:25:57 +0200524 opts.rename_limit = o->merge_rename_limit >= 0 ? o->merge_rename_limit :
525 o->diff_rename_limit >= 0 ? o->diff_rename_limit :
Jeff King92c57e52011-02-19 05:21:28 -0500526 1000;
Kevin Ballard10ae7522010-09-27 16:58:25 -0700527 opts.rename_score = o->rename_score;
Jeff King99bfc662011-02-20 04:53:21 -0500528 opts.show_rename_progress = o->show_rename_progress;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200529 opts.output_format = DIFF_FORMAT_NO_OUTPUT;
Thomas Rast28452652012-08-03 14:16:24 +0200530 diff_setup_done(&opts);
brian m. carlsoned1c9972015-11-10 02:22:29 +0000531 diff_tree_sha1(o_tree->object.oid.hash, tree->object.oid.hash, "", &opts);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200532 diffcore_std(&opts);
Jeff Kingbf0ab102011-02-19 05:20:51 -0500533 if (opts.needed_rename_limit > o->needed_rename_limit)
534 o->needed_rename_limit = opts.needed_rename_limit;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200535 for (i = 0; i < diff_queued_diff.nr; ++i) {
536 struct string_list_item *item;
537 struct rename *re;
538 struct diff_filepair *pair = diff_queued_diff.queue[i];
539 if (pair->status != 'R') {
540 diff_free_filepair(pair);
541 continue;
542 }
543 re = xmalloc(sizeof(*re));
544 re->processed = 0;
545 re->pair = pair;
Julian Phillipse8c8b712010-06-26 00:41:37 +0100546 item = string_list_lookup(entries, re->pair->one->path);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200547 if (!item)
548 re->src_entry = insert_stage_data(re->pair->one->path,
549 o_tree, a_tree, b_tree, entries);
550 else
551 re->src_entry = item->util;
552
Julian Phillipse8c8b712010-06-26 00:41:37 +0100553 item = string_list_lookup(entries, re->pair->two->path);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200554 if (!item)
555 re->dst_entry = insert_stage_data(re->pair->two->path,
556 o_tree, a_tree, b_tree, entries);
557 else
558 re->dst_entry = item->util;
Julian Phillips78a395d2010-06-26 00:41:35 +0100559 item = string_list_insert(renames, pair->one->path);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200560 item->util = re;
561 }
562 opts.output_format = DIFF_FORMAT_NO_OUTPUT;
563 diff_queued_diff.nr = 0;
564 diff_flush(&opts);
565 return renames;
566}
567
Johannes Schindelinbc9204d2016-08-01 13:44:37 +0200568static int update_stages(struct merge_options *opt, const char *path,
569 const struct diff_filespec *o,
Elijah Newren650467c2011-08-11 23:19:52 -0600570 const struct diff_filespec *a,
571 const struct diff_filespec *b)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200572{
Elijah Newrenf53d3972011-08-11 23:20:25 -0600573
574 /*
575 * NOTE: It is usually a bad idea to call update_stages on a path
576 * before calling update_file on that same path, since it can
577 * sometimes lead to spurious "refusing to lose untracked file..."
578 * messages from update_file (via make_room_for path via
579 * would_lose_untracked). Instead, reverse the order of the calls
580 * (executing update_file first and then update_stages).
581 */
Elijah Newren650467c2011-08-11 23:19:52 -0600582 int clear = 1;
583 int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_SKIP_DFCHECK;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200584 if (clear)
585 if (remove_file_from_cache(path))
586 return -1;
587 if (o)
Johannes Schindelinbc9204d2016-08-01 13:44:37 +0200588 if (add_cacheinfo(opt, o->mode, &o->oid, path, 1, 0, options))
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200589 return -1;
590 if (a)
Johannes Schindelinbc9204d2016-08-01 13:44:37 +0200591 if (add_cacheinfo(opt, a->mode, &a->oid, path, 2, 0, options))
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200592 return -1;
593 if (b)
Johannes Schindelinbc9204d2016-08-01 13:44:37 +0200594 if (add_cacheinfo(opt, b->mode, &b->oid, path, 3, 0, options))
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200595 return -1;
596 return 0;
597}
598
Elijah Newrenb8ddf162011-08-11 23:20:02 -0600599static void update_entry(struct stage_data *entry,
600 struct diff_filespec *o,
601 struct diff_filespec *a,
602 struct diff_filespec *b)
Elijah Newren2ff739f2010-09-20 02:28:54 -0600603{
Elijah Newren2ff739f2010-09-20 02:28:54 -0600604 entry->processed = 0;
605 entry->stages[1].mode = o->mode;
606 entry->stages[2].mode = a->mode;
607 entry->stages[3].mode = b->mode;
brian m. carlsonfd429e92016-06-24 23:09:25 +0000608 oidcpy(&entry->stages[1].oid, &o->oid);
609 oidcpy(&entry->stages[2].oid, &a->oid);
610 oidcpy(&entry->stages[3].oid, &b->oid);
Elijah Newren2ff739f2010-09-20 02:28:54 -0600611}
612
Miklos Vajnab7fa51d2008-09-02 23:53:47 +0200613static int remove_file(struct merge_options *o, int clean,
614 const char *path, int no_wd)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200615{
Miklos Vajnab7fa51d2008-09-02 23:53:47 +0200616 int update_cache = o->call_depth || clean;
617 int update_working_directory = !o->call_depth && !no_wd;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200618
619 if (update_cache) {
620 if (remove_file_from_cache(path))
621 return -1;
622 }
623 if (update_working_directory) {
David Turnerae352c72014-05-01 20:21:09 -0400624 if (ignore_case) {
625 struct cache_entry *ce;
626 ce = cache_file_exists(path, strlen(path), ignore_case);
627 if (ce && ce_stage(ce) == 0)
628 return 0;
629 }
Peter Collingbourne25755e82010-03-26 15:25:35 +0000630 if (remove_path(path))
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200631 return -1;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200632 }
633 return 0;
634}
635
Jeff King45bc1312014-06-19 17:30:26 -0400636/* add a string to a strbuf, but converting "/" to "_" */
637static void add_flattened_path(struct strbuf *out, const char *s)
638{
639 size_t i = out->len;
640 strbuf_addstr(out, s);
641 for (; i < out->len; i++)
642 if (out->buf[i] == '/')
643 out->buf[i] = '_';
644}
645
Miklos Vajna696ee232008-09-03 19:08:56 +0200646static char *unique_path(struct merge_options *o, const char *path, const char *branch)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200647{
Jeff King45bc1312014-06-19 17:30:26 -0400648 struct strbuf newpath = STRBUF_INIT;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200649 int suffix = 0;
Jeff King45bc1312014-06-19 17:30:26 -0400650 size_t base_len;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200651
Jeff King45bc1312014-06-19 17:30:26 -0400652 strbuf_addf(&newpath, "%s~", path);
653 add_flattened_path(&newpath, branch);
654
655 base_len = newpath.len;
656 while (string_list_has_string(&o->current_file_set, newpath.buf) ||
657 string_list_has_string(&o->current_directory_set, newpath.buf) ||
Elijah Newren8e1b62f2016-04-09 23:13:36 -0700658 (!o->call_depth && file_exists(newpath.buf))) {
Jeff King45bc1312014-06-19 17:30:26 -0400659 strbuf_setlen(&newpath, base_len);
660 strbuf_addf(&newpath, "_%d", suffix++);
661 }
662
663 string_list_insert(&o->current_file_set, newpath.buf);
664 return strbuf_detach(&newpath, NULL);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200665}
666
Elijah Newrenf2507b42011-08-11 23:19:57 -0600667static int dir_in_way(const char *path, int check_working_copy)
668{
Jeff Kingb4600fb2015-09-24 17:07:43 -0400669 int pos;
670 struct strbuf dirpath = STRBUF_INIT;
Elijah Newrenf2507b42011-08-11 23:19:57 -0600671 struct stat st;
672
Jeff Kingb4600fb2015-09-24 17:07:43 -0400673 strbuf_addstr(&dirpath, path);
674 strbuf_addch(&dirpath, '/');
Elijah Newrenf2507b42011-08-11 23:19:57 -0600675
Jeff Kingb4600fb2015-09-24 17:07:43 -0400676 pos = cache_name_pos(dirpath.buf, dirpath.len);
Elijah Newrenf2507b42011-08-11 23:19:57 -0600677
678 if (pos < 0)
679 pos = -1 - pos;
680 if (pos < active_nr &&
Jeff Kingb4600fb2015-09-24 17:07:43 -0400681 !strncmp(dirpath.buf, active_cache[pos]->name, dirpath.len)) {
682 strbuf_release(&dirpath);
Elijah Newrenf2507b42011-08-11 23:19:57 -0600683 return 1;
684 }
685
Jeff Kingb4600fb2015-09-24 17:07:43 -0400686 strbuf_release(&dirpath);
Elijah Newrenf2507b42011-08-11 23:19:57 -0600687 return check_working_copy && !lstat(path, &st) && S_ISDIR(st.st_mode);
688}
689
Elijah Newrenaacb82d2011-08-11 23:19:59 -0600690static int was_tracked(const char *path)
Junio C Hamano60c91182008-12-15 02:41:24 -0800691{
692 int pos = cache_name_pos(path, strlen(path));
693
Johannes Schindelinf8d83fb2016-07-26 18:05:57 +0200694 if (0 <= pos)
695 /* we have been tracking this path */
696 return 1;
697
698 /*
699 * Look for an unmerged entry for the path,
700 * specifically stage #2, which would indicate
701 * that "our" side before the merge started
702 * had the path tracked (and resulted in a conflict).
703 */
704 for (pos = -1 - pos;
705 pos < active_nr && !strcmp(path, active_cache[pos]->name);
706 pos++)
707 if (ce_stage(active_cache[pos]) == 2)
Elijah Newrenaacb82d2011-08-11 23:19:59 -0600708 return 1;
Elijah Newrenaacb82d2011-08-11 23:19:59 -0600709 return 0;
Junio C Hamano60c91182008-12-15 02:41:24 -0800710}
711
Elijah Newrenaacb82d2011-08-11 23:19:59 -0600712static int would_lose_untracked(const char *path)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200713{
Elijah Newrenaacb82d2011-08-11 23:19:59 -0600714 return !was_tracked(path) && file_exists(path);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200715}
716
Elijah Newrened0148a2011-08-11 23:20:01 -0600717static int make_room_for_path(struct merge_options *o, const char *path)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200718{
Elijah Newrened0148a2011-08-11 23:20:01 -0600719 int status, i;
Jiang Xin55653a62012-07-25 22:53:13 +0800720 const char *msg = _("failed to create path '%s'%s");
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200721
Elijah Newrened0148a2011-08-11 23:20:01 -0600722 /* Unlink any D/F conflict files that are in the way */
723 for (i = 0; i < o->df_conflict_file_set.nr; i++) {
724 const char *df_path = o->df_conflict_file_set.items[i].string;
725 size_t pathlen = strlen(path);
726 size_t df_pathlen = strlen(df_path);
727 if (df_pathlen < pathlen &&
728 path[df_pathlen] == '/' &&
729 strncmp(path, df_path, df_pathlen) == 0) {
730 output(o, 3,
Jiang Xin55653a62012-07-25 22:53:13 +0800731 _("Removing %s to make room for subdirectory\n"),
Elijah Newrened0148a2011-08-11 23:20:01 -0600732 df_path);
733 unlink(df_path);
734 unsorted_string_list_delete_item(&o->df_conflict_file_set,
735 i, 0);
736 break;
737 }
738 }
739
740 /* Make sure leading directories are created */
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200741 status = safe_create_leading_directories_const(path);
742 if (status) {
Johannes Schindelin60033032016-07-26 18:06:26 +0200743 if (status == SCLD_EXISTS)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200744 /* something else exists */
Johannes Schindelinbc9204d2016-08-01 13:44:37 +0200745 return err(o, msg, path, _(": perhaps a D/F conflict?"));
746 return err(o, msg, path, "");
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200747 }
748
Junio C Hamano60c91182008-12-15 02:41:24 -0800749 /*
750 * Do not unlink a file in the work tree if we are not
751 * tracking it.
752 */
753 if (would_lose_untracked(path))
Johannes Schindelinbc9204d2016-08-01 13:44:37 +0200754 return err(o, _("refusing to lose untracked file at '%s'"),
Junio C Hamano60c91182008-12-15 02:41:24 -0800755 path);
756
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200757 /* Successful unlink is good.. */
758 if (!unlink(path))
759 return 0;
760 /* .. and so is no existing file */
761 if (errno == ENOENT)
762 return 0;
763 /* .. but not some other error (who really cares what?) */
Johannes Schindelinbc9204d2016-08-01 13:44:37 +0200764 return err(o, msg, path, _(": perhaps a D/F conflict?"));
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200765}
766
Johannes Schindelin75456f92016-07-26 18:06:21 +0200767static int update_file_flags(struct merge_options *o,
768 const struct object_id *oid,
769 unsigned mode,
770 const char *path,
771 int update_cache,
772 int update_wd)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200773{
Johannes Schindelin60033032016-07-26 18:06:26 +0200774 int ret = 0;
775
Miklos Vajnab7fa51d2008-09-02 23:53:47 +0200776 if (o->call_depth)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200777 update_wd = 0;
778
779 if (update_wd) {
780 enum object_type type;
781 void *buf;
782 unsigned long size;
783
Heiko Voigt68d03e42010-07-07 15:39:13 +0200784 if (S_ISGITLINK(mode)) {
Junio C Hamano0c44c942009-04-29 11:08:18 -0700785 /*
786 * We may later decide to recursively descend into
787 * the submodule directory and update its index
788 * and/or work tree, but we do not do that now.
789 */
Heiko Voigt68d03e42010-07-07 15:39:13 +0200790 update_wd = 0;
Junio C Hamano0c44c942009-04-29 11:08:18 -0700791 goto update_index;
Heiko Voigt68d03e42010-07-07 15:39:13 +0200792 }
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200793
brian m. carlsonb4da9d62016-06-24 23:09:27 +0000794 buf = read_sha1_file(oid->hash, &type, &size);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200795 if (!buf)
Johannes Schindelinbc9204d2016-08-01 13:44:37 +0200796 return err(o, _("cannot read object %s '%s'"), oid_to_hex(oid), path);
Johannes Schindelin60033032016-07-26 18:06:26 +0200797 if (type != OBJ_BLOB) {
Johannes Schindelinbc9204d2016-08-01 13:44:37 +0200798 ret = err(o, _("blob expected for %s '%s'"), oid_to_hex(oid), path);
Johannes Schindelin60033032016-07-26 18:06:26 +0200799 goto free_buf;
800 }
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200801 if (S_ISREG(mode)) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500802 struct strbuf strbuf = STRBUF_INIT;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200803 if (convert_to_working_tree(path, buf, size, &strbuf)) {
804 free(buf);
805 size = strbuf.len;
806 buf = strbuf_detach(&strbuf, NULL);
807 }
808 }
809
Elijah Newrened0148a2011-08-11 23:20:01 -0600810 if (make_room_for_path(o, path) < 0) {
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200811 update_wd = 0;
Johannes Schindelin75456f92016-07-26 18:06:21 +0200812 goto free_buf;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200813 }
814 if (S_ISREG(mode) || (!has_symlinks && S_ISLNK(mode))) {
815 int fd;
816 if (mode & 0100)
817 mode = 0777;
818 else
819 mode = 0666;
820 fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);
Johannes Schindelin60033032016-07-26 18:06:26 +0200821 if (fd < 0) {
Johannes Schindelinbc9204d2016-08-01 13:44:37 +0200822 ret = err(o, _("failed to open '%s': %s"),
823 path, strerror(errno));
Johannes Schindelin60033032016-07-26 18:06:26 +0200824 goto free_buf;
825 }
Thomas Rastf633ea22012-08-03 14:16:25 +0200826 write_in_full(fd, buf, size);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200827 close(fd);
828 } else if (S_ISLNK(mode)) {
829 char *lnk = xmemdupz(buf, size);
830 safe_create_leading_directories_const(path);
831 unlink(path);
Alex Riesen304dcf22008-12-05 01:39:14 +0100832 if (symlink(lnk, path))
Johannes Schindelinbc9204d2016-08-01 13:44:37 +0200833 ret = err(o, _("failed to symlink '%s': %s"),
834 path, strerror(errno));
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200835 free(lnk);
836 } else
Johannes Schindelinbc9204d2016-08-01 13:44:37 +0200837 ret = err(o,
838 _("do not know what to do with %06o %s '%s'"),
839 mode, oid_to_hex(oid), path);
Johannes Schindelin75456f92016-07-26 18:06:21 +0200840 free_buf:
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200841 free(buf);
842 }
843 update_index:
Johannes Schindelin60033032016-07-26 18:06:26 +0200844 if (!ret && update_cache)
Johannes Schindelinbc9204d2016-08-01 13:44:37 +0200845 add_cacheinfo(o, mode, oid, path, 0, update_wd, ADD_CACHE_OK_TO_ADD);
Johannes Schindelin60033032016-07-26 18:06:26 +0200846 return ret;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200847}
848
Johannes Schindelin75456f92016-07-26 18:06:21 +0200849static int update_file(struct merge_options *o,
850 int clean,
851 const struct object_id *oid,
852 unsigned mode,
853 const char *path)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200854{
Johannes Schindelin75456f92016-07-26 18:06:21 +0200855 return update_file_flags(o, oid, mode, path, o->call_depth || clean, !o->call_depth);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200856}
857
858/* Low level file merging, update and removal */
859
Jonathan Nieder9cba13c2011-03-16 02:08:34 -0500860struct merge_file_info {
brian m. carlson9b561492016-06-24 23:09:26 +0000861 struct object_id oid;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200862 unsigned mode;
863 unsigned clean:1,
864 merge:1;
865};
866
Miklos Vajnab7fa51d2008-09-02 23:53:47 +0200867static int merge_3way(struct merge_options *o,
868 mmbuffer_t *result_buf,
Elijah Newren0c059422011-08-11 23:19:51 -0600869 const struct diff_filespec *one,
870 const struct diff_filespec *a,
871 const struct diff_filespec *b,
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200872 const char *branch1,
873 const char *branch2)
874{
875 mmfile_t orig, src1, src2;
Jonathan Nieder712516b2010-08-26 00:49:53 -0500876 struct ll_merge_options ll_opts = {0};
Jonathan Nieder4c5868f2010-03-20 19:41:38 -0500877 char *base_name, *name1, *name2;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200878 int merge_status;
Avery Pennarun8cc5b292009-11-25 21:23:55 -0500879
Jonathan Nieder712516b2010-08-26 00:49:53 -0500880 ll_opts.renormalize = o->renormalize;
Justin Frankel58a1ece2010-08-26 00:50:45 -0500881 ll_opts.xdl_opts = o->xdl_opts;
Jonathan Nieder712516b2010-08-26 00:49:53 -0500882
883 if (o->call_depth) {
884 ll_opts.virtual_ancestor = 1;
885 ll_opts.variant = 0;
886 } else {
Avery Pennarun8cc5b292009-11-25 21:23:55 -0500887 switch (o->recursive_variant) {
888 case MERGE_RECURSIVE_OURS:
Jonathan Nieder712516b2010-08-26 00:49:53 -0500889 ll_opts.variant = XDL_MERGE_FAVOR_OURS;
Avery Pennarun8cc5b292009-11-25 21:23:55 -0500890 break;
891 case MERGE_RECURSIVE_THEIRS:
Jonathan Nieder712516b2010-08-26 00:49:53 -0500892 ll_opts.variant = XDL_MERGE_FAVOR_THEIRS;
Avery Pennarun8cc5b292009-11-25 21:23:55 -0500893 break;
894 default:
Jonathan Nieder712516b2010-08-26 00:49:53 -0500895 ll_opts.variant = 0;
Avery Pennarun8cc5b292009-11-25 21:23:55 -0500896 break;
897 }
898 }
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200899
Jonathan Nieder4c5868f2010-03-20 19:41:38 -0500900 if (strcmp(a->path, b->path) ||
901 (o->ancestor != NULL && strcmp(a->path, one->path) != 0)) {
902 base_name = o->ancestor == NULL ? NULL :
Ramsay Jones4e2d0942012-09-04 18:31:14 +0100903 mkpathdup("%s:%s", o->ancestor, one->path);
904 name1 = mkpathdup("%s:%s", branch1, a->path);
905 name2 = mkpathdup("%s:%s", branch2, b->path);
Martin Renold606475f2009-07-01 22:18:04 +0200906 } else {
Jonathan Nieder4c5868f2010-03-20 19:41:38 -0500907 base_name = o->ancestor == NULL ? NULL :
Ramsay Jones4e2d0942012-09-04 18:31:14 +0100908 mkpathdup("%s", o->ancestor);
909 name1 = mkpathdup("%s", branch1);
910 name2 = mkpathdup("%s", branch2);
Martin Renold606475f2009-07-01 22:18:04 +0200911 }
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200912
brian m. carlsona0d12c42016-06-24 23:09:23 +0000913 read_mmblob(&orig, one->oid.hash);
914 read_mmblob(&src1, a->oid.hash);
915 read_mmblob(&src2, b->oid.hash);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200916
Jonathan Nieder4c5868f2010-03-20 19:41:38 -0500917 merge_status = ll_merge(result_buf, a->path, &orig, base_name,
Jonathan Nieder712516b2010-08-26 00:49:53 -0500918 &src1, name1, &src2, name2, &ll_opts);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200919
Ramsay Jones4e2d0942012-09-04 18:31:14 +0100920 free(base_name);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200921 free(name1);
922 free(name2);
923 free(orig.ptr);
924 free(src1.ptr);
925 free(src2.ptr);
926 return merge_status;
927}
928
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +0200929static int merge_file_1(struct merge_options *o,
Elijah Newren6bdaead2011-08-11 23:20:12 -0600930 const struct diff_filespec *one,
931 const struct diff_filespec *a,
932 const struct diff_filespec *b,
933 const char *branch1,
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +0200934 const char *branch2,
935 struct merge_file_info *result)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200936{
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +0200937 result->merge = 0;
938 result->clean = 1;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200939
940 if ((S_IFMT & a->mode) != (S_IFMT & b->mode)) {
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +0200941 result->clean = 0;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200942 if (S_ISREG(a->mode)) {
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +0200943 result->mode = a->mode;
944 oidcpy(&result->oid, &a->oid);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200945 } else {
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +0200946 result->mode = b->mode;
947 oidcpy(&result->oid, &b->oid);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200948 }
949 } else {
brian m. carlsonb4da9d62016-06-24 23:09:27 +0000950 if (!oid_eq(&a->oid, &one->oid) && !oid_eq(&b->oid, &one->oid))
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +0200951 result->merge = 1;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200952
953 /*
954 * Merge modes
955 */
Miklos Vajnab7fa51d2008-09-02 23:53:47 +0200956 if (a->mode == b->mode || a->mode == one->mode)
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +0200957 result->mode = b->mode;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200958 else {
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +0200959 result->mode = a->mode;
Miklos Vajnab7fa51d2008-09-02 23:53:47 +0200960 if (b->mode != one->mode) {
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +0200961 result->clean = 0;
962 result->merge = 1;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200963 }
964 }
965
brian m. carlsonb4da9d62016-06-24 23:09:27 +0000966 if (oid_eq(&a->oid, &b->oid) || oid_eq(&a->oid, &one->oid))
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +0200967 oidcpy(&result->oid, &b->oid);
brian m. carlsonb4da9d62016-06-24 23:09:27 +0000968 else if (oid_eq(&b->oid, &one->oid))
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +0200969 oidcpy(&result->oid, &a->oid);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200970 else if (S_ISREG(a->mode)) {
971 mmbuffer_t result_buf;
Johannes Schindelin60033032016-07-26 18:06:26 +0200972 int ret = 0, merge_status;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200973
Miklos Vajnab7fa51d2008-09-02 23:53:47 +0200974 merge_status = merge_3way(o, &result_buf, one, a, b,
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200975 branch1, branch2);
976
977 if ((merge_status < 0) || !result_buf.ptr)
Johannes Schindelinbc9204d2016-08-01 13:44:37 +0200978 ret = err(o, _("Failed to execute internal merge"));
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200979
Johannes Schindelin60033032016-07-26 18:06:26 +0200980 if (!ret && write_sha1_file(result_buf.ptr, result_buf.size,
981 blob_type, result->oid.hash))
Johannes Schindelinbc9204d2016-08-01 13:44:37 +0200982 ret = err(o, _("Unable to add %s to database"),
983 a->path);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200984
985 free(result_buf.ptr);
Johannes Schindelin60033032016-07-26 18:06:26 +0200986 if (ret)
987 return ret;
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +0200988 result->clean = (merge_status == 0);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200989 } else if (S_ISGITLINK(a->mode)) {
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +0200990 result->clean = merge_submodule(result->oid.hash,
brian m. carlsona0d12c42016-06-24 23:09:23 +0000991 one->path,
992 one->oid.hash,
993 a->oid.hash,
994 b->oid.hash,
Brad King80988782011-10-13 08:59:05 -0400995 !o->call_depth);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200996 } else if (S_ISLNK(a->mode)) {
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +0200997 oidcpy(&result->oid, &a->oid);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200998
brian m. carlsonb4da9d62016-06-24 23:09:27 +0000999 if (!oid_eq(&a->oid, &b->oid))
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +02001000 result->clean = 0;
Johannes Schindelinef1177d12016-07-26 18:05:50 +02001001 } else
Johannes Schindelin7e97e102016-07-26 18:05:53 +02001002 die("BUG: unsupported object type in the tree");
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001003 }
1004
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +02001005 return 0;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001006}
1007
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +02001008static int merge_file_special_markers(struct merge_options *o,
Elijah Newrendac47412011-08-11 23:20:17 -06001009 const struct diff_filespec *one,
1010 const struct diff_filespec *a,
1011 const struct diff_filespec *b,
1012 const char *branch1,
1013 const char *filename1,
1014 const char *branch2,
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +02001015 const char *filename2,
1016 struct merge_file_info *mfi)
Elijah Newrendac47412011-08-11 23:20:17 -06001017{
1018 char *side1 = NULL;
1019 char *side2 = NULL;
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +02001020 int ret;
Elijah Newrendac47412011-08-11 23:20:17 -06001021
Jeff King28310182014-06-19 17:24:33 -04001022 if (filename1)
1023 side1 = xstrfmt("%s:%s", branch1, filename1);
1024 if (filename2)
1025 side2 = xstrfmt("%s:%s", branch2, filename2);
Elijah Newrendac47412011-08-11 23:20:17 -06001026
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +02001027 ret = merge_file_1(o, one, a, b,
1028 side1 ? side1 : branch1,
1029 side2 ? side2 : branch2, mfi);
Elijah Newrendac47412011-08-11 23:20:17 -06001030 free(side1);
1031 free(side2);
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +02001032 return ret;
Elijah Newrendac47412011-08-11 23:20:17 -06001033}
1034
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +02001035static int merge_file_one(struct merge_options *o,
Elijah Newren6bdaead2011-08-11 23:20:12 -06001036 const char *path,
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001037 const struct object_id *o_oid, int o_mode,
1038 const struct object_id *a_oid, int a_mode,
1039 const struct object_id *b_oid, int b_mode,
Elijah Newren6bdaead2011-08-11 23:20:12 -06001040 const char *branch1,
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +02001041 const char *branch2,
1042 struct merge_file_info *mfi)
Elijah Newren6bdaead2011-08-11 23:20:12 -06001043{
1044 struct diff_filespec one, a, b;
1045
1046 one.path = a.path = b.path = (char *)path;
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001047 oidcpy(&one.oid, o_oid);
Elijah Newren6bdaead2011-08-11 23:20:12 -06001048 one.mode = o_mode;
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001049 oidcpy(&a.oid, a_oid);
Elijah Newren6bdaead2011-08-11 23:20:12 -06001050 a.mode = a_mode;
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001051 oidcpy(&b.oid, b_oid);
Elijah Newren6bdaead2011-08-11 23:20:12 -06001052 b.mode = b_mode;
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +02001053 return merge_file_1(o, &one, &a, &b, branch1, branch2, mfi);
Elijah Newren6bdaead2011-08-11 23:20:12 -06001054}
1055
Johannes Schindelin75456f92016-07-26 18:06:21 +02001056static int handle_change_delete(struct merge_options *o,
Elijah Newrenb7033252011-08-11 23:20:19 -06001057 const char *path,
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001058 const struct object_id *o_oid, int o_mode,
1059 const struct object_id *a_oid, int a_mode,
1060 const struct object_id *b_oid, int b_mode,
Elijah Newrenb7033252011-08-11 23:20:19 -06001061 const char *change, const char *change_past)
1062{
1063 char *renamed = NULL;
Johannes Schindelin75456f92016-07-26 18:06:21 +02001064 int ret = 0;
Elijah Newrenb7033252011-08-11 23:20:19 -06001065 if (dir_in_way(path, !o->call_depth)) {
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001066 renamed = unique_path(o, path, a_oid ? o->branch1 : o->branch2);
Elijah Newrenb7033252011-08-11 23:20:19 -06001067 }
1068
1069 if (o->call_depth) {
1070 /*
1071 * We cannot arbitrarily accept either a_sha or b_sha as
1072 * correct; since there is no true "middle point" between
1073 * them, simply reuse the base version for virtual merge base.
1074 */
Johannes Schindelin75456f92016-07-26 18:06:21 +02001075 ret = remove_file_from_cache(path);
1076 if (!ret)
1077 ret = update_file(o, 0, o_oid, o_mode,
1078 renamed ? renamed : path);
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001079 } else if (!a_oid) {
Jiang Xin55653a62012-07-25 22:53:13 +08001080 if (!renamed) {
1081 output(o, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1082 "and %s in %s. Version %s of %s left in tree."),
1083 change, path, o->branch1, change_past,
1084 o->branch2, o->branch2, path);
Johannes Schindelin75456f92016-07-26 18:06:21 +02001085 ret = update_file(o, 0, b_oid, b_mode, path);
Jiang Xin55653a62012-07-25 22:53:13 +08001086 } else {
1087 output(o, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1088 "and %s in %s. Version %s of %s left in tree at %s."),
1089 change, path, o->branch1, change_past,
1090 o->branch2, o->branch2, path, renamed);
Johannes Schindelin75456f92016-07-26 18:06:21 +02001091 ret = update_file(o, 0, b_oid, b_mode, renamed);
Jiang Xin55653a62012-07-25 22:53:13 +08001092 }
Elijah Newrenb7033252011-08-11 23:20:19 -06001093 } else {
Jiang Xin55653a62012-07-25 22:53:13 +08001094 if (!renamed) {
1095 output(o, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1096 "and %s in %s. Version %s of %s left in tree."),
1097 change, path, o->branch2, change_past,
1098 o->branch1, o->branch1, path);
1099 } else {
1100 output(o, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1101 "and %s in %s. Version %s of %s left in tree at %s."),
1102 change, path, o->branch2, change_past,
1103 o->branch1, o->branch1, path, renamed);
Johannes Schindelin75456f92016-07-26 18:06:21 +02001104 ret = update_file(o, 0, a_oid, a_mode, renamed);
Jiang Xin55653a62012-07-25 22:53:13 +08001105 }
Elijah Newren35a74ab2011-08-11 23:20:27 -06001106 /*
1107 * No need to call update_file() on path when !renamed, since
1108 * that would needlessly touch path. We could call
1109 * update_file_flags() with update_cache=0 and update_wd=0,
1110 * but that's a no-op.
1111 */
Elijah Newrenb7033252011-08-11 23:20:19 -06001112 }
1113 free(renamed);
Johannes Schindelin75456f92016-07-26 18:06:21 +02001114
1115 return ret;
Elijah Newrenb7033252011-08-11 23:20:19 -06001116}
1117
Johannes Schindelin75456f92016-07-26 18:06:21 +02001118static int conflict_rename_delete(struct merge_options *o,
Elijah Newren6ef2cb02010-09-20 02:28:50 -06001119 struct diff_filepair *pair,
1120 const char *rename_branch,
1121 const char *other_branch)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001122{
Elijah Newrene03acb82011-08-11 23:20:20 -06001123 const struct diff_filespec *orig = pair->one;
1124 const struct diff_filespec *dest = pair->two;
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001125 const struct object_id *a_oid = NULL;
1126 const struct object_id *b_oid = NULL;
Elijah Newrene03acb82011-08-11 23:20:20 -06001127 int a_mode = 0;
1128 int b_mode = 0;
Elijah Newren6ef2cb02010-09-20 02:28:50 -06001129
Elijah Newrene03acb82011-08-11 23:20:20 -06001130 if (rename_branch == o->branch1) {
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001131 a_oid = &dest->oid;
Elijah Newrene03acb82011-08-11 23:20:20 -06001132 a_mode = dest->mode;
1133 } else {
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001134 b_oid = &dest->oid;
Elijah Newrene03acb82011-08-11 23:20:20 -06001135 b_mode = dest->mode;
Elijah Newrena0de2f62010-09-20 02:29:05 -06001136 }
Elijah Newrene03acb82011-08-11 23:20:20 -06001137
Johannes Schindelin75456f92016-07-26 18:06:21 +02001138 if (handle_change_delete(o,
1139 o->call_depth ? orig->path : dest->path,
1140 &orig->oid, orig->mode,
1141 a_oid, a_mode,
1142 b_oid, b_mode,
1143 _("rename"), _("renamed")))
1144 return -1;
Elijah Newrenf53d3972011-08-11 23:20:25 -06001145
Johannes Schindelin75456f92016-07-26 18:06:21 +02001146 if (o->call_depth)
1147 return remove_file_from_cache(dest->path);
1148 else
Johannes Schindelinbc9204d2016-08-01 13:44:37 +02001149 return update_stages(o, dest->path, NULL,
Johannes Schindelin75456f92016-07-26 18:06:21 +02001150 rename_branch == o->branch1 ? dest : NULL,
1151 rename_branch == o->branch1 ? NULL : dest);
Elijah Newren6ef2cb02010-09-20 02:28:50 -06001152}
1153
Elijah Newren1ac91b32011-08-11 23:20:21 -06001154static struct diff_filespec *filespec_from_entry(struct diff_filespec *target,
1155 struct stage_data *entry,
1156 int stage)
1157{
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001158 struct object_id *oid = &entry->stages[stage].oid;
Elijah Newren1ac91b32011-08-11 23:20:21 -06001159 unsigned mode = entry->stages[stage].mode;
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001160 if (mode == 0 || is_null_oid(oid))
Elijah Newren1ac91b32011-08-11 23:20:21 -06001161 return NULL;
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001162 oidcpy(&target->oid, oid);
Elijah Newren1ac91b32011-08-11 23:20:21 -06001163 target->mode = mode;
1164 return target;
1165}
1166
Johannes Schindelin75456f92016-07-26 18:06:21 +02001167static int handle_file(struct merge_options *o,
Elijah Newren3672c972011-08-11 23:20:22 -06001168 struct diff_filespec *rename,
1169 int stage,
1170 struct rename_conflict_info *ci)
1171{
1172 char *dst_name = rename->path;
1173 struct stage_data *dst_entry;
1174 const char *cur_branch, *other_branch;
1175 struct diff_filespec other;
1176 struct diff_filespec *add;
Johannes Schindelin75456f92016-07-26 18:06:21 +02001177 int ret;
Elijah Newren3672c972011-08-11 23:20:22 -06001178
1179 if (stage == 2) {
1180 dst_entry = ci->dst_entry1;
1181 cur_branch = ci->branch1;
1182 other_branch = ci->branch2;
1183 } else {
1184 dst_entry = ci->dst_entry2;
1185 cur_branch = ci->branch2;
1186 other_branch = ci->branch1;
1187 }
1188
1189 add = filespec_from_entry(&other, dst_entry, stage ^ 1);
Elijah Newren3672c972011-08-11 23:20:22 -06001190 if (add) {
1191 char *add_name = unique_path(o, rename->path, other_branch);
Johannes Schindelin75456f92016-07-26 18:06:21 +02001192 if (update_file(o, 0, &add->oid, add->mode, add_name))
1193 return -1;
Elijah Newren3672c972011-08-11 23:20:22 -06001194
1195 remove_file(o, 0, rename->path, 0);
1196 dst_name = unique_path(o, rename->path, cur_branch);
1197 } else {
1198 if (dir_in_way(rename->path, !o->call_depth)) {
1199 dst_name = unique_path(o, rename->path, cur_branch);
Jiang Xin55653a62012-07-25 22:53:13 +08001200 output(o, 1, _("%s is a directory in %s adding as %s instead"),
Elijah Newren3672c972011-08-11 23:20:22 -06001201 rename->path, other_branch, dst_name);
1202 }
1203 }
Johannes Schindelin75456f92016-07-26 18:06:21 +02001204 if ((ret = update_file(o, 0, &rename->oid, rename->mode, dst_name)))
1205 ; /* fall through, do allow dst_name to be released */
1206 else if (stage == 2)
Johannes Schindelinbc9204d2016-08-01 13:44:37 +02001207 ret = update_stages(o, rename->path, NULL, rename, add);
Elijah Newrenf53d3972011-08-11 23:20:25 -06001208 else
Johannes Schindelinbc9204d2016-08-01 13:44:37 +02001209 ret = update_stages(o, rename->path, NULL, add, rename);
Elijah Newren3672c972011-08-11 23:20:22 -06001210
1211 if (dst_name != rename->path)
1212 free(dst_name);
Johannes Schindelin75456f92016-07-26 18:06:21 +02001213
1214 return ret;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001215}
1216
Johannes Schindelin75456f92016-07-26 18:06:21 +02001217static int conflict_rename_rename_1to2(struct merge_options *o,
Elijah Newrena99b7f22011-08-11 23:20:14 -06001218 struct rename_conflict_info *ci)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001219{
Elijah Newren09c01f82010-09-20 02:28:48 -06001220 /* One file was renamed in both branches, but to different names. */
Elijah Newrena99b7f22011-08-11 23:20:14 -06001221 struct diff_filespec *one = ci->pair1->one;
1222 struct diff_filespec *a = ci->pair1->two;
1223 struct diff_filespec *b = ci->pair2->two;
Elijah Newren07413c52010-09-20 02:29:00 -06001224
Jiang Xin55653a62012-07-25 22:53:13 +08001225 output(o, 1, _("CONFLICT (rename/rename): "
Elijah Newren4f66dad2011-08-11 23:20:08 -06001226 "Rename \"%s\"->\"%s\" in branch \"%s\" "
Jiang Xin55653a62012-07-25 22:53:13 +08001227 "rename \"%s\"->\"%s\" in \"%s\"%s"),
Elijah Newrena99b7f22011-08-11 23:20:14 -06001228 one->path, a->path, ci->branch1,
1229 one->path, b->path, ci->branch2,
Jiang Xin55653a62012-07-25 22:53:13 +08001230 o->call_depth ? _(" (left unresolved)") : "");
Miklos Vajnab7fa51d2008-09-02 23:53:47 +02001231 if (o->call_depth) {
Elijah Newrenc52ff852011-08-11 23:20:13 -06001232 struct merge_file_info mfi;
Elijah Newren6d630702011-08-11 23:20:29 -06001233 struct diff_filespec other;
1234 struct diff_filespec *add;
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +02001235 if (merge_file_one(o, one->path,
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001236 &one->oid, one->mode,
1237 &a->oid, a->mode,
1238 &b->oid, b->mode,
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +02001239 ci->branch1, ci->branch2, &mfi))
Johannes Schindelin75456f92016-07-26 18:06:21 +02001240 return -1;
1241
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001242 /*
Elijah Newrenc52ff852011-08-11 23:20:13 -06001243 * FIXME: For rename/add-source conflicts (if we could detect
1244 * such), this is wrong. We should instead find a unique
1245 * pathname and then either rename the add-source file to that
1246 * unique path, or use that unique path instead of src here.
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001247 */
Johannes Schindelin75456f92016-07-26 18:06:21 +02001248 if (update_file(o, 0, &mfi.oid, mfi.mode, one->path))
1249 return -1;
Elijah Newren6d630702011-08-11 23:20:29 -06001250
1251 /*
1252 * Above, we put the merged content at the merge-base's
1253 * path. Now we usually need to delete both a->path and
1254 * b->path. However, the rename on each side of the merge
1255 * could also be involved in a rename/add conflict. In
1256 * such cases, we should keep the added file around,
1257 * resolving the conflict at that path in its favor.
1258 */
1259 add = filespec_from_entry(&other, ci->dst_entry1, 2 ^ 1);
Johannes Schindelin75456f92016-07-26 18:06:21 +02001260 if (add) {
1261 if (update_file(o, 0, &add->oid, add->mode, a->path))
1262 return -1;
1263 }
Elijah Newren6d630702011-08-11 23:20:29 -06001264 else
1265 remove_file_from_cache(a->path);
1266 add = filespec_from_entry(&other, ci->dst_entry2, 3 ^ 1);
Johannes Schindelin75456f92016-07-26 18:06:21 +02001267 if (add) {
1268 if (update_file(o, 0, &add->oid, add->mode, b->path))
1269 return -1;
1270 }
Elijah Newren6d630702011-08-11 23:20:29 -06001271 else
1272 remove_file_from_cache(b->path);
Johannes Schindelin75456f92016-07-26 18:06:21 +02001273 } else if (handle_file(o, a, 2, ci) || handle_file(o, b, 3, ci))
1274 return -1;
1275
1276 return 0;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001277}
1278
Johannes Schindelin75456f92016-07-26 18:06:21 +02001279static int conflict_rename_rename_2to1(struct merge_options *o,
Elijah Newren461f5042011-08-11 23:20:15 -06001280 struct rename_conflict_info *ci)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001281{
Elijah Newren461f5042011-08-11 23:20:15 -06001282 /* Two files, a & b, were renamed to the same thing, c. */
1283 struct diff_filespec *a = ci->pair1->one;
1284 struct diff_filespec *b = ci->pair2->one;
1285 struct diff_filespec *c1 = ci->pair1->two;
1286 struct diff_filespec *c2 = ci->pair2->two;
1287 char *path = c1->path; /* == c2->path */
Elijah Newren434b8522011-08-11 23:20:18 -06001288 struct merge_file_info mfi_c1;
1289 struct merge_file_info mfi_c2;
Johannes Schindelin75456f92016-07-26 18:06:21 +02001290 int ret;
Elijah Newren461f5042011-08-11 23:20:15 -06001291
Jiang Xin55653a62012-07-25 22:53:13 +08001292 output(o, 1, _("CONFLICT (rename/rename): "
Elijah Newren461f5042011-08-11 23:20:15 -06001293 "Rename %s->%s in %s. "
Jiang Xin55653a62012-07-25 22:53:13 +08001294 "Rename %s->%s in %s"),
Elijah Newren461f5042011-08-11 23:20:15 -06001295 a->path, c1->path, ci->branch1,
1296 b->path, c2->path, ci->branch2);
1297
Elijah Newren8e1b62f2016-04-09 23:13:36 -07001298 remove_file(o, 1, a->path, o->call_depth || would_lose_untracked(a->path));
1299 remove_file(o, 1, b->path, o->call_depth || would_lose_untracked(b->path));
Elijah Newren461f5042011-08-11 23:20:15 -06001300
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +02001301 if (merge_file_special_markers(o, a, c1, &ci->ren1_other,
1302 o->branch1, c1->path,
1303 o->branch2, ci->ren1_other.path, &mfi_c1) ||
1304 merge_file_special_markers(o, b, &ci->ren2_other, c2,
1305 o->branch1, ci->ren2_other.path,
1306 o->branch2, c2->path, &mfi_c2))
Johannes Schindelin75456f92016-07-26 18:06:21 +02001307 return -1;
Elijah Newren434b8522011-08-11 23:20:18 -06001308
Elijah Newren0a6b8712011-08-11 23:20:04 -06001309 if (o->call_depth) {
Elijah Newren434b8522011-08-11 23:20:18 -06001310 /*
1311 * If mfi_c1.clean && mfi_c2.clean, then it might make
1312 * sense to do a two-way merge of those results. But, I
1313 * think in all cases, it makes sense to have the virtual
1314 * merge base just undo the renames; they can be detected
1315 * again later for the non-recursive merge.
1316 */
1317 remove_file(o, 0, path, 0);
Johannes Schindelin75456f92016-07-26 18:06:21 +02001318 ret = update_file(o, 0, &mfi_c1.oid, mfi_c1.mode, a->path);
1319 if (!ret)
1320 ret = update_file(o, 0, &mfi_c2.oid, mfi_c2.mode,
1321 b->path);
Elijah Newren0a6b8712011-08-11 23:20:04 -06001322 } else {
Elijah Newren461f5042011-08-11 23:20:15 -06001323 char *new_path1 = unique_path(o, path, ci->branch1);
1324 char *new_path2 = unique_path(o, path, ci->branch2);
Jiang Xin55653a62012-07-25 22:53:13 +08001325 output(o, 1, _("Renaming %s to %s and %s to %s instead"),
Elijah Newren461f5042011-08-11 23:20:15 -06001326 a->path, new_path1, b->path, new_path2);
Elijah Newren0a6b8712011-08-11 23:20:04 -06001327 remove_file(o, 0, path, 0);
Johannes Schindelin75456f92016-07-26 18:06:21 +02001328 ret = update_file(o, 0, &mfi_c1.oid, mfi_c1.mode, new_path1);
1329 if (!ret)
1330 ret = update_file(o, 0, &mfi_c2.oid, mfi_c2.mode,
1331 new_path2);
Elijah Newren0a6b8712011-08-11 23:20:04 -06001332 free(new_path2);
1333 free(new_path1);
1334 }
Johannes Schindelin75456f92016-07-26 18:06:21 +02001335
1336 return ret;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001337}
1338
Miklos Vajna8a2fce12008-08-25 16:25:57 +02001339static int process_renames(struct merge_options *o,
1340 struct string_list *a_renames,
1341 struct string_list *b_renames)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001342{
1343 int clean_merge = 1, i, j;
Thiago Farina183113a2010-07-04 16:46:19 -03001344 struct string_list a_by_dst = STRING_LIST_INIT_NODUP;
1345 struct string_list b_by_dst = STRING_LIST_INIT_NODUP;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001346 const struct rename *sre;
1347
1348 for (i = 0; i < a_renames->nr; i++) {
1349 sre = a_renames->items[i].util;
Julian Phillips78a395d2010-06-26 00:41:35 +01001350 string_list_insert(&a_by_dst, sre->pair->two->path)->util
Elijah Newren0a6b8712011-08-11 23:20:04 -06001351 = (void *)sre;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001352 }
1353 for (i = 0; i < b_renames->nr; i++) {
1354 sre = b_renames->items[i].util;
Julian Phillips78a395d2010-06-26 00:41:35 +01001355 string_list_insert(&b_by_dst, sre->pair->two->path)->util
Elijah Newren0a6b8712011-08-11 23:20:04 -06001356 = (void *)sre;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001357 }
1358
1359 for (i = 0, j = 0; i < a_renames->nr || j < b_renames->nr;) {
Benjamin Kramer8e24cba2009-03-15 22:01:20 +01001360 struct string_list *renames1, *renames2Dst;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001361 struct rename *ren1 = NULL, *ren2 = NULL;
1362 const char *branch1, *branch2;
1363 const char *ren1_src, *ren1_dst;
Elijah Newren461f5042011-08-11 23:20:15 -06001364 struct string_list_item *lookup;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001365
1366 if (i >= a_renames->nr) {
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001367 ren2 = b_renames->items[j++].util;
1368 } else if (j >= b_renames->nr) {
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001369 ren1 = a_renames->items[i++].util;
1370 } else {
Benjamin Kramer8e24cba2009-03-15 22:01:20 +01001371 int compare = strcmp(a_renames->items[i].string,
1372 b_renames->items[j].string);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001373 if (compare <= 0)
1374 ren1 = a_renames->items[i++].util;
1375 if (compare >= 0)
1376 ren2 = b_renames->items[j++].util;
1377 }
1378
1379 /* TODO: refactor, so that 1/2 are not needed */
1380 if (ren1) {
1381 renames1 = a_renames;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001382 renames2Dst = &b_by_dst;
Miklos Vajna8a2fce12008-08-25 16:25:57 +02001383 branch1 = o->branch1;
1384 branch2 = o->branch2;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001385 } else {
1386 struct rename *tmp;
1387 renames1 = b_renames;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001388 renames2Dst = &a_by_dst;
Miklos Vajna8a2fce12008-08-25 16:25:57 +02001389 branch1 = o->branch2;
1390 branch2 = o->branch1;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001391 tmp = ren2;
1392 ren2 = ren1;
1393 ren1 = tmp;
1394 }
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001395
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001396 if (ren1->processed)
1397 continue;
1398 ren1->processed = 1;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001399 ren1->dst_entry->processed = 1;
Elijah Newren7769a752011-08-11 23:20:05 -06001400 /* BUG: We should only mark src_entry as processed if we
1401 * are not dealing with a rename + add-source case.
1402 */
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001403 ren1->src_entry->processed = 1;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001404
1405 ren1_src = ren1->pair->one->path;
1406 ren1_dst = ren1->pair->two->path;
1407
1408 if (ren2) {
Elijah Newren461f5042011-08-11 23:20:15 -06001409 /* One file renamed on both sides */
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001410 const char *ren2_src = ren2->pair->one->path;
1411 const char *ren2_dst = ren2->pair->two->path;
Elijah Newren4f66dad2011-08-11 23:20:08 -06001412 enum rename_type rename_type;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001413 if (strcmp(ren1_src, ren2_src) != 0)
Johannes Schindelinef1177d12016-07-26 18:05:50 +02001414 die("BUG: ren1_src != ren2_src");
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001415 ren2->dst_entry->processed = 1;
1416 ren2->processed = 1;
1417 if (strcmp(ren1_dst, ren2_dst) != 0) {
Elijah Newren4f66dad2011-08-11 23:20:08 -06001418 rename_type = RENAME_ONE_FILE_TO_TWO;
Elijah Newren461f5042011-08-11 23:20:15 -06001419 clean_merge = 0;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001420 } else {
Elijah Newren4f66dad2011-08-11 23:20:08 -06001421 rename_type = RENAME_ONE_FILE_TO_ONE;
Elijah Newren7769a752011-08-11 23:20:05 -06001422 /* BUG: We should only remove ren1_src in
1423 * the base stage (think of rename +
1424 * add-source cases).
1425 */
Miklos Vajnab7fa51d2008-09-02 23:53:47 +02001426 remove_file(o, 1, ren1_src, 1);
Elijah Newrenb8ddf162011-08-11 23:20:02 -06001427 update_entry(ren1->dst_entry,
1428 ren1->pair->one,
1429 ren1->pair->two,
1430 ren2->pair->two);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001431 }
Elijah Newren4f66dad2011-08-11 23:20:08 -06001432 setup_rename_conflict_info(rename_type,
1433 ren1->pair,
1434 ren2->pair,
1435 branch1,
1436 branch2,
1437 ren1->dst_entry,
Elijah Newren232c6352011-08-11 23:20:16 -06001438 ren2->dst_entry,
1439 o,
1440 NULL,
1441 NULL);
Elijah Newren461f5042011-08-11 23:20:15 -06001442 } else if ((lookup = string_list_lookup(renames2Dst, ren1_dst))) {
1443 /* Two different files renamed to the same thing */
1444 char *ren2_dst;
1445 ren2 = lookup->util;
1446 ren2_dst = ren2->pair->two->path;
1447 if (strcmp(ren1_dst, ren2_dst) != 0)
Johannes Schindelinef1177d12016-07-26 18:05:50 +02001448 die("BUG: ren1_dst != ren2_dst");
Elijah Newren461f5042011-08-11 23:20:15 -06001449
1450 clean_merge = 0;
1451 ren2->processed = 1;
1452 /*
1453 * BUG: We should only mark src_entry as processed
1454 * if we are not dealing with a rename + add-source
1455 * case.
1456 */
1457 ren2->src_entry->processed = 1;
1458
1459 setup_rename_conflict_info(RENAME_TWO_FILES_TO_ONE,
1460 ren1->pair,
1461 ren2->pair,
1462 branch1,
1463 branch2,
1464 ren1->dst_entry,
Elijah Newren232c6352011-08-11 23:20:16 -06001465 ren2->dst_entry,
1466 o,
1467 ren1->src_entry,
1468 ren2->src_entry);
1469
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001470 } else {
1471 /* Renamed in 1, maybe changed in 2 */
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001472 /* we only use sha1 and mode of these */
1473 struct diff_filespec src_other, dst_other;
Elijah Newren41d70bd2010-09-20 02:28:47 -06001474 int try_merge;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001475
Elijah Newren41d70bd2010-09-20 02:28:47 -06001476 /*
1477 * unpack_trees loads entries from common-commit
1478 * into stage 1, from head-commit into stage 2, and
1479 * from merge-commit into stage 3. We keep track
1480 * of which side corresponds to the rename.
1481 */
1482 int renamed_stage = a_renames == renames1 ? 2 : 3;
1483 int other_stage = a_renames == renames1 ? 3 : 2;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001484
Elijah Newren7769a752011-08-11 23:20:05 -06001485 /* BUG: We should only remove ren1_src in the base
1486 * stage and in other_stage (think of rename +
1487 * add-source case).
1488 */
Elijah Newren531357a2011-08-11 23:20:03 -06001489 remove_file(o, 1, ren1_src,
1490 renamed_stage == 2 || !was_tracked(ren1_src));
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001491
brian m. carlsonfd429e92016-06-24 23:09:25 +00001492 oidcpy(&src_other.oid,
1493 &ren1->src_entry->stages[other_stage].oid);
Elijah Newren41d70bd2010-09-20 02:28:47 -06001494 src_other.mode = ren1->src_entry->stages[other_stage].mode;
brian m. carlsonfd429e92016-06-24 23:09:25 +00001495 oidcpy(&dst_other.oid,
1496 &ren1->dst_entry->stages[other_stage].oid);
Elijah Newren41d70bd2010-09-20 02:28:47 -06001497 dst_other.mode = ren1->dst_entry->stages[other_stage].mode;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001498 try_merge = 0;
1499
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001500 if (oid_eq(&src_other.oid, &null_oid)) {
Elijah Newren4f66dad2011-08-11 23:20:08 -06001501 setup_rename_conflict_info(RENAME_DELETE,
1502 ren1->pair,
1503 NULL,
1504 branch1,
1505 branch2,
1506 ren1->dst_entry,
Elijah Newren232c6352011-08-11 23:20:16 -06001507 NULL,
1508 o,
1509 NULL,
Elijah Newren4f66dad2011-08-11 23:20:08 -06001510 NULL);
Schalk, Kend5af5102010-09-01 13:15:32 -07001511 } else if ((dst_other.mode == ren1->pair->two->mode) &&
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001512 oid_eq(&dst_other.oid, &ren1->pair->two->oid)) {
Elijah Newren35a74ab2011-08-11 23:20:27 -06001513 /*
1514 * Added file on the other side identical to
1515 * the file being renamed: clean merge.
1516 * Also, there is no need to overwrite the
1517 * file already in the working copy, so call
1518 * update_file_flags() instead of
1519 * update_file().
1520 */
Johannes Schindelin75456f92016-07-26 18:06:21 +02001521 if (update_file_flags(o,
1522 &ren1->pair->two->oid,
1523 ren1->pair->two->mode,
1524 ren1_dst,
1525 1, /* update_cache */
1526 0 /* update_wd */))
1527 clean_merge = -1;
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001528 } else if (!oid_eq(&dst_other.oid, &null_oid)) {
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001529 clean_merge = 0;
1530 try_merge = 1;
Jiang Xin55653a62012-07-25 22:53:13 +08001531 output(o, 1, _("CONFLICT (rename/add): Rename %s->%s in %s. "
1532 "%s added in %s"),
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001533 ren1_src, ren1_dst, branch1,
1534 ren1_dst, branch2);
Junio C Hamanoc94736a2009-07-30 17:38:15 -07001535 if (o->call_depth) {
1536 struct merge_file_info mfi;
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +02001537 if (merge_file_one(o, ren1_dst, &null_oid, 0,
1538 &ren1->pair->two->oid,
1539 ren1->pair->two->mode,
1540 &dst_other.oid,
1541 dst_other.mode,
Johannes Schindelin75456f92016-07-26 18:06:21 +02001542 branch1, branch2, &mfi)) {
1543 clean_merge = -1;
1544 goto cleanup_and_return;
1545 }
Jiang Xin55653a62012-07-25 22:53:13 +08001546 output(o, 1, _("Adding merged %s"), ren1_dst);
Johannes Schindelin75456f92016-07-26 18:06:21 +02001547 if (update_file(o, 0, &mfi.oid,
1548 mfi.mode, ren1_dst))
1549 clean_merge = -1;
Elijah Newren2a669c32010-09-20 02:28:59 -06001550 try_merge = 0;
Junio C Hamanoc94736a2009-07-30 17:38:15 -07001551 } else {
Elijah Newren3d6b8e82011-08-11 23:19:53 -06001552 char *new_path = unique_path(o, ren1_dst, branch2);
Jiang Xin55653a62012-07-25 22:53:13 +08001553 output(o, 1, _("Adding as %s instead"), new_path);
Johannes Schindelin75456f92016-07-26 18:06:21 +02001554 if (update_file(o, 0, &dst_other.oid,
1555 dst_other.mode, new_path))
1556 clean_merge = -1;
Elijah Newren3d6b8e82011-08-11 23:19:53 -06001557 free(new_path);
Junio C Hamanoc94736a2009-07-30 17:38:15 -07001558 }
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001559 } else
1560 try_merge = 1;
1561
Johannes Schindelin75456f92016-07-26 18:06:21 +02001562 if (clean_merge < 0)
1563 goto cleanup_and_return;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001564 if (try_merge) {
Miklos Vajna8a2fce12008-08-25 16:25:57 +02001565 struct diff_filespec *one, *a, *b;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001566 src_other.path = (char *)ren1_src;
1567
Miklos Vajna8a2fce12008-08-25 16:25:57 +02001568 one = ren1->pair->one;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001569 if (a_renames == renames1) {
1570 a = ren1->pair->two;
1571 b = &src_other;
1572 } else {
1573 b = ren1->pair->two;
1574 a = &src_other;
1575 }
Elijah Newrenb8ddf162011-08-11 23:20:02 -06001576 update_entry(ren1->dst_entry, one, a, b);
Elijah Newren4f66dad2011-08-11 23:20:08 -06001577 setup_rename_conflict_info(RENAME_NORMAL,
1578 ren1->pair,
1579 NULL,
1580 branch1,
1581 NULL,
1582 ren1->dst_entry,
Elijah Newren232c6352011-08-11 23:20:16 -06001583 NULL,
1584 o,
1585 NULL,
Elijah Newren4f66dad2011-08-11 23:20:08 -06001586 NULL);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001587 }
1588 }
1589 }
Johannes Schindelin75456f92016-07-26 18:06:21 +02001590cleanup_and_return:
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001591 string_list_clear(&a_by_dst, 0);
1592 string_list_clear(&b_by_dst, 0);
1593
1594 return clean_merge;
1595}
1596
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001597static struct object_id *stage_oid(const struct object_id *oid, unsigned mode)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001598{
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001599 return (is_null_oid(oid) || mode == 0) ? NULL: (struct object_id *)oid;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001600}
1601
Johannes Schindelinbc9204d2016-08-01 13:44:37 +02001602static int read_oid_strbuf(struct merge_options *o,
1603 const struct object_id *oid, struct strbuf *dst)
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02001604{
1605 void *buf;
1606 enum object_type type;
1607 unsigned long size;
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001608 buf = read_sha1_file(oid->hash, &type, &size);
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02001609 if (!buf)
Johannes Schindelinbc9204d2016-08-01 13:44:37 +02001610 return err(o, _("cannot read object %s"), oid_to_hex(oid));
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02001611 if (type != OBJ_BLOB) {
1612 free(buf);
Johannes Schindelinbc9204d2016-08-01 13:44:37 +02001613 return err(o, _("object %s is not a blob"), oid_to_hex(oid));
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02001614 }
1615 strbuf_attach(dst, buf, size, size + 1);
1616 return 0;
1617}
1618
Johannes Schindelinbc9204d2016-08-01 13:44:37 +02001619static int blob_unchanged(struct merge_options *opt,
1620 const struct object_id *o_oid,
Jeff King72fac662015-10-26 17:39:39 -04001621 unsigned o_mode,
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001622 const struct object_id *a_oid,
Jeff King72fac662015-10-26 17:39:39 -04001623 unsigned a_mode,
Jonathan Nieder3e7589b2010-08-05 06:13:49 -05001624 int renormalize, const char *path)
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02001625{
1626 struct strbuf o = STRBUF_INIT;
1627 struct strbuf a = STRBUF_INIT;
1628 int ret = 0; /* assume changed for safety */
1629
Jeff King72fac662015-10-26 17:39:39 -04001630 if (a_mode != o_mode)
1631 return 0;
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001632 if (oid_eq(o_oid, a_oid))
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02001633 return 1;
Jonathan Nieder3e7589b2010-08-05 06:13:49 -05001634 if (!renormalize)
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02001635 return 0;
1636
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001637 assert(o_oid && a_oid);
Johannes Schindelinbc9204d2016-08-01 13:44:37 +02001638 if (read_oid_strbuf(opt, o_oid, &o) || read_oid_strbuf(opt, a_oid, &a))
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02001639 goto error_return;
1640 /*
1641 * Note: binary | is used so that both renormalizations are
1642 * performed. Comparison can be skipped if both files are
1643 * unchanged since their sha1s have already been compared.
1644 */
1645 if (renormalize_buffer(path, o.buf, o.len, &o) |
Stefan Beller422af492014-09-21 22:49:46 +02001646 renormalize_buffer(path, a.buf, a.len, &a))
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02001647 ret = (o.len == a.len && !memcmp(o.buf, a.buf, o.len));
1648
1649error_return:
1650 strbuf_release(&o);
1651 strbuf_release(&a);
1652 return ret;
1653}
1654
Johannes Schindelin75456f92016-07-26 18:06:21 +02001655static int handle_modify_delete(struct merge_options *o,
Elijah Newren5e3ce662010-09-20 02:28:51 -06001656 const char *path,
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001657 struct object_id *o_oid, int o_mode,
1658 struct object_id *a_oid, int a_mode,
1659 struct object_id *b_oid, int b_mode)
Elijah Newren5e3ce662010-09-20 02:28:51 -06001660{
Johannes Schindelin75456f92016-07-26 18:06:21 +02001661 return handle_change_delete(o,
1662 path,
1663 o_oid, o_mode,
1664 a_oid, a_mode,
1665 b_oid, b_mode,
1666 _("modify"), _("modified"));
Elijah Newren5e3ce662010-09-20 02:28:51 -06001667}
1668
Elijah Newren0c4918d2010-09-20 02:28:52 -06001669static int merge_content(struct merge_options *o,
1670 const char *path,
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001671 struct object_id *o_oid, int o_mode,
1672 struct object_id *a_oid, int a_mode,
1673 struct object_id *b_oid, int b_mode,
Elijah Newren4f66dad2011-08-11 23:20:08 -06001674 struct rename_conflict_info *rename_conflict_info)
Elijah Newren0c4918d2010-09-20 02:28:52 -06001675{
Jiang Xin55653a62012-07-25 22:53:13 +08001676 const char *reason = _("content");
Elijah Newren5b448b82011-08-11 23:20:10 -06001677 const char *path1 = NULL, *path2 = NULL;
Elijah Newren0c4918d2010-09-20 02:28:52 -06001678 struct merge_file_info mfi;
1679 struct diff_filespec one, a, b;
Elijah Newren4ab9a152010-09-20 02:29:07 -06001680 unsigned df_conflict_remains = 0;
Elijah Newren0c4918d2010-09-20 02:28:52 -06001681
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001682 if (!o_oid) {
Jiang Xin55653a62012-07-25 22:53:13 +08001683 reason = _("add/add");
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001684 o_oid = (struct object_id *)&null_oid;
Elijah Newren0c4918d2010-09-20 02:28:52 -06001685 }
1686 one.path = a.path = b.path = (char *)path;
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001687 oidcpy(&one.oid, o_oid);
Elijah Newren0c4918d2010-09-20 02:28:52 -06001688 one.mode = o_mode;
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001689 oidcpy(&a.oid, a_oid);
Elijah Newren0c4918d2010-09-20 02:28:52 -06001690 a.mode = a_mode;
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001691 oidcpy(&b.oid, b_oid);
Elijah Newren0c4918d2010-09-20 02:28:52 -06001692 b.mode = b_mode;
1693
Elijah Newren3c217c02011-08-11 23:20:09 -06001694 if (rename_conflict_info) {
Elijah Newren3c217c02011-08-11 23:20:09 -06001695 struct diff_filepair *pair1 = rename_conflict_info->pair1;
1696
1697 path1 = (o->branch1 == rename_conflict_info->branch1) ?
1698 pair1->two->path : pair1->one->path;
1699 /* If rename_conflict_info->pair2 != NULL, we are in
1700 * RENAME_ONE_FILE_TO_ONE case. Otherwise, we have a
1701 * normal rename.
1702 */
1703 path2 = (rename_conflict_info->pair2 ||
1704 o->branch2 == rename_conflict_info->branch1) ?
1705 pair1->two->path : pair1->one->path;
Elijah Newren3c217c02011-08-11 23:20:09 -06001706
1707 if (dir_in_way(path, !o->call_depth))
1708 df_conflict_remains = 1;
Elijah Newren4ab9a152010-09-20 02:29:07 -06001709 }
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +02001710 if (merge_file_special_markers(o, &one, &a, &b,
1711 o->branch1, path1,
1712 o->branch2, path2, &mfi))
1713 return -1;
Elijah Newren4ab9a152010-09-20 02:29:07 -06001714
1715 if (mfi.clean && !df_conflict_remains &&
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001716 oid_eq(&mfi.oid, a_oid) && mfi.mode == a_mode) {
Elijah Newren5b448b82011-08-11 23:20:10 -06001717 int path_renamed_outside_HEAD;
Jiang Xin55653a62012-07-25 22:53:13 +08001718 output(o, 3, _("Skipped %s (merged same as existing)"), path);
Elijah Newren5b448b82011-08-11 23:20:10 -06001719 /*
1720 * The content merge resulted in the same file contents we
1721 * already had. We can return early if those file contents
1722 * are recorded at the correct path (which may not be true
1723 * if the merge involves a rename).
1724 */
1725 path_renamed_outside_HEAD = !path2 || !strcmp(path, path2);
1726 if (!path_renamed_outside_HEAD) {
Johannes Schindelinbc9204d2016-08-01 13:44:37 +02001727 add_cacheinfo(o, mfi.mode, &mfi.oid, path,
Junio C Hamanod45b7f42011-09-23 15:21:01 -07001728 0, (!o->call_depth), 0);
Elijah Newren5b448b82011-08-11 23:20:10 -06001729 return mfi.clean;
1730 }
1731 } else
Jiang Xin55653a62012-07-25 22:53:13 +08001732 output(o, 2, _("Auto-merging %s"), path);
Elijah Newren0c4918d2010-09-20 02:28:52 -06001733
1734 if (!mfi.clean) {
1735 if (S_ISGITLINK(mfi.mode))
Jiang Xin55653a62012-07-25 22:53:13 +08001736 reason = _("submodule");
1737 output(o, 1, _("CONFLICT (%s): Merge conflict in %s"),
Elijah Newren0c4918d2010-09-20 02:28:52 -06001738 reason, path);
Elijah Newren4f66dad2011-08-11 23:20:08 -06001739 if (rename_conflict_info && !df_conflict_remains)
Johannes Schindelinbc9204d2016-08-01 13:44:37 +02001740 if (update_stages(o, path, &one, &a, &b))
Johannes Schindelin75456f92016-07-26 18:06:21 +02001741 return -1;
Elijah Newren0c4918d2010-09-20 02:28:52 -06001742 }
1743
Elijah Newren4ab9a152010-09-20 02:29:07 -06001744 if (df_conflict_remains) {
Elijah Newren3d6b8e82011-08-11 23:19:53 -06001745 char *new_path;
Elijah Newren51931bf2011-08-11 23:20:06 -06001746 if (o->call_depth) {
1747 remove_file_from_cache(path);
1748 } else {
Johannes Schindelin75456f92016-07-26 18:06:21 +02001749 if (!mfi.clean) {
Johannes Schindelinbc9204d2016-08-01 13:44:37 +02001750 if (update_stages(o, path, &one, &a, &b))
Johannes Schindelin75456f92016-07-26 18:06:21 +02001751 return -1;
1752 } else {
Elijah Newren51931bf2011-08-11 23:20:06 -06001753 int file_from_stage2 = was_tracked(path);
1754 struct diff_filespec merged;
brian m. carlson9b561492016-06-24 23:09:26 +00001755 oidcpy(&merged.oid, &mfi.oid);
Elijah Newren51931bf2011-08-11 23:20:06 -06001756 merged.mode = mfi.mode;
1757
Johannes Schindelinbc9204d2016-08-01 13:44:37 +02001758 if (update_stages(o, path, NULL,
Johannes Schindelin75456f92016-07-26 18:06:21 +02001759 file_from_stage2 ? &merged : NULL,
1760 file_from_stage2 ? NULL : &merged))
1761 return -1;
Elijah Newren51931bf2011-08-11 23:20:06 -06001762 }
1763
1764 }
Elijah Newren4f66dad2011-08-11 23:20:08 -06001765 new_path = unique_path(o, path, rename_conflict_info->branch1);
Jiang Xin55653a62012-07-25 22:53:13 +08001766 output(o, 1, _("Adding as %s instead"), new_path);
Johannes Schindelin75456f92016-07-26 18:06:21 +02001767 if (update_file(o, 0, &mfi.oid, mfi.mode, new_path)) {
1768 free(new_path);
1769 return -1;
1770 }
Elijah Newren3d6b8e82011-08-11 23:19:53 -06001771 free(new_path);
Elijah Newren51931bf2011-08-11 23:20:06 -06001772 mfi.clean = 0;
Johannes Schindelin75456f92016-07-26 18:06:21 +02001773 } else if (update_file(o, mfi.clean, &mfi.oid, mfi.mode, path))
1774 return -1;
Elijah Newren0c4918d2010-09-20 02:28:52 -06001775 return mfi.clean;
Elijah Newren0c4918d2010-09-20 02:28:52 -06001776}
1777
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001778/* Per entry merge function */
Miklos Vajna8a2fce12008-08-25 16:25:57 +02001779static int process_entry(struct merge_options *o,
1780 const char *path, struct stage_data *entry)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001781{
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001782 int clean_merge = 1;
Jonathan Nieder1bc0ab72010-08-05 06:15:32 -05001783 int normalize = o->renormalize;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001784 unsigned o_mode = entry->stages[1].mode;
1785 unsigned a_mode = entry->stages[2].mode;
1786 unsigned b_mode = entry->stages[3].mode;
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001787 struct object_id *o_oid = stage_oid(&entry->stages[1].oid, o_mode);
1788 struct object_id *a_oid = stage_oid(&entry->stages[2].oid, a_mode);
1789 struct object_id *b_oid = stage_oid(&entry->stages[3].oid, b_mode);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001790
Elijah Newren37348932010-07-09 07:10:53 -06001791 entry->processed = 1;
Elijah Newren4f66dad2011-08-11 23:20:08 -06001792 if (entry->rename_conflict_info) {
1793 struct rename_conflict_info *conflict_info = entry->rename_conflict_info;
Elijah Newren07413c52010-09-20 02:29:00 -06001794 switch (conflict_info->rename_type) {
Elijah Newren882fd112010-09-20 02:29:03 -06001795 case RENAME_NORMAL:
Elijah Newren4f66dad2011-08-11 23:20:08 -06001796 case RENAME_ONE_FILE_TO_ONE:
1797 clean_merge = merge_content(o, path,
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001798 o_oid, o_mode, a_oid, a_mode, b_oid, b_mode,
Elijah Newren4f66dad2011-08-11 23:20:08 -06001799 conflict_info);
Elijah Newren882fd112010-09-20 02:29:03 -06001800 break;
Elijah Newren3b130adf2010-09-20 02:29:02 -06001801 case RENAME_DELETE:
1802 clean_merge = 0;
Johannes Schindelin75456f92016-07-26 18:06:21 +02001803 if (conflict_rename_delete(o,
1804 conflict_info->pair1,
1805 conflict_info->branch1,
1806 conflict_info->branch2))
1807 clean_merge = -1;
Elijah Newren3b130adf2010-09-20 02:29:02 -06001808 break;
Elijah Newren07413c52010-09-20 02:29:00 -06001809 case RENAME_ONE_FILE_TO_TWO:
Elijah Newren07413c52010-09-20 02:29:00 -06001810 clean_merge = 0;
Johannes Schindelin75456f92016-07-26 18:06:21 +02001811 if (conflict_rename_rename_1to2(o, conflict_info))
1812 clean_merge = -1;
Elijah Newren07413c52010-09-20 02:29:00 -06001813 break;
Elijah Newren461f5042011-08-11 23:20:15 -06001814 case RENAME_TWO_FILES_TO_ONE:
1815 clean_merge = 0;
Johannes Schindelin75456f92016-07-26 18:06:21 +02001816 if (conflict_rename_rename_2to1(o, conflict_info))
1817 clean_merge = -1;
Elijah Newren461f5042011-08-11 23:20:15 -06001818 break;
Elijah Newren07413c52010-09-20 02:29:00 -06001819 default:
1820 entry->processed = 0;
1821 break;
1822 }
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001823 } else if (o_oid && (!a_oid || !b_oid)) {
Elijah Newren37348932010-07-09 07:10:53 -06001824 /* Case A: Deleted in one */
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001825 if ((!a_oid && !b_oid) ||
Johannes Schindelinbc9204d2016-08-01 13:44:37 +02001826 (!b_oid && blob_unchanged(o, o_oid, o_mode, a_oid, a_mode, normalize, path)) ||
1827 (!a_oid && blob_unchanged(o, o_oid, o_mode, b_oid, b_mode, normalize, path))) {
Elijah Newren37348932010-07-09 07:10:53 -06001828 /* Deleted in both or deleted in one and
1829 * unchanged in the other */
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001830 if (a_oid)
Jiang Xin55653a62012-07-25 22:53:13 +08001831 output(o, 2, _("Removing %s"), path);
Elijah Newren37348932010-07-09 07:10:53 -06001832 /* do not touch working file if it did not exist */
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001833 remove_file(o, 1, path, !a_oid);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001834 } else {
Elijah Newrenedd2faf2011-08-11 23:20:07 -06001835 /* Modify/delete; deleted side may have put a directory in the way */
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001836 clean_merge = 0;
Johannes Schindelin75456f92016-07-26 18:06:21 +02001837 if (handle_modify_delete(o, path, o_oid, o_mode,
1838 a_oid, a_mode, b_oid, b_mode))
1839 clean_merge = -1;
Miklos Vajna8a2fce12008-08-25 16:25:57 +02001840 }
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001841 } else if ((!o_oid && a_oid && !b_oid) ||
1842 (!o_oid && !a_oid && b_oid)) {
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001843 /* Case B: Added in one. */
Elijah Newrenedd2faf2011-08-11 23:20:07 -06001844 /* [nothing|directory] -> ([nothing|directory], file) */
Miklos Vajna8a2fce12008-08-25 16:25:57 +02001845
Elijah Newren9c0bbb52010-09-20 02:28:56 -06001846 const char *add_branch;
1847 const char *other_branch;
1848 unsigned mode;
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001849 const struct object_id *oid;
Elijah Newren9c0bbb52010-09-20 02:28:56 -06001850 const char *conf;
Elijah Newren37348932010-07-09 07:10:53 -06001851
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001852 if (a_oid) {
Elijah Newren9c0bbb52010-09-20 02:28:56 -06001853 add_branch = o->branch1;
1854 other_branch = o->branch2;
1855 mode = a_mode;
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001856 oid = a_oid;
Jiang Xin55653a62012-07-25 22:53:13 +08001857 conf = _("file/directory");
Elijah Newren9c0bbb52010-09-20 02:28:56 -06001858 } else {
1859 add_branch = o->branch2;
1860 other_branch = o->branch1;
1861 mode = b_mode;
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001862 oid = b_oid;
Jiang Xin55653a62012-07-25 22:53:13 +08001863 conf = _("directory/file");
Elijah Newren9c0bbb52010-09-20 02:28:56 -06001864 }
Elijah Newrenf2507b42011-08-11 23:19:57 -06001865 if (dir_in_way(path, !o->call_depth)) {
Elijah Newren3d6b8e82011-08-11 23:19:53 -06001866 char *new_path = unique_path(o, path, add_branch);
Elijah Newren9c0bbb52010-09-20 02:28:56 -06001867 clean_merge = 0;
Jiang Xin55653a62012-07-25 22:53:13 +08001868 output(o, 1, _("CONFLICT (%s): There is a directory with name %s in %s. "
1869 "Adding %s as %s"),
Elijah Newren9c0bbb52010-09-20 02:28:56 -06001870 conf, path, other_branch, path, new_path);
Johannes Schindelin75456f92016-07-26 18:06:21 +02001871 if (update_file(o, 0, oid, mode, new_path))
1872 clean_merge = -1;
1873 else if (o->call_depth)
Elijah Newren7b1c6102011-08-11 23:19:55 -06001874 remove_file_from_cache(path);
Elijah Newren3d6b8e82011-08-11 23:19:53 -06001875 free(new_path);
Elijah Newren9c0bbb52010-09-20 02:28:56 -06001876 } else {
Jiang Xin55653a62012-07-25 22:53:13 +08001877 output(o, 2, _("Adding %s"), path);
Elijah Newren35a74ab2011-08-11 23:20:27 -06001878 /* do not overwrite file if already present */
Johannes Schindelin75456f92016-07-26 18:06:21 +02001879 if (update_file_flags(o, oid, mode, path, 1, !a_oid))
1880 clean_merge = -1;
Elijah Newren9c0bbb52010-09-20 02:28:56 -06001881 }
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001882 } else if (a_oid && b_oid) {
Elijah Newrenedd2faf2011-08-11 23:20:07 -06001883 /* Case C: Added in both (check for same permissions) and */
1884 /* case D: Modified in both, but differently. */
Elijah Newren4f66dad2011-08-11 23:20:08 -06001885 clean_merge = merge_content(o, path,
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001886 o_oid, o_mode, a_oid, a_mode, b_oid, b_mode,
Elijah Newrenedd2faf2011-08-11 23:20:07 -06001887 NULL);
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001888 } else if (!o_oid && !a_oid && !b_oid) {
Elijah Newrenedd2faf2011-08-11 23:20:07 -06001889 /*
1890 * this entry was deleted altogether. a_mode == 0 means
1891 * we had that path and want to actively remove it.
1892 */
1893 remove_file(o, 1, path, !a_mode);
1894 } else
Johannes Schindelin7e97e102016-07-26 18:05:53 +02001895 die("BUG: fatal merge failure, shouldn't happen.");
Elijah Newren37348932010-07-09 07:10:53 -06001896
1897 return clean_merge;
1898}
1899
Miklos Vajna8a2fce12008-08-25 16:25:57 +02001900int merge_trees(struct merge_options *o,
1901 struct tree *head,
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001902 struct tree *merge,
1903 struct tree *common,
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001904 struct tree **result)
1905{
1906 int code, clean;
1907
Junio C Hamano85e51b72008-06-30 22:18:57 -07001908 if (o->subtree_shift) {
1909 merge = shift_tree_object(head, merge, o->subtree_shift);
1910 common = shift_tree_object(head, common, o->subtree_shift);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001911 }
1912
brian m. carlsonb4da9d62016-06-24 23:09:27 +00001913 if (oid_eq(&common->object.oid, &merge->object.oid)) {
Jiang Xin55653a62012-07-25 22:53:13 +08001914 output(o, 0, _("Already up-to-date!"));
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001915 *result = head;
1916 return 1;
1917 }
1918
Miklos Vajnab7fa51d2008-09-02 23:53:47 +02001919 code = git_merge_trees(o->call_depth, common, head, merge);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001920
Junio C Hamanofadd0692009-09-07 22:43:11 -07001921 if (code != 0) {
1922 if (show(o, 4) || o->call_depth)
Johannes Schindelinbc9204d2016-08-01 13:44:37 +02001923 err(o, _("merging of trees %s and %s failed"),
brian m. carlsonf2fd0762015-11-10 02:22:28 +00001924 oid_to_hex(&head->object.oid),
1925 oid_to_hex(&merge->object.oid));
Johannes Schindelin60033032016-07-26 18:06:26 +02001926 return -1;
Junio C Hamanofadd0692009-09-07 22:43:11 -07001927 }
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001928
1929 if (unmerged_cache()) {
1930 struct string_list *entries, *re_head, *re_merge;
1931 int i;
Miklos Vajna696ee232008-09-03 19:08:56 +02001932 string_list_clear(&o->current_file_set, 1);
1933 string_list_clear(&o->current_directory_set, 1);
1934 get_files_dirs(o, head);
1935 get_files_dirs(o, merge);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001936
1937 entries = get_unmerged();
Elijah Newren70cc3d32011-08-11 23:19:58 -06001938 record_df_conflict_files(o, entries);
Miklos Vajna8a2fce12008-08-25 16:25:57 +02001939 re_head = get_renames(o, head, common, head, merge, entries);
1940 re_merge = get_renames(o, merge, common, head, merge, entries);
1941 clean = process_renames(o, re_head, re_merge);
Johannes Schindelin75456f92016-07-26 18:06:21 +02001942 if (clean < 0)
1943 return clean;
Elijah Newrenedd2faf2011-08-11 23:20:07 -06001944 for (i = entries->nr-1; 0 <= i; i--) {
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001945 const char *path = entries->items[i].string;
1946 struct stage_data *e = entries->items[i].util;
Johannes Schindelin75456f92016-07-26 18:06:21 +02001947 if (!e->processed) {
1948 int ret = process_entry(o, path, e);
1949 if (!ret)
1950 clean = 0;
1951 else if (ret < 0)
1952 return ret;
1953 }
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001954 }
Elijah Newren37348932010-07-09 07:10:53 -06001955 for (i = 0; i < entries->nr; i++) {
Junio C Hamano7edba4c2010-09-20 02:28:35 -06001956 struct stage_data *e = entries->items[i].util;
1957 if (!e->processed)
Johannes Schindelin7e97e102016-07-26 18:05:53 +02001958 die("BUG: unprocessed path??? %s",
Junio C Hamano7edba4c2010-09-20 02:28:35 -06001959 entries->items[i].string);
1960 }
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001961
1962 string_list_clear(re_merge, 0);
1963 string_list_clear(re_head, 0);
1964 string_list_clear(entries, 1);
1965
Stefan Beller473091e2015-03-20 17:28:04 -07001966 free(re_merge);
1967 free(re_head);
1968 free(entries);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001969 }
1970 else
1971 clean = 1;
1972
Johannes Schindelinfbc87eb2016-07-26 18:06:17 +02001973 if (o->call_depth && !(*result = write_tree_from_memory(o)))
1974 return -1;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001975
1976 return clean;
1977}
1978
1979static struct commit_list *reverse_commit_list(struct commit_list *list)
1980{
1981 struct commit_list *next = NULL, *current, *backup;
1982 for (current = list; current; current = backup) {
1983 backup = current->next;
1984 current->next = next;
1985 next = current;
1986 }
1987 return next;
1988}
1989
1990/*
1991 * Merge the commits h1 and h2, return the resulting virtual
1992 * commit object and a flag indicating the cleanness of the merge.
1993 */
Miklos Vajna8a2fce12008-08-25 16:25:57 +02001994int merge_recursive(struct merge_options *o,
1995 struct commit *h1,
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001996 struct commit *h2,
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001997 struct commit_list *ca,
1998 struct commit **result)
1999{
2000 struct commit_list *iter;
2001 struct commit *merged_common_ancestors;
2002 struct tree *mrtree = mrtree;
2003 int clean;
2004
Miklos Vajna8a2fce12008-08-25 16:25:57 +02002005 if (show(o, 4)) {
Jiang Xin55653a62012-07-25 22:53:13 +08002006 output(o, 4, _("Merging:"));
Miklos Vajna50336392008-09-02 23:30:09 +02002007 output_commit_title(o, h1);
2008 output_commit_title(o, h2);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002009 }
2010
2011 if (!ca) {
Junio C Hamano2ce406c2014-10-30 12:20:44 -07002012 ca = get_merge_bases(h1, h2);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002013 ca = reverse_commit_list(ca);
2014 }
2015
Miklos Vajna8a2fce12008-08-25 16:25:57 +02002016 if (show(o, 5)) {
Ralf Thielowe0453cd2012-08-05 19:56:38 +02002017 unsigned cnt = commit_list_count(ca);
2018
2019 output(o, 5, Q_("found %u common ancestor:",
2020 "found %u common ancestors:", cnt), cnt);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002021 for (iter = ca; iter; iter = iter->next)
Miklos Vajna50336392008-09-02 23:30:09 +02002022 output_commit_title(o, iter->item);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002023 }
2024
2025 merged_common_ancestors = pop_commit(&ca);
2026 if (merged_common_ancestors == NULL) {
Jonathan Nieder03f622c2011-08-16 13:27:39 -05002027 /* if there is no common ancestor, use an empty tree */
2028 struct tree *tree;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002029
Jeff Kingcba595b2012-03-22 14:53:24 -04002030 tree = lookup_tree(EMPTY_TREE_SHA1_BIN);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002031 merged_common_ancestors = make_virtual_commit(tree, "ancestor");
2032 }
2033
2034 for (iter = ca; iter; iter = iter->next) {
Miklos Vajna8a2fce12008-08-25 16:25:57 +02002035 const char *saved_b1, *saved_b2;
Miklos Vajna50336392008-09-02 23:30:09 +02002036 o->call_depth++;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002037 /*
2038 * When the merge fails, the result contains files
2039 * with conflict markers. The cleanness flag is
Johannes Schindelinde8946d2016-07-26 18:06:07 +02002040 * ignored (unless indicating an error), it was never
2041 * actually used, as result of merge_trees has always
2042 * overwritten it: the committed "conflicts" were
2043 * already resolved.
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002044 */
2045 discard_cache();
Miklos Vajna8a2fce12008-08-25 16:25:57 +02002046 saved_b1 = o->branch1;
2047 saved_b2 = o->branch2;
2048 o->branch1 = "Temporary merge branch 1";
2049 o->branch2 = "Temporary merge branch 2";
Johannes Schindelinde8946d2016-07-26 18:06:07 +02002050 if (merge_recursive(o, merged_common_ancestors, iter->item,
2051 NULL, &merged_common_ancestors) < 0)
2052 return -1;
Miklos Vajna8a2fce12008-08-25 16:25:57 +02002053 o->branch1 = saved_b1;
2054 o->branch2 = saved_b2;
Miklos Vajna50336392008-09-02 23:30:09 +02002055 o->call_depth--;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002056
2057 if (!merged_common_ancestors)
Johannes Schindelinbc9204d2016-08-01 13:44:37 +02002058 return err(o, _("merge returned no commit"));
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002059 }
2060
2061 discard_cache();
Miklos Vajnab7fa51d2008-09-02 23:53:47 +02002062 if (!o->call_depth)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002063 read_cache();
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002064
Jonathan Nieder7ca56aa2010-03-20 19:52:21 -05002065 o->ancestor = "merged common ancestors";
Miklos Vajna8a2fce12008-08-25 16:25:57 +02002066 clean = merge_trees(o, h1->tree, h2->tree, merged_common_ancestors->tree,
2067 &mrtree);
Johannes Schindelin6999bc72016-08-01 13:44:57 +02002068 if (clean < 0) {
2069 flush_output(o);
Johannes Schindelinde8946d2016-07-26 18:06:07 +02002070 return clean;
Johannes Schindelin6999bc72016-08-01 13:44:57 +02002071 }
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002072
Miklos Vajnab7fa51d2008-09-02 23:53:47 +02002073 if (o->call_depth) {
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002074 *result = make_virtual_commit(mrtree, "merged tree");
2075 commit_list_insert(h1, &(*result)->parents);
2076 commit_list_insert(h2, &(*result)->parents->next);
2077 }
Miklos Vajnac7d84922008-09-03 02:30:03 +02002078 flush_output(o);
Johannes Schindelin548009c2016-08-01 13:44:53 +02002079 if (!o->call_depth && o->buffer_output < 2)
2080 strbuf_release(&o->obuf);
Junio C Hamanof31027c2011-01-06 13:50:06 -08002081 if (show(o, 2))
2082 diff_warn_rename_limit("merge.renamelimit",
2083 o->needed_rename_limit, 0);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002084 return clean;
2085}
2086
brian m. carlson4e8161a2016-06-24 23:09:28 +00002087static struct commit *get_ref(const struct object_id *oid, const char *name)
Stephan Beyer73118f82008-08-12 22:13:59 +02002088{
2089 struct object *object;
2090
brian m. carlson4e8161a2016-06-24 23:09:28 +00002091 object = deref_tag(parse_object(oid->hash), name, strlen(name));
Stephan Beyer73118f82008-08-12 22:13:59 +02002092 if (!object)
2093 return NULL;
2094 if (object->type == OBJ_TREE)
2095 return make_virtual_commit((struct tree*)object, name);
2096 if (object->type != OBJ_COMMIT)
2097 return NULL;
2098 if (parse_commit((struct commit *)object))
2099 return NULL;
2100 return (struct commit *)object;
2101}
2102
Miklos Vajna8a2fce12008-08-25 16:25:57 +02002103int merge_recursive_generic(struct merge_options *o,
brian m. carlson4e8161a2016-06-24 23:09:28 +00002104 const struct object_id *head,
2105 const struct object_id *merge,
Miklos Vajna8a2fce12008-08-25 16:25:57 +02002106 int num_base_list,
brian m. carlson4e8161a2016-06-24 23:09:28 +00002107 const struct object_id **base_list,
Miklos Vajna8a2fce12008-08-25 16:25:57 +02002108 struct commit **result)
Stephan Beyer73118f82008-08-12 22:13:59 +02002109{
Nguyễn Thái Ngọc Duy03b86642014-06-13 19:19:23 +07002110 int clean;
Stephan Beyer73118f82008-08-12 22:13:59 +02002111 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
Miklos Vajna8a2fce12008-08-25 16:25:57 +02002112 struct commit *head_commit = get_ref(head, o->branch1);
2113 struct commit *next_commit = get_ref(merge, o->branch2);
Stephan Beyer73118f82008-08-12 22:13:59 +02002114 struct commit_list *ca = NULL;
2115
2116 if (base_list) {
2117 int i;
Miklos Vajna8a2fce12008-08-25 16:25:57 +02002118 for (i = 0; i < num_base_list; ++i) {
Stephan Beyer73118f82008-08-12 22:13:59 +02002119 struct commit *base;
brian m. carlson4e8161a2016-06-24 23:09:28 +00002120 if (!(base = get_ref(base_list[i], oid_to_hex(base_list[i]))))
Johannes Schindelinbc9204d2016-08-01 13:44:37 +02002121 return err(o, _("Could not parse object '%s'"),
brian m. carlson4e8161a2016-06-24 23:09:28 +00002122 oid_to_hex(base_list[i]));
Stephan Beyer73118f82008-08-12 22:13:59 +02002123 commit_list_insert(base, &ca);
2124 }
2125 }
2126
Nguyễn Thái Ngọc Duy03b86642014-06-13 19:19:23 +07002127 hold_locked_index(lock, 1);
Miklos Vajna8a2fce12008-08-25 16:25:57 +02002128 clean = merge_recursive(o, head_commit, next_commit, ca,
2129 result);
Johannes Schindelinde8946d2016-07-26 18:06:07 +02002130 if (clean < 0)
2131 return clean;
2132
Stephan Beyer73118f82008-08-12 22:13:59 +02002133 if (active_cache_changed &&
Nguyễn Thái Ngọc Duy03b86642014-06-13 19:19:23 +07002134 write_locked_index(&the_index, lock, COMMIT_LOCK))
Johannes Schindelinbc9204d2016-08-01 13:44:37 +02002135 return err(o, _("Unable to write index."));
Stephan Beyer73118f82008-08-12 22:13:59 +02002136
2137 return clean ? 0 : 1;
2138}
2139
Tanay Abhra0e7bcb12014-08-13 01:22:01 -07002140static void merge_recursive_config(struct merge_options *o)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002141{
Tanay Abhra0e7bcb12014-08-13 01:22:01 -07002142 git_config_get_int("merge.verbosity", &o->verbosity);
2143 git_config_get_int("diff.renamelimit", &o->diff_rename_limit);
2144 git_config_get_int("merge.renamelimit", &o->merge_rename_limit);
2145 git_config(git_xmerge_config, NULL);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002146}
2147
Miklos Vajna8a2fce12008-08-25 16:25:57 +02002148void init_merge_options(struct merge_options *o)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002149{
Miklos Vajna8a2fce12008-08-25 16:25:57 +02002150 memset(o, 0, sizeof(struct merge_options));
2151 o->verbosity = 2;
2152 o->buffer_output = 1;
2153 o->diff_rename_limit = -1;
2154 o->merge_rename_limit = -1;
Jonathan Nieder7610fa52010-08-05 06:32:41 -05002155 o->renormalize = 0;
Felipe Gonçalves Assisd2b11ec2016-02-17 01:15:25 -02002156 o->detect_rename = 1;
Tanay Abhra0e7bcb12014-08-13 01:22:01 -07002157 merge_recursive_config(o);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002158 if (getenv("GIT_MERGE_VERBOSITY"))
Miklos Vajna8a2fce12008-08-25 16:25:57 +02002159 o->verbosity =
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002160 strtol(getenv("GIT_MERGE_VERBOSITY"), NULL, 10);
Miklos Vajna8a2fce12008-08-25 16:25:57 +02002161 if (o->verbosity >= 5)
2162 o->buffer_output = 0;
Miklos Vajnac7d84922008-09-03 02:30:03 +02002163 strbuf_init(&o->obuf, 0);
Tanay Abhraf93d7c62014-07-18 02:19:00 -07002164 string_list_init(&o->current_file_set, 1);
2165 string_list_init(&o->current_directory_set, 1);
2166 string_list_init(&o->df_conflict_file_set, 1);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002167}
Jonathan Nieder635a7bb2010-08-26 00:47:58 -05002168
2169int parse_merge_opt(struct merge_options *o, const char *s)
2170{
Jeff King95b567c2014-06-18 15:48:29 -04002171 const char *arg;
2172
Jonathan Nieder635a7bb2010-08-26 00:47:58 -05002173 if (!s || !*s)
2174 return -1;
2175 if (!strcmp(s, "ours"))
2176 o->recursive_variant = MERGE_RECURSIVE_OURS;
2177 else if (!strcmp(s, "theirs"))
2178 o->recursive_variant = MERGE_RECURSIVE_THEIRS;
2179 else if (!strcmp(s, "subtree"))
2180 o->subtree_shift = "";
Jeff King95b567c2014-06-18 15:48:29 -04002181 else if (skip_prefix(s, "subtree=", &arg))
2182 o->subtree_shift = arg;
Justin Frankel58a1ece2010-08-26 00:50:45 -05002183 else if (!strcmp(s, "patience"))
Junio C Hamano307ab202012-02-19 15:36:55 -08002184 o->xdl_opts = DIFF_WITH_ALG(o, PATIENCE_DIFF);
Tay Ray Chuan8c912ee2011-07-12 14:10:25 +08002185 else if (!strcmp(s, "histogram"))
Junio C Hamano307ab202012-02-19 15:36:55 -08002186 o->xdl_opts = DIFF_WITH_ALG(o, HISTOGRAM_DIFF);
Jeff King95b567c2014-06-18 15:48:29 -04002187 else if (skip_prefix(s, "diff-algorithm=", &arg)) {
2188 long value = parse_algorithm_value(arg);
Michal Privoznik07924d42013-01-16 08:51:58 +01002189 if (value < 0)
2190 return -1;
2191 /* clear out previous settings */
2192 DIFF_XDL_CLR(o, NEED_MINIMAL);
2193 o->xdl_opts &= ~XDF_DIFF_ALGORITHM_MASK;
2194 o->xdl_opts |= value;
2195 }
Justin Frankel4e5dd042010-08-26 00:51:47 -05002196 else if (!strcmp(s, "ignore-space-change"))
2197 o->xdl_opts |= XDF_IGNORE_WHITESPACE_CHANGE;
2198 else if (!strcmp(s, "ignore-all-space"))
2199 o->xdl_opts |= XDF_IGNORE_WHITESPACE;
2200 else if (!strcmp(s, "ignore-space-at-eol"))
2201 o->xdl_opts |= XDF_IGNORE_WHITESPACE_AT_EOL;
Jonathan Nieder635a7bb2010-08-26 00:47:58 -05002202 else if (!strcmp(s, "renormalize"))
2203 o->renormalize = 1;
2204 else if (!strcmp(s, "no-renormalize"))
2205 o->renormalize = 0;
Felipe Gonçalves Assisd2b11ec2016-02-17 01:15:25 -02002206 else if (!strcmp(s, "no-renames"))
2207 o->detect_rename = 0;
Felipe Gonçalves Assis87892f62016-02-21 19:59:05 -03002208 else if (!strcmp(s, "find-renames")) {
Felipe Gonçalves Assis1b47ad12016-02-17 01:15:26 -02002209 o->detect_rename = 1;
Felipe Gonçalves Assis87892f62016-02-21 19:59:05 -03002210 o->rename_score = 0;
2211 }
Felipe Gonçalves Assis1b47ad12016-02-17 01:15:26 -02002212 else if (skip_prefix(s, "find-renames=", &arg) ||
2213 skip_prefix(s, "rename-threshold=", &arg)) {
Jeff King95b567c2014-06-18 15:48:29 -04002214 if ((o->rename_score = parse_rename_score(&arg)) == -1 || *arg != 0)
Kevin Ballard10ae7522010-09-27 16:58:25 -07002215 return -1;
Felipe Gonçalves Assisd2b11ec2016-02-17 01:15:25 -02002216 o->detect_rename = 1;
Kevin Ballard10ae7522010-09-27 16:58:25 -07002217 }
Jonathan Nieder635a7bb2010-08-26 00:47:58 -05002218 else
2219 return -1;
2220 return 0;
2221}