merge-recursive.[ch]: thoroughly debug these

As a wise man once told me, "Deleted code is debugged code!" So, move the functions that are shared between merge-recursive and merge-ort from the former to the latter, and then debug the remainder of merge-recursive.[ch]. Joking aside, merge-ort was always intended to replace merge-recursive. It has numerous advantages over merge-recursive (operates much faster, can operate without a worktree or index, and fixes a number of known bugs and suboptimal merges). Since we have now replaced all callers of merge-recursive with equivalent functions from merge-ort, move the shared functions from the former to the latter, and delete the remainder of merge-recursive.[ch]. Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Elijah Newren committed Apr 8, 2025 at 15:48 UTC ad45b327c0a13718ed5955319d1e735025676cc8
9 files changed +225 -4236
Documentation/merge-strategies.adoc
+6 -18
@@ -109,23 +109,11 @@ subtree[=<path>];;
109 two trees to match.
110
111 recursive::
112 - This can only resolve two heads using a 3-way merge
113 - algorithm. When there is more than one common
114 - ancestor that can be used for 3-way merge, it creates a
115 - merged tree of the common ancestors and uses that as
116 - the reference tree for the 3-way merge. This has been
117 - reported to result in fewer merge conflicts without
118 - causing mismerges by tests done on actual merge commits
119 - taken from Linux 2.6 kernel development history.
120 - Additionally this can detect and handle merges involving
121 - renames. It does not make use of detected copies. This was
122 - the default strategy for resolving two heads from Git v0.99.9k
123 - until v2.33.0.
124 -+
125 -For a path that is a submodule, the same caution as 'ort' applies to this
126 -strategy.
127 -+
128 -The 'recursive' strategy takes the same options as 'ort'.
112 + This is now a synonym for `ort`. It was an alternative
113 + implementation until v2.49.0, but was redirected to mean `ort`
114 + in v2.50.0. The previous recursive strategy was the default
115 + strategy for resolving two heads from Git v0.99.9k until
116 + v2.33.0.
117
118 resolve::
119 This can only resolve two heads (i.e. the current branch
@@ -146,7 +134,7 @@ ours::
134 ignoring all changes from all other branches. It is meant to
135 be used to supersede old development history of side
136 branches. Note that this is different from the -Xours option to
149 - the 'recursive' merge strategy.
137 + the 'ort' merge strategy.
138
139 subtree::
140 This is a modified `ort` strategy. When merging trees A and
Documentation/technical/sparse-checkout.adoc
-2
@@ -356,8 +356,6 @@ understanding these differences can be beneficial.
356 The behavior for these commands somewhat depends upon the merge
357 strategy being used:
358 * `ort` behaves as described above
359 - * `recursive` tries to not vivify files unnecessarily, but does sometimes
360 - vivify files without conflicts.
359 * `octopus` and `resolve` will always vivify any file changed in the merge
360 relative to the first parent, which is rather suboptimal.
361
Makefile
-1
@@ -1069,7 +1069,6 @@ LIB_OBJS += merge-blobs.o
1069 LIB_OBJS += merge-ll.o
1070 LIB_OBJS += merge-ort.o
1071 LIB_OBJS += merge-ort-wrappers.o
1072 -LIB_OBJS += merge-recursive.o
1072 LIB_OBJS += merge.o
1073 LIB_OBJS += midx.o
1074 LIB_OBJS += midx-write.o
merge-ort-wrappers.h
+1 -1
@@ -1,7 +1,7 @@
1 #ifndef MERGE_ORT_WRAPPERS_H
2 #define MERGE_ORT_WRAPPERS_H
3
4 -#include "merge-recursive.h"
4 +#include "merge-ort.h"
5
6 /*
7 * rename-detecting three-way merge, no recursion.
merge-ort.c
+159
@@ -26,6 +26,7 @@
26 #include "cache-tree.h"
27 #include "commit.h"
28 #include "commit-reach.h"
29 +#include "config.h"
30 #include "diff.h"
31 #include "diffcore.h"
32 #include "dir.h"
@@ -5322,3 +5323,161 @@ void merge_incore_recursive(struct merge_options *opt,
5323 merge_ort_internal(opt, merge_bases, side1, side2, result);
5324 trace2_region_leave("merge", "incore_recursive", opt->repo);
5325 }
5326 +
5327 +static void merge_recursive_config(struct merge_options *opt, int ui)
5328 +{
5329 + char *value = NULL;
5330 + int renormalize = 0;
5331 + git_config_get_int("merge.verbosity", &opt->verbosity);
5332 + git_config_get_int("diff.renamelimit", &opt->rename_limit);
5333 + git_config_get_int("merge.renamelimit", &opt->rename_limit);
5334 + git_config_get_bool("merge.renormalize", &renormalize);
5335 + opt->renormalize = renormalize;
5336 + if (!git_config_get_string("diff.renames", &value)) {
5337 + opt->detect_renames = git_config_rename("diff.renames", value);
5338 + free(value);
5339 + }
5340 + if (!git_config_get_string("merge.renames", &value)) {
5341 + opt->detect_renames = git_config_rename("merge.renames", value);
5342 + free(value);
5343 + }
5344 + if (!git_config_get_string("merge.directoryrenames", &value)) {
5345 + int boolval = git_parse_maybe_bool(value);
5346 + if (0 <= boolval) {
5347 + opt->detect_directory_renames = boolval ?
5348 + MERGE_DIRECTORY_RENAMES_TRUE :
5349 + MERGE_DIRECTORY_RENAMES_NONE;
5350 + } else if (!strcasecmp(value, "conflict")) {
5351 + opt->detect_directory_renames =
5352 + MERGE_DIRECTORY_RENAMES_CONFLICT;
5353 + } /* avoid erroring on values from future versions of git */
5354 + free(value);
5355 + }
5356 + if (ui) {
5357 + if (!git_config_get_string("diff.algorithm", &value)) {
5358 + long diff_algorithm = parse_algorithm_value(value);
5359 + if (diff_algorithm < 0)
5360 + die(_("unknown value for config '%s': %s"), "diff.algorithm", value);
5361 + opt->xdl_opts = (opt->xdl_opts & ~XDF_DIFF_ALGORITHM_MASK) | diff_algorithm;
5362 + free(value);
5363 + }
5364 + }
5365 + git_config(git_xmerge_config, NULL);
5366 +}
5367 +
5368 +static void init_merge_options(struct merge_options *opt,
5369 + struct repository *repo, int ui)
5370 +{
5371 + const char *merge_verbosity;
5372 + memset(opt, 0, sizeof(struct merge_options));
5373 +
5374 + opt->repo = repo;
5375 +
5376 + opt->detect_renames = -1;
5377 + opt->detect_directory_renames = MERGE_DIRECTORY_RENAMES_CONFLICT;
5378 + opt->rename_limit = -1;
5379 +
5380 + opt->verbosity = 2;
5381 + opt->buffer_output = 1;
5382 + strbuf_init(&opt->obuf, 0);
5383 +
5384 + opt->renormalize = 0;
5385 +
5386 + opt->conflict_style = -1;
5387 + opt->xdl_opts = DIFF_WITH_ALG(opt, HISTOGRAM_DIFF);
5388 +
5389 + merge_recursive_config(opt, ui);
5390 + merge_verbosity = getenv("GIT_MERGE_VERBOSITY");
5391 + if (merge_verbosity)
5392 + opt->verbosity = strtol(merge_verbosity, NULL, 10);
5393 + if (opt->verbosity >= 5)
5394 + opt->buffer_output = 0;
5395 +}
5396 +
5397 +void init_ui_merge_options(struct merge_options *opt,
5398 + struct repository *repo)
5399 +{
5400 + init_merge_options(opt, repo, 1);
5401 +}
5402 +
5403 +void init_basic_merge_options(struct merge_options *opt,
5404 + struct repository *repo)
5405 +{
5406 + init_merge_options(opt, repo, 0);
5407 +}
5408 +
5409 +/*
5410 + * For now, members of merge_options do not need deep copying, but
5411 + * it may change in the future, in which case we would need to update
5412 + * this, and also make a matching change to clear_merge_options() to
5413 + * release the resources held by a copied instance.
5414 + */
5415 +void copy_merge_options(struct merge_options *dst, struct merge_options *src)
5416 +{
5417 + *dst = *src;
5418 +}
5419 +
5420 +void clear_merge_options(struct merge_options *opt UNUSED)
5421 +{
5422 + ; /* no-op as our copy is shallow right now */
5423 +}
5424 +
5425 +int parse_merge_opt(struct merge_options *opt, const char *s)
5426 +{
5427 + const char *arg;
5428 +
5429 + if (!s || !*s)
5430 + return -1;
5431 + if (!strcmp(s, "ours"))
5432 + opt->recursive_variant = MERGE_VARIANT_OURS;
5433 + else if (!strcmp(s, "theirs"))
5434 + opt->recursive_variant = MERGE_VARIANT_THEIRS;
5435 + else if (!strcmp(s, "subtree"))
5436 + opt->subtree_shift = "";
5437 + else if (skip_prefix(s, "subtree=", &arg))
5438 + opt->subtree_shift = arg;
5439 + else if (!strcmp(s, "patience"))
5440 + opt->xdl_opts = DIFF_WITH_ALG(opt, PATIENCE_DIFF);
5441 + else if (!strcmp(s, "histogram"))
5442 + opt->xdl_opts = DIFF_WITH_ALG(opt, HISTOGRAM_DIFF);
5443 + else if (skip_prefix(s, "diff-algorithm=", &arg)) {
5444 + long value = parse_algorithm_value(arg);
5445 + if (value < 0)
5446 + return -1;
5447 + /* clear out previous settings */
5448 + DIFF_XDL_CLR(opt, NEED_MINIMAL);
5449 + opt->xdl_opts &= ~XDF_DIFF_ALGORITHM_MASK;
5450 + opt->xdl_opts |= value;
5451 + }
5452 + else if (!strcmp(s, "ignore-space-change"))
5453 + DIFF_XDL_SET(opt, IGNORE_WHITESPACE_CHANGE);
5454 + else if (!strcmp(s, "ignore-all-space"))
5455 + DIFF_XDL_SET(opt, IGNORE_WHITESPACE);
5456 + else if (!strcmp(s, "ignore-space-at-eol"))
5457 + DIFF_XDL_SET(opt, IGNORE_WHITESPACE_AT_EOL);
5458 + else if (!strcmp(s, "ignore-cr-at-eol"))
5459 + DIFF_XDL_SET(opt, IGNORE_CR_AT_EOL);
5460 + else if (!strcmp(s, "renormalize"))
5461 + opt->renormalize = 1;
5462 + else if (!strcmp(s, "no-renormalize"))
5463 + opt->renormalize = 0;
5464 + else if (!strcmp(s, "no-renames"))
5465 + opt->detect_renames = 0;
5466 + else if (!strcmp(s, "find-renames")) {
5467 + opt->detect_renames = 1;
5468 + opt->rename_score = 0;
5469 + }
5470 + else if (skip_prefix(s, "find-renames=", &arg) ||
5471 + skip_prefix(s, "rename-threshold=", &arg)) {
5472 + if ((opt->rename_score = parse_rename_score(&arg)) == -1 || *arg != 0)
5473 + return -1;
5474 + opt->detect_renames = 1;
5475 + }
5476 + /*
5477 + * Please update $__git_merge_strategy_options in
5478 + * git-completion.bash when you add new options
5479 + */
5480 + else
5481 + return -1;
5482 + return 0;
5483 +}
merge-ort.h
+59 -1
@@ -1,10 +1,11 @@
1 #ifndef MERGE_ORT_H
2 #define MERGE_ORT_H
3
4 -#include "merge-recursive.h"
4 #include "hash.h"
5 +#include "strbuf.h"
6
7 struct commit;
8 +struct commit_list;
9 struct tree;
10 struct strmap;
11
@@ -44,6 +45,51 @@ struct merge_result {
45 unsigned _properly_initialized;
46 };
47
48 +struct merge_options_internal;
49 +struct merge_options {
50 + struct repository *repo;
51 +
52 + /* ref names used in console messages and conflict markers */
53 + const char *ancestor;
54 + const char *branch1;
55 + const char *branch2;
56 +
57 + /* rename related options */
58 + int detect_renames;
59 + enum {
60 + MERGE_DIRECTORY_RENAMES_NONE = 0,
61 + MERGE_DIRECTORY_RENAMES_CONFLICT = 1,
62 + MERGE_DIRECTORY_RENAMES_TRUE = 2
63 + } detect_directory_renames;
64 + int rename_limit;
65 + int rename_score;
66 + int show_rename_progress;
67 +
68 + /* xdiff-related options (patience, ignore whitespace, ours/theirs) */
69 + long xdl_opts;
70 + int conflict_style;
71 + enum {
72 + MERGE_VARIANT_NORMAL = 0,
73 + MERGE_VARIANT_OURS,
74 + MERGE_VARIANT_THEIRS
75 + } recursive_variant;
76 +
77 + /* console output related options */
78 + int verbosity;
79 + unsigned buffer_output; /* 1: output at end, 2: keep buffered */
80 + struct strbuf obuf; /* output buffer; if buffer_output == 2, caller
81 + * must handle and call strbuf_release */
82 +
83 + /* miscellaneous control options */
84 + const char *subtree_shift;
85 + unsigned renormalize : 1;
86 + unsigned record_conflict_msgs_as_headers : 1;
87 + const char *msg_header_prefix;
88 +
89 + /* internal fields used by the implementation */
90 + struct merge_options_internal *priv;
91 +};
92 +
93 /* Mostly internal function also used by merge-ort-wrappers.c */
94 struct commit *make_virtual_commit(struct repository *repo,
95 struct tree *tree,
@@ -119,4 +165,16 @@ void merge_get_conflicted_files(struct merge_result *result,
165 void merge_finalize(struct merge_options *opt,
166 struct merge_result *result);
167
168 +
169 +/* for use by porcelain commands */
170 +void init_ui_merge_options(struct merge_options *opt, struct repository *repo);
171 +/* for use by plumbing commands */
172 +void init_basic_merge_options(struct merge_options *opt, struct repository *repo);
173 +
174 +void copy_merge_options(struct merge_options *dst, struct merge_options *src);
175 +void clear_merge_options(struct merge_options *opt);
176 +
177 +/* parse the option in s and update the relevant field of opt */
178 +int parse_merge_opt(struct merge_options *opt, const char *s);
179 +
180 #endif
merge-recursive.c deleted
-4080
@@ -1,4080 +0,0 @@
1 -/*
2 - * Recursive Merge algorithm stolen from git-merge-recursive.py by
3 - * Fredrik Kuivinen.
4 - * The thieves were Alex Riesen and Johannes Schindelin, in June/July 2006
5 - */
6 -
7 -#define USE_THE_REPOSITORY_VARIABLE
8 -#define DISABLE_SIGN_COMPARE_WARNINGS
9 -
10 -#include "git-compat-util.h"
11 -#include "merge-recursive.h"
12 -
13 -#include "alloc.h"
14 -#include "cache-tree.h"
15 -#include "commit.h"
16 -#include "commit-reach.h"
17 -#include "config.h"
18 -#include "diff.h"
19 -#include "diffcore.h"
20 -#include "dir.h"
21 -#include "environment.h"
22 -#include "gettext.h"
23 -#include "hex.h"
24 -#include "merge-ll.h"
25 -#include "lockfile.h"
26 -#include "match-trees.h"
27 -#include "name-hash.h"
28 -#include "object-file.h"
29 -#include "object-name.h"
30 -#include "object-store-ll.h"
31 -#include "path.h"
32 -#include "repository.h"
33 -#include "revision.h"
34 -#include "sparse-index.h"
35 -#include "string-list.h"
36 -#include "symlinks.h"
37 -#include "tag.h"
38 -#include "tree-walk.h"
39 -#include "unpack-trees.h"
40 -#include "xdiff-interface.h"
41 -
42 -struct merge_options_internal {
43 - int call_depth;
44 - int needed_rename_limit;
45 - struct hashmap current_file_dir_set;
46 - struct string_list df_conflict_file_set;
47 - struct unpack_trees_options unpack_opts;
48 - struct index_state orig_index;
49 -};
50 -
51 -struct path_hashmap_entry {
52 - struct hashmap_entry e;
53 - char path[FLEX_ARRAY];
54 -};
55 -
56 -static int path_hashmap_cmp(const void *cmp_data UNUSED,
57 - const struct hashmap_entry *eptr,
58 - const struct hashmap_entry *entry_or_key,
59 - const void *keydata)
60 -{
61 - const struct path_hashmap_entry *a, *b;
62 - const char *key = keydata;
63 -
64 - a = container_of(eptr, const struct path_hashmap_entry, e);
65 - b = container_of(entry_or_key, const struct path_hashmap_entry, e);
66 -
67 - return fspathcmp(a->path, key ? key : b->path);
68 -}
69 -
70 -/*
71 - * For dir_rename_entry, directory names are stored as a full path from the
72 - * toplevel of the repository and do not include a trailing '/'. Also:
73 - *
74 - * dir: original name of directory being renamed
75 - * non_unique_new_dir: if true, could not determine new_dir
76 - * new_dir: final name of directory being renamed
77 - * possible_new_dirs: temporary used to help determine new_dir; see comments
78 - * in get_directory_renames() for details
79 - */
80 -struct dir_rename_entry {
81 - struct hashmap_entry ent;
82 - char *dir;
83 - unsigned non_unique_new_dir:1;
84 - struct strbuf new_dir;
85 - struct string_list possible_new_dirs;
86 -};
87 -
88 -static struct dir_rename_entry *dir_rename_find_entry(struct hashmap *hashmap,
89 - char *dir)
90 -{
91 - struct dir_rename_entry key;
92 -
93 - if (!dir)
94 - return NULL;
95 - hashmap_entry_init(&key.ent, strhash(dir));
96 - key.dir = dir;
97 - return hashmap_get_entry(hashmap, &key, ent, NULL);
98 -}
99 -
100 -static int dir_rename_cmp(const void *cmp_data UNUSED,
101 - const struct hashmap_entry *eptr,
102 - const struct hashmap_entry *entry_or_key,
103 - const void *keydata UNUSED)
104 -{
105 - const struct dir_rename_entry *e1, *e2;
106 -
107 - e1 = container_of(eptr, const struct dir_rename_entry, ent);
108 - e2 = container_of(entry_or_key, const struct dir_rename_entry, ent);
109 -
110 - return strcmp(e1->dir, e2->dir);
111 -}
112 -
113 -static void dir_rename_init(struct hashmap *map)
114 -{
115 - hashmap_init(map, dir_rename_cmp, NULL, 0);
116 -}
117 -
118 -static void dir_rename_entry_init(struct dir_rename_entry *entry,
119 - char *directory)
120 -{
121 - hashmap_entry_init(&entry->ent, strhash(directory));
122 - entry->dir = directory;
123 - entry->non_unique_new_dir = 0;
124 - strbuf_init(&entry->new_dir, 0);
125 - string_list_init_nodup(&entry->possible_new_dirs);
126 -}
127 -
128 -struct collision_entry {
129 - struct hashmap_entry ent;
130 - char *target_file;
131 - struct string_list source_files;
132 - unsigned reported_already:1;
133 -};
134 -
135 -static struct collision_entry *collision_find_entry(struct hashmap *hashmap,
136 - char *target_file)
137 -{
138 - struct collision_entry key;
139 -
140 - hashmap_entry_init(&key.ent, strhash(target_file));
141 - key.target_file = target_file;
142 - return hashmap_get_entry(hashmap, &key, ent, NULL);
143 -}
144 -
145 -static int collision_cmp(const void *cmp_data UNUSED,
146 - const struct hashmap_entry *eptr,
147 - const struct hashmap_entry *entry_or_key,
148 - const void *keydata UNUSED)
149 -{
150 - const struct collision_entry *e1, *e2;
151 -
152 - e1 = container_of(eptr, const struct collision_entry, ent);
153 - e2 = container_of(entry_or_key, const struct collision_entry, ent);
154 -
155 - return strcmp(e1->target_file, e2->target_file);
156 -}
157 -
158 -static void collision_init(struct hashmap *map)
159 -{
160 - hashmap_init(map, collision_cmp, NULL, 0);
161 -}
162 -
163 -static void flush_output(struct merge_options *opt)
164 -{
165 - if (opt->buffer_output < 2 && opt->obuf.len) {
166 - fputs(opt->obuf.buf, stdout);
167 - strbuf_reset(&opt->obuf);
168 - }
169 -}
170 -
171 -__attribute__((format (printf, 2, 3)))
172 -static int err(struct merge_options *opt, const char *err, ...)
173 -{
174 - va_list params;
175 -
176 - if (opt->buffer_output < 2)
177 - flush_output(opt);
178 - else {
179 - strbuf_complete(&opt->obuf, '\n');
180 - strbuf_addstr(&opt->obuf, "error: ");
181 - }
182 - va_start(params, err);
183 - strbuf_vaddf(&opt->obuf, err, params);
184 - va_end(params);
185 - if (opt->buffer_output > 1)
186 - strbuf_addch(&opt->obuf, '\n');
187 - else {
188 - error("%s", opt->obuf.buf);
189 - strbuf_reset(&opt->obuf);
190 - }
191 -
192 - return -1;
193 -}
194 -
195 -static struct tree *shift_tree_object(struct repository *repo,
196 - struct tree *one, struct tree *two,
197 - const char *subtree_shift)
198 -{
199 - struct object_id shifted;
200 -
201 - if (!*subtree_shift) {
202 - shift_tree(repo, &one->object.oid, &two->object.oid, &shifted, 0);
203 - } else {
204 - shift_tree_by(repo, &one->object.oid, &two->object.oid, &shifted,
205 - subtree_shift);
206 - }
207 - if (oideq(&two->object.oid, &shifted))
208 - return two;
209 - return lookup_tree(repo, &shifted);
210 -}
211 -
212 -static inline void set_commit_tree(struct commit *c, struct tree *t)
213 -{
214 - c->maybe_tree = t;
215 -}
216 -
217 -static struct commit *make_virtual_commit(struct repository *repo,
218 - struct tree *tree,
219 - const char *comment)
220 -{
221 - struct commit *commit = alloc_commit_node(repo);
222 -
223 - set_merge_remote_desc(commit, comment, (struct object *)commit);
224 - set_commit_tree(commit, tree);
225 - commit->object.parsed = 1;
226 - return commit;
227 -}
228 -
229 -enum rename_type {
230 - RENAME_NORMAL = 0,
231 - RENAME_VIA_DIR,
232 - RENAME_ADD,
233 - RENAME_DELETE,
234 - RENAME_ONE_FILE_TO_ONE,
235 - RENAME_ONE_FILE_TO_TWO,
236 - RENAME_TWO_FILES_TO_ONE
237 -};
238 -
239 -/*
240 - * Since we want to write the index eventually, we cannot reuse the index
241 - * for these (temporary) data.
242 - */
243 -struct stage_data {
244 - struct diff_filespec stages[4]; /* mostly for oid & mode; maybe path */
245 - struct rename_conflict_info *rename_conflict_info;
246 - unsigned processed:1,
247 - rename_conflict_info_owned:1;
248 -};
249 -
250 -struct rename {
251 - unsigned processed:1;
252 - struct diff_filepair *pair;
253 - const char *branch; /* branch that the rename occurred on */
254 - /*
255 - * If directory rename detection affected this rename, what was its
256 - * original type ('A' or 'R') and it's original destination before
257 - * the directory rename (otherwise, '\0' and NULL for these two vars).
258 - */
259 - char dir_rename_original_type;
260 - char *dir_rename_original_dest;
261 - /*
262 - * Purpose of src_entry and dst_entry:
263 - *
264 - * If 'before' is renamed to 'after' then src_entry will contain
265 - * the versions of 'before' from the merge_base, HEAD, and MERGE in
266 - * stages 1, 2, and 3; dst_entry will contain the respective
267 - * versions of 'after' in corresponding locations. Thus, we have a
268 - * total of six modes and oids, though some will be null. (Stage 0
269 - * is ignored; we're interested in handling conflicts.)
270 - *
271 - * Since we don't turn on break-rewrites by default, neither
272 - * src_entry nor dst_entry can have all three of their stages have
273 - * non-null oids, meaning at most four of the six will be non-null.
274 - * Also, since this is a rename, both src_entry and dst_entry will
275 - * have at least one non-null oid, meaning at least two will be
276 - * non-null. Of the six oids, a typical rename will have three be
277 - * non-null. Only two implies a rename/delete, and four implies a
278 - * rename/add.
279 - */
280 - struct stage_data *src_entry;
281 - struct stage_data *dst_entry;
282 -};
283 -
284 -struct rename_conflict_info {
285 - enum rename_type rename_type;
286 - struct rename *ren1;
287 - struct rename *ren2;
288 -};
289 -
290 -static inline void setup_rename_conflict_info(enum rename_type rename_type,
291 - struct merge_options *opt,
292 - struct rename *ren1,
293 - struct rename *ren2)
294 -{
295 - struct rename_conflict_info *ci;
296 -
297 - /*
298 - * When we have two renames involved, it's easiest to get the
299 - * correct things into stage 2 and 3, and to make sure that the
300 - * content merge puts HEAD before the other branch if we just
301 - * ensure that branch1 == opt->branch1. So, simply flip arguments
302 - * around if we don't have that.
303 - */
304 - if (ren2 && ren1->branch != opt->branch1) {
305 - setup_rename_conflict_info(rename_type, opt, ren2, ren1);
306 - return;
307 - }
308 -
309 - CALLOC_ARRAY(ci, 1);
310 - ci->rename_type = rename_type;
311 - ci->ren1 = ren1;
312 - ci->ren2 = ren2;
313 -
314 - ci->ren1->dst_entry->processed = 0;
315 - ci->ren1->dst_entry->rename_conflict_info = ci;
316 - ci->ren1->dst_entry->rename_conflict_info_owned = 1;
317 - if (ren2) {
318 - ci->ren2->dst_entry->rename_conflict_info = ci;
319 - }
320 -}
321 -
322 -static int show(struct merge_options *opt, int v)
323 -{
324 - return (!opt->priv->call_depth && opt->verbosity >= v) ||
325 - opt->verbosity >= 5;
326 -}
327 -
328 -__attribute__((format (printf, 3, 4)))
329 -static void output(struct merge_options *opt, int v, const char *fmt, ...)
330 -{
331 - va_list ap;
332 -
333 - if (!show(opt, v))
334 - return;
335 -
336 - strbuf_addchars(&opt->obuf, ' ', opt->priv->call_depth * 2);
337 -
338 - va_start(ap, fmt);
339 - strbuf_vaddf(&opt->obuf, fmt, ap);
340 - va_end(ap);
341 -
342 - strbuf_addch(&opt->obuf, '\n');
343 - if (!opt->buffer_output)
344 - flush_output(opt);
345 -}
346 -
347 -static void repo_output_commit_title(struct merge_options *opt,
348 - struct repository *repo,
349 - struct commit *commit)
350 -{
351 - struct merge_remote_desc *desc;
352 -
353 - strbuf_addchars(&opt->obuf, ' ', opt->priv->call_depth * 2);
354 - desc = merge_remote_util(commit);
355 - if (desc)
356 - strbuf_addf(&opt->obuf, "virtual %s\n", desc->name);
357 - else {
358 - strbuf_repo_add_unique_abbrev(&opt->obuf, repo,
359 - &commit->object.oid,
360 - DEFAULT_ABBREV);
361 - strbuf_addch(&opt->obuf, ' ');
362 - if (repo_parse_commit(repo, commit) != 0)
363 - strbuf_addstr(&opt->obuf, _("(bad commit)\n"));
364 - else {
365 - const char *title;
366 - const char *msg = repo_get_commit_buffer(repo, commit, NULL);
367 - int len = find_commit_subject(msg, &title);
368 - if (len)
369 - strbuf_addf(&opt->obuf, "%.*s\n", len, title);
370 - repo_unuse_commit_buffer(repo, commit, msg);
371 - }
372 - }
373 - flush_output(opt);
374 -}
375 -
376 -static void output_commit_title(struct merge_options *opt, struct commit *commit)
377 -{
378 - repo_output_commit_title(opt, the_repository, commit);
379 -}
380 -
381 -static int add_cacheinfo(struct merge_options *opt,
382 - const struct diff_filespec *blob,
383 - const char *path, int stage, int refresh, int options)
384 -{
385 - struct index_state *istate = opt->repo->index;
386 - struct cache_entry *ce;
387 - int ret;
388 -
389 - ce = make_cache_entry(istate, blob->mode, &blob->oid, path, stage, 0);
390 - if (!ce)
391 - return err(opt, _("add_cacheinfo failed for path '%s'; merge aborting."), path);
392 -
393 - ret = add_index_entry(istate, ce, options);
394 - if (refresh) {
395 - struct cache_entry *nce;
396 -
397 - nce = refresh_cache_entry(istate, ce,
398 - CE_MATCH_REFRESH | CE_MATCH_IGNORE_MISSING);
399 - if (!nce)
400 - return err(opt, _("add_cacheinfo failed to refresh for path '%s'; merge aborting."), path);
401 - if (nce != ce)
402 - ret = add_index_entry(istate, nce, options);
403 - }
404 - return ret;
405 -}
406 -
407 -static inline int merge_detect_rename(struct merge_options *opt)
408 -{
409 - return (opt->detect_renames >= 0) ? opt->detect_renames : 1;
410 -}
411 -
412 -static void init_tree_desc_from_tree(struct tree_desc *desc, struct tree *tree)
413 -{
414 - if (parse_tree(tree) < 0)
415 - exit(128);
416 - init_tree_desc(desc, &tree->object.oid, tree->buffer, tree->size);
417 -}
418 -
419 -static int unpack_trees_start(struct merge_options *opt,
420 - struct tree *common,
421 - struct tree *head,
422 - struct tree *merge)
423 -{
424 - int rc;
425 - struct tree_desc t[3];
426 - struct index_state tmp_index = INDEX_STATE_INIT(opt->repo);
427 -
428 - memset(&opt->priv->unpack_opts, 0, sizeof(opt->priv->unpack_opts));
429 - if (opt->priv->call_depth)
430 - opt->priv->unpack_opts.index_only = 1;
431 - else {
432 - opt->priv->unpack_opts.update = 1;
433 - /* FIXME: should only do this if !overwrite_ignore */
434 - opt->priv->unpack_opts.preserve_ignored = 0;
435 - }
436 - opt->priv->unpack_opts.merge = 1;
437 - opt->priv->unpack_opts.head_idx = 2;
438 - opt->priv->unpack_opts.fn = threeway_merge;
439 - opt->priv->unpack_opts.src_index = opt->repo->index;
440 - opt->priv->unpack_opts.dst_index = &tmp_index;
441 - opt->priv->unpack_opts.aggressive = !merge_detect_rename(opt);
442 - setup_unpack_trees_porcelain(&opt->priv->unpack_opts, "merge");
443 -
444 - init_tree_desc_from_tree(t+0, common);
445 - init_tree_desc_from_tree(t+1, head);
446 - init_tree_desc_from_tree(t+2, merge);
447 -
448 - rc = unpack_trees(3, t, &opt->priv->unpack_opts);
449 - cache_tree_free(&opt->repo->index->cache_tree);
450 -
451 - /*
452 - * Update opt->repo->index to match the new results, AFTER saving a
453 - * copy in opt->priv->orig_index. Update src_index to point to the
454 - * saved copy. (verify_uptodate() checks src_index, and the original
455 - * index is the one that had the necessary modification timestamps.)
456 - */
457 - opt->priv->orig_index = *opt->repo->index;
458 - *opt->repo->index = tmp_index;
459 - opt->priv->unpack_opts.src_index = &opt->priv->orig_index;
460 -
461 - return rc;
462 -}
463 -
464 -static void unpack_trees_finish(struct merge_options *opt)
465 -{
466 - discard_index(&opt->priv->orig_index);
467 - clear_unpack_trees_porcelain(&opt->priv->unpack_opts);
468 -}
469 -
470 -static int save_files_dirs(const struct object_id *oid UNUSED,
471 - struct strbuf *base, const char *path,
472 - unsigned int mode, void *context)
473 -{
474 - struct path_hashmap_entry *entry;
475 - int baselen = base->len;
476 - struct merge_options *opt = context;
477 -
478 - strbuf_addstr(base, path);
479 -
480 - FLEX_ALLOC_MEM(entry, path, base->buf, base->len);
481 - hashmap_entry_init(&entry->e, fspathhash(entry->path));
482 - hashmap_add(&opt->priv->current_file_dir_set, &entry->e);
483 -
484 - strbuf_setlen(base, baselen);
485 - return (S_ISDIR(mode) ? READ_TREE_RECURSIVE : 0);
486 -}
487 -
488 -static void get_files_dirs(struct merge_options *opt, struct tree *tree)
489 -{
490 - struct pathspec match_all;
491 - memset(&match_all, 0, sizeof(match_all));
492 - read_tree(opt->repo, tree,
493 - &match_all, save_files_dirs, opt);
494 -}
495 -
496 -static int get_tree_entry_if_blob(struct repository *r,
497 - const struct object_id *tree,
498 - const char *path,
499 - struct diff_filespec *dfs)
500 -{
501 - int ret;
502 -
503 - ret = get_tree_entry(r, tree, path, &dfs->oid, &dfs->mode);
504 - if (S_ISDIR(dfs->mode)) {
505 - oidcpy(&dfs->oid, null_oid());
506 - dfs->mode = 0;
507 - }
508 - return ret;
509 -}
510 -
511 -/*
512 - * Returns an index_entry instance which doesn't have to correspond to
513 - * a real cache entry in Git's index.
514 - */
515 -static struct stage_data *insert_stage_data(struct repository *r,
516 - const char *path,
517 - struct tree *o, struct tree *a, struct tree *b,
518 - struct string_list *entries)
519 -{
520 - struct string_list_item *item;
521 - struct stage_data *e = xcalloc(1, sizeof(struct stage_data));
522 - get_tree_entry_if_blob(r, &o->object.oid, path, &e->stages[1]);
523 - get_tree_entry_if_blob(r, &a->object.oid, path, &e->stages[2]);
524 - get_tree_entry_if_blob(r, &b->object.oid, path, &e->stages[3]);
525 - item = string_list_insert(entries, path);
526 - item->util = e;
527 - return e;
528 -}
529 -
530 -/*
531 - * Create a dictionary mapping file names to stage_data objects. The
532 - * dictionary contains one entry for every path with a non-zero stage entry.
533 - */
534 -static struct string_list *get_unmerged(struct index_state *istate)
535 -{
536 - struct string_list *unmerged = xmalloc(sizeof(struct string_list));
537 - int i;
538 -
539 - string_list_init_dup(unmerged);
540 -
541 - /* TODO: audit for interaction with sparse-index. */
542 - ensure_full_index(istate);
543 - for (i = 0; i < istate->cache_nr; i++) {
544 - struct string_list_item *item;
545 - struct stage_data *e;
546 - const struct cache_entry *ce = istate->cache[i];
547 - if (!ce_stage(ce))
548 - continue;
549 -
550 - item = string_list_lookup(unmerged, ce->name);
551 - if (!item) {
552 - item = string_list_insert(unmerged, ce->name);
553 - item->util = xcalloc(1, sizeof(struct stage_data));
554 - }
555 - e = item->util;
556 - e->stages[ce_stage(ce)].mode = ce->ce_mode;
557 - oidcpy(&e->stages[ce_stage(ce)].oid, &ce->oid);
558 - }
559 -
560 - return unmerged;
561 -}
562 -
563 -static int string_list_df_name_compare(const char *one, const char *two)
564 -{
565 - int onelen = strlen(one);
566 - int twolen = strlen(two);
567 - /*
568 - * Here we only care that entries for D/F conflicts are
569 - * adjacent, in particular with the file of the D/F conflict
570 - * appearing before files below the corresponding directory.
571 - * The order of the rest of the list is irrelevant for us.
572 - *
573 - * To achieve this, we sort with df_name_compare and provide
574 - * the mode S_IFDIR so that D/F conflicts will sort correctly.
575 - * We use the mode S_IFDIR for everything else for simplicity,
576 - * since in other cases any changes in their order due to
577 - * sorting cause no problems for us.
578 - */
579 - int cmp = df_name_compare(one, onelen, S_IFDIR,
580 - two, twolen, S_IFDIR);
581 - /*
582 - * Now that 'foo' and 'foo/bar' compare equal, we have to make sure
583 - * that 'foo' comes before 'foo/bar'.
584 - */
585 - if (cmp)
586 - return cmp;
587 - return onelen - twolen;
588 -}
589 -
590 -static void record_df_conflict_files(struct merge_options *opt,
591 - struct string_list *entries)
592 -{
593 - /* If there is a D/F conflict and the file for such a conflict
594 - * currently exists in the working tree, we want to allow it to be
595 - * removed to make room for the corresponding directory if needed.
596 - * The files underneath the directories of such D/F conflicts will
597 - * be processed before the corresponding file involved in the D/F
598 - * conflict. If the D/F directory ends up being removed by the
599 - * merge, then we won't have to touch the D/F file. If the D/F
600 - * directory needs to be written to the working copy, then the D/F
601 - * file will simply be removed (in make_room_for_path()) to make
602 - * room for the necessary paths. Note that if both the directory
603 - * and the file need to be present, then the D/F file will be
604 - * reinstated with a new unique name at the time it is processed.
605 - */
606 - struct string_list df_sorted_entries = STRING_LIST_INIT_NODUP;
607 - const char *last_file = NULL;
608 - int last_len = 0;
609 - int i;
610 -
611 - /*
612 - * If we're merging merge-bases, we don't want to bother with
613 - * any working directory changes.
614 - */
615 - if (opt->priv->call_depth)
616 - return;
617 -
618 - /* Ensure D/F conflicts are adjacent in the entries list. */
619 - for (i = 0; i < entries->nr; i++) {
620 - struct string_list_item *next = &entries->items[i];
621 - string_list_append(&df_sorted_entries, next->string)->util =
622 - next->util;
623 - }
624 - df_sorted_entries.cmp = string_list_df_name_compare;
625 - string_list_sort(&df_sorted_entries);
626 -
627 - string_list_clear(&opt->priv->df_conflict_file_set, 1);
628 - for (i = 0; i < df_sorted_entries.nr; i++) {
629 - const char *path = df_sorted_entries.items[i].string;
630 - int len = strlen(path);
631 - struct stage_data *e = df_sorted_entries.items[i].util;
632 -
633 - /*
634 - * Check if last_file & path correspond to a D/F conflict;
635 - * i.e. whether path is last_file+'/'+<something>.
636 - * If so, record that it's okay to remove last_file to make
637 - * room for path and friends if needed.
638 - */
639 - if (last_file &&
640 - len > last_len &&
641 - memcmp(path, last_file, last_len) == 0 &&
642 - path[last_len] == '/') {
643 - string_list_insert(&opt->priv->df_conflict_file_set, last_file);
644 - }
645 -
646 - /*
647 - * Determine whether path could exist as a file in the
648 - * working directory as a possible D/F conflict. This
649 - * will only occur when it exists in stage 2 as a
650 - * file.
651 - */
652 - if (S_ISREG(e->stages[2].mode) || S_ISLNK(e->stages[2].mode)) {
653 - last_file = path;
654 - last_len = len;
655 - } else {
656 - last_file = NULL;
657 - }
658 - }
659 - string_list_clear(&df_sorted_entries, 0);
660 -}
661 -
662 -static int update_stages(struct merge_options *opt, const char *path,
663 - const struct diff_filespec *o,
664 - const struct diff_filespec *a,
665 - const struct diff_filespec *b)
666 -{
667 -
668 - /*
669 - * NOTE: It is usually a bad idea to call update_stages on a path
670 - * before calling update_file on that same path, since it can
671 - * sometimes lead to spurious "refusing to lose untracked file..."
672 - * messages from update_file (via make_room_for path via
673 - * would_lose_untracked). Instead, reverse the order of the calls
674 - * (executing update_file first and then update_stages).
675 - */
676 - int clear = 1;
677 - int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_SKIP_DFCHECK;
678 - if (clear)
679 - if (remove_file_from_index(opt->repo->index, path))
680 - return -1;
681 - if (o)
682 - if (add_cacheinfo(opt, o, path, 1, 0, options))
683 - return -1;
684 - if (a)
685 - if (add_cacheinfo(opt, a, path, 2, 0, options))
686 - return -1;
687 - if (b)
688 - if (add_cacheinfo(opt, b, path, 3, 0, options))
689 - return -1;
690 - return 0;
691 -}
692 -
693 -static void update_entry(struct stage_data *entry,
694 - struct diff_filespec *o,
695 - struct diff_filespec *a,
696 - struct diff_filespec *b)
697 -{
698 - entry->processed = 0;
699 - entry->stages[1].mode = o->mode;
700 - entry->stages[2].mode = a->mode;
701 - entry->stages[3].mode = b->mode;
702 - oidcpy(&entry->stages[1].oid, &o->oid);
703 - oidcpy(&entry->stages[2].oid, &a->oid);
704 - oidcpy(&entry->stages[3].oid, &b->oid);
705 -}
706 -
707 -static int remove_file(struct merge_options *opt, int clean,
708 - const char *path, int no_wd)
709 -{
710 - int update_cache = opt->priv->call_depth || clean;
711 - int update_working_directory = !opt->priv->call_depth && !no_wd;
712 -
713 - if (update_cache) {
714 - if (remove_file_from_index(opt->repo->index, path))
715 - return -1;
716 - }
717 - if (update_working_directory) {
718 - if (ignore_case) {
719 - struct cache_entry *ce;
720 - ce = index_file_exists(opt->repo->index, path, strlen(path),
721 - ignore_case);
722 - if (ce && ce_stage(ce) == 0 && strcmp(path, ce->name))
723 - return 0;
724 - }
725 - if (remove_path(path))
726 - return -1;
727 - }
728 - return 0;
729 -}
730 -
731 -/* add a string to a strbuf, but converting "/" to "_" */
732 -static void add_flattened_path(struct strbuf *out, const char *s)
733 -{
734 - size_t i = out->len;
735 - strbuf_addstr(out, s);
736 - for (; i < out->len; i++)
737 - if (out->buf[i] == '/')
738 - out->buf[i] = '_';
739 -}
740 -
741 -static char *unique_path(struct merge_options *opt,
742 - const char *path,
743 - const char *branch)
744 -{
745 - struct path_hashmap_entry *entry;
746 - struct strbuf newpath = STRBUF_INIT;
747 - int suffix = 0;
748 - size_t base_len;
749 -
750 - strbuf_addf(&newpath, "%s~", path);
751 - add_flattened_path(&newpath, branch);
752 -
753 - base_len = newpath.len;
754 - while (hashmap_get_from_hash(&opt->priv->current_file_dir_set,
755 - fspathhash(newpath.buf), newpath.buf) ||
756 - (!opt->priv->call_depth && file_exists(newpath.buf))) {
757 - strbuf_setlen(&newpath, base_len);
758 - strbuf_addf(&newpath, "_%d", suffix++);
759 - }
760 -
761 - FLEX_ALLOC_MEM(entry, path, newpath.buf, newpath.len);
762 - hashmap_entry_init(&entry->e, fspathhash(entry->path));
763 - hashmap_add(&opt->priv->current_file_dir_set, &entry->e);
764 - return strbuf_detach(&newpath, NULL);
765 -}
766 -
767 -/**
768 - * Check whether a directory in the index is in the way of an incoming
769 - * file. Return 1 if so. If check_working_copy is non-zero, also
770 - * check the working directory. If empty_ok is non-zero, also return
771 - * 0 in the case where the working-tree dir exists but is empty.
772 - */
773 -static int dir_in_way(struct index_state *istate, const char *path,
774 - int check_working_copy, int empty_ok)
775 -{
776 - int pos;
777 - struct strbuf dirpath = STRBUF_INIT;
778 - struct stat st;
779 -
780 - strbuf_addstr(&dirpath, path);
781 - strbuf_addch(&dirpath, '/');
782 -
783 - pos = index_name_pos(istate, dirpath.buf, dirpath.len);
784 -
785 - if (pos < 0)
786 - pos = -1 - pos;
787 - if (pos < istate->cache_nr &&
788 - !strncmp(dirpath.buf, istate->cache[pos]->name, dirpath.len)) {
789 - strbuf_release(&dirpath);
790 - return 1;
791 - }
792 -
793 - strbuf_release(&dirpath);
794 - return check_working_copy && !lstat(path, &st) && S_ISDIR(st.st_mode) &&
795 - !(empty_ok && is_empty_dir(path)) &&
796 - !has_symlink_leading_path(path, strlen(path));
797 -}
798 -
799 -/*
800 - * Returns whether path was tracked in the index before the merge started,
801 - * and its oid and mode match the specified values
802 - */
803 -static int was_tracked_and_matches(struct merge_options *opt, const char *path,
804 - const struct diff_filespec *blob)
805 -{
806 - int pos = index_name_pos(&opt->priv->orig_index, path, strlen(path));
807 - struct cache_entry *ce;
808 -
809 - if (0 > pos)
810 - /* we were not tracking this path before the merge */
811 - return 0;
812 -
813 - /* See if the file we were tracking before matches */
814 - ce = opt->priv->orig_index.cache[pos];
815 - return (oideq(&ce->oid, &blob->oid) && ce->ce_mode == blob->mode);
816 -}
817 -
818 -/*
819 - * Returns whether path was tracked in the index before the merge started
820 - */
821 -static int was_tracked(struct merge_options *opt, const char *path)
822 -{
823 - int pos = index_name_pos(&opt->priv->orig_index, path, strlen(path));
824 -
825 - if (0 <= pos)
826 - /* we were tracking this path before the merge */
827 - return 1;
828 -
829 - return 0;
830 -}
831 -
832 -static int would_lose_untracked(struct merge_options *opt, const char *path)
833 -{
834 - struct index_state *istate = opt->repo->index;
835 -
836 - /*
837 - * This may look like it can be simplified to:
838 - * return !was_tracked(opt, path) && file_exists(path)
839 - * but it can't. This function needs to know whether path was in
840 - * the working tree due to EITHER having been tracked in the index
841 - * before the merge OR having been put into the working copy and
842 - * index by unpack_trees(). Due to that either-or requirement, we
843 - * check the current index instead of the original one.
844 - *
845 - * Note that we do not need to worry about merge-recursive itself
846 - * updating the index after unpack_trees() and before calling this
847 - * function, because we strictly require all code paths in
848 - * merge-recursive to update the working tree first and the index
849 - * second. Doing otherwise would break
850 - * update_file()/would_lose_untracked(); see every comment in this
851 - * file which mentions "update_stages".
852 - */
853 - int pos = index_name_pos(istate, path, strlen(path));
854 -
855 - if (pos < 0)
856 - pos = -1 - pos;
857 - while (pos < istate->cache_nr &&
858 - !strcmp(path, istate->cache[pos]->name)) {
859 - /*
860 - * If stage #0, it is definitely tracked.
861 - * If it has stage #2 then it was tracked
862 - * before this merge started. All other
863 - * cases the path was not tracked.
864 - */
865 - switch (ce_stage(istate->cache[pos])) {
866 - case 0:
867 - case 2:
868 - return 0;
869 - }
870 - pos++;
871 - }
872 - return file_exists(path);
873 -}
874 -
875 -static int was_dirty(struct merge_options *opt, const char *path)
876 -{
877 - struct cache_entry *ce;
878 - int dirty = 1;
879 -
880 - if (opt->priv->call_depth || !was_tracked(opt, path))
881 - return !dirty;
882 -
883 - ce = index_file_exists(opt->priv->unpack_opts.src_index,
884 - path, strlen(path), ignore_case);
885 - dirty = verify_uptodate(ce, &opt->priv->unpack_opts) != 0;
886 - return dirty;
887 -}
888 -
889 -static int make_room_for_path(struct merge_options *opt, const char *path)
890 -{
891 - int status, i;
892 - const char *msg = _("failed to create path '%s'%s");
893 -
894 - /* Unlink any D/F conflict files that are in the way */
895 - for (i = 0; i < opt->priv->df_conflict_file_set.nr; i++) {
896 - const char *df_path = opt->priv->df_conflict_file_set.items[i].string;
897 - size_t pathlen = strlen(path);
898 - size_t df_pathlen = strlen(df_path);
899 - if (df_pathlen < pathlen &&
900 - path[df_pathlen] == '/' &&
901 - strncmp(path, df_path, df_pathlen) == 0) {
902 - output(opt, 3,
903 - _("Removing %s to make room for subdirectory\n"),
904 - df_path);
905 - unlink(df_path);
906 - unsorted_string_list_delete_item(&opt->priv->df_conflict_file_set,
907 - i, 0);
908 - break;
909 - }
910 - }
911 -
912 - /* Make sure leading directories are created */
913 - status = safe_create_leading_directories_const(path);
914 - if (status) {
915 - if (status == SCLD_EXISTS)
916 - /* something else exists */
917 - return err(opt, msg, path, _(": perhaps a D/F conflict?"));
918 - return err(opt, msg, path, "");
919 - }
920 -
921 - /*
922 - * Do not unlink a file in the work tree if we are not
923 - * tracking it.
924 - */
925 - if (would_lose_untracked(opt, path))
926 - return err(opt, _("refusing to lose untracked file at '%s'"),
927 - path);
928 -
929 - /* Successful unlink is good.. */
930 - if (!unlink(path))
931 - return 0;
932 - /* .. and so is no existing file */
933 - if (errno == ENOENT)
934 - return 0;
935 - /* .. but not some other error (who really cares what?) */
936 - return err(opt, msg, path, _(": perhaps a D/F conflict?"));
937 -}
938 -
939 -static int update_file_flags(struct merge_options *opt,
940 - const struct diff_filespec *contents,
941 - const char *path,
942 - int update_cache,
943 - int update_wd)
944 -{
945 - int ret = 0;
946 -
947 - if (opt->priv->call_depth)
948 - update_wd = 0;
949 -
950 - if (update_wd) {
951 - enum object_type type;
952 - void *buf;
953 - unsigned long size;
954 -
955 - if (S_ISGITLINK(contents->mode)) {
956 - /*
957 - * We may later decide to recursively descend into
958 - * the submodule directory and update its index
959 - * and/or work tree, but we do not do that now.
960 - */
961 - update_wd = 0;
962 - goto update_index;
963 - }
964 -
965 - buf = repo_read_object_file(the_repository, &contents->oid,
966 - &type, &size);
967 - if (!buf) {
968 - ret = err(opt, _("cannot read object %s '%s'"),
969 - oid_to_hex(&contents->oid), path);
970 - goto free_buf;
971 - }
972 - if (type != OBJ_BLOB) {
973 - ret = err(opt, _("blob expected for %s '%s'"),
974 - oid_to_hex(&contents->oid), path);
975 - goto free_buf;
976 - }
977 - if (S_ISREG(contents->mode)) {
978 - struct strbuf strbuf = STRBUF_INIT;
979 - if (convert_to_working_tree(opt->repo->index,
980 - path, buf, size, &strbuf, NULL)) {
981 - free(buf);
982 - size = strbuf.len;
983 - buf = strbuf_detach(&strbuf, NULL);
984 - }
985 - }
986 -
987 - if (make_room_for_path(opt, path) < 0) {
988 - update_wd = 0;
989 - goto free_buf;
990 - }
991 - if (S_ISREG(contents->mode) ||
992 - (!has_symlinks && S_ISLNK(contents->mode))) {
993 - int fd;
994 - int mode = (contents->mode & 0100 ? 0777 : 0666);
995 -
996 - fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);
997 - if (fd < 0) {
998 - ret = err(opt, _("failed to open '%s': %s"),
999 - path, strerror(errno));
1000 - goto free_buf;
1001 - }
1002 - write_in_full(fd, buf, size);
1003 - close(fd);
1004 - } else if (S_ISLNK(contents->mode)) {
1005 - char *lnk = xmemdupz(buf, size);
1006 - safe_create_leading_directories_const(path);
1007 - unlink(path);
1008 - if (symlink(lnk, path))
1009 - ret = err(opt, _("failed to symlink '%s': %s"),
1010 - path, strerror(errno));
1011 - free(lnk);
1012 - } else
1013 - ret = err(opt,
1014 - _("do not know what to do with %06o %s '%s'"),
1015 - contents->mode, oid_to_hex(&contents->oid), path);
1016 - free_buf:
1017 - free(buf);
1018 - }
1019 -update_index:
1020 - if (!ret && update_cache) {
1021 - int refresh = (!opt->priv->call_depth &&
1022 - contents->mode != S_IFGITLINK);
1023 - if (add_cacheinfo(opt, contents, path, 0, refresh,
1024 - ADD_CACHE_OK_TO_ADD))
1025 - return -1;
1026 - }
1027 - return ret;
1028 -}
1029 -
1030 -static int update_file(struct merge_options *opt,
1031 - int clean,
1032 - const struct diff_filespec *contents,
1033 - const char *path)
1034 -{
1035 - return update_file_flags(opt, contents, path,
1036 - opt->priv->call_depth || clean, !opt->priv->call_depth);
1037 -}
1038 -
1039 -/* Low level file merging, update and removal */
1040 -
1041 -struct merge_file_info {
1042 - struct diff_filespec blob; /* mostly use oid & mode; sometimes path */
1043 - unsigned clean:1,
1044 - merge:1;
1045 -};
1046 -
1047 -static int merge_3way(struct merge_options *opt,
1048 - mmbuffer_t *result_buf,
1049 - const struct diff_filespec *o,
1050 - const struct diff_filespec *a,
1051 - const struct diff_filespec *b,
1052 - const char *branch1,
1053 - const char *branch2,
1054 - const int extra_marker_size)
1055 -{
1056 - mmfile_t orig, src1, src2;
1057 - struct ll_merge_options ll_opts = LL_MERGE_OPTIONS_INIT;
1058 - char *base, *name1, *name2;
1059 - enum ll_merge_result merge_status;
1060 -
1061 - ll_opts.renormalize = opt->renormalize;
1062 - ll_opts.extra_marker_size = extra_marker_size;
1063 - ll_opts.xdl_opts = opt->xdl_opts;
1064 - ll_opts.conflict_style = opt->conflict_style;
1065 -
1066 - if (opt->priv->call_depth) {
1067 - ll_opts.virtual_ancestor = 1;
1068 - ll_opts.variant = 0;
1069 - } else {
1070 - switch (opt->recursive_variant) {
1071 - case MERGE_VARIANT_OURS:
1072 - ll_opts.variant = XDL_MERGE_FAVOR_OURS;
1073 - break;
1074 - case MERGE_VARIANT_THEIRS:
1075 - ll_opts.variant = XDL_MERGE_FAVOR_THEIRS;
1076 - break;
1077 - default:
1078 - ll_opts.variant = 0;
1079 - break;
1080 - }
1081 - }
1082 -
1083 - assert(a->path && b->path && o->path && opt->ancestor);
1084 - if (strcmp(a->path, b->path) || strcmp(a->path, o->path) != 0) {
1085 - base = mkpathdup("%s:%s", opt->ancestor, o->path);
1086 - name1 = mkpathdup("%s:%s", branch1, a->path);
1087 - name2 = mkpathdup("%s:%s", branch2, b->path);
1088 - } else {
1089 - base = mkpathdup("%s", opt->ancestor);
1090 - name1 = mkpathdup("%s", branch1);
1091 - name2 = mkpathdup("%s", branch2);
1092 - }
1093 -
1094 - read_mmblob(&orig, &o->oid);
1095 - read_mmblob(&src1, &a->oid);
1096 - read_mmblob(&src2, &b->oid);
1097 -
1098 - /*
1099 - * FIXME: Using a->path for normalization rules in ll_merge could be
1100 - * wrong if we renamed from a->path to b->path. We should use the
1101 - * target path for where the file will be written.
1102 - */
1103 - merge_status = ll_merge(result_buf, a->path, &orig, base,
1104 - &src1, name1, &src2, name2,
1105 - opt->repo->index, &ll_opts);
1106 - if (merge_status == LL_MERGE_BINARY_CONFLICT)
1107 - warning("Cannot merge binary files: %s (%s vs. %s)",
1108 - a->path, name1, name2);
1109 -
1110 - free(base);
1111 - free(name1);
1112 - free(name2);
1113 - free(orig.ptr);
1114 - free(src1.ptr);
1115 - free(src2.ptr);
1116 - return merge_status;
1117 -}
1118 -
1119 -static int find_first_merges(struct repository *repo,
1120 - struct object_array *result, const char *path,
1121 - struct commit *a, struct commit *b)
1122 -{
1123 - int i, j;
1124 - struct object_array merges = OBJECT_ARRAY_INIT;
1125 - struct commit *commit;
1126 - int contains_another;
1127 -
1128 - char merged_revision[GIT_MAX_HEXSZ + 2];
1129 - const char *rev_args[] = { "rev-list", "--merges", "--ancestry-path",
1130 - "--all", merged_revision, NULL };
1131 - struct rev_info revs;
1132 - struct setup_revision_opt rev_opts;
1133 -
1134 - memset(result, 0, sizeof(struct object_array));
1135 - memset(&rev_opts, 0, sizeof(rev_opts));
1136 -
1137 - /* get all revisions that merge commit a */
1138 - xsnprintf(merged_revision, sizeof(merged_revision), "^%s",
1139 - oid_to_hex(&a->object.oid));
1140 - repo_init_revisions(repo, &revs, NULL);
1141 - /* FIXME: can't handle linked worktrees in submodules yet */
1142 - revs.single_worktree = path != NULL;
1143 - setup_revisions(ARRAY_SIZE(rev_args)-1, rev_args, &revs, &rev_opts);
1144 -
1145 - /* save all revisions from the above list that contain b */
1146 - if (prepare_revision_walk(&revs))
1147 - die("revision walk setup failed");
1148 - while ((commit = get_revision(&revs)) != NULL) {
1149 - struct object *o = &(commit->object);
1150 - int ret = repo_in_merge_bases(repo, b, commit);
1151 - if (ret < 0) {
1152 - object_array_clear(&merges);
1153 - release_revisions(&revs);
1154 - return ret;
1155 - }
1156 - if (ret)
1157 - add_object_array(o, NULL, &merges);
1158 - }
1159 - reset_revision_walk();
1160 -
1161 - /* Now we've got all merges that contain a and b. Prune all
1162 - * merges that contain another found merge and save them in
1163 - * result.
1164 - */
1165 - for (i = 0; i < merges.nr; i++) {
1166 - struct commit *m1 = (struct commit *) merges.objects[i].item;
1167 -
1168 - contains_another = 0;
1169 - for (j = 0; j < merges.nr; j++) {
1170 - struct commit *m2 = (struct commit *) merges.objects[j].item;
1171 - if (i != j) {
1172 - int ret = repo_in_merge_bases(repo, m2, m1);
1173 - if (ret < 0) {
1174 - object_array_clear(&merges);
1175 - release_revisions(&revs);
1176 - return ret;
1177 - }
1178 - if (ret > 0) {
1179 - contains_another = 1;
1180 - break;
1181 - }
1182 - }
1183 - }
1184 -
1185 - if (!contains_another)
1186 - add_object_array(merges.objects[i].item, NULL, result);
1187 - }
1188 -
1189 - object_array_clear(&merges);
1190 - release_revisions(&revs);
1191 - return result->nr;
1192 -}
1193 -
1194 -static void print_commit(struct repository *repo, struct commit *commit)
1195 -{
1196 - struct strbuf sb = STRBUF_INIT;
1197 - struct pretty_print_context ctx = {0};
1198 - ctx.date_mode.type = DATE_NORMAL;
1199 - /* FIXME: Merge this with output_commit_title() */
1200 - assert(!merge_remote_util(commit));
1201 - repo_format_commit_message(repo, commit, " %h: %m %s", &sb, &ctx);
1202 - fprintf(stderr, "%s\n", sb.buf);
1203 - strbuf_release(&sb);
1204 -}
1205 -
1206 -static int is_valid(const struct diff_filespec *dfs)
1207 -{
1208 - return dfs->mode != 0 && !is_null_oid(&dfs->oid);
1209 -}
1210 -
1211 -static int merge_submodule(struct merge_options *opt,
1212 - struct object_id *result, const char *path,
1213 - const struct object_id *base, const struct object_id *a,
1214 - const struct object_id *b)
1215 -{
1216 - struct repository subrepo;
1217 - int ret = 0, ret2;
1218 - struct commit *commit_base, *commit_a, *commit_b;
1219 - int parent_count;
1220 - struct object_array merges;
1221 -
1222 - int i;
1223 - int search = !opt->priv->call_depth;
1224 -
1225 - /* store a in result in case we fail */
1226 - /* FIXME: This is the WRONG resolution for the recursive case when
1227 - * we need to be careful to avoid accidentally matching either side.
1228 - * Should probably use o instead there, much like we do for merging
1229 - * binaries.
1230 - */
1231 - oidcpy(result, a);
1232 -
1233 - /* we can not handle deletion conflicts */
1234 - if (is_null_oid(base))
1235 - return 0;
1236 - if (is_null_oid(a))
1237 - return 0;
1238 - if (is_null_oid(b))
1239 - return 0;
1240 -
1241 - if (repo_submodule_init(&subrepo, opt->repo, path, null_oid())) {
1242 - output(opt, 1, _("Failed to merge submodule %s (not checked out)"), path);
1243 - return 0;
1244 - }
1245 -
1246 - if (!(commit_base = lookup_commit_reference(&subrepo, base)) ||
1247 - !(commit_a = lookup_commit_reference(&subrepo, a)) ||
1248 - !(commit_b = lookup_commit_reference(&subrepo, b))) {
1249 - output(opt, 1, _("Failed to merge submodule %s (commits not present)"), path);
1250 - goto cleanup;
1251 - }
1252 -
1253 - /* check whether both changes are forward */
1254 - ret2 = repo_in_merge_bases(&subrepo, commit_base, commit_a);
1255 - if (ret2 < 0) {
1256 - output(opt, 1, _("Failed to merge submodule %s (repository corrupt)"), path);
1257 - ret = -1;
1258 - goto cleanup;
1259 - }
1260 - if (ret2 > 0)
1261 - ret2 = repo_in_merge_bases(&subrepo, commit_base, commit_b);
1262 - if (ret2 < 0) {
1263 - output(opt, 1, _("Failed to merge submodule %s (repository corrupt)"), path);
1264 - ret = -1;
1265 - goto cleanup;
1266 - }
1267 - if (!ret2) {
1268 - output(opt, 1, _("Failed to merge submodule %s (commits don't follow merge-base)"), path);
1269 - goto cleanup;
1270 - }
1271 -
1272 - /* Case #1: a is contained in b or vice versa */
1273 - ret2 = repo_in_merge_bases(&subrepo, commit_a, commit_b);
1274 - if (ret2 < 0) {
1275 - output(opt, 1, _("Failed to merge submodule %s (repository corrupt)"), path);
1276 - ret = -1;
1277 - goto cleanup;
1278 - }
1279 - if (ret2) {
1280 - oidcpy(result, b);
1281 - if (show(opt, 3)) {
1282 - output(opt, 3, _("Fast-forwarding submodule %s to the following commit:"), path);
1283 - repo_output_commit_title(opt, &subrepo, commit_b);
1284 - } else if (show(opt, 2))
1285 - output(opt, 2, _("Fast-forwarding submodule %s"), path);
1286 - else
1287 - ; /* no output */
1288 -
1289 - ret = 1;
1290 - goto cleanup;
1291 - }
1292 - ret2 = repo_in_merge_bases(&subrepo, commit_b, commit_a);
1293 - if (ret2 < 0) {
1294 - output(opt, 1, _("Failed to merge submodule %s (repository corrupt)"), path);
1295 - ret = -1;
1296 - goto cleanup;
1297 - }
1298 - if (ret2) {
1299 - oidcpy(result, a);
1300 - if (show(opt, 3)) {
1301 - output(opt, 3, _("Fast-forwarding submodule %s to the following commit:"), path);
1302 - repo_output_commit_title(opt, &subrepo, commit_a);
1303 - } else if (show(opt, 2))
1304 - output(opt, 2, _("Fast-forwarding submodule %s"), path);
1305 - else
1306 - ; /* no output */
1307 -
1308 - ret = 1;
1309 - goto cleanup;
1310 - }
1311 -
1312 - /*
1313 - * Case #2: There are one or more merges that contain a and b in
1314 - * the submodule. If there is only one, then present it as a
1315 - * suggestion to the user, but leave it marked unmerged so the
1316 - * user needs to confirm the resolution.
1317 - */
1318 -
1319 - /* Skip the search if makes no sense to the calling context. */
1320 - if (!search)
1321 - goto cleanup;
1322 -
1323 - /* find commit which merges them */
1324 - parent_count = find_first_merges(&subrepo, &merges, path,
1325 - commit_a, commit_b);
1326 - switch (parent_count) {
1327 - case -1:
1328 - output(opt, 1,_("Failed to merge submodule %s (repository corrupt)"), path);
1329 - ret = -1;
1330 - break;
1331 - case 0:
1332 - output(opt, 1, _("Failed to merge submodule %s (merge following commits not found)"), path);
1333 - break;
1334 -
1335 - case 1:
1336 - output(opt, 1, _("Failed to merge submodule %s (not fast-forward)"), path);
1337 - output(opt, 2, _("Found a possible merge resolution for the submodule:\n"));
1338 - print_commit(&subrepo, (struct commit *) merges.objects[0].item);
1339 - output(opt, 2, _(
1340 - "If this is correct simply add it to the index "
1341 - "for example\n"
1342 - "by using:\n\n"
1343 - " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
1344 - "which will accept this suggestion.\n"),
1345 - oid_to_hex(&merges.objects[0].item->oid), path);
1346 - break;
1347 -
1348 - default:
1349 - output(opt, 1, _("Failed to merge submodule %s (multiple merges found)"), path);
1350 - for (i = 0; i < merges.nr; i++)
1351 - print_commit(&subrepo, (struct commit *) merges.objects[i].item);
1352 - }
1353 -
1354 - object_array_clear(&merges);
1355 -cleanup:
1356 - repo_clear(&subrepo);
1357 - return ret;
1358 -}
1359 -
1360 -static int merge_mode_and_contents(struct merge_options *opt,
1361 - const struct diff_filespec *o,
1362 - const struct diff_filespec *a,
1363 - const struct diff_filespec *b,
1364 - const char *filename,
1365 - const char *branch1,
1366 - const char *branch2,
1367 - const int extra_marker_size,
1368 - struct merge_file_info *result)
1369 -{
1370 - if (opt->branch1 != branch1) {
1371 - /*
1372 - * It's weird getting a reverse merge with HEAD on the bottom
1373 - * side of the conflict markers and the other branch on the
1374 - * top. Fix that.
1375 - */
1376 - return merge_mode_and_contents(opt, o, b, a,
1377 - filename,
1378 - branch2, branch1,
1379 - extra_marker_size, result);
1380 - }
1381 -
1382 - result->merge = 0;
1383 - result->clean = 1;
1384 -
1385 - if ((S_IFMT & a->mode) != (S_IFMT & b->mode)) {
1386 - result->clean = 0;
1387 - /*
1388 - * FIXME: This is a bad resolution for recursive case; for
1389 - * the recursive case we want something that is unlikely to
1390 - * accidentally match either side. Also, while it makes
1391 - * sense to prefer regular files over symlinks, it doesn't
1392 - * make sense to prefer regular files over submodules.
1393 - */
1394 - if (S_ISREG(a->mode)) {
1395 - result->blob.mode = a->mode;
1396 - oidcpy(&result->blob.oid, &a->oid);
1397 - } else {
1398 - result->blob.mode = b->mode;
1399 - oidcpy(&result->blob.oid, &b->oid);
1400 - }
1401 - } else {
1402 - if (!oideq(&a->oid, &o->oid) && !oideq(&b->oid, &o->oid))
1403 - result->merge = 1;
1404 -
1405 - /*
1406 - * Merge modes
1407 - */
1408 - if (a->mode == b->mode || a->mode == o->mode)
1409 - result->blob.mode = b->mode;
1410 - else {
1411 - result->blob.mode = a->mode;
1412 - if (b->mode != o->mode) {
1413 - result->clean = 0;
1414 - result->merge = 1;
1415 - }
1416 - }
1417 -
1418 - if (oideq(&a->oid, &b->oid) || oideq(&a->oid, &o->oid))
1419 - oidcpy(&result->blob.oid, &b->oid);
1420 - else if (oideq(&b->oid, &o->oid))
1421 - oidcpy(&result->blob.oid, &a->oid);
1422 - else if (S_ISREG(a->mode)) {
1423 - mmbuffer_t result_buf;
1424 - int ret = 0, merge_status;
1425 -
1426 - merge_status = merge_3way(opt, &result_buf, o, a, b,
1427 - branch1, branch2,
1428 - extra_marker_size);
1429 -
1430 - if ((merge_status < 0) || !result_buf.ptr)
1431 - ret = err(opt, _("failed to execute internal merge"));
1432 -
1433 - if (!ret &&
1434 - write_object_file(result_buf.ptr, result_buf.size,
1435 - OBJ_BLOB, &result->blob.oid))
1436 - ret = err(opt, _("unable to add %s to database"),
1437 - a->path);
1438 -
1439 - free(result_buf.ptr);
1440 - if (ret)
1441 - return ret;
1442 - /* FIXME: bug, what if modes didn't match? */
1443 - result->clean = (merge_status == 0);
1444 - } else if (S_ISGITLINK(a->mode)) {
1445 - int clean = merge_submodule(opt, &result->blob.oid,
1446 - o->path,
1447 - &o->oid,
1448 - &a->oid,
1449 - &b->oid);
1450 - if (clean < 0)
1451 - return -1;
1452 - result->clean = clean;
1453 - } else if (S_ISLNK(a->mode)) {
1454 - switch (opt->recursive_variant) {
1455 - case MERGE_VARIANT_NORMAL:
1456 - oidcpy(&result->blob.oid, &a->oid);
1457 - if (!oideq(&a->oid, &b->oid))
1458 - result->clean = 0;
1459 - break;
1460 - case MERGE_VARIANT_OURS:
1461 - oidcpy(&result->blob.oid, &a->oid);
1462 - break;
1463 - case MERGE_VARIANT_THEIRS:
1464 - oidcpy(&result->blob.oid, &b->oid);
1465 - break;
1466 - }
1467 - } else
1468 - BUG("unsupported object type in the tree");
1469 - }
1470 -
1471 - if (result->merge)
1472 - output(opt, 2, _("Auto-merging %s"), filename);
1473 -
1474 - return 0;
1475 -}
1476 -
1477 -static int handle_rename_via_dir(struct merge_options *opt,
1478 - struct rename_conflict_info *ci)
1479 -{
1480 - /*
1481 - * Handle file adds that need to be renamed due to directory rename
1482 - * detection. This differs from handle_rename_normal, because
1483 - * there is no content merge to do; just move the file into the
1484 - * desired final location.
1485 - */
1486 - const struct rename *ren = ci->ren1;
1487 - const struct diff_filespec *dest = ren->pair->two;
1488 - char *file_path = dest->path;
1489 - int mark_conflicted = (opt->detect_directory_renames ==
1490 - MERGE_DIRECTORY_RENAMES_CONFLICT);
1491 - assert(ren->dir_rename_original_dest);
1492 -
1493 - if (!opt->priv->call_depth && would_lose_untracked(opt, dest->path)) {
1494 - mark_conflicted = 1;
1495 - file_path = unique_path(opt, dest->path, ren->branch);
1496 - output(opt, 1, _("Error: Refusing to lose untracked file at %s; "
1497 - "writing to %s instead."),
1498 - dest->path, file_path);
1499 - }
1500 -
1501 - if (mark_conflicted) {
1502 - /*
1503 - * Write the file in worktree at file_path. In the index,
1504 - * only record the file at dest->path in the appropriate
1505 - * higher stage.
1506 - */
1507 - if (update_file(opt, 0, dest, file_path))
1508 - return -1;
1509 - if (file_path != dest->path)
1510 - free(file_path);
1511 - if (update_stages(opt, dest->path, NULL,
1512 - ren->branch == opt->branch1 ? dest : NULL,
1513 - ren->branch == opt->branch1 ? NULL : dest))
1514 - return -1;
1515 - return 0; /* not clean, but conflicted */
1516 - } else {
1517 - /* Update dest->path both in index and in worktree */
1518 - if (update_file(opt, 1, dest, dest->path))
1519 - return -1;
1520 - return 1; /* clean */
1521 - }
1522 -}
1523 -
1524 -static int handle_change_delete(struct merge_options *opt,
1525 - const char *path, const char *old_path,
1526 - const struct diff_filespec *o,
1527 - const struct diff_filespec *changed,
1528 - const char *change_branch,
1529 - const char *delete_branch,
1530 - const char *change, const char *change_past)
1531 -{
1532 - char *alt_path = NULL;
1533 - const char *update_path = path;
1534 - int ret = 0;
1535 -
1536 - if (dir_in_way(opt->repo->index, path, !opt->priv->call_depth, 0) ||
1537 - (!opt->priv->call_depth && would_lose_untracked(opt, path))) {
1538 - update_path = alt_path = unique_path(opt, path, change_branch);
1539 - }
1540 -
1541 - if (opt->priv->call_depth) {
1542 - /*
1543 - * We cannot arbitrarily accept either a_sha or b_sha as
1544 - * correct; since there is no true "middle point" between
1545 - * them, simply reuse the base version for virtual merge base.
1546 - */
1547 - ret = remove_file_from_index(opt->repo->index, path);
1548 - if (!ret)
1549 - ret = update_file(opt, 0, o, update_path);
1550 - } else {
1551 - /*
1552 - * Despite the four nearly duplicate messages and argument
1553 - * lists below and the ugliness of the nested if-statements,
1554 - * having complete messages makes the job easier for
1555 - * translators.
1556 - *
1557 - * The slight variance among the cases is due to the fact
1558 - * that:
1559 - * 1) directory/file conflicts (in effect if
1560 - * !alt_path) could cause us to need to write the
1561 - * file to a different path.
1562 - * 2) renames (in effect if !old_path) could mean that
1563 - * there are two names for the path that the user
1564 - * may know the file by.
1565 - */
1566 - if (!alt_path) {
1567 - if (!old_path) {
1568 - output(opt, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1569 - "and %s in %s. Version %s of %s left in tree."),
1570 - change, path, delete_branch, change_past,
1571 - change_branch, change_branch, path);
1572 - } else {
1573 - output(opt, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1574 - "and %s to %s in %s. Version %s of %s left in tree."),
1575 - change, old_path, delete_branch, change_past, path,
1576 - change_branch, change_branch, path);
1577 - }
1578 - } else {
1579 - if (!old_path) {
1580 - output(opt, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1581 - "and %s in %s. Version %s of %s left in tree at %s."),
1582 - change, path, delete_branch, change_past,
1583 - change_branch, change_branch, path, alt_path);
1584 - } else {
1585 - output(opt, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1586 - "and %s to %s in %s. Version %s of %s left in tree at %s."),
1587 - change, old_path, delete_branch, change_past, path,
1588 - change_branch, change_branch, path, alt_path);
1589 - }
1590 - }
1591 - /*
1592 - * No need to call update_file() on path when change_branch ==
1593 - * opt->branch1 && !alt_path, since that would needlessly touch
1594 - * path. We could call update_file_flags() with update_cache=0
1595 - * and update_wd=0, but that's a no-op.
1596 - */
1597 - if (change_branch != opt->branch1 || alt_path)
1598 - ret = update_file(opt, 0, changed, update_path);
1599 - }
1600 - free(alt_path);
1601 -
1602 - return ret;
1603 -}
1604 -
1605 -static int handle_rename_delete(struct merge_options *opt,
1606 - struct rename_conflict_info *ci)
1607 -{
1608 - const struct rename *ren = ci->ren1;
1609 - const struct diff_filespec *orig = ren->pair->one;
1610 - const struct diff_filespec *dest = ren->pair->two;
1611 - const char *rename_branch = ren->branch;
1612 - const char *delete_branch = (opt->branch1 == ren->branch ?
1613 - opt->branch2 : opt->branch1);
1614 -
1615 - if (handle_change_delete(opt,
1616 - opt->priv->call_depth ? orig->path : dest->path,
1617 - opt->priv->call_depth ? NULL : orig->path,
1618 - orig, dest,
1619 - rename_branch, delete_branch,
1620 - _("rename"), _("renamed")))
1621 - return -1;
1622 -
1623 - if (opt->priv->call_depth)
1624 - return remove_file_from_index(opt->repo->index, dest->path);
1625 - else
1626 - return update_stages(opt, dest->path, NULL,
1627 - rename_branch == opt->branch1 ? dest : NULL,
1628 - rename_branch == opt->branch1 ? NULL : dest);
1629 -}
1630 -
1631 -static int handle_file_collision(struct merge_options *opt,
1632 - const char *collide_path,
1633 - const char *prev_path1,
1634 - const char *prev_path2,
1635 - const char *branch1, const char *branch2,
1636 - struct diff_filespec *a,
1637 - struct diff_filespec *b)
1638 -{
1639 - struct merge_file_info mfi;
1640 - struct diff_filespec null;
1641 - char *alt_path = NULL;
1642 - const char *update_path = collide_path;
1643 -
1644 - /*
1645 - * It's easiest to get the correct things into stage 2 and 3, and
1646 - * to make sure that the content merge puts HEAD before the other
1647 - * branch if we just ensure that branch1 == opt->branch1. So, simply
1648 - * flip arguments around if we don't have that.
1649 - */
1650 - if (branch1 != opt->branch1) {
1651 - return handle_file_collision(opt, collide_path,
1652 - prev_path2, prev_path1,
1653 - branch2, branch1,
1654 - b, a);
1655 - }
1656 -
1657 - /* Remove rename sources if rename/add or rename/rename(2to1) */
1658 - if (prev_path1)
1659 - remove_file(opt, 1, prev_path1,
1660 - opt->priv->call_depth || would_lose_untracked(opt, prev_path1));
1661 - if (prev_path2)
1662 - remove_file(opt, 1, prev_path2,
1663 - opt->priv->call_depth || would_lose_untracked(opt, prev_path2));
1664 -
1665 - /*
1666 - * Remove the collision path, if it wouldn't cause dirty contents
1667 - * or an untracked file to get lost. We'll either overwrite with
1668 - * merged contents, or just write out to differently named files.
1669 - */
1670 - if (was_dirty(opt, collide_path)) {
1671 - output(opt, 1, _("Refusing to lose dirty file at %s"),
1672 - collide_path);
1673 - update_path = alt_path = unique_path(opt, collide_path, "merged");
1674 - } else if (would_lose_untracked(opt, collide_path)) {
1675 - /*
1676 - * Only way we get here is if both renames were from
1677 - * a directory rename AND user had an untracked file
1678 - * at the location where both files end up after the
1679 - * two directory renames. See testcase 10d of t6043.
1680 - */
1681 - output(opt, 1, _("Refusing to lose untracked file at "
1682 - "%s, even though it's in the way."),
1683 - collide_path);
1684 - update_path = alt_path = unique_path(opt, collide_path, "merged");
1685 - } else {
1686 - /*
1687 - * FIXME: It's possible that the two files are identical
1688 - * and that the current working copy happens to match, in
1689 - * which case we are unnecessarily touching the working
1690 - * tree file. It's not a likely enough scenario that I
1691 - * want to code up the checks for it and a better fix is
1692 - * available if we restructure how unpack_trees() and
1693 - * merge-recursive interoperate anyway, so punting for
1694 - * now...
1695 - */
1696 - remove_file(opt, 0, collide_path, 0);
1697 - }
1698 -
1699 - /* Store things in diff_filespecs for functions that need it */
1700 - null.path = (char *)collide_path;
1701 - oidcpy(&null.oid, null_oid());
1702 - null.mode = 0;
1703 -
1704 - if (merge_mode_and_contents(opt, &null, a, b, collide_path,
1705 - branch1, branch2, opt->priv->call_depth * 2, &mfi))
1706 - return -1;
1707 - mfi.clean &= !alt_path;
1708 - if (update_file(opt, mfi.clean, &mfi.blob, update_path))
1709 - return -1;
1710 - if (!mfi.clean && !opt->priv->call_depth &&
1711 - update_stages(opt, collide_path, NULL, a, b))
1712 - return -1;
1713 - free(alt_path);
1714 - /*
1715 - * FIXME: If both a & b both started with conflicts (only possible
1716 - * if they came from a rename/rename(2to1)), but had IDENTICAL
1717 - * contents including those conflicts, then in the next line we claim
1718 - * it was clean. If someone cares about this case, we should have the
1719 - * caller notify us if we started with conflicts.
1720 - */
1721 - return mfi.clean;
1722 -}
1723 -
1724 -static int handle_rename_add(struct merge_options *opt,
1725 - struct rename_conflict_info *ci)
1726 -{
1727 - /* a was renamed to c, and a separate c was added. */
1728 - struct diff_filespec *a = ci->ren1->pair->one;
1729 - struct diff_filespec *c = ci->ren1->pair->two;
1730 - char *path = c->path;
1731 - char *prev_path_desc;
1732 - struct merge_file_info mfi;
1733 -
1734 - const char *rename_branch = ci->ren1->branch;
1735 - const char *add_branch = (opt->branch1 == rename_branch ?
1736 - opt->branch2 : opt->branch1);
1737 - int other_stage = (ci->ren1->branch == opt->branch1 ? 3 : 2);
1738 -
1739 - output(opt, 1, _("CONFLICT (rename/add): "
1740 - "Rename %s->%s in %s. Added %s in %s"),
1741 - a->path, c->path, rename_branch,
1742 - c->path, add_branch);
1743 -
1744 - prev_path_desc = xstrfmt("version of %s from %s", path, a->path);
1745 - ci->ren1->src_entry->stages[other_stage].path = a->path;
1746 - if (merge_mode_and_contents(opt, a, c,
1747 - &ci->ren1->src_entry->stages[other_stage],
1748 - prev_path_desc,
1749 - opt->branch1, opt->branch2,
1750 - 1 + opt->priv->call_depth * 2, &mfi))
1751 - return -1;
1752 - free(prev_path_desc);
1753 -
1754 - ci->ren1->dst_entry->stages[other_stage].path = mfi.blob.path = c->path;
1755 - return handle_file_collision(opt,
1756 - c->path, a->path, NULL,
1757 - rename_branch, add_branch,
1758 - &mfi.blob,
1759 - &ci->ren1->dst_entry->stages[other_stage]);
1760 -}
1761 -
1762 -static char *find_path_for_conflict(struct merge_options *opt,
1763 - const char *path,
1764 - const char *branch1,
1765 - const char *branch2)
1766 -{
1767 - char *new_path = NULL;
1768 - if (dir_in_way(opt->repo->index, path, !opt->priv->call_depth, 0)) {
1769 - new_path = unique_path(opt, path, branch1);
1770 - output(opt, 1, _("%s is a directory in %s adding "
1771 - "as %s instead"),
1772 - path, branch2, new_path);
1773 - } else if (would_lose_untracked(opt, path)) {
1774 - new_path = unique_path(opt, path, branch1);
1775 - output(opt, 1, _("Refusing to lose untracked file"
1776 - " at %s; adding as %s instead"),
1777 - path, new_path);
1778 - }
1779 -
1780 - return new_path;
1781 -}
1782 -
1783 -/*
1784 - * Toggle the stage number between "ours" and "theirs" (2 and 3).
1785 - */
1786 -static inline int flip_stage(int stage)
1787 -{
1788 - return (2 + 3) - stage;
1789 -}
1790 -
1791 -static int handle_rename_rename_1to2(struct merge_options *opt,
1792 - struct rename_conflict_info *ci)
1793 -{
1794 - /* One file was renamed in both branches, but to different names. */
1795 - struct merge_file_info mfi;
1796 - struct diff_filespec *add;
1797 - struct diff_filespec *o = ci->ren1->pair->one;
1798 - struct diff_filespec *a = ci->ren1->pair->two;
1799 - struct diff_filespec *b = ci->ren2->pair->two;
1800 - char *path_desc;
1801 -
1802 - output(opt, 1, _("CONFLICT (rename/rename): "
1803 - "Rename \"%s\"->\"%s\" in branch \"%s\" "
1804 - "rename \"%s\"->\"%s\" in \"%s\"%s"),
1805 - o->path, a->path, ci->ren1->branch,
1806 - o->path, b->path, ci->ren2->branch,
1807 - opt->priv->call_depth ? _(" (left unresolved)") : "");
1808 -
1809 - path_desc = xstrfmt("%s and %s, both renamed from %s",
1810 - a->path, b->path, o->path);
1811 - if (merge_mode_and_contents(opt, o, a, b, path_desc,
1812 - ci->ren1->branch, ci->ren2->branch,
1813 - opt->priv->call_depth * 2, &mfi))
1814 - return -1;
1815 - free(path_desc);
1816 -
1817 - if (opt->priv->call_depth)
1818 - remove_file_from_index(opt->repo->index, o->path);
1819 -
1820 - /*
1821 - * For each destination path, we need to see if there is a
1822 - * rename/add collision. If not, we can write the file out
1823 - * to the specified location.
1824 - */
1825 - add = &ci->ren1->dst_entry->stages[flip_stage(2)];
1826 - if (is_valid(add)) {
1827 - add->path = mfi.blob.path = a->path;
1828 - if (handle_file_collision(opt, a->path,
1829 - NULL, NULL,
1830 - ci->ren1->branch,
1831 - ci->ren2->branch,
1832 - &mfi.blob, add) < 0)
1833 - return -1;
1834 - } else {
1835 - char *new_path = find_path_for_conflict(opt, a->path,
1836 - ci->ren1->branch,
1837 - ci->ren2->branch);
1838 - if (update_file(opt, 0, &mfi.blob,
1839 - new_path ? new_path : a->path))
1840 - return -1;
1841 - free(new_path);
1842 - if (!opt->priv->call_depth &&
1843 - update_stages(opt, a->path, NULL, a, NULL))
1844 - return -1;
1845 - }
1846 -
1847 - if (!mfi.clean && mfi.blob.mode == a->mode &&
1848 - oideq(&mfi.blob.oid, &a->oid)) {
1849 - /*
1850 - * Getting here means we were attempting to merge a binary
1851 - * blob. Since we can't merge binaries, the merge algorithm
1852 - * just takes one side. But we don't want to copy the
1853 - * contents of one side to both paths; we'd rather use the
1854 - * original content at the given path for each path.
1855 - */
1856 - oidcpy(&mfi.blob.oid, &b->oid);
1857 - mfi.blob.mode = b->mode;
1858 - }
1859 - add = &ci->ren2->dst_entry->stages[flip_stage(3)];
1860 - if (is_valid(add)) {
1861 - add->path = mfi.blob.path = b->path;
1862 - if (handle_file_collision(opt, b->path,
1863 - NULL, NULL,
1864 - ci->ren1->branch,
1865 - ci->ren2->branch,
1866 - add, &mfi.blob) < 0)
1867 - return -1;
1868 - } else {
1869 - char *new_path = find_path_for_conflict(opt, b->path,
1870 - ci->ren2->branch,
1871 - ci->ren1->branch);
1872 - if (update_file(opt, 0, &mfi.blob,
1873 - new_path ? new_path : b->path))
1874 - return -1;
1875 - free(new_path);
1876 - if (!opt->priv->call_depth &&
1877 - update_stages(opt, b->path, NULL, NULL, b))
1878 - return -1;
1879 - }
1880 -
1881 - return 0;
1882 -}
1883 -
1884 -static int handle_rename_rename_2to1(struct merge_options *opt,
1885 - struct rename_conflict_info *ci)
1886 -{
1887 - /* Two files, a & b, were renamed to the same thing, c. */
1888 - struct diff_filespec *a = ci->ren1->pair->one;
1889 - struct diff_filespec *b = ci->ren2->pair->one;
1890 - struct diff_filespec *c1 = ci->ren1->pair->two;
1891 - struct diff_filespec *c2 = ci->ren2->pair->two;
1892 - char *path = c1->path; /* == c2->path */
1893 - char *path_side_1_desc;
1894 - char *path_side_2_desc;
1895 - struct merge_file_info mfi_c1;
1896 - struct merge_file_info mfi_c2;
1897 - int ostage1, ostage2;
1898 -
1899 - output(opt, 1, _("CONFLICT (rename/rename): "
1900 - "Rename %s->%s in %s. "
1901 - "Rename %s->%s in %s"),
1902 - a->path, c1->path, ci->ren1->branch,
1903 - b->path, c2->path, ci->ren2->branch);
1904 -
1905 - path_side_1_desc = xstrfmt("version of %s from %s", path, a->path);
1906 - path_side_2_desc = xstrfmt("version of %s from %s", path, b->path);
1907 - ostage1 = ci->ren1->branch == opt->branch1 ? 3 : 2;
1908 - ostage2 = flip_stage(ostage1);
1909 - ci->ren1->src_entry->stages[ostage1].path = a->path;
1910 - ci->ren2->src_entry->stages[ostage2].path = b->path;
1911 - if (merge_mode_and_contents(opt, a, c1,
1912 - &ci->ren1->src_entry->stages[ostage1],
1913 - path_side_1_desc,
1914 - opt->branch1, opt->branch2,
1915 - 1 + opt->priv->call_depth * 2, &mfi_c1) ||
1916 - merge_mode_and_contents(opt, b,
1917 - &ci->ren2->src_entry->stages[ostage2],
1918 - c2, path_side_2_desc,
1919 - opt->branch1, opt->branch2,
1920 - 1 + opt->priv->call_depth * 2, &mfi_c2))
1921 - return -1;
1922 - free(path_side_1_desc);
1923 - free(path_side_2_desc);
1924 - mfi_c1.blob.path = path;
1925 - mfi_c2.blob.path = path;
1926 -
1927 - return handle_file_collision(opt, path, a->path, b->path,
1928 - ci->ren1->branch, ci->ren2->branch,
1929 - &mfi_c1.blob, &mfi_c2.blob);
1930 -}
1931 -
1932 -/*
1933 - * Get the diff_filepairs changed between o_tree and tree.
1934 - */
1935 -static struct diff_queue_struct *get_diffpairs(struct merge_options *opt,
1936 - struct tree *o_tree,
1937 - struct tree *tree)
1938 -{
1939 - struct diff_queue_struct *ret;
1940 - struct diff_options opts;
1941 -
1942 - repo_diff_setup(opt->repo, &opts);
1943 - opts.flags.recursive = 1;
1944 - opts.flags.rename_empty = 0;
1945 - opts.detect_rename = merge_detect_rename(opt);
1946 - /*
1947 - * We do not have logic to handle the detection of copies. In
1948 - * fact, it may not even make sense to add such logic: would we
1949 - * really want a change to a base file to be propagated through
1950 - * multiple other files by a merge?
1951 - */
1952 - if (opts.detect_rename > DIFF_DETECT_RENAME)
1953 - opts.detect_rename = DIFF_DETECT_RENAME;
1954 - opts.rename_limit = (opt->rename_limit >= 0) ? opt->rename_limit : 7000;
1955 - opts.rename_score = opt->rename_score;
1956 - opts.show_rename_progress = opt->show_rename_progress;
1957 - opts.output_format = DIFF_FORMAT_NO_OUTPUT;
1958 - diff_setup_done(&opts);
1959 - diff_tree_oid(&o_tree->object.oid, &tree->object.oid, "", &opts);
1960 - diffcore_std(&opts);
1961 - if (opts.needed_rename_limit > opt->priv->needed_rename_limit)
1962 - opt->priv->needed_rename_limit = opts.needed_rename_limit;
1963 -
1964 - ret = xmalloc(sizeof(*ret));
1965 - *ret = diff_queued_diff;
1966 -
1967 - opts.output_format = DIFF_FORMAT_NO_OUTPUT;
1968 - diff_queued_diff.nr = 0;
1969 - diff_queued_diff.queue = NULL;
1970 - diff_flush(&opts);
1971 - return ret;
1972 -}
1973 -
1974 -static int tree_has_path(struct repository *r, struct tree *tree,
1975 - const char *path)
1976 -{
1977 - struct object_id hashy;
1978 - unsigned short mode_o;
1979 -
1980 - return !get_tree_entry(r,
1981 - &tree->object.oid, path,
1982 - &hashy, &mode_o);
1983 -}
1984 -
1985 -/*
1986 - * Return a new string that replaces the beginning portion (which matches
1987 - * entry->dir), with entry->new_dir. In perl-speak:
1988 - * new_path_name = (old_path =~ s/entry->dir/entry->new_dir/);
1989 - * NOTE:
1990 - * Caller must ensure that old_path starts with entry->dir + '/'.
1991 - */
1992 -static char *apply_dir_rename(struct dir_rename_entry *entry,
1993 - const char *old_path)
1994 -{
1995 - struct strbuf new_path = STRBUF_INIT;
1996 - int oldlen, newlen;
1997 -
1998 - if (entry->non_unique_new_dir)
1999 - return NULL;
2000 -
2001 - oldlen = strlen(entry->dir);
2002 - if (entry->new_dir.len == 0)
2003 - /*
2004 - * If someone renamed/merged a subdirectory into the root
2005 - * directory (e.g. 'some/subdir' -> ''), then we want to
2006 - * avoid returning
2007 - * '' + '/filename'
2008 - * as the rename; we need to make old_path + oldlen advance
2009 - * past the '/' character.
2010 - */
2011 - oldlen++;
2012 - newlen = entry->new_dir.len + (strlen(old_path) - oldlen) + 1;
2013 - strbuf_grow(&new_path, newlen);
2014 - strbuf_addbuf(&new_path, &entry->new_dir);
2015 - strbuf_addstr(&new_path, &old_path[oldlen]);
2016 -
2017 - return strbuf_detach(&new_path, NULL);
2018 -}
2019 -
2020 -static void get_renamed_dir_portion(const char *old_path, const char *new_path,
2021 - char **old_dir, char **new_dir)
2022 -{
2023 - char *end_of_old, *end_of_new;
2024 -
2025 - /* Default return values: NULL, meaning no rename */
2026 - *old_dir = NULL;
2027 - *new_dir = NULL;
2028 -
2029 - /*
2030 - * For
2031 - * "a/b/c/d/e/foo.c" -> "a/b/some/thing/else/e/foo.c"
2032 - * the "e/foo.c" part is the same, we just want to know that
2033 - * "a/b/c/d" was renamed to "a/b/some/thing/else"
2034 - * so, for this example, this function returns "a/b/c/d" in
2035 - * *old_dir and "a/b/some/thing/else" in *new_dir.
2036 - */
2037 -
2038 - /*
2039 - * If the basename of the file changed, we don't care. We want
2040 - * to know which portion of the directory, if any, changed.
2041 - */
2042 - end_of_old = strrchr(old_path, '/');
2043 - end_of_new = strrchr(new_path, '/');
2044 -
2045 - /*
2046 - * If end_of_old is NULL, old_path wasn't in a directory, so there
2047 - * could not be a directory rename (our rule elsewhere that a
2048 - * directory which still exists is not considered to have been
2049 - * renamed means the root directory can never be renamed -- because
2050 - * the root directory always exists).
2051 - */
2052 - if (!end_of_old)
2053 - return; /* Note: *old_dir and *new_dir are still NULL */
2054 -
2055 - /*
2056 - * If new_path contains no directory (end_of_new is NULL), then we
2057 - * have a rename of old_path's directory to the root directory.
2058 - */
2059 - if (!end_of_new) {
2060 - *old_dir = xstrndup(old_path, end_of_old - old_path);
2061 - *new_dir = xstrdup("");
2062 - return;
2063 - }
2064 -
2065 - /* Find the first non-matching character traversing backwards */
2066 - while (*--end_of_new == *--end_of_old &&
2067 - end_of_old != old_path &&
2068 - end_of_new != new_path)
2069 - ; /* Do nothing; all in the while loop */
2070 -
2071 - /*
2072 - * If both got back to the beginning of their strings, then the
2073 - * directory didn't change at all, only the basename did.
2074 - */
2075 - if (end_of_old == old_path && end_of_new == new_path &&
2076 - *end_of_old == *end_of_new)
2077 - return; /* Note: *old_dir and *new_dir are still NULL */
2078 -
2079 - /*
2080 - * If end_of_new got back to the beginning of its string, and
2081 - * end_of_old got back to the beginning of some subdirectory, then
2082 - * we have a rename/merge of a subdirectory into the root, which
2083 - * needs slightly special handling.
2084 - *
2085 - * Note: There is no need to consider the opposite case, with a
2086 - * rename/merge of the root directory into some subdirectory
2087 - * because as noted above the root directory always exists so it
2088 - * cannot be considered to be renamed.
2089 - */
2090 - if (end_of_new == new_path &&
2091 - end_of_old != old_path && end_of_old[-1] == '/') {
2092 - *old_dir = xstrndup(old_path, --end_of_old - old_path);
2093 - *new_dir = xstrdup("");
2094 - return;
2095 - }
2096 -
2097 - /*
2098 - * We've found the first non-matching character in the directory
2099 - * paths. That means the current characters we were looking at
2100 - * were part of the first non-matching subdir name going back from
2101 - * the end of the strings. Get the whole name by advancing both
2102 - * end_of_old and end_of_new to the NEXT '/' character. That will
2103 - * represent the entire directory rename.
2104 - *
2105 - * The reason for the increment is cases like
2106 - * a/b/star/foo/whatever.c -> a/b/tar/foo/random.c
2107 - * After dropping the basename and going back to the first
2108 - * non-matching character, we're now comparing:
2109 - * a/b/s and a/b/
2110 - * and we want to be comparing:
2111 - * a/b/star/ and a/b/tar/
2112 - * but without the pre-increment, the one on the right would stay
2113 - * a/b/.
2114 - */
2115 - end_of_old = strchr(++end_of_old, '/');
2116 - end_of_new = strchr(++end_of_new, '/');
2117 -
2118 - /* Copy the old and new directories into *old_dir and *new_dir. */
2119 - *old_dir = xstrndup(old_path, end_of_old - old_path);
2120 - *new_dir = xstrndup(new_path, end_of_new - new_path);
2121 -}
2122 -
2123 -static void remove_hashmap_entries(struct hashmap *dir_renames,
2124 - struct string_list *items_to_remove)
2125 -{
2126 - int i;
2127 - struct dir_rename_entry *entry;
2128 -
2129 - for (i = 0; i < items_to_remove->nr; i++) {
2130 - entry = items_to_remove->items[i].util;
2131 - hashmap_remove(dir_renames, &entry->ent, NULL);
2132 - }
2133 - string_list_clear(items_to_remove, 0);
2134 -}
2135 -
2136 -/*
2137 - * See if there is a directory rename for path, and if there are any file
2138 - * level conflicts for the renamed location. If there is a rename and
2139 - * there are no conflicts, return the new name. Otherwise, return NULL.
2140 - */
2141 -static char *handle_path_level_conflicts(struct merge_options *opt,
2142 - const char *path,
2143 - struct dir_rename_entry *entry,
2144 - struct hashmap *collisions,
2145 - struct tree *tree)
2146 -{
2147 - char *new_path = NULL;
2148 - struct collision_entry *collision_ent;
2149 - int clean = 1;
2150 - struct strbuf collision_paths = STRBUF_INIT;
2151 -
2152 - /*
2153 - * entry has the mapping of old directory name to new directory name
2154 - * that we want to apply to path.
2155 - */
2156 - new_path = apply_dir_rename(entry, path);
2157 -
2158 - if (!new_path) {
2159 - /* This should only happen when entry->non_unique_new_dir set */
2160 - if (!entry->non_unique_new_dir)
2161 - BUG("entry->non_unique_new_dir not set and !new_path");
2162 - output(opt, 1, _("CONFLICT (directory rename split): "
2163 - "Unclear where to place %s because directory "
2164 - "%s was renamed to multiple other directories, "
2165 - "with no destination getting a majority of the "
2166 - "files."),
2167 - path, entry->dir);
2168 - clean = 0;
2169 - return NULL;
2170 - }
2171 -
2172 - /*
2173 - * The caller needs to have ensured that it has pre-populated
2174 - * collisions with all paths that map to new_path. Do a quick check
2175 - * to ensure that's the case.
2176 - */
2177 - collision_ent = collision_find_entry(collisions, new_path);
2178 - if (!collision_ent)
2179 - BUG("collision_ent is NULL");
2180 -
2181 - /*
2182 - * Check for one-sided add/add/.../add conflicts, i.e.
2183 - * where implicit renames from the other side doing
2184 - * directory rename(s) can affect this side of history
2185 - * to put multiple paths into the same location. Warn
2186 - * and bail on directory renames for such paths.
2187 - */
2188 - if (collision_ent->reported_already) {
2189 - clean = 0;
2190 - } else if (tree_has_path(opt->repo, tree, new_path)) {
2191 - collision_ent->reported_already = 1;
2192 - strbuf_add_separated_string_list(&collision_paths, ", ",
2193 - &collision_ent->source_files);
2194 - output(opt, 1, _("CONFLICT (implicit dir rename): Existing "
2195 - "file/dir at %s in the way of implicit "
2196 - "directory rename(s) putting the following "
2197 - "path(s) there: %s."),
2198 - new_path, collision_paths.buf);
2199 - clean = 0;
2200 - } else if (collision_ent->source_files.nr > 1) {
2201 - collision_ent->reported_already = 1;
2202 - strbuf_add_separated_string_list(&collision_paths, ", ",
2203 - &collision_ent->source_files);
2204 - output(opt, 1, _("CONFLICT (implicit dir rename): Cannot map "
2205 - "more than one path to %s; implicit directory "
2206 - "renames tried to put these paths there: %s"),
2207 - new_path, collision_paths.buf);
2208 - clean = 0;
2209 - }
2210 -
2211 - /* Free memory we no longer need */
2212 - strbuf_release(&collision_paths);
2213 - if (!clean && new_path) {
2214 - free(new_path);
2215 - return NULL;
2216 - }
2217 -
2218 - return new_path;
2219 -}
2220 -
2221 -/*
2222 - * There are a couple things we want to do at the directory level:
2223 - * 1. Check for both sides renaming to the same thing, in order to avoid
2224 - * implicit renaming of files that should be left in place. (See
2225 - * testcase 6b in t6043 for details.)
2226 - * 2. Prune directory renames if there are still files left in the
2227 - * original directory. These represent a partial directory rename,
2228 - * i.e. a rename where only some of the files within the directory
2229 - * were renamed elsewhere. (Technically, this could be done earlier
2230 - * in get_directory_renames(), except that would prevent us from
2231 - * doing the previous check and thus failing testcase 6b.)
2232 - * 3. Check for rename/rename(1to2) conflicts (at the directory level).
2233 - * In the future, we could potentially record this info as well and
2234 - * omit reporting rename/rename(1to2) conflicts for each path within
2235 - * the affected directories, thus cleaning up the merge output.
2236 - * NOTE: We do NOT check for rename/rename(2to1) conflicts at the
2237 - * directory level, because merging directories is fine. If it
2238 - * causes conflicts for files within those merged directories, then
2239 - * that should be detected at the individual path level.
2240 - */
2241 -static void handle_directory_level_conflicts(struct merge_options *opt,
2242 - struct hashmap *dir_re_head,
2243 - struct tree *head,
2244 - struct hashmap *dir_re_merge,
2245 - struct tree *merge)
2246 -{
2247 - struct hashmap_iter iter;
2248 - struct dir_rename_entry *head_ent;
2249 - struct dir_rename_entry *merge_ent;
2250 -
2251 - struct string_list remove_from_head = STRING_LIST_INIT_NODUP;
2252 - struct string_list remove_from_merge = STRING_LIST_INIT_NODUP;
2253 -
2254 - hashmap_for_each_entry(dir_re_head, &iter, head_ent,
2255 - ent /* member name */) {
2256 - merge_ent = dir_rename_find_entry(dir_re_merge, head_ent->dir);
2257 - if (merge_ent &&
2258 - !head_ent->non_unique_new_dir &&
2259 - !merge_ent->non_unique_new_dir &&
2260 - !strbuf_cmp(&head_ent->new_dir, &merge_ent->new_dir)) {
2261 - /* 1. Renamed identically; remove it from both sides */
2262 - string_list_append(&remove_from_head,
2263 - head_ent->dir)->util = head_ent;
2264 - strbuf_release(&head_ent->new_dir);
2265 - string_list_append(&remove_from_merge,
2266 - merge_ent->dir)->util = merge_ent;
2267 - strbuf_release(&merge_ent->new_dir);
2268 - } else if (tree_has_path(opt->repo, head, head_ent->dir)) {
2269 - /* 2. This wasn't a directory rename after all */
2270 - string_list_append(&remove_from_head,
2271 - head_ent->dir)->util = head_ent;
2272 - strbuf_release(&head_ent->new_dir);
2273 - }
2274 - }
2275 -
2276 - remove_hashmap_entries(dir_re_head, &remove_from_head);
2277 - remove_hashmap_entries(dir_re_merge, &remove_from_merge);
2278 -
2279 - hashmap_for_each_entry(dir_re_merge, &iter, merge_ent,
2280 - ent /* member name */) {
2281 - head_ent = dir_rename_find_entry(dir_re_head, merge_ent->dir);
2282 - if (tree_has_path(opt->repo, merge, merge_ent->dir)) {
2283 - /* 2. This wasn't a directory rename after all */
2284 - string_list_append(&remove_from_merge,
2285 - merge_ent->dir)->util = merge_ent;
2286 - } else if (head_ent &&
2287 - !head_ent->non_unique_new_dir &&
2288 - !merge_ent->non_unique_new_dir) {
2289 - /* 3. rename/rename(1to2) */
2290 - /*
2291 - * We can assume it's not rename/rename(1to1) because
2292 - * that was case (1), already checked above. So we
2293 - * know that head_ent->new_dir and merge_ent->new_dir
2294 - * are different strings.
2295 - */
2296 - output(opt, 1, _("CONFLICT (rename/rename): "
2297 - "Rename directory %s->%s in %s. "
2298 - "Rename directory %s->%s in %s"),
2299 - head_ent->dir, head_ent->new_dir.buf, opt->branch1,
2300 - head_ent->dir, merge_ent->new_dir.buf, opt->branch2);
2301 - string_list_append(&remove_from_head,
2302 - head_ent->dir)->util = head_ent;
2303 - strbuf_release(&head_ent->new_dir);
2304 - string_list_append(&remove_from_merge,
2305 - merge_ent->dir)->util = merge_ent;
2306 - strbuf_release(&merge_ent->new_dir);
2307 - }
2308 - }
2309 -
2310 - remove_hashmap_entries(dir_re_head, &remove_from_head);
2311 - remove_hashmap_entries(dir_re_merge, &remove_from_merge);
2312 -}
2313 -
2314 -static struct hashmap *get_directory_renames(struct diff_queue_struct *pairs)
2315 -{
2316 - struct hashmap *dir_renames;
2317 - struct hashmap_iter iter;
2318 - struct dir_rename_entry *entry;
2319 - int i;
2320 -
2321 - /*
2322 - * Typically, we think of a directory rename as all files from a
2323 - * certain directory being moved to a target directory. However,
2324 - * what if someone first moved two files from the original
2325 - * directory in one commit, and then renamed the directory
2326 - * somewhere else in a later commit? At merge time, we just know
2327 - * that files from the original directory went to two different
2328 - * places, and that the bulk of them ended up in the same place.
2329 - * We want each directory rename to represent where the bulk of the
2330 - * files from that directory end up; this function exists to find
2331 - * where the bulk of the files went.
2332 - *
2333 - * The first loop below simply iterates through the list of file
2334 - * renames, finding out how often each directory rename pair
2335 - * possibility occurs.
2336 - */
2337 - dir_renames = xmalloc(sizeof(*dir_renames));
2338 - dir_rename_init(dir_renames);
2339 - for (i = 0; i < pairs->nr; ++i) {
2340 - struct string_list_item *item;
2341 - int *count;
2342 - struct diff_filepair *pair = pairs->queue[i];
2343 - char *old_dir, *new_dir;
2344 -
2345 - /* File not part of directory rename if it wasn't renamed */
2346 - if (pair->status != 'R')
2347 - continue;
2348 -
2349 - get_renamed_dir_portion(pair->one->path, pair->two->path,
2350 - &old_dir, &new_dir);
2351 - if (!old_dir)
2352 - /* Directory didn't change at all; ignore this one. */
2353 - continue;
2354 -
2355 - entry = dir_rename_find_entry(dir_renames, old_dir);
2356 - if (!entry) {
2357 - entry = xmalloc(sizeof(*entry));
2358 - dir_rename_entry_init(entry, old_dir);
2359 - hashmap_put(dir_renames, &entry->ent);
2360 - } else {
2361 - free(old_dir);
2362 - }
2363 - item = string_list_lookup(&entry->possible_new_dirs, new_dir);
2364 - if (!item) {
2365 - item = string_list_insert(&entry->possible_new_dirs,
2366 - new_dir);
2367 - item->util = xcalloc(1, sizeof(int));
2368 - } else {
2369 - free(new_dir);
2370 - }
2371 - count = item->util;
2372 - *count += 1;
2373 - }
2374 -
2375 - /*
2376 - * For each directory with files moved out of it, we find out which
2377 - * target directory received the most files so we can declare it to
2378 - * be the "winning" target location for the directory rename. This
2379 - * winner gets recorded in new_dir. If there is no winner
2380 - * (multiple target directories received the same number of files),
2381 - * we set non_unique_new_dir. Once we've determined the winner (or
2382 - * that there is no winner), we no longer need possible_new_dirs.
2383 - */
2384 - hashmap_for_each_entry(dir_renames, &iter, entry,
2385 - ent /* member name */) {
2386 - int max = 0;
2387 - int bad_max = 0;
2388 - char *best = NULL;
2389 -
2390 - for (i = 0; i < entry->possible_new_dirs.nr; i++) {
2391 - int *count = entry->possible_new_dirs.items[i].util;
2392 -
2393 - if (*count == max)
2394 - bad_max = max;
2395 - else if (*count > max) {
2396 - max = *count;
2397 - best = entry->possible_new_dirs.items[i].string;
2398 - }
2399 - }
2400 - if (bad_max == max)
2401 - entry->non_unique_new_dir = 1;
2402 - else {
2403 - assert(entry->new_dir.len == 0);
2404 - strbuf_addstr(&entry->new_dir, best);
2405 - }
2406 - /*
2407 - * The relevant directory sub-portion of the original full
2408 - * filepaths were xstrndup'ed before inserting into
2409 - * possible_new_dirs, and instead of manually iterating the
2410 - * list and free'ing each, just lie and tell
2411 - * possible_new_dirs that it did the strdup'ing so that it
2412 - * will free them for us.
2413 - */
2414 - entry->possible_new_dirs.strdup_strings = 1;
2415 - string_list_clear(&entry->possible_new_dirs, 1);
2416 - }
2417 -
2418 - return dir_renames;
2419 -}
2420 -
2421 -static struct dir_rename_entry *check_dir_renamed(const char *path,
2422 - struct hashmap *dir_renames)
2423 -{
2424 - char *temp = xstrdup(path);
2425 - char *end;
2426 - struct dir_rename_entry *entry = NULL;
2427 -
2428 - while ((end = strrchr(temp, '/'))) {
2429 - *end = '\0';
2430 - entry = dir_rename_find_entry(dir_renames, temp);
2431 - if (entry)
2432 - break;
2433 - }
2434 - free(temp);
2435 - return entry;
2436 -}
2437 -
2438 -static void compute_collisions(struct hashmap *collisions,
2439 - struct hashmap *dir_renames,
2440 - struct diff_queue_struct *pairs)
2441 -{
2442 - int i;
2443 -
2444 - /*
2445 - * Multiple files can be mapped to the same path due to directory
2446 - * renames done by the other side of history. Since that other
2447 - * side of history could have merged multiple directories into one,
2448 - * if our side of history added the same file basename to each of
2449 - * those directories, then all N of them would get implicitly
2450 - * renamed by the directory rename detection into the same path,
2451 - * and we'd get an add/add/.../add conflict, and all those adds
2452 - * from *this* side of history. This is not representable in the
2453 - * index, and users aren't going to easily be able to make sense of
2454 - * it. So we need to provide a good warning about what's
2455 - * happening, and fall back to no-directory-rename detection
2456 - * behavior for those paths.
2457 - *
2458 - * See testcases 9e and all of section 5 from t6043 for examples.
2459 - */
2460 - collision_init(collisions);
2461 -
2462 - for (i = 0; i < pairs->nr; ++i) {
2463 - struct dir_rename_entry *dir_rename_ent;
2464 - struct collision_entry *collision_ent;
2465 - char *new_path;
2466 - struct diff_filepair *pair = pairs->queue[i];
2467 -
2468 - if (pair->status != 'A' && pair->status != 'R')
2469 - continue;
2470 - dir_rename_ent = check_dir_renamed(pair->two->path,
2471 - dir_renames);
2472 - if (!dir_rename_ent)
2473 - continue;
2474 -
2475 - new_path = apply_dir_rename(dir_rename_ent, pair->two->path);
2476 - if (!new_path)
2477 - /*
2478 - * dir_rename_ent->non_unique_new_path is true, which
2479 - * means there is no directory rename for us to use,
2480 - * which means it won't cause us any additional
2481 - * collisions.
2482 - */
2483 - continue;
2484 - collision_ent = collision_find_entry(collisions, new_path);
2485 - if (!collision_ent) {
2486 - CALLOC_ARRAY(collision_ent, 1);
2487 - hashmap_entry_init(&collision_ent->ent,
2488 - strhash(new_path));
2489 - hashmap_put(collisions, &collision_ent->ent);
2490 - collision_ent->target_file = new_path;
2491 - } else {
2492 - free(new_path);
2493 - }
2494 - string_list_insert(&collision_ent->source_files,
2495 - pair->two->path);
2496 - }
2497 -}
2498 -
2499 -static char *check_for_directory_rename(struct merge_options *opt,
2500 - const char *path,
2501 - struct tree *tree,
2502 - struct hashmap *dir_renames,
2503 - struct hashmap *dir_rename_exclusions,
2504 - struct hashmap *collisions,
2505 - int *clean_merge)
2506 -{
2507 - char *new_path = NULL;
2508 - struct dir_rename_entry *entry = check_dir_renamed(path, dir_renames);
2509 - struct dir_rename_entry *oentry = NULL;
2510 -
2511 - if (!entry)
2512 - return new_path;
2513 -
2514 - /*
2515 - * This next part is a little weird. We do not want to do an
2516 - * implicit rename into a directory we renamed on our side, because
2517 - * that will result in a spurious rename/rename(1to2) conflict. An
2518 - * example:
2519 - * Base commit: dumbdir/afile, otherdir/bfile
2520 - * Side 1: smrtdir/afile, otherdir/bfile
2521 - * Side 2: dumbdir/afile, dumbdir/bfile
2522 - * Here, while working on Side 1, we could notice that otherdir was
2523 - * renamed/merged to dumbdir, and change the diff_filepair for
2524 - * otherdir/bfile into a rename into dumbdir/bfile. However, Side
2525 - * 2 will notice the rename from dumbdir to smrtdir, and do the
2526 - * transitive rename to move it from dumbdir/bfile to
2527 - * smrtdir/bfile. That gives us bfile in dumbdir vs being in
2528 - * smrtdir, a rename/rename(1to2) conflict. We really just want
2529 - * the file to end up in smrtdir. And the way to achieve that is
2530 - * to not let Side1 do the rename to dumbdir, since we know that is
2531 - * the source of one of our directory renames.
2532 - *
2533 - * That's why oentry and dir_rename_exclusions is here.
2534 - *
2535 - * As it turns out, this also prevents N-way transient rename
2536 - * confusion; See testcases 9c and 9d of t6043.
2537 - */
2538 - oentry = dir_rename_find_entry(dir_rename_exclusions, entry->new_dir.buf);
2539 - if (oentry) {
2540 - output(opt, 1, _("WARNING: Avoiding applying %s -> %s rename "
2541 - "to %s, because %s itself was renamed."),
2542 - entry->dir, entry->new_dir.buf, path, entry->new_dir.buf);
2543 - } else {
2544 - new_path = handle_path_level_conflicts(opt, path, entry,
2545 - collisions, tree);
2546 - *clean_merge &= (new_path != NULL);
2547 - }
2548 -
2549 - return new_path;
2550 -}
2551 -
2552 -static void apply_directory_rename_modifications(struct merge_options *opt,
2553 - struct diff_filepair *pair,
2554 - char *new_path,
2555 - struct rename *re,
2556 - struct tree *tree,
2557 - struct tree *o_tree,
2558 - struct tree *a_tree,
2559 - struct tree *b_tree,
2560 - struct string_list *entries)
2561 -{
2562 - struct string_list_item *item;
2563 - int stage = (tree == a_tree ? 2 : 3);
2564 - int update_wd;
2565 -
2566 - /*
2567 - * In all cases where we can do directory rename detection,
2568 - * unpack_trees() will have read pair->two->path into the
2569 - * index and the working copy. We need to remove it so that
2570 - * we can instead place it at new_path. It is guaranteed to
2571 - * not be untracked (unpack_trees() would have errored out
2572 - * saying the file would have been overwritten), but it might
2573 - * be dirty, though.
2574 - */
2575 - update_wd = !was_dirty(opt, pair->two->path);
2576 - if (!update_wd)
2577 - output(opt, 1, _("Refusing to lose dirty file at %s"),
2578 - pair->two->path);
2579 - remove_file(opt, 1, pair->two->path, !update_wd);
2580 -
2581 - /* Find or create a new re->dst_entry */
2582 - item = string_list_lookup(entries, new_path);
2583 - if (item) {
2584 - /*
2585 - * Since we're renaming on this side of history, and it's
2586 - * due to a directory rename on the other side of history
2587 - * (which we only allow when the directory in question no
2588 - * longer exists on the other side of history), the
2589 - * original entry for re->dst_entry is no longer
2590 - * necessary...
2591 - */
2592 - re->dst_entry->processed = 1;
2593 -
2594 - /*
2595 - * ...because we'll be using this new one.
2596 - */
2597 - re->dst_entry = item->util;
2598 - } else {
2599 - /*
2600 - * re->dst_entry is for the before-dir-rename path, and we
2601 - * need it to hold information for the after-dir-rename
2602 - * path. Before creating a new entry, we need to mark the
2603 - * old one as unnecessary (...unless it is shared by
2604 - * src_entry, i.e. this didn't use to be a rename, in which
2605 - * case we can just allow the normal processing to happen
2606 - * for it).
2607 - */
2608 - if (pair->status == 'R')
2609 - re->dst_entry->processed = 1;
2610 -
2611 - re->dst_entry = insert_stage_data(opt->repo, new_path,
2612 - o_tree, a_tree, b_tree,
2613 - entries);
2614 - item = string_list_insert(entries, new_path);
2615 - item->util = re->dst_entry;
2616 - }
2617 -
2618 - /*
2619 - * Update the stage_data with the information about the path we are
2620 - * moving into place. That slot will be empty and available for us
2621 - * to write to because of the collision checks in
2622 - * handle_path_level_conflicts(). In other words,
2623 - * re->dst_entry->stages[stage].oid will be the null_oid, so it's
2624 - * open for us to write to.
2625 - *
2626 - * It may be tempting to actually update the index at this point as
2627 - * well, using update_stages_for_stage_data(), but as per the big
2628 - * "NOTE" in update_stages(), doing so will modify the current
2629 - * in-memory index which will break calls to would_lose_untracked()
2630 - * that we need to make. Instead, we need to just make sure that
2631 - * the various handle_rename_*() functions update the index
2632 - * explicitly rather than relying on unpack_trees() to have done it.
2633 - */
2634 - get_tree_entry(opt->repo,
2635 - &tree->object.oid,
2636 - pair->two->path,
2637 - &re->dst_entry->stages[stage].oid,
2638 - &re->dst_entry->stages[stage].mode);
2639 -
2640 - /*
2641 - * Record the original change status (or 'type' of change). If it
2642 - * was originally an add ('A'), this lets us differentiate later
2643 - * between a RENAME_DELETE conflict and RENAME_VIA_DIR (they
2644 - * otherwise look the same). If it was originally a rename ('R'),
2645 - * this lets us remember and report accurately about the transitive
2646 - * renaming that occurred via the directory rename detection. Also,
2647 - * record the original destination name.
2648 - */
2649 - re->dir_rename_original_type = pair->status;
2650 - re->dir_rename_original_dest = pair->two->path;
2651 -
2652 - /*
2653 - * We don't actually look at pair->status again, but it seems
2654 - * pedagogically correct to adjust it.
2655 - */
2656 - pair->status = 'R';
2657 -
2658 - /*
2659 - * Finally, record the new location.
2660 - */
2661 - pair->two->path = new_path;
2662 -}
2663 -
2664 -/*
2665 - * Get information of all renames which occurred in 'pairs', making use of
2666 - * any implicit directory renames inferred from the other side of history.
2667 - * We need the three trees in the merge ('o_tree', 'a_tree' and 'b_tree')
2668 - * to be able to associate the correct cache entries with the rename
2669 - * information; tree is always equal to either a_tree or b_tree.
2670 - */
2671 -static struct string_list *get_renames(struct merge_options *opt,
2672 - const char *branch,
2673 - struct diff_queue_struct *pairs,
2674 - struct hashmap *dir_renames,
2675 - struct hashmap *dir_rename_exclusions,
2676 - struct tree *tree,
2677 - struct tree *o_tree,
2678 - struct tree *a_tree,
2679 - struct tree *b_tree,
2680 - struct string_list *entries,
2681 - int *clean_merge)
2682 -{
2683 - int i;
2684 - struct hashmap collisions;
2685 - struct hashmap_iter iter;
2686 - struct collision_entry *e;
2687 - struct string_list *renames;
2688 -
2689 - compute_collisions(&collisions, dir_renames, pairs);
2690 - CALLOC_ARRAY(renames, 1);
2691 -
2692 - for (i = 0; i < pairs->nr; ++i) {
2693 - struct string_list_item *item;
2694 - struct rename *re;
2695 - struct diff_filepair *pair = pairs->queue[i];
2696 - char *new_path; /* non-NULL only with directory renames */
2697 -
2698 - if (pair->status != 'A' && pair->status != 'R') {
2699 - diff_free_filepair(pair);
2700 - continue;
2701 - }
2702 - new_path = check_for_directory_rename(opt, pair->two->path, tree,
2703 - dir_renames,
2704 - dir_rename_exclusions,
2705 - &collisions,
2706 - clean_merge);
2707 - if (pair->status != 'R' && !new_path) {
2708 - diff_free_filepair(pair);
2709 - continue;
2710 - }
2711 -
2712 - re = xmalloc(sizeof(*re));
2713 - re->processed = 0;
2714 - re->pair = pair;
2715 - re->branch = branch;
2716 - re->dir_rename_original_type = '\0';
2717 - re->dir_rename_original_dest = NULL;
2718 - item = string_list_lookup(entries, re->pair->one->path);
2719 - if (!item)
2720 - re->src_entry = insert_stage_data(opt->repo,
2721 - re->pair->one->path,
2722 - o_tree, a_tree, b_tree, entries);
2723 - else
2724 - re->src_entry = item->util;
2725 -
2726 - item = string_list_lookup(entries, re->pair->two->path);
2727 - if (!item)
2728 - re->dst_entry = insert_stage_data(opt->repo,
2729 - re->pair->two->path,
2730 - o_tree, a_tree, b_tree, entries);
2731 - else
2732 - re->dst_entry = item->util;
2733 - item = string_list_insert(renames, pair->one->path);
2734 - item->util = re;
2735 - if (new_path)
2736 - apply_directory_rename_modifications(opt, pair, new_path,
2737 - re, tree, o_tree,
2738 - a_tree, b_tree,
2739 - entries);
2740 - }
2741 -
2742 - hashmap_for_each_entry(&collisions, &iter, e,
2743 - ent /* member name */) {
2744 - free(e->target_file);
2745 - string_list_clear(&e->source_files, 0);
2746 - }
2747 - hashmap_clear_and_free(&collisions, struct collision_entry, ent);
2748 - return renames;
2749 -}
2750 -
2751 -static int process_renames(struct merge_options *opt,
2752 - struct string_list *a_renames,
2753 - struct string_list *b_renames)
2754 -{
2755 - int clean_merge = 1, i, j;
2756 - struct string_list a_by_dst = STRING_LIST_INIT_NODUP;
2757 - struct string_list b_by_dst = STRING_LIST_INIT_NODUP;
2758 - const struct rename *sre;
2759 -
2760 - /*
2761 - * Note that as we build the list, we do not need to check if the
2762 - * existing destination path is already in the list, because the
2763 - * structure of diffcore_rename guarantees we won't have duplicates.
2764 - */
2765 - for (i = 0; i < a_renames->nr; i++) {
2766 - sre = a_renames->items[i].util;
2767 - string_list_append(&a_by_dst, sre->pair->two->path)->util
2768 - = (void *)sre;
2769 - }
2770 - for (i = 0; i < b_renames->nr; i++) {
2771 - sre = b_renames->items[i].util;
2772 - string_list_append(&b_by_dst, sre->pair->two->path)->util
2773 - = (void *)sre;
2774 - }
2775 - string_list_sort(&a_by_dst);
2776 - string_list_sort(&b_by_dst);
2777 -
2778 - for (i = 0, j = 0; i < a_renames->nr || j < b_renames->nr;) {
2779 - struct string_list *renames1, *renames2Dst;
2780 - struct rename *ren1 = NULL, *ren2 = NULL;
2781 - const char *ren1_src, *ren1_dst;
2782 - struct string_list_item *lookup;
2783 -
2784 - if (i >= a_renames->nr) {
2785 - ren2 = b_renames->items[j++].util;
2786 - } else if (j >= b_renames->nr) {
2787 - ren1 = a_renames->items[i++].util;
2788 - } else {
2789 - int compare = strcmp(a_renames->items[i].string,
2790 - b_renames->items[j].string);
2791 - if (compare <= 0)
2792 - ren1 = a_renames->items[i++].util;
2793 - if (compare >= 0)
2794 - ren2 = b_renames->items[j++].util;
2795 - }
2796 -
2797 - /* TODO: refactor, so that 1/2 are not needed */
2798 - if (ren1) {
2799 - renames1 = a_renames;
2800 - renames2Dst = &b_by_dst;
2801 - } else {
2802 - renames1 = b_renames;
2803 - renames2Dst = &a_by_dst;
2804 - SWAP(ren2, ren1);
2805 - }
2806 -
2807 - if (ren1->processed)
2808 - continue;
2809 - ren1->processed = 1;
2810 - ren1->dst_entry->processed = 1;
2811 - /* BUG: We should only mark src_entry as processed if we
2812 - * are not dealing with a rename + add-source case.
2813 - */
2814 - ren1->src_entry->processed = 1;
2815 -
2816 - ren1_src = ren1->pair->one->path;
2817 - ren1_dst = ren1->pair->two->path;
2818 -
2819 - if (ren2) {
2820 - /* One file renamed on both sides */
2821 - const char *ren2_src = ren2->pair->one->path;
2822 - const char *ren2_dst = ren2->pair->two->path;
2823 - enum rename_type rename_type;
2824 - if (strcmp(ren1_src, ren2_src) != 0)
2825 - BUG("ren1_src != ren2_src");
2826 - ren2->dst_entry->processed = 1;
2827 - ren2->processed = 1;
2828 - if (strcmp(ren1_dst, ren2_dst) != 0) {
2829 - rename_type = RENAME_ONE_FILE_TO_TWO;
2830 - clean_merge = 0;
2831 - } else {
2832 - rename_type = RENAME_ONE_FILE_TO_ONE;
2833 - /* BUG: We should only remove ren1_src in
2834 - * the base stage (think of rename +
2835 - * add-source cases).
2836 - */
2837 - remove_file(opt, 1, ren1_src, 1);
2838 - update_entry(ren1->dst_entry,
2839 - ren1->pair->one,
2840 - ren1->pair->two,
2841 - ren2->pair->two);
2842 - }
2843 - setup_rename_conflict_info(rename_type, opt, ren1, ren2);
2844 - } else if ((lookup = string_list_lookup(renames2Dst, ren1_dst))) {
2845 - /* Two different files renamed to the same thing */
2846 - char *ren2_dst;
2847 - ren2 = lookup->util;
2848 - ren2_dst = ren2->pair->two->path;
2849 - if (strcmp(ren1_dst, ren2_dst) != 0)
2850 - BUG("ren1_dst != ren2_dst");
2851 -
2852 - clean_merge = 0;
2853 - ren2->processed = 1;
2854 - /*
2855 - * BUG: We should only mark src_entry as processed
2856 - * if we are not dealing with a rename + add-source
2857 - * case.
2858 - */
2859 - ren2->src_entry->processed = 1;
2860 -
2861 - setup_rename_conflict_info(RENAME_TWO_FILES_TO_ONE,
2862 - opt, ren1, ren2);
2863 - } else {
2864 - /* Renamed in 1, maybe changed in 2 */
2865 - /* we only use sha1 and mode of these */
2866 - struct diff_filespec src_other, dst_other;
2867 - int try_merge;
2868 -
2869 - /*
2870 - * unpack_trees loads entries from common-commit
2871 - * into stage 1, from head-commit into stage 2, and
2872 - * from merge-commit into stage 3. We keep track
2873 - * of which side corresponds to the rename.
2874 - */
2875 - int renamed_stage = a_renames == renames1 ? 2 : 3;
2876 - int other_stage = a_renames == renames1 ? 3 : 2;
2877 -
2878 - /*
2879 - * Directory renames have a funny corner case...
2880 - */
2881 - int renamed_to_self = !strcmp(ren1_src, ren1_dst);
2882 -
2883 - /* BUG: We should only remove ren1_src in the base
2884 - * stage and in other_stage (think of rename +
2885 - * add-source case).
2886 - */
2887 - if (!renamed_to_self)
2888 - remove_file(opt, 1, ren1_src,
2889 - renamed_stage == 2 ||
2890 - !was_tracked(opt, ren1_src));
2891 -
2892 - oidcpy(&src_other.oid,
2893 - &ren1->src_entry->stages[other_stage].oid);
2894 - src_other.mode = ren1->src_entry->stages[other_stage].mode;
2895 - oidcpy(&dst_other.oid,
2896 - &ren1->dst_entry->stages[other_stage].oid);
2897 - dst_other.mode = ren1->dst_entry->stages[other_stage].mode;
2898 - try_merge = 0;
2899 -
2900 - if (oideq(&src_other.oid, null_oid()) &&
2901 - ren1->dir_rename_original_type == 'A') {
2902 - setup_rename_conflict_info(RENAME_VIA_DIR,
2903 - opt, ren1, NULL);
2904 - } else if (renamed_to_self) {
2905 - setup_rename_conflict_info(RENAME_NORMAL,
2906 - opt, ren1, NULL);
2907 - } else if (oideq(&src_other.oid, null_oid())) {
2908 - setup_rename_conflict_info(RENAME_DELETE,
2909 - opt, ren1, NULL);
2910 - } else if ((dst_other.mode == ren1->pair->two->mode) &&
2911 - oideq(&dst_other.oid, &ren1->pair->two->oid)) {
2912 - /*
2913 - * Added file on the other side identical to
2914 - * the file being renamed: clean merge.
2915 - * Also, there is no need to overwrite the
2916 - * file already in the working copy, so call
2917 - * update_file_flags() instead of
2918 - * update_file().
2919 - */
2920 - if (update_file_flags(opt,
2921 - ren1->pair->two,
2922 - ren1_dst,
2923 - 1, /* update_cache */
2924 - 0 /* update_wd */))
2925 - clean_merge = -1;
2926 - } else if (!oideq(&dst_other.oid, null_oid())) {
2927 - /*
2928 - * Probably not a clean merge, but it's
2929 - * premature to set clean_merge to 0 here,
2930 - * because if the rename merges cleanly and
2931 - * the merge exactly matches the newly added
2932 - * file, then the merge will be clean.
2933 - */
2934 - setup_rename_conflict_info(RENAME_ADD,
2935 - opt, ren1, NULL);
2936 - } else
2937 - try_merge = 1;
2938 -
2939 - if (clean_merge < 0)
2940 - goto cleanup_and_return;
2941 - if (try_merge) {
2942 - struct diff_filespec *o, *a, *b;
2943 - src_other.path = (char *)ren1_src;
2944 -
2945 - o = ren1->pair->one;
2946 - if (a_renames == renames1) {
2947 - a = ren1->pair->two;
2948 - b = &src_other;
2949 - } else {
2950 - b = ren1->pair->two;
2951 - a = &src_other;
2952 - }
2953 - update_entry(ren1->dst_entry, o, a, b);
2954 - setup_rename_conflict_info(RENAME_NORMAL,
2955 - opt, ren1, NULL);
2956 - }
2957 - }
2958 - }
2959 -cleanup_and_return:
2960 - string_list_clear(&a_by_dst, 0);
2961 - string_list_clear(&b_by_dst, 0);
2962 -
2963 - return clean_merge;
2964 -}
2965 -
2966 -struct rename_info {
2967 - struct string_list *head_renames;
2968 - struct string_list *merge_renames;
2969 -};
2970 -
2971 -static void initial_cleanup_rename(struct diff_queue_struct *pairs,
2972 - struct hashmap *dir_renames)
2973 -{
2974 - struct hashmap_iter iter;
2975 - struct dir_rename_entry *e;
2976 -
2977 - hashmap_for_each_entry(dir_renames, &iter, e,
2978 - ent /* member name */) {
2979 - free(e->dir);
2980 - strbuf_release(&e->new_dir);
2981 - /* possible_new_dirs already cleared in get_directory_renames */
2982 - }
2983 - hashmap_clear_and_free(dir_renames, struct dir_rename_entry, ent);
2984 - free(dir_renames);
2985 -
2986 - free(pairs->queue);
2987 - free(pairs);
2988 -}
2989 -
2990 -static int detect_and_process_renames(struct merge_options *opt,
2991 - struct tree *common,
2992 - struct tree *head,
2993 - struct tree *merge,
2994 - struct string_list *entries,
2995 - struct rename_info *ri)
2996 -{
2997 - struct diff_queue_struct *head_pairs, *merge_pairs;
2998 - struct hashmap *dir_re_head, *dir_re_merge;
2999 - int clean = 1;
3000 -
3001 - ri->head_renames = NULL;
3002 - ri->merge_renames = NULL;
3003 -
3004 - if (!merge_detect_rename(opt))
3005 - return 1;
3006 -
3007 - head_pairs = get_diffpairs(opt, common, head);
3008 - merge_pairs = get_diffpairs(opt, common, merge);
3009 -
3010 - if ((opt->detect_directory_renames == MERGE_DIRECTORY_RENAMES_TRUE) ||
3011 - (opt->detect_directory_renames == MERGE_DIRECTORY_RENAMES_CONFLICT &&
3012 - !opt->priv->call_depth)) {
3013 - dir_re_head = get_directory_renames(head_pairs);
3014 - dir_re_merge = get_directory_renames(merge_pairs);
3015 -
3016 - handle_directory_level_conflicts(opt,
3017 - dir_re_head, head,
3018 - dir_re_merge, merge);
3019 - } else {
3020 - dir_re_head = xmalloc(sizeof(*dir_re_head));
3021 - dir_re_merge = xmalloc(sizeof(*dir_re_merge));
3022 - dir_rename_init(dir_re_head);
3023 - dir_rename_init(dir_re_merge);
3024 - }
3025 -
3026 - ri->head_renames = get_renames(opt, opt->branch1, head_pairs,
3027 - dir_re_merge, dir_re_head, head,
3028 - common, head, merge, entries,
3029 - &clean);
3030 - if (clean < 0)
3031 - goto cleanup;
3032 - ri->merge_renames = get_renames(opt, opt->branch2, merge_pairs,
3033 - dir_re_head, dir_re_merge, merge,
3034 - common, head, merge, entries,
3035 - &clean);
3036 - if (clean < 0)
3037 - goto cleanup;
3038 - clean &= process_renames(opt, ri->head_renames, ri->merge_renames);
3039 -
3040 -cleanup:
3041 - /*
3042 - * Some cleanup is deferred until cleanup_renames() because the
3043 - * data structures are still needed and referenced in
3044 - * process_entry(). But there are a few things we can free now.
3045 - */
3046 - initial_cleanup_rename(head_pairs, dir_re_head);
3047 - initial_cleanup_rename(merge_pairs, dir_re_merge);
3048 -
3049 - return clean;
3050 -}
3051 -
3052 -static void final_cleanup_rename(struct string_list *rename)
3053 -{
3054 - const struct rename *re;
3055 - int i;
3056 -
3057 - if (!rename)
3058 - return;
3059 -
3060 - for (i = 0; i < rename->nr; i++) {
3061 - re = rename->items[i].util;
3062 - diff_free_filepair(re->pair);
3063 - if (re->src_entry->rename_conflict_info_owned)
3064 - FREE_AND_NULL(re->src_entry->rename_conflict_info);
3065 - if (re->dst_entry->rename_conflict_info_owned)
3066 - FREE_AND_NULL(re->dst_entry->rename_conflict_info);
3067 - }
3068 - string_list_clear(rename, 1);
3069 - free(rename);
3070 -}
3071 -
3072 -static void final_cleanup_renames(struct rename_info *re_info)
3073 -{
3074 - final_cleanup_rename(re_info->head_renames);
3075 - final_cleanup_rename(re_info->merge_renames);
3076 -}
3077 -
3078 -static int read_oid_strbuf(struct merge_options *opt,
3079 - const struct object_id *oid,
3080 - struct strbuf *dst)
3081 -{
3082 - void *buf;
3083 - enum object_type type;
3084 - unsigned long size;
3085 - buf = repo_read_object_file(the_repository, oid, &type, &size);
3086 - if (!buf)
3087 - return err(opt, _("cannot read object %s"), oid_to_hex(oid));
3088 - if (type != OBJ_BLOB) {
3089 - free(buf);
3090 - return err(opt, _("object %s is not a blob"), oid_to_hex(oid));
3091 - }
3092 - strbuf_attach(dst, buf, size, size + 1);
3093 - return 0;
3094 -}
3095 -
3096 -static int blob_unchanged(struct merge_options *opt,
3097 - const struct diff_filespec *o,
3098 - const struct diff_filespec *a,
3099 - int renormalize, const char *path)
3100 -{
3101 - struct strbuf obuf = STRBUF_INIT;
3102 - struct strbuf abuf = STRBUF_INIT;
3103 - int ret = 0; /* assume changed for safety */
3104 - struct index_state *idx = opt->repo->index;
3105 -
3106 - if (a->mode != o->mode)
3107 - return 0;
3108 - if (oideq(&o->oid, &a->oid))
3109 - return 1;
3110 - if (!renormalize)
3111 - return 0;
3112 -
3113 - if (read_oid_strbuf(opt, &o->oid, &obuf) ||
3114 - read_oid_strbuf(opt, &a->oid, &abuf))
3115 - goto error_return;
3116 - /*
3117 - * Note: binary | is used so that both renormalizations are
3118 - * performed. Comparison can be skipped if both files are
3119 - * unchanged since their sha1s have already been compared.
3120 - */
3121 - if (renormalize_buffer(idx, path, obuf.buf, obuf.len, &obuf) |
3122 - renormalize_buffer(idx, path, abuf.buf, abuf.len, &abuf))
3123 - ret = (obuf.len == abuf.len && !memcmp(obuf.buf, abuf.buf, obuf.len));
3124 -
3125 -error_return:
3126 - strbuf_release(&obuf);
3127 - strbuf_release(&abuf);
3128 - return ret;
3129 -}
3130 -
3131 -static int handle_modify_delete(struct merge_options *opt,
3132 - const char *path,
3133 - const struct diff_filespec *o,
3134 - const struct diff_filespec *a,
3135 - const struct diff_filespec *b)
3136 -{
3137 - const char *modify_branch, *delete_branch;
3138 - const struct diff_filespec *changed;
3139 -
3140 - if (is_valid(a)) {
3141 - modify_branch = opt->branch1;
3142 - delete_branch = opt->branch2;
3143 - changed = a;
3144 - } else {
3145 - modify_branch = opt->branch2;
3146 - delete_branch = opt->branch1;
3147 - changed = b;
3148 - }
3149 -
3150 - return handle_change_delete(opt,
3151 - path, NULL,
3152 - o, changed,
3153 - modify_branch, delete_branch,
3154 - _("modify"), _("modified"));
3155 -}
3156 -
3157 -static int handle_content_merge(struct merge_file_info *mfi,
3158 - struct merge_options *opt,
3159 - const char *path,
3160 - int is_dirty,
3161 - const struct diff_filespec *o,
3162 - const struct diff_filespec *a,
3163 - const struct diff_filespec *b,
3164 - struct rename_conflict_info *ci)
3165 -{
3166 - const char *reason = _("content");
3167 - unsigned df_conflict_remains = 0;
3168 -
3169 - if (!is_valid(o))
3170 - reason = _("add/add");
3171 -
3172 - assert(o->path && a->path && b->path);
3173 - if (ci && dir_in_way(opt->repo->index, path, !opt->priv->call_depth,
3174 - S_ISGITLINK(ci->ren1->pair->two->mode)))
3175 - df_conflict_remains = 1;
3176 -
3177 - if (merge_mode_and_contents(opt, o, a, b, path,
3178 - opt->branch1, opt->branch2,
3179 - opt->priv->call_depth * 2, mfi))
3180 - return -1;
3181 -
3182 - /*
3183 - * We can skip updating the working tree file iff:
3184 - * a) The merge is clean
3185 - * b) The merge matches what was in HEAD (content, mode, pathname)
3186 - * c) The target path is usable (i.e. not involved in D/F conflict)
3187 - */
3188 - if (mfi->clean && was_tracked_and_matches(opt, path, &mfi->blob) &&
3189 - !df_conflict_remains) {
3190 - int pos;
3191 - struct cache_entry *ce;
3192 -
3193 - output(opt, 3, _("Skipped %s (merged same as existing)"), path);
3194 - if (add_cacheinfo(opt, &mfi->blob, path,
3195 - 0, (!opt->priv->call_depth && !is_dirty), 0))
3196 - return -1;
3197 - /*
3198 - * However, add_cacheinfo() will delete the old cache entry
3199 - * and add a new one. We need to copy over any skip_worktree
3200 - * flag to avoid making the file appear as if it were
3201 - * deleted by the user.
3202 - */
3203 - pos = index_name_pos(&opt->priv->orig_index, path, strlen(path));
3204 - ce = opt->priv->orig_index.cache[pos];
3205 - if (ce_skip_worktree(ce)) {
3206 - pos = index_name_pos(opt->repo->index, path, strlen(path));
3207 - ce = opt->repo->index->cache[pos];
3208 - ce->ce_flags |= CE_SKIP_WORKTREE;
3209 - }
3210 - return mfi->clean;
3211 - }
3212 -
3213 - if (!mfi->clean) {
3214 - if (S_ISGITLINK(mfi->blob.mode))
3215 - reason = _("submodule");
3216 - output(opt, 1, _("CONFLICT (%s): Merge conflict in %s"),
3217 - reason, path);
3218 - if (ci && !df_conflict_remains)
3219 - if (update_stages(opt, path, o, a, b))
3220 - return -1;
3221 - }
3222 -
3223 - if (df_conflict_remains || is_dirty) {
3224 - char *new_path;
3225 - if (opt->priv->call_depth) {
3226 - remove_file_from_index(opt->repo->index, path);
3227 - } else {
3228 - if (!mfi->clean) {
3229 - if (update_stages(opt, path, o, a, b))
3230 - return -1;
3231 - } else {
3232 - int file_from_stage2 = was_tracked(opt, path);
3233 -
3234 - if (update_stages(opt, path, NULL,
3235 - file_from_stage2 ? &mfi->blob : NULL,
3236 - file_from_stage2 ? NULL : &mfi->blob))
3237 - return -1;
3238 - }
3239 -
3240 - }
3241 - new_path = unique_path(opt, path, ci->ren1->branch);
3242 - if (is_dirty) {
3243 - output(opt, 1, _("Refusing to lose dirty file at %s"),
3244 - path);
3245 - }
3246 - output(opt, 1, _("Adding as %s instead"), new_path);
3247 - if (update_file(opt, 0, &mfi->blob, new_path)) {
3248 - free(new_path);
3249 - return -1;
3250 - }
3251 - free(new_path);
3252 - mfi->clean = 0;
3253 - } else if (update_file(opt, mfi->clean, &mfi->blob, path))
3254 - return -1;
3255 - return !is_dirty && mfi->clean;
3256 -}
3257 -
3258 -static int handle_rename_normal(struct merge_options *opt,
3259 - const char *path,
3260 - const struct diff_filespec *o,
3261 - const struct diff_filespec *a,
3262 - const struct diff_filespec *b,
3263 - struct rename_conflict_info *ci)
3264 -{
3265 - struct rename *ren = ci->ren1;
3266 - struct merge_file_info mfi;
3267 - int clean;
3268 -
3269 - /* Merge the content and write it out */
3270 - clean = handle_content_merge(&mfi, opt, path, was_dirty(opt, path),
3271 - o, a, b, ci);
3272 -
3273 - if (clean &&
3274 - opt->detect_directory_renames == MERGE_DIRECTORY_RENAMES_CONFLICT &&
3275 - ren->dir_rename_original_dest) {
3276 - if (update_stages(opt, path,
3277 - &mfi.blob, &mfi.blob, &mfi.blob))
3278 - return -1;
3279 - clean = 0; /* not clean, but conflicted */
3280 - }
3281 - return clean;
3282 -}
3283 -
3284 -static void dir_rename_warning(const char *msg,
3285 - int is_add,
3286 - int clean,
3287 - struct merge_options *opt,
3288 - struct rename *ren)
3289 -{
3290 - const char *other_branch;
3291 - other_branch = (ren->branch == opt->branch1 ?
3292 - opt->branch2 : opt->branch1);
3293 - if (is_add) {
3294 - output(opt, clean ? 2 : 1, msg,
3295 - ren->pair->one->path, ren->branch,
3296 - other_branch, ren->pair->two->path);
3297 - return;
3298 - }
3299 - output(opt, clean ? 2 : 1, msg,
3300 - ren->pair->one->path, ren->dir_rename_original_dest, ren->branch,
3301 - other_branch, ren->pair->two->path);
3302 -}
3303 -static int warn_about_dir_renamed_entries(struct merge_options *opt,
3304 - struct rename *ren)
3305 -{
3306 - const char *msg;
3307 - int clean = 1, is_add;
3308 -
3309 - if (!ren)
3310 - return clean;
3311 -
3312 - /* Return early if ren was not affected/created by a directory rename */
3313 - if (!ren->dir_rename_original_dest)
3314 - return clean;
3315 -
3316 - /* Sanity checks */
3317 - assert(opt->detect_directory_renames > MERGE_DIRECTORY_RENAMES_NONE);
3318 - assert(ren->dir_rename_original_type == 'A' ||
3319 - ren->dir_rename_original_type == 'R');
3320 -
3321 - /* Check whether to treat directory renames as a conflict */
3322 - clean = (opt->detect_directory_renames == MERGE_DIRECTORY_RENAMES_TRUE);
3323 -
3324 - is_add = (ren->dir_rename_original_type == 'A');
3325 - if (ren->dir_rename_original_type == 'A' && clean) {
3326 - msg = _("Path updated: %s added in %s inside a "
3327 - "directory that was renamed in %s; moving it to %s.");
3328 - } else if (ren->dir_rename_original_type == 'A' && !clean) {
3329 - msg = _("CONFLICT (file location): %s added in %s "
3330 - "inside a directory that was renamed in %s, "
3331 - "suggesting it should perhaps be moved to %s.");
3332 - } else if (ren->dir_rename_original_type == 'R' && clean) {
3333 - msg = _("Path updated: %s renamed to %s in %s, inside a "
3334 - "directory that was renamed in %s; moving it to %s.");
3335 - } else if (ren->dir_rename_original_type == 'R' && !clean) {
3336 - msg = _("CONFLICT (file location): %s renamed to %s in %s, "
3337 - "inside a directory that was renamed in %s, "
3338 - "suggesting it should perhaps be moved to %s.");
3339 - } else {
3340 - BUG("Impossible dir_rename_original_type/clean combination");
3341 - }
3342 - dir_rename_warning(msg, is_add, clean, opt, ren);
3343 -
3344 - return clean;
3345 -}
3346 -
3347 -/* Per entry merge function */
3348 -static int process_entry(struct merge_options *opt,
3349 - const char *path, struct stage_data *entry)
3350 -{
3351 - int clean_merge = 1;
3352 - int normalize = opt->renormalize;
3353 -
3354 - struct diff_filespec *o = &entry->stages[1];
3355 - struct diff_filespec *a = &entry->stages[2];
3356 - struct diff_filespec *b = &entry->stages[3];
3357 - int o_valid = is_valid(o);
3358 - int a_valid = is_valid(a);
3359 - int b_valid = is_valid(b);
3360 - o->path = a->path = b->path = (char*)path;
3361 -
3362 - entry->processed = 1;
3363 - if (entry->rename_conflict_info) {
3364 - struct rename_conflict_info *ci = entry->rename_conflict_info;
3365 - struct diff_filespec *temp;
3366 - int path_clean;
3367 -
3368 - path_clean = warn_about_dir_renamed_entries(opt, ci->ren1);
3369 - path_clean &= warn_about_dir_renamed_entries(opt, ci->ren2);
3370 -
3371 - /*
3372 - * For cases with a single rename, {o,a,b}->path have all been
3373 - * set to the rename target path; we need to set two of these
3374 - * back to the rename source.
3375 - * For rename/rename conflicts, we'll manually fix paths below.
3376 - */
3377 - temp = (opt->branch1 == ci->ren1->branch) ? b : a;
3378 - o->path = temp->path = ci->ren1->pair->one->path;
3379 - if (ci->ren2) {
3380 - assert(opt->branch1 == ci->ren1->branch);
3381 - }
3382 -
3383 - switch (ci->rename_type) {
3384 - case RENAME_NORMAL:
3385 - case RENAME_ONE_FILE_TO_ONE:
3386 - clean_merge = handle_rename_normal(opt, path, o, a, b,
3387 - ci);
3388 - break;
3389 - case RENAME_VIA_DIR:
3390 - clean_merge = handle_rename_via_dir(opt, ci);
3391 - break;
3392 - case RENAME_ADD:
3393 - /*
3394 - * Probably unclean merge, but if the renamed file
3395 - * merges cleanly and the result can then be
3396 - * two-way merged cleanly with the added file, I
3397 - * guess it's a clean merge?
3398 - */
3399 - clean_merge = handle_rename_add(opt, ci);
3400 - break;
3401 - case RENAME_DELETE:
3402 - clean_merge = 0;
3403 - if (handle_rename_delete(opt, ci))
3404 - clean_merge = -1;
3405 - break;
3406 - case RENAME_ONE_FILE_TO_TWO:
3407 - /*
3408 - * Manually fix up paths; note:
3409 - * ren[12]->pair->one->path are equal.
3410 - */
3411 - o->path = ci->ren1->pair->one->path;
3412 - a->path = ci->ren1->pair->two->path;
3413 - b->path = ci->ren2->pair->two->path;
3414 -
3415 - clean_merge = 0;
3416 - if (handle_rename_rename_1to2(opt, ci))
3417 - clean_merge = -1;
3418 - break;
3419 - case RENAME_TWO_FILES_TO_ONE:
3420 - /*
3421 - * Manually fix up paths; note,
3422 - * ren[12]->pair->two->path are actually equal.
3423 - */
3424 - o->path = NULL;
3425 - a->path = ci->ren1->pair->two->path;
3426 - b->path = ci->ren2->pair->two->path;
3427 -
3428 - /*
3429 - * Probably unclean merge, but if the two renamed
3430 - * files merge cleanly and the two resulting files
3431 - * can then be two-way merged cleanly, I guess it's
3432 - * a clean merge?
3433 - */
3434 - clean_merge = handle_rename_rename_2to1(opt, ci);
3435 - break;
3436 - default:
3437 - entry->processed = 0;
3438 - break;
3439 - }
3440 - if (path_clean < clean_merge)
3441 - clean_merge = path_clean;
3442 - } else if (o_valid && (!a_valid || !b_valid)) {
3443 - /* Case A: Deleted in one */
3444 - if ((!a_valid && !b_valid) ||
3445 - (!b_valid && blob_unchanged(opt, o, a, normalize, path)) ||
3446 - (!a_valid && blob_unchanged(opt, o, b, normalize, path))) {
3447 - /* Deleted in both or deleted in one and
3448 - * unchanged in the other */
3449 - if (a_valid)
3450 - output(opt, 2, _("Removing %s"), path);
3451 - /* do not touch working file if it did not exist */
3452 - remove_file(opt, 1, path, !a_valid);
3453 - } else {
3454 - /* Modify/delete; deleted side may have put a directory in the way */
3455 - clean_merge = 0;
3456 - if (handle_modify_delete(opt, path, o, a, b))
3457 - clean_merge = -1;
3458 - }
3459 - } else if ((!o_valid && a_valid && !b_valid) ||
3460 - (!o_valid && !a_valid && b_valid)) {
3461 - /* Case B: Added in one. */
3462 - /* [nothing|directory] -> ([nothing|directory], file) */
3463 -
3464 - const char *add_branch;
3465 - const char *other_branch;
3466 - const char *conf;
3467 - const struct diff_filespec *contents;
3468 -
3469 - if (a_valid) {
3470 - add_branch = opt->branch1;
3471 - other_branch = opt->branch2;
3472 - contents = a;
3473 - conf = _("file/directory");
3474 - } else {
3475 - add_branch = opt->branch2;
3476 - other_branch = opt->branch1;
3477 - contents = b;
3478 - conf = _("directory/file");
3479 - }
3480 - if (dir_in_way(opt->repo->index, path,
3481 - !opt->priv->call_depth && !S_ISGITLINK(a->mode),
3482 - 0)) {
3483 - char *new_path = unique_path(opt, path, add_branch);
3484 - clean_merge = 0;
3485 - output(opt, 1, _("CONFLICT (%s): There is a directory with name %s in %s. "
3486 - "Adding %s as %s"),
3487 - conf, path, other_branch, path, new_path);
3488 - if (update_file(opt, 0, contents, new_path))
3489 - clean_merge = -1;
3490 - else if (opt->priv->call_depth)
3491 - remove_file_from_index(opt->repo->index, path);
3492 - free(new_path);
3493 - } else {
3494 - output(opt, 2, _("Adding %s"), path);
3495 - /* do not overwrite file if already present */
3496 - if (update_file_flags(opt, contents, path, 1, !a_valid))
3497 - clean_merge = -1;
3498 - }
3499 - } else if (a_valid && b_valid) {
3500 - if (!o_valid) {
3501 - /* Case C: Added in both (check for same permissions) */
3502 - output(opt, 1,
3503 - _("CONFLICT (add/add): Merge conflict in %s"),
3504 - path);
3505 - clean_merge = handle_file_collision(opt,
3506 - path, NULL, NULL,
3507 - opt->branch1,
3508 - opt->branch2,
3509 - a, b);
3510 - } else {
3511 - /* case D: Modified in both, but differently. */
3512 - struct merge_file_info mfi;
3513 - int is_dirty = 0; /* unpack_trees would have bailed if dirty */
3514 - clean_merge = handle_content_merge(&mfi, opt, path,
3515 - is_dirty,
3516 - o, a, b, NULL);
3517 - }
3518 - } else if (!o_valid && !a_valid && !b_valid) {
3519 - /*
3520 - * this entry was deleted altogether. a_mode == 0 means
3521 - * we had that path and want to actively remove it.
3522 - */
3523 - remove_file(opt, 1, path, !a->mode);
3524 - } else
3525 - BUG("fatal merge failure, shouldn't happen.");
3526 -
3527 - return clean_merge;
3528 -}
3529 -
3530 -static int merge_trees_internal(struct merge_options *opt,
3531 - struct tree *head,
3532 - struct tree *merge,
3533 - struct tree *merge_base,
3534 - struct tree **result)
3535 -{
3536 - struct index_state *istate = opt->repo->index;
3537 - int code, clean;
3538 -
3539 - if (opt->subtree_shift) {
3540 - merge = shift_tree_object(opt->repo, head, merge,
3541 - opt->subtree_shift);
3542 - merge_base = shift_tree_object(opt->repo, head, merge_base,
3543 - opt->subtree_shift);
3544 - }
3545 -
3546 - if (oideq(&merge_base->object.oid, &merge->object.oid)) {
3547 - output(opt, 0, _("Already up to date."));
3548 - *result = head;
3549 - return 1;
3550 - }
3551 -
3552 - code = unpack_trees_start(opt, merge_base, head, merge);
3553 -
3554 - if (code != 0) {
3555 - if (show(opt, 4) || opt->priv->call_depth)
3556 - err(opt, _("merging of trees %s and %s failed"),
3557 - oid_to_hex(&head->object.oid),
3558 - oid_to_hex(&merge->object.oid));
3559 - unpack_trees_finish(opt);
3560 - return -1;
3561 - }
3562 -
3563 - if (unmerged_index(istate)) {
3564 - struct string_list *entries;
3565 - struct rename_info re_info;
3566 - int i;
3567 - /*
3568 - * Only need the hashmap while processing entries, so
3569 - * initialize it here and free it when we are done running
3570 - * through the entries. Keeping it in the merge_options as
3571 - * opposed to decaring a local hashmap is for convenience
3572 - * so that we don't have to pass it to around.
3573 - */
3574 - hashmap_init(&opt->priv->current_file_dir_set, path_hashmap_cmp,
3575 - NULL, 512);
3576 - get_files_dirs(opt, head);
3577 - get_files_dirs(opt, merge);
3578 -
3579 - entries = get_unmerged(opt->repo->index);
3580 - clean = detect_and_process_renames(opt, merge_base, head, merge,
3581 - entries, &re_info);
3582 - record_df_conflict_files(opt, entries);
3583 - if (clean < 0)
3584 - goto cleanup;
3585 - for (i = entries->nr-1; 0 <= i; i--) {
3586 - const char *path = entries->items[i].string;
3587 - struct stage_data *e = entries->items[i].util;
3588 - if (!e->processed) {
3589 - int ret = process_entry(opt, path, e);
3590 - if (!ret)
3591 - clean = 0;
3592 - else if (ret < 0) {
3593 - clean = ret;
3594 - goto cleanup;
3595 - }
3596 - }
3597 - }
3598 - for (i = 0; i < entries->nr; i++) {
3599 - struct stage_data *e = entries->items[i].util;
3600 - if (!e->processed)
3601 - BUG("unprocessed path??? %s",
3602 - entries->items[i].string);
3603 - }
3604 -
3605 - cleanup:
3606 - final_cleanup_renames(&re_info);
3607 -
3608 - string_list_clear(entries, 1);
3609 - free(entries);
3610 -
3611 - hashmap_clear_and_free(&opt->priv->current_file_dir_set,
3612 - struct path_hashmap_entry, e);
3613 -
3614 - if (clean < 0) {
3615 - unpack_trees_finish(opt);
3616 - return clean;
3617 - }
3618 - }
3619 - else
3620 - clean = 1;
3621 -
3622 - unpack_trees_finish(opt);
3623 -
3624 - if (opt->priv->call_depth &&
3625 - !(*result = write_in_core_index_as_tree(opt->repo)))
3626 - return -1;
3627 -
3628 - return clean;
3629 -}
3630 -
3631 -/*
3632 - * Merge the commits h1 and h2, returning a flag (int) indicating the
3633 - * cleanness of the merge. Also, if opt->priv->call_depth, create a
3634 - * virtual commit and write its location to *result.
3635 - */
3636 -static int merge_recursive_internal(struct merge_options *opt,
3637 - struct commit *h1,
3638 - struct commit *h2,
3639 - const struct commit_list *_merge_bases,
3640 - struct commit **result)
3641 -{
3642 - struct commit_list *merge_bases = copy_commit_list(_merge_bases);
3643 - struct commit_list *iter;
3644 - struct commit *merged_merge_bases;
3645 - struct tree *result_tree;
3646 - const char *ancestor_name;
3647 - struct strbuf merge_base_abbrev = STRBUF_INIT;
3648 - int ret;
3649 -
3650 - if (show(opt, 4)) {
3651 - output(opt, 4, _("Merging:"));
3652 - output_commit_title(opt, h1);
3653 - output_commit_title(opt, h2);
3654 - }
3655 -
3656 - if (!merge_bases) {
3657 - if (repo_get_merge_bases(the_repository, h1, h2,
3658 - &merge_bases) < 0) {
3659 - ret = -1;
3660 - goto out;
3661 - }
3662 - merge_bases = reverse_commit_list(merge_bases);
3663 - }
3664 -
3665 - if (show(opt, 5)) {
3666 - unsigned cnt = commit_list_count(merge_bases);
3667 -
3668 - output(opt, 5, Q_("found %u common ancestor:",
3669 - "found %u common ancestors:", cnt), cnt);
3670 - for (iter = merge_bases; iter; iter = iter->next)
3671 - output_commit_title(opt, iter->item);
3672 - }
3673 -
3674 - merged_merge_bases = pop_commit(&merge_bases);
3675 - if (!merged_merge_bases) {
3676 - /* if there is no common ancestor, use an empty tree */
3677 - struct tree *tree;
3678 -
3679 - tree = lookup_tree(opt->repo, opt->repo->hash_algo->empty_tree);
3680 - merged_merge_bases = make_virtual_commit(opt->repo, tree,
3681 - "ancestor");
3682 - ancestor_name = "empty tree";
3683 - } else if (opt->ancestor && !opt->priv->call_depth) {
3684 - ancestor_name = opt->ancestor;
3685 - } else if (merge_bases) {
3686 - ancestor_name = "merged common ancestors";
3687 - } else {
3688 - strbuf_add_unique_abbrev(&merge_base_abbrev,
3689 - &merged_merge_bases->object.oid,
3690 - DEFAULT_ABBREV);
3691 - ancestor_name = merge_base_abbrev.buf;
3692 - }
3693 -
3694 - for (iter = merge_bases; iter; iter = iter->next) {
3695 - const char *saved_b1, *saved_b2;
3696 - opt->priv->call_depth++;
3697 - /*
3698 - * When the merge fails, the result contains files
3699 - * with conflict markers. The cleanness flag is
3700 - * ignored (unless indicating an error), it was never
3701 - * actually used, as result of merge_trees has always
3702 - * overwritten it: the committed "conflicts" were
3703 - * already resolved.
3704 - */
3705 - discard_index(opt->repo->index);
3706 - saved_b1 = opt->branch1;
3707 - saved_b2 = opt->branch2;
3708 - opt->branch1 = "Temporary merge branch 1";
3709 - opt->branch2 = "Temporary merge branch 2";
3710 - if (merge_recursive_internal(opt, merged_merge_bases, iter->item,
3711 - NULL, &merged_merge_bases) < 0) {
3712 - ret = -1;
3713 - goto out;
3714 - }
3715 - opt->branch1 = saved_b1;
3716 - opt->branch2 = saved_b2;
3717 - opt->priv->call_depth--;
3718 -
3719 - if (!merged_merge_bases) {
3720 - ret = err(opt, _("merge returned no commit"));
3721 - goto out;
3722 - }
3723 - }
3724 -
3725 - /*
3726 - * FIXME: Since merge_recursive_internal() is only ever called by
3727 - * places that ensure the index is loaded first
3728 - * (e.g. builtin/merge.c, rebase/sequencer, etc.), in the common
3729 - * case where the merge base was unique that means when we get here
3730 - * we immediately discard the index and re-read it, which is a
3731 - * complete waste of time. We should only be discarding and
3732 - * re-reading if we were forced to recurse.
3733 - */
3734 - discard_index(opt->repo->index);
3735 - if (!opt->priv->call_depth)
3736 - repo_read_index(opt->repo);
3737 -
3738 - opt->ancestor = ancestor_name;
3739 - ret = merge_trees_internal(opt,
3740 - repo_get_commit_tree(opt->repo, h1),
3741 - repo_get_commit_tree(opt->repo, h2),
3742 - repo_get_commit_tree(opt->repo,
3743 - merged_merge_bases),
3744 - &result_tree);
3745 - opt->ancestor = NULL; /* avoid accidental re-use of opt->ancestor */
3746 - if (ret < 0) {
3747 - flush_output(opt);
3748 - goto out;
3749 - }
3750 -
3751 - if (opt->priv->call_depth) {
3752 - *result = make_virtual_commit(opt->repo, result_tree,
3753 - "merged tree");
3754 - commit_list_insert(h1, &(*result)->parents);
3755 - commit_list_insert(h2, &(*result)->parents->next);
3756 - }
3757 -
3758 -out:
3759 - strbuf_release(&merge_base_abbrev);
3760 - free_commit_list(merge_bases);
3761 - return ret;
3762 -}
3763 -
3764 -static int merge_start(struct merge_options *opt, struct tree *head)
3765 -{
3766 - struct strbuf sb = STRBUF_INIT;
3767 -
3768 - /* Sanity checks on opt */
3769 - assert(opt->repo);
3770 -
3771 - assert(opt->branch1 && opt->branch2);
3772 -
3773 - assert(opt->detect_renames >= -1 &&
3774 - opt->detect_renames <= DIFF_DETECT_COPY);
3775 - assert(opt->detect_directory_renames >= MERGE_DIRECTORY_RENAMES_NONE &&
3776 - opt->detect_directory_renames <= MERGE_DIRECTORY_RENAMES_TRUE);
3777 - assert(opt->rename_limit >= -1);
3778 - assert(opt->rename_score >= 0 && opt->rename_score <= MAX_SCORE);
3779 - assert(opt->show_rename_progress >= 0 && opt->show_rename_progress <= 1);
3780 -
3781 - assert(opt->xdl_opts >= 0);
3782 - assert(opt->recursive_variant >= MERGE_VARIANT_NORMAL &&
3783 - opt->recursive_variant <= MERGE_VARIANT_THEIRS);
3784 -
3785 - assert(opt->verbosity >= 0 && opt->verbosity <= 5);
3786 - assert(opt->buffer_output <= 2);
3787 - assert(opt->obuf.len == 0);
3788 -
3789 - assert(opt->priv == NULL);
3790 -
3791 - /* Not supported; option specific to merge-ort */
3792 - assert(!opt->record_conflict_msgs_as_headers);
3793 - assert(!opt->msg_header_prefix);
3794 -
3795 - /* Sanity check on repo state; index must match head */
3796 - if (repo_index_has_changes(opt->repo, head, &sb)) {
3797 - err(opt, _("Your local changes to the following files would be overwritten by merge:\n %s"),
3798 - sb.buf);
3799 - strbuf_release(&sb);
3800 - return -1;
3801 - }
3802 -
3803 - CALLOC_ARRAY(opt->priv, 1);
3804 - string_list_init_dup(&opt->priv->df_conflict_file_set);
3805 - return 0;
3806 -}
3807 -
3808 -static void merge_finalize(struct merge_options *opt)
3809 -{
3810 - flush_output(opt);
3811 - if (!opt->priv->call_depth && opt->buffer_output < 2)
3812 - strbuf_release(&opt->obuf);
3813 - if (show(opt, 2))
3814 - diff_warn_rename_limit("merge.renamelimit",
3815 - opt->priv->needed_rename_limit, 0);
3816 - hashmap_clear_and_free(&opt->priv->current_file_dir_set,
3817 - struct path_hashmap_entry, e);
3818 - string_list_clear(&opt->priv->df_conflict_file_set, 0);
3819 - FREE_AND_NULL(opt->priv);
3820 -}
3821 -
3822 -int merge_trees(struct merge_options *opt,
3823 - struct tree *head,
3824 - struct tree *merge,
3825 - struct tree *merge_base)
3826 -{
3827 - int clean;
3828 - struct tree *ignored;
3829 -
3830 - assert(opt->ancestor != NULL);
3831 -
3832 - if (merge_start(opt, head))
3833 - return -1;
3834 - clean = merge_trees_internal(opt, head, merge, merge_base, &ignored);
3835 - merge_finalize(opt);
3836 -
3837 - return clean;
3838 -}
3839 -
3840 -int merge_recursive(struct merge_options *opt,
3841 - struct commit *h1,
3842 - struct commit *h2,
3843 - const struct commit_list *merge_bases,
3844 - struct commit **result)
3845 -{
3846 - int clean;
3847 -
3848 - assert(opt->ancestor == NULL ||
3849 - !strcmp(opt->ancestor, "constructed merge base"));
3850 -
3851 - prepare_repo_settings(opt->repo);
3852 - opt->repo->settings.command_requires_full_index = 1;
3853 -
3854 - if (merge_start(opt, repo_get_commit_tree(opt->repo, h1)))
3855 - return -1;
3856 - clean = merge_recursive_internal(opt, h1, h2, merge_bases, result);
3857 - merge_finalize(opt);
3858 -
3859 - return clean;
3860 -}
3861 -
3862 -static struct commit *get_ref(struct repository *repo,
3863 - const struct object_id *oid,
3864 - const char *name)
3865 -{
3866 - struct object *object;
3867 -
3868 - object = deref_tag(repo, parse_object(repo, oid),
3869 - name, strlen(name));
3870 - if (!object)
3871 - return NULL;
3872 - if (object->type == OBJ_TREE)
3873 - return make_virtual_commit(repo, (struct tree*)object, name);
3874 - if (object->type != OBJ_COMMIT)
3875 - return NULL;
3876 - if (repo_parse_commit(repo, (struct commit *)object))
3877 - return NULL;
3878 - return (struct commit *)object;
3879 -}
3880 -
3881 -int merge_recursive_generic(struct merge_options *opt,
3882 - const struct object_id *head,
3883 - const struct object_id *merge,
3884 - int num_merge_bases,
3885 - const struct object_id *merge_bases,
3886 - struct commit **result)
3887 -{
3888 - int clean;
3889 - struct lock_file lock = LOCK_INIT;
3890 - struct commit *head_commit = get_ref(opt->repo, head, opt->branch1);
3891 - struct commit *next_commit = get_ref(opt->repo, merge, opt->branch2);
3892 - struct commit_list *ca = NULL;
3893 -
3894 - if (merge_bases) {
3895 - int i;
3896 - for (i = 0; i < num_merge_bases; ++i) {
3897 - struct commit *base;
3898 - if (!(base = get_ref(opt->repo, &merge_bases[i],
3899 - oid_to_hex(&merge_bases[i]))))
3900 - return err(opt, _("Could not parse object '%s'"),
3901 - oid_to_hex(&merge_bases[i]));
3902 - commit_list_insert(base, &ca);
3903 - }
3904 - if (num_merge_bases == 1)
3905 - opt->ancestor = "constructed merge base";
3906 - }
3907 -
3908 - repo_hold_locked_index(opt->repo, &lock, LOCK_DIE_ON_ERROR);
3909 - clean = merge_recursive(opt, head_commit, next_commit, ca,
3910 - result);
3911 - free_commit_list(ca);
3912 - if (clean < 0) {
3913 - rollback_lock_file(&lock);
3914 - return clean;
3915 - }
3916 -
3917 - if (write_locked_index(opt->repo->index, &lock,
3918 - COMMIT_LOCK | SKIP_IF_UNCHANGED))
3919 - return err(opt, _("Unable to write index."));
3920 -
3921 - return clean ? 0 : 1;
3922 -}
3923 -
3924 -static void merge_recursive_config(struct merge_options *opt, int ui)
3925 -{
3926 - char *value = NULL;
3927 - int renormalize = 0;
3928 - git_config_get_int("merge.verbosity", &opt->verbosity);
3929 - git_config_get_int("diff.renamelimit", &opt->rename_limit);
3930 - git_config_get_int("merge.renamelimit", &opt->rename_limit);
3931 - git_config_get_bool("merge.renormalize", &renormalize);
3932 - opt->renormalize = renormalize;
3933 - if (!git_config_get_string("diff.renames", &value)) {
3934 - opt->detect_renames = git_config_rename("diff.renames", value);
3935 - free(value);
3936 - }
3937 - if (!git_config_get_string("merge.renames", &value)) {
3938 - opt->detect_renames = git_config_rename("merge.renames", value);
3939 - free(value);
3940 - }
3941 - if (!git_config_get_string("merge.directoryrenames", &value)) {
3942 - int boolval = git_parse_maybe_bool(value);
3943 - if (0 <= boolval) {
3944 - opt->detect_directory_renames = boolval ?
3945 - MERGE_DIRECTORY_RENAMES_TRUE :
3946 - MERGE_DIRECTORY_RENAMES_NONE;
3947 - } else if (!strcasecmp(value, "conflict")) {
3948 - opt->detect_directory_renames =
3949 - MERGE_DIRECTORY_RENAMES_CONFLICT;
3950 - } /* avoid erroring on values from future versions of git */
3951 - free(value);
3952 - }
3953 - if (ui) {
3954 - if (!git_config_get_string("diff.algorithm", &value)) {
3955 - long diff_algorithm = parse_algorithm_value(value);
3956 - if (diff_algorithm < 0)
3957 - die(_("unknown value for config '%s': %s"), "diff.algorithm", value);
3958 - opt->xdl_opts = (opt->xdl_opts & ~XDF_DIFF_ALGORITHM_MASK) | diff_algorithm;
3959 - free(value);
3960 - }
3961 - }
3962 - git_config(git_xmerge_config, NULL);
3963 -}
3964 -
3965 -static void init_merge_options(struct merge_options *opt,
3966 - struct repository *repo, int ui)
3967 -{
3968 - const char *merge_verbosity;
3969 - memset(opt, 0, sizeof(struct merge_options));
3970 -
3971 - opt->repo = repo;
3972 -
3973 - opt->detect_renames = -1;
3974 - opt->detect_directory_renames = MERGE_DIRECTORY_RENAMES_CONFLICT;
3975 - opt->rename_limit = -1;
3976 -
3977 - opt->verbosity = 2;
3978 - opt->buffer_output = 1;
3979 - strbuf_init(&opt->obuf, 0);
3980 -
3981 - opt->renormalize = 0;
3982 -
3983 - opt->conflict_style = -1;
3984 - opt->xdl_opts = DIFF_WITH_ALG(opt, HISTOGRAM_DIFF);
3985 -
3986 - merge_recursive_config(opt, ui);
3987 - merge_verbosity = getenv("GIT_MERGE_VERBOSITY");
3988 - if (merge_verbosity)
3989 - opt->verbosity = strtol(merge_verbosity, NULL, 10);
3990 - if (opt->verbosity >= 5)
3991 - opt->buffer_output = 0;
3992 -}
3993 -
3994 -void init_ui_merge_options(struct merge_options *opt,
3995 - struct repository *repo)
3996 -{
3997 - init_merge_options(opt, repo, 1);
3998 -}
3999 -
4000 -void init_basic_merge_options(struct merge_options *opt,
4001 - struct repository *repo)
4002 -{
4003 - init_merge_options(opt, repo, 0);
4004 -}
4005 -
4006 -/*
4007 - * For now, members of merge_options do not need deep copying, but
4008 - * it may change in the future, in which case we would need to update
4009 - * this, and also make a matching change to clear_merge_options() to
4010 - * release the resources held by a copied instance.
4011 - */
4012 -void copy_merge_options(struct merge_options *dst, struct merge_options *src)
4013 -{
4014 - *dst = *src;
4015 -}
4016 -
4017 -void clear_merge_options(struct merge_options *opt UNUSED)
4018 -{
4019 - ; /* no-op as our copy is shallow right now */
4020 -}
4021 -
4022 -int parse_merge_opt(struct merge_options *opt, const char *s)
4023 -{
4024 - const char *arg;
4025 -
4026 - if (!s || !*s)
4027 - return -1;
4028 - if (!strcmp(s, "ours"))
4029 - opt->recursive_variant = MERGE_VARIANT_OURS;
4030 - else if (!strcmp(s, "theirs"))
4031 - opt->recursive_variant = MERGE_VARIANT_THEIRS;
4032 - else if (!strcmp(s, "subtree"))
4033 - opt->subtree_shift = "";
4034 - else if (skip_prefix(s, "subtree=", &arg))
4035 - opt->subtree_shift = arg;
4036 - else if (!strcmp(s, "patience"))
4037 - opt->xdl_opts = DIFF_WITH_ALG(opt, PATIENCE_DIFF);
4038 - else if (!strcmp(s, "histogram"))
4039 - opt->xdl_opts = DIFF_WITH_ALG(opt, HISTOGRAM_DIFF);
4040 - else if (skip_prefix(s, "diff-algorithm=", &arg)) {
4041 - long value = parse_algorithm_value(arg);
4042 - if (value < 0)
4043 - return -1;
4044 - /* clear out previous settings */
4045 - DIFF_XDL_CLR(opt, NEED_MINIMAL);
4046 - opt->xdl_opts &= ~XDF_DIFF_ALGORITHM_MASK;
4047 - opt->xdl_opts |= value;
4048 - }
4049 - else if (!strcmp(s, "ignore-space-change"))
4050 - DIFF_XDL_SET(opt, IGNORE_WHITESPACE_CHANGE);
4051 - else if (!strcmp(s, "ignore-all-space"))
4052 - DIFF_XDL_SET(opt, IGNORE_WHITESPACE);
4053 - else if (!strcmp(s, "ignore-space-at-eol"))
4054 - DIFF_XDL_SET(opt, IGNORE_WHITESPACE_AT_EOL);
4055 - else if (!strcmp(s, "ignore-cr-at-eol"))
4056 - DIFF_XDL_SET(opt, IGNORE_CR_AT_EOL);
4057 - else if (!strcmp(s, "renormalize"))
4058 - opt->renormalize = 1;
4059 - else if (!strcmp(s, "no-renormalize"))
4060 - opt->renormalize = 0;
4061 - else if (!strcmp(s, "no-renames"))
4062 - opt->detect_renames = 0;
4063 - else if (!strcmp(s, "find-renames")) {
4064 - opt->detect_renames = 1;
4065 - opt->rename_score = 0;
4066 - }
4067 - else if (skip_prefix(s, "find-renames=", &arg) ||
4068 - skip_prefix(s, "rename-threshold=", &arg)) {
4069 - if ((opt->rename_score = parse_rename_score(&arg)) == -1 || *arg != 0)
4070 - return -1;
4071 - opt->detect_renames = 1;
4072 - }
4073 - /*
4074 - * Please update $__git_merge_strategy_options in
4075 - * git-completion.bash when you add new options
4076 - */
4077 - else
4078 - return -1;
4079 - return 0;
4080 -}
merge-recursive.h deleted
-132
@@ -1,132 +0,0 @@
1 -#ifndef MERGE_RECURSIVE_H
2 -#define MERGE_RECURSIVE_H
3 -
4 -#include "strbuf.h"
5 -
6 -struct commit;
7 -struct commit_list;
8 -struct object_id;
9 -struct repository;
10 -struct tree;
11 -
12 -struct merge_options_internal;
13 -struct merge_options {
14 - struct repository *repo;
15 -
16 - /* ref names used in console messages and conflict markers */
17 - const char *ancestor;
18 - const char *branch1;
19 - const char *branch2;
20 -
21 - /* rename related options */
22 - int detect_renames;
23 - enum {
24 - MERGE_DIRECTORY_RENAMES_NONE = 0,
25 - MERGE_DIRECTORY_RENAMES_CONFLICT = 1,
26 - MERGE_DIRECTORY_RENAMES_TRUE = 2
27 - } detect_directory_renames;
28 - int rename_limit;
29 - int rename_score;
30 - int show_rename_progress;
31 -
32 - /* xdiff-related options (patience, ignore whitespace, ours/theirs) */
33 - long xdl_opts;
34 - int conflict_style;
35 - enum {
36 - MERGE_VARIANT_NORMAL = 0,
37 - MERGE_VARIANT_OURS,
38 - MERGE_VARIANT_THEIRS
39 - } recursive_variant;
40 -
41 - /* console output related options */
42 - int verbosity;
43 - unsigned buffer_output; /* 1: output at end, 2: keep buffered */
44 - struct strbuf obuf; /* output buffer; if buffer_output == 2, caller
45 - * must handle and call strbuf_release */
46 -
47 - /* miscellaneous control options */
48 - const char *subtree_shift;
49 - unsigned renormalize : 1;
50 - unsigned record_conflict_msgs_as_headers : 1;
51 - const char *msg_header_prefix;
52 -
53 - /* internal fields used by the implementation */
54 - struct merge_options_internal *priv;
55 -};
56 -
57 -/* for use by porcelain commands */
58 -void init_ui_merge_options(struct merge_options *opt, struct repository *repo);
59 -/* for use by plumbing commands */
60 -void init_basic_merge_options(struct merge_options *opt, struct repository *repo);
61 -
62 -void copy_merge_options(struct merge_options *dst, struct merge_options *src);
63 -void clear_merge_options(struct merge_options *opt);
64 -
65 -/* parse the option in s and update the relevant field of opt */
66 -int parse_merge_opt(struct merge_options *opt, const char *s);
67 -
68 -/*
69 - * RETURN VALUES: All the merge_* functions below return a value as follows:
70 - * > 0 Merge was clean
71 - * = 0 Merge had conflicts
72 - * < 0 Merge hit an unexpected and unrecoverable problem (e.g. disk
73 - * full) and aborted merge part-way through.
74 - */
75 -
76 -/*
77 - * rename-detecting three-way merge, no recursion.
78 - *
79 - * Outputs:
80 - * - See RETURN VALUES above
81 - * - opt->repo->index has the new index
82 - * - new index NOT written to disk
83 - * - The working tree is updated with results of the merge
84 - */
85 -int merge_trees(struct merge_options *opt,
86 - struct tree *head,
87 - struct tree *merge,
88 - struct tree *merge_base);
89 -
90 -/*
91 - * merge_recursive is like merge_trees() but with recursive ancestor
92 - * consolidation.
93 - *
94 - * NOTE: empirically, about a decade ago it was determined that with more
95 - * than two merge bases, optimal behavior was found when the
96 - * merge_bases were passed in the order of oldest commit to newest
97 - * commit. Also, merge_bases will be consumed (emptied) so make a
98 - * copy if you need it.
99 - *
100 - * Outputs:
101 - * - See RETURN VALUES above
102 - * - *result is treated as scratch space for temporary recursive merges
103 - * - opt->repo->index has the new index
104 - * - new index NOT written to disk
105 - * - The working tree is updated with results of the merge
106 - */
107 -int merge_recursive(struct merge_options *opt,
108 - struct commit *h1,
109 - struct commit *h2,
110 - const struct commit_list *merge_bases,
111 - struct commit **result);
112 -
113 -/*
114 - * merge_recursive_generic can operate on trees instead of commits, by
115 - * wrapping the trees into virtual commits, and calling merge_recursive().
116 - * It also writes out the in-memory index to disk if the merge is successful.
117 - *
118 - * Outputs:
119 - * - See RETURN VALUES above
120 - * - *result is treated as scratch space for temporary recursive merges
121 - * - opt->repo->index has the new index
122 - * - new index also written to $GIT_INDEX_FILE on disk
123 - * - The working tree is updated with results of the merge
124 - */
125 -int merge_recursive_generic(struct merge_options *opt,
126 - const struct object_id *head,
127 - const struct object_id *merge,
128 - int num_merge_bases,
129 - const struct object_id *merge_bases,
130 - struct commit **result);
131 -
132 -#endif
meson.build
-1
@@ -338,7 +338,6 @@ libgit_sources = [
338 'merge-ll.c',
339 'merge-ort.c',
340 'merge-ort-wrappers.c',
341 - 'merge-recursive.c',
341 'merge.c',
342 'midx.c',
343 'midx-write.c',