blob: a0c3e7a2d9105dd895f0ae2613d0f87f00b8d794 [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 */
Elijah Newrenbc5c5ec2023-05-16 06:33:57 +00006#include "git-compat-util.h"
Elijah Newren4615a8c2019-08-17 11:41:44 -07007#include "merge-recursive.h"
8
Elijah Newren4615a8c2019-08-17 11:41:44 -07009#include "alloc.h"
Elijah Newren4615a8c2019-08-17 11:41:44 -070010#include "cache-tree.h"
11#include "commit.h"
12#include "commit-reach.h"
13#include "config.h"
Miklos Vajna9047ebb2008-08-12 18:45:14 +020014#include "diff.h"
15#include "diffcore.h"
Shawn O. Pearce9800c0d2008-09-29 11:04:20 -070016#include "dir.h"
Elijah Newren32a8f512023-03-21 06:26:03 +000017#include "environment.h"
Elijah Newrenf394e092023-03-21 06:25:54 +000018#include "gettext.h"
Elijah Newren41771fa2023-02-24 00:09:27 +000019#include "hex.h"
Elijah Newren67238992023-05-16 06:34:04 +000020#include "merge-ll.h"
Elijah Newren4615a8c2019-08-17 11:41:44 -070021#include "lockfile.h"
Elijah Newrend4ff2072023-04-22 20:17:15 +000022#include "match-trees.h"
Elijah Newrenf5653852023-05-16 06:33:50 +000023#include "name-hash.h"
Elijah Newren87bed172023-04-11 00:41:53 -070024#include "object-file.h"
Elijah Newrendabab1d2023-04-11 00:41:49 -070025#include "object-name.h"
Elijah Newrena034e912023-05-16 06:34:06 +000026#include "object-store-ll.h"
Elijah Newrenc3399322023-05-16 06:33:59 +000027#include "path.h"
Elijah Newren4615a8c2019-08-17 11:41:44 -070028#include "repository.h"
Stefan Beller18cfc082018-05-15 13:00:28 -070029#include "revision.h"
Elijah Newrenbaf889c2023-05-16 06:33:51 +000030#include "sparse-index.h"
Elijah Newren4615a8c2019-08-17 11:41:44 -070031#include "string-list.h"
Elijah Newrencb2a5132023-04-22 20:17:09 +000032#include "symlinks.h"
Elijah Newren4615a8c2019-08-17 11:41:44 -070033#include "tag.h"
34#include "tree-walk.h"
35#include "unpack-trees.h"
36#include "xdiff-interface.h"
Miklos Vajna9047ebb2008-08-12 18:45:14 +020037
Elijah Newren5bf7e572019-08-17 11:41:41 -070038struct merge_options_internal {
39 int call_depth;
40 int needed_rename_limit;
41 struct hashmap current_file_dir_set;
42 struct string_list df_conflict_file_set;
43 struct unpack_trees_options unpack_opts;
44 struct index_state orig_index;
45};
Miklos Vajna9047ebb2008-08-12 18:45:14 +020046
Kevin Willfordfc65b002017-09-07 10:25:56 -060047struct path_hashmap_entry {
48 struct hashmap_entry e;
49 char path[FLEX_ARRAY];
50};
51
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +020052static int path_hashmap_cmp(const void *cmp_data UNUSED,
Eric Wong939af162019-10-06 23:30:37 +000053 const struct hashmap_entry *eptr,
54 const struct hashmap_entry *entry_or_key,
Kevin Willfordfc65b002017-09-07 10:25:56 -060055 const void *keydata)
56{
Eric Wong939af162019-10-06 23:30:37 +000057 const struct path_hashmap_entry *a, *b;
Kevin Willfordfc65b002017-09-07 10:25:56 -060058 const char *key = keydata;
59
Eric Wong939af162019-10-06 23:30:37 +000060 a = container_of(eptr, const struct path_hashmap_entry, e);
61 b = container_of(entry_or_key, const struct path_hashmap_entry, e);
62
René Scharfe2dee7e62021-08-28 23:30:49 +020063 return fspathcmp(a->path, key ? key : b->path);
Kevin Willfordfc65b002017-09-07 10:25:56 -060064}
65
Elijah Newren7c0a6c82019-08-17 11:41:37 -070066/*
67 * For dir_rename_entry, directory names are stored as a full path from the
68 * toplevel of the repository and do not include a trailing '/'. Also:
69 *
70 * dir: original name of directory being renamed
71 * non_unique_new_dir: if true, could not determine new_dir
72 * new_dir: final name of directory being renamed
73 * possible_new_dirs: temporary used to help determine new_dir; see comments
74 * in get_directory_renames() for details
75 */
76struct dir_rename_entry {
Junio C Hamano5efabc72019-10-15 13:48:01 +090077 struct hashmap_entry ent;
Elijah Newren7c0a6c82019-08-17 11:41:37 -070078 char *dir;
79 unsigned non_unique_new_dir:1;
80 struct strbuf new_dir;
81 struct string_list possible_new_dirs;
82};
83
Elijah Newren7fe40b82018-04-19 10:58:05 -070084static struct dir_rename_entry *dir_rename_find_entry(struct hashmap *hashmap,
85 char *dir)
86{
87 struct dir_rename_entry key;
88
Junio C Hamanoafe8a902022-05-02 09:50:37 -070089 if (!dir)
Elijah Newren7fe40b82018-04-19 10:58:05 -070090 return NULL;
Eric Wongd22245a2019-10-06 23:30:27 +000091 hashmap_entry_init(&key.ent, strhash(dir));
Elijah Newren7fe40b82018-04-19 10:58:05 -070092 key.dir = dir;
Eric Wong404ab782019-10-06 23:30:42 +000093 return hashmap_get_entry(hashmap, &key, ent, NULL);
Elijah Newren7fe40b82018-04-19 10:58:05 -070094}
95
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +020096static int dir_rename_cmp(const void *cmp_data UNUSED,
Eric Wong939af162019-10-06 23:30:37 +000097 const struct hashmap_entry *eptr,
98 const struct hashmap_entry *entry_or_key,
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +020099 const void *keydata UNUSED)
Elijah Newren7fe40b82018-04-19 10:58:05 -0700100{
Eric Wong939af162019-10-06 23:30:37 +0000101 const struct dir_rename_entry *e1, *e2;
102
103 e1 = container_of(eptr, const struct dir_rename_entry, ent);
104 e2 = container_of(entry_or_key, const struct dir_rename_entry, ent);
Elijah Newren7fe40b82018-04-19 10:58:05 -0700105
106 return strcmp(e1->dir, e2->dir);
107}
108
109static void dir_rename_init(struct hashmap *map)
110{
111 hashmap_init(map, dir_rename_cmp, NULL, 0);
112}
113
114static void dir_rename_entry_init(struct dir_rename_entry *entry,
115 char *directory)
116{
Eric Wongd22245a2019-10-06 23:30:27 +0000117 hashmap_entry_init(&entry->ent, strhash(directory));
Elijah Newren7fe40b82018-04-19 10:58:05 -0700118 entry->dir = directory;
119 entry->non_unique_new_dir = 0;
120 strbuf_init(&entry->new_dir, 0);
Ævar Arnfjörð Bjarmasonbc40dfb2021-07-01 12:51:29 +0200121 string_list_init_nodup(&entry->possible_new_dirs);
Elijah Newren7fe40b82018-04-19 10:58:05 -0700122}
123
Elijah Newren7c0a6c82019-08-17 11:41:37 -0700124struct collision_entry {
Junio C Hamano5efabc72019-10-15 13:48:01 +0900125 struct hashmap_entry ent;
Elijah Newren7c0a6c82019-08-17 11:41:37 -0700126 char *target_file;
127 struct string_list source_files;
128 unsigned reported_already:1;
129};
130
Elijah Newrene95ab702018-04-19 10:58:07 -0700131static struct collision_entry *collision_find_entry(struct hashmap *hashmap,
132 char *target_file)
133{
134 struct collision_entry key;
135
Eric Wongd22245a2019-10-06 23:30:27 +0000136 hashmap_entry_init(&key.ent, strhash(target_file));
Elijah Newrene95ab702018-04-19 10:58:07 -0700137 key.target_file = target_file;
Eric Wong404ab782019-10-06 23:30:42 +0000138 return hashmap_get_entry(hashmap, &key, ent, NULL);
Elijah Newrene95ab702018-04-19 10:58:07 -0700139}
140
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +0200141static int collision_cmp(const void *cmp_data UNUSED,
Eric Wong939af162019-10-06 23:30:37 +0000142 const struct hashmap_entry *eptr,
143 const struct hashmap_entry *entry_or_key,
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +0200144 const void *keydata UNUSED)
Elijah Newrene95ab702018-04-19 10:58:07 -0700145{
Eric Wong939af162019-10-06 23:30:37 +0000146 const struct collision_entry *e1, *e2;
147
148 e1 = container_of(eptr, const struct collision_entry, ent);
149 e2 = container_of(entry_or_key, const struct collision_entry, ent);
150
Elijah Newrene95ab702018-04-19 10:58:07 -0700151 return strcmp(e1->target_file, e2->target_file);
152}
153
154static void collision_init(struct hashmap *map)
155{
Eric Wong939af162019-10-06 23:30:37 +0000156 hashmap_init(map, collision_cmp, NULL, 0);
Elijah Newrene95ab702018-04-19 10:58:07 -0700157}
158
Elijah Newren259ccb62019-04-05 08:00:13 -0700159static void flush_output(struct merge_options *opt)
Johannes Schindelinbc9204d2016-08-01 13:44:37 +0200160{
Elijah Newren259ccb62019-04-05 08:00:13 -0700161 if (opt->buffer_output < 2 && opt->obuf.len) {
162 fputs(opt->obuf.buf, stdout);
163 strbuf_reset(&opt->obuf);
Johannes Schindelinbc9204d2016-08-01 13:44:37 +0200164 }
165}
166
Ævar Arnfjörð Bjarmason48ca53c2021-07-13 10:05:18 +0200167__attribute__((format (printf, 2, 3)))
Elijah Newren259ccb62019-04-05 08:00:13 -0700168static int err(struct merge_options *opt, const char *err, ...)
Johannes Schindelinbc9204d2016-08-01 13:44:37 +0200169{
170 va_list params;
171
Elijah Newren259ccb62019-04-05 08:00:13 -0700172 if (opt->buffer_output < 2)
173 flush_output(opt);
Johannes Schindelinf1e24262016-08-01 13:44:50 +0200174 else {
Elijah Newren259ccb62019-04-05 08:00:13 -0700175 strbuf_complete(&opt->obuf, '\n');
176 strbuf_addstr(&opt->obuf, "error: ");
Johannes Schindelinf1e24262016-08-01 13:44:50 +0200177 }
Johannes Schindelinbc9204d2016-08-01 13:44:37 +0200178 va_start(params, err);
Elijah Newren259ccb62019-04-05 08:00:13 -0700179 strbuf_vaddf(&opt->obuf, err, params);
Johannes Schindelinbc9204d2016-08-01 13:44:37 +0200180 va_end(params);
Elijah Newren259ccb62019-04-05 08:00:13 -0700181 if (opt->buffer_output > 1)
182 strbuf_addch(&opt->obuf, '\n');
Johannes Schindelinf1e24262016-08-01 13:44:50 +0200183 else {
Elijah Newren259ccb62019-04-05 08:00:13 -0700184 error("%s", opt->obuf.buf);
185 strbuf_reset(&opt->obuf);
Johannes Schindelinf1e24262016-08-01 13:44:50 +0200186 }
Johannes Schindelinbc9204d2016-08-01 13:44:37 +0200187
188 return -1;
189}
190
Nguyễn Thái Ngọc Duyd7cf3a92019-01-12 09:13:30 +0700191static struct tree *shift_tree_object(struct repository *repo,
192 struct tree *one, struct tree *two,
Junio C Hamano85e51b72008-06-30 22:18:57 -0700193 const char *subtree_shift)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200194{
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000195 struct object_id shifted;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200196
Junio C Hamano85e51b72008-06-30 22:18:57 -0700197 if (!*subtree_shift) {
Nguyễn Thái Ngọc Duy90d34052019-06-27 16:28:51 +0700198 shift_tree(repo, &one->object.oid, &two->object.oid, &shifted, 0);
Junio C Hamano85e51b72008-06-30 22:18:57 -0700199 } else {
Nguyễn Thái Ngọc Duy90d34052019-06-27 16:28:51 +0700200 shift_tree_by(repo, &one->object.oid, &two->object.oid, &shifted,
Junio C Hamano85e51b72008-06-30 22:18:57 -0700201 subtree_shift);
202 }
Jeff King4a7e27e2018-08-28 17:22:40 -0400203 if (oideq(&two->object.oid, &shifted))
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200204 return two;
Nguyễn Thái Ngọc Duyd7cf3a92019-01-12 09:13:30 +0700205 return lookup_tree(repo, &shifted);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200206}
207
Nguyễn Thái Ngọc Duya133c402019-04-16 16:33:18 +0700208static inline void set_commit_tree(struct commit *c, struct tree *t)
209{
210 c->maybe_tree = t;
211}
212
Nguyễn Thái Ngọc Duyd7cf3a92019-01-12 09:13:30 +0700213static struct commit *make_virtual_commit(struct repository *repo,
214 struct tree *tree,
215 const char *comment)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200216{
Nguyễn Thái Ngọc Duyd7cf3a92019-01-12 09:13:30 +0700217 struct commit *commit = alloc_commit_node(repo);
Junio C Hamanoae8e4c92011-11-07 13:26:22 -0800218
René Scharfea2571652016-08-13 14:16:04 +0200219 set_merge_remote_desc(commit, comment, (struct object *)commit);
Nguyễn Thái Ngọc Duya133c402019-04-16 16:33:18 +0700220 set_commit_tree(commit, tree);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200221 commit->object.parsed = 1;
222 return commit;
223}
224
Elijah Newren25c39362010-09-20 02:28:53 -0600225enum rename_type {
226 RENAME_NORMAL = 0,
Elijah Newren5455c332018-06-09 21:16:14 -0700227 RENAME_VIA_DIR,
Elijah Newren7f867162018-11-07 20:40:26 -0800228 RENAME_ADD,
Elijah Newren25c39362010-09-20 02:28:53 -0600229 RENAME_DELETE,
Elijah Newren4f66dad2011-08-11 23:20:08 -0600230 RENAME_ONE_FILE_TO_ONE,
Elijah Newren461f5042011-08-11 23:20:15 -0600231 RENAME_ONE_FILE_TO_TWO,
232 RENAME_TWO_FILES_TO_ONE
Elijah Newren25c39362010-09-20 02:28:53 -0600233};
234
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200235/*
236 * Since we want to write the index eventually, we cannot reuse the index
237 * for these (temporary) data.
238 */
Jonathan Nieder9cba13c2011-03-16 02:08:34 -0500239struct stage_data {
Elijah Newren8daec1d2019-04-05 08:00:22 -0700240 struct diff_filespec stages[4]; /* mostly for oid & mode; maybe path */
Elijah Newren4f66dad2011-08-11 23:20:08 -0600241 struct rename_conflict_info *rename_conflict_info;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200242 unsigned processed:1;
243};
244
Elijah Newren967d6be2019-04-05 08:00:17 -0700245struct rename {
Elijah Newren6d169fd2019-04-05 08:00:24 -0700246 unsigned processed:1;
Elijah Newren967d6be2019-04-05 08:00:17 -0700247 struct diff_filepair *pair;
Elijah Newrenc336ab82019-04-05 08:00:20 -0700248 const char *branch; /* branch that the rename occurred on */
Elijah Newren967d6be2019-04-05 08:00:17 -0700249 /*
Elijah Newren6d169fd2019-04-05 08:00:24 -0700250 * If directory rename detection affected this rename, what was its
251 * original type ('A' or 'R') and it's original destination before
252 * the directory rename (otherwise, '\0' and NULL for these two vars).
253 */
254 char dir_rename_original_type;
255 char *dir_rename_original_dest;
256 /*
Elijah Newren967d6be2019-04-05 08:00:17 -0700257 * Purpose of src_entry and dst_entry:
258 *
259 * If 'before' is renamed to 'after' then src_entry will contain
260 * the versions of 'before' from the merge_base, HEAD, and MERGE in
261 * stages 1, 2, and 3; dst_entry will contain the respective
262 * versions of 'after' in corresponding locations. Thus, we have a
263 * total of six modes and oids, though some will be null. (Stage 0
264 * is ignored; we're interested in handling conflicts.)
265 *
266 * Since we don't turn on break-rewrites by default, neither
267 * src_entry nor dst_entry can have all three of their stages have
268 * non-null oids, meaning at most four of the six will be non-null.
269 * Also, since this is a rename, both src_entry and dst_entry will
270 * have at least one non-null oid, meaning at least two will be
271 * non-null. Of the six oids, a typical rename will have three be
272 * non-null. Only two implies a rename/delete, and four implies a
273 * rename/add.
274 */
275 struct stage_data *src_entry;
276 struct stage_data *dst_entry;
Elijah Newren967d6be2019-04-05 08:00:17 -0700277};
278
279struct rename_conflict_info {
280 enum rename_type rename_type;
Elijah Newrene9cd1b52019-04-05 08:00:18 -0700281 struct rename *ren1;
282 struct rename *ren2;
Elijah Newren967d6be2019-04-05 08:00:17 -0700283};
284
Elijah Newren4f66dad2011-08-11 23:20:08 -0600285static inline void setup_rename_conflict_info(enum rename_type rename_type,
Elijah Newrene9cd1b52019-04-05 08:00:18 -0700286 struct merge_options *opt,
287 struct rename *ren1,
Elijah Newrenc336ab82019-04-05 08:00:20 -0700288 struct rename *ren2)
Elijah Newren25c39362010-09-20 02:28:53 -0600289{
Elijah Newren4f445452018-10-16 13:19:48 -0700290 struct rename_conflict_info *ci;
291
292 /*
293 * When we have two renames involved, it's easiest to get the
294 * correct things into stage 2 and 3, and to make sure that the
295 * content merge puts HEAD before the other branch if we just
Elijah Newren259ccb62019-04-05 08:00:13 -0700296 * ensure that branch1 == opt->branch1. So, simply flip arguments
Elijah Newren4f445452018-10-16 13:19:48 -0700297 * around if we don't have that.
298 */
Elijah Newrenc336ab82019-04-05 08:00:20 -0700299 if (ren2 && ren1->branch != opt->branch1) {
300 setup_rename_conflict_info(rename_type, opt, ren2, ren1);
Elijah Newren4f445452018-10-16 13:19:48 -0700301 return;
302 }
303
René Scharfeca56dad2021-03-13 17:17:22 +0100304 CALLOC_ARRAY(ci, 1);
Elijah Newren25c39362010-09-20 02:28:53 -0600305 ci->rename_type = rename_type;
Elijah Newrene9cd1b52019-04-05 08:00:18 -0700306 ci->ren1 = ren1;
307 ci->ren2 = ren2;
Elijah Newren25c39362010-09-20 02:28:53 -0600308
Elijah Newrene9cd1b52019-04-05 08:00:18 -0700309 ci->ren1->dst_entry->processed = 0;
310 ci->ren1->dst_entry->rename_conflict_info = ci;
Elijah Newrene9cd1b52019-04-05 08:00:18 -0700311 if (ren2) {
312 ci->ren2->dst_entry->rename_conflict_info = ci;
Elijah Newren25c39362010-09-20 02:28:53 -0600313 }
314}
315
Elijah Newren259ccb62019-04-05 08:00:13 -0700316static int show(struct merge_options *opt, int v)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200317{
Elijah Newren5bf7e572019-08-17 11:41:41 -0700318 return (!opt->priv->call_depth && opt->verbosity >= v) ||
319 opt->verbosity >= 5;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200320}
321
Tarmigan Casebolt28bea9e2009-11-14 13:33:13 -0800322__attribute__((format (printf, 3, 4)))
Elijah Newren259ccb62019-04-05 08:00:13 -0700323static void output(struct merge_options *opt, int v, const char *fmt, ...)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200324{
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200325 va_list ap;
326
Elijah Newren259ccb62019-04-05 08:00:13 -0700327 if (!show(opt, v))
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200328 return;
329
Elijah Newren5bf7e572019-08-17 11:41:41 -0700330 strbuf_addchars(&opt->obuf, ' ', opt->priv->call_depth * 2);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200331
332 va_start(ap, fmt);
Elijah Newren259ccb62019-04-05 08:00:13 -0700333 strbuf_vaddf(&opt->obuf, fmt, ap);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200334 va_end(ap);
335
Elijah Newren259ccb62019-04-05 08:00:13 -0700336 strbuf_addch(&opt->obuf, '\n');
337 if (!opt->buffer_output)
338 flush_output(opt);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200339}
340
Jonathan Tan155b5172021-10-08 14:08:17 -0700341static void repo_output_commit_title(struct merge_options *opt,
342 struct repository *repo,
343 struct commit *commit)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200344{
Nguyễn Thái Ngọc Duye2e5ac22018-05-19 07:28:30 +0200345 struct merge_remote_desc *desc;
346
Elijah Newren5bf7e572019-08-17 11:41:41 -0700347 strbuf_addchars(&opt->obuf, ' ', opt->priv->call_depth * 2);
Nguyễn Thái Ngọc Duye2e5ac22018-05-19 07:28:30 +0200348 desc = merge_remote_util(commit);
349 if (desc)
Elijah Newren259ccb62019-04-05 08:00:13 -0700350 strbuf_addf(&opt->obuf, "virtual %s\n", desc->name);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200351 else {
Jonathan Tan155b5172021-10-08 14:08:17 -0700352 strbuf_repo_add_unique_abbrev(&opt->obuf, repo,
353 &commit->object.oid,
354 DEFAULT_ABBREV);
Elijah Newren259ccb62019-04-05 08:00:13 -0700355 strbuf_addch(&opt->obuf, ' ');
Jonathan Tan155b5172021-10-08 14:08:17 -0700356 if (repo_parse_commit(repo, commit) != 0)
Elijah Newren259ccb62019-04-05 08:00:13 -0700357 strbuf_addstr(&opt->obuf, _("(bad commit)\n"));
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200358 else {
Christian Couder49b71202010-07-22 15:18:34 +0200359 const char *title;
Jonathan Tan155b5172021-10-08 14:08:17 -0700360 const char *msg = repo_get_commit_buffer(repo, commit, NULL);
Jeff Kingbc6b8fc2014-06-10 17:41:51 -0400361 int len = find_commit_subject(msg, &title);
Christian Couder49b71202010-07-22 15:18:34 +0200362 if (len)
Elijah Newren259ccb62019-04-05 08:00:13 -0700363 strbuf_addf(&opt->obuf, "%.*s\n", len, title);
Jonathan Tan155b5172021-10-08 14:08:17 -0700364 repo_unuse_commit_buffer(repo, commit, msg);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200365 }
366 }
Elijah Newren259ccb62019-04-05 08:00:13 -0700367 flush_output(opt);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200368}
369
Jonathan Tan155b5172021-10-08 14:08:17 -0700370static void output_commit_title(struct merge_options *opt, struct commit *commit)
371{
372 repo_output_commit_title(opt, the_repository, commit);
373}
374
Elijah Newren259ccb62019-04-05 08:00:13 -0700375static int add_cacheinfo(struct merge_options *opt,
Elijah Newren8daec1d2019-04-05 08:00:22 -0700376 const struct diff_filespec *blob,
Elijah Newrend90e7592018-06-09 21:16:12 -0700377 const char *path, int stage, int refresh, int options)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200378{
Elijah Newren259ccb62019-04-05 08:00:13 -0700379 struct index_state *istate = opt->repo->index;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200380 struct cache_entry *ce;
Junio C Hamano1335d762016-07-08 10:59:15 -0700381 int ret;
382
Elijah Newren8daec1d2019-04-05 08:00:22 -0700383 ce = make_cache_entry(istate, blob->mode, &blob->oid, path, stage, 0);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200384 if (!ce)
Elijah Newren259ccb62019-04-05 08:00:13 -0700385 return err(opt, _("add_cacheinfo failed for path '%s'; merge aborting."), path);
Junio C Hamano1335d762016-07-08 10:59:15 -0700386
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700387 ret = add_index_entry(istate, ce, options);
Junio C Hamano1335d762016-07-08 10:59:15 -0700388 if (refresh) {
389 struct cache_entry *nce;
390
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700391 nce = refresh_cache_entry(istate, ce,
392 CE_MATCH_REFRESH | CE_MATCH_IGNORE_MISSING);
Johannes Schindelin55e9f0e2016-11-26 13:48:06 +0100393 if (!nce)
Elijah Newren259ccb62019-04-05 08:00:13 -0700394 return err(opt, _("add_cacheinfo failed to refresh for path '%s'; merge aborting."), path);
Junio C Hamano1335d762016-07-08 10:59:15 -0700395 if (nce != ce)
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700396 ret = add_index_entry(istate, nce, options);
Junio C Hamano1335d762016-07-08 10:59:15 -0700397 }
398 return ret;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200399}
400
Elijah Newren7c0a6c82019-08-17 11:41:37 -0700401static inline int merge_detect_rename(struct merge_options *opt)
402{
Elijah Newren8599ab42019-08-17 11:41:38 -0700403 return (opt->detect_renames >= 0) ? opt->detect_renames : 1;
Elijah Newren7c0a6c82019-08-17 11:41:37 -0700404}
405
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200406static void init_tree_desc_from_tree(struct tree_desc *desc, struct tree *tree)
407{
408 parse_tree(tree);
409 init_tree_desc(desc, tree->buffer, tree->size);
410}
411
Elijah Newren259ccb62019-04-05 08:00:13 -0700412static int unpack_trees_start(struct merge_options *opt,
Elijah Newren3f1c1c32018-05-20 12:17:35 +0200413 struct tree *common,
414 struct tree *head,
415 struct tree *merge)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200416{
417 int rc;
418 struct tree_desc t[3];
Ævar Arnfjörð Bjarmason6269f8e2023-01-17 14:57:00 +0100419 struct index_state tmp_index = INDEX_STATE_INIT(opt->repo);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200420
Elijah Newren5bf7e572019-08-17 11:41:41 -0700421 memset(&opt->priv->unpack_opts, 0, sizeof(opt->priv->unpack_opts));
422 if (opt->priv->call_depth)
423 opt->priv->unpack_opts.index_only = 1;
Elijah Newren491a7572021-09-27 16:33:40 +0000424 else {
Elijah Newren5bf7e572019-08-17 11:41:41 -0700425 opt->priv->unpack_opts.update = 1;
Elijah Newren491a7572021-09-27 16:33:40 +0000426 /* FIXME: should only do this if !overwrite_ignore */
Elijah Newren04988c82021-09-27 16:33:41 +0000427 opt->priv->unpack_opts.preserve_ignored = 0;
Elijah Newren491a7572021-09-27 16:33:40 +0000428 }
Elijah Newren5bf7e572019-08-17 11:41:41 -0700429 opt->priv->unpack_opts.merge = 1;
430 opt->priv->unpack_opts.head_idx = 2;
431 opt->priv->unpack_opts.fn = threeway_merge;
432 opt->priv->unpack_opts.src_index = opt->repo->index;
433 opt->priv->unpack_opts.dst_index = &tmp_index;
434 opt->priv->unpack_opts.aggressive = !merge_detect_rename(opt);
435 setup_unpack_trees_porcelain(&opt->priv->unpack_opts, "merge");
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200436
437 init_tree_desc_from_tree(t+0, common);
438 init_tree_desc_from_tree(t+1, head);
439 init_tree_desc_from_tree(t+2, merge);
440
Elijah Newren5bf7e572019-08-17 11:41:41 -0700441 rc = unpack_trees(3, t, &opt->priv->unpack_opts);
Elijah Newren259ccb62019-04-05 08:00:13 -0700442 cache_tree_free(&opt->repo->index->cache_tree);
Elijah Newrena35edc82018-04-19 10:58:20 -0700443
444 /*
Elijah Newren5bf7e572019-08-17 11:41:41 -0700445 * Update opt->repo->index to match the new results, AFTER saving a
446 * copy in opt->priv->orig_index. Update src_index to point to the
447 * saved copy. (verify_uptodate() checks src_index, and the original
448 * index is the one that had the necessary modification timestamps.)
Elijah Newrena35edc82018-04-19 10:58:20 -0700449 */
Elijah Newren5bf7e572019-08-17 11:41:41 -0700450 opt->priv->orig_index = *opt->repo->index;
Elijah Newren259ccb62019-04-05 08:00:13 -0700451 *opt->repo->index = tmp_index;
Elijah Newren5bf7e572019-08-17 11:41:41 -0700452 opt->priv->unpack_opts.src_index = &opt->priv->orig_index;
Elijah Newrena35edc82018-04-19 10:58:20 -0700453
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200454 return rc;
455}
456
Elijah Newren259ccb62019-04-05 08:00:13 -0700457static void unpack_trees_finish(struct merge_options *opt)
Elijah Newren3f1c1c32018-05-20 12:17:35 +0200458{
Elijah Newren5bf7e572019-08-17 11:41:41 -0700459 discard_index(&opt->priv->orig_index);
460 clear_unpack_trees_porcelain(&opt->priv->unpack_opts);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200461}
462
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +0200463static int save_files_dirs(const struct object_id *oid UNUSED,
Elijah Newrend90e7592018-06-09 21:16:12 -0700464 struct strbuf *base, const char *path,
Ævar Arnfjörð Bjarmason47957482021-03-20 23:37:51 +0100465 unsigned int mode, void *context)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200466{
Kevin Willfordfc65b002017-09-07 10:25:56 -0600467 struct path_hashmap_entry *entry;
Nguyễn Thái Ngọc Duy6a0b0b62014-11-30 16:05:00 +0700468 int baselen = base->len;
Elijah Newren259ccb62019-04-05 08:00:13 -0700469 struct merge_options *opt = context;
Miklos Vajna696ee232008-09-03 19:08:56 +0200470
Nguyễn Thái Ngọc Duy6a0b0b62014-11-30 16:05:00 +0700471 strbuf_addstr(base, path);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200472
Kevin Willfordfc65b002017-09-07 10:25:56 -0600473 FLEX_ALLOC_MEM(entry, path, base->buf, base->len);
René Scharfe74318422021-07-30 21:06:58 +0200474 hashmap_entry_init(&entry->e, fspathhash(entry->path));
Junio C Hamano5efabc72019-10-15 13:48:01 +0900475 hashmap_add(&opt->priv->current_file_dir_set, &entry->e);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200476
Nguyễn Thái Ngọc Duy6a0b0b62014-11-30 16:05:00 +0700477 strbuf_setlen(base, baselen);
Lars Hjemlid3bee162009-01-25 01:52:05 +0100478 return (S_ISDIR(mode) ? READ_TREE_RECURSIVE : 0);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200479}
480
Elijah Newren259ccb62019-04-05 08:00:13 -0700481static void get_files_dirs(struct merge_options *opt, struct tree *tree)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200482{
Nguyễn Thái Ngọc Duyf0096c02011-03-25 16:34:19 +0700483 struct pathspec match_all;
Nguyễn Thái Ngọc Duy9a087272013-07-14 15:35:59 +0700484 memset(&match_all, 0, sizeof(match_all));
Ævar Arnfjörð Bjarmason47957482021-03-20 23:37:51 +0100485 read_tree(opt->repo, tree,
486 &match_all, save_files_dirs, opt);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200487}
488
Nguyễn Thái Ngọc Duy34e77712019-06-27 16:28:52 +0700489static int get_tree_entry_if_blob(struct repository *r,
490 const struct object_id *tree,
Elijah Newren5b047ac2018-04-19 10:58:09 -0700491 const char *path,
Elijah Newren8daec1d2019-04-05 08:00:22 -0700492 struct diff_filespec *dfs)
Elijah Newren5b047ac2018-04-19 10:58:09 -0700493{
494 int ret;
495
Nguyễn Thái Ngọc Duy34e77712019-06-27 16:28:52 +0700496 ret = get_tree_entry(r, tree, path, &dfs->oid, &dfs->mode);
Elijah Newren8daec1d2019-04-05 08:00:22 -0700497 if (S_ISDIR(dfs->mode)) {
brian m. carlson14228442021-04-26 01:02:56 +0000498 oidcpy(&dfs->oid, null_oid());
Elijah Newren8daec1d2019-04-05 08:00:22 -0700499 dfs->mode = 0;
Elijah Newren5b047ac2018-04-19 10:58:09 -0700500 }
501 return ret;
502}
503
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200504/*
505 * Returns an index_entry instance which doesn't have to correspond to
506 * a real cache entry in Git's index.
507 */
Nguyễn Thái Ngọc Duy34e77712019-06-27 16:28:52 +0700508static struct stage_data *insert_stage_data(struct repository *r,
509 const char *path,
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200510 struct tree *o, struct tree *a, struct tree *b,
511 struct string_list *entries)
512{
513 struct string_list_item *item;
514 struct stage_data *e = xcalloc(1, sizeof(struct stage_data));
Nguyễn Thái Ngọc Duy34e77712019-06-27 16:28:52 +0700515 get_tree_entry_if_blob(r, &o->object.oid, path, &e->stages[1]);
516 get_tree_entry_if_blob(r, &a->object.oid, path, &e->stages[2]);
517 get_tree_entry_if_blob(r, &b->object.oid, path, &e->stages[3]);
Julian Phillips78a395d2010-06-26 00:41:35 +0100518 item = string_list_insert(entries, path);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200519 item->util = e;
520 return e;
521}
522
523/*
524 * Create a dictionary mapping file names to stage_data objects. The
525 * dictionary contains one entry for every path with a non-zero stage entry.
526 */
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700527static struct string_list *get_unmerged(struct index_state *istate)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200528{
Ævar Arnfjörð Bjarmasonf2605052022-04-13 22:01:32 +0200529 struct string_list *unmerged = xmalloc(sizeof(struct string_list));
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200530 int i;
531
Ævar Arnfjörð Bjarmasonf2605052022-04-13 22:01:32 +0200532 string_list_init_dup(unmerged);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200533
Derrick Stoleef7ef64b2021-04-01 01:49:56 +0000534 /* TODO: audit for interaction with sparse-index. */
535 ensure_full_index(istate);
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700536 for (i = 0; i < istate->cache_nr; i++) {
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200537 struct string_list_item *item;
538 struct stage_data *e;
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700539 const struct cache_entry *ce = istate->cache[i];
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200540 if (!ce_stage(ce))
541 continue;
542
Julian Phillipse8c8b712010-06-26 00:41:37 +0100543 item = string_list_lookup(unmerged, ce->name);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200544 if (!item) {
Julian Phillips78a395d2010-06-26 00:41:35 +0100545 item = string_list_insert(unmerged, ce->name);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200546 item->util = xcalloc(1, sizeof(struct stage_data));
547 }
548 e = item->util;
549 e->stages[ce_stage(ce)].mode = ce->ce_mode;
brian m. carlson99d1a982016-09-05 20:07:52 +0000550 oidcpy(&e->stages[ce_stage(ce)].oid, &ce->oid);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200551 }
552
553 return unmerged;
554}
555
Nguyễn Thái Ngọc Duyfa6ca112016-11-24 18:45:36 +0700556static int string_list_df_name_compare(const char *one, const char *two)
Elijah Newrenef02b312010-09-20 02:29:09 -0600557{
Nguyễn Thái Ngọc Duyfa6ca112016-11-24 18:45:36 +0700558 int onelen = strlen(one);
559 int twolen = strlen(two);
Elijah Newrenf0fd4d02011-08-11 23:19:56 -0600560 /*
561 * Here we only care that entries for D/F conflicts are
562 * adjacent, in particular with the file of the D/F conflict
563 * appearing before files below the corresponding directory.
564 * The order of the rest of the list is irrelevant for us.
Elijah Newrenef02b312010-09-20 02:29:09 -0600565 *
Elijah Newrenf0fd4d02011-08-11 23:19:56 -0600566 * To achieve this, we sort with df_name_compare and provide
567 * the mode S_IFDIR so that D/F conflicts will sort correctly.
568 * We use the mode S_IFDIR for everything else for simplicity,
569 * since in other cases any changes in their order due to
570 * sorting cause no problems for us.
Elijah Newrenef02b312010-09-20 02:29:09 -0600571 */
Nguyễn Thái Ngọc Duyfa6ca112016-11-24 18:45:36 +0700572 int cmp = df_name_compare(one, onelen, S_IFDIR,
573 two, twolen, S_IFDIR);
Elijah Newrenf0fd4d02011-08-11 23:19:56 -0600574 /*
575 * Now that 'foo' and 'foo/bar' compare equal, we have to make sure
576 * that 'foo' comes before 'foo/bar'.
577 */
578 if (cmp)
579 return cmp;
580 return onelen - twolen;
581}
582
Elijah Newren259ccb62019-04-05 08:00:13 -0700583static void record_df_conflict_files(struct merge_options *opt,
Elijah Newren70cc3d32011-08-11 23:19:58 -0600584 struct string_list *entries)
Elijah Newrenef02b312010-09-20 02:29:09 -0600585{
Elijah Newren70cc3d32011-08-11 23:19:58 -0600586 /* If there is a D/F conflict and the file for such a conflict
Elijah Newren2d6bad92018-06-09 21:16:11 -0700587 * currently exists in the working tree, we want to allow it to be
Elijah Newrenedd2faf2011-08-11 23:20:07 -0600588 * removed to make room for the corresponding directory if needed.
589 * The files underneath the directories of such D/F conflicts will
590 * be processed before the corresponding file involved in the D/F
591 * conflict. If the D/F directory ends up being removed by the
592 * merge, then we won't have to touch the D/F file. If the D/F
593 * directory needs to be written to the working copy, then the D/F
594 * file will simply be removed (in make_room_for_path()) to make
595 * room for the necessary paths. Note that if both the directory
596 * and the file need to be present, then the D/F file will be
597 * reinstated with a new unique name at the time it is processed.
Elijah Newrenef02b312010-09-20 02:29:09 -0600598 */
René Scharfeaf4941d2016-08-05 22:42:12 +0200599 struct string_list df_sorted_entries = STRING_LIST_INIT_NODUP;
Elijah Newrenef02b312010-09-20 02:29:09 -0600600 const char *last_file = NULL;
Junio C Hamanoc8516502010-10-21 07:34:33 -0700601 int last_len = 0;
Elijah Newrenef02b312010-09-20 02:29:09 -0600602 int i;
603
Elijah Newren0b30e812011-08-11 23:19:54 -0600604 /*
605 * If we're merging merge-bases, we don't want to bother with
606 * any working directory changes.
607 */
Elijah Newren5bf7e572019-08-17 11:41:41 -0700608 if (opt->priv->call_depth)
Elijah Newren0b30e812011-08-11 23:19:54 -0600609 return;
610
Elijah Newrenf0fd4d02011-08-11 23:19:56 -0600611 /* Ensure D/F conflicts are adjacent in the entries list. */
Elijah Newrenef02b312010-09-20 02:29:09 -0600612 for (i = 0; i < entries->nr; i++) {
Elijah Newrenf701aae2011-08-12 20:23:51 -0600613 struct string_list_item *next = &entries->items[i];
614 string_list_append(&df_sorted_entries, next->string)->util =
615 next->util;
616 }
Nguyễn Thái Ngọc Duyfa6ca112016-11-24 18:45:36 +0700617 df_sorted_entries.cmp = string_list_df_name_compare;
618 string_list_sort(&df_sorted_entries);
Elijah Newrenf0fd4d02011-08-11 23:19:56 -0600619
Elijah Newren5bf7e572019-08-17 11:41:41 -0700620 string_list_clear(&opt->priv->df_conflict_file_set, 1);
Elijah Newrenf701aae2011-08-12 20:23:51 -0600621 for (i = 0; i < df_sorted_entries.nr; i++) {
622 const char *path = df_sorted_entries.items[i].string;
Elijah Newrenef02b312010-09-20 02:29:09 -0600623 int len = strlen(path);
Elijah Newrenf701aae2011-08-12 20:23:51 -0600624 struct stage_data *e = df_sorted_entries.items[i].util;
Elijah Newrenef02b312010-09-20 02:29:09 -0600625
626 /*
627 * Check if last_file & path correspond to a D/F conflict;
628 * i.e. whether path is last_file+'/'+<something>.
Elijah Newren70cc3d32011-08-11 23:19:58 -0600629 * If so, record that it's okay to remove last_file to make
630 * room for path and friends if needed.
Elijah Newrenef02b312010-09-20 02:29:09 -0600631 */
632 if (last_file &&
633 len > last_len &&
634 memcmp(path, last_file, last_len) == 0 &&
635 path[last_len] == '/') {
Elijah Newren5bf7e572019-08-17 11:41:41 -0700636 string_list_insert(&opt->priv->df_conflict_file_set, last_file);
Elijah Newrenef02b312010-09-20 02:29:09 -0600637 }
638
639 /*
640 * Determine whether path could exist as a file in the
641 * working directory as a possible D/F conflict. This
642 * will only occur when it exists in stage 2 as a
643 * file.
644 */
645 if (S_ISREG(e->stages[2].mode) || S_ISLNK(e->stages[2].mode)) {
646 last_file = path;
647 last_len = len;
Elijah Newrenef02b312010-09-20 02:29:09 -0600648 } else {
649 last_file = NULL;
650 }
651 }
Elijah Newrenf701aae2011-08-12 20:23:51 -0600652 string_list_clear(&df_sorted_entries, 0);
Elijah Newrenef02b312010-09-20 02:29:09 -0600653}
654
Johannes Schindelinbc9204d2016-08-01 13:44:37 +0200655static int update_stages(struct merge_options *opt, const char *path,
656 const struct diff_filespec *o,
Elijah Newren650467c2011-08-11 23:19:52 -0600657 const struct diff_filespec *a,
658 const struct diff_filespec *b)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200659{
Elijah Newrenf53d3972011-08-11 23:20:25 -0600660
661 /*
662 * NOTE: It is usually a bad idea to call update_stages on a path
663 * before calling update_file on that same path, since it can
664 * sometimes lead to spurious "refusing to lose untracked file..."
665 * messages from update_file (via make_room_for path via
666 * would_lose_untracked). Instead, reverse the order of the calls
667 * (executing update_file first and then update_stages).
668 */
Elijah Newren650467c2011-08-11 23:19:52 -0600669 int clear = 1;
670 int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_SKIP_DFCHECK;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200671 if (clear)
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700672 if (remove_file_from_index(opt->repo->index, path))
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200673 return -1;
674 if (o)
Elijah Newren8daec1d2019-04-05 08:00:22 -0700675 if (add_cacheinfo(opt, o, path, 1, 0, options))
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200676 return -1;
677 if (a)
Elijah Newren8daec1d2019-04-05 08:00:22 -0700678 if (add_cacheinfo(opt, a, path, 2, 0, options))
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200679 return -1;
680 if (b)
Elijah Newren8daec1d2019-04-05 08:00:22 -0700681 if (add_cacheinfo(opt, b, path, 3, 0, options))
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200682 return -1;
683 return 0;
684}
685
Elijah Newrenb8ddf162011-08-11 23:20:02 -0600686static void update_entry(struct stage_data *entry,
687 struct diff_filespec *o,
688 struct diff_filespec *a,
689 struct diff_filespec *b)
Elijah Newren2ff739f2010-09-20 02:28:54 -0600690{
Elijah Newren2ff739f2010-09-20 02:28:54 -0600691 entry->processed = 0;
692 entry->stages[1].mode = o->mode;
693 entry->stages[2].mode = a->mode;
694 entry->stages[3].mode = b->mode;
brian m. carlsonfd429e92016-06-24 23:09:25 +0000695 oidcpy(&entry->stages[1].oid, &o->oid);
696 oidcpy(&entry->stages[2].oid, &a->oid);
697 oidcpy(&entry->stages[3].oid, &b->oid);
Elijah Newren2ff739f2010-09-20 02:28:54 -0600698}
699
Elijah Newren259ccb62019-04-05 08:00:13 -0700700static int remove_file(struct merge_options *opt, int clean,
Miklos Vajnab7fa51d2008-09-02 23:53:47 +0200701 const char *path, int no_wd)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200702{
Elijah Newren5bf7e572019-08-17 11:41:41 -0700703 int update_cache = opt->priv->call_depth || clean;
704 int update_working_directory = !opt->priv->call_depth && !no_wd;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200705
706 if (update_cache) {
Elijah Newren259ccb62019-04-05 08:00:13 -0700707 if (remove_file_from_index(opt->repo->index, path))
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200708 return -1;
709 }
710 if (update_working_directory) {
David Turnerae352c72014-05-01 20:21:09 -0400711 if (ignore_case) {
712 struct cache_entry *ce;
Elijah Newren259ccb62019-04-05 08:00:13 -0700713 ce = index_file_exists(opt->repo->index, path, strlen(path),
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700714 ignore_case);
Elijah Newren4cba2b02017-11-24 11:59:01 -0800715 if (ce && ce_stage(ce) == 0 && strcmp(path, ce->name))
David Turnerae352c72014-05-01 20:21:09 -0400716 return 0;
717 }
Peter Collingbourne25755e82010-03-26 15:25:35 +0000718 if (remove_path(path))
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200719 return -1;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200720 }
721 return 0;
722}
723
Jeff King45bc1312014-06-19 17:30:26 -0400724/* add a string to a strbuf, but converting "/" to "_" */
725static void add_flattened_path(struct strbuf *out, const char *s)
726{
727 size_t i = out->len;
728 strbuf_addstr(out, s);
729 for (; i < out->len; i++)
730 if (out->buf[i] == '/')
731 out->buf[i] = '_';
732}
733
Elijah Newren4d7101e2019-08-17 11:41:33 -0700734static char *unique_path(struct merge_options *opt,
735 const char *path,
736 const char *branch)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200737{
Kevin Willfordfc65b002017-09-07 10:25:56 -0600738 struct path_hashmap_entry *entry;
Jeff King45bc1312014-06-19 17:30:26 -0400739 struct strbuf newpath = STRBUF_INIT;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200740 int suffix = 0;
Jeff King45bc1312014-06-19 17:30:26 -0400741 size_t base_len;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200742
Jeff King45bc1312014-06-19 17:30:26 -0400743 strbuf_addf(&newpath, "%s~", path);
744 add_flattened_path(&newpath, branch);
745
746 base_len = newpath.len;
Elijah Newren5bf7e572019-08-17 11:41:41 -0700747 while (hashmap_get_from_hash(&opt->priv->current_file_dir_set,
René Scharfe74318422021-07-30 21:06:58 +0200748 fspathhash(newpath.buf), newpath.buf) ||
Elijah Newren5bf7e572019-08-17 11:41:41 -0700749 (!opt->priv->call_depth && file_exists(newpath.buf))) {
Jeff King45bc1312014-06-19 17:30:26 -0400750 strbuf_setlen(&newpath, base_len);
751 strbuf_addf(&newpath, "_%d", suffix++);
752 }
753
Kevin Willfordfc65b002017-09-07 10:25:56 -0600754 FLEX_ALLOC_MEM(entry, path, newpath.buf, newpath.len);
René Scharfe74318422021-07-30 21:06:58 +0200755 hashmap_entry_init(&entry->e, fspathhash(entry->path));
Junio C Hamano5efabc72019-10-15 13:48:01 +0900756 hashmap_add(&opt->priv->current_file_dir_set, &entry->e);
Jeff King45bc1312014-06-19 17:30:26 -0400757 return strbuf_detach(&newpath, NULL);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200758}
759
David Turner5423d2e2016-11-07 13:31:31 -0500760/**
761 * Check whether a directory in the index is in the way of an incoming
762 * file. Return 1 if so. If check_working_copy is non-zero, also
763 * check the working directory. If empty_ok is non-zero, also return
764 * 0 in the case where the working-tree dir exists but is empty.
765 */
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700766static int dir_in_way(struct index_state *istate, const char *path,
767 int check_working_copy, int empty_ok)
Elijah Newrenf2507b42011-08-11 23:19:57 -0600768{
Jeff Kingb4600fb2015-09-24 17:07:43 -0400769 int pos;
770 struct strbuf dirpath = STRBUF_INIT;
Elijah Newrenf2507b42011-08-11 23:19:57 -0600771 struct stat st;
772
Jeff Kingb4600fb2015-09-24 17:07:43 -0400773 strbuf_addstr(&dirpath, path);
774 strbuf_addch(&dirpath, '/');
Elijah Newrenf2507b42011-08-11 23:19:57 -0600775
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700776 pos = index_name_pos(istate, dirpath.buf, dirpath.len);
Elijah Newrenf2507b42011-08-11 23:19:57 -0600777
778 if (pos < 0)
779 pos = -1 - pos;
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700780 if (pos < istate->cache_nr &&
781 !strncmp(dirpath.buf, istate->cache[pos]->name, dirpath.len)) {
Jeff Kingb4600fb2015-09-24 17:07:43 -0400782 strbuf_release(&dirpath);
Elijah Newrenf2507b42011-08-11 23:19:57 -0600783 return 1;
784 }
785
Jeff Kingb4600fb2015-09-24 17:07:43 -0400786 strbuf_release(&dirpath);
David Turner5423d2e2016-11-07 13:31:31 -0500787 return check_working_copy && !lstat(path, &st) && S_ISDIR(st.st_mode) &&
Jonathan Tan83e3ad32019-09-18 13:27:38 -0700788 !(empty_ok && is_empty_dir(path)) &&
789 !has_symlink_leading_path(path, strlen(path));
Elijah Newrenf2507b42011-08-11 23:19:57 -0600790}
791
Elijah Newrena35edc82018-04-19 10:58:20 -0700792/*
Elijah Newren1de70db2018-04-19 10:58:23 -0700793 * Returns whether path was tracked in the index before the merge started,
794 * and its oid and mode match the specified values
795 */
Elijah Newren259ccb62019-04-05 08:00:13 -0700796static int was_tracked_and_matches(struct merge_options *opt, const char *path,
Elijah Newren8daec1d2019-04-05 08:00:22 -0700797 const struct diff_filespec *blob)
Junio C Hamano60c91182008-12-15 02:41:24 -0800798{
Elijah Newren5bf7e572019-08-17 11:41:41 -0700799 int pos = index_name_pos(&opt->priv->orig_index, path, strlen(path));
Elijah Newren1de70db2018-04-19 10:58:23 -0700800 struct cache_entry *ce;
801
802 if (0 > pos)
803 /* we were not tracking this path before the merge */
804 return 0;
805
806 /* See if the file we were tracking before matches */
Elijah Newren5bf7e572019-08-17 11:41:41 -0700807 ce = opt->priv->orig_index.cache[pos];
Elijah Newren763a59e2020-01-01 05:20:57 +0000808 return (oideq(&ce->oid, &blob->oid) && ce->ce_mode == blob->mode);
Elijah Newren1de70db2018-04-19 10:58:23 -0700809}
810
811/*
Elijah Newrena35edc82018-04-19 10:58:20 -0700812 * Returns whether path was tracked in the index before the merge started
813 */
Elijah Newren259ccb62019-04-05 08:00:13 -0700814static int was_tracked(struct merge_options *opt, const char *path)
Junio C Hamano60c91182008-12-15 02:41:24 -0800815{
Elijah Newren5bf7e572019-08-17 11:41:41 -0700816 int pos = index_name_pos(&opt->priv->orig_index, path, strlen(path));
Junio C Hamano60c91182008-12-15 02:41:24 -0800817
Johannes Schindelinf8d83fb2016-07-26 18:05:57 +0200818 if (0 <= pos)
Elijah Newrena35edc82018-04-19 10:58:20 -0700819 /* we were tracking this path before the merge */
Johannes Schindelinf8d83fb2016-07-26 18:05:57 +0200820 return 1;
821
Elijah Newrenaacb82d2011-08-11 23:19:59 -0600822 return 0;
Junio C Hamano60c91182008-12-15 02:41:24 -0800823}
824
Elijah Newren259ccb62019-04-05 08:00:13 -0700825static int would_lose_untracked(struct merge_options *opt, const char *path)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200826{
Elijah Newren259ccb62019-04-05 08:00:13 -0700827 struct index_state *istate = opt->repo->index;
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700828
Elijah Newrena35edc82018-04-19 10:58:20 -0700829 /*
830 * This may look like it can be simplified to:
Elijah Newren259ccb62019-04-05 08:00:13 -0700831 * return !was_tracked(opt, path) && file_exists(path)
Elijah Newrena35edc82018-04-19 10:58:20 -0700832 * but it can't. This function needs to know whether path was in
833 * the working tree due to EITHER having been tracked in the index
834 * before the merge OR having been put into the working copy and
835 * index by unpack_trees(). Due to that either-or requirement, we
836 * check the current index instead of the original one.
837 *
838 * Note that we do not need to worry about merge-recursive itself
839 * updating the index after unpack_trees() and before calling this
840 * function, because we strictly require all code paths in
841 * merge-recursive to update the working tree first and the index
842 * second. Doing otherwise would break
843 * update_file()/would_lose_untracked(); see every comment in this
844 * file which mentions "update_stages".
845 */
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700846 int pos = index_name_pos(istate, path, strlen(path));
Elijah Newrena35edc82018-04-19 10:58:20 -0700847
848 if (pos < 0)
849 pos = -1 - pos;
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700850 while (pos < istate->cache_nr &&
851 !strcmp(path, istate->cache[pos]->name)) {
Elijah Newrena35edc82018-04-19 10:58:20 -0700852 /*
853 * If stage #0, it is definitely tracked.
854 * If it has stage #2 then it was tracked
855 * before this merge started. All other
856 * cases the path was not tracked.
857 */
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700858 switch (ce_stage(istate->cache[pos])) {
Elijah Newrena35edc82018-04-19 10:58:20 -0700859 case 0:
860 case 2:
861 return 0;
862 }
863 pos++;
864 }
865 return file_exists(path);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200866}
867
Elijah Newren259ccb62019-04-05 08:00:13 -0700868static int was_dirty(struct merge_options *opt, const char *path)
Elijah Newren64b1abe2018-04-19 10:58:12 -0700869{
870 struct cache_entry *ce;
871 int dirty = 1;
872
Elijah Newren5bf7e572019-08-17 11:41:41 -0700873 if (opt->priv->call_depth || !was_tracked(opt, path))
Elijah Newren64b1abe2018-04-19 10:58:12 -0700874 return !dirty;
875
Elijah Newren5bf7e572019-08-17 11:41:41 -0700876 ce = index_file_exists(opt->priv->unpack_opts.src_index,
Elijah Newren277292d2018-04-19 10:58:21 -0700877 path, strlen(path), ignore_case);
Elijah Newren5bf7e572019-08-17 11:41:41 -0700878 dirty = verify_uptodate(ce, &opt->priv->unpack_opts) != 0;
Elijah Newren64b1abe2018-04-19 10:58:12 -0700879 return dirty;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200880}
881
Elijah Newren259ccb62019-04-05 08:00:13 -0700882static int make_room_for_path(struct merge_options *opt, const char *path)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200883{
Elijah Newrened0148a2011-08-11 23:20:01 -0600884 int status, i;
Jiang Xin55653a62012-07-25 22:53:13 +0800885 const char *msg = _("failed to create path '%s'%s");
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200886
Elijah Newrened0148a2011-08-11 23:20:01 -0600887 /* Unlink any D/F conflict files that are in the way */
Elijah Newren5bf7e572019-08-17 11:41:41 -0700888 for (i = 0; i < opt->priv->df_conflict_file_set.nr; i++) {
889 const char *df_path = opt->priv->df_conflict_file_set.items[i].string;
Elijah Newrened0148a2011-08-11 23:20:01 -0600890 size_t pathlen = strlen(path);
891 size_t df_pathlen = strlen(df_path);
892 if (df_pathlen < pathlen &&
893 path[df_pathlen] == '/' &&
894 strncmp(path, df_path, df_pathlen) == 0) {
Elijah Newren259ccb62019-04-05 08:00:13 -0700895 output(opt, 3,
Jiang Xin55653a62012-07-25 22:53:13 +0800896 _("Removing %s to make room for subdirectory\n"),
Elijah Newrened0148a2011-08-11 23:20:01 -0600897 df_path);
898 unlink(df_path);
Elijah Newren5bf7e572019-08-17 11:41:41 -0700899 unsorted_string_list_delete_item(&opt->priv->df_conflict_file_set,
Elijah Newrened0148a2011-08-11 23:20:01 -0600900 i, 0);
901 break;
902 }
903 }
904
905 /* Make sure leading directories are created */
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200906 status = safe_create_leading_directories_const(path);
907 if (status) {
Johannes Schindelin60033032016-07-26 18:06:26 +0200908 if (status == SCLD_EXISTS)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200909 /* something else exists */
Elijah Newren259ccb62019-04-05 08:00:13 -0700910 return err(opt, msg, path, _(": perhaps a D/F conflict?"));
911 return err(opt, msg, path, "");
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200912 }
913
Junio C Hamano60c91182008-12-15 02:41:24 -0800914 /*
915 * Do not unlink a file in the work tree if we are not
916 * tracking it.
917 */
Elijah Newren259ccb62019-04-05 08:00:13 -0700918 if (would_lose_untracked(opt, path))
919 return err(opt, _("refusing to lose untracked file at '%s'"),
Elijah Newrend90e7592018-06-09 21:16:12 -0700920 path);
Junio C Hamano60c91182008-12-15 02:41:24 -0800921
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200922 /* Successful unlink is good.. */
923 if (!unlink(path))
924 return 0;
925 /* .. and so is no existing file */
926 if (errno == ENOENT)
927 return 0;
928 /* .. but not some other error (who really cares what?) */
Elijah Newren259ccb62019-04-05 08:00:13 -0700929 return err(opt, msg, path, _(": perhaps a D/F conflict?"));
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200930}
931
Elijah Newren259ccb62019-04-05 08:00:13 -0700932static int update_file_flags(struct merge_options *opt,
Elijah Newren8daec1d2019-04-05 08:00:22 -0700933 const struct diff_filespec *contents,
Johannes Schindelin75456f92016-07-26 18:06:21 +0200934 const char *path,
935 int update_cache,
936 int update_wd)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200937{
Johannes Schindelin60033032016-07-26 18:06:26 +0200938 int ret = 0;
939
Elijah Newren5bf7e572019-08-17 11:41:41 -0700940 if (opt->priv->call_depth)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200941 update_wd = 0;
942
943 if (update_wd) {
944 enum object_type type;
945 void *buf;
946 unsigned long size;
947
Elijah Newren8daec1d2019-04-05 08:00:22 -0700948 if (S_ISGITLINK(contents->mode)) {
Junio C Hamano0c44c942009-04-29 11:08:18 -0700949 /*
950 * We may later decide to recursively descend into
951 * the submodule directory and update its index
952 * and/or work tree, but we do not do that now.
953 */
Heiko Voigt68d03e42010-07-07 15:39:13 +0200954 update_wd = 0;
Junio C Hamano0c44c942009-04-29 11:08:18 -0700955 goto update_index;
Heiko Voigt68d03e42010-07-07 15:39:13 +0200956 }
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200957
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +0200958 buf = repo_read_object_file(the_repository, &contents->oid,
959 &type, &size);
Elijah Newrenf836bf32019-08-17 11:41:26 -0700960 if (!buf) {
961 ret = err(opt, _("cannot read object %s '%s'"),
962 oid_to_hex(&contents->oid), path);
963 goto free_buf;
964 }
Johannes Schindelin60033032016-07-26 18:06:26 +0200965 if (type != OBJ_BLOB) {
Elijah Newren8daec1d2019-04-05 08:00:22 -0700966 ret = err(opt, _("blob expected for %s '%s'"),
967 oid_to_hex(&contents->oid), path);
Johannes Schindelin60033032016-07-26 18:06:26 +0200968 goto free_buf;
969 }
Elijah Newren8daec1d2019-04-05 08:00:22 -0700970 if (S_ISREG(contents->mode)) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500971 struct strbuf strbuf = STRBUF_INIT;
Elijah Newren4d7101e2019-08-17 11:41:33 -0700972 if (convert_to_working_tree(opt->repo->index,
brian m. carlsonab90eca2020-03-16 18:05:02 +0000973 path, buf, size, &strbuf, NULL)) {
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200974 free(buf);
975 size = strbuf.len;
976 buf = strbuf_detach(&strbuf, NULL);
977 }
978 }
979
Elijah Newren259ccb62019-04-05 08:00:13 -0700980 if (make_room_for_path(opt, path) < 0) {
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200981 update_wd = 0;
Johannes Schindelin75456f92016-07-26 18:06:21 +0200982 goto free_buf;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200983 }
Elijah Newren8daec1d2019-04-05 08:00:22 -0700984 if (S_ISREG(contents->mode) ||
985 (!has_symlinks && S_ISLNK(contents->mode))) {
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200986 int fd;
Elijah Newren8daec1d2019-04-05 08:00:22 -0700987 int mode = (contents->mode & 0100 ? 0777 : 0666);
988
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200989 fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);
Johannes Schindelin60033032016-07-26 18:06:26 +0200990 if (fd < 0) {
Elijah Newren259ccb62019-04-05 08:00:13 -0700991 ret = err(opt, _("failed to open '%s': %s"),
Johannes Schindelinbc9204d2016-08-01 13:44:37 +0200992 path, strerror(errno));
Johannes Schindelin60033032016-07-26 18:06:26 +0200993 goto free_buf;
994 }
Thomas Rastf633ea22012-08-03 14:16:25 +0200995 write_in_full(fd, buf, size);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200996 close(fd);
Elijah Newren8daec1d2019-04-05 08:00:22 -0700997 } else if (S_ISLNK(contents->mode)) {
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200998 char *lnk = xmemdupz(buf, size);
999 safe_create_leading_directories_const(path);
1000 unlink(path);
Alex Riesen304dcf22008-12-05 01:39:14 +01001001 if (symlink(lnk, path))
Elijah Newren259ccb62019-04-05 08:00:13 -07001002 ret = err(opt, _("failed to symlink '%s': %s"),
Elijah Newrend90e7592018-06-09 21:16:12 -07001003 path, strerror(errno));
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001004 free(lnk);
1005 } else
Elijah Newren259ccb62019-04-05 08:00:13 -07001006 ret = err(opt,
Johannes Schindelinbc9204d2016-08-01 13:44:37 +02001007 _("do not know what to do with %06o %s '%s'"),
Elijah Newren8daec1d2019-04-05 08:00:22 -07001008 contents->mode, oid_to_hex(&contents->oid), path);
Elijah Newren93665362018-06-09 21:16:13 -07001009 free_buf:
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001010 free(buf);
1011 }
Elijah Newren93665362018-06-09 21:16:13 -07001012update_index:
Elijah Newrenfb1c18f2020-02-19 17:04:07 +00001013 if (!ret && update_cache) {
1014 int refresh = (!opt->priv->call_depth &&
1015 contents->mode != S_IFGITLINK);
1016 if (add_cacheinfo(opt, contents, path, 0, refresh,
Elijah Newrenfd53b7ff2018-04-19 10:58:16 -07001017 ADD_CACHE_OK_TO_ADD))
1018 return -1;
Elijah Newrenfb1c18f2020-02-19 17:04:07 +00001019 }
Johannes Schindelin60033032016-07-26 18:06:26 +02001020 return ret;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001021}
1022
Elijah Newren259ccb62019-04-05 08:00:13 -07001023static int update_file(struct merge_options *opt,
Johannes Schindelin75456f92016-07-26 18:06:21 +02001024 int clean,
Elijah Newren8daec1d2019-04-05 08:00:22 -07001025 const struct diff_filespec *contents,
Johannes Schindelin75456f92016-07-26 18:06:21 +02001026 const char *path)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001027{
Elijah Newren8daec1d2019-04-05 08:00:22 -07001028 return update_file_flags(opt, contents, path,
Elijah Newren5bf7e572019-08-17 11:41:41 -07001029 opt->priv->call_depth || clean, !opt->priv->call_depth);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001030}
1031
1032/* Low level file merging, update and removal */
1033
Jonathan Nieder9cba13c2011-03-16 02:08:34 -05001034struct merge_file_info {
Elijah Newren8daec1d2019-04-05 08:00:22 -07001035 struct diff_filespec blob; /* mostly use oid & mode; sometimes path */
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001036 unsigned clean:1,
1037 merge:1;
1038};
1039
Elijah Newren259ccb62019-04-05 08:00:13 -07001040static int merge_3way(struct merge_options *opt,
Miklos Vajnab7fa51d2008-09-02 23:53:47 +02001041 mmbuffer_t *result_buf,
Elijah Newrene3de8882019-04-05 08:00:14 -07001042 const struct diff_filespec *o,
Elijah Newren0c059422011-08-11 23:19:51 -06001043 const struct diff_filespec *a,
1044 const struct diff_filespec *b,
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001045 const char *branch1,
Elijah Newrenb2a79422018-11-07 20:40:24 -08001046 const char *branch2,
1047 const int extra_marker_size)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001048{
1049 mmfile_t orig, src1, src2;
Jonathan Nieder712516b2010-08-26 00:49:53 -05001050 struct ll_merge_options ll_opts = {0};
Elijah Newren139ef372019-08-15 14:40:32 -07001051 char *base, *name1, *name2;
Elijah Newren35f69672022-02-02 02:37:30 +00001052 enum ll_merge_result merge_status;
Avery Pennarun8cc5b292009-11-25 21:23:55 -05001053
Elijah Newren259ccb62019-04-05 08:00:13 -07001054 ll_opts.renormalize = opt->renormalize;
Elijah Newrenb2a79422018-11-07 20:40:24 -08001055 ll_opts.extra_marker_size = extra_marker_size;
Elijah Newren259ccb62019-04-05 08:00:13 -07001056 ll_opts.xdl_opts = opt->xdl_opts;
Jonathan Nieder712516b2010-08-26 00:49:53 -05001057
Elijah Newren5bf7e572019-08-17 11:41:41 -07001058 if (opt->priv->call_depth) {
Jonathan Nieder712516b2010-08-26 00:49:53 -05001059 ll_opts.virtual_ancestor = 1;
1060 ll_opts.variant = 0;
1061 } else {
Elijah Newren259ccb62019-04-05 08:00:13 -07001062 switch (opt->recursive_variant) {
Elijah Newrenf3081da2019-08-17 11:41:42 -07001063 case MERGE_VARIANT_OURS:
Jonathan Nieder712516b2010-08-26 00:49:53 -05001064 ll_opts.variant = XDL_MERGE_FAVOR_OURS;
Avery Pennarun8cc5b292009-11-25 21:23:55 -05001065 break;
Elijah Newrenf3081da2019-08-17 11:41:42 -07001066 case MERGE_VARIANT_THEIRS:
Jonathan Nieder712516b2010-08-26 00:49:53 -05001067 ll_opts.variant = XDL_MERGE_FAVOR_THEIRS;
Avery Pennarun8cc5b292009-11-25 21:23:55 -05001068 break;
1069 default:
Jonathan Nieder712516b2010-08-26 00:49:53 -05001070 ll_opts.variant = 0;
Avery Pennarun8cc5b292009-11-25 21:23:55 -05001071 break;
1072 }
1073 }
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001074
Elijah Newren139ef372019-08-15 14:40:32 -07001075 assert(a->path && b->path && o->path && opt->ancestor);
1076 if (strcmp(a->path, b->path) || strcmp(a->path, o->path) != 0) {
1077 base = mkpathdup("%s:%s", opt->ancestor, o->path);
Ramsay Jones4e2d0942012-09-04 18:31:14 +01001078 name1 = mkpathdup("%s:%s", branch1, a->path);
1079 name2 = mkpathdup("%s:%s", branch2, b->path);
Martin Renold606475f2009-07-01 22:18:04 +02001080 } else {
Elijah Newren139ef372019-08-15 14:40:32 -07001081 base = mkpathdup("%s", opt->ancestor);
Ramsay Jones4e2d0942012-09-04 18:31:14 +01001082 name1 = mkpathdup("%s", branch1);
1083 name2 = mkpathdup("%s", branch2);
Martin Renold606475f2009-07-01 22:18:04 +02001084 }
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001085
Elijah Newrene3de8882019-04-05 08:00:14 -07001086 read_mmblob(&orig, &o->oid);
brian m. carlsond4493472016-09-05 20:08:02 +00001087 read_mmblob(&src1, &a->oid);
1088 read_mmblob(&src2, &b->oid);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001089
Elijah Newren816147e2021-03-20 00:03:53 +00001090 /*
1091 * FIXME: Using a->path for normalization rules in ll_merge could be
1092 * wrong if we renamed from a->path to b->path. We should use the
1093 * target path for where the file will be written.
1094 */
Elijah Newren139ef372019-08-15 14:40:32 -07001095 merge_status = ll_merge(result_buf, a->path, &orig, base,
Nguyễn Thái Ngọc Duy32eaa462018-09-21 17:57:27 +02001096 &src1, name1, &src2, name2,
Elijah Newren259ccb62019-04-05 08:00:13 -07001097 opt->repo->index, &ll_opts);
Elijah Newren35f69672022-02-02 02:37:30 +00001098 if (merge_status == LL_MERGE_BINARY_CONFLICT)
1099 warning("Cannot merge binary files: %s (%s vs. %s)",
1100 a->path, name1, name2);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001101
Elijah Newren139ef372019-08-15 14:40:32 -07001102 free(base);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001103 free(name1);
1104 free(name2);
1105 free(orig.ptr);
1106 free(src1.ptr);
1107 free(src2.ptr);
1108 return merge_status;
1109}
1110
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +07001111static int find_first_merges(struct repository *repo,
1112 struct object_array *result, const char *path,
Elijah Newrend90e7592018-06-09 21:16:12 -07001113 struct commit *a, struct commit *b)
Stefan Beller18cfc082018-05-15 13:00:28 -07001114{
1115 int i, j;
1116 struct object_array merges = OBJECT_ARRAY_INIT;
1117 struct commit *commit;
1118 int contains_another;
1119
brian m. carlsondb1ba2a2019-02-19 00:04:59 +00001120 char merged_revision[GIT_MAX_HEXSZ + 2];
Stefan Beller18cfc082018-05-15 13:00:28 -07001121 const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path",
1122 "--all", merged_revision, NULL };
1123 struct rev_info revs;
1124 struct setup_revision_opt rev_opts;
1125
1126 memset(result, 0, sizeof(struct object_array));
1127 memset(&rev_opts, 0, sizeof(rev_opts));
1128
1129 /* get all revisions that merge commit a */
1130 xsnprintf(merged_revision, sizeof(merged_revision), "^%s",
Elijah Newrend90e7592018-06-09 21:16:12 -07001131 oid_to_hex(&a->object.oid));
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +07001132 repo_init_revisions(repo, &revs, NULL);
Stefan Beller18cfc082018-05-15 13:00:28 -07001133 /* FIXME: can't handle linked worktrees in submodules yet */
1134 revs.single_worktree = path != NULL;
1135 setup_revisions(ARRAY_SIZE(rev_args)-1, rev_args, &revs, &rev_opts);
1136
1137 /* save all revisions from the above list that contain b */
1138 if (prepare_revision_walk(&revs))
1139 die("revision walk setup failed");
1140 while ((commit = get_revision(&revs)) != NULL) {
1141 struct object *o = &(commit->object);
Jonathan Tan10a0d6a2021-09-09 11:47:29 -07001142 if (repo_in_merge_bases(repo, b, commit))
Stefan Beller18cfc082018-05-15 13:00:28 -07001143 add_object_array(o, NULL, &merges);
1144 }
1145 reset_revision_walk();
1146
1147 /* Now we've got all merges that contain a and b. Prune all
1148 * merges that contain another found merge and save them in
1149 * result.
1150 */
1151 for (i = 0; i < merges.nr; i++) {
1152 struct commit *m1 = (struct commit *) merges.objects[i].item;
1153
1154 contains_another = 0;
1155 for (j = 0; j < merges.nr; j++) {
1156 struct commit *m2 = (struct commit *) merges.objects[j].item;
Jonathan Tan10a0d6a2021-09-09 11:47:29 -07001157 if (i != j && repo_in_merge_bases(repo, m2, m1)) {
Stefan Beller18cfc082018-05-15 13:00:28 -07001158 contains_another = 1;
1159 break;
1160 }
1161 }
1162
1163 if (!contains_another)
1164 add_object_array(merges.objects[i].item, NULL, result);
1165 }
1166
1167 object_array_clear(&merges);
Ævar Arnfjörð Bjarmason2108fe42022-04-13 22:01:36 +02001168 release_revisions(&revs);
Stefan Beller18cfc082018-05-15 13:00:28 -07001169 return result->nr;
1170}
1171
Jonathan Tan155b5172021-10-08 14:08:17 -07001172static void print_commit(struct repository *repo, struct commit *commit)
Stefan Beller18cfc082018-05-15 13:00:28 -07001173{
1174 struct strbuf sb = STRBUF_INIT;
1175 struct pretty_print_context ctx = {0};
1176 ctx.date_mode.type = DATE_NORMAL;
Elijah Newren816147e2021-03-20 00:03:53 +00001177 /* FIXME: Merge this with output_commit_title() */
1178 assert(!merge_remote_util(commit));
Jonathan Tan155b5172021-10-08 14:08:17 -07001179 repo_format_commit_message(repo, commit, " %h: %m %s", &sb, &ctx);
Stefan Beller18cfc082018-05-15 13:00:28 -07001180 fprintf(stderr, "%s\n", sb.buf);
1181 strbuf_release(&sb);
1182}
1183
Elijah Newren8daec1d2019-04-05 08:00:22 -07001184static int is_valid(const struct diff_filespec *dfs)
1185{
1186 return dfs->mode != 0 && !is_null_oid(&dfs->oid);
1187}
1188
Elijah Newren259ccb62019-04-05 08:00:13 -07001189static int merge_submodule(struct merge_options *opt,
Stefan Beller325f3a82018-05-15 13:00:29 -07001190 struct object_id *result, const char *path,
Stefan Beller18cfc082018-05-15 13:00:28 -07001191 const struct object_id *base, const struct object_id *a,
Stefan Beller325f3a82018-05-15 13:00:29 -07001192 const struct object_id *b)
Stefan Beller18cfc082018-05-15 13:00:28 -07001193{
Jonathan Tan10a0d6a2021-09-09 11:47:29 -07001194 struct repository subrepo;
1195 int ret = 0;
Stefan Beller18cfc082018-05-15 13:00:28 -07001196 struct commit *commit_base, *commit_a, *commit_b;
1197 int parent_count;
1198 struct object_array merges;
1199
1200 int i;
Elijah Newren5bf7e572019-08-17 11:41:41 -07001201 int search = !opt->priv->call_depth;
Stefan Beller18cfc082018-05-15 13:00:28 -07001202
1203 /* store a in result in case we fail */
Elijah Newren816147e2021-03-20 00:03:53 +00001204 /* FIXME: This is the WRONG resolution for the recursive case when
1205 * we need to be careful to avoid accidentally matching either side.
1206 * Should probably use o instead there, much like we do for merging
1207 * binaries.
1208 */
Stefan Beller18cfc082018-05-15 13:00:28 -07001209 oidcpy(result, a);
1210
1211 /* we can not handle deletion conflicts */
1212 if (is_null_oid(base))
1213 return 0;
1214 if (is_null_oid(a))
1215 return 0;
1216 if (is_null_oid(b))
1217 return 0;
1218
Jonathan Tan10a0d6a2021-09-09 11:47:29 -07001219 if (repo_submodule_init(&subrepo, opt->repo, path, null_oid())) {
1220 output(opt, 1, _("Failed to merge submodule %s (not checked out)"), path);
Stefan Beller18cfc082018-05-15 13:00:28 -07001221 return 0;
1222 }
1223
Jonathan Tan10a0d6a2021-09-09 11:47:29 -07001224 if (!(commit_base = lookup_commit_reference(&subrepo, base)) ||
1225 !(commit_a = lookup_commit_reference(&subrepo, a)) ||
1226 !(commit_b = lookup_commit_reference(&subrepo, b))) {
1227 output(opt, 1, _("Failed to merge submodule %s (commits not present)"), path);
1228 goto cleanup;
1229 }
1230
Stefan Beller18cfc082018-05-15 13:00:28 -07001231 /* check whether both changes are forward */
Jonathan Tan10a0d6a2021-09-09 11:47:29 -07001232 if (!repo_in_merge_bases(&subrepo, commit_base, commit_a) ||
1233 !repo_in_merge_bases(&subrepo, commit_base, commit_b)) {
Elijah Newren259ccb62019-04-05 08:00:13 -07001234 output(opt, 1, _("Failed to merge submodule %s (commits don't follow merge-base)"), path);
Jonathan Tan10a0d6a2021-09-09 11:47:29 -07001235 goto cleanup;
Stefan Beller18cfc082018-05-15 13:00:28 -07001236 }
1237
1238 /* Case #1: a is contained in b or vice versa */
Jonathan Tan10a0d6a2021-09-09 11:47:29 -07001239 if (repo_in_merge_bases(&subrepo, commit_a, commit_b)) {
Stefan Beller18cfc082018-05-15 13:00:28 -07001240 oidcpy(result, b);
Elijah Newren259ccb62019-04-05 08:00:13 -07001241 if (show(opt, 3)) {
1242 output(opt, 3, _("Fast-forwarding submodule %s to the following commit:"), path);
Jonathan Tan155b5172021-10-08 14:08:17 -07001243 repo_output_commit_title(opt, &subrepo, commit_b);
Elijah Newren259ccb62019-04-05 08:00:13 -07001244 } else if (show(opt, 2))
1245 output(opt, 2, _("Fast-forwarding submodule %s"), path);
Leif Middelschulte76f42122018-05-17 11:40:08 -07001246 else
1247 ; /* no output */
1248
Jonathan Tan10a0d6a2021-09-09 11:47:29 -07001249 ret = 1;
1250 goto cleanup;
Stefan Beller18cfc082018-05-15 13:00:28 -07001251 }
Jonathan Tan10a0d6a2021-09-09 11:47:29 -07001252 if (repo_in_merge_bases(&subrepo, commit_b, commit_a)) {
Stefan Beller18cfc082018-05-15 13:00:28 -07001253 oidcpy(result, a);
Elijah Newren259ccb62019-04-05 08:00:13 -07001254 if (show(opt, 3)) {
1255 output(opt, 3, _("Fast-forwarding submodule %s to the following commit:"), path);
Jonathan Tan155b5172021-10-08 14:08:17 -07001256 repo_output_commit_title(opt, &subrepo, commit_a);
Elijah Newren259ccb62019-04-05 08:00:13 -07001257 } else if (show(opt, 2))
1258 output(opt, 2, _("Fast-forwarding submodule %s"), path);
Leif Middelschulte76f42122018-05-17 11:40:08 -07001259 else
1260 ; /* no output */
1261
Jonathan Tan10a0d6a2021-09-09 11:47:29 -07001262 ret = 1;
1263 goto cleanup;
Stefan Beller18cfc082018-05-15 13:00:28 -07001264 }
1265
1266 /*
1267 * Case #2: There are one or more merges that contain a and b in
1268 * the submodule. If there is only one, then present it as a
1269 * suggestion to the user, but leave it marked unmerged so the
1270 * user needs to confirm the resolution.
1271 */
1272
1273 /* Skip the search if makes no sense to the calling context. */
1274 if (!search)
Jonathan Tan10a0d6a2021-09-09 11:47:29 -07001275 goto cleanup;
Stefan Beller18cfc082018-05-15 13:00:28 -07001276
1277 /* find commit which merges them */
Jonathan Tan10a0d6a2021-09-09 11:47:29 -07001278 parent_count = find_first_merges(&subrepo, &merges, path,
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +07001279 commit_a, commit_b);
Stefan Beller18cfc082018-05-15 13:00:28 -07001280 switch (parent_count) {
1281 case 0:
Elijah Newren259ccb62019-04-05 08:00:13 -07001282 output(opt, 1, _("Failed to merge submodule %s (merge following commits not found)"), path);
Stefan Beller18cfc082018-05-15 13:00:28 -07001283 break;
1284
1285 case 1:
Elijah Newren259ccb62019-04-05 08:00:13 -07001286 output(opt, 1, _("Failed to merge submodule %s (not fast-forward)"), path);
1287 output(opt, 2, _("Found a possible merge resolution for the submodule:\n"));
Jonathan Tan155b5172021-10-08 14:08:17 -07001288 print_commit(&subrepo, (struct commit *) merges.objects[0].item);
Elijah Newren259ccb62019-04-05 08:00:13 -07001289 output(opt, 2, _(
Elijah Newrend90e7592018-06-09 21:16:12 -07001290 "If this is correct simply add it to the index "
1291 "for example\n"
1292 "by using:\n\n"
1293 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
1294 "which will accept this suggestion.\n"),
1295 oid_to_hex(&merges.objects[0].item->oid), path);
Stefan Beller18cfc082018-05-15 13:00:28 -07001296 break;
1297
1298 default:
Elijah Newren259ccb62019-04-05 08:00:13 -07001299 output(opt, 1, _("Failed to merge submodule %s (multiple merges found)"), path);
Stefan Beller18cfc082018-05-15 13:00:28 -07001300 for (i = 0; i < merges.nr; i++)
Jonathan Tan155b5172021-10-08 14:08:17 -07001301 print_commit(&subrepo, (struct commit *) merges.objects[i].item);
Stefan Beller18cfc082018-05-15 13:00:28 -07001302 }
1303
1304 object_array_clear(&merges);
Jonathan Tan10a0d6a2021-09-09 11:47:29 -07001305cleanup:
1306 repo_clear(&subrepo);
1307 return ret;
Stefan Beller18cfc082018-05-15 13:00:28 -07001308}
1309
Elijah Newren259ccb62019-04-05 08:00:13 -07001310static int merge_mode_and_contents(struct merge_options *opt,
Elijah Newrene3de8882019-04-05 08:00:14 -07001311 const struct diff_filespec *o,
Elijah Newrend9573552018-09-19 09:14:34 -07001312 const struct diff_filespec *a,
1313 const struct diff_filespec *b,
1314 const char *filename,
1315 const char *branch1,
1316 const char *branch2,
Elijah Newrenb2a79422018-11-07 20:40:24 -08001317 const int extra_marker_size,
Elijah Newrend9573552018-09-19 09:14:34 -07001318 struct merge_file_info *result)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001319{
Elijah Newren259ccb62019-04-05 08:00:13 -07001320 if (opt->branch1 != branch1) {
Elijah Newren4f445452018-10-16 13:19:48 -07001321 /*
1322 * It's weird getting a reverse merge with HEAD on the bottom
1323 * side of the conflict markers and the other branch on the
1324 * top. Fix that.
1325 */
Elijah Newrene3de8882019-04-05 08:00:14 -07001326 return merge_mode_and_contents(opt, o, b, a,
Elijah Newren4f445452018-10-16 13:19:48 -07001327 filename,
Elijah Newrenb2a79422018-11-07 20:40:24 -08001328 branch2, branch1,
1329 extra_marker_size, result);
Elijah Newren4f445452018-10-16 13:19:48 -07001330 }
1331
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +02001332 result->merge = 0;
1333 result->clean = 1;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001334
1335 if ((S_IFMT & a->mode) != (S_IFMT & b->mode)) {
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +02001336 result->clean = 0;
Elijah Newren816147e2021-03-20 00:03:53 +00001337 /*
1338 * FIXME: This is a bad resolution for recursive case; for
1339 * the recursive case we want something that is unlikely to
1340 * accidentally match either side. Also, while it makes
1341 * sense to prefer regular files over symlinks, it doesn't
1342 * make sense to prefer regular files over submodules.
1343 */
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001344 if (S_ISREG(a->mode)) {
Elijah Newren8daec1d2019-04-05 08:00:22 -07001345 result->blob.mode = a->mode;
1346 oidcpy(&result->blob.oid, &a->oid);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001347 } else {
Elijah Newren8daec1d2019-04-05 08:00:22 -07001348 result->blob.mode = b->mode;
1349 oidcpy(&result->blob.oid, &b->oid);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001350 }
1351 } else {
Elijah Newren763a59e2020-01-01 05:20:57 +00001352 if (!oideq(&a->oid, &o->oid) && !oideq(&b->oid, &o->oid))
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +02001353 result->merge = 1;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001354
1355 /*
1356 * Merge modes
1357 */
Elijah Newrene3de8882019-04-05 08:00:14 -07001358 if (a->mode == b->mode || a->mode == o->mode)
Elijah Newren8daec1d2019-04-05 08:00:22 -07001359 result->blob.mode = b->mode;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001360 else {
Elijah Newren8daec1d2019-04-05 08:00:22 -07001361 result->blob.mode = a->mode;
Elijah Newrene3de8882019-04-05 08:00:14 -07001362 if (b->mode != o->mode) {
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +02001363 result->clean = 0;
1364 result->merge = 1;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001365 }
1366 }
1367
Elijah Newren763a59e2020-01-01 05:20:57 +00001368 if (oideq(&a->oid, &b->oid) || oideq(&a->oid, &o->oid))
Elijah Newren8daec1d2019-04-05 08:00:22 -07001369 oidcpy(&result->blob.oid, &b->oid);
Elijah Newren763a59e2020-01-01 05:20:57 +00001370 else if (oideq(&b->oid, &o->oid))
Elijah Newren8daec1d2019-04-05 08:00:22 -07001371 oidcpy(&result->blob.oid, &a->oid);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001372 else if (S_ISREG(a->mode)) {
1373 mmbuffer_t result_buf;
Johannes Schindelin60033032016-07-26 18:06:26 +02001374 int ret = 0, merge_status;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001375
Elijah Newrene3de8882019-04-05 08:00:14 -07001376 merge_status = merge_3way(opt, &result_buf, o, a, b,
Elijah Newrenb2a79422018-11-07 20:40:24 -08001377 branch1, branch2,
1378 extra_marker_size);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001379
1380 if ((merge_status < 0) || !result_buf.ptr)
Jeff King24c5a272023-09-16 18:11:46 -04001381 ret = err(opt, _("failed to execute internal merge"));
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001382
Patryk Obaraa09c9852018-01-28 01:13:19 +01001383 if (!ret &&
1384 write_object_file(result_buf.ptr, result_buf.size,
Ævar Arnfjörð Bjarmasonc80d2262022-02-05 00:48:26 +01001385 OBJ_BLOB, &result->blob.oid))
Jeff King24c5a272023-09-16 18:11:46 -04001386 ret = err(opt, _("unable to add %s to database"),
Johannes Schindelinbc9204d2016-08-01 13:44:37 +02001387 a->path);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001388
1389 free(result_buf.ptr);
Johannes Schindelin60033032016-07-26 18:06:26 +02001390 if (ret)
1391 return ret;
Elijah Newren816147e2021-03-20 00:03:53 +00001392 /* FIXME: bug, what if modes didn't match? */
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +02001393 result->clean = (merge_status == 0);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001394 } else if (S_ISGITLINK(a->mode)) {
Elijah Newren8daec1d2019-04-05 08:00:22 -07001395 result->clean = merge_submodule(opt, &result->blob.oid,
Elijah Newrene3de8882019-04-05 08:00:14 -07001396 o->path,
1397 &o->oid,
Elijah Newrend90e7592018-06-09 21:16:12 -07001398 &a->oid,
1399 &b->oid);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001400 } else if (S_ISLNK(a->mode)) {
Elijah Newren259ccb62019-04-05 08:00:13 -07001401 switch (opt->recursive_variant) {
Elijah Newrenf3081da2019-08-17 11:41:42 -07001402 case MERGE_VARIANT_NORMAL:
Elijah Newren8daec1d2019-04-05 08:00:22 -07001403 oidcpy(&result->blob.oid, &a->oid);
Elijah Newren763a59e2020-01-01 05:20:57 +00001404 if (!oideq(&a->oid, &b->oid))
Junio C Hamanofd48b462017-09-26 11:40:42 +09001405 result->clean = 0;
1406 break;
Elijah Newrenf3081da2019-08-17 11:41:42 -07001407 case MERGE_VARIANT_OURS:
Elijah Newren8daec1d2019-04-05 08:00:22 -07001408 oidcpy(&result->blob.oid, &a->oid);
Junio C Hamanofd48b462017-09-26 11:40:42 +09001409 break;
Elijah Newrenf3081da2019-08-17 11:41:42 -07001410 case MERGE_VARIANT_THEIRS:
Elijah Newren8daec1d2019-04-05 08:00:22 -07001411 oidcpy(&result->blob.oid, &b->oid);
Junio C Hamanofd48b462017-09-26 11:40:42 +09001412 break;
1413 }
Johannes Schindelinef1177d12016-07-26 18:05:50 +02001414 } else
Johannes Schindelin033abf92018-05-02 11:38:39 +02001415 BUG("unsupported object type in the tree");
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001416 }
1417
Elijah Newren05cf21e2018-04-19 10:58:22 -07001418 if (result->merge)
Elijah Newren259ccb62019-04-05 08:00:13 -07001419 output(opt, 2, _("Auto-merging %s"), filename);
Elijah Newren05cf21e2018-04-19 10:58:22 -07001420
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +02001421 return 0;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001422}
1423
Elijah Newren259ccb62019-04-05 08:00:13 -07001424static int handle_rename_via_dir(struct merge_options *opt,
Elijah Newrene2d563d2019-04-05 08:00:21 -07001425 struct rename_conflict_info *ci)
Elijah Newren9c0743f2018-04-19 10:58:10 -07001426{
Elijah Newren5455c332018-06-09 21:16:14 -07001427 /*
1428 * Handle file adds that need to be renamed due to directory rename
1429 * detection. This differs from handle_rename_normal, because
1430 * there is no content merge to do; just move the file into the
1431 * desired final location.
1432 */
Elijah Newrene2d563d2019-04-05 08:00:21 -07001433 const struct rename *ren = ci->ren1;
1434 const struct diff_filespec *dest = ren->pair->two;
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07001435 char *file_path = dest->path;
Derrick Stolee8e012512019-08-17 11:41:25 -07001436 int mark_conflicted = (opt->detect_directory_renames ==
1437 MERGE_DIRECTORY_RENAMES_CONFLICT);
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07001438 assert(ren->dir_rename_original_dest);
Elijah Newren9c0743f2018-04-19 10:58:10 -07001439
Elijah Newren5bf7e572019-08-17 11:41:41 -07001440 if (!opt->priv->call_depth && would_lose_untracked(opt, dest->path)) {
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07001441 mark_conflicted = 1;
1442 file_path = unique_path(opt, dest->path, ren->branch);
Elijah Newren259ccb62019-04-05 08:00:13 -07001443 output(opt, 1, _("Error: Refusing to lose untracked file at %s; "
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07001444 "writing to %s instead."),
1445 dest->path, file_path);
Elijah Newren79c47592018-04-19 10:58:11 -07001446 }
1447
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07001448 if (mark_conflicted) {
1449 /*
1450 * Write the file in worktree at file_path. In the index,
1451 * only record the file at dest->path in the appropriate
1452 * higher stage.
1453 */
1454 if (update_file(opt, 0, dest, file_path))
1455 return -1;
1456 if (file_path != dest->path)
1457 free(file_path);
1458 if (update_stages(opt, dest->path, NULL,
1459 ren->branch == opt->branch1 ? dest : NULL,
1460 ren->branch == opt->branch1 ? NULL : dest))
1461 return -1;
1462 return 0; /* not clean, but conflicted */
1463 } else {
1464 /* Update dest->path both in index and in worktree */
1465 if (update_file(opt, 1, dest, dest->path))
1466 return -1;
1467 return 1; /* clean */
1468 }
Elijah Newren6bdaead2011-08-11 23:20:12 -06001469}
1470
Elijah Newren259ccb62019-04-05 08:00:13 -07001471static int handle_change_delete(struct merge_options *opt,
Elijah Newrend90e7592018-06-09 21:16:12 -07001472 const char *path, const char *old_path,
Elijah Newren8daec1d2019-04-05 08:00:22 -07001473 const struct diff_filespec *o,
1474 const struct diff_filespec *changed,
Elijah Newrend90e7592018-06-09 21:16:12 -07001475 const char *change_branch,
1476 const char *delete_branch,
1477 const char *change, const char *change_past)
Elijah Newrenb7033252011-08-11 23:20:19 -06001478{
Matt McCutchenb26d87f2017-01-28 15:37:01 -05001479 char *alt_path = NULL;
1480 const char *update_path = path;
Johannes Schindelin75456f92016-07-26 18:06:21 +02001481 int ret = 0;
Matt McCutchenb26d87f2017-01-28 15:37:01 -05001482
Elijah Newren5bf7e572019-08-17 11:41:41 -07001483 if (dir_in_way(opt->repo->index, path, !opt->priv->call_depth, 0) ||
1484 (!opt->priv->call_depth && would_lose_untracked(opt, path))) {
Elijah Newren259ccb62019-04-05 08:00:13 -07001485 update_path = alt_path = unique_path(opt, path, change_branch);
Elijah Newrenb7033252011-08-11 23:20:19 -06001486 }
1487
Elijah Newren5bf7e572019-08-17 11:41:41 -07001488 if (opt->priv->call_depth) {
Elijah Newrenb7033252011-08-11 23:20:19 -06001489 /*
1490 * We cannot arbitrarily accept either a_sha or b_sha as
1491 * correct; since there is no true "middle point" between
1492 * them, simply reuse the base version for virtual merge base.
1493 */
Elijah Newren259ccb62019-04-05 08:00:13 -07001494 ret = remove_file_from_index(opt->repo->index, path);
Johannes Schindelin75456f92016-07-26 18:06:21 +02001495 if (!ret)
Elijah Newren8daec1d2019-04-05 08:00:22 -07001496 ret = update_file(opt, 0, o, update_path);
Elijah Newrenb7033252011-08-11 23:20:19 -06001497 } else {
Elijah Newren5b26c3c2018-06-09 21:16:16 -07001498 /*
1499 * Despite the four nearly duplicate messages and argument
1500 * lists below and the ugliness of the nested if-statements,
1501 * having complete messages makes the job easier for
1502 * translators.
1503 *
1504 * The slight variance among the cases is due to the fact
1505 * that:
1506 * 1) directory/file conflicts (in effect if
1507 * !alt_path) could cause us to need to write the
1508 * file to a different path.
1509 * 2) renames (in effect if !old_path) could mean that
1510 * there are two names for the path that the user
1511 * may know the file by.
1512 */
Matt McCutchenb26d87f2017-01-28 15:37:01 -05001513 if (!alt_path) {
1514 if (!old_path) {
Elijah Newren259ccb62019-04-05 08:00:13 -07001515 output(opt, 1, _("CONFLICT (%s/delete): %s deleted in %s "
Matt McCutchenb26d87f2017-01-28 15:37:01 -05001516 "and %s in %s. Version %s of %s left in tree."),
1517 change, path, delete_branch, change_past,
1518 change_branch, change_branch, path);
1519 } else {
Elijah Newren259ccb62019-04-05 08:00:13 -07001520 output(opt, 1, _("CONFLICT (%s/delete): %s deleted in %s "
Matt McCutchenb26d87f2017-01-28 15:37:01 -05001521 "and %s to %s in %s. Version %s of %s left in tree."),
1522 change, old_path, delete_branch, change_past, path,
1523 change_branch, change_branch, path);
1524 }
Jiang Xin55653a62012-07-25 22:53:13 +08001525 } else {
Matt McCutchenb26d87f2017-01-28 15:37:01 -05001526 if (!old_path) {
Elijah Newren259ccb62019-04-05 08:00:13 -07001527 output(opt, 1, _("CONFLICT (%s/delete): %s deleted in %s "
Matt McCutchenb26d87f2017-01-28 15:37:01 -05001528 "and %s in %s. Version %s of %s left in tree at %s."),
1529 change, path, delete_branch, change_past,
1530 change_branch, change_branch, path, alt_path);
1531 } else {
Elijah Newren259ccb62019-04-05 08:00:13 -07001532 output(opt, 1, _("CONFLICT (%s/delete): %s deleted in %s "
Matt McCutchenb26d87f2017-01-28 15:37:01 -05001533 "and %s to %s in %s. Version %s of %s left in tree at %s."),
1534 change, old_path, delete_branch, change_past, path,
1535 change_branch, change_branch, path, alt_path);
1536 }
Jiang Xin55653a62012-07-25 22:53:13 +08001537 }
Elijah Newren35a74ab2011-08-11 23:20:27 -06001538 /*
Matt McCutchenb26d87f2017-01-28 15:37:01 -05001539 * No need to call update_file() on path when change_branch ==
Elijah Newren259ccb62019-04-05 08:00:13 -07001540 * opt->branch1 && !alt_path, since that would needlessly touch
Matt McCutchenb26d87f2017-01-28 15:37:01 -05001541 * path. We could call update_file_flags() with update_cache=0
1542 * and update_wd=0, but that's a no-op.
Elijah Newren35a74ab2011-08-11 23:20:27 -06001543 */
Elijah Newren259ccb62019-04-05 08:00:13 -07001544 if (change_branch != opt->branch1 || alt_path)
Elijah Newren8daec1d2019-04-05 08:00:22 -07001545 ret = update_file(opt, 0, changed, update_path);
Elijah Newrenb7033252011-08-11 23:20:19 -06001546 }
Matt McCutchenb26d87f2017-01-28 15:37:01 -05001547 free(alt_path);
Johannes Schindelin75456f92016-07-26 18:06:21 +02001548
1549 return ret;
Elijah Newrenb7033252011-08-11 23:20:19 -06001550}
1551
Elijah Newren259ccb62019-04-05 08:00:13 -07001552static int handle_rename_delete(struct merge_options *opt,
Elijah Newrene2d563d2019-04-05 08:00:21 -07001553 struct rename_conflict_info *ci)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001554{
Elijah Newrene2d563d2019-04-05 08:00:21 -07001555 const struct rename *ren = ci->ren1;
1556 const struct diff_filespec *orig = ren->pair->one;
1557 const struct diff_filespec *dest = ren->pair->two;
1558 const char *rename_branch = ren->branch;
1559 const char *delete_branch = (opt->branch1 == ren->branch ?
1560 opt->branch2 : opt->branch1);
Elijah Newrene03acb82011-08-11 23:20:20 -06001561
Elijah Newren259ccb62019-04-05 08:00:13 -07001562 if (handle_change_delete(opt,
Elijah Newren5bf7e572019-08-17 11:41:41 -07001563 opt->priv->call_depth ? orig->path : dest->path,
1564 opt->priv->call_depth ? NULL : orig->path,
Elijah Newren8daec1d2019-04-05 08:00:22 -07001565 orig, dest,
Matt McCutchenb26d87f2017-01-28 15:37:01 -05001566 rename_branch, delete_branch,
Johannes Schindelin75456f92016-07-26 18:06:21 +02001567 _("rename"), _("renamed")))
1568 return -1;
Elijah Newrenf53d3972011-08-11 23:20:25 -06001569
Elijah Newren5bf7e572019-08-17 11:41:41 -07001570 if (opt->priv->call_depth)
Elijah Newren259ccb62019-04-05 08:00:13 -07001571 return remove_file_from_index(opt->repo->index, dest->path);
Johannes Schindelin75456f92016-07-26 18:06:21 +02001572 else
Elijah Newren259ccb62019-04-05 08:00:13 -07001573 return update_stages(opt, dest->path, NULL,
1574 rename_branch == opt->branch1 ? dest : NULL,
1575 rename_branch == opt->branch1 ? NULL : dest);
Elijah Newren6ef2cb02010-09-20 02:28:50 -06001576}
1577
Elijah Newren259ccb62019-04-05 08:00:13 -07001578static int handle_file_collision(struct merge_options *opt,
Elijah Newren37b65ce2018-11-07 20:40:25 -08001579 const char *collide_path,
1580 const char *prev_path1,
1581 const char *prev_path2,
1582 const char *branch1, const char *branch2,
Elijah Newren8daec1d2019-04-05 08:00:22 -07001583 struct diff_filespec *a,
1584 struct diff_filespec *b)
Elijah Newren3672c972011-08-11 23:20:22 -06001585{
Elijah Newren37b65ce2018-11-07 20:40:25 -08001586 struct merge_file_info mfi;
Elijah Newren8daec1d2019-04-05 08:00:22 -07001587 struct diff_filespec null;
Elijah Newren37b65ce2018-11-07 20:40:25 -08001588 char *alt_path = NULL;
1589 const char *update_path = collide_path;
Elijah Newren3672c972011-08-11 23:20:22 -06001590
Elijah Newren37b65ce2018-11-07 20:40:25 -08001591 /*
Elijah Newren7f867162018-11-07 20:40:26 -08001592 * It's easiest to get the correct things into stage 2 and 3, and
1593 * to make sure that the content merge puts HEAD before the other
Elijah Newren259ccb62019-04-05 08:00:13 -07001594 * branch if we just ensure that branch1 == opt->branch1. So, simply
Elijah Newren7f867162018-11-07 20:40:26 -08001595 * flip arguments around if we don't have that.
1596 */
Elijah Newren259ccb62019-04-05 08:00:13 -07001597 if (branch1 != opt->branch1) {
1598 return handle_file_collision(opt, collide_path,
Elijah Newren7f867162018-11-07 20:40:26 -08001599 prev_path2, prev_path1,
1600 branch2, branch1,
Elijah Newren8daec1d2019-04-05 08:00:22 -07001601 b, a);
Elijah Newren3672c972011-08-11 23:20:22 -06001602 }
1603
Elijah Newren37b65ce2018-11-07 20:40:25 -08001604 /* Remove rename sources if rename/add or rename/rename(2to1) */
1605 if (prev_path1)
Elijah Newren259ccb62019-04-05 08:00:13 -07001606 remove_file(opt, 1, prev_path1,
Elijah Newren5bf7e572019-08-17 11:41:41 -07001607 opt->priv->call_depth || would_lose_untracked(opt, prev_path1));
Elijah Newren37b65ce2018-11-07 20:40:25 -08001608 if (prev_path2)
Elijah Newren259ccb62019-04-05 08:00:13 -07001609 remove_file(opt, 1, prev_path2,
Elijah Newren5bf7e572019-08-17 11:41:41 -07001610 opt->priv->call_depth || would_lose_untracked(opt, prev_path2));
Elijah Newren37b65ce2018-11-07 20:40:25 -08001611
1612 /*
1613 * Remove the collision path, if it wouldn't cause dirty contents
1614 * or an untracked file to get lost. We'll either overwrite with
1615 * merged contents, or just write out to differently named files.
1616 */
Elijah Newren259ccb62019-04-05 08:00:13 -07001617 if (was_dirty(opt, collide_path)) {
1618 output(opt, 1, _("Refusing to lose dirty file at %s"),
Elijah Newren37b65ce2018-11-07 20:40:25 -08001619 collide_path);
Elijah Newren259ccb62019-04-05 08:00:13 -07001620 update_path = alt_path = unique_path(opt, collide_path, "merged");
1621 } else if (would_lose_untracked(opt, collide_path)) {
Elijah Newren18797a32018-04-19 10:58:13 -07001622 /*
Elijah Newren37b65ce2018-11-07 20:40:25 -08001623 * Only way we get here is if both renames were from
1624 * a directory rename AND user had an untracked file
1625 * at the location where both files end up after the
1626 * two directory renames. See testcase 10d of t6043.
Elijah Newren18797a32018-04-19 10:58:13 -07001627 */
Elijah Newren259ccb62019-04-05 08:00:13 -07001628 output(opt, 1, _("Refusing to lose untracked file at "
Elijah Newren37b65ce2018-11-07 20:40:25 -08001629 "%s, even though it's in the way."),
1630 collide_path);
Elijah Newren259ccb62019-04-05 08:00:13 -07001631 update_path = alt_path = unique_path(opt, collide_path, "merged");
Elijah Newren3672c972011-08-11 23:20:22 -06001632 } else {
Elijah Newren37b65ce2018-11-07 20:40:25 -08001633 /*
1634 * FIXME: It's possible that the two files are identical
1635 * and that the current working copy happens to match, in
1636 * which case we are unnecessarily touching the working
1637 * tree file. It's not a likely enough scenario that I
1638 * want to code up the checks for it and a better fix is
1639 * available if we restructure how unpack_trees() and
1640 * merge-recursive interoperate anyway, so punting for
1641 * now...
1642 */
Elijah Newren259ccb62019-04-05 08:00:13 -07001643 remove_file(opt, 0, collide_path, 0);
Elijah Newren3672c972011-08-11 23:20:22 -06001644 }
Elijah Newren3672c972011-08-11 23:20:22 -06001645
Elijah Newren37b65ce2018-11-07 20:40:25 -08001646 /* Store things in diff_filespecs for functions that need it */
Elijah Newren8daec1d2019-04-05 08:00:22 -07001647 null.path = (char *)collide_path;
brian m. carlson14228442021-04-26 01:02:56 +00001648 oidcpy(&null.oid, null_oid());
Elijah Newren37b65ce2018-11-07 20:40:25 -08001649 null.mode = 0;
Johannes Schindelin75456f92016-07-26 18:06:21 +02001650
Elijah Newren8daec1d2019-04-05 08:00:22 -07001651 if (merge_mode_and_contents(opt, &null, a, b, collide_path,
Elijah Newren5bf7e572019-08-17 11:41:41 -07001652 branch1, branch2, opt->priv->call_depth * 2, &mfi))
Elijah Newren37b65ce2018-11-07 20:40:25 -08001653 return -1;
1654 mfi.clean &= !alt_path;
Elijah Newren8daec1d2019-04-05 08:00:22 -07001655 if (update_file(opt, mfi.clean, &mfi.blob, update_path))
Elijah Newren37b65ce2018-11-07 20:40:25 -08001656 return -1;
Elijah Newren5bf7e572019-08-17 11:41:41 -07001657 if (!mfi.clean && !opt->priv->call_depth &&
Elijah Newren8daec1d2019-04-05 08:00:22 -07001658 update_stages(opt, collide_path, NULL, a, b))
Elijah Newren37b65ce2018-11-07 20:40:25 -08001659 return -1;
1660 free(alt_path);
1661 /*
1662 * FIXME: If both a & b both started with conflicts (only possible
1663 * if they came from a rename/rename(2to1)), but had IDENTICAL
1664 * contents including those conflicts, then in the next line we claim
1665 * it was clean. If someone cares about this case, we should have the
1666 * caller notify us if we started with conflicts.
1667 */
1668 return mfi.clean;
1669}
Elijah Newren7f867162018-11-07 20:40:26 -08001670
Elijah Newren259ccb62019-04-05 08:00:13 -07001671static int handle_rename_add(struct merge_options *opt,
Elijah Newren7f867162018-11-07 20:40:26 -08001672 struct rename_conflict_info *ci)
1673{
1674 /* a was renamed to c, and a separate c was added. */
Elijah Newrene9cd1b52019-04-05 08:00:18 -07001675 struct diff_filespec *a = ci->ren1->pair->one;
1676 struct diff_filespec *c = ci->ren1->pair->two;
Elijah Newren7f867162018-11-07 20:40:26 -08001677 char *path = c->path;
1678 char *prev_path_desc;
1679 struct merge_file_info mfi;
1680
Elijah Newrenc336ab82019-04-05 08:00:20 -07001681 const char *rename_branch = ci->ren1->branch;
1682 const char *add_branch = (opt->branch1 == rename_branch ?
1683 opt->branch2 : opt->branch1);
1684 int other_stage = (ci->ren1->branch == opt->branch1 ? 3 : 2);
Elijah Newren7f867162018-11-07 20:40:26 -08001685
Elijah Newren259ccb62019-04-05 08:00:13 -07001686 output(opt, 1, _("CONFLICT (rename/add): "
Elijah Newren7f867162018-11-07 20:40:26 -08001687 "Rename %s->%s in %s. Added %s in %s"),
Elijah Newrenc336ab82019-04-05 08:00:20 -07001688 a->path, c->path, rename_branch,
1689 c->path, add_branch);
Elijah Newren7f867162018-11-07 20:40:26 -08001690
1691 prev_path_desc = xstrfmt("version of %s from %s", path, a->path);
Elijah Newren481de8a2019-06-04 13:27:50 -07001692 ci->ren1->src_entry->stages[other_stage].path = a->path;
Elijah Newren8daec1d2019-04-05 08:00:22 -07001693 if (merge_mode_and_contents(opt, a, c,
1694 &ci->ren1->src_entry->stages[other_stage],
Elijah Newren3f9c92e2019-04-05 08:00:19 -07001695 prev_path_desc,
Elijah Newren259ccb62019-04-05 08:00:13 -07001696 opt->branch1, opt->branch2,
Elijah Newren5bf7e572019-08-17 11:41:41 -07001697 1 + opt->priv->call_depth * 2, &mfi))
Elijah Newren7f867162018-11-07 20:40:26 -08001698 return -1;
1699 free(prev_path_desc);
1700
Elijah Newren8daec1d2019-04-05 08:00:22 -07001701 ci->ren1->dst_entry->stages[other_stage].path = mfi.blob.path = c->path;
Elijah Newren259ccb62019-04-05 08:00:13 -07001702 return handle_file_collision(opt,
Elijah Newren7f867162018-11-07 20:40:26 -08001703 c->path, a->path, NULL,
Elijah Newrenc336ab82019-04-05 08:00:20 -07001704 rename_branch, add_branch,
Elijah Newren8daec1d2019-04-05 08:00:22 -07001705 &mfi.blob,
1706 &ci->ren1->dst_entry->stages[other_stage]);
Elijah Newren7f867162018-11-07 20:40:26 -08001707}
Elijah Newren37b65ce2018-11-07 20:40:25 -08001708
Elijah Newren259ccb62019-04-05 08:00:13 -07001709static char *find_path_for_conflict(struct merge_options *opt,
Derrick Stolee80cee6e2018-11-07 20:40:31 -08001710 const char *path,
1711 const char *branch1,
1712 const char *branch2)
1713{
1714 char *new_path = NULL;
Elijah Newren5bf7e572019-08-17 11:41:41 -07001715 if (dir_in_way(opt->repo->index, path, !opt->priv->call_depth, 0)) {
Elijah Newren259ccb62019-04-05 08:00:13 -07001716 new_path = unique_path(opt, path, branch1);
1717 output(opt, 1, _("%s is a directory in %s adding "
Derrick Stolee80cee6e2018-11-07 20:40:31 -08001718 "as %s instead"),
1719 path, branch2, new_path);
Elijah Newren259ccb62019-04-05 08:00:13 -07001720 } else if (would_lose_untracked(opt, path)) {
1721 new_path = unique_path(opt, path, branch1);
1722 output(opt, 1, _("Refusing to lose untracked file"
Derrick Stolee80cee6e2018-11-07 20:40:31 -08001723 " at %s; adding as %s instead"),
1724 path, new_path);
1725 }
1726
1727 return new_path;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001728}
1729
Jeff Kingee798742020-01-25 00:37:23 -05001730/*
Junio C Hamano4c616c22020-01-25 18:57:45 -05001731 * Toggle the stage number between "ours" and "theirs" (2 and 3).
Jeff Kingee798742020-01-25 00:37:23 -05001732 */
1733static inline int flip_stage(int stage)
1734{
Junio C Hamano4c616c22020-01-25 18:57:45 -05001735 return (2 + 3) - stage;
Jeff Kingee798742020-01-25 00:37:23 -05001736}
1737
Elijah Newren259ccb62019-04-05 08:00:13 -07001738static int handle_rename_rename_1to2(struct merge_options *opt,
Elijah Newren8ebe7b02018-06-09 21:16:15 -07001739 struct rename_conflict_info *ci)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001740{
Elijah Newren09c01f82010-09-20 02:28:48 -06001741 /* One file was renamed in both branches, but to different names. */
Elijah Newren48c9cb92018-11-07 20:40:29 -08001742 struct merge_file_info mfi;
Elijah Newren48c9cb92018-11-07 20:40:29 -08001743 struct diff_filespec *add;
Elijah Newrene9cd1b52019-04-05 08:00:18 -07001744 struct diff_filespec *o = ci->ren1->pair->one;
1745 struct diff_filespec *a = ci->ren1->pair->two;
1746 struct diff_filespec *b = ci->ren2->pair->two;
Elijah Newren48c9cb92018-11-07 20:40:29 -08001747 char *path_desc;
Elijah Newren07413c52010-09-20 02:29:00 -06001748
Elijah Newren259ccb62019-04-05 08:00:13 -07001749 output(opt, 1, _("CONFLICT (rename/rename): "
Elijah Newren4f66dad2011-08-11 23:20:08 -06001750 "Rename \"%s\"->\"%s\" in branch \"%s\" "
Jiang Xin55653a62012-07-25 22:53:13 +08001751 "rename \"%s\"->\"%s\" in \"%s\"%s"),
Elijah Newrenc336ab82019-04-05 08:00:20 -07001752 o->path, a->path, ci->ren1->branch,
1753 o->path, b->path, ci->ren2->branch,
Elijah Newren5bf7e572019-08-17 11:41:41 -07001754 opt->priv->call_depth ? _(" (left unresolved)") : "");
Johannes Schindelin75456f92016-07-26 18:06:21 +02001755
Elijah Newren48c9cb92018-11-07 20:40:29 -08001756 path_desc = xstrfmt("%s and %s, both renamed from %s",
Elijah Newrene3de8882019-04-05 08:00:14 -07001757 a->path, b->path, o->path);
1758 if (merge_mode_and_contents(opt, o, a, b, path_desc,
Elijah Newrenc336ab82019-04-05 08:00:20 -07001759 ci->ren1->branch, ci->ren2->branch,
Elijah Newren5bf7e572019-08-17 11:41:41 -07001760 opt->priv->call_depth * 2, &mfi))
Elijah Newren48c9cb92018-11-07 20:40:29 -08001761 return -1;
1762 free(path_desc);
1763
Elijah Newren80205042020-02-27 00:05:05 +00001764 if (opt->priv->call_depth)
1765 remove_file_from_index(opt->repo->index, o->path);
1766
1767 /*
1768 * For each destination path, we need to see if there is a
1769 * rename/add collision. If not, we can write the file out
1770 * to the specified location.
1771 */
1772 add = &ci->ren1->dst_entry->stages[flip_stage(2)];
1773 if (is_valid(add)) {
1774 add->path = mfi.blob.path = a->path;
1775 if (handle_file_collision(opt, a->path,
1776 NULL, NULL,
1777 ci->ren1->branch,
1778 ci->ren2->branch,
1779 &mfi.blob, add) < 0)
Johannes Schindelin75456f92016-07-26 18:06:21 +02001780 return -1;
Elijah Newren48c9cb92018-11-07 20:40:29 -08001781 } else {
Elijah Newren80205042020-02-27 00:05:05 +00001782 char *new_path = find_path_for_conflict(opt, a->path,
1783 ci->ren1->branch,
1784 ci->ren2->branch);
1785 if (update_file(opt, 0, &mfi.blob,
1786 new_path ? new_path : a->path))
1787 return -1;
1788 free(new_path);
1789 if (!opt->priv->call_depth &&
1790 update_stages(opt, a->path, NULL, a, NULL))
1791 return -1;
1792 }
Elijah Newren48c9cb92018-11-07 20:40:29 -08001793
Elijah Newren95983da2020-05-13 23:56:32 +00001794 if (!mfi.clean && mfi.blob.mode == a->mode &&
1795 oideq(&mfi.blob.oid, &a->oid)) {
1796 /*
1797 * Getting here means we were attempting to merge a binary
1798 * blob. Since we can't merge binaries, the merge algorithm
1799 * just takes one side. But we don't want to copy the
1800 * contents of one side to both paths; we'd rather use the
1801 * original content at the given path for each path.
1802 */
1803 oidcpy(&mfi.blob.oid, &b->oid);
1804 mfi.blob.mode = b->mode;
1805 }
Elijah Newren80205042020-02-27 00:05:05 +00001806 add = &ci->ren2->dst_entry->stages[flip_stage(3)];
1807 if (is_valid(add)) {
1808 add->path = mfi.blob.path = b->path;
1809 if (handle_file_collision(opt, b->path,
1810 NULL, NULL,
1811 ci->ren1->branch,
1812 ci->ren2->branch,
1813 add, &mfi.blob) < 0)
1814 return -1;
1815 } else {
1816 char *new_path = find_path_for_conflict(opt, b->path,
1817 ci->ren2->branch,
1818 ci->ren1->branch);
1819 if (update_file(opt, 0, &mfi.blob,
1820 new_path ? new_path : b->path))
1821 return -1;
1822 free(new_path);
1823 if (!opt->priv->call_depth &&
1824 update_stages(opt, b->path, NULL, NULL, b))
1825 return -1;
Elijah Newren48c9cb92018-11-07 20:40:29 -08001826 }
Johannes Schindelin75456f92016-07-26 18:06:21 +02001827
1828 return 0;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001829}
1830
Elijah Newren259ccb62019-04-05 08:00:13 -07001831static int handle_rename_rename_2to1(struct merge_options *opt,
Elijah Newren8ebe7b02018-06-09 21:16:15 -07001832 struct rename_conflict_info *ci)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001833{
Elijah Newren461f5042011-08-11 23:20:15 -06001834 /* Two files, a & b, were renamed to the same thing, c. */
Elijah Newrene9cd1b52019-04-05 08:00:18 -07001835 struct diff_filespec *a = ci->ren1->pair->one;
1836 struct diff_filespec *b = ci->ren2->pair->one;
1837 struct diff_filespec *c1 = ci->ren1->pair->two;
1838 struct diff_filespec *c2 = ci->ren2->pair->two;
Elijah Newren461f5042011-08-11 23:20:15 -06001839 char *path = c1->path; /* == c2->path */
Elijah Newren05cf21e2018-04-19 10:58:22 -07001840 char *path_side_1_desc;
1841 char *path_side_2_desc;
Elijah Newren434b8522011-08-11 23:20:18 -06001842 struct merge_file_info mfi_c1;
1843 struct merge_file_info mfi_c2;
Elijah Newren8daec1d2019-04-05 08:00:22 -07001844 int ostage1, ostage2;
Elijah Newren461f5042011-08-11 23:20:15 -06001845
Elijah Newren259ccb62019-04-05 08:00:13 -07001846 output(opt, 1, _("CONFLICT (rename/rename): "
Elijah Newren461f5042011-08-11 23:20:15 -06001847 "Rename %s->%s in %s. "
Jiang Xin55653a62012-07-25 22:53:13 +08001848 "Rename %s->%s in %s"),
Elijah Newrenc336ab82019-04-05 08:00:20 -07001849 a->path, c1->path, ci->ren1->branch,
1850 b->path, c2->path, ci->ren2->branch);
Elijah Newren461f5042011-08-11 23:20:15 -06001851
Elijah Newren2b168ef2018-10-16 13:19:47 -07001852 path_side_1_desc = xstrfmt("version of %s from %s", path, a->path);
1853 path_side_2_desc = xstrfmt("version of %s from %s", path, b->path);
Elijah Newren8daec1d2019-04-05 08:00:22 -07001854 ostage1 = ci->ren1->branch == opt->branch1 ? 3 : 2;
Jeff Kingee798742020-01-25 00:37:23 -05001855 ostage2 = flip_stage(ostage1);
Elijah Newren8daec1d2019-04-05 08:00:22 -07001856 ci->ren1->src_entry->stages[ostage1].path = a->path;
1857 ci->ren2->src_entry->stages[ostage2].path = b->path;
1858 if (merge_mode_and_contents(opt, a, c1,
1859 &ci->ren1->src_entry->stages[ostage1],
1860 path_side_1_desc,
Elijah Newren259ccb62019-04-05 08:00:13 -07001861 opt->branch1, opt->branch2,
Elijah Newren5bf7e572019-08-17 11:41:41 -07001862 1 + opt->priv->call_depth * 2, &mfi_c1) ||
Elijah Newren8daec1d2019-04-05 08:00:22 -07001863 merge_mode_and_contents(opt, b,
1864 &ci->ren2->src_entry->stages[ostage2],
1865 c2, path_side_2_desc,
Elijah Newren259ccb62019-04-05 08:00:13 -07001866 opt->branch1, opt->branch2,
Elijah Newren5bf7e572019-08-17 11:41:41 -07001867 1 + opt->priv->call_depth * 2, &mfi_c2))
Johannes Schindelin75456f92016-07-26 18:06:21 +02001868 return -1;
Elijah Newren05cf21e2018-04-19 10:58:22 -07001869 free(path_side_1_desc);
1870 free(path_side_2_desc);
Elijah Newren8daec1d2019-04-05 08:00:22 -07001871 mfi_c1.blob.path = path;
1872 mfi_c2.blob.path = path;
Elijah Newren434b8522011-08-11 23:20:18 -06001873
Elijah Newren259ccb62019-04-05 08:00:13 -07001874 return handle_file_collision(opt, path, a->path, b->path,
Elijah Newrenc336ab82019-04-05 08:00:20 -07001875 ci->ren1->branch, ci->ren2->branch,
Elijah Newren8daec1d2019-04-05 08:00:22 -07001876 &mfi_c1.blob, &mfi_c2.blob);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001877}
1878
Elijah Newren9ba91552018-04-19 10:57:59 -07001879/*
Elijah Newrene5257b22018-04-19 10:58:03 -07001880 * Get the diff_filepairs changed between o_tree and tree.
Elijah Newren9ba91552018-04-19 10:57:59 -07001881 */
Elijah Newren259ccb62019-04-05 08:00:13 -07001882static struct diff_queue_struct *get_diffpairs(struct merge_options *opt,
Elijah Newrene5257b22018-04-19 10:58:03 -07001883 struct tree *o_tree,
1884 struct tree *tree)
Elijah Newren9ba91552018-04-19 10:57:59 -07001885{
Elijah Newrene5257b22018-04-19 10:58:03 -07001886 struct diff_queue_struct *ret;
Elijah Newren9ba91552018-04-19 10:57:59 -07001887 struct diff_options opts;
1888
Elijah Newren259ccb62019-04-05 08:00:13 -07001889 repo_diff_setup(opt->repo, &opts);
Elijah Newren9ba91552018-04-19 10:57:59 -07001890 opts.flags.recursive = 1;
1891 opts.flags.rename_empty = 0;
Elijah Newren259ccb62019-04-05 08:00:13 -07001892 opts.detect_rename = merge_detect_rename(opt);
Ben Peart85b46032018-05-02 16:01:14 +00001893 /*
1894 * We do not have logic to handle the detection of copies. In
1895 * fact, it may not even make sense to add such logic: would we
1896 * really want a change to a base file to be propagated through
1897 * multiple other files by a merge?
1898 */
1899 if (opts.detect_rename > DIFF_DETECT_RENAME)
1900 opts.detect_rename = DIFF_DETECT_RENAME;
Elijah Newren94b82d52021-07-15 00:45:24 +00001901 opts.rename_limit = (opt->rename_limit >= 0) ? opt->rename_limit : 7000;
Elijah Newren259ccb62019-04-05 08:00:13 -07001902 opts.rename_score = opt->rename_score;
1903 opts.show_rename_progress = opt->show_rename_progress;
Elijah Newren9ba91552018-04-19 10:57:59 -07001904 opts.output_format = DIFF_FORMAT_NO_OUTPUT;
1905 diff_setup_done(&opts);
1906 diff_tree_oid(&o_tree->object.oid, &tree->object.oid, "", &opts);
1907 diffcore_std(&opts);
Elijah Newren5bf7e572019-08-17 11:41:41 -07001908 if (opts.needed_rename_limit > opt->priv->needed_rename_limit)
1909 opt->priv->needed_rename_limit = opts.needed_rename_limit;
Elijah Newrene5257b22018-04-19 10:58:03 -07001910
1911 ret = xmalloc(sizeof(*ret));
1912 *ret = diff_queued_diff;
1913
1914 opts.output_format = DIFF_FORMAT_NO_OUTPUT;
1915 diff_queued_diff.nr = 0;
1916 diff_queued_diff.queue = NULL;
1917 diff_flush(&opts);
1918 return ret;
1919}
1920
Nguyễn Thái Ngọc Duy34e77712019-06-27 16:28:52 +07001921static int tree_has_path(struct repository *r, struct tree *tree,
1922 const char *path)
Elijah Newren96e7ffb2018-04-19 10:58:06 -07001923{
1924 struct object_id hashy;
Elijah Newren5ec1e722019-04-05 08:00:12 -07001925 unsigned short mode_o;
Elijah Newren96e7ffb2018-04-19 10:58:06 -07001926
Nguyễn Thái Ngọc Duy34e77712019-06-27 16:28:52 +07001927 return !get_tree_entry(r,
Nguyễn Thái Ngọc Duy50ddb082019-06-27 16:28:49 +07001928 &tree->object.oid, path,
Elijah Newren96e7ffb2018-04-19 10:58:06 -07001929 &hashy, &mode_o);
1930}
1931
Elijah Newrene95ab702018-04-19 10:58:07 -07001932/*
1933 * Return a new string that replaces the beginning portion (which matches
1934 * entry->dir), with entry->new_dir. In perl-speak:
1935 * new_path_name = (old_path =~ s/entry->dir/entry->new_dir/);
1936 * NOTE:
1937 * Caller must ensure that old_path starts with entry->dir + '/'.
1938 */
1939static char *apply_dir_rename(struct dir_rename_entry *entry,
1940 const char *old_path)
1941{
1942 struct strbuf new_path = STRBUF_INIT;
1943 int oldlen, newlen;
1944
1945 if (entry->non_unique_new_dir)
1946 return NULL;
1947
1948 oldlen = strlen(entry->dir);
Elijah Newren49b8133a2019-10-22 21:22:50 +00001949 if (entry->new_dir.len == 0)
1950 /*
1951 * If someone renamed/merged a subdirectory into the root
1952 * directory (e.g. 'some/subdir' -> ''), then we want to
1953 * avoid returning
1954 * '' + '/filename'
1955 * as the rename; we need to make old_path + oldlen advance
1956 * past the '/' character.
1957 */
1958 oldlen++;
Elijah Newrene95ab702018-04-19 10:58:07 -07001959 newlen = entry->new_dir.len + (strlen(old_path) - oldlen) + 1;
1960 strbuf_grow(&new_path, newlen);
1961 strbuf_addbuf(&new_path, &entry->new_dir);
1962 strbuf_addstr(&new_path, &old_path[oldlen]);
1963
1964 return strbuf_detach(&new_path, NULL);
1965}
1966
Elijah Newren7fe40b82018-04-19 10:58:05 -07001967static void get_renamed_dir_portion(const char *old_path, const char *new_path,
1968 char **old_dir, char **new_dir)
1969{
1970 char *end_of_old, *end_of_new;
Elijah Newren7fe40b82018-04-19 10:58:05 -07001971
Elijah Newrend3eebaa2019-10-22 21:22:49 +00001972 /* Default return values: NULL, meaning no rename */
Elijah Newren7fe40b82018-04-19 10:58:05 -07001973 *old_dir = NULL;
1974 *new_dir = NULL;
1975
1976 /*
1977 * For
1978 * "a/b/c/d/e/foo.c" -> "a/b/some/thing/else/e/foo.c"
1979 * the "e/foo.c" part is the same, we just want to know that
1980 * "a/b/c/d" was renamed to "a/b/some/thing/else"
1981 * so, for this example, this function returns "a/b/c/d" in
1982 * *old_dir and "a/b/some/thing/else" in *new_dir.
Elijah Newrend3eebaa2019-10-22 21:22:49 +00001983 */
1984
1985 /*
1986 * If the basename of the file changed, we don't care. We want
1987 * to know which portion of the directory, if any, changed.
Elijah Newren7fe40b82018-04-19 10:58:05 -07001988 */
1989 end_of_old = strrchr(old_path, '/');
1990 end_of_new = strrchr(new_path, '/');
1991
Elijah Newren49b8133a2019-10-22 21:22:50 +00001992 /*
1993 * If end_of_old is NULL, old_path wasn't in a directory, so there
1994 * could not be a directory rename (our rule elsewhere that a
1995 * directory which still exists is not considered to have been
1996 * renamed means the root directory can never be renamed -- because
1997 * the root directory always exists).
1998 */
Junio C Hamanoafe8a902022-05-02 09:50:37 -07001999 if (!end_of_old)
Elijah Newren49b8133a2019-10-22 21:22:50 +00002000 return; /* Note: *old_dir and *new_dir are still NULL */
2001
2002 /*
2003 * If new_path contains no directory (end_of_new is NULL), then we
2004 * have a rename of old_path's directory to the root directory.
2005 */
Junio C Hamanoafe8a902022-05-02 09:50:37 -07002006 if (!end_of_new) {
Elijah Newren49b8133a2019-10-22 21:22:50 +00002007 *old_dir = xstrndup(old_path, end_of_old - old_path);
2008 *new_dir = xstrdup("");
Elijah Newren7fe40b82018-04-19 10:58:05 -07002009 return;
Elijah Newren49b8133a2019-10-22 21:22:50 +00002010 }
Elijah Newrend3eebaa2019-10-22 21:22:49 +00002011
2012 /* Find the first non-matching character traversing backwards */
Elijah Newren7fe40b82018-04-19 10:58:05 -07002013 while (*--end_of_new == *--end_of_old &&
2014 end_of_old != old_path &&
2015 end_of_new != new_path)
2016 ; /* Do nothing; all in the while loop */
Elijah Newrend3eebaa2019-10-22 21:22:49 +00002017
2018 /*
2019 * If both got back to the beginning of their strings, then the
2020 * directory didn't change at all, only the basename did.
2021 */
2022 if (end_of_old == old_path && end_of_new == new_path &&
2023 *end_of_old == *end_of_new)
Elijah Newren49b8133a2019-10-22 21:22:50 +00002024 return; /* Note: *old_dir and *new_dir are still NULL */
2025
2026 /*
2027 * If end_of_new got back to the beginning of its string, and
2028 * end_of_old got back to the beginning of some subdirectory, then
2029 * we have a rename/merge of a subdirectory into the root, which
2030 * needs slightly special handling.
2031 *
2032 * Note: There is no need to consider the opposite case, with a
2033 * rename/merge of the root directory into some subdirectory
2034 * because as noted above the root directory always exists so it
2035 * cannot be considered to be renamed.
2036 */
2037 if (end_of_new == new_path &&
2038 end_of_old != old_path && end_of_old[-1] == '/') {
2039 *old_dir = xstrndup(old_path, --end_of_old - old_path);
2040 *new_dir = xstrdup("");
2041 return;
2042 }
Elijah Newrend3eebaa2019-10-22 21:22:49 +00002043
Elijah Newren7fe40b82018-04-19 10:58:05 -07002044 /*
2045 * We've found the first non-matching character in the directory
Elijah Newrend3eebaa2019-10-22 21:22:49 +00002046 * paths. That means the current characters we were looking at
2047 * were part of the first non-matching subdir name going back from
2048 * the end of the strings. Get the whole name by advancing both
2049 * end_of_old and end_of_new to the NEXT '/' character. That will
2050 * represent the entire directory rename.
2051 *
2052 * The reason for the increment is cases like
2053 * a/b/star/foo/whatever.c -> a/b/tar/foo/random.c
2054 * After dropping the basename and going back to the first
2055 * non-matching character, we're now comparing:
2056 * a/b/s and a/b/
2057 * and we want to be comparing:
2058 * a/b/star/ and a/b/tar/
2059 * but without the pre-increment, the one on the right would stay
2060 * a/b/.
Elijah Newren7fe40b82018-04-19 10:58:05 -07002061 */
Elijah Newrend3eebaa2019-10-22 21:22:49 +00002062 end_of_old = strchr(++end_of_old, '/');
2063 end_of_new = strchr(++end_of_new, '/');
Elijah Newren7fe40b82018-04-19 10:58:05 -07002064
Elijah Newrend3eebaa2019-10-22 21:22:49 +00002065 /* Copy the old and new directories into *old_dir and *new_dir. */
2066 *old_dir = xstrndup(old_path, end_of_old - old_path);
2067 *new_dir = xstrndup(new_path, end_of_new - new_path);
Elijah Newren7fe40b82018-04-19 10:58:05 -07002068}
2069
Elijah Newren96e7ffb2018-04-19 10:58:06 -07002070static void remove_hashmap_entries(struct hashmap *dir_renames,
2071 struct string_list *items_to_remove)
2072{
2073 int i;
2074 struct dir_rename_entry *entry;
2075
2076 for (i = 0; i < items_to_remove->nr; i++) {
2077 entry = items_to_remove->items[i].util;
Eric Wong28ee7942019-10-06 23:30:31 +00002078 hashmap_remove(dir_renames, &entry->ent, NULL);
Elijah Newren96e7ffb2018-04-19 10:58:06 -07002079 }
2080 string_list_clear(items_to_remove, 0);
2081}
2082
2083/*
Elijah Newrenf6f77552018-04-19 10:58:08 -07002084 * See if there is a directory rename for path, and if there are any file
2085 * level conflicts for the renamed location. If there is a rename and
2086 * there are no conflicts, return the new name. Otherwise, return NULL.
2087 */
Elijah Newren259ccb62019-04-05 08:00:13 -07002088static char *handle_path_level_conflicts(struct merge_options *opt,
Elijah Newrenf6f77552018-04-19 10:58:08 -07002089 const char *path,
2090 struct dir_rename_entry *entry,
2091 struct hashmap *collisions,
2092 struct tree *tree)
2093{
2094 char *new_path = NULL;
2095 struct collision_entry *collision_ent;
2096 int clean = 1;
2097 struct strbuf collision_paths = STRBUF_INIT;
2098
2099 /*
2100 * entry has the mapping of old directory name to new directory name
2101 * that we want to apply to path.
2102 */
2103 new_path = apply_dir_rename(entry, path);
2104
2105 if (!new_path) {
2106 /* This should only happen when entry->non_unique_new_dir set */
2107 if (!entry->non_unique_new_dir)
Kyle Meyer42db3242022-11-25 12:37:45 -05002108 BUG("entry->non_unique_new_dir not set and !new_path");
Elijah Newren259ccb62019-04-05 08:00:13 -07002109 output(opt, 1, _("CONFLICT (directory rename split): "
Elijah Newrenf6f77552018-04-19 10:58:08 -07002110 "Unclear where to place %s because directory "
2111 "%s was renamed to multiple other directories, "
2112 "with no destination getting a majority of the "
2113 "files."),
2114 path, entry->dir);
2115 clean = 0;
2116 return NULL;
2117 }
2118
2119 /*
2120 * The caller needs to have ensured that it has pre-populated
2121 * collisions with all paths that map to new_path. Do a quick check
2122 * to ensure that's the case.
2123 */
2124 collision_ent = collision_find_entry(collisions, new_path);
Junio C Hamanoafe8a902022-05-02 09:50:37 -07002125 if (!collision_ent)
Elijah Newrenf6f77552018-04-19 10:58:08 -07002126 BUG("collision_ent is NULL");
2127
2128 /*
2129 * Check for one-sided add/add/.../add conflicts, i.e.
2130 * where implicit renames from the other side doing
2131 * directory rename(s) can affect this side of history
2132 * to put multiple paths into the same location. Warn
2133 * and bail on directory renames for such paths.
2134 */
2135 if (collision_ent->reported_already) {
2136 clean = 0;
Nguyễn Thái Ngọc Duy34e77712019-06-27 16:28:52 +07002137 } else if (tree_has_path(opt->repo, tree, new_path)) {
Elijah Newrenf6f77552018-04-19 10:58:08 -07002138 collision_ent->reported_already = 1;
2139 strbuf_add_separated_string_list(&collision_paths, ", ",
2140 &collision_ent->source_files);
Elijah Newren259ccb62019-04-05 08:00:13 -07002141 output(opt, 1, _("CONFLICT (implicit dir rename): Existing "
Elijah Newrenf6f77552018-04-19 10:58:08 -07002142 "file/dir at %s in the way of implicit "
2143 "directory rename(s) putting the following "
2144 "path(s) there: %s."),
2145 new_path, collision_paths.buf);
2146 clean = 0;
2147 } else if (collision_ent->source_files.nr > 1) {
2148 collision_ent->reported_already = 1;
2149 strbuf_add_separated_string_list(&collision_paths, ", ",
2150 &collision_ent->source_files);
Elijah Newren259ccb62019-04-05 08:00:13 -07002151 output(opt, 1, _("CONFLICT (implicit dir rename): Cannot map "
Elijah Newrenf6f77552018-04-19 10:58:08 -07002152 "more than one path to %s; implicit directory "
2153 "renames tried to put these paths there: %s"),
2154 new_path, collision_paths.buf);
2155 clean = 0;
2156 }
2157
2158 /* Free memory we no longer need */
2159 strbuf_release(&collision_paths);
2160 if (!clean && new_path) {
2161 free(new_path);
2162 return NULL;
2163 }
2164
2165 return new_path;
2166}
2167
2168/*
Elijah Newren96e7ffb2018-04-19 10:58:06 -07002169 * There are a couple things we want to do at the directory level:
2170 * 1. Check for both sides renaming to the same thing, in order to avoid
2171 * implicit renaming of files that should be left in place. (See
2172 * testcase 6b in t6043 for details.)
2173 * 2. Prune directory renames if there are still files left in the
Andrei Rybakabcb66c2021-06-11 13:18:50 +02002174 * original directory. These represent a partial directory rename,
Elijah Newren96e7ffb2018-04-19 10:58:06 -07002175 * i.e. a rename where only some of the files within the directory
2176 * were renamed elsewhere. (Technically, this could be done earlier
2177 * in get_directory_renames(), except that would prevent us from
2178 * doing the previous check and thus failing testcase 6b.)
2179 * 3. Check for rename/rename(1to2) conflicts (at the directory level).
2180 * In the future, we could potentially record this info as well and
2181 * omit reporting rename/rename(1to2) conflicts for each path within
2182 * the affected directories, thus cleaning up the merge output.
2183 * NOTE: We do NOT check for rename/rename(2to1) conflicts at the
2184 * directory level, because merging directories is fine. If it
2185 * causes conflicts for files within those merged directories, then
2186 * that should be detected at the individual path level.
2187 */
Elijah Newren259ccb62019-04-05 08:00:13 -07002188static void handle_directory_level_conflicts(struct merge_options *opt,
Elijah Newren96e7ffb2018-04-19 10:58:06 -07002189 struct hashmap *dir_re_head,
2190 struct tree *head,
2191 struct hashmap *dir_re_merge,
2192 struct tree *merge)
2193{
2194 struct hashmap_iter iter;
2195 struct dir_rename_entry *head_ent;
2196 struct dir_rename_entry *merge_ent;
2197
2198 struct string_list remove_from_head = STRING_LIST_INIT_NODUP;
2199 struct string_list remove_from_merge = STRING_LIST_INIT_NODUP;
2200
Eric Wong87571c32019-10-06 23:30:38 +00002201 hashmap_for_each_entry(dir_re_head, &iter, head_ent,
Eric Wong87571c32019-10-06 23:30:38 +00002202 ent /* member name */) {
Elijah Newren96e7ffb2018-04-19 10:58:06 -07002203 merge_ent = dir_rename_find_entry(dir_re_merge, head_ent->dir);
2204 if (merge_ent &&
2205 !head_ent->non_unique_new_dir &&
2206 !merge_ent->non_unique_new_dir &&
2207 !strbuf_cmp(&head_ent->new_dir, &merge_ent->new_dir)) {
2208 /* 1. Renamed identically; remove it from both sides */
2209 string_list_append(&remove_from_head,
2210 head_ent->dir)->util = head_ent;
2211 strbuf_release(&head_ent->new_dir);
2212 string_list_append(&remove_from_merge,
2213 merge_ent->dir)->util = merge_ent;
2214 strbuf_release(&merge_ent->new_dir);
Nguyễn Thái Ngọc Duy34e77712019-06-27 16:28:52 +07002215 } else if (tree_has_path(opt->repo, head, head_ent->dir)) {
Elijah Newren96e7ffb2018-04-19 10:58:06 -07002216 /* 2. This wasn't a directory rename after all */
2217 string_list_append(&remove_from_head,
2218 head_ent->dir)->util = head_ent;
2219 strbuf_release(&head_ent->new_dir);
2220 }
2221 }
2222
2223 remove_hashmap_entries(dir_re_head, &remove_from_head);
2224 remove_hashmap_entries(dir_re_merge, &remove_from_merge);
2225
Eric Wong87571c32019-10-06 23:30:38 +00002226 hashmap_for_each_entry(dir_re_merge, &iter, merge_ent,
Eric Wong87571c32019-10-06 23:30:38 +00002227 ent /* member name */) {
Elijah Newren96e7ffb2018-04-19 10:58:06 -07002228 head_ent = dir_rename_find_entry(dir_re_head, merge_ent->dir);
Nguyễn Thái Ngọc Duy34e77712019-06-27 16:28:52 +07002229 if (tree_has_path(opt->repo, merge, merge_ent->dir)) {
Elijah Newren96e7ffb2018-04-19 10:58:06 -07002230 /* 2. This wasn't a directory rename after all */
2231 string_list_append(&remove_from_merge,
2232 merge_ent->dir)->util = merge_ent;
2233 } else if (head_ent &&
2234 !head_ent->non_unique_new_dir &&
2235 !merge_ent->non_unique_new_dir) {
2236 /* 3. rename/rename(1to2) */
2237 /*
2238 * We can assume it's not rename/rename(1to1) because
2239 * that was case (1), already checked above. So we
2240 * know that head_ent->new_dir and merge_ent->new_dir
2241 * are different strings.
2242 */
Elijah Newren259ccb62019-04-05 08:00:13 -07002243 output(opt, 1, _("CONFLICT (rename/rename): "
Elijah Newren96e7ffb2018-04-19 10:58:06 -07002244 "Rename directory %s->%s in %s. "
2245 "Rename directory %s->%s in %s"),
Elijah Newren259ccb62019-04-05 08:00:13 -07002246 head_ent->dir, head_ent->new_dir.buf, opt->branch1,
2247 head_ent->dir, merge_ent->new_dir.buf, opt->branch2);
Elijah Newren96e7ffb2018-04-19 10:58:06 -07002248 string_list_append(&remove_from_head,
2249 head_ent->dir)->util = head_ent;
2250 strbuf_release(&head_ent->new_dir);
2251 string_list_append(&remove_from_merge,
2252 merge_ent->dir)->util = merge_ent;
2253 strbuf_release(&merge_ent->new_dir);
2254 }
2255 }
2256
2257 remove_hashmap_entries(dir_re_head, &remove_from_head);
2258 remove_hashmap_entries(dir_re_merge, &remove_from_merge);
2259}
2260
Jeff Kingb53c5022019-02-14 00:50:02 -05002261static struct hashmap *get_directory_renames(struct diff_queue_struct *pairs)
Elijah Newren7fe40b82018-04-19 10:58:05 -07002262{
2263 struct hashmap *dir_renames;
2264 struct hashmap_iter iter;
2265 struct dir_rename_entry *entry;
2266 int i;
2267
2268 /*
2269 * Typically, we think of a directory rename as all files from a
2270 * certain directory being moved to a target directory. However,
2271 * what if someone first moved two files from the original
2272 * directory in one commit, and then renamed the directory
2273 * somewhere else in a later commit? At merge time, we just know
2274 * that files from the original directory went to two different
2275 * places, and that the bulk of them ended up in the same place.
2276 * We want each directory rename to represent where the bulk of the
2277 * files from that directory end up; this function exists to find
2278 * where the bulk of the files went.
2279 *
2280 * The first loop below simply iterates through the list of file
2281 * renames, finding out how often each directory rename pair
2282 * possibility occurs.
2283 */
2284 dir_renames = xmalloc(sizeof(*dir_renames));
2285 dir_rename_init(dir_renames);
2286 for (i = 0; i < pairs->nr; ++i) {
2287 struct string_list_item *item;
2288 int *count;
2289 struct diff_filepair *pair = pairs->queue[i];
2290 char *old_dir, *new_dir;
2291
2292 /* File not part of directory rename if it wasn't renamed */
2293 if (pair->status != 'R')
2294 continue;
2295
2296 get_renamed_dir_portion(pair->one->path, pair->two->path,
2297 &old_dir, &new_dir);
2298 if (!old_dir)
2299 /* Directory didn't change at all; ignore this one. */
2300 continue;
2301
2302 entry = dir_rename_find_entry(dir_renames, old_dir);
2303 if (!entry) {
2304 entry = xmalloc(sizeof(*entry));
2305 dir_rename_entry_init(entry, old_dir);
Eric Wong26b455f2019-10-06 23:30:32 +00002306 hashmap_put(dir_renames, &entry->ent);
Elijah Newren7fe40b82018-04-19 10:58:05 -07002307 } else {
2308 free(old_dir);
2309 }
2310 item = string_list_lookup(&entry->possible_new_dirs, new_dir);
2311 if (!item) {
2312 item = string_list_insert(&entry->possible_new_dirs,
2313 new_dir);
2314 item->util = xcalloc(1, sizeof(int));
2315 } else {
2316 free(new_dir);
2317 }
2318 count = item->util;
2319 *count += 1;
2320 }
2321
2322 /*
2323 * For each directory with files moved out of it, we find out which
2324 * target directory received the most files so we can declare it to
2325 * be the "winning" target location for the directory rename. This
2326 * winner gets recorded in new_dir. If there is no winner
2327 * (multiple target directories received the same number of files),
2328 * we set non_unique_new_dir. Once we've determined the winner (or
2329 * that there is no winner), we no longer need possible_new_dirs.
2330 */
Eric Wong87571c32019-10-06 23:30:38 +00002331 hashmap_for_each_entry(dir_renames, &iter, entry,
Eric Wong87571c32019-10-06 23:30:38 +00002332 ent /* member name */) {
Elijah Newren7fe40b82018-04-19 10:58:05 -07002333 int max = 0;
2334 int bad_max = 0;
2335 char *best = NULL;
2336
2337 for (i = 0; i < entry->possible_new_dirs.nr; i++) {
2338 int *count = entry->possible_new_dirs.items[i].util;
2339
2340 if (*count == max)
2341 bad_max = max;
2342 else if (*count > max) {
2343 max = *count;
2344 best = entry->possible_new_dirs.items[i].string;
2345 }
2346 }
2347 if (bad_max == max)
2348 entry->non_unique_new_dir = 1;
2349 else {
2350 assert(entry->new_dir.len == 0);
2351 strbuf_addstr(&entry->new_dir, best);
2352 }
2353 /*
2354 * The relevant directory sub-portion of the original full
2355 * filepaths were xstrndup'ed before inserting into
2356 * possible_new_dirs, and instead of manually iterating the
2357 * list and free'ing each, just lie and tell
2358 * possible_new_dirs that it did the strdup'ing so that it
2359 * will free them for us.
2360 */
2361 entry->possible_new_dirs.strdup_strings = 1;
2362 string_list_clear(&entry->possible_new_dirs, 1);
2363 }
2364
2365 return dir_renames;
2366}
2367
Elijah Newrene95ab702018-04-19 10:58:07 -07002368static struct dir_rename_entry *check_dir_renamed(const char *path,
2369 struct hashmap *dir_renames)
2370{
René Scharfe9da2d032018-06-10 12:56:31 +02002371 char *temp = xstrdup(path);
Elijah Newrene95ab702018-04-19 10:58:07 -07002372 char *end;
Elijah Newrenc3b9bc92018-09-05 10:03:07 -07002373 struct dir_rename_entry *entry = NULL;
Elijah Newrene95ab702018-04-19 10:58:07 -07002374
Elijah Newrene95ab702018-04-19 10:58:07 -07002375 while ((end = strrchr(temp, '/'))) {
2376 *end = '\0';
2377 entry = dir_rename_find_entry(dir_renames, temp);
2378 if (entry)
René Scharfe9da2d032018-06-10 12:56:31 +02002379 break;
Elijah Newrene95ab702018-04-19 10:58:07 -07002380 }
René Scharfe9da2d032018-06-10 12:56:31 +02002381 free(temp);
2382 return entry;
Elijah Newrene95ab702018-04-19 10:58:07 -07002383}
2384
2385static void compute_collisions(struct hashmap *collisions,
2386 struct hashmap *dir_renames,
2387 struct diff_queue_struct *pairs)
2388{
2389 int i;
2390
2391 /*
2392 * Multiple files can be mapped to the same path due to directory
2393 * renames done by the other side of history. Since that other
2394 * side of history could have merged multiple directories into one,
2395 * if our side of history added the same file basename to each of
2396 * those directories, then all N of them would get implicitly
2397 * renamed by the directory rename detection into the same path,
2398 * and we'd get an add/add/.../add conflict, and all those adds
2399 * from *this* side of history. This is not representable in the
2400 * index, and users aren't going to easily be able to make sense of
2401 * it. So we need to provide a good warning about what's
2402 * happening, and fall back to no-directory-rename detection
2403 * behavior for those paths.
2404 *
2405 * See testcases 9e and all of section 5 from t6043 for examples.
2406 */
2407 collision_init(collisions);
2408
2409 for (i = 0; i < pairs->nr; ++i) {
2410 struct dir_rename_entry *dir_rename_ent;
2411 struct collision_entry *collision_ent;
2412 char *new_path;
2413 struct diff_filepair *pair = pairs->queue[i];
2414
Elijah Newren6e7e0272018-04-19 10:58:15 -07002415 if (pair->status != 'A' && pair->status != 'R')
Elijah Newrene95ab702018-04-19 10:58:07 -07002416 continue;
2417 dir_rename_ent = check_dir_renamed(pair->two->path,
2418 dir_renames);
2419 if (!dir_rename_ent)
2420 continue;
2421
2422 new_path = apply_dir_rename(dir_rename_ent, pair->two->path);
2423 if (!new_path)
2424 /*
2425 * dir_rename_ent->non_unique_new_path is true, which
2426 * means there is no directory rename for us to use,
2427 * which means it won't cause us any additional
2428 * collisions.
2429 */
2430 continue;
2431 collision_ent = collision_find_entry(collisions, new_path);
2432 if (!collision_ent) {
René Scharfeca56dad2021-03-13 17:17:22 +01002433 CALLOC_ARRAY(collision_ent, 1);
Eric Wongd22245a2019-10-06 23:30:27 +00002434 hashmap_entry_init(&collision_ent->ent,
2435 strhash(new_path));
Eric Wong26b455f2019-10-06 23:30:32 +00002436 hashmap_put(collisions, &collision_ent->ent);
Elijah Newrene95ab702018-04-19 10:58:07 -07002437 collision_ent->target_file = new_path;
2438 } else {
2439 free(new_path);
2440 }
2441 string_list_insert(&collision_ent->source_files,
2442 pair->two->path);
2443 }
2444}
2445
Elijah Newren259ccb62019-04-05 08:00:13 -07002446static char *check_for_directory_rename(struct merge_options *opt,
Elijah Newrenf6f77552018-04-19 10:58:08 -07002447 const char *path,
2448 struct tree *tree,
2449 struct hashmap *dir_renames,
2450 struct hashmap *dir_rename_exclusions,
2451 struct hashmap *collisions,
2452 int *clean_merge)
2453{
2454 char *new_path = NULL;
2455 struct dir_rename_entry *entry = check_dir_renamed(path, dir_renames);
2456 struct dir_rename_entry *oentry = NULL;
2457
2458 if (!entry)
2459 return new_path;
2460
2461 /*
2462 * This next part is a little weird. We do not want to do an
2463 * implicit rename into a directory we renamed on our side, because
2464 * that will result in a spurious rename/rename(1to2) conflict. An
2465 * example:
2466 * Base commit: dumbdir/afile, otherdir/bfile
2467 * Side 1: smrtdir/afile, otherdir/bfile
2468 * Side 2: dumbdir/afile, dumbdir/bfile
2469 * Here, while working on Side 1, we could notice that otherdir was
2470 * renamed/merged to dumbdir, and change the diff_filepair for
2471 * otherdir/bfile into a rename into dumbdir/bfile. However, Side
2472 * 2 will notice the rename from dumbdir to smrtdir, and do the
2473 * transitive rename to move it from dumbdir/bfile to
2474 * smrtdir/bfile. That gives us bfile in dumbdir vs being in
2475 * smrtdir, a rename/rename(1to2) conflict. We really just want
2476 * the file to end up in smrtdir. And the way to achieve that is
2477 * to not let Side1 do the rename to dumbdir, since we know that is
2478 * the source of one of our directory renames.
2479 *
2480 * That's why oentry and dir_rename_exclusions is here.
2481 *
2482 * As it turns out, this also prevents N-way transient rename
2483 * confusion; See testcases 9c and 9d of t6043.
2484 */
2485 oentry = dir_rename_find_entry(dir_rename_exclusions, entry->new_dir.buf);
2486 if (oentry) {
Elijah Newren259ccb62019-04-05 08:00:13 -07002487 output(opt, 1, _("WARNING: Avoiding applying %s -> %s rename "
Elijah Newrenf6f77552018-04-19 10:58:08 -07002488 "to %s, because %s itself was renamed."),
2489 entry->dir, entry->new_dir.buf, path, entry->new_dir.buf);
2490 } else {
Elijah Newren259ccb62019-04-05 08:00:13 -07002491 new_path = handle_path_level_conflicts(opt, path, entry,
Elijah Newrenf6f77552018-04-19 10:58:08 -07002492 collisions, tree);
2493 *clean_merge &= (new_path != NULL);
2494 }
2495
2496 return new_path;
2497}
2498
Elijah Newren259ccb62019-04-05 08:00:13 -07002499static void apply_directory_rename_modifications(struct merge_options *opt,
Elijah Newren9c0743f2018-04-19 10:58:10 -07002500 struct diff_filepair *pair,
2501 char *new_path,
2502 struct rename *re,
2503 struct tree *tree,
2504 struct tree *o_tree,
2505 struct tree *a_tree,
2506 struct tree *b_tree,
Jeff Kingb53c5022019-02-14 00:50:02 -05002507 struct string_list *entries)
Elijah Newren9c0743f2018-04-19 10:58:10 -07002508{
2509 struct string_list_item *item;
2510 int stage = (tree == a_tree ? 2 : 3);
Elijah Newren18797a32018-04-19 10:58:13 -07002511 int update_wd;
Elijah Newren9c0743f2018-04-19 10:58:10 -07002512
2513 /*
2514 * In all cases where we can do directory rename detection,
2515 * unpack_trees() will have read pair->two->path into the
2516 * index and the working copy. We need to remove it so that
2517 * we can instead place it at new_path. It is guaranteed to
2518 * not be untracked (unpack_trees() would have errored out
2519 * saying the file would have been overwritten), but it might
2520 * be dirty, though.
2521 */
Elijah Newren259ccb62019-04-05 08:00:13 -07002522 update_wd = !was_dirty(opt, pair->two->path);
Elijah Newren18797a32018-04-19 10:58:13 -07002523 if (!update_wd)
Elijah Newren259ccb62019-04-05 08:00:13 -07002524 output(opt, 1, _("Refusing to lose dirty file at %s"),
Elijah Newren18797a32018-04-19 10:58:13 -07002525 pair->two->path);
Elijah Newren259ccb62019-04-05 08:00:13 -07002526 remove_file(opt, 1, pair->two->path, !update_wd);
Elijah Newren9c0743f2018-04-19 10:58:10 -07002527
2528 /* Find or create a new re->dst_entry */
2529 item = string_list_lookup(entries, new_path);
2530 if (item) {
2531 /*
2532 * Since we're renaming on this side of history, and it's
2533 * due to a directory rename on the other side of history
2534 * (which we only allow when the directory in question no
2535 * longer exists on the other side of history), the
2536 * original entry for re->dst_entry is no longer
2537 * necessary...
2538 */
2539 re->dst_entry->processed = 1;
2540
2541 /*
2542 * ...because we'll be using this new one.
2543 */
2544 re->dst_entry = item->util;
2545 } else {
2546 /*
2547 * re->dst_entry is for the before-dir-rename path, and we
2548 * need it to hold information for the after-dir-rename
2549 * path. Before creating a new entry, we need to mark the
2550 * old one as unnecessary (...unless it is shared by
2551 * src_entry, i.e. this didn't use to be a rename, in which
2552 * case we can just allow the normal processing to happen
2553 * for it).
2554 */
2555 if (pair->status == 'R')
2556 re->dst_entry->processed = 1;
2557
Nguyễn Thái Ngọc Duy34e77712019-06-27 16:28:52 +07002558 re->dst_entry = insert_stage_data(opt->repo, new_path,
Elijah Newren9c0743f2018-04-19 10:58:10 -07002559 o_tree, a_tree, b_tree,
2560 entries);
2561 item = string_list_insert(entries, new_path);
2562 item->util = re->dst_entry;
2563 }
2564
2565 /*
2566 * Update the stage_data with the information about the path we are
2567 * moving into place. That slot will be empty and available for us
2568 * to write to because of the collision checks in
2569 * handle_path_level_conflicts(). In other words,
2570 * re->dst_entry->stages[stage].oid will be the null_oid, so it's
2571 * open for us to write to.
2572 *
2573 * It may be tempting to actually update the index at this point as
2574 * well, using update_stages_for_stage_data(), but as per the big
2575 * "NOTE" in update_stages(), doing so will modify the current
2576 * in-memory index which will break calls to would_lose_untracked()
2577 * that we need to make. Instead, we need to just make sure that
Elijah Newren8ebe7b02018-06-09 21:16:15 -07002578 * the various handle_rename_*() functions update the index
Elijah Newren9c0743f2018-04-19 10:58:10 -07002579 * explicitly rather than relying on unpack_trees() to have done it.
2580 */
Nguyễn Thái Ngọc Duy50ddb082019-06-27 16:28:49 +07002581 get_tree_entry(opt->repo,
2582 &tree->object.oid,
Elijah Newren9c0743f2018-04-19 10:58:10 -07002583 pair->two->path,
2584 &re->dst_entry->stages[stage].oid,
2585 &re->dst_entry->stages[stage].mode);
2586
Elijah Newren6d169fd2019-04-05 08:00:24 -07002587 /*
2588 * Record the original change status (or 'type' of change). If it
2589 * was originally an add ('A'), this lets us differentiate later
2590 * between a RENAME_DELETE conflict and RENAME_VIA_DIR (they
2591 * otherwise look the same). If it was originally a rename ('R'),
2592 * this lets us remember and report accurately about the transitive
2593 * renaming that occurred via the directory rename detection. Also,
2594 * record the original destination name.
2595 */
2596 re->dir_rename_original_type = pair->status;
2597 re->dir_rename_original_dest = pair->two->path;
2598
Elijah Newren9c0743f2018-04-19 10:58:10 -07002599 /*
2600 * We don't actually look at pair->status again, but it seems
2601 * pedagogically correct to adjust it.
2602 */
2603 pair->status = 'R';
2604
2605 /*
2606 * Finally, record the new location.
2607 */
2608 pair->two->path = new_path;
2609}
2610
Elijah Newrene5257b22018-04-19 10:58:03 -07002611/*
2612 * Get information of all renames which occurred in 'pairs', making use of
2613 * any implicit directory renames inferred from the other side of history.
2614 * We need the three trees in the merge ('o_tree', 'a_tree' and 'b_tree')
2615 * to be able to associate the correct cache entries with the rename
2616 * information; tree is always equal to either a_tree or b_tree.
2617 */
Elijah Newren259ccb62019-04-05 08:00:13 -07002618static struct string_list *get_renames(struct merge_options *opt,
Elijah Newrenc336ab82019-04-05 08:00:20 -07002619 const char *branch,
Elijah Newrene5257b22018-04-19 10:58:03 -07002620 struct diff_queue_struct *pairs,
Elijah Newrene95ab702018-04-19 10:58:07 -07002621 struct hashmap *dir_renames,
Elijah Newrenf6f77552018-04-19 10:58:08 -07002622 struct hashmap *dir_rename_exclusions,
Elijah Newrene5257b22018-04-19 10:58:03 -07002623 struct tree *tree,
2624 struct tree *o_tree,
2625 struct tree *a_tree,
2626 struct tree *b_tree,
Elijah Newrenf6f77552018-04-19 10:58:08 -07002627 struct string_list *entries,
2628 int *clean_merge)
Elijah Newrene5257b22018-04-19 10:58:03 -07002629{
2630 int i;
Elijah Newrene95ab702018-04-19 10:58:07 -07002631 struct hashmap collisions;
2632 struct hashmap_iter iter;
2633 struct collision_entry *e;
Elijah Newrene5257b22018-04-19 10:58:03 -07002634 struct string_list *renames;
2635
Elijah Newrene95ab702018-04-19 10:58:07 -07002636 compute_collisions(&collisions, dir_renames, pairs);
René Scharfeca56dad2021-03-13 17:17:22 +01002637 CALLOC_ARRAY(renames, 1);
Elijah Newrene5257b22018-04-19 10:58:03 -07002638
2639 for (i = 0; i < pairs->nr; ++i) {
Elijah Newren9ba91552018-04-19 10:57:59 -07002640 struct string_list_item *item;
2641 struct rename *re;
Elijah Newrene5257b22018-04-19 10:58:03 -07002642 struct diff_filepair *pair = pairs->queue[i];
Elijah Newrenf6f77552018-04-19 10:58:08 -07002643 char *new_path; /* non-NULL only with directory renames */
Elijah Newren9ba91552018-04-19 10:57:59 -07002644
Elijah Newren6e7e0272018-04-19 10:58:15 -07002645 if (pair->status != 'A' && pair->status != 'R') {
Elijah Newren9ba91552018-04-19 10:57:59 -07002646 diff_free_filepair(pair);
2647 continue;
2648 }
Elijah Newren259ccb62019-04-05 08:00:13 -07002649 new_path = check_for_directory_rename(opt, pair->two->path, tree,
Elijah Newrenf6f77552018-04-19 10:58:08 -07002650 dir_renames,
2651 dir_rename_exclusions,
2652 &collisions,
2653 clean_merge);
2654 if (pair->status != 'R' && !new_path) {
2655 diff_free_filepair(pair);
2656 continue;
2657 }
2658
Elijah Newren9ba91552018-04-19 10:57:59 -07002659 re = xmalloc(sizeof(*re));
2660 re->processed = 0;
2661 re->pair = pair;
Elijah Newrenc336ab82019-04-05 08:00:20 -07002662 re->branch = branch;
Elijah Newren6d169fd2019-04-05 08:00:24 -07002663 re->dir_rename_original_type = '\0';
2664 re->dir_rename_original_dest = NULL;
Elijah Newren9ba91552018-04-19 10:57:59 -07002665 item = string_list_lookup(entries, re->pair->one->path);
2666 if (!item)
Nguyễn Thái Ngọc Duy34e77712019-06-27 16:28:52 +07002667 re->src_entry = insert_stage_data(opt->repo,
2668 re->pair->one->path,
Elijah Newren9ba91552018-04-19 10:57:59 -07002669 o_tree, a_tree, b_tree, entries);
2670 else
2671 re->src_entry = item->util;
2672
2673 item = string_list_lookup(entries, re->pair->two->path);
2674 if (!item)
Nguyễn Thái Ngọc Duy34e77712019-06-27 16:28:52 +07002675 re->dst_entry = insert_stage_data(opt->repo,
2676 re->pair->two->path,
Elijah Newren9ba91552018-04-19 10:57:59 -07002677 o_tree, a_tree, b_tree, entries);
2678 else
2679 re->dst_entry = item->util;
2680 item = string_list_insert(renames, pair->one->path);
2681 item->util = re;
Elijah Newren9c0743f2018-04-19 10:58:10 -07002682 if (new_path)
Elijah Newren259ccb62019-04-05 08:00:13 -07002683 apply_directory_rename_modifications(opt, pair, new_path,
Elijah Newren9c0743f2018-04-19 10:58:10 -07002684 re, tree, o_tree,
2685 a_tree, b_tree,
Jeff Kingb53c5022019-02-14 00:50:02 -05002686 entries);
Elijah Newren9ba91552018-04-19 10:57:59 -07002687 }
Elijah Newrene95ab702018-04-19 10:58:07 -07002688
Eric Wong87571c32019-10-06 23:30:38 +00002689 hashmap_for_each_entry(&collisions, &iter, e,
Eric Wong87571c32019-10-06 23:30:38 +00002690 ent /* member name */) {
Elijah Newrene95ab702018-04-19 10:58:07 -07002691 free(e->target_file);
2692 string_list_clear(&e->source_files, 0);
2693 }
Elijah Newren6da1a252020-11-02 18:55:05 +00002694 hashmap_clear_and_free(&collisions, struct collision_entry, ent);
Elijah Newren9ba91552018-04-19 10:57:59 -07002695 return renames;
2696}
2697
Elijah Newren259ccb62019-04-05 08:00:13 -07002698static int process_renames(struct merge_options *opt,
Miklos Vajna8a2fce12008-08-25 16:25:57 +02002699 struct string_list *a_renames,
2700 struct string_list *b_renames)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002701{
2702 int clean_merge = 1, i, j;
Thiago Farina183113a2010-07-04 16:46:19 -03002703 struct string_list a_by_dst = STRING_LIST_INIT_NODUP;
2704 struct string_list b_by_dst = STRING_LIST_INIT_NODUP;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002705 const struct rename *sre;
2706
Elijah Newren816147e2021-03-20 00:03:53 +00002707 /*
2708 * FIXME: As string-list.h notes, it's O(n^2) to build a sorted
2709 * string_list one-by-one, but O(n log n) to build it unsorted and
2710 * then sort it. Note that as we build the list, we do not need to
2711 * check if the existing destination path is already in the list,
2712 * because the structure of diffcore_rename guarantees we won't
2713 * have duplicates.
2714 */
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002715 for (i = 0; i < a_renames->nr; i++) {
2716 sre = a_renames->items[i].util;
Julian Phillips78a395d2010-06-26 00:41:35 +01002717 string_list_insert(&a_by_dst, sre->pair->two->path)->util
Elijah Newren0a6b8712011-08-11 23:20:04 -06002718 = (void *)sre;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002719 }
2720 for (i = 0; i < b_renames->nr; i++) {
2721 sre = b_renames->items[i].util;
Julian Phillips78a395d2010-06-26 00:41:35 +01002722 string_list_insert(&b_by_dst, sre->pair->two->path)->util
Elijah Newren0a6b8712011-08-11 23:20:04 -06002723 = (void *)sre;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002724 }
2725
2726 for (i = 0, j = 0; i < a_renames->nr || j < b_renames->nr;) {
Benjamin Kramer8e24cba2009-03-15 22:01:20 +01002727 struct string_list *renames1, *renames2Dst;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002728 struct rename *ren1 = NULL, *ren2 = NULL;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002729 const char *ren1_src, *ren1_dst;
Elijah Newren461f5042011-08-11 23:20:15 -06002730 struct string_list_item *lookup;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002731
2732 if (i >= a_renames->nr) {
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002733 ren2 = b_renames->items[j++].util;
2734 } else if (j >= b_renames->nr) {
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002735 ren1 = a_renames->items[i++].util;
2736 } else {
Benjamin Kramer8e24cba2009-03-15 22:01:20 +01002737 int compare = strcmp(a_renames->items[i].string,
2738 b_renames->items[j].string);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002739 if (compare <= 0)
2740 ren1 = a_renames->items[i++].util;
2741 if (compare >= 0)
2742 ren2 = b_renames->items[j++].util;
2743 }
2744
2745 /* TODO: refactor, so that 1/2 are not needed */
2746 if (ren1) {
2747 renames1 = a_renames;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002748 renames2Dst = &b_by_dst;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002749 } else {
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002750 renames1 = b_renames;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002751 renames2Dst = &a_by_dst;
René Scharfe35d803b2017-01-28 22:40:58 +01002752 SWAP(ren2, ren1);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002753 }
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002754
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002755 if (ren1->processed)
2756 continue;
2757 ren1->processed = 1;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002758 ren1->dst_entry->processed = 1;
Elijah Newren7769a752011-08-11 23:20:05 -06002759 /* BUG: We should only mark src_entry as processed if we
2760 * are not dealing with a rename + add-source case.
2761 */
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002762 ren1->src_entry->processed = 1;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002763
2764 ren1_src = ren1->pair->one->path;
2765 ren1_dst = ren1->pair->two->path;
2766
2767 if (ren2) {
Elijah Newren461f5042011-08-11 23:20:15 -06002768 /* One file renamed on both sides */
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002769 const char *ren2_src = ren2->pair->one->path;
2770 const char *ren2_dst = ren2->pair->two->path;
Elijah Newren4f66dad2011-08-11 23:20:08 -06002771 enum rename_type rename_type;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002772 if (strcmp(ren1_src, ren2_src) != 0)
Johannes Schindelin033abf92018-05-02 11:38:39 +02002773 BUG("ren1_src != ren2_src");
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002774 ren2->dst_entry->processed = 1;
2775 ren2->processed = 1;
2776 if (strcmp(ren1_dst, ren2_dst) != 0) {
Elijah Newren4f66dad2011-08-11 23:20:08 -06002777 rename_type = RENAME_ONE_FILE_TO_TWO;
Elijah Newren461f5042011-08-11 23:20:15 -06002778 clean_merge = 0;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002779 } else {
Elijah Newren4f66dad2011-08-11 23:20:08 -06002780 rename_type = RENAME_ONE_FILE_TO_ONE;
Elijah Newren7769a752011-08-11 23:20:05 -06002781 /* BUG: We should only remove ren1_src in
2782 * the base stage (think of rename +
2783 * add-source cases).
2784 */
Elijah Newren259ccb62019-04-05 08:00:13 -07002785 remove_file(opt, 1, ren1_src, 1);
Elijah Newrenb8ddf162011-08-11 23:20:02 -06002786 update_entry(ren1->dst_entry,
2787 ren1->pair->one,
2788 ren1->pair->two,
2789 ren2->pair->two);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002790 }
Elijah Newrenc336ab82019-04-05 08:00:20 -07002791 setup_rename_conflict_info(rename_type, opt, ren1, ren2);
Elijah Newren461f5042011-08-11 23:20:15 -06002792 } else if ((lookup = string_list_lookup(renames2Dst, ren1_dst))) {
2793 /* Two different files renamed to the same thing */
2794 char *ren2_dst;
2795 ren2 = lookup->util;
2796 ren2_dst = ren2->pair->two->path;
2797 if (strcmp(ren1_dst, ren2_dst) != 0)
Johannes Schindelin033abf92018-05-02 11:38:39 +02002798 BUG("ren1_dst != ren2_dst");
Elijah Newren461f5042011-08-11 23:20:15 -06002799
2800 clean_merge = 0;
2801 ren2->processed = 1;
2802 /*
2803 * BUG: We should only mark src_entry as processed
2804 * if we are not dealing with a rename + add-source
2805 * case.
2806 */
2807 ren2->src_entry->processed = 1;
2808
2809 setup_rename_conflict_info(RENAME_TWO_FILES_TO_ONE,
Elijah Newrenc336ab82019-04-05 08:00:20 -07002810 opt, ren1, ren2);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002811 } else {
2812 /* Renamed in 1, maybe changed in 2 */
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002813 /* we only use sha1 and mode of these */
2814 struct diff_filespec src_other, dst_other;
Elijah Newren41d70bd2010-09-20 02:28:47 -06002815 int try_merge;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002816
Elijah Newren41d70bd2010-09-20 02:28:47 -06002817 /*
2818 * unpack_trees loads entries from common-commit
2819 * into stage 1, from head-commit into stage 2, and
2820 * from merge-commit into stage 3. We keep track
2821 * of which side corresponds to the rename.
2822 */
2823 int renamed_stage = a_renames == renames1 ? 2 : 3;
2824 int other_stage = a_renames == renames1 ? 3 : 2;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002825
Elijah Newren3585d0e2021-06-30 17:30:00 +00002826 /*
2827 * Directory renames have a funny corner case...
2828 */
2829 int renamed_to_self = !strcmp(ren1_src, ren1_dst);
2830
Elijah Newren7769a752011-08-11 23:20:05 -06002831 /* BUG: We should only remove ren1_src in the base
2832 * stage and in other_stage (think of rename +
2833 * add-source case).
2834 */
Elijah Newren3585d0e2021-06-30 17:30:00 +00002835 if (!renamed_to_self)
2836 remove_file(opt, 1, ren1_src,
2837 renamed_stage == 2 ||
2838 !was_tracked(opt, ren1_src));
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002839
brian m. carlsonfd429e92016-06-24 23:09:25 +00002840 oidcpy(&src_other.oid,
2841 &ren1->src_entry->stages[other_stage].oid);
Elijah Newren41d70bd2010-09-20 02:28:47 -06002842 src_other.mode = ren1->src_entry->stages[other_stage].mode;
brian m. carlsonfd429e92016-06-24 23:09:25 +00002843 oidcpy(&dst_other.oid,
2844 &ren1->dst_entry->stages[other_stage].oid);
Elijah Newren41d70bd2010-09-20 02:28:47 -06002845 dst_other.mode = ren1->dst_entry->stages[other_stage].mode;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002846 try_merge = 0;
2847
brian m. carlson14228442021-04-26 01:02:56 +00002848 if (oideq(&src_other.oid, null_oid()) &&
Elijah Newren6d169fd2019-04-05 08:00:24 -07002849 ren1->dir_rename_original_type == 'A') {
Elijah Newren5455c332018-06-09 21:16:14 -07002850 setup_rename_conflict_info(RENAME_VIA_DIR,
Elijah Newrenc336ab82019-04-05 08:00:20 -07002851 opt, ren1, NULL);
Elijah Newren3585d0e2021-06-30 17:30:00 +00002852 } else if (renamed_to_self) {
2853 setup_rename_conflict_info(RENAME_NORMAL,
2854 opt, ren1, NULL);
brian m. carlson14228442021-04-26 01:02:56 +00002855 } else if (oideq(&src_other.oid, null_oid())) {
Elijah Newren4f66dad2011-08-11 23:20:08 -06002856 setup_rename_conflict_info(RENAME_DELETE,
Elijah Newrenc336ab82019-04-05 08:00:20 -07002857 opt, ren1, NULL);
Schalk, Kend5af5102010-09-01 13:15:32 -07002858 } else if ((dst_other.mode == ren1->pair->two->mode) &&
Elijah Newren763a59e2020-01-01 05:20:57 +00002859 oideq(&dst_other.oid, &ren1->pair->two->oid)) {
Elijah Newren35a74ab2011-08-11 23:20:27 -06002860 /*
2861 * Added file on the other side identical to
2862 * the file being renamed: clean merge.
2863 * Also, there is no need to overwrite the
2864 * file already in the working copy, so call
2865 * update_file_flags() instead of
2866 * update_file().
2867 */
Elijah Newren259ccb62019-04-05 08:00:13 -07002868 if (update_file_flags(opt,
Elijah Newren8daec1d2019-04-05 08:00:22 -07002869 ren1->pair->two,
Johannes Schindelin75456f92016-07-26 18:06:21 +02002870 ren1_dst,
2871 1, /* update_cache */
2872 0 /* update_wd */))
2873 clean_merge = -1;
brian m. carlson14228442021-04-26 01:02:56 +00002874 } else if (!oideq(&dst_other.oid, null_oid())) {
Elijah Newren7f867162018-11-07 20:40:26 -08002875 /*
2876 * Probably not a clean merge, but it's
2877 * premature to set clean_merge to 0 here,
2878 * because if the rename merges cleanly and
2879 * the merge exactly matches the newly added
2880 * file, then the merge will be clean.
2881 */
2882 setup_rename_conflict_info(RENAME_ADD,
Elijah Newrenc336ab82019-04-05 08:00:20 -07002883 opt, ren1, NULL);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002884 } else
2885 try_merge = 1;
2886
Johannes Schindelin75456f92016-07-26 18:06:21 +02002887 if (clean_merge < 0)
2888 goto cleanup_and_return;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002889 if (try_merge) {
Elijah Newrene3de8882019-04-05 08:00:14 -07002890 struct diff_filespec *o, *a, *b;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002891 src_other.path = (char *)ren1_src;
2892
Elijah Newrene3de8882019-04-05 08:00:14 -07002893 o = ren1->pair->one;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002894 if (a_renames == renames1) {
2895 a = ren1->pair->two;
2896 b = &src_other;
2897 } else {
2898 b = ren1->pair->two;
2899 a = &src_other;
2900 }
Elijah Newrene3de8882019-04-05 08:00:14 -07002901 update_entry(ren1->dst_entry, o, a, b);
Elijah Newren4f66dad2011-08-11 23:20:08 -06002902 setup_rename_conflict_info(RENAME_NORMAL,
Elijah Newrenc336ab82019-04-05 08:00:20 -07002903 opt, ren1, NULL);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002904 }
2905 }
2906 }
Johannes Schindelin75456f92016-07-26 18:06:21 +02002907cleanup_and_return:
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002908 string_list_clear(&a_by_dst, 0);
2909 string_list_clear(&b_by_dst, 0);
2910
2911 return clean_merge;
2912}
2913
Elijah Newrenf1725892018-04-19 10:58:00 -07002914struct rename_info {
2915 struct string_list *head_renames;
2916 struct string_list *merge_renames;
2917};
2918
Elijah Newren7fe40b82018-04-19 10:58:05 -07002919static void initial_cleanup_rename(struct diff_queue_struct *pairs,
2920 struct hashmap *dir_renames)
Elijah Newrenffc16c42018-04-19 10:58:04 -07002921{
Elijah Newren7fe40b82018-04-19 10:58:05 -07002922 struct hashmap_iter iter;
2923 struct dir_rename_entry *e;
2924
Eric Wong87571c32019-10-06 23:30:38 +00002925 hashmap_for_each_entry(dir_renames, &iter, e,
Eric Wong87571c32019-10-06 23:30:38 +00002926 ent /* member name */) {
Elijah Newren7fe40b82018-04-19 10:58:05 -07002927 free(e->dir);
2928 strbuf_release(&e->new_dir);
2929 /* possible_new_dirs already cleared in get_directory_renames */
2930 }
Elijah Newren6da1a252020-11-02 18:55:05 +00002931 hashmap_clear_and_free(dir_renames, struct dir_rename_entry, ent);
Elijah Newren7fe40b82018-04-19 10:58:05 -07002932 free(dir_renames);
2933
Elijah Newrenffc16c42018-04-19 10:58:04 -07002934 free(pairs->queue);
2935 free(pairs);
2936}
2937
Elijah Newren259ccb62019-04-05 08:00:13 -07002938static int detect_and_process_renames(struct merge_options *opt,
Elijah Newren8ebe7b02018-06-09 21:16:15 -07002939 struct tree *common,
2940 struct tree *head,
2941 struct tree *merge,
2942 struct string_list *entries,
2943 struct rename_info *ri)
Elijah Newrenf1725892018-04-19 10:58:00 -07002944{
Elijah Newrene5257b22018-04-19 10:58:03 -07002945 struct diff_queue_struct *head_pairs, *merge_pairs;
Elijah Newren7fe40b82018-04-19 10:58:05 -07002946 struct hashmap *dir_re_head, *dir_re_merge;
Elijah Newrenf6f77552018-04-19 10:58:08 -07002947 int clean = 1;
Elijah Newrene5257b22018-04-19 10:58:03 -07002948
Elijah Newren3992ff02018-04-19 10:58:02 -07002949 ri->head_renames = NULL;
2950 ri->merge_renames = NULL;
2951
Elijah Newren259ccb62019-04-05 08:00:13 -07002952 if (!merge_detect_rename(opt))
Elijah Newren3992ff02018-04-19 10:58:02 -07002953 return 1;
2954
Elijah Newren259ccb62019-04-05 08:00:13 -07002955 head_pairs = get_diffpairs(opt, common, head);
2956 merge_pairs = get_diffpairs(opt, common, merge);
Elijah Newrene5257b22018-04-19 10:58:03 -07002957
Derrick Stolee8e012512019-08-17 11:41:25 -07002958 if ((opt->detect_directory_renames == MERGE_DIRECTORY_RENAMES_TRUE) ||
2959 (opt->detect_directory_renames == MERGE_DIRECTORY_RENAMES_CONFLICT &&
Elijah Newren5bf7e572019-08-17 11:41:41 -07002960 !opt->priv->call_depth)) {
Jeff Kingb53c5022019-02-14 00:50:02 -05002961 dir_re_head = get_directory_renames(head_pairs);
2962 dir_re_merge = get_directory_renames(merge_pairs);
Elijah Newren7fe40b82018-04-19 10:58:05 -07002963
Elijah Newren259ccb62019-04-05 08:00:13 -07002964 handle_directory_level_conflicts(opt,
Elijah Newren5fdddd92018-08-29 00:06:12 -07002965 dir_re_head, head,
2966 dir_re_merge, merge);
2967 } else {
2968 dir_re_head = xmalloc(sizeof(*dir_re_head));
2969 dir_re_merge = xmalloc(sizeof(*dir_re_merge));
2970 dir_rename_init(dir_re_head);
2971 dir_rename_init(dir_re_merge);
2972 }
Elijah Newren96e7ffb2018-04-19 10:58:06 -07002973
Elijah Newrenc336ab82019-04-05 08:00:20 -07002974 ri->head_renames = get_renames(opt, opt->branch1, head_pairs,
Elijah Newrenf6f77552018-04-19 10:58:08 -07002975 dir_re_merge, dir_re_head, head,
2976 common, head, merge, entries,
2977 &clean);
2978 if (clean < 0)
2979 goto cleanup;
Elijah Newrenc336ab82019-04-05 08:00:20 -07002980 ri->merge_renames = get_renames(opt, opt->branch2, merge_pairs,
Elijah Newrenf6f77552018-04-19 10:58:08 -07002981 dir_re_head, dir_re_merge, merge,
2982 common, head, merge, entries,
2983 &clean);
2984 if (clean < 0)
2985 goto cleanup;
Elijah Newren259ccb62019-04-05 08:00:13 -07002986 clean &= process_renames(opt, ri->head_renames, ri->merge_renames);
Elijah Newrene5257b22018-04-19 10:58:03 -07002987
Elijah Newrenf6f77552018-04-19 10:58:08 -07002988cleanup:
Elijah Newrene5257b22018-04-19 10:58:03 -07002989 /*
2990 * Some cleanup is deferred until cleanup_renames() because the
2991 * data structures are still needed and referenced in
2992 * process_entry(). But there are a few things we can free now.
2993 */
Elijah Newren7fe40b82018-04-19 10:58:05 -07002994 initial_cleanup_rename(head_pairs, dir_re_head);
2995 initial_cleanup_rename(merge_pairs, dir_re_merge);
Elijah Newrene5257b22018-04-19 10:58:03 -07002996
2997 return clean;
Elijah Newrenf1725892018-04-19 10:58:00 -07002998}
2999
Elijah Newrenffc16c42018-04-19 10:58:04 -07003000static void final_cleanup_rename(struct string_list *rename)
Elijah Newren9cfee252018-04-19 10:58:01 -07003001{
3002 const struct rename *re;
3003 int i;
3004
Junio C Hamanoafe8a902022-05-02 09:50:37 -07003005 if (!rename)
Elijah Newren3992ff02018-04-19 10:58:02 -07003006 return;
3007
Elijah Newren9cfee252018-04-19 10:58:01 -07003008 for (i = 0; i < rename->nr; i++) {
3009 re = rename->items[i].util;
3010 diff_free_filepair(re->pair);
3011 }
3012 string_list_clear(rename, 1);
3013 free(rename);
3014}
3015
Elijah Newrenffc16c42018-04-19 10:58:04 -07003016static void final_cleanup_renames(struct rename_info *re_info)
Elijah Newrenf1725892018-04-19 10:58:00 -07003017{
Elijah Newrenffc16c42018-04-19 10:58:04 -07003018 final_cleanup_rename(re_info->head_renames);
3019 final_cleanup_rename(re_info->merge_renames);
Elijah Newrenf1725892018-04-19 10:58:00 -07003020}
3021
Elijah Newren259ccb62019-04-05 08:00:13 -07003022static int read_oid_strbuf(struct merge_options *opt,
Elijah Newrend90e7592018-06-09 21:16:12 -07003023 const struct object_id *oid,
3024 struct strbuf *dst)
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02003025{
3026 void *buf;
3027 enum object_type type;
3028 unsigned long size;
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +02003029 buf = repo_read_object_file(the_repository, oid, &type, &size);
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02003030 if (!buf)
Elijah Newren259ccb62019-04-05 08:00:13 -07003031 return err(opt, _("cannot read object %s"), oid_to_hex(oid));
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02003032 if (type != OBJ_BLOB) {
3033 free(buf);
Elijah Newren259ccb62019-04-05 08:00:13 -07003034 return err(opt, _("object %s is not a blob"), oid_to_hex(oid));
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02003035 }
3036 strbuf_attach(dst, buf, size, size + 1);
3037 return 0;
3038}
3039
Johannes Schindelinbc9204d2016-08-01 13:44:37 +02003040static int blob_unchanged(struct merge_options *opt,
Elijah Newren8daec1d2019-04-05 08:00:22 -07003041 const struct diff_filespec *o,
3042 const struct diff_filespec *a,
Jonathan Nieder3e7589b2010-08-05 06:13:49 -05003043 int renormalize, const char *path)
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02003044{
Elijah Newren93a02c52019-04-05 08:00:15 -07003045 struct strbuf obuf = STRBUF_INIT;
3046 struct strbuf abuf = STRBUF_INIT;
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02003047 int ret = 0; /* assume changed for safety */
Derrick Stolee847a9e52021-04-01 01:49:39 +00003048 struct index_state *idx = opt->repo->index;
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02003049
Elijah Newren8daec1d2019-04-05 08:00:22 -07003050 if (a->mode != o->mode)
Jeff King72fac662015-10-26 17:39:39 -04003051 return 0;
Elijah Newren763a59e2020-01-01 05:20:57 +00003052 if (oideq(&o->oid, &a->oid))
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02003053 return 1;
Jonathan Nieder3e7589b2010-08-05 06:13:49 -05003054 if (!renormalize)
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02003055 return 0;
3056
Elijah Newren8daec1d2019-04-05 08:00:22 -07003057 if (read_oid_strbuf(opt, &o->oid, &obuf) ||
3058 read_oid_strbuf(opt, &a->oid, &abuf))
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02003059 goto error_return;
3060 /*
3061 * Note: binary | is used so that both renormalizations are
3062 * performed. Comparison can be skipped if both files are
3063 * unchanged since their sha1s have already been compared.
3064 */
Elijah Newren93a02c52019-04-05 08:00:15 -07003065 if (renormalize_buffer(idx, path, obuf.buf, obuf.len, &obuf) |
3066 renormalize_buffer(idx, path, abuf.buf, abuf.len, &abuf))
3067 ret = (obuf.len == abuf.len && !memcmp(obuf.buf, abuf.buf, obuf.len));
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02003068
3069error_return:
Elijah Newren93a02c52019-04-05 08:00:15 -07003070 strbuf_release(&obuf);
3071 strbuf_release(&abuf);
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02003072 return ret;
3073}
3074
Elijah Newren259ccb62019-04-05 08:00:13 -07003075static int handle_modify_delete(struct merge_options *opt,
Elijah Newrend90e7592018-06-09 21:16:12 -07003076 const char *path,
Elijah Newren8daec1d2019-04-05 08:00:22 -07003077 const struct diff_filespec *o,
3078 const struct diff_filespec *a,
3079 const struct diff_filespec *b)
Elijah Newren5e3ce662010-09-20 02:28:51 -06003080{
Matt McCutchenb26d87f2017-01-28 15:37:01 -05003081 const char *modify_branch, *delete_branch;
Elijah Newren8daec1d2019-04-05 08:00:22 -07003082 const struct diff_filespec *changed;
Matt McCutchenb26d87f2017-01-28 15:37:01 -05003083
Elijah Newren8daec1d2019-04-05 08:00:22 -07003084 if (is_valid(a)) {
Elijah Newren259ccb62019-04-05 08:00:13 -07003085 modify_branch = opt->branch1;
3086 delete_branch = opt->branch2;
Elijah Newren8daec1d2019-04-05 08:00:22 -07003087 changed = a;
Matt McCutchenb26d87f2017-01-28 15:37:01 -05003088 } else {
Elijah Newren259ccb62019-04-05 08:00:13 -07003089 modify_branch = opt->branch2;
3090 delete_branch = opt->branch1;
Elijah Newren8daec1d2019-04-05 08:00:22 -07003091 changed = b;
Matt McCutchenb26d87f2017-01-28 15:37:01 -05003092 }
3093
Elijah Newren259ccb62019-04-05 08:00:13 -07003094 return handle_change_delete(opt,
Matt McCutchenb26d87f2017-01-28 15:37:01 -05003095 path, NULL,
Elijah Newren8daec1d2019-04-05 08:00:22 -07003096 o, changed,
Matt McCutchenb26d87f2017-01-28 15:37:01 -05003097 modify_branch, delete_branch,
Johannes Schindelin75456f92016-07-26 18:06:21 +02003098 _("modify"), _("modified"));
Elijah Newren5e3ce662010-09-20 02:28:51 -06003099}
3100
Elijah Newrene62d1122019-04-05 08:00:25 -07003101static int handle_content_merge(struct merge_file_info *mfi,
3102 struct merge_options *opt,
Elijah Newrend9573552018-09-19 09:14:34 -07003103 const char *path,
3104 int is_dirty,
Elijah Newren8daec1d2019-04-05 08:00:22 -07003105 const struct diff_filespec *o,
3106 const struct diff_filespec *a,
3107 const struct diff_filespec *b,
Elijah Newren043622b2019-04-05 08:00:16 -07003108 struct rename_conflict_info *ci)
Elijah Newren0c4918d2010-09-20 02:28:52 -06003109{
Jiang Xin55653a62012-07-25 22:53:13 +08003110 const char *reason = _("content");
Elijah Newren4ab9a152010-09-20 02:29:07 -06003111 unsigned df_conflict_remains = 0;
Elijah Newren0c4918d2010-09-20 02:28:52 -06003112
Elijah Newren8daec1d2019-04-05 08:00:22 -07003113 if (!is_valid(o))
Jiang Xin55653a62012-07-25 22:53:13 +08003114 reason = _("add/add");
Elijah Newren0c4918d2010-09-20 02:28:52 -06003115
Elijah Newren8daec1d2019-04-05 08:00:22 -07003116 assert(o->path && a->path && b->path);
Elijah Newren5bf7e572019-08-17 11:41:41 -07003117 if (ci && dir_in_way(opt->repo->index, path, !opt->priv->call_depth,
Elijah Newren8daec1d2019-04-05 08:00:22 -07003118 S_ISGITLINK(ci->ren1->pair->two->mode)))
3119 df_conflict_remains = 1;
Elijah Newren3c217c02011-08-11 23:20:09 -06003120
Elijah Newren8daec1d2019-04-05 08:00:22 -07003121 if (merge_mode_and_contents(opt, o, a, b, path,
Elijah Newren259ccb62019-04-05 08:00:13 -07003122 opt->branch1, opt->branch2,
Elijah Newren5bf7e572019-08-17 11:41:41 -07003123 opt->priv->call_depth * 2, mfi))
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +02003124 return -1;
Elijah Newren4ab9a152010-09-20 02:29:07 -06003125
Elijah Newren1de70db2018-04-19 10:58:23 -07003126 /*
3127 * We can skip updating the working tree file iff:
3128 * a) The merge is clean
3129 * b) The merge matches what was in HEAD (content, mode, pathname)
3130 * c) The target path is usable (i.e. not involved in D/F conflict)
3131 */
Elijah Newrene62d1122019-04-05 08:00:25 -07003132 if (mfi->clean && was_tracked_and_matches(opt, path, &mfi->blob) &&
Elijah Newren1de70db2018-04-19 10:58:23 -07003133 !df_conflict_remains) {
Elijah Newren2b75fb62018-07-27 12:59:44 +00003134 int pos;
3135 struct cache_entry *ce;
3136
Elijah Newren259ccb62019-04-05 08:00:13 -07003137 output(opt, 3, _("Skipped %s (merged same as existing)"), path);
Elijah Newrene62d1122019-04-05 08:00:25 -07003138 if (add_cacheinfo(opt, &mfi->blob, path,
Elijah Newren5bf7e572019-08-17 11:41:41 -07003139 0, (!opt->priv->call_depth && !is_dirty), 0))
Elijah Newren1de70db2018-04-19 10:58:23 -07003140 return -1;
Elijah Newren2b75fb62018-07-27 12:59:44 +00003141 /*
3142 * However, add_cacheinfo() will delete the old cache entry
3143 * and add a new one. We need to copy over any skip_worktree
3144 * flag to avoid making the file appear as if it were
3145 * deleted by the user.
3146 */
Elijah Newren5bf7e572019-08-17 11:41:41 -07003147 pos = index_name_pos(&opt->priv->orig_index, path, strlen(path));
3148 ce = opt->priv->orig_index.cache[pos];
Elijah Newren2b75fb62018-07-27 12:59:44 +00003149 if (ce_skip_worktree(ce)) {
Elijah Newren259ccb62019-04-05 08:00:13 -07003150 pos = index_name_pos(opt->repo->index, path, strlen(path));
3151 ce = opt->repo->index->cache[pos];
Elijah Newren2b75fb62018-07-27 12:59:44 +00003152 ce->ce_flags |= CE_SKIP_WORKTREE;
3153 }
Elijah Newrene62d1122019-04-05 08:00:25 -07003154 return mfi->clean;
Elijah Newren05cf21e2018-04-19 10:58:22 -07003155 }
Elijah Newren0c4918d2010-09-20 02:28:52 -06003156
Elijah Newrene62d1122019-04-05 08:00:25 -07003157 if (!mfi->clean) {
3158 if (S_ISGITLINK(mfi->blob.mode))
Jiang Xin55653a62012-07-25 22:53:13 +08003159 reason = _("submodule");
Elijah Newren259ccb62019-04-05 08:00:13 -07003160 output(opt, 1, _("CONFLICT (%s): Merge conflict in %s"),
Elijah Newren0c4918d2010-09-20 02:28:52 -06003161 reason, path);
Elijah Newren043622b2019-04-05 08:00:16 -07003162 if (ci && !df_conflict_remains)
Elijah Newren8daec1d2019-04-05 08:00:22 -07003163 if (update_stages(opt, path, o, a, b))
Johannes Schindelin75456f92016-07-26 18:06:21 +02003164 return -1;
Elijah Newren0c4918d2010-09-20 02:28:52 -06003165 }
3166
Elijah Newrenbd423802018-04-19 10:58:17 -07003167 if (df_conflict_remains || is_dirty) {
Elijah Newren3d6b8e82011-08-11 23:19:53 -06003168 char *new_path;
Elijah Newren5bf7e572019-08-17 11:41:41 -07003169 if (opt->priv->call_depth) {
Elijah Newren259ccb62019-04-05 08:00:13 -07003170 remove_file_from_index(opt->repo->index, path);
Elijah Newren51931bf2011-08-11 23:20:06 -06003171 } else {
Elijah Newrene62d1122019-04-05 08:00:25 -07003172 if (!mfi->clean) {
Elijah Newren8daec1d2019-04-05 08:00:22 -07003173 if (update_stages(opt, path, o, a, b))
Johannes Schindelin75456f92016-07-26 18:06:21 +02003174 return -1;
3175 } else {
Elijah Newren259ccb62019-04-05 08:00:13 -07003176 int file_from_stage2 = was_tracked(opt, path);
Elijah Newren51931bf2011-08-11 23:20:06 -06003177
Elijah Newren259ccb62019-04-05 08:00:13 -07003178 if (update_stages(opt, path, NULL,
Elijah Newrene62d1122019-04-05 08:00:25 -07003179 file_from_stage2 ? &mfi->blob : NULL,
3180 file_from_stage2 ? NULL : &mfi->blob))
Johannes Schindelin75456f92016-07-26 18:06:21 +02003181 return -1;
Elijah Newren51931bf2011-08-11 23:20:06 -06003182 }
3183
3184 }
Elijah Newrenc336ab82019-04-05 08:00:20 -07003185 new_path = unique_path(opt, path, ci->ren1->branch);
Elijah Newrenbd423802018-04-19 10:58:17 -07003186 if (is_dirty) {
Elijah Newren259ccb62019-04-05 08:00:13 -07003187 output(opt, 1, _("Refusing to lose dirty file at %s"),
Elijah Newrenbd423802018-04-19 10:58:17 -07003188 path);
3189 }
Elijah Newren259ccb62019-04-05 08:00:13 -07003190 output(opt, 1, _("Adding as %s instead"), new_path);
Elijah Newrene62d1122019-04-05 08:00:25 -07003191 if (update_file(opt, 0, &mfi->blob, new_path)) {
Johannes Schindelin75456f92016-07-26 18:06:21 +02003192 free(new_path);
3193 return -1;
3194 }
Elijah Newren3d6b8e82011-08-11 23:19:53 -06003195 free(new_path);
Elijah Newrene62d1122019-04-05 08:00:25 -07003196 mfi->clean = 0;
3197 } else if (update_file(opt, mfi->clean, &mfi->blob, path))
Johannes Schindelin75456f92016-07-26 18:06:21 +02003198 return -1;
Elijah Newrene62d1122019-04-05 08:00:25 -07003199 return !is_dirty && mfi->clean;
Elijah Newren0c4918d2010-09-20 02:28:52 -06003200}
3201
Elijah Newren259ccb62019-04-05 08:00:13 -07003202static int handle_rename_normal(struct merge_options *opt,
Elijah Newren8ebe7b02018-06-09 21:16:15 -07003203 const char *path,
Elijah Newren8daec1d2019-04-05 08:00:22 -07003204 const struct diff_filespec *o,
3205 const struct diff_filespec *a,
3206 const struct diff_filespec *b,
Elijah Newren8ebe7b02018-06-09 21:16:15 -07003207 struct rename_conflict_info *ci)
Elijah Newren64b1abe2018-04-19 10:58:12 -07003208{
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07003209 struct rename *ren = ci->ren1;
Elijah Newrene62d1122019-04-05 08:00:25 -07003210 struct merge_file_info mfi;
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07003211 int clean;
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07003212
Elijah Newren64b1abe2018-04-19 10:58:12 -07003213 /* Merge the content and write it out */
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07003214 clean = handle_content_merge(&mfi, opt, path, was_dirty(opt, path),
3215 o, a, b, ci);
3216
Derrick Stolee8e012512019-08-17 11:41:25 -07003217 if (clean &&
3218 opt->detect_directory_renames == MERGE_DIRECTORY_RENAMES_CONFLICT &&
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07003219 ren->dir_rename_original_dest) {
3220 if (update_stages(opt, path,
Elijah Newren3585d0e2021-06-30 17:30:00 +00003221 &mfi.blob, &mfi.blob, &mfi.blob))
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07003222 return -1;
3223 clean = 0; /* not clean, but conflicted */
3224 }
3225 return clean;
3226}
3227
3228static void dir_rename_warning(const char *msg,
3229 int is_add,
3230 int clean,
3231 struct merge_options *opt,
3232 struct rename *ren)
3233{
3234 const char *other_branch;
3235 other_branch = (ren->branch == opt->branch1 ?
3236 opt->branch2 : opt->branch1);
3237 if (is_add) {
3238 output(opt, clean ? 2 : 1, msg,
3239 ren->pair->one->path, ren->branch,
3240 other_branch, ren->pair->two->path);
3241 return;
3242 }
3243 output(opt, clean ? 2 : 1, msg,
3244 ren->pair->one->path, ren->dir_rename_original_dest, ren->branch,
3245 other_branch, ren->pair->two->path);
3246}
3247static int warn_about_dir_renamed_entries(struct merge_options *opt,
3248 struct rename *ren)
3249{
3250 const char *msg;
3251 int clean = 1, is_add;
3252
3253 if (!ren)
3254 return clean;
3255
3256 /* Return early if ren was not affected/created by a directory rename */
3257 if (!ren->dir_rename_original_dest)
3258 return clean;
3259
3260 /* Sanity checks */
Derrick Stolee8e012512019-08-17 11:41:25 -07003261 assert(opt->detect_directory_renames > MERGE_DIRECTORY_RENAMES_NONE);
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07003262 assert(ren->dir_rename_original_type == 'A' ||
3263 ren->dir_rename_original_type == 'R');
3264
3265 /* Check whether to treat directory renames as a conflict */
Derrick Stolee8e012512019-08-17 11:41:25 -07003266 clean = (opt->detect_directory_renames == MERGE_DIRECTORY_RENAMES_TRUE);
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07003267
3268 is_add = (ren->dir_rename_original_type == 'A');
3269 if (ren->dir_rename_original_type == 'A' && clean) {
3270 msg = _("Path updated: %s added in %s inside a "
3271 "directory that was renamed in %s; moving it to %s.");
3272 } else if (ren->dir_rename_original_type == 'A' && !clean) {
3273 msg = _("CONFLICT (file location): %s added in %s "
3274 "inside a directory that was renamed in %s, "
3275 "suggesting it should perhaps be moved to %s.");
3276 } else if (ren->dir_rename_original_type == 'R' && clean) {
3277 msg = _("Path updated: %s renamed to %s in %s, inside a "
3278 "directory that was renamed in %s; moving it to %s.");
3279 } else if (ren->dir_rename_original_type == 'R' && !clean) {
3280 msg = _("CONFLICT (file location): %s renamed to %s in %s, "
3281 "inside a directory that was renamed in %s, "
3282 "suggesting it should perhaps be moved to %s.");
3283 } else {
3284 BUG("Impossible dir_rename_original_type/clean combination");
3285 }
3286 dir_rename_warning(msg, is_add, clean, opt, ren);
3287
3288 return clean;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003289}
3290
3291/* Per entry merge function */
Elijah Newren259ccb62019-04-05 08:00:13 -07003292static int process_entry(struct merge_options *opt,
Miklos Vajna8a2fce12008-08-25 16:25:57 +02003293 const char *path, struct stage_data *entry)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003294{
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003295 int clean_merge = 1;
Elijah Newren259ccb62019-04-05 08:00:13 -07003296 int normalize = opt->renormalize;
Elijah Newren8daec1d2019-04-05 08:00:22 -07003297
3298 struct diff_filespec *o = &entry->stages[1];
3299 struct diff_filespec *a = &entry->stages[2];
3300 struct diff_filespec *b = &entry->stages[3];
3301 int o_valid = is_valid(o);
3302 int a_valid = is_valid(a);
3303 int b_valid = is_valid(b);
3304 o->path = a->path = b->path = (char*)path;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003305
Elijah Newren37348932010-07-09 07:10:53 -06003306 entry->processed = 1;
Elijah Newren4f66dad2011-08-11 23:20:08 -06003307 if (entry->rename_conflict_info) {
Elijah Newren043622b2019-04-05 08:00:16 -07003308 struct rename_conflict_info *ci = entry->rename_conflict_info;
Elijah Newren8daec1d2019-04-05 08:00:22 -07003309 struct diff_filespec *temp;
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07003310 int path_clean;
3311
3312 path_clean = warn_about_dir_renamed_entries(opt, ci->ren1);
3313 path_clean &= warn_about_dir_renamed_entries(opt, ci->ren2);
Elijah Newren8daec1d2019-04-05 08:00:22 -07003314
3315 /*
3316 * For cases with a single rename, {o,a,b}->path have all been
3317 * set to the rename target path; we need to set two of these
3318 * back to the rename source.
3319 * For rename/rename conflicts, we'll manually fix paths below.
3320 */
3321 temp = (opt->branch1 == ci->ren1->branch) ? b : a;
3322 o->path = temp->path = ci->ren1->pair->one->path;
3323 if (ci->ren2) {
3324 assert(opt->branch1 == ci->ren1->branch);
3325 }
3326
Elijah Newren043622b2019-04-05 08:00:16 -07003327 switch (ci->rename_type) {
Elijah Newren882fd112010-09-20 02:29:03 -06003328 case RENAME_NORMAL:
Elijah Newren4f66dad2011-08-11 23:20:08 -06003329 case RENAME_ONE_FILE_TO_ONE:
Elijah Newren8daec1d2019-04-05 08:00:22 -07003330 clean_merge = handle_rename_normal(opt, path, o, a, b,
Elijah Newren043622b2019-04-05 08:00:16 -07003331 ci);
Elijah Newren882fd112010-09-20 02:29:03 -06003332 break;
Elijah Newren5455c332018-06-09 21:16:14 -07003333 case RENAME_VIA_DIR:
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07003334 clean_merge = handle_rename_via_dir(opt, ci);
Elijah Newren9c0bbb52010-09-20 02:28:56 -06003335 break;
Elijah Newren7f867162018-11-07 20:40:26 -08003336 case RENAME_ADD:
3337 /*
3338 * Probably unclean merge, but if the renamed file
3339 * merges cleanly and the result can then be
3340 * two-way merged cleanly with the added file, I
3341 * guess it's a clean merge?
3342 */
Elijah Newren043622b2019-04-05 08:00:16 -07003343 clean_merge = handle_rename_add(opt, ci);
Elijah Newren7f867162018-11-07 20:40:26 -08003344 break;
Elijah Newren3b130adf2010-09-20 02:29:02 -06003345 case RENAME_DELETE:
3346 clean_merge = 0;
Elijah Newrene2d563d2019-04-05 08:00:21 -07003347 if (handle_rename_delete(opt, ci))
Johannes Schindelin75456f92016-07-26 18:06:21 +02003348 clean_merge = -1;
Elijah Newren3b130adf2010-09-20 02:29:02 -06003349 break;
Elijah Newren07413c52010-09-20 02:29:00 -06003350 case RENAME_ONE_FILE_TO_TWO:
Elijah Newren8daec1d2019-04-05 08:00:22 -07003351 /*
3352 * Manually fix up paths; note:
3353 * ren[12]->pair->one->path are equal.
3354 */
3355 o->path = ci->ren1->pair->one->path;
3356 a->path = ci->ren1->pair->two->path;
3357 b->path = ci->ren2->pair->two->path;
3358
Elijah Newren07413c52010-09-20 02:29:00 -06003359 clean_merge = 0;
Elijah Newren043622b2019-04-05 08:00:16 -07003360 if (handle_rename_rename_1to2(opt, ci))
Johannes Schindelin75456f92016-07-26 18:06:21 +02003361 clean_merge = -1;
Elijah Newren07413c52010-09-20 02:29:00 -06003362 break;
Elijah Newren461f5042011-08-11 23:20:15 -06003363 case RENAME_TWO_FILES_TO_ONE:
Elijah Newrenbbafc9c2018-11-07 20:40:27 -08003364 /*
Elijah Newren8daec1d2019-04-05 08:00:22 -07003365 * Manually fix up paths; note,
3366 * ren[12]->pair->two->path are actually equal.
3367 */
3368 o->path = NULL;
3369 a->path = ci->ren1->pair->two->path;
3370 b->path = ci->ren2->pair->two->path;
3371
3372 /*
Elijah Newrenbbafc9c2018-11-07 20:40:27 -08003373 * Probably unclean merge, but if the two renamed
3374 * files merge cleanly and the two resulting files
3375 * can then be two-way merged cleanly, I guess it's
3376 * a clean merge?
3377 */
Elijah Newren043622b2019-04-05 08:00:16 -07003378 clean_merge = handle_rename_rename_2to1(opt, ci);
Elijah Newren461f5042011-08-11 23:20:15 -06003379 break;
Elijah Newren07413c52010-09-20 02:29:00 -06003380 default:
3381 entry->processed = 0;
3382 break;
3383 }
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07003384 if (path_clean < clean_merge)
3385 clean_merge = path_clean;
Elijah Newren8daec1d2019-04-05 08:00:22 -07003386 } else if (o_valid && (!a_valid || !b_valid)) {
Elijah Newren37348932010-07-09 07:10:53 -06003387 /* Case A: Deleted in one */
Elijah Newren8daec1d2019-04-05 08:00:22 -07003388 if ((!a_valid && !b_valid) ||
3389 (!b_valid && blob_unchanged(opt, o, a, normalize, path)) ||
3390 (!a_valid && blob_unchanged(opt, o, b, normalize, path))) {
Elijah Newren37348932010-07-09 07:10:53 -06003391 /* Deleted in both or deleted in one and
3392 * unchanged in the other */
Elijah Newren8daec1d2019-04-05 08:00:22 -07003393 if (a_valid)
Elijah Newren259ccb62019-04-05 08:00:13 -07003394 output(opt, 2, _("Removing %s"), path);
Elijah Newren37348932010-07-09 07:10:53 -06003395 /* do not touch working file if it did not exist */
Elijah Newren8daec1d2019-04-05 08:00:22 -07003396 remove_file(opt, 1, path, !a_valid);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003397 } else {
Elijah Newrenedd2faf2011-08-11 23:20:07 -06003398 /* Modify/delete; deleted side may have put a directory in the way */
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003399 clean_merge = 0;
Elijah Newren8daec1d2019-04-05 08:00:22 -07003400 if (handle_modify_delete(opt, path, o, a, b))
Johannes Schindelin75456f92016-07-26 18:06:21 +02003401 clean_merge = -1;
Miklos Vajna8a2fce12008-08-25 16:25:57 +02003402 }
Elijah Newren8daec1d2019-04-05 08:00:22 -07003403 } else if ((!o_valid && a_valid && !b_valid) ||
3404 (!o_valid && !a_valid && b_valid)) {
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003405 /* Case B: Added in one. */
Elijah Newrenedd2faf2011-08-11 23:20:07 -06003406 /* [nothing|directory] -> ([nothing|directory], file) */
Miklos Vajna8a2fce12008-08-25 16:25:57 +02003407
Elijah Newren9c0bbb52010-09-20 02:28:56 -06003408 const char *add_branch;
3409 const char *other_branch;
Elijah Newren9c0bbb52010-09-20 02:28:56 -06003410 const char *conf;
Elijah Newren8daec1d2019-04-05 08:00:22 -07003411 const struct diff_filespec *contents;
Elijah Newren37348932010-07-09 07:10:53 -06003412
Elijah Newren8daec1d2019-04-05 08:00:22 -07003413 if (a_valid) {
Elijah Newren259ccb62019-04-05 08:00:13 -07003414 add_branch = opt->branch1;
3415 other_branch = opt->branch2;
Elijah Newren8daec1d2019-04-05 08:00:22 -07003416 contents = a;
Jiang Xin55653a62012-07-25 22:53:13 +08003417 conf = _("file/directory");
Elijah Newren9c0bbb52010-09-20 02:28:56 -06003418 } else {
Elijah Newren259ccb62019-04-05 08:00:13 -07003419 add_branch = opt->branch2;
3420 other_branch = opt->branch1;
Elijah Newren8daec1d2019-04-05 08:00:22 -07003421 contents = b;
Jiang Xin55653a62012-07-25 22:53:13 +08003422 conf = _("directory/file");
Elijah Newren9c0bbb52010-09-20 02:28:56 -06003423 }
Elijah Newren259ccb62019-04-05 08:00:13 -07003424 if (dir_in_way(opt->repo->index, path,
Elijah Newren5bf7e572019-08-17 11:41:41 -07003425 !opt->priv->call_depth && !S_ISGITLINK(a->mode),
Elijah Newrenc641ca62017-11-14 09:31:24 -08003426 0)) {
Elijah Newren259ccb62019-04-05 08:00:13 -07003427 char *new_path = unique_path(opt, path, add_branch);
Elijah Newren9c0bbb52010-09-20 02:28:56 -06003428 clean_merge = 0;
Elijah Newren259ccb62019-04-05 08:00:13 -07003429 output(opt, 1, _("CONFLICT (%s): There is a directory with name %s in %s. "
Jiang Xin55653a62012-07-25 22:53:13 +08003430 "Adding %s as %s"),
Elijah Newren9c0bbb52010-09-20 02:28:56 -06003431 conf, path, other_branch, path, new_path);
Elijah Newren8daec1d2019-04-05 08:00:22 -07003432 if (update_file(opt, 0, contents, new_path))
Johannes Schindelin75456f92016-07-26 18:06:21 +02003433 clean_merge = -1;
Elijah Newren5bf7e572019-08-17 11:41:41 -07003434 else if (opt->priv->call_depth)
Elijah Newren259ccb62019-04-05 08:00:13 -07003435 remove_file_from_index(opt->repo->index, path);
Elijah Newren3d6b8e82011-08-11 23:19:53 -06003436 free(new_path);
Elijah Newren9c0bbb52010-09-20 02:28:56 -06003437 } else {
Elijah Newren259ccb62019-04-05 08:00:13 -07003438 output(opt, 2, _("Adding %s"), path);
Elijah Newren35a74ab2011-08-11 23:20:27 -06003439 /* do not overwrite file if already present */
Elijah Newren8daec1d2019-04-05 08:00:22 -07003440 if (update_file_flags(opt, contents, path, 1, !a_valid))
Johannes Schindelin75456f92016-07-26 18:06:21 +02003441 clean_merge = -1;
Elijah Newren9c0bbb52010-09-20 02:28:56 -06003442 }
Elijah Newren8daec1d2019-04-05 08:00:22 -07003443 } else if (a_valid && b_valid) {
3444 if (!o_valid) {
Elijah Newrendcf28152018-11-07 20:40:28 -08003445 /* Case C: Added in both (check for same permissions) */
Elijah Newren259ccb62019-04-05 08:00:13 -07003446 output(opt, 1,
Elijah Newrendcf28152018-11-07 20:40:28 -08003447 _("CONFLICT (add/add): Merge conflict in %s"),
3448 path);
Elijah Newren259ccb62019-04-05 08:00:13 -07003449 clean_merge = handle_file_collision(opt,
Elijah Newrendcf28152018-11-07 20:40:28 -08003450 path, NULL, NULL,
Elijah Newren259ccb62019-04-05 08:00:13 -07003451 opt->branch1,
3452 opt->branch2,
Elijah Newren8daec1d2019-04-05 08:00:22 -07003453 a, b);
Elijah Newrendcf28152018-11-07 20:40:28 -08003454 } else {
3455 /* case D: Modified in both, but differently. */
Elijah Newrene62d1122019-04-05 08:00:25 -07003456 struct merge_file_info mfi;
Elijah Newrendcf28152018-11-07 20:40:28 -08003457 int is_dirty = 0; /* unpack_trees would have bailed if dirty */
Elijah Newrene62d1122019-04-05 08:00:25 -07003458 clean_merge = handle_content_merge(&mfi, opt, path,
Elijah Newrendcf28152018-11-07 20:40:28 -08003459 is_dirty,
Elijah Newren8daec1d2019-04-05 08:00:22 -07003460 o, a, b, NULL);
Elijah Newrendcf28152018-11-07 20:40:28 -08003461 }
Elijah Newren8daec1d2019-04-05 08:00:22 -07003462 } else if (!o_valid && !a_valid && !b_valid) {
Elijah Newrenedd2faf2011-08-11 23:20:07 -06003463 /*
3464 * this entry was deleted altogether. a_mode == 0 means
3465 * we had that path and want to actively remove it.
3466 */
Elijah Newren8daec1d2019-04-05 08:00:22 -07003467 remove_file(opt, 1, path, !a->mode);
Elijah Newrenedd2faf2011-08-11 23:20:07 -06003468 } else
Johannes Schindelin033abf92018-05-02 11:38:39 +02003469 BUG("fatal merge failure, shouldn't happen.");
Elijah Newren37348932010-07-09 07:10:53 -06003470
3471 return clean_merge;
3472}
3473
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003474static int merge_trees_internal(struct merge_options *opt,
3475 struct tree *head,
3476 struct tree *merge,
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003477 struct tree *merge_base,
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003478 struct tree **result)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003479{
Elijah Newren259ccb62019-04-05 08:00:13 -07003480 struct index_state *istate = opt->repo->index;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003481 int code, clean;
3482
Elijah Newren259ccb62019-04-05 08:00:13 -07003483 if (opt->subtree_shift) {
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003484 merge = shift_tree_object(opt->repo, head, merge,
3485 opt->subtree_shift);
3486 merge_base = shift_tree_object(opt->repo, head, merge_base,
3487 opt->subtree_shift);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003488 }
3489
Elijah Newren763a59e2020-01-01 05:20:57 +00003490 if (oideq(&merge_base->object.oid, &merge->object.oid)) {
Eric Sunshine80cde952021-05-02 01:14:22 -04003491 output(opt, 0, _("Already up to date."));
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003492 *result = head;
3493 return 1;
3494 }
3495
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003496 code = unpack_trees_start(opt, merge_base, head, merge);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003497
Junio C Hamanofadd0692009-09-07 22:43:11 -07003498 if (code != 0) {
Elijah Newren5bf7e572019-08-17 11:41:41 -07003499 if (show(opt, 4) || opt->priv->call_depth)
Elijah Newren259ccb62019-04-05 08:00:13 -07003500 err(opt, _("merging of trees %s and %s failed"),
brian m. carlsonf2fd0762015-11-10 02:22:28 +00003501 oid_to_hex(&head->object.oid),
3502 oid_to_hex(&merge->object.oid));
Elijah Newren259ccb62019-04-05 08:00:13 -07003503 unpack_trees_finish(opt);
Johannes Schindelin60033032016-07-26 18:06:26 +02003504 return -1;
Junio C Hamanofadd0692009-09-07 22:43:11 -07003505 }
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003506
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +07003507 if (unmerged_index(istate)) {
Elijah Newrenf1725892018-04-19 10:58:00 -07003508 struct string_list *entries;
3509 struct rename_info re_info;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003510 int i;
Kevin Willfordfc65b002017-09-07 10:25:56 -06003511 /*
3512 * Only need the hashmap while processing entries, so
3513 * initialize it here and free it when we are done running
3514 * through the entries. Keeping it in the merge_options as
3515 * opposed to decaring a local hashmap is for convenience
3516 * so that we don't have to pass it to around.
3517 */
Elijah Newren5bf7e572019-08-17 11:41:41 -07003518 hashmap_init(&opt->priv->current_file_dir_set, path_hashmap_cmp,
Elijah Newren4d7101e2019-08-17 11:41:33 -07003519 NULL, 512);
Elijah Newren259ccb62019-04-05 08:00:13 -07003520 get_files_dirs(opt, head);
3521 get_files_dirs(opt, merge);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003522
Elijah Newren259ccb62019-04-05 08:00:13 -07003523 entries = get_unmerged(opt->repo->index);
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003524 clean = detect_and_process_renames(opt, merge_base, head, merge,
Elijah Newren8ebe7b02018-06-09 21:16:15 -07003525 entries, &re_info);
Elijah Newren259ccb62019-04-05 08:00:13 -07003526 record_df_conflict_files(opt, entries);
Johannes Schindelin75456f92016-07-26 18:06:21 +02003527 if (clean < 0)
Kevin Willforde336bdc2017-08-28 14:28:27 -06003528 goto cleanup;
Elijah Newrenedd2faf2011-08-11 23:20:07 -06003529 for (i = entries->nr-1; 0 <= i; i--) {
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003530 const char *path = entries->items[i].string;
3531 struct stage_data *e = entries->items[i].util;
Johannes Schindelin75456f92016-07-26 18:06:21 +02003532 if (!e->processed) {
Elijah Newren259ccb62019-04-05 08:00:13 -07003533 int ret = process_entry(opt, path, e);
Johannes Schindelin75456f92016-07-26 18:06:21 +02003534 if (!ret)
3535 clean = 0;
Kevin Willforde336bdc2017-08-28 14:28:27 -06003536 else if (ret < 0) {
3537 clean = ret;
3538 goto cleanup;
3539 }
Johannes Schindelin75456f92016-07-26 18:06:21 +02003540 }
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003541 }
Elijah Newren37348932010-07-09 07:10:53 -06003542 for (i = 0; i < entries->nr; i++) {
Junio C Hamano7edba4c2010-09-20 02:28:35 -06003543 struct stage_data *e = entries->items[i].util;
3544 if (!e->processed)
Johannes Schindelin033abf92018-05-02 11:38:39 +02003545 BUG("unprocessed path??? %s",
Junio C Hamano7edba4c2010-09-20 02:28:35 -06003546 entries->items[i].string);
3547 }
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003548
Elijah Newren93665362018-06-09 21:16:13 -07003549 cleanup:
Elijah Newrenffc16c42018-04-19 10:58:04 -07003550 final_cleanup_renames(&re_info);
Elijah Newrenf1725892018-04-19 10:58:00 -07003551
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003552 string_list_clear(entries, 1);
Elijah Newrenf1725892018-04-19 10:58:00 -07003553 free(entries);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003554
Elijah Newren6da1a252020-11-02 18:55:05 +00003555 hashmap_clear_and_free(&opt->priv->current_file_dir_set,
Eric Wongc8e424c2019-10-06 23:30:40 +00003556 struct path_hashmap_entry, e);
Kevin Willfordfc65b002017-09-07 10:25:56 -06003557
Elijah Newren3f1c1c32018-05-20 12:17:35 +02003558 if (clean < 0) {
Elijah Newren259ccb62019-04-05 08:00:13 -07003559 unpack_trees_finish(opt);
Kevin Willforde336bdc2017-08-28 14:28:27 -06003560 return clean;
Elijah Newren3f1c1c32018-05-20 12:17:35 +02003561 }
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003562 }
3563 else
3564 clean = 1;
3565
Elijah Newren259ccb62019-04-05 08:00:13 -07003566 unpack_trees_finish(opt);
Elijah Newrena35edc82018-04-19 10:58:20 -07003567
Elijah Newren5bf7e572019-08-17 11:41:41 -07003568 if (opt->priv->call_depth &&
Elijah Newren724dd762019-08-17 11:41:32 -07003569 !(*result = write_in_core_index_as_tree(opt->repo)))
Johannes Schindelinfbc87eb2016-07-26 18:06:17 +02003570 return -1;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003571
3572 return clean;
3573}
3574
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003575/*
Elijah Newren56e74342020-08-02 03:14:27 +00003576 * Merge the commits h1 and h2, returning a flag (int) indicating the
3577 * cleanness of the merge. Also, if opt->priv->call_depth, create a
3578 * virtual commit and write its location to *result.
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003579 */
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003580static int merge_recursive_internal(struct merge_options *opt,
3581 struct commit *h1,
3582 struct commit *h2,
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003583 struct commit_list *merge_bases,
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003584 struct commit **result)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003585{
3586 struct commit_list *iter;
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003587 struct commit *merged_merge_bases;
Elijah Newrenbab56872019-08-17 11:41:35 -07003588 struct tree *result_tree;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003589 int clean;
Elijah Newren743474c2019-08-17 11:41:24 -07003590 const char *ancestor_name;
3591 struct strbuf merge_base_abbrev = STRBUF_INIT;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003592
Elijah Newren259ccb62019-04-05 08:00:13 -07003593 if (show(opt, 4)) {
3594 output(opt, 4, _("Merging:"));
3595 output_commit_title(opt, h1);
3596 output_commit_title(opt, h2);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003597 }
3598
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003599 if (!merge_bases) {
Ævar Arnfjörð Bjarmasoncb338c22023-03-28 15:58:47 +02003600 merge_bases = repo_get_merge_bases(the_repository, h1, h2);
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003601 merge_bases = reverse_commit_list(merge_bases);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003602 }
3603
Elijah Newren259ccb62019-04-05 08:00:13 -07003604 if (show(opt, 5)) {
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003605 unsigned cnt = commit_list_count(merge_bases);
Ralf Thielowe0453cd2012-08-05 19:56:38 +02003606
Elijah Newren259ccb62019-04-05 08:00:13 -07003607 output(opt, 5, Q_("found %u common ancestor:",
Ralf Thielowe0453cd2012-08-05 19:56:38 +02003608 "found %u common ancestors:", cnt), cnt);
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003609 for (iter = merge_bases; iter; iter = iter->next)
Elijah Newren259ccb62019-04-05 08:00:13 -07003610 output_commit_title(opt, iter->item);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003611 }
3612
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003613 merged_merge_bases = pop_commit(&merge_bases);
Junio C Hamanoafe8a902022-05-02 09:50:37 -07003614 if (!merged_merge_bases) {
Jonathan Nieder03f622c2011-08-16 13:27:39 -05003615 /* if there is no common ancestor, use an empty tree */
3616 struct tree *tree;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003617
Elijah Newren259ccb62019-04-05 08:00:13 -07003618 tree = lookup_tree(opt->repo, opt->repo->hash_algo->empty_tree);
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003619 merged_merge_bases = make_virtual_commit(opt->repo, tree,
3620 "ancestor");
Elijah Newren743474c2019-08-17 11:41:24 -07003621 ancestor_name = "empty tree";
Elijah Newrenb6570472019-10-07 08:52:11 -07003622 } else if (opt->ancestor && !opt->priv->call_depth) {
Elijah Newren8e4ec332019-10-01 11:17:27 -07003623 ancestor_name = opt->ancestor;
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003624 } else if (merge_bases) {
Elijah Newren743474c2019-08-17 11:41:24 -07003625 ancestor_name = "merged common ancestors";
3626 } else {
3627 strbuf_add_unique_abbrev(&merge_base_abbrev,
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003628 &merged_merge_bases->object.oid,
Elijah Newren743474c2019-08-17 11:41:24 -07003629 DEFAULT_ABBREV);
3630 ancestor_name = merge_base_abbrev.buf;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003631 }
3632
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003633 for (iter = merge_bases; iter; iter = iter->next) {
Miklos Vajna8a2fce12008-08-25 16:25:57 +02003634 const char *saved_b1, *saved_b2;
Elijah Newren5bf7e572019-08-17 11:41:41 -07003635 opt->priv->call_depth++;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003636 /*
3637 * When the merge fails, the result contains files
3638 * with conflict markers. The cleanness flag is
Johannes Schindelinde8946d2016-07-26 18:06:07 +02003639 * ignored (unless indicating an error), it was never
3640 * actually used, as result of merge_trees has always
3641 * overwritten it: the committed "conflicts" were
3642 * already resolved.
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003643 */
Elijah Newren259ccb62019-04-05 08:00:13 -07003644 discard_index(opt->repo->index);
3645 saved_b1 = opt->branch1;
3646 saved_b2 = opt->branch2;
3647 opt->branch1 = "Temporary merge branch 1";
3648 opt->branch2 = "Temporary merge branch 2";
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003649 if (merge_recursive_internal(opt, merged_merge_bases, iter->item,
3650 NULL, &merged_merge_bases) < 0)
Johannes Schindelinde8946d2016-07-26 18:06:07 +02003651 return -1;
Elijah Newren259ccb62019-04-05 08:00:13 -07003652 opt->branch1 = saved_b1;
3653 opt->branch2 = saved_b2;
Elijah Newren5bf7e572019-08-17 11:41:41 -07003654 opt->priv->call_depth--;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003655
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003656 if (!merged_merge_bases)
Elijah Newren259ccb62019-04-05 08:00:13 -07003657 return err(opt, _("merge returned no commit"));
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003658 }
3659
Elijah Newren816147e2021-03-20 00:03:53 +00003660 /*
3661 * FIXME: Since merge_recursive_internal() is only ever called by
3662 * places that ensure the index is loaded first
3663 * (e.g. builtin/merge.c, rebase/sequencer, etc.), in the common
3664 * case where the merge base was unique that means when we get here
3665 * we immediately discard the index and re-read it, which is a
3666 * complete waste of time. We should only be discarding and
3667 * re-reading if we were forced to recurse.
3668 */
Elijah Newren259ccb62019-04-05 08:00:13 -07003669 discard_index(opt->repo->index);
Elijah Newren5bf7e572019-08-17 11:41:41 -07003670 if (!opt->priv->call_depth)
Elijah Newren259ccb62019-04-05 08:00:13 -07003671 repo_read_index(opt->repo);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003672
Elijah Newren743474c2019-08-17 11:41:24 -07003673 opt->ancestor = ancestor_name;
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003674 clean = merge_trees_internal(opt,
3675 repo_get_commit_tree(opt->repo, h1),
3676 repo_get_commit_tree(opt->repo, h2),
3677 repo_get_commit_tree(opt->repo,
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003678 merged_merge_bases),
Elijah Newrenbab56872019-08-17 11:41:35 -07003679 &result_tree);
Elijah Newren743474c2019-08-17 11:41:24 -07003680 strbuf_release(&merge_base_abbrev);
Elijah Newrenb6570472019-10-07 08:52:11 -07003681 opt->ancestor = NULL; /* avoid accidental re-use of opt->ancestor */
Johannes Schindelin6999bc72016-08-01 13:44:57 +02003682 if (clean < 0) {
Elijah Newren259ccb62019-04-05 08:00:13 -07003683 flush_output(opt);
Johannes Schindelinde8946d2016-07-26 18:06:07 +02003684 return clean;
Johannes Schindelin6999bc72016-08-01 13:44:57 +02003685 }
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003686
Elijah Newren5bf7e572019-08-17 11:41:41 -07003687 if (opt->priv->call_depth) {
Elijah Newrenbab56872019-08-17 11:41:35 -07003688 *result = make_virtual_commit(opt->repo, result_tree,
3689 "merged tree");
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003690 commit_list_insert(h1, &(*result)->parents);
3691 commit_list_insert(h2, &(*result)->parents->next);
3692 }
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003693 return clean;
3694}
3695
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003696static int merge_start(struct merge_options *opt, struct tree *head)
3697{
3698 struct strbuf sb = STRBUF_INIT;
3699
Elijah Newren45ef16f2019-08-17 11:41:43 -07003700 /* Sanity checks on opt */
3701 assert(opt->repo);
3702
3703 assert(opt->branch1 && opt->branch2);
3704
3705 assert(opt->detect_renames >= -1 &&
3706 opt->detect_renames <= DIFF_DETECT_COPY);
3707 assert(opt->detect_directory_renames >= MERGE_DIRECTORY_RENAMES_NONE &&
3708 opt->detect_directory_renames <= MERGE_DIRECTORY_RENAMES_TRUE);
3709 assert(opt->rename_limit >= -1);
3710 assert(opt->rename_score >= 0 && opt->rename_score <= MAX_SCORE);
3711 assert(opt->show_rename_progress >= 0 && opt->show_rename_progress <= 1);
3712
3713 assert(opt->xdl_opts >= 0);
3714 assert(opt->recursive_variant >= MERGE_VARIANT_NORMAL &&
3715 opt->recursive_variant <= MERGE_VARIANT_THEIRS);
3716
3717 assert(opt->verbosity >= 0 && opt->verbosity <= 5);
3718 assert(opt->buffer_output <= 2);
3719 assert(opt->obuf.len == 0);
3720
3721 assert(opt->priv == NULL);
3722
Elijah Newren6054d1a2022-02-02 02:37:33 +00003723 /* Not supported; option specific to merge-ort */
3724 assert(!opt->record_conflict_msgs_as_headers);
3725 assert(!opt->msg_header_prefix);
3726
Elijah Newren45ef16f2019-08-17 11:41:43 -07003727 /* Sanity check on repo state; index must match head */
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003728 if (repo_index_has_changes(opt->repo, head, &sb)) {
3729 err(opt, _("Your local changes to the following files would be overwritten by merge:\n %s"),
3730 sb.buf);
3731 strbuf_release(&sb);
3732 return -1;
3733 }
3734
René Scharfeca56dad2021-03-13 17:17:22 +01003735 CALLOC_ARRAY(opt->priv, 1);
Ævar Arnfjörð Bjarmasonbc40dfb2021-07-01 12:51:29 +02003736 string_list_init_dup(&opt->priv->df_conflict_file_set);
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003737 return 0;
3738}
3739
3740static void merge_finalize(struct merge_options *opt)
3741{
Elijah Newrene95e4812019-08-17 11:41:40 -07003742 flush_output(opt);
Elijah Newren5bf7e572019-08-17 11:41:41 -07003743 if (!opt->priv->call_depth && opt->buffer_output < 2)
Elijah Newrene95e4812019-08-17 11:41:40 -07003744 strbuf_release(&opt->obuf);
Elijah Newren345480d2019-08-17 11:41:31 -07003745 if (show(opt, 2))
3746 diff_warn_rename_limit("merge.renamelimit",
Elijah Newren5bf7e572019-08-17 11:41:41 -07003747 opt->priv->needed_rename_limit, 0);
3748 FREE_AND_NULL(opt->priv);
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003749}
3750
3751int merge_trees(struct merge_options *opt,
3752 struct tree *head,
3753 struct tree *merge,
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003754 struct tree *merge_base)
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003755{
3756 int clean;
Elijah Newrenb4db8a22019-08-17 11:41:30 -07003757 struct tree *ignored;
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003758
3759 assert(opt->ancestor != NULL);
3760
3761 if (merge_start(opt, head))
3762 return -1;
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003763 clean = merge_trees_internal(opt, head, merge, merge_base, &ignored);
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003764 merge_finalize(opt);
3765
3766 return clean;
3767}
3768
3769int merge_recursive(struct merge_options *opt,
3770 struct commit *h1,
3771 struct commit *h2,
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003772 struct commit_list *merge_bases,
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003773 struct commit **result)
3774{
3775 int clean;
3776
Elijah Newren8e4ec332019-10-01 11:17:27 -07003777 assert(opt->ancestor == NULL ||
3778 !strcmp(opt->ancestor, "constructed merge base"));
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003779
Derrick Stoleea3380632021-09-08 11:23:57 +00003780 prepare_repo_settings(opt->repo);
3781 opt->repo->settings.command_requires_full_index = 1;
3782
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003783 if (merge_start(opt, repo_get_commit_tree(opt->repo, h1)))
3784 return -1;
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003785 clean = merge_recursive_internal(opt, h1, h2, merge_bases, result);
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003786 merge_finalize(opt);
3787
3788 return clean;
3789}
3790
Elijah Newren4d7101e2019-08-17 11:41:33 -07003791static struct commit *get_ref(struct repository *repo,
3792 const struct object_id *oid,
Nguyễn Thái Ngọc Duyd7cf3a92019-01-12 09:13:30 +07003793 const char *name)
Stephan Beyer73118f82008-08-12 22:13:59 +02003794{
3795 struct object *object;
3796
Nguyễn Thái Ngọc Duyd7cf3a92019-01-12 09:13:30 +07003797 object = deref_tag(repo, parse_object(repo, oid),
3798 name, strlen(name));
Stephan Beyer73118f82008-08-12 22:13:59 +02003799 if (!object)
3800 return NULL;
3801 if (object->type == OBJ_TREE)
Nguyễn Thái Ngọc Duyd7cf3a92019-01-12 09:13:30 +07003802 return make_virtual_commit(repo, (struct tree*)object, name);
Stephan Beyer73118f82008-08-12 22:13:59 +02003803 if (object->type != OBJ_COMMIT)
3804 return NULL;
Ævar Arnfjörð Bjarmason4a93b892023-03-28 15:58:58 +02003805 if (repo_parse_commit(repo, (struct commit *)object))
Stephan Beyer73118f82008-08-12 22:13:59 +02003806 return NULL;
3807 return (struct commit *)object;
3808}
3809
Elijah Newren259ccb62019-04-05 08:00:13 -07003810int merge_recursive_generic(struct merge_options *opt,
brian m. carlson4e8161a2016-06-24 23:09:28 +00003811 const struct object_id *head,
3812 const struct object_id *merge,
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003813 int num_merge_bases,
3814 const struct object_id **merge_bases,
Miklos Vajna8a2fce12008-08-25 16:25:57 +02003815 struct commit **result)
Stephan Beyer73118f82008-08-12 22:13:59 +02003816{
Nguyễn Thái Ngọc Duy03b86642014-06-13 19:19:23 +07003817 int clean;
Martin Ågren837e34e2017-10-05 22:32:04 +02003818 struct lock_file lock = LOCK_INIT;
Elijah Newren259ccb62019-04-05 08:00:13 -07003819 struct commit *head_commit = get_ref(opt->repo, head, opt->branch1);
3820 struct commit *next_commit = get_ref(opt->repo, merge, opt->branch2);
Stephan Beyer73118f82008-08-12 22:13:59 +02003821 struct commit_list *ca = NULL;
3822
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003823 if (merge_bases) {
Stephan Beyer73118f82008-08-12 22:13:59 +02003824 int i;
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003825 for (i = 0; i < num_merge_bases; ++i) {
Stephan Beyer73118f82008-08-12 22:13:59 +02003826 struct commit *base;
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003827 if (!(base = get_ref(opt->repo, merge_bases[i],
3828 oid_to_hex(merge_bases[i]))))
Elijah Newren259ccb62019-04-05 08:00:13 -07003829 return err(opt, _("Could not parse object '%s'"),
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003830 oid_to_hex(merge_bases[i]));
Stephan Beyer73118f82008-08-12 22:13:59 +02003831 commit_list_insert(base, &ca);
3832 }
Elijah Newren8e4ec332019-10-01 11:17:27 -07003833 if (num_merge_bases == 1)
3834 opt->ancestor = "constructed merge base";
Stephan Beyer73118f82008-08-12 22:13:59 +02003835 }
3836
Elijah Newren259ccb62019-04-05 08:00:13 -07003837 repo_hold_locked_index(opt->repo, &lock, LOCK_DIE_ON_ERROR);
3838 clean = merge_recursive(opt, head_commit, next_commit, ca,
Elijah Newrend90e7592018-06-09 21:16:12 -07003839 result);
Martin Ågren51d3f432018-02-28 20:07:56 +01003840 if (clean < 0) {
3841 rollback_lock_file(&lock);
Johannes Schindelinde8946d2016-07-26 18:06:07 +02003842 return clean;
Martin Ågren51d3f432018-02-28 20:07:56 +01003843 }
Johannes Schindelinde8946d2016-07-26 18:06:07 +02003844
Elijah Newren259ccb62019-04-05 08:00:13 -07003845 if (write_locked_index(opt->repo->index, &lock,
Martin Ågren61000812018-03-01 21:40:20 +01003846 COMMIT_LOCK | SKIP_IF_UNCHANGED))
Elijah Newren259ccb62019-04-05 08:00:13 -07003847 return err(opt, _("Unable to write index."));
Stephan Beyer73118f82008-08-12 22:13:59 +02003848
3849 return clean ? 0 : 1;
3850}
3851
Elijah Newren259ccb62019-04-05 08:00:13 -07003852static void merge_recursive_config(struct merge_options *opt)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003853{
Ben Peart85b46032018-05-02 16:01:14 +00003854 char *value = NULL;
Elijah Newren8d552252020-08-03 18:41:19 +00003855 int renormalize = 0;
Elijah Newren259ccb62019-04-05 08:00:13 -07003856 git_config_get_int("merge.verbosity", &opt->verbosity);
Elijah Newren8599ab42019-08-17 11:41:38 -07003857 git_config_get_int("diff.renamelimit", &opt->rename_limit);
3858 git_config_get_int("merge.renamelimit", &opt->rename_limit);
Elijah Newren8d552252020-08-03 18:41:19 +00003859 git_config_get_bool("merge.renormalize", &renormalize);
3860 opt->renormalize = renormalize;
Ben Peart85b46032018-05-02 16:01:14 +00003861 if (!git_config_get_string("diff.renames", &value)) {
Elijah Newren8599ab42019-08-17 11:41:38 -07003862 opt->detect_renames = git_config_rename("diff.renames", value);
Ben Peart85b46032018-05-02 16:01:14 +00003863 free(value);
3864 }
3865 if (!git_config_get_string("merge.renames", &value)) {
Elijah Newren8599ab42019-08-17 11:41:38 -07003866 opt->detect_renames = git_config_rename("merge.renames", value);
Ben Peart85b46032018-05-02 16:01:14 +00003867 free(value);
3868 }
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07003869 if (!git_config_get_string("merge.directoryrenames", &value)) {
3870 int boolval = git_parse_maybe_bool(value);
3871 if (0 <= boolval) {
Derrick Stolee8e012512019-08-17 11:41:25 -07003872 opt->detect_directory_renames = boolval ?
3873 MERGE_DIRECTORY_RENAMES_TRUE :
3874 MERGE_DIRECTORY_RENAMES_NONE;
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07003875 } else if (!strcasecmp(value, "conflict")) {
Derrick Stolee8e012512019-08-17 11:41:25 -07003876 opt->detect_directory_renames =
3877 MERGE_DIRECTORY_RENAMES_CONFLICT;
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07003878 } /* avoid erroring on values from future versions of git */
Tanay Abhra0e7bcb12014-08-13 01:22:01 -07003879 free(value);
3880 }
3881 git_config(git_xmerge_config, NULL);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003882}
3883
Elijah Newren259ccb62019-04-05 08:00:13 -07003884void init_merge_options(struct merge_options *opt,
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +07003885 struct repository *repo)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003886{
Andrey Okoshkin80486222017-10-31 12:09:13 +03003887 const char *merge_verbosity;
Elijah Newren259ccb62019-04-05 08:00:13 -07003888 memset(opt, 0, sizeof(struct merge_options));
Elijah Newrena779fb82019-08-17 11:41:39 -07003889
Elijah Newren259ccb62019-04-05 08:00:13 -07003890 opt->repo = repo;
Elijah Newrena779fb82019-08-17 11:41:39 -07003891
Elijah Newren8599ab42019-08-17 11:41:38 -07003892 opt->detect_renames = -1;
Derrick Stolee8e012512019-08-17 11:41:25 -07003893 opt->detect_directory_renames = MERGE_DIRECTORY_RENAMES_CONFLICT;
Elijah Newrena779fb82019-08-17 11:41:39 -07003894 opt->rename_limit = -1;
3895
Elijah Newren259ccb62019-04-05 08:00:13 -07003896 opt->verbosity = 2;
3897 opt->buffer_output = 1;
Elijah Newrena779fb82019-08-17 11:41:39 -07003898 strbuf_init(&opt->obuf, 0);
3899
Elijah Newren259ccb62019-04-05 08:00:13 -07003900 opt->renormalize = 0;
Elijah Newrena779fb82019-08-17 11:41:39 -07003901
Elijah Newren259ccb62019-04-05 08:00:13 -07003902 merge_recursive_config(opt);
Andrey Okoshkin80486222017-10-31 12:09:13 +03003903 merge_verbosity = getenv("GIT_MERGE_VERBOSITY");
3904 if (merge_verbosity)
Elijah Newren259ccb62019-04-05 08:00:13 -07003905 opt->verbosity = strtol(merge_verbosity, NULL, 10);
3906 if (opt->verbosity >= 5)
3907 opt->buffer_output = 0;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003908}
Jonathan Nieder635a7bb2010-08-26 00:47:58 -05003909
Junio C Hamanob1826582023-10-11 13:37:47 -07003910/*
3911 * For now, members of merge_options do not need deep copying, but
3912 * it may change in the future, in which case we would need to update
3913 * this, and also make a matching change to clear_merge_options() to
3914 * release the resources held by a copied instance.
3915 */
3916void copy_merge_options(struct merge_options *dst, struct merge_options *src)
3917{
3918 *dst = *src;
3919}
3920
3921void clear_merge_options(struct merge_options *opt UNUSED)
3922{
3923 ; /* no-op as our copy is shallow right now */
3924}
3925
Elijah Newren259ccb62019-04-05 08:00:13 -07003926int parse_merge_opt(struct merge_options *opt, const char *s)
Jonathan Nieder635a7bb2010-08-26 00:47:58 -05003927{
Jeff King95b567c2014-06-18 15:48:29 -04003928 const char *arg;
3929
Jonathan Nieder635a7bb2010-08-26 00:47:58 -05003930 if (!s || !*s)
3931 return -1;
3932 if (!strcmp(s, "ours"))
Elijah Newrenf3081da2019-08-17 11:41:42 -07003933 opt->recursive_variant = MERGE_VARIANT_OURS;
Jonathan Nieder635a7bb2010-08-26 00:47:58 -05003934 else if (!strcmp(s, "theirs"))
Elijah Newrenf3081da2019-08-17 11:41:42 -07003935 opt->recursive_variant = MERGE_VARIANT_THEIRS;
Jonathan Nieder635a7bb2010-08-26 00:47:58 -05003936 else if (!strcmp(s, "subtree"))
Elijah Newren259ccb62019-04-05 08:00:13 -07003937 opt->subtree_shift = "";
Jeff King95b567c2014-06-18 15:48:29 -04003938 else if (skip_prefix(s, "subtree=", &arg))
Elijah Newren259ccb62019-04-05 08:00:13 -07003939 opt->subtree_shift = arg;
Justin Frankel58a1ece2010-08-26 00:50:45 -05003940 else if (!strcmp(s, "patience"))
Elijah Newren259ccb62019-04-05 08:00:13 -07003941 opt->xdl_opts = DIFF_WITH_ALG(opt, PATIENCE_DIFF);
Tay Ray Chuan8c912ee2011-07-12 14:10:25 +08003942 else if (!strcmp(s, "histogram"))
Elijah Newren259ccb62019-04-05 08:00:13 -07003943 opt->xdl_opts = DIFF_WITH_ALG(opt, HISTOGRAM_DIFF);
Jeff King95b567c2014-06-18 15:48:29 -04003944 else if (skip_prefix(s, "diff-algorithm=", &arg)) {
3945 long value = parse_algorithm_value(arg);
Michal Privoznik07924d42013-01-16 08:51:58 +01003946 if (value < 0)
3947 return -1;
3948 /* clear out previous settings */
Elijah Newren259ccb62019-04-05 08:00:13 -07003949 DIFF_XDL_CLR(opt, NEED_MINIMAL);
3950 opt->xdl_opts &= ~XDF_DIFF_ALGORITHM_MASK;
3951 opt->xdl_opts |= value;
Michal Privoznik07924d42013-01-16 08:51:58 +01003952 }
Justin Frankel4e5dd042010-08-26 00:51:47 -05003953 else if (!strcmp(s, "ignore-space-change"))
Elijah Newren259ccb62019-04-05 08:00:13 -07003954 DIFF_XDL_SET(opt, IGNORE_WHITESPACE_CHANGE);
Justin Frankel4e5dd042010-08-26 00:51:47 -05003955 else if (!strcmp(s, "ignore-all-space"))
Elijah Newren259ccb62019-04-05 08:00:13 -07003956 DIFF_XDL_SET(opt, IGNORE_WHITESPACE);
Justin Frankel4e5dd042010-08-26 00:51:47 -05003957 else if (!strcmp(s, "ignore-space-at-eol"))
Elijah Newren259ccb62019-04-05 08:00:13 -07003958 DIFF_XDL_SET(opt, IGNORE_WHITESPACE_AT_EOL);
Junio C Hamanoe9282f02017-10-26 15:32:27 +09003959 else if (!strcmp(s, "ignore-cr-at-eol"))
Elijah Newren259ccb62019-04-05 08:00:13 -07003960 DIFF_XDL_SET(opt, IGNORE_CR_AT_EOL);
Jonathan Nieder635a7bb2010-08-26 00:47:58 -05003961 else if (!strcmp(s, "renormalize"))
Elijah Newren259ccb62019-04-05 08:00:13 -07003962 opt->renormalize = 1;
Jonathan Nieder635a7bb2010-08-26 00:47:58 -05003963 else if (!strcmp(s, "no-renormalize"))
Elijah Newren259ccb62019-04-05 08:00:13 -07003964 opt->renormalize = 0;
Felipe Gonçalves Assisd2b11ec2016-02-17 01:15:25 -02003965 else if (!strcmp(s, "no-renames"))
Elijah Newren8599ab42019-08-17 11:41:38 -07003966 opt->detect_renames = 0;
Felipe Gonçalves Assis87892f62016-02-21 19:59:05 -03003967 else if (!strcmp(s, "find-renames")) {
Elijah Newren8599ab42019-08-17 11:41:38 -07003968 opt->detect_renames = 1;
Elijah Newren259ccb62019-04-05 08:00:13 -07003969 opt->rename_score = 0;
Felipe Gonçalves Assis87892f62016-02-21 19:59:05 -03003970 }
Felipe Gonçalves Assis1b47ad12016-02-17 01:15:26 -02003971 else if (skip_prefix(s, "find-renames=", &arg) ||
3972 skip_prefix(s, "rename-threshold=", &arg)) {
Elijah Newren259ccb62019-04-05 08:00:13 -07003973 if ((opt->rename_score = parse_rename_score(&arg)) == -1 || *arg != 0)
Kevin Ballard10ae7522010-09-27 16:58:25 -07003974 return -1;
Elijah Newren8599ab42019-08-17 11:41:38 -07003975 opt->detect_renames = 1;
Kevin Ballard10ae7522010-09-27 16:58:25 -07003976 }
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +07003977 /*
3978 * Please update $__git_merge_strategy_options in
3979 * git-completion.bash when you add new options
3980 */
Jonathan Nieder635a7bb2010-08-26 00:47:58 -05003981 else
3982 return -1;
3983 return 0;
3984}