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