]>
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" | |
b2141fc1 | 7 | #include "config.h" |
1c4b6604 | 8 | #include "advice.h" |
697cc8ef | 9 | #include "lockfile.h" |
9047ebbc MV |
10 | #include "cache-tree.h" |
11 | #include "commit.h" | |
12 | #include "blob.h" | |
13 | #include "builtin.h" | |
14 | #include "tree-walk.h" | |
15 | #include "diff.h" | |
16 | #include "diffcore.h" | |
17 | #include "tag.h" | |
18 | #include "unpack-trees.h" | |
19 | #include "string-list.h" | |
20 | #include "xdiff-interface.h" | |
21 | #include "ll-merge.h" | |
9047ebbc MV |
22 | #include "attr.h" |
23 | #include "merge-recursive.h" | |
9800c0df | 24 | #include "dir.h" |
68d03e4a | 25 | #include "submodule.h" |
9047ebbc | 26 | |
fc65b00d KW |
27 | struct path_hashmap_entry { |
28 | struct hashmap_entry e; | |
29 | char path[FLEX_ARRAY]; | |
30 | }; | |
31 | ||
32 | static int path_hashmap_cmp(const void *cmp_data, | |
33 | const void *entry, | |
34 | const void *entry_or_key, | |
35 | const void *keydata) | |
36 | { | |
37 | const struct path_hashmap_entry *a = entry; | |
38 | const struct path_hashmap_entry *b = entry_or_key; | |
39 | const char *key = keydata; | |
40 | ||
41 | if (ignore_case) | |
42 | return strcasecmp(a->path, key ? key : b->path); | |
43 | else | |
44 | return strcmp(a->path, key ? key : b->path); | |
45 | } | |
46 | ||
47 | static unsigned int path_hash(const char *path) | |
48 | { | |
49 | return ignore_case ? strihash(path) : strhash(path); | |
50 | } | |
51 | ||
bc9204d4 JS |
52 | static void flush_output(struct merge_options *o) |
53 | { | |
f1e2426b | 54 | if (o->buffer_output < 2 && o->obuf.len) { |
bc9204d4 JS |
55 | fputs(o->obuf.buf, stdout); |
56 | strbuf_reset(&o->obuf); | |
57 | } | |
58 | } | |
59 | ||
60 | static int err(struct merge_options *o, const char *err, ...) | |
61 | { | |
62 | va_list params; | |
63 | ||
f1e2426b JS |
64 | if (o->buffer_output < 2) |
65 | flush_output(o); | |
66 | else { | |
67 | strbuf_complete(&o->obuf, '\n'); | |
68 | strbuf_addstr(&o->obuf, "error: "); | |
69 | } | |
bc9204d4 JS |
70 | va_start(params, err); |
71 | strbuf_vaddf(&o->obuf, err, params); | |
72 | va_end(params); | |
f1e2426b JS |
73 | if (o->buffer_output > 1) |
74 | strbuf_addch(&o->obuf, '\n'); | |
75 | else { | |
76 | error("%s", o->obuf.buf); | |
77 | strbuf_reset(&o->obuf); | |
78 | } | |
bc9204d4 JS |
79 | |
80 | return -1; | |
81 | } | |
82 | ||
85e51b78 JH |
83 | static struct tree *shift_tree_object(struct tree *one, struct tree *two, |
84 | const char *subtree_shift) | |
9047ebbc | 85 | { |
f2fd0760 | 86 | struct object_id shifted; |
9047ebbc | 87 | |
85e51b78 | 88 | if (!*subtree_shift) { |
82db3d44 | 89 | shift_tree(&one->object.oid, &two->object.oid, &shifted, 0); |
85e51b78 | 90 | } else { |
82db3d44 | 91 | shift_tree_by(&one->object.oid, &two->object.oid, &shifted, |
85e51b78 JH |
92 | subtree_shift); |
93 | } | |
f2fd0760 | 94 | if (!oidcmp(&two->object.oid, &shifted)) |
9047ebbc | 95 | return two; |
740ee055 | 96 | return lookup_tree(&shifted); |
9047ebbc MV |
97 | } |
98 | ||
2af202be | 99 | static struct commit *make_virtual_commit(struct tree *tree, const char *comment) |
9047ebbc | 100 | { |
10322a0a | 101 | struct commit *commit = alloc_commit_node(); |
ae8e4c9c | 102 | |
a2571653 | 103 | set_merge_remote_desc(commit, comment, (struct object *)commit); |
9047ebbc | 104 | commit->tree = tree; |
9047ebbc MV |
105 | commit->object.parsed = 1; |
106 | return commit; | |
107 | } | |
108 | ||
109 | /* | |
110 | * Since we use get_tree_entry(), which does not put the read object into | |
111 | * the object pool, we cannot rely on a == b. | |
112 | */ | |
b4da9d62 | 113 | static int oid_eq(const struct object_id *a, const struct object_id *b) |
9047ebbc MV |
114 | { |
115 | if (!a && !b) | |
116 | return 2; | |
b4da9d62 | 117 | return a && b && oidcmp(a, b) == 0; |
9047ebbc MV |
118 | } |
119 | ||
25c39363 EN |
120 | enum rename_type { |
121 | RENAME_NORMAL = 0, | |
122 | RENAME_DELETE, | |
4f66dade | 123 | RENAME_ONE_FILE_TO_ONE, |
461f5041 EN |
124 | RENAME_ONE_FILE_TO_TWO, |
125 | RENAME_TWO_FILES_TO_ONE | |
25c39363 EN |
126 | }; |
127 | ||
4f66dade | 128 | struct rename_conflict_info { |
25c39363 EN |
129 | enum rename_type rename_type; |
130 | struct diff_filepair *pair1; | |
131 | struct diff_filepair *pair2; | |
132 | const char *branch1; | |
133 | const char *branch2; | |
134 | struct stage_data *dst_entry1; | |
135 | struct stage_data *dst_entry2; | |
232c635f EN |
136 | struct diff_filespec ren1_other; |
137 | struct diff_filespec ren2_other; | |
25c39363 EN |
138 | }; |
139 | ||
9047ebbc MV |
140 | /* |
141 | * Since we want to write the index eventually, we cannot reuse the index | |
142 | * for these (temporary) data. | |
143 | */ | |
9cba13ca JN |
144 | struct stage_data { |
145 | struct { | |
9047ebbc | 146 | unsigned mode; |
fd429e98 | 147 | struct object_id oid; |
9047ebbc | 148 | } stages[4]; |
4f66dade | 149 | struct rename_conflict_info *rename_conflict_info; |
9047ebbc MV |
150 | unsigned processed:1; |
151 | }; | |
152 | ||
4f66dade EN |
153 | static inline void setup_rename_conflict_info(enum rename_type rename_type, |
154 | struct diff_filepair *pair1, | |
155 | struct diff_filepair *pair2, | |
156 | const char *branch1, | |
157 | const char *branch2, | |
158 | struct stage_data *dst_entry1, | |
232c635f EN |
159 | struct stage_data *dst_entry2, |
160 | struct merge_options *o, | |
161 | struct stage_data *src_entry1, | |
162 | struct stage_data *src_entry2) | |
25c39363 | 163 | { |
4f66dade | 164 | struct rename_conflict_info *ci = xcalloc(1, sizeof(struct rename_conflict_info)); |
25c39363 EN |
165 | ci->rename_type = rename_type; |
166 | ci->pair1 = pair1; | |
167 | ci->branch1 = branch1; | |
168 | ci->branch2 = branch2; | |
169 | ||
170 | ci->dst_entry1 = dst_entry1; | |
4f66dade | 171 | dst_entry1->rename_conflict_info = ci; |
25c39363 EN |
172 | dst_entry1->processed = 0; |
173 | ||
174 | assert(!pair2 == !dst_entry2); | |
175 | if (dst_entry2) { | |
176 | ci->dst_entry2 = dst_entry2; | |
177 | ci->pair2 = pair2; | |
4f66dade | 178 | dst_entry2->rename_conflict_info = ci; |
25c39363 | 179 | } |
232c635f EN |
180 | |
181 | if (rename_type == RENAME_TWO_FILES_TO_ONE) { | |
182 | /* | |
183 | * For each rename, there could have been | |
184 | * modifications on the side of history where that | |
185 | * file was not renamed. | |
186 | */ | |
187 | int ostage1 = o->branch1 == branch1 ? 3 : 2; | |
188 | int ostage2 = ostage1 ^ 1; | |
189 | ||
190 | ci->ren1_other.path = pair1->one->path; | |
fd429e98 | 191 | oidcpy(&ci->ren1_other.oid, &src_entry1->stages[ostage1].oid); |
232c635f EN |
192 | ci->ren1_other.mode = src_entry1->stages[ostage1].mode; |
193 | ||
194 | ci->ren2_other.path = pair2->one->path; | |
fd429e98 | 195 | oidcpy(&ci->ren2_other.oid, &src_entry2->stages[ostage2].oid); |
232c635f | 196 | ci->ren2_other.mode = src_entry2->stages[ostage2].mode; |
25c39363 EN |
197 | } |
198 | } | |
199 | ||
8a2fce18 | 200 | static int show(struct merge_options *o, int v) |
9047ebbc | 201 | { |
5033639c | 202 | return (!o->call_depth && o->verbosity >= v) || o->verbosity >= 5; |
9047ebbc MV |
203 | } |
204 | ||
28bea9e5 | 205 | __attribute__((format (printf, 3, 4))) |
8a2fce18 | 206 | static void output(struct merge_options *o, int v, const char *fmt, ...) |
9047ebbc | 207 | { |
9047ebbc MV |
208 | va_list ap; |
209 | ||
8a2fce18 | 210 | if (!show(o, v)) |
9047ebbc MV |
211 | return; |
212 | ||
415792ed | 213 | strbuf_addchars(&o->obuf, ' ', o->call_depth * 2); |
9047ebbc MV |
214 | |
215 | va_start(ap, fmt); | |
ebeb6090 | 216 | strbuf_vaddf(&o->obuf, fmt, ap); |
9047ebbc MV |
217 | va_end(ap); |
218 | ||
294b2680 | 219 | strbuf_addch(&o->obuf, '\n'); |
8a2fce18 | 220 | if (!o->buffer_output) |
c7d84924 | 221 | flush_output(o); |
9047ebbc MV |
222 | } |
223 | ||
5033639c | 224 | static void output_commit_title(struct merge_options *o, struct commit *commit) |
9047ebbc | 225 | { |
dde75cb0 | 226 | strbuf_addchars(&o->obuf, ' ', o->call_depth * 2); |
9047ebbc | 227 | if (commit->util) |
dde75cb0 JS |
228 | strbuf_addf(&o->obuf, "virtual %s\n", |
229 | merge_remote_util(commit)->name); | |
9047ebbc | 230 | else { |
30e677e0 | 231 | strbuf_add_unique_abbrev(&o->obuf, &commit->object.oid, |
a94bb683 RS |
232 | DEFAULT_ABBREV); |
233 | strbuf_addch(&o->obuf, ' '); | |
9047ebbc | 234 | if (parse_commit(commit) != 0) |
a22ae753 | 235 | strbuf_addstr(&o->obuf, _("(bad commit)\n")); |
9047ebbc | 236 | else { |
49b7120e | 237 | const char *title; |
8597ea3a | 238 | const char *msg = get_commit_buffer(commit, NULL); |
bc6b8fc1 | 239 | int len = find_commit_subject(msg, &title); |
49b7120e | 240 | if (len) |
dde75cb0 | 241 | strbuf_addf(&o->obuf, "%.*s\n", len, title); |
bc6b8fc1 | 242 | unuse_commit_buffer(commit, msg); |
9047ebbc MV |
243 | } |
244 | } | |
dde75cb0 | 245 | flush_output(o); |
9047ebbc MV |
246 | } |
247 | ||
bc9204d4 JS |
248 | static int add_cacheinfo(struct merge_options *o, |
249 | unsigned int mode, const struct object_id *oid, | |
9047ebbc MV |
250 | const char *path, int stage, int refresh, int options) |
251 | { | |
252 | struct cache_entry *ce; | |
1335d76e JH |
253 | int ret; |
254 | ||
21bed620 | 255 | ce = make_cache_entry(mode, oid ? oid->hash : null_sha1, path, stage, 0); |
9047ebbc | 256 | if (!ce) |
bc9204d4 | 257 | return err(o, _("addinfo_cache failed for path '%s'"), path); |
1335d76e JH |
258 | |
259 | ret = add_cache_entry(ce, options); | |
260 | if (refresh) { | |
261 | struct cache_entry *nce; | |
262 | ||
263 | nce = refresh_cache_entry(ce, CE_MATCH_REFRESH | CE_MATCH_IGNORE_MISSING); | |
55e9f0e5 | 264 | if (!nce) |
1749053d | 265 | return err(o, _("addinfo_cache failed for path '%s'"), path); |
1335d76e JH |
266 | if (nce != ce) |
267 | ret = add_cache_entry(nce, options); | |
268 | } | |
269 | return ret; | |
9047ebbc MV |
270 | } |
271 | ||
9047ebbc MV |
272 | static void init_tree_desc_from_tree(struct tree_desc *desc, struct tree *tree) |
273 | { | |
274 | parse_tree(tree); | |
275 | init_tree_desc(desc, tree->buffer, tree->size); | |
276 | } | |
277 | ||
8b026eda | 278 | static int git_merge_trees(int index_only, |
9047ebbc MV |
279 | struct tree *common, |
280 | struct tree *head, | |
281 | struct tree *merge) | |
282 | { | |
283 | int rc; | |
284 | struct tree_desc t[3]; | |
8b026eda | 285 | struct unpack_trees_options opts; |
9047ebbc | 286 | |
8b026eda JH |
287 | memset(&opts, 0, sizeof(opts)); |
288 | if (index_only) | |
289 | opts.index_only = 1; | |
9047ebbc | 290 | else |
8b026eda JH |
291 | opts.update = 1; |
292 | opts.merge = 1; | |
293 | opts.head_idx = 2; | |
294 | opts.fn = threeway_merge; | |
295 | opts.src_index = &the_index; | |
296 | opts.dst_index = &the_index; | |
297 | setup_unpack_trees_porcelain(&opts, "merge"); | |
9047ebbc MV |
298 | |
299 | init_tree_desc_from_tree(t+0, common); | |
300 | init_tree_desc_from_tree(t+1, head); | |
301 | init_tree_desc_from_tree(t+2, merge); | |
302 | ||
8b026eda | 303 | rc = unpack_trees(3, t, &opts); |
9047ebbc MV |
304 | cache_tree_free(&active_cache_tree); |
305 | return rc; | |
306 | } | |
307 | ||
8a2fce18 | 308 | struct tree *write_tree_from_memory(struct merge_options *o) |
9047ebbc MV |
309 | { |
310 | struct tree *result = NULL; | |
311 | ||
312 | if (unmerged_cache()) { | |
313 | int i; | |
19c6a4f8 | 314 | fprintf(stderr, "BUG: There are unmerged index entries:\n"); |
9047ebbc | 315 | for (i = 0; i < active_nr; i++) { |
9c5e6c80 | 316 | const struct cache_entry *ce = active_cache[i]; |
9047ebbc | 317 | if (ce_stage(ce)) |
c43ba42e | 318 | fprintf(stderr, "BUG: %d %.*s\n", ce_stage(ce), |
19c6a4f8 | 319 | (int)ce_namelen(ce), ce->name); |
9047ebbc | 320 | } |
ef1177d1 | 321 | die("BUG: unmerged index entries in merge-recursive.c"); |
9047ebbc MV |
322 | } |
323 | ||
324 | if (!active_cache_tree) | |
325 | active_cache_tree = cache_tree(); | |
326 | ||
327 | if (!cache_tree_fully_valid(active_cache_tree) && | |
6003303a | 328 | cache_tree_update(&the_index, 0) < 0) { |
bc9204d4 | 329 | err(o, _("error building trees")); |
6003303a JS |
330 | return NULL; |
331 | } | |
9047ebbc | 332 | |
740ee055 | 333 | result = lookup_tree(&active_cache_tree->oid); |
9047ebbc MV |
334 | |
335 | return result; | |
336 | } | |
337 | ||
df46d77e | 338 | static int save_files_dirs(const struct object_id *oid, |
6a0b0b6d | 339 | struct strbuf *base, const char *path, |
9047ebbc MV |
340 | unsigned int mode, int stage, void *context) |
341 | { | |
fc65b00d | 342 | struct path_hashmap_entry *entry; |
6a0b0b6d | 343 | int baselen = base->len; |
696ee23c MV |
344 | struct merge_options *o = context; |
345 | ||
6a0b0b6d | 346 | strbuf_addstr(base, path); |
9047ebbc | 347 | |
fc65b00d KW |
348 | FLEX_ALLOC_MEM(entry, path, base->buf, base->len); |
349 | hashmap_entry_init(entry, path_hash(entry->path)); | |
350 | hashmap_add(&o->current_file_dir_set, entry); | |
9047ebbc | 351 | |
6a0b0b6d | 352 | strbuf_setlen(base, baselen); |
d3bee161 | 353 | return (S_ISDIR(mode) ? READ_TREE_RECURSIVE : 0); |
9047ebbc MV |
354 | } |
355 | ||
ef9c4dc3 | 356 | static void get_files_dirs(struct merge_options *o, struct tree *tree) |
9047ebbc | 357 | { |
f0096c06 | 358 | struct pathspec match_all; |
9a087274 | 359 | memset(&match_all, 0, sizeof(match_all)); |
ef9c4dc3 | 360 | read_tree_recursive(tree, "", 0, 0, &match_all, save_files_dirs, o); |
9047ebbc MV |
361 | } |
362 | ||
363 | /* | |
364 | * Returns an index_entry instance which doesn't have to correspond to | |
365 | * a real cache entry in Git's index. | |
366 | */ | |
367 | static struct stage_data *insert_stage_data(const char *path, | |
368 | struct tree *o, struct tree *a, struct tree *b, | |
369 | struct string_list *entries) | |
370 | { | |
371 | struct string_list_item *item; | |
372 | struct stage_data *e = xcalloc(1, sizeof(struct stage_data)); | |
8b026eda JH |
373 | get_tree_entry(&o->object.oid, path, |
374 | &e->stages[1].oid, &e->stages[1].mode); | |
375 | get_tree_entry(&a->object.oid, path, | |
376 | &e->stages[2].oid, &e->stages[2].mode); | |
377 | get_tree_entry(&b->object.oid, path, | |
378 | &e->stages[3].oid, &e->stages[3].mode); | |
78a395d3 | 379 | item = string_list_insert(entries, path); |
9047ebbc MV |
380 | item->util = e; |
381 | return e; | |
382 | } | |
383 | ||
384 | /* | |
385 | * Create a dictionary mapping file names to stage_data objects. The | |
386 | * dictionary contains one entry for every path with a non-zero stage entry. | |
387 | */ | |
388 | static struct string_list *get_unmerged(void) | |
389 | { | |
390 | struct string_list *unmerged = xcalloc(1, sizeof(struct string_list)); | |
391 | int i; | |
392 | ||
393 | unmerged->strdup_strings = 1; | |
394 | ||
395 | for (i = 0; i < active_nr; i++) { | |
396 | struct string_list_item *item; | |
397 | struct stage_data *e; | |
9c5e6c80 | 398 | const struct cache_entry *ce = active_cache[i]; |
9047ebbc MV |
399 | if (!ce_stage(ce)) |
400 | continue; | |
401 | ||
e8c8b713 | 402 | item = string_list_lookup(unmerged, ce->name); |
9047ebbc | 403 | if (!item) { |
78a395d3 | 404 | item = string_list_insert(unmerged, ce->name); |
9047ebbc MV |
405 | item->util = xcalloc(1, sizeof(struct stage_data)); |
406 | } | |
407 | e = item->util; | |
408 | e->stages[ce_stage(ce)].mode = ce->ce_mode; | |
99d1a986 | 409 | oidcpy(&e->stages[ce_stage(ce)].oid, &ce->oid); |
9047ebbc MV |
410 | } |
411 | ||
412 | return unmerged; | |
413 | } | |
414 | ||
fa6ca111 | 415 | static int string_list_df_name_compare(const char *one, const char *two) |
ef02b317 | 416 | { |
fa6ca111 NTND |
417 | int onelen = strlen(one); |
418 | int twolen = strlen(two); | |
f0fd4d05 EN |
419 | /* |
420 | * Here we only care that entries for D/F conflicts are | |
421 | * adjacent, in particular with the file of the D/F conflict | |
422 | * appearing before files below the corresponding directory. | |
423 | * The order of the rest of the list is irrelevant for us. | |
ef02b317 | 424 | * |
f0fd4d05 EN |
425 | * To achieve this, we sort with df_name_compare and provide |
426 | * the mode S_IFDIR so that D/F conflicts will sort correctly. | |
427 | * We use the mode S_IFDIR for everything else for simplicity, | |
428 | * since in other cases any changes in their order due to | |
429 | * sorting cause no problems for us. | |
430 | */ | |
fa6ca111 NTND |
431 | int cmp = df_name_compare(one, onelen, S_IFDIR, |
432 | two, twolen, S_IFDIR); | |
f0fd4d05 EN |
433 | /* |
434 | * Now that 'foo' and 'foo/bar' compare equal, we have to make sure | |
435 | * that 'foo' comes before 'foo/bar'. | |
ef02b317 | 436 | */ |
f0fd4d05 EN |
437 | if (cmp) |
438 | return cmp; | |
439 | return onelen - twolen; | |
440 | } | |
441 | ||
70cc3d36 EN |
442 | static void record_df_conflict_files(struct merge_options *o, |
443 | struct string_list *entries) | |
ef02b317 | 444 | { |
70cc3d36 | 445 | /* If there is a D/F conflict and the file for such a conflict |
f7d650c0 | 446 | * currently exist in the working tree, we want to allow it to be |
edd2faf5 EN |
447 | * removed to make room for the corresponding directory if needed. |
448 | * The files underneath the directories of such D/F conflicts will | |
449 | * be processed before the corresponding file involved in the D/F | |
450 | * conflict. If the D/F directory ends up being removed by the | |
451 | * merge, then we won't have to touch the D/F file. If the D/F | |
452 | * directory needs to be written to the working copy, then the D/F | |
453 | * file will simply be removed (in make_room_for_path()) to make | |
454 | * room for the necessary paths. Note that if both the directory | |
455 | * and the file need to be present, then the D/F file will be | |
456 | * reinstated with a new unique name at the time it is processed. | |
ef02b317 | 457 | */ |
af4941d4 | 458 | struct string_list df_sorted_entries = STRING_LIST_INIT_NODUP; |
ef02b317 | 459 | const char *last_file = NULL; |
c8516500 | 460 | int last_len = 0; |
ef02b317 EN |
461 | int i; |
462 | ||
0b30e812 EN |
463 | /* |
464 | * If we're merging merge-bases, we don't want to bother with | |
465 | * any working directory changes. | |
466 | */ | |
467 | if (o->call_depth) | |
468 | return; | |
469 | ||
f0fd4d05 | 470 | /* Ensure D/F conflicts are adjacent in the entries list. */ |
ef02b317 | 471 | for (i = 0; i < entries->nr; i++) { |
f701aae0 EN |
472 | struct string_list_item *next = &entries->items[i]; |
473 | string_list_append(&df_sorted_entries, next->string)->util = | |
474 | next->util; | |
475 | } | |
fa6ca111 NTND |
476 | df_sorted_entries.cmp = string_list_df_name_compare; |
477 | string_list_sort(&df_sorted_entries); | |
f0fd4d05 | 478 | |
70cc3d36 | 479 | string_list_clear(&o->df_conflict_file_set, 1); |
f701aae0 EN |
480 | for (i = 0; i < df_sorted_entries.nr; i++) { |
481 | const char *path = df_sorted_entries.items[i].string; | |
ef02b317 | 482 | int len = strlen(path); |
f701aae0 | 483 | struct stage_data *e = df_sorted_entries.items[i].util; |
ef02b317 EN |
484 | |
485 | /* | |
486 | * Check if last_file & path correspond to a D/F conflict; | |
487 | * i.e. whether path is last_file+'/'+<something>. | |
70cc3d36 EN |
488 | * If so, record that it's okay to remove last_file to make |
489 | * room for path and friends if needed. | |
ef02b317 EN |
490 | */ |
491 | if (last_file && | |
492 | len > last_len && | |
493 | memcmp(path, last_file, last_len) == 0 && | |
494 | path[last_len] == '/') { | |
70cc3d36 | 495 | string_list_insert(&o->df_conflict_file_set, last_file); |
ef02b317 EN |
496 | } |
497 | ||
498 | /* | |
499 | * Determine whether path could exist as a file in the | |
500 | * working directory as a possible D/F conflict. This | |
501 | * will only occur when it exists in stage 2 as a | |
502 | * file. | |
503 | */ | |
504 | if (S_ISREG(e->stages[2].mode) || S_ISLNK(e->stages[2].mode)) { | |
505 | last_file = path; | |
506 | last_len = len; | |
ef02b317 EN |
507 | } else { |
508 | last_file = NULL; | |
509 | } | |
510 | } | |
f701aae0 | 511 | string_list_clear(&df_sorted_entries, 0); |
ef02b317 EN |
512 | } |
513 | ||
9cba13ca | 514 | struct rename { |
9047ebbc | 515 | struct diff_filepair *pair; |
379fc378 EN |
516 | /* |
517 | * Purpose of src_entry and dst_entry: | |
518 | * | |
519 | * If 'before' is renamed to 'after' then src_entry will contain | |
520 | * the versions of 'before' from the merge_base, HEAD, and MERGE in | |
521 | * stages 1, 2, and 3; dst_entry will contain the respective | |
522 | * versions of 'after' in corresponding locations. Thus, we have a | |
523 | * total of six modes and oids, though some will be null. (Stage 0 | |
524 | * is ignored; we're interested in handling conflicts.) | |
525 | * | |
526 | * Since we don't turn on break-rewrites by default, neither | |
527 | * src_entry nor dst_entry can have all three of their stages have | |
528 | * non-null oids, meaning at most four of the six will be non-null. | |
529 | * Also, since this is a rename, both src_entry and dst_entry will | |
530 | * have at least one non-null oid, meaning at least two will be | |
531 | * non-null. Of the six oids, a typical rename will have three be | |
532 | * non-null. Only two implies a rename/delete, and four implies a | |
533 | * rename/add. | |
534 | */ | |
9047ebbc MV |
535 | struct stage_data *src_entry; |
536 | struct stage_data *dst_entry; | |
537 | unsigned processed:1; | |
538 | }; | |
539 | ||
8b026eda JH |
540 | /* |
541 | * Get information of all renames which occurred between 'o_tree' and | |
542 | * 'tree'. We need the three trees in the merge ('o_tree', 'a_tree' and | |
543 | * 'b_tree') to be able to associate the correct cache entries with | |
544 | * the rename information. 'tree' is always equal to either a_tree or b_tree. | |
545 | */ | |
546 | static struct string_list *get_renames(struct merge_options *o, | |
547 | struct tree *tree, | |
548 | struct tree *o_tree, | |
549 | struct tree *a_tree, | |
550 | struct tree *b_tree, | |
551 | struct string_list *entries) | |
552 | { | |
553 | int i; | |
554 | struct string_list *renames; | |
555 | struct diff_options opts; | |
556 | ||
557 | renames = xcalloc(1, sizeof(struct string_list)); | |
558 | if (!o->detect_rename) | |
559 | return renames; | |
560 | ||
561 | diff_setup(&opts); | |
562 | opts.flags.recursive = 1; | |
563 | opts.flags.rename_empty = 0; | |
564 | opts.detect_rename = DIFF_DETECT_RENAME; | |
565 | opts.rename_limit = o->merge_rename_limit >= 0 ? o->merge_rename_limit : | |
566 | o->diff_rename_limit >= 0 ? o->diff_rename_limit : | |
567 | 1000; | |
568 | opts.rename_score = o->rename_score; | |
569 | opts.show_rename_progress = o->show_rename_progress; | |
570 | opts.output_format = DIFF_FORMAT_NO_OUTPUT; | |
571 | diff_setup_done(&opts); | |
572 | diff_tree_oid(&o_tree->object.oid, &tree->object.oid, "", &opts); | |
573 | diffcore_std(&opts); | |
574 | if (opts.needed_rename_limit > o->needed_rename_limit) | |
575 | o->needed_rename_limit = opts.needed_rename_limit; | |
576 | for (i = 0; i < diff_queued_diff.nr; ++i) { | |
577 | struct string_list_item *item; | |
578 | struct rename *re; | |
579 | struct diff_filepair *pair = diff_queued_diff.queue[i]; | |
580 | if (pair->status != 'R') { | |
581 | diff_free_filepair(pair); | |
582 | continue; | |
583 | } | |
584 | re = xmalloc(sizeof(*re)); | |
585 | re->processed = 0; | |
586 | re->pair = pair; | |
587 | item = string_list_lookup(entries, re->pair->one->path); | |
588 | if (!item) | |
589 | re->src_entry = insert_stage_data(re->pair->one->path, | |
590 | o_tree, a_tree, b_tree, entries); | |
591 | else | |
592 | re->src_entry = item->util; | |
593 | ||
594 | item = string_list_lookup(entries, re->pair->two->path); | |
595 | if (!item) | |
596 | re->dst_entry = insert_stage_data(re->pair->two->path, | |
597 | o_tree, a_tree, b_tree, entries); | |
598 | else | |
599 | re->dst_entry = item->util; | |
600 | item = string_list_insert(renames, pair->one->path); | |
601 | item->util = re; | |
602 | } | |
603 | opts.output_format = DIFF_FORMAT_NO_OUTPUT; | |
604 | diff_queued_diff.nr = 0; | |
605 | diff_flush(&opts); | |
606 | return renames; | |
607 | } | |
608 | ||
bc9204d4 JS |
609 | static int update_stages(struct merge_options *opt, const char *path, |
610 | const struct diff_filespec *o, | |
650467cf EN |
611 | const struct diff_filespec *a, |
612 | const struct diff_filespec *b) | |
9047ebbc | 613 | { |
f53d3977 EN |
614 | |
615 | /* | |
616 | * NOTE: It is usually a bad idea to call update_stages on a path | |
617 | * before calling update_file on that same path, since it can | |
618 | * sometimes lead to spurious "refusing to lose untracked file..." | |
619 | * messages from update_file (via make_room_for path via | |
620 | * would_lose_untracked). Instead, reverse the order of the calls | |
621 | * (executing update_file first and then update_stages). | |
622 | */ | |
650467cf EN |
623 | int clear = 1; |
624 | int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_SKIP_DFCHECK; | |
9047ebbc MV |
625 | if (clear) |
626 | if (remove_file_from_cache(path)) | |
627 | return -1; | |
628 | if (o) | |
bc9204d4 | 629 | if (add_cacheinfo(opt, o->mode, &o->oid, path, 1, 0, options)) |
9047ebbc MV |
630 | return -1; |
631 | if (a) | |
bc9204d4 | 632 | if (add_cacheinfo(opt, a->mode, &a->oid, path, 2, 0, options)) |
9047ebbc MV |
633 | return -1; |
634 | if (b) | |
bc9204d4 | 635 | if (add_cacheinfo(opt, b->mode, &b->oid, path, 3, 0, options)) |
9047ebbc MV |
636 | return -1; |
637 | return 0; | |
638 | } | |
639 | ||
b8ddf164 EN |
640 | static void update_entry(struct stage_data *entry, |
641 | struct diff_filespec *o, | |
642 | struct diff_filespec *a, | |
643 | struct diff_filespec *b) | |
2ff739f9 | 644 | { |
2ff739f9 EN |
645 | entry->processed = 0; |
646 | entry->stages[1].mode = o->mode; | |
647 | entry->stages[2].mode = a->mode; | |
648 | entry->stages[3].mode = b->mode; | |
fd429e98 | 649 | oidcpy(&entry->stages[1].oid, &o->oid); |
650 | oidcpy(&entry->stages[2].oid, &a->oid); | |
651 | oidcpy(&entry->stages[3].oid, &b->oid); | |
2ff739f9 EN |
652 | } |
653 | ||
b7fa51da MV |
654 | static int remove_file(struct merge_options *o, int clean, |
655 | const char *path, int no_wd) | |
9047ebbc | 656 | { |
b7fa51da MV |
657 | int update_cache = o->call_depth || clean; |
658 | int update_working_directory = !o->call_depth && !no_wd; | |
9047ebbc MV |
659 | |
660 | if (update_cache) { | |
661 | if (remove_file_from_cache(path)) | |
662 | return -1; | |
663 | } | |
664 | if (update_working_directory) { | |
ae352c7f DT |
665 | if (ignore_case) { |
666 | struct cache_entry *ce; | |
667 | ce = cache_file_exists(path, strlen(path), ignore_case); | |
4cba2b01 | 668 | if (ce && ce_stage(ce) == 0 && strcmp(path, ce->name)) |
ae352c7f DT |
669 | return 0; |
670 | } | |
25755e84 | 671 | if (remove_path(path)) |
9047ebbc | 672 | return -1; |
9047ebbc MV |
673 | } |
674 | return 0; | |
675 | } | |
676 | ||
45bc131d JK |
677 | /* add a string to a strbuf, but converting "/" to "_" */ |
678 | static void add_flattened_path(struct strbuf *out, const char *s) | |
679 | { | |
680 | size_t i = out->len; | |
681 | strbuf_addstr(out, s); | |
682 | for (; i < out->len; i++) | |
683 | if (out->buf[i] == '/') | |
684 | out->buf[i] = '_'; | |
685 | } | |
686 | ||
696ee23c | 687 | static char *unique_path(struct merge_options *o, const char *path, const char *branch) |
9047ebbc | 688 | { |
fc65b00d | 689 | struct path_hashmap_entry *entry; |
45bc131d | 690 | struct strbuf newpath = STRBUF_INIT; |
9047ebbc | 691 | int suffix = 0; |
45bc131d JK |
692 | size_t base_len; |
693 | ||
694 | strbuf_addf(&newpath, "%s~", path); | |
695 | add_flattened_path(&newpath, branch); | |
696 | ||
697 | base_len = newpath.len; | |
fc65b00d KW |
698 | while (hashmap_get_from_hash(&o->current_file_dir_set, |
699 | path_hash(newpath.buf), newpath.buf) || | |
8e1b62f1 | 700 | (!o->call_depth && file_exists(newpath.buf))) { |
45bc131d JK |
701 | strbuf_setlen(&newpath, base_len); |
702 | strbuf_addf(&newpath, "_%d", suffix++); | |
703 | } | |
704 | ||
fc65b00d KW |
705 | FLEX_ALLOC_MEM(entry, path, newpath.buf, newpath.len); |
706 | hashmap_entry_init(entry, path_hash(entry->path)); | |
707 | hashmap_add(&o->current_file_dir_set, entry); | |
45bc131d | 708 | return strbuf_detach(&newpath, NULL); |
9047ebbc MV |
709 | } |
710 | ||
5423d2e7 DT |
711 | /** |
712 | * Check whether a directory in the index is in the way of an incoming | |
713 | * file. Return 1 if so. If check_working_copy is non-zero, also | |
714 | * check the working directory. If empty_ok is non-zero, also return | |
715 | * 0 in the case where the working-tree dir exists but is empty. | |
716 | */ | |
717 | static int dir_in_way(const char *path, int check_working_copy, int empty_ok) | |
f2507b4e | 718 | { |
b4600fbe JK |
719 | int pos; |
720 | struct strbuf dirpath = STRBUF_INIT; | |
f2507b4e EN |
721 | struct stat st; |
722 | ||
b4600fbe JK |
723 | strbuf_addstr(&dirpath, path); |
724 | strbuf_addch(&dirpath, '/'); | |
f2507b4e | 725 | |
b4600fbe | 726 | pos = cache_name_pos(dirpath.buf, dirpath.len); |
f2507b4e EN |
727 | |
728 | if (pos < 0) | |
729 | pos = -1 - pos; | |
730 | if (pos < active_nr && | |
b4600fbe JK |
731 | !strncmp(dirpath.buf, active_cache[pos]->name, dirpath.len)) { |
732 | strbuf_release(&dirpath); | |
f2507b4e EN |
733 | return 1; |
734 | } | |
735 | ||
b4600fbe | 736 | strbuf_release(&dirpath); |
5423d2e7 DT |
737 | return check_working_copy && !lstat(path, &st) && S_ISDIR(st.st_mode) && |
738 | !(empty_ok && is_empty_dir(path)); | |
f2507b4e EN |
739 | } |
740 | ||
aacb82de | 741 | static int was_tracked(const char *path) |
60c91181 JH |
742 | { |
743 | int pos = cache_name_pos(path, strlen(path)); | |
744 | ||
f8d83fb6 JS |
745 | if (0 <= pos) |
746 | /* we have been tracking this path */ | |
747 | return 1; | |
748 | ||
749 | /* | |
750 | * Look for an unmerged entry for the path, | |
751 | * specifically stage #2, which would indicate | |
752 | * that "our" side before the merge started | |
753 | * had the path tracked (and resulted in a conflict). | |
754 | */ | |
755 | for (pos = -1 - pos; | |
756 | pos < active_nr && !strcmp(path, active_cache[pos]->name); | |
757 | pos++) | |
758 | if (ce_stage(active_cache[pos]) == 2) | |
aacb82de | 759 | return 1; |
aacb82de | 760 | return 0; |
60c91181 JH |
761 | } |
762 | ||
aacb82de | 763 | static int would_lose_untracked(const char *path) |
9047ebbc | 764 | { |
aacb82de | 765 | return !was_tracked(path) && file_exists(path); |
60c91181 JH |
766 | } |
767 | ||
ed0148a5 | 768 | static int make_room_for_path(struct merge_options *o, const char *path) |
9047ebbc | 769 | { |
ed0148a5 | 770 | int status, i; |
55653a68 | 771 | const char *msg = _("failed to create path '%s'%s"); |
9047ebbc | 772 | |
ed0148a5 EN |
773 | /* Unlink any D/F conflict files that are in the way */ |
774 | for (i = 0; i < o->df_conflict_file_set.nr; i++) { | |
775 | const char *df_path = o->df_conflict_file_set.items[i].string; | |
776 | size_t pathlen = strlen(path); | |
777 | size_t df_pathlen = strlen(df_path); | |
778 | if (df_pathlen < pathlen && | |
779 | path[df_pathlen] == '/' && | |
780 | strncmp(path, df_path, df_pathlen) == 0) { | |
781 | output(o, 3, | |
55653a68 | 782 | _("Removing %s to make room for subdirectory\n"), |
ed0148a5 EN |
783 | df_path); |
784 | unlink(df_path); | |
785 | unsorted_string_list_delete_item(&o->df_conflict_file_set, | |
786 | i, 0); | |
787 | break; | |
788 | } | |
789 | } | |
790 | ||
791 | /* Make sure leading directories are created */ | |
9047ebbc MV |
792 | status = safe_create_leading_directories_const(path); |
793 | if (status) { | |
6003303a | 794 | if (status == SCLD_EXISTS) |
9047ebbc | 795 | /* something else exists */ |
bc9204d4 JS |
796 | return err(o, msg, path, _(": perhaps a D/F conflict?")); |
797 | return err(o, msg, path, ""); | |
9047ebbc MV |
798 | } |
799 | ||
60c91181 JH |
800 | /* |
801 | * Do not unlink a file in the work tree if we are not | |
802 | * tracking it. | |
803 | */ | |
804 | if (would_lose_untracked(path)) | |
bc9204d4 | 805 | return err(o, _("refusing to lose untracked file at '%s'"), |
60c91181 JH |
806 | path); |
807 | ||
9047ebbc MV |
808 | /* Successful unlink is good.. */ |
809 | if (!unlink(path)) | |
810 | return 0; | |
811 | /* .. and so is no existing file */ | |
812 | if (errno == ENOENT) | |
813 | return 0; | |
814 | /* .. but not some other error (who really cares what?) */ | |
bc9204d4 | 815 | return err(o, msg, path, _(": perhaps a D/F conflict?")); |
9047ebbc MV |
816 | } |
817 | ||
75456f96 JS |
818 | static int update_file_flags(struct merge_options *o, |
819 | const struct object_id *oid, | |
820 | unsigned mode, | |
821 | const char *path, | |
822 | int update_cache, | |
823 | int update_wd) | |
9047ebbc | 824 | { |
6003303a JS |
825 | int ret = 0; |
826 | ||
b7fa51da | 827 | if (o->call_depth) |
9047ebbc MV |
828 | update_wd = 0; |
829 | ||
830 | if (update_wd) { | |
831 | enum object_type type; | |
832 | void *buf; | |
833 | unsigned long size; | |
834 | ||
68d03e4a | 835 | if (S_ISGITLINK(mode)) { |
0c44c943 JH |
836 | /* |
837 | * We may later decide to recursively descend into | |
838 | * the submodule directory and update its index | |
839 | * and/or work tree, but we do not do that now. | |
840 | */ | |
68d03e4a | 841 | update_wd = 0; |
0c44c943 | 842 | goto update_index; |
68d03e4a | 843 | } |
9047ebbc | 844 | |
b4f5aca4 | 845 | buf = read_object_file(oid, &type, &size); |
9047ebbc | 846 | if (!buf) |
bc9204d4 | 847 | return err(o, _("cannot read object %s '%s'"), oid_to_hex(oid), path); |
6003303a | 848 | if (type != OBJ_BLOB) { |
bc9204d4 | 849 | ret = err(o, _("blob expected for %s '%s'"), oid_to_hex(oid), path); |
6003303a JS |
850 | goto free_buf; |
851 | } | |
9047ebbc | 852 | if (S_ISREG(mode)) { |
f285a2d7 | 853 | struct strbuf strbuf = STRBUF_INIT; |
9047ebbc MV |
854 | if (convert_to_working_tree(path, buf, size, &strbuf)) { |
855 | free(buf); | |
856 | size = strbuf.len; | |
857 | buf = strbuf_detach(&strbuf, NULL); | |
858 | } | |
859 | } | |
860 | ||
ed0148a5 | 861 | if (make_room_for_path(o, path) < 0) { |
9047ebbc | 862 | update_wd = 0; |
75456f96 | 863 | goto free_buf; |
9047ebbc MV |
864 | } |
865 | if (S_ISREG(mode) || (!has_symlinks && S_ISLNK(mode))) { | |
866 | int fd; | |
867 | if (mode & 0100) | |
868 | mode = 0777; | |
869 | else | |
870 | mode = 0666; | |
871 | fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode); | |
6003303a | 872 | if (fd < 0) { |
bc9204d4 JS |
873 | ret = err(o, _("failed to open '%s': %s"), |
874 | path, strerror(errno)); | |
6003303a JS |
875 | goto free_buf; |
876 | } | |
f633ea2c | 877 | write_in_full(fd, buf, size); |
9047ebbc MV |
878 | close(fd); |
879 | } else if (S_ISLNK(mode)) { | |
880 | char *lnk = xmemdupz(buf, size); | |
881 | safe_create_leading_directories_const(path); | |
882 | unlink(path); | |
304dcf26 | 883 | if (symlink(lnk, path)) |
bc9204d4 JS |
884 | ret = err(o, _("failed to symlink '%s': %s"), |
885 | path, strerror(errno)); | |
9047ebbc MV |
886 | free(lnk); |
887 | } else | |
bc9204d4 JS |
888 | ret = err(o, |
889 | _("do not know what to do with %06o %s '%s'"), | |
890 | mode, oid_to_hex(oid), path); | |
75456f96 | 891 | free_buf: |
9047ebbc MV |
892 | free(buf); |
893 | } | |
894 | update_index: | |
6003303a | 895 | if (!ret && update_cache) |
bc9204d4 | 896 | add_cacheinfo(o, mode, oid, path, 0, update_wd, ADD_CACHE_OK_TO_ADD); |
6003303a | 897 | return ret; |
9047ebbc MV |
898 | } |
899 | ||
75456f96 JS |
900 | static int update_file(struct merge_options *o, |
901 | int clean, | |
902 | const struct object_id *oid, | |
903 | unsigned mode, | |
904 | const char *path) | |
9047ebbc | 905 | { |
75456f96 | 906 | return update_file_flags(o, oid, mode, path, o->call_depth || clean, !o->call_depth); |
9047ebbc MV |
907 | } |
908 | ||
909 | /* Low level file merging, update and removal */ | |
910 | ||
9cba13ca | 911 | struct merge_file_info { |
9b561499 | 912 | struct object_id oid; |
9047ebbc MV |
913 | unsigned mode; |
914 | unsigned clean:1, | |
915 | merge:1; | |
916 | }; | |
917 | ||
b7fa51da MV |
918 | static int merge_3way(struct merge_options *o, |
919 | mmbuffer_t *result_buf, | |
0c059420 EN |
920 | const struct diff_filespec *one, |
921 | const struct diff_filespec *a, | |
922 | const struct diff_filespec *b, | |
9047ebbc MV |
923 | const char *branch1, |
924 | const char *branch2) | |
925 | { | |
926 | mmfile_t orig, src1, src2; | |
712516bc | 927 | struct ll_merge_options ll_opts = {0}; |
4c5868f4 | 928 | char *base_name, *name1, *name2; |
9047ebbc | 929 | int merge_status; |
8cc5b290 | 930 | |
712516bc | 931 | ll_opts.renormalize = o->renormalize; |
58a1ece4 | 932 | ll_opts.xdl_opts = o->xdl_opts; |
712516bc JN |
933 | |
934 | if (o->call_depth) { | |
935 | ll_opts.virtual_ancestor = 1; | |
936 | ll_opts.variant = 0; | |
937 | } else { | |
8cc5b290 AP |
938 | switch (o->recursive_variant) { |
939 | case MERGE_RECURSIVE_OURS: | |
712516bc | 940 | ll_opts.variant = XDL_MERGE_FAVOR_OURS; |
8cc5b290 AP |
941 | break; |
942 | case MERGE_RECURSIVE_THEIRS: | |
712516bc | 943 | ll_opts.variant = XDL_MERGE_FAVOR_THEIRS; |
8cc5b290 AP |
944 | break; |
945 | default: | |
712516bc | 946 | ll_opts.variant = 0; |
8cc5b290 AP |
947 | break; |
948 | } | |
949 | } | |
9047ebbc | 950 | |
4c5868f4 JN |
951 | if (strcmp(a->path, b->path) || |
952 | (o->ancestor != NULL && strcmp(a->path, one->path) != 0)) { | |
953 | base_name = o->ancestor == NULL ? NULL : | |
4e2d094d RJ |
954 | mkpathdup("%s:%s", o->ancestor, one->path); |
955 | name1 = mkpathdup("%s:%s", branch1, a->path); | |
956 | name2 = mkpathdup("%s:%s", branch2, b->path); | |
606475f3 | 957 | } else { |
4c5868f4 | 958 | base_name = o->ancestor == NULL ? NULL : |
4e2d094d RJ |
959 | mkpathdup("%s", o->ancestor); |
960 | name1 = mkpathdup("%s", branch1); | |
961 | name2 = mkpathdup("%s", branch2); | |
606475f3 | 962 | } |
9047ebbc | 963 | |
d449347d | 964 | read_mmblob(&orig, &one->oid); |
965 | read_mmblob(&src1, &a->oid); | |
966 | read_mmblob(&src2, &b->oid); | |
9047ebbc | 967 | |
4c5868f4 | 968 | merge_status = ll_merge(result_buf, a->path, &orig, base_name, |
712516bc | 969 | &src1, name1, &src2, name2, &ll_opts); |
9047ebbc | 970 | |
4e2d094d | 971 | free(base_name); |
9047ebbc MV |
972 | free(name1); |
973 | free(name2); | |
974 | free(orig.ptr); | |
975 | free(src1.ptr); | |
976 | free(src2.ptr); | |
977 | return merge_status; | |
978 | } | |
979 | ||
3c8a51e8 | 980 | static int merge_file_1(struct merge_options *o, |
6bdaead1 EN |
981 | const struct diff_filespec *one, |
982 | const struct diff_filespec *a, | |
983 | const struct diff_filespec *b, | |
984 | const char *branch1, | |
3c8a51e8 JS |
985 | const char *branch2, |
986 | struct merge_file_info *result) | |
9047ebbc | 987 | { |
3c8a51e8 JS |
988 | result->merge = 0; |
989 | result->clean = 1; | |
9047ebbc MV |
990 | |
991 | if ((S_IFMT & a->mode) != (S_IFMT & b->mode)) { | |
3c8a51e8 | 992 | result->clean = 0; |
9047ebbc | 993 | if (S_ISREG(a->mode)) { |
3c8a51e8 JS |
994 | result->mode = a->mode; |
995 | oidcpy(&result->oid, &a->oid); | |
9047ebbc | 996 | } else { |
3c8a51e8 JS |
997 | result->mode = b->mode; |
998 | oidcpy(&result->oid, &b->oid); | |
9047ebbc MV |
999 | } |
1000 | } else { | |
b4da9d62 | 1001 | if (!oid_eq(&a->oid, &one->oid) && !oid_eq(&b->oid, &one->oid)) |
3c8a51e8 | 1002 | result->merge = 1; |
9047ebbc MV |
1003 | |
1004 | /* | |
1005 | * Merge modes | |
1006 | */ | |
b7fa51da | 1007 | if (a->mode == b->mode || a->mode == one->mode) |
3c8a51e8 | 1008 | result->mode = b->mode; |
9047ebbc | 1009 | else { |
3c8a51e8 | 1010 | result->mode = a->mode; |
b7fa51da | 1011 | if (b->mode != one->mode) { |
3c8a51e8 JS |
1012 | result->clean = 0; |
1013 | result->merge = 1; | |
9047ebbc MV |
1014 | } |
1015 | } | |
1016 | ||
b4da9d62 | 1017 | if (oid_eq(&a->oid, &b->oid) || oid_eq(&a->oid, &one->oid)) |
3c8a51e8 | 1018 | oidcpy(&result->oid, &b->oid); |
b4da9d62 | 1019 | else if (oid_eq(&b->oid, &one->oid)) |
3c8a51e8 | 1020 | oidcpy(&result->oid, &a->oid); |
9047ebbc MV |
1021 | else if (S_ISREG(a->mode)) { |
1022 | mmbuffer_t result_buf; | |
6003303a | 1023 | int ret = 0, merge_status; |
9047ebbc | 1024 | |
b7fa51da | 1025 | merge_status = merge_3way(o, &result_buf, one, a, b, |
9047ebbc MV |
1026 | branch1, branch2); |
1027 | ||
1028 | if ((merge_status < 0) || !result_buf.ptr) | |
bc9204d4 | 1029 | ret = err(o, _("Failed to execute internal merge")); |
9047ebbc | 1030 | |
a09c985e PO |
1031 | if (!ret && |
1032 | write_object_file(result_buf.ptr, result_buf.size, | |
1033 | blob_type, &result->oid)) | |
bc9204d4 JS |
1034 | ret = err(o, _("Unable to add %s to database"), |
1035 | a->path); | |
9047ebbc MV |
1036 | |
1037 | free(result_buf.ptr); | |
6003303a JS |
1038 | if (ret) |
1039 | return ret; | |
3c8a51e8 | 1040 | result->clean = (merge_status == 0); |
9047ebbc | 1041 | } else if (S_ISGITLINK(a->mode)) { |
71f35d5c | 1042 | result->clean = merge_submodule(&result->oid, |
a0d12c44 | 1043 | one->path, |
71f35d5c | 1044 | &one->oid, |
1045 | &a->oid, | |
1046 | &b->oid, | |
80988783 | 1047 | !o->call_depth); |
9047ebbc | 1048 | } else if (S_ISLNK(a->mode)) { |
fd48b464 JH |
1049 | switch (o->recursive_variant) { |
1050 | case MERGE_RECURSIVE_NORMAL: | |
1051 | oidcpy(&result->oid, &a->oid); | |
1052 | if (!oid_eq(&a->oid, &b->oid)) | |
1053 | result->clean = 0; | |
1054 | break; | |
1055 | case MERGE_RECURSIVE_OURS: | |
1056 | oidcpy(&result->oid, &a->oid); | |
1057 | break; | |
1058 | case MERGE_RECURSIVE_THEIRS: | |
1059 | oidcpy(&result->oid, &b->oid); | |
1060 | break; | |
1061 | } | |
ef1177d1 | 1062 | } else |
7e97e100 | 1063 | die("BUG: unsupported object type in the tree"); |
9047ebbc MV |
1064 | } |
1065 | ||
3c8a51e8 | 1066 | return 0; |
9047ebbc MV |
1067 | } |
1068 | ||
3c8a51e8 | 1069 | static int merge_file_special_markers(struct merge_options *o, |
dac47415 EN |
1070 | const struct diff_filespec *one, |
1071 | const struct diff_filespec *a, | |
1072 | const struct diff_filespec *b, | |
1073 | const char *branch1, | |
1074 | const char *filename1, | |
1075 | const char *branch2, | |
3c8a51e8 JS |
1076 | const char *filename2, |
1077 | struct merge_file_info *mfi) | |
dac47415 EN |
1078 | { |
1079 | char *side1 = NULL; | |
1080 | char *side2 = NULL; | |
3c8a51e8 | 1081 | int ret; |
dac47415 | 1082 | |
28310186 JK |
1083 | if (filename1) |
1084 | side1 = xstrfmt("%s:%s", branch1, filename1); | |
1085 | if (filename2) | |
1086 | side2 = xstrfmt("%s:%s", branch2, filename2); | |
dac47415 | 1087 | |
3c8a51e8 JS |
1088 | ret = merge_file_1(o, one, a, b, |
1089 | side1 ? side1 : branch1, | |
1090 | side2 ? side2 : branch2, mfi); | |
dac47415 EN |
1091 | free(side1); |
1092 | free(side2); | |
3c8a51e8 | 1093 | return ret; |
dac47415 EN |
1094 | } |
1095 | ||
3c8a51e8 | 1096 | static int merge_file_one(struct merge_options *o, |
6bdaead1 | 1097 | const char *path, |
b4da9d62 | 1098 | const struct object_id *o_oid, int o_mode, |
1099 | const struct object_id *a_oid, int a_mode, | |
1100 | const struct object_id *b_oid, int b_mode, | |
6bdaead1 | 1101 | const char *branch1, |
3c8a51e8 JS |
1102 | const char *branch2, |
1103 | struct merge_file_info *mfi) | |
6bdaead1 EN |
1104 | { |
1105 | struct diff_filespec one, a, b; | |
1106 | ||
1107 | one.path = a.path = b.path = (char *)path; | |
b4da9d62 | 1108 | oidcpy(&one.oid, o_oid); |
6bdaead1 | 1109 | one.mode = o_mode; |
b4da9d62 | 1110 | oidcpy(&a.oid, a_oid); |
6bdaead1 | 1111 | a.mode = a_mode; |
b4da9d62 | 1112 | oidcpy(&b.oid, b_oid); |
6bdaead1 | 1113 | b.mode = b_mode; |
3c8a51e8 | 1114 | return merge_file_1(o, &one, &a, &b, branch1, branch2, mfi); |
6bdaead1 EN |
1115 | } |
1116 | ||
75456f96 | 1117 | static int handle_change_delete(struct merge_options *o, |
b26d87f2 | 1118 | const char *path, const char *old_path, |
b4da9d62 | 1119 | const struct object_id *o_oid, int o_mode, |
b26d87f2 MM |
1120 | const struct object_id *changed_oid, |
1121 | int changed_mode, | |
1122 | const char *change_branch, | |
1123 | const char *delete_branch, | |
b7033252 EN |
1124 | const char *change, const char *change_past) |
1125 | { | |
b26d87f2 MM |
1126 | char *alt_path = NULL; |
1127 | const char *update_path = path; | |
75456f96 | 1128 | int ret = 0; |
b26d87f2 | 1129 | |
8b026eda | 1130 | if (dir_in_way(path, !o->call_depth, 0)) { |
b26d87f2 | 1131 | update_path = alt_path = unique_path(o, path, change_branch); |
b7033252 EN |
1132 | } |
1133 | ||
1134 | if (o->call_depth) { | |
1135 | /* | |
1136 | * We cannot arbitrarily accept either a_sha or b_sha as | |
1137 | * correct; since there is no true "middle point" between | |
1138 | * them, simply reuse the base version for virtual merge base. | |
1139 | */ | |
75456f96 JS |
1140 | ret = remove_file_from_cache(path); |
1141 | if (!ret) | |
b26d87f2 | 1142 | ret = update_file(o, 0, o_oid, o_mode, update_path); |
b7033252 | 1143 | } else { |
b26d87f2 MM |
1144 | if (!alt_path) { |
1145 | if (!old_path) { | |
1146 | output(o, 1, _("CONFLICT (%s/delete): %s deleted in %s " | |
1147 | "and %s in %s. Version %s of %s left in tree."), | |
1148 | change, path, delete_branch, change_past, | |
1149 | change_branch, change_branch, path); | |
1150 | } else { | |
1151 | output(o, 1, _("CONFLICT (%s/delete): %s deleted in %s " | |
1152 | "and %s to %s in %s. Version %s of %s left in tree."), | |
1153 | change, old_path, delete_branch, change_past, path, | |
1154 | change_branch, change_branch, path); | |
1155 | } | |
55653a68 | 1156 | } else { |
b26d87f2 MM |
1157 | if (!old_path) { |
1158 | output(o, 1, _("CONFLICT (%s/delete): %s deleted in %s " | |
1159 | "and %s in %s. Version %s of %s left in tree at %s."), | |
1160 | change, path, delete_branch, change_past, | |
1161 | change_branch, change_branch, path, alt_path); | |
1162 | } else { | |
1163 | output(o, 1, _("CONFLICT (%s/delete): %s deleted in %s " | |
1164 | "and %s to %s in %s. Version %s of %s left in tree at %s."), | |
1165 | change, old_path, delete_branch, change_past, path, | |
1166 | change_branch, change_branch, path, alt_path); | |
1167 | } | |
55653a68 | 1168 | } |
35a74abf | 1169 | /* |
b26d87f2 MM |
1170 | * No need to call update_file() on path when change_branch == |
1171 | * o->branch1 && !alt_path, since that would needlessly touch | |
1172 | * path. We could call update_file_flags() with update_cache=0 | |
1173 | * and update_wd=0, but that's a no-op. | |
35a74abf | 1174 | */ |
b26d87f2 MM |
1175 | if (change_branch != o->branch1 || alt_path) |
1176 | ret = update_file(o, 0, changed_oid, changed_mode, update_path); | |
b7033252 | 1177 | } |
b26d87f2 | 1178 | free(alt_path); |
75456f96 JS |
1179 | |
1180 | return ret; | |
b7033252 EN |
1181 | } |
1182 | ||
75456f96 | 1183 | static int conflict_rename_delete(struct merge_options *o, |
6ef2cb00 EN |
1184 | struct diff_filepair *pair, |
1185 | const char *rename_branch, | |
b26d87f2 | 1186 | const char *delete_branch) |
9047ebbc | 1187 | { |
e03acb8b EN |
1188 | const struct diff_filespec *orig = pair->one; |
1189 | const struct diff_filespec *dest = pair->two; | |
6ef2cb00 | 1190 | |
75456f96 JS |
1191 | if (handle_change_delete(o, |
1192 | o->call_depth ? orig->path : dest->path, | |
b26d87f2 | 1193 | o->call_depth ? NULL : orig->path, |
75456f96 | 1194 | &orig->oid, orig->mode, |
b26d87f2 MM |
1195 | &dest->oid, dest->mode, |
1196 | rename_branch, delete_branch, | |
75456f96 JS |
1197 | _("rename"), _("renamed"))) |
1198 | return -1; | |
e03acb8b | 1199 | |
75456f96 JS |
1200 | if (o->call_depth) |
1201 | return remove_file_from_cache(dest->path); | |
1202 | else | |
bc9204d4 | 1203 | return update_stages(o, dest->path, NULL, |
75456f96 JS |
1204 | rename_branch == o->branch1 ? dest : NULL, |
1205 | rename_branch == o->branch1 ? NULL : dest); | |
6ef2cb00 EN |
1206 | } |
1207 | ||
1ac91b32 EN |
1208 | static struct diff_filespec *filespec_from_entry(struct diff_filespec *target, |
1209 | struct stage_data *entry, | |
1210 | int stage) | |
9047ebbc | 1211 | { |
b4da9d62 | 1212 | struct object_id *oid = &entry->stages[stage].oid; |
1ac91b32 | 1213 | unsigned mode = entry->stages[stage].mode; |
b4da9d62 | 1214 | if (mode == 0 || is_null_oid(oid)) |
1ac91b32 | 1215 | return NULL; |
b4da9d62 | 1216 | oidcpy(&target->oid, oid); |
1ac91b32 EN |
1217 | target->mode = mode; |
1218 | return target; | |
1219 | } | |
1220 | ||
75456f96 | 1221 | static int handle_file(struct merge_options *o, |
3672c971 EN |
1222 | struct diff_filespec *rename, |
1223 | int stage, | |
1224 | struct rename_conflict_info *ci) | |
1225 | { | |
1226 | char *dst_name = rename->path; | |
1227 | struct stage_data *dst_entry; | |
1228 | const char *cur_branch, *other_branch; | |
1229 | struct diff_filespec other; | |
1230 | struct diff_filespec *add; | |
75456f96 | 1231 | int ret; |
3672c971 EN |
1232 | |
1233 | if (stage == 2) { | |
1234 | dst_entry = ci->dst_entry1; | |
1235 | cur_branch = ci->branch1; | |
1236 | other_branch = ci->branch2; | |
1237 | } else { | |
1238 | dst_entry = ci->dst_entry2; | |
1239 | cur_branch = ci->branch2; | |
1240 | other_branch = ci->branch1; | |
9047ebbc | 1241 | } |
3672c971 EN |
1242 | |
1243 | add = filespec_from_entry(&other, dst_entry, stage ^ 1); | |
3672c971 EN |
1244 | if (add) { |
1245 | char *add_name = unique_path(o, rename->path, other_branch); | |
75456f96 JS |
1246 | if (update_file(o, 0, &add->oid, add->mode, add_name)) |
1247 | return -1; | |
3672c971 | 1248 | |
8b026eda | 1249 | remove_file(o, 0, rename->path, 0); |
3672c971 EN |
1250 | dst_name = unique_path(o, rename->path, cur_branch); |
1251 | } else { | |
5423d2e7 | 1252 | if (dir_in_way(rename->path, !o->call_depth, 0)) { |
3672c971 | 1253 | dst_name = unique_path(o, rename->path, cur_branch); |
55653a68 | 1254 | output(o, 1, _("%s is a directory in %s adding as %s instead"), |
3672c971 EN |
1255 | rename->path, other_branch, dst_name); |
1256 | } | |
9047ebbc | 1257 | } |
75456f96 JS |
1258 | if ((ret = update_file(o, 0, &rename->oid, rename->mode, dst_name))) |
1259 | ; /* fall through, do allow dst_name to be released */ | |
1260 | else if (stage == 2) | |
bc9204d4 | 1261 | ret = update_stages(o, rename->path, NULL, rename, add); |
f53d3977 | 1262 | else |
bc9204d4 | 1263 | ret = update_stages(o, rename->path, NULL, add, rename); |
3672c971 EN |
1264 | |
1265 | if (dst_name != rename->path) | |
1266 | free(dst_name); | |
75456f96 JS |
1267 | |
1268 | return ret; | |
3672c971 EN |
1269 | } |
1270 | ||
75456f96 | 1271 | static int conflict_rename_rename_1to2(struct merge_options *o, |
a99b7f22 | 1272 | struct rename_conflict_info *ci) |
9047ebbc | 1273 | { |
09c01f85 | 1274 | /* One file was renamed in both branches, but to different names. */ |
a99b7f22 EN |
1275 | struct diff_filespec *one = ci->pair1->one; |
1276 | struct diff_filespec *a = ci->pair1->two; | |
1277 | struct diff_filespec *b = ci->pair2->two; | |
4f66dade | 1278 | |
55653a68 | 1279 | output(o, 1, _("CONFLICT (rename/rename): " |
4f66dade | 1280 | "Rename \"%s\"->\"%s\" in branch \"%s\" " |
55653a68 | 1281 | "rename \"%s\"->\"%s\" in \"%s\"%s"), |
a99b7f22 EN |
1282 | one->path, a->path, ci->branch1, |
1283 | one->path, b->path, ci->branch2, | |
55653a68 | 1284 | o->call_depth ? _(" (left unresolved)") : ""); |
b7fa51da | 1285 | if (o->call_depth) { |
c52ff85d | 1286 | struct merge_file_info mfi; |
6d63070c EN |
1287 | struct diff_filespec other; |
1288 | struct diff_filespec *add; | |
3c8a51e8 | 1289 | if (merge_file_one(o, one->path, |
b4da9d62 | 1290 | &one->oid, one->mode, |
1291 | &a->oid, a->mode, | |
1292 | &b->oid, b->mode, | |
3c8a51e8 | 1293 | ci->branch1, ci->branch2, &mfi)) |
75456f96 JS |
1294 | return -1; |
1295 | ||
9047ebbc | 1296 | /* |
c52ff85d EN |
1297 | * FIXME: For rename/add-source conflicts (if we could detect |
1298 | * such), this is wrong. We should instead find a unique | |
1299 | * pathname and then either rename the add-source file to that | |
1300 | * unique path, or use that unique path instead of src here. | |
9047ebbc | 1301 | */ |
75456f96 JS |
1302 | if (update_file(o, 0, &mfi.oid, mfi.mode, one->path)) |
1303 | return -1; | |
07413c5a | 1304 | |
6d63070c EN |
1305 | /* |
1306 | * Above, we put the merged content at the merge-base's | |
1307 | * path. Now we usually need to delete both a->path and | |
1308 | * b->path. However, the rename on each side of the merge | |
1309 | * could also be involved in a rename/add conflict. In | |
1310 | * such cases, we should keep the added file around, | |
1311 | * resolving the conflict at that path in its favor. | |
1312 | */ | |
1313 | add = filespec_from_entry(&other, ci->dst_entry1, 2 ^ 1); | |
75456f96 JS |
1314 | if (add) { |
1315 | if (update_file(o, 0, &add->oid, add->mode, a->path)) | |
1316 | return -1; | |
1317 | } | |
6d63070c EN |
1318 | else |
1319 | remove_file_from_cache(a->path); | |
1320 | add = filespec_from_entry(&other, ci->dst_entry2, 3 ^ 1); | |
75456f96 JS |
1321 | if (add) { |
1322 | if (update_file(o, 0, &add->oid, add->mode, b->path)) | |
1323 | return -1; | |
1324 | } | |
6d63070c EN |
1325 | else |
1326 | remove_file_from_cache(b->path); | |
75456f96 JS |
1327 | } else if (handle_file(o, a, 2, ci) || handle_file(o, b, 3, ci)) |
1328 | return -1; | |
1329 | ||
1330 | return 0; | |
9047ebbc MV |
1331 | } |
1332 | ||
75456f96 | 1333 | static int conflict_rename_rename_2to1(struct merge_options *o, |
461f5041 | 1334 | struct rename_conflict_info *ci) |
9047ebbc | 1335 | { |
461f5041 EN |
1336 | /* Two files, a & b, were renamed to the same thing, c. */ |
1337 | struct diff_filespec *a = ci->pair1->one; | |
1338 | struct diff_filespec *b = ci->pair2->one; | |
1339 | struct diff_filespec *c1 = ci->pair1->two; | |
1340 | struct diff_filespec *c2 = ci->pair2->two; | |
1341 | char *path = c1->path; /* == c2->path */ | |
434b8525 EN |
1342 | struct merge_file_info mfi_c1; |
1343 | struct merge_file_info mfi_c2; | |
75456f96 | 1344 | int ret; |
461f5041 | 1345 | |
55653a68 | 1346 | output(o, 1, _("CONFLICT (rename/rename): " |
461f5041 | 1347 | "Rename %s->%s in %s. " |
55653a68 | 1348 | "Rename %s->%s in %s"), |
461f5041 EN |
1349 | a->path, c1->path, ci->branch1, |
1350 | b->path, c2->path, ci->branch2); | |
1351 | ||
8e1b62f1 EN |
1352 | remove_file(o, 1, a->path, o->call_depth || would_lose_untracked(a->path)); |
1353 | remove_file(o, 1, b->path, o->call_depth || would_lose_untracked(b->path)); | |
461f5041 | 1354 | |
3c8a51e8 JS |
1355 | if (merge_file_special_markers(o, a, c1, &ci->ren1_other, |
1356 | o->branch1, c1->path, | |
1357 | o->branch2, ci->ren1_other.path, &mfi_c1) || | |
1358 | merge_file_special_markers(o, b, &ci->ren2_other, c2, | |
1359 | o->branch1, ci->ren2_other.path, | |
1360 | o->branch2, c2->path, &mfi_c2)) | |
75456f96 | 1361 | return -1; |
434b8525 | 1362 | |
0a6b8712 | 1363 | if (o->call_depth) { |
434b8525 EN |
1364 | /* |
1365 | * If mfi_c1.clean && mfi_c2.clean, then it might make | |
1366 | * sense to do a two-way merge of those results. But, I | |
1367 | * think in all cases, it makes sense to have the virtual | |
1368 | * merge base just undo the renames; they can be detected | |
1369 | * again later for the non-recursive merge. | |
1370 | */ | |
1371 | remove_file(o, 0, path, 0); | |
75456f96 JS |
1372 | ret = update_file(o, 0, &mfi_c1.oid, mfi_c1.mode, a->path); |
1373 | if (!ret) | |
1374 | ret = update_file(o, 0, &mfi_c2.oid, mfi_c2.mode, | |
1375 | b->path); | |
0a6b8712 | 1376 | } else { |
461f5041 EN |
1377 | char *new_path1 = unique_path(o, path, ci->branch1); |
1378 | char *new_path2 = unique_path(o, path, ci->branch2); | |
55653a68 | 1379 | output(o, 1, _("Renaming %s to %s and %s to %s instead"), |
461f5041 | 1380 | a->path, new_path1, b->path, new_path2); |
8b026eda | 1381 | remove_file(o, 0, path, 0); |
75456f96 JS |
1382 | ret = update_file(o, 0, &mfi_c1.oid, mfi_c1.mode, new_path1); |
1383 | if (!ret) | |
1384 | ret = update_file(o, 0, &mfi_c2.oid, mfi_c2.mode, | |
1385 | new_path2); | |
0a6b8712 EN |
1386 | free(new_path2); |
1387 | free(new_path1); | |
1388 | } | |
75456f96 JS |
1389 | |
1390 | return ret; | |
9047ebbc MV |
1391 | } |
1392 | ||
8a2fce18 MV |
1393 | static int process_renames(struct merge_options *o, |
1394 | struct string_list *a_renames, | |
1395 | struct string_list *b_renames) | |
9047ebbc MV |
1396 | { |
1397 | int clean_merge = 1, i, j; | |
183113a5 TF |
1398 | struct string_list a_by_dst = STRING_LIST_INIT_NODUP; |
1399 | struct string_list b_by_dst = STRING_LIST_INIT_NODUP; | |
9047ebbc MV |
1400 | const struct rename *sre; |
1401 | ||
1402 | for (i = 0; i < a_renames->nr; i++) { | |
1403 | sre = a_renames->items[i].util; | |
78a395d3 | 1404 | string_list_insert(&a_by_dst, sre->pair->two->path)->util |
0a6b8712 | 1405 | = (void *)sre; |
9047ebbc MV |
1406 | } |
1407 | for (i = 0; i < b_renames->nr; i++) { | |
1408 | sre = b_renames->items[i].util; | |
78a395d3 | 1409 | string_list_insert(&b_by_dst, sre->pair->two->path)->util |
0a6b8712 | 1410 | = (void *)sre; |
9047ebbc MV |
1411 | } |
1412 | ||
1413 | for (i = 0, j = 0; i < a_renames->nr || j < b_renames->nr;) { | |
8e24cbae | 1414 | struct string_list *renames1, *renames2Dst; |
9047ebbc MV |
1415 | struct rename *ren1 = NULL, *ren2 = NULL; |
1416 | const char *branch1, *branch2; | |
1417 | const char *ren1_src, *ren1_dst; | |
461f5041 | 1418 | struct string_list_item *lookup; |
9047ebbc MV |
1419 | |
1420 | if (i >= a_renames->nr) { | |
9047ebbc MV |
1421 | ren2 = b_renames->items[j++].util; |
1422 | } else if (j >= b_renames->nr) { | |
9047ebbc MV |
1423 | ren1 = a_renames->items[i++].util; |
1424 | } else { | |
8e24cbae BK |
1425 | int compare = strcmp(a_renames->items[i].string, |
1426 | b_renames->items[j].string); | |
9047ebbc MV |
1427 | if (compare <= 0) |
1428 | ren1 = a_renames->items[i++].util; | |
1429 | if (compare >= 0) | |
1430 | ren2 = b_renames->items[j++].util; | |
1431 | } | |
1432 | ||
1433 | /* TODO: refactor, so that 1/2 are not needed */ | |
1434 | if (ren1) { | |
1435 | renames1 = a_renames; | |
9047ebbc | 1436 | renames2Dst = &b_by_dst; |
8a2fce18 MV |
1437 | branch1 = o->branch1; |
1438 | branch2 = o->branch2; | |
9047ebbc | 1439 | } else { |
9047ebbc | 1440 | renames1 = b_renames; |
9047ebbc | 1441 | renames2Dst = &a_by_dst; |
8a2fce18 MV |
1442 | branch1 = o->branch2; |
1443 | branch2 = o->branch1; | |
35d803bc | 1444 | SWAP(ren2, ren1); |
9047ebbc | 1445 | } |
9047ebbc | 1446 | |
9047ebbc MV |
1447 | if (ren1->processed) |
1448 | continue; | |
1449 | ren1->processed = 1; | |
9047ebbc | 1450 | ren1->dst_entry->processed = 1; |
7769a75e EN |
1451 | /* BUG: We should only mark src_entry as processed if we |
1452 | * are not dealing with a rename + add-source case. | |
1453 | */ | |
9047ebbc | 1454 | ren1->src_entry->processed = 1; |
9047ebbc MV |
1455 | |
1456 | ren1_src = ren1->pair->one->path; | |
1457 | ren1_dst = ren1->pair->two->path; | |
1458 | ||
1459 | if (ren2) { | |
461f5041 | 1460 | /* One file renamed on both sides */ |
9047ebbc MV |
1461 | const char *ren2_src = ren2->pair->one->path; |
1462 | const char *ren2_dst = ren2->pair->two->path; | |
4f66dade | 1463 | enum rename_type rename_type; |
9047ebbc | 1464 | if (strcmp(ren1_src, ren2_src) != 0) |
ef1177d1 | 1465 | die("BUG: ren1_src != ren2_src"); |
9047ebbc MV |
1466 | ren2->dst_entry->processed = 1; |
1467 | ren2->processed = 1; | |
1468 | if (strcmp(ren1_dst, ren2_dst) != 0) { | |
4f66dade | 1469 | rename_type = RENAME_ONE_FILE_TO_TWO; |
461f5041 | 1470 | clean_merge = 0; |
9047ebbc | 1471 | } else { |
4f66dade | 1472 | rename_type = RENAME_ONE_FILE_TO_ONE; |
7769a75e EN |
1473 | /* BUG: We should only remove ren1_src in |
1474 | * the base stage (think of rename + | |
1475 | * add-source cases). | |
1476 | */ | |
b7fa51da | 1477 | remove_file(o, 1, ren1_src, 1); |
b8ddf164 EN |
1478 | update_entry(ren1->dst_entry, |
1479 | ren1->pair->one, | |
1480 | ren1->pair->two, | |
1481 | ren2->pair->two); | |
9047ebbc | 1482 | } |
4f66dade EN |
1483 | setup_rename_conflict_info(rename_type, |
1484 | ren1->pair, | |
1485 | ren2->pair, | |
1486 | branch1, | |
1487 | branch2, | |
1488 | ren1->dst_entry, | |
232c635f EN |
1489 | ren2->dst_entry, |
1490 | o, | |
1491 | NULL, | |
1492 | NULL); | |
461f5041 EN |
1493 | } else if ((lookup = string_list_lookup(renames2Dst, ren1_dst))) { |
1494 | /* Two different files renamed to the same thing */ | |
1495 | char *ren2_dst; | |
1496 | ren2 = lookup->util; | |
1497 | ren2_dst = ren2->pair->two->path; | |
1498 | if (strcmp(ren1_dst, ren2_dst) != 0) | |
ef1177d1 | 1499 | die("BUG: ren1_dst != ren2_dst"); |
461f5041 EN |
1500 | |
1501 | clean_merge = 0; | |
1502 | ren2->processed = 1; | |
1503 | /* | |
1504 | * BUG: We should only mark src_entry as processed | |
1505 | * if we are not dealing with a rename + add-source | |
1506 | * case. | |
1507 | */ | |
1508 | ren2->src_entry->processed = 1; | |
1509 | ||
1510 | setup_rename_conflict_info(RENAME_TWO_FILES_TO_ONE, | |
1511 | ren1->pair, | |
1512 | ren2->pair, | |
1513 | branch1, | |
1514 | branch2, | |
1515 | ren1->dst_entry, | |
232c635f EN |
1516 | ren2->dst_entry, |
1517 | o, | |
1518 | ren1->src_entry, | |
1519 | ren2->src_entry); | |
1520 | ||
9047ebbc MV |
1521 | } else { |
1522 | /* Renamed in 1, maybe changed in 2 */ | |
9047ebbc MV |
1523 | /* we only use sha1 and mode of these */ |
1524 | struct diff_filespec src_other, dst_other; | |
41d70bd6 | 1525 | int try_merge; |
9047ebbc | 1526 | |
41d70bd6 EN |
1527 | /* |
1528 | * unpack_trees loads entries from common-commit | |
1529 | * into stage 1, from head-commit into stage 2, and | |
1530 | * from merge-commit into stage 3. We keep track | |
1531 | * of which side corresponds to the rename. | |
1532 | */ | |
1533 | int renamed_stage = a_renames == renames1 ? 2 : 3; | |
1534 | int other_stage = a_renames == renames1 ? 3 : 2; | |
9047ebbc | 1535 | |
7769a75e EN |
1536 | /* BUG: We should only remove ren1_src in the base |
1537 | * stage and in other_stage (think of rename + | |
1538 | * add-source case). | |
1539 | */ | |
531357a4 EN |
1540 | remove_file(o, 1, ren1_src, |
1541 | renamed_stage == 2 || !was_tracked(ren1_src)); | |
9047ebbc | 1542 | |
fd429e98 | 1543 | oidcpy(&src_other.oid, |
1544 | &ren1->src_entry->stages[other_stage].oid); | |
41d70bd6 | 1545 | src_other.mode = ren1->src_entry->stages[other_stage].mode; |
fd429e98 | 1546 | oidcpy(&dst_other.oid, |
1547 | &ren1->dst_entry->stages[other_stage].oid); | |
41d70bd6 | 1548 | dst_other.mode = ren1->dst_entry->stages[other_stage].mode; |
9047ebbc MV |
1549 | try_merge = 0; |
1550 | ||
8b026eda | 1551 | if (oid_eq(&src_other.oid, &null_oid)) { |
4f66dade EN |
1552 | setup_rename_conflict_info(RENAME_DELETE, |
1553 | ren1->pair, | |
1554 | NULL, | |
1555 | branch1, | |
1556 | branch2, | |
1557 | ren1->dst_entry, | |
232c635f EN |
1558 | NULL, |
1559 | o, | |
1560 | NULL, | |
4f66dade | 1561 | NULL); |
d5af5105 | 1562 | } else if ((dst_other.mode == ren1->pair->two->mode) && |
b4da9d62 | 1563 | oid_eq(&dst_other.oid, &ren1->pair->two->oid)) { |
35a74abf EN |
1564 | /* |
1565 | * Added file on the other side identical to | |
1566 | * the file being renamed: clean merge. | |
1567 | * Also, there is no need to overwrite the | |
1568 | * file already in the working copy, so call | |
1569 | * update_file_flags() instead of | |
1570 | * update_file(). | |
1571 | */ | |
75456f96 JS |
1572 | if (update_file_flags(o, |
1573 | &ren1->pair->two->oid, | |
1574 | ren1->pair->two->mode, | |
1575 | ren1_dst, | |
1576 | 1, /* update_cache */ | |
1577 | 0 /* update_wd */)) | |
1578 | clean_merge = -1; | |
b4da9d62 | 1579 | } else if (!oid_eq(&dst_other.oid, &null_oid)) { |
9047ebbc MV |
1580 | clean_merge = 0; |
1581 | try_merge = 1; | |
55653a68 JX |
1582 | output(o, 1, _("CONFLICT (rename/add): Rename %s->%s in %s. " |
1583 | "%s added in %s"), | |
9047ebbc MV |
1584 | ren1_src, ren1_dst, branch1, |
1585 | ren1_dst, branch2); | |
c94736a2 JH |
1586 | if (o->call_depth) { |
1587 | struct merge_file_info mfi; | |
3c8a51e8 JS |
1588 | if (merge_file_one(o, ren1_dst, &null_oid, 0, |
1589 | &ren1->pair->two->oid, | |
1590 | ren1->pair->two->mode, | |
1591 | &dst_other.oid, | |
1592 | dst_other.mode, | |
75456f96 JS |
1593 | branch1, branch2, &mfi)) { |
1594 | clean_merge = -1; | |
1595 | goto cleanup_and_return; | |
1596 | } | |
55653a68 | 1597 | output(o, 1, _("Adding merged %s"), ren1_dst); |
75456f96 JS |
1598 | if (update_file(o, 0, &mfi.oid, |
1599 | mfi.mode, ren1_dst)) | |
1600 | clean_merge = -1; | |
2a669c34 | 1601 | try_merge = 0; |
c94736a2 | 1602 | } else { |
3d6b8e88 | 1603 | char *new_path = unique_path(o, ren1_dst, branch2); |
55653a68 | 1604 | output(o, 1, _("Adding as %s instead"), new_path); |
75456f96 JS |
1605 | if (update_file(o, 0, &dst_other.oid, |
1606 | dst_other.mode, new_path)) | |
1607 | clean_merge = -1; | |
3d6b8e88 | 1608 | free(new_path); |
c94736a2 | 1609 | } |
9047ebbc MV |
1610 | } else |
1611 | try_merge = 1; | |
1612 | ||
75456f96 JS |
1613 | if (clean_merge < 0) |
1614 | goto cleanup_and_return; | |
9047ebbc | 1615 | if (try_merge) { |
8a2fce18 | 1616 | struct diff_filespec *one, *a, *b; |
9047ebbc MV |
1617 | src_other.path = (char *)ren1_src; |
1618 | ||
8a2fce18 | 1619 | one = ren1->pair->one; |
9047ebbc MV |
1620 | if (a_renames == renames1) { |
1621 | a = ren1->pair->two; | |
1622 | b = &src_other; | |
1623 | } else { | |
1624 | b = ren1->pair->two; | |
1625 | a = &src_other; | |
1626 | } | |
b8ddf164 | 1627 | update_entry(ren1->dst_entry, one, a, b); |
4f66dade EN |
1628 | setup_rename_conflict_info(RENAME_NORMAL, |
1629 | ren1->pair, | |
1630 | NULL, | |
1631 | branch1, | |
1632 | NULL, | |
1633 | ren1->dst_entry, | |
232c635f EN |
1634 | NULL, |
1635 | o, | |
1636 | NULL, | |
4f66dade | 1637 | NULL); |
9047ebbc MV |
1638 | } |
1639 | } | |
1640 | } | |
75456f96 | 1641 | cleanup_and_return: |
9047ebbc MV |
1642 | string_list_clear(&a_by_dst, 0); |
1643 | string_list_clear(&b_by_dst, 0); | |
1644 | ||
1645 | return clean_merge; | |
1646 | } | |
1647 | ||
b4da9d62 | 1648 | static struct object_id *stage_oid(const struct object_id *oid, unsigned mode) |
9047ebbc | 1649 | { |
b4da9d62 | 1650 | return (is_null_oid(oid) || mode == 0) ? NULL: (struct object_id *)oid; |
9047ebbc MV |
1651 | } |
1652 | ||
bc9204d4 JS |
1653 | static int read_oid_strbuf(struct merge_options *o, |
1654 | const struct object_id *oid, struct strbuf *dst) | |
331a1838 EB |
1655 | { |
1656 | void *buf; | |
1657 | enum object_type type; | |
1658 | unsigned long size; | |
b4f5aca4 | 1659 | buf = read_object_file(oid, &type, &size); |
331a1838 | 1660 | if (!buf) |
bc9204d4 | 1661 | return err(o, _("cannot read object %s"), oid_to_hex(oid)); |
331a1838 EB |
1662 | if (type != OBJ_BLOB) { |
1663 | free(buf); | |
bc9204d4 | 1664 | return err(o, _("object %s is not a blob"), oid_to_hex(oid)); |
331a1838 EB |
1665 | } |
1666 | strbuf_attach(dst, buf, size, size + 1); | |
1667 | return 0; | |
1668 | } | |
1669 | ||
bc9204d4 JS |
1670 | static int blob_unchanged(struct merge_options *opt, |
1671 | const struct object_id *o_oid, | |
72fac66b | 1672 | unsigned o_mode, |
b4da9d62 | 1673 | const struct object_id *a_oid, |
72fac66b | 1674 | unsigned a_mode, |
3e7589b7 | 1675 | int renormalize, const char *path) |
331a1838 EB |
1676 | { |
1677 | struct strbuf o = STRBUF_INIT; | |
1678 | struct strbuf a = STRBUF_INIT; | |
1679 | int ret = 0; /* assume changed for safety */ | |
1680 | ||
72fac66b JK |
1681 | if (a_mode != o_mode) |
1682 | return 0; | |
b4da9d62 | 1683 | if (oid_eq(o_oid, a_oid)) |
331a1838 | 1684 | return 1; |
3e7589b7 | 1685 | if (!renormalize) |
331a1838 EB |
1686 | return 0; |
1687 | ||
b4da9d62 | 1688 | assert(o_oid && a_oid); |
bc9204d4 | 1689 | if (read_oid_strbuf(opt, o_oid, &o) || read_oid_strbuf(opt, a_oid, &a)) |
331a1838 EB |
1690 | goto error_return; |
1691 | /* | |
1692 | * Note: binary | is used so that both renormalizations are | |
1693 | * performed. Comparison can be skipped if both files are | |
1694 | * unchanged since their sha1s have already been compared. | |
1695 | */ | |
a33e0b2a BW |
1696 | if (renormalize_buffer(&the_index, path, o.buf, o.len, &o) | |
1697 | renormalize_buffer(&the_index, path, a.buf, a.len, &a)) | |
331a1838 EB |
1698 | ret = (o.len == a.len && !memcmp(o.buf, a.buf, o.len)); |
1699 | ||
1700 | error_return: | |
1701 | strbuf_release(&o); | |
1702 | strbuf_release(&a); | |
1703 | return ret; | |
1704 | } | |
1705 | ||
75456f96 | 1706 | static int handle_modify_delete(struct merge_options *o, |
5e3ce663 | 1707 | const char *path, |
b4da9d62 | 1708 | struct object_id *o_oid, int o_mode, |
1709 | struct object_id *a_oid, int a_mode, | |
1710 | struct object_id *b_oid, int b_mode) | |
5e3ce663 | 1711 | { |
b26d87f2 MM |
1712 | const char *modify_branch, *delete_branch; |
1713 | struct object_id *changed_oid; | |
1714 | int changed_mode; | |
1715 | ||
1716 | if (a_oid) { | |
1717 | modify_branch = o->branch1; | |
1718 | delete_branch = o->branch2; | |
1719 | changed_oid = a_oid; | |
1720 | changed_mode = a_mode; | |
1721 | } else { | |
1722 | modify_branch = o->branch2; | |
1723 | delete_branch = o->branch1; | |
1724 | changed_oid = b_oid; | |
1725 | changed_mode = b_mode; | |
1726 | } | |
1727 | ||
75456f96 | 1728 | return handle_change_delete(o, |
b26d87f2 | 1729 | path, NULL, |
75456f96 | 1730 | o_oid, o_mode, |
b26d87f2 MM |
1731 | changed_oid, changed_mode, |
1732 | modify_branch, delete_branch, | |
75456f96 | 1733 | _("modify"), _("modified")); |
5e3ce663 EN |
1734 | } |
1735 | ||
0c4918d1 EN |
1736 | static int merge_content(struct merge_options *o, |
1737 | const char *path, | |
b4da9d62 | 1738 | struct object_id *o_oid, int o_mode, |
1739 | struct object_id *a_oid, int a_mode, | |
1740 | struct object_id *b_oid, int b_mode, | |
4f66dade | 1741 | struct rename_conflict_info *rename_conflict_info) |
0c4918d1 | 1742 | { |
55653a68 | 1743 | const char *reason = _("content"); |
5b448b85 | 1744 | const char *path1 = NULL, *path2 = NULL; |
0c4918d1 EN |
1745 | struct merge_file_info mfi; |
1746 | struct diff_filespec one, a, b; | |
4ab9a157 | 1747 | unsigned df_conflict_remains = 0; |
0c4918d1 | 1748 | |
b4da9d62 | 1749 | if (!o_oid) { |
55653a68 | 1750 | reason = _("add/add"); |
b4da9d62 | 1751 | o_oid = (struct object_id *)&null_oid; |
0c4918d1 EN |
1752 | } |
1753 | one.path = a.path = b.path = (char *)path; | |
b4da9d62 | 1754 | oidcpy(&one.oid, o_oid); |
0c4918d1 | 1755 | one.mode = o_mode; |
b4da9d62 | 1756 | oidcpy(&a.oid, a_oid); |
0c4918d1 | 1757 | a.mode = a_mode; |
b4da9d62 | 1758 | oidcpy(&b.oid, b_oid); |
0c4918d1 EN |
1759 | b.mode = b_mode; |
1760 | ||
3c217c07 | 1761 | if (rename_conflict_info) { |
3c217c07 EN |
1762 | struct diff_filepair *pair1 = rename_conflict_info->pair1; |
1763 | ||
1764 | path1 = (o->branch1 == rename_conflict_info->branch1) ? | |
1765 | pair1->two->path : pair1->one->path; | |
1766 | /* If rename_conflict_info->pair2 != NULL, we are in | |
1767 | * RENAME_ONE_FILE_TO_ONE case. Otherwise, we have a | |
1768 | * normal rename. | |
1769 | */ | |
1770 | path2 = (rename_conflict_info->pair2 || | |
1771 | o->branch2 == rename_conflict_info->branch1) ? | |
1772 | pair1->two->path : pair1->one->path; | |
3c217c07 | 1773 | |
5423d2e7 DT |
1774 | if (dir_in_way(path, !o->call_depth, |
1775 | S_ISGITLINK(pair1->two->mode))) | |
3c217c07 | 1776 | df_conflict_remains = 1; |
4ab9a157 | 1777 | } |
3c8a51e8 JS |
1778 | if (merge_file_special_markers(o, &one, &a, &b, |
1779 | o->branch1, path1, | |
1780 | o->branch2, path2, &mfi)) | |
1781 | return -1; | |
4ab9a157 EN |
1782 | |
1783 | if (mfi.clean && !df_conflict_remains && | |
b4da9d62 | 1784 | oid_eq(&mfi.oid, a_oid) && mfi.mode == a_mode) { |
8b026eda | 1785 | int path_renamed_outside_HEAD; |
55653a68 | 1786 | output(o, 3, _("Skipped %s (merged same as existing)"), path); |
5b448b85 EN |
1787 | /* |
1788 | * The content merge resulted in the same file contents we | |
1789 | * already had. We can return early if those file contents | |
1790 | * are recorded at the correct path (which may not be true | |
1791 | * if the merge involves a rename). | |
1792 | */ | |
8b026eda JH |
1793 | path_renamed_outside_HEAD = !path2 || !strcmp(path, path2); |
1794 | if (!path_renamed_outside_HEAD) { | |
bc9204d4 | 1795 | add_cacheinfo(o, mfi.mode, &mfi.oid, path, |
d45b7f40 | 1796 | 0, (!o->call_depth), 0); |
5b448b85 EN |
1797 | return mfi.clean; |
1798 | } | |
1799 | } else | |
55653a68 | 1800 | output(o, 2, _("Auto-merging %s"), path); |
0c4918d1 EN |
1801 | |
1802 | if (!mfi.clean) { | |
1803 | if (S_ISGITLINK(mfi.mode)) | |
55653a68 JX |
1804 | reason = _("submodule"); |
1805 | output(o, 1, _("CONFLICT (%s): Merge conflict in %s"), | |
0c4918d1 | 1806 | reason, path); |
4f66dade | 1807 | if (rename_conflict_info && !df_conflict_remains) |
bc9204d4 | 1808 | if (update_stages(o, path, &one, &a, &b)) |
75456f96 | 1809 | return -1; |
0c4918d1 EN |
1810 | } |
1811 | ||
8b026eda | 1812 | if (df_conflict_remains) { |
3d6b8e88 | 1813 | char *new_path; |
51931bf0 EN |
1814 | if (o->call_depth) { |
1815 | remove_file_from_cache(path); | |
1816 | } else { | |
75456f96 | 1817 | if (!mfi.clean) { |
bc9204d4 | 1818 | if (update_stages(o, path, &one, &a, &b)) |
75456f96 JS |
1819 | return -1; |
1820 | } else { | |
51931bf0 EN |
1821 | int file_from_stage2 = was_tracked(path); |
1822 | struct diff_filespec merged; | |
9b561499 | 1823 | oidcpy(&merged.oid, &mfi.oid); |
51931bf0 EN |
1824 | merged.mode = mfi.mode; |
1825 | ||
bc9204d4 | 1826 | if (update_stages(o, path, NULL, |
75456f96 JS |
1827 | file_from_stage2 ? &merged : NULL, |
1828 | file_from_stage2 ? NULL : &merged)) | |
1829 | return -1; | |
51931bf0 EN |
1830 | } |
1831 | ||
1832 | } | |
4f66dade | 1833 | new_path = unique_path(o, path, rename_conflict_info->branch1); |
55653a68 | 1834 | output(o, 1, _("Adding as %s instead"), new_path); |
75456f96 JS |
1835 | if (update_file(o, 0, &mfi.oid, mfi.mode, new_path)) { |
1836 | free(new_path); | |
1837 | return -1; | |
1838 | } | |
3d6b8e88 | 1839 | free(new_path); |
51931bf0 | 1840 | mfi.clean = 0; |
75456f96 JS |
1841 | } else if (update_file(o, mfi.clean, &mfi.oid, mfi.mode, path)) |
1842 | return -1; | |
0c4918d1 | 1843 | return mfi.clean; |
0c4918d1 EN |
1844 | } |
1845 | ||
9047ebbc | 1846 | /* Per entry merge function */ |
8a2fce18 MV |
1847 | static int process_entry(struct merge_options *o, |
1848 | const char *path, struct stage_data *entry) | |
9047ebbc | 1849 | { |
9047ebbc | 1850 | int clean_merge = 1; |
1bc0ab7c | 1851 | int normalize = o->renormalize; |
9047ebbc MV |
1852 | unsigned o_mode = entry->stages[1].mode; |
1853 | unsigned a_mode = entry->stages[2].mode; | |
1854 | unsigned b_mode = entry->stages[3].mode; | |
b4da9d62 | 1855 | struct object_id *o_oid = stage_oid(&entry->stages[1].oid, o_mode); |
1856 | struct object_id *a_oid = stage_oid(&entry->stages[2].oid, a_mode); | |
1857 | struct object_id *b_oid = stage_oid(&entry->stages[3].oid, b_mode); | |
9047ebbc | 1858 | |
37348937 | 1859 | entry->processed = 1; |
4f66dade EN |
1860 | if (entry->rename_conflict_info) { |
1861 | struct rename_conflict_info *conflict_info = entry->rename_conflict_info; | |
07413c5a | 1862 | switch (conflict_info->rename_type) { |
882fd11a | 1863 | case RENAME_NORMAL: |
4f66dade | 1864 | case RENAME_ONE_FILE_TO_ONE: |
8b026eda JH |
1865 | clean_merge = merge_content(o, path, |
1866 | o_oid, o_mode, a_oid, a_mode, b_oid, b_mode, | |
1867 | conflict_info); | |
882fd11a | 1868 | break; |
3b130adf EN |
1869 | case RENAME_DELETE: |
1870 | clean_merge = 0; | |
75456f96 JS |
1871 | if (conflict_rename_delete(o, |
1872 | conflict_info->pair1, | |
1873 | conflict_info->branch1, | |
1874 | conflict_info->branch2)) | |
1875 | clean_merge = -1; | |
3b130adf | 1876 | break; |
07413c5a | 1877 | case RENAME_ONE_FILE_TO_TWO: |
07413c5a | 1878 | clean_merge = 0; |
75456f96 JS |
1879 | if (conflict_rename_rename_1to2(o, conflict_info)) |
1880 | clean_merge = -1; | |
07413c5a | 1881 | break; |
461f5041 EN |
1882 | case RENAME_TWO_FILES_TO_ONE: |
1883 | clean_merge = 0; | |
75456f96 JS |
1884 | if (conflict_rename_rename_2to1(o, conflict_info)) |
1885 | clean_merge = -1; | |
07413c5a EN |
1886 | break; |
1887 | default: | |
1888 | entry->processed = 0; | |
1889 | break; | |
1890 | } | |
b4da9d62 | 1891 | } else if (o_oid && (!a_oid || !b_oid)) { |
edd2faf5 | 1892 | /* Case A: Deleted in one */ |
b4da9d62 | 1893 | if ((!a_oid && !b_oid) || |
bc9204d4 JS |
1894 | (!b_oid && blob_unchanged(o, o_oid, o_mode, a_oid, a_mode, normalize, path)) || |
1895 | (!a_oid && blob_unchanged(o, o_oid, o_mode, b_oid, b_mode, normalize, path))) { | |
edd2faf5 EN |
1896 | /* Deleted in both or deleted in one and |
1897 | * unchanged in the other */ | |
b4da9d62 | 1898 | if (a_oid) |
55653a68 | 1899 | output(o, 2, _("Removing %s"), path); |
edd2faf5 | 1900 | /* do not touch working file if it did not exist */ |
b4da9d62 | 1901 | remove_file(o, 1, path, !a_oid); |
edd2faf5 EN |
1902 | } else { |
1903 | /* Modify/delete; deleted side may have put a directory in the way */ | |
edd2faf5 | 1904 | clean_merge = 0; |
75456f96 JS |
1905 | if (handle_modify_delete(o, path, o_oid, o_mode, |
1906 | a_oid, a_mode, b_oid, b_mode)) | |
1907 | clean_merge = -1; | |
3d6b8e88 | 1908 | } |
b4da9d62 | 1909 | } else if ((!o_oid && a_oid && !b_oid) || |
1910 | (!o_oid && !a_oid && b_oid)) { | |
edd2faf5 EN |
1911 | /* Case B: Added in one. */ |
1912 | /* [nothing|directory] -> ([nothing|directory], file) */ | |
1913 | ||
9c0bbb50 EN |
1914 | const char *add_branch; |
1915 | const char *other_branch; | |
1916 | unsigned mode; | |
b4da9d62 | 1917 | const struct object_id *oid; |
9c0bbb50 | 1918 | const char *conf; |
37348937 | 1919 | |
b4da9d62 | 1920 | if (a_oid) { |
9c0bbb50 EN |
1921 | add_branch = o->branch1; |
1922 | other_branch = o->branch2; | |
1923 | mode = a_mode; | |
b4da9d62 | 1924 | oid = a_oid; |
55653a68 | 1925 | conf = _("file/directory"); |
9c0bbb50 EN |
1926 | } else { |
1927 | add_branch = o->branch2; | |
1928 | other_branch = o->branch1; | |
1929 | mode = b_mode; | |
b4da9d62 | 1930 | oid = b_oid; |
55653a68 | 1931 | conf = _("directory/file"); |
9c0bbb50 | 1932 | } |
c641ca67 EN |
1933 | if (dir_in_way(path, |
1934 | !o->call_depth && !S_ISGITLINK(a_mode), | |
1935 | 0)) { | |
3d6b8e88 | 1936 | char *new_path = unique_path(o, path, add_branch); |
9c0bbb50 | 1937 | clean_merge = 0; |
55653a68 JX |
1938 | output(o, 1, _("CONFLICT (%s): There is a directory with name %s in %s. " |
1939 | "Adding %s as %s"), | |
9c0bbb50 | 1940 | conf, path, other_branch, path, new_path); |
75456f96 JS |
1941 | if (update_file(o, 0, oid, mode, new_path)) |
1942 | clean_merge = -1; | |
1943 | else if (o->call_depth) | |
7b1c610f | 1944 | remove_file_from_cache(path); |
3d6b8e88 | 1945 | free(new_path); |
9c0bbb50 | 1946 | } else { |
55653a68 | 1947 | output(o, 2, _("Adding %s"), path); |
35a74abf | 1948 | /* do not overwrite file if already present */ |
75456f96 JS |
1949 | if (update_file_flags(o, oid, mode, path, 1, !a_oid)) |
1950 | clean_merge = -1; | |
9c0bbb50 | 1951 | } |
b4da9d62 | 1952 | } else if (a_oid && b_oid) { |
edd2faf5 EN |
1953 | /* Case C: Added in both (check for same permissions) and */ |
1954 | /* case D: Modified in both, but differently. */ | |
8b026eda | 1955 | clean_merge = merge_content(o, path, |
b4da9d62 | 1956 | o_oid, o_mode, a_oid, a_mode, b_oid, b_mode, |
edd2faf5 | 1957 | NULL); |
b4da9d62 | 1958 | } else if (!o_oid && !a_oid && !b_oid) { |
edd2faf5 EN |
1959 | /* |
1960 | * this entry was deleted altogether. a_mode == 0 means | |
1961 | * we had that path and want to actively remove it. | |
1962 | */ | |
1963 | remove_file(o, 1, path, !a_mode); | |
1964 | } else | |
7e97e100 | 1965 | die("BUG: fatal merge failure, shouldn't happen."); |
37348937 EN |
1966 | |
1967 | return clean_merge; | |
1968 | } | |
1969 | ||
8a2fce18 MV |
1970 | int merge_trees(struct merge_options *o, |
1971 | struct tree *head, | |
9047ebbc MV |
1972 | struct tree *merge, |
1973 | struct tree *common, | |
9047ebbc MV |
1974 | struct tree **result) |
1975 | { | |
1976 | int code, clean; | |
1977 | ||
85e51b78 JH |
1978 | if (o->subtree_shift) { |
1979 | merge = shift_tree_object(head, merge, o->subtree_shift); | |
1980 | common = shift_tree_object(head, common, o->subtree_shift); | |
9047ebbc MV |
1981 | } |
1982 | ||
b4da9d62 | 1983 | if (oid_eq(&common->object.oid, &merge->object.oid)) { |
65170c07 EN |
1984 | struct strbuf sb = STRBUF_INIT; |
1985 | ||
f309e8e7 | 1986 | if (!o->call_depth && index_has_changes(&sb)) { |
65170c07 EN |
1987 | err(o, _("Dirty index: cannot merge (dirty: %s)"), |
1988 | sb.buf); | |
1989 | return 0; | |
1990 | } | |
7560f547 | 1991 | output(o, 0, _("Already up to date!")); |
9047ebbc MV |
1992 | *result = head; |
1993 | return 1; | |
1994 | } | |
1995 | ||
8b026eda | 1996 | code = git_merge_trees(o->call_depth, common, head, merge); |
9047ebbc | 1997 | |
fadd069d JH |
1998 | if (code != 0) { |
1999 | if (show(o, 4) || o->call_depth) | |
bc9204d4 | 2000 | err(o, _("merging of trees %s and %s failed"), |
f2fd0760 | 2001 | oid_to_hex(&head->object.oid), |
2002 | oid_to_hex(&merge->object.oid)); | |
6003303a | 2003 | return -1; |
fadd069d | 2004 | } |
9047ebbc MV |
2005 | |
2006 | if (unmerged_cache()) { | |
8b026eda | 2007 | struct string_list *entries, *re_head, *re_merge; |
9047ebbc | 2008 | int i; |
fc65b00d KW |
2009 | /* |
2010 | * Only need the hashmap while processing entries, so | |
2011 | * initialize it here and free it when we are done running | |
2012 | * through the entries. Keeping it in the merge_options as | |
2013 | * opposed to decaring a local hashmap is for convenience | |
2014 | * so that we don't have to pass it to around. | |
2015 | */ | |
2016 | hashmap_init(&o->current_file_dir_set, path_hashmap_cmp, NULL, 512); | |
696ee23c MV |
2017 | get_files_dirs(o, head); |
2018 | get_files_dirs(o, merge); | |
9047ebbc MV |
2019 | |
2020 | entries = get_unmerged(); | |
8b026eda JH |
2021 | re_head = get_renames(o, head, common, head, merge, entries); |
2022 | re_merge = get_renames(o, merge, common, head, merge, entries); | |
2023 | clean = process_renames(o, re_head, re_merge); | |
6c8647da | 2024 | record_df_conflict_files(o, entries); |
75456f96 | 2025 | if (clean < 0) |
e336bdc5 | 2026 | goto cleanup; |
edd2faf5 | 2027 | for (i = entries->nr-1; 0 <= i; i--) { |
9047ebbc MV |
2028 | const char *path = entries->items[i].string; |
2029 | struct stage_data *e = entries->items[i].util; | |
75456f96 JS |
2030 | if (!e->processed) { |
2031 | int ret = process_entry(o, path, e); | |
2032 | if (!ret) | |
2033 | clean = 0; | |
e336bdc5 KW |
2034 | else if (ret < 0) { |
2035 | clean = ret; | |
2036 | goto cleanup; | |
2037 | } | |
75456f96 | 2038 | } |
9047ebbc | 2039 | } |
7edba4c4 JH |
2040 | for (i = 0; i < entries->nr; i++) { |
2041 | struct stage_data *e = entries->items[i].util; | |
2042 | if (!e->processed) | |
7e97e100 | 2043 | die("BUG: unprocessed path??? %s", |
7edba4c4 JH |
2044 | entries->items[i].string); |
2045 | } | |
9047ebbc | 2046 | |
e336bdc5 | 2047 | cleanup: |
8b026eda JH |
2048 | string_list_clear(re_merge, 0); |
2049 | string_list_clear(re_head, 0); | |
9047ebbc MV |
2050 | string_list_clear(entries, 1); |
2051 | ||
fc65b00d KW |
2052 | hashmap_free(&o->current_file_dir_set, 1); |
2053 | ||
8b026eda JH |
2054 | free(re_merge); |
2055 | free(re_head); | |
2056 | free(entries); | |
2057 | ||
e336bdc5 KW |
2058 | if (clean < 0) |
2059 | return clean; | |
9047ebbc MV |
2060 | } |
2061 | else | |
2062 | clean = 1; | |
2063 | ||
fbc87eb5 JS |
2064 | if (o->call_depth && !(*result = write_tree_from_memory(o))) |
2065 | return -1; | |
9047ebbc MV |
2066 | |
2067 | return clean; | |
2068 | } | |
2069 | ||
2070 | static struct commit_list *reverse_commit_list(struct commit_list *list) | |
2071 | { | |
2072 | struct commit_list *next = NULL, *current, *backup; | |
2073 | for (current = list; current; current = backup) { | |
2074 | backup = current->next; | |
2075 | current->next = next; | |
2076 | next = current; | |
2077 | } | |
2078 | return next; | |
2079 | } | |
2080 | ||
2081 | /* | |
2082 | * Merge the commits h1 and h2, return the resulting virtual | |
2083 | * commit object and a flag indicating the cleanness of the merge. | |
2084 | */ | |
8a2fce18 MV |
2085 | int merge_recursive(struct merge_options *o, |
2086 | struct commit *h1, | |
9047ebbc | 2087 | struct commit *h2, |
9047ebbc MV |
2088 | struct commit_list *ca, |
2089 | struct commit **result) | |
2090 | { | |
2091 | struct commit_list *iter; | |
2092 | struct commit *merged_common_ancestors; | |
156e1782 | 2093 | struct tree *mrtree; |
9047ebbc MV |
2094 | int clean; |
2095 | ||
8a2fce18 | 2096 | if (show(o, 4)) { |
55653a68 | 2097 | output(o, 4, _("Merging:")); |
5033639c MV |
2098 | output_commit_title(o, h1); |
2099 | output_commit_title(o, h2); | |
9047ebbc MV |
2100 | } |
2101 | ||
2102 | if (!ca) { | |
2ce406cc | 2103 | ca = get_merge_bases(h1, h2); |
9047ebbc MV |
2104 | ca = reverse_commit_list(ca); |
2105 | } | |
2106 | ||
8a2fce18 | 2107 | if (show(o, 5)) { |
e0453cd8 RT |
2108 | unsigned cnt = commit_list_count(ca); |
2109 | ||
2110 | output(o, 5, Q_("found %u common ancestor:", | |
2111 | "found %u common ancestors:", cnt), cnt); | |
9047ebbc | 2112 | for (iter = ca; iter; iter = iter->next) |
5033639c | 2113 | output_commit_title(o, iter->item); |
9047ebbc MV |
2114 | } |
2115 | ||
2116 | merged_common_ancestors = pop_commit(&ca); | |
2117 | if (merged_common_ancestors == NULL) { | |
03f622c8 JN |
2118 | /* if there is no common ancestor, use an empty tree */ |
2119 | struct tree *tree; | |
9047ebbc | 2120 | |
eb0ccfd7 | 2121 | tree = lookup_tree(the_hash_algo->empty_tree); |
9047ebbc MV |
2122 | merged_common_ancestors = make_virtual_commit(tree, "ancestor"); |
2123 | } | |
2124 | ||
2125 | for (iter = ca; iter; iter = iter->next) { | |
8a2fce18 | 2126 | const char *saved_b1, *saved_b2; |
5033639c | 2127 | o->call_depth++; |
9047ebbc MV |
2128 | /* |
2129 | * When the merge fails, the result contains files | |
2130 | * with conflict markers. The cleanness flag is | |
de8946de JS |
2131 | * ignored (unless indicating an error), it was never |
2132 | * actually used, as result of merge_trees has always | |
2133 | * overwritten it: the committed "conflicts" were | |
2134 | * already resolved. | |
9047ebbc MV |
2135 | */ |
2136 | discard_cache(); | |
8a2fce18 MV |
2137 | saved_b1 = o->branch1; |
2138 | saved_b2 = o->branch2; | |
2139 | o->branch1 = "Temporary merge branch 1"; | |
2140 | o->branch2 = "Temporary merge branch 2"; | |
de8946de JS |
2141 | if (merge_recursive(o, merged_common_ancestors, iter->item, |
2142 | NULL, &merged_common_ancestors) < 0) | |
2143 | return -1; | |
8a2fce18 MV |
2144 | o->branch1 = saved_b1; |
2145 | o->branch2 = saved_b2; | |
5033639c | 2146 | o->call_depth--; |
9047ebbc MV |
2147 | |
2148 | if (!merged_common_ancestors) | |
bc9204d4 | 2149 | return err(o, _("merge returned no commit")); |
9047ebbc MV |
2150 | } |
2151 | ||
2152 | discard_cache(); | |
b7fa51da | 2153 | if (!o->call_depth) |
9047ebbc | 2154 | read_cache(); |
9047ebbc | 2155 | |
7ca56aa0 | 2156 | o->ancestor = "merged common ancestors"; |
8a2fce18 MV |
2157 | clean = merge_trees(o, h1->tree, h2->tree, merged_common_ancestors->tree, |
2158 | &mrtree); | |
6999bc70 JS |
2159 | if (clean < 0) { |
2160 | flush_output(o); | |
de8946de | 2161 | return clean; |
6999bc70 | 2162 | } |
9047ebbc | 2163 | |
b7fa51da | 2164 | if (o->call_depth) { |
9047ebbc MV |
2165 | *result = make_virtual_commit(mrtree, "merged tree"); |
2166 | commit_list_insert(h1, &(*result)->parents); | |
2167 | commit_list_insert(h2, &(*result)->parents->next); | |
2168 | } | |
c7d84924 | 2169 | flush_output(o); |
548009c0 JS |
2170 | if (!o->call_depth && o->buffer_output < 2) |
2171 | strbuf_release(&o->obuf); | |
f31027c9 JH |
2172 | if (show(o, 2)) |
2173 | diff_warn_rename_limit("merge.renamelimit", | |
2174 | o->needed_rename_limit, 0); | |
9047ebbc MV |
2175 | return clean; |
2176 | } | |
2177 | ||
4e8161a8 | 2178 | static struct commit *get_ref(const struct object_id *oid, const char *name) |
73118f89 SB |
2179 | { |
2180 | struct object *object; | |
2181 | ||
c251c83d | 2182 | object = deref_tag(parse_object(oid), name, strlen(name)); |
73118f89 SB |
2183 | if (!object) |
2184 | return NULL; | |
2185 | if (object->type == OBJ_TREE) | |
2186 | return make_virtual_commit((struct tree*)object, name); | |
2187 | if (object->type != OBJ_COMMIT) | |
2188 | return NULL; | |
2189 | if (parse_commit((struct commit *)object)) | |
2190 | return NULL; | |
2191 | return (struct commit *)object; | |
2192 | } | |
2193 | ||
8a2fce18 | 2194 | int merge_recursive_generic(struct merge_options *o, |
4e8161a8 | 2195 | const struct object_id *head, |
2196 | const struct object_id *merge, | |
8a2fce18 | 2197 | int num_base_list, |
4e8161a8 | 2198 | const struct object_id **base_list, |
8a2fce18 | 2199 | struct commit **result) |
73118f89 | 2200 | { |
03b86647 | 2201 | int clean; |
837e34eb | 2202 | struct lock_file lock = LOCK_INIT; |
8a2fce18 MV |
2203 | struct commit *head_commit = get_ref(head, o->branch1); |
2204 | struct commit *next_commit = get_ref(merge, o->branch2); | |
73118f89 SB |
2205 | struct commit_list *ca = NULL; |
2206 | ||
2207 | if (base_list) { | |
2208 | int i; | |
8a2fce18 | 2209 | for (i = 0; i < num_base_list; ++i) { |
73118f89 | 2210 | struct commit *base; |
4e8161a8 | 2211 | if (!(base = get_ref(base_list[i], oid_to_hex(base_list[i])))) |
bc9204d4 | 2212 | return err(o, _("Could not parse object '%s'"), |
4e8161a8 | 2213 | oid_to_hex(base_list[i])); |
73118f89 SB |
2214 | commit_list_insert(base, &ca); |
2215 | } | |
2216 | } | |
2217 | ||
837e34eb | 2218 | hold_locked_index(&lock, LOCK_DIE_ON_ERROR); |
8a2fce18 MV |
2219 | clean = merge_recursive(o, head_commit, next_commit, ca, |
2220 | result); | |
51d3f43d MÅ |
2221 | if (clean < 0) { |
2222 | rollback_lock_file(&lock); | |
de8946de | 2223 | return clean; |
51d3f43d | 2224 | } |
de8946de | 2225 | |
61000814 MÅ |
2226 | if (write_locked_index(&the_index, &lock, |
2227 | COMMIT_LOCK | SKIP_IF_UNCHANGED)) | |
bc9204d4 | 2228 | return err(o, _("Unable to write index.")); |
73118f89 SB |
2229 | |
2230 | return clean ? 0 : 1; | |
2231 | } | |
2232 | ||
0e7bcb1b | 2233 | static void merge_recursive_config(struct merge_options *o) |
9047ebbc | 2234 | { |
0e7bcb1b TA |
2235 | git_config_get_int("merge.verbosity", &o->verbosity); |
2236 | git_config_get_int("diff.renamelimit", &o->diff_rename_limit); | |
2237 | git_config_get_int("merge.renamelimit", &o->merge_rename_limit); | |
2238 | git_config(git_xmerge_config, NULL); | |
9047ebbc MV |
2239 | } |
2240 | ||
8a2fce18 | 2241 | void init_merge_options(struct merge_options *o) |
9047ebbc | 2242 | { |
80486220 | 2243 | const char *merge_verbosity; |
8a2fce18 MV |
2244 | memset(o, 0, sizeof(struct merge_options)); |
2245 | o->verbosity = 2; | |
2246 | o->buffer_output = 1; | |
2247 | o->diff_rename_limit = -1; | |
2248 | o->merge_rename_limit = -1; | |
7610fa57 | 2249 | o->renormalize = 0; |
d2b11eca | 2250 | o->detect_rename = 1; |
0e7bcb1b | 2251 | merge_recursive_config(o); |
80486220 AO |
2252 | merge_verbosity = getenv("GIT_MERGE_VERBOSITY"); |
2253 | if (merge_verbosity) | |
2254 | o->verbosity = strtol(merge_verbosity, NULL, 10); | |
8a2fce18 MV |
2255 | if (o->verbosity >= 5) |
2256 | o->buffer_output = 0; | |
c7d84924 | 2257 | strbuf_init(&o->obuf, 0); |
f93d7c6f | 2258 | string_list_init(&o->df_conflict_file_set, 1); |
9047ebbc | 2259 | } |
635a7bb1 JN |
2260 | |
2261 | int parse_merge_opt(struct merge_options *o, const char *s) | |
2262 | { | |
95b567c7 JK |
2263 | const char *arg; |
2264 | ||
635a7bb1 JN |
2265 | if (!s || !*s) |
2266 | return -1; | |
2267 | if (!strcmp(s, "ours")) | |
2268 | o->recursive_variant = MERGE_RECURSIVE_OURS; | |
2269 | else if (!strcmp(s, "theirs")) | |
2270 | o->recursive_variant = MERGE_RECURSIVE_THEIRS; | |
2271 | else if (!strcmp(s, "subtree")) | |
2272 | o->subtree_shift = ""; | |
95b567c7 JK |
2273 | else if (skip_prefix(s, "subtree=", &arg)) |
2274 | o->subtree_shift = arg; | |
58a1ece4 | 2275 | else if (!strcmp(s, "patience")) |
307ab20b | 2276 | o->xdl_opts = DIFF_WITH_ALG(o, PATIENCE_DIFF); |
8c912eea | 2277 | else if (!strcmp(s, "histogram")) |
307ab20b | 2278 | o->xdl_opts = DIFF_WITH_ALG(o, HISTOGRAM_DIFF); |
95b567c7 JK |
2279 | else if (skip_prefix(s, "diff-algorithm=", &arg)) { |
2280 | long value = parse_algorithm_value(arg); | |
07924d4d MP |
2281 | if (value < 0) |
2282 | return -1; | |
2283 | /* clear out previous settings */ | |
2284 | DIFF_XDL_CLR(o, NEED_MINIMAL); | |
2285 | o->xdl_opts &= ~XDF_DIFF_ALGORITHM_MASK; | |
2286 | o->xdl_opts |= value; | |
2287 | } | |
4e5dd044 | 2288 | else if (!strcmp(s, "ignore-space-change")) |
c2d4b4cd | 2289 | DIFF_XDL_SET(o, IGNORE_WHITESPACE_CHANGE); |
4e5dd044 | 2290 | else if (!strcmp(s, "ignore-all-space")) |
c2d4b4cd | 2291 | DIFF_XDL_SET(o, IGNORE_WHITESPACE); |
4e5dd044 | 2292 | else if (!strcmp(s, "ignore-space-at-eol")) |
c2d4b4cd | 2293 | DIFF_XDL_SET(o, IGNORE_WHITESPACE_AT_EOL); |
e9282f02 JH |
2294 | else if (!strcmp(s, "ignore-cr-at-eol")) |
2295 | DIFF_XDL_SET(o, IGNORE_CR_AT_EOL); | |
635a7bb1 JN |
2296 | else if (!strcmp(s, "renormalize")) |
2297 | o->renormalize = 1; | |
2298 | else if (!strcmp(s, "no-renormalize")) | |
2299 | o->renormalize = 0; | |
d2b11eca FGA |
2300 | else if (!strcmp(s, "no-renames")) |
2301 | o->detect_rename = 0; | |
87892f60 | 2302 | else if (!strcmp(s, "find-renames")) { |
1b47ad16 | 2303 | o->detect_rename = 1; |
87892f60 FGA |
2304 | o->rename_score = 0; |
2305 | } | |
1b47ad16 FGA |
2306 | else if (skip_prefix(s, "find-renames=", &arg) || |
2307 | skip_prefix(s, "rename-threshold=", &arg)) { | |
95b567c7 | 2308 | if ((o->rename_score = parse_rename_score(&arg)) == -1 || *arg != 0) |
10ae7526 | 2309 | return -1; |
d2b11eca | 2310 | o->detect_rename = 1; |
10ae7526 | 2311 | } |
635a7bb1 JN |
2312 | else |
2313 | return -1; | |
2314 | return 0; | |
2315 | } |