blob: 8ff29ed09efb3303adf82d39c5fb54a0d11fc897 [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{
Johannes Schindelinaa9f6182024-02-23 08:34:23 +0000408 if (parse_tree(tree) < 0)
409 exit(128);
Eric W. Biedermanefed6872023-10-01 21:40:28 -0500410 init_tree_desc(desc, &tree->object.oid, tree->buffer, tree->size);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200411}
412
Elijah Newren259ccb62019-04-05 08:00:13 -0700413static int unpack_trees_start(struct merge_options *opt,
Elijah Newren3f1c1c32018-05-20 12:17:35 +0200414 struct tree *common,
415 struct tree *head,
416 struct tree *merge)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200417{
418 int rc;
419 struct tree_desc t[3];
Ævar Arnfjörð Bjarmason6269f8e2023-01-17 14:57:00 +0100420 struct index_state tmp_index = INDEX_STATE_INIT(opt->repo);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200421
Elijah Newren5bf7e572019-08-17 11:41:41 -0700422 memset(&opt->priv->unpack_opts, 0, sizeof(opt->priv->unpack_opts));
423 if (opt->priv->call_depth)
424 opt->priv->unpack_opts.index_only = 1;
Elijah Newren491a7572021-09-27 16:33:40 +0000425 else {
Elijah Newren5bf7e572019-08-17 11:41:41 -0700426 opt->priv->unpack_opts.update = 1;
Elijah Newren491a7572021-09-27 16:33:40 +0000427 /* FIXME: should only do this if !overwrite_ignore */
Elijah Newren04988c82021-09-27 16:33:41 +0000428 opt->priv->unpack_opts.preserve_ignored = 0;
Elijah Newren491a7572021-09-27 16:33:40 +0000429 }
Elijah Newren5bf7e572019-08-17 11:41:41 -0700430 opt->priv->unpack_opts.merge = 1;
431 opt->priv->unpack_opts.head_idx = 2;
432 opt->priv->unpack_opts.fn = threeway_merge;
433 opt->priv->unpack_opts.src_index = opt->repo->index;
434 opt->priv->unpack_opts.dst_index = &tmp_index;
435 opt->priv->unpack_opts.aggressive = !merge_detect_rename(opt);
436 setup_unpack_trees_porcelain(&opt->priv->unpack_opts, "merge");
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200437
438 init_tree_desc_from_tree(t+0, common);
439 init_tree_desc_from_tree(t+1, head);
440 init_tree_desc_from_tree(t+2, merge);
441
Elijah Newren5bf7e572019-08-17 11:41:41 -0700442 rc = unpack_trees(3, t, &opt->priv->unpack_opts);
Elijah Newren259ccb62019-04-05 08:00:13 -0700443 cache_tree_free(&opt->repo->index->cache_tree);
Elijah Newrena35edc82018-04-19 10:58:20 -0700444
445 /*
Elijah Newren5bf7e572019-08-17 11:41:41 -0700446 * Update opt->repo->index to match the new results, AFTER saving a
447 * copy in opt->priv->orig_index. Update src_index to point to the
448 * saved copy. (verify_uptodate() checks src_index, and the original
449 * index is the one that had the necessary modification timestamps.)
Elijah Newrena35edc82018-04-19 10:58:20 -0700450 */
Elijah Newren5bf7e572019-08-17 11:41:41 -0700451 opt->priv->orig_index = *opt->repo->index;
Elijah Newren259ccb62019-04-05 08:00:13 -0700452 *opt->repo->index = tmp_index;
Elijah Newren5bf7e572019-08-17 11:41:41 -0700453 opt->priv->unpack_opts.src_index = &opt->priv->orig_index;
Elijah Newrena35edc82018-04-19 10:58:20 -0700454
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200455 return rc;
456}
457
Elijah Newren259ccb62019-04-05 08:00:13 -0700458static void unpack_trees_finish(struct merge_options *opt)
Elijah Newren3f1c1c32018-05-20 12:17:35 +0200459{
Elijah Newren5bf7e572019-08-17 11:41:41 -0700460 discard_index(&opt->priv->orig_index);
461 clear_unpack_trees_porcelain(&opt->priv->unpack_opts);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200462}
463
Ævar Arnfjörð Bjarmason5cf88fd2022-08-25 19:09:48 +0200464static int save_files_dirs(const struct object_id *oid UNUSED,
Elijah Newrend90e7592018-06-09 21:16:12 -0700465 struct strbuf *base, const char *path,
Ævar Arnfjörð Bjarmason47957482021-03-20 23:37:51 +0100466 unsigned int mode, void *context)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200467{
Kevin Willfordfc65b002017-09-07 10:25:56 -0600468 struct path_hashmap_entry *entry;
Nguyễn Thái Ngọc Duy6a0b0b62014-11-30 16:05:00 +0700469 int baselen = base->len;
Elijah Newren259ccb62019-04-05 08:00:13 -0700470 struct merge_options *opt = context;
Miklos Vajna696ee232008-09-03 19:08:56 +0200471
Nguyễn Thái Ngọc Duy6a0b0b62014-11-30 16:05:00 +0700472 strbuf_addstr(base, path);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200473
Kevin Willfordfc65b002017-09-07 10:25:56 -0600474 FLEX_ALLOC_MEM(entry, path, base->buf, base->len);
René Scharfe74318422021-07-30 21:06:58 +0200475 hashmap_entry_init(&entry->e, fspathhash(entry->path));
Junio C Hamano5efabc72019-10-15 13:48:01 +0900476 hashmap_add(&opt->priv->current_file_dir_set, &entry->e);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200477
Nguyễn Thái Ngọc Duy6a0b0b62014-11-30 16:05:00 +0700478 strbuf_setlen(base, baselen);
Lars Hjemlid3bee162009-01-25 01:52:05 +0100479 return (S_ISDIR(mode) ? READ_TREE_RECURSIVE : 0);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200480}
481
Elijah Newren259ccb62019-04-05 08:00:13 -0700482static void get_files_dirs(struct merge_options *opt, struct tree *tree)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200483{
Nguyễn Thái Ngọc Duyf0096c02011-03-25 16:34:19 +0700484 struct pathspec match_all;
Nguyễn Thái Ngọc Duy9a087272013-07-14 15:35:59 +0700485 memset(&match_all, 0, sizeof(match_all));
Ævar Arnfjörð Bjarmason47957482021-03-20 23:37:51 +0100486 read_tree(opt->repo, tree,
487 &match_all, save_files_dirs, opt);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200488}
489
Nguyễn Thái Ngọc Duy34e77712019-06-27 16:28:52 +0700490static int get_tree_entry_if_blob(struct repository *r,
491 const struct object_id *tree,
Elijah Newren5b047ac2018-04-19 10:58:09 -0700492 const char *path,
Elijah Newren8daec1d2019-04-05 08:00:22 -0700493 struct diff_filespec *dfs)
Elijah Newren5b047ac2018-04-19 10:58:09 -0700494{
495 int ret;
496
Nguyễn Thái Ngọc Duy34e77712019-06-27 16:28:52 +0700497 ret = get_tree_entry(r, tree, path, &dfs->oid, &dfs->mode);
Elijah Newren8daec1d2019-04-05 08:00:22 -0700498 if (S_ISDIR(dfs->mode)) {
brian m. carlson14228442021-04-26 01:02:56 +0000499 oidcpy(&dfs->oid, null_oid());
Elijah Newren8daec1d2019-04-05 08:00:22 -0700500 dfs->mode = 0;
Elijah Newren5b047ac2018-04-19 10:58:09 -0700501 }
502 return ret;
503}
504
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200505/*
506 * Returns an index_entry instance which doesn't have to correspond to
507 * a real cache entry in Git's index.
508 */
Nguyễn Thái Ngọc Duy34e77712019-06-27 16:28:52 +0700509static struct stage_data *insert_stage_data(struct repository *r,
510 const char *path,
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200511 struct tree *o, struct tree *a, struct tree *b,
512 struct string_list *entries)
513{
514 struct string_list_item *item;
515 struct stage_data *e = xcalloc(1, sizeof(struct stage_data));
Nguyễn Thái Ngọc Duy34e77712019-06-27 16:28:52 +0700516 get_tree_entry_if_blob(r, &o->object.oid, path, &e->stages[1]);
517 get_tree_entry_if_blob(r, &a->object.oid, path, &e->stages[2]);
518 get_tree_entry_if_blob(r, &b->object.oid, path, &e->stages[3]);
Julian Phillips78a395d2010-06-26 00:41:35 +0100519 item = string_list_insert(entries, path);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200520 item->util = e;
521 return e;
522}
523
524/*
525 * Create a dictionary mapping file names to stage_data objects. The
526 * dictionary contains one entry for every path with a non-zero stage entry.
527 */
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700528static struct string_list *get_unmerged(struct index_state *istate)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200529{
Ævar Arnfjörð Bjarmasonf2605052022-04-13 22:01:32 +0200530 struct string_list *unmerged = xmalloc(sizeof(struct string_list));
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200531 int i;
532
Ævar Arnfjörð Bjarmasonf2605052022-04-13 22:01:32 +0200533 string_list_init_dup(unmerged);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200534
Derrick Stoleef7ef64b2021-04-01 01:49:56 +0000535 /* TODO: audit for interaction with sparse-index. */
536 ensure_full_index(istate);
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700537 for (i = 0; i < istate->cache_nr; i++) {
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200538 struct string_list_item *item;
539 struct stage_data *e;
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700540 const struct cache_entry *ce = istate->cache[i];
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200541 if (!ce_stage(ce))
542 continue;
543
Julian Phillipse8c8b712010-06-26 00:41:37 +0100544 item = string_list_lookup(unmerged, ce->name);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200545 if (!item) {
Julian Phillips78a395d2010-06-26 00:41:35 +0100546 item = string_list_insert(unmerged, ce->name);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200547 item->util = xcalloc(1, sizeof(struct stage_data));
548 }
549 e = item->util;
550 e->stages[ce_stage(ce)].mode = ce->ce_mode;
brian m. carlson99d1a982016-09-05 20:07:52 +0000551 oidcpy(&e->stages[ce_stage(ce)].oid, &ce->oid);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200552 }
553
554 return unmerged;
555}
556
Nguyễn Thái Ngọc Duyfa6ca112016-11-24 18:45:36 +0700557static int string_list_df_name_compare(const char *one, const char *two)
Elijah Newrenef02b312010-09-20 02:29:09 -0600558{
Nguyễn Thái Ngọc Duyfa6ca112016-11-24 18:45:36 +0700559 int onelen = strlen(one);
560 int twolen = strlen(two);
Elijah Newrenf0fd4d02011-08-11 23:19:56 -0600561 /*
562 * Here we only care that entries for D/F conflicts are
563 * adjacent, in particular with the file of the D/F conflict
564 * appearing before files below the corresponding directory.
565 * The order of the rest of the list is irrelevant for us.
Elijah Newrenef02b312010-09-20 02:29:09 -0600566 *
Elijah Newrenf0fd4d02011-08-11 23:19:56 -0600567 * To achieve this, we sort with df_name_compare and provide
568 * the mode S_IFDIR so that D/F conflicts will sort correctly.
569 * We use the mode S_IFDIR for everything else for simplicity,
570 * since in other cases any changes in their order due to
571 * sorting cause no problems for us.
Elijah Newrenef02b312010-09-20 02:29:09 -0600572 */
Nguyễn Thái Ngọc Duyfa6ca112016-11-24 18:45:36 +0700573 int cmp = df_name_compare(one, onelen, S_IFDIR,
574 two, twolen, S_IFDIR);
Elijah Newrenf0fd4d02011-08-11 23:19:56 -0600575 /*
576 * Now that 'foo' and 'foo/bar' compare equal, we have to make sure
577 * that 'foo' comes before 'foo/bar'.
578 */
579 if (cmp)
580 return cmp;
581 return onelen - twolen;
582}
583
Elijah Newren259ccb62019-04-05 08:00:13 -0700584static void record_df_conflict_files(struct merge_options *opt,
Elijah Newren70cc3d32011-08-11 23:19:58 -0600585 struct string_list *entries)
Elijah Newrenef02b312010-09-20 02:29:09 -0600586{
Elijah Newren70cc3d32011-08-11 23:19:58 -0600587 /* If there is a D/F conflict and the file for such a conflict
Elijah Newren2d6bad92018-06-09 21:16:11 -0700588 * currently exists in the working tree, we want to allow it to be
Elijah Newrenedd2faf2011-08-11 23:20:07 -0600589 * removed to make room for the corresponding directory if needed.
590 * The files underneath the directories of such D/F conflicts will
591 * be processed before the corresponding file involved in the D/F
592 * conflict. If the D/F directory ends up being removed by the
593 * merge, then we won't have to touch the D/F file. If the D/F
594 * directory needs to be written to the working copy, then the D/F
595 * file will simply be removed (in make_room_for_path()) to make
596 * room for the necessary paths. Note that if both the directory
597 * and the file need to be present, then the D/F file will be
598 * reinstated with a new unique name at the time it is processed.
Elijah Newrenef02b312010-09-20 02:29:09 -0600599 */
René Scharfeaf4941d2016-08-05 22:42:12 +0200600 struct string_list df_sorted_entries = STRING_LIST_INIT_NODUP;
Elijah Newrenef02b312010-09-20 02:29:09 -0600601 const char *last_file = NULL;
Junio C Hamanoc8516502010-10-21 07:34:33 -0700602 int last_len = 0;
Elijah Newrenef02b312010-09-20 02:29:09 -0600603 int i;
604
Elijah Newren0b30e812011-08-11 23:19:54 -0600605 /*
606 * If we're merging merge-bases, we don't want to bother with
607 * any working directory changes.
608 */
Elijah Newren5bf7e572019-08-17 11:41:41 -0700609 if (opt->priv->call_depth)
Elijah Newren0b30e812011-08-11 23:19:54 -0600610 return;
611
Elijah Newrenf0fd4d02011-08-11 23:19:56 -0600612 /* Ensure D/F conflicts are adjacent in the entries list. */
Elijah Newrenef02b312010-09-20 02:29:09 -0600613 for (i = 0; i < entries->nr; i++) {
Elijah Newrenf701aae2011-08-12 20:23:51 -0600614 struct string_list_item *next = &entries->items[i];
615 string_list_append(&df_sorted_entries, next->string)->util =
616 next->util;
617 }
Nguyễn Thái Ngọc Duyfa6ca112016-11-24 18:45:36 +0700618 df_sorted_entries.cmp = string_list_df_name_compare;
619 string_list_sort(&df_sorted_entries);
Elijah Newrenf0fd4d02011-08-11 23:19:56 -0600620
Elijah Newren5bf7e572019-08-17 11:41:41 -0700621 string_list_clear(&opt->priv->df_conflict_file_set, 1);
Elijah Newrenf701aae2011-08-12 20:23:51 -0600622 for (i = 0; i < df_sorted_entries.nr; i++) {
623 const char *path = df_sorted_entries.items[i].string;
Elijah Newrenef02b312010-09-20 02:29:09 -0600624 int len = strlen(path);
Elijah Newrenf701aae2011-08-12 20:23:51 -0600625 struct stage_data *e = df_sorted_entries.items[i].util;
Elijah Newrenef02b312010-09-20 02:29:09 -0600626
627 /*
628 * Check if last_file & path correspond to a D/F conflict;
629 * i.e. whether path is last_file+'/'+<something>.
Elijah Newren70cc3d32011-08-11 23:19:58 -0600630 * If so, record that it's okay to remove last_file to make
631 * room for path and friends if needed.
Elijah Newrenef02b312010-09-20 02:29:09 -0600632 */
633 if (last_file &&
634 len > last_len &&
635 memcmp(path, last_file, last_len) == 0 &&
636 path[last_len] == '/') {
Elijah Newren5bf7e572019-08-17 11:41:41 -0700637 string_list_insert(&opt->priv->df_conflict_file_set, last_file);
Elijah Newrenef02b312010-09-20 02:29:09 -0600638 }
639
640 /*
641 * Determine whether path could exist as a file in the
642 * working directory as a possible D/F conflict. This
643 * will only occur when it exists in stage 2 as a
644 * file.
645 */
646 if (S_ISREG(e->stages[2].mode) || S_ISLNK(e->stages[2].mode)) {
647 last_file = path;
648 last_len = len;
Elijah Newrenef02b312010-09-20 02:29:09 -0600649 } else {
650 last_file = NULL;
651 }
652 }
Elijah Newrenf701aae2011-08-12 20:23:51 -0600653 string_list_clear(&df_sorted_entries, 0);
Elijah Newrenef02b312010-09-20 02:29:09 -0600654}
655
Johannes Schindelinbc9204d2016-08-01 13:44:37 +0200656static int update_stages(struct merge_options *opt, const char *path,
657 const struct diff_filespec *o,
Elijah Newren650467c2011-08-11 23:19:52 -0600658 const struct diff_filespec *a,
659 const struct diff_filespec *b)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200660{
Elijah Newrenf53d3972011-08-11 23:20:25 -0600661
662 /*
663 * NOTE: It is usually a bad idea to call update_stages on a path
664 * before calling update_file on that same path, since it can
665 * sometimes lead to spurious "refusing to lose untracked file..."
666 * messages from update_file (via make_room_for path via
667 * would_lose_untracked). Instead, reverse the order of the calls
668 * (executing update_file first and then update_stages).
669 */
Elijah Newren650467c2011-08-11 23:19:52 -0600670 int clear = 1;
671 int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_SKIP_DFCHECK;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200672 if (clear)
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700673 if (remove_file_from_index(opt->repo->index, path))
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200674 return -1;
675 if (o)
Elijah Newren8daec1d2019-04-05 08:00:22 -0700676 if (add_cacheinfo(opt, o, path, 1, 0, options))
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200677 return -1;
678 if (a)
Elijah Newren8daec1d2019-04-05 08:00:22 -0700679 if (add_cacheinfo(opt, a, path, 2, 0, options))
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200680 return -1;
681 if (b)
Elijah Newren8daec1d2019-04-05 08:00:22 -0700682 if (add_cacheinfo(opt, b, path, 3, 0, options))
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200683 return -1;
684 return 0;
685}
686
Elijah Newrenb8ddf162011-08-11 23:20:02 -0600687static void update_entry(struct stage_data *entry,
688 struct diff_filespec *o,
689 struct diff_filespec *a,
690 struct diff_filespec *b)
Elijah Newren2ff739f2010-09-20 02:28:54 -0600691{
Elijah Newren2ff739f2010-09-20 02:28:54 -0600692 entry->processed = 0;
693 entry->stages[1].mode = o->mode;
694 entry->stages[2].mode = a->mode;
695 entry->stages[3].mode = b->mode;
brian m. carlsonfd429e92016-06-24 23:09:25 +0000696 oidcpy(&entry->stages[1].oid, &o->oid);
697 oidcpy(&entry->stages[2].oid, &a->oid);
698 oidcpy(&entry->stages[3].oid, &b->oid);
Elijah Newren2ff739f2010-09-20 02:28:54 -0600699}
700
Elijah Newren259ccb62019-04-05 08:00:13 -0700701static int remove_file(struct merge_options *opt, int clean,
Miklos Vajnab7fa51d2008-09-02 23:53:47 +0200702 const char *path, int no_wd)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200703{
Elijah Newren5bf7e572019-08-17 11:41:41 -0700704 int update_cache = opt->priv->call_depth || clean;
705 int update_working_directory = !opt->priv->call_depth && !no_wd;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200706
707 if (update_cache) {
Elijah Newren259ccb62019-04-05 08:00:13 -0700708 if (remove_file_from_index(opt->repo->index, path))
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200709 return -1;
710 }
711 if (update_working_directory) {
David Turnerae352c72014-05-01 20:21:09 -0400712 if (ignore_case) {
713 struct cache_entry *ce;
Elijah Newren259ccb62019-04-05 08:00:13 -0700714 ce = index_file_exists(opt->repo->index, path, strlen(path),
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700715 ignore_case);
Elijah Newren4cba2b02017-11-24 11:59:01 -0800716 if (ce && ce_stage(ce) == 0 && strcmp(path, ce->name))
David Turnerae352c72014-05-01 20:21:09 -0400717 return 0;
718 }
Peter Collingbourne25755e82010-03-26 15:25:35 +0000719 if (remove_path(path))
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200720 return -1;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200721 }
722 return 0;
723}
724
Jeff King45bc1312014-06-19 17:30:26 -0400725/* add a string to a strbuf, but converting "/" to "_" */
726static void add_flattened_path(struct strbuf *out, const char *s)
727{
728 size_t i = out->len;
729 strbuf_addstr(out, s);
730 for (; i < out->len; i++)
731 if (out->buf[i] == '/')
732 out->buf[i] = '_';
733}
734
Elijah Newren4d7101e2019-08-17 11:41:33 -0700735static char *unique_path(struct merge_options *opt,
736 const char *path,
737 const char *branch)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200738{
Kevin Willfordfc65b002017-09-07 10:25:56 -0600739 struct path_hashmap_entry *entry;
Jeff King45bc1312014-06-19 17:30:26 -0400740 struct strbuf newpath = STRBUF_INIT;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200741 int suffix = 0;
Jeff King45bc1312014-06-19 17:30:26 -0400742 size_t base_len;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200743
Jeff King45bc1312014-06-19 17:30:26 -0400744 strbuf_addf(&newpath, "%s~", path);
745 add_flattened_path(&newpath, branch);
746
747 base_len = newpath.len;
Elijah Newren5bf7e572019-08-17 11:41:41 -0700748 while (hashmap_get_from_hash(&opt->priv->current_file_dir_set,
René Scharfe74318422021-07-30 21:06:58 +0200749 fspathhash(newpath.buf), newpath.buf) ||
Elijah Newren5bf7e572019-08-17 11:41:41 -0700750 (!opt->priv->call_depth && file_exists(newpath.buf))) {
Jeff King45bc1312014-06-19 17:30:26 -0400751 strbuf_setlen(&newpath, base_len);
752 strbuf_addf(&newpath, "_%d", suffix++);
753 }
754
Kevin Willfordfc65b002017-09-07 10:25:56 -0600755 FLEX_ALLOC_MEM(entry, path, newpath.buf, newpath.len);
René Scharfe74318422021-07-30 21:06:58 +0200756 hashmap_entry_init(&entry->e, fspathhash(entry->path));
Junio C Hamano5efabc72019-10-15 13:48:01 +0900757 hashmap_add(&opt->priv->current_file_dir_set, &entry->e);
Jeff King45bc1312014-06-19 17:30:26 -0400758 return strbuf_detach(&newpath, NULL);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200759}
760
David Turner5423d2e2016-11-07 13:31:31 -0500761/**
762 * Check whether a directory in the index is in the way of an incoming
763 * file. Return 1 if so. If check_working_copy is non-zero, also
764 * check the working directory. If empty_ok is non-zero, also return
765 * 0 in the case where the working-tree dir exists but is empty.
766 */
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700767static int dir_in_way(struct index_state *istate, const char *path,
768 int check_working_copy, int empty_ok)
Elijah Newrenf2507b42011-08-11 23:19:57 -0600769{
Jeff Kingb4600fb2015-09-24 17:07:43 -0400770 int pos;
771 struct strbuf dirpath = STRBUF_INIT;
Elijah Newrenf2507b42011-08-11 23:19:57 -0600772 struct stat st;
773
Jeff Kingb4600fb2015-09-24 17:07:43 -0400774 strbuf_addstr(&dirpath, path);
775 strbuf_addch(&dirpath, '/');
Elijah Newrenf2507b42011-08-11 23:19:57 -0600776
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700777 pos = index_name_pos(istate, dirpath.buf, dirpath.len);
Elijah Newrenf2507b42011-08-11 23:19:57 -0600778
779 if (pos < 0)
780 pos = -1 - pos;
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700781 if (pos < istate->cache_nr &&
782 !strncmp(dirpath.buf, istate->cache[pos]->name, dirpath.len)) {
Jeff Kingb4600fb2015-09-24 17:07:43 -0400783 strbuf_release(&dirpath);
Elijah Newrenf2507b42011-08-11 23:19:57 -0600784 return 1;
785 }
786
Jeff Kingb4600fb2015-09-24 17:07:43 -0400787 strbuf_release(&dirpath);
David Turner5423d2e2016-11-07 13:31:31 -0500788 return check_working_copy && !lstat(path, &st) && S_ISDIR(st.st_mode) &&
Jonathan Tan83e3ad32019-09-18 13:27:38 -0700789 !(empty_ok && is_empty_dir(path)) &&
790 !has_symlink_leading_path(path, strlen(path));
Elijah Newrenf2507b42011-08-11 23:19:57 -0600791}
792
Elijah Newrena35edc82018-04-19 10:58:20 -0700793/*
Elijah Newren1de70db2018-04-19 10:58:23 -0700794 * Returns whether path was tracked in the index before the merge started,
795 * and its oid and mode match the specified values
796 */
Elijah Newren259ccb62019-04-05 08:00:13 -0700797static int was_tracked_and_matches(struct merge_options *opt, const char *path,
Elijah Newren8daec1d2019-04-05 08:00:22 -0700798 const struct diff_filespec *blob)
Junio C Hamano60c91182008-12-15 02:41:24 -0800799{
Elijah Newren5bf7e572019-08-17 11:41:41 -0700800 int pos = index_name_pos(&opt->priv->orig_index, path, strlen(path));
Elijah Newren1de70db2018-04-19 10:58:23 -0700801 struct cache_entry *ce;
802
803 if (0 > pos)
804 /* we were not tracking this path before the merge */
805 return 0;
806
807 /* See if the file we were tracking before matches */
Elijah Newren5bf7e572019-08-17 11:41:41 -0700808 ce = opt->priv->orig_index.cache[pos];
Elijah Newren763a59e2020-01-01 05:20:57 +0000809 return (oideq(&ce->oid, &blob->oid) && ce->ce_mode == blob->mode);
Elijah Newren1de70db2018-04-19 10:58:23 -0700810}
811
812/*
Elijah Newrena35edc82018-04-19 10:58:20 -0700813 * Returns whether path was tracked in the index before the merge started
814 */
Elijah Newren259ccb62019-04-05 08:00:13 -0700815static int was_tracked(struct merge_options *opt, const char *path)
Junio C Hamano60c91182008-12-15 02:41:24 -0800816{
Elijah Newren5bf7e572019-08-17 11:41:41 -0700817 int pos = index_name_pos(&opt->priv->orig_index, path, strlen(path));
Junio C Hamano60c91182008-12-15 02:41:24 -0800818
Johannes Schindelinf8d83fb2016-07-26 18:05:57 +0200819 if (0 <= pos)
Elijah Newrena35edc82018-04-19 10:58:20 -0700820 /* we were tracking this path before the merge */
Johannes Schindelinf8d83fb2016-07-26 18:05:57 +0200821 return 1;
822
Elijah Newrenaacb82d2011-08-11 23:19:59 -0600823 return 0;
Junio C Hamano60c91182008-12-15 02:41:24 -0800824}
825
Elijah Newren259ccb62019-04-05 08:00:13 -0700826static int would_lose_untracked(struct merge_options *opt, const char *path)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200827{
Elijah Newren259ccb62019-04-05 08:00:13 -0700828 struct index_state *istate = opt->repo->index;
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700829
Elijah Newrena35edc82018-04-19 10:58:20 -0700830 /*
831 * This may look like it can be simplified to:
Elijah Newren259ccb62019-04-05 08:00:13 -0700832 * return !was_tracked(opt, path) && file_exists(path)
Elijah Newrena35edc82018-04-19 10:58:20 -0700833 * but it can't. This function needs to know whether path was in
834 * the working tree due to EITHER having been tracked in the index
835 * before the merge OR having been put into the working copy and
836 * index by unpack_trees(). Due to that either-or requirement, we
837 * check the current index instead of the original one.
838 *
839 * Note that we do not need to worry about merge-recursive itself
840 * updating the index after unpack_trees() and before calling this
841 * function, because we strictly require all code paths in
842 * merge-recursive to update the working tree first and the index
843 * second. Doing otherwise would break
844 * update_file()/would_lose_untracked(); see every comment in this
845 * file which mentions "update_stages".
846 */
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700847 int pos = index_name_pos(istate, path, strlen(path));
Elijah Newrena35edc82018-04-19 10:58:20 -0700848
849 if (pos < 0)
850 pos = -1 - pos;
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700851 while (pos < istate->cache_nr &&
852 !strcmp(path, istate->cache[pos]->name)) {
Elijah Newrena35edc82018-04-19 10:58:20 -0700853 /*
854 * If stage #0, it is definitely tracked.
855 * If it has stage #2 then it was tracked
856 * before this merge started. All other
857 * cases the path was not tracked.
858 */
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +0700859 switch (ce_stage(istate->cache[pos])) {
Elijah Newrena35edc82018-04-19 10:58:20 -0700860 case 0:
861 case 2:
862 return 0;
863 }
864 pos++;
865 }
866 return file_exists(path);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200867}
868
Elijah Newren259ccb62019-04-05 08:00:13 -0700869static int was_dirty(struct merge_options *opt, const char *path)
Elijah Newren64b1abe2018-04-19 10:58:12 -0700870{
871 struct cache_entry *ce;
872 int dirty = 1;
873
Elijah Newren5bf7e572019-08-17 11:41:41 -0700874 if (opt->priv->call_depth || !was_tracked(opt, path))
Elijah Newren64b1abe2018-04-19 10:58:12 -0700875 return !dirty;
876
Elijah Newren5bf7e572019-08-17 11:41:41 -0700877 ce = index_file_exists(opt->priv->unpack_opts.src_index,
Elijah Newren277292d2018-04-19 10:58:21 -0700878 path, strlen(path), ignore_case);
Elijah Newren5bf7e572019-08-17 11:41:41 -0700879 dirty = verify_uptodate(ce, &opt->priv->unpack_opts) != 0;
Elijah Newren64b1abe2018-04-19 10:58:12 -0700880 return dirty;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200881}
882
Elijah Newren259ccb62019-04-05 08:00:13 -0700883static int make_room_for_path(struct merge_options *opt, const char *path)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200884{
Elijah Newrened0148a2011-08-11 23:20:01 -0600885 int status, i;
Jiang Xin55653a62012-07-25 22:53:13 +0800886 const char *msg = _("failed to create path '%s'%s");
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200887
Elijah Newrened0148a2011-08-11 23:20:01 -0600888 /* Unlink any D/F conflict files that are in the way */
Elijah Newren5bf7e572019-08-17 11:41:41 -0700889 for (i = 0; i < opt->priv->df_conflict_file_set.nr; i++) {
890 const char *df_path = opt->priv->df_conflict_file_set.items[i].string;
Elijah Newrened0148a2011-08-11 23:20:01 -0600891 size_t pathlen = strlen(path);
892 size_t df_pathlen = strlen(df_path);
893 if (df_pathlen < pathlen &&
894 path[df_pathlen] == '/' &&
895 strncmp(path, df_path, df_pathlen) == 0) {
Elijah Newren259ccb62019-04-05 08:00:13 -0700896 output(opt, 3,
Jiang Xin55653a62012-07-25 22:53:13 +0800897 _("Removing %s to make room for subdirectory\n"),
Elijah Newrened0148a2011-08-11 23:20:01 -0600898 df_path);
899 unlink(df_path);
Elijah Newren5bf7e572019-08-17 11:41:41 -0700900 unsorted_string_list_delete_item(&opt->priv->df_conflict_file_set,
Elijah Newrened0148a2011-08-11 23:20:01 -0600901 i, 0);
902 break;
903 }
904 }
905
906 /* Make sure leading directories are created */
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200907 status = safe_create_leading_directories_const(path);
908 if (status) {
Johannes Schindelin60033032016-07-26 18:06:26 +0200909 if (status == SCLD_EXISTS)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200910 /* something else exists */
Elijah Newren259ccb62019-04-05 08:00:13 -0700911 return err(opt, msg, path, _(": perhaps a D/F conflict?"));
912 return err(opt, msg, path, "");
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200913 }
914
Junio C Hamano60c91182008-12-15 02:41:24 -0800915 /*
916 * Do not unlink a file in the work tree if we are not
917 * tracking it.
918 */
Elijah Newren259ccb62019-04-05 08:00:13 -0700919 if (would_lose_untracked(opt, path))
920 return err(opt, _("refusing to lose untracked file at '%s'"),
Elijah Newrend90e7592018-06-09 21:16:12 -0700921 path);
Junio C Hamano60c91182008-12-15 02:41:24 -0800922
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200923 /* Successful unlink is good.. */
924 if (!unlink(path))
925 return 0;
926 /* .. and so is no existing file */
927 if (errno == ENOENT)
928 return 0;
929 /* .. but not some other error (who really cares what?) */
Elijah Newren259ccb62019-04-05 08:00:13 -0700930 return err(opt, msg, path, _(": perhaps a D/F conflict?"));
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200931}
932
Elijah Newren259ccb62019-04-05 08:00:13 -0700933static int update_file_flags(struct merge_options *opt,
Elijah Newren8daec1d2019-04-05 08:00:22 -0700934 const struct diff_filespec *contents,
Johannes Schindelin75456f92016-07-26 18:06:21 +0200935 const char *path,
936 int update_cache,
937 int update_wd)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200938{
Johannes Schindelin60033032016-07-26 18:06:26 +0200939 int ret = 0;
940
Elijah Newren5bf7e572019-08-17 11:41:41 -0700941 if (opt->priv->call_depth)
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200942 update_wd = 0;
943
944 if (update_wd) {
945 enum object_type type;
946 void *buf;
947 unsigned long size;
948
Elijah Newren8daec1d2019-04-05 08:00:22 -0700949 if (S_ISGITLINK(contents->mode)) {
Junio C Hamano0c44c942009-04-29 11:08:18 -0700950 /*
951 * We may later decide to recursively descend into
952 * the submodule directory and update its index
953 * and/or work tree, but we do not do that now.
954 */
Heiko Voigt68d03e42010-07-07 15:39:13 +0200955 update_wd = 0;
Junio C Hamano0c44c942009-04-29 11:08:18 -0700956 goto update_index;
Heiko Voigt68d03e42010-07-07 15:39:13 +0200957 }
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200958
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +0200959 buf = repo_read_object_file(the_repository, &contents->oid,
960 &type, &size);
Elijah Newrenf836bf32019-08-17 11:41:26 -0700961 if (!buf) {
962 ret = err(opt, _("cannot read object %s '%s'"),
963 oid_to_hex(&contents->oid), path);
964 goto free_buf;
965 }
Johannes Schindelin60033032016-07-26 18:06:26 +0200966 if (type != OBJ_BLOB) {
Elijah Newren8daec1d2019-04-05 08:00:22 -0700967 ret = err(opt, _("blob expected for %s '%s'"),
968 oid_to_hex(&contents->oid), path);
Johannes Schindelin60033032016-07-26 18:06:26 +0200969 goto free_buf;
970 }
Elijah Newren8daec1d2019-04-05 08:00:22 -0700971 if (S_ISREG(contents->mode)) {
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500972 struct strbuf strbuf = STRBUF_INIT;
Elijah Newren4d7101e2019-08-17 11:41:33 -0700973 if (convert_to_working_tree(opt->repo->index,
brian m. carlsonab90eca2020-03-16 18:05:02 +0000974 path, buf, size, &strbuf, NULL)) {
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200975 free(buf);
976 size = strbuf.len;
977 buf = strbuf_detach(&strbuf, NULL);
978 }
979 }
980
Elijah Newren259ccb62019-04-05 08:00:13 -0700981 if (make_room_for_path(opt, path) < 0) {
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200982 update_wd = 0;
Johannes Schindelin75456f92016-07-26 18:06:21 +0200983 goto free_buf;
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200984 }
Elijah Newren8daec1d2019-04-05 08:00:22 -0700985 if (S_ISREG(contents->mode) ||
986 (!has_symlinks && S_ISLNK(contents->mode))) {
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200987 int fd;
Elijah Newren8daec1d2019-04-05 08:00:22 -0700988 int mode = (contents->mode & 0100 ? 0777 : 0666);
989
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200990 fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);
Johannes Schindelin60033032016-07-26 18:06:26 +0200991 if (fd < 0) {
Elijah Newren259ccb62019-04-05 08:00:13 -0700992 ret = err(opt, _("failed to open '%s': %s"),
Johannes Schindelinbc9204d2016-08-01 13:44:37 +0200993 path, strerror(errno));
Johannes Schindelin60033032016-07-26 18:06:26 +0200994 goto free_buf;
995 }
Thomas Rastf633ea22012-08-03 14:16:25 +0200996 write_in_full(fd, buf, size);
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200997 close(fd);
Elijah Newren8daec1d2019-04-05 08:00:22 -0700998 } else if (S_ISLNK(contents->mode)) {
Miklos Vajna9047ebb2008-08-12 18:45:14 +0200999 char *lnk = xmemdupz(buf, size);
1000 safe_create_leading_directories_const(path);
1001 unlink(path);
Alex Riesen304dcf22008-12-05 01:39:14 +01001002 if (symlink(lnk, path))
Elijah Newren259ccb62019-04-05 08:00:13 -07001003 ret = err(opt, _("failed to symlink '%s': %s"),
Elijah Newrend90e7592018-06-09 21:16:12 -07001004 path, strerror(errno));
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001005 free(lnk);
1006 } else
Elijah Newren259ccb62019-04-05 08:00:13 -07001007 ret = err(opt,
Johannes Schindelinbc9204d2016-08-01 13:44:37 +02001008 _("do not know what to do with %06o %s '%s'"),
Elijah Newren8daec1d2019-04-05 08:00:22 -07001009 contents->mode, oid_to_hex(&contents->oid), path);
Elijah Newren93665362018-06-09 21:16:13 -07001010 free_buf:
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001011 free(buf);
1012 }
Elijah Newren93665362018-06-09 21:16:13 -07001013update_index:
Elijah Newrenfb1c18f2020-02-19 17:04:07 +00001014 if (!ret && update_cache) {
1015 int refresh = (!opt->priv->call_depth &&
1016 contents->mode != S_IFGITLINK);
1017 if (add_cacheinfo(opt, contents, path, 0, refresh,
Elijah Newrenfd53b7ff2018-04-19 10:58:16 -07001018 ADD_CACHE_OK_TO_ADD))
1019 return -1;
Elijah Newrenfb1c18f2020-02-19 17:04:07 +00001020 }
Johannes Schindelin60033032016-07-26 18:06:26 +02001021 return ret;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001022}
1023
Elijah Newren259ccb62019-04-05 08:00:13 -07001024static int update_file(struct merge_options *opt,
Johannes Schindelin75456f92016-07-26 18:06:21 +02001025 int clean,
Elijah Newren8daec1d2019-04-05 08:00:22 -07001026 const struct diff_filespec *contents,
Johannes Schindelin75456f92016-07-26 18:06:21 +02001027 const char *path)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001028{
Elijah Newren8daec1d2019-04-05 08:00:22 -07001029 return update_file_flags(opt, contents, path,
Elijah Newren5bf7e572019-08-17 11:41:41 -07001030 opt->priv->call_depth || clean, !opt->priv->call_depth);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001031}
1032
1033/* Low level file merging, update and removal */
1034
Jonathan Nieder9cba13c2011-03-16 02:08:34 -05001035struct merge_file_info {
Elijah Newren8daec1d2019-04-05 08:00:22 -07001036 struct diff_filespec blob; /* mostly use oid & mode; sometimes path */
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001037 unsigned clean:1,
1038 merge:1;
1039};
1040
Elijah Newren259ccb62019-04-05 08:00:13 -07001041static int merge_3way(struct merge_options *opt,
Miklos Vajnab7fa51d2008-09-02 23:53:47 +02001042 mmbuffer_t *result_buf,
Elijah Newrene3de8882019-04-05 08:00:14 -07001043 const struct diff_filespec *o,
Elijah Newren0c059422011-08-11 23:19:51 -06001044 const struct diff_filespec *a,
1045 const struct diff_filespec *b,
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001046 const char *branch1,
Elijah Newrenb2a79422018-11-07 20:40:24 -08001047 const char *branch2,
1048 const int extra_marker_size)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001049{
1050 mmfile_t orig, src1, src2;
Phillip Wood412aff72024-03-14 17:05:04 +00001051 struct ll_merge_options ll_opts = LL_MERGE_OPTIONS_INIT;
Elijah Newren139ef372019-08-15 14:40:32 -07001052 char *base, *name1, *name2;
Elijah Newren35f69672022-02-02 02:37:30 +00001053 enum ll_merge_result merge_status;
Avery Pennarun8cc5b292009-11-25 21:23:55 -05001054
Elijah Newren259ccb62019-04-05 08:00:13 -07001055 ll_opts.renormalize = opt->renormalize;
Elijah Newrenb2a79422018-11-07 20:40:24 -08001056 ll_opts.extra_marker_size = extra_marker_size;
Elijah Newren259ccb62019-04-05 08:00:13 -07001057 ll_opts.xdl_opts = opt->xdl_opts;
Phillip Wood135cc712024-03-14 17:05:05 +00001058 ll_opts.conflict_style = opt->conflict_style;
Jonathan Nieder712516b2010-08-26 00:49:53 -05001059
Elijah Newren5bf7e572019-08-17 11:41:41 -07001060 if (opt->priv->call_depth) {
Jonathan Nieder712516b2010-08-26 00:49:53 -05001061 ll_opts.virtual_ancestor = 1;
1062 ll_opts.variant = 0;
1063 } else {
Elijah Newren259ccb62019-04-05 08:00:13 -07001064 switch (opt->recursive_variant) {
Elijah Newrenf3081da2019-08-17 11:41:42 -07001065 case MERGE_VARIANT_OURS:
Jonathan Nieder712516b2010-08-26 00:49:53 -05001066 ll_opts.variant = XDL_MERGE_FAVOR_OURS;
Avery Pennarun8cc5b292009-11-25 21:23:55 -05001067 break;
Elijah Newrenf3081da2019-08-17 11:41:42 -07001068 case MERGE_VARIANT_THEIRS:
Jonathan Nieder712516b2010-08-26 00:49:53 -05001069 ll_opts.variant = XDL_MERGE_FAVOR_THEIRS;
Avery Pennarun8cc5b292009-11-25 21:23:55 -05001070 break;
1071 default:
Jonathan Nieder712516b2010-08-26 00:49:53 -05001072 ll_opts.variant = 0;
Avery Pennarun8cc5b292009-11-25 21:23:55 -05001073 break;
1074 }
1075 }
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001076
Elijah Newren139ef372019-08-15 14:40:32 -07001077 assert(a->path && b->path && o->path && opt->ancestor);
1078 if (strcmp(a->path, b->path) || strcmp(a->path, o->path) != 0) {
1079 base = mkpathdup("%s:%s", opt->ancestor, o->path);
Ramsay Jones4e2d0942012-09-04 18:31:14 +01001080 name1 = mkpathdup("%s:%s", branch1, a->path);
1081 name2 = mkpathdup("%s:%s", branch2, b->path);
Martin Renold606475f2009-07-01 22:18:04 +02001082 } else {
Elijah Newren139ef372019-08-15 14:40:32 -07001083 base = mkpathdup("%s", opt->ancestor);
Ramsay Jones4e2d0942012-09-04 18:31:14 +01001084 name1 = mkpathdup("%s", branch1);
1085 name2 = mkpathdup("%s", branch2);
Martin Renold606475f2009-07-01 22:18:04 +02001086 }
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001087
Elijah Newrene3de8882019-04-05 08:00:14 -07001088 read_mmblob(&orig, &o->oid);
brian m. carlsond4493472016-09-05 20:08:02 +00001089 read_mmblob(&src1, &a->oid);
1090 read_mmblob(&src2, &b->oid);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001091
Elijah Newren816147e2021-03-20 00:03:53 +00001092 /*
1093 * FIXME: Using a->path for normalization rules in ll_merge could be
1094 * wrong if we renamed from a->path to b->path. We should use the
1095 * target path for where the file will be written.
1096 */
Elijah Newren139ef372019-08-15 14:40:32 -07001097 merge_status = ll_merge(result_buf, a->path, &orig, base,
Nguyễn Thái Ngọc Duy32eaa462018-09-21 17:57:27 +02001098 &src1, name1, &src2, name2,
Elijah Newren259ccb62019-04-05 08:00:13 -07001099 opt->repo->index, &ll_opts);
Elijah Newren35f69672022-02-02 02:37:30 +00001100 if (merge_status == LL_MERGE_BINARY_CONFLICT)
1101 warning("Cannot merge binary files: %s (%s vs. %s)",
1102 a->path, name1, name2);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001103
Elijah Newren139ef372019-08-15 14:40:32 -07001104 free(base);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001105 free(name1);
1106 free(name2);
1107 free(orig.ptr);
1108 free(src1.ptr);
1109 free(src2.ptr);
1110 return merge_status;
1111}
1112
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +07001113static int find_first_merges(struct repository *repo,
1114 struct object_array *result, const char *path,
Elijah Newrend90e7592018-06-09 21:16:12 -07001115 struct commit *a, struct commit *b)
Stefan Beller18cfc082018-05-15 13:00:28 -07001116{
1117 int i, j;
1118 struct object_array merges = OBJECT_ARRAY_INIT;
1119 struct commit *commit;
1120 int contains_another;
1121
brian m. carlsondb1ba2a2019-02-19 00:04:59 +00001122 char merged_revision[GIT_MAX_HEXSZ + 2];
Stefan Beller18cfc082018-05-15 13:00:28 -07001123 const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path",
1124 "--all", merged_revision, NULL };
1125 struct rev_info revs;
1126 struct setup_revision_opt rev_opts;
1127
1128 memset(result, 0, sizeof(struct object_array));
1129 memset(&rev_opts, 0, sizeof(rev_opts));
1130
1131 /* get all revisions that merge commit a */
1132 xsnprintf(merged_revision, sizeof(merged_revision), "^%s",
Elijah Newrend90e7592018-06-09 21:16:12 -07001133 oid_to_hex(&a->object.oid));
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +07001134 repo_init_revisions(repo, &revs, NULL);
Stefan Beller18cfc082018-05-15 13:00:28 -07001135 /* FIXME: can't handle linked worktrees in submodules yet */
1136 revs.single_worktree = path != NULL;
1137 setup_revisions(ARRAY_SIZE(rev_args)-1, rev_args, &revs, &rev_opts);
1138
1139 /* save all revisions from the above list that contain b */
1140 if (prepare_revision_walk(&revs))
1141 die("revision walk setup failed");
1142 while ((commit = get_revision(&revs)) != NULL) {
1143 struct object *o = &(commit->object);
Johannes Schindelin24876eb2024-02-28 09:44:09 +00001144 int ret = repo_in_merge_bases(repo, b, commit);
1145 if (ret < 0) {
1146 object_array_clear(&merges);
1147 release_revisions(&revs);
1148 return ret;
1149 }
1150 if (ret)
Stefan Beller18cfc082018-05-15 13:00:28 -07001151 add_object_array(o, NULL, &merges);
1152 }
1153 reset_revision_walk();
1154
1155 /* Now we've got all merges that contain a and b. Prune all
1156 * merges that contain another found merge and save them in
1157 * result.
1158 */
1159 for (i = 0; i < merges.nr; i++) {
1160 struct commit *m1 = (struct commit *) merges.objects[i].item;
1161
1162 contains_another = 0;
1163 for (j = 0; j < merges.nr; j++) {
1164 struct commit *m2 = (struct commit *) merges.objects[j].item;
Johannes Schindelin24876eb2024-02-28 09:44:09 +00001165 if (i != j) {
1166 int ret = repo_in_merge_bases(repo, m2, m1);
1167 if (ret < 0) {
1168 object_array_clear(&merges);
1169 release_revisions(&revs);
1170 return ret;
1171 }
1172 if (ret > 0) {
1173 contains_another = 1;
1174 break;
1175 }
Stefan Beller18cfc082018-05-15 13:00:28 -07001176 }
1177 }
1178
1179 if (!contains_another)
1180 add_object_array(merges.objects[i].item, NULL, result);
1181 }
1182
1183 object_array_clear(&merges);
Ævar Arnfjörð Bjarmason2108fe42022-04-13 22:01:36 +02001184 release_revisions(&revs);
Stefan Beller18cfc082018-05-15 13:00:28 -07001185 return result->nr;
1186}
1187
Jonathan Tan155b5172021-10-08 14:08:17 -07001188static void print_commit(struct repository *repo, struct commit *commit)
Stefan Beller18cfc082018-05-15 13:00:28 -07001189{
1190 struct strbuf sb = STRBUF_INIT;
1191 struct pretty_print_context ctx = {0};
1192 ctx.date_mode.type = DATE_NORMAL;
Elijah Newren816147e2021-03-20 00:03:53 +00001193 /* FIXME: Merge this with output_commit_title() */
1194 assert(!merge_remote_util(commit));
Jonathan Tan155b5172021-10-08 14:08:17 -07001195 repo_format_commit_message(repo, commit, " %h: %m %s", &sb, &ctx);
Stefan Beller18cfc082018-05-15 13:00:28 -07001196 fprintf(stderr, "%s\n", sb.buf);
1197 strbuf_release(&sb);
1198}
1199
Elijah Newren8daec1d2019-04-05 08:00:22 -07001200static int is_valid(const struct diff_filespec *dfs)
1201{
1202 return dfs->mode != 0 && !is_null_oid(&dfs->oid);
1203}
1204
Elijah Newren259ccb62019-04-05 08:00:13 -07001205static int merge_submodule(struct merge_options *opt,
Stefan Beller325f3a82018-05-15 13:00:29 -07001206 struct object_id *result, const char *path,
Stefan Beller18cfc082018-05-15 13:00:28 -07001207 const struct object_id *base, const struct object_id *a,
Stefan Beller325f3a82018-05-15 13:00:29 -07001208 const struct object_id *b)
Stefan Beller18cfc082018-05-15 13:00:28 -07001209{
Jonathan Tan10a0d6a2021-09-09 11:47:29 -07001210 struct repository subrepo;
Johannes Schindelin24876eb2024-02-28 09:44:09 +00001211 int ret = 0, ret2;
Stefan Beller18cfc082018-05-15 13:00:28 -07001212 struct commit *commit_base, *commit_a, *commit_b;
1213 int parent_count;
1214 struct object_array merges;
1215
1216 int i;
Elijah Newren5bf7e572019-08-17 11:41:41 -07001217 int search = !opt->priv->call_depth;
Stefan Beller18cfc082018-05-15 13:00:28 -07001218
1219 /* store a in result in case we fail */
Elijah Newren816147e2021-03-20 00:03:53 +00001220 /* FIXME: This is the WRONG resolution for the recursive case when
1221 * we need to be careful to avoid accidentally matching either side.
1222 * Should probably use o instead there, much like we do for merging
1223 * binaries.
1224 */
Stefan Beller18cfc082018-05-15 13:00:28 -07001225 oidcpy(result, a);
1226
1227 /* we can not handle deletion conflicts */
1228 if (is_null_oid(base))
1229 return 0;
1230 if (is_null_oid(a))
1231 return 0;
1232 if (is_null_oid(b))
1233 return 0;
1234
Jonathan Tan10a0d6a2021-09-09 11:47:29 -07001235 if (repo_submodule_init(&subrepo, opt->repo, path, null_oid())) {
1236 output(opt, 1, _("Failed to merge submodule %s (not checked out)"), path);
Stefan Beller18cfc082018-05-15 13:00:28 -07001237 return 0;
1238 }
1239
Jonathan Tan10a0d6a2021-09-09 11:47:29 -07001240 if (!(commit_base = lookup_commit_reference(&subrepo, base)) ||
1241 !(commit_a = lookup_commit_reference(&subrepo, a)) ||
1242 !(commit_b = lookup_commit_reference(&subrepo, b))) {
1243 output(opt, 1, _("Failed to merge submodule %s (commits not present)"), path);
1244 goto cleanup;
1245 }
1246
Stefan Beller18cfc082018-05-15 13:00:28 -07001247 /* check whether both changes are forward */
Johannes Schindelin24876eb2024-02-28 09:44:09 +00001248 ret2 = repo_in_merge_bases(&subrepo, commit_base, commit_a);
1249 if (ret2 < 0) {
1250 output(opt, 1, _("Failed to merge submodule %s (repository corrupt)"), path);
Johannes Schindelin25fd20e2024-03-09 14:09:57 +00001251 ret = -1;
Johannes Schindelin24876eb2024-02-28 09:44:09 +00001252 goto cleanup;
1253 }
1254 if (ret2 > 0)
1255 ret2 = repo_in_merge_bases(&subrepo, commit_base, commit_b);
1256 if (ret2 < 0) {
1257 output(opt, 1, _("Failed to merge submodule %s (repository corrupt)"), path);
Johannes Schindelin25fd20e2024-03-09 14:09:57 +00001258 ret = -1;
Johannes Schindelin24876eb2024-02-28 09:44:09 +00001259 goto cleanup;
1260 }
1261 if (!ret2) {
Elijah Newren259ccb62019-04-05 08:00:13 -07001262 output(opt, 1, _("Failed to merge submodule %s (commits don't follow merge-base)"), path);
Jonathan Tan10a0d6a2021-09-09 11:47:29 -07001263 goto cleanup;
Stefan Beller18cfc082018-05-15 13:00:28 -07001264 }
1265
1266 /* Case #1: a is contained in b or vice versa */
Johannes Schindelin24876eb2024-02-28 09:44:09 +00001267 ret2 = repo_in_merge_bases(&subrepo, commit_a, commit_b);
1268 if (ret2 < 0) {
1269 output(opt, 1, _("Failed to merge submodule %s (repository corrupt)"), path);
Johannes Schindelin25fd20e2024-03-09 14:09:57 +00001270 ret = -1;
Johannes Schindelin24876eb2024-02-28 09:44:09 +00001271 goto cleanup;
1272 }
1273 if (ret2) {
Stefan Beller18cfc082018-05-15 13:00:28 -07001274 oidcpy(result, b);
Elijah Newren259ccb62019-04-05 08:00:13 -07001275 if (show(opt, 3)) {
1276 output(opt, 3, _("Fast-forwarding submodule %s to the following commit:"), path);
Jonathan Tan155b5172021-10-08 14:08:17 -07001277 repo_output_commit_title(opt, &subrepo, commit_b);
Elijah Newren259ccb62019-04-05 08:00:13 -07001278 } else if (show(opt, 2))
1279 output(opt, 2, _("Fast-forwarding submodule %s"), path);
Leif Middelschulte76f42122018-05-17 11:40:08 -07001280 else
1281 ; /* no output */
1282
Jonathan Tan10a0d6a2021-09-09 11:47:29 -07001283 ret = 1;
1284 goto cleanup;
Stefan Beller18cfc082018-05-15 13:00:28 -07001285 }
Johannes Schindelin24876eb2024-02-28 09:44:09 +00001286 ret2 = repo_in_merge_bases(&subrepo, commit_b, commit_a);
1287 if (ret2 < 0) {
1288 output(opt, 1, _("Failed to merge submodule %s (repository corrupt)"), path);
Johannes Schindelin25fd20e2024-03-09 14:09:57 +00001289 ret = -1;
Johannes Schindelin24876eb2024-02-28 09:44:09 +00001290 goto cleanup;
1291 }
1292 if (ret2) {
Stefan Beller18cfc082018-05-15 13:00:28 -07001293 oidcpy(result, a);
Elijah Newren259ccb62019-04-05 08:00:13 -07001294 if (show(opt, 3)) {
1295 output(opt, 3, _("Fast-forwarding submodule %s to the following commit:"), path);
Jonathan Tan155b5172021-10-08 14:08:17 -07001296 repo_output_commit_title(opt, &subrepo, commit_a);
Elijah Newren259ccb62019-04-05 08:00:13 -07001297 } else if (show(opt, 2))
1298 output(opt, 2, _("Fast-forwarding submodule %s"), path);
Leif Middelschulte76f42122018-05-17 11:40:08 -07001299 else
1300 ; /* no output */
1301
Jonathan Tan10a0d6a2021-09-09 11:47:29 -07001302 ret = 1;
1303 goto cleanup;
Stefan Beller18cfc082018-05-15 13:00:28 -07001304 }
1305
1306 /*
1307 * Case #2: There are one or more merges that contain a and b in
1308 * the submodule. If there is only one, then present it as a
1309 * suggestion to the user, but leave it marked unmerged so the
1310 * user needs to confirm the resolution.
1311 */
1312
1313 /* Skip the search if makes no sense to the calling context. */
1314 if (!search)
Jonathan Tan10a0d6a2021-09-09 11:47:29 -07001315 goto cleanup;
Stefan Beller18cfc082018-05-15 13:00:28 -07001316
1317 /* find commit which merges them */
Jonathan Tan10a0d6a2021-09-09 11:47:29 -07001318 parent_count = find_first_merges(&subrepo, &merges, path,
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +07001319 commit_a, commit_b);
Stefan Beller18cfc082018-05-15 13:00:28 -07001320 switch (parent_count) {
Johannes Schindelin25fd20e2024-03-09 14:09:57 +00001321 case -1:
1322 output(opt, 1,_("Failed to merge submodule %s (repository corrupt)"), path);
1323 ret = -1;
1324 break;
Stefan Beller18cfc082018-05-15 13:00:28 -07001325 case 0:
Elijah Newren259ccb62019-04-05 08:00:13 -07001326 output(opt, 1, _("Failed to merge submodule %s (merge following commits not found)"), path);
Stefan Beller18cfc082018-05-15 13:00:28 -07001327 break;
1328
1329 case 1:
Elijah Newren259ccb62019-04-05 08:00:13 -07001330 output(opt, 1, _("Failed to merge submodule %s (not fast-forward)"), path);
1331 output(opt, 2, _("Found a possible merge resolution for the submodule:\n"));
Jonathan Tan155b5172021-10-08 14:08:17 -07001332 print_commit(&subrepo, (struct commit *) merges.objects[0].item);
Elijah Newren259ccb62019-04-05 08:00:13 -07001333 output(opt, 2, _(
Elijah Newrend90e7592018-06-09 21:16:12 -07001334 "If this is correct simply add it to the index "
1335 "for example\n"
1336 "by using:\n\n"
1337 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
1338 "which will accept this suggestion.\n"),
1339 oid_to_hex(&merges.objects[0].item->oid), path);
Stefan Beller18cfc082018-05-15 13:00:28 -07001340 break;
1341
1342 default:
Elijah Newren259ccb62019-04-05 08:00:13 -07001343 output(opt, 1, _("Failed to merge submodule %s (multiple merges found)"), path);
Stefan Beller18cfc082018-05-15 13:00:28 -07001344 for (i = 0; i < merges.nr; i++)
Jonathan Tan155b5172021-10-08 14:08:17 -07001345 print_commit(&subrepo, (struct commit *) merges.objects[i].item);
Stefan Beller18cfc082018-05-15 13:00:28 -07001346 }
1347
1348 object_array_clear(&merges);
Jonathan Tan10a0d6a2021-09-09 11:47:29 -07001349cleanup:
1350 repo_clear(&subrepo);
1351 return ret;
Stefan Beller18cfc082018-05-15 13:00:28 -07001352}
1353
Elijah Newren259ccb62019-04-05 08:00:13 -07001354static int merge_mode_and_contents(struct merge_options *opt,
Elijah Newrene3de8882019-04-05 08:00:14 -07001355 const struct diff_filespec *o,
Elijah Newrend9573552018-09-19 09:14:34 -07001356 const struct diff_filespec *a,
1357 const struct diff_filespec *b,
1358 const char *filename,
1359 const char *branch1,
1360 const char *branch2,
Elijah Newrenb2a79422018-11-07 20:40:24 -08001361 const int extra_marker_size,
Elijah Newrend9573552018-09-19 09:14:34 -07001362 struct merge_file_info *result)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001363{
Elijah Newren259ccb62019-04-05 08:00:13 -07001364 if (opt->branch1 != branch1) {
Elijah Newren4f445452018-10-16 13:19:48 -07001365 /*
1366 * It's weird getting a reverse merge with HEAD on the bottom
1367 * side of the conflict markers and the other branch on the
1368 * top. Fix that.
1369 */
Elijah Newrene3de8882019-04-05 08:00:14 -07001370 return merge_mode_and_contents(opt, o, b, a,
Elijah Newren4f445452018-10-16 13:19:48 -07001371 filename,
Elijah Newrenb2a79422018-11-07 20:40:24 -08001372 branch2, branch1,
1373 extra_marker_size, result);
Elijah Newren4f445452018-10-16 13:19:48 -07001374 }
1375
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +02001376 result->merge = 0;
1377 result->clean = 1;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001378
1379 if ((S_IFMT & a->mode) != (S_IFMT & b->mode)) {
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +02001380 result->clean = 0;
Elijah Newren816147e2021-03-20 00:03:53 +00001381 /*
1382 * FIXME: This is a bad resolution for recursive case; for
1383 * the recursive case we want something that is unlikely to
1384 * accidentally match either side. Also, while it makes
1385 * sense to prefer regular files over symlinks, it doesn't
1386 * make sense to prefer regular files over submodules.
1387 */
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001388 if (S_ISREG(a->mode)) {
Elijah Newren8daec1d2019-04-05 08:00:22 -07001389 result->blob.mode = a->mode;
1390 oidcpy(&result->blob.oid, &a->oid);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001391 } else {
Elijah Newren8daec1d2019-04-05 08:00:22 -07001392 result->blob.mode = b->mode;
1393 oidcpy(&result->blob.oid, &b->oid);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001394 }
1395 } else {
Elijah Newren763a59e2020-01-01 05:20:57 +00001396 if (!oideq(&a->oid, &o->oid) && !oideq(&b->oid, &o->oid))
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +02001397 result->merge = 1;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001398
1399 /*
1400 * Merge modes
1401 */
Elijah Newrene3de8882019-04-05 08:00:14 -07001402 if (a->mode == b->mode || a->mode == o->mode)
Elijah Newren8daec1d2019-04-05 08:00:22 -07001403 result->blob.mode = b->mode;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001404 else {
Elijah Newren8daec1d2019-04-05 08:00:22 -07001405 result->blob.mode = a->mode;
Elijah Newrene3de8882019-04-05 08:00:14 -07001406 if (b->mode != o->mode) {
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +02001407 result->clean = 0;
1408 result->merge = 1;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001409 }
1410 }
1411
Elijah Newren763a59e2020-01-01 05:20:57 +00001412 if (oideq(&a->oid, &b->oid) || oideq(&a->oid, &o->oid))
Elijah Newren8daec1d2019-04-05 08:00:22 -07001413 oidcpy(&result->blob.oid, &b->oid);
Elijah Newren763a59e2020-01-01 05:20:57 +00001414 else if (oideq(&b->oid, &o->oid))
Elijah Newren8daec1d2019-04-05 08:00:22 -07001415 oidcpy(&result->blob.oid, &a->oid);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001416 else if (S_ISREG(a->mode)) {
1417 mmbuffer_t result_buf;
Johannes Schindelin60033032016-07-26 18:06:26 +02001418 int ret = 0, merge_status;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001419
Elijah Newrene3de8882019-04-05 08:00:14 -07001420 merge_status = merge_3way(opt, &result_buf, o, a, b,
Elijah Newrenb2a79422018-11-07 20:40:24 -08001421 branch1, branch2,
1422 extra_marker_size);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001423
1424 if ((merge_status < 0) || !result_buf.ptr)
Jeff King24c5a272023-09-16 18:11:46 -04001425 ret = err(opt, _("failed to execute internal merge"));
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001426
Patryk Obaraa09c9852018-01-28 01:13:19 +01001427 if (!ret &&
1428 write_object_file(result_buf.ptr, result_buf.size,
Ævar Arnfjörð Bjarmasonc80d2262022-02-05 00:48:26 +01001429 OBJ_BLOB, &result->blob.oid))
Jeff King24c5a272023-09-16 18:11:46 -04001430 ret = err(opt, _("unable to add %s to database"),
Johannes Schindelinbc9204d2016-08-01 13:44:37 +02001431 a->path);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001432
1433 free(result_buf.ptr);
Johannes Schindelin60033032016-07-26 18:06:26 +02001434 if (ret)
1435 return ret;
Elijah Newren816147e2021-03-20 00:03:53 +00001436 /* FIXME: bug, what if modes didn't match? */
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +02001437 result->clean = (merge_status == 0);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001438 } else if (S_ISGITLINK(a->mode)) {
Johannes Schindelin81a34cb2024-03-09 14:09:56 +00001439 int clean = merge_submodule(opt, &result->blob.oid,
1440 o->path,
1441 &o->oid,
1442 &a->oid,
1443 &b->oid);
1444 if (clean < 0)
Johannes Schindelin24876eb2024-02-28 09:44:09 +00001445 return -1;
Johannes Schindelin81a34cb2024-03-09 14:09:56 +00001446 result->clean = clean;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001447 } else if (S_ISLNK(a->mode)) {
Elijah Newren259ccb62019-04-05 08:00:13 -07001448 switch (opt->recursive_variant) {
Elijah Newrenf3081da2019-08-17 11:41:42 -07001449 case MERGE_VARIANT_NORMAL:
Elijah Newren8daec1d2019-04-05 08:00:22 -07001450 oidcpy(&result->blob.oid, &a->oid);
Elijah Newren763a59e2020-01-01 05:20:57 +00001451 if (!oideq(&a->oid, &b->oid))
Junio C Hamanofd48b462017-09-26 11:40:42 +09001452 result->clean = 0;
1453 break;
Elijah Newrenf3081da2019-08-17 11:41:42 -07001454 case MERGE_VARIANT_OURS:
Elijah Newren8daec1d2019-04-05 08:00:22 -07001455 oidcpy(&result->blob.oid, &a->oid);
Junio C Hamanofd48b462017-09-26 11:40:42 +09001456 break;
Elijah Newrenf3081da2019-08-17 11:41:42 -07001457 case MERGE_VARIANT_THEIRS:
Elijah Newren8daec1d2019-04-05 08:00:22 -07001458 oidcpy(&result->blob.oid, &b->oid);
Junio C Hamanofd48b462017-09-26 11:40:42 +09001459 break;
1460 }
Johannes Schindelinef1177d12016-07-26 18:05:50 +02001461 } else
Johannes Schindelin033abf92018-05-02 11:38:39 +02001462 BUG("unsupported object type in the tree");
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001463 }
1464
Elijah Newren05cf21e2018-04-19 10:58:22 -07001465 if (result->merge)
Elijah Newren259ccb62019-04-05 08:00:13 -07001466 output(opt, 2, _("Auto-merging %s"), filename);
Elijah Newren05cf21e2018-04-19 10:58:22 -07001467
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +02001468 return 0;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001469}
1470
Elijah Newren259ccb62019-04-05 08:00:13 -07001471static int handle_rename_via_dir(struct merge_options *opt,
Elijah Newrene2d563d2019-04-05 08:00:21 -07001472 struct rename_conflict_info *ci)
Elijah Newren9c0743f2018-04-19 10:58:10 -07001473{
Elijah Newren5455c332018-06-09 21:16:14 -07001474 /*
1475 * Handle file adds that need to be renamed due to directory rename
1476 * detection. This differs from handle_rename_normal, because
1477 * there is no content merge to do; just move the file into the
1478 * desired final location.
1479 */
Elijah Newrene2d563d2019-04-05 08:00:21 -07001480 const struct rename *ren = ci->ren1;
1481 const struct diff_filespec *dest = ren->pair->two;
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07001482 char *file_path = dest->path;
Derrick Stolee8e012512019-08-17 11:41:25 -07001483 int mark_conflicted = (opt->detect_directory_renames ==
1484 MERGE_DIRECTORY_RENAMES_CONFLICT);
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07001485 assert(ren->dir_rename_original_dest);
Elijah Newren9c0743f2018-04-19 10:58:10 -07001486
Elijah Newren5bf7e572019-08-17 11:41:41 -07001487 if (!opt->priv->call_depth && would_lose_untracked(opt, dest->path)) {
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07001488 mark_conflicted = 1;
1489 file_path = unique_path(opt, dest->path, ren->branch);
Elijah Newren259ccb62019-04-05 08:00:13 -07001490 output(opt, 1, _("Error: Refusing to lose untracked file at %s; "
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07001491 "writing to %s instead."),
1492 dest->path, file_path);
Elijah Newren79c47592018-04-19 10:58:11 -07001493 }
1494
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07001495 if (mark_conflicted) {
1496 /*
1497 * Write the file in worktree at file_path. In the index,
1498 * only record the file at dest->path in the appropriate
1499 * higher stage.
1500 */
1501 if (update_file(opt, 0, dest, file_path))
1502 return -1;
1503 if (file_path != dest->path)
1504 free(file_path);
1505 if (update_stages(opt, dest->path, NULL,
1506 ren->branch == opt->branch1 ? dest : NULL,
1507 ren->branch == opt->branch1 ? NULL : dest))
1508 return -1;
1509 return 0; /* not clean, but conflicted */
1510 } else {
1511 /* Update dest->path both in index and in worktree */
1512 if (update_file(opt, 1, dest, dest->path))
1513 return -1;
1514 return 1; /* clean */
1515 }
Elijah Newren6bdaead2011-08-11 23:20:12 -06001516}
1517
Elijah Newren259ccb62019-04-05 08:00:13 -07001518static int handle_change_delete(struct merge_options *opt,
Elijah Newrend90e7592018-06-09 21:16:12 -07001519 const char *path, const char *old_path,
Elijah Newren8daec1d2019-04-05 08:00:22 -07001520 const struct diff_filespec *o,
1521 const struct diff_filespec *changed,
Elijah Newrend90e7592018-06-09 21:16:12 -07001522 const char *change_branch,
1523 const char *delete_branch,
1524 const char *change, const char *change_past)
Elijah Newrenb7033252011-08-11 23:20:19 -06001525{
Matt McCutchenb26d87f2017-01-28 15:37:01 -05001526 char *alt_path = NULL;
1527 const char *update_path = path;
Johannes Schindelin75456f92016-07-26 18:06:21 +02001528 int ret = 0;
Matt McCutchenb26d87f2017-01-28 15:37:01 -05001529
Elijah Newren5bf7e572019-08-17 11:41:41 -07001530 if (dir_in_way(opt->repo->index, path, !opt->priv->call_depth, 0) ||
1531 (!opt->priv->call_depth && would_lose_untracked(opt, path))) {
Elijah Newren259ccb62019-04-05 08:00:13 -07001532 update_path = alt_path = unique_path(opt, path, change_branch);
Elijah Newrenb7033252011-08-11 23:20:19 -06001533 }
1534
Elijah Newren5bf7e572019-08-17 11:41:41 -07001535 if (opt->priv->call_depth) {
Elijah Newrenb7033252011-08-11 23:20:19 -06001536 /*
1537 * We cannot arbitrarily accept either a_sha or b_sha as
1538 * correct; since there is no true "middle point" between
1539 * them, simply reuse the base version for virtual merge base.
1540 */
Elijah Newren259ccb62019-04-05 08:00:13 -07001541 ret = remove_file_from_index(opt->repo->index, path);
Johannes Schindelin75456f92016-07-26 18:06:21 +02001542 if (!ret)
Elijah Newren8daec1d2019-04-05 08:00:22 -07001543 ret = update_file(opt, 0, o, update_path);
Elijah Newrenb7033252011-08-11 23:20:19 -06001544 } else {
Elijah Newren5b26c3c2018-06-09 21:16:16 -07001545 /*
1546 * Despite the four nearly duplicate messages and argument
1547 * lists below and the ugliness of the nested if-statements,
1548 * having complete messages makes the job easier for
1549 * translators.
1550 *
1551 * The slight variance among the cases is due to the fact
1552 * that:
1553 * 1) directory/file conflicts (in effect if
1554 * !alt_path) could cause us to need to write the
1555 * file to a different path.
1556 * 2) renames (in effect if !old_path) could mean that
1557 * there are two names for the path that the user
1558 * may know the file by.
1559 */
Matt McCutchenb26d87f2017-01-28 15:37:01 -05001560 if (!alt_path) {
1561 if (!old_path) {
Elijah Newren259ccb62019-04-05 08:00:13 -07001562 output(opt, 1, _("CONFLICT (%s/delete): %s deleted in %s "
Matt McCutchenb26d87f2017-01-28 15:37:01 -05001563 "and %s in %s. Version %s of %s left in tree."),
1564 change, path, delete_branch, change_past,
1565 change_branch, change_branch, path);
1566 } else {
Elijah Newren259ccb62019-04-05 08:00:13 -07001567 output(opt, 1, _("CONFLICT (%s/delete): %s deleted in %s "
Matt McCutchenb26d87f2017-01-28 15:37:01 -05001568 "and %s to %s in %s. Version %s of %s left in tree."),
1569 change, old_path, delete_branch, change_past, path,
1570 change_branch, change_branch, path);
1571 }
Jiang Xin55653a62012-07-25 22:53:13 +08001572 } else {
Matt McCutchenb26d87f2017-01-28 15:37:01 -05001573 if (!old_path) {
Elijah Newren259ccb62019-04-05 08:00:13 -07001574 output(opt, 1, _("CONFLICT (%s/delete): %s deleted in %s "
Matt McCutchenb26d87f2017-01-28 15:37:01 -05001575 "and %s in %s. Version %s of %s left in tree at %s."),
1576 change, path, delete_branch, change_past,
1577 change_branch, change_branch, path, alt_path);
1578 } else {
Elijah Newren259ccb62019-04-05 08:00:13 -07001579 output(opt, 1, _("CONFLICT (%s/delete): %s deleted in %s "
Matt McCutchenb26d87f2017-01-28 15:37:01 -05001580 "and %s to %s in %s. Version %s of %s left in tree at %s."),
1581 change, old_path, delete_branch, change_past, path,
1582 change_branch, change_branch, path, alt_path);
1583 }
Jiang Xin55653a62012-07-25 22:53:13 +08001584 }
Elijah Newren35a74ab2011-08-11 23:20:27 -06001585 /*
Matt McCutchenb26d87f2017-01-28 15:37:01 -05001586 * No need to call update_file() on path when change_branch ==
Elijah Newren259ccb62019-04-05 08:00:13 -07001587 * opt->branch1 && !alt_path, since that would needlessly touch
Matt McCutchenb26d87f2017-01-28 15:37:01 -05001588 * path. We could call update_file_flags() with update_cache=0
1589 * and update_wd=0, but that's a no-op.
Elijah Newren35a74ab2011-08-11 23:20:27 -06001590 */
Elijah Newren259ccb62019-04-05 08:00:13 -07001591 if (change_branch != opt->branch1 || alt_path)
Elijah Newren8daec1d2019-04-05 08:00:22 -07001592 ret = update_file(opt, 0, changed, update_path);
Elijah Newrenb7033252011-08-11 23:20:19 -06001593 }
Matt McCutchenb26d87f2017-01-28 15:37:01 -05001594 free(alt_path);
Johannes Schindelin75456f92016-07-26 18:06:21 +02001595
1596 return ret;
Elijah Newrenb7033252011-08-11 23:20:19 -06001597}
1598
Elijah Newren259ccb62019-04-05 08:00:13 -07001599static int handle_rename_delete(struct merge_options *opt,
Elijah Newrene2d563d2019-04-05 08:00:21 -07001600 struct rename_conflict_info *ci)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001601{
Elijah Newrene2d563d2019-04-05 08:00:21 -07001602 const struct rename *ren = ci->ren1;
1603 const struct diff_filespec *orig = ren->pair->one;
1604 const struct diff_filespec *dest = ren->pair->two;
1605 const char *rename_branch = ren->branch;
1606 const char *delete_branch = (opt->branch1 == ren->branch ?
1607 opt->branch2 : opt->branch1);
Elijah Newrene03acb82011-08-11 23:20:20 -06001608
Elijah Newren259ccb62019-04-05 08:00:13 -07001609 if (handle_change_delete(opt,
Elijah Newren5bf7e572019-08-17 11:41:41 -07001610 opt->priv->call_depth ? orig->path : dest->path,
1611 opt->priv->call_depth ? NULL : orig->path,
Elijah Newren8daec1d2019-04-05 08:00:22 -07001612 orig, dest,
Matt McCutchenb26d87f2017-01-28 15:37:01 -05001613 rename_branch, delete_branch,
Johannes Schindelin75456f92016-07-26 18:06:21 +02001614 _("rename"), _("renamed")))
1615 return -1;
Elijah Newrenf53d3972011-08-11 23:20:25 -06001616
Elijah Newren5bf7e572019-08-17 11:41:41 -07001617 if (opt->priv->call_depth)
Elijah Newren259ccb62019-04-05 08:00:13 -07001618 return remove_file_from_index(opt->repo->index, dest->path);
Johannes Schindelin75456f92016-07-26 18:06:21 +02001619 else
Elijah Newren259ccb62019-04-05 08:00:13 -07001620 return update_stages(opt, dest->path, NULL,
1621 rename_branch == opt->branch1 ? dest : NULL,
1622 rename_branch == opt->branch1 ? NULL : dest);
Elijah Newren6ef2cb02010-09-20 02:28:50 -06001623}
1624
Elijah Newren259ccb62019-04-05 08:00:13 -07001625static int handle_file_collision(struct merge_options *opt,
Elijah Newren37b65ce2018-11-07 20:40:25 -08001626 const char *collide_path,
1627 const char *prev_path1,
1628 const char *prev_path2,
1629 const char *branch1, const char *branch2,
Elijah Newren8daec1d2019-04-05 08:00:22 -07001630 struct diff_filespec *a,
1631 struct diff_filespec *b)
Elijah Newren3672c972011-08-11 23:20:22 -06001632{
Elijah Newren37b65ce2018-11-07 20:40:25 -08001633 struct merge_file_info mfi;
Elijah Newren8daec1d2019-04-05 08:00:22 -07001634 struct diff_filespec null;
Elijah Newren37b65ce2018-11-07 20:40:25 -08001635 char *alt_path = NULL;
1636 const char *update_path = collide_path;
Elijah Newren3672c972011-08-11 23:20:22 -06001637
Elijah Newren37b65ce2018-11-07 20:40:25 -08001638 /*
Elijah Newren7f867162018-11-07 20:40:26 -08001639 * It's easiest to get the correct things into stage 2 and 3, and
1640 * to make sure that the content merge puts HEAD before the other
Elijah Newren259ccb62019-04-05 08:00:13 -07001641 * branch if we just ensure that branch1 == opt->branch1. So, simply
Elijah Newren7f867162018-11-07 20:40:26 -08001642 * flip arguments around if we don't have that.
1643 */
Elijah Newren259ccb62019-04-05 08:00:13 -07001644 if (branch1 != opt->branch1) {
1645 return handle_file_collision(opt, collide_path,
Elijah Newren7f867162018-11-07 20:40:26 -08001646 prev_path2, prev_path1,
1647 branch2, branch1,
Elijah Newren8daec1d2019-04-05 08:00:22 -07001648 b, a);
Elijah Newren3672c972011-08-11 23:20:22 -06001649 }
1650
Elijah Newren37b65ce2018-11-07 20:40:25 -08001651 /* Remove rename sources if rename/add or rename/rename(2to1) */
1652 if (prev_path1)
Elijah Newren259ccb62019-04-05 08:00:13 -07001653 remove_file(opt, 1, prev_path1,
Elijah Newren5bf7e572019-08-17 11:41:41 -07001654 opt->priv->call_depth || would_lose_untracked(opt, prev_path1));
Elijah Newren37b65ce2018-11-07 20:40:25 -08001655 if (prev_path2)
Elijah Newren259ccb62019-04-05 08:00:13 -07001656 remove_file(opt, 1, prev_path2,
Elijah Newren5bf7e572019-08-17 11:41:41 -07001657 opt->priv->call_depth || would_lose_untracked(opt, prev_path2));
Elijah Newren37b65ce2018-11-07 20:40:25 -08001658
1659 /*
1660 * Remove the collision path, if it wouldn't cause dirty contents
1661 * or an untracked file to get lost. We'll either overwrite with
1662 * merged contents, or just write out to differently named files.
1663 */
Elijah Newren259ccb62019-04-05 08:00:13 -07001664 if (was_dirty(opt, collide_path)) {
1665 output(opt, 1, _("Refusing to lose dirty file at %s"),
Elijah Newren37b65ce2018-11-07 20:40:25 -08001666 collide_path);
Elijah Newren259ccb62019-04-05 08:00:13 -07001667 update_path = alt_path = unique_path(opt, collide_path, "merged");
1668 } else if (would_lose_untracked(opt, collide_path)) {
Elijah Newren18797a32018-04-19 10:58:13 -07001669 /*
Elijah Newren37b65ce2018-11-07 20:40:25 -08001670 * Only way we get here is if both renames were from
1671 * a directory rename AND user had an untracked file
1672 * at the location where both files end up after the
1673 * two directory renames. See testcase 10d of t6043.
Elijah Newren18797a32018-04-19 10:58:13 -07001674 */
Elijah Newren259ccb62019-04-05 08:00:13 -07001675 output(opt, 1, _("Refusing to lose untracked file at "
Elijah Newren37b65ce2018-11-07 20:40:25 -08001676 "%s, even though it's in the way."),
1677 collide_path);
Elijah Newren259ccb62019-04-05 08:00:13 -07001678 update_path = alt_path = unique_path(opt, collide_path, "merged");
Elijah Newren3672c972011-08-11 23:20:22 -06001679 } else {
Elijah Newren37b65ce2018-11-07 20:40:25 -08001680 /*
1681 * FIXME: It's possible that the two files are identical
1682 * and that the current working copy happens to match, in
1683 * which case we are unnecessarily touching the working
1684 * tree file. It's not a likely enough scenario that I
1685 * want to code up the checks for it and a better fix is
1686 * available if we restructure how unpack_trees() and
1687 * merge-recursive interoperate anyway, so punting for
1688 * now...
1689 */
Elijah Newren259ccb62019-04-05 08:00:13 -07001690 remove_file(opt, 0, collide_path, 0);
Elijah Newren3672c972011-08-11 23:20:22 -06001691 }
Elijah Newren3672c972011-08-11 23:20:22 -06001692
Elijah Newren37b65ce2018-11-07 20:40:25 -08001693 /* Store things in diff_filespecs for functions that need it */
Elijah Newren8daec1d2019-04-05 08:00:22 -07001694 null.path = (char *)collide_path;
brian m. carlson14228442021-04-26 01:02:56 +00001695 oidcpy(&null.oid, null_oid());
Elijah Newren37b65ce2018-11-07 20:40:25 -08001696 null.mode = 0;
Johannes Schindelin75456f92016-07-26 18:06:21 +02001697
Elijah Newren8daec1d2019-04-05 08:00:22 -07001698 if (merge_mode_and_contents(opt, &null, a, b, collide_path,
Elijah Newren5bf7e572019-08-17 11:41:41 -07001699 branch1, branch2, opt->priv->call_depth * 2, &mfi))
Elijah Newren37b65ce2018-11-07 20:40:25 -08001700 return -1;
1701 mfi.clean &= !alt_path;
Elijah Newren8daec1d2019-04-05 08:00:22 -07001702 if (update_file(opt, mfi.clean, &mfi.blob, update_path))
Elijah Newren37b65ce2018-11-07 20:40:25 -08001703 return -1;
Elijah Newren5bf7e572019-08-17 11:41:41 -07001704 if (!mfi.clean && !opt->priv->call_depth &&
Elijah Newren8daec1d2019-04-05 08:00:22 -07001705 update_stages(opt, collide_path, NULL, a, b))
Elijah Newren37b65ce2018-11-07 20:40:25 -08001706 return -1;
1707 free(alt_path);
1708 /*
1709 * FIXME: If both a & b both started with conflicts (only possible
1710 * if they came from a rename/rename(2to1)), but had IDENTICAL
1711 * contents including those conflicts, then in the next line we claim
1712 * it was clean. If someone cares about this case, we should have the
1713 * caller notify us if we started with conflicts.
1714 */
1715 return mfi.clean;
1716}
Elijah Newren7f867162018-11-07 20:40:26 -08001717
Elijah Newren259ccb62019-04-05 08:00:13 -07001718static int handle_rename_add(struct merge_options *opt,
Elijah Newren7f867162018-11-07 20:40:26 -08001719 struct rename_conflict_info *ci)
1720{
1721 /* a was renamed to c, and a separate c was added. */
Elijah Newrene9cd1b52019-04-05 08:00:18 -07001722 struct diff_filespec *a = ci->ren1->pair->one;
1723 struct diff_filespec *c = ci->ren1->pair->two;
Elijah Newren7f867162018-11-07 20:40:26 -08001724 char *path = c->path;
1725 char *prev_path_desc;
1726 struct merge_file_info mfi;
1727
Elijah Newrenc336ab82019-04-05 08:00:20 -07001728 const char *rename_branch = ci->ren1->branch;
1729 const char *add_branch = (opt->branch1 == rename_branch ?
1730 opt->branch2 : opt->branch1);
1731 int other_stage = (ci->ren1->branch == opt->branch1 ? 3 : 2);
Elijah Newren7f867162018-11-07 20:40:26 -08001732
Elijah Newren259ccb62019-04-05 08:00:13 -07001733 output(opt, 1, _("CONFLICT (rename/add): "
Elijah Newren7f867162018-11-07 20:40:26 -08001734 "Rename %s->%s in %s. Added %s in %s"),
Elijah Newrenc336ab82019-04-05 08:00:20 -07001735 a->path, c->path, rename_branch,
1736 c->path, add_branch);
Elijah Newren7f867162018-11-07 20:40:26 -08001737
1738 prev_path_desc = xstrfmt("version of %s from %s", path, a->path);
Elijah Newren481de8a2019-06-04 13:27:50 -07001739 ci->ren1->src_entry->stages[other_stage].path = a->path;
Elijah Newren8daec1d2019-04-05 08:00:22 -07001740 if (merge_mode_and_contents(opt, a, c,
1741 &ci->ren1->src_entry->stages[other_stage],
Elijah Newren3f9c92e2019-04-05 08:00:19 -07001742 prev_path_desc,
Elijah Newren259ccb62019-04-05 08:00:13 -07001743 opt->branch1, opt->branch2,
Elijah Newren5bf7e572019-08-17 11:41:41 -07001744 1 + opt->priv->call_depth * 2, &mfi))
Elijah Newren7f867162018-11-07 20:40:26 -08001745 return -1;
1746 free(prev_path_desc);
1747
Elijah Newren8daec1d2019-04-05 08:00:22 -07001748 ci->ren1->dst_entry->stages[other_stage].path = mfi.blob.path = c->path;
Elijah Newren259ccb62019-04-05 08:00:13 -07001749 return handle_file_collision(opt,
Elijah Newren7f867162018-11-07 20:40:26 -08001750 c->path, a->path, NULL,
Elijah Newrenc336ab82019-04-05 08:00:20 -07001751 rename_branch, add_branch,
Elijah Newren8daec1d2019-04-05 08:00:22 -07001752 &mfi.blob,
1753 &ci->ren1->dst_entry->stages[other_stage]);
Elijah Newren7f867162018-11-07 20:40:26 -08001754}
Elijah Newren37b65ce2018-11-07 20:40:25 -08001755
Elijah Newren259ccb62019-04-05 08:00:13 -07001756static char *find_path_for_conflict(struct merge_options *opt,
Derrick Stolee80cee6e2018-11-07 20:40:31 -08001757 const char *path,
1758 const char *branch1,
1759 const char *branch2)
1760{
1761 char *new_path = NULL;
Elijah Newren5bf7e572019-08-17 11:41:41 -07001762 if (dir_in_way(opt->repo->index, path, !opt->priv->call_depth, 0)) {
Elijah Newren259ccb62019-04-05 08:00:13 -07001763 new_path = unique_path(opt, path, branch1);
1764 output(opt, 1, _("%s is a directory in %s adding "
Derrick Stolee80cee6e2018-11-07 20:40:31 -08001765 "as %s instead"),
1766 path, branch2, new_path);
Elijah Newren259ccb62019-04-05 08:00:13 -07001767 } else if (would_lose_untracked(opt, path)) {
1768 new_path = unique_path(opt, path, branch1);
1769 output(opt, 1, _("Refusing to lose untracked file"
Derrick Stolee80cee6e2018-11-07 20:40:31 -08001770 " at %s; adding as %s instead"),
1771 path, new_path);
1772 }
1773
1774 return new_path;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001775}
1776
Jeff Kingee798742020-01-25 00:37:23 -05001777/*
Junio C Hamano4c616c22020-01-25 18:57:45 -05001778 * Toggle the stage number between "ours" and "theirs" (2 and 3).
Jeff Kingee798742020-01-25 00:37:23 -05001779 */
1780static inline int flip_stage(int stage)
1781{
Junio C Hamano4c616c22020-01-25 18:57:45 -05001782 return (2 + 3) - stage;
Jeff Kingee798742020-01-25 00:37:23 -05001783}
1784
Elijah Newren259ccb62019-04-05 08:00:13 -07001785static int handle_rename_rename_1to2(struct merge_options *opt,
Elijah Newren8ebe7b02018-06-09 21:16:15 -07001786 struct rename_conflict_info *ci)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001787{
Elijah Newren09c01f82010-09-20 02:28:48 -06001788 /* One file was renamed in both branches, but to different names. */
Elijah Newren48c9cb92018-11-07 20:40:29 -08001789 struct merge_file_info mfi;
Elijah Newren48c9cb92018-11-07 20:40:29 -08001790 struct diff_filespec *add;
Elijah Newrene9cd1b52019-04-05 08:00:18 -07001791 struct diff_filespec *o = ci->ren1->pair->one;
1792 struct diff_filespec *a = ci->ren1->pair->two;
1793 struct diff_filespec *b = ci->ren2->pair->two;
Elijah Newren48c9cb92018-11-07 20:40:29 -08001794 char *path_desc;
Elijah Newren07413c52010-09-20 02:29:00 -06001795
Elijah Newren259ccb62019-04-05 08:00:13 -07001796 output(opt, 1, _("CONFLICT (rename/rename): "
Elijah Newren4f66dad2011-08-11 23:20:08 -06001797 "Rename \"%s\"->\"%s\" in branch \"%s\" "
Jiang Xin55653a62012-07-25 22:53:13 +08001798 "rename \"%s\"->\"%s\" in \"%s\"%s"),
Elijah Newrenc336ab82019-04-05 08:00:20 -07001799 o->path, a->path, ci->ren1->branch,
1800 o->path, b->path, ci->ren2->branch,
Elijah Newren5bf7e572019-08-17 11:41:41 -07001801 opt->priv->call_depth ? _(" (left unresolved)") : "");
Johannes Schindelin75456f92016-07-26 18:06:21 +02001802
Elijah Newren48c9cb92018-11-07 20:40:29 -08001803 path_desc = xstrfmt("%s and %s, both renamed from %s",
Elijah Newrene3de8882019-04-05 08:00:14 -07001804 a->path, b->path, o->path);
1805 if (merge_mode_and_contents(opt, o, a, b, path_desc,
Elijah Newrenc336ab82019-04-05 08:00:20 -07001806 ci->ren1->branch, ci->ren2->branch,
Elijah Newren5bf7e572019-08-17 11:41:41 -07001807 opt->priv->call_depth * 2, &mfi))
Elijah Newren48c9cb92018-11-07 20:40:29 -08001808 return -1;
1809 free(path_desc);
1810
Elijah Newren80205042020-02-27 00:05:05 +00001811 if (opt->priv->call_depth)
1812 remove_file_from_index(opt->repo->index, o->path);
1813
1814 /*
1815 * For each destination path, we need to see if there is a
1816 * rename/add collision. If not, we can write the file out
1817 * to the specified location.
1818 */
1819 add = &ci->ren1->dst_entry->stages[flip_stage(2)];
1820 if (is_valid(add)) {
1821 add->path = mfi.blob.path = a->path;
1822 if (handle_file_collision(opt, a->path,
1823 NULL, NULL,
1824 ci->ren1->branch,
1825 ci->ren2->branch,
1826 &mfi.blob, add) < 0)
Johannes Schindelin75456f92016-07-26 18:06:21 +02001827 return -1;
Elijah Newren48c9cb92018-11-07 20:40:29 -08001828 } else {
Elijah Newren80205042020-02-27 00:05:05 +00001829 char *new_path = find_path_for_conflict(opt, a->path,
1830 ci->ren1->branch,
1831 ci->ren2->branch);
1832 if (update_file(opt, 0, &mfi.blob,
1833 new_path ? new_path : a->path))
1834 return -1;
1835 free(new_path);
1836 if (!opt->priv->call_depth &&
1837 update_stages(opt, a->path, NULL, a, NULL))
1838 return -1;
1839 }
Elijah Newren48c9cb92018-11-07 20:40:29 -08001840
Elijah Newren95983da2020-05-13 23:56:32 +00001841 if (!mfi.clean && mfi.blob.mode == a->mode &&
1842 oideq(&mfi.blob.oid, &a->oid)) {
1843 /*
1844 * Getting here means we were attempting to merge a binary
1845 * blob. Since we can't merge binaries, the merge algorithm
1846 * just takes one side. But we don't want to copy the
1847 * contents of one side to both paths; we'd rather use the
1848 * original content at the given path for each path.
1849 */
1850 oidcpy(&mfi.blob.oid, &b->oid);
1851 mfi.blob.mode = b->mode;
1852 }
Elijah Newren80205042020-02-27 00:05:05 +00001853 add = &ci->ren2->dst_entry->stages[flip_stage(3)];
1854 if (is_valid(add)) {
1855 add->path = mfi.blob.path = b->path;
1856 if (handle_file_collision(opt, b->path,
1857 NULL, NULL,
1858 ci->ren1->branch,
1859 ci->ren2->branch,
1860 add, &mfi.blob) < 0)
1861 return -1;
1862 } else {
1863 char *new_path = find_path_for_conflict(opt, b->path,
1864 ci->ren2->branch,
1865 ci->ren1->branch);
1866 if (update_file(opt, 0, &mfi.blob,
1867 new_path ? new_path : b->path))
1868 return -1;
1869 free(new_path);
1870 if (!opt->priv->call_depth &&
1871 update_stages(opt, b->path, NULL, NULL, b))
1872 return -1;
Elijah Newren48c9cb92018-11-07 20:40:29 -08001873 }
Johannes Schindelin75456f92016-07-26 18:06:21 +02001874
1875 return 0;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001876}
1877
Elijah Newren259ccb62019-04-05 08:00:13 -07001878static int handle_rename_rename_2to1(struct merge_options *opt,
Elijah Newren8ebe7b02018-06-09 21:16:15 -07001879 struct rename_conflict_info *ci)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001880{
Elijah Newren461f5042011-08-11 23:20:15 -06001881 /* Two files, a & b, were renamed to the same thing, c. */
Elijah Newrene9cd1b52019-04-05 08:00:18 -07001882 struct diff_filespec *a = ci->ren1->pair->one;
1883 struct diff_filespec *b = ci->ren2->pair->one;
1884 struct diff_filespec *c1 = ci->ren1->pair->two;
1885 struct diff_filespec *c2 = ci->ren2->pair->two;
Elijah Newren461f5042011-08-11 23:20:15 -06001886 char *path = c1->path; /* == c2->path */
Elijah Newren05cf21e2018-04-19 10:58:22 -07001887 char *path_side_1_desc;
1888 char *path_side_2_desc;
Elijah Newren434b8522011-08-11 23:20:18 -06001889 struct merge_file_info mfi_c1;
1890 struct merge_file_info mfi_c2;
Elijah Newren8daec1d2019-04-05 08:00:22 -07001891 int ostage1, ostage2;
Elijah Newren461f5042011-08-11 23:20:15 -06001892
Elijah Newren259ccb62019-04-05 08:00:13 -07001893 output(opt, 1, _("CONFLICT (rename/rename): "
Elijah Newren461f5042011-08-11 23:20:15 -06001894 "Rename %s->%s in %s. "
Jiang Xin55653a62012-07-25 22:53:13 +08001895 "Rename %s->%s in %s"),
Elijah Newrenc336ab82019-04-05 08:00:20 -07001896 a->path, c1->path, ci->ren1->branch,
1897 b->path, c2->path, ci->ren2->branch);
Elijah Newren461f5042011-08-11 23:20:15 -06001898
Elijah Newren2b168ef2018-10-16 13:19:47 -07001899 path_side_1_desc = xstrfmt("version of %s from %s", path, a->path);
1900 path_side_2_desc = xstrfmt("version of %s from %s", path, b->path);
Elijah Newren8daec1d2019-04-05 08:00:22 -07001901 ostage1 = ci->ren1->branch == opt->branch1 ? 3 : 2;
Jeff Kingee798742020-01-25 00:37:23 -05001902 ostage2 = flip_stage(ostage1);
Elijah Newren8daec1d2019-04-05 08:00:22 -07001903 ci->ren1->src_entry->stages[ostage1].path = a->path;
1904 ci->ren2->src_entry->stages[ostage2].path = b->path;
1905 if (merge_mode_and_contents(opt, a, c1,
1906 &ci->ren1->src_entry->stages[ostage1],
1907 path_side_1_desc,
Elijah Newren259ccb62019-04-05 08:00:13 -07001908 opt->branch1, opt->branch2,
Elijah Newren5bf7e572019-08-17 11:41:41 -07001909 1 + opt->priv->call_depth * 2, &mfi_c1) ||
Elijah Newren8daec1d2019-04-05 08:00:22 -07001910 merge_mode_and_contents(opt, b,
1911 &ci->ren2->src_entry->stages[ostage2],
1912 c2, path_side_2_desc,
Elijah Newren259ccb62019-04-05 08:00:13 -07001913 opt->branch1, opt->branch2,
Elijah Newren5bf7e572019-08-17 11:41:41 -07001914 1 + opt->priv->call_depth * 2, &mfi_c2))
Johannes Schindelin75456f92016-07-26 18:06:21 +02001915 return -1;
Elijah Newren05cf21e2018-04-19 10:58:22 -07001916 free(path_side_1_desc);
1917 free(path_side_2_desc);
Elijah Newren8daec1d2019-04-05 08:00:22 -07001918 mfi_c1.blob.path = path;
1919 mfi_c2.blob.path = path;
Elijah Newren434b8522011-08-11 23:20:18 -06001920
Elijah Newren259ccb62019-04-05 08:00:13 -07001921 return handle_file_collision(opt, path, a->path, b->path,
Elijah Newrenc336ab82019-04-05 08:00:20 -07001922 ci->ren1->branch, ci->ren2->branch,
Elijah Newren8daec1d2019-04-05 08:00:22 -07001923 &mfi_c1.blob, &mfi_c2.blob);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02001924}
1925
Elijah Newren9ba91552018-04-19 10:57:59 -07001926/*
Elijah Newrene5257b22018-04-19 10:58:03 -07001927 * Get the diff_filepairs changed between o_tree and tree.
Elijah Newren9ba91552018-04-19 10:57:59 -07001928 */
Elijah Newren259ccb62019-04-05 08:00:13 -07001929static struct diff_queue_struct *get_diffpairs(struct merge_options *opt,
Elijah Newrene5257b22018-04-19 10:58:03 -07001930 struct tree *o_tree,
1931 struct tree *tree)
Elijah Newren9ba91552018-04-19 10:57:59 -07001932{
Elijah Newrene5257b22018-04-19 10:58:03 -07001933 struct diff_queue_struct *ret;
Elijah Newren9ba91552018-04-19 10:57:59 -07001934 struct diff_options opts;
1935
Elijah Newren259ccb62019-04-05 08:00:13 -07001936 repo_diff_setup(opt->repo, &opts);
Elijah Newren9ba91552018-04-19 10:57:59 -07001937 opts.flags.recursive = 1;
1938 opts.flags.rename_empty = 0;
Elijah Newren259ccb62019-04-05 08:00:13 -07001939 opts.detect_rename = merge_detect_rename(opt);
Ben Peart85b46032018-05-02 16:01:14 +00001940 /*
1941 * We do not have logic to handle the detection of copies. In
1942 * fact, it may not even make sense to add such logic: would we
1943 * really want a change to a base file to be propagated through
1944 * multiple other files by a merge?
1945 */
1946 if (opts.detect_rename > DIFF_DETECT_RENAME)
1947 opts.detect_rename = DIFF_DETECT_RENAME;
Elijah Newren94b82d52021-07-15 00:45:24 +00001948 opts.rename_limit = (opt->rename_limit >= 0) ? opt->rename_limit : 7000;
Elijah Newren259ccb62019-04-05 08:00:13 -07001949 opts.rename_score = opt->rename_score;
1950 opts.show_rename_progress = opt->show_rename_progress;
Elijah Newren9ba91552018-04-19 10:57:59 -07001951 opts.output_format = DIFF_FORMAT_NO_OUTPUT;
1952 diff_setup_done(&opts);
1953 diff_tree_oid(&o_tree->object.oid, &tree->object.oid, "", &opts);
1954 diffcore_std(&opts);
Elijah Newren5bf7e572019-08-17 11:41:41 -07001955 if (opts.needed_rename_limit > opt->priv->needed_rename_limit)
1956 opt->priv->needed_rename_limit = opts.needed_rename_limit;
Elijah Newrene5257b22018-04-19 10:58:03 -07001957
1958 ret = xmalloc(sizeof(*ret));
1959 *ret = diff_queued_diff;
1960
1961 opts.output_format = DIFF_FORMAT_NO_OUTPUT;
1962 diff_queued_diff.nr = 0;
1963 diff_queued_diff.queue = NULL;
1964 diff_flush(&opts);
1965 return ret;
1966}
1967
Nguyễn Thái Ngọc Duy34e77712019-06-27 16:28:52 +07001968static int tree_has_path(struct repository *r, struct tree *tree,
1969 const char *path)
Elijah Newren96e7ffb2018-04-19 10:58:06 -07001970{
1971 struct object_id hashy;
Elijah Newren5ec1e722019-04-05 08:00:12 -07001972 unsigned short mode_o;
Elijah Newren96e7ffb2018-04-19 10:58:06 -07001973
Nguyễn Thái Ngọc Duy34e77712019-06-27 16:28:52 +07001974 return !get_tree_entry(r,
Nguyễn Thái Ngọc Duy50ddb082019-06-27 16:28:49 +07001975 &tree->object.oid, path,
Elijah Newren96e7ffb2018-04-19 10:58:06 -07001976 &hashy, &mode_o);
1977}
1978
Elijah Newrene95ab702018-04-19 10:58:07 -07001979/*
1980 * Return a new string that replaces the beginning portion (which matches
1981 * entry->dir), with entry->new_dir. In perl-speak:
1982 * new_path_name = (old_path =~ s/entry->dir/entry->new_dir/);
1983 * NOTE:
1984 * Caller must ensure that old_path starts with entry->dir + '/'.
1985 */
1986static char *apply_dir_rename(struct dir_rename_entry *entry,
1987 const char *old_path)
1988{
1989 struct strbuf new_path = STRBUF_INIT;
1990 int oldlen, newlen;
1991
1992 if (entry->non_unique_new_dir)
1993 return NULL;
1994
1995 oldlen = strlen(entry->dir);
Elijah Newren49b8133a2019-10-22 21:22:50 +00001996 if (entry->new_dir.len == 0)
1997 /*
1998 * If someone renamed/merged a subdirectory into the root
1999 * directory (e.g. 'some/subdir' -> ''), then we want to
2000 * avoid returning
2001 * '' + '/filename'
2002 * as the rename; we need to make old_path + oldlen advance
2003 * past the '/' character.
2004 */
2005 oldlen++;
Elijah Newrene95ab702018-04-19 10:58:07 -07002006 newlen = entry->new_dir.len + (strlen(old_path) - oldlen) + 1;
2007 strbuf_grow(&new_path, newlen);
2008 strbuf_addbuf(&new_path, &entry->new_dir);
2009 strbuf_addstr(&new_path, &old_path[oldlen]);
2010
2011 return strbuf_detach(&new_path, NULL);
2012}
2013
Elijah Newren7fe40b82018-04-19 10:58:05 -07002014static void get_renamed_dir_portion(const char *old_path, const char *new_path,
2015 char **old_dir, char **new_dir)
2016{
2017 char *end_of_old, *end_of_new;
Elijah Newren7fe40b82018-04-19 10:58:05 -07002018
Elijah Newrend3eebaa2019-10-22 21:22:49 +00002019 /* Default return values: NULL, meaning no rename */
Elijah Newren7fe40b82018-04-19 10:58:05 -07002020 *old_dir = NULL;
2021 *new_dir = NULL;
2022
2023 /*
2024 * For
2025 * "a/b/c/d/e/foo.c" -> "a/b/some/thing/else/e/foo.c"
2026 * the "e/foo.c" part is the same, we just want to know that
2027 * "a/b/c/d" was renamed to "a/b/some/thing/else"
2028 * so, for this example, this function returns "a/b/c/d" in
2029 * *old_dir and "a/b/some/thing/else" in *new_dir.
Elijah Newrend3eebaa2019-10-22 21:22:49 +00002030 */
2031
2032 /*
2033 * If the basename of the file changed, we don't care. We want
2034 * to know which portion of the directory, if any, changed.
Elijah Newren7fe40b82018-04-19 10:58:05 -07002035 */
2036 end_of_old = strrchr(old_path, '/');
2037 end_of_new = strrchr(new_path, '/');
2038
Elijah Newren49b8133a2019-10-22 21:22:50 +00002039 /*
2040 * If end_of_old is NULL, old_path wasn't in a directory, so there
2041 * could not be a directory rename (our rule elsewhere that a
2042 * directory which still exists is not considered to have been
2043 * renamed means the root directory can never be renamed -- because
2044 * the root directory always exists).
2045 */
Junio C Hamanoafe8a902022-05-02 09:50:37 -07002046 if (!end_of_old)
Elijah Newren49b8133a2019-10-22 21:22:50 +00002047 return; /* Note: *old_dir and *new_dir are still NULL */
2048
2049 /*
2050 * If new_path contains no directory (end_of_new is NULL), then we
2051 * have a rename of old_path's directory to the root directory.
2052 */
Junio C Hamanoafe8a902022-05-02 09:50:37 -07002053 if (!end_of_new) {
Elijah Newren49b8133a2019-10-22 21:22:50 +00002054 *old_dir = xstrndup(old_path, end_of_old - old_path);
2055 *new_dir = xstrdup("");
Elijah Newren7fe40b82018-04-19 10:58:05 -07002056 return;
Elijah Newren49b8133a2019-10-22 21:22:50 +00002057 }
Elijah Newrend3eebaa2019-10-22 21:22:49 +00002058
2059 /* Find the first non-matching character traversing backwards */
Elijah Newren7fe40b82018-04-19 10:58:05 -07002060 while (*--end_of_new == *--end_of_old &&
2061 end_of_old != old_path &&
2062 end_of_new != new_path)
2063 ; /* Do nothing; all in the while loop */
Elijah Newrend3eebaa2019-10-22 21:22:49 +00002064
2065 /*
2066 * If both got back to the beginning of their strings, then the
2067 * directory didn't change at all, only the basename did.
2068 */
2069 if (end_of_old == old_path && end_of_new == new_path &&
2070 *end_of_old == *end_of_new)
Elijah Newren49b8133a2019-10-22 21:22:50 +00002071 return; /* Note: *old_dir and *new_dir are still NULL */
2072
2073 /*
2074 * If end_of_new got back to the beginning of its string, and
2075 * end_of_old got back to the beginning of some subdirectory, then
2076 * we have a rename/merge of a subdirectory into the root, which
2077 * needs slightly special handling.
2078 *
2079 * Note: There is no need to consider the opposite case, with a
2080 * rename/merge of the root directory into some subdirectory
2081 * because as noted above the root directory always exists so it
2082 * cannot be considered to be renamed.
2083 */
2084 if (end_of_new == new_path &&
2085 end_of_old != old_path && end_of_old[-1] == '/') {
2086 *old_dir = xstrndup(old_path, --end_of_old - old_path);
2087 *new_dir = xstrdup("");
2088 return;
2089 }
Elijah Newrend3eebaa2019-10-22 21:22:49 +00002090
Elijah Newren7fe40b82018-04-19 10:58:05 -07002091 /*
2092 * We've found the first non-matching character in the directory
Elijah Newrend3eebaa2019-10-22 21:22:49 +00002093 * paths. That means the current characters we were looking at
2094 * were part of the first non-matching subdir name going back from
2095 * the end of the strings. Get the whole name by advancing both
2096 * end_of_old and end_of_new to the NEXT '/' character. That will
2097 * represent the entire directory rename.
2098 *
2099 * The reason for the increment is cases like
2100 * a/b/star/foo/whatever.c -> a/b/tar/foo/random.c
2101 * After dropping the basename and going back to the first
2102 * non-matching character, we're now comparing:
2103 * a/b/s and a/b/
2104 * and we want to be comparing:
2105 * a/b/star/ and a/b/tar/
2106 * but without the pre-increment, the one on the right would stay
2107 * a/b/.
Elijah Newren7fe40b82018-04-19 10:58:05 -07002108 */
Elijah Newrend3eebaa2019-10-22 21:22:49 +00002109 end_of_old = strchr(++end_of_old, '/');
2110 end_of_new = strchr(++end_of_new, '/');
Elijah Newren7fe40b82018-04-19 10:58:05 -07002111
Elijah Newrend3eebaa2019-10-22 21:22:49 +00002112 /* Copy the old and new directories into *old_dir and *new_dir. */
2113 *old_dir = xstrndup(old_path, end_of_old - old_path);
2114 *new_dir = xstrndup(new_path, end_of_new - new_path);
Elijah Newren7fe40b82018-04-19 10:58:05 -07002115}
2116
Elijah Newren96e7ffb2018-04-19 10:58:06 -07002117static void remove_hashmap_entries(struct hashmap *dir_renames,
2118 struct string_list *items_to_remove)
2119{
2120 int i;
2121 struct dir_rename_entry *entry;
2122
2123 for (i = 0; i < items_to_remove->nr; i++) {
2124 entry = items_to_remove->items[i].util;
Eric Wong28ee7942019-10-06 23:30:31 +00002125 hashmap_remove(dir_renames, &entry->ent, NULL);
Elijah Newren96e7ffb2018-04-19 10:58:06 -07002126 }
2127 string_list_clear(items_to_remove, 0);
2128}
2129
2130/*
Elijah Newrenf6f77552018-04-19 10:58:08 -07002131 * See if there is a directory rename for path, and if there are any file
2132 * level conflicts for the renamed location. If there is a rename and
2133 * there are no conflicts, return the new name. Otherwise, return NULL.
2134 */
Elijah Newren259ccb62019-04-05 08:00:13 -07002135static char *handle_path_level_conflicts(struct merge_options *opt,
Elijah Newrenf6f77552018-04-19 10:58:08 -07002136 const char *path,
2137 struct dir_rename_entry *entry,
2138 struct hashmap *collisions,
2139 struct tree *tree)
2140{
2141 char *new_path = NULL;
2142 struct collision_entry *collision_ent;
2143 int clean = 1;
2144 struct strbuf collision_paths = STRBUF_INIT;
2145
2146 /*
2147 * entry has the mapping of old directory name to new directory name
2148 * that we want to apply to path.
2149 */
2150 new_path = apply_dir_rename(entry, path);
2151
2152 if (!new_path) {
2153 /* This should only happen when entry->non_unique_new_dir set */
2154 if (!entry->non_unique_new_dir)
Kyle Meyer42db3242022-11-25 12:37:45 -05002155 BUG("entry->non_unique_new_dir not set and !new_path");
Elijah Newren259ccb62019-04-05 08:00:13 -07002156 output(opt, 1, _("CONFLICT (directory rename split): "
Elijah Newrenf6f77552018-04-19 10:58:08 -07002157 "Unclear where to place %s because directory "
2158 "%s was renamed to multiple other directories, "
2159 "with no destination getting a majority of the "
2160 "files."),
2161 path, entry->dir);
2162 clean = 0;
2163 return NULL;
2164 }
2165
2166 /*
2167 * The caller needs to have ensured that it has pre-populated
2168 * collisions with all paths that map to new_path. Do a quick check
2169 * to ensure that's the case.
2170 */
2171 collision_ent = collision_find_entry(collisions, new_path);
Junio C Hamanoafe8a902022-05-02 09:50:37 -07002172 if (!collision_ent)
Elijah Newrenf6f77552018-04-19 10:58:08 -07002173 BUG("collision_ent is NULL");
2174
2175 /*
2176 * Check for one-sided add/add/.../add conflicts, i.e.
2177 * where implicit renames from the other side doing
2178 * directory rename(s) can affect this side of history
2179 * to put multiple paths into the same location. Warn
2180 * and bail on directory renames for such paths.
2181 */
2182 if (collision_ent->reported_already) {
2183 clean = 0;
Nguyễn Thái Ngọc Duy34e77712019-06-27 16:28:52 +07002184 } else if (tree_has_path(opt->repo, tree, new_path)) {
Elijah Newrenf6f77552018-04-19 10:58:08 -07002185 collision_ent->reported_already = 1;
2186 strbuf_add_separated_string_list(&collision_paths, ", ",
2187 &collision_ent->source_files);
Elijah Newren259ccb62019-04-05 08:00:13 -07002188 output(opt, 1, _("CONFLICT (implicit dir rename): Existing "
Elijah Newrenf6f77552018-04-19 10:58:08 -07002189 "file/dir at %s in the way of implicit "
2190 "directory rename(s) putting the following "
2191 "path(s) there: %s."),
2192 new_path, collision_paths.buf);
2193 clean = 0;
2194 } else if (collision_ent->source_files.nr > 1) {
2195 collision_ent->reported_already = 1;
2196 strbuf_add_separated_string_list(&collision_paths, ", ",
2197 &collision_ent->source_files);
Elijah Newren259ccb62019-04-05 08:00:13 -07002198 output(opt, 1, _("CONFLICT (implicit dir rename): Cannot map "
Elijah Newrenf6f77552018-04-19 10:58:08 -07002199 "more than one path to %s; implicit directory "
2200 "renames tried to put these paths there: %s"),
2201 new_path, collision_paths.buf);
2202 clean = 0;
2203 }
2204
2205 /* Free memory we no longer need */
2206 strbuf_release(&collision_paths);
2207 if (!clean && new_path) {
2208 free(new_path);
2209 return NULL;
2210 }
2211
2212 return new_path;
2213}
2214
2215/*
Elijah Newren96e7ffb2018-04-19 10:58:06 -07002216 * There are a couple things we want to do at the directory level:
2217 * 1. Check for both sides renaming to the same thing, in order to avoid
2218 * implicit renaming of files that should be left in place. (See
2219 * testcase 6b in t6043 for details.)
2220 * 2. Prune directory renames if there are still files left in the
Andrei Rybakabcb66c2021-06-11 13:18:50 +02002221 * original directory. These represent a partial directory rename,
Elijah Newren96e7ffb2018-04-19 10:58:06 -07002222 * i.e. a rename where only some of the files within the directory
2223 * were renamed elsewhere. (Technically, this could be done earlier
2224 * in get_directory_renames(), except that would prevent us from
2225 * doing the previous check and thus failing testcase 6b.)
2226 * 3. Check for rename/rename(1to2) conflicts (at the directory level).
2227 * In the future, we could potentially record this info as well and
2228 * omit reporting rename/rename(1to2) conflicts for each path within
2229 * the affected directories, thus cleaning up the merge output.
2230 * NOTE: We do NOT check for rename/rename(2to1) conflicts at the
2231 * directory level, because merging directories is fine. If it
2232 * causes conflicts for files within those merged directories, then
2233 * that should be detected at the individual path level.
2234 */
Elijah Newren259ccb62019-04-05 08:00:13 -07002235static void handle_directory_level_conflicts(struct merge_options *opt,
Elijah Newren96e7ffb2018-04-19 10:58:06 -07002236 struct hashmap *dir_re_head,
2237 struct tree *head,
2238 struct hashmap *dir_re_merge,
2239 struct tree *merge)
2240{
2241 struct hashmap_iter iter;
2242 struct dir_rename_entry *head_ent;
2243 struct dir_rename_entry *merge_ent;
2244
2245 struct string_list remove_from_head = STRING_LIST_INIT_NODUP;
2246 struct string_list remove_from_merge = STRING_LIST_INIT_NODUP;
2247
Eric Wong87571c32019-10-06 23:30:38 +00002248 hashmap_for_each_entry(dir_re_head, &iter, head_ent,
Eric Wong87571c32019-10-06 23:30:38 +00002249 ent /* member name */) {
Elijah Newren96e7ffb2018-04-19 10:58:06 -07002250 merge_ent = dir_rename_find_entry(dir_re_merge, head_ent->dir);
2251 if (merge_ent &&
2252 !head_ent->non_unique_new_dir &&
2253 !merge_ent->non_unique_new_dir &&
2254 !strbuf_cmp(&head_ent->new_dir, &merge_ent->new_dir)) {
2255 /* 1. Renamed identically; remove it from both sides */
2256 string_list_append(&remove_from_head,
2257 head_ent->dir)->util = head_ent;
2258 strbuf_release(&head_ent->new_dir);
2259 string_list_append(&remove_from_merge,
2260 merge_ent->dir)->util = merge_ent;
2261 strbuf_release(&merge_ent->new_dir);
Nguyễn Thái Ngọc Duy34e77712019-06-27 16:28:52 +07002262 } else if (tree_has_path(opt->repo, head, head_ent->dir)) {
Elijah Newren96e7ffb2018-04-19 10:58:06 -07002263 /* 2. This wasn't a directory rename after all */
2264 string_list_append(&remove_from_head,
2265 head_ent->dir)->util = head_ent;
2266 strbuf_release(&head_ent->new_dir);
2267 }
2268 }
2269
2270 remove_hashmap_entries(dir_re_head, &remove_from_head);
2271 remove_hashmap_entries(dir_re_merge, &remove_from_merge);
2272
Eric Wong87571c32019-10-06 23:30:38 +00002273 hashmap_for_each_entry(dir_re_merge, &iter, merge_ent,
Eric Wong87571c32019-10-06 23:30:38 +00002274 ent /* member name */) {
Elijah Newren96e7ffb2018-04-19 10:58:06 -07002275 head_ent = dir_rename_find_entry(dir_re_head, merge_ent->dir);
Nguyễn Thái Ngọc Duy34e77712019-06-27 16:28:52 +07002276 if (tree_has_path(opt->repo, merge, merge_ent->dir)) {
Elijah Newren96e7ffb2018-04-19 10:58:06 -07002277 /* 2. This wasn't a directory rename after all */
2278 string_list_append(&remove_from_merge,
2279 merge_ent->dir)->util = merge_ent;
2280 } else if (head_ent &&
2281 !head_ent->non_unique_new_dir &&
2282 !merge_ent->non_unique_new_dir) {
2283 /* 3. rename/rename(1to2) */
2284 /*
2285 * We can assume it's not rename/rename(1to1) because
2286 * that was case (1), already checked above. So we
2287 * know that head_ent->new_dir and merge_ent->new_dir
2288 * are different strings.
2289 */
Elijah Newren259ccb62019-04-05 08:00:13 -07002290 output(opt, 1, _("CONFLICT (rename/rename): "
Elijah Newren96e7ffb2018-04-19 10:58:06 -07002291 "Rename directory %s->%s in %s. "
2292 "Rename directory %s->%s in %s"),
Elijah Newren259ccb62019-04-05 08:00:13 -07002293 head_ent->dir, head_ent->new_dir.buf, opt->branch1,
2294 head_ent->dir, merge_ent->new_dir.buf, opt->branch2);
Elijah Newren96e7ffb2018-04-19 10:58:06 -07002295 string_list_append(&remove_from_head,
2296 head_ent->dir)->util = head_ent;
2297 strbuf_release(&head_ent->new_dir);
2298 string_list_append(&remove_from_merge,
2299 merge_ent->dir)->util = merge_ent;
2300 strbuf_release(&merge_ent->new_dir);
2301 }
2302 }
2303
2304 remove_hashmap_entries(dir_re_head, &remove_from_head);
2305 remove_hashmap_entries(dir_re_merge, &remove_from_merge);
2306}
2307
Jeff Kingb53c5022019-02-14 00:50:02 -05002308static struct hashmap *get_directory_renames(struct diff_queue_struct *pairs)
Elijah Newren7fe40b82018-04-19 10:58:05 -07002309{
2310 struct hashmap *dir_renames;
2311 struct hashmap_iter iter;
2312 struct dir_rename_entry *entry;
2313 int i;
2314
2315 /*
2316 * Typically, we think of a directory rename as all files from a
2317 * certain directory being moved to a target directory. However,
2318 * what if someone first moved two files from the original
2319 * directory in one commit, and then renamed the directory
2320 * somewhere else in a later commit? At merge time, we just know
2321 * that files from the original directory went to two different
2322 * places, and that the bulk of them ended up in the same place.
2323 * We want each directory rename to represent where the bulk of the
2324 * files from that directory end up; this function exists to find
2325 * where the bulk of the files went.
2326 *
2327 * The first loop below simply iterates through the list of file
2328 * renames, finding out how often each directory rename pair
2329 * possibility occurs.
2330 */
2331 dir_renames = xmalloc(sizeof(*dir_renames));
2332 dir_rename_init(dir_renames);
2333 for (i = 0; i < pairs->nr; ++i) {
2334 struct string_list_item *item;
2335 int *count;
2336 struct diff_filepair *pair = pairs->queue[i];
2337 char *old_dir, *new_dir;
2338
2339 /* File not part of directory rename if it wasn't renamed */
2340 if (pair->status != 'R')
2341 continue;
2342
2343 get_renamed_dir_portion(pair->one->path, pair->two->path,
2344 &old_dir, &new_dir);
2345 if (!old_dir)
2346 /* Directory didn't change at all; ignore this one. */
2347 continue;
2348
2349 entry = dir_rename_find_entry(dir_renames, old_dir);
2350 if (!entry) {
2351 entry = xmalloc(sizeof(*entry));
2352 dir_rename_entry_init(entry, old_dir);
Eric Wong26b455f2019-10-06 23:30:32 +00002353 hashmap_put(dir_renames, &entry->ent);
Elijah Newren7fe40b82018-04-19 10:58:05 -07002354 } else {
2355 free(old_dir);
2356 }
2357 item = string_list_lookup(&entry->possible_new_dirs, new_dir);
2358 if (!item) {
2359 item = string_list_insert(&entry->possible_new_dirs,
2360 new_dir);
2361 item->util = xcalloc(1, sizeof(int));
2362 } else {
2363 free(new_dir);
2364 }
2365 count = item->util;
2366 *count += 1;
2367 }
2368
2369 /*
2370 * For each directory with files moved out of it, we find out which
2371 * target directory received the most files so we can declare it to
2372 * be the "winning" target location for the directory rename. This
2373 * winner gets recorded in new_dir. If there is no winner
2374 * (multiple target directories received the same number of files),
2375 * we set non_unique_new_dir. Once we've determined the winner (or
2376 * that there is no winner), we no longer need possible_new_dirs.
2377 */
Eric Wong87571c32019-10-06 23:30:38 +00002378 hashmap_for_each_entry(dir_renames, &iter, entry,
Eric Wong87571c32019-10-06 23:30:38 +00002379 ent /* member name */) {
Elijah Newren7fe40b82018-04-19 10:58:05 -07002380 int max = 0;
2381 int bad_max = 0;
2382 char *best = NULL;
2383
2384 for (i = 0; i < entry->possible_new_dirs.nr; i++) {
2385 int *count = entry->possible_new_dirs.items[i].util;
2386
2387 if (*count == max)
2388 bad_max = max;
2389 else if (*count > max) {
2390 max = *count;
2391 best = entry->possible_new_dirs.items[i].string;
2392 }
2393 }
2394 if (bad_max == max)
2395 entry->non_unique_new_dir = 1;
2396 else {
2397 assert(entry->new_dir.len == 0);
2398 strbuf_addstr(&entry->new_dir, best);
2399 }
2400 /*
2401 * The relevant directory sub-portion of the original full
2402 * filepaths were xstrndup'ed before inserting into
2403 * possible_new_dirs, and instead of manually iterating the
2404 * list and free'ing each, just lie and tell
2405 * possible_new_dirs that it did the strdup'ing so that it
2406 * will free them for us.
2407 */
2408 entry->possible_new_dirs.strdup_strings = 1;
2409 string_list_clear(&entry->possible_new_dirs, 1);
2410 }
2411
2412 return dir_renames;
2413}
2414
Elijah Newrene95ab702018-04-19 10:58:07 -07002415static struct dir_rename_entry *check_dir_renamed(const char *path,
2416 struct hashmap *dir_renames)
2417{
René Scharfe9da2d032018-06-10 12:56:31 +02002418 char *temp = xstrdup(path);
Elijah Newrene95ab702018-04-19 10:58:07 -07002419 char *end;
Elijah Newrenc3b9bc92018-09-05 10:03:07 -07002420 struct dir_rename_entry *entry = NULL;
Elijah Newrene95ab702018-04-19 10:58:07 -07002421
Elijah Newrene95ab702018-04-19 10:58:07 -07002422 while ((end = strrchr(temp, '/'))) {
2423 *end = '\0';
2424 entry = dir_rename_find_entry(dir_renames, temp);
2425 if (entry)
René Scharfe9da2d032018-06-10 12:56:31 +02002426 break;
Elijah Newrene95ab702018-04-19 10:58:07 -07002427 }
René Scharfe9da2d032018-06-10 12:56:31 +02002428 free(temp);
2429 return entry;
Elijah Newrene95ab702018-04-19 10:58:07 -07002430}
2431
2432static void compute_collisions(struct hashmap *collisions,
2433 struct hashmap *dir_renames,
2434 struct diff_queue_struct *pairs)
2435{
2436 int i;
2437
2438 /*
2439 * Multiple files can be mapped to the same path due to directory
2440 * renames done by the other side of history. Since that other
2441 * side of history could have merged multiple directories into one,
2442 * if our side of history added the same file basename to each of
2443 * those directories, then all N of them would get implicitly
2444 * renamed by the directory rename detection into the same path,
2445 * and we'd get an add/add/.../add conflict, and all those adds
2446 * from *this* side of history. This is not representable in the
2447 * index, and users aren't going to easily be able to make sense of
2448 * it. So we need to provide a good warning about what's
2449 * happening, and fall back to no-directory-rename detection
2450 * behavior for those paths.
2451 *
2452 * See testcases 9e and all of section 5 from t6043 for examples.
2453 */
2454 collision_init(collisions);
2455
2456 for (i = 0; i < pairs->nr; ++i) {
2457 struct dir_rename_entry *dir_rename_ent;
2458 struct collision_entry *collision_ent;
2459 char *new_path;
2460 struct diff_filepair *pair = pairs->queue[i];
2461
Elijah Newren6e7e0272018-04-19 10:58:15 -07002462 if (pair->status != 'A' && pair->status != 'R')
Elijah Newrene95ab702018-04-19 10:58:07 -07002463 continue;
2464 dir_rename_ent = check_dir_renamed(pair->two->path,
2465 dir_renames);
2466 if (!dir_rename_ent)
2467 continue;
2468
2469 new_path = apply_dir_rename(dir_rename_ent, pair->two->path);
2470 if (!new_path)
2471 /*
2472 * dir_rename_ent->non_unique_new_path is true, which
2473 * means there is no directory rename for us to use,
2474 * which means it won't cause us any additional
2475 * collisions.
2476 */
2477 continue;
2478 collision_ent = collision_find_entry(collisions, new_path);
2479 if (!collision_ent) {
René Scharfeca56dad2021-03-13 17:17:22 +01002480 CALLOC_ARRAY(collision_ent, 1);
Eric Wongd22245a2019-10-06 23:30:27 +00002481 hashmap_entry_init(&collision_ent->ent,
2482 strhash(new_path));
Eric Wong26b455f2019-10-06 23:30:32 +00002483 hashmap_put(collisions, &collision_ent->ent);
Elijah Newrene95ab702018-04-19 10:58:07 -07002484 collision_ent->target_file = new_path;
2485 } else {
2486 free(new_path);
2487 }
2488 string_list_insert(&collision_ent->source_files,
2489 pair->two->path);
2490 }
2491}
2492
Elijah Newren259ccb62019-04-05 08:00:13 -07002493static char *check_for_directory_rename(struct merge_options *opt,
Elijah Newrenf6f77552018-04-19 10:58:08 -07002494 const char *path,
2495 struct tree *tree,
2496 struct hashmap *dir_renames,
2497 struct hashmap *dir_rename_exclusions,
2498 struct hashmap *collisions,
2499 int *clean_merge)
2500{
2501 char *new_path = NULL;
2502 struct dir_rename_entry *entry = check_dir_renamed(path, dir_renames);
2503 struct dir_rename_entry *oentry = NULL;
2504
2505 if (!entry)
2506 return new_path;
2507
2508 /*
2509 * This next part is a little weird. We do not want to do an
2510 * implicit rename into a directory we renamed on our side, because
2511 * that will result in a spurious rename/rename(1to2) conflict. An
2512 * example:
2513 * Base commit: dumbdir/afile, otherdir/bfile
2514 * Side 1: smrtdir/afile, otherdir/bfile
2515 * Side 2: dumbdir/afile, dumbdir/bfile
2516 * Here, while working on Side 1, we could notice that otherdir was
2517 * renamed/merged to dumbdir, and change the diff_filepair for
2518 * otherdir/bfile into a rename into dumbdir/bfile. However, Side
2519 * 2 will notice the rename from dumbdir to smrtdir, and do the
2520 * transitive rename to move it from dumbdir/bfile to
2521 * smrtdir/bfile. That gives us bfile in dumbdir vs being in
2522 * smrtdir, a rename/rename(1to2) conflict. We really just want
2523 * the file to end up in smrtdir. And the way to achieve that is
2524 * to not let Side1 do the rename to dumbdir, since we know that is
2525 * the source of one of our directory renames.
2526 *
2527 * That's why oentry and dir_rename_exclusions is here.
2528 *
2529 * As it turns out, this also prevents N-way transient rename
2530 * confusion; See testcases 9c and 9d of t6043.
2531 */
2532 oentry = dir_rename_find_entry(dir_rename_exclusions, entry->new_dir.buf);
2533 if (oentry) {
Elijah Newren259ccb62019-04-05 08:00:13 -07002534 output(opt, 1, _("WARNING: Avoiding applying %s -> %s rename "
Elijah Newrenf6f77552018-04-19 10:58:08 -07002535 "to %s, because %s itself was renamed."),
2536 entry->dir, entry->new_dir.buf, path, entry->new_dir.buf);
2537 } else {
Elijah Newren259ccb62019-04-05 08:00:13 -07002538 new_path = handle_path_level_conflicts(opt, path, entry,
Elijah Newrenf6f77552018-04-19 10:58:08 -07002539 collisions, tree);
2540 *clean_merge &= (new_path != NULL);
2541 }
2542
2543 return new_path;
2544}
2545
Elijah Newren259ccb62019-04-05 08:00:13 -07002546static void apply_directory_rename_modifications(struct merge_options *opt,
Elijah Newren9c0743f2018-04-19 10:58:10 -07002547 struct diff_filepair *pair,
2548 char *new_path,
2549 struct rename *re,
2550 struct tree *tree,
2551 struct tree *o_tree,
2552 struct tree *a_tree,
2553 struct tree *b_tree,
Jeff Kingb53c5022019-02-14 00:50:02 -05002554 struct string_list *entries)
Elijah Newren9c0743f2018-04-19 10:58:10 -07002555{
2556 struct string_list_item *item;
2557 int stage = (tree == a_tree ? 2 : 3);
Elijah Newren18797a32018-04-19 10:58:13 -07002558 int update_wd;
Elijah Newren9c0743f2018-04-19 10:58:10 -07002559
2560 /*
2561 * In all cases where we can do directory rename detection,
2562 * unpack_trees() will have read pair->two->path into the
2563 * index and the working copy. We need to remove it so that
2564 * we can instead place it at new_path. It is guaranteed to
2565 * not be untracked (unpack_trees() would have errored out
2566 * saying the file would have been overwritten), but it might
2567 * be dirty, though.
2568 */
Elijah Newren259ccb62019-04-05 08:00:13 -07002569 update_wd = !was_dirty(opt, pair->two->path);
Elijah Newren18797a32018-04-19 10:58:13 -07002570 if (!update_wd)
Elijah Newren259ccb62019-04-05 08:00:13 -07002571 output(opt, 1, _("Refusing to lose dirty file at %s"),
Elijah Newren18797a32018-04-19 10:58:13 -07002572 pair->two->path);
Elijah Newren259ccb62019-04-05 08:00:13 -07002573 remove_file(opt, 1, pair->two->path, !update_wd);
Elijah Newren9c0743f2018-04-19 10:58:10 -07002574
2575 /* Find or create a new re->dst_entry */
2576 item = string_list_lookup(entries, new_path);
2577 if (item) {
2578 /*
2579 * Since we're renaming on this side of history, and it's
2580 * due to a directory rename on the other side of history
2581 * (which we only allow when the directory in question no
2582 * longer exists on the other side of history), the
2583 * original entry for re->dst_entry is no longer
2584 * necessary...
2585 */
2586 re->dst_entry->processed = 1;
2587
2588 /*
2589 * ...because we'll be using this new one.
2590 */
2591 re->dst_entry = item->util;
2592 } else {
2593 /*
2594 * re->dst_entry is for the before-dir-rename path, and we
2595 * need it to hold information for the after-dir-rename
2596 * path. Before creating a new entry, we need to mark the
2597 * old one as unnecessary (...unless it is shared by
2598 * src_entry, i.e. this didn't use to be a rename, in which
2599 * case we can just allow the normal processing to happen
2600 * for it).
2601 */
2602 if (pair->status == 'R')
2603 re->dst_entry->processed = 1;
2604
Nguyễn Thái Ngọc Duy34e77712019-06-27 16:28:52 +07002605 re->dst_entry = insert_stage_data(opt->repo, new_path,
Elijah Newren9c0743f2018-04-19 10:58:10 -07002606 o_tree, a_tree, b_tree,
2607 entries);
2608 item = string_list_insert(entries, new_path);
2609 item->util = re->dst_entry;
2610 }
2611
2612 /*
2613 * Update the stage_data with the information about the path we are
2614 * moving into place. That slot will be empty and available for us
2615 * to write to because of the collision checks in
2616 * handle_path_level_conflicts(). In other words,
2617 * re->dst_entry->stages[stage].oid will be the null_oid, so it's
2618 * open for us to write to.
2619 *
2620 * It may be tempting to actually update the index at this point as
2621 * well, using update_stages_for_stage_data(), but as per the big
2622 * "NOTE" in update_stages(), doing so will modify the current
2623 * in-memory index which will break calls to would_lose_untracked()
2624 * that we need to make. Instead, we need to just make sure that
Elijah Newren8ebe7b02018-06-09 21:16:15 -07002625 * the various handle_rename_*() functions update the index
Elijah Newren9c0743f2018-04-19 10:58:10 -07002626 * explicitly rather than relying on unpack_trees() to have done it.
2627 */
Nguyễn Thái Ngọc Duy50ddb082019-06-27 16:28:49 +07002628 get_tree_entry(opt->repo,
2629 &tree->object.oid,
Elijah Newren9c0743f2018-04-19 10:58:10 -07002630 pair->two->path,
2631 &re->dst_entry->stages[stage].oid,
2632 &re->dst_entry->stages[stage].mode);
2633
Elijah Newren6d169fd2019-04-05 08:00:24 -07002634 /*
2635 * Record the original change status (or 'type' of change). If it
2636 * was originally an add ('A'), this lets us differentiate later
2637 * between a RENAME_DELETE conflict and RENAME_VIA_DIR (they
2638 * otherwise look the same). If it was originally a rename ('R'),
2639 * this lets us remember and report accurately about the transitive
2640 * renaming that occurred via the directory rename detection. Also,
2641 * record the original destination name.
2642 */
2643 re->dir_rename_original_type = pair->status;
2644 re->dir_rename_original_dest = pair->two->path;
2645
Elijah Newren9c0743f2018-04-19 10:58:10 -07002646 /*
2647 * We don't actually look at pair->status again, but it seems
2648 * pedagogically correct to adjust it.
2649 */
2650 pair->status = 'R';
2651
2652 /*
2653 * Finally, record the new location.
2654 */
2655 pair->two->path = new_path;
2656}
2657
Elijah Newrene5257b22018-04-19 10:58:03 -07002658/*
2659 * Get information of all renames which occurred in 'pairs', making use of
2660 * any implicit directory renames inferred from the other side of history.
2661 * We need the three trees in the merge ('o_tree', 'a_tree' and 'b_tree')
2662 * to be able to associate the correct cache entries with the rename
2663 * information; tree is always equal to either a_tree or b_tree.
2664 */
Elijah Newren259ccb62019-04-05 08:00:13 -07002665static struct string_list *get_renames(struct merge_options *opt,
Elijah Newrenc336ab82019-04-05 08:00:20 -07002666 const char *branch,
Elijah Newrene5257b22018-04-19 10:58:03 -07002667 struct diff_queue_struct *pairs,
Elijah Newrene95ab702018-04-19 10:58:07 -07002668 struct hashmap *dir_renames,
Elijah Newrenf6f77552018-04-19 10:58:08 -07002669 struct hashmap *dir_rename_exclusions,
Elijah Newrene5257b22018-04-19 10:58:03 -07002670 struct tree *tree,
2671 struct tree *o_tree,
2672 struct tree *a_tree,
2673 struct tree *b_tree,
Elijah Newrenf6f77552018-04-19 10:58:08 -07002674 struct string_list *entries,
2675 int *clean_merge)
Elijah Newrene5257b22018-04-19 10:58:03 -07002676{
2677 int i;
Elijah Newrene95ab702018-04-19 10:58:07 -07002678 struct hashmap collisions;
2679 struct hashmap_iter iter;
2680 struct collision_entry *e;
Elijah Newrene5257b22018-04-19 10:58:03 -07002681 struct string_list *renames;
2682
Elijah Newrene95ab702018-04-19 10:58:07 -07002683 compute_collisions(&collisions, dir_renames, pairs);
René Scharfeca56dad2021-03-13 17:17:22 +01002684 CALLOC_ARRAY(renames, 1);
Elijah Newrene5257b22018-04-19 10:58:03 -07002685
2686 for (i = 0; i < pairs->nr; ++i) {
Elijah Newren9ba91552018-04-19 10:57:59 -07002687 struct string_list_item *item;
2688 struct rename *re;
Elijah Newrene5257b22018-04-19 10:58:03 -07002689 struct diff_filepair *pair = pairs->queue[i];
Elijah Newrenf6f77552018-04-19 10:58:08 -07002690 char *new_path; /* non-NULL only with directory renames */
Elijah Newren9ba91552018-04-19 10:57:59 -07002691
Elijah Newren6e7e0272018-04-19 10:58:15 -07002692 if (pair->status != 'A' && pair->status != 'R') {
Elijah Newren9ba91552018-04-19 10:57:59 -07002693 diff_free_filepair(pair);
2694 continue;
2695 }
Elijah Newren259ccb62019-04-05 08:00:13 -07002696 new_path = check_for_directory_rename(opt, pair->two->path, tree,
Elijah Newrenf6f77552018-04-19 10:58:08 -07002697 dir_renames,
2698 dir_rename_exclusions,
2699 &collisions,
2700 clean_merge);
2701 if (pair->status != 'R' && !new_path) {
2702 diff_free_filepair(pair);
2703 continue;
2704 }
2705
Elijah Newren9ba91552018-04-19 10:57:59 -07002706 re = xmalloc(sizeof(*re));
2707 re->processed = 0;
2708 re->pair = pair;
Elijah Newrenc336ab82019-04-05 08:00:20 -07002709 re->branch = branch;
Elijah Newren6d169fd2019-04-05 08:00:24 -07002710 re->dir_rename_original_type = '\0';
2711 re->dir_rename_original_dest = NULL;
Elijah Newren9ba91552018-04-19 10:57:59 -07002712 item = string_list_lookup(entries, re->pair->one->path);
2713 if (!item)
Nguyễn Thái Ngọc Duy34e77712019-06-27 16:28:52 +07002714 re->src_entry = insert_stage_data(opt->repo,
2715 re->pair->one->path,
Elijah Newren9ba91552018-04-19 10:57:59 -07002716 o_tree, a_tree, b_tree, entries);
2717 else
2718 re->src_entry = item->util;
2719
2720 item = string_list_lookup(entries, re->pair->two->path);
2721 if (!item)
Nguyễn Thái Ngọc Duy34e77712019-06-27 16:28:52 +07002722 re->dst_entry = insert_stage_data(opt->repo,
2723 re->pair->two->path,
Elijah Newren9ba91552018-04-19 10:57:59 -07002724 o_tree, a_tree, b_tree, entries);
2725 else
2726 re->dst_entry = item->util;
2727 item = string_list_insert(renames, pair->one->path);
2728 item->util = re;
Elijah Newren9c0743f2018-04-19 10:58:10 -07002729 if (new_path)
Elijah Newren259ccb62019-04-05 08:00:13 -07002730 apply_directory_rename_modifications(opt, pair, new_path,
Elijah Newren9c0743f2018-04-19 10:58:10 -07002731 re, tree, o_tree,
2732 a_tree, b_tree,
Jeff Kingb53c5022019-02-14 00:50:02 -05002733 entries);
Elijah Newren9ba91552018-04-19 10:57:59 -07002734 }
Elijah Newrene95ab702018-04-19 10:58:07 -07002735
Eric Wong87571c32019-10-06 23:30:38 +00002736 hashmap_for_each_entry(&collisions, &iter, e,
Eric Wong87571c32019-10-06 23:30:38 +00002737 ent /* member name */) {
Elijah Newrene95ab702018-04-19 10:58:07 -07002738 free(e->target_file);
2739 string_list_clear(&e->source_files, 0);
2740 }
Elijah Newren6da1a252020-11-02 18:55:05 +00002741 hashmap_clear_and_free(&collisions, struct collision_entry, ent);
Elijah Newren9ba91552018-04-19 10:57:59 -07002742 return renames;
2743}
2744
Elijah Newren259ccb62019-04-05 08:00:13 -07002745static int process_renames(struct merge_options *opt,
Miklos Vajna8a2fce12008-08-25 16:25:57 +02002746 struct string_list *a_renames,
2747 struct string_list *b_renames)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002748{
2749 int clean_merge = 1, i, j;
Thiago Farina183113a2010-07-04 16:46:19 -03002750 struct string_list a_by_dst = STRING_LIST_INIT_NODUP;
2751 struct string_list b_by_dst = STRING_LIST_INIT_NODUP;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002752 const struct rename *sre;
2753
Elijah Newren816147e2021-03-20 00:03:53 +00002754 /*
2755 * FIXME: As string-list.h notes, it's O(n^2) to build a sorted
2756 * string_list one-by-one, but O(n log n) to build it unsorted and
2757 * then sort it. Note that as we build the list, we do not need to
2758 * check if the existing destination path is already in the list,
2759 * because the structure of diffcore_rename guarantees we won't
2760 * have duplicates.
2761 */
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002762 for (i = 0; i < a_renames->nr; i++) {
2763 sre = a_renames->items[i].util;
Julian Phillips78a395d2010-06-26 00:41:35 +01002764 string_list_insert(&a_by_dst, sre->pair->two->path)->util
Elijah Newren0a6b8712011-08-11 23:20:04 -06002765 = (void *)sre;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002766 }
2767 for (i = 0; i < b_renames->nr; i++) {
2768 sre = b_renames->items[i].util;
Julian Phillips78a395d2010-06-26 00:41:35 +01002769 string_list_insert(&b_by_dst, sre->pair->two->path)->util
Elijah Newren0a6b8712011-08-11 23:20:04 -06002770 = (void *)sre;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002771 }
2772
2773 for (i = 0, j = 0; i < a_renames->nr || j < b_renames->nr;) {
Benjamin Kramer8e24cba2009-03-15 22:01:20 +01002774 struct string_list *renames1, *renames2Dst;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002775 struct rename *ren1 = NULL, *ren2 = NULL;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002776 const char *ren1_src, *ren1_dst;
Elijah Newren461f5042011-08-11 23:20:15 -06002777 struct string_list_item *lookup;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002778
2779 if (i >= a_renames->nr) {
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002780 ren2 = b_renames->items[j++].util;
2781 } else if (j >= b_renames->nr) {
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002782 ren1 = a_renames->items[i++].util;
2783 } else {
Benjamin Kramer8e24cba2009-03-15 22:01:20 +01002784 int compare = strcmp(a_renames->items[i].string,
2785 b_renames->items[j].string);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002786 if (compare <= 0)
2787 ren1 = a_renames->items[i++].util;
2788 if (compare >= 0)
2789 ren2 = b_renames->items[j++].util;
2790 }
2791
2792 /* TODO: refactor, so that 1/2 are not needed */
2793 if (ren1) {
2794 renames1 = a_renames;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002795 renames2Dst = &b_by_dst;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002796 } else {
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002797 renames1 = b_renames;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002798 renames2Dst = &a_by_dst;
René Scharfe35d803b2017-01-28 22:40:58 +01002799 SWAP(ren2, ren1);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002800 }
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002801
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002802 if (ren1->processed)
2803 continue;
2804 ren1->processed = 1;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002805 ren1->dst_entry->processed = 1;
Elijah Newren7769a752011-08-11 23:20:05 -06002806 /* BUG: We should only mark src_entry as processed if we
2807 * are not dealing with a rename + add-source case.
2808 */
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002809 ren1->src_entry->processed = 1;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002810
2811 ren1_src = ren1->pair->one->path;
2812 ren1_dst = ren1->pair->two->path;
2813
2814 if (ren2) {
Elijah Newren461f5042011-08-11 23:20:15 -06002815 /* One file renamed on both sides */
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002816 const char *ren2_src = ren2->pair->one->path;
2817 const char *ren2_dst = ren2->pair->two->path;
Elijah Newren4f66dad2011-08-11 23:20:08 -06002818 enum rename_type rename_type;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002819 if (strcmp(ren1_src, ren2_src) != 0)
Johannes Schindelin033abf92018-05-02 11:38:39 +02002820 BUG("ren1_src != ren2_src");
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002821 ren2->dst_entry->processed = 1;
2822 ren2->processed = 1;
2823 if (strcmp(ren1_dst, ren2_dst) != 0) {
Elijah Newren4f66dad2011-08-11 23:20:08 -06002824 rename_type = RENAME_ONE_FILE_TO_TWO;
Elijah Newren461f5042011-08-11 23:20:15 -06002825 clean_merge = 0;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002826 } else {
Elijah Newren4f66dad2011-08-11 23:20:08 -06002827 rename_type = RENAME_ONE_FILE_TO_ONE;
Elijah Newren7769a752011-08-11 23:20:05 -06002828 /* BUG: We should only remove ren1_src in
2829 * the base stage (think of rename +
2830 * add-source cases).
2831 */
Elijah Newren259ccb62019-04-05 08:00:13 -07002832 remove_file(opt, 1, ren1_src, 1);
Elijah Newrenb8ddf162011-08-11 23:20:02 -06002833 update_entry(ren1->dst_entry,
2834 ren1->pair->one,
2835 ren1->pair->two,
2836 ren2->pair->two);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002837 }
Elijah Newrenc336ab82019-04-05 08:00:20 -07002838 setup_rename_conflict_info(rename_type, opt, ren1, ren2);
Elijah Newren461f5042011-08-11 23:20:15 -06002839 } else if ((lookup = string_list_lookup(renames2Dst, ren1_dst))) {
2840 /* Two different files renamed to the same thing */
2841 char *ren2_dst;
2842 ren2 = lookup->util;
2843 ren2_dst = ren2->pair->two->path;
2844 if (strcmp(ren1_dst, ren2_dst) != 0)
Johannes Schindelin033abf92018-05-02 11:38:39 +02002845 BUG("ren1_dst != ren2_dst");
Elijah Newren461f5042011-08-11 23:20:15 -06002846
2847 clean_merge = 0;
2848 ren2->processed = 1;
2849 /*
2850 * BUG: We should only mark src_entry as processed
2851 * if we are not dealing with a rename + add-source
2852 * case.
2853 */
2854 ren2->src_entry->processed = 1;
2855
2856 setup_rename_conflict_info(RENAME_TWO_FILES_TO_ONE,
Elijah Newrenc336ab82019-04-05 08:00:20 -07002857 opt, ren1, ren2);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002858 } else {
2859 /* Renamed in 1, maybe changed in 2 */
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002860 /* we only use sha1 and mode of these */
2861 struct diff_filespec src_other, dst_other;
Elijah Newren41d70bd2010-09-20 02:28:47 -06002862 int try_merge;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002863
Elijah Newren41d70bd2010-09-20 02:28:47 -06002864 /*
2865 * unpack_trees loads entries from common-commit
2866 * into stage 1, from head-commit into stage 2, and
2867 * from merge-commit into stage 3. We keep track
2868 * of which side corresponds to the rename.
2869 */
2870 int renamed_stage = a_renames == renames1 ? 2 : 3;
2871 int other_stage = a_renames == renames1 ? 3 : 2;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002872
Elijah Newren3585d0e2021-06-30 17:30:00 +00002873 /*
2874 * Directory renames have a funny corner case...
2875 */
2876 int renamed_to_self = !strcmp(ren1_src, ren1_dst);
2877
Elijah Newren7769a752011-08-11 23:20:05 -06002878 /* BUG: We should only remove ren1_src in the base
2879 * stage and in other_stage (think of rename +
2880 * add-source case).
2881 */
Elijah Newren3585d0e2021-06-30 17:30:00 +00002882 if (!renamed_to_self)
2883 remove_file(opt, 1, ren1_src,
2884 renamed_stage == 2 ||
2885 !was_tracked(opt, ren1_src));
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002886
brian m. carlsonfd429e92016-06-24 23:09:25 +00002887 oidcpy(&src_other.oid,
2888 &ren1->src_entry->stages[other_stage].oid);
Elijah Newren41d70bd2010-09-20 02:28:47 -06002889 src_other.mode = ren1->src_entry->stages[other_stage].mode;
brian m. carlsonfd429e92016-06-24 23:09:25 +00002890 oidcpy(&dst_other.oid,
2891 &ren1->dst_entry->stages[other_stage].oid);
Elijah Newren41d70bd2010-09-20 02:28:47 -06002892 dst_other.mode = ren1->dst_entry->stages[other_stage].mode;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002893 try_merge = 0;
2894
brian m. carlson14228442021-04-26 01:02:56 +00002895 if (oideq(&src_other.oid, null_oid()) &&
Elijah Newren6d169fd2019-04-05 08:00:24 -07002896 ren1->dir_rename_original_type == 'A') {
Elijah Newren5455c332018-06-09 21:16:14 -07002897 setup_rename_conflict_info(RENAME_VIA_DIR,
Elijah Newrenc336ab82019-04-05 08:00:20 -07002898 opt, ren1, NULL);
Elijah Newren3585d0e2021-06-30 17:30:00 +00002899 } else if (renamed_to_self) {
2900 setup_rename_conflict_info(RENAME_NORMAL,
2901 opt, ren1, NULL);
brian m. carlson14228442021-04-26 01:02:56 +00002902 } else if (oideq(&src_other.oid, null_oid())) {
Elijah Newren4f66dad2011-08-11 23:20:08 -06002903 setup_rename_conflict_info(RENAME_DELETE,
Elijah Newrenc336ab82019-04-05 08:00:20 -07002904 opt, ren1, NULL);
Schalk, Kend5af5102010-09-01 13:15:32 -07002905 } else if ((dst_other.mode == ren1->pair->two->mode) &&
Elijah Newren763a59e2020-01-01 05:20:57 +00002906 oideq(&dst_other.oid, &ren1->pair->two->oid)) {
Elijah Newren35a74ab2011-08-11 23:20:27 -06002907 /*
2908 * Added file on the other side identical to
2909 * the file being renamed: clean merge.
2910 * Also, there is no need to overwrite the
2911 * file already in the working copy, so call
2912 * update_file_flags() instead of
2913 * update_file().
2914 */
Elijah Newren259ccb62019-04-05 08:00:13 -07002915 if (update_file_flags(opt,
Elijah Newren8daec1d2019-04-05 08:00:22 -07002916 ren1->pair->two,
Johannes Schindelin75456f92016-07-26 18:06:21 +02002917 ren1_dst,
2918 1, /* update_cache */
2919 0 /* update_wd */))
2920 clean_merge = -1;
brian m. carlson14228442021-04-26 01:02:56 +00002921 } else if (!oideq(&dst_other.oid, null_oid())) {
Elijah Newren7f867162018-11-07 20:40:26 -08002922 /*
2923 * Probably not a clean merge, but it's
2924 * premature to set clean_merge to 0 here,
2925 * because if the rename merges cleanly and
2926 * the merge exactly matches the newly added
2927 * file, then the merge will be clean.
2928 */
2929 setup_rename_conflict_info(RENAME_ADD,
Elijah Newrenc336ab82019-04-05 08:00:20 -07002930 opt, ren1, NULL);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002931 } else
2932 try_merge = 1;
2933
Johannes Schindelin75456f92016-07-26 18:06:21 +02002934 if (clean_merge < 0)
2935 goto cleanup_and_return;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002936 if (try_merge) {
Elijah Newrene3de8882019-04-05 08:00:14 -07002937 struct diff_filespec *o, *a, *b;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002938 src_other.path = (char *)ren1_src;
2939
Elijah Newrene3de8882019-04-05 08:00:14 -07002940 o = ren1->pair->one;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002941 if (a_renames == renames1) {
2942 a = ren1->pair->two;
2943 b = &src_other;
2944 } else {
2945 b = ren1->pair->two;
2946 a = &src_other;
2947 }
Elijah Newrene3de8882019-04-05 08:00:14 -07002948 update_entry(ren1->dst_entry, o, a, b);
Elijah Newren4f66dad2011-08-11 23:20:08 -06002949 setup_rename_conflict_info(RENAME_NORMAL,
Elijah Newrenc336ab82019-04-05 08:00:20 -07002950 opt, ren1, NULL);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002951 }
2952 }
2953 }
Johannes Schindelin75456f92016-07-26 18:06:21 +02002954cleanup_and_return:
Miklos Vajna9047ebb2008-08-12 18:45:14 +02002955 string_list_clear(&a_by_dst, 0);
2956 string_list_clear(&b_by_dst, 0);
2957
2958 return clean_merge;
2959}
2960
Elijah Newrenf1725892018-04-19 10:58:00 -07002961struct rename_info {
2962 struct string_list *head_renames;
2963 struct string_list *merge_renames;
2964};
2965
Elijah Newren7fe40b82018-04-19 10:58:05 -07002966static void initial_cleanup_rename(struct diff_queue_struct *pairs,
2967 struct hashmap *dir_renames)
Elijah Newrenffc16c42018-04-19 10:58:04 -07002968{
Elijah Newren7fe40b82018-04-19 10:58:05 -07002969 struct hashmap_iter iter;
2970 struct dir_rename_entry *e;
2971
Eric Wong87571c32019-10-06 23:30:38 +00002972 hashmap_for_each_entry(dir_renames, &iter, e,
Eric Wong87571c32019-10-06 23:30:38 +00002973 ent /* member name */) {
Elijah Newren7fe40b82018-04-19 10:58:05 -07002974 free(e->dir);
2975 strbuf_release(&e->new_dir);
2976 /* possible_new_dirs already cleared in get_directory_renames */
2977 }
Elijah Newren6da1a252020-11-02 18:55:05 +00002978 hashmap_clear_and_free(dir_renames, struct dir_rename_entry, ent);
Elijah Newren7fe40b82018-04-19 10:58:05 -07002979 free(dir_renames);
2980
Elijah Newrenffc16c42018-04-19 10:58:04 -07002981 free(pairs->queue);
2982 free(pairs);
2983}
2984
Elijah Newren259ccb62019-04-05 08:00:13 -07002985static int detect_and_process_renames(struct merge_options *opt,
Elijah Newren8ebe7b02018-06-09 21:16:15 -07002986 struct tree *common,
2987 struct tree *head,
2988 struct tree *merge,
2989 struct string_list *entries,
2990 struct rename_info *ri)
Elijah Newrenf1725892018-04-19 10:58:00 -07002991{
Elijah Newrene5257b22018-04-19 10:58:03 -07002992 struct diff_queue_struct *head_pairs, *merge_pairs;
Elijah Newren7fe40b82018-04-19 10:58:05 -07002993 struct hashmap *dir_re_head, *dir_re_merge;
Elijah Newrenf6f77552018-04-19 10:58:08 -07002994 int clean = 1;
Elijah Newrene5257b22018-04-19 10:58:03 -07002995
Elijah Newren3992ff02018-04-19 10:58:02 -07002996 ri->head_renames = NULL;
2997 ri->merge_renames = NULL;
2998
Elijah Newren259ccb62019-04-05 08:00:13 -07002999 if (!merge_detect_rename(opt))
Elijah Newren3992ff02018-04-19 10:58:02 -07003000 return 1;
3001
Elijah Newren259ccb62019-04-05 08:00:13 -07003002 head_pairs = get_diffpairs(opt, common, head);
3003 merge_pairs = get_diffpairs(opt, common, merge);
Elijah Newrene5257b22018-04-19 10:58:03 -07003004
Derrick Stolee8e012512019-08-17 11:41:25 -07003005 if ((opt->detect_directory_renames == MERGE_DIRECTORY_RENAMES_TRUE) ||
3006 (opt->detect_directory_renames == MERGE_DIRECTORY_RENAMES_CONFLICT &&
Elijah Newren5bf7e572019-08-17 11:41:41 -07003007 !opt->priv->call_depth)) {
Jeff Kingb53c5022019-02-14 00:50:02 -05003008 dir_re_head = get_directory_renames(head_pairs);
3009 dir_re_merge = get_directory_renames(merge_pairs);
Elijah Newren7fe40b82018-04-19 10:58:05 -07003010
Elijah Newren259ccb62019-04-05 08:00:13 -07003011 handle_directory_level_conflicts(opt,
Elijah Newren5fdddd92018-08-29 00:06:12 -07003012 dir_re_head, head,
3013 dir_re_merge, merge);
3014 } else {
3015 dir_re_head = xmalloc(sizeof(*dir_re_head));
3016 dir_re_merge = xmalloc(sizeof(*dir_re_merge));
3017 dir_rename_init(dir_re_head);
3018 dir_rename_init(dir_re_merge);
3019 }
Elijah Newren96e7ffb2018-04-19 10:58:06 -07003020
Elijah Newrenc336ab82019-04-05 08:00:20 -07003021 ri->head_renames = get_renames(opt, opt->branch1, head_pairs,
Elijah Newrenf6f77552018-04-19 10:58:08 -07003022 dir_re_merge, dir_re_head, head,
3023 common, head, merge, entries,
3024 &clean);
3025 if (clean < 0)
3026 goto cleanup;
Elijah Newrenc336ab82019-04-05 08:00:20 -07003027 ri->merge_renames = get_renames(opt, opt->branch2, merge_pairs,
Elijah Newrenf6f77552018-04-19 10:58:08 -07003028 dir_re_head, dir_re_merge, merge,
3029 common, head, merge, entries,
3030 &clean);
3031 if (clean < 0)
3032 goto cleanup;
Elijah Newren259ccb62019-04-05 08:00:13 -07003033 clean &= process_renames(opt, ri->head_renames, ri->merge_renames);
Elijah Newrene5257b22018-04-19 10:58:03 -07003034
Elijah Newrenf6f77552018-04-19 10:58:08 -07003035cleanup:
Elijah Newrene5257b22018-04-19 10:58:03 -07003036 /*
3037 * Some cleanup is deferred until cleanup_renames() because the
3038 * data structures are still needed and referenced in
3039 * process_entry(). But there are a few things we can free now.
3040 */
Elijah Newren7fe40b82018-04-19 10:58:05 -07003041 initial_cleanup_rename(head_pairs, dir_re_head);
3042 initial_cleanup_rename(merge_pairs, dir_re_merge);
Elijah Newrene5257b22018-04-19 10:58:03 -07003043
3044 return clean;
Elijah Newrenf1725892018-04-19 10:58:00 -07003045}
3046
Elijah Newrenffc16c42018-04-19 10:58:04 -07003047static void final_cleanup_rename(struct string_list *rename)
Elijah Newren9cfee252018-04-19 10:58:01 -07003048{
3049 const struct rename *re;
3050 int i;
3051
Junio C Hamanoafe8a902022-05-02 09:50:37 -07003052 if (!rename)
Elijah Newren3992ff02018-04-19 10:58:02 -07003053 return;
3054
Elijah Newren9cfee252018-04-19 10:58:01 -07003055 for (i = 0; i < rename->nr; i++) {
3056 re = rename->items[i].util;
3057 diff_free_filepair(re->pair);
3058 }
3059 string_list_clear(rename, 1);
3060 free(rename);
3061}
3062
Elijah Newrenffc16c42018-04-19 10:58:04 -07003063static void final_cleanup_renames(struct rename_info *re_info)
Elijah Newrenf1725892018-04-19 10:58:00 -07003064{
Elijah Newrenffc16c42018-04-19 10:58:04 -07003065 final_cleanup_rename(re_info->head_renames);
3066 final_cleanup_rename(re_info->merge_renames);
Elijah Newrenf1725892018-04-19 10:58:00 -07003067}
3068
Elijah Newren259ccb62019-04-05 08:00:13 -07003069static int read_oid_strbuf(struct merge_options *opt,
Elijah Newrend90e7592018-06-09 21:16:12 -07003070 const struct object_id *oid,
3071 struct strbuf *dst)
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02003072{
3073 void *buf;
3074 enum object_type type;
3075 unsigned long size;
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +02003076 buf = repo_read_object_file(the_repository, oid, &type, &size);
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02003077 if (!buf)
Elijah Newren259ccb62019-04-05 08:00:13 -07003078 return err(opt, _("cannot read object %s"), oid_to_hex(oid));
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02003079 if (type != OBJ_BLOB) {
3080 free(buf);
Elijah Newren259ccb62019-04-05 08:00:13 -07003081 return err(opt, _("object %s is not a blob"), oid_to_hex(oid));
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02003082 }
3083 strbuf_attach(dst, buf, size, size + 1);
3084 return 0;
3085}
3086
Johannes Schindelinbc9204d2016-08-01 13:44:37 +02003087static int blob_unchanged(struct merge_options *opt,
Elijah Newren8daec1d2019-04-05 08:00:22 -07003088 const struct diff_filespec *o,
3089 const struct diff_filespec *a,
Jonathan Nieder3e7589b2010-08-05 06:13:49 -05003090 int renormalize, const char *path)
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02003091{
Elijah Newren93a02c52019-04-05 08:00:15 -07003092 struct strbuf obuf = STRBUF_INIT;
3093 struct strbuf abuf = STRBUF_INIT;
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02003094 int ret = 0; /* assume changed for safety */
Derrick Stolee847a9e52021-04-01 01:49:39 +00003095 struct index_state *idx = opt->repo->index;
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02003096
Elijah Newren8daec1d2019-04-05 08:00:22 -07003097 if (a->mode != o->mode)
Jeff King72fac662015-10-26 17:39:39 -04003098 return 0;
Elijah Newren763a59e2020-01-01 05:20:57 +00003099 if (oideq(&o->oid, &a->oid))
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02003100 return 1;
Jonathan Nieder3e7589b2010-08-05 06:13:49 -05003101 if (!renormalize)
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02003102 return 0;
3103
Elijah Newren8daec1d2019-04-05 08:00:22 -07003104 if (read_oid_strbuf(opt, &o->oid, &obuf) ||
3105 read_oid_strbuf(opt, &a->oid, &abuf))
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02003106 goto error_return;
3107 /*
3108 * Note: binary | is used so that both renormalizations are
3109 * performed. Comparison can be skipped if both files are
3110 * unchanged since their sha1s have already been compared.
3111 */
Elijah Newren93a02c52019-04-05 08:00:15 -07003112 if (renormalize_buffer(idx, path, obuf.buf, obuf.len, &obuf) |
3113 renormalize_buffer(idx, path, abuf.buf, abuf.len, &abuf))
3114 ret = (obuf.len == abuf.len && !memcmp(obuf.buf, abuf.buf, obuf.len));
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02003115
3116error_return:
Elijah Newren93a02c52019-04-05 08:00:15 -07003117 strbuf_release(&obuf);
3118 strbuf_release(&abuf);
Eyvind Bernhardsen331a1832010-07-02 21:20:48 +02003119 return ret;
3120}
3121
Elijah Newren259ccb62019-04-05 08:00:13 -07003122static int handle_modify_delete(struct merge_options *opt,
Elijah Newrend90e7592018-06-09 21:16:12 -07003123 const char *path,
Elijah Newren8daec1d2019-04-05 08:00:22 -07003124 const struct diff_filespec *o,
3125 const struct diff_filespec *a,
3126 const struct diff_filespec *b)
Elijah Newren5e3ce662010-09-20 02:28:51 -06003127{
Matt McCutchenb26d87f2017-01-28 15:37:01 -05003128 const char *modify_branch, *delete_branch;
Elijah Newren8daec1d2019-04-05 08:00:22 -07003129 const struct diff_filespec *changed;
Matt McCutchenb26d87f2017-01-28 15:37:01 -05003130
Elijah Newren8daec1d2019-04-05 08:00:22 -07003131 if (is_valid(a)) {
Elijah Newren259ccb62019-04-05 08:00:13 -07003132 modify_branch = opt->branch1;
3133 delete_branch = opt->branch2;
Elijah Newren8daec1d2019-04-05 08:00:22 -07003134 changed = a;
Matt McCutchenb26d87f2017-01-28 15:37:01 -05003135 } else {
Elijah Newren259ccb62019-04-05 08:00:13 -07003136 modify_branch = opt->branch2;
3137 delete_branch = opt->branch1;
Elijah Newren8daec1d2019-04-05 08:00:22 -07003138 changed = b;
Matt McCutchenb26d87f2017-01-28 15:37:01 -05003139 }
3140
Elijah Newren259ccb62019-04-05 08:00:13 -07003141 return handle_change_delete(opt,
Matt McCutchenb26d87f2017-01-28 15:37:01 -05003142 path, NULL,
Elijah Newren8daec1d2019-04-05 08:00:22 -07003143 o, changed,
Matt McCutchenb26d87f2017-01-28 15:37:01 -05003144 modify_branch, delete_branch,
Johannes Schindelin75456f92016-07-26 18:06:21 +02003145 _("modify"), _("modified"));
Elijah Newren5e3ce662010-09-20 02:28:51 -06003146}
3147
Elijah Newrene62d1122019-04-05 08:00:25 -07003148static int handle_content_merge(struct merge_file_info *mfi,
3149 struct merge_options *opt,
Elijah Newrend9573552018-09-19 09:14:34 -07003150 const char *path,
3151 int is_dirty,
Elijah Newren8daec1d2019-04-05 08:00:22 -07003152 const struct diff_filespec *o,
3153 const struct diff_filespec *a,
3154 const struct diff_filespec *b,
Elijah Newren043622b2019-04-05 08:00:16 -07003155 struct rename_conflict_info *ci)
Elijah Newren0c4918d2010-09-20 02:28:52 -06003156{
Jiang Xin55653a62012-07-25 22:53:13 +08003157 const char *reason = _("content");
Elijah Newren4ab9a152010-09-20 02:29:07 -06003158 unsigned df_conflict_remains = 0;
Elijah Newren0c4918d2010-09-20 02:28:52 -06003159
Elijah Newren8daec1d2019-04-05 08:00:22 -07003160 if (!is_valid(o))
Jiang Xin55653a62012-07-25 22:53:13 +08003161 reason = _("add/add");
Elijah Newren0c4918d2010-09-20 02:28:52 -06003162
Elijah Newren8daec1d2019-04-05 08:00:22 -07003163 assert(o->path && a->path && b->path);
Elijah Newren5bf7e572019-08-17 11:41:41 -07003164 if (ci && dir_in_way(opt->repo->index, path, !opt->priv->call_depth,
Elijah Newren8daec1d2019-04-05 08:00:22 -07003165 S_ISGITLINK(ci->ren1->pair->two->mode)))
3166 df_conflict_remains = 1;
Elijah Newren3c217c02011-08-11 23:20:09 -06003167
Elijah Newren8daec1d2019-04-05 08:00:22 -07003168 if (merge_mode_and_contents(opt, o, a, b, path,
Elijah Newren259ccb62019-04-05 08:00:13 -07003169 opt->branch1, opt->branch2,
Elijah Newren5bf7e572019-08-17 11:41:41 -07003170 opt->priv->call_depth * 2, mfi))
Johannes Schindelin3c8a51e2016-07-26 18:06:10 +02003171 return -1;
Elijah Newren4ab9a152010-09-20 02:29:07 -06003172
Elijah Newren1de70db2018-04-19 10:58:23 -07003173 /*
3174 * We can skip updating the working tree file iff:
3175 * a) The merge is clean
3176 * b) The merge matches what was in HEAD (content, mode, pathname)
3177 * c) The target path is usable (i.e. not involved in D/F conflict)
3178 */
Elijah Newrene62d1122019-04-05 08:00:25 -07003179 if (mfi->clean && was_tracked_and_matches(opt, path, &mfi->blob) &&
Elijah Newren1de70db2018-04-19 10:58:23 -07003180 !df_conflict_remains) {
Elijah Newren2b75fb62018-07-27 12:59:44 +00003181 int pos;
3182 struct cache_entry *ce;
3183
Elijah Newren259ccb62019-04-05 08:00:13 -07003184 output(opt, 3, _("Skipped %s (merged same as existing)"), path);
Elijah Newrene62d1122019-04-05 08:00:25 -07003185 if (add_cacheinfo(opt, &mfi->blob, path,
Elijah Newren5bf7e572019-08-17 11:41:41 -07003186 0, (!opt->priv->call_depth && !is_dirty), 0))
Elijah Newren1de70db2018-04-19 10:58:23 -07003187 return -1;
Elijah Newren2b75fb62018-07-27 12:59:44 +00003188 /*
3189 * However, add_cacheinfo() will delete the old cache entry
3190 * and add a new one. We need to copy over any skip_worktree
3191 * flag to avoid making the file appear as if it were
3192 * deleted by the user.
3193 */
Elijah Newren5bf7e572019-08-17 11:41:41 -07003194 pos = index_name_pos(&opt->priv->orig_index, path, strlen(path));
3195 ce = opt->priv->orig_index.cache[pos];
Elijah Newren2b75fb62018-07-27 12:59:44 +00003196 if (ce_skip_worktree(ce)) {
Elijah Newren259ccb62019-04-05 08:00:13 -07003197 pos = index_name_pos(opt->repo->index, path, strlen(path));
3198 ce = opt->repo->index->cache[pos];
Elijah Newren2b75fb62018-07-27 12:59:44 +00003199 ce->ce_flags |= CE_SKIP_WORKTREE;
3200 }
Elijah Newrene62d1122019-04-05 08:00:25 -07003201 return mfi->clean;
Elijah Newren05cf21e2018-04-19 10:58:22 -07003202 }
Elijah Newren0c4918d2010-09-20 02:28:52 -06003203
Elijah Newrene62d1122019-04-05 08:00:25 -07003204 if (!mfi->clean) {
3205 if (S_ISGITLINK(mfi->blob.mode))
Jiang Xin55653a62012-07-25 22:53:13 +08003206 reason = _("submodule");
Elijah Newren259ccb62019-04-05 08:00:13 -07003207 output(opt, 1, _("CONFLICT (%s): Merge conflict in %s"),
Elijah Newren0c4918d2010-09-20 02:28:52 -06003208 reason, path);
Elijah Newren043622b2019-04-05 08:00:16 -07003209 if (ci && !df_conflict_remains)
Elijah Newren8daec1d2019-04-05 08:00:22 -07003210 if (update_stages(opt, path, o, a, b))
Johannes Schindelin75456f92016-07-26 18:06:21 +02003211 return -1;
Elijah Newren0c4918d2010-09-20 02:28:52 -06003212 }
3213
Elijah Newrenbd423802018-04-19 10:58:17 -07003214 if (df_conflict_remains || is_dirty) {
Elijah Newren3d6b8e82011-08-11 23:19:53 -06003215 char *new_path;
Elijah Newren5bf7e572019-08-17 11:41:41 -07003216 if (opt->priv->call_depth) {
Elijah Newren259ccb62019-04-05 08:00:13 -07003217 remove_file_from_index(opt->repo->index, path);
Elijah Newren51931bf2011-08-11 23:20:06 -06003218 } else {
Elijah Newrene62d1122019-04-05 08:00:25 -07003219 if (!mfi->clean) {
Elijah Newren8daec1d2019-04-05 08:00:22 -07003220 if (update_stages(opt, path, o, a, b))
Johannes Schindelin75456f92016-07-26 18:06:21 +02003221 return -1;
3222 } else {
Elijah Newren259ccb62019-04-05 08:00:13 -07003223 int file_from_stage2 = was_tracked(opt, path);
Elijah Newren51931bf2011-08-11 23:20:06 -06003224
Elijah Newren259ccb62019-04-05 08:00:13 -07003225 if (update_stages(opt, path, NULL,
Elijah Newrene62d1122019-04-05 08:00:25 -07003226 file_from_stage2 ? &mfi->blob : NULL,
3227 file_from_stage2 ? NULL : &mfi->blob))
Johannes Schindelin75456f92016-07-26 18:06:21 +02003228 return -1;
Elijah Newren51931bf2011-08-11 23:20:06 -06003229 }
3230
3231 }
Elijah Newrenc336ab82019-04-05 08:00:20 -07003232 new_path = unique_path(opt, path, ci->ren1->branch);
Elijah Newrenbd423802018-04-19 10:58:17 -07003233 if (is_dirty) {
Elijah Newren259ccb62019-04-05 08:00:13 -07003234 output(opt, 1, _("Refusing to lose dirty file at %s"),
Elijah Newrenbd423802018-04-19 10:58:17 -07003235 path);
3236 }
Elijah Newren259ccb62019-04-05 08:00:13 -07003237 output(opt, 1, _("Adding as %s instead"), new_path);
Elijah Newrene62d1122019-04-05 08:00:25 -07003238 if (update_file(opt, 0, &mfi->blob, new_path)) {
Johannes Schindelin75456f92016-07-26 18:06:21 +02003239 free(new_path);
3240 return -1;
3241 }
Elijah Newren3d6b8e82011-08-11 23:19:53 -06003242 free(new_path);
Elijah Newrene62d1122019-04-05 08:00:25 -07003243 mfi->clean = 0;
3244 } else if (update_file(opt, mfi->clean, &mfi->blob, path))
Johannes Schindelin75456f92016-07-26 18:06:21 +02003245 return -1;
Elijah Newrene62d1122019-04-05 08:00:25 -07003246 return !is_dirty && mfi->clean;
Elijah Newren0c4918d2010-09-20 02:28:52 -06003247}
3248
Elijah Newren259ccb62019-04-05 08:00:13 -07003249static int handle_rename_normal(struct merge_options *opt,
Elijah Newren8ebe7b02018-06-09 21:16:15 -07003250 const char *path,
Elijah Newren8daec1d2019-04-05 08:00:22 -07003251 const struct diff_filespec *o,
3252 const struct diff_filespec *a,
3253 const struct diff_filespec *b,
Elijah Newren8ebe7b02018-06-09 21:16:15 -07003254 struct rename_conflict_info *ci)
Elijah Newren64b1abe2018-04-19 10:58:12 -07003255{
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07003256 struct rename *ren = ci->ren1;
Elijah Newrene62d1122019-04-05 08:00:25 -07003257 struct merge_file_info mfi;
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07003258 int clean;
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07003259
Elijah Newren64b1abe2018-04-19 10:58:12 -07003260 /* Merge the content and write it out */
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07003261 clean = handle_content_merge(&mfi, opt, path, was_dirty(opt, path),
3262 o, a, b, ci);
3263
Derrick Stolee8e012512019-08-17 11:41:25 -07003264 if (clean &&
3265 opt->detect_directory_renames == MERGE_DIRECTORY_RENAMES_CONFLICT &&
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07003266 ren->dir_rename_original_dest) {
3267 if (update_stages(opt, path,
Elijah Newren3585d0e2021-06-30 17:30:00 +00003268 &mfi.blob, &mfi.blob, &mfi.blob))
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07003269 return -1;
3270 clean = 0; /* not clean, but conflicted */
3271 }
3272 return clean;
3273}
3274
3275static void dir_rename_warning(const char *msg,
3276 int is_add,
3277 int clean,
3278 struct merge_options *opt,
3279 struct rename *ren)
3280{
3281 const char *other_branch;
3282 other_branch = (ren->branch == opt->branch1 ?
3283 opt->branch2 : opt->branch1);
3284 if (is_add) {
3285 output(opt, clean ? 2 : 1, msg,
3286 ren->pair->one->path, ren->branch,
3287 other_branch, ren->pair->two->path);
3288 return;
3289 }
3290 output(opt, clean ? 2 : 1, msg,
3291 ren->pair->one->path, ren->dir_rename_original_dest, ren->branch,
3292 other_branch, ren->pair->two->path);
3293}
3294static int warn_about_dir_renamed_entries(struct merge_options *opt,
3295 struct rename *ren)
3296{
3297 const char *msg;
3298 int clean = 1, is_add;
3299
3300 if (!ren)
3301 return clean;
3302
3303 /* Return early if ren was not affected/created by a directory rename */
3304 if (!ren->dir_rename_original_dest)
3305 return clean;
3306
3307 /* Sanity checks */
Derrick Stolee8e012512019-08-17 11:41:25 -07003308 assert(opt->detect_directory_renames > MERGE_DIRECTORY_RENAMES_NONE);
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07003309 assert(ren->dir_rename_original_type == 'A' ||
3310 ren->dir_rename_original_type == 'R');
3311
3312 /* Check whether to treat directory renames as a conflict */
Derrick Stolee8e012512019-08-17 11:41:25 -07003313 clean = (opt->detect_directory_renames == MERGE_DIRECTORY_RENAMES_TRUE);
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07003314
3315 is_add = (ren->dir_rename_original_type == 'A');
3316 if (ren->dir_rename_original_type == 'A' && clean) {
3317 msg = _("Path updated: %s added in %s inside a "
3318 "directory that was renamed in %s; moving it to %s.");
3319 } else if (ren->dir_rename_original_type == 'A' && !clean) {
3320 msg = _("CONFLICT (file location): %s added in %s "
3321 "inside a directory that was renamed in %s, "
3322 "suggesting it should perhaps be moved to %s.");
3323 } else if (ren->dir_rename_original_type == 'R' && clean) {
3324 msg = _("Path updated: %s renamed to %s in %s, inside a "
3325 "directory that was renamed in %s; moving it to %s.");
3326 } else if (ren->dir_rename_original_type == 'R' && !clean) {
3327 msg = _("CONFLICT (file location): %s renamed to %s in %s, "
3328 "inside a directory that was renamed in %s, "
3329 "suggesting it should perhaps be moved to %s.");
3330 } else {
3331 BUG("Impossible dir_rename_original_type/clean combination");
3332 }
3333 dir_rename_warning(msg, is_add, clean, opt, ren);
3334
3335 return clean;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003336}
3337
3338/* Per entry merge function */
Elijah Newren259ccb62019-04-05 08:00:13 -07003339static int process_entry(struct merge_options *opt,
Miklos Vajna8a2fce12008-08-25 16:25:57 +02003340 const char *path, struct stage_data *entry)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003341{
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003342 int clean_merge = 1;
Elijah Newren259ccb62019-04-05 08:00:13 -07003343 int normalize = opt->renormalize;
Elijah Newren8daec1d2019-04-05 08:00:22 -07003344
3345 struct diff_filespec *o = &entry->stages[1];
3346 struct diff_filespec *a = &entry->stages[2];
3347 struct diff_filespec *b = &entry->stages[3];
3348 int o_valid = is_valid(o);
3349 int a_valid = is_valid(a);
3350 int b_valid = is_valid(b);
3351 o->path = a->path = b->path = (char*)path;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003352
Elijah Newren37348932010-07-09 07:10:53 -06003353 entry->processed = 1;
Elijah Newren4f66dad2011-08-11 23:20:08 -06003354 if (entry->rename_conflict_info) {
Elijah Newren043622b2019-04-05 08:00:16 -07003355 struct rename_conflict_info *ci = entry->rename_conflict_info;
Elijah Newren8daec1d2019-04-05 08:00:22 -07003356 struct diff_filespec *temp;
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07003357 int path_clean;
3358
3359 path_clean = warn_about_dir_renamed_entries(opt, ci->ren1);
3360 path_clean &= warn_about_dir_renamed_entries(opt, ci->ren2);
Elijah Newren8daec1d2019-04-05 08:00:22 -07003361
3362 /*
3363 * For cases with a single rename, {o,a,b}->path have all been
3364 * set to the rename target path; we need to set two of these
3365 * back to the rename source.
3366 * For rename/rename conflicts, we'll manually fix paths below.
3367 */
3368 temp = (opt->branch1 == ci->ren1->branch) ? b : a;
3369 o->path = temp->path = ci->ren1->pair->one->path;
3370 if (ci->ren2) {
3371 assert(opt->branch1 == ci->ren1->branch);
3372 }
3373
Elijah Newren043622b2019-04-05 08:00:16 -07003374 switch (ci->rename_type) {
Elijah Newren882fd112010-09-20 02:29:03 -06003375 case RENAME_NORMAL:
Elijah Newren4f66dad2011-08-11 23:20:08 -06003376 case RENAME_ONE_FILE_TO_ONE:
Elijah Newren8daec1d2019-04-05 08:00:22 -07003377 clean_merge = handle_rename_normal(opt, path, o, a, b,
Elijah Newren043622b2019-04-05 08:00:16 -07003378 ci);
Elijah Newren882fd112010-09-20 02:29:03 -06003379 break;
Elijah Newren5455c332018-06-09 21:16:14 -07003380 case RENAME_VIA_DIR:
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07003381 clean_merge = handle_rename_via_dir(opt, ci);
Elijah Newren9c0bbb52010-09-20 02:28:56 -06003382 break;
Elijah Newren7f867162018-11-07 20:40:26 -08003383 case RENAME_ADD:
3384 /*
3385 * Probably unclean merge, but if the renamed file
3386 * merges cleanly and the result can then be
3387 * two-way merged cleanly with the added file, I
3388 * guess it's a clean merge?
3389 */
Elijah Newren043622b2019-04-05 08:00:16 -07003390 clean_merge = handle_rename_add(opt, ci);
Elijah Newren7f867162018-11-07 20:40:26 -08003391 break;
Elijah Newren3b130adf2010-09-20 02:29:02 -06003392 case RENAME_DELETE:
3393 clean_merge = 0;
Elijah Newrene2d563d2019-04-05 08:00:21 -07003394 if (handle_rename_delete(opt, ci))
Johannes Schindelin75456f92016-07-26 18:06:21 +02003395 clean_merge = -1;
Elijah Newren3b130adf2010-09-20 02:29:02 -06003396 break;
Elijah Newren07413c52010-09-20 02:29:00 -06003397 case RENAME_ONE_FILE_TO_TWO:
Elijah Newren8daec1d2019-04-05 08:00:22 -07003398 /*
3399 * Manually fix up paths; note:
3400 * ren[12]->pair->one->path are equal.
3401 */
3402 o->path = ci->ren1->pair->one->path;
3403 a->path = ci->ren1->pair->two->path;
3404 b->path = ci->ren2->pair->two->path;
3405
Elijah Newren07413c52010-09-20 02:29:00 -06003406 clean_merge = 0;
Elijah Newren043622b2019-04-05 08:00:16 -07003407 if (handle_rename_rename_1to2(opt, ci))
Johannes Schindelin75456f92016-07-26 18:06:21 +02003408 clean_merge = -1;
Elijah Newren07413c52010-09-20 02:29:00 -06003409 break;
Elijah Newren461f5042011-08-11 23:20:15 -06003410 case RENAME_TWO_FILES_TO_ONE:
Elijah Newrenbbafc9c2018-11-07 20:40:27 -08003411 /*
Elijah Newren8daec1d2019-04-05 08:00:22 -07003412 * Manually fix up paths; note,
3413 * ren[12]->pair->two->path are actually equal.
3414 */
3415 o->path = NULL;
3416 a->path = ci->ren1->pair->two->path;
3417 b->path = ci->ren2->pair->two->path;
3418
3419 /*
Elijah Newrenbbafc9c2018-11-07 20:40:27 -08003420 * Probably unclean merge, but if the two renamed
3421 * files merge cleanly and the two resulting files
3422 * can then be two-way merged cleanly, I guess it's
3423 * a clean merge?
3424 */
Elijah Newren043622b2019-04-05 08:00:16 -07003425 clean_merge = handle_rename_rename_2to1(opt, ci);
Elijah Newren461f5042011-08-11 23:20:15 -06003426 break;
Elijah Newren07413c52010-09-20 02:29:00 -06003427 default:
3428 entry->processed = 0;
3429 break;
3430 }
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07003431 if (path_clean < clean_merge)
3432 clean_merge = path_clean;
Elijah Newren8daec1d2019-04-05 08:00:22 -07003433 } else if (o_valid && (!a_valid || !b_valid)) {
Elijah Newren37348932010-07-09 07:10:53 -06003434 /* Case A: Deleted in one */
Elijah Newren8daec1d2019-04-05 08:00:22 -07003435 if ((!a_valid && !b_valid) ||
3436 (!b_valid && blob_unchanged(opt, o, a, normalize, path)) ||
3437 (!a_valid && blob_unchanged(opt, o, b, normalize, path))) {
Elijah Newren37348932010-07-09 07:10:53 -06003438 /* Deleted in both or deleted in one and
3439 * unchanged in the other */
Elijah Newren8daec1d2019-04-05 08:00:22 -07003440 if (a_valid)
Elijah Newren259ccb62019-04-05 08:00:13 -07003441 output(opt, 2, _("Removing %s"), path);
Elijah Newren37348932010-07-09 07:10:53 -06003442 /* do not touch working file if it did not exist */
Elijah Newren8daec1d2019-04-05 08:00:22 -07003443 remove_file(opt, 1, path, !a_valid);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003444 } else {
Elijah Newrenedd2faf2011-08-11 23:20:07 -06003445 /* Modify/delete; deleted side may have put a directory in the way */
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003446 clean_merge = 0;
Elijah Newren8daec1d2019-04-05 08:00:22 -07003447 if (handle_modify_delete(opt, path, o, a, b))
Johannes Schindelin75456f92016-07-26 18:06:21 +02003448 clean_merge = -1;
Miklos Vajna8a2fce12008-08-25 16:25:57 +02003449 }
Elijah Newren8daec1d2019-04-05 08:00:22 -07003450 } else if ((!o_valid && a_valid && !b_valid) ||
3451 (!o_valid && !a_valid && b_valid)) {
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003452 /* Case B: Added in one. */
Elijah Newrenedd2faf2011-08-11 23:20:07 -06003453 /* [nothing|directory] -> ([nothing|directory], file) */
Miklos Vajna8a2fce12008-08-25 16:25:57 +02003454
Elijah Newren9c0bbb52010-09-20 02:28:56 -06003455 const char *add_branch;
3456 const char *other_branch;
Elijah Newren9c0bbb52010-09-20 02:28:56 -06003457 const char *conf;
Elijah Newren8daec1d2019-04-05 08:00:22 -07003458 const struct diff_filespec *contents;
Elijah Newren37348932010-07-09 07:10:53 -06003459
Elijah Newren8daec1d2019-04-05 08:00:22 -07003460 if (a_valid) {
Elijah Newren259ccb62019-04-05 08:00:13 -07003461 add_branch = opt->branch1;
3462 other_branch = opt->branch2;
Elijah Newren8daec1d2019-04-05 08:00:22 -07003463 contents = a;
Jiang Xin55653a62012-07-25 22:53:13 +08003464 conf = _("file/directory");
Elijah Newren9c0bbb52010-09-20 02:28:56 -06003465 } else {
Elijah Newren259ccb62019-04-05 08:00:13 -07003466 add_branch = opt->branch2;
3467 other_branch = opt->branch1;
Elijah Newren8daec1d2019-04-05 08:00:22 -07003468 contents = b;
Jiang Xin55653a62012-07-25 22:53:13 +08003469 conf = _("directory/file");
Elijah Newren9c0bbb52010-09-20 02:28:56 -06003470 }
Elijah Newren259ccb62019-04-05 08:00:13 -07003471 if (dir_in_way(opt->repo->index, path,
Elijah Newren5bf7e572019-08-17 11:41:41 -07003472 !opt->priv->call_depth && !S_ISGITLINK(a->mode),
Elijah Newrenc641ca62017-11-14 09:31:24 -08003473 0)) {
Elijah Newren259ccb62019-04-05 08:00:13 -07003474 char *new_path = unique_path(opt, path, add_branch);
Elijah Newren9c0bbb52010-09-20 02:28:56 -06003475 clean_merge = 0;
Elijah Newren259ccb62019-04-05 08:00:13 -07003476 output(opt, 1, _("CONFLICT (%s): There is a directory with name %s in %s. "
Jiang Xin55653a62012-07-25 22:53:13 +08003477 "Adding %s as %s"),
Elijah Newren9c0bbb52010-09-20 02:28:56 -06003478 conf, path, other_branch, path, new_path);
Elijah Newren8daec1d2019-04-05 08:00:22 -07003479 if (update_file(opt, 0, contents, new_path))
Johannes Schindelin75456f92016-07-26 18:06:21 +02003480 clean_merge = -1;
Elijah Newren5bf7e572019-08-17 11:41:41 -07003481 else if (opt->priv->call_depth)
Elijah Newren259ccb62019-04-05 08:00:13 -07003482 remove_file_from_index(opt->repo->index, path);
Elijah Newren3d6b8e82011-08-11 23:19:53 -06003483 free(new_path);
Elijah Newren9c0bbb52010-09-20 02:28:56 -06003484 } else {
Elijah Newren259ccb62019-04-05 08:00:13 -07003485 output(opt, 2, _("Adding %s"), path);
Elijah Newren35a74ab2011-08-11 23:20:27 -06003486 /* do not overwrite file if already present */
Elijah Newren8daec1d2019-04-05 08:00:22 -07003487 if (update_file_flags(opt, contents, path, 1, !a_valid))
Johannes Schindelin75456f92016-07-26 18:06:21 +02003488 clean_merge = -1;
Elijah Newren9c0bbb52010-09-20 02:28:56 -06003489 }
Elijah Newren8daec1d2019-04-05 08:00:22 -07003490 } else if (a_valid && b_valid) {
3491 if (!o_valid) {
Elijah Newrendcf28152018-11-07 20:40:28 -08003492 /* Case C: Added in both (check for same permissions) */
Elijah Newren259ccb62019-04-05 08:00:13 -07003493 output(opt, 1,
Elijah Newrendcf28152018-11-07 20:40:28 -08003494 _("CONFLICT (add/add): Merge conflict in %s"),
3495 path);
Elijah Newren259ccb62019-04-05 08:00:13 -07003496 clean_merge = handle_file_collision(opt,
Elijah Newrendcf28152018-11-07 20:40:28 -08003497 path, NULL, NULL,
Elijah Newren259ccb62019-04-05 08:00:13 -07003498 opt->branch1,
3499 opt->branch2,
Elijah Newren8daec1d2019-04-05 08:00:22 -07003500 a, b);
Elijah Newrendcf28152018-11-07 20:40:28 -08003501 } else {
3502 /* case D: Modified in both, but differently. */
Elijah Newrene62d1122019-04-05 08:00:25 -07003503 struct merge_file_info mfi;
Elijah Newrendcf28152018-11-07 20:40:28 -08003504 int is_dirty = 0; /* unpack_trees would have bailed if dirty */
Elijah Newrene62d1122019-04-05 08:00:25 -07003505 clean_merge = handle_content_merge(&mfi, opt, path,
Elijah Newrendcf28152018-11-07 20:40:28 -08003506 is_dirty,
Elijah Newren8daec1d2019-04-05 08:00:22 -07003507 o, a, b, NULL);
Elijah Newrendcf28152018-11-07 20:40:28 -08003508 }
Elijah Newren8daec1d2019-04-05 08:00:22 -07003509 } else if (!o_valid && !a_valid && !b_valid) {
Elijah Newrenedd2faf2011-08-11 23:20:07 -06003510 /*
3511 * this entry was deleted altogether. a_mode == 0 means
3512 * we had that path and want to actively remove it.
3513 */
Elijah Newren8daec1d2019-04-05 08:00:22 -07003514 remove_file(opt, 1, path, !a->mode);
Elijah Newrenedd2faf2011-08-11 23:20:07 -06003515 } else
Johannes Schindelin033abf92018-05-02 11:38:39 +02003516 BUG("fatal merge failure, shouldn't happen.");
Elijah Newren37348932010-07-09 07:10:53 -06003517
3518 return clean_merge;
3519}
3520
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003521static int merge_trees_internal(struct merge_options *opt,
3522 struct tree *head,
3523 struct tree *merge,
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003524 struct tree *merge_base,
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003525 struct tree **result)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003526{
Elijah Newren259ccb62019-04-05 08:00:13 -07003527 struct index_state *istate = opt->repo->index;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003528 int code, clean;
3529
Elijah Newren259ccb62019-04-05 08:00:13 -07003530 if (opt->subtree_shift) {
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003531 merge = shift_tree_object(opt->repo, head, merge,
3532 opt->subtree_shift);
3533 merge_base = shift_tree_object(opt->repo, head, merge_base,
3534 opt->subtree_shift);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003535 }
3536
Elijah Newren763a59e2020-01-01 05:20:57 +00003537 if (oideq(&merge_base->object.oid, &merge->object.oid)) {
Eric Sunshine80cde952021-05-02 01:14:22 -04003538 output(opt, 0, _("Already up to date."));
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003539 *result = head;
3540 return 1;
3541 }
3542
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003543 code = unpack_trees_start(opt, merge_base, head, merge);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003544
Junio C Hamanofadd0692009-09-07 22:43:11 -07003545 if (code != 0) {
Elijah Newren5bf7e572019-08-17 11:41:41 -07003546 if (show(opt, 4) || opt->priv->call_depth)
Elijah Newren259ccb62019-04-05 08:00:13 -07003547 err(opt, _("merging of trees %s and %s failed"),
brian m. carlsonf2fd0762015-11-10 02:22:28 +00003548 oid_to_hex(&head->object.oid),
3549 oid_to_hex(&merge->object.oid));
Elijah Newren259ccb62019-04-05 08:00:13 -07003550 unpack_trees_finish(opt);
Johannes Schindelin60033032016-07-26 18:06:26 +02003551 return -1;
Junio C Hamanofadd0692009-09-07 22:43:11 -07003552 }
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003553
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +07003554 if (unmerged_index(istate)) {
Elijah Newrenf1725892018-04-19 10:58:00 -07003555 struct string_list *entries;
3556 struct rename_info re_info;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003557 int i;
Kevin Willfordfc65b002017-09-07 10:25:56 -06003558 /*
3559 * Only need the hashmap while processing entries, so
3560 * initialize it here and free it when we are done running
3561 * through the entries. Keeping it in the merge_options as
3562 * opposed to decaring a local hashmap is for convenience
3563 * so that we don't have to pass it to around.
3564 */
Elijah Newren5bf7e572019-08-17 11:41:41 -07003565 hashmap_init(&opt->priv->current_file_dir_set, path_hashmap_cmp,
Elijah Newren4d7101e2019-08-17 11:41:33 -07003566 NULL, 512);
Elijah Newren259ccb62019-04-05 08:00:13 -07003567 get_files_dirs(opt, head);
3568 get_files_dirs(opt, merge);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003569
Elijah Newren259ccb62019-04-05 08:00:13 -07003570 entries = get_unmerged(opt->repo->index);
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003571 clean = detect_and_process_renames(opt, merge_base, head, merge,
Elijah Newren8ebe7b02018-06-09 21:16:15 -07003572 entries, &re_info);
Elijah Newren259ccb62019-04-05 08:00:13 -07003573 record_df_conflict_files(opt, entries);
Johannes Schindelin75456f92016-07-26 18:06:21 +02003574 if (clean < 0)
Kevin Willforde336bdc2017-08-28 14:28:27 -06003575 goto cleanup;
Elijah Newrenedd2faf2011-08-11 23:20:07 -06003576 for (i = entries->nr-1; 0 <= i; i--) {
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003577 const char *path = entries->items[i].string;
3578 struct stage_data *e = entries->items[i].util;
Johannes Schindelin75456f92016-07-26 18:06:21 +02003579 if (!e->processed) {
Elijah Newren259ccb62019-04-05 08:00:13 -07003580 int ret = process_entry(opt, path, e);
Johannes Schindelin75456f92016-07-26 18:06:21 +02003581 if (!ret)
3582 clean = 0;
Kevin Willforde336bdc2017-08-28 14:28:27 -06003583 else if (ret < 0) {
3584 clean = ret;
3585 goto cleanup;
3586 }
Johannes Schindelin75456f92016-07-26 18:06:21 +02003587 }
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003588 }
Elijah Newren37348932010-07-09 07:10:53 -06003589 for (i = 0; i < entries->nr; i++) {
Junio C Hamano7edba4c2010-09-20 02:28:35 -06003590 struct stage_data *e = entries->items[i].util;
3591 if (!e->processed)
Johannes Schindelin033abf92018-05-02 11:38:39 +02003592 BUG("unprocessed path??? %s",
Junio C Hamano7edba4c2010-09-20 02:28:35 -06003593 entries->items[i].string);
3594 }
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003595
Elijah Newren93665362018-06-09 21:16:13 -07003596 cleanup:
Elijah Newrenffc16c42018-04-19 10:58:04 -07003597 final_cleanup_renames(&re_info);
Elijah Newrenf1725892018-04-19 10:58:00 -07003598
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003599 string_list_clear(entries, 1);
Elijah Newrenf1725892018-04-19 10:58:00 -07003600 free(entries);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003601
Elijah Newren6da1a252020-11-02 18:55:05 +00003602 hashmap_clear_and_free(&opt->priv->current_file_dir_set,
Eric Wongc8e424c2019-10-06 23:30:40 +00003603 struct path_hashmap_entry, e);
Kevin Willfordfc65b002017-09-07 10:25:56 -06003604
Elijah Newren3f1c1c32018-05-20 12:17:35 +02003605 if (clean < 0) {
Elijah Newren259ccb62019-04-05 08:00:13 -07003606 unpack_trees_finish(opt);
Kevin Willforde336bdc2017-08-28 14:28:27 -06003607 return clean;
Elijah Newren3f1c1c32018-05-20 12:17:35 +02003608 }
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003609 }
3610 else
3611 clean = 1;
3612
Elijah Newren259ccb62019-04-05 08:00:13 -07003613 unpack_trees_finish(opt);
Elijah Newrena35edc82018-04-19 10:58:20 -07003614
Elijah Newren5bf7e572019-08-17 11:41:41 -07003615 if (opt->priv->call_depth &&
Elijah Newren724dd762019-08-17 11:41:32 -07003616 !(*result = write_in_core_index_as_tree(opt->repo)))
Johannes Schindelinfbc87eb2016-07-26 18:06:17 +02003617 return -1;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003618
3619 return clean;
3620}
3621
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003622/*
Elijah Newren56e74342020-08-02 03:14:27 +00003623 * Merge the commits h1 and h2, returning a flag (int) indicating the
3624 * cleanness of the merge. Also, if opt->priv->call_depth, create a
3625 * virtual commit and write its location to *result.
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003626 */
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003627static int merge_recursive_internal(struct merge_options *opt,
3628 struct commit *h1,
3629 struct commit *h2,
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003630 struct commit_list *merge_bases,
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003631 struct commit **result)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003632{
3633 struct commit_list *iter;
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003634 struct commit *merged_merge_bases;
Elijah Newrenbab56872019-08-17 11:41:35 -07003635 struct tree *result_tree;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003636 int clean;
Elijah Newren743474c2019-08-17 11:41:24 -07003637 const char *ancestor_name;
3638 struct strbuf merge_base_abbrev = STRBUF_INIT;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003639
Elijah Newren259ccb62019-04-05 08:00:13 -07003640 if (show(opt, 4)) {
3641 output(opt, 4, _("Merging:"));
3642 output_commit_title(opt, h1);
3643 output_commit_title(opt, h2);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003644 }
3645
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003646 if (!merge_bases) {
Johannes Schindelin76e2a092024-02-28 09:44:14 +00003647 if (repo_get_merge_bases(the_repository, h1, h2,
3648 &merge_bases) < 0)
3649 return -1;
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003650 merge_bases = reverse_commit_list(merge_bases);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003651 }
3652
Elijah Newren259ccb62019-04-05 08:00:13 -07003653 if (show(opt, 5)) {
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003654 unsigned cnt = commit_list_count(merge_bases);
Ralf Thielowe0453cd2012-08-05 19:56:38 +02003655
Elijah Newren259ccb62019-04-05 08:00:13 -07003656 output(opt, 5, Q_("found %u common ancestor:",
Ralf Thielowe0453cd2012-08-05 19:56:38 +02003657 "found %u common ancestors:", cnt), cnt);
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003658 for (iter = merge_bases; iter; iter = iter->next)
Elijah Newren259ccb62019-04-05 08:00:13 -07003659 output_commit_title(opt, iter->item);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003660 }
3661
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003662 merged_merge_bases = pop_commit(&merge_bases);
Junio C Hamanoafe8a902022-05-02 09:50:37 -07003663 if (!merged_merge_bases) {
Jonathan Nieder03f622c2011-08-16 13:27:39 -05003664 /* if there is no common ancestor, use an empty tree */
3665 struct tree *tree;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003666
Elijah Newren259ccb62019-04-05 08:00:13 -07003667 tree = lookup_tree(opt->repo, opt->repo->hash_algo->empty_tree);
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003668 merged_merge_bases = make_virtual_commit(opt->repo, tree,
3669 "ancestor");
Elijah Newren743474c2019-08-17 11:41:24 -07003670 ancestor_name = "empty tree";
Elijah Newrenb6570472019-10-07 08:52:11 -07003671 } else if (opt->ancestor && !opt->priv->call_depth) {
Elijah Newren8e4ec332019-10-01 11:17:27 -07003672 ancestor_name = opt->ancestor;
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003673 } else if (merge_bases) {
Elijah Newren743474c2019-08-17 11:41:24 -07003674 ancestor_name = "merged common ancestors";
3675 } else {
3676 strbuf_add_unique_abbrev(&merge_base_abbrev,
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003677 &merged_merge_bases->object.oid,
Elijah Newren743474c2019-08-17 11:41:24 -07003678 DEFAULT_ABBREV);
3679 ancestor_name = merge_base_abbrev.buf;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003680 }
3681
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003682 for (iter = merge_bases; iter; iter = iter->next) {
Miklos Vajna8a2fce12008-08-25 16:25:57 +02003683 const char *saved_b1, *saved_b2;
Elijah Newren5bf7e572019-08-17 11:41:41 -07003684 opt->priv->call_depth++;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003685 /*
3686 * When the merge fails, the result contains files
3687 * with conflict markers. The cleanness flag is
Johannes Schindelinde8946d2016-07-26 18:06:07 +02003688 * ignored (unless indicating an error), it was never
3689 * actually used, as result of merge_trees has always
3690 * overwritten it: the committed "conflicts" were
3691 * already resolved.
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003692 */
Elijah Newren259ccb62019-04-05 08:00:13 -07003693 discard_index(opt->repo->index);
3694 saved_b1 = opt->branch1;
3695 saved_b2 = opt->branch2;
3696 opt->branch1 = "Temporary merge branch 1";
3697 opt->branch2 = "Temporary merge branch 2";
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003698 if (merge_recursive_internal(opt, merged_merge_bases, iter->item,
3699 NULL, &merged_merge_bases) < 0)
Johannes Schindelinde8946d2016-07-26 18:06:07 +02003700 return -1;
Elijah Newren259ccb62019-04-05 08:00:13 -07003701 opt->branch1 = saved_b1;
3702 opt->branch2 = saved_b2;
Elijah Newren5bf7e572019-08-17 11:41:41 -07003703 opt->priv->call_depth--;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003704
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003705 if (!merged_merge_bases)
Elijah Newren259ccb62019-04-05 08:00:13 -07003706 return err(opt, _("merge returned no commit"));
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003707 }
3708
Elijah Newren816147e2021-03-20 00:03:53 +00003709 /*
3710 * FIXME: Since merge_recursive_internal() is only ever called by
3711 * places that ensure the index is loaded first
3712 * (e.g. builtin/merge.c, rebase/sequencer, etc.), in the common
3713 * case where the merge base was unique that means when we get here
3714 * we immediately discard the index and re-read it, which is a
3715 * complete waste of time. We should only be discarding and
3716 * re-reading if we were forced to recurse.
3717 */
Elijah Newren259ccb62019-04-05 08:00:13 -07003718 discard_index(opt->repo->index);
Elijah Newren5bf7e572019-08-17 11:41:41 -07003719 if (!opt->priv->call_depth)
Elijah Newren259ccb62019-04-05 08:00:13 -07003720 repo_read_index(opt->repo);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003721
Elijah Newren743474c2019-08-17 11:41:24 -07003722 opt->ancestor = ancestor_name;
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003723 clean = merge_trees_internal(opt,
3724 repo_get_commit_tree(opt->repo, h1),
3725 repo_get_commit_tree(opt->repo, h2),
3726 repo_get_commit_tree(opt->repo,
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003727 merged_merge_bases),
Elijah Newrenbab56872019-08-17 11:41:35 -07003728 &result_tree);
Elijah Newren743474c2019-08-17 11:41:24 -07003729 strbuf_release(&merge_base_abbrev);
Elijah Newrenb6570472019-10-07 08:52:11 -07003730 opt->ancestor = NULL; /* avoid accidental re-use of opt->ancestor */
Johannes Schindelin6999bc72016-08-01 13:44:57 +02003731 if (clean < 0) {
Elijah Newren259ccb62019-04-05 08:00:13 -07003732 flush_output(opt);
Johannes Schindelinde8946d2016-07-26 18:06:07 +02003733 return clean;
Johannes Schindelin6999bc72016-08-01 13:44:57 +02003734 }
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003735
Elijah Newren5bf7e572019-08-17 11:41:41 -07003736 if (opt->priv->call_depth) {
Elijah Newrenbab56872019-08-17 11:41:35 -07003737 *result = make_virtual_commit(opt->repo, result_tree,
3738 "merged tree");
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003739 commit_list_insert(h1, &(*result)->parents);
3740 commit_list_insert(h2, &(*result)->parents->next);
3741 }
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003742 return clean;
3743}
3744
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003745static int merge_start(struct merge_options *opt, struct tree *head)
3746{
3747 struct strbuf sb = STRBUF_INIT;
3748
Elijah Newren45ef16f2019-08-17 11:41:43 -07003749 /* Sanity checks on opt */
3750 assert(opt->repo);
3751
3752 assert(opt->branch1 && opt->branch2);
3753
3754 assert(opt->detect_renames >= -1 &&
3755 opt->detect_renames <= DIFF_DETECT_COPY);
3756 assert(opt->detect_directory_renames >= MERGE_DIRECTORY_RENAMES_NONE &&
3757 opt->detect_directory_renames <= MERGE_DIRECTORY_RENAMES_TRUE);
3758 assert(opt->rename_limit >= -1);
3759 assert(opt->rename_score >= 0 && opt->rename_score <= MAX_SCORE);
3760 assert(opt->show_rename_progress >= 0 && opt->show_rename_progress <= 1);
3761
3762 assert(opt->xdl_opts >= 0);
3763 assert(opt->recursive_variant >= MERGE_VARIANT_NORMAL &&
3764 opt->recursive_variant <= MERGE_VARIANT_THEIRS);
3765
3766 assert(opt->verbosity >= 0 && opt->verbosity <= 5);
3767 assert(opt->buffer_output <= 2);
3768 assert(opt->obuf.len == 0);
3769
3770 assert(opt->priv == NULL);
3771
Elijah Newren6054d1a2022-02-02 02:37:33 +00003772 /* Not supported; option specific to merge-ort */
3773 assert(!opt->record_conflict_msgs_as_headers);
3774 assert(!opt->msg_header_prefix);
3775
Elijah Newren45ef16f2019-08-17 11:41:43 -07003776 /* Sanity check on repo state; index must match head */
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003777 if (repo_index_has_changes(opt->repo, head, &sb)) {
3778 err(opt, _("Your local changes to the following files would be overwritten by merge:\n %s"),
3779 sb.buf);
3780 strbuf_release(&sb);
3781 return -1;
3782 }
3783
René Scharfeca56dad2021-03-13 17:17:22 +01003784 CALLOC_ARRAY(opt->priv, 1);
Ævar Arnfjörð Bjarmasonbc40dfb2021-07-01 12:51:29 +02003785 string_list_init_dup(&opt->priv->df_conflict_file_set);
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003786 return 0;
3787}
3788
3789static void merge_finalize(struct merge_options *opt)
3790{
Elijah Newrene95e4812019-08-17 11:41:40 -07003791 flush_output(opt);
Elijah Newren5bf7e572019-08-17 11:41:41 -07003792 if (!opt->priv->call_depth && opt->buffer_output < 2)
Elijah Newrene95e4812019-08-17 11:41:40 -07003793 strbuf_release(&opt->obuf);
Elijah Newren345480d2019-08-17 11:41:31 -07003794 if (show(opt, 2))
3795 diff_warn_rename_limit("merge.renamelimit",
Elijah Newren5bf7e572019-08-17 11:41:41 -07003796 opt->priv->needed_rename_limit, 0);
3797 FREE_AND_NULL(opt->priv);
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003798}
3799
3800int merge_trees(struct merge_options *opt,
3801 struct tree *head,
3802 struct tree *merge,
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003803 struct tree *merge_base)
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003804{
3805 int clean;
Elijah Newrenb4db8a22019-08-17 11:41:30 -07003806 struct tree *ignored;
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003807
3808 assert(opt->ancestor != NULL);
3809
3810 if (merge_start(opt, head))
3811 return -1;
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003812 clean = merge_trees_internal(opt, head, merge, merge_base, &ignored);
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003813 merge_finalize(opt);
3814
3815 return clean;
3816}
3817
3818int merge_recursive(struct merge_options *opt,
3819 struct commit *h1,
3820 struct commit *h2,
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003821 struct commit_list *merge_bases,
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003822 struct commit **result)
3823{
3824 int clean;
3825
Elijah Newren8e4ec332019-10-01 11:17:27 -07003826 assert(opt->ancestor == NULL ||
3827 !strcmp(opt->ancestor, "constructed merge base"));
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003828
Derrick Stoleea3380632021-09-08 11:23:57 +00003829 prepare_repo_settings(opt->repo);
3830 opt->repo->settings.command_requires_full_index = 1;
3831
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003832 if (merge_start(opt, repo_get_commit_tree(opt->repo, h1)))
3833 return -1;
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003834 clean = merge_recursive_internal(opt, h1, h2, merge_bases, result);
Elijah Newren98a1d3d2019-08-17 11:41:29 -07003835 merge_finalize(opt);
3836
3837 return clean;
3838}
3839
Elijah Newren4d7101e2019-08-17 11:41:33 -07003840static struct commit *get_ref(struct repository *repo,
3841 const struct object_id *oid,
Nguyễn Thái Ngọc Duyd7cf3a92019-01-12 09:13:30 +07003842 const char *name)
Stephan Beyer73118f82008-08-12 22:13:59 +02003843{
3844 struct object *object;
3845
Nguyễn Thái Ngọc Duyd7cf3a92019-01-12 09:13:30 +07003846 object = deref_tag(repo, parse_object(repo, oid),
3847 name, strlen(name));
Stephan Beyer73118f82008-08-12 22:13:59 +02003848 if (!object)
3849 return NULL;
3850 if (object->type == OBJ_TREE)
Nguyễn Thái Ngọc Duyd7cf3a92019-01-12 09:13:30 +07003851 return make_virtual_commit(repo, (struct tree*)object, name);
Stephan Beyer73118f82008-08-12 22:13:59 +02003852 if (object->type != OBJ_COMMIT)
3853 return NULL;
Ævar Arnfjörð Bjarmason4a93b892023-03-28 15:58:58 +02003854 if (repo_parse_commit(repo, (struct commit *)object))
Stephan Beyer73118f82008-08-12 22:13:59 +02003855 return NULL;
3856 return (struct commit *)object;
3857}
3858
Elijah Newren259ccb62019-04-05 08:00:13 -07003859int merge_recursive_generic(struct merge_options *opt,
brian m. carlson4e8161a2016-06-24 23:09:28 +00003860 const struct object_id *head,
3861 const struct object_id *merge,
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003862 int num_merge_bases,
3863 const struct object_id **merge_bases,
Miklos Vajna8a2fce12008-08-25 16:25:57 +02003864 struct commit **result)
Stephan Beyer73118f82008-08-12 22:13:59 +02003865{
Nguyễn Thái Ngọc Duy03b86642014-06-13 19:19:23 +07003866 int clean;
Martin Ågren837e34e2017-10-05 22:32:04 +02003867 struct lock_file lock = LOCK_INIT;
Elijah Newren259ccb62019-04-05 08:00:13 -07003868 struct commit *head_commit = get_ref(opt->repo, head, opt->branch1);
3869 struct commit *next_commit = get_ref(opt->repo, merge, opt->branch2);
Stephan Beyer73118f82008-08-12 22:13:59 +02003870 struct commit_list *ca = NULL;
3871
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003872 if (merge_bases) {
Stephan Beyer73118f82008-08-12 22:13:59 +02003873 int i;
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003874 for (i = 0; i < num_merge_bases; ++i) {
Stephan Beyer73118f82008-08-12 22:13:59 +02003875 struct commit *base;
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003876 if (!(base = get_ref(opt->repo, merge_bases[i],
3877 oid_to_hex(merge_bases[i]))))
Elijah Newren259ccb62019-04-05 08:00:13 -07003878 return err(opt, _("Could not parse object '%s'"),
Elijah Newrenff1bfa22019-08-17 11:41:34 -07003879 oid_to_hex(merge_bases[i]));
Stephan Beyer73118f82008-08-12 22:13:59 +02003880 commit_list_insert(base, &ca);
3881 }
Elijah Newren8e4ec332019-10-01 11:17:27 -07003882 if (num_merge_bases == 1)
3883 opt->ancestor = "constructed merge base";
Stephan Beyer73118f82008-08-12 22:13:59 +02003884 }
3885
Elijah Newren259ccb62019-04-05 08:00:13 -07003886 repo_hold_locked_index(opt->repo, &lock, LOCK_DIE_ON_ERROR);
3887 clean = merge_recursive(opt, head_commit, next_commit, ca,
Elijah Newrend90e7592018-06-09 21:16:12 -07003888 result);
Martin Ågren51d3f432018-02-28 20:07:56 +01003889 if (clean < 0) {
3890 rollback_lock_file(&lock);
Johannes Schindelinde8946d2016-07-26 18:06:07 +02003891 return clean;
Martin Ågren51d3f432018-02-28 20:07:56 +01003892 }
Johannes Schindelinde8946d2016-07-26 18:06:07 +02003893
Elijah Newren259ccb62019-04-05 08:00:13 -07003894 if (write_locked_index(opt->repo->index, &lock,
Martin Ågren61000812018-03-01 21:40:20 +01003895 COMMIT_LOCK | SKIP_IF_UNCHANGED))
Elijah Newren259ccb62019-04-05 08:00:13 -07003896 return err(opt, _("Unable to write index."));
Stephan Beyer73118f82008-08-12 22:13:59 +02003897
3898 return clean ? 0 : 1;
3899}
3900
Elijah Newren259ccb62019-04-05 08:00:13 -07003901static void merge_recursive_config(struct merge_options *opt)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003902{
Ben Peart85b46032018-05-02 16:01:14 +00003903 char *value = NULL;
Elijah Newren8d552252020-08-03 18:41:19 +00003904 int renormalize = 0;
Elijah Newren259ccb62019-04-05 08:00:13 -07003905 git_config_get_int("merge.verbosity", &opt->verbosity);
Elijah Newren8599ab42019-08-17 11:41:38 -07003906 git_config_get_int("diff.renamelimit", &opt->rename_limit);
3907 git_config_get_int("merge.renamelimit", &opt->rename_limit);
Elijah Newren8d552252020-08-03 18:41:19 +00003908 git_config_get_bool("merge.renormalize", &renormalize);
3909 opt->renormalize = renormalize;
Ben Peart85b46032018-05-02 16:01:14 +00003910 if (!git_config_get_string("diff.renames", &value)) {
Elijah Newren8599ab42019-08-17 11:41:38 -07003911 opt->detect_renames = git_config_rename("diff.renames", value);
Ben Peart85b46032018-05-02 16:01:14 +00003912 free(value);
3913 }
3914 if (!git_config_get_string("merge.renames", &value)) {
Elijah Newren8599ab42019-08-17 11:41:38 -07003915 opt->detect_renames = git_config_rename("merge.renames", value);
Ben Peart85b46032018-05-02 16:01:14 +00003916 free(value);
3917 }
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07003918 if (!git_config_get_string("merge.directoryrenames", &value)) {
3919 int boolval = git_parse_maybe_bool(value);
3920 if (0 <= boolval) {
Derrick Stolee8e012512019-08-17 11:41:25 -07003921 opt->detect_directory_renames = boolval ?
3922 MERGE_DIRECTORY_RENAMES_TRUE :
3923 MERGE_DIRECTORY_RENAMES_NONE;
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07003924 } else if (!strcasecmp(value, "conflict")) {
Derrick Stolee8e012512019-08-17 11:41:25 -07003925 opt->detect_directory_renames =
3926 MERGE_DIRECTORY_RENAMES_CONFLICT;
Elijah Newren8c8e5bd2019-04-05 08:00:26 -07003927 } /* avoid erroring on values from future versions of git */
Tanay Abhra0e7bcb12014-08-13 01:22:01 -07003928 free(value);
3929 }
3930 git_config(git_xmerge_config, NULL);
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003931}
3932
Elijah Newren259ccb62019-04-05 08:00:13 -07003933void init_merge_options(struct merge_options *opt,
Nguyễn Thái Ngọc Duy0d6caa22019-01-12 09:13:29 +07003934 struct repository *repo)
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003935{
Andrey Okoshkin80486222017-10-31 12:09:13 +03003936 const char *merge_verbosity;
Elijah Newren259ccb62019-04-05 08:00:13 -07003937 memset(opt, 0, sizeof(struct merge_options));
Elijah Newrena779fb82019-08-17 11:41:39 -07003938
Elijah Newren259ccb62019-04-05 08:00:13 -07003939 opt->repo = repo;
Elijah Newrena779fb82019-08-17 11:41:39 -07003940
Elijah Newren8599ab42019-08-17 11:41:38 -07003941 opt->detect_renames = -1;
Derrick Stolee8e012512019-08-17 11:41:25 -07003942 opt->detect_directory_renames = MERGE_DIRECTORY_RENAMES_CONFLICT;
Elijah Newrena779fb82019-08-17 11:41:39 -07003943 opt->rename_limit = -1;
3944
Elijah Newren259ccb62019-04-05 08:00:13 -07003945 opt->verbosity = 2;
3946 opt->buffer_output = 1;
Elijah Newrena779fb82019-08-17 11:41:39 -07003947 strbuf_init(&opt->obuf, 0);
3948
Elijah Newren259ccb62019-04-05 08:00:13 -07003949 opt->renormalize = 0;
Elijah Newrena779fb82019-08-17 11:41:39 -07003950
Phillip Wood135cc712024-03-14 17:05:05 +00003951 opt->conflict_style = -1;
3952
Elijah Newren259ccb62019-04-05 08:00:13 -07003953 merge_recursive_config(opt);
Andrey Okoshkin80486222017-10-31 12:09:13 +03003954 merge_verbosity = getenv("GIT_MERGE_VERBOSITY");
3955 if (merge_verbosity)
Elijah Newren259ccb62019-04-05 08:00:13 -07003956 opt->verbosity = strtol(merge_verbosity, NULL, 10);
3957 if (opt->verbosity >= 5)
3958 opt->buffer_output = 0;
Miklos Vajna9047ebb2008-08-12 18:45:14 +02003959}
Jonathan Nieder635a7bb2010-08-26 00:47:58 -05003960
Junio C Hamanob1826582023-10-11 13:37:47 -07003961/*
3962 * For now, members of merge_options do not need deep copying, but
3963 * it may change in the future, in which case we would need to update
3964 * this, and also make a matching change to clear_merge_options() to
3965 * release the resources held by a copied instance.
3966 */
3967void copy_merge_options(struct merge_options *dst, struct merge_options *src)
3968{
3969 *dst = *src;
3970}
3971
3972void clear_merge_options(struct merge_options *opt UNUSED)
3973{
3974 ; /* no-op as our copy is shallow right now */
3975}
3976
Elijah Newren259ccb62019-04-05 08:00:13 -07003977int parse_merge_opt(struct merge_options *opt, const char *s)
Jonathan Nieder635a7bb2010-08-26 00:47:58 -05003978{
Jeff King95b567c2014-06-18 15:48:29 -04003979 const char *arg;
3980
Jonathan Nieder635a7bb2010-08-26 00:47:58 -05003981 if (!s || !*s)
3982 return -1;
3983 if (!strcmp(s, "ours"))
Elijah Newrenf3081da2019-08-17 11:41:42 -07003984 opt->recursive_variant = MERGE_VARIANT_OURS;
Jonathan Nieder635a7bb2010-08-26 00:47:58 -05003985 else if (!strcmp(s, "theirs"))
Elijah Newrenf3081da2019-08-17 11:41:42 -07003986 opt->recursive_variant = MERGE_VARIANT_THEIRS;
Jonathan Nieder635a7bb2010-08-26 00:47:58 -05003987 else if (!strcmp(s, "subtree"))
Elijah Newren259ccb62019-04-05 08:00:13 -07003988 opt->subtree_shift = "";
Jeff King95b567c2014-06-18 15:48:29 -04003989 else if (skip_prefix(s, "subtree=", &arg))
Elijah Newren259ccb62019-04-05 08:00:13 -07003990 opt->subtree_shift = arg;
Justin Frankel58a1ece2010-08-26 00:50:45 -05003991 else if (!strcmp(s, "patience"))
Elijah Newren259ccb62019-04-05 08:00:13 -07003992 opt->xdl_opts = DIFF_WITH_ALG(opt, PATIENCE_DIFF);
Tay Ray Chuan8c912ee2011-07-12 14:10:25 +08003993 else if (!strcmp(s, "histogram"))
Elijah Newren259ccb62019-04-05 08:00:13 -07003994 opt->xdl_opts = DIFF_WITH_ALG(opt, HISTOGRAM_DIFF);
Jeff King95b567c2014-06-18 15:48:29 -04003995 else if (skip_prefix(s, "diff-algorithm=", &arg)) {
3996 long value = parse_algorithm_value(arg);
Michal Privoznik07924d42013-01-16 08:51:58 +01003997 if (value < 0)
3998 return -1;
3999 /* clear out previous settings */
Elijah Newren259ccb62019-04-05 08:00:13 -07004000 DIFF_XDL_CLR(opt, NEED_MINIMAL);
4001 opt->xdl_opts &= ~XDF_DIFF_ALGORITHM_MASK;
4002 opt->xdl_opts |= value;
Michal Privoznik07924d42013-01-16 08:51:58 +01004003 }
Justin Frankel4e5dd042010-08-26 00:51:47 -05004004 else if (!strcmp(s, "ignore-space-change"))
Elijah Newren259ccb62019-04-05 08:00:13 -07004005 DIFF_XDL_SET(opt, IGNORE_WHITESPACE_CHANGE);
Justin Frankel4e5dd042010-08-26 00:51:47 -05004006 else if (!strcmp(s, "ignore-all-space"))
Elijah Newren259ccb62019-04-05 08:00:13 -07004007 DIFF_XDL_SET(opt, IGNORE_WHITESPACE);
Justin Frankel4e5dd042010-08-26 00:51:47 -05004008 else if (!strcmp(s, "ignore-space-at-eol"))
Elijah Newren259ccb62019-04-05 08:00:13 -07004009 DIFF_XDL_SET(opt, IGNORE_WHITESPACE_AT_EOL);
Junio C Hamanoe9282f02017-10-26 15:32:27 +09004010 else if (!strcmp(s, "ignore-cr-at-eol"))
Elijah Newren259ccb62019-04-05 08:00:13 -07004011 DIFF_XDL_SET(opt, IGNORE_CR_AT_EOL);
Jonathan Nieder635a7bb2010-08-26 00:47:58 -05004012 else if (!strcmp(s, "renormalize"))
Elijah Newren259ccb62019-04-05 08:00:13 -07004013 opt->renormalize = 1;
Jonathan Nieder635a7bb2010-08-26 00:47:58 -05004014 else if (!strcmp(s, "no-renormalize"))
Elijah Newren259ccb62019-04-05 08:00:13 -07004015 opt->renormalize = 0;
Felipe Gonçalves Assisd2b11ec2016-02-17 01:15:25 -02004016 else if (!strcmp(s, "no-renames"))
Elijah Newren8599ab42019-08-17 11:41:38 -07004017 opt->detect_renames = 0;
Felipe Gonçalves Assis87892f62016-02-21 19:59:05 -03004018 else if (!strcmp(s, "find-renames")) {
Elijah Newren8599ab42019-08-17 11:41:38 -07004019 opt->detect_renames = 1;
Elijah Newren259ccb62019-04-05 08:00:13 -07004020 opt->rename_score = 0;
Felipe Gonçalves Assis87892f62016-02-21 19:59:05 -03004021 }
Felipe Gonçalves Assis1b47ad12016-02-17 01:15:26 -02004022 else if (skip_prefix(s, "find-renames=", &arg) ||
4023 skip_prefix(s, "rename-threshold=", &arg)) {
Elijah Newren259ccb62019-04-05 08:00:13 -07004024 if ((opt->rename_score = parse_rename_score(&arg)) == -1 || *arg != 0)
Kevin Ballard10ae7522010-09-27 16:58:25 -07004025 return -1;
Elijah Newren8599ab42019-08-17 11:41:38 -07004026 opt->detect_renames = 1;
Kevin Ballard10ae7522010-09-27 16:58:25 -07004027 }
Nguyễn Thái Ngọc Duy5a59a232019-02-16 18:24:41 +07004028 /*
4029 * Please update $__git_merge_strategy_options in
4030 * git-completion.bash when you add new options
4031 */
Jonathan Nieder635a7bb2010-08-26 00:47:58 -05004032 else
4033 return -1;
4034 return 0;
4035}