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