2 * Recursive Merge algorithm stolen from git-merge-recursive.py by
4 * The thieves were Alex Riesen and Johannes Schindelin, in June/July 2006
7 #include "merge-recursive.h"
13 #include "cache-tree.h"
15 #include "commit-reach.h"
20 #include "environment.h"
25 #include "object-store.h"
26 #include "repository.h"
28 #include "string-list.h"
29 #include "submodule-config.h"
30 #include "submodule.h"
32 #include "tree-walk.h"
33 #include "unpack-trees.h"
35 #include "xdiff-interface.h"
37 struct merge_options_internal
{
39 int needed_rename_limit
;
40 struct hashmap current_file_dir_set
;
41 struct string_list df_conflict_file_set
;
42 struct unpack_trees_options unpack_opts
;
43 struct index_state orig_index
;
46 struct path_hashmap_entry
{
47 struct hashmap_entry e
;
48 char path
[FLEX_ARRAY
];
51 static int path_hashmap_cmp(const void *cmp_data UNUSED
,
52 const struct hashmap_entry
*eptr
,
53 const struct hashmap_entry
*entry_or_key
,
56 const struct path_hashmap_entry
*a
, *b
;
57 const char *key
= keydata
;
59 a
= container_of(eptr
, const struct path_hashmap_entry
, e
);
60 b
= container_of(entry_or_key
, const struct path_hashmap_entry
, e
);
62 return fspathcmp(a
->path
, key
? key
: b
->path
);
66 * For dir_rename_entry, directory names are stored as a full path from the
67 * toplevel of the repository and do not include a trailing '/'. Also:
69 * dir: original name of directory being renamed
70 * non_unique_new_dir: if true, could not determine new_dir
71 * new_dir: final name of directory being renamed
72 * possible_new_dirs: temporary used to help determine new_dir; see comments
73 * in get_directory_renames() for details
75 struct dir_rename_entry
{
76 struct hashmap_entry ent
;
78 unsigned non_unique_new_dir
:1;
79 struct strbuf new_dir
;
80 struct string_list possible_new_dirs
;
83 static struct dir_rename_entry
*dir_rename_find_entry(struct hashmap
*hashmap
,
86 struct dir_rename_entry key
;
90 hashmap_entry_init(&key
.ent
, strhash(dir
));
92 return hashmap_get_entry(hashmap
, &key
, ent
, NULL
);
95 static int dir_rename_cmp(const void *cmp_data UNUSED
,
96 const struct hashmap_entry
*eptr
,
97 const struct hashmap_entry
*entry_or_key
,
98 const void *keydata UNUSED
)
100 const struct dir_rename_entry
*e1
, *e2
;
102 e1
= container_of(eptr
, const struct dir_rename_entry
, ent
);
103 e2
= container_of(entry_or_key
, const struct dir_rename_entry
, ent
);
105 return strcmp(e1
->dir
, e2
->dir
);
108 static void dir_rename_init(struct hashmap
*map
)
110 hashmap_init(map
, dir_rename_cmp
, NULL
, 0);
113 static void dir_rename_entry_init(struct dir_rename_entry
*entry
,
116 hashmap_entry_init(&entry
->ent
, strhash(directory
));
117 entry
->dir
= directory
;
118 entry
->non_unique_new_dir
= 0;
119 strbuf_init(&entry
->new_dir
, 0);
120 string_list_init_nodup(&entry
->possible_new_dirs
);
123 struct collision_entry
{
124 struct hashmap_entry ent
;
126 struct string_list source_files
;
127 unsigned reported_already
:1;
130 static struct collision_entry
*collision_find_entry(struct hashmap
*hashmap
,
133 struct collision_entry key
;
135 hashmap_entry_init(&key
.ent
, strhash(target_file
));
136 key
.target_file
= target_file
;
137 return hashmap_get_entry(hashmap
, &key
, ent
, NULL
);
140 static int collision_cmp(const void *cmp_data UNUSED
,
141 const struct hashmap_entry
*eptr
,
142 const struct hashmap_entry
*entry_or_key
,
143 const void *keydata UNUSED
)
145 const struct collision_entry
*e1
, *e2
;
147 e1
= container_of(eptr
, const struct collision_entry
, ent
);
148 e2
= container_of(entry_or_key
, const struct collision_entry
, ent
);
150 return strcmp(e1
->target_file
, e2
->target_file
);
153 static void collision_init(struct hashmap
*map
)
155 hashmap_init(map
, collision_cmp
, NULL
, 0);
158 static void flush_output(struct merge_options
*opt
)
160 if (opt
->buffer_output
< 2 && opt
->obuf
.len
) {
161 fputs(opt
->obuf
.buf
, stdout
);
162 strbuf_reset(&opt
->obuf
);
166 __attribute__((format (printf
, 2, 3)))
167 static int err(struct merge_options
*opt
, const char *err
, ...)
171 if (opt
->buffer_output
< 2)
174 strbuf_complete(&opt
->obuf
, '\n');
175 strbuf_addstr(&opt
->obuf
, "error: ");
177 va_start(params
, err
);
178 strbuf_vaddf(&opt
->obuf
, err
, params
);
180 if (opt
->buffer_output
> 1)
181 strbuf_addch(&opt
->obuf
, '\n');
183 error("%s", opt
->obuf
.buf
);
184 strbuf_reset(&opt
->obuf
);
190 static struct tree
*shift_tree_object(struct repository
*repo
,
191 struct tree
*one
, struct tree
*two
,
192 const char *subtree_shift
)
194 struct object_id shifted
;
196 if (!*subtree_shift
) {
197 shift_tree(repo
, &one
->object
.oid
, &two
->object
.oid
, &shifted
, 0);
199 shift_tree_by(repo
, &one
->object
.oid
, &two
->object
.oid
, &shifted
,
202 if (oideq(&two
->object
.oid
, &shifted
))
204 return lookup_tree(repo
, &shifted
);
207 static inline void set_commit_tree(struct commit
*c
, struct tree
*t
)
212 static struct commit
*make_virtual_commit(struct repository
*repo
,
216 struct commit
*commit
= alloc_commit_node(repo
);
218 set_merge_remote_desc(commit
, comment
, (struct object
*)commit
);
219 set_commit_tree(commit
, tree
);
220 commit
->object
.parsed
= 1;
229 RENAME_ONE_FILE_TO_ONE
,
230 RENAME_ONE_FILE_TO_TWO
,
231 RENAME_TWO_FILES_TO_ONE
235 * Since we want to write the index eventually, we cannot reuse the index
236 * for these (temporary) data.
239 struct diff_filespec stages
[4]; /* mostly for oid & mode; maybe path */
240 struct rename_conflict_info
*rename_conflict_info
;
241 unsigned processed
:1;
245 unsigned processed
:1;
246 struct diff_filepair
*pair
;
247 const char *branch
; /* branch that the rename occurred on */
249 * If directory rename detection affected this rename, what was its
250 * original type ('A' or 'R') and it's original destination before
251 * the directory rename (otherwise, '\0' and NULL for these two vars).
253 char dir_rename_original_type
;
254 char *dir_rename_original_dest
;
256 * Purpose of src_entry and dst_entry:
258 * If 'before' is renamed to 'after' then src_entry will contain
259 * the versions of 'before' from the merge_base, HEAD, and MERGE in
260 * stages 1, 2, and 3; dst_entry will contain the respective
261 * versions of 'after' in corresponding locations. Thus, we have a
262 * total of six modes and oids, though some will be null. (Stage 0
263 * is ignored; we're interested in handling conflicts.)
265 * Since we don't turn on break-rewrites by default, neither
266 * src_entry nor dst_entry can have all three of their stages have
267 * non-null oids, meaning at most four of the six will be non-null.
268 * Also, since this is a rename, both src_entry and dst_entry will
269 * have at least one non-null oid, meaning at least two will be
270 * non-null. Of the six oids, a typical rename will have three be
271 * non-null. Only two implies a rename/delete, and four implies a
274 struct stage_data
*src_entry
;
275 struct stage_data
*dst_entry
;
278 struct rename_conflict_info
{
279 enum rename_type rename_type
;
284 static inline void setup_rename_conflict_info(enum rename_type rename_type
,
285 struct merge_options
*opt
,
289 struct rename_conflict_info
*ci
;
292 * When we have two renames involved, it's easiest to get the
293 * correct things into stage 2 and 3, and to make sure that the
294 * content merge puts HEAD before the other branch if we just
295 * ensure that branch1 == opt->branch1. So, simply flip arguments
296 * around if we don't have that.
298 if (ren2
&& ren1
->branch
!= opt
->branch1
) {
299 setup_rename_conflict_info(rename_type
, opt
, ren2
, ren1
);
304 ci
->rename_type
= rename_type
;
308 ci
->ren1
->dst_entry
->processed
= 0;
309 ci
->ren1
->dst_entry
->rename_conflict_info
= ci
;
311 ci
->ren2
->dst_entry
->rename_conflict_info
= ci
;
315 static int show(struct merge_options
*opt
, int v
)
317 return (!opt
->priv
->call_depth
&& opt
->verbosity
>= v
) ||
321 __attribute__((format (printf
, 3, 4)))
322 static void output(struct merge_options
*opt
, int v
, const char *fmt
, ...)
329 strbuf_addchars(&opt
->obuf
, ' ', opt
->priv
->call_depth
* 2);
332 strbuf_vaddf(&opt
->obuf
, fmt
, ap
);
335 strbuf_addch(&opt
->obuf
, '\n');
336 if (!opt
->buffer_output
)
340 static void repo_output_commit_title(struct merge_options
*opt
,
341 struct repository
*repo
,
342 struct commit
*commit
)
344 struct merge_remote_desc
*desc
;
346 strbuf_addchars(&opt
->obuf
, ' ', opt
->priv
->call_depth
* 2);
347 desc
= merge_remote_util(commit
);
349 strbuf_addf(&opt
->obuf
, "virtual %s\n", desc
->name
);
351 strbuf_repo_add_unique_abbrev(&opt
->obuf
, repo
,
354 strbuf_addch(&opt
->obuf
, ' ');
355 if (repo_parse_commit(repo
, commit
) != 0)
356 strbuf_addstr(&opt
->obuf
, _("(bad commit)\n"));
359 const char *msg
= repo_get_commit_buffer(repo
, commit
, NULL
);
360 int len
= find_commit_subject(msg
, &title
);
362 strbuf_addf(&opt
->obuf
, "%.*s\n", len
, title
);
363 repo_unuse_commit_buffer(repo
, commit
, msg
);
369 static void output_commit_title(struct merge_options
*opt
, struct commit
*commit
)
371 repo_output_commit_title(opt
, the_repository
, commit
);
374 static int add_cacheinfo(struct merge_options
*opt
,
375 const struct diff_filespec
*blob
,
376 const char *path
, int stage
, int refresh
, int options
)
378 struct index_state
*istate
= opt
->repo
->index
;
379 struct cache_entry
*ce
;
382 ce
= make_cache_entry(istate
, blob
->mode
, &blob
->oid
, path
, stage
, 0);
384 return err(opt
, _("add_cacheinfo failed for path '%s'; merge aborting."), path
);
386 ret
= add_index_entry(istate
, ce
, options
);
388 struct cache_entry
*nce
;
390 nce
= refresh_cache_entry(istate
, ce
,
391 CE_MATCH_REFRESH
| CE_MATCH_IGNORE_MISSING
);
393 return err(opt
, _("add_cacheinfo failed to refresh for path '%s'; merge aborting."), path
);
395 ret
= add_index_entry(istate
, nce
, options
);
400 static inline int merge_detect_rename(struct merge_options
*opt
)
402 return (opt
->detect_renames
>= 0) ? opt
->detect_renames
: 1;
405 static void init_tree_desc_from_tree(struct tree_desc
*desc
, struct tree
*tree
)
408 init_tree_desc(desc
, tree
->buffer
, tree
->size
);
411 static int unpack_trees_start(struct merge_options
*opt
,
417 struct tree_desc t
[3];
418 struct index_state tmp_index
= INDEX_STATE_INIT(opt
->repo
);
420 memset(&opt
->priv
->unpack_opts
, 0, sizeof(opt
->priv
->unpack_opts
));
421 if (opt
->priv
->call_depth
)
422 opt
->priv
->unpack_opts
.index_only
= 1;
424 opt
->priv
->unpack_opts
.update
= 1;
425 /* FIXME: should only do this if !overwrite_ignore */
426 opt
->priv
->unpack_opts
.preserve_ignored
= 0;
428 opt
->priv
->unpack_opts
.merge
= 1;
429 opt
->priv
->unpack_opts
.head_idx
= 2;
430 opt
->priv
->unpack_opts
.fn
= threeway_merge
;
431 opt
->priv
->unpack_opts
.src_index
= opt
->repo
->index
;
432 opt
->priv
->unpack_opts
.dst_index
= &tmp_index
;
433 opt
->priv
->unpack_opts
.aggressive
= !merge_detect_rename(opt
);
434 setup_unpack_trees_porcelain(&opt
->priv
->unpack_opts
, "merge");
436 init_tree_desc_from_tree(t
+0, common
);
437 init_tree_desc_from_tree(t
+1, head
);
438 init_tree_desc_from_tree(t
+2, merge
);
440 rc
= unpack_trees(3, t
, &opt
->priv
->unpack_opts
);
441 cache_tree_free(&opt
->repo
->index
->cache_tree
);
444 * Update opt->repo->index to match the new results, AFTER saving a
445 * copy in opt->priv->orig_index. Update src_index to point to the
446 * saved copy. (verify_uptodate() checks src_index, and the original
447 * index is the one that had the necessary modification timestamps.)
449 opt
->priv
->orig_index
= *opt
->repo
->index
;
450 *opt
->repo
->index
= tmp_index
;
451 opt
->priv
->unpack_opts
.src_index
= &opt
->priv
->orig_index
;
456 static void unpack_trees_finish(struct merge_options
*opt
)
458 discard_index(&opt
->priv
->orig_index
);
459 clear_unpack_trees_porcelain(&opt
->priv
->unpack_opts
);
462 static int save_files_dirs(const struct object_id
*oid UNUSED
,
463 struct strbuf
*base
, const char *path
,
464 unsigned int mode
, void *context
)
466 struct path_hashmap_entry
*entry
;
467 int baselen
= base
->len
;
468 struct merge_options
*opt
= context
;
470 strbuf_addstr(base
, path
);
472 FLEX_ALLOC_MEM(entry
, path
, base
->buf
, base
->len
);
473 hashmap_entry_init(&entry
->e
, fspathhash(entry
->path
));
474 hashmap_add(&opt
->priv
->current_file_dir_set
, &entry
->e
);
476 strbuf_setlen(base
, baselen
);
477 return (S_ISDIR(mode
) ? READ_TREE_RECURSIVE
: 0);
480 static void get_files_dirs(struct merge_options
*opt
, struct tree
*tree
)
482 struct pathspec match_all
;
483 memset(&match_all
, 0, sizeof(match_all
));
484 read_tree(opt
->repo
, tree
,
485 &match_all
, save_files_dirs
, opt
);
488 static int get_tree_entry_if_blob(struct repository
*r
,
489 const struct object_id
*tree
,
491 struct diff_filespec
*dfs
)
495 ret
= get_tree_entry(r
, tree
, path
, &dfs
->oid
, &dfs
->mode
);
496 if (S_ISDIR(dfs
->mode
)) {
497 oidcpy(&dfs
->oid
, null_oid());
504 * Returns an index_entry instance which doesn't have to correspond to
505 * a real cache entry in Git's index.
507 static struct stage_data
*insert_stage_data(struct repository
*r
,
509 struct tree
*o
, struct tree
*a
, struct tree
*b
,
510 struct string_list
*entries
)
512 struct string_list_item
*item
;
513 struct stage_data
*e
= xcalloc(1, sizeof(struct stage_data
));
514 get_tree_entry_if_blob(r
, &o
->object
.oid
, path
, &e
->stages
[1]);
515 get_tree_entry_if_blob(r
, &a
->object
.oid
, path
, &e
->stages
[2]);
516 get_tree_entry_if_blob(r
, &b
->object
.oid
, path
, &e
->stages
[3]);
517 item
= string_list_insert(entries
, path
);
523 * Create a dictionary mapping file names to stage_data objects. The
524 * dictionary contains one entry for every path with a non-zero stage entry.
526 static struct string_list
*get_unmerged(struct index_state
*istate
)
528 struct string_list
*unmerged
= xmalloc(sizeof(struct string_list
));
531 string_list_init_dup(unmerged
);
533 /* TODO: audit for interaction with sparse-index. */
534 ensure_full_index(istate
);
535 for (i
= 0; i
< istate
->cache_nr
; i
++) {
536 struct string_list_item
*item
;
537 struct stage_data
*e
;
538 const struct cache_entry
*ce
= istate
->cache
[i
];
542 item
= string_list_lookup(unmerged
, ce
->name
);
544 item
= string_list_insert(unmerged
, ce
->name
);
545 item
->util
= xcalloc(1, sizeof(struct stage_data
));
548 e
->stages
[ce_stage(ce
)].mode
= ce
->ce_mode
;
549 oidcpy(&e
->stages
[ce_stage(ce
)].oid
, &ce
->oid
);
555 static int string_list_df_name_compare(const char *one
, const char *two
)
557 int onelen
= strlen(one
);
558 int twolen
= strlen(two
);
560 * Here we only care that entries for D/F conflicts are
561 * adjacent, in particular with the file of the D/F conflict
562 * appearing before files below the corresponding directory.
563 * The order of the rest of the list is irrelevant for us.
565 * To achieve this, we sort with df_name_compare and provide
566 * the mode S_IFDIR so that D/F conflicts will sort correctly.
567 * We use the mode S_IFDIR for everything else for simplicity,
568 * since in other cases any changes in their order due to
569 * sorting cause no problems for us.
571 int cmp
= df_name_compare(one
, onelen
, S_IFDIR
,
572 two
, twolen
, S_IFDIR
);
574 * Now that 'foo' and 'foo/bar' compare equal, we have to make sure
575 * that 'foo' comes before 'foo/bar'.
579 return onelen
- twolen
;
582 static void record_df_conflict_files(struct merge_options
*opt
,
583 struct string_list
*entries
)
585 /* If there is a D/F conflict and the file for such a conflict
586 * currently exists in the working tree, we want to allow it to be
587 * removed to make room for the corresponding directory if needed.
588 * The files underneath the directories of such D/F conflicts will
589 * be processed before the corresponding file involved in the D/F
590 * conflict. If the D/F directory ends up being removed by the
591 * merge, then we won't have to touch the D/F file. If the D/F
592 * directory needs to be written to the working copy, then the D/F
593 * file will simply be removed (in make_room_for_path()) to make
594 * room for the necessary paths. Note that if both the directory
595 * and the file need to be present, then the D/F file will be
596 * reinstated with a new unique name at the time it is processed.
598 struct string_list df_sorted_entries
= STRING_LIST_INIT_NODUP
;
599 const char *last_file
= NULL
;
604 * If we're merging merge-bases, we don't want to bother with
605 * any working directory changes.
607 if (opt
->priv
->call_depth
)
610 /* Ensure D/F conflicts are adjacent in the entries list. */
611 for (i
= 0; i
< entries
->nr
; i
++) {
612 struct string_list_item
*next
= &entries
->items
[i
];
613 string_list_append(&df_sorted_entries
, next
->string
)->util
=
616 df_sorted_entries
.cmp
= string_list_df_name_compare
;
617 string_list_sort(&df_sorted_entries
);
619 string_list_clear(&opt
->priv
->df_conflict_file_set
, 1);
620 for (i
= 0; i
< df_sorted_entries
.nr
; i
++) {
621 const char *path
= df_sorted_entries
.items
[i
].string
;
622 int len
= strlen(path
);
623 struct stage_data
*e
= df_sorted_entries
.items
[i
].util
;
626 * Check if last_file & path correspond to a D/F conflict;
627 * i.e. whether path is last_file+'/'+<something>.
628 * If so, record that it's okay to remove last_file to make
629 * room for path and friends if needed.
633 memcmp(path
, last_file
, last_len
) == 0 &&
634 path
[last_len
] == '/') {
635 string_list_insert(&opt
->priv
->df_conflict_file_set
, last_file
);
639 * Determine whether path could exist as a file in the
640 * working directory as a possible D/F conflict. This
641 * will only occur when it exists in stage 2 as a
644 if (S_ISREG(e
->stages
[2].mode
) || S_ISLNK(e
->stages
[2].mode
)) {
651 string_list_clear(&df_sorted_entries
, 0);
654 static int update_stages(struct merge_options
*opt
, const char *path
,
655 const struct diff_filespec
*o
,
656 const struct diff_filespec
*a
,
657 const struct diff_filespec
*b
)
661 * NOTE: It is usually a bad idea to call update_stages on a path
662 * before calling update_file on that same path, since it can
663 * sometimes lead to spurious "refusing to lose untracked file..."
664 * messages from update_file (via make_room_for path via
665 * would_lose_untracked). Instead, reverse the order of the calls
666 * (executing update_file first and then update_stages).
669 int options
= ADD_CACHE_OK_TO_ADD
| ADD_CACHE_SKIP_DFCHECK
;
671 if (remove_file_from_index(opt
->repo
->index
, path
))
674 if (add_cacheinfo(opt
, o
, path
, 1, 0, options
))
677 if (add_cacheinfo(opt
, a
, path
, 2, 0, options
))
680 if (add_cacheinfo(opt
, b
, path
, 3, 0, options
))
685 static void update_entry(struct stage_data
*entry
,
686 struct diff_filespec
*o
,
687 struct diff_filespec
*a
,
688 struct diff_filespec
*b
)
690 entry
->processed
= 0;
691 entry
->stages
[1].mode
= o
->mode
;
692 entry
->stages
[2].mode
= a
->mode
;
693 entry
->stages
[3].mode
= b
->mode
;
694 oidcpy(&entry
->stages
[1].oid
, &o
->oid
);
695 oidcpy(&entry
->stages
[2].oid
, &a
->oid
);
696 oidcpy(&entry
->stages
[3].oid
, &b
->oid
);
699 static int remove_file(struct merge_options
*opt
, int clean
,
700 const char *path
, int no_wd
)
702 int update_cache
= opt
->priv
->call_depth
|| clean
;
703 int update_working_directory
= !opt
->priv
->call_depth
&& !no_wd
;
706 if (remove_file_from_index(opt
->repo
->index
, path
))
709 if (update_working_directory
) {
711 struct cache_entry
*ce
;
712 ce
= index_file_exists(opt
->repo
->index
, path
, strlen(path
),
714 if (ce
&& ce_stage(ce
) == 0 && strcmp(path
, ce
->name
))
717 if (remove_path(path
))
723 /* add a string to a strbuf, but converting "/" to "_" */
724 static void add_flattened_path(struct strbuf
*out
, const char *s
)
727 strbuf_addstr(out
, s
);
728 for (; i
< out
->len
; i
++)
729 if (out
->buf
[i
] == '/')
733 static char *unique_path(struct merge_options
*opt
,
737 struct path_hashmap_entry
*entry
;
738 struct strbuf newpath
= STRBUF_INIT
;
742 strbuf_addf(&newpath
, "%s~", path
);
743 add_flattened_path(&newpath
, branch
);
745 base_len
= newpath
.len
;
746 while (hashmap_get_from_hash(&opt
->priv
->current_file_dir_set
,
747 fspathhash(newpath
.buf
), newpath
.buf
) ||
748 (!opt
->priv
->call_depth
&& file_exists(newpath
.buf
))) {
749 strbuf_setlen(&newpath
, base_len
);
750 strbuf_addf(&newpath
, "_%d", suffix
++);
753 FLEX_ALLOC_MEM(entry
, path
, newpath
.buf
, newpath
.len
);
754 hashmap_entry_init(&entry
->e
, fspathhash(entry
->path
));
755 hashmap_add(&opt
->priv
->current_file_dir_set
, &entry
->e
);
756 return strbuf_detach(&newpath
, NULL
);
760 * Check whether a directory in the index is in the way of an incoming
761 * file. Return 1 if so. If check_working_copy is non-zero, also
762 * check the working directory. If empty_ok is non-zero, also return
763 * 0 in the case where the working-tree dir exists but is empty.
765 static int dir_in_way(struct index_state
*istate
, const char *path
,
766 int check_working_copy
, int empty_ok
)
769 struct strbuf dirpath
= STRBUF_INIT
;
772 strbuf_addstr(&dirpath
, path
);
773 strbuf_addch(&dirpath
, '/');
775 pos
= index_name_pos(istate
, dirpath
.buf
, dirpath
.len
);
779 if (pos
< istate
->cache_nr
&&
780 !strncmp(dirpath
.buf
, istate
->cache
[pos
]->name
, dirpath
.len
)) {
781 strbuf_release(&dirpath
);
785 strbuf_release(&dirpath
);
786 return check_working_copy
&& !lstat(path
, &st
) && S_ISDIR(st
.st_mode
) &&
787 !(empty_ok
&& is_empty_dir(path
)) &&
788 !has_symlink_leading_path(path
, strlen(path
));
792 * Returns whether path was tracked in the index before the merge started,
793 * and its oid and mode match the specified values
795 static int was_tracked_and_matches(struct merge_options
*opt
, const char *path
,
796 const struct diff_filespec
*blob
)
798 int pos
= index_name_pos(&opt
->priv
->orig_index
, path
, strlen(path
));
799 struct cache_entry
*ce
;
802 /* we were not tracking this path before the merge */
805 /* See if the file we were tracking before matches */
806 ce
= opt
->priv
->orig_index
.cache
[pos
];
807 return (oideq(&ce
->oid
, &blob
->oid
) && ce
->ce_mode
== blob
->mode
);
811 * Returns whether path was tracked in the index before the merge started
813 static int was_tracked(struct merge_options
*opt
, const char *path
)
815 int pos
= index_name_pos(&opt
->priv
->orig_index
, path
, strlen(path
));
818 /* we were tracking this path before the merge */
824 static int would_lose_untracked(struct merge_options
*opt
, const char *path
)
826 struct index_state
*istate
= opt
->repo
->index
;
829 * This may look like it can be simplified to:
830 * return !was_tracked(opt, path) && file_exists(path)
831 * but it can't. This function needs to know whether path was in
832 * the working tree due to EITHER having been tracked in the index
833 * before the merge OR having been put into the working copy and
834 * index by unpack_trees(). Due to that either-or requirement, we
835 * check the current index instead of the original one.
837 * Note that we do not need to worry about merge-recursive itself
838 * updating the index after unpack_trees() and before calling this
839 * function, because we strictly require all code paths in
840 * merge-recursive to update the working tree first and the index
841 * second. Doing otherwise would break
842 * update_file()/would_lose_untracked(); see every comment in this
843 * file which mentions "update_stages".
845 int pos
= index_name_pos(istate
, path
, strlen(path
));
849 while (pos
< istate
->cache_nr
&&
850 !strcmp(path
, istate
->cache
[pos
]->name
)) {
852 * If stage #0, it is definitely tracked.
853 * If it has stage #2 then it was tracked
854 * before this merge started. All other
855 * cases the path was not tracked.
857 switch (ce_stage(istate
->cache
[pos
])) {
864 return file_exists(path
);
867 static int was_dirty(struct merge_options
*opt
, const char *path
)
869 struct cache_entry
*ce
;
872 if (opt
->priv
->call_depth
|| !was_tracked(opt
, path
))
875 ce
= index_file_exists(opt
->priv
->unpack_opts
.src_index
,
876 path
, strlen(path
), ignore_case
);
877 dirty
= verify_uptodate(ce
, &opt
->priv
->unpack_opts
) != 0;
881 static int make_room_for_path(struct merge_options
*opt
, const char *path
)
884 const char *msg
= _("failed to create path '%s'%s");
886 /* Unlink any D/F conflict files that are in the way */
887 for (i
= 0; i
< opt
->priv
->df_conflict_file_set
.nr
; i
++) {
888 const char *df_path
= opt
->priv
->df_conflict_file_set
.items
[i
].string
;
889 size_t pathlen
= strlen(path
);
890 size_t df_pathlen
= strlen(df_path
);
891 if (df_pathlen
< pathlen
&&
892 path
[df_pathlen
] == '/' &&
893 strncmp(path
, df_path
, df_pathlen
) == 0) {
895 _("Removing %s to make room for subdirectory\n"),
898 unsorted_string_list_delete_item(&opt
->priv
->df_conflict_file_set
,
904 /* Make sure leading directories are created */
905 status
= safe_create_leading_directories_const(path
);
907 if (status
== SCLD_EXISTS
)
908 /* something else exists */
909 return err(opt
, msg
, path
, _(": perhaps a D/F conflict?"));
910 return err(opt
, msg
, path
, "");
914 * Do not unlink a file in the work tree if we are not
917 if (would_lose_untracked(opt
, path
))
918 return err(opt
, _("refusing to lose untracked file at '%s'"),
921 /* Successful unlink is good.. */
924 /* .. and so is no existing file */
927 /* .. but not some other error (who really cares what?) */
928 return err(opt
, msg
, path
, _(": perhaps a D/F conflict?"));
931 static int update_file_flags(struct merge_options
*opt
,
932 const struct diff_filespec
*contents
,
939 if (opt
->priv
->call_depth
)
943 enum object_type type
;
947 if (S_ISGITLINK(contents
->mode
)) {
949 * We may later decide to recursively descend into
950 * the submodule directory and update its index
951 * and/or work tree, but we do not do that now.
957 buf
= read_object_file(&contents
->oid
, &type
, &size
);
959 ret
= err(opt
, _("cannot read object %s '%s'"),
960 oid_to_hex(&contents
->oid
), path
);
963 if (type
!= OBJ_BLOB
) {
964 ret
= err(opt
, _("blob expected for %s '%s'"),
965 oid_to_hex(&contents
->oid
), path
);
968 if (S_ISREG(contents
->mode
)) {
969 struct strbuf strbuf
= STRBUF_INIT
;
970 if (convert_to_working_tree(opt
->repo
->index
,
971 path
, buf
, size
, &strbuf
, NULL
)) {
974 buf
= strbuf_detach(&strbuf
, NULL
);
978 if (make_room_for_path(opt
, path
) < 0) {
982 if (S_ISREG(contents
->mode
) ||
983 (!has_symlinks
&& S_ISLNK(contents
->mode
))) {
985 int mode
= (contents
->mode
& 0100 ? 0777 : 0666);
987 fd
= open(path
, O_WRONLY
| O_TRUNC
| O_CREAT
, mode
);
989 ret
= err(opt
, _("failed to open '%s': %s"),
990 path
, strerror(errno
));
993 write_in_full(fd
, buf
, size
);
995 } else if (S_ISLNK(contents
->mode
)) {
996 char *lnk
= xmemdupz(buf
, size
);
997 safe_create_leading_directories_const(path
);
999 if (symlink(lnk
, path
))
1000 ret
= err(opt
, _("failed to symlink '%s': %s"),
1001 path
, strerror(errno
));
1005 _("do not know what to do with %06o %s '%s'"),
1006 contents
->mode
, oid_to_hex(&contents
->oid
), path
);
1011 if (!ret
&& update_cache
) {
1012 int refresh
= (!opt
->priv
->call_depth
&&
1013 contents
->mode
!= S_IFGITLINK
);
1014 if (add_cacheinfo(opt
, contents
, path
, 0, refresh
,
1015 ADD_CACHE_OK_TO_ADD
))
1021 static int update_file(struct merge_options
*opt
,
1023 const struct diff_filespec
*contents
,
1026 return update_file_flags(opt
, contents
, path
,
1027 opt
->priv
->call_depth
|| clean
, !opt
->priv
->call_depth
);
1030 /* Low level file merging, update and removal */
1032 struct merge_file_info
{
1033 struct diff_filespec blob
; /* mostly use oid & mode; sometimes path */
1038 static int merge_3way(struct merge_options
*opt
,
1039 mmbuffer_t
*result_buf
,
1040 const struct diff_filespec
*o
,
1041 const struct diff_filespec
*a
,
1042 const struct diff_filespec
*b
,
1043 const char *branch1
,
1044 const char *branch2
,
1045 const int extra_marker_size
)
1047 mmfile_t orig
, src1
, src2
;
1048 struct ll_merge_options ll_opts
= {0};
1049 char *base
, *name1
, *name2
;
1050 enum ll_merge_result merge_status
;
1052 ll_opts
.renormalize
= opt
->renormalize
;
1053 ll_opts
.extra_marker_size
= extra_marker_size
;
1054 ll_opts
.xdl_opts
= opt
->xdl_opts
;
1056 if (opt
->priv
->call_depth
) {
1057 ll_opts
.virtual_ancestor
= 1;
1058 ll_opts
.variant
= 0;
1060 switch (opt
->recursive_variant
) {
1061 case MERGE_VARIANT_OURS
:
1062 ll_opts
.variant
= XDL_MERGE_FAVOR_OURS
;
1064 case MERGE_VARIANT_THEIRS
:
1065 ll_opts
.variant
= XDL_MERGE_FAVOR_THEIRS
;
1068 ll_opts
.variant
= 0;
1073 assert(a
->path
&& b
->path
&& o
->path
&& opt
->ancestor
);
1074 if (strcmp(a
->path
, b
->path
) || strcmp(a
->path
, o
->path
) != 0) {
1075 base
= mkpathdup("%s:%s", opt
->ancestor
, o
->path
);
1076 name1
= mkpathdup("%s:%s", branch1
, a
->path
);
1077 name2
= mkpathdup("%s:%s", branch2
, b
->path
);
1079 base
= mkpathdup("%s", opt
->ancestor
);
1080 name1
= mkpathdup("%s", branch1
);
1081 name2
= mkpathdup("%s", branch2
);
1084 read_mmblob(&orig
, &o
->oid
);
1085 read_mmblob(&src1
, &a
->oid
);
1086 read_mmblob(&src2
, &b
->oid
);
1089 * FIXME: Using a->path for normalization rules in ll_merge could be
1090 * wrong if we renamed from a->path to b->path. We should use the
1091 * target path for where the file will be written.
1093 merge_status
= ll_merge(result_buf
, a
->path
, &orig
, base
,
1094 &src1
, name1
, &src2
, name2
,
1095 opt
->repo
->index
, &ll_opts
);
1096 if (merge_status
== LL_MERGE_BINARY_CONFLICT
)
1097 warning("Cannot merge binary files: %s (%s vs. %s)",
1098 a
->path
, name1
, name2
);
1106 return merge_status
;
1109 static int find_first_merges(struct repository
*repo
,
1110 struct object_array
*result
, const char *path
,
1111 struct commit
*a
, struct commit
*b
)
1114 struct object_array merges
= OBJECT_ARRAY_INIT
;
1115 struct commit
*commit
;
1116 int contains_another
;
1118 char merged_revision
[GIT_MAX_HEXSZ
+ 2];
1119 const char *rev_args
[] = { "rev-list", "--merges", "--ancestry-path",
1120 "--all", merged_revision
, NULL
};
1121 struct rev_info revs
;
1122 struct setup_revision_opt rev_opts
;
1124 memset(result
, 0, sizeof(struct object_array
));
1125 memset(&rev_opts
, 0, sizeof(rev_opts
));
1127 /* get all revisions that merge commit a */
1128 xsnprintf(merged_revision
, sizeof(merged_revision
), "^%s",
1129 oid_to_hex(&a
->object
.oid
));
1130 repo_init_revisions(repo
, &revs
, NULL
);
1131 /* FIXME: can't handle linked worktrees in submodules yet */
1132 revs
.single_worktree
= path
!= NULL
;
1133 setup_revisions(ARRAY_SIZE(rev_args
)-1, rev_args
, &revs
, &rev_opts
);
1135 /* save all revisions from the above list that contain b */
1136 if (prepare_revision_walk(&revs
))
1137 die("revision walk setup failed");
1138 while ((commit
= get_revision(&revs
)) != NULL
) {
1139 struct object
*o
= &(commit
->object
);
1140 if (repo_in_merge_bases(repo
, b
, commit
))
1141 add_object_array(o
, NULL
, &merges
);
1143 reset_revision_walk();
1145 /* Now we've got all merges that contain a and b. Prune all
1146 * merges that contain another found merge and save them in
1149 for (i
= 0; i
< merges
.nr
; i
++) {
1150 struct commit
*m1
= (struct commit
*) merges
.objects
[i
].item
;
1152 contains_another
= 0;
1153 for (j
= 0; j
< merges
.nr
; j
++) {
1154 struct commit
*m2
= (struct commit
*) merges
.objects
[j
].item
;
1155 if (i
!= j
&& repo_in_merge_bases(repo
, m2
, m1
)) {
1156 contains_another
= 1;
1161 if (!contains_another
)
1162 add_object_array(merges
.objects
[i
].item
, NULL
, result
);
1165 object_array_clear(&merges
);
1166 release_revisions(&revs
);
1170 static void print_commit(struct repository
*repo
, struct commit
*commit
)
1172 struct strbuf sb
= STRBUF_INIT
;
1173 struct pretty_print_context ctx
= {0};
1174 ctx
.date_mode
.type
= DATE_NORMAL
;
1175 /* FIXME: Merge this with output_commit_title() */
1176 assert(!merge_remote_util(commit
));
1177 repo_format_commit_message(repo
, commit
, " %h: %m %s", &sb
, &ctx
);
1178 fprintf(stderr
, "%s\n", sb
.buf
);
1179 strbuf_release(&sb
);
1182 static int is_valid(const struct diff_filespec
*dfs
)
1184 return dfs
->mode
!= 0 && !is_null_oid(&dfs
->oid
);
1187 static int merge_submodule(struct merge_options
*opt
,
1188 struct object_id
*result
, const char *path
,
1189 const struct object_id
*base
, const struct object_id
*a
,
1190 const struct object_id
*b
)
1192 struct repository subrepo
;
1194 struct commit
*commit_base
, *commit_a
, *commit_b
;
1196 struct object_array merges
;
1199 int search
= !opt
->priv
->call_depth
;
1201 /* store a in result in case we fail */
1202 /* FIXME: This is the WRONG resolution for the recursive case when
1203 * we need to be careful to avoid accidentally matching either side.
1204 * Should probably use o instead there, much like we do for merging
1209 /* we can not handle deletion conflicts */
1210 if (is_null_oid(base
))
1217 if (repo_submodule_init(&subrepo
, opt
->repo
, path
, null_oid())) {
1218 output(opt
, 1, _("Failed to merge submodule %s (not checked out)"), path
);
1222 if (!(commit_base
= lookup_commit_reference(&subrepo
, base
)) ||
1223 !(commit_a
= lookup_commit_reference(&subrepo
, a
)) ||
1224 !(commit_b
= lookup_commit_reference(&subrepo
, b
))) {
1225 output(opt
, 1, _("Failed to merge submodule %s (commits not present)"), path
);
1229 /* check whether both changes are forward */
1230 if (!repo_in_merge_bases(&subrepo
, commit_base
, commit_a
) ||
1231 !repo_in_merge_bases(&subrepo
, commit_base
, commit_b
)) {
1232 output(opt
, 1, _("Failed to merge submodule %s (commits don't follow merge-base)"), path
);
1236 /* Case #1: a is contained in b or vice versa */
1237 if (repo_in_merge_bases(&subrepo
, commit_a
, commit_b
)) {
1240 output(opt
, 3, _("Fast-forwarding submodule %s to the following commit:"), path
);
1241 repo_output_commit_title(opt
, &subrepo
, commit_b
);
1242 } else if (show(opt
, 2))
1243 output(opt
, 2, _("Fast-forwarding submodule %s"), path
);
1250 if (repo_in_merge_bases(&subrepo
, commit_b
, commit_a
)) {
1253 output(opt
, 3, _("Fast-forwarding submodule %s to the following commit:"), path
);
1254 repo_output_commit_title(opt
, &subrepo
, commit_a
);
1255 } else if (show(opt
, 2))
1256 output(opt
, 2, _("Fast-forwarding submodule %s"), path
);
1265 * Case #2: There are one or more merges that contain a and b in
1266 * the submodule. If there is only one, then present it as a
1267 * suggestion to the user, but leave it marked unmerged so the
1268 * user needs to confirm the resolution.
1271 /* Skip the search if makes no sense to the calling context. */
1275 /* find commit which merges them */
1276 parent_count
= find_first_merges(&subrepo
, &merges
, path
,
1277 commit_a
, commit_b
);
1278 switch (parent_count
) {
1280 output(opt
, 1, _("Failed to merge submodule %s (merge following commits not found)"), path
);
1284 output(opt
, 1, _("Failed to merge submodule %s (not fast-forward)"), path
);
1285 output(opt
, 2, _("Found a possible merge resolution for the submodule:\n"));
1286 print_commit(&subrepo
, (struct commit
*) merges
.objects
[0].item
);
1288 "If this is correct simply add it to the index "
1291 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
1292 "which will accept this suggestion.\n"),
1293 oid_to_hex(&merges
.objects
[0].item
->oid
), path
);
1297 output(opt
, 1, _("Failed to merge submodule %s (multiple merges found)"), path
);
1298 for (i
= 0; i
< merges
.nr
; i
++)
1299 print_commit(&subrepo
, (struct commit
*) merges
.objects
[i
].item
);
1302 object_array_clear(&merges
);
1304 repo_clear(&subrepo
);
1308 static int merge_mode_and_contents(struct merge_options
*opt
,
1309 const struct diff_filespec
*o
,
1310 const struct diff_filespec
*a
,
1311 const struct diff_filespec
*b
,
1312 const char *filename
,
1313 const char *branch1
,
1314 const char *branch2
,
1315 const int extra_marker_size
,
1316 struct merge_file_info
*result
)
1318 if (opt
->branch1
!= branch1
) {
1320 * It's weird getting a reverse merge with HEAD on the bottom
1321 * side of the conflict markers and the other branch on the
1324 return merge_mode_and_contents(opt
, o
, b
, a
,
1327 extra_marker_size
, result
);
1333 if ((S_IFMT
& a
->mode
) != (S_IFMT
& b
->mode
)) {
1336 * FIXME: This is a bad resolution for recursive case; for
1337 * the recursive case we want something that is unlikely to
1338 * accidentally match either side. Also, while it makes
1339 * sense to prefer regular files over symlinks, it doesn't
1340 * make sense to prefer regular files over submodules.
1342 if (S_ISREG(a
->mode
)) {
1343 result
->blob
.mode
= a
->mode
;
1344 oidcpy(&result
->blob
.oid
, &a
->oid
);
1346 result
->blob
.mode
= b
->mode
;
1347 oidcpy(&result
->blob
.oid
, &b
->oid
);
1350 if (!oideq(&a
->oid
, &o
->oid
) && !oideq(&b
->oid
, &o
->oid
))
1356 if (a
->mode
== b
->mode
|| a
->mode
== o
->mode
)
1357 result
->blob
.mode
= b
->mode
;
1359 result
->blob
.mode
= a
->mode
;
1360 if (b
->mode
!= o
->mode
) {
1366 if (oideq(&a
->oid
, &b
->oid
) || oideq(&a
->oid
, &o
->oid
))
1367 oidcpy(&result
->blob
.oid
, &b
->oid
);
1368 else if (oideq(&b
->oid
, &o
->oid
))
1369 oidcpy(&result
->blob
.oid
, &a
->oid
);
1370 else if (S_ISREG(a
->mode
)) {
1371 mmbuffer_t result_buf
;
1372 int ret
= 0, merge_status
;
1374 merge_status
= merge_3way(opt
, &result_buf
, o
, a
, b
,
1378 if ((merge_status
< 0) || !result_buf
.ptr
)
1379 ret
= err(opt
, _("Failed to execute internal merge"));
1382 write_object_file(result_buf
.ptr
, result_buf
.size
,
1383 OBJ_BLOB
, &result
->blob
.oid
))
1384 ret
= err(opt
, _("Unable to add %s to database"),
1387 free(result_buf
.ptr
);
1390 /* FIXME: bug, what if modes didn't match? */
1391 result
->clean
= (merge_status
== 0);
1392 } else if (S_ISGITLINK(a
->mode
)) {
1393 result
->clean
= merge_submodule(opt
, &result
->blob
.oid
,
1398 } else if (S_ISLNK(a
->mode
)) {
1399 switch (opt
->recursive_variant
) {
1400 case MERGE_VARIANT_NORMAL
:
1401 oidcpy(&result
->blob
.oid
, &a
->oid
);
1402 if (!oideq(&a
->oid
, &b
->oid
))
1405 case MERGE_VARIANT_OURS
:
1406 oidcpy(&result
->blob
.oid
, &a
->oid
);
1408 case MERGE_VARIANT_THEIRS
:
1409 oidcpy(&result
->blob
.oid
, &b
->oid
);
1413 BUG("unsupported object type in the tree");
1417 output(opt
, 2, _("Auto-merging %s"), filename
);
1422 static int handle_rename_via_dir(struct merge_options
*opt
,
1423 struct rename_conflict_info
*ci
)
1426 * Handle file adds that need to be renamed due to directory rename
1427 * detection. This differs from handle_rename_normal, because
1428 * there is no content merge to do; just move the file into the
1429 * desired final location.
1431 const struct rename
*ren
= ci
->ren1
;
1432 const struct diff_filespec
*dest
= ren
->pair
->two
;
1433 char *file_path
= dest
->path
;
1434 int mark_conflicted
= (opt
->detect_directory_renames
==
1435 MERGE_DIRECTORY_RENAMES_CONFLICT
);
1436 assert(ren
->dir_rename_original_dest
);
1438 if (!opt
->priv
->call_depth
&& would_lose_untracked(opt
, dest
->path
)) {
1439 mark_conflicted
= 1;
1440 file_path
= unique_path(opt
, dest
->path
, ren
->branch
);
1441 output(opt
, 1, _("Error: Refusing to lose untracked file at %s; "
1442 "writing to %s instead."),
1443 dest
->path
, file_path
);
1446 if (mark_conflicted
) {
1448 * Write the file in worktree at file_path. In the index,
1449 * only record the file at dest->path in the appropriate
1452 if (update_file(opt
, 0, dest
, file_path
))
1454 if (file_path
!= dest
->path
)
1456 if (update_stages(opt
, dest
->path
, NULL
,
1457 ren
->branch
== opt
->branch1
? dest
: NULL
,
1458 ren
->branch
== opt
->branch1
? NULL
: dest
))
1460 return 0; /* not clean, but conflicted */
1462 /* Update dest->path both in index and in worktree */
1463 if (update_file(opt
, 1, dest
, dest
->path
))
1465 return 1; /* clean */
1469 static int handle_change_delete(struct merge_options
*opt
,
1470 const char *path
, const char *old_path
,
1471 const struct diff_filespec
*o
,
1472 const struct diff_filespec
*changed
,
1473 const char *change_branch
,
1474 const char *delete_branch
,
1475 const char *change
, const char *change_past
)
1477 char *alt_path
= NULL
;
1478 const char *update_path
= path
;
1481 if (dir_in_way(opt
->repo
->index
, path
, !opt
->priv
->call_depth
, 0) ||
1482 (!opt
->priv
->call_depth
&& would_lose_untracked(opt
, path
))) {
1483 update_path
= alt_path
= unique_path(opt
, path
, change_branch
);
1486 if (opt
->priv
->call_depth
) {
1488 * We cannot arbitrarily accept either a_sha or b_sha as
1489 * correct; since there is no true "middle point" between
1490 * them, simply reuse the base version for virtual merge base.
1492 ret
= remove_file_from_index(opt
->repo
->index
, path
);
1494 ret
= update_file(opt
, 0, o
, update_path
);
1497 * Despite the four nearly duplicate messages and argument
1498 * lists below and the ugliness of the nested if-statements,
1499 * having complete messages makes the job easier for
1502 * The slight variance among the cases is due to the fact
1504 * 1) directory/file conflicts (in effect if
1505 * !alt_path) could cause us to need to write the
1506 * file to a different path.
1507 * 2) renames (in effect if !old_path) could mean that
1508 * there are two names for the path that the user
1509 * may know the file by.
1513 output(opt
, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1514 "and %s in %s. Version %s of %s left in tree."),
1515 change
, path
, delete_branch
, change_past
,
1516 change_branch
, change_branch
, path
);
1518 output(opt
, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1519 "and %s to %s in %s. Version %s of %s left in tree."),
1520 change
, old_path
, delete_branch
, change_past
, path
,
1521 change_branch
, change_branch
, path
);
1525 output(opt
, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1526 "and %s in %s. Version %s of %s left in tree at %s."),
1527 change
, path
, delete_branch
, change_past
,
1528 change_branch
, change_branch
, path
, alt_path
);
1530 output(opt
, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1531 "and %s to %s in %s. Version %s of %s left in tree at %s."),
1532 change
, old_path
, delete_branch
, change_past
, path
,
1533 change_branch
, change_branch
, path
, alt_path
);
1537 * No need to call update_file() on path when change_branch ==
1538 * opt->branch1 && !alt_path, since that would needlessly touch
1539 * path. We could call update_file_flags() with update_cache=0
1540 * and update_wd=0, but that's a no-op.
1542 if (change_branch
!= opt
->branch1
|| alt_path
)
1543 ret
= update_file(opt
, 0, changed
, update_path
);
1550 static int handle_rename_delete(struct merge_options
*opt
,
1551 struct rename_conflict_info
*ci
)
1553 const struct rename
*ren
= ci
->ren1
;
1554 const struct diff_filespec
*orig
= ren
->pair
->one
;
1555 const struct diff_filespec
*dest
= ren
->pair
->two
;
1556 const char *rename_branch
= ren
->branch
;
1557 const char *delete_branch
= (opt
->branch1
== ren
->branch
?
1558 opt
->branch2
: opt
->branch1
);
1560 if (handle_change_delete(opt
,
1561 opt
->priv
->call_depth
? orig
->path
: dest
->path
,
1562 opt
->priv
->call_depth
? NULL
: orig
->path
,
1564 rename_branch
, delete_branch
,
1565 _("rename"), _("renamed")))
1568 if (opt
->priv
->call_depth
)
1569 return remove_file_from_index(opt
->repo
->index
, dest
->path
);
1571 return update_stages(opt
, dest
->path
, NULL
,
1572 rename_branch
== opt
->branch1
? dest
: NULL
,
1573 rename_branch
== opt
->branch1
? NULL
: dest
);
1576 static int handle_file_collision(struct merge_options
*opt
,
1577 const char *collide_path
,
1578 const char *prev_path1
,
1579 const char *prev_path2
,
1580 const char *branch1
, const char *branch2
,
1581 struct diff_filespec
*a
,
1582 struct diff_filespec
*b
)
1584 struct merge_file_info mfi
;
1585 struct diff_filespec null
;
1586 char *alt_path
= NULL
;
1587 const char *update_path
= collide_path
;
1590 * It's easiest to get the correct things into stage 2 and 3, and
1591 * to make sure that the content merge puts HEAD before the other
1592 * branch if we just ensure that branch1 == opt->branch1. So, simply
1593 * flip arguments around if we don't have that.
1595 if (branch1
!= opt
->branch1
) {
1596 return handle_file_collision(opt
, collide_path
,
1597 prev_path2
, prev_path1
,
1602 /* Remove rename sources if rename/add or rename/rename(2to1) */
1604 remove_file(opt
, 1, prev_path1
,
1605 opt
->priv
->call_depth
|| would_lose_untracked(opt
, prev_path1
));
1607 remove_file(opt
, 1, prev_path2
,
1608 opt
->priv
->call_depth
|| would_lose_untracked(opt
, prev_path2
));
1611 * Remove the collision path, if it wouldn't cause dirty contents
1612 * or an untracked file to get lost. We'll either overwrite with
1613 * merged contents, or just write out to differently named files.
1615 if (was_dirty(opt
, collide_path
)) {
1616 output(opt
, 1, _("Refusing to lose dirty file at %s"),
1618 update_path
= alt_path
= unique_path(opt
, collide_path
, "merged");
1619 } else if (would_lose_untracked(opt
, collide_path
)) {
1621 * Only way we get here is if both renames were from
1622 * a directory rename AND user had an untracked file
1623 * at the location where both files end up after the
1624 * two directory renames. See testcase 10d of t6043.
1626 output(opt
, 1, _("Refusing to lose untracked file at "
1627 "%s, even though it's in the way."),
1629 update_path
= alt_path
= unique_path(opt
, collide_path
, "merged");
1632 * FIXME: It's possible that the two files are identical
1633 * and that the current working copy happens to match, in
1634 * which case we are unnecessarily touching the working
1635 * tree file. It's not a likely enough scenario that I
1636 * want to code up the checks for it and a better fix is
1637 * available if we restructure how unpack_trees() and
1638 * merge-recursive interoperate anyway, so punting for
1641 remove_file(opt
, 0, collide_path
, 0);
1644 /* Store things in diff_filespecs for functions that need it */
1645 null
.path
= (char *)collide_path
;
1646 oidcpy(&null
.oid
, null_oid());
1649 if (merge_mode_and_contents(opt
, &null
, a
, b
, collide_path
,
1650 branch1
, branch2
, opt
->priv
->call_depth
* 2, &mfi
))
1652 mfi
.clean
&= !alt_path
;
1653 if (update_file(opt
, mfi
.clean
, &mfi
.blob
, update_path
))
1655 if (!mfi
.clean
&& !opt
->priv
->call_depth
&&
1656 update_stages(opt
, collide_path
, NULL
, a
, b
))
1660 * FIXME: If both a & b both started with conflicts (only possible
1661 * if they came from a rename/rename(2to1)), but had IDENTICAL
1662 * contents including those conflicts, then in the next line we claim
1663 * it was clean. If someone cares about this case, we should have the
1664 * caller notify us if we started with conflicts.
1669 static int handle_rename_add(struct merge_options
*opt
,
1670 struct rename_conflict_info
*ci
)
1672 /* a was renamed to c, and a separate c was added. */
1673 struct diff_filespec
*a
= ci
->ren1
->pair
->one
;
1674 struct diff_filespec
*c
= ci
->ren1
->pair
->two
;
1675 char *path
= c
->path
;
1676 char *prev_path_desc
;
1677 struct merge_file_info mfi
;
1679 const char *rename_branch
= ci
->ren1
->branch
;
1680 const char *add_branch
= (opt
->branch1
== rename_branch
?
1681 opt
->branch2
: opt
->branch1
);
1682 int other_stage
= (ci
->ren1
->branch
== opt
->branch1
? 3 : 2);
1684 output(opt
, 1, _("CONFLICT (rename/add): "
1685 "Rename %s->%s in %s. Added %s in %s"),
1686 a
->path
, c
->path
, rename_branch
,
1687 c
->path
, add_branch
);
1689 prev_path_desc
= xstrfmt("version of %s from %s", path
, a
->path
);
1690 ci
->ren1
->src_entry
->stages
[other_stage
].path
= a
->path
;
1691 if (merge_mode_and_contents(opt
, a
, c
,
1692 &ci
->ren1
->src_entry
->stages
[other_stage
],
1694 opt
->branch1
, opt
->branch2
,
1695 1 + opt
->priv
->call_depth
* 2, &mfi
))
1697 free(prev_path_desc
);
1699 ci
->ren1
->dst_entry
->stages
[other_stage
].path
= mfi
.blob
.path
= c
->path
;
1700 return handle_file_collision(opt
,
1701 c
->path
, a
->path
, NULL
,
1702 rename_branch
, add_branch
,
1704 &ci
->ren1
->dst_entry
->stages
[other_stage
]);
1707 static char *find_path_for_conflict(struct merge_options
*opt
,
1709 const char *branch1
,
1710 const char *branch2
)
1712 char *new_path
= NULL
;
1713 if (dir_in_way(opt
->repo
->index
, path
, !opt
->priv
->call_depth
, 0)) {
1714 new_path
= unique_path(opt
, path
, branch1
);
1715 output(opt
, 1, _("%s is a directory in %s adding "
1717 path
, branch2
, new_path
);
1718 } else if (would_lose_untracked(opt
, path
)) {
1719 new_path
= unique_path(opt
, path
, branch1
);
1720 output(opt
, 1, _("Refusing to lose untracked file"
1721 " at %s; adding as %s instead"),
1729 * Toggle the stage number between "ours" and "theirs" (2 and 3).
1731 static inline int flip_stage(int stage
)
1733 return (2 + 3) - stage
;
1736 static int handle_rename_rename_1to2(struct merge_options
*opt
,
1737 struct rename_conflict_info
*ci
)
1739 /* One file was renamed in both branches, but to different names. */
1740 struct merge_file_info mfi
;
1741 struct diff_filespec
*add
;
1742 struct diff_filespec
*o
= ci
->ren1
->pair
->one
;
1743 struct diff_filespec
*a
= ci
->ren1
->pair
->two
;
1744 struct diff_filespec
*b
= ci
->ren2
->pair
->two
;
1747 output(opt
, 1, _("CONFLICT (rename/rename): "
1748 "Rename \"%s\"->\"%s\" in branch \"%s\" "
1749 "rename \"%s\"->\"%s\" in \"%s\"%s"),
1750 o
->path
, a
->path
, ci
->ren1
->branch
,
1751 o
->path
, b
->path
, ci
->ren2
->branch
,
1752 opt
->priv
->call_depth
? _(" (left unresolved)") : "");
1754 path_desc
= xstrfmt("%s and %s, both renamed from %s",
1755 a
->path
, b
->path
, o
->path
);
1756 if (merge_mode_and_contents(opt
, o
, a
, b
, path_desc
,
1757 ci
->ren1
->branch
, ci
->ren2
->branch
,
1758 opt
->priv
->call_depth
* 2, &mfi
))
1762 if (opt
->priv
->call_depth
)
1763 remove_file_from_index(opt
->repo
->index
, o
->path
);
1766 * For each destination path, we need to see if there is a
1767 * rename/add collision. If not, we can write the file out
1768 * to the specified location.
1770 add
= &ci
->ren1
->dst_entry
->stages
[flip_stage(2)];
1771 if (is_valid(add
)) {
1772 add
->path
= mfi
.blob
.path
= a
->path
;
1773 if (handle_file_collision(opt
, a
->path
,
1777 &mfi
.blob
, add
) < 0)
1780 char *new_path
= find_path_for_conflict(opt
, a
->path
,
1783 if (update_file(opt
, 0, &mfi
.blob
,
1784 new_path
? new_path
: a
->path
))
1787 if (!opt
->priv
->call_depth
&&
1788 update_stages(opt
, a
->path
, NULL
, a
, NULL
))
1792 if (!mfi
.clean
&& mfi
.blob
.mode
== a
->mode
&&
1793 oideq(&mfi
.blob
.oid
, &a
->oid
)) {
1795 * Getting here means we were attempting to merge a binary
1796 * blob. Since we can't merge binaries, the merge algorithm
1797 * just takes one side. But we don't want to copy the
1798 * contents of one side to both paths; we'd rather use the
1799 * original content at the given path for each path.
1801 oidcpy(&mfi
.blob
.oid
, &b
->oid
);
1802 mfi
.blob
.mode
= b
->mode
;
1804 add
= &ci
->ren2
->dst_entry
->stages
[flip_stage(3)];
1805 if (is_valid(add
)) {
1806 add
->path
= mfi
.blob
.path
= b
->path
;
1807 if (handle_file_collision(opt
, b
->path
,
1811 add
, &mfi
.blob
) < 0)
1814 char *new_path
= find_path_for_conflict(opt
, b
->path
,
1817 if (update_file(opt
, 0, &mfi
.blob
,
1818 new_path
? new_path
: b
->path
))
1821 if (!opt
->priv
->call_depth
&&
1822 update_stages(opt
, b
->path
, NULL
, NULL
, b
))
1829 static int handle_rename_rename_2to1(struct merge_options
*opt
,
1830 struct rename_conflict_info
*ci
)
1832 /* Two files, a & b, were renamed to the same thing, c. */
1833 struct diff_filespec
*a
= ci
->ren1
->pair
->one
;
1834 struct diff_filespec
*b
= ci
->ren2
->pair
->one
;
1835 struct diff_filespec
*c1
= ci
->ren1
->pair
->two
;
1836 struct diff_filespec
*c2
= ci
->ren2
->pair
->two
;
1837 char *path
= c1
->path
; /* == c2->path */
1838 char *path_side_1_desc
;
1839 char *path_side_2_desc
;
1840 struct merge_file_info mfi_c1
;
1841 struct merge_file_info mfi_c2
;
1842 int ostage1
, ostage2
;
1844 output(opt
, 1, _("CONFLICT (rename/rename): "
1845 "Rename %s->%s in %s. "
1846 "Rename %s->%s in %s"),
1847 a
->path
, c1
->path
, ci
->ren1
->branch
,
1848 b
->path
, c2
->path
, ci
->ren2
->branch
);
1850 path_side_1_desc
= xstrfmt("version of %s from %s", path
, a
->path
);
1851 path_side_2_desc
= xstrfmt("version of %s from %s", path
, b
->path
);
1852 ostage1
= ci
->ren1
->branch
== opt
->branch1
? 3 : 2;
1853 ostage2
= flip_stage(ostage1
);
1854 ci
->ren1
->src_entry
->stages
[ostage1
].path
= a
->path
;
1855 ci
->ren2
->src_entry
->stages
[ostage2
].path
= b
->path
;
1856 if (merge_mode_and_contents(opt
, a
, c1
,
1857 &ci
->ren1
->src_entry
->stages
[ostage1
],
1859 opt
->branch1
, opt
->branch2
,
1860 1 + opt
->priv
->call_depth
* 2, &mfi_c1
) ||
1861 merge_mode_and_contents(opt
, b
,
1862 &ci
->ren2
->src_entry
->stages
[ostage2
],
1863 c2
, path_side_2_desc
,
1864 opt
->branch1
, opt
->branch2
,
1865 1 + opt
->priv
->call_depth
* 2, &mfi_c2
))
1867 free(path_side_1_desc
);
1868 free(path_side_2_desc
);
1869 mfi_c1
.blob
.path
= path
;
1870 mfi_c2
.blob
.path
= path
;
1872 return handle_file_collision(opt
, path
, a
->path
, b
->path
,
1873 ci
->ren1
->branch
, ci
->ren2
->branch
,
1874 &mfi_c1
.blob
, &mfi_c2
.blob
);
1878 * Get the diff_filepairs changed between o_tree and tree.
1880 static struct diff_queue_struct
*get_diffpairs(struct merge_options
*opt
,
1881 struct tree
*o_tree
,
1884 struct diff_queue_struct
*ret
;
1885 struct diff_options opts
;
1887 repo_diff_setup(opt
->repo
, &opts
);
1888 opts
.flags
.recursive
= 1;
1889 opts
.flags
.rename_empty
= 0;
1890 opts
.detect_rename
= merge_detect_rename(opt
);
1892 * We do not have logic to handle the detection of copies. In
1893 * fact, it may not even make sense to add such logic: would we
1894 * really want a change to a base file to be propagated through
1895 * multiple other files by a merge?
1897 if (opts
.detect_rename
> DIFF_DETECT_RENAME
)
1898 opts
.detect_rename
= DIFF_DETECT_RENAME
;
1899 opts
.rename_limit
= (opt
->rename_limit
>= 0) ? opt
->rename_limit
: 7000;
1900 opts
.rename_score
= opt
->rename_score
;
1901 opts
.show_rename_progress
= opt
->show_rename_progress
;
1902 opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1903 diff_setup_done(&opts
);
1904 diff_tree_oid(&o_tree
->object
.oid
, &tree
->object
.oid
, "", &opts
);
1905 diffcore_std(&opts
);
1906 if (opts
.needed_rename_limit
> opt
->priv
->needed_rename_limit
)
1907 opt
->priv
->needed_rename_limit
= opts
.needed_rename_limit
;
1909 ret
= xmalloc(sizeof(*ret
));
1910 *ret
= diff_queued_diff
;
1912 opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1913 diff_queued_diff
.nr
= 0;
1914 diff_queued_diff
.queue
= NULL
;
1919 static int tree_has_path(struct repository
*r
, struct tree
*tree
,
1922 struct object_id hashy
;
1923 unsigned short mode_o
;
1925 return !get_tree_entry(r
,
1926 &tree
->object
.oid
, path
,
1931 * Return a new string that replaces the beginning portion (which matches
1932 * entry->dir), with entry->new_dir. In perl-speak:
1933 * new_path_name = (old_path =~ s/entry->dir/entry->new_dir/);
1935 * Caller must ensure that old_path starts with entry->dir + '/'.
1937 static char *apply_dir_rename(struct dir_rename_entry
*entry
,
1938 const char *old_path
)
1940 struct strbuf new_path
= STRBUF_INIT
;
1943 if (entry
->non_unique_new_dir
)
1946 oldlen
= strlen(entry
->dir
);
1947 if (entry
->new_dir
.len
== 0)
1949 * If someone renamed/merged a subdirectory into the root
1950 * directory (e.g. 'some/subdir' -> ''), then we want to
1953 * as the rename; we need to make old_path + oldlen advance
1954 * past the '/' character.
1957 newlen
= entry
->new_dir
.len
+ (strlen(old_path
) - oldlen
) + 1;
1958 strbuf_grow(&new_path
, newlen
);
1959 strbuf_addbuf(&new_path
, &entry
->new_dir
);
1960 strbuf_addstr(&new_path
, &old_path
[oldlen
]);
1962 return strbuf_detach(&new_path
, NULL
);
1965 static void get_renamed_dir_portion(const char *old_path
, const char *new_path
,
1966 char **old_dir
, char **new_dir
)
1968 char *end_of_old
, *end_of_new
;
1970 /* Default return values: NULL, meaning no rename */
1976 * "a/b/c/d/e/foo.c" -> "a/b/some/thing/else/e/foo.c"
1977 * the "e/foo.c" part is the same, we just want to know that
1978 * "a/b/c/d" was renamed to "a/b/some/thing/else"
1979 * so, for this example, this function returns "a/b/c/d" in
1980 * *old_dir and "a/b/some/thing/else" in *new_dir.
1984 * If the basename of the file changed, we don't care. We want
1985 * to know which portion of the directory, if any, changed.
1987 end_of_old
= strrchr(old_path
, '/');
1988 end_of_new
= strrchr(new_path
, '/');
1991 * If end_of_old is NULL, old_path wasn't in a directory, so there
1992 * could not be a directory rename (our rule elsewhere that a
1993 * directory which still exists is not considered to have been
1994 * renamed means the root directory can never be renamed -- because
1995 * the root directory always exists).
1998 return; /* Note: *old_dir and *new_dir are still NULL */
2001 * If new_path contains no directory (end_of_new is NULL), then we
2002 * have a rename of old_path's directory to the root directory.
2005 *old_dir
= xstrndup(old_path
, end_of_old
- old_path
);
2006 *new_dir
= xstrdup("");
2010 /* Find the first non-matching character traversing backwards */
2011 while (*--end_of_new
== *--end_of_old
&&
2012 end_of_old
!= old_path
&&
2013 end_of_new
!= new_path
)
2014 ; /* Do nothing; all in the while loop */
2017 * If both got back to the beginning of their strings, then the
2018 * directory didn't change at all, only the basename did.
2020 if (end_of_old
== old_path
&& end_of_new
== new_path
&&
2021 *end_of_old
== *end_of_new
)
2022 return; /* Note: *old_dir and *new_dir are still NULL */
2025 * If end_of_new got back to the beginning of its string, and
2026 * end_of_old got back to the beginning of some subdirectory, then
2027 * we have a rename/merge of a subdirectory into the root, which
2028 * needs slightly special handling.
2030 * Note: There is no need to consider the opposite case, with a
2031 * rename/merge of the root directory into some subdirectory
2032 * because as noted above the root directory always exists so it
2033 * cannot be considered to be renamed.
2035 if (end_of_new
== new_path
&&
2036 end_of_old
!= old_path
&& end_of_old
[-1] == '/') {
2037 *old_dir
= xstrndup(old_path
, --end_of_old
- old_path
);
2038 *new_dir
= xstrdup("");
2043 * We've found the first non-matching character in the directory
2044 * paths. That means the current characters we were looking at
2045 * were part of the first non-matching subdir name going back from
2046 * the end of the strings. Get the whole name by advancing both
2047 * end_of_old and end_of_new to the NEXT '/' character. That will
2048 * represent the entire directory rename.
2050 * The reason for the increment is cases like
2051 * a/b/star/foo/whatever.c -> a/b/tar/foo/random.c
2052 * After dropping the basename and going back to the first
2053 * non-matching character, we're now comparing:
2055 * and we want to be comparing:
2056 * a/b/star/ and a/b/tar/
2057 * but without the pre-increment, the one on the right would stay
2060 end_of_old
= strchr(++end_of_old
, '/');
2061 end_of_new
= strchr(++end_of_new
, '/');
2063 /* Copy the old and new directories into *old_dir and *new_dir. */
2064 *old_dir
= xstrndup(old_path
, end_of_old
- old_path
);
2065 *new_dir
= xstrndup(new_path
, end_of_new
- new_path
);
2068 static void remove_hashmap_entries(struct hashmap
*dir_renames
,
2069 struct string_list
*items_to_remove
)
2072 struct dir_rename_entry
*entry
;
2074 for (i
= 0; i
< items_to_remove
->nr
; i
++) {
2075 entry
= items_to_remove
->items
[i
].util
;
2076 hashmap_remove(dir_renames
, &entry
->ent
, NULL
);
2078 string_list_clear(items_to_remove
, 0);
2082 * See if there is a directory rename for path, and if there are any file
2083 * level conflicts for the renamed location. If there is a rename and
2084 * there are no conflicts, return the new name. Otherwise, return NULL.
2086 static char *handle_path_level_conflicts(struct merge_options
*opt
,
2088 struct dir_rename_entry
*entry
,
2089 struct hashmap
*collisions
,
2092 char *new_path
= NULL
;
2093 struct collision_entry
*collision_ent
;
2095 struct strbuf collision_paths
= STRBUF_INIT
;
2098 * entry has the mapping of old directory name to new directory name
2099 * that we want to apply to path.
2101 new_path
= apply_dir_rename(entry
, path
);
2104 /* This should only happen when entry->non_unique_new_dir set */
2105 if (!entry
->non_unique_new_dir
)
2106 BUG("entry->non_unique_new_dir not set and !new_path");
2107 output(opt
, 1, _("CONFLICT (directory rename split): "
2108 "Unclear where to place %s because directory "
2109 "%s was renamed to multiple other directories, "
2110 "with no destination getting a majority of the "
2118 * The caller needs to have ensured that it has pre-populated
2119 * collisions with all paths that map to new_path. Do a quick check
2120 * to ensure that's the case.
2122 collision_ent
= collision_find_entry(collisions
, new_path
);
2124 BUG("collision_ent is NULL");
2127 * Check for one-sided add/add/.../add conflicts, i.e.
2128 * where implicit renames from the other side doing
2129 * directory rename(s) can affect this side of history
2130 * to put multiple paths into the same location. Warn
2131 * and bail on directory renames for such paths.
2133 if (collision_ent
->reported_already
) {
2135 } else if (tree_has_path(opt
->repo
, tree
, new_path
)) {
2136 collision_ent
->reported_already
= 1;
2137 strbuf_add_separated_string_list(&collision_paths
, ", ",
2138 &collision_ent
->source_files
);
2139 output(opt
, 1, _("CONFLICT (implicit dir rename): Existing "
2140 "file/dir at %s in the way of implicit "
2141 "directory rename(s) putting the following "
2142 "path(s) there: %s."),
2143 new_path
, collision_paths
.buf
);
2145 } else if (collision_ent
->source_files
.nr
> 1) {
2146 collision_ent
->reported_already
= 1;
2147 strbuf_add_separated_string_list(&collision_paths
, ", ",
2148 &collision_ent
->source_files
);
2149 output(opt
, 1, _("CONFLICT (implicit dir rename): Cannot map "
2150 "more than one path to %s; implicit directory "
2151 "renames tried to put these paths there: %s"),
2152 new_path
, collision_paths
.buf
);
2156 /* Free memory we no longer need */
2157 strbuf_release(&collision_paths
);
2158 if (!clean
&& new_path
) {
2167 * There are a couple things we want to do at the directory level:
2168 * 1. Check for both sides renaming to the same thing, in order to avoid
2169 * implicit renaming of files that should be left in place. (See
2170 * testcase 6b in t6043 for details.)
2171 * 2. Prune directory renames if there are still files left in the
2172 * original directory. These represent a partial directory rename,
2173 * i.e. a rename where only some of the files within the directory
2174 * were renamed elsewhere. (Technically, this could be done earlier
2175 * in get_directory_renames(), except that would prevent us from
2176 * doing the previous check and thus failing testcase 6b.)
2177 * 3. Check for rename/rename(1to2) conflicts (at the directory level).
2178 * In the future, we could potentially record this info as well and
2179 * omit reporting rename/rename(1to2) conflicts for each path within
2180 * the affected directories, thus cleaning up the merge output.
2181 * NOTE: We do NOT check for rename/rename(2to1) conflicts at the
2182 * directory level, because merging directories is fine. If it
2183 * causes conflicts for files within those merged directories, then
2184 * that should be detected at the individual path level.
2186 static void handle_directory_level_conflicts(struct merge_options
*opt
,
2187 struct hashmap
*dir_re_head
,
2189 struct hashmap
*dir_re_merge
,
2192 struct hashmap_iter iter
;
2193 struct dir_rename_entry
*head_ent
;
2194 struct dir_rename_entry
*merge_ent
;
2196 struct string_list remove_from_head
= STRING_LIST_INIT_NODUP
;
2197 struct string_list remove_from_merge
= STRING_LIST_INIT_NODUP
;
2199 hashmap_for_each_entry(dir_re_head
, &iter
, head_ent
,
2200 ent
/* member name */) {
2201 merge_ent
= dir_rename_find_entry(dir_re_merge
, head_ent
->dir
);
2203 !head_ent
->non_unique_new_dir
&&
2204 !merge_ent
->non_unique_new_dir
&&
2205 !strbuf_cmp(&head_ent
->new_dir
, &merge_ent
->new_dir
)) {
2206 /* 1. Renamed identically; remove it from both sides */
2207 string_list_append(&remove_from_head
,
2208 head_ent
->dir
)->util
= head_ent
;
2209 strbuf_release(&head_ent
->new_dir
);
2210 string_list_append(&remove_from_merge
,
2211 merge_ent
->dir
)->util
= merge_ent
;
2212 strbuf_release(&merge_ent
->new_dir
);
2213 } else if (tree_has_path(opt
->repo
, head
, head_ent
->dir
)) {
2214 /* 2. This wasn't a directory rename after all */
2215 string_list_append(&remove_from_head
,
2216 head_ent
->dir
)->util
= head_ent
;
2217 strbuf_release(&head_ent
->new_dir
);
2221 remove_hashmap_entries(dir_re_head
, &remove_from_head
);
2222 remove_hashmap_entries(dir_re_merge
, &remove_from_merge
);
2224 hashmap_for_each_entry(dir_re_merge
, &iter
, merge_ent
,
2225 ent
/* member name */) {
2226 head_ent
= dir_rename_find_entry(dir_re_head
, merge_ent
->dir
);
2227 if (tree_has_path(opt
->repo
, merge
, merge_ent
->dir
)) {
2228 /* 2. This wasn't a directory rename after all */
2229 string_list_append(&remove_from_merge
,
2230 merge_ent
->dir
)->util
= merge_ent
;
2231 } else if (head_ent
&&
2232 !head_ent
->non_unique_new_dir
&&
2233 !merge_ent
->non_unique_new_dir
) {
2234 /* 3. rename/rename(1to2) */
2236 * We can assume it's not rename/rename(1to1) because
2237 * that was case (1), already checked above. So we
2238 * know that head_ent->new_dir and merge_ent->new_dir
2239 * are different strings.
2241 output(opt
, 1, _("CONFLICT (rename/rename): "
2242 "Rename directory %s->%s in %s. "
2243 "Rename directory %s->%s in %s"),
2244 head_ent
->dir
, head_ent
->new_dir
.buf
, opt
->branch1
,
2245 head_ent
->dir
, merge_ent
->new_dir
.buf
, opt
->branch2
);
2246 string_list_append(&remove_from_head
,
2247 head_ent
->dir
)->util
= head_ent
;
2248 strbuf_release(&head_ent
->new_dir
);
2249 string_list_append(&remove_from_merge
,
2250 merge_ent
->dir
)->util
= merge_ent
;
2251 strbuf_release(&merge_ent
->new_dir
);
2255 remove_hashmap_entries(dir_re_head
, &remove_from_head
);
2256 remove_hashmap_entries(dir_re_merge
, &remove_from_merge
);
2259 static struct hashmap
*get_directory_renames(struct diff_queue_struct
*pairs
)
2261 struct hashmap
*dir_renames
;
2262 struct hashmap_iter iter
;
2263 struct dir_rename_entry
*entry
;
2267 * Typically, we think of a directory rename as all files from a
2268 * certain directory being moved to a target directory. However,
2269 * what if someone first moved two files from the original
2270 * directory in one commit, and then renamed the directory
2271 * somewhere else in a later commit? At merge time, we just know
2272 * that files from the original directory went to two different
2273 * places, and that the bulk of them ended up in the same place.
2274 * We want each directory rename to represent where the bulk of the
2275 * files from that directory end up; this function exists to find
2276 * where the bulk of the files went.
2278 * The first loop below simply iterates through the list of file
2279 * renames, finding out how often each directory rename pair
2280 * possibility occurs.
2282 dir_renames
= xmalloc(sizeof(*dir_renames
));
2283 dir_rename_init(dir_renames
);
2284 for (i
= 0; i
< pairs
->nr
; ++i
) {
2285 struct string_list_item
*item
;
2287 struct diff_filepair
*pair
= pairs
->queue
[i
];
2288 char *old_dir
, *new_dir
;
2290 /* File not part of directory rename if it wasn't renamed */
2291 if (pair
->status
!= 'R')
2294 get_renamed_dir_portion(pair
->one
->path
, pair
->two
->path
,
2295 &old_dir
, &new_dir
);
2297 /* Directory didn't change at all; ignore this one. */
2300 entry
= dir_rename_find_entry(dir_renames
, old_dir
);
2302 entry
= xmalloc(sizeof(*entry
));
2303 dir_rename_entry_init(entry
, old_dir
);
2304 hashmap_put(dir_renames
, &entry
->ent
);
2308 item
= string_list_lookup(&entry
->possible_new_dirs
, new_dir
);
2310 item
= string_list_insert(&entry
->possible_new_dirs
,
2312 item
->util
= xcalloc(1, sizeof(int));
2321 * For each directory with files moved out of it, we find out which
2322 * target directory received the most files so we can declare it to
2323 * be the "winning" target location for the directory rename. This
2324 * winner gets recorded in new_dir. If there is no winner
2325 * (multiple target directories received the same number of files),
2326 * we set non_unique_new_dir. Once we've determined the winner (or
2327 * that there is no winner), we no longer need possible_new_dirs.
2329 hashmap_for_each_entry(dir_renames
, &iter
, entry
,
2330 ent
/* member name */) {
2335 for (i
= 0; i
< entry
->possible_new_dirs
.nr
; i
++) {
2336 int *count
= entry
->possible_new_dirs
.items
[i
].util
;
2340 else if (*count
> max
) {
2342 best
= entry
->possible_new_dirs
.items
[i
].string
;
2346 entry
->non_unique_new_dir
= 1;
2348 assert(entry
->new_dir
.len
== 0);
2349 strbuf_addstr(&entry
->new_dir
, best
);
2352 * The relevant directory sub-portion of the original full
2353 * filepaths were xstrndup'ed before inserting into
2354 * possible_new_dirs, and instead of manually iterating the
2355 * list and free'ing each, just lie and tell
2356 * possible_new_dirs that it did the strdup'ing so that it
2357 * will free them for us.
2359 entry
->possible_new_dirs
.strdup_strings
= 1;
2360 string_list_clear(&entry
->possible_new_dirs
, 1);
2366 static struct dir_rename_entry
*check_dir_renamed(const char *path
,
2367 struct hashmap
*dir_renames
)
2369 char *temp
= xstrdup(path
);
2371 struct dir_rename_entry
*entry
= NULL
;
2373 while ((end
= strrchr(temp
, '/'))) {
2375 entry
= dir_rename_find_entry(dir_renames
, temp
);
2383 static void compute_collisions(struct hashmap
*collisions
,
2384 struct hashmap
*dir_renames
,
2385 struct diff_queue_struct
*pairs
)
2390 * Multiple files can be mapped to the same path due to directory
2391 * renames done by the other side of history. Since that other
2392 * side of history could have merged multiple directories into one,
2393 * if our side of history added the same file basename to each of
2394 * those directories, then all N of them would get implicitly
2395 * renamed by the directory rename detection into the same path,
2396 * and we'd get an add/add/.../add conflict, and all those adds
2397 * from *this* side of history. This is not representable in the
2398 * index, and users aren't going to easily be able to make sense of
2399 * it. So we need to provide a good warning about what's
2400 * happening, and fall back to no-directory-rename detection
2401 * behavior for those paths.
2403 * See testcases 9e and all of section 5 from t6043 for examples.
2405 collision_init(collisions
);
2407 for (i
= 0; i
< pairs
->nr
; ++i
) {
2408 struct dir_rename_entry
*dir_rename_ent
;
2409 struct collision_entry
*collision_ent
;
2411 struct diff_filepair
*pair
= pairs
->queue
[i
];
2413 if (pair
->status
!= 'A' && pair
->status
!= 'R')
2415 dir_rename_ent
= check_dir_renamed(pair
->two
->path
,
2417 if (!dir_rename_ent
)
2420 new_path
= apply_dir_rename(dir_rename_ent
, pair
->two
->path
);
2423 * dir_rename_ent->non_unique_new_path is true, which
2424 * means there is no directory rename for us to use,
2425 * which means it won't cause us any additional
2429 collision_ent
= collision_find_entry(collisions
, new_path
);
2430 if (!collision_ent
) {
2431 CALLOC_ARRAY(collision_ent
, 1);
2432 hashmap_entry_init(&collision_ent
->ent
,
2434 hashmap_put(collisions
, &collision_ent
->ent
);
2435 collision_ent
->target_file
= new_path
;
2439 string_list_insert(&collision_ent
->source_files
,
2444 static char *check_for_directory_rename(struct merge_options
*opt
,
2447 struct hashmap
*dir_renames
,
2448 struct hashmap
*dir_rename_exclusions
,
2449 struct hashmap
*collisions
,
2452 char *new_path
= NULL
;
2453 struct dir_rename_entry
*entry
= check_dir_renamed(path
, dir_renames
);
2454 struct dir_rename_entry
*oentry
= NULL
;
2460 * This next part is a little weird. We do not want to do an
2461 * implicit rename into a directory we renamed on our side, because
2462 * that will result in a spurious rename/rename(1to2) conflict. An
2464 * Base commit: dumbdir/afile, otherdir/bfile
2465 * Side 1: smrtdir/afile, otherdir/bfile
2466 * Side 2: dumbdir/afile, dumbdir/bfile
2467 * Here, while working on Side 1, we could notice that otherdir was
2468 * renamed/merged to dumbdir, and change the diff_filepair for
2469 * otherdir/bfile into a rename into dumbdir/bfile. However, Side
2470 * 2 will notice the rename from dumbdir to smrtdir, and do the
2471 * transitive rename to move it from dumbdir/bfile to
2472 * smrtdir/bfile. That gives us bfile in dumbdir vs being in
2473 * smrtdir, a rename/rename(1to2) conflict. We really just want
2474 * the file to end up in smrtdir. And the way to achieve that is
2475 * to not let Side1 do the rename to dumbdir, since we know that is
2476 * the source of one of our directory renames.
2478 * That's why oentry and dir_rename_exclusions is here.
2480 * As it turns out, this also prevents N-way transient rename
2481 * confusion; See testcases 9c and 9d of t6043.
2483 oentry
= dir_rename_find_entry(dir_rename_exclusions
, entry
->new_dir
.buf
);
2485 output(opt
, 1, _("WARNING: Avoiding applying %s -> %s rename "
2486 "to %s, because %s itself was renamed."),
2487 entry
->dir
, entry
->new_dir
.buf
, path
, entry
->new_dir
.buf
);
2489 new_path
= handle_path_level_conflicts(opt
, path
, entry
,
2491 *clean_merge
&= (new_path
!= NULL
);
2497 static void apply_directory_rename_modifications(struct merge_options
*opt
,
2498 struct diff_filepair
*pair
,
2502 struct tree
*o_tree
,
2503 struct tree
*a_tree
,
2504 struct tree
*b_tree
,
2505 struct string_list
*entries
)
2507 struct string_list_item
*item
;
2508 int stage
= (tree
== a_tree
? 2 : 3);
2512 * In all cases where we can do directory rename detection,
2513 * unpack_trees() will have read pair->two->path into the
2514 * index and the working copy. We need to remove it so that
2515 * we can instead place it at new_path. It is guaranteed to
2516 * not be untracked (unpack_trees() would have errored out
2517 * saying the file would have been overwritten), but it might
2520 update_wd
= !was_dirty(opt
, pair
->two
->path
);
2522 output(opt
, 1, _("Refusing to lose dirty file at %s"),
2524 remove_file(opt
, 1, pair
->two
->path
, !update_wd
);
2526 /* Find or create a new re->dst_entry */
2527 item
= string_list_lookup(entries
, new_path
);
2530 * Since we're renaming on this side of history, and it's
2531 * due to a directory rename on the other side of history
2532 * (which we only allow when the directory in question no
2533 * longer exists on the other side of history), the
2534 * original entry for re->dst_entry is no longer
2537 re
->dst_entry
->processed
= 1;
2540 * ...because we'll be using this new one.
2542 re
->dst_entry
= item
->util
;
2545 * re->dst_entry is for the before-dir-rename path, and we
2546 * need it to hold information for the after-dir-rename
2547 * path. Before creating a new entry, we need to mark the
2548 * old one as unnecessary (...unless it is shared by
2549 * src_entry, i.e. this didn't use to be a rename, in which
2550 * case we can just allow the normal processing to happen
2553 if (pair
->status
== 'R')
2554 re
->dst_entry
->processed
= 1;
2556 re
->dst_entry
= insert_stage_data(opt
->repo
, new_path
,
2557 o_tree
, a_tree
, b_tree
,
2559 item
= string_list_insert(entries
, new_path
);
2560 item
->util
= re
->dst_entry
;
2564 * Update the stage_data with the information about the path we are
2565 * moving into place. That slot will be empty and available for us
2566 * to write to because of the collision checks in
2567 * handle_path_level_conflicts(). In other words,
2568 * re->dst_entry->stages[stage].oid will be the null_oid, so it's
2569 * open for us to write to.
2571 * It may be tempting to actually update the index at this point as
2572 * well, using update_stages_for_stage_data(), but as per the big
2573 * "NOTE" in update_stages(), doing so will modify the current
2574 * in-memory index which will break calls to would_lose_untracked()
2575 * that we need to make. Instead, we need to just make sure that
2576 * the various handle_rename_*() functions update the index
2577 * explicitly rather than relying on unpack_trees() to have done it.
2579 get_tree_entry(opt
->repo
,
2582 &re
->dst_entry
->stages
[stage
].oid
,
2583 &re
->dst_entry
->stages
[stage
].mode
);
2586 * Record the original change status (or 'type' of change). If it
2587 * was originally an add ('A'), this lets us differentiate later
2588 * between a RENAME_DELETE conflict and RENAME_VIA_DIR (they
2589 * otherwise look the same). If it was originally a rename ('R'),
2590 * this lets us remember and report accurately about the transitive
2591 * renaming that occurred via the directory rename detection. Also,
2592 * record the original destination name.
2594 re
->dir_rename_original_type
= pair
->status
;
2595 re
->dir_rename_original_dest
= pair
->two
->path
;
2598 * We don't actually look at pair->status again, but it seems
2599 * pedagogically correct to adjust it.
2604 * Finally, record the new location.
2606 pair
->two
->path
= new_path
;
2610 * Get information of all renames which occurred in 'pairs', making use of
2611 * any implicit directory renames inferred from the other side of history.
2612 * We need the three trees in the merge ('o_tree', 'a_tree' and 'b_tree')
2613 * to be able to associate the correct cache entries with the rename
2614 * information; tree is always equal to either a_tree or b_tree.
2616 static struct string_list
*get_renames(struct merge_options
*opt
,
2618 struct diff_queue_struct
*pairs
,
2619 struct hashmap
*dir_renames
,
2620 struct hashmap
*dir_rename_exclusions
,
2622 struct tree
*o_tree
,
2623 struct tree
*a_tree
,
2624 struct tree
*b_tree
,
2625 struct string_list
*entries
,
2629 struct hashmap collisions
;
2630 struct hashmap_iter iter
;
2631 struct collision_entry
*e
;
2632 struct string_list
*renames
;
2634 compute_collisions(&collisions
, dir_renames
, pairs
);
2635 CALLOC_ARRAY(renames
, 1);
2637 for (i
= 0; i
< pairs
->nr
; ++i
) {
2638 struct string_list_item
*item
;
2640 struct diff_filepair
*pair
= pairs
->queue
[i
];
2641 char *new_path
; /* non-NULL only with directory renames */
2643 if (pair
->status
!= 'A' && pair
->status
!= 'R') {
2644 diff_free_filepair(pair
);
2647 new_path
= check_for_directory_rename(opt
, pair
->two
->path
, tree
,
2649 dir_rename_exclusions
,
2652 if (pair
->status
!= 'R' && !new_path
) {
2653 diff_free_filepair(pair
);
2657 re
= xmalloc(sizeof(*re
));
2660 re
->branch
= branch
;
2661 re
->dir_rename_original_type
= '\0';
2662 re
->dir_rename_original_dest
= NULL
;
2663 item
= string_list_lookup(entries
, re
->pair
->one
->path
);
2665 re
->src_entry
= insert_stage_data(opt
->repo
,
2666 re
->pair
->one
->path
,
2667 o_tree
, a_tree
, b_tree
, entries
);
2669 re
->src_entry
= item
->util
;
2671 item
= string_list_lookup(entries
, re
->pair
->two
->path
);
2673 re
->dst_entry
= insert_stage_data(opt
->repo
,
2674 re
->pair
->two
->path
,
2675 o_tree
, a_tree
, b_tree
, entries
);
2677 re
->dst_entry
= item
->util
;
2678 item
= string_list_insert(renames
, pair
->one
->path
);
2681 apply_directory_rename_modifications(opt
, pair
, new_path
,
2687 hashmap_for_each_entry(&collisions
, &iter
, e
,
2688 ent
/* member name */) {
2689 free(e
->target_file
);
2690 string_list_clear(&e
->source_files
, 0);
2692 hashmap_clear_and_free(&collisions
, struct collision_entry
, ent
);
2696 static int process_renames(struct merge_options
*opt
,
2697 struct string_list
*a_renames
,
2698 struct string_list
*b_renames
)
2700 int clean_merge
= 1, i
, j
;
2701 struct string_list a_by_dst
= STRING_LIST_INIT_NODUP
;
2702 struct string_list b_by_dst
= STRING_LIST_INIT_NODUP
;
2703 const struct rename
*sre
;
2706 * FIXME: As string-list.h notes, it's O(n^2) to build a sorted
2707 * string_list one-by-one, but O(n log n) to build it unsorted and
2708 * then sort it. Note that as we build the list, we do not need to
2709 * check if the existing destination path is already in the list,
2710 * because the structure of diffcore_rename guarantees we won't
2713 for (i
= 0; i
< a_renames
->nr
; i
++) {
2714 sre
= a_renames
->items
[i
].util
;
2715 string_list_insert(&a_by_dst
, sre
->pair
->two
->path
)->util
2718 for (i
= 0; i
< b_renames
->nr
; i
++) {
2719 sre
= b_renames
->items
[i
].util
;
2720 string_list_insert(&b_by_dst
, sre
->pair
->two
->path
)->util
2724 for (i
= 0, j
= 0; i
< a_renames
->nr
|| j
< b_renames
->nr
;) {
2725 struct string_list
*renames1
, *renames2Dst
;
2726 struct rename
*ren1
= NULL
, *ren2
= NULL
;
2727 const char *ren1_src
, *ren1_dst
;
2728 struct string_list_item
*lookup
;
2730 if (i
>= a_renames
->nr
) {
2731 ren2
= b_renames
->items
[j
++].util
;
2732 } else if (j
>= b_renames
->nr
) {
2733 ren1
= a_renames
->items
[i
++].util
;
2735 int compare
= strcmp(a_renames
->items
[i
].string
,
2736 b_renames
->items
[j
].string
);
2738 ren1
= a_renames
->items
[i
++].util
;
2740 ren2
= b_renames
->items
[j
++].util
;
2743 /* TODO: refactor, so that 1/2 are not needed */
2745 renames1
= a_renames
;
2746 renames2Dst
= &b_by_dst
;
2748 renames1
= b_renames
;
2749 renames2Dst
= &a_by_dst
;
2753 if (ren1
->processed
)
2755 ren1
->processed
= 1;
2756 ren1
->dst_entry
->processed
= 1;
2757 /* BUG: We should only mark src_entry as processed if we
2758 * are not dealing with a rename + add-source case.
2760 ren1
->src_entry
->processed
= 1;
2762 ren1_src
= ren1
->pair
->one
->path
;
2763 ren1_dst
= ren1
->pair
->two
->path
;
2766 /* One file renamed on both sides */
2767 const char *ren2_src
= ren2
->pair
->one
->path
;
2768 const char *ren2_dst
= ren2
->pair
->two
->path
;
2769 enum rename_type rename_type
;
2770 if (strcmp(ren1_src
, ren2_src
) != 0)
2771 BUG("ren1_src != ren2_src");
2772 ren2
->dst_entry
->processed
= 1;
2773 ren2
->processed
= 1;
2774 if (strcmp(ren1_dst
, ren2_dst
) != 0) {
2775 rename_type
= RENAME_ONE_FILE_TO_TWO
;
2778 rename_type
= RENAME_ONE_FILE_TO_ONE
;
2779 /* BUG: We should only remove ren1_src in
2780 * the base stage (think of rename +
2781 * add-source cases).
2783 remove_file(opt
, 1, ren1_src
, 1);
2784 update_entry(ren1
->dst_entry
,
2789 setup_rename_conflict_info(rename_type
, opt
, ren1
, ren2
);
2790 } else if ((lookup
= string_list_lookup(renames2Dst
, ren1_dst
))) {
2791 /* Two different files renamed to the same thing */
2793 ren2
= lookup
->util
;
2794 ren2_dst
= ren2
->pair
->two
->path
;
2795 if (strcmp(ren1_dst
, ren2_dst
) != 0)
2796 BUG("ren1_dst != ren2_dst");
2799 ren2
->processed
= 1;
2801 * BUG: We should only mark src_entry as processed
2802 * if we are not dealing with a rename + add-source
2805 ren2
->src_entry
->processed
= 1;
2807 setup_rename_conflict_info(RENAME_TWO_FILES_TO_ONE
,
2810 /* Renamed in 1, maybe changed in 2 */
2811 /* we only use sha1 and mode of these */
2812 struct diff_filespec src_other
, dst_other
;
2816 * unpack_trees loads entries from common-commit
2817 * into stage 1, from head-commit into stage 2, and
2818 * from merge-commit into stage 3. We keep track
2819 * of which side corresponds to the rename.
2821 int renamed_stage
= a_renames
== renames1
? 2 : 3;
2822 int other_stage
= a_renames
== renames1
? 3 : 2;
2825 * Directory renames have a funny corner case...
2827 int renamed_to_self
= !strcmp(ren1_src
, ren1_dst
);
2829 /* BUG: We should only remove ren1_src in the base
2830 * stage and in other_stage (think of rename +
2833 if (!renamed_to_self
)
2834 remove_file(opt
, 1, ren1_src
,
2835 renamed_stage
== 2 ||
2836 !was_tracked(opt
, ren1_src
));
2838 oidcpy(&src_other
.oid
,
2839 &ren1
->src_entry
->stages
[other_stage
].oid
);
2840 src_other
.mode
= ren1
->src_entry
->stages
[other_stage
].mode
;
2841 oidcpy(&dst_other
.oid
,
2842 &ren1
->dst_entry
->stages
[other_stage
].oid
);
2843 dst_other
.mode
= ren1
->dst_entry
->stages
[other_stage
].mode
;
2846 if (oideq(&src_other
.oid
, null_oid()) &&
2847 ren1
->dir_rename_original_type
== 'A') {
2848 setup_rename_conflict_info(RENAME_VIA_DIR
,
2850 } else if (renamed_to_self
) {
2851 setup_rename_conflict_info(RENAME_NORMAL
,
2853 } else if (oideq(&src_other
.oid
, null_oid())) {
2854 setup_rename_conflict_info(RENAME_DELETE
,
2856 } else if ((dst_other
.mode
== ren1
->pair
->two
->mode
) &&
2857 oideq(&dst_other
.oid
, &ren1
->pair
->two
->oid
)) {
2859 * Added file on the other side identical to
2860 * the file being renamed: clean merge.
2861 * Also, there is no need to overwrite the
2862 * file already in the working copy, so call
2863 * update_file_flags() instead of
2866 if (update_file_flags(opt
,
2869 1, /* update_cache */
2872 } else if (!oideq(&dst_other
.oid
, null_oid())) {
2874 * Probably not a clean merge, but it's
2875 * premature to set clean_merge to 0 here,
2876 * because if the rename merges cleanly and
2877 * the merge exactly matches the newly added
2878 * file, then the merge will be clean.
2880 setup_rename_conflict_info(RENAME_ADD
,
2885 if (clean_merge
< 0)
2886 goto cleanup_and_return
;
2888 struct diff_filespec
*o
, *a
, *b
;
2889 src_other
.path
= (char *)ren1_src
;
2891 o
= ren1
->pair
->one
;
2892 if (a_renames
== renames1
) {
2893 a
= ren1
->pair
->two
;
2896 b
= ren1
->pair
->two
;
2899 update_entry(ren1
->dst_entry
, o
, a
, b
);
2900 setup_rename_conflict_info(RENAME_NORMAL
,
2906 string_list_clear(&a_by_dst
, 0);
2907 string_list_clear(&b_by_dst
, 0);
2912 struct rename_info
{
2913 struct string_list
*head_renames
;
2914 struct string_list
*merge_renames
;
2917 static void initial_cleanup_rename(struct diff_queue_struct
*pairs
,
2918 struct hashmap
*dir_renames
)
2920 struct hashmap_iter iter
;
2921 struct dir_rename_entry
*e
;
2923 hashmap_for_each_entry(dir_renames
, &iter
, e
,
2924 ent
/* member name */) {
2926 strbuf_release(&e
->new_dir
);
2927 /* possible_new_dirs already cleared in get_directory_renames */
2929 hashmap_clear_and_free(dir_renames
, struct dir_rename_entry
, ent
);
2936 static int detect_and_process_renames(struct merge_options
*opt
,
2937 struct tree
*common
,
2940 struct string_list
*entries
,
2941 struct rename_info
*ri
)
2943 struct diff_queue_struct
*head_pairs
, *merge_pairs
;
2944 struct hashmap
*dir_re_head
, *dir_re_merge
;
2947 ri
->head_renames
= NULL
;
2948 ri
->merge_renames
= NULL
;
2950 if (!merge_detect_rename(opt
))
2953 head_pairs
= get_diffpairs(opt
, common
, head
);
2954 merge_pairs
= get_diffpairs(opt
, common
, merge
);
2956 if ((opt
->detect_directory_renames
== MERGE_DIRECTORY_RENAMES_TRUE
) ||
2957 (opt
->detect_directory_renames
== MERGE_DIRECTORY_RENAMES_CONFLICT
&&
2958 !opt
->priv
->call_depth
)) {
2959 dir_re_head
= get_directory_renames(head_pairs
);
2960 dir_re_merge
= get_directory_renames(merge_pairs
);
2962 handle_directory_level_conflicts(opt
,
2964 dir_re_merge
, merge
);
2966 dir_re_head
= xmalloc(sizeof(*dir_re_head
));
2967 dir_re_merge
= xmalloc(sizeof(*dir_re_merge
));
2968 dir_rename_init(dir_re_head
);
2969 dir_rename_init(dir_re_merge
);
2972 ri
->head_renames
= get_renames(opt
, opt
->branch1
, head_pairs
,
2973 dir_re_merge
, dir_re_head
, head
,
2974 common
, head
, merge
, entries
,
2978 ri
->merge_renames
= get_renames(opt
, opt
->branch2
, merge_pairs
,
2979 dir_re_head
, dir_re_merge
, merge
,
2980 common
, head
, merge
, entries
,
2984 clean
&= process_renames(opt
, ri
->head_renames
, ri
->merge_renames
);
2988 * Some cleanup is deferred until cleanup_renames() because the
2989 * data structures are still needed and referenced in
2990 * process_entry(). But there are a few things we can free now.
2992 initial_cleanup_rename(head_pairs
, dir_re_head
);
2993 initial_cleanup_rename(merge_pairs
, dir_re_merge
);
2998 static void final_cleanup_rename(struct string_list
*rename
)
3000 const struct rename
*re
;
3006 for (i
= 0; i
< rename
->nr
; i
++) {
3007 re
= rename
->items
[i
].util
;
3008 diff_free_filepair(re
->pair
);
3010 string_list_clear(rename
, 1);
3014 static void final_cleanup_renames(struct rename_info
*re_info
)
3016 final_cleanup_rename(re_info
->head_renames
);
3017 final_cleanup_rename(re_info
->merge_renames
);
3020 static int read_oid_strbuf(struct merge_options
*opt
,
3021 const struct object_id
*oid
,
3025 enum object_type type
;
3027 buf
= read_object_file(oid
, &type
, &size
);
3029 return err(opt
, _("cannot read object %s"), oid_to_hex(oid
));
3030 if (type
!= OBJ_BLOB
) {
3032 return err(opt
, _("object %s is not a blob"), oid_to_hex(oid
));
3034 strbuf_attach(dst
, buf
, size
, size
+ 1);
3038 static int blob_unchanged(struct merge_options
*opt
,
3039 const struct diff_filespec
*o
,
3040 const struct diff_filespec
*a
,
3041 int renormalize
, const char *path
)
3043 struct strbuf obuf
= STRBUF_INIT
;
3044 struct strbuf abuf
= STRBUF_INIT
;
3045 int ret
= 0; /* assume changed for safety */
3046 struct index_state
*idx
= opt
->repo
->index
;
3048 if (a
->mode
!= o
->mode
)
3050 if (oideq(&o
->oid
, &a
->oid
))
3055 if (read_oid_strbuf(opt
, &o
->oid
, &obuf
) ||
3056 read_oid_strbuf(opt
, &a
->oid
, &abuf
))
3059 * Note: binary | is used so that both renormalizations are
3060 * performed. Comparison can be skipped if both files are
3061 * unchanged since their sha1s have already been compared.
3063 if (renormalize_buffer(idx
, path
, obuf
.buf
, obuf
.len
, &obuf
) |
3064 renormalize_buffer(idx
, path
, abuf
.buf
, abuf
.len
, &abuf
))
3065 ret
= (obuf
.len
== abuf
.len
&& !memcmp(obuf
.buf
, abuf
.buf
, obuf
.len
));
3068 strbuf_release(&obuf
);
3069 strbuf_release(&abuf
);
3073 static int handle_modify_delete(struct merge_options
*opt
,
3075 const struct diff_filespec
*o
,
3076 const struct diff_filespec
*a
,
3077 const struct diff_filespec
*b
)
3079 const char *modify_branch
, *delete_branch
;
3080 const struct diff_filespec
*changed
;
3083 modify_branch
= opt
->branch1
;
3084 delete_branch
= opt
->branch2
;
3087 modify_branch
= opt
->branch2
;
3088 delete_branch
= opt
->branch1
;
3092 return handle_change_delete(opt
,
3095 modify_branch
, delete_branch
,
3096 _("modify"), _("modified"));
3099 static int handle_content_merge(struct merge_file_info
*mfi
,
3100 struct merge_options
*opt
,
3103 const struct diff_filespec
*o
,
3104 const struct diff_filespec
*a
,
3105 const struct diff_filespec
*b
,
3106 struct rename_conflict_info
*ci
)
3108 const char *reason
= _("content");
3109 unsigned df_conflict_remains
= 0;
3112 reason
= _("add/add");
3114 assert(o
->path
&& a
->path
&& b
->path
);
3115 if (ci
&& dir_in_way(opt
->repo
->index
, path
, !opt
->priv
->call_depth
,
3116 S_ISGITLINK(ci
->ren1
->pair
->two
->mode
)))
3117 df_conflict_remains
= 1;
3119 if (merge_mode_and_contents(opt
, o
, a
, b
, path
,
3120 opt
->branch1
, opt
->branch2
,
3121 opt
->priv
->call_depth
* 2, mfi
))
3125 * We can skip updating the working tree file iff:
3126 * a) The merge is clean
3127 * b) The merge matches what was in HEAD (content, mode, pathname)
3128 * c) The target path is usable (i.e. not involved in D/F conflict)
3130 if (mfi
->clean
&& was_tracked_and_matches(opt
, path
, &mfi
->blob
) &&
3131 !df_conflict_remains
) {
3133 struct cache_entry
*ce
;
3135 output(opt
, 3, _("Skipped %s (merged same as existing)"), path
);
3136 if (add_cacheinfo(opt
, &mfi
->blob
, path
,
3137 0, (!opt
->priv
->call_depth
&& !is_dirty
), 0))
3140 * However, add_cacheinfo() will delete the old cache entry
3141 * and add a new one. We need to copy over any skip_worktree
3142 * flag to avoid making the file appear as if it were
3143 * deleted by the user.
3145 pos
= index_name_pos(&opt
->priv
->orig_index
, path
, strlen(path
));
3146 ce
= opt
->priv
->orig_index
.cache
[pos
];
3147 if (ce_skip_worktree(ce
)) {
3148 pos
= index_name_pos(opt
->repo
->index
, path
, strlen(path
));
3149 ce
= opt
->repo
->index
->cache
[pos
];
3150 ce
->ce_flags
|= CE_SKIP_WORKTREE
;
3156 if (S_ISGITLINK(mfi
->blob
.mode
))
3157 reason
= _("submodule");
3158 output(opt
, 1, _("CONFLICT (%s): Merge conflict in %s"),
3160 if (ci
&& !df_conflict_remains
)
3161 if (update_stages(opt
, path
, o
, a
, b
))
3165 if (df_conflict_remains
|| is_dirty
) {
3167 if (opt
->priv
->call_depth
) {
3168 remove_file_from_index(opt
->repo
->index
, path
);
3171 if (update_stages(opt
, path
, o
, a
, b
))
3174 int file_from_stage2
= was_tracked(opt
, path
);
3176 if (update_stages(opt
, path
, NULL
,
3177 file_from_stage2
? &mfi
->blob
: NULL
,
3178 file_from_stage2
? NULL
: &mfi
->blob
))
3183 new_path
= unique_path(opt
, path
, ci
->ren1
->branch
);
3185 output(opt
, 1, _("Refusing to lose dirty file at %s"),
3188 output(opt
, 1, _("Adding as %s instead"), new_path
);
3189 if (update_file(opt
, 0, &mfi
->blob
, new_path
)) {
3195 } else if (update_file(opt
, mfi
->clean
, &mfi
->blob
, path
))
3197 return !is_dirty
&& mfi
->clean
;
3200 static int handle_rename_normal(struct merge_options
*opt
,
3202 const struct diff_filespec
*o
,
3203 const struct diff_filespec
*a
,
3204 const struct diff_filespec
*b
,
3205 struct rename_conflict_info
*ci
)
3207 struct rename
*ren
= ci
->ren1
;
3208 struct merge_file_info mfi
;
3211 /* Merge the content and write it out */
3212 clean
= handle_content_merge(&mfi
, opt
, path
, was_dirty(opt
, path
),
3216 opt
->detect_directory_renames
== MERGE_DIRECTORY_RENAMES_CONFLICT
&&
3217 ren
->dir_rename_original_dest
) {
3218 if (update_stages(opt
, path
,
3219 &mfi
.blob
, &mfi
.blob
, &mfi
.blob
))
3221 clean
= 0; /* not clean, but conflicted */
3226 static void dir_rename_warning(const char *msg
,
3229 struct merge_options
*opt
,
3232 const char *other_branch
;
3233 other_branch
= (ren
->branch
== opt
->branch1
?
3234 opt
->branch2
: opt
->branch1
);
3236 output(opt
, clean
? 2 : 1, msg
,
3237 ren
->pair
->one
->path
, ren
->branch
,
3238 other_branch
, ren
->pair
->two
->path
);
3241 output(opt
, clean
? 2 : 1, msg
,
3242 ren
->pair
->one
->path
, ren
->dir_rename_original_dest
, ren
->branch
,
3243 other_branch
, ren
->pair
->two
->path
);
3245 static int warn_about_dir_renamed_entries(struct merge_options
*opt
,
3249 int clean
= 1, is_add
;
3254 /* Return early if ren was not affected/created by a directory rename */
3255 if (!ren
->dir_rename_original_dest
)
3259 assert(opt
->detect_directory_renames
> MERGE_DIRECTORY_RENAMES_NONE
);
3260 assert(ren
->dir_rename_original_type
== 'A' ||
3261 ren
->dir_rename_original_type
== 'R');
3263 /* Check whether to treat directory renames as a conflict */
3264 clean
= (opt
->detect_directory_renames
== MERGE_DIRECTORY_RENAMES_TRUE
);
3266 is_add
= (ren
->dir_rename_original_type
== 'A');
3267 if (ren
->dir_rename_original_type
== 'A' && clean
) {
3268 msg
= _("Path updated: %s added in %s inside a "
3269 "directory that was renamed in %s; moving it to %s.");
3270 } else if (ren
->dir_rename_original_type
== 'A' && !clean
) {
3271 msg
= _("CONFLICT (file location): %s added in %s "
3272 "inside a directory that was renamed in %s, "
3273 "suggesting it should perhaps be moved to %s.");
3274 } else if (ren
->dir_rename_original_type
== 'R' && clean
) {
3275 msg
= _("Path updated: %s renamed to %s in %s, inside a "
3276 "directory that was renamed in %s; moving it to %s.");
3277 } else if (ren
->dir_rename_original_type
== 'R' && !clean
) {
3278 msg
= _("CONFLICT (file location): %s renamed to %s in %s, "
3279 "inside a directory that was renamed in %s, "
3280 "suggesting it should perhaps be moved to %s.");
3282 BUG("Impossible dir_rename_original_type/clean combination");
3284 dir_rename_warning(msg
, is_add
, clean
, opt
, ren
);
3289 /* Per entry merge function */
3290 static int process_entry(struct merge_options
*opt
,
3291 const char *path
, struct stage_data
*entry
)
3293 int clean_merge
= 1;
3294 int normalize
= opt
->renormalize
;
3296 struct diff_filespec
*o
= &entry
->stages
[1];
3297 struct diff_filespec
*a
= &entry
->stages
[2];
3298 struct diff_filespec
*b
= &entry
->stages
[3];
3299 int o_valid
= is_valid(o
);
3300 int a_valid
= is_valid(a
);
3301 int b_valid
= is_valid(b
);
3302 o
->path
= a
->path
= b
->path
= (char*)path
;
3304 entry
->processed
= 1;
3305 if (entry
->rename_conflict_info
) {
3306 struct rename_conflict_info
*ci
= entry
->rename_conflict_info
;
3307 struct diff_filespec
*temp
;
3310 path_clean
= warn_about_dir_renamed_entries(opt
, ci
->ren1
);
3311 path_clean
&= warn_about_dir_renamed_entries(opt
, ci
->ren2
);
3314 * For cases with a single rename, {o,a,b}->path have all been
3315 * set to the rename target path; we need to set two of these
3316 * back to the rename source.
3317 * For rename/rename conflicts, we'll manually fix paths below.
3319 temp
= (opt
->branch1
== ci
->ren1
->branch
) ? b
: a
;
3320 o
->path
= temp
->path
= ci
->ren1
->pair
->one
->path
;
3322 assert(opt
->branch1
== ci
->ren1
->branch
);
3325 switch (ci
->rename_type
) {
3327 case RENAME_ONE_FILE_TO_ONE
:
3328 clean_merge
= handle_rename_normal(opt
, path
, o
, a
, b
,
3331 case RENAME_VIA_DIR
:
3332 clean_merge
= handle_rename_via_dir(opt
, ci
);
3336 * Probably unclean merge, but if the renamed file
3337 * merges cleanly and the result can then be
3338 * two-way merged cleanly with the added file, I
3339 * guess it's a clean merge?
3341 clean_merge
= handle_rename_add(opt
, ci
);
3345 if (handle_rename_delete(opt
, ci
))
3348 case RENAME_ONE_FILE_TO_TWO
:
3350 * Manually fix up paths; note:
3351 * ren[12]->pair->one->path are equal.
3353 o
->path
= ci
->ren1
->pair
->one
->path
;
3354 a
->path
= ci
->ren1
->pair
->two
->path
;
3355 b
->path
= ci
->ren2
->pair
->two
->path
;
3358 if (handle_rename_rename_1to2(opt
, ci
))
3361 case RENAME_TWO_FILES_TO_ONE
:
3363 * Manually fix up paths; note,
3364 * ren[12]->pair->two->path are actually equal.
3367 a
->path
= ci
->ren1
->pair
->two
->path
;
3368 b
->path
= ci
->ren2
->pair
->two
->path
;
3371 * Probably unclean merge, but if the two renamed
3372 * files merge cleanly and the two resulting files
3373 * can then be two-way merged cleanly, I guess it's
3376 clean_merge
= handle_rename_rename_2to1(opt
, ci
);
3379 entry
->processed
= 0;
3382 if (path_clean
< clean_merge
)
3383 clean_merge
= path_clean
;
3384 } else if (o_valid
&& (!a_valid
|| !b_valid
)) {
3385 /* Case A: Deleted in one */
3386 if ((!a_valid
&& !b_valid
) ||
3387 (!b_valid
&& blob_unchanged(opt
, o
, a
, normalize
, path
)) ||
3388 (!a_valid
&& blob_unchanged(opt
, o
, b
, normalize
, path
))) {
3389 /* Deleted in both or deleted in one and
3390 * unchanged in the other */
3392 output(opt
, 2, _("Removing %s"), path
);
3393 /* do not touch working file if it did not exist */
3394 remove_file(opt
, 1, path
, !a_valid
);
3396 /* Modify/delete; deleted side may have put a directory in the way */
3398 if (handle_modify_delete(opt
, path
, o
, a
, b
))
3401 } else if ((!o_valid
&& a_valid
&& !b_valid
) ||
3402 (!o_valid
&& !a_valid
&& b_valid
)) {
3403 /* Case B: Added in one. */
3404 /* [nothing|directory] -> ([nothing|directory], file) */
3406 const char *add_branch
;
3407 const char *other_branch
;
3409 const struct diff_filespec
*contents
;
3412 add_branch
= opt
->branch1
;
3413 other_branch
= opt
->branch2
;
3415 conf
= _("file/directory");
3417 add_branch
= opt
->branch2
;
3418 other_branch
= opt
->branch1
;
3420 conf
= _("directory/file");
3422 if (dir_in_way(opt
->repo
->index
, path
,
3423 !opt
->priv
->call_depth
&& !S_ISGITLINK(a
->mode
),
3425 char *new_path
= unique_path(opt
, path
, add_branch
);
3427 output(opt
, 1, _("CONFLICT (%s): There is a directory with name %s in %s. "
3429 conf
, path
, other_branch
, path
, new_path
);
3430 if (update_file(opt
, 0, contents
, new_path
))
3432 else if (opt
->priv
->call_depth
)
3433 remove_file_from_index(opt
->repo
->index
, path
);
3436 output(opt
, 2, _("Adding %s"), path
);
3437 /* do not overwrite file if already present */
3438 if (update_file_flags(opt
, contents
, path
, 1, !a_valid
))
3441 } else if (a_valid
&& b_valid
) {
3443 /* Case C: Added in both (check for same permissions) */
3445 _("CONFLICT (add/add): Merge conflict in %s"),
3447 clean_merge
= handle_file_collision(opt
,
3453 /* case D: Modified in both, but differently. */
3454 struct merge_file_info mfi
;
3455 int is_dirty
= 0; /* unpack_trees would have bailed if dirty */
3456 clean_merge
= handle_content_merge(&mfi
, opt
, path
,
3460 } else if (!o_valid
&& !a_valid
&& !b_valid
) {
3462 * this entry was deleted altogether. a_mode == 0 means
3463 * we had that path and want to actively remove it.
3465 remove_file(opt
, 1, path
, !a
->mode
);
3467 BUG("fatal merge failure, shouldn't happen.");
3472 static int merge_trees_internal(struct merge_options
*opt
,
3475 struct tree
*merge_base
,
3476 struct tree
**result
)
3478 struct index_state
*istate
= opt
->repo
->index
;
3481 if (opt
->subtree_shift
) {
3482 merge
= shift_tree_object(opt
->repo
, head
, merge
,
3483 opt
->subtree_shift
);
3484 merge_base
= shift_tree_object(opt
->repo
, head
, merge_base
,
3485 opt
->subtree_shift
);
3488 if (oideq(&merge_base
->object
.oid
, &merge
->object
.oid
)) {
3489 output(opt
, 0, _("Already up to date."));
3494 code
= unpack_trees_start(opt
, merge_base
, head
, merge
);
3497 if (show(opt
, 4) || opt
->priv
->call_depth
)
3498 err(opt
, _("merging of trees %s and %s failed"),
3499 oid_to_hex(&head
->object
.oid
),
3500 oid_to_hex(&merge
->object
.oid
));
3501 unpack_trees_finish(opt
);
3505 if (unmerged_index(istate
)) {
3506 struct string_list
*entries
;
3507 struct rename_info re_info
;
3510 * Only need the hashmap while processing entries, so
3511 * initialize it here and free it when we are done running
3512 * through the entries. Keeping it in the merge_options as
3513 * opposed to decaring a local hashmap is for convenience
3514 * so that we don't have to pass it to around.
3516 hashmap_init(&opt
->priv
->current_file_dir_set
, path_hashmap_cmp
,
3518 get_files_dirs(opt
, head
);
3519 get_files_dirs(opt
, merge
);
3521 entries
= get_unmerged(opt
->repo
->index
);
3522 clean
= detect_and_process_renames(opt
, merge_base
, head
, merge
,
3524 record_df_conflict_files(opt
, entries
);
3527 for (i
= entries
->nr
-1; 0 <= i
; i
--) {
3528 const char *path
= entries
->items
[i
].string
;
3529 struct stage_data
*e
= entries
->items
[i
].util
;
3530 if (!e
->processed
) {
3531 int ret
= process_entry(opt
, path
, e
);
3540 for (i
= 0; i
< entries
->nr
; i
++) {
3541 struct stage_data
*e
= entries
->items
[i
].util
;
3543 BUG("unprocessed path??? %s",
3544 entries
->items
[i
].string
);
3548 final_cleanup_renames(&re_info
);
3550 string_list_clear(entries
, 1);
3553 hashmap_clear_and_free(&opt
->priv
->current_file_dir_set
,
3554 struct path_hashmap_entry
, e
);
3557 unpack_trees_finish(opt
);
3564 unpack_trees_finish(opt
);
3566 if (opt
->priv
->call_depth
&&
3567 !(*result
= write_in_core_index_as_tree(opt
->repo
)))
3574 * Merge the commits h1 and h2, returning a flag (int) indicating the
3575 * cleanness of the merge. Also, if opt->priv->call_depth, create a
3576 * virtual commit and write its location to *result.
3578 static int merge_recursive_internal(struct merge_options
*opt
,
3581 struct commit_list
*merge_bases
,
3582 struct commit
**result
)
3584 struct commit_list
*iter
;
3585 struct commit
*merged_merge_bases
;
3586 struct tree
*result_tree
;
3588 const char *ancestor_name
;
3589 struct strbuf merge_base_abbrev
= STRBUF_INIT
;
3592 output(opt
, 4, _("Merging:"));
3593 output_commit_title(opt
, h1
);
3594 output_commit_title(opt
, h2
);
3598 merge_bases
= get_merge_bases(h1
, h2
);
3599 merge_bases
= reverse_commit_list(merge_bases
);
3603 unsigned cnt
= commit_list_count(merge_bases
);
3605 output(opt
, 5, Q_("found %u common ancestor:",
3606 "found %u common ancestors:", cnt
), cnt
);
3607 for (iter
= merge_bases
; iter
; iter
= iter
->next
)
3608 output_commit_title(opt
, iter
->item
);
3611 merged_merge_bases
= pop_commit(&merge_bases
);
3612 if (!merged_merge_bases
) {
3613 /* if there is no common ancestor, use an empty tree */
3616 tree
= lookup_tree(opt
->repo
, opt
->repo
->hash_algo
->empty_tree
);
3617 merged_merge_bases
= make_virtual_commit(opt
->repo
, tree
,
3619 ancestor_name
= "empty tree";
3620 } else if (opt
->ancestor
&& !opt
->priv
->call_depth
) {
3621 ancestor_name
= opt
->ancestor
;
3622 } else if (merge_bases
) {
3623 ancestor_name
= "merged common ancestors";
3625 strbuf_add_unique_abbrev(&merge_base_abbrev
,
3626 &merged_merge_bases
->object
.oid
,
3628 ancestor_name
= merge_base_abbrev
.buf
;
3631 for (iter
= merge_bases
; iter
; iter
= iter
->next
) {
3632 const char *saved_b1
, *saved_b2
;
3633 opt
->priv
->call_depth
++;
3635 * When the merge fails, the result contains files
3636 * with conflict markers. The cleanness flag is
3637 * ignored (unless indicating an error), it was never
3638 * actually used, as result of merge_trees has always
3639 * overwritten it: the committed "conflicts" were
3642 discard_index(opt
->repo
->index
);
3643 saved_b1
= opt
->branch1
;
3644 saved_b2
= opt
->branch2
;
3645 opt
->branch1
= "Temporary merge branch 1";
3646 opt
->branch2
= "Temporary merge branch 2";
3647 if (merge_recursive_internal(opt
, merged_merge_bases
, iter
->item
,
3648 NULL
, &merged_merge_bases
) < 0)
3650 opt
->branch1
= saved_b1
;
3651 opt
->branch2
= saved_b2
;
3652 opt
->priv
->call_depth
--;
3654 if (!merged_merge_bases
)
3655 return err(opt
, _("merge returned no commit"));
3659 * FIXME: Since merge_recursive_internal() is only ever called by
3660 * places that ensure the index is loaded first
3661 * (e.g. builtin/merge.c, rebase/sequencer, etc.), in the common
3662 * case where the merge base was unique that means when we get here
3663 * we immediately discard the index and re-read it, which is a
3664 * complete waste of time. We should only be discarding and
3665 * re-reading if we were forced to recurse.
3667 discard_index(opt
->repo
->index
);
3668 if (!opt
->priv
->call_depth
)
3669 repo_read_index(opt
->repo
);
3671 opt
->ancestor
= ancestor_name
;
3672 clean
= merge_trees_internal(opt
,
3673 repo_get_commit_tree(opt
->repo
, h1
),
3674 repo_get_commit_tree(opt
->repo
, h2
),
3675 repo_get_commit_tree(opt
->repo
,
3676 merged_merge_bases
),
3678 strbuf_release(&merge_base_abbrev
);
3679 opt
->ancestor
= NULL
; /* avoid accidental re-use of opt->ancestor */
3685 if (opt
->priv
->call_depth
) {
3686 *result
= make_virtual_commit(opt
->repo
, result_tree
,
3688 commit_list_insert(h1
, &(*result
)->parents
);
3689 commit_list_insert(h2
, &(*result
)->parents
->next
);
3694 static int merge_start(struct merge_options
*opt
, struct tree
*head
)
3696 struct strbuf sb
= STRBUF_INIT
;
3698 /* Sanity checks on opt */
3701 assert(opt
->branch1
&& opt
->branch2
);
3703 assert(opt
->detect_renames
>= -1 &&
3704 opt
->detect_renames
<= DIFF_DETECT_COPY
);
3705 assert(opt
->detect_directory_renames
>= MERGE_DIRECTORY_RENAMES_NONE
&&
3706 opt
->detect_directory_renames
<= MERGE_DIRECTORY_RENAMES_TRUE
);
3707 assert(opt
->rename_limit
>= -1);
3708 assert(opt
->rename_score
>= 0 && opt
->rename_score
<= MAX_SCORE
);
3709 assert(opt
->show_rename_progress
>= 0 && opt
->show_rename_progress
<= 1);
3711 assert(opt
->xdl_opts
>= 0);
3712 assert(opt
->recursive_variant
>= MERGE_VARIANT_NORMAL
&&
3713 opt
->recursive_variant
<= MERGE_VARIANT_THEIRS
);
3715 assert(opt
->verbosity
>= 0 && opt
->verbosity
<= 5);
3716 assert(opt
->buffer_output
<= 2);
3717 assert(opt
->obuf
.len
== 0);
3719 assert(opt
->priv
== NULL
);
3721 /* Not supported; option specific to merge-ort */
3722 assert(!opt
->record_conflict_msgs_as_headers
);
3723 assert(!opt
->msg_header_prefix
);
3725 /* Sanity check on repo state; index must match head */
3726 if (repo_index_has_changes(opt
->repo
, head
, &sb
)) {
3727 err(opt
, _("Your local changes to the following files would be overwritten by merge:\n %s"),
3729 strbuf_release(&sb
);
3733 CALLOC_ARRAY(opt
->priv
, 1);
3734 string_list_init_dup(&opt
->priv
->df_conflict_file_set
);
3738 static void merge_finalize(struct merge_options
*opt
)
3741 if (!opt
->priv
->call_depth
&& opt
->buffer_output
< 2)
3742 strbuf_release(&opt
->obuf
);
3744 diff_warn_rename_limit("merge.renamelimit",
3745 opt
->priv
->needed_rename_limit
, 0);
3746 FREE_AND_NULL(opt
->priv
);
3749 int merge_trees(struct merge_options
*opt
,
3752 struct tree
*merge_base
)
3755 struct tree
*ignored
;
3757 assert(opt
->ancestor
!= NULL
);
3759 if (merge_start(opt
, head
))
3761 clean
= merge_trees_internal(opt
, head
, merge
, merge_base
, &ignored
);
3762 merge_finalize(opt
);
3767 int merge_recursive(struct merge_options
*opt
,
3770 struct commit_list
*merge_bases
,
3771 struct commit
**result
)
3775 assert(opt
->ancestor
== NULL
||
3776 !strcmp(opt
->ancestor
, "constructed merge base"));
3778 prepare_repo_settings(opt
->repo
);
3779 opt
->repo
->settings
.command_requires_full_index
= 1;
3781 if (merge_start(opt
, repo_get_commit_tree(opt
->repo
, h1
)))
3783 clean
= merge_recursive_internal(opt
, h1
, h2
, merge_bases
, result
);
3784 merge_finalize(opt
);
3789 static struct commit
*get_ref(struct repository
*repo
,
3790 const struct object_id
*oid
,
3793 struct object
*object
;
3795 object
= deref_tag(repo
, parse_object(repo
, oid
),
3796 name
, strlen(name
));
3799 if (object
->type
== OBJ_TREE
)
3800 return make_virtual_commit(repo
, (struct tree
*)object
, name
);
3801 if (object
->type
!= OBJ_COMMIT
)
3803 if (parse_commit((struct commit
*)object
))
3805 return (struct commit
*)object
;
3808 int merge_recursive_generic(struct merge_options
*opt
,
3809 const struct object_id
*head
,
3810 const struct object_id
*merge
,
3811 int num_merge_bases
,
3812 const struct object_id
**merge_bases
,
3813 struct commit
**result
)
3816 struct lock_file lock
= LOCK_INIT
;
3817 struct commit
*head_commit
= get_ref(opt
->repo
, head
, opt
->branch1
);
3818 struct commit
*next_commit
= get_ref(opt
->repo
, merge
, opt
->branch2
);
3819 struct commit_list
*ca
= NULL
;
3823 for (i
= 0; i
< num_merge_bases
; ++i
) {
3824 struct commit
*base
;
3825 if (!(base
= get_ref(opt
->repo
, merge_bases
[i
],
3826 oid_to_hex(merge_bases
[i
]))))
3827 return err(opt
, _("Could not parse object '%s'"),
3828 oid_to_hex(merge_bases
[i
]));
3829 commit_list_insert(base
, &ca
);
3831 if (num_merge_bases
== 1)
3832 opt
->ancestor
= "constructed merge base";
3835 repo_hold_locked_index(opt
->repo
, &lock
, LOCK_DIE_ON_ERROR
);
3836 clean
= merge_recursive(opt
, head_commit
, next_commit
, ca
,
3839 rollback_lock_file(&lock
);
3843 if (write_locked_index(opt
->repo
->index
, &lock
,
3844 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
3845 return err(opt
, _("Unable to write index."));
3847 return clean
? 0 : 1;
3850 static void merge_recursive_config(struct merge_options
*opt
)
3853 int renormalize
= 0;
3854 git_config_get_int("merge.verbosity", &opt
->verbosity
);
3855 git_config_get_int("diff.renamelimit", &opt
->rename_limit
);
3856 git_config_get_int("merge.renamelimit", &opt
->rename_limit
);
3857 git_config_get_bool("merge.renormalize", &renormalize
);
3858 opt
->renormalize
= renormalize
;
3859 if (!git_config_get_string("diff.renames", &value
)) {
3860 opt
->detect_renames
= git_config_rename("diff.renames", value
);
3863 if (!git_config_get_string("merge.renames", &value
)) {
3864 opt
->detect_renames
= git_config_rename("merge.renames", value
);
3867 if (!git_config_get_string("merge.directoryrenames", &value
)) {
3868 int boolval
= git_parse_maybe_bool(value
);
3870 opt
->detect_directory_renames
= boolval
?
3871 MERGE_DIRECTORY_RENAMES_TRUE
:
3872 MERGE_DIRECTORY_RENAMES_NONE
;
3873 } else if (!strcasecmp(value
, "conflict")) {
3874 opt
->detect_directory_renames
=
3875 MERGE_DIRECTORY_RENAMES_CONFLICT
;
3876 } /* avoid erroring on values from future versions of git */
3879 git_config(git_xmerge_config
, NULL
);
3882 void init_merge_options(struct merge_options
*opt
,
3883 struct repository
*repo
)
3885 const char *merge_verbosity
;
3886 memset(opt
, 0, sizeof(struct merge_options
));
3890 opt
->detect_renames
= -1;
3891 opt
->detect_directory_renames
= MERGE_DIRECTORY_RENAMES_CONFLICT
;
3892 opt
->rename_limit
= -1;
3895 opt
->buffer_output
= 1;
3896 strbuf_init(&opt
->obuf
, 0);
3898 opt
->renormalize
= 0;
3900 merge_recursive_config(opt
);
3901 merge_verbosity
= getenv("GIT_MERGE_VERBOSITY");
3902 if (merge_verbosity
)
3903 opt
->verbosity
= strtol(merge_verbosity
, NULL
, 10);
3904 if (opt
->verbosity
>= 5)
3905 opt
->buffer_output
= 0;
3908 int parse_merge_opt(struct merge_options
*opt
, const char *s
)
3914 if (!strcmp(s
, "ours"))
3915 opt
->recursive_variant
= MERGE_VARIANT_OURS
;
3916 else if (!strcmp(s
, "theirs"))
3917 opt
->recursive_variant
= MERGE_VARIANT_THEIRS
;
3918 else if (!strcmp(s
, "subtree"))
3919 opt
->subtree_shift
= "";
3920 else if (skip_prefix(s
, "subtree=", &arg
))
3921 opt
->subtree_shift
= arg
;
3922 else if (!strcmp(s
, "patience"))
3923 opt
->xdl_opts
= DIFF_WITH_ALG(opt
, PATIENCE_DIFF
);
3924 else if (!strcmp(s
, "histogram"))
3925 opt
->xdl_opts
= DIFF_WITH_ALG(opt
, HISTOGRAM_DIFF
);
3926 else if (skip_prefix(s
, "diff-algorithm=", &arg
)) {
3927 long value
= parse_algorithm_value(arg
);
3930 /* clear out previous settings */
3931 DIFF_XDL_CLR(opt
, NEED_MINIMAL
);
3932 opt
->xdl_opts
&= ~XDF_DIFF_ALGORITHM_MASK
;
3933 opt
->xdl_opts
|= value
;
3935 else if (!strcmp(s
, "ignore-space-change"))
3936 DIFF_XDL_SET(opt
, IGNORE_WHITESPACE_CHANGE
);
3937 else if (!strcmp(s
, "ignore-all-space"))
3938 DIFF_XDL_SET(opt
, IGNORE_WHITESPACE
);
3939 else if (!strcmp(s
, "ignore-space-at-eol"))
3940 DIFF_XDL_SET(opt
, IGNORE_WHITESPACE_AT_EOL
);
3941 else if (!strcmp(s
, "ignore-cr-at-eol"))
3942 DIFF_XDL_SET(opt
, IGNORE_CR_AT_EOL
);
3943 else if (!strcmp(s
, "renormalize"))
3944 opt
->renormalize
= 1;
3945 else if (!strcmp(s
, "no-renormalize"))
3946 opt
->renormalize
= 0;
3947 else if (!strcmp(s
, "no-renames"))
3948 opt
->detect_renames
= 0;
3949 else if (!strcmp(s
, "find-renames")) {
3950 opt
->detect_renames
= 1;
3951 opt
->rename_score
= 0;
3953 else if (skip_prefix(s
, "find-renames=", &arg
) ||
3954 skip_prefix(s
, "rename-threshold=", &arg
)) {
3955 if ((opt
->rename_score
= parse_rename_score(&arg
)) == -1 || *arg
!= 0)
3957 opt
->detect_renames
= 1;
3960 * Please update $__git_merge_strategy_options in
3961 * git-completion.bash when you add new options