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