blob: 9ec1e6d043a2f85f61422138c97b379e1a5b70c9 [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"
Elijah Newren4615a8c2019-08-17 11:41:44 -07007#include "merge-recursive.h"
8
David Aguilar1c4b6602014-09-14 00:40:45 -07009#include "advice.h"
Elijah Newren4615a8c2019-08-17 11:41:44 -070010#include "alloc.h"
11#include "attr.h"
Miklos Vajna9047ebb2008-08-12 18:45:14 +020012#include "blob.h"
13#include "builtin.h"
Elijah Newren4615a8c2019-08-17 11:41:44 -070014#include "cache-tree.h"
15#include "commit.h"
16#include "commit-reach.h"
17#include "config.h"
Miklos Vajna9047ebb2008-08-12 18:45:14 +020018#include "diff.h"
19#include "diffcore.h"
Shawn O. Pearce9800c0d2008-09-29 11:04:20 -070020#include "dir.h"
Elijah Newren4615a8c2019-08-17 11:41:44 -070021#include "ll-merge.h"
22#include "lockfile.h"
23#include "object-store.h"
24#include "repository.h"
Stefan Beller18cfc082018-05-15 13:00:28 -070025#include "revision.h"
Elijah Newren4615a8c2019-08-17 11:41:44 -070026#include "string-list.h"
Jonathan Tan10a0d6a2021-09-09 11:47:29 -070027#include "submodule-config.h"
Elijah Newren4615a8c2019-08-17 11:41:44 -070028#include "submodule.h"
29#include "tag.h"
30#include "tree-walk.h"
31#include "unpack-trees.h"
32#include "xdiff-interface.h"
Miklos Vajna9047ebb2008-08-12 18:45:14 +020033
Elijah Newren5bf7e572019-08-17 11:41:41 -070034struct merge_options_internal {
35 int call_depth;
36 int needed_rename_limit;
37 struct hashmap current_file_dir_set;
38 struct string_list df_conflict_file_set;
39 struct unpack_trees_options unpack_opts;
40 struct index_state orig_index;
41};
Miklos Vajna9047ebb2008-08-12 18:45:14 +020042
Kevin Willfordfc65b002017-09-07 10:25:56 -060043struct path_hashmap_entry {
44 struct hashmap_entry e;
45 char path[FLEX_ARRAY];
46};
47
48static int path_hashmap_cmp(const void *cmp_data,
Eric Wong939af162019-10-06 23:30:37 +000049 const struct hashmap_entry *eptr,
50 const struct hashmap_entry *entry_or_key,
Kevin Willfordfc65b002017-09-07 10:25:56 -060051 const void *keydata)
52{
Eric Wong939af162019-10-06 23:30:37 +000053 const struct path_hashmap_entry *a, *b;
Kevin Willfordfc65b002017-09-07 10:25:56 -060054 const char *key = keydata;
55
Eric Wong939af162019-10-06 23:30:37 +000056 a = container_of(eptr, const struct path_hashmap_entry, e);
57 b = container_of(entry_or_key, const struct path_hashmap_entry, e);
58
René Scharfe2dee7e62021-08-28 23:30:49 +020059 return fspathcmp(a->path, key ? key : b->path);
Kevin Willfordfc65b002017-09-07 10:25:56 -060060}
61
Elijah Newren7c0a6c82019-08-17 11:41:37 -070062/*
63 * For dir_rename_entry, directory names are stored as a full path from the
64 * toplevel of the repository and do not include a trailing '/'. Also:
65 *
66 * dir: original name of directory being renamed
67 * non_unique_new_dir: if true, could not determine new_dir
68 * new_dir: final name of directory being renamed
69 * possible_new_dirs: temporary used to help determine new_dir; see comments
70 * in get_directory_renames() for details
71 */
72struct dir_rename_entry {
Junio C Hamano5efabc72019-10-15 13:48:01 +090073 struct hashmap_entry ent;
Elijah Newren7c0a6c82019-08-17 11:41:37 -070074 char *dir;
75 unsigned non_unique_new_dir:1;
76 struct strbuf new_dir;
77 struct string_list possible_new_dirs;
78};
79
Elijah Newren7fe40b82018-04-19 10:58:05 -070080static struct dir_rename_entry *dir_rename_find_entry(struct hashmap *hashmap,
81 char *dir)
82{
83 struct dir_rename_entry key;
84
85 if (dir == NULL)
86 return NULL;
Eric Wongd22245a2019-10-06 23:30:27 +000087 hashmap_entry_init(&key.ent, strhash(dir));
Elijah Newren7fe40b82018-04-19 10:58:05 -070088 key.dir = dir;
Eric Wong404ab782019-10-06 23:30:42 +000089 return hashmap_get_entry(hashmap, &key, ent, NULL);
Elijah Newren7fe40b82018-04-19 10:58:05 -070090}
91
92static int dir_rename_cmp(const void *unused_cmp_data,
Eric Wong939af162019-10-06 23:30:37 +000093 const struct hashmap_entry *eptr,
94 const struct hashmap_entry *entry_or_key,
Elijah Newren7fe40b82018-04-19 10:58:05 -070095 const void *unused_keydata)
96{
Eric Wong939af162019-10-06 23:30:37 +000097 const struct dir_rename_entry *e1, *e2;
98
99 e1 = container_of(eptr, const struct dir_rename_entry, ent);
100 e2 = container_of(entry_or_key, const struct dir_rename_entry, ent);
Elijah Newren7fe40b82018-04-19 10:58:05 -0700101
102 return strcmp(e1->dir, e2->dir);
103}
104
105static void dir_rename_init(struct hashmap *map)
106{
107 hashmap_init(map, dir_rename_cmp, NULL, 0);
108}
109
110static void dir_rename_entry_init(struct dir_rename_entry *entry,
111 char *directory)
112{
Eric Wongd22245a2019-10-06 23:30:27 +0000113 hashmap_entry_init(&entry->ent, strhash(directory));
Elijah Newren7fe40b82018-04-19 10:58:05 -0700114 entry->dir = directory;
115 entry->non_unique_new_dir = 0;
116 strbuf_init(&entry->new_dir, 0);
Ævar Arnfjörð Bjarmasonbc40dfb2021-07-01 12:51:29 +0200117 string_list_init_nodup(&entry->possible_new_dirs);
Elijah Newren7fe40b82018-04-19 10:58:05 -0700118}
119
Elijah Newren7c0a6c82019-08-17 11:41:37 -0700120struct collision_entry {
Junio C Hamano5efabc72019-10-15 13:48:01 +0900121 struct hashmap_entry ent;
Elijah Newren7c0a6c82019-08-17 11:41:37 -0700122 char *target_file;
123 struct string_list source_files;
124 unsigned reported_already:1;
125};
126
Elijah Newrene95ab702018-04-19 10:58:07 -0700127static struct collision_entry *collision_find_entry(struct hashmap *hashmap,
128 char *target_file)
129{
130 struct collision_entry key;
131
Eric Wongd22245a2019-10-06 23:30:27 +0000132 hashmap_entry_init(&key.ent, strhash(target_file));
Elijah Newrene95ab702018-04-19 10:58:07 -0700133 key.target_file = target_file;
Eric Wong404ab782019-10-06 23:30:42 +0000134 return hashmap_get_entry(hashmap, &key, ent, NULL);
Elijah Newrene95ab702018-04-19 10:58:07 -0700135}
136
Eric Wong939af162019-10-06 23:30:37 +0000137static int collision_cmp(const void *unused_cmp_data,
138 const struct hashmap_entry *eptr,
139 const struct hashmap_entry *entry_or_key,
Elijah Newrene95ab702018-04-19 10:58:07 -0700140 const void *unused_keydata)
141{
Eric Wong939af162019-10-06 23:30:37 +0000142 const struct collision_entry *e1, *e2;
143
144 e1 = container_of(eptr, const struct collision_entry, ent);
145 e2 = container_of(entry_or_key, const struct collision_entry, ent);
146
Elijah Newrene95ab702018-04-19 10:58:07 -0700147 return strcmp(e1->target_file, e2->target_file);
148}
149
150static void collision_init(struct hashmap *map)
151{
Eric Wong939af162019-10-06 23:30:37 +0000152 hashmap_init(map, collision_cmp, NULL, 0);
Elijah Newrene95ab702018-04-19 10:58:07 -0700153}
154
Elijah Newren259ccb62019-04-05 08:00:13 -0700155static void flush_output(struct merge_options *opt)
Johannes Schindelinbc9204d2016-08-01 13:44:37 +0200156{
Elijah Newren259ccb62019-04-05 08:00:13 -0700157 if (opt->buffer_output < 2 && opt->obuf.len) {
158 fputs(opt->obuf.buf, stdout);
159 strbuf_reset(&opt->obuf);
Johannes Schindelinbc9204d2016-08-01 13:44:37 +0200160 }
161}
162
Ævar Arnfjörð Bjarmason48ca53c2021-07-13 10:05:18 +0200163__attribute__((format (printf, 2, 3)))
Elijah Newren259ccb62019-04-05 08:00:13 -0700164static int err(struct merge_options *opt, const char *err, ...)
Johannes Schindelinbc9204d2016-08-01 13:44:37 +0200165{
166 va_list params;
167
Elijah Newren259ccb62019-04-05 08:00:13 -0700168 if (opt->buffer_output < 2)
169 flush_output(opt);
Johannes Schindelinf1e24262016-08-01 13:44:50 +0200170 else {
Elijah Newren259ccb62019-04-05 08:00:13 -0700171 strbuf_complete(&opt->obuf, '\n');
172 strbuf_addstr(&opt->obuf, "error: ");
Johannes Schindelinf1e24262016-08-01 13:44:50 +0200173 }
Johannes Schindelinbc9204d2016-08-01 13:44:37 +0200174 va_start(params, err);
Elijah Newren259ccb62019-04-05 08:00:13 -0700175 strbuf_vaddf(&opt->obuf, err, params);
Johannes Schindelinbc9204d2016-08-01 13:44:37 +0200176 va_end(params);
Elijah Newren259ccb62019-04-05 08:00:13 -0700177 if (opt->buffer_output > 1)
178 strbuf_addch(&opt->obuf, '\n');
Johannes Schindelinf1e24262016-08-01 13:44:50 +0200179 else {
Elijah Newren259ccb62019-04-05 08:00:13 -0700180 error("%s", opt->obuf.buf);
181 strbuf_reset(&opt->obuf);
Johannes Schindelinf1e24262016-08-01 13:44:50 +0200182 }
Johannes Schindelinbc9204d2016-08-01 13:44:37 +0200183
184 return -1;
185}
186
Nguyễn Thái Ngọc Duyd7cf3a92019-01-12 09:13:30 +0700187static struct tree *shift_tree_object(struct repository *repo,
188 struct tree *one, struct tree *two,
Junio C Hamano85e51b72008-06-30 22:18:57 -0700189 const char *subtree_shift)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200190{
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000191 struct object_id shifted;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200192
Junio C Hamano85e51b72008-06-30 22:18:57 -0700193 if (!*subtree_shift) {
Nguyễn Thái Ngọc Duy90d34052019-06-27 16:28:51 +0700194 shift_tree(repo, &one->object.oid, &two->object.oid, &shifted, 0);
Junio C Hamano85e51b72008-06-30 22:18:57 -0700195 } else {
Nguyễn Thái Ngọc Duy90d34052019-06-27 16:28:51 +0700196 shift_tree_by(repo, &one->object.oid, &two->object.oid, &shifted,
Junio C Hamano85e51b72008-06-30 22:18:57 -0700197 subtree_shift);
198 }
Jeff King4a7e27e2018-08-28 17:22:40 -0400199 if (oideq(&two->object.oid, &shifted))
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200200 return two;
Nguyễn Thái Ngọc Duyd7cf3a92019-01-12 09:13:30 +0700201 return lookup_tree(repo, &shifted);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200202}
203
Nguyễn Thái Ngọc Duya133c402019-04-16 16:33:18 +0700204static inline void set_commit_tree(struct commit *c, struct tree *t)
205{
206 c->maybe_tree = t;
207}
208
Nguyễn Thái Ngọc Duyd7cf3a92019-01-12 09:13:30 +0700209static struct commit *make_virtual_commit(struct repository *repo,
210 struct tree *tree,
211 const char *comment)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200212{
Nguyễn Thái Ngọc Duyd7cf3a92019-01-12 09:13:30 +0700213 struct commit *commit = alloc_commit_node(repo);
Junio C Hamanoae8e4c92011-11-07 13:26:22 -0800214
René Scharfea2571652016-08-13 14:16:04 +0200215 set_merge_remote_desc(commit, comment, (struct object *)commit);
Nguyễn Thái Ngọc Duya133c402019-04-16 16:33:18 +0700216 set_commit_tree(commit, tree);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200217 commit->object.parsed = 1;
218 return commit;
219}
220
Elijah Newren25c39362010-09-20 02:28:53 -0600221enum rename_type {
222 RENAME_NORMAL = 0,
Elijah Newren5455c332018-06-09 21:16:14 -0700223 RENAME_VIA_DIR,
Elijah Newren7f867162018-11-07 20:40:26 -0800224 RENAME_ADD,
Elijah Newren25c39362010-09-20 02:28:53 -0600225 RENAME_DELETE,
Elijah Newren4f66dad2011-08-11 23:20:08 -0600226 RENAME_ONE_FILE_TO_ONE,
Elijah Newren461f5042011-08-11 23:20:15 -0600227 RENAME_ONE_FILE_TO_TWO,
228 RENAME_TWO_FILES_TO_ONE
Elijah Newren25c39362010-09-20 02:28:53 -0600229};
230
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200231/*
232 * Since we want to write the index eventually, we cannot reuse the index
233 * for these (temporary) data.
234 */
Jonathan Nieder9cba13c2011-03-16 02:08:34 -0500235struct stage_data {
Elijah Newren8daec1d2019-04-05 08:00:22 -0700236 struct diff_filespec stages[4]; /* mostly for oid & mode; maybe path */
Elijah Newren4f66dad2011-08-11 23:20:08 -0600237 struct rename_conflict_info *rename_conflict_info;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200238 unsigned processed:1;
239};
240
Elijah Newren967d6be2019-04-05 08:00:17 -0700241struct rename {
Elijah Newren6d169fd2019-04-05 08:00:24 -0700242 unsigned processed:1;
Elijah Newren967d6be2019-04-05 08:00:17 -0700243 struct diff_filepair *pair;
Elijah Newrenc336ab82019-04-05 08:00:20 -0700244 const char *branch; /* branch that the rename occurred on */
Elijah Newren967d6be2019-04-05 08:00:17 -0700245 /*
Elijah Newren6d169fd2019-04-05 08:00:24 -0700246 * If directory rename detection affected this rename, what was its
247 * original type ('A' or 'R') and it's original destination before
248 * the directory rename (otherwise, '\0' and NULL for these two vars).
249 */
250 char dir_rename_original_type;
251 char *dir_rename_original_dest;
252 /*
Elijah Newren967d6be2019-04-05 08:00:17 -0700253 * Purpose of src_entry and dst_entry:
254 *
255 * If 'before' is renamed to 'after' then src_entry will contain
256 * the versions of 'before' from the merge_base, HEAD, and MERGE in
257 * stages 1, 2, and 3; dst_entry will contain the respective
258 * versions of 'after' in corresponding locations. Thus, we have a
259 * total of six modes and oids, though some will be null. (Stage 0
260 * is ignored; we're interested in handling conflicts.)
261 *
262 * Since we don't turn on break-rewrites by default, neither
263 * src_entry nor dst_entry can have all three of their stages have
264 * non-null oids, meaning at most four of the six will be non-null.
265 * Also, since this is a rename, both src_entry and dst_entry will
266 * have at least one non-null oid, meaning at least two will be
267 * non-null. Of the six oids, a typical rename will have three be
268 * non-null. Only two implies a rename/delete, and four implies a
269 * rename/add.
270 */
271 struct stage_data *src_entry;
272 struct stage_data *dst_entry;
Elijah Newren967d6be2019-04-05 08:00:17 -0700273};
274
275struct rename_conflict_info {
276 enum rename_type rename_type;
Elijah Newrene9cd1b52019-04-05 08:00:18 -0700277 struct rename *ren1;
278 struct rename *ren2;
Elijah Newren967d6be2019-04-05 08:00:17 -0700279};
280
Elijah Newren4f66dad2011-08-11 23:20:08 -0600281static inline void setup_rename_conflict_info(enum rename_type rename_type,
Elijah Newrene9cd1b52019-04-05 08:00:18 -0700282 struct merge_options *opt,
283 struct rename *ren1,
Elijah Newrenc336ab82019-04-05 08:00:20 -0700284 struct rename *ren2)
Elijah Newren25c39362010-09-20 02:28:53 -0600285{
Elijah Newren4f445452018-10-16 13:19:48 -0700286 struct rename_conflict_info *ci;
287
288 /*
289 * When we have two renames involved, it's easiest to get the
290 * correct things into stage 2 and 3, and to make sure that the
291 * content merge puts HEAD before the other branch if we just
Elijah Newren259ccb62019-04-05 08:00:13 -0700292 * ensure that branch1 == opt->branch1. So, simply flip arguments
Elijah Newren4f445452018-10-16 13:19:48 -0700293 * around if we don't have that.
294 */
Elijah Newrenc336ab82019-04-05 08:00:20 -0700295 if (ren2 && ren1->branch != opt->branch1) {
296 setup_rename_conflict_info(rename_type, opt, ren2, ren1);
Elijah Newren4f445452018-10-16 13:19:48 -0700297 return;
298 }
299
René Scharfeca56dad2021-03-13 17:17:22 +0100300 CALLOC_ARRAY(ci, 1);
Elijah Newren25c39362010-09-20 02:28:53 -0600301 ci->rename_type = rename_type;
Elijah Newrene9cd1b52019-04-05 08:00:18 -0700302 ci->ren1 = ren1;
303 ci->ren2 = ren2;
Elijah Newren25c39362010-09-20 02:28:53 -0600304
Elijah Newrene9cd1b52019-04-05 08:00:18 -0700305 ci->ren1->dst_entry->processed = 0;
306 ci->ren1->dst_entry->rename_conflict_info = ci;
Elijah Newrene9cd1b52019-04-05 08:00:18 -0700307 if (ren2) {
308 ci->ren2->dst_entry->rename_conflict_info = ci;
Elijah Newren25c39362010-09-20 02:28:53 -0600309 }
310}
311
Elijah Newren259ccb62019-04-05 08:00:13 -0700312static int show(struct merge_options *opt, int v)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200313{
Elijah Newren5bf7e572019-08-17 11:41:41 -0700314 return (!opt->priv->call_depth && opt->verbosity >= v) ||
315 opt->verbosity >= 5;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200316}
317
Tarmigan Casebolt28bea9e2009-11-14 13:33:13 -0800318__attribute__((format (printf, 3, 4)))
Elijah Newren259ccb62019-04-05 08:00:13 -0700319static void output(struct merge_options *opt, int v, const char *fmt, ...)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200320{
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200321 va_list ap;
322
Elijah Newren259ccb62019-04-05 08:00:13 -0700323 if (!show(opt, v))
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200324 return;
325
Elijah Newren5bf7e572019-08-17 11:41:41 -0700326 strbuf_addchars(&opt->obuf, ' ', opt->priv->call_depth * 2);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200327
328 va_start(ap, fmt);
Elijah Newren259ccb62019-04-05 08:00:13 -0700329 strbuf_vaddf(&opt->obuf, fmt, ap);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200330 va_end(ap);
331
Elijah Newren259ccb62019-04-05 08:00:13 -0700332 strbuf_addch(&opt->obuf, '\n');
333 if (!opt->buffer_output)
334 flush_output(opt);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200335}
336
Jonathan Tan155b5172021-10-08 14:08:17 -0700337static void repo_output_commit_title(struct merge_options *opt,
338 struct repository *repo,
339 struct commit *commit)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200340{
Nguyễn Thái Ngọc Duye2e5ac22018-05-19 07:28:30 +0200341 struct merge_remote_desc *desc;
342
Elijah Newren5bf7e572019-08-17 11:41:41 -0700343 strbuf_addchars(&opt->obuf, ' ', opt->priv->call_depth * 2);
Nguyễn Thái Ngọc Duye2e5ac22018-05-19 07:28:30 +0200344 desc = merge_remote_util(commit);
345 if (desc)
Elijah Newren259ccb62019-04-05 08:00:13 -0700346 strbuf_addf(&opt->obuf, "virtual %s\n", desc->name);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200347 else {
Jonathan Tan155b5172021-10-08 14:08:17 -0700348 strbuf_repo_add_unique_abbrev(&opt->obuf, repo,
349 &commit->object.oid,
350 DEFAULT_ABBREV);
Elijah Newren259ccb62019-04-05 08:00:13 -0700351 strbuf_addch(&opt->obuf, ' ');
Jonathan Tan155b5172021-10-08 14:08:17 -0700352 if (repo_parse_commit(repo, commit) != 0)
Elijah Newren259ccb62019-04-05 08:00:13 -0700353 strbuf_addstr(&opt->obuf, _("(bad commit)\n"));
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200354 else {
Christian Couder49b71202010-07-22 15:18:34 +0200355 const char *title;
Jonathan Tan155b5172021-10-08 14:08:17 -0700356 const char *msg = repo_get_commit_buffer(repo, commit, NULL);
Jeff Kingbc6b8fc2014-06-10 17:41:51 -0400357 int len = find_commit_subject(msg, &title);
Christian Couder49b71202010-07-22 15:18:34 +0200358 if (len)
Elijah Newren259ccb62019-04-05 08:00:13 -0700359 strbuf_addf(&opt->obuf, "%.*s\n", len, title);
Jonathan Tan155b5172021-10-08 14:08:17 -0700360 repo_unuse_commit_buffer(repo, commit, msg);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200361 }
362 }
Elijah Newren259ccb62019-04-05 08:00:13 -0700363 flush_output(opt);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200364}
365
Jonathan Tan155b5172021-10-08 14:08:17 -0700366static void output_commit_title(struct merge_options *opt, struct commit *commit)
367{
368 repo_output_commit_title(opt, the_repository, commit);
369}
370
Elijah Newren259ccb62019-04-05 08:00:13 -0700371static int add_cacheinfo(struct merge_options *opt,
Elijah Newren8daec1d2019-04-05 08:00:22 -0700372 const struct diff_filespec *blob,
Elijah Newrend90e7592018-06-09 21:16:12 -0700373 const char *path, int stage, int refresh, int options)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200374{
Elijah Newren259ccb62019-04-05 08:00:13 -0700375 struct index_state *istate = opt->repo->index;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200376 struct cache_entry *ce;
Junio C Hamano1335d762016-07-08 10:59:15 -0700377 int ret;
378
Elijah Newren8daec1d2019-04-05 08:00:22 -0700379 ce = make_cache_entry(istate, blob->mode, &blob->oid, path, stage, 0);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200380 if (!ce)
Elijah Newren259ccb62019-04-05 08:00:13 -0700381 return err(opt, _("add_cacheinfo failed for path '%s'; merge aborting."), path);
Junio C Hamano1335d762016-07-08 10:59:15 -0700382
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700383 ret = add_index_entry(istate, ce, options);
Junio C Hamano1335d762016-07-08 10:59:15 -0700384 if (refresh) {
385 struct cache_entry *nce;
386
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700387 nce = refresh_cache_entry(istate, ce,
388 CE_MATCH_REFRESH | CE_MATCH_IGNORE_MISSING);
Johannes Schindelin55e9f0e2016-11-26 13:48:06 +0100389 if (!nce)
Elijah Newren259ccb62019-04-05 08:00:13 -0700390 return err(opt, _("add_cacheinfo failed to refresh for path '%s'; merge aborting."), path);
Junio C Hamano1335d762016-07-08 10:59:15 -0700391 if (nce != ce)
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700392 ret = add_index_entry(istate, nce, options);
Junio C Hamano1335d762016-07-08 10:59:15 -0700393 }
394 return ret;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200395}
396
Elijah Newren7c0a6c82019-08-17 11:41:37 -0700397static inline int merge_detect_rename(struct merge_options *opt)
398{
Elijah Newren8599ab42019-08-17 11:41:38 -0700399 return (opt->detect_renames >= 0) ? opt->detect_renames : 1;
Elijah Newren7c0a6c82019-08-17 11:41:37 -0700400}
401
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200402static void init_tree_desc_from_tree(struct tree_desc *desc, struct tree *tree)
403{
404 parse_tree(tree);
405 init_tree_desc(desc, tree->buffer, tree->size);
406}
407
Elijah Newren259ccb62019-04-05 08:00:13 -0700408static int unpack_trees_start(struct merge_options *opt,
Elijah Newren3f1c1c32018-05-20 12:17:35 +0200409 struct tree *common,
410 struct tree *head,
411 struct tree *merge)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200412{
413 int rc;
414 struct tree_desc t[3];
Elijah Newrena35edc82018-04-19 10:58:20 -0700415 struct index_state tmp_index = { NULL };
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200416
Elijah Newren5bf7e572019-08-17 11:41:41 -0700417 memset(&opt->priv->unpack_opts, 0, sizeof(opt->priv->unpack_opts));
418 if (opt->priv->call_depth)
419 opt->priv->unpack_opts.index_only = 1;
Elijah Newren491a7572021-09-27 16:33:40 +0000420 else {
Elijah Newren5bf7e572019-08-17 11:41:41 -0700421 opt->priv->unpack_opts.update = 1;
Elijah Newren491a7572021-09-27 16:33:40 +0000422 /* FIXME: should only do this if !overwrite_ignore */
Elijah Newren04988c82021-09-27 16:33:41 +0000423 opt->priv->unpack_opts.preserve_ignored = 0;
Elijah Newren491a7572021-09-27 16:33:40 +0000424 }
Elijah Newren5bf7e572019-08-17 11:41:41 -0700425 opt->priv->unpack_opts.merge = 1;
426 opt->priv->unpack_opts.head_idx = 2;
427 opt->priv->unpack_opts.fn = threeway_merge;
428 opt->priv->unpack_opts.src_index = opt->repo->index;
429 opt->priv->unpack_opts.dst_index = &tmp_index;
430 opt->priv->unpack_opts.aggressive = !merge_detect_rename(opt);
431 setup_unpack_trees_porcelain(&opt->priv->unpack_opts, "merge");
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200432
433 init_tree_desc_from_tree(t+0, common);
434 init_tree_desc_from_tree(t+1, head);
435 init_tree_desc_from_tree(t+2, merge);
436
Elijah Newren5bf7e572019-08-17 11:41:41 -0700437 rc = unpack_trees(3, t, &opt->priv->unpack_opts);
Elijah Newren259ccb62019-04-05 08:00:13 -0700438 cache_tree_free(&opt->repo->index->cache_tree);
Elijah Newrena35edc82018-04-19 10:58:20 -0700439
440 /*
Elijah Newren5bf7e572019-08-17 11:41:41 -0700441 * Update opt->repo->index to match the new results, AFTER saving a
442 * copy in opt->priv->orig_index. Update src_index to point to the
443 * saved copy. (verify_uptodate() checks src_index, and the original
444 * index is the one that had the necessary modification timestamps.)
Elijah Newrena35edc82018-04-19 10:58:20 -0700445 */
Elijah Newren5bf7e572019-08-17 11:41:41 -0700446 opt->priv->orig_index = *opt->repo->index;
Elijah Newren259ccb62019-04-05 08:00:13 -0700447 *opt->repo->index = tmp_index;
Elijah Newren5bf7e572019-08-17 11:41:41 -0700448 opt->priv->unpack_opts.src_index = &opt->priv->orig_index;
Elijah Newrena35edc82018-04-19 10:58:20 -0700449
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200450 return rc;
451}
452
Elijah Newren259ccb62019-04-05 08:00:13 -0700453static void unpack_trees_finish(struct merge_options *opt)
Elijah Newren3f1c1c32018-05-20 12:17:35 +0200454{
Elijah Newren5bf7e572019-08-17 11:41:41 -0700455 discard_index(&opt->priv->orig_index);
456 clear_unpack_trees_porcelain(&opt->priv->unpack_opts);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200457}
458
brian m. carlsondf46d772018-03-12 02:27:26 +0000459static int save_files_dirs(const struct object_id *oid,
Elijah Newrend90e7592018-06-09 21:16:12 -0700460 struct strbuf *base, const char *path,
Ævar Arnfjörð Bjarmason47957482021-03-20 23:37:51 +0100461 unsigned int mode, void *context)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200462{
Kevin Willfordfc65b002017-09-07 10:25:56 -0600463 struct path_hashmap_entry *entry;
Nguyễn Thái Ngọc Duy6a0b0b62014-11-30 16:05:00 +0700464 int baselen = base->len;
Elijah Newren259ccb62019-04-05 08:00:13 -0700465 struct merge_options *opt = context;
Miklos Vajna696ee232008-09-03 19:08:56 +0200466
Nguyễn Thái Ngọc Duy6a0b0b62014-11-30 16:05:00 +0700467 strbuf_addstr(base, path);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200468
Kevin Willfordfc65b002017-09-07 10:25:56 -0600469 FLEX_ALLOC_MEM(entry, path, base->buf, base->len);
René Scharfe74318422021-07-30 21:06:58 +0200470 hashmap_entry_init(&entry->e, fspathhash(entry->path));
Junio C Hamano5efabc72019-10-15 13:48:01 +0900471 hashmap_add(&opt->priv->current_file_dir_set, &entry->e);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200472
Nguyễn Thái Ngọc Duy6a0b0b62014-11-30 16:05:00 +0700473 strbuf_setlen(base, baselen);
Lars Hjemlid3bee162009-01-25 01:52:05 +0100474 return (S_ISDIR(mode) ? READ_TREE_RECURSIVE : 0);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200475}
476
Elijah Newren259ccb62019-04-05 08:00:13 -0700477static void get_files_dirs(struct merge_options *opt, struct tree *tree)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200478{
Nguyễn Thái Ngọc Duyf0096c02011-03-25 16:34:19 +0700479 struct pathspec match_all;
Nguyễn Thái Ngọc Duy9a087272013-07-14 15:35:59 +0700480 memset(&match_all, 0, sizeof(match_all));
Ævar Arnfjörð Bjarmason47957482021-03-20 23:37:51 +0100481 read_tree(opt->repo, tree,
482 &match_all, save_files_dirs, opt);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200483}
484
Nguyễn Thái Ngọc Duy34e77712019-06-27 16:28:52 +0700485static int get_tree_entry_if_blob(struct repository *r,
486 const struct object_id *tree,
Elijah Newren5b047ac2018-04-19 10:58:09 -0700487 const char *path,
Elijah Newren8daec1d2019-04-05 08:00:22 -0700488 struct diff_filespec *dfs)
Elijah Newren5b047ac2018-04-19 10:58:09 -0700489{
490 int ret;
491
Nguyễn Thái Ngọc Duy34e77712019-06-27 16:28:52 +0700492 ret = get_tree_entry(r, tree, path, &dfs->oid, &dfs->mode);
Elijah Newren8daec1d2019-04-05 08:00:22 -0700493 if (S_ISDIR(dfs->mode)) {
brian m. carlson14228442021-04-26 01:02:56 +0000494 oidcpy(&dfs->oid, null_oid());
Elijah Newren8daec1d2019-04-05 08:00:22 -0700495 dfs->mode = 0;
Elijah Newren5b047ac2018-04-19 10:58:09 -0700496 }
497 return ret;
498}
499
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200500/*
501 * Returns an index_entry instance which doesn't have to correspond to
502 * a real cache entry in Git's index.
503 */
Nguyễn Thái Ngọc Duy34e77712019-06-27 16:28:52 +0700504static struct stage_data *insert_stage_data(struct repository *r,
505 const char *path,
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200506 struct tree *o, struct tree *a, struct tree *b,
507 struct string_list *entries)
508{
509 struct string_list_item *item;
510 struct stage_data *e = xcalloc(1, sizeof(struct stage_data));
Nguyễn Thái Ngọc Duy34e77712019-06-27 16:28:52 +0700511 get_tree_entry_if_blob(r, &o->object.oid, path, &e->stages[1]);
512 get_tree_entry_if_blob(r, &a->object.oid, path, &e->stages[2]);
513 get_tree_entry_if_blob(r, &b->object.oid, path, &e->stages[3]);
Julian Phillips78a395d2010-06-26 00:41:35 +0100514 item = string_list_insert(entries, path);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200515 item->util = e;
516 return e;
517}
518
519/*
520 * Create a dictionary mapping file names to stage_data objects. The
521 * dictionary contains one entry for every path with a non-zero stage entry.
522 */
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700523static struct string_list *get_unmerged(struct index_state *istate)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200524{
525 struct string_list *unmerged = xcalloc(1, sizeof(struct string_list));
526 int i;
527
528 unmerged->strdup_strings = 1;
529
Derrick Stoleef7ef64b2021-04-01 01:49:56 +0000530 /* TODO: audit for interaction with sparse-index. */
531 ensure_full_index(istate);
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700532 for (i = 0; i < istate->cache_nr; i++) {
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200533 struct string_list_item *item;
534 struct stage_data *e;
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700535 const struct cache_entry *ce = istate->cache[i];
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200536 if (!ce_stage(ce))
537 continue;
538
Julian Phillipse8c8b712010-06-26 00:41:37 +0100539 item = string_list_lookup(unmerged, ce->name);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200540 if (!item) {
Julian Phillips78a395d2010-06-26 00:41:35 +0100541 item = string_list_insert(unmerged, ce->name);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200542 item->util = xcalloc(1, sizeof(struct stage_data));
543 }
544 e = item->util;
545 e->stages[ce_stage(ce)].mode = ce->ce_mode;
brian m. carlson99d1a982016-09-05 20:07:52 +0000546 oidcpy(&e->stages[ce_stage(ce)].oid, &ce->oid);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200547 }
548
549 return unmerged;
550}
551
Nguyễn Thái Ngọc Duyfa6ca112016-11-24 18:45:36 +0700552static int string_list_df_name_compare(const char *one, const char *two)
Elijah Newrenef02b312010-09-20 02:29:09 -0600553{
Nguyễn Thái Ngọc Duyfa6ca112016-11-24 18:45:36 +0700554 int onelen = strlen(one);
555 int twolen = strlen(two);
Elijah Newrenf0fd4d02011-08-11 23:19:56 -0600556 /*
557 * Here we only care that entries for D/F conflicts are
558 * adjacent, in particular with the file of the D/F conflict
559 * appearing before files below the corresponding directory.
560 * The order of the rest of the list is irrelevant for us.
Elijah Newrenef02b312010-09-20 02:29:09 -0600561 *
Elijah Newrenf0fd4d02011-08-11 23:19:56 -0600562 * To achieve this, we sort with df_name_compare and provide
563 * the mode S_IFDIR so that D/F conflicts will sort correctly.
564 * We use the mode S_IFDIR for everything else for simplicity,
565 * since in other cases any changes in their order due to
566 * sorting cause no problems for us.
Elijah Newrenef02b312010-09-20 02:29:09 -0600567 */
Nguyễn Thái Ngọc Duyfa6ca112016-11-24 18:45:36 +0700568 int cmp = df_name_compare(one, onelen, S_IFDIR,
569 two, twolen, S_IFDIR);
Elijah Newrenf0fd4d02011-08-11 23:19:56 -0600570 /*
571 * Now that 'foo' and 'foo/bar' compare equal, we have to make sure
572 * that 'foo' comes before 'foo/bar'.
573 */
574 if (cmp)
575 return cmp;
576 return onelen - twolen;
577}
578
Elijah Newren259ccb62019-04-05 08:00:13 -0700579static void record_df_conflict_files(struct merge_options *opt,
Elijah Newren70cc3d32011-08-11 23:19:58 -0600580 struct string_list *entries)
Elijah Newrenef02b312010-09-20 02:29:09 -0600581{
Elijah Newren70cc3d32011-08-11 23:19:58 -0600582 /* If there is a D/F conflict and the file for such a conflict
Elijah Newren2d6bad92018-06-09 21:16:11 -0700583 * currently exists in the working tree, we want to allow it to be
Elijah Newrenedd2faf2011-08-11 23:20:07 -0600584 * removed to make room for the corresponding directory if needed.
585 * The files underneath the directories of such D/F conflicts will
586 * be processed before the corresponding file involved in the D/F
587 * conflict. If the D/F directory ends up being removed by the
588 * merge, then we won't have to touch the D/F file. If the D/F
589 * directory needs to be written to the working copy, then the D/F
590 * file will simply be removed (in make_room_for_path()) to make
591 * room for the necessary paths. Note that if both the directory
592 * and the file need to be present, then the D/F file will be
593 * reinstated with a new unique name at the time it is processed.
Elijah Newrenef02b312010-09-20 02:29:09 -0600594 */
René Scharfeaf4941d2016-08-05 22:42:12 +0200595 struct string_list df_sorted_entries = STRING_LIST_INIT_NODUP;
Elijah Newrenef02b312010-09-20 02:29:09 -0600596 const char *last_file = NULL;
Junio C Hamanoc8516502010-10-21 07:34:33 -0700597 int last_len = 0;
Elijah Newrenef02b312010-09-20 02:29:09 -0600598 int i;
599
Elijah Newren0b30e812011-08-11 23:19:54 -0600600 /*
601 * If we're merging merge-bases, we don't want to bother with
602 * any working directory changes.
603 */
Elijah Newren5bf7e572019-08-17 11:41:41 -0700604 if (opt->priv->call_depth)
Elijah Newren0b30e812011-08-11 23:19:54 -0600605 return;
606
Elijah Newrenf0fd4d02011-08-11 23:19:56 -0600607 /* Ensure D/F conflicts are adjacent in the entries list. */
Elijah Newrenef02b312010-09-20 02:29:09 -0600608 for (i = 0; i < entries->nr; i++) {
Elijah Newrenf701aae2011-08-12 20:23:51 -0600609 struct string_list_item *next = &entries->items[i];
610 string_list_append(&df_sorted_entries, next->string)->util =
611 next->util;
612 }
Nguyễn Thái Ngọc Duyfa6ca112016-11-24 18:45:36 +0700613 df_sorted_entries.cmp = string_list_df_name_compare;
614 string_list_sort(&df_sorted_entries);
Elijah Newrenf0fd4d02011-08-11 23:19:56 -0600615
Elijah Newren5bf7e572019-08-17 11:41:41 -0700616 string_list_clear(&opt->priv->df_conflict_file_set, 1);
Elijah Newrenf701aae2011-08-12 20:23:51 -0600617 for (i = 0; i < df_sorted_entries.nr; i++) {
618 const char *path = df_sorted_entries.items[i].string;
Elijah Newrenef02b312010-09-20 02:29:09 -0600619 int len = strlen(path);
Elijah Newrenf701aae2011-08-12 20:23:51 -0600620 struct stage_data *e = df_sorted_entries.items[i].util;
Elijah Newrenef02b312010-09-20 02:29:09 -0600621
622 /*
623 * Check if last_file & path correspond to a D/F conflict;
624 * i.e. whether path is last_file+'/'+<something>.
Elijah Newren70cc3d32011-08-11 23:19:58 -0600625 * If so, record that it's okay to remove last_file to make
626 * room for path and friends if needed.
Elijah Newrenef02b312010-09-20 02:29:09 -0600627 */
628 if (last_file &&
629 len > last_len &&
630 memcmp(path, last_file, last_len) == 0 &&
631 path[last_len] == '/') {
Elijah Newren5bf7e572019-08-17 11:41:41 -0700632 string_list_insert(&opt->priv->df_conflict_file_set, last_file);
Elijah Newrenef02b312010-09-20 02:29:09 -0600633 }
634
635 /*
636 * Determine whether path could exist as a file in the
637 * working directory as a possible D/F conflict. This
638 * will only occur when it exists in stage 2 as a
639 * file.
640 */
641 if (S_ISREG(e->stages[2].mode) || S_ISLNK(e->stages[2].mode)) {
642 last_file = path;
643 last_len = len;
Elijah Newrenef02b312010-09-20 02:29:09 -0600644 } else {
645 last_file = NULL;
646 }
647 }
Elijah Newrenf701aae2011-08-12 20:23:51 -0600648 string_list_clear(&df_sorted_entries, 0);
Elijah Newrenef02b312010-09-20 02:29:09 -0600649}
650
Johannes Schindelinbc9204d2016-08-01 13:44:37 +0200651static int update_stages(struct merge_options *opt, const char *path,
652 const struct diff_filespec *o,
Elijah Newren650467c2011-08-11 23:19:52 -0600653 const struct diff_filespec *a,
654 const struct diff_filespec *b)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200655{
Elijah Newrenf53d3972011-08-11 23:20:25 -0600656
657 /*
658 * NOTE: It is usually a bad idea to call update_stages on a path
659 * before calling update_file on that same path, since it can
660 * sometimes lead to spurious "refusing to lose untracked file..."
661 * messages from update_file (via make_room_for path via
662 * would_lose_untracked). Instead, reverse the order of the calls
663 * (executing update_file first and then update_stages).
664 */
Elijah Newren650467c2011-08-11 23:19:52 -0600665 int clear = 1;
666 int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_SKIP_DFCHECK;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200667 if (clear)
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700668 if (remove_file_from_index(opt->repo->index, path))
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200669 return -1;
670 if (o)
Elijah Newren8daec1d2019-04-05 08:00:22 -0700671 if (add_cacheinfo(opt, o, path, 1, 0, options))
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200672 return -1;
673 if (a)
Elijah Newren8daec1d2019-04-05 08:00:22 -0700674 if (add_cacheinfo(opt, a, path, 2, 0, options))
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200675 return -1;
676 if (b)
Elijah Newren8daec1d2019-04-05 08:00:22 -0700677 if (add_cacheinfo(opt, b, path, 3, 0, options))
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200678 return -1;
679 return 0;
680}
681
Elijah Newrenb8ddf162011-08-11 23:20:02 -0600682static void update_entry(struct stage_data *entry,
683 struct diff_filespec *o,
684 struct diff_filespec *a,
685 struct diff_filespec *b)
Elijah Newren2ff739f2010-09-20 02:28:54 -0600686{
Elijah Newren2ff739f2010-09-20 02:28:54 -0600687 entry->processed = 0;
688 entry->stages[1].mode = o->mode;
689 entry->stages[2].mode = a->mode;
690 entry->stages[3].mode = b->mode;
brian m. carlsonfd429e92016-06-24 23:09:25 +0000691 oidcpy(&entry->stages[1].oid, &o->oid);
692 oidcpy(&entry->stages[2].oid, &a->oid);
693 oidcpy(&entry->stages[3].oid, &b->oid);
Elijah Newren2ff739f2010-09-20 02:28:54 -0600694}
695
Elijah Newren259ccb62019-04-05 08:00:13 -0700696static int remove_file(struct merge_options *opt, int clean,
Miklos Vajnab7fa51d2008-09-02 23:53:47 +0200697 const char *path, int no_wd)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200698{
Elijah Newren5bf7e572019-08-17 11:41:41 -0700699 int update_cache = opt->priv->call_depth || clean;
700 int update_working_directory = !opt->priv->call_depth && !no_wd;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200701
702 if (update_cache) {
Elijah Newren259ccb62019-04-05 08:00:13 -0700703 if (remove_file_from_index(opt->repo->index, path))
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200704 return -1;
705 }
706 if (update_working_directory) {
David Turnerae352c72014-05-01 20:21:09 -0400707 if (ignore_case) {
708 struct cache_entry *ce;
Elijah Newren259ccb62019-04-05 08:00:13 -0700709 ce = index_file_exists(opt->repo->index, path, strlen(path),
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700710 ignore_case);
Elijah Newren4cba2b02017-11-24 11:59:01 -0800711 if (ce && ce_stage(ce) == 0 && strcmp(path, ce->name))
David Turnerae352c72014-05-01 20:21:09 -0400712 return 0;
713 }
Peter Collingbourne25755e82010-03-26 15:25:35 +0000714 if (remove_path(path))
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200715 return -1;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200716 }
717 return 0;
718}
719
Jeff King45bc1312014-06-19 17:30:26 -0400720/* add a string to a strbuf, but converting "/" to "_" */
721static void add_flattened_path(struct strbuf *out, const char *s)
722{
723 size_t i = out->len;
724 strbuf_addstr(out, s);
725 for (; i < out->len; i++)
726 if (out->buf[i] == '/')
727 out->buf[i] = '_';
728}
729
Elijah Newren4d7101e2019-08-17 11:41:33 -0700730static char *unique_path(struct merge_options *opt,
731 const char *path,
732 const char *branch)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200733{
Kevin Willfordfc65b002017-09-07 10:25:56 -0600734 struct path_hashmap_entry *entry;
Jeff King45bc1312014-06-19 17:30:26 -0400735 struct strbuf newpath = STRBUF_INIT;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200736 int suffix = 0;
Jeff King45bc1312014-06-19 17:30:26 -0400737 size_t base_len;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200738
Jeff King45bc1312014-06-19 17:30:26 -0400739 strbuf_addf(&newpath, "%s~", path);
740 add_flattened_path(&newpath, branch);
741
742 base_len = newpath.len;
Elijah Newren5bf7e572019-08-17 11:41:41 -0700743 while (hashmap_get_from_hash(&opt->priv->current_file_dir_set,
René Scharfe74318422021-07-30 21:06:58 +0200744 fspathhash(newpath.buf), newpath.buf) ||
Elijah Newren5bf7e572019-08-17 11:41:41 -0700745 (!opt->priv->call_depth && file_exists(newpath.buf))) {
Jeff King45bc1312014-06-19 17:30:26 -0400746 strbuf_setlen(&newpath, base_len);
747 strbuf_addf(&newpath, "_%d", suffix++);
748 }
749
Kevin Willfordfc65b002017-09-07 10:25:56 -0600750 FLEX_ALLOC_MEM(entry, path, newpath.buf, newpath.len);
René Scharfe74318422021-07-30 21:06:58 +0200751 hashmap_entry_init(&entry->e, fspathhash(entry->path));
Junio C Hamano5efabc72019-10-15 13:48:01 +0900752 hashmap_add(&opt->priv->current_file_dir_set, &entry->e);
Jeff King45bc1312014-06-19 17:30:26 -0400753 return strbuf_detach(&newpath, NULL);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200754}
755
David Turner5423d2e2016-11-07 13:31:31 -0500756/**
757 * Check whether a directory in the index is in the way of an incoming
758 * file. Return 1 if so. If check_working_copy is non-zero, also
759 * check the working directory. If empty_ok is non-zero, also return
760 * 0 in the case where the working-tree dir exists but is empty.
761 */
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700762static int dir_in_way(struct index_state *istate, const char *path,
763 int check_working_copy, int empty_ok)
Elijah Newrenf2507b42011-08-11 23:19:57 -0600764{
Jeff Kingb4600fb2015-09-24 17:07:43 -0400765 int pos;
766 struct strbuf dirpath = STRBUF_INIT;
Elijah Newrenf2507b42011-08-11 23:19:57 -0600767 struct stat st;
768
Jeff Kingb4600fb2015-09-24 17:07:43 -0400769 strbuf_addstr(&dirpath, path);
770 strbuf_addch(&dirpath, '/');
Elijah Newrenf2507b42011-08-11 23:19:57 -0600771
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700772 pos = index_name_pos(istate, dirpath.buf, dirpath.len);
Elijah Newrenf2507b42011-08-11 23:19:57 -0600773
774 if (pos < 0)
775 pos = -1 - pos;
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700776 if (pos < istate->cache_nr &&
777 !strncmp(dirpath.buf, istate->cache[pos]->name, dirpath.len)) {
Jeff Kingb4600fb2015-09-24 17:07:43 -0400778 strbuf_release(&dirpath);
Elijah Newrenf2507b42011-08-11 23:19:57 -0600779 return 1;
780 }
781
Jeff Kingb4600fb2015-09-24 17:07:43 -0400782 strbuf_release(&dirpath);
David Turner5423d2e2016-11-07 13:31:31 -0500783 return check_working_copy && !lstat(path, &st) && S_ISDIR(st.st_mode) &&
Jonathan Tan83e3ad32019-09-18 13:27:38 -0700784 !(empty_ok && is_empty_dir(path)) &&
785 !has_symlink_leading_path(path, strlen(path));
Elijah Newrenf2507b42011-08-11 23:19:57 -0600786}
787
Elijah Newrena35edc82018-04-19 10:58:20 -0700788/*
Elijah Newren1de70db2018-04-19 10:58:23 -0700789 * Returns whether path was tracked in the index before the merge started,
790 * and its oid and mode match the specified values
791 */
Elijah Newren259ccb62019-04-05 08:00:13 -0700792static int was_tracked_and_matches(struct merge_options *opt, const char *path,
Elijah Newren8daec1d2019-04-05 08:00:22 -0700793 const struct diff_filespec *blob)
Junio C Hamano60c91182008-12-15 02:41:24 -0800794{
Elijah Newren5bf7e572019-08-17 11:41:41 -0700795 int pos = index_name_pos(&opt->priv->orig_index, path, strlen(path));
Elijah Newren1de70db2018-04-19 10:58:23 -0700796 struct cache_entry *ce;
797
798 if (0 > pos)
799 /* we were not tracking this path before the merge */
800 return 0;
801
802 /* See if the file we were tracking before matches */
Elijah Newren5bf7e572019-08-17 11:41:41 -0700803 ce = opt->priv->orig_index.cache[pos];
Elijah Newren763a59e2020-01-01 05:20:57 +0000804 return (oideq(&ce->oid, &blob->oid) && ce->ce_mode == blob->mode);
Elijah Newren1de70db2018-04-19 10:58:23 -0700805}
806
807/*
Elijah Newrena35edc82018-04-19 10:58:20 -0700808 * Returns whether path was tracked in the index before the merge started
809 */
Elijah Newren259ccb62019-04-05 08:00:13 -0700810static int was_tracked(struct merge_options *opt, const char *path)
Junio C Hamano60c91182008-12-15 02:41:24 -0800811{
Elijah Newren5bf7e572019-08-17 11:41:41 -0700812 int pos = index_name_pos(&opt->priv->orig_index, path, strlen(path));
Junio C Hamano60c91182008-12-15 02:41:24 -0800813
Johannes Schindelinf8d83fb2016-07-26 18:05:57 +0200814 if (0 <= pos)
Elijah Newrena35edc82018-04-19 10:58:20 -0700815 /* we were tracking this path before the merge */
Johannes Schindelinf8d83fb2016-07-26 18:05:57 +0200816 return 1;
817
Elijah Newrenaacb82d2011-08-11 23:19:59 -0600818 return 0;
Junio C Hamano60c91182008-12-15 02:41:24 -0800819}
820
Elijah Newren259ccb62019-04-05 08:00:13 -0700821static int would_lose_untracked(struct merge_options *opt, const char *path)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200822{
Elijah Newren259ccb62019-04-05 08:00:13 -0700823 struct index_state *istate = opt->repo->index;
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700824
Elijah Newrena35edc82018-04-19 10:58:20 -0700825 /*
826 * This may look like it can be simplified to:
Elijah Newren259ccb62019-04-05 08:00:13 -0700827 * return !was_tracked(opt, path) && file_exists(path)
Elijah Newrena35edc82018-04-19 10:58:20 -0700828 * but it can't. This function needs to know whether path was in
829 * the working tree due to EITHER having been tracked in the index
830 * before the merge OR having been put into the working copy and
831 * index by unpack_trees(). Due to that either-or requirement, we
832 * check the current index instead of the original one.
833 *
834 * Note that we do not need to worry about merge-recursive itself
835 * updating the index after unpack_trees() and before calling this
836 * function, because we strictly require all code paths in
837 * merge-recursive to update the working tree first and the index
838 * second. Doing otherwise would break
839 * update_file()/would_lose_untracked(); see every comment in this
840 * file which mentions "update_stages".
841 */
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700842 int pos = index_name_pos(istate, path, strlen(path));
Elijah Newrena35edc82018-04-19 10:58:20 -0700843
844 if (pos < 0)
845 pos = -1 - pos;
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700846 while (pos < istate->cache_nr &&
847 !strcmp(path, istate->cache[pos]->name)) {
Elijah Newrena35edc82018-04-19 10:58:20 -0700848 /*
849 * If stage #0, it is definitely tracked.
850 * If it has stage #2 then it was tracked
851 * before this merge started. All other
852 * cases the path was not tracked.
853 */
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700854 switch (ce_stage(istate->cache[pos])) {
Elijah Newrena35edc82018-04-19 10:58:20 -0700855 case 0:
856 case 2:
857 return 0;
858 }
859 pos++;
860 }
861 return file_exists(path);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200862}
863
Elijah Newren259ccb62019-04-05 08:00:13 -0700864static int was_dirty(struct merge_options *opt, const char *path)
Elijah Newren64b1abe2018-04-19 10:58:12 -0700865{
866 struct cache_entry *ce;
867 int dirty = 1;
868
Elijah Newren5bf7e572019-08-17 11:41:41 -0700869 if (opt->priv->call_depth || !was_tracked(opt, path))
Elijah Newren64b1abe2018-04-19 10:58:12 -0700870 return !dirty;
871
Elijah Newren5bf7e572019-08-17 11:41:41 -0700872 ce = index_file_exists(opt->priv->unpack_opts.src_index,
Elijah Newren277292d2018-04-19 10:58:21 -0700873 path, strlen(path), ignore_case);
Elijah Newren5bf7e572019-08-17 11:41:41 -0700874 dirty = verify_uptodate(ce, &opt->priv->unpack_opts) != 0;
Elijah Newren64b1abe2018-04-19 10:58:12 -0700875 return dirty;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200876}
877
Elijah Newren259ccb62019-04-05 08:00:13 -0700878static int make_room_for_path(struct merge_options *opt, const char *path)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200879{
Elijah Newrened0148a2011-08-11 23:20:01 -0600880 int status, i;
Jiang Xin55653a62012-07-25 22:53:13 +0800881 const char *msg = _("failed to create path '%s'%s");
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200882
Elijah Newrened0148a2011-08-11 23:20:01 -0600883 /* Unlink any D/F conflict files that are in the way */
Elijah Newren5bf7e572019-08-17 11:41:41 -0700884 for (i = 0; i < opt->priv->df_conflict_file_set.nr; i++) {
885 const char *df_path = opt->priv->df_conflict_file_set.items[i].string;
Elijah Newrened0148a2011-08-11 23:20:01 -0600886 size_t pathlen = strlen(path);
887 size_t df_pathlen = strlen(df_path);
888 if (df_pathlen < pathlen &&
889 path[df_pathlen] == '/' &&
890 strncmp(path, df_path, df_pathlen) == 0) {
Elijah Newren259ccb62019-04-05 08:00:13 -0700891 output(opt, 3,
Jiang Xin55653a62012-07-25 22:53:13 +0800892 _("Removing %s to make room for subdirectory\n"),
Elijah Newrened0148a2011-08-11 23:20:01 -0600893 df_path);
894 unlink(df_path);
Elijah Newren5bf7e572019-08-17 11:41:41 -0700895 unsorted_string_list_delete_item(&opt->priv->df_conflict_file_set,
Elijah Newrened0148a2011-08-11 23:20:01 -0600896 i, 0);
897 break;
898 }
899 }
900
901 /* Make sure leading directories are created */
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200902 status = safe_create_leading_directories_const(path);
903 if (status) {
Johannes Schindelin60033032016-07-26 18:06:26 +0200904 if (status == SCLD_EXISTS)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200905 /* something else exists */
Elijah Newren259ccb62019-04-05 08:00:13 -0700906 return err(opt, msg, path, _(": perhaps a D/F conflict?"));
907 return err(opt, msg, path, "");
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200908 }
909
Junio C Hamano60c91182008-12-15 02:41:24 -0800910 /*
911 * Do not unlink a file in the work tree if we are not
912 * tracking it.
913 */
Elijah Newren259ccb62019-04-05 08:00:13 -0700914 if (would_lose_untracked(opt, path))
915 return err(opt, _("refusing to lose untracked file at '%s'"),
Elijah Newrend90e7592018-06-09 21:16:12 -0700916 path);
Junio C Hamano60c91182008-12-15 02:41:24 -0800917
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200918 /* Successful unlink is good.. */
919 if (!unlink(path))
920 return 0;
921 /* .. and so is no existing file */
922 if (errno == ENOENT)
923 return 0;
924 /* .. but not some other error (who really cares what?) */
Elijah Newren259ccb62019-04-05 08:00:13 -0700925 return err(opt, msg, path, _(": perhaps a D/F conflict?"));
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200926}
927
Elijah Newren259ccb62019-04-05 08:00:13 -0700928static int update_file_flags(struct merge_options *opt,
Elijah Newren8daec1d2019-04-05 08:00:22 -0700929 const struct diff_filespec *contents,
Johannes Schindelin75456f92016-07-26 18:06:21 +0200930 const char *path,
931 int update_cache,
932 int update_wd)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200933{
Johannes Schindelin60033032016-07-26 18:06:26 +0200934 int ret = 0;
935
Elijah Newren5bf7e572019-08-17 11:41:41 -0700936 if (opt->priv->call_depth)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200937 update_wd = 0;
938
939 if (update_wd) {
940 enum object_type type;
941 void *buf;
942 unsigned long size;
943
Elijah Newren8daec1d2019-04-05 08:00:22 -0700944 if (S_ISGITLINK(contents->mode)) {
Junio C Hamano0c44c942009-04-29 11:08:18 -0700945 /*
946 * We may later decide to recursively descend into
947 * the submodule directory and update its index
948 * and/or work tree, but we do not do that now.
949 */
Heiko Voigt68d03e42010-07-07 15:39:13 +0200950 update_wd = 0;
Junio C Hamano0c44c942009-04-29 11:08:18 -0700951 goto update_index;
Heiko Voigt68d03e42010-07-07 15:39:13 +0200952 }
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200953
Elijah Newren8daec1d2019-04-05 08:00:22 -0700954 buf = read_object_file(&contents->oid, &type, &size);
Elijah Newrenf836bf32019-08-17 11:41:26 -0700955 if (!buf) {
956 ret = err(opt, _("cannot read object %s '%s'"),
957 oid_to_hex(&contents->oid), path);
958 goto free_buf;
959 }
Johannes Schindelin60033032016-07-26 18:06:26 +0200960 if (type != OBJ_BLOB) {
Elijah Newren8daec1d2019-04-05 08:00:22 -0700961 ret = err(opt, _("blob expected for %s '%s'"),
962 oid_to_hex(&contents->oid), path);
Johannes Schindelin60033032016-07-26 18:06:26 +0200963 goto free_buf;
964 }
Elijah Newren8daec1d2019-04-05 08:00:22 -0700965 if (S_ISREG(contents->mode)) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500966 struct strbuf strbuf = STRBUF_INIT;
Elijah Newren4d7101e2019-08-17 11:41:33 -0700967 if (convert_to_working_tree(opt->repo->index,
brian m. carlsonab90eca2020-03-16 18:05:02 +0000968 path, buf, size, &strbuf, NULL)) {
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200969 free(buf);
970 size = strbuf.len;
971 buf = strbuf_detach(&strbuf, NULL);
972 }
973 }
974
Elijah Newren259ccb62019-04-05 08:00:13 -0700975 if (make_room_for_path(opt, path) < 0) {
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200976 update_wd = 0;
Johannes Schindelin75456f92016-07-26 18:06:21 +0200977 goto free_buf;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200978 }
Elijah Newren8daec1d2019-04-05 08:00:22 -0700979 if (S_ISREG(contents->mode) ||
980 (!has_symlinks && S_ISLNK(contents->mode))) {
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200981 int fd;
Elijah Newren8daec1d2019-04-05 08:00:22 -0700982 int mode = (contents->mode & 0100 ? 0777 : 0666);
983
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200984 fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);
Johannes Schindelin60033032016-07-26 18:06:26 +0200985 if (fd < 0) {
Elijah Newren259ccb62019-04-05 08:00:13 -0700986 ret = err(opt, _("failed to open '%s': %s"),
Johannes Schindelinbc9204d2016-08-01 13:44:37 +0200987 path, strerror(errno));
Johannes Schindelin60033032016-07-26 18:06:26 +0200988 goto free_buf;
989 }
Thomas Rastf633ea22012-08-03 14:16:25 +0200990 write_in_full(fd, buf, size);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200991 close(fd);
Elijah Newren8daec1d2019-04-05 08:00:22 -0700992 } else if (S_ISLNK(contents->mode)) {
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200993 char *lnk = xmemdupz(buf, size);
994 safe_create_leading_directories_const(path);
995 unlink(path);
Alex Riesen304dcf22008-12-05 01:39:14 +0100996 if (symlink(lnk, path))
Elijah Newren259ccb62019-04-05 08:00:13 -0700997 ret = err(opt, _("failed to symlink '%s': %s"),
Elijah Newrend90e7592018-06-09 21:16:12 -0700998 path, strerror(errno));
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200999 free(lnk);
1000 } else
Elijah Newren259ccb62019-04-05 08:00:13 -07001001 ret = err(opt,
Johannes Schindelinbc9204d2016-08-01 13:44:37 +02001002 _("do not know what to do with %06o %s '%s'"),
Elijah Newren8daec1d2019-04-05 08:00:22 -07001003 contents->mode, oid_to_hex(&contents->oid), path);
Elijah Newren93665362018-06-09 21:16:13 -07001004 free_buf:
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001005 free(buf);
1006 }
Elijah Newren93665362018-06-09 21:16:13 -07001007update_index:
Elijah Newrenfb1c18f2020-02-19 17:04:07 +00001008 if (!ret && update_cache) {
1009 int refresh = (!opt->priv->call_depth &&
1010 contents->mode != S_IFGITLINK);
1011 if (add_cacheinfo(opt, contents, path, 0, refresh,
Elijah Newrenfd53b7ff2018-04-19 10:58:16 -07001012 ADD_CACHE_OK_TO_ADD))
1013 return -1;
Elijah Newrenfb1c18f2020-02-19 17:04:07 +00001014 }
Johannes Schindelin60033032016-07-26 18:06:26 +02001015 return ret;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001016}
1017
Elijah Newren259ccb62019-04-05 08:00:13 -07001018static int update_file(struct merge_options *opt,
Johannes Schindelin75456f92016-07-26 18:06:21 +02001019 int clean,
Elijah Newren8daec1d2019-04-05 08:00:22 -07001020 const struct diff_filespec *contents,
Johannes Schindelin75456f92016-07-26 18:06:21 +02001021 const char *path)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001022{
Elijah Newren8daec1d2019-04-05 08:00:22 -07001023 return update_file_flags(opt, contents, path,
Elijah Newren5bf7e572019-08-17 11:41:41 -07001024 opt->priv->call_depth || clean, !opt->priv->call_depth);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001025}
1026
1027/* Low level file merging, update and removal */
1028
Jonathan Nieder9cba13c2011-03-16 02:08:34 -05001029struct merge_file_info {
Elijah Newren8daec1d2019-04-05 08:00:22 -07001030 struct diff_filespec blob; /* mostly use oid & mode; sometimes path */
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001031 unsigned clean:1,
1032 merge:1;
1033};
1034
Elijah Newren259ccb62019-04-05 08:00:13 -07001035static int merge_3way(struct merge_options *opt,
Miklos Vajnab7fa51d2008-09-02 23:53:47 +02001036 mmbuffer_t *result_buf,
Elijah Newrene3de8882019-04-05 08:00:14 -07001037 const struct diff_filespec *o,
Elijah Newren0c059422011-08-11 23:19:51 -06001038 const struct diff_filespec *a,
1039 const struct diff_filespec *b,
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001040 const char *branch1,
Elijah Newrenb2a79422018-11-07 20:40:24 -08001041 const char *branch2,
1042 const int extra_marker_size)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001043{
1044 mmfile_t orig, src1, src2;
Jonathan Nieder712516b2010-08-26 00:49:53 -05001045 struct ll_merge_options ll_opts = {0};
Elijah Newren139ef372019-08-15 14:40:32 -07001046 char *base, *name1, *name2;
Elijah Newren35f69672022-02-02 02:37:30 +00001047 enum ll_merge_result merge_status;
Avery Pennarun8cc5b292009-11-25 21:23:55 -05001048
Elijah Newren259ccb62019-04-05 08:00:13 -07001049 ll_opts.renormalize = opt->renormalize;
Elijah Newrenb2a79422018-11-07 20:40:24 -08001050 ll_opts.extra_marker_size = extra_marker_size;
Elijah Newren259ccb62019-04-05 08:00:13 -07001051 ll_opts.xdl_opts = opt->xdl_opts;
Jonathan Nieder712516b2010-08-26 00:49:53 -05001052
Elijah Newren5bf7e572019-08-17 11:41:41 -07001053 if (opt->priv->call_depth) {
Jonathan Nieder712516b2010-08-26 00:49:53 -05001054 ll_opts.virtual_ancestor = 1;
1055 ll_opts.variant = 0;
1056 } else {
Elijah Newren259ccb62019-04-05 08:00:13 -07001057 switch (opt->recursive_variant) {
Elijah Newrenf3081da2019-08-17 11:41:42 -07001058 case MERGE_VARIANT_OURS:
Jonathan Nieder712516b2010-08-26 00:49:53 -05001059 ll_opts.variant = XDL_MERGE_FAVOR_OURS;
Avery Pennarun8cc5b292009-11-25 21:23:55 -05001060 break;
Elijah Newrenf3081da2019-08-17 11:41:42 -07001061 case MERGE_VARIANT_THEIRS:
Jonathan Nieder712516b2010-08-26 00:49:53 -05001062 ll_opts.variant = XDL_MERGE_FAVOR_THEIRS;
Avery Pennarun8cc5b292009-11-25 21:23:55 -05001063 break;
1064 default:
Jonathan Nieder712516b2010-08-26 00:49:53 -05001065 ll_opts.variant = 0;
Avery Pennarun8cc5b292009-11-25 21:23:55 -05001066 break;
1067 }
1068 }
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001069
Elijah Newren139ef372019-08-15 14:40:32 -07001070 assert(a->path && b->path && o->path && opt->ancestor);
1071 if (strcmp(a->path, b->path) || strcmp(a->path, o->path) != 0) {
1072 base = mkpathdup("%s:%s", opt->ancestor, o->path);
Ramsay Jones4e2d0942012-09-04 18:31:14 +01001073 name1 = mkpathdup("%s:%s", branch1, a->path);
1074 name2 = mkpathdup("%s:%s", branch2, b->path);
Martin Renold606475f2009-07-01 22:18:04 +02001075 } else {
Elijah Newren139ef372019-08-15 14:40:32 -07001076 base = mkpathdup("%s", opt->ancestor);
Ramsay Jones4e2d0942012-09-04 18:31:14 +01001077 name1 = mkpathdup("%s", branch1);
1078 name2 = mkpathdup("%s", branch2);
Martin Renold606475f2009-07-01 22:18:04 +02001079 }
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001080
Elijah Newrene3de8882019-04-05 08:00:14 -07001081 read_mmblob(&orig, &o->oid);
brian m. carlsond4493472016-09-05 20:08:02 +00001082 read_mmblob(&src1, &a->oid);
1083 read_mmblob(&src2, &b->oid);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001084
Elijah Newren816147e2021-03-20 00:03:53 +00001085 /*
1086 * FIXME: Using a->path for normalization rules in ll_merge could be
1087 * wrong if we renamed from a->path to b->path. We should use the
1088 * target path for where the file will be written.
1089 */
Elijah Newren139ef372019-08-15 14:40:32 -07001090 merge_status = ll_merge(result_buf, a->path, &orig, base,
Nguyễn Thái Ngọc Duy32eaa462018-09-21 17:57:27 +02001091 &src1, name1, &src2, name2,
Elijah Newren259ccb62019-04-05 08:00:13 -07001092 opt->repo->index, &ll_opts);
Elijah Newren35f69672022-02-02 02:37:30 +00001093 if (merge_status == LL_MERGE_BINARY_CONFLICT)
1094 warning("Cannot merge binary files: %s (%s vs. %s)",
1095 a->path, name1, name2);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001096
Elijah Newren139ef372019-08-15 14:40:32 -07001097 free(base);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001098 free(name1);
1099 free(name2);
1100 free(orig.ptr);
1101 free(src1.ptr);
1102 free(src2.ptr);
1103 return merge_status;
1104}
1105
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +07001106static int find_first_merges(struct repository *repo,
1107 struct object_array *result, const char *path,
Elijah Newrend90e7592018-06-09 21:16:12 -07001108 struct commit *a, struct commit *b)
Stefan Beller18cfc082018-05-15 13:00:28 -07001109{
1110 int i, j;
1111 struct object_array merges = OBJECT_ARRAY_INIT;
1112 struct commit *commit;
1113 int contains_another;
1114
brian m. carlsondb1ba2a2019-02-19 00:04:59 +00001115 char merged_revision[GIT_MAX_HEXSZ + 2];
Stefan Beller18cfc082018-05-15 13:00:28 -07001116 const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path",
1117 "--all", merged_revision, NULL };
1118 struct rev_info revs;
1119 struct setup_revision_opt rev_opts;
1120
1121 memset(result, 0, sizeof(struct object_array));
1122 memset(&rev_opts, 0, sizeof(rev_opts));
1123
1124 /* get all revisions that merge commit a */
1125 xsnprintf(merged_revision, sizeof(merged_revision), "^%s",
Elijah Newrend90e7592018-06-09 21:16:12 -07001126 oid_to_hex(&a->object.oid));
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +07001127 repo_init_revisions(repo, &revs, NULL);
Stefan Beller18cfc082018-05-15 13:00:28 -07001128 /* FIXME: can't handle linked worktrees in submodules yet */
1129 revs.single_worktree = path != NULL;
1130 setup_revisions(ARRAY_SIZE(rev_args)-1, rev_args, &revs, &rev_opts);
1131
1132 /* save all revisions from the above list that contain b */
1133 if (prepare_revision_walk(&revs))
1134 die("revision walk setup failed");
1135 while ((commit = get_revision(&revs)) != NULL) {
1136 struct object *o = &(commit->object);
Jonathan Tan10a0d6a2021-09-09 11:47:29 -07001137 if (repo_in_merge_bases(repo, b, commit))
Stefan Beller18cfc082018-05-15 13:00:28 -07001138 add_object_array(o, NULL, &merges);
1139 }
1140 reset_revision_walk();
1141
1142 /* Now we've got all merges that contain a and b. Prune all
1143 * merges that contain another found merge and save them in
1144 * result.
1145 */
1146 for (i = 0; i < merges.nr; i++) {
1147 struct commit *m1 = (struct commit *) merges.objects[i].item;
1148
1149 contains_another = 0;
1150 for (j = 0; j < merges.nr; j++) {
1151 struct commit *m2 = (struct commit *) merges.objects[j].item;
Jonathan Tan10a0d6a2021-09-09 11:47:29 -07001152 if (i != j && repo_in_merge_bases(repo, m2, m1)) {
Stefan Beller18cfc082018-05-15 13:00:28 -07001153 contains_another = 1;
1154 break;
1155 }
1156 }
1157
1158 if (!contains_another)
1159 add_object_array(merges.objects[i].item, NULL, result);
1160 }
1161
1162 object_array_clear(&merges);
1163 return result->nr;
1164}
1165
Jonathan Tan155b5172021-10-08 14:08:17 -07001166static void print_commit(struct repository *repo, struct commit *commit)
Stefan Beller18cfc082018-05-15 13:00:28 -07001167{
1168 struct strbuf sb = STRBUF_INIT;
1169 struct pretty_print_context ctx = {0};
1170 ctx.date_mode.type = DATE_NORMAL;
Elijah Newren816147e2021-03-20 00:03:53 +00001171 /* FIXME: Merge this with output_commit_title() */
1172 assert(!merge_remote_util(commit));
Jonathan Tan155b5172021-10-08 14:08:17 -07001173 repo_format_commit_message(repo, commit, " %h: %m %s", &sb, &ctx);
Stefan Beller18cfc082018-05-15 13:00:28 -07001174 fprintf(stderr, "%s\n", sb.buf);
1175 strbuf_release(&sb);
1176}
1177
Elijah Newren8daec1d2019-04-05 08:00:22 -07001178static int is_valid(const struct diff_filespec *dfs)
1179{
1180 return dfs->mode != 0 && !is_null_oid(&dfs->oid);
1181}
1182
Elijah Newren259ccb62019-04-05 08:00:13 -07001183static int merge_submodule(struct merge_options *opt,
Stefan Beller325f3a82018-05-15 13:00:29 -07001184 struct object_id *result, const char *path,
Stefan Beller18cfc082018-05-15 13:00:28 -07001185 const struct object_id *base, const struct object_id *a,
Stefan Beller325f3a82018-05-15 13:00:29 -07001186 const struct object_id *b)
Stefan Beller18cfc082018-05-15 13:00:28 -07001187{
Jonathan Tan10a0d6a2021-09-09 11:47:29 -07001188 struct repository subrepo;
1189 int ret = 0;
Stefan Beller18cfc082018-05-15 13:00:28 -07001190 struct commit *commit_base, *commit_a, *commit_b;
1191 int parent_count;
1192 struct object_array merges;
1193
1194 int i;
Elijah Newren5bf7e572019-08-17 11:41:41 -07001195 int search = !opt->priv->call_depth;
Stefan Beller18cfc082018-05-15 13:00:28 -07001196
1197 /* store a in result in case we fail */
Elijah Newren816147e2021-03-20 00:03:53 +00001198 /* FIXME: This is the WRONG resolution for the recursive case when
1199 * we need to be careful to avoid accidentally matching either side.
1200 * Should probably use o instead there, much like we do for merging
1201 * binaries.
1202 */
Stefan Beller18cfc082018-05-15 13:00:28 -07001203 oidcpy(result, a);
1204
1205 /* we can not handle deletion conflicts */
1206 if (is_null_oid(base))
1207 return 0;
1208 if (is_null_oid(a))
1209 return 0;
1210 if (is_null_oid(b))
1211 return 0;
1212
Jonathan Tan10a0d6a2021-09-09 11:47:29 -07001213 if (repo_submodule_init(&subrepo, opt->repo, path, null_oid())) {
1214 output(opt, 1, _("Failed to merge submodule %s (not checked out)"), path);
Stefan Beller18cfc082018-05-15 13:00:28 -07001215 return 0;
1216 }
1217
Jonathan Tan10a0d6a2021-09-09 11:47:29 -07001218 if (!(commit_base = lookup_commit_reference(&subrepo, base)) ||
1219 !(commit_a = lookup_commit_reference(&subrepo, a)) ||
1220 !(commit_b = lookup_commit_reference(&subrepo, b))) {
1221 output(opt, 1, _("Failed to merge submodule %s (commits not present)"), path);
1222 goto cleanup;
1223 }
1224
Stefan Beller18cfc082018-05-15 13:00:28 -07001225 /* check whether both changes are forward */
Jonathan Tan10a0d6a2021-09-09 11:47:29 -07001226 if (!repo_in_merge_bases(&subrepo, commit_base, commit_a) ||
1227 !repo_in_merge_bases(&subrepo, commit_base, commit_b)) {
Elijah Newren259ccb62019-04-05 08:00:13 -07001228 output(opt, 1, _("Failed to merge submodule %s (commits don't follow merge-base)"), path);
Jonathan Tan10a0d6a2021-09-09 11:47:29 -07001229 goto cleanup;
Stefan Beller18cfc082018-05-15 13:00:28 -07001230 }
1231
1232 /* Case #1: a is contained in b or vice versa */
Jonathan Tan10a0d6a2021-09-09 11:47:29 -07001233 if (repo_in_merge_bases(&subrepo, commit_a, commit_b)) {
Stefan Beller18cfc082018-05-15 13:00:28 -07001234 oidcpy(result, b);
Elijah Newren259ccb62019-04-05 08:00:13 -07001235 if (show(opt, 3)) {
1236 output(opt, 3, _("Fast-forwarding submodule %s to the following commit:"), path);
Jonathan Tan155b5172021-10-08 14:08:17 -07001237 repo_output_commit_title(opt, &subrepo, commit_b);
Elijah Newren259ccb62019-04-05 08:00:13 -07001238 } else if (show(opt, 2))
1239 output(opt, 2, _("Fast-forwarding submodule %s"), path);
Leif Middelschulte76f42122018-05-17 11:40:08 -07001240 else
1241 ; /* no output */
1242
Jonathan Tan10a0d6a2021-09-09 11:47:29 -07001243 ret = 1;
1244 goto cleanup;
Stefan Beller18cfc082018-05-15 13:00:28 -07001245 }
Jonathan Tan10a0d6a2021-09-09 11:47:29 -07001246 if (repo_in_merge_bases(&subrepo, commit_b, commit_a)) {
Stefan Beller18cfc082018-05-15 13:00:28 -07001247 oidcpy(result, a);
Elijah Newren259ccb62019-04-05 08:00:13 -07001248 if (show(opt, 3)) {
1249 output(opt, 3, _("Fast-forwarding submodule %s to the following commit:"), path);
Jonathan Tan155b5172021-10-08 14:08:17 -07001250 repo_output_commit_title(opt, &subrepo, commit_a);
Elijah Newren259ccb62019-04-05 08:00:13 -07001251 } else if (show(opt, 2))
1252 output(opt, 2, _("Fast-forwarding submodule %s"), path);
Leif Middelschulte76f42122018-05-17 11:40:08 -07001253 else
1254 ; /* no output */
1255
Jonathan Tan10a0d6a2021-09-09 11:47:29 -07001256 ret = 1;
1257 goto cleanup;
Stefan Beller18cfc082018-05-15 13:00:28 -07001258 }
1259
1260 /*
1261 * Case #2: There are one or more merges that contain a and b in
1262 * the submodule. If there is only one, then present it as a
1263 * suggestion to the user, but leave it marked unmerged so the
1264 * user needs to confirm the resolution.
1265 */
1266
1267 /* Skip the search if makes no sense to the calling context. */
1268 if (!search)
Jonathan Tan10a0d6a2021-09-09 11:47:29 -07001269 goto cleanup;
Stefan Beller18cfc082018-05-15 13:00:28 -07001270
1271 /* find commit which merges them */
Jonathan Tan10a0d6a2021-09-09 11:47:29 -07001272 parent_count = find_first_merges(&subrepo, &merges, path,
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +07001273 commit_a, commit_b);
Stefan Beller18cfc082018-05-15 13:00:28 -07001274 switch (parent_count) {
1275 case 0:
Elijah Newren259ccb62019-04-05 08:00:13 -07001276 output(opt, 1, _("Failed to merge submodule %s (merge following commits not found)"), path);
Stefan Beller18cfc082018-05-15 13:00:28 -07001277 break;
1278
1279 case 1:
Elijah Newren259ccb62019-04-05 08:00:13 -07001280 output(opt, 1, _("Failed to merge submodule %s (not fast-forward)"), path);
1281 output(opt, 2, _("Found a possible merge resolution for the submodule:\n"));
Jonathan Tan155b5172021-10-08 14:08:17 -07001282 print_commit(&subrepo, (struct commit *) merges.objects[0].item);
Elijah Newren259ccb62019-04-05 08:00:13 -07001283 output(opt, 2, _(
Elijah Newrend90e7592018-06-09 21:16:12 -07001284 "If this is correct simply add it to the index "
1285 "for example\n"
1286 "by using:\n\n"
1287 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
1288 "which will accept this suggestion.\n"),
1289 oid_to_hex(&merges.objects[0].item->oid), path);
Stefan Beller18cfc082018-05-15 13:00:28 -07001290 break;
1291
1292 default:
Elijah Newren259ccb62019-04-05 08:00:13 -07001293 output(opt, 1, _("Failed to merge submodule %s (multiple merges found)"), path);
Stefan Beller18cfc082018-05-15 13:00:28 -07001294 for (i = 0; i < merges.nr; i++)
Jonathan Tan155b5172021-10-08 14:08:17 -07001295 print_commit(&subrepo, (struct commit *) merges.objects[i].item);
Stefan Beller18cfc082018-05-15 13:00:28 -07001296 }
1297
1298 object_array_clear(&merges);
Jonathan Tan10a0d6a2021-09-09 11:47:29 -07001299cleanup:
1300 repo_clear(&subrepo);
1301 return ret;
Stefan Beller18cfc082018-05-15 13:00:28 -07001302}
1303
Elijah Newren259ccb62019-04-05 08:00:13 -07001304static int merge_mode_and_contents(struct merge_options *opt,
Elijah Newrene3de8882019-04-05 08:00:14 -07001305 const struct diff_filespec *o,
Elijah Newrend9573552018-09-19 09:14:34 -07001306 const struct diff_filespec *a,
1307 const struct diff_filespec *b,
1308 const char *filename,
1309 const char *branch1,
1310 const char *branch2,
Elijah Newrenb2a79422018-11-07 20:40:24 -08001311 const int extra_marker_size,
Elijah Newrend9573552018-09-19 09:14:34 -07001312 struct merge_file_info *result)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001313{
Elijah Newren259ccb62019-04-05 08:00:13 -07001314 if (opt->branch1 != branch1) {
Elijah Newren4f445452018-10-16 13:19:48 -07001315 /*
1316 * It's weird getting a reverse merge with HEAD on the bottom
1317 * side of the conflict markers and the other branch on the
1318 * top. Fix that.
1319 */
Elijah Newrene3de8882019-04-05 08:00:14 -07001320 return merge_mode_and_contents(opt, o, b, a,
Elijah Newren4f445452018-10-16 13:19:48 -07001321 filename,
Elijah Newrenb2a79422018-11-07 20:40:24 -08001322 branch2, branch1,
1323 extra_marker_size, result);
Elijah Newren4f445452018-10-16 13:19:48 -07001324 }
1325
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +02001326 result->merge = 0;
1327 result->clean = 1;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001328
1329 if ((S_IFMT & a->mode) != (S_IFMT & b->mode)) {
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +02001330 result->clean = 0;
Elijah Newren816147e2021-03-20 00:03:53 +00001331 /*
1332 * FIXME: This is a bad resolution for recursive case; for
1333 * the recursive case we want something that is unlikely to
1334 * accidentally match either side. Also, while it makes
1335 * sense to prefer regular files over symlinks, it doesn't
1336 * make sense to prefer regular files over submodules.
1337 */
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001338 if (S_ISREG(a->mode)) {
Elijah Newren8daec1d2019-04-05 08:00:22 -07001339 result->blob.mode = a->mode;
1340 oidcpy(&result->blob.oid, &a->oid);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001341 } else {
Elijah Newren8daec1d2019-04-05 08:00:22 -07001342 result->blob.mode = b->mode;
1343 oidcpy(&result->blob.oid, &b->oid);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001344 }
1345 } else {
Elijah Newren763a59e2020-01-01 05:20:57 +00001346 if (!oideq(&a->oid, &o->oid) && !oideq(&b->oid, &o->oid))
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +02001347 result->merge = 1;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001348
1349 /*
1350 * Merge modes
1351 */
Elijah Newrene3de8882019-04-05 08:00:14 -07001352 if (a->mode == b->mode || a->mode == o->mode)
Elijah Newren8daec1d2019-04-05 08:00:22 -07001353 result->blob.mode = b->mode;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001354 else {
Elijah Newren8daec1d2019-04-05 08:00:22 -07001355 result->blob.mode = a->mode;
Elijah Newrene3de8882019-04-05 08:00:14 -07001356 if (b->mode != o->mode) {
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +02001357 result->clean = 0;
1358 result->merge = 1;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001359 }
1360 }
1361
Elijah Newren763a59e2020-01-01 05:20:57 +00001362 if (oideq(&a->oid, &b->oid) || oideq(&a->oid, &o->oid))
Elijah Newren8daec1d2019-04-05 08:00:22 -07001363 oidcpy(&result->blob.oid, &b->oid);
Elijah Newren763a59e2020-01-01 05:20:57 +00001364 else if (oideq(&b->oid, &o->oid))
Elijah Newren8daec1d2019-04-05 08:00:22 -07001365 oidcpy(&result->blob.oid, &a->oid);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001366 else if (S_ISREG(a->mode)) {
1367 mmbuffer_t result_buf;
Johannes Schindelin60033032016-07-26 18:06:26 +02001368 int ret = 0, merge_status;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001369
Elijah Newrene3de8882019-04-05 08:00:14 -07001370 merge_status = merge_3way(opt, &result_buf, o, a, b,
Elijah Newrenb2a79422018-11-07 20:40:24 -08001371 branch1, branch2,
1372 extra_marker_size);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001373
1374 if ((merge_status < 0) || !result_buf.ptr)
Elijah Newren259ccb62019-04-05 08:00:13 -07001375 ret = err(opt, _("Failed to execute internal merge"));
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001376
Patryk Obaraa09c9852018-01-28 01:13:19 +01001377 if (!ret &&
1378 write_object_file(result_buf.ptr, result_buf.size,
Elijah Newren8daec1d2019-04-05 08:00:22 -07001379 blob_type, &result->blob.oid))
Elijah Newren259ccb62019-04-05 08:00:13 -07001380 ret = err(opt, _("Unable to add %s to database"),
Johannes Schindelinbc9204d2016-08-01 13:44:37 +02001381 a->path);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001382
1383 free(result_buf.ptr);
Johannes Schindelin60033032016-07-26 18:06:26 +02001384 if (ret)
1385 return ret;
Elijah Newren816147e2021-03-20 00:03:53 +00001386 /* FIXME: bug, what if modes didn't match? */
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +02001387 result->clean = (merge_status == 0);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001388 } else if (S_ISGITLINK(a->mode)) {
Elijah Newren8daec1d2019-04-05 08:00:22 -07001389 result->clean = merge_submodule(opt, &result->blob.oid,
Elijah Newrene3de8882019-04-05 08:00:14 -07001390 o->path,
1391 &o->oid,
Elijah Newrend90e7592018-06-09 21:16:12 -07001392 &a->oid,
1393 &b->oid);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001394 } else if (S_ISLNK(a->mode)) {
Elijah Newren259ccb62019-04-05 08:00:13 -07001395 switch (opt->recursive_variant) {
Elijah Newrenf3081da2019-08-17 11:41:42 -07001396 case MERGE_VARIANT_NORMAL:
Elijah Newren8daec1d2019-04-05 08:00:22 -07001397 oidcpy(&result->blob.oid, &a->oid);
Elijah Newren763a59e2020-01-01 05:20:57 +00001398 if (!oideq(&a->oid, &b->oid))
Junio C Hamanofd48b462017-09-26 11:40:42 +09001399 result->clean = 0;
1400 break;
Elijah Newrenf3081da2019-08-17 11:41:42 -07001401 case MERGE_VARIANT_OURS:
Elijah Newren8daec1d2019-04-05 08:00:22 -07001402 oidcpy(&result->blob.oid, &a->oid);
Junio C Hamanofd48b462017-09-26 11:40:42 +09001403 break;
Elijah Newrenf3081da2019-08-17 11:41:42 -07001404 case MERGE_VARIANT_THEIRS:
Elijah Newren8daec1d2019-04-05 08:00:22 -07001405 oidcpy(&result->blob.oid, &b->oid);
Junio C Hamanofd48b462017-09-26 11:40:42 +09001406 break;
1407 }
Johannes Schindelinef1177d12016-07-26 18:05:50 +02001408 } else
Johannes Schindelin033abf92018-05-02 11:38:39 +02001409 BUG("unsupported object type in the tree");
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001410 }
1411
Elijah Newren05cf21e2018-04-19 10:58:22 -07001412 if (result->merge)
Elijah Newren259ccb62019-04-05 08:00:13 -07001413 output(opt, 2, _("Auto-merging %s"), filename);
Elijah Newren05cf21e2018-04-19 10:58:22 -07001414
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +02001415 return 0;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001416}
1417
Elijah Newren259ccb62019-04-05 08:00:13 -07001418static int handle_rename_via_dir(struct merge_options *opt,
Elijah Newrene2d563d2019-04-05 08:00:21 -07001419 struct rename_conflict_info *ci)
Elijah Newren9c0743f2018-04-19 10:58:10 -07001420{
Elijah Newren5455c332018-06-09 21:16:14 -07001421 /*
1422 * Handle file adds that need to be renamed due to directory rename
1423 * detection. This differs from handle_rename_normal, because
1424 * there is no content merge to do; just move the file into the
1425 * desired final location.
1426 */
Elijah Newrene2d563d2019-04-05 08:00:21 -07001427 const struct rename *ren = ci->ren1;
1428 const struct diff_filespec *dest = ren->pair->two;
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07001429 char *file_path = dest->path;
Derrick Stolee8e012512019-08-17 11:41:25 -07001430 int mark_conflicted = (opt->detect_directory_renames ==
1431 MERGE_DIRECTORY_RENAMES_CONFLICT);
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07001432 assert(ren->dir_rename_original_dest);
Elijah Newren9c0743f2018-04-19 10:58:10 -07001433
Elijah Newren5bf7e572019-08-17 11:41:41 -07001434 if (!opt->priv->call_depth && would_lose_untracked(opt, dest->path)) {
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07001435 mark_conflicted = 1;
1436 file_path = unique_path(opt, dest->path, ren->branch);
Elijah Newren259ccb62019-04-05 08:00:13 -07001437 output(opt, 1, _("Error: Refusing to lose untracked file at %s; "
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07001438 "writing to %s instead."),
1439 dest->path, file_path);
Elijah Newren79c47592018-04-19 10:58:11 -07001440 }
1441
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07001442 if (mark_conflicted) {
1443 /*
1444 * Write the file in worktree at file_path. In the index,
1445 * only record the file at dest->path in the appropriate
1446 * higher stage.
1447 */
1448 if (update_file(opt, 0, dest, file_path))
1449 return -1;
1450 if (file_path != dest->path)
1451 free(file_path);
1452 if (update_stages(opt, dest->path, NULL,
1453 ren->branch == opt->branch1 ? dest : NULL,
1454 ren->branch == opt->branch1 ? NULL : dest))
1455 return -1;
1456 return 0; /* not clean, but conflicted */
1457 } else {
1458 /* Update dest->path both in index and in worktree */
1459 if (update_file(opt, 1, dest, dest->path))
1460 return -1;
1461 return 1; /* clean */
1462 }
Elijah Newren6bdaead2011-08-11 23:20:12 -06001463}
1464
Elijah Newren259ccb62019-04-05 08:00:13 -07001465static int handle_change_delete(struct merge_options *opt,
Elijah Newrend90e7592018-06-09 21:16:12 -07001466 const char *path, const char *old_path,
Elijah Newren8daec1d2019-04-05 08:00:22 -07001467 const struct diff_filespec *o,
1468 const struct diff_filespec *changed,
Elijah Newrend90e7592018-06-09 21:16:12 -07001469 const char *change_branch,
1470 const char *delete_branch,
1471 const char *change, const char *change_past)
Elijah Newrenb7033252011-08-11 23:20:19 -06001472{
Matt McCutchenb26d87f2017-01-28 15:37:01 -05001473 char *alt_path = NULL;
1474 const char *update_path = path;
Johannes Schindelin75456f92016-07-26 18:06:21 +02001475 int ret = 0;
Matt McCutchenb26d87f2017-01-28 15:37:01 -05001476
Elijah Newren5bf7e572019-08-17 11:41:41 -07001477 if (dir_in_way(opt->repo->index, path, !opt->priv->call_depth, 0) ||
1478 (!opt->priv->call_depth && would_lose_untracked(opt, path))) {
Elijah Newren259ccb62019-04-05 08:00:13 -07001479 update_path = alt_path = unique_path(opt, path, change_branch);
Elijah Newrenb7033252011-08-11 23:20:19 -06001480 }
1481
Elijah Newren5bf7e572019-08-17 11:41:41 -07001482 if (opt->priv->call_depth) {
Elijah Newrenb7033252011-08-11 23:20:19 -06001483 /*
1484 * We cannot arbitrarily accept either a_sha or b_sha as
1485 * correct; since there is no true "middle point" between
1486 * them, simply reuse the base version for virtual merge base.
1487 */
Elijah Newren259ccb62019-04-05 08:00:13 -07001488 ret = remove_file_from_index(opt->repo->index, path);
Johannes Schindelin75456f92016-07-26 18:06:21 +02001489 if (!ret)
Elijah Newren8daec1d2019-04-05 08:00:22 -07001490 ret = update_file(opt, 0, o, update_path);
Elijah Newrenb7033252011-08-11 23:20:19 -06001491 } else {
Elijah Newren5b26c3c2018-06-09 21:16:16 -07001492 /*
1493 * Despite the four nearly duplicate messages and argument
1494 * lists below and the ugliness of the nested if-statements,
1495 * having complete messages makes the job easier for
1496 * translators.
1497 *
1498 * The slight variance among the cases is due to the fact
1499 * that:
1500 * 1) directory/file conflicts (in effect if
1501 * !alt_path) could cause us to need to write the
1502 * file to a different path.
1503 * 2) renames (in effect if !old_path) could mean that
1504 * there are two names for the path that the user
1505 * may know the file by.
1506 */
Matt McCutchenb26d87f2017-01-28 15:37:01 -05001507 if (!alt_path) {
1508 if (!old_path) {
Elijah Newren259ccb62019-04-05 08:00:13 -07001509 output(opt, 1, _("CONFLICT (%s/delete): %s deleted in %s "
Matt McCutchenb26d87f2017-01-28 15:37:01 -05001510 "and %s in %s. Version %s of %s left in tree."),
1511 change, path, delete_branch, change_past,
1512 change_branch, change_branch, path);
1513 } else {
Elijah Newren259ccb62019-04-05 08:00:13 -07001514 output(opt, 1, _("CONFLICT (%s/delete): %s deleted in %s "
Matt McCutchenb26d87f2017-01-28 15:37:01 -05001515 "and %s to %s in %s. Version %s of %s left in tree."),
1516 change, old_path, delete_branch, change_past, path,
1517 change_branch, change_branch, path);
1518 }
Jiang Xin55653a62012-07-25 22:53:13 +08001519 } else {
Matt McCutchenb26d87f2017-01-28 15:37:01 -05001520 if (!old_path) {
Elijah Newren259ccb62019-04-05 08:00:13 -07001521 output(opt, 1, _("CONFLICT (%s/delete): %s deleted in %s "
Matt McCutchenb26d87f2017-01-28 15:37:01 -05001522 "and %s in %s. Version %s of %s left in tree at %s."),
1523 change, path, delete_branch, change_past,
1524 change_branch, change_branch, path, alt_path);
1525 } else {
Elijah Newren259ccb62019-04-05 08:00:13 -07001526 output(opt, 1, _("CONFLICT (%s/delete): %s deleted in %s "
Matt McCutchenb26d87f2017-01-28 15:37:01 -05001527 "and %s to %s in %s. Version %s of %s left in tree at %s."),
1528 change, old_path, delete_branch, change_past, path,
1529 change_branch, change_branch, path, alt_path);
1530 }
Jiang Xin55653a62012-07-25 22:53:13 +08001531 }
Elijah Newren35a74ab2011-08-11 23:20:27 -06001532 /*
Matt McCutchenb26d87f2017-01-28 15:37:01 -05001533 * No need to call update_file() on path when change_branch ==
Elijah Newren259ccb62019-04-05 08:00:13 -07001534 * opt->branch1 && !alt_path, since that would needlessly touch
Matt McCutchenb26d87f2017-01-28 15:37:01 -05001535 * path. We could call update_file_flags() with update_cache=0
1536 * and update_wd=0, but that's a no-op.
Elijah Newren35a74ab2011-08-11 23:20:27 -06001537 */
Elijah Newren259ccb62019-04-05 08:00:13 -07001538 if (change_branch != opt->branch1 || alt_path)
Elijah Newren8daec1d2019-04-05 08:00:22 -07001539 ret = update_file(opt, 0, changed, update_path);
Elijah Newrenb7033252011-08-11 23:20:19 -06001540 }
Matt McCutchenb26d87f2017-01-28 15:37:01 -05001541 free(alt_path);
Johannes Schindelin75456f92016-07-26 18:06:21 +02001542
1543 return ret;
Elijah Newrenb7033252011-08-11 23:20:19 -06001544}
1545
Elijah Newren259ccb62019-04-05 08:00:13 -07001546static int handle_rename_delete(struct merge_options *opt,
Elijah Newrene2d563d2019-04-05 08:00:21 -07001547 struct rename_conflict_info *ci)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001548{
Elijah Newrene2d563d2019-04-05 08:00:21 -07001549 const struct rename *ren = ci->ren1;
1550 const struct diff_filespec *orig = ren->pair->one;
1551 const struct diff_filespec *dest = ren->pair->two;
1552 const char *rename_branch = ren->branch;
1553 const char *delete_branch = (opt->branch1 == ren->branch ?
1554 opt->branch2 : opt->branch1);
Elijah Newrene03acb82011-08-11 23:20:20 -06001555
Elijah Newren259ccb62019-04-05 08:00:13 -07001556 if (handle_change_delete(opt,
Elijah Newren5bf7e572019-08-17 11:41:41 -07001557 opt->priv->call_depth ? orig->path : dest->path,
1558 opt->priv->call_depth ? NULL : orig->path,
Elijah Newren8daec1d2019-04-05 08:00:22 -07001559 orig, dest,
Matt McCutchenb26d87f2017-01-28 15:37:01 -05001560 rename_branch, delete_branch,
Johannes Schindelin75456f92016-07-26 18:06:21 +02001561 _("rename"), _("renamed")))
1562 return -1;
Elijah Newrenf53d3972011-08-11 23:20:25 -06001563
Elijah Newren5bf7e572019-08-17 11:41:41 -07001564 if (opt->priv->call_depth)
Elijah Newren259ccb62019-04-05 08:00:13 -07001565 return remove_file_from_index(opt->repo->index, dest->path);
Johannes Schindelin75456f92016-07-26 18:06:21 +02001566 else
Elijah Newren259ccb62019-04-05 08:00:13 -07001567 return update_stages(opt, dest->path, NULL,
1568 rename_branch == opt->branch1 ? dest : NULL,
1569 rename_branch == opt->branch1 ? NULL : dest);
Elijah Newren6ef2cb02010-09-20 02:28:50 -06001570}
1571
Elijah Newren259ccb62019-04-05 08:00:13 -07001572static int handle_file_collision(struct merge_options *opt,
Elijah Newren37b65ce2018-11-07 20:40:25 -08001573 const char *collide_path,
1574 const char *prev_path1,
1575 const char *prev_path2,
1576 const char *branch1, const char *branch2,
Elijah Newren8daec1d2019-04-05 08:00:22 -07001577 struct diff_filespec *a,
1578 struct diff_filespec *b)
Elijah Newren3672c972011-08-11 23:20:22 -06001579{
Elijah Newren37b65ce2018-11-07 20:40:25 -08001580 struct merge_file_info mfi;
Elijah Newren8daec1d2019-04-05 08:00:22 -07001581 struct diff_filespec null;
Elijah Newren37b65ce2018-11-07 20:40:25 -08001582 char *alt_path = NULL;
1583 const char *update_path = collide_path;
Elijah Newren3672c972011-08-11 23:20:22 -06001584
Elijah Newren37b65ce2018-11-07 20:40:25 -08001585 /*
Elijah Newren7f867162018-11-07 20:40:26 -08001586 * It's easiest to get the correct things into stage 2 and 3, and
1587 * to make sure that the content merge puts HEAD before the other
Elijah Newren259ccb62019-04-05 08:00:13 -07001588 * branch if we just ensure that branch1 == opt->branch1. So, simply
Elijah Newren7f867162018-11-07 20:40:26 -08001589 * flip arguments around if we don't have that.
1590 */
Elijah Newren259ccb62019-04-05 08:00:13 -07001591 if (branch1 != opt->branch1) {
1592 return handle_file_collision(opt, collide_path,
Elijah Newren7f867162018-11-07 20:40:26 -08001593 prev_path2, prev_path1,
1594 branch2, branch1,
Elijah Newren8daec1d2019-04-05 08:00:22 -07001595 b, a);
Elijah Newren3672c972011-08-11 23:20:22 -06001596 }
1597
Elijah Newren37b65ce2018-11-07 20:40:25 -08001598 /* Remove rename sources if rename/add or rename/rename(2to1) */
1599 if (prev_path1)
Elijah Newren259ccb62019-04-05 08:00:13 -07001600 remove_file(opt, 1, prev_path1,
Elijah Newren5bf7e572019-08-17 11:41:41 -07001601 opt->priv->call_depth || would_lose_untracked(opt, prev_path1));
Elijah Newren37b65ce2018-11-07 20:40:25 -08001602 if (prev_path2)
Elijah Newren259ccb62019-04-05 08:00:13 -07001603 remove_file(opt, 1, prev_path2,
Elijah Newren5bf7e572019-08-17 11:41:41 -07001604 opt->priv->call_depth || would_lose_untracked(opt, prev_path2));
Elijah Newren37b65ce2018-11-07 20:40:25 -08001605
1606 /*
1607 * Remove the collision path, if it wouldn't cause dirty contents
1608 * or an untracked file to get lost. We'll either overwrite with
1609 * merged contents, or just write out to differently named files.
1610 */
Elijah Newren259ccb62019-04-05 08:00:13 -07001611 if (was_dirty(opt, collide_path)) {
1612 output(opt, 1, _("Refusing to lose dirty file at %s"),
Elijah Newren37b65ce2018-11-07 20:40:25 -08001613 collide_path);
Elijah Newren259ccb62019-04-05 08:00:13 -07001614 update_path = alt_path = unique_path(opt, collide_path, "merged");
1615 } else if (would_lose_untracked(opt, collide_path)) {
Elijah Newren18797a32018-04-19 10:58:13 -07001616 /*
Elijah Newren37b65ce2018-11-07 20:40:25 -08001617 * Only way we get here is if both renames were from
1618 * a directory rename AND user had an untracked file
1619 * at the location where both files end up after the
1620 * two directory renames. See testcase 10d of t6043.
Elijah Newren18797a32018-04-19 10:58:13 -07001621 */
Elijah Newren259ccb62019-04-05 08:00:13 -07001622 output(opt, 1, _("Refusing to lose untracked file at "
Elijah Newren37b65ce2018-11-07 20:40:25 -08001623 "%s, even though it's in the way."),
1624 collide_path);
Elijah Newren259ccb62019-04-05 08:00:13 -07001625 update_path = alt_path = unique_path(opt, collide_path, "merged");
Elijah Newren3672c972011-08-11 23:20:22 -06001626 } else {
Elijah Newren37b65ce2018-11-07 20:40:25 -08001627 /*
1628 * FIXME: It's possible that the two files are identical
1629 * and that the current working copy happens to match, in
1630 * which case we are unnecessarily touching the working
1631 * tree file. It's not a likely enough scenario that I
1632 * want to code up the checks for it and a better fix is
1633 * available if we restructure how unpack_trees() and
1634 * merge-recursive interoperate anyway, so punting for
1635 * now...
1636 */
Elijah Newren259ccb62019-04-05 08:00:13 -07001637 remove_file(opt, 0, collide_path, 0);
Elijah Newren3672c972011-08-11 23:20:22 -06001638 }
Elijah Newren3672c972011-08-11 23:20:22 -06001639
Elijah Newren37b65ce2018-11-07 20:40:25 -08001640 /* Store things in diff_filespecs for functions that need it */
Elijah Newren8daec1d2019-04-05 08:00:22 -07001641 null.path = (char *)collide_path;
brian m. carlson14228442021-04-26 01:02:56 +00001642 oidcpy(&null.oid, null_oid());
Elijah Newren37b65ce2018-11-07 20:40:25 -08001643 null.mode = 0;
Johannes Schindelin75456f92016-07-26 18:06:21 +02001644
Elijah Newren8daec1d2019-04-05 08:00:22 -07001645 if (merge_mode_and_contents(opt, &null, a, b, collide_path,
Elijah Newren5bf7e572019-08-17 11:41:41 -07001646 branch1, branch2, opt->priv->call_depth * 2, &mfi))
Elijah Newren37b65ce2018-11-07 20:40:25 -08001647 return -1;
1648 mfi.clean &= !alt_path;
Elijah Newren8daec1d2019-04-05 08:00:22 -07001649 if (update_file(opt, mfi.clean, &mfi.blob, update_path))
Elijah Newren37b65ce2018-11-07 20:40:25 -08001650 return -1;
Elijah Newren5bf7e572019-08-17 11:41:41 -07001651 if (!mfi.clean && !opt->priv->call_depth &&
Elijah Newren8daec1d2019-04-05 08:00:22 -07001652 update_stages(opt, collide_path, NULL, a, b))
Elijah Newren37b65ce2018-11-07 20:40:25 -08001653 return -1;
1654 free(alt_path);
1655 /*
1656 * FIXME: If both a & b both started with conflicts (only possible
1657 * if they came from a rename/rename(2to1)), but had IDENTICAL
1658 * contents including those conflicts, then in the next line we claim
1659 * it was clean. If someone cares about this case, we should have the
1660 * caller notify us if we started with conflicts.
1661 */
1662 return mfi.clean;
1663}
Elijah Newren7f867162018-11-07 20:40:26 -08001664
Elijah Newren259ccb62019-04-05 08:00:13 -07001665static int handle_rename_add(struct merge_options *opt,
Elijah Newren7f867162018-11-07 20:40:26 -08001666 struct rename_conflict_info *ci)
1667{
1668 /* a was renamed to c, and a separate c was added. */
Elijah Newrene9cd1b52019-04-05 08:00:18 -07001669 struct diff_filespec *a = ci->ren1->pair->one;
1670 struct diff_filespec *c = ci->ren1->pair->two;
Elijah Newren7f867162018-11-07 20:40:26 -08001671 char *path = c->path;
1672 char *prev_path_desc;
1673 struct merge_file_info mfi;
1674
Elijah Newrenc336ab82019-04-05 08:00:20 -07001675 const char *rename_branch = ci->ren1->branch;
1676 const char *add_branch = (opt->branch1 == rename_branch ?
1677 opt->branch2 : opt->branch1);
1678 int other_stage = (ci->ren1->branch == opt->branch1 ? 3 : 2);
Elijah Newren7f867162018-11-07 20:40:26 -08001679
Elijah Newren259ccb62019-04-05 08:00:13 -07001680 output(opt, 1, _("CONFLICT (rename/add): "
Elijah Newren7f867162018-11-07 20:40:26 -08001681 "Rename %s->%s in %s. Added %s in %s"),
Elijah Newrenc336ab82019-04-05 08:00:20 -07001682 a->path, c->path, rename_branch,
1683 c->path, add_branch);
Elijah Newren7f867162018-11-07 20:40:26 -08001684
1685 prev_path_desc = xstrfmt("version of %s from %s", path, a->path);
Elijah Newren481de8a2019-06-04 13:27:50 -07001686 ci->ren1->src_entry->stages[other_stage].path = a->path;
Elijah Newren8daec1d2019-04-05 08:00:22 -07001687 if (merge_mode_and_contents(opt, a, c,
1688 &ci->ren1->src_entry->stages[other_stage],
Elijah Newren3f9c92e2019-04-05 08:00:19 -07001689 prev_path_desc,
Elijah Newren259ccb62019-04-05 08:00:13 -07001690 opt->branch1, opt->branch2,
Elijah Newren5bf7e572019-08-17 11:41:41 -07001691 1 + opt->priv->call_depth * 2, &mfi))
Elijah Newren7f867162018-11-07 20:40:26 -08001692 return -1;
1693 free(prev_path_desc);
1694
Elijah Newren8daec1d2019-04-05 08:00:22 -07001695 ci->ren1->dst_entry->stages[other_stage].path = mfi.blob.path = c->path;
Elijah Newren259ccb62019-04-05 08:00:13 -07001696 return handle_file_collision(opt,
Elijah Newren7f867162018-11-07 20:40:26 -08001697 c->path, a->path, NULL,
Elijah Newrenc336ab82019-04-05 08:00:20 -07001698 rename_branch, add_branch,
Elijah Newren8daec1d2019-04-05 08:00:22 -07001699 &mfi.blob,
1700 &ci->ren1->dst_entry->stages[other_stage]);
Elijah Newren7f867162018-11-07 20:40:26 -08001701}
Elijah Newren37b65ce2018-11-07 20:40:25 -08001702
Elijah Newren259ccb62019-04-05 08:00:13 -07001703static char *find_path_for_conflict(struct merge_options *opt,
Derrick Stolee80cee6e2018-11-07 20:40:31 -08001704 const char *path,
1705 const char *branch1,
1706 const char *branch2)
1707{
1708 char *new_path = NULL;
Elijah Newren5bf7e572019-08-17 11:41:41 -07001709 if (dir_in_way(opt->repo->index, path, !opt->priv->call_depth, 0)) {
Elijah Newren259ccb62019-04-05 08:00:13 -07001710 new_path = unique_path(opt, path, branch1);
1711 output(opt, 1, _("%s is a directory in %s adding "
Derrick Stolee80cee6e2018-11-07 20:40:31 -08001712 "as %s instead"),
1713 path, branch2, new_path);
Elijah Newren259ccb62019-04-05 08:00:13 -07001714 } else if (would_lose_untracked(opt, path)) {
1715 new_path = unique_path(opt, path, branch1);
1716 output(opt, 1, _("Refusing to lose untracked file"
Derrick Stolee80cee6e2018-11-07 20:40:31 -08001717 " at %s; adding as %s instead"),
1718 path, new_path);
1719 }
1720
1721 return new_path;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001722}
1723
Jeff Kingee798742020-01-25 00:37:23 -05001724/*
Junio C Hamano4c616c22020-01-25 18:57:45 -05001725 * Toggle the stage number between "ours" and "theirs" (2 and 3).
Jeff Kingee798742020-01-25 00:37:23 -05001726 */
1727static inline int flip_stage(int stage)
1728{
Junio C Hamano4c616c22020-01-25 18:57:45 -05001729 return (2 + 3) - stage;
Jeff Kingee798742020-01-25 00:37:23 -05001730}
1731
Elijah Newren259ccb62019-04-05 08:00:13 -07001732static int handle_rename_rename_1to2(struct merge_options *opt,
Elijah Newren8ebe7b02018-06-09 21:16:15 -07001733 struct rename_conflict_info *ci)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001734{
Elijah Newren09c01f82010-09-20 02:28:48 -06001735 /* One file was renamed in both branches, but to different names. */
Elijah Newren48c9cb92018-11-07 20:40:29 -08001736 struct merge_file_info mfi;
Elijah Newren48c9cb92018-11-07 20:40:29 -08001737 struct diff_filespec *add;
Elijah Newrene9cd1b52019-04-05 08:00:18 -07001738 struct diff_filespec *o = ci->ren1->pair->one;
1739 struct diff_filespec *a = ci->ren1->pair->two;
1740 struct diff_filespec *b = ci->ren2->pair->two;
Elijah Newren48c9cb92018-11-07 20:40:29 -08001741 char *path_desc;
Elijah Newren07413c52010-09-20 02:29:00 -06001742
Elijah Newren259ccb62019-04-05 08:00:13 -07001743 output(opt, 1, _("CONFLICT (rename/rename): "
Elijah Newren4f66dad2011-08-11 23:20:08 -06001744 "Rename \"%s\"->\"%s\" in branch \"%s\" "
Jiang Xin55653a62012-07-25 22:53:13 +08001745 "rename \"%s\"->\"%s\" in \"%s\"%s"),
Elijah Newrenc336ab82019-04-05 08:00:20 -07001746 o->path, a->path, ci->ren1->branch,
1747 o->path, b->path, ci->ren2->branch,
Elijah Newren5bf7e572019-08-17 11:41:41 -07001748 opt->priv->call_depth ? _(" (left unresolved)") : "");
Johannes Schindelin75456f92016-07-26 18:06:21 +02001749
Elijah Newren48c9cb92018-11-07 20:40:29 -08001750 path_desc = xstrfmt("%s and %s, both renamed from %s",
Elijah Newrene3de8882019-04-05 08:00:14 -07001751 a->path, b->path, o->path);
1752 if (merge_mode_and_contents(opt, o, a, b, path_desc,
Elijah Newrenc336ab82019-04-05 08:00:20 -07001753 ci->ren1->branch, ci->ren2->branch,
Elijah Newren5bf7e572019-08-17 11:41:41 -07001754 opt->priv->call_depth * 2, &mfi))
Elijah Newren48c9cb92018-11-07 20:40:29 -08001755 return -1;
1756 free(path_desc);
1757
Elijah Newren80205042020-02-27 00:05:05 +00001758 if (opt->priv->call_depth)
1759 remove_file_from_index(opt->repo->index, o->path);
1760
1761 /*
1762 * For each destination path, we need to see if there is a
1763 * rename/add collision. If not, we can write the file out
1764 * to the specified location.
1765 */
1766 add = &ci->ren1->dst_entry->stages[flip_stage(2)];
1767 if (is_valid(add)) {
1768 add->path = mfi.blob.path = a->path;
1769 if (handle_file_collision(opt, a->path,
1770 NULL, NULL,
1771 ci->ren1->branch,
1772 ci->ren2->branch,
1773 &mfi.blob, add) < 0)
Johannes Schindelin75456f92016-07-26 18:06:21 +02001774 return -1;
Elijah Newren48c9cb92018-11-07 20:40:29 -08001775 } else {
Elijah Newren80205042020-02-27 00:05:05 +00001776 char *new_path = find_path_for_conflict(opt, a->path,
1777 ci->ren1->branch,
1778 ci->ren2->branch);
1779 if (update_file(opt, 0, &mfi.blob,
1780 new_path ? new_path : a->path))
1781 return -1;
1782 free(new_path);
1783 if (!opt->priv->call_depth &&
1784 update_stages(opt, a->path, NULL, a, NULL))
1785 return -1;
1786 }
Elijah Newren48c9cb92018-11-07 20:40:29 -08001787
Elijah Newren95983da2020-05-13 23:56:32 +00001788 if (!mfi.clean && mfi.blob.mode == a->mode &&
1789 oideq(&mfi.blob.oid, &a->oid)) {
1790 /*
1791 * Getting here means we were attempting to merge a binary
1792 * blob. Since we can't merge binaries, the merge algorithm
1793 * just takes one side. But we don't want to copy the
1794 * contents of one side to both paths; we'd rather use the
1795 * original content at the given path for each path.
1796 */
1797 oidcpy(&mfi.blob.oid, &b->oid);
1798 mfi.blob.mode = b->mode;
1799 }
Elijah Newren80205042020-02-27 00:05:05 +00001800 add = &ci->ren2->dst_entry->stages[flip_stage(3)];
1801 if (is_valid(add)) {
1802 add->path = mfi.blob.path = b->path;
1803 if (handle_file_collision(opt, b->path,
1804 NULL, NULL,
1805 ci->ren1->branch,
1806 ci->ren2->branch,
1807 add, &mfi.blob) < 0)
1808 return -1;
1809 } else {
1810 char *new_path = find_path_for_conflict(opt, b->path,
1811 ci->ren2->branch,
1812 ci->ren1->branch);
1813 if (update_file(opt, 0, &mfi.blob,
1814 new_path ? new_path : b->path))
1815 return -1;
1816 free(new_path);
1817 if (!opt->priv->call_depth &&
1818 update_stages(opt, b->path, NULL, NULL, b))
1819 return -1;
Elijah Newren48c9cb92018-11-07 20:40:29 -08001820 }
Johannes Schindelin75456f92016-07-26 18:06:21 +02001821
1822 return 0;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001823}
1824
Elijah Newren259ccb62019-04-05 08:00:13 -07001825static int handle_rename_rename_2to1(struct merge_options *opt,
Elijah Newren8ebe7b02018-06-09 21:16:15 -07001826 struct rename_conflict_info *ci)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001827{
Elijah Newren461f5042011-08-11 23:20:15 -06001828 /* Two files, a & b, were renamed to the same thing, c. */
Elijah Newrene9cd1b52019-04-05 08:00:18 -07001829 struct diff_filespec *a = ci->ren1->pair->one;
1830 struct diff_filespec *b = ci->ren2->pair->one;
1831 struct diff_filespec *c1 = ci->ren1->pair->two;
1832 struct diff_filespec *c2 = ci->ren2->pair->two;
Elijah Newren461f5042011-08-11 23:20:15 -06001833 char *path = c1->path; /* == c2->path */
Elijah Newren05cf21e2018-04-19 10:58:22 -07001834 char *path_side_1_desc;
1835 char *path_side_2_desc;
Elijah Newren434b8522011-08-11 23:20:18 -06001836 struct merge_file_info mfi_c1;
1837 struct merge_file_info mfi_c2;
Elijah Newren8daec1d2019-04-05 08:00:22 -07001838 int ostage1, ostage2;
Elijah Newren461f5042011-08-11 23:20:15 -06001839
Elijah Newren259ccb62019-04-05 08:00:13 -07001840 output(opt, 1, _("CONFLICT (rename/rename): "
Elijah Newren461f5042011-08-11 23:20:15 -06001841 "Rename %s->%s in %s. "
Jiang Xin55653a62012-07-25 22:53:13 +08001842 "Rename %s->%s in %s"),
Elijah Newrenc336ab82019-04-05 08:00:20 -07001843 a->path, c1->path, ci->ren1->branch,
1844 b->path, c2->path, ci->ren2->branch);
Elijah Newren461f5042011-08-11 23:20:15 -06001845
Elijah Newren2b168ef2018-10-16 13:19:47 -07001846 path_side_1_desc = xstrfmt("version of %s from %s", path, a->path);
1847 path_side_2_desc = xstrfmt("version of %s from %s", path, b->path);
Elijah Newren8daec1d2019-04-05 08:00:22 -07001848 ostage1 = ci->ren1->branch == opt->branch1 ? 3 : 2;
Jeff Kingee798742020-01-25 00:37:23 -05001849 ostage2 = flip_stage(ostage1);
Elijah Newren8daec1d2019-04-05 08:00:22 -07001850 ci->ren1->src_entry->stages[ostage1].path = a->path;
1851 ci->ren2->src_entry->stages[ostage2].path = b->path;
1852 if (merge_mode_and_contents(opt, a, c1,
1853 &ci->ren1->src_entry->stages[ostage1],
1854 path_side_1_desc,
Elijah Newren259ccb62019-04-05 08:00:13 -07001855 opt->branch1, opt->branch2,
Elijah Newren5bf7e572019-08-17 11:41:41 -07001856 1 + opt->priv->call_depth * 2, &mfi_c1) ||
Elijah Newren8daec1d2019-04-05 08:00:22 -07001857 merge_mode_and_contents(opt, b,
1858 &ci->ren2->src_entry->stages[ostage2],
1859 c2, path_side_2_desc,
Elijah Newren259ccb62019-04-05 08:00:13 -07001860 opt->branch1, opt->branch2,
Elijah Newren5bf7e572019-08-17 11:41:41 -07001861 1 + opt->priv->call_depth * 2, &mfi_c2))
Johannes Schindelin75456f92016-07-26 18:06:21 +02001862 return -1;
Elijah Newren05cf21e2018-04-19 10:58:22 -07001863 free(path_side_1_desc);
1864 free(path_side_2_desc);
Elijah Newren8daec1d2019-04-05 08:00:22 -07001865 mfi_c1.blob.path = path;
1866 mfi_c2.blob.path = path;
Elijah Newren434b8522011-08-11 23:20:18 -06001867
Elijah Newren259ccb62019-04-05 08:00:13 -07001868 return handle_file_collision(opt, path, a->path, b->path,
Elijah Newrenc336ab82019-04-05 08:00:20 -07001869 ci->ren1->branch, ci->ren2->branch,
Elijah Newren8daec1d2019-04-05 08:00:22 -07001870 &mfi_c1.blob, &mfi_c2.blob);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001871}
1872
Elijah Newren9ba91552018-04-19 10:57:59 -07001873/*
Elijah Newrene5257b22018-04-19 10:58:03 -07001874 * Get the diff_filepairs changed between o_tree and tree.
Elijah Newren9ba91552018-04-19 10:57:59 -07001875 */
Elijah Newren259ccb62019-04-05 08:00:13 -07001876static struct diff_queue_struct *get_diffpairs(struct merge_options *opt,
Elijah Newrene5257b22018-04-19 10:58:03 -07001877 struct tree *o_tree,
1878 struct tree *tree)
Elijah Newren9ba91552018-04-19 10:57:59 -07001879{
Elijah Newrene5257b22018-04-19 10:58:03 -07001880 struct diff_queue_struct *ret;
Elijah Newren9ba91552018-04-19 10:57:59 -07001881 struct diff_options opts;
1882
Elijah Newren259ccb62019-04-05 08:00:13 -07001883 repo_diff_setup(opt->repo, &opts);
Elijah Newren9ba91552018-04-19 10:57:59 -07001884 opts.flags.recursive = 1;
1885 opts.flags.rename_empty = 0;
Elijah Newren259ccb62019-04-05 08:00:13 -07001886 opts.detect_rename = merge_detect_rename(opt);
Ben Peart85b46032018-05-02 16:01:14 +00001887 /*
1888 * We do not have logic to handle the detection of copies. In
1889 * fact, it may not even make sense to add such logic: would we
1890 * really want a change to a base file to be propagated through
1891 * multiple other files by a merge?
1892 */
1893 if (opts.detect_rename > DIFF_DETECT_RENAME)
1894 opts.detect_rename = DIFF_DETECT_RENAME;
Elijah Newren94b82d52021-07-15 00:45:24 +00001895 opts.rename_limit = (opt->rename_limit >= 0) ? opt->rename_limit : 7000;
Elijah Newren259ccb62019-04-05 08:00:13 -07001896 opts.rename_score = opt->rename_score;
1897 opts.show_rename_progress = opt->show_rename_progress;
Elijah Newren9ba91552018-04-19 10:57:59 -07001898 opts.output_format = DIFF_FORMAT_NO_OUTPUT;
1899 diff_setup_done(&opts);
1900 diff_tree_oid(&o_tree->object.oid, &tree->object.oid, "", &opts);
1901 diffcore_std(&opts);
Elijah Newren5bf7e572019-08-17 11:41:41 -07001902 if (opts.needed_rename_limit > opt->priv->needed_rename_limit)
1903 opt->priv->needed_rename_limit = opts.needed_rename_limit;
Elijah Newrene5257b22018-04-19 10:58:03 -07001904
1905 ret = xmalloc(sizeof(*ret));
1906 *ret = diff_queued_diff;
1907
1908 opts.output_format = DIFF_FORMAT_NO_OUTPUT;
1909 diff_queued_diff.nr = 0;
1910 diff_queued_diff.queue = NULL;
1911 diff_flush(&opts);
1912 return ret;
1913}
1914
Nguyễn Thái Ngọc Duy34e77712019-06-27 16:28:52 +07001915static int tree_has_path(struct repository *r, struct tree *tree,
1916 const char *path)
Elijah Newren96e7ffb2018-04-19 10:58:06 -07001917{
1918 struct object_id hashy;
Elijah Newren5ec1e722019-04-05 08:00:12 -07001919 unsigned short mode_o;
Elijah Newren96e7ffb2018-04-19 10:58:06 -07001920
Nguyễn Thái Ngọc Duy34e77712019-06-27 16:28:52 +07001921 return !get_tree_entry(r,
Nguyễn Thái Ngọc Duy50ddb082019-06-27 16:28:49 +07001922 &tree->object.oid, path,
Elijah Newren96e7ffb2018-04-19 10:58:06 -07001923 &hashy, &mode_o);
1924}
1925
Elijah Newrene95ab702018-04-19 10:58:07 -07001926/*
1927 * Return a new string that replaces the beginning portion (which matches
1928 * entry->dir), with entry->new_dir. In perl-speak:
1929 * new_path_name = (old_path =~ s/entry->dir/entry->new_dir/);
1930 * NOTE:
1931 * Caller must ensure that old_path starts with entry->dir + '/'.
1932 */
1933static char *apply_dir_rename(struct dir_rename_entry *entry,
1934 const char *old_path)
1935{
1936 struct strbuf new_path = STRBUF_INIT;
1937 int oldlen, newlen;
1938
1939 if (entry->non_unique_new_dir)
1940 return NULL;
1941
1942 oldlen = strlen(entry->dir);
Elijah Newren49b8133a2019-10-22 21:22:50 +00001943 if (entry->new_dir.len == 0)
1944 /*
1945 * If someone renamed/merged a subdirectory into the root
1946 * directory (e.g. 'some/subdir' -> ''), then we want to
1947 * avoid returning
1948 * '' + '/filename'
1949 * as the rename; we need to make old_path + oldlen advance
1950 * past the '/' character.
1951 */
1952 oldlen++;
Elijah Newrene95ab702018-04-19 10:58:07 -07001953 newlen = entry->new_dir.len + (strlen(old_path) - oldlen) + 1;
1954 strbuf_grow(&new_path, newlen);
1955 strbuf_addbuf(&new_path, &entry->new_dir);
1956 strbuf_addstr(&new_path, &old_path[oldlen]);
1957
1958 return strbuf_detach(&new_path, NULL);
1959}
1960
Elijah Newren7fe40b82018-04-19 10:58:05 -07001961static void get_renamed_dir_portion(const char *old_path, const char *new_path,
1962 char **old_dir, char **new_dir)
1963{
1964 char *end_of_old, *end_of_new;
Elijah Newren7fe40b82018-04-19 10:58:05 -07001965
Elijah Newrend3eebaa2019-10-22 21:22:49 +00001966 /* Default return values: NULL, meaning no rename */
Elijah Newren7fe40b82018-04-19 10:58:05 -07001967 *old_dir = NULL;
1968 *new_dir = NULL;
1969
1970 /*
1971 * For
1972 * "a/b/c/d/e/foo.c" -> "a/b/some/thing/else/e/foo.c"
1973 * the "e/foo.c" part is the same, we just want to know that
1974 * "a/b/c/d" was renamed to "a/b/some/thing/else"
1975 * so, for this example, this function returns "a/b/c/d" in
1976 * *old_dir and "a/b/some/thing/else" in *new_dir.
Elijah Newrend3eebaa2019-10-22 21:22:49 +00001977 */
1978
1979 /*
1980 * If the basename of the file changed, we don't care. We want
1981 * to know which portion of the directory, if any, changed.
Elijah Newren7fe40b82018-04-19 10:58:05 -07001982 */
1983 end_of_old = strrchr(old_path, '/');
1984 end_of_new = strrchr(new_path, '/');
1985
Elijah Newren49b8133a2019-10-22 21:22:50 +00001986 /*
1987 * If end_of_old is NULL, old_path wasn't in a directory, so there
1988 * could not be a directory rename (our rule elsewhere that a
1989 * directory which still exists is not considered to have been
1990 * renamed means the root directory can never be renamed -- because
1991 * the root directory always exists).
1992 */
1993 if (end_of_old == NULL)
1994 return; /* Note: *old_dir and *new_dir are still NULL */
1995
1996 /*
1997 * If new_path contains no directory (end_of_new is NULL), then we
1998 * have a rename of old_path's directory to the root directory.
1999 */
2000 if (end_of_new == NULL) {
2001 *old_dir = xstrndup(old_path, end_of_old - old_path);
2002 *new_dir = xstrdup("");
Elijah Newren7fe40b82018-04-19 10:58:05 -07002003 return;
Elijah Newren49b8133a2019-10-22 21:22:50 +00002004 }
Elijah Newrend3eebaa2019-10-22 21:22:49 +00002005
2006 /* Find the first non-matching character traversing backwards */
Elijah Newren7fe40b82018-04-19 10:58:05 -07002007 while (*--end_of_new == *--end_of_old &&
2008 end_of_old != old_path &&
2009 end_of_new != new_path)
2010 ; /* Do nothing; all in the while loop */
Elijah Newrend3eebaa2019-10-22 21:22:49 +00002011
2012 /*
2013 * If both got back to the beginning of their strings, then the
2014 * directory didn't change at all, only the basename did.
2015 */
2016 if (end_of_old == old_path && end_of_new == new_path &&
2017 *end_of_old == *end_of_new)
Elijah Newren49b8133a2019-10-22 21:22:50 +00002018 return; /* Note: *old_dir and *new_dir are still NULL */
2019
2020 /*
2021 * If end_of_new got back to the beginning of its string, and
2022 * end_of_old got back to the beginning of some subdirectory, then
2023 * we have a rename/merge of a subdirectory into the root, which
2024 * needs slightly special handling.
2025 *
2026 * Note: There is no need to consider the opposite case, with a
2027 * rename/merge of the root directory into some subdirectory
2028 * because as noted above the root directory always exists so it
2029 * cannot be considered to be renamed.
2030 */
2031 if (end_of_new == new_path &&
2032 end_of_old != old_path && end_of_old[-1] == '/') {
2033 *old_dir = xstrndup(old_path, --end_of_old - old_path);
2034 *new_dir = xstrdup("");
2035 return;
2036 }
Elijah Newrend3eebaa2019-10-22 21:22:49 +00002037
Elijah Newren7fe40b82018-04-19 10:58:05 -07002038 /*
2039 * We've found the first non-matching character in the directory
Elijah Newrend3eebaa2019-10-22 21:22:49 +00002040 * paths. That means the current characters we were looking at
2041 * were part of the first non-matching subdir name going back from
2042 * the end of the strings. Get the whole name by advancing both
2043 * end_of_old and end_of_new to the NEXT '/' character. That will
2044 * represent the entire directory rename.
2045 *
2046 * The reason for the increment is cases like
2047 * a/b/star/foo/whatever.c -> a/b/tar/foo/random.c
2048 * After dropping the basename and going back to the first
2049 * non-matching character, we're now comparing:
2050 * a/b/s and a/b/
2051 * and we want to be comparing:
2052 * a/b/star/ and a/b/tar/
2053 * but without the pre-increment, the one on the right would stay
2054 * a/b/.
Elijah Newren7fe40b82018-04-19 10:58:05 -07002055 */
Elijah Newrend3eebaa2019-10-22 21:22:49 +00002056 end_of_old = strchr(++end_of_old, '/');
2057 end_of_new = strchr(++end_of_new, '/');
Elijah Newren7fe40b82018-04-19 10:58:05 -07002058
Elijah Newrend3eebaa2019-10-22 21:22:49 +00002059 /* Copy the old and new directories into *old_dir and *new_dir. */
2060 *old_dir = xstrndup(old_path, end_of_old - old_path);
2061 *new_dir = xstrndup(new_path, end_of_new - new_path);
Elijah Newren7fe40b82018-04-19 10:58:05 -07002062}
2063
Elijah Newren96e7ffb2018-04-19 10:58:06 -07002064static void remove_hashmap_entries(struct hashmap *dir_renames,
2065 struct string_list *items_to_remove)
2066{
2067 int i;
2068 struct dir_rename_entry *entry;
2069
2070 for (i = 0; i < items_to_remove->nr; i++) {
2071 entry = items_to_remove->items[i].util;
Eric Wong28ee7942019-10-06 23:30:31 +00002072 hashmap_remove(dir_renames, &entry->ent, NULL);
Elijah Newren96e7ffb2018-04-19 10:58:06 -07002073 }
2074 string_list_clear(items_to_remove, 0);
2075}
2076
2077/*
Elijah Newrenf6f77552018-04-19 10:58:08 -07002078 * See if there is a directory rename for path, and if there are any file
2079 * level conflicts for the renamed location. If there is a rename and
2080 * there are no conflicts, return the new name. Otherwise, return NULL.
2081 */
Elijah Newren259ccb62019-04-05 08:00:13 -07002082static char *handle_path_level_conflicts(struct merge_options *opt,
Elijah Newrenf6f77552018-04-19 10:58:08 -07002083 const char *path,
2084 struct dir_rename_entry *entry,
2085 struct hashmap *collisions,
2086 struct tree *tree)
2087{
2088 char *new_path = NULL;
2089 struct collision_entry *collision_ent;
2090 int clean = 1;
2091 struct strbuf collision_paths = STRBUF_INIT;
2092
2093 /*
2094 * entry has the mapping of old directory name to new directory name
2095 * that we want to apply to path.
2096 */
2097 new_path = apply_dir_rename(entry, path);
2098
2099 if (!new_path) {
2100 /* This should only happen when entry->non_unique_new_dir set */
2101 if (!entry->non_unique_new_dir)
2102 BUG("entry->non_unqiue_dir not set and !new_path");
Elijah Newren259ccb62019-04-05 08:00:13 -07002103 output(opt, 1, _("CONFLICT (directory rename split): "
Elijah Newrenf6f77552018-04-19 10:58:08 -07002104 "Unclear where to place %s because directory "
2105 "%s was renamed to multiple other directories, "
2106 "with no destination getting a majority of the "
2107 "files."),
2108 path, entry->dir);
2109 clean = 0;
2110 return NULL;
2111 }
2112
2113 /*
2114 * The caller needs to have ensured that it has pre-populated
2115 * collisions with all paths that map to new_path. Do a quick check
2116 * to ensure that's the case.
2117 */
2118 collision_ent = collision_find_entry(collisions, new_path);
2119 if (collision_ent == NULL)
2120 BUG("collision_ent is NULL");
2121
2122 /*
2123 * Check for one-sided add/add/.../add conflicts, i.e.
2124 * where implicit renames from the other side doing
2125 * directory rename(s) can affect this side of history
2126 * to put multiple paths into the same location. Warn
2127 * and bail on directory renames for such paths.
2128 */
2129 if (collision_ent->reported_already) {
2130 clean = 0;
Nguyễn Thái Ngọc Duy34e77712019-06-27 16:28:52 +07002131 } else if (tree_has_path(opt->repo, tree, new_path)) {
Elijah Newrenf6f77552018-04-19 10:58:08 -07002132 collision_ent->reported_already = 1;
2133 strbuf_add_separated_string_list(&collision_paths, ", ",
2134 &collision_ent->source_files);
Elijah Newren259ccb62019-04-05 08:00:13 -07002135 output(opt, 1, _("CONFLICT (implicit dir rename): Existing "
Elijah Newrenf6f77552018-04-19 10:58:08 -07002136 "file/dir at %s in the way of implicit "
2137 "directory rename(s) putting the following "
2138 "path(s) there: %s."),
2139 new_path, collision_paths.buf);
2140 clean = 0;
2141 } else if (collision_ent->source_files.nr > 1) {
2142 collision_ent->reported_already = 1;
2143 strbuf_add_separated_string_list(&collision_paths, ", ",
2144 &collision_ent->source_files);
Elijah Newren259ccb62019-04-05 08:00:13 -07002145 output(opt, 1, _("CONFLICT (implicit dir rename): Cannot map "
Elijah Newrenf6f77552018-04-19 10:58:08 -07002146 "more than one path to %s; implicit directory "
2147 "renames tried to put these paths there: %s"),
2148 new_path, collision_paths.buf);
2149 clean = 0;
2150 }
2151
2152 /* Free memory we no longer need */
2153 strbuf_release(&collision_paths);
2154 if (!clean && new_path) {
2155 free(new_path);
2156 return NULL;
2157 }
2158
2159 return new_path;
2160}
2161
2162/*
Elijah Newren96e7ffb2018-04-19 10:58:06 -07002163 * There are a couple things we want to do at the directory level:
2164 * 1. Check for both sides renaming to the same thing, in order to avoid
2165 * implicit renaming of files that should be left in place. (See
2166 * testcase 6b in t6043 for details.)
2167 * 2. Prune directory renames if there are still files left in the
Andrei Rybakabcb66c2021-06-11 13:18:50 +02002168 * original directory. These represent a partial directory rename,
Elijah Newren96e7ffb2018-04-19 10:58:06 -07002169 * i.e. a rename where only some of the files within the directory
2170 * were renamed elsewhere. (Technically, this could be done earlier
2171 * in get_directory_renames(), except that would prevent us from
2172 * doing the previous check and thus failing testcase 6b.)
2173 * 3. Check for rename/rename(1to2) conflicts (at the directory level).
2174 * In the future, we could potentially record this info as well and
2175 * omit reporting rename/rename(1to2) conflicts for each path within
2176 * the affected directories, thus cleaning up the merge output.
2177 * NOTE: We do NOT check for rename/rename(2to1) conflicts at the
2178 * directory level, because merging directories is fine. If it
2179 * causes conflicts for files within those merged directories, then
2180 * that should be detected at the individual path level.
2181 */
Elijah Newren259ccb62019-04-05 08:00:13 -07002182static void handle_directory_level_conflicts(struct merge_options *opt,
Elijah Newren96e7ffb2018-04-19 10:58:06 -07002183 struct hashmap *dir_re_head,
2184 struct tree *head,
2185 struct hashmap *dir_re_merge,
2186 struct tree *merge)
2187{
2188 struct hashmap_iter iter;
2189 struct dir_rename_entry *head_ent;
2190 struct dir_rename_entry *merge_ent;
2191
2192 struct string_list remove_from_head = STRING_LIST_INIT_NODUP;
2193 struct string_list remove_from_merge = STRING_LIST_INIT_NODUP;
2194
Eric Wong87571c32019-10-06 23:30:38 +00002195 hashmap_for_each_entry(dir_re_head, &iter, head_ent,
Eric Wong87571c32019-10-06 23:30:38 +00002196 ent /* member name */) {
Elijah Newren96e7ffb2018-04-19 10:58:06 -07002197 merge_ent = dir_rename_find_entry(dir_re_merge, head_ent->dir);
2198 if (merge_ent &&
2199 !head_ent->non_unique_new_dir &&
2200 !merge_ent->non_unique_new_dir &&
2201 !strbuf_cmp(&head_ent->new_dir, &merge_ent->new_dir)) {
2202 /* 1. Renamed identically; remove it from both sides */
2203 string_list_append(&remove_from_head,
2204 head_ent->dir)->util = head_ent;
2205 strbuf_release(&head_ent->new_dir);
2206 string_list_append(&remove_from_merge,
2207 merge_ent->dir)->util = merge_ent;
2208 strbuf_release(&merge_ent->new_dir);
Nguyễn Thái Ngọc Duy34e77712019-06-27 16:28:52 +07002209 } else if (tree_has_path(opt->repo, head, head_ent->dir)) {
Elijah Newren96e7ffb2018-04-19 10:58:06 -07002210 /* 2. This wasn't a directory rename after all */
2211 string_list_append(&remove_from_head,
2212 head_ent->dir)->util = head_ent;
2213 strbuf_release(&head_ent->new_dir);
2214 }
2215 }
2216
2217 remove_hashmap_entries(dir_re_head, &remove_from_head);
2218 remove_hashmap_entries(dir_re_merge, &remove_from_merge);
2219
Eric Wong87571c32019-10-06 23:30:38 +00002220 hashmap_for_each_entry(dir_re_merge, &iter, merge_ent,
Eric Wong87571c32019-10-06 23:30:38 +00002221 ent /* member name */) {
Elijah Newren96e7ffb2018-04-19 10:58:06 -07002222 head_ent = dir_rename_find_entry(dir_re_head, merge_ent->dir);
Nguyễn Thái Ngọc Duy34e77712019-06-27 16:28:52 +07002223 if (tree_has_path(opt->repo, merge, merge_ent->dir)) {
Elijah Newren96e7ffb2018-04-19 10:58:06 -07002224 /* 2. This wasn't a directory rename after all */
2225 string_list_append(&remove_from_merge,
2226 merge_ent->dir)->util = merge_ent;
2227 } else if (head_ent &&
2228 !head_ent->non_unique_new_dir &&
2229 !merge_ent->non_unique_new_dir) {
2230 /* 3. rename/rename(1to2) */
2231 /*
2232 * We can assume it's not rename/rename(1to1) because
2233 * that was case (1), already checked above. So we
2234 * know that head_ent->new_dir and merge_ent->new_dir
2235 * are different strings.
2236 */
Elijah Newren259ccb62019-04-05 08:00:13 -07002237 output(opt, 1, _("CONFLICT (rename/rename): "
Elijah Newren96e7ffb2018-04-19 10:58:06 -07002238 "Rename directory %s->%s in %s. "
2239 "Rename directory %s->%s in %s"),
Elijah Newren259ccb62019-04-05 08:00:13 -07002240 head_ent->dir, head_ent->new_dir.buf, opt->branch1,
2241 head_ent->dir, merge_ent->new_dir.buf, opt->branch2);
Elijah Newren96e7ffb2018-04-19 10:58:06 -07002242 string_list_append(&remove_from_head,
2243 head_ent->dir)->util = head_ent;
2244 strbuf_release(&head_ent->new_dir);
2245 string_list_append(&remove_from_merge,
2246 merge_ent->dir)->util = merge_ent;
2247 strbuf_release(&merge_ent->new_dir);
2248 }
2249 }
2250
2251 remove_hashmap_entries(dir_re_head, &remove_from_head);
2252 remove_hashmap_entries(dir_re_merge, &remove_from_merge);
2253}
2254
Jeff Kingb53c5022019-02-14 00:50:02 -05002255static struct hashmap *get_directory_renames(struct diff_queue_struct *pairs)
Elijah Newren7fe40b82018-04-19 10:58:05 -07002256{
2257 struct hashmap *dir_renames;
2258 struct hashmap_iter iter;
2259 struct dir_rename_entry *entry;
2260 int i;
2261
2262 /*
2263 * Typically, we think of a directory rename as all files from a
2264 * certain directory being moved to a target directory. However,
2265 * what if someone first moved two files from the original
2266 * directory in one commit, and then renamed the directory
2267 * somewhere else in a later commit? At merge time, we just know
2268 * that files from the original directory went to two different
2269 * places, and that the bulk of them ended up in the same place.
2270 * We want each directory rename to represent where the bulk of the
2271 * files from that directory end up; this function exists to find
2272 * where the bulk of the files went.
2273 *
2274 * The first loop below simply iterates through the list of file
2275 * renames, finding out how often each directory rename pair
2276 * possibility occurs.
2277 */
2278 dir_renames = xmalloc(sizeof(*dir_renames));
2279 dir_rename_init(dir_renames);
2280 for (i = 0; i < pairs->nr; ++i) {
2281 struct string_list_item *item;
2282 int *count;
2283 struct diff_filepair *pair = pairs->queue[i];
2284 char *old_dir, *new_dir;
2285
2286 /* File not part of directory rename if it wasn't renamed */
2287 if (pair->status != 'R')
2288 continue;
2289
2290 get_renamed_dir_portion(pair->one->path, pair->two->path,
2291 &old_dir, &new_dir);
2292 if (!old_dir)
2293 /* Directory didn't change at all; ignore this one. */
2294 continue;
2295
2296 entry = dir_rename_find_entry(dir_renames, old_dir);
2297 if (!entry) {
2298 entry = xmalloc(sizeof(*entry));
2299 dir_rename_entry_init(entry, old_dir);
Eric Wong26b455f2019-10-06 23:30:32 +00002300 hashmap_put(dir_renames, &entry->ent);
Elijah Newren7fe40b82018-04-19 10:58:05 -07002301 } else {
2302 free(old_dir);
2303 }
2304 item = string_list_lookup(&entry->possible_new_dirs, new_dir);
2305 if (!item) {
2306 item = string_list_insert(&entry->possible_new_dirs,
2307 new_dir);
2308 item->util = xcalloc(1, sizeof(int));
2309 } else {
2310 free(new_dir);
2311 }
2312 count = item->util;
2313 *count += 1;
2314 }
2315
2316 /*
2317 * For each directory with files moved out of it, we find out which
2318 * target directory received the most files so we can declare it to
2319 * be the "winning" target location for the directory rename. This
2320 * winner gets recorded in new_dir. If there is no winner
2321 * (multiple target directories received the same number of files),
2322 * we set non_unique_new_dir. Once we've determined the winner (or
2323 * that there is no winner), we no longer need possible_new_dirs.
2324 */
Eric Wong87571c32019-10-06 23:30:38 +00002325 hashmap_for_each_entry(dir_renames, &iter, entry,
Eric Wong87571c32019-10-06 23:30:38 +00002326 ent /* member name */) {
Elijah Newren7fe40b82018-04-19 10:58:05 -07002327 int max = 0;
2328 int bad_max = 0;
2329 char *best = NULL;
2330
2331 for (i = 0; i < entry->possible_new_dirs.nr; i++) {
2332 int *count = entry->possible_new_dirs.items[i].util;
2333
2334 if (*count == max)
2335 bad_max = max;
2336 else if (*count > max) {
2337 max = *count;
2338 best = entry->possible_new_dirs.items[i].string;
2339 }
2340 }
2341 if (bad_max == max)
2342 entry->non_unique_new_dir = 1;
2343 else {
2344 assert(entry->new_dir.len == 0);
2345 strbuf_addstr(&entry->new_dir, best);
2346 }
2347 /*
2348 * The relevant directory sub-portion of the original full
2349 * filepaths were xstrndup'ed before inserting into
2350 * possible_new_dirs, and instead of manually iterating the
2351 * list and free'ing each, just lie and tell
2352 * possible_new_dirs that it did the strdup'ing so that it
2353 * will free them for us.
2354 */
2355 entry->possible_new_dirs.strdup_strings = 1;
2356 string_list_clear(&entry->possible_new_dirs, 1);
2357 }
2358
2359 return dir_renames;
2360}
2361
Elijah Newrene95ab702018-04-19 10:58:07 -07002362static struct dir_rename_entry *check_dir_renamed(const char *path,
2363 struct hashmap *dir_renames)
2364{
René Scharfe9da2d032018-06-10 12:56:31 +02002365 char *temp = xstrdup(path);
Elijah Newrene95ab702018-04-19 10:58:07 -07002366 char *end;
Elijah Newrenc3b9bc92018-09-05 10:03:07 -07002367 struct dir_rename_entry *entry = NULL;
Elijah Newrene95ab702018-04-19 10:58:07 -07002368
Elijah Newrene95ab702018-04-19 10:58:07 -07002369 while ((end = strrchr(temp, '/'))) {
2370 *end = '\0';
2371 entry = dir_rename_find_entry(dir_renames, temp);
2372 if (entry)
René Scharfe9da2d032018-06-10 12:56:31 +02002373 break;
Elijah Newrene95ab702018-04-19 10:58:07 -07002374 }
René Scharfe9da2d032018-06-10 12:56:31 +02002375 free(temp);
2376 return entry;
Elijah Newrene95ab702018-04-19 10:58:07 -07002377}
2378
2379static void compute_collisions(struct hashmap *collisions,
2380 struct hashmap *dir_renames,
2381 struct diff_queue_struct *pairs)
2382{
2383 int i;
2384
2385 /*
2386 * Multiple files can be mapped to the same path due to directory
2387 * renames done by the other side of history. Since that other
2388 * side of history could have merged multiple directories into one,
2389 * if our side of history added the same file basename to each of
2390 * those directories, then all N of them would get implicitly
2391 * renamed by the directory rename detection into the same path,
2392 * and we'd get an add/add/.../add conflict, and all those adds
2393 * from *this* side of history. This is not representable in the
2394 * index, and users aren't going to easily be able to make sense of
2395 * it. So we need to provide a good warning about what's
2396 * happening, and fall back to no-directory-rename detection
2397 * behavior for those paths.
2398 *
2399 * See testcases 9e and all of section 5 from t6043 for examples.
2400 */
2401 collision_init(collisions);
2402
2403 for (i = 0; i < pairs->nr; ++i) {
2404 struct dir_rename_entry *dir_rename_ent;
2405 struct collision_entry *collision_ent;
2406 char *new_path;
2407 struct diff_filepair *pair = pairs->queue[i];
2408
Elijah Newren6e7e0272018-04-19 10:58:15 -07002409 if (pair->status != 'A' && pair->status != 'R')
Elijah Newrene95ab702018-04-19 10:58:07 -07002410 continue;
2411 dir_rename_ent = check_dir_renamed(pair->two->path,
2412 dir_renames);
2413 if (!dir_rename_ent)
2414 continue;
2415
2416 new_path = apply_dir_rename(dir_rename_ent, pair->two->path);
2417 if (!new_path)
2418 /*
2419 * dir_rename_ent->non_unique_new_path is true, which
2420 * means there is no directory rename for us to use,
2421 * which means it won't cause us any additional
2422 * collisions.
2423 */
2424 continue;
2425 collision_ent = collision_find_entry(collisions, new_path);
2426 if (!collision_ent) {
René Scharfeca56dad2021-03-13 17:17:22 +01002427 CALLOC_ARRAY(collision_ent, 1);
Eric Wongd22245a2019-10-06 23:30:27 +00002428 hashmap_entry_init(&collision_ent->ent,
2429 strhash(new_path));
Eric Wong26b455f2019-10-06 23:30:32 +00002430 hashmap_put(collisions, &collision_ent->ent);
Elijah Newrene95ab702018-04-19 10:58:07 -07002431 collision_ent->target_file = new_path;
2432 } else {
2433 free(new_path);
2434 }
2435 string_list_insert(&collision_ent->source_files,
2436 pair->two->path);
2437 }
2438}
2439
Elijah Newren259ccb62019-04-05 08:00:13 -07002440static char *check_for_directory_rename(struct merge_options *opt,
Elijah Newrenf6f77552018-04-19 10:58:08 -07002441 const char *path,
2442 struct tree *tree,
2443 struct hashmap *dir_renames,
2444 struct hashmap *dir_rename_exclusions,
2445 struct hashmap *collisions,
2446 int *clean_merge)
2447{
2448 char *new_path = NULL;
2449 struct dir_rename_entry *entry = check_dir_renamed(path, dir_renames);
2450 struct dir_rename_entry *oentry = NULL;
2451
2452 if (!entry)
2453 return new_path;
2454
2455 /*
2456 * This next part is a little weird. We do not want to do an
2457 * implicit rename into a directory we renamed on our side, because
2458 * that will result in a spurious rename/rename(1to2) conflict. An
2459 * example:
2460 * Base commit: dumbdir/afile, otherdir/bfile
2461 * Side 1: smrtdir/afile, otherdir/bfile
2462 * Side 2: dumbdir/afile, dumbdir/bfile
2463 * Here, while working on Side 1, we could notice that otherdir was
2464 * renamed/merged to dumbdir, and change the diff_filepair for
2465 * otherdir/bfile into a rename into dumbdir/bfile. However, Side
2466 * 2 will notice the rename from dumbdir to smrtdir, and do the
2467 * transitive rename to move it from dumbdir/bfile to
2468 * smrtdir/bfile. That gives us bfile in dumbdir vs being in
2469 * smrtdir, a rename/rename(1to2) conflict. We really just want
2470 * the file to end up in smrtdir. And the way to achieve that is
2471 * to not let Side1 do the rename to dumbdir, since we know that is
2472 * the source of one of our directory renames.
2473 *
2474 * That's why oentry and dir_rename_exclusions is here.
2475 *
2476 * As it turns out, this also prevents N-way transient rename
2477 * confusion; See testcases 9c and 9d of t6043.
2478 */
2479 oentry = dir_rename_find_entry(dir_rename_exclusions, entry->new_dir.buf);
2480 if (oentry) {
Elijah Newren259ccb62019-04-05 08:00:13 -07002481 output(opt, 1, _("WARNING: Avoiding applying %s -> %s rename "
Elijah Newrenf6f77552018-04-19 10:58:08 -07002482 "to %s, because %s itself was renamed."),
2483 entry->dir, entry->new_dir.buf, path, entry->new_dir.buf);
2484 } else {
Elijah Newren259ccb62019-04-05 08:00:13 -07002485 new_path = handle_path_level_conflicts(opt, path, entry,
Elijah Newrenf6f77552018-04-19 10:58:08 -07002486 collisions, tree);
2487 *clean_merge &= (new_path != NULL);
2488 }
2489
2490 return new_path;
2491}
2492
Elijah Newren259ccb62019-04-05 08:00:13 -07002493static void apply_directory_rename_modifications(struct merge_options *opt,
Elijah Newren9c0743f2018-04-19 10:58:10 -07002494 struct diff_filepair *pair,
2495 char *new_path,
2496 struct rename *re,
2497 struct tree *tree,
2498 struct tree *o_tree,
2499 struct tree *a_tree,
2500 struct tree *b_tree,
Jeff Kingb53c5022019-02-14 00:50:02 -05002501 struct string_list *entries)
Elijah Newren9c0743f2018-04-19 10:58:10 -07002502{
2503 struct string_list_item *item;
2504 int stage = (tree == a_tree ? 2 : 3);
Elijah Newren18797a32018-04-19 10:58:13 -07002505 int update_wd;
Elijah Newren9c0743f2018-04-19 10:58:10 -07002506
2507 /*
2508 * In all cases where we can do directory rename detection,
2509 * unpack_trees() will have read pair->two->path into the
2510 * index and the working copy. We need to remove it so that
2511 * we can instead place it at new_path. It is guaranteed to
2512 * not be untracked (unpack_trees() would have errored out
2513 * saying the file would have been overwritten), but it might
2514 * be dirty, though.
2515 */
Elijah Newren259ccb62019-04-05 08:00:13 -07002516 update_wd = !was_dirty(opt, pair->two->path);
Elijah Newren18797a32018-04-19 10:58:13 -07002517 if (!update_wd)
Elijah Newren259ccb62019-04-05 08:00:13 -07002518 output(opt, 1, _("Refusing to lose dirty file at %s"),
Elijah Newren18797a32018-04-19 10:58:13 -07002519 pair->two->path);
Elijah Newren259ccb62019-04-05 08:00:13 -07002520 remove_file(opt, 1, pair->two->path, !update_wd);
Elijah Newren9c0743f2018-04-19 10:58:10 -07002521
2522 /* Find or create a new re->dst_entry */
2523 item = string_list_lookup(entries, new_path);
2524 if (item) {
2525 /*
2526 * Since we're renaming on this side of history, and it's
2527 * due to a directory rename on the other side of history
2528 * (which we only allow when the directory in question no
2529 * longer exists on the other side of history), the
2530 * original entry for re->dst_entry is no longer
2531 * necessary...
2532 */
2533 re->dst_entry->processed = 1;
2534
2535 /*
2536 * ...because we'll be using this new one.
2537 */
2538 re->dst_entry = item->util;
2539 } else {
2540 /*
2541 * re->dst_entry is for the before-dir-rename path, and we
2542 * need it to hold information for the after-dir-rename
2543 * path. Before creating a new entry, we need to mark the
2544 * old one as unnecessary (...unless it is shared by
2545 * src_entry, i.e. this didn't use to be a rename, in which
2546 * case we can just allow the normal processing to happen
2547 * for it).
2548 */
2549 if (pair->status == 'R')
2550 re->dst_entry->processed = 1;
2551
Nguyễn Thái Ngọc Duy34e77712019-06-27 16:28:52 +07002552 re->dst_entry = insert_stage_data(opt->repo, new_path,
Elijah Newren9c0743f2018-04-19 10:58:10 -07002553 o_tree, a_tree, b_tree,
2554 entries);
2555 item = string_list_insert(entries, new_path);
2556 item->util = re->dst_entry;
2557 }
2558
2559 /*
2560 * Update the stage_data with the information about the path we are
2561 * moving into place. That slot will be empty and available for us
2562 * to write to because of the collision checks in
2563 * handle_path_level_conflicts(). In other words,
2564 * re->dst_entry->stages[stage].oid will be the null_oid, so it's
2565 * open for us to write to.
2566 *
2567 * It may be tempting to actually update the index at this point as
2568 * well, using update_stages_for_stage_data(), but as per the big
2569 * "NOTE" in update_stages(), doing so will modify the current
2570 * in-memory index which will break calls to would_lose_untracked()
2571 * that we need to make. Instead, we need to just make sure that
Elijah Newren8ebe7b02018-06-09 21:16:15 -07002572 * the various handle_rename_*() functions update the index
Elijah Newren9c0743f2018-04-19 10:58:10 -07002573 * explicitly rather than relying on unpack_trees() to have done it.
2574 */
Nguyễn Thái Ngọc Duy50ddb082019-06-27 16:28:49 +07002575 get_tree_entry(opt->repo,
2576 &tree->object.oid,
Elijah Newren9c0743f2018-04-19 10:58:10 -07002577 pair->two->path,
2578 &re->dst_entry->stages[stage].oid,
2579 &re->dst_entry->stages[stage].mode);
2580
Elijah Newren6d169fd2019-04-05 08:00:24 -07002581 /*
2582 * Record the original change status (or 'type' of change). If it
2583 * was originally an add ('A'), this lets us differentiate later
2584 * between a RENAME_DELETE conflict and RENAME_VIA_DIR (they
2585 * otherwise look the same). If it was originally a rename ('R'),
2586 * this lets us remember and report accurately about the transitive
2587 * renaming that occurred via the directory rename detection. Also,
2588 * record the original destination name.
2589 */
2590 re->dir_rename_original_type = pair->status;
2591 re->dir_rename_original_dest = pair->two->path;
2592
Elijah Newren9c0743f2018-04-19 10:58:10 -07002593 /*
2594 * We don't actually look at pair->status again, but it seems
2595 * pedagogically correct to adjust it.
2596 */
2597 pair->status = 'R';
2598
2599 /*
2600 * Finally, record the new location.
2601 */
2602 pair->two->path = new_path;
2603}
2604
Elijah Newrene5257b22018-04-19 10:58:03 -07002605/*
2606 * Get information of all renames which occurred in 'pairs', making use of
2607 * any implicit directory renames inferred from the other side of history.
2608 * We need the three trees in the merge ('o_tree', 'a_tree' and 'b_tree')
2609 * to be able to associate the correct cache entries with the rename
2610 * information; tree is always equal to either a_tree or b_tree.
2611 */
Elijah Newren259ccb62019-04-05 08:00:13 -07002612static struct string_list *get_renames(struct merge_options *opt,
Elijah Newrenc336ab82019-04-05 08:00:20 -07002613 const char *branch,
Elijah Newrene5257b22018-04-19 10:58:03 -07002614 struct diff_queue_struct *pairs,
Elijah Newrene95ab702018-04-19 10:58:07 -07002615 struct hashmap *dir_renames,
Elijah Newrenf6f77552018-04-19 10:58:08 -07002616 struct hashmap *dir_rename_exclusions,
Elijah Newrene5257b22018-04-19 10:58:03 -07002617 struct tree *tree,
2618 struct tree *o_tree,
2619 struct tree *a_tree,
2620 struct tree *b_tree,
Elijah Newrenf6f77552018-04-19 10:58:08 -07002621 struct string_list *entries,
2622 int *clean_merge)
Elijah Newrene5257b22018-04-19 10:58:03 -07002623{
2624 int i;
Elijah Newrene95ab702018-04-19 10:58:07 -07002625 struct hashmap collisions;
2626 struct hashmap_iter iter;
2627 struct collision_entry *e;
Elijah Newrene5257b22018-04-19 10:58:03 -07002628 struct string_list *renames;
2629
Elijah Newrene95ab702018-04-19 10:58:07 -07002630 compute_collisions(&collisions, dir_renames, pairs);
René Scharfeca56dad2021-03-13 17:17:22 +01002631 CALLOC_ARRAY(renames, 1);
Elijah Newrene5257b22018-04-19 10:58:03 -07002632
2633 for (i = 0; i < pairs->nr; ++i) {
Elijah Newren9ba91552018-04-19 10:57:59 -07002634 struct string_list_item *item;
2635 struct rename *re;
Elijah Newrene5257b22018-04-19 10:58:03 -07002636 struct diff_filepair *pair = pairs->queue[i];
Elijah Newrenf6f77552018-04-19 10:58:08 -07002637 char *new_path; /* non-NULL only with directory renames */
Elijah Newren9ba91552018-04-19 10:57:59 -07002638
Elijah Newren6e7e0272018-04-19 10:58:15 -07002639 if (pair->status != 'A' && pair->status != 'R') {
Elijah Newren9ba91552018-04-19 10:57:59 -07002640 diff_free_filepair(pair);
2641 continue;
2642 }
Elijah Newren259ccb62019-04-05 08:00:13 -07002643 new_path = check_for_directory_rename(opt, pair->two->path, tree,
Elijah Newrenf6f77552018-04-19 10:58:08 -07002644 dir_renames,
2645 dir_rename_exclusions,
2646 &collisions,
2647 clean_merge);
2648 if (pair->status != 'R' && !new_path) {
2649 diff_free_filepair(pair);
2650 continue;
2651 }
2652
Elijah Newren9ba91552018-04-19 10:57:59 -07002653 re = xmalloc(sizeof(*re));
2654 re->processed = 0;
2655 re->pair = pair;
Elijah Newrenc336ab82019-04-05 08:00:20 -07002656 re->branch = branch;
Elijah Newren6d169fd2019-04-05 08:00:24 -07002657 re->dir_rename_original_type = '\0';
2658 re->dir_rename_original_dest = NULL;
Elijah Newren9ba91552018-04-19 10:57:59 -07002659 item = string_list_lookup(entries, re->pair->one->path);
2660 if (!item)
Nguyễn Thái Ngọc Duy34e77712019-06-27 16:28:52 +07002661 re->src_entry = insert_stage_data(opt->repo,
2662 re->pair->one->path,
Elijah Newren9ba91552018-04-19 10:57:59 -07002663 o_tree, a_tree, b_tree, entries);
2664 else
2665 re->src_entry = item->util;
2666
2667 item = string_list_lookup(entries, re->pair->two->path);
2668 if (!item)
Nguyễn Thái Ngọc Duy34e77712019-06-27 16:28:52 +07002669 re->dst_entry = insert_stage_data(opt->repo,
2670 re->pair->two->path,
Elijah Newren9ba91552018-04-19 10:57:59 -07002671 o_tree, a_tree, b_tree, entries);
2672 else
2673 re->dst_entry = item->util;
2674 item = string_list_insert(renames, pair->one->path);
2675 item->util = re;
Elijah Newren9c0743f2018-04-19 10:58:10 -07002676 if (new_path)
Elijah Newren259ccb62019-04-05 08:00:13 -07002677 apply_directory_rename_modifications(opt, pair, new_path,
Elijah Newren9c0743f2018-04-19 10:58:10 -07002678 re, tree, o_tree,
2679 a_tree, b_tree,
Jeff Kingb53c5022019-02-14 00:50:02 -05002680 entries);
Elijah Newren9ba91552018-04-19 10:57:59 -07002681 }
Elijah Newrene95ab702018-04-19 10:58:07 -07002682
Eric Wong87571c32019-10-06 23:30:38 +00002683 hashmap_for_each_entry(&collisions, &iter, e,
Eric Wong87571c32019-10-06 23:30:38 +00002684 ent /* member name */) {
Elijah Newrene95ab702018-04-19 10:58:07 -07002685 free(e->target_file);
2686 string_list_clear(&e->source_files, 0);
2687 }
Elijah Newren6da1a252020-11-02 18:55:05 +00002688 hashmap_clear_and_free(&collisions, struct collision_entry, ent);
Elijah Newren9ba91552018-04-19 10:57:59 -07002689 return renames;
2690}
2691
Elijah Newren259ccb62019-04-05 08:00:13 -07002692static int process_renames(struct merge_options *opt,
Miklos Vajna8a2fce12008-08-25 16:25:57 +02002693 struct string_list *a_renames,
2694 struct string_list *b_renames)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002695{
2696 int clean_merge = 1, i, j;
Thiago Farina183113a2010-07-04 16:46:19 -03002697 struct string_list a_by_dst = STRING_LIST_INIT_NODUP;
2698 struct string_list b_by_dst = STRING_LIST_INIT_NODUP;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002699 const struct rename *sre;
2700
Elijah Newren816147e2021-03-20 00:03:53 +00002701 /*
2702 * FIXME: As string-list.h notes, it's O(n^2) to build a sorted
2703 * string_list one-by-one, but O(n log n) to build it unsorted and
2704 * then sort it. Note that as we build the list, we do not need to
2705 * check if the existing destination path is already in the list,
2706 * because the structure of diffcore_rename guarantees we won't
2707 * have duplicates.
2708 */
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002709 for (i = 0; i < a_renames->nr; i++) {
2710 sre = a_renames->items[i].util;
Julian Phillips78a395d2010-06-26 00:41:35 +01002711 string_list_insert(&a_by_dst, sre->pair->two->path)->util
Elijah Newren0a6b8712011-08-11 23:20:04 -06002712 = (void *)sre;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002713 }
2714 for (i = 0; i < b_renames->nr; i++) {
2715 sre = b_renames->items[i].util;
Julian Phillips78a395d2010-06-26 00:41:35 +01002716 string_list_insert(&b_by_dst, sre->pair->two->path)->util
Elijah Newren0a6b8712011-08-11 23:20:04 -06002717 = (void *)sre;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002718 }
2719
2720 for (i = 0, j = 0; i < a_renames->nr || j < b_renames->nr;) {
Benjamin Kramer8e24cba2009-03-15 22:01:20 +01002721 struct string_list *renames1, *renames2Dst;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002722 struct rename *ren1 = NULL, *ren2 = NULL;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002723 const char *ren1_src, *ren1_dst;
Elijah Newren461f5042011-08-11 23:20:15 -06002724 struct string_list_item *lookup;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002725
2726 if (i >= a_renames->nr) {
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002727 ren2 = b_renames->items[j++].util;
2728 } else if (j >= b_renames->nr) {
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002729 ren1 = a_renames->items[i++].util;
2730 } else {
Benjamin Kramer8e24cba2009-03-15 22:01:20 +01002731 int compare = strcmp(a_renames->items[i].string,
2732 b_renames->items[j].string);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002733 if (compare <= 0)
2734 ren1 = a_renames->items[i++].util;
2735 if (compare >= 0)
2736 ren2 = b_renames->items[j++].util;
2737 }
2738
2739 /* TODO: refactor, so that 1/2 are not needed */
2740 if (ren1) {
2741 renames1 = a_renames;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002742 renames2Dst = &b_by_dst;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002743 } else {
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002744 renames1 = b_renames;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002745 renames2Dst = &a_by_dst;
René Scharfe35d803b2017-01-28 22:40:58 +01002746 SWAP(ren2, ren1);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002747 }
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002748
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002749 if (ren1->processed)
2750 continue;
2751 ren1->processed = 1;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002752 ren1->dst_entry->processed = 1;
Elijah Newren7769a752011-08-11 23:20:05 -06002753 /* BUG: We should only mark src_entry as processed if we
2754 * are not dealing with a rename + add-source case.
2755 */
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002756 ren1->src_entry->processed = 1;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002757
2758 ren1_src = ren1->pair->one->path;
2759 ren1_dst = ren1->pair->two->path;
2760
2761 if (ren2) {
Elijah Newren461f5042011-08-11 23:20:15 -06002762 /* One file renamed on both sides */
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002763 const char *ren2_src = ren2->pair->one->path;
2764 const char *ren2_dst = ren2->pair->two->path;
Elijah Newren4f66dad2011-08-11 23:20:08 -06002765 enum rename_type rename_type;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002766 if (strcmp(ren1_src, ren2_src) != 0)
Johannes Schindelin033abf92018-05-02 11:38:39 +02002767 BUG("ren1_src != ren2_src");
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002768 ren2->dst_entry->processed = 1;
2769 ren2->processed = 1;
2770 if (strcmp(ren1_dst, ren2_dst) != 0) {
Elijah Newren4f66dad2011-08-11 23:20:08 -06002771 rename_type = RENAME_ONE_FILE_TO_TWO;
Elijah Newren461f5042011-08-11 23:20:15 -06002772 clean_merge = 0;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002773 } else {
Elijah Newren4f66dad2011-08-11 23:20:08 -06002774 rename_type = RENAME_ONE_FILE_TO_ONE;
Elijah Newren7769a752011-08-11 23:20:05 -06002775 /* BUG: We should only remove ren1_src in
2776 * the base stage (think of rename +
2777 * add-source cases).
2778 */
Elijah Newren259ccb62019-04-05 08:00:13 -07002779 remove_file(opt, 1, ren1_src, 1);
Elijah Newrenb8ddf162011-08-11 23:20:02 -06002780 update_entry(ren1->dst_entry,
2781 ren1->pair->one,
2782 ren1->pair->two,
2783 ren2->pair->two);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002784 }
Elijah Newrenc336ab82019-04-05 08:00:20 -07002785 setup_rename_conflict_info(rename_type, opt, ren1, ren2);
Elijah Newren461f5042011-08-11 23:20:15 -06002786 } else if ((lookup = string_list_lookup(renames2Dst, ren1_dst))) {
2787 /* Two different files renamed to the same thing */
2788 char *ren2_dst;
2789 ren2 = lookup->util;
2790 ren2_dst = ren2->pair->two->path;
2791 if (strcmp(ren1_dst, ren2_dst) != 0)
Johannes Schindelin033abf92018-05-02 11:38:39 +02002792 BUG("ren1_dst != ren2_dst");
Elijah Newren461f5042011-08-11 23:20:15 -06002793
2794 clean_merge = 0;
2795 ren2->processed = 1;
2796 /*
2797 * BUG: We should only mark src_entry as processed
2798 * if we are not dealing with a rename + add-source
2799 * case.
2800 */
2801 ren2->src_entry->processed = 1;
2802
2803 setup_rename_conflict_info(RENAME_TWO_FILES_TO_ONE,
Elijah Newrenc336ab82019-04-05 08:00:20 -07002804 opt, ren1, ren2);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002805 } else {
2806 /* Renamed in 1, maybe changed in 2 */
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002807 /* we only use sha1 and mode of these */
2808 struct diff_filespec src_other, dst_other;
Elijah Newren41d70bd2010-09-20 02:28:47 -06002809 int try_merge;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002810
Elijah Newren41d70bd2010-09-20 02:28:47 -06002811 /*
2812 * unpack_trees loads entries from common-commit
2813 * into stage 1, from head-commit into stage 2, and
2814 * from merge-commit into stage 3. We keep track
2815 * of which side corresponds to the rename.
2816 */
2817 int renamed_stage = a_renames == renames1 ? 2 : 3;
2818 int other_stage = a_renames == renames1 ? 3 : 2;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002819
Elijah Newren3585d0e2021-06-30 17:30:00 +00002820 /*
2821 * Directory renames have a funny corner case...
2822 */
2823 int renamed_to_self = !strcmp(ren1_src, ren1_dst);
2824
Elijah Newren7769a752011-08-11 23:20:05 -06002825 /* BUG: We should only remove ren1_src in the base
2826 * stage and in other_stage (think of rename +
2827 * add-source case).
2828 */
Elijah Newren3585d0e2021-06-30 17:30:00 +00002829 if (!renamed_to_self)
2830 remove_file(opt, 1, ren1_src,
2831 renamed_stage == 2 ||
2832 !was_tracked(opt, ren1_src));
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002833
brian m. carlsonfd429e92016-06-24 23:09:25 +00002834 oidcpy(&src_other.oid,
2835 &ren1->src_entry->stages[other_stage].oid);
Elijah Newren41d70bd2010-09-20 02:28:47 -06002836 src_other.mode = ren1->src_entry->stages[other_stage].mode;
brian m. carlsonfd429e92016-06-24 23:09:25 +00002837 oidcpy(&dst_other.oid,
2838 &ren1->dst_entry->stages[other_stage].oid);
Elijah Newren41d70bd2010-09-20 02:28:47 -06002839 dst_other.mode = ren1->dst_entry->stages[other_stage].mode;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002840 try_merge = 0;
2841
brian m. carlson14228442021-04-26 01:02:56 +00002842 if (oideq(&src_other.oid, null_oid()) &&
Elijah Newren6d169fd2019-04-05 08:00:24 -07002843 ren1->dir_rename_original_type == 'A') {
Elijah Newren5455c332018-06-09 21:16:14 -07002844 setup_rename_conflict_info(RENAME_VIA_DIR,
Elijah Newrenc336ab82019-04-05 08:00:20 -07002845 opt, ren1, NULL);
Elijah Newren3585d0e2021-06-30 17:30:00 +00002846 } else if (renamed_to_self) {
2847 setup_rename_conflict_info(RENAME_NORMAL,
2848 opt, ren1, NULL);
brian m. carlson14228442021-04-26 01:02:56 +00002849 } else if (oideq(&src_other.oid, null_oid())) {
Elijah Newren4f66dad2011-08-11 23:20:08 -06002850 setup_rename_conflict_info(RENAME_DELETE,
Elijah Newrenc336ab82019-04-05 08:00:20 -07002851 opt, ren1, NULL);
Schalk, Kend5af5102010-09-01 13:15:32 -07002852 } else if ((dst_other.mode == ren1->pair->two->mode) &&
Elijah Newren763a59e2020-01-01 05:20:57 +00002853 oideq(&dst_other.oid, &ren1->pair->two->oid)) {
Elijah Newren35a74ab2011-08-11 23:20:27 -06002854 /*
2855 * Added file on the other side identical to
2856 * the file being renamed: clean merge.
2857 * Also, there is no need to overwrite the
2858 * file already in the working copy, so call
2859 * update_file_flags() instead of
2860 * update_file().
2861 */
Elijah Newren259ccb62019-04-05 08:00:13 -07002862 if (update_file_flags(opt,
Elijah Newren8daec1d2019-04-05 08:00:22 -07002863 ren1->pair->two,
Johannes Schindelin75456f92016-07-26 18:06:21 +02002864 ren1_dst,
2865 1, /* update_cache */
2866 0 /* update_wd */))
2867 clean_merge = -1;
brian m. carlson14228442021-04-26 01:02:56 +00002868 } else if (!oideq(&dst_other.oid, null_oid())) {
Elijah Newren7f867162018-11-07 20:40:26 -08002869 /*
2870 * Probably not a clean merge, but it's
2871 * premature to set clean_merge to 0 here,
2872 * because if the rename merges cleanly and
2873 * the merge exactly matches the newly added
2874 * file, then the merge will be clean.
2875 */
2876 setup_rename_conflict_info(RENAME_ADD,
Elijah Newrenc336ab82019-04-05 08:00:20 -07002877 opt, ren1, NULL);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002878 } else
2879 try_merge = 1;
2880
Johannes Schindelin75456f92016-07-26 18:06:21 +02002881 if (clean_merge < 0)
2882 goto cleanup_and_return;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002883 if (try_merge) {
Elijah Newrene3de8882019-04-05 08:00:14 -07002884 struct diff_filespec *o, *a, *b;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002885 src_other.path = (char *)ren1_src;
2886
Elijah Newrene3de8882019-04-05 08:00:14 -07002887 o = ren1->pair->one;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002888 if (a_renames == renames1) {
2889 a = ren1->pair->two;
2890 b = &src_other;
2891 } else {
2892 b = ren1->pair->two;
2893 a = &src_other;
2894 }
Elijah Newrene3de8882019-04-05 08:00:14 -07002895 update_entry(ren1->dst_entry, o, a, b);
Elijah Newren4f66dad2011-08-11 23:20:08 -06002896 setup_rename_conflict_info(RENAME_NORMAL,
Elijah Newrenc336ab82019-04-05 08:00:20 -07002897 opt, ren1, NULL);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002898 }
2899 }
2900 }
Johannes Schindelin75456f92016-07-26 18:06:21 +02002901cleanup_and_return:
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002902 string_list_clear(&a_by_dst, 0);
2903 string_list_clear(&b_by_dst, 0);
2904
2905 return clean_merge;
2906}
2907
Elijah Newrenf1725892018-04-19 10:58:00 -07002908struct rename_info {
2909 struct string_list *head_renames;
2910 struct string_list *merge_renames;
2911};
2912
Elijah Newren7fe40b82018-04-19 10:58:05 -07002913static void initial_cleanup_rename(struct diff_queue_struct *pairs,
2914 struct hashmap *dir_renames)
Elijah Newrenffc16c42018-04-19 10:58:04 -07002915{
Elijah Newren7fe40b82018-04-19 10:58:05 -07002916 struct hashmap_iter iter;
2917 struct dir_rename_entry *e;
2918
Eric Wong87571c32019-10-06 23:30:38 +00002919 hashmap_for_each_entry(dir_renames, &iter, e,
Eric Wong87571c32019-10-06 23:30:38 +00002920 ent /* member name */) {
Elijah Newren7fe40b82018-04-19 10:58:05 -07002921 free(e->dir);
2922 strbuf_release(&e->new_dir);
2923 /* possible_new_dirs already cleared in get_directory_renames */
2924 }
Elijah Newren6da1a252020-11-02 18:55:05 +00002925 hashmap_clear_and_free(dir_renames, struct dir_rename_entry, ent);
Elijah Newren7fe40b82018-04-19 10:58:05 -07002926 free(dir_renames);
2927
Elijah Newrenffc16c42018-04-19 10:58:04 -07002928 free(pairs->queue);
2929 free(pairs);
2930}
2931
Elijah Newren259ccb62019-04-05 08:00:13 -07002932static int detect_and_process_renames(struct merge_options *opt,
Elijah Newren8ebe7b02018-06-09 21:16:15 -07002933 struct tree *common,
2934 struct tree *head,
2935 struct tree *merge,
2936 struct string_list *entries,
2937 struct rename_info *ri)
Elijah Newrenf1725892018-04-19 10:58:00 -07002938{
Elijah Newrene5257b22018-04-19 10:58:03 -07002939 struct diff_queue_struct *head_pairs, *merge_pairs;
Elijah Newren7fe40b82018-04-19 10:58:05 -07002940 struct hashmap *dir_re_head, *dir_re_merge;
Elijah Newrenf6f77552018-04-19 10:58:08 -07002941 int clean = 1;
Elijah Newrene5257b22018-04-19 10:58:03 -07002942
Elijah Newren3992ff02018-04-19 10:58:02 -07002943 ri->head_renames = NULL;
2944 ri->merge_renames = NULL;
2945
Elijah Newren259ccb62019-04-05 08:00:13 -07002946 if (!merge_detect_rename(opt))
Elijah Newren3992ff02018-04-19 10:58:02 -07002947 return 1;
2948
Elijah Newren259ccb62019-04-05 08:00:13 -07002949 head_pairs = get_diffpairs(opt, common, head);
2950 merge_pairs = get_diffpairs(opt, common, merge);
Elijah Newrene5257b22018-04-19 10:58:03 -07002951
Derrick Stolee8e012512019-08-17 11:41:25 -07002952 if ((opt->detect_directory_renames == MERGE_DIRECTORY_RENAMES_TRUE) ||
2953 (opt->detect_directory_renames == MERGE_DIRECTORY_RENAMES_CONFLICT &&
Elijah Newren5bf7e572019-08-17 11:41:41 -07002954 !opt->priv->call_depth)) {
Jeff Kingb53c5022019-02-14 00:50:02 -05002955 dir_re_head = get_directory_renames(head_pairs);
2956 dir_re_merge = get_directory_renames(merge_pairs);
Elijah Newren7fe40b82018-04-19 10:58:05 -07002957
Elijah Newren259ccb62019-04-05 08:00:13 -07002958 handle_directory_level_conflicts(opt,
Elijah Newren5fdddd92018-08-29 00:06:12 -07002959 dir_re_head, head,
2960 dir_re_merge, merge);
2961 } else {
2962 dir_re_head = xmalloc(sizeof(*dir_re_head));
2963 dir_re_merge = xmalloc(sizeof(*dir_re_merge));
2964 dir_rename_init(dir_re_head);
2965 dir_rename_init(dir_re_merge);
2966 }
Elijah Newren96e7ffb2018-04-19 10:58:06 -07002967
Elijah Newrenc336ab82019-04-05 08:00:20 -07002968 ri->head_renames = get_renames(opt, opt->branch1, head_pairs,
Elijah Newrenf6f77552018-04-19 10:58:08 -07002969 dir_re_merge, dir_re_head, head,
2970 common, head, merge, entries,
2971 &clean);
2972 if (clean < 0)
2973 goto cleanup;
Elijah Newrenc336ab82019-04-05 08:00:20 -07002974 ri->merge_renames = get_renames(opt, opt->branch2, merge_pairs,
Elijah Newrenf6f77552018-04-19 10:58:08 -07002975 dir_re_head, dir_re_merge, merge,
2976 common, head, merge, entries,
2977 &clean);
2978 if (clean < 0)
2979 goto cleanup;
Elijah Newren259ccb62019-04-05 08:00:13 -07002980 clean &= process_renames(opt, ri->head_renames, ri->merge_renames);
Elijah Newrene5257b22018-04-19 10:58:03 -07002981
Elijah Newrenf6f77552018-04-19 10:58:08 -07002982cleanup:
Elijah Newrene5257b22018-04-19 10:58:03 -07002983 /*
2984 * Some cleanup is deferred until cleanup_renames() because the
2985 * data structures are still needed and referenced in
2986 * process_entry(). But there are a few things we can free now.
2987 */
Elijah Newren7fe40b82018-04-19 10:58:05 -07002988 initial_cleanup_rename(head_pairs, dir_re_head);
2989 initial_cleanup_rename(merge_pairs, dir_re_merge);
Elijah Newrene5257b22018-04-19 10:58:03 -07002990
2991 return clean;
Elijah Newrenf1725892018-04-19 10:58:00 -07002992}
2993
Elijah Newrenffc16c42018-04-19 10:58:04 -07002994static void final_cleanup_rename(struct string_list *rename)
Elijah Newren9cfee252018-04-19 10:58:01 -07002995{
2996 const struct rename *re;
2997 int i;
2998
Elijah Newren3992ff02018-04-19 10:58:02 -07002999 if (rename == NULL)
3000 return;
3001
Elijah Newren9cfee252018-04-19 10:58:01 -07003002 for (i = 0; i < rename->nr; i++) {
3003 re = rename->items[i].util;
3004 diff_free_filepair(re->pair);
3005 }
3006 string_list_clear(rename, 1);
3007 free(rename);
3008}
3009
Elijah Newrenffc16c42018-04-19 10:58:04 -07003010static void final_cleanup_renames(struct rename_info *re_info)
Elijah Newrenf1725892018-04-19 10:58:00 -07003011{
Elijah Newrenffc16c42018-04-19 10:58:04 -07003012 final_cleanup_rename(re_info->head_renames);
3013 final_cleanup_rename(re_info->merge_renames);
Elijah Newrenf1725892018-04-19 10:58:00 -07003014}
3015
Elijah Newren259ccb62019-04-05 08:00:13 -07003016static int read_oid_strbuf(struct merge_options *opt,
Elijah Newrend90e7592018-06-09 21:16:12 -07003017 const struct object_id *oid,
3018 struct strbuf *dst)
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02003019{
3020 void *buf;
3021 enum object_type type;
3022 unsigned long size;
brian m. carlsonb4f5aca2018-03-12 02:27:53 +00003023 buf = read_object_file(oid, &type, &size);
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02003024 if (!buf)
Elijah Newren259ccb62019-04-05 08:00:13 -07003025 return err(opt, _("cannot read object %s"), oid_to_hex(oid));
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02003026 if (type != OBJ_BLOB) {
3027 free(buf);
Elijah Newren259ccb62019-04-05 08:00:13 -07003028 return err(opt, _("object %s is not a blob"), oid_to_hex(oid));
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02003029 }
3030 strbuf_attach(dst, buf, size, size + 1);
3031 return 0;
3032}
3033
Johannes Schindelinbc9204d2016-08-01 13:44:37 +02003034static int blob_unchanged(struct merge_options *opt,
Elijah Newren8daec1d2019-04-05 08:00:22 -07003035 const struct diff_filespec *o,
3036 const struct diff_filespec *a,
Jonathan Nieder3e7589b2010-08-05 06:13:49 -05003037 int renormalize, const char *path)
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02003038{
Elijah Newren93a02c52019-04-05 08:00:15 -07003039 struct strbuf obuf = STRBUF_INIT;
3040 struct strbuf abuf = STRBUF_INIT;
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02003041 int ret = 0; /* assume changed for safety */
Derrick Stolee847a9e52021-04-01 01:49:39 +00003042 struct index_state *idx = opt->repo->index;
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02003043
Elijah Newren8daec1d2019-04-05 08:00:22 -07003044 if (a->mode != o->mode)
Jeff King72fac662015-10-26 17:39:39 -04003045 return 0;
Elijah Newren763a59e2020-01-01 05:20:57 +00003046 if (oideq(&o->oid, &a->oid))
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02003047 return 1;
Jonathan Nieder3e7589b2010-08-05 06:13:49 -05003048 if (!renormalize)
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02003049 return 0;
3050
Elijah Newren8daec1d2019-04-05 08:00:22 -07003051 if (read_oid_strbuf(opt, &o->oid, &obuf) ||
3052 read_oid_strbuf(opt, &a->oid, &abuf))
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02003053 goto error_return;
3054 /*
3055 * Note: binary | is used so that both renormalizations are
3056 * performed. Comparison can be skipped if both files are
3057 * unchanged since their sha1s have already been compared.
3058 */
Elijah Newren93a02c52019-04-05 08:00:15 -07003059 if (renormalize_buffer(idx, path, obuf.buf, obuf.len, &obuf) |
3060 renormalize_buffer(idx, path, abuf.buf, abuf.len, &abuf))
3061 ret = (obuf.len == abuf.len && !memcmp(obuf.buf, abuf.buf, obuf.len));
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02003062
3063error_return:
Elijah Newren93a02c52019-04-05 08:00:15 -07003064 strbuf_release(&obuf);
3065 strbuf_release(&abuf);
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02003066 return ret;
3067}
3068
Elijah Newren259ccb62019-04-05 08:00:13 -07003069static int handle_modify_delete(struct merge_options *opt,
Elijah Newrend90e7592018-06-09 21:16:12 -07003070 const char *path,
Elijah Newren8daec1d2019-04-05 08:00:22 -07003071 const struct diff_filespec *o,
3072 const struct diff_filespec *a,
3073 const struct diff_filespec *b)
Elijah Newren5e3ce662010-09-20 02:28:51 -06003074{
Matt McCutchenb26d87f2017-01-28 15:37:01 -05003075 const char *modify_branch, *delete_branch;
Elijah Newren8daec1d2019-04-05 08:00:22 -07003076 const struct diff_filespec *changed;
Matt McCutchenb26d87f2017-01-28 15:37:01 -05003077
Elijah Newren8daec1d2019-04-05 08:00:22 -07003078 if (is_valid(a)) {
Elijah Newren259ccb62019-04-05 08:00:13 -07003079 modify_branch = opt->branch1;
3080 delete_branch = opt->branch2;
Elijah Newren8daec1d2019-04-05 08:00:22 -07003081 changed = a;
Matt McCutchenb26d87f2017-01-28 15:37:01 -05003082 } else {
Elijah Newren259ccb62019-04-05 08:00:13 -07003083 modify_branch = opt->branch2;
3084 delete_branch = opt->branch1;
Elijah Newren8daec1d2019-04-05 08:00:22 -07003085 changed = b;
Matt McCutchenb26d87f2017-01-28 15:37:01 -05003086 }
3087
Elijah Newren259ccb62019-04-05 08:00:13 -07003088 return handle_change_delete(opt,
Matt McCutchenb26d87f2017-01-28 15:37:01 -05003089 path, NULL,
Elijah Newren8daec1d2019-04-05 08:00:22 -07003090 o, changed,
Matt McCutchenb26d87f2017-01-28 15:37:01 -05003091 modify_branch, delete_branch,
Johannes Schindelin75456f92016-07-26 18:06:21 +02003092 _("modify"), _("modified"));
Elijah Newren5e3ce662010-09-20 02:28:51 -06003093}
3094
Elijah Newrene62d1122019-04-05 08:00:25 -07003095static int handle_content_merge(struct merge_file_info *mfi,
3096 struct merge_options *opt,
Elijah Newrend9573552018-09-19 09:14:34 -07003097 const char *path,
3098 int is_dirty,
Elijah Newren8daec1d2019-04-05 08:00:22 -07003099 const struct diff_filespec *o,
3100 const struct diff_filespec *a,
3101 const struct diff_filespec *b,
Elijah Newren043622b2019-04-05 08:00:16 -07003102 struct rename_conflict_info *ci)
Elijah Newren0c4918d2010-09-20 02:28:52 -06003103{
Jiang Xin55653a62012-07-25 22:53:13 +08003104 const char *reason = _("content");
Elijah Newren4ab9a152010-09-20 02:29:07 -06003105 unsigned df_conflict_remains = 0;
Elijah Newren0c4918d2010-09-20 02:28:52 -06003106
Elijah Newren8daec1d2019-04-05 08:00:22 -07003107 if (!is_valid(o))
Jiang Xin55653a62012-07-25 22:53:13 +08003108 reason = _("add/add");
Elijah Newren0c4918d2010-09-20 02:28:52 -06003109
Elijah Newren8daec1d2019-04-05 08:00:22 -07003110 assert(o->path && a->path && b->path);
Elijah Newren5bf7e572019-08-17 11:41:41 -07003111 if (ci && dir_in_way(opt->repo->index, path, !opt->priv->call_depth,
Elijah Newren8daec1d2019-04-05 08:00:22 -07003112 S_ISGITLINK(ci->ren1->pair->two->mode)))
3113 df_conflict_remains = 1;
Elijah Newren3c217c02011-08-11 23:20:09 -06003114
Elijah Newren8daec1d2019-04-05 08:00:22 -07003115 if (merge_mode_and_contents(opt, o, a, b, path,
Elijah Newren259ccb62019-04-05 08:00:13 -07003116 opt->branch1, opt->branch2,
Elijah Newren5bf7e572019-08-17 11:41:41 -07003117 opt->priv->call_depth * 2, mfi))
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +02003118 return -1;
Elijah Newren4ab9a152010-09-20 02:29:07 -06003119
Elijah Newren1de70db2018-04-19 10:58:23 -07003120 /*
3121 * We can skip updating the working tree file iff:
3122 * a) The merge is clean
3123 * b) The merge matches what was in HEAD (content, mode, pathname)
3124 * c) The target path is usable (i.e. not involved in D/F conflict)
3125 */
Elijah Newrene62d1122019-04-05 08:00:25 -07003126 if (mfi->clean && was_tracked_and_matches(opt, path, &mfi->blob) &&
Elijah Newren1de70db2018-04-19 10:58:23 -07003127 !df_conflict_remains) {
Elijah Newren2b75fb62018-07-27 12:59:44 +00003128 int pos;
3129 struct cache_entry *ce;
3130
Elijah Newren259ccb62019-04-05 08:00:13 -07003131 output(opt, 3, _("Skipped %s (merged same as existing)"), path);
Elijah Newrene62d1122019-04-05 08:00:25 -07003132 if (add_cacheinfo(opt, &mfi->blob, path,
Elijah Newren5bf7e572019-08-17 11:41:41 -07003133 0, (!opt->priv->call_depth && !is_dirty), 0))
Elijah Newren1de70db2018-04-19 10:58:23 -07003134 return -1;
Elijah Newren2b75fb62018-07-27 12:59:44 +00003135 /*
3136 * However, add_cacheinfo() will delete the old cache entry
3137 * and add a new one. We need to copy over any skip_worktree
3138 * flag to avoid making the file appear as if it were
3139 * deleted by the user.
3140 */
Elijah Newren5bf7e572019-08-17 11:41:41 -07003141 pos = index_name_pos(&opt->priv->orig_index, path, strlen(path));
3142 ce = opt->priv->orig_index.cache[pos];
Elijah Newren2b75fb62018-07-27 12:59:44 +00003143 if (ce_skip_worktree(ce)) {
Elijah Newren259ccb62019-04-05 08:00:13 -07003144 pos = index_name_pos(opt->repo->index, path, strlen(path));
3145 ce = opt->repo->index->cache[pos];
Elijah Newren2b75fb62018-07-27 12:59:44 +00003146 ce->ce_flags |= CE_SKIP_WORKTREE;
3147 }
Elijah Newrene62d1122019-04-05 08:00:25 -07003148 return mfi->clean;
Elijah Newren05cf21e2018-04-19 10:58:22 -07003149 }
Elijah Newren0c4918d2010-09-20 02:28:52 -06003150
Elijah Newrene62d1122019-04-05 08:00:25 -07003151 if (!mfi->clean) {
3152 if (S_ISGITLINK(mfi->blob.mode))
Jiang Xin55653a62012-07-25 22:53:13 +08003153 reason = _("submodule");
Elijah Newren259ccb62019-04-05 08:00:13 -07003154 output(opt, 1, _("CONFLICT (%s): Merge conflict in %s"),
Elijah Newren0c4918d2010-09-20 02:28:52 -06003155 reason, path);
Elijah Newren043622b2019-04-05 08:00:16 -07003156 if (ci && !df_conflict_remains)
Elijah Newren8daec1d2019-04-05 08:00:22 -07003157 if (update_stages(opt, path, o, a, b))
Johannes Schindelin75456f92016-07-26 18:06:21 +02003158 return -1;
Elijah Newren0c4918d2010-09-20 02:28:52 -06003159 }
3160
Elijah Newrenbd423802018-04-19 10:58:17 -07003161 if (df_conflict_remains || is_dirty) {
Elijah Newren3d6b8e82011-08-11 23:19:53 -06003162 char *new_path;
Elijah Newren5bf7e572019-08-17 11:41:41 -07003163 if (opt->priv->call_depth) {
Elijah Newren259ccb62019-04-05 08:00:13 -07003164 remove_file_from_index(opt->repo->index, path);
Elijah Newren51931bf2011-08-11 23:20:06 -06003165 } else {
Elijah Newrene62d1122019-04-05 08:00:25 -07003166 if (!mfi->clean) {
Elijah Newren8daec1d2019-04-05 08:00:22 -07003167 if (update_stages(opt, path, o, a, b))
Johannes Schindelin75456f92016-07-26 18:06:21 +02003168 return -1;
3169 } else {
Elijah Newren259ccb62019-04-05 08:00:13 -07003170 int file_from_stage2 = was_tracked(opt, path);
Elijah Newren51931bf2011-08-11 23:20:06 -06003171
Elijah Newren259ccb62019-04-05 08:00:13 -07003172 if (update_stages(opt, path, NULL,
Elijah Newrene62d1122019-04-05 08:00:25 -07003173 file_from_stage2 ? &mfi->blob : NULL,
3174 file_from_stage2 ? NULL : &mfi->blob))
Johannes Schindelin75456f92016-07-26 18:06:21 +02003175 return -1;
Elijah Newren51931bf2011-08-11 23:20:06 -06003176 }
3177
3178 }
Elijah Newrenc336ab82019-04-05 08:00:20 -07003179 new_path = unique_path(opt, path, ci->ren1->branch);
Elijah Newrenbd423802018-04-19 10:58:17 -07003180 if (is_dirty) {
Elijah Newren259ccb62019-04-05 08:00:13 -07003181 output(opt, 1, _("Refusing to lose dirty file at %s"),
Elijah Newrenbd423802018-04-19 10:58:17 -07003182 path);
3183 }
Elijah Newren259ccb62019-04-05 08:00:13 -07003184 output(opt, 1, _("Adding as %s instead"), new_path);
Elijah Newrene62d1122019-04-05 08:00:25 -07003185 if (update_file(opt, 0, &mfi->blob, new_path)) {
Johannes Schindelin75456f92016-07-26 18:06:21 +02003186 free(new_path);
3187 return -1;
3188 }
Elijah Newren3d6b8e82011-08-11 23:19:53 -06003189 free(new_path);
Elijah Newrene62d1122019-04-05 08:00:25 -07003190 mfi->clean = 0;
3191 } else if (update_file(opt, mfi->clean, &mfi->blob, path))
Johannes Schindelin75456f92016-07-26 18:06:21 +02003192 return -1;
Elijah Newrene62d1122019-04-05 08:00:25 -07003193 return !is_dirty && mfi->clean;
Elijah Newren0c4918d2010-09-20 02:28:52 -06003194}
3195
Elijah Newren259ccb62019-04-05 08:00:13 -07003196static int handle_rename_normal(struct merge_options *opt,
Elijah Newren8ebe7b02018-06-09 21:16:15 -07003197 const char *path,
Elijah Newren8daec1d2019-04-05 08:00:22 -07003198 const struct diff_filespec *o,
3199 const struct diff_filespec *a,
3200 const struct diff_filespec *b,
Elijah Newren8ebe7b02018-06-09 21:16:15 -07003201 struct rename_conflict_info *ci)
Elijah Newren64b1abe2018-04-19 10:58:12 -07003202{
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07003203 struct rename *ren = ci->ren1;
Elijah Newrene62d1122019-04-05 08:00:25 -07003204 struct merge_file_info mfi;
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07003205 int clean;
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07003206
Elijah Newren64b1abe2018-04-19 10:58:12 -07003207 /* Merge the content and write it out */
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07003208 clean = handle_content_merge(&mfi, opt, path, was_dirty(opt, path),
3209 o, a, b, ci);
3210
Derrick Stolee8e012512019-08-17 11:41:25 -07003211 if (clean &&
3212 opt->detect_directory_renames == MERGE_DIRECTORY_RENAMES_CONFLICT &&
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07003213 ren->dir_rename_original_dest) {
3214 if (update_stages(opt, path,
Elijah Newren3585d0e2021-06-30 17:30:00 +00003215 &mfi.blob, &mfi.blob, &mfi.blob))
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07003216 return -1;
3217 clean = 0; /* not clean, but conflicted */
3218 }
3219 return clean;
3220}
3221
3222static void dir_rename_warning(const char *msg,
3223 int is_add,
3224 int clean,
3225 struct merge_options *opt,
3226 struct rename *ren)
3227{
3228 const char *other_branch;
3229 other_branch = (ren->branch == opt->branch1 ?
3230 opt->branch2 : opt->branch1);
3231 if (is_add) {
3232 output(opt, clean ? 2 : 1, msg,
3233 ren->pair->one->path, ren->branch,
3234 other_branch, ren->pair->two->path);
3235 return;
3236 }
3237 output(opt, clean ? 2 : 1, msg,
3238 ren->pair->one->path, ren->dir_rename_original_dest, ren->branch,
3239 other_branch, ren->pair->two->path);
3240}
3241static int warn_about_dir_renamed_entries(struct merge_options *opt,
3242 struct rename *ren)
3243{
3244 const char *msg;
3245 int clean = 1, is_add;
3246
3247 if (!ren)
3248 return clean;
3249
3250 /* Return early if ren was not affected/created by a directory rename */
3251 if (!ren->dir_rename_original_dest)
3252 return clean;
3253
3254 /* Sanity checks */
Derrick Stolee8e012512019-08-17 11:41:25 -07003255 assert(opt->detect_directory_renames > MERGE_DIRECTORY_RENAMES_NONE);
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07003256 assert(ren->dir_rename_original_type == 'A' ||
3257 ren->dir_rename_original_type == 'R');
3258
3259 /* Check whether to treat directory renames as a conflict */
Derrick Stolee8e012512019-08-17 11:41:25 -07003260 clean = (opt->detect_directory_renames == MERGE_DIRECTORY_RENAMES_TRUE);
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07003261
3262 is_add = (ren->dir_rename_original_type == 'A');
3263 if (ren->dir_rename_original_type == 'A' && clean) {
3264 msg = _("Path updated: %s added in %s inside a "
3265 "directory that was renamed in %s; moving it to %s.");
3266 } else if (ren->dir_rename_original_type == 'A' && !clean) {
3267 msg = _("CONFLICT (file location): %s added in %s "
3268 "inside a directory that was renamed in %s, "
3269 "suggesting it should perhaps be moved to %s.");
3270 } else if (ren->dir_rename_original_type == 'R' && clean) {
3271 msg = _("Path updated: %s renamed to %s in %s, inside a "
3272 "directory that was renamed in %s; moving it to %s.");
3273 } else if (ren->dir_rename_original_type == 'R' && !clean) {
3274 msg = _("CONFLICT (file location): %s renamed to %s in %s, "
3275 "inside a directory that was renamed in %s, "
3276 "suggesting it should perhaps be moved to %s.");
3277 } else {
3278 BUG("Impossible dir_rename_original_type/clean combination");
3279 }
3280 dir_rename_warning(msg, is_add, clean, opt, ren);
3281
3282 return clean;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003283}
3284
3285/* Per entry merge function */
Elijah Newren259ccb62019-04-05 08:00:13 -07003286static int process_entry(struct merge_options *opt,
Miklos Vajna8a2fce12008-08-25 16:25:57 +02003287 const char *path, struct stage_data *entry)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003288{
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003289 int clean_merge = 1;
Elijah Newren259ccb62019-04-05 08:00:13 -07003290 int normalize = opt->renormalize;
Elijah Newren8daec1d2019-04-05 08:00:22 -07003291
3292 struct diff_filespec *o = &entry->stages[1];
3293 struct diff_filespec *a = &entry->stages[2];
3294 struct diff_filespec *b = &entry->stages[3];
3295 int o_valid = is_valid(o);
3296 int a_valid = is_valid(a);
3297 int b_valid = is_valid(b);
3298 o->path = a->path = b->path = (char*)path;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003299
Elijah Newren37348932010-07-09 07:10:53 -06003300 entry->processed = 1;
Elijah Newren4f66dad2011-08-11 23:20:08 -06003301 if (entry->rename_conflict_info) {
Elijah Newren043622b2019-04-05 08:00:16 -07003302 struct rename_conflict_info *ci = entry->rename_conflict_info;
Elijah Newren8daec1d2019-04-05 08:00:22 -07003303 struct diff_filespec *temp;
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07003304 int path_clean;
3305
3306 path_clean = warn_about_dir_renamed_entries(opt, ci->ren1);
3307 path_clean &= warn_about_dir_renamed_entries(opt, ci->ren2);
Elijah Newren8daec1d2019-04-05 08:00:22 -07003308
3309 /*
3310 * For cases with a single rename, {o,a,b}->path have all been
3311 * set to the rename target path; we need to set two of these
3312 * back to the rename source.
3313 * For rename/rename conflicts, we'll manually fix paths below.
3314 */
3315 temp = (opt->branch1 == ci->ren1->branch) ? b : a;
3316 o->path = temp->path = ci->ren1->pair->one->path;
3317 if (ci->ren2) {
3318 assert(opt->branch1 == ci->ren1->branch);
3319 }
3320
Elijah Newren043622b2019-04-05 08:00:16 -07003321 switch (ci->rename_type) {
Elijah Newren882fd112010-09-20 02:29:03 -06003322 case RENAME_NORMAL:
Elijah Newren4f66dad2011-08-11 23:20:08 -06003323 case RENAME_ONE_FILE_TO_ONE:
Elijah Newren8daec1d2019-04-05 08:00:22 -07003324 clean_merge = handle_rename_normal(opt, path, o, a, b,
Elijah Newren043622b2019-04-05 08:00:16 -07003325 ci);
Elijah Newren882fd112010-09-20 02:29:03 -06003326 break;
Elijah Newren5455c332018-06-09 21:16:14 -07003327 case RENAME_VIA_DIR:
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07003328 clean_merge = handle_rename_via_dir(opt, ci);
Elijah Newren9c0bbb52010-09-20 02:28:56 -06003329 break;
Elijah Newren7f867162018-11-07 20:40:26 -08003330 case RENAME_ADD:
3331 /*
3332 * Probably unclean merge, but if the renamed file
3333 * merges cleanly and the result can then be
3334 * two-way merged cleanly with the added file, I
3335 * guess it's a clean merge?
3336 */
Elijah Newren043622b2019-04-05 08:00:16 -07003337 clean_merge = handle_rename_add(opt, ci);
Elijah Newren7f867162018-11-07 20:40:26 -08003338 break;
Elijah Newren3b130adf2010-09-20 02:29:02 -06003339 case RENAME_DELETE:
3340 clean_merge = 0;
Elijah Newrene2d563d2019-04-05 08:00:21 -07003341 if (handle_rename_delete(opt, ci))
Johannes Schindelin75456f92016-07-26 18:06:21 +02003342 clean_merge = -1;
Elijah Newren3b130adf2010-09-20 02:29:02 -06003343 break;
Elijah Newren07413c52010-09-20 02:29:00 -06003344 case RENAME_ONE_FILE_TO_TWO:
Elijah Newren8daec1d2019-04-05 08:00:22 -07003345 /*
3346 * Manually fix up paths; note:
3347 * ren[12]->pair->one->path are equal.
3348 */
3349 o->path = ci->ren1->pair->one->path;
3350 a->path = ci->ren1->pair->two->path;
3351 b->path = ci->ren2->pair->two->path;
3352
Elijah Newren07413c52010-09-20 02:29:00 -06003353 clean_merge = 0;
Elijah Newren043622b2019-04-05 08:00:16 -07003354 if (handle_rename_rename_1to2(opt, ci))
Johannes Schindelin75456f92016-07-26 18:06:21 +02003355 clean_merge = -1;
Elijah Newren07413c52010-09-20 02:29:00 -06003356 break;
Elijah Newren461f5042011-08-11 23:20:15 -06003357 case RENAME_TWO_FILES_TO_ONE:
Elijah Newrenbbafc9c2018-11-07 20:40:27 -08003358 /*
Elijah Newren8daec1d2019-04-05 08:00:22 -07003359 * Manually fix up paths; note,
3360 * ren[12]->pair->two->path are actually equal.
3361 */
3362 o->path = NULL;
3363 a->path = ci->ren1->pair->two->path;
3364 b->path = ci->ren2->pair->two->path;
3365
3366 /*
Elijah Newrenbbafc9c2018-11-07 20:40:27 -08003367 * Probably unclean merge, but if the two renamed
3368 * files merge cleanly and the two resulting files
3369 * can then be two-way merged cleanly, I guess it's
3370 * a clean merge?
3371 */
Elijah Newren043622b2019-04-05 08:00:16 -07003372 clean_merge = handle_rename_rename_2to1(opt, ci);
Elijah Newren461f5042011-08-11 23:20:15 -06003373 break;
Elijah Newren07413c52010-09-20 02:29:00 -06003374 default:
3375 entry->processed = 0;
3376 break;
3377 }
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07003378 if (path_clean < clean_merge)
3379 clean_merge = path_clean;
Elijah Newren8daec1d2019-04-05 08:00:22 -07003380 } else if (o_valid && (!a_valid || !b_valid)) {
Elijah Newren37348932010-07-09 07:10:53 -06003381 /* Case A: Deleted in one */
Elijah Newren8daec1d2019-04-05 08:00:22 -07003382 if ((!a_valid && !b_valid) ||
3383 (!b_valid && blob_unchanged(opt, o, a, normalize, path)) ||
3384 (!a_valid && blob_unchanged(opt, o, b, normalize, path))) {
Elijah Newren37348932010-07-09 07:10:53 -06003385 /* Deleted in both or deleted in one and
3386 * unchanged in the other */
Elijah Newren8daec1d2019-04-05 08:00:22 -07003387 if (a_valid)
Elijah Newren259ccb62019-04-05 08:00:13 -07003388 output(opt, 2, _("Removing %s"), path);
Elijah Newren37348932010-07-09 07:10:53 -06003389 /* do not touch working file if it did not exist */
Elijah Newren8daec1d2019-04-05 08:00:22 -07003390 remove_file(opt, 1, path, !a_valid);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003391 } else {
Elijah Newrenedd2faf2011-08-11 23:20:07 -06003392 /* Modify/delete; deleted side may have put a directory in the way */
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003393 clean_merge = 0;
Elijah Newren8daec1d2019-04-05 08:00:22 -07003394 if (handle_modify_delete(opt, path, o, a, b))
Johannes Schindelin75456f92016-07-26 18:06:21 +02003395 clean_merge = -1;
Miklos Vajna8a2fce12008-08-25 16:25:57 +02003396 }
Elijah Newren8daec1d2019-04-05 08:00:22 -07003397 } else if ((!o_valid && a_valid && !b_valid) ||
3398 (!o_valid && !a_valid && b_valid)) {
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003399 /* Case B: Added in one. */
Elijah Newrenedd2faf2011-08-11 23:20:07 -06003400 /* [nothing|directory] -> ([nothing|directory], file) */
Miklos Vajna8a2fce12008-08-25 16:25:57 +02003401
Elijah Newren9c0bbb52010-09-20 02:28:56 -06003402 const char *add_branch;
3403 const char *other_branch;
Elijah Newren9c0bbb52010-09-20 02:28:56 -06003404 const char *conf;
Elijah Newren8daec1d2019-04-05 08:00:22 -07003405 const struct diff_filespec *contents;
Elijah Newren37348932010-07-09 07:10:53 -06003406
Elijah Newren8daec1d2019-04-05 08:00:22 -07003407 if (a_valid) {
Elijah Newren259ccb62019-04-05 08:00:13 -07003408 add_branch = opt->branch1;
3409 other_branch = opt->branch2;
Elijah Newren8daec1d2019-04-05 08:00:22 -07003410 contents = a;
Jiang Xin55653a62012-07-25 22:53:13 +08003411 conf = _("file/directory");
Elijah Newren9c0bbb52010-09-20 02:28:56 -06003412 } else {
Elijah Newren259ccb62019-04-05 08:00:13 -07003413 add_branch = opt->branch2;
3414 other_branch = opt->branch1;
Elijah Newren8daec1d2019-04-05 08:00:22 -07003415 contents = b;
Jiang Xin55653a62012-07-25 22:53:13 +08003416 conf = _("directory/file");
Elijah Newren9c0bbb52010-09-20 02:28:56 -06003417 }
Elijah Newren259ccb62019-04-05 08:00:13 -07003418 if (dir_in_way(opt->repo->index, path,
Elijah Newren5bf7e572019-08-17 11:41:41 -07003419 !opt->priv->call_depth && !S_ISGITLINK(a->mode),
Elijah Newrenc641ca62017-11-14 09:31:24 -08003420 0)) {
Elijah Newren259ccb62019-04-05 08:00:13 -07003421 char *new_path = unique_path(opt, path, add_branch);
Elijah Newren9c0bbb52010-09-20 02:28:56 -06003422 clean_merge = 0;
Elijah Newren259ccb62019-04-05 08:00:13 -07003423 output(opt, 1, _("CONFLICT (%s): There is a directory with name %s in %s. "
Jiang Xin55653a62012-07-25 22:53:13 +08003424 "Adding %s as %s"),
Elijah Newren9c0bbb52010-09-20 02:28:56 -06003425 conf, path, other_branch, path, new_path);
Elijah Newren8daec1d2019-04-05 08:00:22 -07003426 if (update_file(opt, 0, contents, new_path))
Johannes Schindelin75456f92016-07-26 18:06:21 +02003427 clean_merge = -1;
Elijah Newren5bf7e572019-08-17 11:41:41 -07003428 else if (opt->priv->call_depth)
Elijah Newren259ccb62019-04-05 08:00:13 -07003429 remove_file_from_index(opt->repo->index, path);
Elijah Newren3d6b8e82011-08-11 23:19:53 -06003430 free(new_path);
Elijah Newren9c0bbb52010-09-20 02:28:56 -06003431 } else {
Elijah Newren259ccb62019-04-05 08:00:13 -07003432 output(opt, 2, _("Adding %s"), path);
Elijah Newren35a74ab2011-08-11 23:20:27 -06003433 /* do not overwrite file if already present */
Elijah Newren8daec1d2019-04-05 08:00:22 -07003434 if (update_file_flags(opt, contents, path, 1, !a_valid))
Johannes Schindelin75456f92016-07-26 18:06:21 +02003435 clean_merge = -1;
Elijah Newren9c0bbb52010-09-20 02:28:56 -06003436 }
Elijah Newren8daec1d2019-04-05 08:00:22 -07003437 } else if (a_valid && b_valid) {
3438 if (!o_valid) {
Elijah Newrendcf28152018-11-07 20:40:28 -08003439 /* Case C: Added in both (check for same permissions) */
Elijah Newren259ccb62019-04-05 08:00:13 -07003440 output(opt, 1,
Elijah Newrendcf28152018-11-07 20:40:28 -08003441 _("CONFLICT (add/add): Merge conflict in %s"),
3442 path);
Elijah Newren259ccb62019-04-05 08:00:13 -07003443 clean_merge = handle_file_collision(opt,
Elijah Newrendcf28152018-11-07 20:40:28 -08003444 path, NULL, NULL,
Elijah Newren259ccb62019-04-05 08:00:13 -07003445 opt->branch1,
3446 opt->branch2,
Elijah Newren8daec1d2019-04-05 08:00:22 -07003447 a, b);
Elijah Newrendcf28152018-11-07 20:40:28 -08003448 } else {
3449 /* case D: Modified in both, but differently. */
Elijah Newrene62d1122019-04-05 08:00:25 -07003450 struct merge_file_info mfi;
Elijah Newrendcf28152018-11-07 20:40:28 -08003451 int is_dirty = 0; /* unpack_trees would have bailed if dirty */
Elijah Newrene62d1122019-04-05 08:00:25 -07003452 clean_merge = handle_content_merge(&mfi, opt, path,
Elijah Newrendcf28152018-11-07 20:40:28 -08003453 is_dirty,
Elijah Newren8daec1d2019-04-05 08:00:22 -07003454 o, a, b, NULL);
Elijah Newrendcf28152018-11-07 20:40:28 -08003455 }
Elijah Newren8daec1d2019-04-05 08:00:22 -07003456 } else if (!o_valid && !a_valid && !b_valid) {
Elijah Newrenedd2faf2011-08-11 23:20:07 -06003457 /*
3458 * this entry was deleted altogether. a_mode == 0 means
3459 * we had that path and want to actively remove it.
3460 */
Elijah Newren8daec1d2019-04-05 08:00:22 -07003461 remove_file(opt, 1, path, !a->mode);
Elijah Newrenedd2faf2011-08-11 23:20:07 -06003462 } else
Johannes Schindelin033abf92018-05-02 11:38:39 +02003463 BUG("fatal merge failure, shouldn't happen.");
Elijah Newren37348932010-07-09 07:10:53 -06003464
3465 return clean_merge;
3466}
3467
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003468static int merge_trees_internal(struct merge_options *opt,
3469 struct tree *head,
3470 struct tree *merge,
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003471 struct tree *merge_base,
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003472 struct tree **result)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003473{
Elijah Newren259ccb62019-04-05 08:00:13 -07003474 struct index_state *istate = opt->repo->index;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003475 int code, clean;
3476
Elijah Newren259ccb62019-04-05 08:00:13 -07003477 if (opt->subtree_shift) {
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003478 merge = shift_tree_object(opt->repo, head, merge,
3479 opt->subtree_shift);
3480 merge_base = shift_tree_object(opt->repo, head, merge_base,
3481 opt->subtree_shift);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003482 }
3483
Elijah Newren763a59e2020-01-01 05:20:57 +00003484 if (oideq(&merge_base->object.oid, &merge->object.oid)) {
Eric Sunshine80cde952021-05-02 01:14:22 -04003485 output(opt, 0, _("Already up to date."));
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003486 *result = head;
3487 return 1;
3488 }
3489
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003490 code = unpack_trees_start(opt, merge_base, head, merge);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003491
Junio C Hamanofadd0692009-09-07 22:43:11 -07003492 if (code != 0) {
Elijah Newren5bf7e572019-08-17 11:41:41 -07003493 if (show(opt, 4) || opt->priv->call_depth)
Elijah Newren259ccb62019-04-05 08:00:13 -07003494 err(opt, _("merging of trees %s and %s failed"),
brian m. carlsonf2fd0762015-11-10 02:22:28 +00003495 oid_to_hex(&head->object.oid),
3496 oid_to_hex(&merge->object.oid));
Elijah Newren259ccb62019-04-05 08:00:13 -07003497 unpack_trees_finish(opt);
Johannes Schindelin60033032016-07-26 18:06:26 +02003498 return -1;
Junio C Hamanofadd0692009-09-07 22:43:11 -07003499 }
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003500
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +07003501 if (unmerged_index(istate)) {
Elijah Newrenf1725892018-04-19 10:58:00 -07003502 struct string_list *entries;
3503 struct rename_info re_info;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003504 int i;
Kevin Willfordfc65b002017-09-07 10:25:56 -06003505 /*
3506 * Only need the hashmap while processing entries, so
3507 * initialize it here and free it when we are done running
3508 * through the entries. Keeping it in the merge_options as
3509 * opposed to decaring a local hashmap is for convenience
3510 * so that we don't have to pass it to around.
3511 */
Elijah Newren5bf7e572019-08-17 11:41:41 -07003512 hashmap_init(&opt->priv->current_file_dir_set, path_hashmap_cmp,
Elijah Newren4d7101e2019-08-17 11:41:33 -07003513 NULL, 512);
Elijah Newren259ccb62019-04-05 08:00:13 -07003514 get_files_dirs(opt, head);
3515 get_files_dirs(opt, merge);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003516
Elijah Newren259ccb62019-04-05 08:00:13 -07003517 entries = get_unmerged(opt->repo->index);
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003518 clean = detect_and_process_renames(opt, merge_base, head, merge,
Elijah Newren8ebe7b02018-06-09 21:16:15 -07003519 entries, &re_info);
Elijah Newren259ccb62019-04-05 08:00:13 -07003520 record_df_conflict_files(opt, entries);
Johannes Schindelin75456f92016-07-26 18:06:21 +02003521 if (clean < 0)
Kevin Willforde336bdc2017-08-28 14:28:27 -06003522 goto cleanup;
Elijah Newrenedd2faf2011-08-11 23:20:07 -06003523 for (i = entries->nr-1; 0 <= i; i--) {
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003524 const char *path = entries->items[i].string;
3525 struct stage_data *e = entries->items[i].util;
Johannes Schindelin75456f92016-07-26 18:06:21 +02003526 if (!e->processed) {
Elijah Newren259ccb62019-04-05 08:00:13 -07003527 int ret = process_entry(opt, path, e);
Johannes Schindelin75456f92016-07-26 18:06:21 +02003528 if (!ret)
3529 clean = 0;
Kevin Willforde336bdc2017-08-28 14:28:27 -06003530 else if (ret < 0) {
3531 clean = ret;
3532 goto cleanup;
3533 }
Johannes Schindelin75456f92016-07-26 18:06:21 +02003534 }
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003535 }
Elijah Newren37348932010-07-09 07:10:53 -06003536 for (i = 0; i < entries->nr; i++) {
Junio C Hamano7edba4c2010-09-20 02:28:35 -06003537 struct stage_data *e = entries->items[i].util;
3538 if (!e->processed)
Johannes Schindelin033abf92018-05-02 11:38:39 +02003539 BUG("unprocessed path??? %s",
Junio C Hamano7edba4c2010-09-20 02:28:35 -06003540 entries->items[i].string);
3541 }
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003542
Elijah Newren93665362018-06-09 21:16:13 -07003543 cleanup:
Elijah Newrenffc16c42018-04-19 10:58:04 -07003544 final_cleanup_renames(&re_info);
Elijah Newrenf1725892018-04-19 10:58:00 -07003545
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003546 string_list_clear(entries, 1);
Elijah Newrenf1725892018-04-19 10:58:00 -07003547 free(entries);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003548
Elijah Newren6da1a252020-11-02 18:55:05 +00003549 hashmap_clear_and_free(&opt->priv->current_file_dir_set,
Eric Wongc8e424c2019-10-06 23:30:40 +00003550 struct path_hashmap_entry, e);
Kevin Willfordfc65b002017-09-07 10:25:56 -06003551
Elijah Newren3f1c1c32018-05-20 12:17:35 +02003552 if (clean < 0) {
Elijah Newren259ccb62019-04-05 08:00:13 -07003553 unpack_trees_finish(opt);
Kevin Willforde336bdc2017-08-28 14:28:27 -06003554 return clean;
Elijah Newren3f1c1c32018-05-20 12:17:35 +02003555 }
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003556 }
3557 else
3558 clean = 1;
3559
Elijah Newren259ccb62019-04-05 08:00:13 -07003560 unpack_trees_finish(opt);
Elijah Newrena35edc82018-04-19 10:58:20 -07003561
Elijah Newren5bf7e572019-08-17 11:41:41 -07003562 if (opt->priv->call_depth &&
Elijah Newren724dd762019-08-17 11:41:32 -07003563 !(*result = write_in_core_index_as_tree(opt->repo)))
Johannes Schindelinfbc87eb2016-07-26 18:06:17 +02003564 return -1;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003565
3566 return clean;
3567}
3568
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003569/*
Elijah Newren56e74342020-08-02 03:14:27 +00003570 * Merge the commits h1 and h2, returning a flag (int) indicating the
3571 * cleanness of the merge. Also, if opt->priv->call_depth, create a
3572 * virtual commit and write its location to *result.
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003573 */
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003574static int merge_recursive_internal(struct merge_options *opt,
3575 struct commit *h1,
3576 struct commit *h2,
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003577 struct commit_list *merge_bases,
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003578 struct commit **result)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003579{
3580 struct commit_list *iter;
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003581 struct commit *merged_merge_bases;
Elijah Newrenbab56872019-08-17 11:41:35 -07003582 struct tree *result_tree;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003583 int clean;
Elijah Newren743474c2019-08-17 11:41:24 -07003584 const char *ancestor_name;
3585 struct strbuf merge_base_abbrev = STRBUF_INIT;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003586
Elijah Newren259ccb62019-04-05 08:00:13 -07003587 if (show(opt, 4)) {
3588 output(opt, 4, _("Merging:"));
3589 output_commit_title(opt, h1);
3590 output_commit_title(opt, h2);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003591 }
3592
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003593 if (!merge_bases) {
3594 merge_bases = get_merge_bases(h1, h2);
3595 merge_bases = reverse_commit_list(merge_bases);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003596 }
3597
Elijah Newren259ccb62019-04-05 08:00:13 -07003598 if (show(opt, 5)) {
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003599 unsigned cnt = commit_list_count(merge_bases);
Ralf Thielowe0453cd2012-08-05 19:56:38 +02003600
Elijah Newren259ccb62019-04-05 08:00:13 -07003601 output(opt, 5, Q_("found %u common ancestor:",
Ralf Thielowe0453cd2012-08-05 19:56:38 +02003602 "found %u common ancestors:", cnt), cnt);
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003603 for (iter = merge_bases; iter; iter = iter->next)
Elijah Newren259ccb62019-04-05 08:00:13 -07003604 output_commit_title(opt, iter->item);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003605 }
3606
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003607 merged_merge_bases = pop_commit(&merge_bases);
3608 if (merged_merge_bases == NULL) {
Jonathan Nieder03f622c2011-08-16 13:27:39 -05003609 /* if there is no common ancestor, use an empty tree */
3610 struct tree *tree;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003611
Elijah Newren259ccb62019-04-05 08:00:13 -07003612 tree = lookup_tree(opt->repo, opt->repo->hash_algo->empty_tree);
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003613 merged_merge_bases = make_virtual_commit(opt->repo, tree,
3614 "ancestor");
Elijah Newren743474c2019-08-17 11:41:24 -07003615 ancestor_name = "empty tree";
Elijah Newrenb6570472019-10-07 08:52:11 -07003616 } else if (opt->ancestor && !opt->priv->call_depth) {
Elijah Newren8e4ec332019-10-01 11:17:27 -07003617 ancestor_name = opt->ancestor;
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003618 } else if (merge_bases) {
Elijah Newren743474c2019-08-17 11:41:24 -07003619 ancestor_name = "merged common ancestors";
3620 } else {
3621 strbuf_add_unique_abbrev(&merge_base_abbrev,
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003622 &merged_merge_bases->object.oid,
Elijah Newren743474c2019-08-17 11:41:24 -07003623 DEFAULT_ABBREV);
3624 ancestor_name = merge_base_abbrev.buf;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003625 }
3626
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003627 for (iter = merge_bases; iter; iter = iter->next) {
Miklos Vajna8a2fce12008-08-25 16:25:57 +02003628 const char *saved_b1, *saved_b2;
Elijah Newren5bf7e572019-08-17 11:41:41 -07003629 opt->priv->call_depth++;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003630 /*
3631 * When the merge fails, the result contains files
3632 * with conflict markers. The cleanness flag is
Johannes Schindelinde8946d2016-07-26 18:06:07 +02003633 * ignored (unless indicating an error), it was never
3634 * actually used, as result of merge_trees has always
3635 * overwritten it: the committed "conflicts" were
3636 * already resolved.
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003637 */
Elijah Newren259ccb62019-04-05 08:00:13 -07003638 discard_index(opt->repo->index);
3639 saved_b1 = opt->branch1;
3640 saved_b2 = opt->branch2;
3641 opt->branch1 = "Temporary merge branch 1";
3642 opt->branch2 = "Temporary merge branch 2";
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003643 if (merge_recursive_internal(opt, merged_merge_bases, iter->item,
3644 NULL, &merged_merge_bases) < 0)
Johannes Schindelinde8946d2016-07-26 18:06:07 +02003645 return -1;
Elijah Newren259ccb62019-04-05 08:00:13 -07003646 opt->branch1 = saved_b1;
3647 opt->branch2 = saved_b2;
Elijah Newren5bf7e572019-08-17 11:41:41 -07003648 opt->priv->call_depth--;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003649
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003650 if (!merged_merge_bases)
Elijah Newren259ccb62019-04-05 08:00:13 -07003651 return err(opt, _("merge returned no commit"));
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003652 }
3653
Elijah Newren816147e2021-03-20 00:03:53 +00003654 /*
3655 * FIXME: Since merge_recursive_internal() is only ever called by
3656 * places that ensure the index is loaded first
3657 * (e.g. builtin/merge.c, rebase/sequencer, etc.), in the common
3658 * case where the merge base was unique that means when we get here
3659 * we immediately discard the index and re-read it, which is a
3660 * complete waste of time. We should only be discarding and
3661 * re-reading if we were forced to recurse.
3662 */
Elijah Newren259ccb62019-04-05 08:00:13 -07003663 discard_index(opt->repo->index);
Elijah Newren5bf7e572019-08-17 11:41:41 -07003664 if (!opt->priv->call_depth)
Elijah Newren259ccb62019-04-05 08:00:13 -07003665 repo_read_index(opt->repo);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003666
Elijah Newren743474c2019-08-17 11:41:24 -07003667 opt->ancestor = ancestor_name;
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003668 clean = merge_trees_internal(opt,
3669 repo_get_commit_tree(opt->repo, h1),
3670 repo_get_commit_tree(opt->repo, h2),
3671 repo_get_commit_tree(opt->repo,
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003672 merged_merge_bases),
Elijah Newrenbab56872019-08-17 11:41:35 -07003673 &result_tree);
Elijah Newren743474c2019-08-17 11:41:24 -07003674 strbuf_release(&merge_base_abbrev);
Elijah Newrenb6570472019-10-07 08:52:11 -07003675 opt->ancestor = NULL; /* avoid accidental re-use of opt->ancestor */
Johannes Schindelin6999bc72016-08-01 13:44:57 +02003676 if (clean < 0) {
Elijah Newren259ccb62019-04-05 08:00:13 -07003677 flush_output(opt);
Johannes Schindelinde8946d2016-07-26 18:06:07 +02003678 return clean;
Johannes Schindelin6999bc72016-08-01 13:44:57 +02003679 }
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003680
Elijah Newren5bf7e572019-08-17 11:41:41 -07003681 if (opt->priv->call_depth) {
Elijah Newrenbab56872019-08-17 11:41:35 -07003682 *result = make_virtual_commit(opt->repo, result_tree,
3683 "merged tree");
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003684 commit_list_insert(h1, &(*result)->parents);
3685 commit_list_insert(h2, &(*result)->parents->next);
3686 }
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003687 return clean;
3688}
3689
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003690static int merge_start(struct merge_options *opt, struct tree *head)
3691{
3692 struct strbuf sb = STRBUF_INIT;
3693
Elijah Newren45ef16f2019-08-17 11:41:43 -07003694 /* Sanity checks on opt */
3695 assert(opt->repo);
3696
3697 assert(opt->branch1 && opt->branch2);
3698
3699 assert(opt->detect_renames >= -1 &&
3700 opt->detect_renames <= DIFF_DETECT_COPY);
3701 assert(opt->detect_directory_renames >= MERGE_DIRECTORY_RENAMES_NONE &&
3702 opt->detect_directory_renames <= MERGE_DIRECTORY_RENAMES_TRUE);
3703 assert(opt->rename_limit >= -1);
3704 assert(opt->rename_score >= 0 && opt->rename_score <= MAX_SCORE);
3705 assert(opt->show_rename_progress >= 0 && opt->show_rename_progress <= 1);
3706
3707 assert(opt->xdl_opts >= 0);
3708 assert(opt->recursive_variant >= MERGE_VARIANT_NORMAL &&
3709 opt->recursive_variant <= MERGE_VARIANT_THEIRS);
3710
3711 assert(opt->verbosity >= 0 && opt->verbosity <= 5);
3712 assert(opt->buffer_output <= 2);
3713 assert(opt->obuf.len == 0);
3714
3715 assert(opt->priv == NULL);
3716
Elijah Newren6054d1a2022-02-02 02:37:33 +00003717 /* Not supported; option specific to merge-ort */
3718 assert(!opt->record_conflict_msgs_as_headers);
3719 assert(!opt->msg_header_prefix);
3720
Elijah Newren45ef16f2019-08-17 11:41:43 -07003721 /* Sanity check on repo state; index must match head */
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003722 if (repo_index_has_changes(opt->repo, head, &sb)) {
3723 err(opt, _("Your local changes to the following files would be overwritten by merge:\n %s"),
3724 sb.buf);
3725 strbuf_release(&sb);
3726 return -1;
3727 }
3728
René Scharfeca56dad2021-03-13 17:17:22 +01003729 CALLOC_ARRAY(opt->priv, 1);
Ævar Arnfjörð Bjarmasonbc40dfb2021-07-01 12:51:29 +02003730 string_list_init_dup(&opt->priv->df_conflict_file_set);
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003731 return 0;
3732}
3733
3734static void merge_finalize(struct merge_options *opt)
3735{
Elijah Newrene95e4812019-08-17 11:41:40 -07003736 flush_output(opt);
Elijah Newren5bf7e572019-08-17 11:41:41 -07003737 if (!opt->priv->call_depth && opt->buffer_output < 2)
Elijah Newrene95e4812019-08-17 11:41:40 -07003738 strbuf_release(&opt->obuf);
Elijah Newren345480d2019-08-17 11:41:31 -07003739 if (show(opt, 2))
3740 diff_warn_rename_limit("merge.renamelimit",
Elijah Newren5bf7e572019-08-17 11:41:41 -07003741 opt->priv->needed_rename_limit, 0);
3742 FREE_AND_NULL(opt->priv);
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003743}
3744
3745int merge_trees(struct merge_options *opt,
3746 struct tree *head,
3747 struct tree *merge,
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003748 struct tree *merge_base)
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003749{
3750 int clean;
Elijah Newrenb4db8a22019-08-17 11:41:30 -07003751 struct tree *ignored;
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003752
3753 assert(opt->ancestor != NULL);
3754
3755 if (merge_start(opt, head))
3756 return -1;
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003757 clean = merge_trees_internal(opt, head, merge, merge_base, &ignored);
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003758 merge_finalize(opt);
3759
3760 return clean;
3761}
3762
3763int merge_recursive(struct merge_options *opt,
3764 struct commit *h1,
3765 struct commit *h2,
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003766 struct commit_list *merge_bases,
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003767 struct commit **result)
3768{
3769 int clean;
3770
Elijah Newren8e4ec332019-10-01 11:17:27 -07003771 assert(opt->ancestor == NULL ||
3772 !strcmp(opt->ancestor, "constructed merge base"));
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003773
Derrick Stoleea3380632021-09-08 11:23:57 +00003774 prepare_repo_settings(opt->repo);
3775 opt->repo->settings.command_requires_full_index = 1;
3776
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003777 if (merge_start(opt, repo_get_commit_tree(opt->repo, h1)))
3778 return -1;
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003779 clean = merge_recursive_internal(opt, h1, h2, merge_bases, result);
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003780 merge_finalize(opt);
3781
3782 return clean;
3783}
3784
Elijah Newren4d7101e2019-08-17 11:41:33 -07003785static struct commit *get_ref(struct repository *repo,
3786 const struct object_id *oid,
Nguyễn Thái Ngọc Duyd7cf3a92019-01-12 09:13:30 +07003787 const char *name)
Stephan Beyer73118f82008-08-12 22:13:59 +02003788{
3789 struct object *object;
3790
Nguyễn Thái Ngọc Duyd7cf3a92019-01-12 09:13:30 +07003791 object = deref_tag(repo, parse_object(repo, oid),
3792 name, strlen(name));
Stephan Beyer73118f82008-08-12 22:13:59 +02003793 if (!object)
3794 return NULL;
3795 if (object->type == OBJ_TREE)
Nguyễn Thái Ngọc Duyd7cf3a92019-01-12 09:13:30 +07003796 return make_virtual_commit(repo, (struct tree*)object, name);
Stephan Beyer73118f82008-08-12 22:13:59 +02003797 if (object->type != OBJ_COMMIT)
3798 return NULL;
3799 if (parse_commit((struct commit *)object))
3800 return NULL;
3801 return (struct commit *)object;
3802}
3803
Elijah Newren259ccb62019-04-05 08:00:13 -07003804int merge_recursive_generic(struct merge_options *opt,
brian m. carlson4e8161a2016-06-24 23:09:28 +00003805 const struct object_id *head,
3806 const struct object_id *merge,
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003807 int num_merge_bases,
3808 const struct object_id **merge_bases,
Miklos Vajna8a2fce12008-08-25 16:25:57 +02003809 struct commit **result)
Stephan Beyer73118f82008-08-12 22:13:59 +02003810{
Nguyễn Thái Ngọc Duy03b86642014-06-13 19:19:23 +07003811 int clean;
Martin Ågren837e34e2017-10-05 22:32:04 +02003812 struct lock_file lock = LOCK_INIT;
Elijah Newren259ccb62019-04-05 08:00:13 -07003813 struct commit *head_commit = get_ref(opt->repo, head, opt->branch1);
3814 struct commit *next_commit = get_ref(opt->repo, merge, opt->branch2);
Stephan Beyer73118f82008-08-12 22:13:59 +02003815 struct commit_list *ca = NULL;
3816
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003817 if (merge_bases) {
Stephan Beyer73118f82008-08-12 22:13:59 +02003818 int i;
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003819 for (i = 0; i < num_merge_bases; ++i) {
Stephan Beyer73118f82008-08-12 22:13:59 +02003820 struct commit *base;
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003821 if (!(base = get_ref(opt->repo, merge_bases[i],
3822 oid_to_hex(merge_bases[i]))))
Elijah Newren259ccb62019-04-05 08:00:13 -07003823 return err(opt, _("Could not parse object '%s'"),
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003824 oid_to_hex(merge_bases[i]));
Stephan Beyer73118f82008-08-12 22:13:59 +02003825 commit_list_insert(base, &ca);
3826 }
Elijah Newren8e4ec332019-10-01 11:17:27 -07003827 if (num_merge_bases == 1)
3828 opt->ancestor = "constructed merge base";
Stephan Beyer73118f82008-08-12 22:13:59 +02003829 }
3830
Elijah Newren259ccb62019-04-05 08:00:13 -07003831 repo_hold_locked_index(opt->repo, &lock, LOCK_DIE_ON_ERROR);
3832 clean = merge_recursive(opt, head_commit, next_commit, ca,
Elijah Newrend90e7592018-06-09 21:16:12 -07003833 result);
Martin Ågren51d3f432018-02-28 20:07:56 +01003834 if (clean < 0) {
3835 rollback_lock_file(&lock);
Johannes Schindelinde8946d2016-07-26 18:06:07 +02003836 return clean;
Martin Ågren51d3f432018-02-28 20:07:56 +01003837 }
Johannes Schindelinde8946d2016-07-26 18:06:07 +02003838
Elijah Newren259ccb62019-04-05 08:00:13 -07003839 if (write_locked_index(opt->repo->index, &lock,
Martin Ågren61000812018-03-01 21:40:20 +01003840 COMMIT_LOCK | SKIP_IF_UNCHANGED))
Elijah Newren259ccb62019-04-05 08:00:13 -07003841 return err(opt, _("Unable to write index."));
Stephan Beyer73118f82008-08-12 22:13:59 +02003842
3843 return clean ? 0 : 1;
3844}
3845
Elijah Newren259ccb62019-04-05 08:00:13 -07003846static void merge_recursive_config(struct merge_options *opt)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003847{
Ben Peart85b46032018-05-02 16:01:14 +00003848 char *value = NULL;
Elijah Newren8d552252020-08-03 18:41:19 +00003849 int renormalize = 0;
Elijah Newren259ccb62019-04-05 08:00:13 -07003850 git_config_get_int("merge.verbosity", &opt->verbosity);
Elijah Newren8599ab42019-08-17 11:41:38 -07003851 git_config_get_int("diff.renamelimit", &opt->rename_limit);
3852 git_config_get_int("merge.renamelimit", &opt->rename_limit);
Elijah Newren8d552252020-08-03 18:41:19 +00003853 git_config_get_bool("merge.renormalize", &renormalize);
3854 opt->renormalize = renormalize;
Ben Peart85b46032018-05-02 16:01:14 +00003855 if (!git_config_get_string("diff.renames", &value)) {
Elijah Newren8599ab42019-08-17 11:41:38 -07003856 opt->detect_renames = git_config_rename("diff.renames", value);
Ben Peart85b46032018-05-02 16:01:14 +00003857 free(value);
3858 }
3859 if (!git_config_get_string("merge.renames", &value)) {
Elijah Newren8599ab42019-08-17 11:41:38 -07003860 opt->detect_renames = git_config_rename("merge.renames", value);
Ben Peart85b46032018-05-02 16:01:14 +00003861 free(value);
3862 }
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07003863 if (!git_config_get_string("merge.directoryrenames", &value)) {
3864 int boolval = git_parse_maybe_bool(value);
3865 if (0 <= boolval) {
Derrick Stolee8e012512019-08-17 11:41:25 -07003866 opt->detect_directory_renames = boolval ?
3867 MERGE_DIRECTORY_RENAMES_TRUE :
3868 MERGE_DIRECTORY_RENAMES_NONE;
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07003869 } else if (!strcasecmp(value, "conflict")) {
Derrick Stolee8e012512019-08-17 11:41:25 -07003870 opt->detect_directory_renames =
3871 MERGE_DIRECTORY_RENAMES_CONFLICT;
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07003872 } /* avoid erroring on values from future versions of git */
Tanay Abhra0e7bcb12014-08-13 01:22:01 -07003873 free(value);
3874 }
3875 git_config(git_xmerge_config, NULL);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003876}
3877
Elijah Newren259ccb62019-04-05 08:00:13 -07003878void init_merge_options(struct merge_options *opt,
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +07003879 struct repository *repo)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003880{
Andrey Okoshkin80486222017-10-31 12:09:13 +03003881 const char *merge_verbosity;
Elijah Newren259ccb62019-04-05 08:00:13 -07003882 memset(opt, 0, sizeof(struct merge_options));
Elijah Newrena779fb82019-08-17 11:41:39 -07003883
Elijah Newren259ccb62019-04-05 08:00:13 -07003884 opt->repo = repo;
Elijah Newrena779fb82019-08-17 11:41:39 -07003885
Elijah Newren8599ab42019-08-17 11:41:38 -07003886 opt->detect_renames = -1;
Derrick Stolee8e012512019-08-17 11:41:25 -07003887 opt->detect_directory_renames = MERGE_DIRECTORY_RENAMES_CONFLICT;
Elijah Newrena779fb82019-08-17 11:41:39 -07003888 opt->rename_limit = -1;
3889
Elijah Newren259ccb62019-04-05 08:00:13 -07003890 opt->verbosity = 2;
3891 opt->buffer_output = 1;
Elijah Newrena779fb82019-08-17 11:41:39 -07003892 strbuf_init(&opt->obuf, 0);
3893
Elijah Newren259ccb62019-04-05 08:00:13 -07003894 opt->renormalize = 0;
Elijah Newrena779fb82019-08-17 11:41:39 -07003895
Elijah Newren259ccb62019-04-05 08:00:13 -07003896 merge_recursive_config(opt);
Andrey Okoshkin80486222017-10-31 12:09:13 +03003897 merge_verbosity = getenv("GIT_MERGE_VERBOSITY");
3898 if (merge_verbosity)
Elijah Newren259ccb62019-04-05 08:00:13 -07003899 opt->verbosity = strtol(merge_verbosity, NULL, 10);
3900 if (opt->verbosity >= 5)
3901 opt->buffer_output = 0;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003902}
Jonathan Nieder635a7bb2010-08-26 00:47:58 -05003903
Elijah Newren259ccb62019-04-05 08:00:13 -07003904int parse_merge_opt(struct merge_options *opt, const char *s)
Jonathan Nieder635a7bb2010-08-26 00:47:58 -05003905{
Jeff King95b567c2014-06-18 15:48:29 -04003906 const char *arg;
3907
Jonathan Nieder635a7bb2010-08-26 00:47:58 -05003908 if (!s || !*s)
3909 return -1;
3910 if (!strcmp(s, "ours"))
Elijah Newrenf3081da2019-08-17 11:41:42 -07003911 opt->recursive_variant = MERGE_VARIANT_OURS;
Jonathan Nieder635a7bb2010-08-26 00:47:58 -05003912 else if (!strcmp(s, "theirs"))
Elijah Newrenf3081da2019-08-17 11:41:42 -07003913 opt->recursive_variant = MERGE_VARIANT_THEIRS;
Jonathan Nieder635a7bb2010-08-26 00:47:58 -05003914 else if (!strcmp(s, "subtree"))
Elijah Newren259ccb62019-04-05 08:00:13 -07003915 opt->subtree_shift = "";
Jeff King95b567c2014-06-18 15:48:29 -04003916 else if (skip_prefix(s, "subtree=", &arg))
Elijah Newren259ccb62019-04-05 08:00:13 -07003917 opt->subtree_shift = arg;
Justin Frankel58a1ece2010-08-26 00:50:45 -05003918 else if (!strcmp(s, "patience"))
Elijah Newren259ccb62019-04-05 08:00:13 -07003919 opt->xdl_opts = DIFF_WITH_ALG(opt, PATIENCE_DIFF);
Tay Ray Chuan8c912ee2011-07-12 14:10:25 +08003920 else if (!strcmp(s, "histogram"))
Elijah Newren259ccb62019-04-05 08:00:13 -07003921 opt->xdl_opts = DIFF_WITH_ALG(opt, HISTOGRAM_DIFF);
Jeff King95b567c2014-06-18 15:48:29 -04003922 else if (skip_prefix(s, "diff-algorithm=", &arg)) {
3923 long value = parse_algorithm_value(arg);
Michal Privoznik07924d42013-01-16 08:51:58 +01003924 if (value < 0)
3925 return -1;
3926 /* clear out previous settings */
Elijah Newren259ccb62019-04-05 08:00:13 -07003927 DIFF_XDL_CLR(opt, NEED_MINIMAL);
3928 opt->xdl_opts &= ~XDF_DIFF_ALGORITHM_MASK;
3929 opt->xdl_opts |= value;
Michal Privoznik07924d42013-01-16 08:51:58 +01003930 }
Justin Frankel4e5dd042010-08-26 00:51:47 -05003931 else if (!strcmp(s, "ignore-space-change"))
Elijah Newren259ccb62019-04-05 08:00:13 -07003932 DIFF_XDL_SET(opt, IGNORE_WHITESPACE_CHANGE);
Justin Frankel4e5dd042010-08-26 00:51:47 -05003933 else if (!strcmp(s, "ignore-all-space"))
Elijah Newren259ccb62019-04-05 08:00:13 -07003934 DIFF_XDL_SET(opt, IGNORE_WHITESPACE);
Justin Frankel4e5dd042010-08-26 00:51:47 -05003935 else if (!strcmp(s, "ignore-space-at-eol"))
Elijah Newren259ccb62019-04-05 08:00:13 -07003936 DIFF_XDL_SET(opt, IGNORE_WHITESPACE_AT_EOL);
Junio C Hamanoe9282f02017-10-26 15:32:27 +09003937 else if (!strcmp(s, "ignore-cr-at-eol"))
Elijah Newren259ccb62019-04-05 08:00:13 -07003938 DIFF_XDL_SET(opt, IGNORE_CR_AT_EOL);
Jonathan Nieder635a7bb2010-08-26 00:47:58 -05003939 else if (!strcmp(s, "renormalize"))
Elijah Newren259ccb62019-04-05 08:00:13 -07003940 opt->renormalize = 1;
Jonathan Nieder635a7bb2010-08-26 00:47:58 -05003941 else if (!strcmp(s, "no-renormalize"))
Elijah Newren259ccb62019-04-05 08:00:13 -07003942 opt->renormalize = 0;
Felipe Gonçalves Assisd2b11ec2016-02-17 01:15:25 -02003943 else if (!strcmp(s, "no-renames"))
Elijah Newren8599ab42019-08-17 11:41:38 -07003944 opt->detect_renames = 0;
Felipe Gonçalves Assis87892f62016-02-21 19:59:05 -03003945 else if (!strcmp(s, "find-renames")) {
Elijah Newren8599ab42019-08-17 11:41:38 -07003946 opt->detect_renames = 1;
Elijah Newren259ccb62019-04-05 08:00:13 -07003947 opt->rename_score = 0;
Felipe Gonçalves Assis87892f62016-02-21 19:59:05 -03003948 }
Felipe Gonçalves Assis1b47ad12016-02-17 01:15:26 -02003949 else if (skip_prefix(s, "find-renames=", &arg) ||
3950 skip_prefix(s, "rename-threshold=", &arg)) {
Elijah Newren259ccb62019-04-05 08:00:13 -07003951 if ((opt->rename_score = parse_rename_score(&arg)) == -1 || *arg != 0)
Kevin Ballard10ae7522010-09-27 16:58:25 -07003952 return -1;
Elijah Newren8599ab42019-08-17 11:41:38 -07003953 opt->detect_renames = 1;
Kevin Ballard10ae7522010-09-27 16:58:25 -07003954 }
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +07003955 /*
3956 * Please update $__git_merge_strategy_options in
3957 * git-completion.bash when you add new options
3958 */
Jonathan Nieder635a7bb2010-08-26 00:47:58 -05003959 else
3960 return -1;
3961 return 0;
3962}