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