2 * Recursive Merge algorithm stolen from git-merge-recursive.py by
4 * The thieves were Alex Riesen and Johannes Schindelin, in June/July 2006
6 #include "git-compat-util.h"
7 #include "merge-recursive.h"
13 #include "cache-tree.h"
15 #include "commit-reach.h"
20 #include "environment.h"
25 #include "match-trees.h"
26 #include "name-hash.h"
27 #include "object-file.h"
28 #include "object-name.h"
29 #include "object-store.h"
31 #include "repository.h"
33 #include "sparse-index.h"
34 #include "string-list.h"
35 #include "submodule-config.h"
36 #include "submodule.h"
39 #include "tree-walk.h"
40 #include "unpack-trees.h"
42 #include "xdiff-interface.h"
44 struct merge_options_internal
{
46 int needed_rename_limit
;
47 struct hashmap current_file_dir_set
;
48 struct string_list df_conflict_file_set
;
49 struct unpack_trees_options unpack_opts
;
50 struct index_state orig_index
;
53 struct path_hashmap_entry
{
54 struct hashmap_entry e
;
55 char path
[FLEX_ARRAY
];
58 static int path_hashmap_cmp(const void *cmp_data UNUSED
,
59 const struct hashmap_entry
*eptr
,
60 const struct hashmap_entry
*entry_or_key
,
63 const struct path_hashmap_entry
*a
, *b
;
64 const char *key
= keydata
;
66 a
= container_of(eptr
, const struct path_hashmap_entry
, e
);
67 b
= container_of(entry_or_key
, const struct path_hashmap_entry
, e
);
69 return fspathcmp(a
->path
, key
? key
: b
->path
);
73 * For dir_rename_entry, directory names are stored as a full path from the
74 * toplevel of the repository and do not include a trailing '/'. Also:
76 * dir: original name of directory being renamed
77 * non_unique_new_dir: if true, could not determine new_dir
78 * new_dir: final name of directory being renamed
79 * possible_new_dirs: temporary used to help determine new_dir; see comments
80 * in get_directory_renames() for details
82 struct dir_rename_entry
{
83 struct hashmap_entry ent
;
85 unsigned non_unique_new_dir
:1;
86 struct strbuf new_dir
;
87 struct string_list possible_new_dirs
;
90 static struct dir_rename_entry
*dir_rename_find_entry(struct hashmap
*hashmap
,
93 struct dir_rename_entry key
;
97 hashmap_entry_init(&key
.ent
, strhash(dir
));
99 return hashmap_get_entry(hashmap
, &key
, ent
, NULL
);
102 static int dir_rename_cmp(const void *cmp_data UNUSED
,
103 const struct hashmap_entry
*eptr
,
104 const struct hashmap_entry
*entry_or_key
,
105 const void *keydata UNUSED
)
107 const struct dir_rename_entry
*e1
, *e2
;
109 e1
= container_of(eptr
, const struct dir_rename_entry
, ent
);
110 e2
= container_of(entry_or_key
, const struct dir_rename_entry
, ent
);
112 return strcmp(e1
->dir
, e2
->dir
);
115 static void dir_rename_init(struct hashmap
*map
)
117 hashmap_init(map
, dir_rename_cmp
, NULL
, 0);
120 static void dir_rename_entry_init(struct dir_rename_entry
*entry
,
123 hashmap_entry_init(&entry
->ent
, strhash(directory
));
124 entry
->dir
= directory
;
125 entry
->non_unique_new_dir
= 0;
126 strbuf_init(&entry
->new_dir
, 0);
127 string_list_init_nodup(&entry
->possible_new_dirs
);
130 struct collision_entry
{
131 struct hashmap_entry ent
;
133 struct string_list source_files
;
134 unsigned reported_already
:1;
137 static struct collision_entry
*collision_find_entry(struct hashmap
*hashmap
,
140 struct collision_entry key
;
142 hashmap_entry_init(&key
.ent
, strhash(target_file
));
143 key
.target_file
= target_file
;
144 return hashmap_get_entry(hashmap
, &key
, ent
, NULL
);
147 static int collision_cmp(const void *cmp_data UNUSED
,
148 const struct hashmap_entry
*eptr
,
149 const struct hashmap_entry
*entry_or_key
,
150 const void *keydata UNUSED
)
152 const struct collision_entry
*e1
, *e2
;
154 e1
= container_of(eptr
, const struct collision_entry
, ent
);
155 e2
= container_of(entry_or_key
, const struct collision_entry
, ent
);
157 return strcmp(e1
->target_file
, e2
->target_file
);
160 static void collision_init(struct hashmap
*map
)
162 hashmap_init(map
, collision_cmp
, NULL
, 0);
165 static void flush_output(struct merge_options
*opt
)
167 if (opt
->buffer_output
< 2 && opt
->obuf
.len
) {
168 fputs(opt
->obuf
.buf
, stdout
);
169 strbuf_reset(&opt
->obuf
);
173 __attribute__((format (printf
, 2, 3)))
174 static int err(struct merge_options
*opt
, const char *err
, ...)
178 if (opt
->buffer_output
< 2)
181 strbuf_complete(&opt
->obuf
, '\n');
182 strbuf_addstr(&opt
->obuf
, "error: ");
184 va_start(params
, err
);
185 strbuf_vaddf(&opt
->obuf
, err
, params
);
187 if (opt
->buffer_output
> 1)
188 strbuf_addch(&opt
->obuf
, '\n');
190 error("%s", opt
->obuf
.buf
);
191 strbuf_reset(&opt
->obuf
);
197 static struct tree
*shift_tree_object(struct repository
*repo
,
198 struct tree
*one
, struct tree
*two
,
199 const char *subtree_shift
)
201 struct object_id shifted
;
203 if (!*subtree_shift
) {
204 shift_tree(repo
, &one
->object
.oid
, &two
->object
.oid
, &shifted
, 0);
206 shift_tree_by(repo
, &one
->object
.oid
, &two
->object
.oid
, &shifted
,
209 if (oideq(&two
->object
.oid
, &shifted
))
211 return lookup_tree(repo
, &shifted
);
214 static inline void set_commit_tree(struct commit
*c
, struct tree
*t
)
219 static struct commit
*make_virtual_commit(struct repository
*repo
,
223 struct commit
*commit
= alloc_commit_node(repo
);
225 set_merge_remote_desc(commit
, comment
, (struct object
*)commit
);
226 set_commit_tree(commit
, tree
);
227 commit
->object
.parsed
= 1;
236 RENAME_ONE_FILE_TO_ONE
,
237 RENAME_ONE_FILE_TO_TWO
,
238 RENAME_TWO_FILES_TO_ONE
242 * Since we want to write the index eventually, we cannot reuse the index
243 * for these (temporary) data.
246 struct diff_filespec stages
[4]; /* mostly for oid & mode; maybe path */
247 struct rename_conflict_info
*rename_conflict_info
;
248 unsigned processed
:1;
252 unsigned processed
:1;
253 struct diff_filepair
*pair
;
254 const char *branch
; /* branch that the rename occurred on */
256 * If directory rename detection affected this rename, what was its
257 * original type ('A' or 'R') and it's original destination before
258 * the directory rename (otherwise, '\0' and NULL for these two vars).
260 char dir_rename_original_type
;
261 char *dir_rename_original_dest
;
263 * Purpose of src_entry and dst_entry:
265 * If 'before' is renamed to 'after' then src_entry will contain
266 * the versions of 'before' from the merge_base, HEAD, and MERGE in
267 * stages 1, 2, and 3; dst_entry will contain the respective
268 * versions of 'after' in corresponding locations. Thus, we have a
269 * total of six modes and oids, though some will be null. (Stage 0
270 * is ignored; we're interested in handling conflicts.)
272 * Since we don't turn on break-rewrites by default, neither
273 * src_entry nor dst_entry can have all three of their stages have
274 * non-null oids, meaning at most four of the six will be non-null.
275 * Also, since this is a rename, both src_entry and dst_entry will
276 * have at least one non-null oid, meaning at least two will be
277 * non-null. Of the six oids, a typical rename will have three be
278 * non-null. Only two implies a rename/delete, and four implies a
281 struct stage_data
*src_entry
;
282 struct stage_data
*dst_entry
;
285 struct rename_conflict_info
{
286 enum rename_type rename_type
;
291 static inline void setup_rename_conflict_info(enum rename_type rename_type
,
292 struct merge_options
*opt
,
296 struct rename_conflict_info
*ci
;
299 * When we have two renames involved, it's easiest to get the
300 * correct things into stage 2 and 3, and to make sure that the
301 * content merge puts HEAD before the other branch if we just
302 * ensure that branch1 == opt->branch1. So, simply flip arguments
303 * around if we don't have that.
305 if (ren2
&& ren1
->branch
!= opt
->branch1
) {
306 setup_rename_conflict_info(rename_type
, opt
, ren2
, ren1
);
311 ci
->rename_type
= rename_type
;
315 ci
->ren1
->dst_entry
->processed
= 0;
316 ci
->ren1
->dst_entry
->rename_conflict_info
= ci
;
318 ci
->ren2
->dst_entry
->rename_conflict_info
= ci
;
322 static int show(struct merge_options
*opt
, int v
)
324 return (!opt
->priv
->call_depth
&& opt
->verbosity
>= v
) ||
328 __attribute__((format (printf
, 3, 4)))
329 static void output(struct merge_options
*opt
, int v
, const char *fmt
, ...)
336 strbuf_addchars(&opt
->obuf
, ' ', opt
->priv
->call_depth
* 2);
339 strbuf_vaddf(&opt
->obuf
, fmt
, ap
);
342 strbuf_addch(&opt
->obuf
, '\n');
343 if (!opt
->buffer_output
)
347 static void repo_output_commit_title(struct merge_options
*opt
,
348 struct repository
*repo
,
349 struct commit
*commit
)
351 struct merge_remote_desc
*desc
;
353 strbuf_addchars(&opt
->obuf
, ' ', opt
->priv
->call_depth
* 2);
354 desc
= merge_remote_util(commit
);
356 strbuf_addf(&opt
->obuf
, "virtual %s\n", desc
->name
);
358 strbuf_repo_add_unique_abbrev(&opt
->obuf
, repo
,
361 strbuf_addch(&opt
->obuf
, ' ');
362 if (repo_parse_commit(repo
, commit
) != 0)
363 strbuf_addstr(&opt
->obuf
, _("(bad commit)\n"));
366 const char *msg
= repo_get_commit_buffer(repo
, commit
, NULL
);
367 int len
= find_commit_subject(msg
, &title
);
369 strbuf_addf(&opt
->obuf
, "%.*s\n", len
, title
);
370 repo_unuse_commit_buffer(repo
, commit
, msg
);
376 static void output_commit_title(struct merge_options
*opt
, struct commit
*commit
)
378 repo_output_commit_title(opt
, the_repository
, commit
);
381 static int add_cacheinfo(struct merge_options
*opt
,
382 const struct diff_filespec
*blob
,
383 const char *path
, int stage
, int refresh
, int options
)
385 struct index_state
*istate
= opt
->repo
->index
;
386 struct cache_entry
*ce
;
389 ce
= make_cache_entry(istate
, blob
->mode
, &blob
->oid
, path
, stage
, 0);
391 return err(opt
, _("add_cacheinfo failed for path '%s'; merge aborting."), path
);
393 ret
= add_index_entry(istate
, ce
, options
);
395 struct cache_entry
*nce
;
397 nce
= refresh_cache_entry(istate
, ce
,
398 CE_MATCH_REFRESH
| CE_MATCH_IGNORE_MISSING
);
400 return err(opt
, _("add_cacheinfo failed to refresh for path '%s'; merge aborting."), path
);
402 ret
= add_index_entry(istate
, nce
, options
);
407 static inline int merge_detect_rename(struct merge_options
*opt
)
409 return (opt
->detect_renames
>= 0) ? opt
->detect_renames
: 1;
412 static void init_tree_desc_from_tree(struct tree_desc
*desc
, struct tree
*tree
)
415 init_tree_desc(desc
, tree
->buffer
, tree
->size
);
418 static int unpack_trees_start(struct merge_options
*opt
,
424 struct tree_desc t
[3];
425 struct index_state tmp_index
= INDEX_STATE_INIT(opt
->repo
);
427 memset(&opt
->priv
->unpack_opts
, 0, sizeof(opt
->priv
->unpack_opts
));
428 if (opt
->priv
->call_depth
)
429 opt
->priv
->unpack_opts
.index_only
= 1;
431 opt
->priv
->unpack_opts
.update
= 1;
432 /* FIXME: should only do this if !overwrite_ignore */
433 opt
->priv
->unpack_opts
.preserve_ignored
= 0;
435 opt
->priv
->unpack_opts
.merge
= 1;
436 opt
->priv
->unpack_opts
.head_idx
= 2;
437 opt
->priv
->unpack_opts
.fn
= threeway_merge
;
438 opt
->priv
->unpack_opts
.src_index
= opt
->repo
->index
;
439 opt
->priv
->unpack_opts
.dst_index
= &tmp_index
;
440 opt
->priv
->unpack_opts
.aggressive
= !merge_detect_rename(opt
);
441 setup_unpack_trees_porcelain(&opt
->priv
->unpack_opts
, "merge");
443 init_tree_desc_from_tree(t
+0, common
);
444 init_tree_desc_from_tree(t
+1, head
);
445 init_tree_desc_from_tree(t
+2, merge
);
447 rc
= unpack_trees(3, t
, &opt
->priv
->unpack_opts
);
448 cache_tree_free(&opt
->repo
->index
->cache_tree
);
451 * Update opt->repo->index to match the new results, AFTER saving a
452 * copy in opt->priv->orig_index. Update src_index to point to the
453 * saved copy. (verify_uptodate() checks src_index, and the original
454 * index is the one that had the necessary modification timestamps.)
456 opt
->priv
->orig_index
= *opt
->repo
->index
;
457 *opt
->repo
->index
= tmp_index
;
458 opt
->priv
->unpack_opts
.src_index
= &opt
->priv
->orig_index
;
463 static void unpack_trees_finish(struct merge_options
*opt
)
465 discard_index(&opt
->priv
->orig_index
);
466 clear_unpack_trees_porcelain(&opt
->priv
->unpack_opts
);
469 static int save_files_dirs(const struct object_id
*oid UNUSED
,
470 struct strbuf
*base
, const char *path
,
471 unsigned int mode
, void *context
)
473 struct path_hashmap_entry
*entry
;
474 int baselen
= base
->len
;
475 struct merge_options
*opt
= context
;
477 strbuf_addstr(base
, path
);
479 FLEX_ALLOC_MEM(entry
, path
, base
->buf
, base
->len
);
480 hashmap_entry_init(&entry
->e
, fspathhash(entry
->path
));
481 hashmap_add(&opt
->priv
->current_file_dir_set
, &entry
->e
);
483 strbuf_setlen(base
, baselen
);
484 return (S_ISDIR(mode
) ? READ_TREE_RECURSIVE
: 0);
487 static void get_files_dirs(struct merge_options
*opt
, struct tree
*tree
)
489 struct pathspec match_all
;
490 memset(&match_all
, 0, sizeof(match_all
));
491 read_tree(opt
->repo
, tree
,
492 &match_all
, save_files_dirs
, opt
);
495 static int get_tree_entry_if_blob(struct repository
*r
,
496 const struct object_id
*tree
,
498 struct diff_filespec
*dfs
)
502 ret
= get_tree_entry(r
, tree
, path
, &dfs
->oid
, &dfs
->mode
);
503 if (S_ISDIR(dfs
->mode
)) {
504 oidcpy(&dfs
->oid
, null_oid());
511 * Returns an index_entry instance which doesn't have to correspond to
512 * a real cache entry in Git's index.
514 static struct stage_data
*insert_stage_data(struct repository
*r
,
516 struct tree
*o
, struct tree
*a
, struct tree
*b
,
517 struct string_list
*entries
)
519 struct string_list_item
*item
;
520 struct stage_data
*e
= xcalloc(1, sizeof(struct stage_data
));
521 get_tree_entry_if_blob(r
, &o
->object
.oid
, path
, &e
->stages
[1]);
522 get_tree_entry_if_blob(r
, &a
->object
.oid
, path
, &e
->stages
[2]);
523 get_tree_entry_if_blob(r
, &b
->object
.oid
, path
, &e
->stages
[3]);
524 item
= string_list_insert(entries
, path
);
530 * Create a dictionary mapping file names to stage_data objects. The
531 * dictionary contains one entry for every path with a non-zero stage entry.
533 static struct string_list
*get_unmerged(struct index_state
*istate
)
535 struct string_list
*unmerged
= xmalloc(sizeof(struct string_list
));
538 string_list_init_dup(unmerged
);
540 /* TODO: audit for interaction with sparse-index. */
541 ensure_full_index(istate
);
542 for (i
= 0; i
< istate
->cache_nr
; i
++) {
543 struct string_list_item
*item
;
544 struct stage_data
*e
;
545 const struct cache_entry
*ce
= istate
->cache
[i
];
549 item
= string_list_lookup(unmerged
, ce
->name
);
551 item
= string_list_insert(unmerged
, ce
->name
);
552 item
->util
= xcalloc(1, sizeof(struct stage_data
));
555 e
->stages
[ce_stage(ce
)].mode
= ce
->ce_mode
;
556 oidcpy(&e
->stages
[ce_stage(ce
)].oid
, &ce
->oid
);
562 static int string_list_df_name_compare(const char *one
, const char *two
)
564 int onelen
= strlen(one
);
565 int twolen
= strlen(two
);
567 * Here we only care that entries for D/F conflicts are
568 * adjacent, in particular with the file of the D/F conflict
569 * appearing before files below the corresponding directory.
570 * The order of the rest of the list is irrelevant for us.
572 * To achieve this, we sort with df_name_compare and provide
573 * the mode S_IFDIR so that D/F conflicts will sort correctly.
574 * We use the mode S_IFDIR for everything else for simplicity,
575 * since in other cases any changes in their order due to
576 * sorting cause no problems for us.
578 int cmp
= df_name_compare(one
, onelen
, S_IFDIR
,
579 two
, twolen
, S_IFDIR
);
581 * Now that 'foo' and 'foo/bar' compare equal, we have to make sure
582 * that 'foo' comes before 'foo/bar'.
586 return onelen
- twolen
;
589 static void record_df_conflict_files(struct merge_options
*opt
,
590 struct string_list
*entries
)
592 /* If there is a D/F conflict and the file for such a conflict
593 * currently exists in the working tree, we want to allow it to be
594 * removed to make room for the corresponding directory if needed.
595 * The files underneath the directories of such D/F conflicts will
596 * be processed before the corresponding file involved in the D/F
597 * conflict. If the D/F directory ends up being removed by the
598 * merge, then we won't have to touch the D/F file. If the D/F
599 * directory needs to be written to the working copy, then the D/F
600 * file will simply be removed (in make_room_for_path()) to make
601 * room for the necessary paths. Note that if both the directory
602 * and the file need to be present, then the D/F file will be
603 * reinstated with a new unique name at the time it is processed.
605 struct string_list df_sorted_entries
= STRING_LIST_INIT_NODUP
;
606 const char *last_file
= NULL
;
611 * If we're merging merge-bases, we don't want to bother with
612 * any working directory changes.
614 if (opt
->priv
->call_depth
)
617 /* Ensure D/F conflicts are adjacent in the entries list. */
618 for (i
= 0; i
< entries
->nr
; i
++) {
619 struct string_list_item
*next
= &entries
->items
[i
];
620 string_list_append(&df_sorted_entries
, next
->string
)->util
=
623 df_sorted_entries
.cmp
= string_list_df_name_compare
;
624 string_list_sort(&df_sorted_entries
);
626 string_list_clear(&opt
->priv
->df_conflict_file_set
, 1);
627 for (i
= 0; i
< df_sorted_entries
.nr
; i
++) {
628 const char *path
= df_sorted_entries
.items
[i
].string
;
629 int len
= strlen(path
);
630 struct stage_data
*e
= df_sorted_entries
.items
[i
].util
;
633 * Check if last_file & path correspond to a D/F conflict;
634 * i.e. whether path is last_file+'/'+<something>.
635 * If so, record that it's okay to remove last_file to make
636 * room for path and friends if needed.
640 memcmp(path
, last_file
, last_len
) == 0 &&
641 path
[last_len
] == '/') {
642 string_list_insert(&opt
->priv
->df_conflict_file_set
, last_file
);
646 * Determine whether path could exist as a file in the
647 * working directory as a possible D/F conflict. This
648 * will only occur when it exists in stage 2 as a
651 if (S_ISREG(e
->stages
[2].mode
) || S_ISLNK(e
->stages
[2].mode
)) {
658 string_list_clear(&df_sorted_entries
, 0);
661 static int update_stages(struct merge_options
*opt
, const char *path
,
662 const struct diff_filespec
*o
,
663 const struct diff_filespec
*a
,
664 const struct diff_filespec
*b
)
668 * NOTE: It is usually a bad idea to call update_stages on a path
669 * before calling update_file on that same path, since it can
670 * sometimes lead to spurious "refusing to lose untracked file..."
671 * messages from update_file (via make_room_for path via
672 * would_lose_untracked). Instead, reverse the order of the calls
673 * (executing update_file first and then update_stages).
676 int options
= ADD_CACHE_OK_TO_ADD
| ADD_CACHE_SKIP_DFCHECK
;
678 if (remove_file_from_index(opt
->repo
->index
, path
))
681 if (add_cacheinfo(opt
, o
, path
, 1, 0, options
))
684 if (add_cacheinfo(opt
, a
, path
, 2, 0, options
))
687 if (add_cacheinfo(opt
, b
, path
, 3, 0, options
))
692 static void update_entry(struct stage_data
*entry
,
693 struct diff_filespec
*o
,
694 struct diff_filespec
*a
,
695 struct diff_filespec
*b
)
697 entry
->processed
= 0;
698 entry
->stages
[1].mode
= o
->mode
;
699 entry
->stages
[2].mode
= a
->mode
;
700 entry
->stages
[3].mode
= b
->mode
;
701 oidcpy(&entry
->stages
[1].oid
, &o
->oid
);
702 oidcpy(&entry
->stages
[2].oid
, &a
->oid
);
703 oidcpy(&entry
->stages
[3].oid
, &b
->oid
);
706 static int remove_file(struct merge_options
*opt
, int clean
,
707 const char *path
, int no_wd
)
709 int update_cache
= opt
->priv
->call_depth
|| clean
;
710 int update_working_directory
= !opt
->priv
->call_depth
&& !no_wd
;
713 if (remove_file_from_index(opt
->repo
->index
, path
))
716 if (update_working_directory
) {
718 struct cache_entry
*ce
;
719 ce
= index_file_exists(opt
->repo
->index
, path
, strlen(path
),
721 if (ce
&& ce_stage(ce
) == 0 && strcmp(path
, ce
->name
))
724 if (remove_path(path
))
730 /* add a string to a strbuf, but converting "/" to "_" */
731 static void add_flattened_path(struct strbuf
*out
, const char *s
)
734 strbuf_addstr(out
, s
);
735 for (; i
< out
->len
; i
++)
736 if (out
->buf
[i
] == '/')
740 static char *unique_path(struct merge_options
*opt
,
744 struct path_hashmap_entry
*entry
;
745 struct strbuf newpath
= STRBUF_INIT
;
749 strbuf_addf(&newpath
, "%s~", path
);
750 add_flattened_path(&newpath
, branch
);
752 base_len
= newpath
.len
;
753 while (hashmap_get_from_hash(&opt
->priv
->current_file_dir_set
,
754 fspathhash(newpath
.buf
), newpath
.buf
) ||
755 (!opt
->priv
->call_depth
&& file_exists(newpath
.buf
))) {
756 strbuf_setlen(&newpath
, base_len
);
757 strbuf_addf(&newpath
, "_%d", suffix
++);
760 FLEX_ALLOC_MEM(entry
, path
, newpath
.buf
, newpath
.len
);
761 hashmap_entry_init(&entry
->e
, fspathhash(entry
->path
));
762 hashmap_add(&opt
->priv
->current_file_dir_set
, &entry
->e
);
763 return strbuf_detach(&newpath
, NULL
);
767 * Check whether a directory in the index is in the way of an incoming
768 * file. Return 1 if so. If check_working_copy is non-zero, also
769 * check the working directory. If empty_ok is non-zero, also return
770 * 0 in the case where the working-tree dir exists but is empty.
772 static int dir_in_way(struct index_state
*istate
, const char *path
,
773 int check_working_copy
, int empty_ok
)
776 struct strbuf dirpath
= STRBUF_INIT
;
779 strbuf_addstr(&dirpath
, path
);
780 strbuf_addch(&dirpath
, '/');
782 pos
= index_name_pos(istate
, dirpath
.buf
, dirpath
.len
);
786 if (pos
< istate
->cache_nr
&&
787 !strncmp(dirpath
.buf
, istate
->cache
[pos
]->name
, dirpath
.len
)) {
788 strbuf_release(&dirpath
);
792 strbuf_release(&dirpath
);
793 return check_working_copy
&& !lstat(path
, &st
) && S_ISDIR(st
.st_mode
) &&
794 !(empty_ok
&& is_empty_dir(path
)) &&
795 !has_symlink_leading_path(path
, strlen(path
));
799 * Returns whether path was tracked in the index before the merge started,
800 * and its oid and mode match the specified values
802 static int was_tracked_and_matches(struct merge_options
*opt
, const char *path
,
803 const struct diff_filespec
*blob
)
805 int pos
= index_name_pos(&opt
->priv
->orig_index
, path
, strlen(path
));
806 struct cache_entry
*ce
;
809 /* we were not tracking this path before the merge */
812 /* See if the file we were tracking before matches */
813 ce
= opt
->priv
->orig_index
.cache
[pos
];
814 return (oideq(&ce
->oid
, &blob
->oid
) && ce
->ce_mode
== blob
->mode
);
818 * Returns whether path was tracked in the index before the merge started
820 static int was_tracked(struct merge_options
*opt
, const char *path
)
822 int pos
= index_name_pos(&opt
->priv
->orig_index
, path
, strlen(path
));
825 /* we were tracking this path before the merge */
831 static int would_lose_untracked(struct merge_options
*opt
, const char *path
)
833 struct index_state
*istate
= opt
->repo
->index
;
836 * This may look like it can be simplified to:
837 * return !was_tracked(opt, path) && file_exists(path)
838 * but it can't. This function needs to know whether path was in
839 * the working tree due to EITHER having been tracked in the index
840 * before the merge OR having been put into the working copy and
841 * index by unpack_trees(). Due to that either-or requirement, we
842 * check the current index instead of the original one.
844 * Note that we do not need to worry about merge-recursive itself
845 * updating the index after unpack_trees() and before calling this
846 * function, because we strictly require all code paths in
847 * merge-recursive to update the working tree first and the index
848 * second. Doing otherwise would break
849 * update_file()/would_lose_untracked(); see every comment in this
850 * file which mentions "update_stages".
852 int pos
= index_name_pos(istate
, path
, strlen(path
));
856 while (pos
< istate
->cache_nr
&&
857 !strcmp(path
, istate
->cache
[pos
]->name
)) {
859 * If stage #0, it is definitely tracked.
860 * If it has stage #2 then it was tracked
861 * before this merge started. All other
862 * cases the path was not tracked.
864 switch (ce_stage(istate
->cache
[pos
])) {
871 return file_exists(path
);
874 static int was_dirty(struct merge_options
*opt
, const char *path
)
876 struct cache_entry
*ce
;
879 if (opt
->priv
->call_depth
|| !was_tracked(opt
, path
))
882 ce
= index_file_exists(opt
->priv
->unpack_opts
.src_index
,
883 path
, strlen(path
), ignore_case
);
884 dirty
= verify_uptodate(ce
, &opt
->priv
->unpack_opts
) != 0;
888 static int make_room_for_path(struct merge_options
*opt
, const char *path
)
891 const char *msg
= _("failed to create path '%s'%s");
893 /* Unlink any D/F conflict files that are in the way */
894 for (i
= 0; i
< opt
->priv
->df_conflict_file_set
.nr
; i
++) {
895 const char *df_path
= opt
->priv
->df_conflict_file_set
.items
[i
].string
;
896 size_t pathlen
= strlen(path
);
897 size_t df_pathlen
= strlen(df_path
);
898 if (df_pathlen
< pathlen
&&
899 path
[df_pathlen
] == '/' &&
900 strncmp(path
, df_path
, df_pathlen
) == 0) {
902 _("Removing %s to make room for subdirectory\n"),
905 unsorted_string_list_delete_item(&opt
->priv
->df_conflict_file_set
,
911 /* Make sure leading directories are created */
912 status
= safe_create_leading_directories_const(path
);
914 if (status
== SCLD_EXISTS
)
915 /* something else exists */
916 return err(opt
, msg
, path
, _(": perhaps a D/F conflict?"));
917 return err(opt
, msg
, path
, "");
921 * Do not unlink a file in the work tree if we are not
924 if (would_lose_untracked(opt
, path
))
925 return err(opt
, _("refusing to lose untracked file at '%s'"),
928 /* Successful unlink is good.. */
931 /* .. and so is no existing file */
934 /* .. but not some other error (who really cares what?) */
935 return err(opt
, msg
, path
, _(": perhaps a D/F conflict?"));
938 static int update_file_flags(struct merge_options
*opt
,
939 const struct diff_filespec
*contents
,
946 if (opt
->priv
->call_depth
)
950 enum object_type type
;
954 if (S_ISGITLINK(contents
->mode
)) {
956 * We may later decide to recursively descend into
957 * the submodule directory and update its index
958 * and/or work tree, but we do not do that now.
964 buf
= repo_read_object_file(the_repository
, &contents
->oid
,
967 ret
= err(opt
, _("cannot read object %s '%s'"),
968 oid_to_hex(&contents
->oid
), path
);
971 if (type
!= OBJ_BLOB
) {
972 ret
= err(opt
, _("blob expected for %s '%s'"),
973 oid_to_hex(&contents
->oid
), path
);
976 if (S_ISREG(contents
->mode
)) {
977 struct strbuf strbuf
= STRBUF_INIT
;
978 if (convert_to_working_tree(opt
->repo
->index
,
979 path
, buf
, size
, &strbuf
, NULL
)) {
982 buf
= strbuf_detach(&strbuf
, NULL
);
986 if (make_room_for_path(opt
, path
) < 0) {
990 if (S_ISREG(contents
->mode
) ||
991 (!has_symlinks
&& S_ISLNK(contents
->mode
))) {
993 int mode
= (contents
->mode
& 0100 ? 0777 : 0666);
995 fd
= open(path
, O_WRONLY
| O_TRUNC
| O_CREAT
, mode
);
997 ret
= err(opt
, _("failed to open '%s': %s"),
998 path
, strerror(errno
));
1001 write_in_full(fd
, buf
, size
);
1003 } else if (S_ISLNK(contents
->mode
)) {
1004 char *lnk
= xmemdupz(buf
, size
);
1005 safe_create_leading_directories_const(path
);
1007 if (symlink(lnk
, path
))
1008 ret
= err(opt
, _("failed to symlink '%s': %s"),
1009 path
, strerror(errno
));
1013 _("do not know what to do with %06o %s '%s'"),
1014 contents
->mode
, oid_to_hex(&contents
->oid
), path
);
1019 if (!ret
&& update_cache
) {
1020 int refresh
= (!opt
->priv
->call_depth
&&
1021 contents
->mode
!= S_IFGITLINK
);
1022 if (add_cacheinfo(opt
, contents
, path
, 0, refresh
,
1023 ADD_CACHE_OK_TO_ADD
))
1029 static int update_file(struct merge_options
*opt
,
1031 const struct diff_filespec
*contents
,
1034 return update_file_flags(opt
, contents
, path
,
1035 opt
->priv
->call_depth
|| clean
, !opt
->priv
->call_depth
);
1038 /* Low level file merging, update and removal */
1040 struct merge_file_info
{
1041 struct diff_filespec blob
; /* mostly use oid & mode; sometimes path */
1046 static int merge_3way(struct merge_options
*opt
,
1047 mmbuffer_t
*result_buf
,
1048 const struct diff_filespec
*o
,
1049 const struct diff_filespec
*a
,
1050 const struct diff_filespec
*b
,
1051 const char *branch1
,
1052 const char *branch2
,
1053 const int extra_marker_size
)
1055 mmfile_t orig
, src1
, src2
;
1056 struct ll_merge_options ll_opts
= {0};
1057 char *base
, *name1
, *name2
;
1058 enum ll_merge_result merge_status
;
1060 ll_opts
.renormalize
= opt
->renormalize
;
1061 ll_opts
.extra_marker_size
= extra_marker_size
;
1062 ll_opts
.xdl_opts
= opt
->xdl_opts
;
1064 if (opt
->priv
->call_depth
) {
1065 ll_opts
.virtual_ancestor
= 1;
1066 ll_opts
.variant
= 0;
1068 switch (opt
->recursive_variant
) {
1069 case MERGE_VARIANT_OURS
:
1070 ll_opts
.variant
= XDL_MERGE_FAVOR_OURS
;
1072 case MERGE_VARIANT_THEIRS
:
1073 ll_opts
.variant
= XDL_MERGE_FAVOR_THEIRS
;
1076 ll_opts
.variant
= 0;
1081 assert(a
->path
&& b
->path
&& o
->path
&& opt
->ancestor
);
1082 if (strcmp(a
->path
, b
->path
) || strcmp(a
->path
, o
->path
) != 0) {
1083 base
= mkpathdup("%s:%s", opt
->ancestor
, o
->path
);
1084 name1
= mkpathdup("%s:%s", branch1
, a
->path
);
1085 name2
= mkpathdup("%s:%s", branch2
, b
->path
);
1087 base
= mkpathdup("%s", opt
->ancestor
);
1088 name1
= mkpathdup("%s", branch1
);
1089 name2
= mkpathdup("%s", branch2
);
1092 read_mmblob(&orig
, &o
->oid
);
1093 read_mmblob(&src1
, &a
->oid
);
1094 read_mmblob(&src2
, &b
->oid
);
1097 * FIXME: Using a->path for normalization rules in ll_merge could be
1098 * wrong if we renamed from a->path to b->path. We should use the
1099 * target path for where the file will be written.
1101 merge_status
= ll_merge(result_buf
, a
->path
, &orig
, base
,
1102 &src1
, name1
, &src2
, name2
,
1103 opt
->repo
->index
, &ll_opts
);
1104 if (merge_status
== LL_MERGE_BINARY_CONFLICT
)
1105 warning("Cannot merge binary files: %s (%s vs. %s)",
1106 a
->path
, name1
, name2
);
1114 return merge_status
;
1117 static int find_first_merges(struct repository
*repo
,
1118 struct object_array
*result
, const char *path
,
1119 struct commit
*a
, struct commit
*b
)
1122 struct object_array merges
= OBJECT_ARRAY_INIT
;
1123 struct commit
*commit
;
1124 int contains_another
;
1126 char merged_revision
[GIT_MAX_HEXSZ
+ 2];
1127 const char *rev_args
[] = { "rev-list", "--merges", "--ancestry-path",
1128 "--all", merged_revision
, NULL
};
1129 struct rev_info revs
;
1130 struct setup_revision_opt rev_opts
;
1132 memset(result
, 0, sizeof(struct object_array
));
1133 memset(&rev_opts
, 0, sizeof(rev_opts
));
1135 /* get all revisions that merge commit a */
1136 xsnprintf(merged_revision
, sizeof(merged_revision
), "^%s",
1137 oid_to_hex(&a
->object
.oid
));
1138 repo_init_revisions(repo
, &revs
, NULL
);
1139 /* FIXME: can't handle linked worktrees in submodules yet */
1140 revs
.single_worktree
= path
!= NULL
;
1141 setup_revisions(ARRAY_SIZE(rev_args
)-1, rev_args
, &revs
, &rev_opts
);
1143 /* save all revisions from the above list that contain b */
1144 if (prepare_revision_walk(&revs
))
1145 die("revision walk setup failed");
1146 while ((commit
= get_revision(&revs
)) != NULL
) {
1147 struct object
*o
= &(commit
->object
);
1148 if (repo_in_merge_bases(repo
, b
, commit
))
1149 add_object_array(o
, NULL
, &merges
);
1151 reset_revision_walk();
1153 /* Now we've got all merges that contain a and b. Prune all
1154 * merges that contain another found merge and save them in
1157 for (i
= 0; i
< merges
.nr
; i
++) {
1158 struct commit
*m1
= (struct commit
*) merges
.objects
[i
].item
;
1160 contains_another
= 0;
1161 for (j
= 0; j
< merges
.nr
; j
++) {
1162 struct commit
*m2
= (struct commit
*) merges
.objects
[j
].item
;
1163 if (i
!= j
&& repo_in_merge_bases(repo
, m2
, m1
)) {
1164 contains_another
= 1;
1169 if (!contains_another
)
1170 add_object_array(merges
.objects
[i
].item
, NULL
, result
);
1173 object_array_clear(&merges
);
1174 release_revisions(&revs
);
1178 static void print_commit(struct repository
*repo
, struct commit
*commit
)
1180 struct strbuf sb
= STRBUF_INIT
;
1181 struct pretty_print_context ctx
= {0};
1182 ctx
.date_mode
.type
= DATE_NORMAL
;
1183 /* FIXME: Merge this with output_commit_title() */
1184 assert(!merge_remote_util(commit
));
1185 repo_format_commit_message(repo
, commit
, " %h: %m %s", &sb
, &ctx
);
1186 fprintf(stderr
, "%s\n", sb
.buf
);
1187 strbuf_release(&sb
);
1190 static int is_valid(const struct diff_filespec
*dfs
)
1192 return dfs
->mode
!= 0 && !is_null_oid(&dfs
->oid
);
1195 static int merge_submodule(struct merge_options
*opt
,
1196 struct object_id
*result
, const char *path
,
1197 const struct object_id
*base
, const struct object_id
*a
,
1198 const struct object_id
*b
)
1200 struct repository subrepo
;
1202 struct commit
*commit_base
, *commit_a
, *commit_b
;
1204 struct object_array merges
;
1207 int search
= !opt
->priv
->call_depth
;
1209 /* store a in result in case we fail */
1210 /* FIXME: This is the WRONG resolution for the recursive case when
1211 * we need to be careful to avoid accidentally matching either side.
1212 * Should probably use o instead there, much like we do for merging
1217 /* we can not handle deletion conflicts */
1218 if (is_null_oid(base
))
1225 if (repo_submodule_init(&subrepo
, opt
->repo
, path
, null_oid())) {
1226 output(opt
, 1, _("Failed to merge submodule %s (not checked out)"), path
);
1230 if (!(commit_base
= lookup_commit_reference(&subrepo
, base
)) ||
1231 !(commit_a
= lookup_commit_reference(&subrepo
, a
)) ||
1232 !(commit_b
= lookup_commit_reference(&subrepo
, b
))) {
1233 output(opt
, 1, _("Failed to merge submodule %s (commits not present)"), path
);
1237 /* check whether both changes are forward */
1238 if (!repo_in_merge_bases(&subrepo
, commit_base
, commit_a
) ||
1239 !repo_in_merge_bases(&subrepo
, commit_base
, commit_b
)) {
1240 output(opt
, 1, _("Failed to merge submodule %s (commits don't follow merge-base)"), path
);
1244 /* Case #1: a is contained in b or vice versa */
1245 if (repo_in_merge_bases(&subrepo
, commit_a
, commit_b
)) {
1248 output(opt
, 3, _("Fast-forwarding submodule %s to the following commit:"), path
);
1249 repo_output_commit_title(opt
, &subrepo
, commit_b
);
1250 } else if (show(opt
, 2))
1251 output(opt
, 2, _("Fast-forwarding submodule %s"), path
);
1258 if (repo_in_merge_bases(&subrepo
, commit_b
, commit_a
)) {
1261 output(opt
, 3, _("Fast-forwarding submodule %s to the following commit:"), path
);
1262 repo_output_commit_title(opt
, &subrepo
, commit_a
);
1263 } else if (show(opt
, 2))
1264 output(opt
, 2, _("Fast-forwarding submodule %s"), path
);
1273 * Case #2: There are one or more merges that contain a and b in
1274 * the submodule. If there is only one, then present it as a
1275 * suggestion to the user, but leave it marked unmerged so the
1276 * user needs to confirm the resolution.
1279 /* Skip the search if makes no sense to the calling context. */
1283 /* find commit which merges them */
1284 parent_count
= find_first_merges(&subrepo
, &merges
, path
,
1285 commit_a
, commit_b
);
1286 switch (parent_count
) {
1288 output(opt
, 1, _("Failed to merge submodule %s (merge following commits not found)"), path
);
1292 output(opt
, 1, _("Failed to merge submodule %s (not fast-forward)"), path
);
1293 output(opt
, 2, _("Found a possible merge resolution for the submodule:\n"));
1294 print_commit(&subrepo
, (struct commit
*) merges
.objects
[0].item
);
1296 "If this is correct simply add it to the index "
1299 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
1300 "which will accept this suggestion.\n"),
1301 oid_to_hex(&merges
.objects
[0].item
->oid
), path
);
1305 output(opt
, 1, _("Failed to merge submodule %s (multiple merges found)"), path
);
1306 for (i
= 0; i
< merges
.nr
; i
++)
1307 print_commit(&subrepo
, (struct commit
*) merges
.objects
[i
].item
);
1310 object_array_clear(&merges
);
1312 repo_clear(&subrepo
);
1316 static int merge_mode_and_contents(struct merge_options
*opt
,
1317 const struct diff_filespec
*o
,
1318 const struct diff_filespec
*a
,
1319 const struct diff_filespec
*b
,
1320 const char *filename
,
1321 const char *branch1
,
1322 const char *branch2
,
1323 const int extra_marker_size
,
1324 struct merge_file_info
*result
)
1326 if (opt
->branch1
!= branch1
) {
1328 * It's weird getting a reverse merge with HEAD on the bottom
1329 * side of the conflict markers and the other branch on the
1332 return merge_mode_and_contents(opt
, o
, b
, a
,
1335 extra_marker_size
, result
);
1341 if ((S_IFMT
& a
->mode
) != (S_IFMT
& b
->mode
)) {
1344 * FIXME: This is a bad resolution for recursive case; for
1345 * the recursive case we want something that is unlikely to
1346 * accidentally match either side. Also, while it makes
1347 * sense to prefer regular files over symlinks, it doesn't
1348 * make sense to prefer regular files over submodules.
1350 if (S_ISREG(a
->mode
)) {
1351 result
->blob
.mode
= a
->mode
;
1352 oidcpy(&result
->blob
.oid
, &a
->oid
);
1354 result
->blob
.mode
= b
->mode
;
1355 oidcpy(&result
->blob
.oid
, &b
->oid
);
1358 if (!oideq(&a
->oid
, &o
->oid
) && !oideq(&b
->oid
, &o
->oid
))
1364 if (a
->mode
== b
->mode
|| a
->mode
== o
->mode
)
1365 result
->blob
.mode
= b
->mode
;
1367 result
->blob
.mode
= a
->mode
;
1368 if (b
->mode
!= o
->mode
) {
1374 if (oideq(&a
->oid
, &b
->oid
) || oideq(&a
->oid
, &o
->oid
))
1375 oidcpy(&result
->blob
.oid
, &b
->oid
);
1376 else if (oideq(&b
->oid
, &o
->oid
))
1377 oidcpy(&result
->blob
.oid
, &a
->oid
);
1378 else if (S_ISREG(a
->mode
)) {
1379 mmbuffer_t result_buf
;
1380 int ret
= 0, merge_status
;
1382 merge_status
= merge_3way(opt
, &result_buf
, o
, a
, b
,
1386 if ((merge_status
< 0) || !result_buf
.ptr
)
1387 ret
= err(opt
, _("Failed to execute internal merge"));
1390 write_object_file(result_buf
.ptr
, result_buf
.size
,
1391 OBJ_BLOB
, &result
->blob
.oid
))
1392 ret
= err(opt
, _("Unable to add %s to database"),
1395 free(result_buf
.ptr
);
1398 /* FIXME: bug, what if modes didn't match? */
1399 result
->clean
= (merge_status
== 0);
1400 } else if (S_ISGITLINK(a
->mode
)) {
1401 result
->clean
= merge_submodule(opt
, &result
->blob
.oid
,
1406 } else if (S_ISLNK(a
->mode
)) {
1407 switch (opt
->recursive_variant
) {
1408 case MERGE_VARIANT_NORMAL
:
1409 oidcpy(&result
->blob
.oid
, &a
->oid
);
1410 if (!oideq(&a
->oid
, &b
->oid
))
1413 case MERGE_VARIANT_OURS
:
1414 oidcpy(&result
->blob
.oid
, &a
->oid
);
1416 case MERGE_VARIANT_THEIRS
:
1417 oidcpy(&result
->blob
.oid
, &b
->oid
);
1421 BUG("unsupported object type in the tree");
1425 output(opt
, 2, _("Auto-merging %s"), filename
);
1430 static int handle_rename_via_dir(struct merge_options
*opt
,
1431 struct rename_conflict_info
*ci
)
1434 * Handle file adds that need to be renamed due to directory rename
1435 * detection. This differs from handle_rename_normal, because
1436 * there is no content merge to do; just move the file into the
1437 * desired final location.
1439 const struct rename
*ren
= ci
->ren1
;
1440 const struct diff_filespec
*dest
= ren
->pair
->two
;
1441 char *file_path
= dest
->path
;
1442 int mark_conflicted
= (opt
->detect_directory_renames
==
1443 MERGE_DIRECTORY_RENAMES_CONFLICT
);
1444 assert(ren
->dir_rename_original_dest
);
1446 if (!opt
->priv
->call_depth
&& would_lose_untracked(opt
, dest
->path
)) {
1447 mark_conflicted
= 1;
1448 file_path
= unique_path(opt
, dest
->path
, ren
->branch
);
1449 output(opt
, 1, _("Error: Refusing to lose untracked file at %s; "
1450 "writing to %s instead."),
1451 dest
->path
, file_path
);
1454 if (mark_conflicted
) {
1456 * Write the file in worktree at file_path. In the index,
1457 * only record the file at dest->path in the appropriate
1460 if (update_file(opt
, 0, dest
, file_path
))
1462 if (file_path
!= dest
->path
)
1464 if (update_stages(opt
, dest
->path
, NULL
,
1465 ren
->branch
== opt
->branch1
? dest
: NULL
,
1466 ren
->branch
== opt
->branch1
? NULL
: dest
))
1468 return 0; /* not clean, but conflicted */
1470 /* Update dest->path both in index and in worktree */
1471 if (update_file(opt
, 1, dest
, dest
->path
))
1473 return 1; /* clean */
1477 static int handle_change_delete(struct merge_options
*opt
,
1478 const char *path
, const char *old_path
,
1479 const struct diff_filespec
*o
,
1480 const struct diff_filespec
*changed
,
1481 const char *change_branch
,
1482 const char *delete_branch
,
1483 const char *change
, const char *change_past
)
1485 char *alt_path
= NULL
;
1486 const char *update_path
= path
;
1489 if (dir_in_way(opt
->repo
->index
, path
, !opt
->priv
->call_depth
, 0) ||
1490 (!opt
->priv
->call_depth
&& would_lose_untracked(opt
, path
))) {
1491 update_path
= alt_path
= unique_path(opt
, path
, change_branch
);
1494 if (opt
->priv
->call_depth
) {
1496 * We cannot arbitrarily accept either a_sha or b_sha as
1497 * correct; since there is no true "middle point" between
1498 * them, simply reuse the base version for virtual merge base.
1500 ret
= remove_file_from_index(opt
->repo
->index
, path
);
1502 ret
= update_file(opt
, 0, o
, update_path
);
1505 * Despite the four nearly duplicate messages and argument
1506 * lists below and the ugliness of the nested if-statements,
1507 * having complete messages makes the job easier for
1510 * The slight variance among the cases is due to the fact
1512 * 1) directory/file conflicts (in effect if
1513 * !alt_path) could cause us to need to write the
1514 * file to a different path.
1515 * 2) renames (in effect if !old_path) could mean that
1516 * there are two names for the path that the user
1517 * may know the file by.
1521 output(opt
, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1522 "and %s in %s. Version %s of %s left in tree."),
1523 change
, path
, delete_branch
, change_past
,
1524 change_branch
, change_branch
, path
);
1526 output(opt
, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1527 "and %s to %s in %s. Version %s of %s left in tree."),
1528 change
, old_path
, delete_branch
, change_past
, path
,
1529 change_branch
, change_branch
, path
);
1533 output(opt
, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1534 "and %s in %s. Version %s of %s left in tree at %s."),
1535 change
, path
, delete_branch
, change_past
,
1536 change_branch
, change_branch
, path
, alt_path
);
1538 output(opt
, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1539 "and %s to %s in %s. Version %s of %s left in tree at %s."),
1540 change
, old_path
, delete_branch
, change_past
, path
,
1541 change_branch
, change_branch
, path
, alt_path
);
1545 * No need to call update_file() on path when change_branch ==
1546 * opt->branch1 && !alt_path, since that would needlessly touch
1547 * path. We could call update_file_flags() with update_cache=0
1548 * and update_wd=0, but that's a no-op.
1550 if (change_branch
!= opt
->branch1
|| alt_path
)
1551 ret
= update_file(opt
, 0, changed
, update_path
);
1558 static int handle_rename_delete(struct merge_options
*opt
,
1559 struct rename_conflict_info
*ci
)
1561 const struct rename
*ren
= ci
->ren1
;
1562 const struct diff_filespec
*orig
= ren
->pair
->one
;
1563 const struct diff_filespec
*dest
= ren
->pair
->two
;
1564 const char *rename_branch
= ren
->branch
;
1565 const char *delete_branch
= (opt
->branch1
== ren
->branch
?
1566 opt
->branch2
: opt
->branch1
);
1568 if (handle_change_delete(opt
,
1569 opt
->priv
->call_depth
? orig
->path
: dest
->path
,
1570 opt
->priv
->call_depth
? NULL
: orig
->path
,
1572 rename_branch
, delete_branch
,
1573 _("rename"), _("renamed")))
1576 if (opt
->priv
->call_depth
)
1577 return remove_file_from_index(opt
->repo
->index
, dest
->path
);
1579 return update_stages(opt
, dest
->path
, NULL
,
1580 rename_branch
== opt
->branch1
? dest
: NULL
,
1581 rename_branch
== opt
->branch1
? NULL
: dest
);
1584 static int handle_file_collision(struct merge_options
*opt
,
1585 const char *collide_path
,
1586 const char *prev_path1
,
1587 const char *prev_path2
,
1588 const char *branch1
, const char *branch2
,
1589 struct diff_filespec
*a
,
1590 struct diff_filespec
*b
)
1592 struct merge_file_info mfi
;
1593 struct diff_filespec null
;
1594 char *alt_path
= NULL
;
1595 const char *update_path
= collide_path
;
1598 * It's easiest to get the correct things into stage 2 and 3, and
1599 * to make sure that the content merge puts HEAD before the other
1600 * branch if we just ensure that branch1 == opt->branch1. So, simply
1601 * flip arguments around if we don't have that.
1603 if (branch1
!= opt
->branch1
) {
1604 return handle_file_collision(opt
, collide_path
,
1605 prev_path2
, prev_path1
,
1610 /* Remove rename sources if rename/add or rename/rename(2to1) */
1612 remove_file(opt
, 1, prev_path1
,
1613 opt
->priv
->call_depth
|| would_lose_untracked(opt
, prev_path1
));
1615 remove_file(opt
, 1, prev_path2
,
1616 opt
->priv
->call_depth
|| would_lose_untracked(opt
, prev_path2
));
1619 * Remove the collision path, if it wouldn't cause dirty contents
1620 * or an untracked file to get lost. We'll either overwrite with
1621 * merged contents, or just write out to differently named files.
1623 if (was_dirty(opt
, collide_path
)) {
1624 output(opt
, 1, _("Refusing to lose dirty file at %s"),
1626 update_path
= alt_path
= unique_path(opt
, collide_path
, "merged");
1627 } else if (would_lose_untracked(opt
, collide_path
)) {
1629 * Only way we get here is if both renames were from
1630 * a directory rename AND user had an untracked file
1631 * at the location where both files end up after the
1632 * two directory renames. See testcase 10d of t6043.
1634 output(opt
, 1, _("Refusing to lose untracked file at "
1635 "%s, even though it's in the way."),
1637 update_path
= alt_path
= unique_path(opt
, collide_path
, "merged");
1640 * FIXME: It's possible that the two files are identical
1641 * and that the current working copy happens to match, in
1642 * which case we are unnecessarily touching the working
1643 * tree file. It's not a likely enough scenario that I
1644 * want to code up the checks for it and a better fix is
1645 * available if we restructure how unpack_trees() and
1646 * merge-recursive interoperate anyway, so punting for
1649 remove_file(opt
, 0, collide_path
, 0);
1652 /* Store things in diff_filespecs for functions that need it */
1653 null
.path
= (char *)collide_path
;
1654 oidcpy(&null
.oid
, null_oid());
1657 if (merge_mode_and_contents(opt
, &null
, a
, b
, collide_path
,
1658 branch1
, branch2
, opt
->priv
->call_depth
* 2, &mfi
))
1660 mfi
.clean
&= !alt_path
;
1661 if (update_file(opt
, mfi
.clean
, &mfi
.blob
, update_path
))
1663 if (!mfi
.clean
&& !opt
->priv
->call_depth
&&
1664 update_stages(opt
, collide_path
, NULL
, a
, b
))
1668 * FIXME: If both a & b both started with conflicts (only possible
1669 * if they came from a rename/rename(2to1)), but had IDENTICAL
1670 * contents including those conflicts, then in the next line we claim
1671 * it was clean. If someone cares about this case, we should have the
1672 * caller notify us if we started with conflicts.
1677 static int handle_rename_add(struct merge_options
*opt
,
1678 struct rename_conflict_info
*ci
)
1680 /* a was renamed to c, and a separate c was added. */
1681 struct diff_filespec
*a
= ci
->ren1
->pair
->one
;
1682 struct diff_filespec
*c
= ci
->ren1
->pair
->two
;
1683 char *path
= c
->path
;
1684 char *prev_path_desc
;
1685 struct merge_file_info mfi
;
1687 const char *rename_branch
= ci
->ren1
->branch
;
1688 const char *add_branch
= (opt
->branch1
== rename_branch
?
1689 opt
->branch2
: opt
->branch1
);
1690 int other_stage
= (ci
->ren1
->branch
== opt
->branch1
? 3 : 2);
1692 output(opt
, 1, _("CONFLICT (rename/add): "
1693 "Rename %s->%s in %s. Added %s in %s"),
1694 a
->path
, c
->path
, rename_branch
,
1695 c
->path
, add_branch
);
1697 prev_path_desc
= xstrfmt("version of %s from %s", path
, a
->path
);
1698 ci
->ren1
->src_entry
->stages
[other_stage
].path
= a
->path
;
1699 if (merge_mode_and_contents(opt
, a
, c
,
1700 &ci
->ren1
->src_entry
->stages
[other_stage
],
1702 opt
->branch1
, opt
->branch2
,
1703 1 + opt
->priv
->call_depth
* 2, &mfi
))
1705 free(prev_path_desc
);
1707 ci
->ren1
->dst_entry
->stages
[other_stage
].path
= mfi
.blob
.path
= c
->path
;
1708 return handle_file_collision(opt
,
1709 c
->path
, a
->path
, NULL
,
1710 rename_branch
, add_branch
,
1712 &ci
->ren1
->dst_entry
->stages
[other_stage
]);
1715 static char *find_path_for_conflict(struct merge_options
*opt
,
1717 const char *branch1
,
1718 const char *branch2
)
1720 char *new_path
= NULL
;
1721 if (dir_in_way(opt
->repo
->index
, path
, !opt
->priv
->call_depth
, 0)) {
1722 new_path
= unique_path(opt
, path
, branch1
);
1723 output(opt
, 1, _("%s is a directory in %s adding "
1725 path
, branch2
, new_path
);
1726 } else if (would_lose_untracked(opt
, path
)) {
1727 new_path
= unique_path(opt
, path
, branch1
);
1728 output(opt
, 1, _("Refusing to lose untracked file"
1729 " at %s; adding as %s instead"),
1737 * Toggle the stage number between "ours" and "theirs" (2 and 3).
1739 static inline int flip_stage(int stage
)
1741 return (2 + 3) - stage
;
1744 static int handle_rename_rename_1to2(struct merge_options
*opt
,
1745 struct rename_conflict_info
*ci
)
1747 /* One file was renamed in both branches, but to different names. */
1748 struct merge_file_info mfi
;
1749 struct diff_filespec
*add
;
1750 struct diff_filespec
*o
= ci
->ren1
->pair
->one
;
1751 struct diff_filespec
*a
= ci
->ren1
->pair
->two
;
1752 struct diff_filespec
*b
= ci
->ren2
->pair
->two
;
1755 output(opt
, 1, _("CONFLICT (rename/rename): "
1756 "Rename \"%s\"->\"%s\" in branch \"%s\" "
1757 "rename \"%s\"->\"%s\" in \"%s\"%s"),
1758 o
->path
, a
->path
, ci
->ren1
->branch
,
1759 o
->path
, b
->path
, ci
->ren2
->branch
,
1760 opt
->priv
->call_depth
? _(" (left unresolved)") : "");
1762 path_desc
= xstrfmt("%s and %s, both renamed from %s",
1763 a
->path
, b
->path
, o
->path
);
1764 if (merge_mode_and_contents(opt
, o
, a
, b
, path_desc
,
1765 ci
->ren1
->branch
, ci
->ren2
->branch
,
1766 opt
->priv
->call_depth
* 2, &mfi
))
1770 if (opt
->priv
->call_depth
)
1771 remove_file_from_index(opt
->repo
->index
, o
->path
);
1774 * For each destination path, we need to see if there is a
1775 * rename/add collision. If not, we can write the file out
1776 * to the specified location.
1778 add
= &ci
->ren1
->dst_entry
->stages
[flip_stage(2)];
1779 if (is_valid(add
)) {
1780 add
->path
= mfi
.blob
.path
= a
->path
;
1781 if (handle_file_collision(opt
, a
->path
,
1785 &mfi
.blob
, add
) < 0)
1788 char *new_path
= find_path_for_conflict(opt
, a
->path
,
1791 if (update_file(opt
, 0, &mfi
.blob
,
1792 new_path
? new_path
: a
->path
))
1795 if (!opt
->priv
->call_depth
&&
1796 update_stages(opt
, a
->path
, NULL
, a
, NULL
))
1800 if (!mfi
.clean
&& mfi
.blob
.mode
== a
->mode
&&
1801 oideq(&mfi
.blob
.oid
, &a
->oid
)) {
1803 * Getting here means we were attempting to merge a binary
1804 * blob. Since we can't merge binaries, the merge algorithm
1805 * just takes one side. But we don't want to copy the
1806 * contents of one side to both paths; we'd rather use the
1807 * original content at the given path for each path.
1809 oidcpy(&mfi
.blob
.oid
, &b
->oid
);
1810 mfi
.blob
.mode
= b
->mode
;
1812 add
= &ci
->ren2
->dst_entry
->stages
[flip_stage(3)];
1813 if (is_valid(add
)) {
1814 add
->path
= mfi
.blob
.path
= b
->path
;
1815 if (handle_file_collision(opt
, b
->path
,
1819 add
, &mfi
.blob
) < 0)
1822 char *new_path
= find_path_for_conflict(opt
, b
->path
,
1825 if (update_file(opt
, 0, &mfi
.blob
,
1826 new_path
? new_path
: b
->path
))
1829 if (!opt
->priv
->call_depth
&&
1830 update_stages(opt
, b
->path
, NULL
, NULL
, b
))
1837 static int handle_rename_rename_2to1(struct merge_options
*opt
,
1838 struct rename_conflict_info
*ci
)
1840 /* Two files, a & b, were renamed to the same thing, c. */
1841 struct diff_filespec
*a
= ci
->ren1
->pair
->one
;
1842 struct diff_filespec
*b
= ci
->ren2
->pair
->one
;
1843 struct diff_filespec
*c1
= ci
->ren1
->pair
->two
;
1844 struct diff_filespec
*c2
= ci
->ren2
->pair
->two
;
1845 char *path
= c1
->path
; /* == c2->path */
1846 char *path_side_1_desc
;
1847 char *path_side_2_desc
;
1848 struct merge_file_info mfi_c1
;
1849 struct merge_file_info mfi_c2
;
1850 int ostage1
, ostage2
;
1852 output(opt
, 1, _("CONFLICT (rename/rename): "
1853 "Rename %s->%s in %s. "
1854 "Rename %s->%s in %s"),
1855 a
->path
, c1
->path
, ci
->ren1
->branch
,
1856 b
->path
, c2
->path
, ci
->ren2
->branch
);
1858 path_side_1_desc
= xstrfmt("version of %s from %s", path
, a
->path
);
1859 path_side_2_desc
= xstrfmt("version of %s from %s", path
, b
->path
);
1860 ostage1
= ci
->ren1
->branch
== opt
->branch1
? 3 : 2;
1861 ostage2
= flip_stage(ostage1
);
1862 ci
->ren1
->src_entry
->stages
[ostage1
].path
= a
->path
;
1863 ci
->ren2
->src_entry
->stages
[ostage2
].path
= b
->path
;
1864 if (merge_mode_and_contents(opt
, a
, c1
,
1865 &ci
->ren1
->src_entry
->stages
[ostage1
],
1867 opt
->branch1
, opt
->branch2
,
1868 1 + opt
->priv
->call_depth
* 2, &mfi_c1
) ||
1869 merge_mode_and_contents(opt
, b
,
1870 &ci
->ren2
->src_entry
->stages
[ostage2
],
1871 c2
, path_side_2_desc
,
1872 opt
->branch1
, opt
->branch2
,
1873 1 + opt
->priv
->call_depth
* 2, &mfi_c2
))
1875 free(path_side_1_desc
);
1876 free(path_side_2_desc
);
1877 mfi_c1
.blob
.path
= path
;
1878 mfi_c2
.blob
.path
= path
;
1880 return handle_file_collision(opt
, path
, a
->path
, b
->path
,
1881 ci
->ren1
->branch
, ci
->ren2
->branch
,
1882 &mfi_c1
.blob
, &mfi_c2
.blob
);
1886 * Get the diff_filepairs changed between o_tree and tree.
1888 static struct diff_queue_struct
*get_diffpairs(struct merge_options
*opt
,
1889 struct tree
*o_tree
,
1892 struct diff_queue_struct
*ret
;
1893 struct diff_options opts
;
1895 repo_diff_setup(opt
->repo
, &opts
);
1896 opts
.flags
.recursive
= 1;
1897 opts
.flags
.rename_empty
= 0;
1898 opts
.detect_rename
= merge_detect_rename(opt
);
1900 * We do not have logic to handle the detection of copies. In
1901 * fact, it may not even make sense to add such logic: would we
1902 * really want a change to a base file to be propagated through
1903 * multiple other files by a merge?
1905 if (opts
.detect_rename
> DIFF_DETECT_RENAME
)
1906 opts
.detect_rename
= DIFF_DETECT_RENAME
;
1907 opts
.rename_limit
= (opt
->rename_limit
>= 0) ? opt
->rename_limit
: 7000;
1908 opts
.rename_score
= opt
->rename_score
;
1909 opts
.show_rename_progress
= opt
->show_rename_progress
;
1910 opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1911 diff_setup_done(&opts
);
1912 diff_tree_oid(&o_tree
->object
.oid
, &tree
->object
.oid
, "", &opts
);
1913 diffcore_std(&opts
);
1914 if (opts
.needed_rename_limit
> opt
->priv
->needed_rename_limit
)
1915 opt
->priv
->needed_rename_limit
= opts
.needed_rename_limit
;
1917 ret
= xmalloc(sizeof(*ret
));
1918 *ret
= diff_queued_diff
;
1920 opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1921 diff_queued_diff
.nr
= 0;
1922 diff_queued_diff
.queue
= NULL
;
1927 static int tree_has_path(struct repository
*r
, struct tree
*tree
,
1930 struct object_id hashy
;
1931 unsigned short mode_o
;
1933 return !get_tree_entry(r
,
1934 &tree
->object
.oid
, path
,
1939 * Return a new string that replaces the beginning portion (which matches
1940 * entry->dir), with entry->new_dir. In perl-speak:
1941 * new_path_name = (old_path =~ s/entry->dir/entry->new_dir/);
1943 * Caller must ensure that old_path starts with entry->dir + '/'.
1945 static char *apply_dir_rename(struct dir_rename_entry
*entry
,
1946 const char *old_path
)
1948 struct strbuf new_path
= STRBUF_INIT
;
1951 if (entry
->non_unique_new_dir
)
1954 oldlen
= strlen(entry
->dir
);
1955 if (entry
->new_dir
.len
== 0)
1957 * If someone renamed/merged a subdirectory into the root
1958 * directory (e.g. 'some/subdir' -> ''), then we want to
1961 * as the rename; we need to make old_path + oldlen advance
1962 * past the '/' character.
1965 newlen
= entry
->new_dir
.len
+ (strlen(old_path
) - oldlen
) + 1;
1966 strbuf_grow(&new_path
, newlen
);
1967 strbuf_addbuf(&new_path
, &entry
->new_dir
);
1968 strbuf_addstr(&new_path
, &old_path
[oldlen
]);
1970 return strbuf_detach(&new_path
, NULL
);
1973 static void get_renamed_dir_portion(const char *old_path
, const char *new_path
,
1974 char **old_dir
, char **new_dir
)
1976 char *end_of_old
, *end_of_new
;
1978 /* Default return values: NULL, meaning no rename */
1984 * "a/b/c/d/e/foo.c" -> "a/b/some/thing/else/e/foo.c"
1985 * the "e/foo.c" part is the same, we just want to know that
1986 * "a/b/c/d" was renamed to "a/b/some/thing/else"
1987 * so, for this example, this function returns "a/b/c/d" in
1988 * *old_dir and "a/b/some/thing/else" in *new_dir.
1992 * If the basename of the file changed, we don't care. We want
1993 * to know which portion of the directory, if any, changed.
1995 end_of_old
= strrchr(old_path
, '/');
1996 end_of_new
= strrchr(new_path
, '/');
1999 * If end_of_old is NULL, old_path wasn't in a directory, so there
2000 * could not be a directory rename (our rule elsewhere that a
2001 * directory which still exists is not considered to have been
2002 * renamed means the root directory can never be renamed -- because
2003 * the root directory always exists).
2006 return; /* Note: *old_dir and *new_dir are still NULL */
2009 * If new_path contains no directory (end_of_new is NULL), then we
2010 * have a rename of old_path's directory to the root directory.
2013 *old_dir
= xstrndup(old_path
, end_of_old
- old_path
);
2014 *new_dir
= xstrdup("");
2018 /* Find the first non-matching character traversing backwards */
2019 while (*--end_of_new
== *--end_of_old
&&
2020 end_of_old
!= old_path
&&
2021 end_of_new
!= new_path
)
2022 ; /* Do nothing; all in the while loop */
2025 * If both got back to the beginning of their strings, then the
2026 * directory didn't change at all, only the basename did.
2028 if (end_of_old
== old_path
&& end_of_new
== new_path
&&
2029 *end_of_old
== *end_of_new
)
2030 return; /* Note: *old_dir and *new_dir are still NULL */
2033 * If end_of_new got back to the beginning of its string, and
2034 * end_of_old got back to the beginning of some subdirectory, then
2035 * we have a rename/merge of a subdirectory into the root, which
2036 * needs slightly special handling.
2038 * Note: There is no need to consider the opposite case, with a
2039 * rename/merge of the root directory into some subdirectory
2040 * because as noted above the root directory always exists so it
2041 * cannot be considered to be renamed.
2043 if (end_of_new
== new_path
&&
2044 end_of_old
!= old_path
&& end_of_old
[-1] == '/') {
2045 *old_dir
= xstrndup(old_path
, --end_of_old
- old_path
);
2046 *new_dir
= xstrdup("");
2051 * We've found the first non-matching character in the directory
2052 * paths. That means the current characters we were looking at
2053 * were part of the first non-matching subdir name going back from
2054 * the end of the strings. Get the whole name by advancing both
2055 * end_of_old and end_of_new to the NEXT '/' character. That will
2056 * represent the entire directory rename.
2058 * The reason for the increment is cases like
2059 * a/b/star/foo/whatever.c -> a/b/tar/foo/random.c
2060 * After dropping the basename and going back to the first
2061 * non-matching character, we're now comparing:
2063 * and we want to be comparing:
2064 * a/b/star/ and a/b/tar/
2065 * but without the pre-increment, the one on the right would stay
2068 end_of_old
= strchr(++end_of_old
, '/');
2069 end_of_new
= strchr(++end_of_new
, '/');
2071 /* Copy the old and new directories into *old_dir and *new_dir. */
2072 *old_dir
= xstrndup(old_path
, end_of_old
- old_path
);
2073 *new_dir
= xstrndup(new_path
, end_of_new
- new_path
);
2076 static void remove_hashmap_entries(struct hashmap
*dir_renames
,
2077 struct string_list
*items_to_remove
)
2080 struct dir_rename_entry
*entry
;
2082 for (i
= 0; i
< items_to_remove
->nr
; i
++) {
2083 entry
= items_to_remove
->items
[i
].util
;
2084 hashmap_remove(dir_renames
, &entry
->ent
, NULL
);
2086 string_list_clear(items_to_remove
, 0);
2090 * See if there is a directory rename for path, and if there are any file
2091 * level conflicts for the renamed location. If there is a rename and
2092 * there are no conflicts, return the new name. Otherwise, return NULL.
2094 static char *handle_path_level_conflicts(struct merge_options
*opt
,
2096 struct dir_rename_entry
*entry
,
2097 struct hashmap
*collisions
,
2100 char *new_path
= NULL
;
2101 struct collision_entry
*collision_ent
;
2103 struct strbuf collision_paths
= STRBUF_INIT
;
2106 * entry has the mapping of old directory name to new directory name
2107 * that we want to apply to path.
2109 new_path
= apply_dir_rename(entry
, path
);
2112 /* This should only happen when entry->non_unique_new_dir set */
2113 if (!entry
->non_unique_new_dir
)
2114 BUG("entry->non_unique_new_dir not set and !new_path");
2115 output(opt
, 1, _("CONFLICT (directory rename split): "
2116 "Unclear where to place %s because directory "
2117 "%s was renamed to multiple other directories, "
2118 "with no destination getting a majority of the "
2126 * The caller needs to have ensured that it has pre-populated
2127 * collisions with all paths that map to new_path. Do a quick check
2128 * to ensure that's the case.
2130 collision_ent
= collision_find_entry(collisions
, new_path
);
2132 BUG("collision_ent is NULL");
2135 * Check for one-sided add/add/.../add conflicts, i.e.
2136 * where implicit renames from the other side doing
2137 * directory rename(s) can affect this side of history
2138 * to put multiple paths into the same location. Warn
2139 * and bail on directory renames for such paths.
2141 if (collision_ent
->reported_already
) {
2143 } else if (tree_has_path(opt
->repo
, tree
, new_path
)) {
2144 collision_ent
->reported_already
= 1;
2145 strbuf_add_separated_string_list(&collision_paths
, ", ",
2146 &collision_ent
->source_files
);
2147 output(opt
, 1, _("CONFLICT (implicit dir rename): Existing "
2148 "file/dir at %s in the way of implicit "
2149 "directory rename(s) putting the following "
2150 "path(s) there: %s."),
2151 new_path
, collision_paths
.buf
);
2153 } else if (collision_ent
->source_files
.nr
> 1) {
2154 collision_ent
->reported_already
= 1;
2155 strbuf_add_separated_string_list(&collision_paths
, ", ",
2156 &collision_ent
->source_files
);
2157 output(opt
, 1, _("CONFLICT (implicit dir rename): Cannot map "
2158 "more than one path to %s; implicit directory "
2159 "renames tried to put these paths there: %s"),
2160 new_path
, collision_paths
.buf
);
2164 /* Free memory we no longer need */
2165 strbuf_release(&collision_paths
);
2166 if (!clean
&& new_path
) {
2175 * There are a couple things we want to do at the directory level:
2176 * 1. Check for both sides renaming to the same thing, in order to avoid
2177 * implicit renaming of files that should be left in place. (See
2178 * testcase 6b in t6043 for details.)
2179 * 2. Prune directory renames if there are still files left in the
2180 * original directory. These represent a partial directory rename,
2181 * i.e. a rename where only some of the files within the directory
2182 * were renamed elsewhere. (Technically, this could be done earlier
2183 * in get_directory_renames(), except that would prevent us from
2184 * doing the previous check and thus failing testcase 6b.)
2185 * 3. Check for rename/rename(1to2) conflicts (at the directory level).
2186 * In the future, we could potentially record this info as well and
2187 * omit reporting rename/rename(1to2) conflicts for each path within
2188 * the affected directories, thus cleaning up the merge output.
2189 * NOTE: We do NOT check for rename/rename(2to1) conflicts at the
2190 * directory level, because merging directories is fine. If it
2191 * causes conflicts for files within those merged directories, then
2192 * that should be detected at the individual path level.
2194 static void handle_directory_level_conflicts(struct merge_options
*opt
,
2195 struct hashmap
*dir_re_head
,
2197 struct hashmap
*dir_re_merge
,
2200 struct hashmap_iter iter
;
2201 struct dir_rename_entry
*head_ent
;
2202 struct dir_rename_entry
*merge_ent
;
2204 struct string_list remove_from_head
= STRING_LIST_INIT_NODUP
;
2205 struct string_list remove_from_merge
= STRING_LIST_INIT_NODUP
;
2207 hashmap_for_each_entry(dir_re_head
, &iter
, head_ent
,
2208 ent
/* member name */) {
2209 merge_ent
= dir_rename_find_entry(dir_re_merge
, head_ent
->dir
);
2211 !head_ent
->non_unique_new_dir
&&
2212 !merge_ent
->non_unique_new_dir
&&
2213 !strbuf_cmp(&head_ent
->new_dir
, &merge_ent
->new_dir
)) {
2214 /* 1. Renamed identically; remove it from both sides */
2215 string_list_append(&remove_from_head
,
2216 head_ent
->dir
)->util
= head_ent
;
2217 strbuf_release(&head_ent
->new_dir
);
2218 string_list_append(&remove_from_merge
,
2219 merge_ent
->dir
)->util
= merge_ent
;
2220 strbuf_release(&merge_ent
->new_dir
);
2221 } else if (tree_has_path(opt
->repo
, head
, head_ent
->dir
)) {
2222 /* 2. This wasn't a directory rename after all */
2223 string_list_append(&remove_from_head
,
2224 head_ent
->dir
)->util
= head_ent
;
2225 strbuf_release(&head_ent
->new_dir
);
2229 remove_hashmap_entries(dir_re_head
, &remove_from_head
);
2230 remove_hashmap_entries(dir_re_merge
, &remove_from_merge
);
2232 hashmap_for_each_entry(dir_re_merge
, &iter
, merge_ent
,
2233 ent
/* member name */) {
2234 head_ent
= dir_rename_find_entry(dir_re_head
, merge_ent
->dir
);
2235 if (tree_has_path(opt
->repo
, merge
, merge_ent
->dir
)) {
2236 /* 2. This wasn't a directory rename after all */
2237 string_list_append(&remove_from_merge
,
2238 merge_ent
->dir
)->util
= merge_ent
;
2239 } else if (head_ent
&&
2240 !head_ent
->non_unique_new_dir
&&
2241 !merge_ent
->non_unique_new_dir
) {
2242 /* 3. rename/rename(1to2) */
2244 * We can assume it's not rename/rename(1to1) because
2245 * that was case (1), already checked above. So we
2246 * know that head_ent->new_dir and merge_ent->new_dir
2247 * are different strings.
2249 output(opt
, 1, _("CONFLICT (rename/rename): "
2250 "Rename directory %s->%s in %s. "
2251 "Rename directory %s->%s in %s"),
2252 head_ent
->dir
, head_ent
->new_dir
.buf
, opt
->branch1
,
2253 head_ent
->dir
, merge_ent
->new_dir
.buf
, opt
->branch2
);
2254 string_list_append(&remove_from_head
,
2255 head_ent
->dir
)->util
= head_ent
;
2256 strbuf_release(&head_ent
->new_dir
);
2257 string_list_append(&remove_from_merge
,
2258 merge_ent
->dir
)->util
= merge_ent
;
2259 strbuf_release(&merge_ent
->new_dir
);
2263 remove_hashmap_entries(dir_re_head
, &remove_from_head
);
2264 remove_hashmap_entries(dir_re_merge
, &remove_from_merge
);
2267 static struct hashmap
*get_directory_renames(struct diff_queue_struct
*pairs
)
2269 struct hashmap
*dir_renames
;
2270 struct hashmap_iter iter
;
2271 struct dir_rename_entry
*entry
;
2275 * Typically, we think of a directory rename as all files from a
2276 * certain directory being moved to a target directory. However,
2277 * what if someone first moved two files from the original
2278 * directory in one commit, and then renamed the directory
2279 * somewhere else in a later commit? At merge time, we just know
2280 * that files from the original directory went to two different
2281 * places, and that the bulk of them ended up in the same place.
2282 * We want each directory rename to represent where the bulk of the
2283 * files from that directory end up; this function exists to find
2284 * where the bulk of the files went.
2286 * The first loop below simply iterates through the list of file
2287 * renames, finding out how often each directory rename pair
2288 * possibility occurs.
2290 dir_renames
= xmalloc(sizeof(*dir_renames
));
2291 dir_rename_init(dir_renames
);
2292 for (i
= 0; i
< pairs
->nr
; ++i
) {
2293 struct string_list_item
*item
;
2295 struct diff_filepair
*pair
= pairs
->queue
[i
];
2296 char *old_dir
, *new_dir
;
2298 /* File not part of directory rename if it wasn't renamed */
2299 if (pair
->status
!= 'R')
2302 get_renamed_dir_portion(pair
->one
->path
, pair
->two
->path
,
2303 &old_dir
, &new_dir
);
2305 /* Directory didn't change at all; ignore this one. */
2308 entry
= dir_rename_find_entry(dir_renames
, old_dir
);
2310 entry
= xmalloc(sizeof(*entry
));
2311 dir_rename_entry_init(entry
, old_dir
);
2312 hashmap_put(dir_renames
, &entry
->ent
);
2316 item
= string_list_lookup(&entry
->possible_new_dirs
, new_dir
);
2318 item
= string_list_insert(&entry
->possible_new_dirs
,
2320 item
->util
= xcalloc(1, sizeof(int));
2329 * For each directory with files moved out of it, we find out which
2330 * target directory received the most files so we can declare it to
2331 * be the "winning" target location for the directory rename. This
2332 * winner gets recorded in new_dir. If there is no winner
2333 * (multiple target directories received the same number of files),
2334 * we set non_unique_new_dir. Once we've determined the winner (or
2335 * that there is no winner), we no longer need possible_new_dirs.
2337 hashmap_for_each_entry(dir_renames
, &iter
, entry
,
2338 ent
/* member name */) {
2343 for (i
= 0; i
< entry
->possible_new_dirs
.nr
; i
++) {
2344 int *count
= entry
->possible_new_dirs
.items
[i
].util
;
2348 else if (*count
> max
) {
2350 best
= entry
->possible_new_dirs
.items
[i
].string
;
2354 entry
->non_unique_new_dir
= 1;
2356 assert(entry
->new_dir
.len
== 0);
2357 strbuf_addstr(&entry
->new_dir
, best
);
2360 * The relevant directory sub-portion of the original full
2361 * filepaths were xstrndup'ed before inserting into
2362 * possible_new_dirs, and instead of manually iterating the
2363 * list and free'ing each, just lie and tell
2364 * possible_new_dirs that it did the strdup'ing so that it
2365 * will free them for us.
2367 entry
->possible_new_dirs
.strdup_strings
= 1;
2368 string_list_clear(&entry
->possible_new_dirs
, 1);
2374 static struct dir_rename_entry
*check_dir_renamed(const char *path
,
2375 struct hashmap
*dir_renames
)
2377 char *temp
= xstrdup(path
);
2379 struct dir_rename_entry
*entry
= NULL
;
2381 while ((end
= strrchr(temp
, '/'))) {
2383 entry
= dir_rename_find_entry(dir_renames
, temp
);
2391 static void compute_collisions(struct hashmap
*collisions
,
2392 struct hashmap
*dir_renames
,
2393 struct diff_queue_struct
*pairs
)
2398 * Multiple files can be mapped to the same path due to directory
2399 * renames done by the other side of history. Since that other
2400 * side of history could have merged multiple directories into one,
2401 * if our side of history added the same file basename to each of
2402 * those directories, then all N of them would get implicitly
2403 * renamed by the directory rename detection into the same path,
2404 * and we'd get an add/add/.../add conflict, and all those adds
2405 * from *this* side of history. This is not representable in the
2406 * index, and users aren't going to easily be able to make sense of
2407 * it. So we need to provide a good warning about what's
2408 * happening, and fall back to no-directory-rename detection
2409 * behavior for those paths.
2411 * See testcases 9e and all of section 5 from t6043 for examples.
2413 collision_init(collisions
);
2415 for (i
= 0; i
< pairs
->nr
; ++i
) {
2416 struct dir_rename_entry
*dir_rename_ent
;
2417 struct collision_entry
*collision_ent
;
2419 struct diff_filepair
*pair
= pairs
->queue
[i
];
2421 if (pair
->status
!= 'A' && pair
->status
!= 'R')
2423 dir_rename_ent
= check_dir_renamed(pair
->two
->path
,
2425 if (!dir_rename_ent
)
2428 new_path
= apply_dir_rename(dir_rename_ent
, pair
->two
->path
);
2431 * dir_rename_ent->non_unique_new_path is true, which
2432 * means there is no directory rename for us to use,
2433 * which means it won't cause us any additional
2437 collision_ent
= collision_find_entry(collisions
, new_path
);
2438 if (!collision_ent
) {
2439 CALLOC_ARRAY(collision_ent
, 1);
2440 hashmap_entry_init(&collision_ent
->ent
,
2442 hashmap_put(collisions
, &collision_ent
->ent
);
2443 collision_ent
->target_file
= new_path
;
2447 string_list_insert(&collision_ent
->source_files
,
2452 static char *check_for_directory_rename(struct merge_options
*opt
,
2455 struct hashmap
*dir_renames
,
2456 struct hashmap
*dir_rename_exclusions
,
2457 struct hashmap
*collisions
,
2460 char *new_path
= NULL
;
2461 struct dir_rename_entry
*entry
= check_dir_renamed(path
, dir_renames
);
2462 struct dir_rename_entry
*oentry
= NULL
;
2468 * This next part is a little weird. We do not want to do an
2469 * implicit rename into a directory we renamed on our side, because
2470 * that will result in a spurious rename/rename(1to2) conflict. An
2472 * Base commit: dumbdir/afile, otherdir/bfile
2473 * Side 1: smrtdir/afile, otherdir/bfile
2474 * Side 2: dumbdir/afile, dumbdir/bfile
2475 * Here, while working on Side 1, we could notice that otherdir was
2476 * renamed/merged to dumbdir, and change the diff_filepair for
2477 * otherdir/bfile into a rename into dumbdir/bfile. However, Side
2478 * 2 will notice the rename from dumbdir to smrtdir, and do the
2479 * transitive rename to move it from dumbdir/bfile to
2480 * smrtdir/bfile. That gives us bfile in dumbdir vs being in
2481 * smrtdir, a rename/rename(1to2) conflict. We really just want
2482 * the file to end up in smrtdir. And the way to achieve that is
2483 * to not let Side1 do the rename to dumbdir, since we know that is
2484 * the source of one of our directory renames.
2486 * That's why oentry and dir_rename_exclusions is here.
2488 * As it turns out, this also prevents N-way transient rename
2489 * confusion; See testcases 9c and 9d of t6043.
2491 oentry
= dir_rename_find_entry(dir_rename_exclusions
, entry
->new_dir
.buf
);
2493 output(opt
, 1, _("WARNING: Avoiding applying %s -> %s rename "
2494 "to %s, because %s itself was renamed."),
2495 entry
->dir
, entry
->new_dir
.buf
, path
, entry
->new_dir
.buf
);
2497 new_path
= handle_path_level_conflicts(opt
, path
, entry
,
2499 *clean_merge
&= (new_path
!= NULL
);
2505 static void apply_directory_rename_modifications(struct merge_options
*opt
,
2506 struct diff_filepair
*pair
,
2510 struct tree
*o_tree
,
2511 struct tree
*a_tree
,
2512 struct tree
*b_tree
,
2513 struct string_list
*entries
)
2515 struct string_list_item
*item
;
2516 int stage
= (tree
== a_tree
? 2 : 3);
2520 * In all cases where we can do directory rename detection,
2521 * unpack_trees() will have read pair->two->path into the
2522 * index and the working copy. We need to remove it so that
2523 * we can instead place it at new_path. It is guaranteed to
2524 * not be untracked (unpack_trees() would have errored out
2525 * saying the file would have been overwritten), but it might
2528 update_wd
= !was_dirty(opt
, pair
->two
->path
);
2530 output(opt
, 1, _("Refusing to lose dirty file at %s"),
2532 remove_file(opt
, 1, pair
->two
->path
, !update_wd
);
2534 /* Find or create a new re->dst_entry */
2535 item
= string_list_lookup(entries
, new_path
);
2538 * Since we're renaming on this side of history, and it's
2539 * due to a directory rename on the other side of history
2540 * (which we only allow when the directory in question no
2541 * longer exists on the other side of history), the
2542 * original entry for re->dst_entry is no longer
2545 re
->dst_entry
->processed
= 1;
2548 * ...because we'll be using this new one.
2550 re
->dst_entry
= item
->util
;
2553 * re->dst_entry is for the before-dir-rename path, and we
2554 * need it to hold information for the after-dir-rename
2555 * path. Before creating a new entry, we need to mark the
2556 * old one as unnecessary (...unless it is shared by
2557 * src_entry, i.e. this didn't use to be a rename, in which
2558 * case we can just allow the normal processing to happen
2561 if (pair
->status
== 'R')
2562 re
->dst_entry
->processed
= 1;
2564 re
->dst_entry
= insert_stage_data(opt
->repo
, new_path
,
2565 o_tree
, a_tree
, b_tree
,
2567 item
= string_list_insert(entries
, new_path
);
2568 item
->util
= re
->dst_entry
;
2572 * Update the stage_data with the information about the path we are
2573 * moving into place. That slot will be empty and available for us
2574 * to write to because of the collision checks in
2575 * handle_path_level_conflicts(). In other words,
2576 * re->dst_entry->stages[stage].oid will be the null_oid, so it's
2577 * open for us to write to.
2579 * It may be tempting to actually update the index at this point as
2580 * well, using update_stages_for_stage_data(), but as per the big
2581 * "NOTE" in update_stages(), doing so will modify the current
2582 * in-memory index which will break calls to would_lose_untracked()
2583 * that we need to make. Instead, we need to just make sure that
2584 * the various handle_rename_*() functions update the index
2585 * explicitly rather than relying on unpack_trees() to have done it.
2587 get_tree_entry(opt
->repo
,
2590 &re
->dst_entry
->stages
[stage
].oid
,
2591 &re
->dst_entry
->stages
[stage
].mode
);
2594 * Record the original change status (or 'type' of change). If it
2595 * was originally an add ('A'), this lets us differentiate later
2596 * between a RENAME_DELETE conflict and RENAME_VIA_DIR (they
2597 * otherwise look the same). If it was originally a rename ('R'),
2598 * this lets us remember and report accurately about the transitive
2599 * renaming that occurred via the directory rename detection. Also,
2600 * record the original destination name.
2602 re
->dir_rename_original_type
= pair
->status
;
2603 re
->dir_rename_original_dest
= pair
->two
->path
;
2606 * We don't actually look at pair->status again, but it seems
2607 * pedagogically correct to adjust it.
2612 * Finally, record the new location.
2614 pair
->two
->path
= new_path
;
2618 * Get information of all renames which occurred in 'pairs', making use of
2619 * any implicit directory renames inferred from the other side of history.
2620 * We need the three trees in the merge ('o_tree', 'a_tree' and 'b_tree')
2621 * to be able to associate the correct cache entries with the rename
2622 * information; tree is always equal to either a_tree or b_tree.
2624 static struct string_list
*get_renames(struct merge_options
*opt
,
2626 struct diff_queue_struct
*pairs
,
2627 struct hashmap
*dir_renames
,
2628 struct hashmap
*dir_rename_exclusions
,
2630 struct tree
*o_tree
,
2631 struct tree
*a_tree
,
2632 struct tree
*b_tree
,
2633 struct string_list
*entries
,
2637 struct hashmap collisions
;
2638 struct hashmap_iter iter
;
2639 struct collision_entry
*e
;
2640 struct string_list
*renames
;
2642 compute_collisions(&collisions
, dir_renames
, pairs
);
2643 CALLOC_ARRAY(renames
, 1);
2645 for (i
= 0; i
< pairs
->nr
; ++i
) {
2646 struct string_list_item
*item
;
2648 struct diff_filepair
*pair
= pairs
->queue
[i
];
2649 char *new_path
; /* non-NULL only with directory renames */
2651 if (pair
->status
!= 'A' && pair
->status
!= 'R') {
2652 diff_free_filepair(pair
);
2655 new_path
= check_for_directory_rename(opt
, pair
->two
->path
, tree
,
2657 dir_rename_exclusions
,
2660 if (pair
->status
!= 'R' && !new_path
) {
2661 diff_free_filepair(pair
);
2665 re
= xmalloc(sizeof(*re
));
2668 re
->branch
= branch
;
2669 re
->dir_rename_original_type
= '\0';
2670 re
->dir_rename_original_dest
= NULL
;
2671 item
= string_list_lookup(entries
, re
->pair
->one
->path
);
2673 re
->src_entry
= insert_stage_data(opt
->repo
,
2674 re
->pair
->one
->path
,
2675 o_tree
, a_tree
, b_tree
, entries
);
2677 re
->src_entry
= item
->util
;
2679 item
= string_list_lookup(entries
, re
->pair
->two
->path
);
2681 re
->dst_entry
= insert_stage_data(opt
->repo
,
2682 re
->pair
->two
->path
,
2683 o_tree
, a_tree
, b_tree
, entries
);
2685 re
->dst_entry
= item
->util
;
2686 item
= string_list_insert(renames
, pair
->one
->path
);
2689 apply_directory_rename_modifications(opt
, pair
, new_path
,
2695 hashmap_for_each_entry(&collisions
, &iter
, e
,
2696 ent
/* member name */) {
2697 free(e
->target_file
);
2698 string_list_clear(&e
->source_files
, 0);
2700 hashmap_clear_and_free(&collisions
, struct collision_entry
, ent
);
2704 static int process_renames(struct merge_options
*opt
,
2705 struct string_list
*a_renames
,
2706 struct string_list
*b_renames
)
2708 int clean_merge
= 1, i
, j
;
2709 struct string_list a_by_dst
= STRING_LIST_INIT_NODUP
;
2710 struct string_list b_by_dst
= STRING_LIST_INIT_NODUP
;
2711 const struct rename
*sre
;
2714 * FIXME: As string-list.h notes, it's O(n^2) to build a sorted
2715 * string_list one-by-one, but O(n log n) to build it unsorted and
2716 * then sort it. Note that as we build the list, we do not need to
2717 * check if the existing destination path is already in the list,
2718 * because the structure of diffcore_rename guarantees we won't
2721 for (i
= 0; i
< a_renames
->nr
; i
++) {
2722 sre
= a_renames
->items
[i
].util
;
2723 string_list_insert(&a_by_dst
, sre
->pair
->two
->path
)->util
2726 for (i
= 0; i
< b_renames
->nr
; i
++) {
2727 sre
= b_renames
->items
[i
].util
;
2728 string_list_insert(&b_by_dst
, sre
->pair
->two
->path
)->util
2732 for (i
= 0, j
= 0; i
< a_renames
->nr
|| j
< b_renames
->nr
;) {
2733 struct string_list
*renames1
, *renames2Dst
;
2734 struct rename
*ren1
= NULL
, *ren2
= NULL
;
2735 const char *ren1_src
, *ren1_dst
;
2736 struct string_list_item
*lookup
;
2738 if (i
>= a_renames
->nr
) {
2739 ren2
= b_renames
->items
[j
++].util
;
2740 } else if (j
>= b_renames
->nr
) {
2741 ren1
= a_renames
->items
[i
++].util
;
2743 int compare
= strcmp(a_renames
->items
[i
].string
,
2744 b_renames
->items
[j
].string
);
2746 ren1
= a_renames
->items
[i
++].util
;
2748 ren2
= b_renames
->items
[j
++].util
;
2751 /* TODO: refactor, so that 1/2 are not needed */
2753 renames1
= a_renames
;
2754 renames2Dst
= &b_by_dst
;
2756 renames1
= b_renames
;
2757 renames2Dst
= &a_by_dst
;
2761 if (ren1
->processed
)
2763 ren1
->processed
= 1;
2764 ren1
->dst_entry
->processed
= 1;
2765 /* BUG: We should only mark src_entry as processed if we
2766 * are not dealing with a rename + add-source case.
2768 ren1
->src_entry
->processed
= 1;
2770 ren1_src
= ren1
->pair
->one
->path
;
2771 ren1_dst
= ren1
->pair
->two
->path
;
2774 /* One file renamed on both sides */
2775 const char *ren2_src
= ren2
->pair
->one
->path
;
2776 const char *ren2_dst
= ren2
->pair
->two
->path
;
2777 enum rename_type rename_type
;
2778 if (strcmp(ren1_src
, ren2_src
) != 0)
2779 BUG("ren1_src != ren2_src");
2780 ren2
->dst_entry
->processed
= 1;
2781 ren2
->processed
= 1;
2782 if (strcmp(ren1_dst
, ren2_dst
) != 0) {
2783 rename_type
= RENAME_ONE_FILE_TO_TWO
;
2786 rename_type
= RENAME_ONE_FILE_TO_ONE
;
2787 /* BUG: We should only remove ren1_src in
2788 * the base stage (think of rename +
2789 * add-source cases).
2791 remove_file(opt
, 1, ren1_src
, 1);
2792 update_entry(ren1
->dst_entry
,
2797 setup_rename_conflict_info(rename_type
, opt
, ren1
, ren2
);
2798 } else if ((lookup
= string_list_lookup(renames2Dst
, ren1_dst
))) {
2799 /* Two different files renamed to the same thing */
2801 ren2
= lookup
->util
;
2802 ren2_dst
= ren2
->pair
->two
->path
;
2803 if (strcmp(ren1_dst
, ren2_dst
) != 0)
2804 BUG("ren1_dst != ren2_dst");
2807 ren2
->processed
= 1;
2809 * BUG: We should only mark src_entry as processed
2810 * if we are not dealing with a rename + add-source
2813 ren2
->src_entry
->processed
= 1;
2815 setup_rename_conflict_info(RENAME_TWO_FILES_TO_ONE
,
2818 /* Renamed in 1, maybe changed in 2 */
2819 /* we only use sha1 and mode of these */
2820 struct diff_filespec src_other
, dst_other
;
2824 * unpack_trees loads entries from common-commit
2825 * into stage 1, from head-commit into stage 2, and
2826 * from merge-commit into stage 3. We keep track
2827 * of which side corresponds to the rename.
2829 int renamed_stage
= a_renames
== renames1
? 2 : 3;
2830 int other_stage
= a_renames
== renames1
? 3 : 2;
2833 * Directory renames have a funny corner case...
2835 int renamed_to_self
= !strcmp(ren1_src
, ren1_dst
);
2837 /* BUG: We should only remove ren1_src in the base
2838 * stage and in other_stage (think of rename +
2841 if (!renamed_to_self
)
2842 remove_file(opt
, 1, ren1_src
,
2843 renamed_stage
== 2 ||
2844 !was_tracked(opt
, ren1_src
));
2846 oidcpy(&src_other
.oid
,
2847 &ren1
->src_entry
->stages
[other_stage
].oid
);
2848 src_other
.mode
= ren1
->src_entry
->stages
[other_stage
].mode
;
2849 oidcpy(&dst_other
.oid
,
2850 &ren1
->dst_entry
->stages
[other_stage
].oid
);
2851 dst_other
.mode
= ren1
->dst_entry
->stages
[other_stage
].mode
;
2854 if (oideq(&src_other
.oid
, null_oid()) &&
2855 ren1
->dir_rename_original_type
== 'A') {
2856 setup_rename_conflict_info(RENAME_VIA_DIR
,
2858 } else if (renamed_to_self
) {
2859 setup_rename_conflict_info(RENAME_NORMAL
,
2861 } else if (oideq(&src_other
.oid
, null_oid())) {
2862 setup_rename_conflict_info(RENAME_DELETE
,
2864 } else if ((dst_other
.mode
== ren1
->pair
->two
->mode
) &&
2865 oideq(&dst_other
.oid
, &ren1
->pair
->two
->oid
)) {
2867 * Added file on the other side identical to
2868 * the file being renamed: clean merge.
2869 * Also, there is no need to overwrite the
2870 * file already in the working copy, so call
2871 * update_file_flags() instead of
2874 if (update_file_flags(opt
,
2877 1, /* update_cache */
2880 } else if (!oideq(&dst_other
.oid
, null_oid())) {
2882 * Probably not a clean merge, but it's
2883 * premature to set clean_merge to 0 here,
2884 * because if the rename merges cleanly and
2885 * the merge exactly matches the newly added
2886 * file, then the merge will be clean.
2888 setup_rename_conflict_info(RENAME_ADD
,
2893 if (clean_merge
< 0)
2894 goto cleanup_and_return
;
2896 struct diff_filespec
*o
, *a
, *b
;
2897 src_other
.path
= (char *)ren1_src
;
2899 o
= ren1
->pair
->one
;
2900 if (a_renames
== renames1
) {
2901 a
= ren1
->pair
->two
;
2904 b
= ren1
->pair
->two
;
2907 update_entry(ren1
->dst_entry
, o
, a
, b
);
2908 setup_rename_conflict_info(RENAME_NORMAL
,
2914 string_list_clear(&a_by_dst
, 0);
2915 string_list_clear(&b_by_dst
, 0);
2920 struct rename_info
{
2921 struct string_list
*head_renames
;
2922 struct string_list
*merge_renames
;
2925 static void initial_cleanup_rename(struct diff_queue_struct
*pairs
,
2926 struct hashmap
*dir_renames
)
2928 struct hashmap_iter iter
;
2929 struct dir_rename_entry
*e
;
2931 hashmap_for_each_entry(dir_renames
, &iter
, e
,
2932 ent
/* member name */) {
2934 strbuf_release(&e
->new_dir
);
2935 /* possible_new_dirs already cleared in get_directory_renames */
2937 hashmap_clear_and_free(dir_renames
, struct dir_rename_entry
, ent
);
2944 static int detect_and_process_renames(struct merge_options
*opt
,
2945 struct tree
*common
,
2948 struct string_list
*entries
,
2949 struct rename_info
*ri
)
2951 struct diff_queue_struct
*head_pairs
, *merge_pairs
;
2952 struct hashmap
*dir_re_head
, *dir_re_merge
;
2955 ri
->head_renames
= NULL
;
2956 ri
->merge_renames
= NULL
;
2958 if (!merge_detect_rename(opt
))
2961 head_pairs
= get_diffpairs(opt
, common
, head
);
2962 merge_pairs
= get_diffpairs(opt
, common
, merge
);
2964 if ((opt
->detect_directory_renames
== MERGE_DIRECTORY_RENAMES_TRUE
) ||
2965 (opt
->detect_directory_renames
== MERGE_DIRECTORY_RENAMES_CONFLICT
&&
2966 !opt
->priv
->call_depth
)) {
2967 dir_re_head
= get_directory_renames(head_pairs
);
2968 dir_re_merge
= get_directory_renames(merge_pairs
);
2970 handle_directory_level_conflicts(opt
,
2972 dir_re_merge
, merge
);
2974 dir_re_head
= xmalloc(sizeof(*dir_re_head
));
2975 dir_re_merge
= xmalloc(sizeof(*dir_re_merge
));
2976 dir_rename_init(dir_re_head
);
2977 dir_rename_init(dir_re_merge
);
2980 ri
->head_renames
= get_renames(opt
, opt
->branch1
, head_pairs
,
2981 dir_re_merge
, dir_re_head
, head
,
2982 common
, head
, merge
, entries
,
2986 ri
->merge_renames
= get_renames(opt
, opt
->branch2
, merge_pairs
,
2987 dir_re_head
, dir_re_merge
, merge
,
2988 common
, head
, merge
, entries
,
2992 clean
&= process_renames(opt
, ri
->head_renames
, ri
->merge_renames
);
2996 * Some cleanup is deferred until cleanup_renames() because the
2997 * data structures are still needed and referenced in
2998 * process_entry(). But there are a few things we can free now.
3000 initial_cleanup_rename(head_pairs
, dir_re_head
);
3001 initial_cleanup_rename(merge_pairs
, dir_re_merge
);
3006 static void final_cleanup_rename(struct string_list
*rename
)
3008 const struct rename
*re
;
3014 for (i
= 0; i
< rename
->nr
; i
++) {
3015 re
= rename
->items
[i
].util
;
3016 diff_free_filepair(re
->pair
);
3018 string_list_clear(rename
, 1);
3022 static void final_cleanup_renames(struct rename_info
*re_info
)
3024 final_cleanup_rename(re_info
->head_renames
);
3025 final_cleanup_rename(re_info
->merge_renames
);
3028 static int read_oid_strbuf(struct merge_options
*opt
,
3029 const struct object_id
*oid
,
3033 enum object_type type
;
3035 buf
= repo_read_object_file(the_repository
, oid
, &type
, &size
);
3037 return err(opt
, _("cannot read object %s"), oid_to_hex(oid
));
3038 if (type
!= OBJ_BLOB
) {
3040 return err(opt
, _("object %s is not a blob"), oid_to_hex(oid
));
3042 strbuf_attach(dst
, buf
, size
, size
+ 1);
3046 static int blob_unchanged(struct merge_options
*opt
,
3047 const struct diff_filespec
*o
,
3048 const struct diff_filespec
*a
,
3049 int renormalize
, const char *path
)
3051 struct strbuf obuf
= STRBUF_INIT
;
3052 struct strbuf abuf
= STRBUF_INIT
;
3053 int ret
= 0; /* assume changed for safety */
3054 struct index_state
*idx
= opt
->repo
->index
;
3056 if (a
->mode
!= o
->mode
)
3058 if (oideq(&o
->oid
, &a
->oid
))
3063 if (read_oid_strbuf(opt
, &o
->oid
, &obuf
) ||
3064 read_oid_strbuf(opt
, &a
->oid
, &abuf
))
3067 * Note: binary | is used so that both renormalizations are
3068 * performed. Comparison can be skipped if both files are
3069 * unchanged since their sha1s have already been compared.
3071 if (renormalize_buffer(idx
, path
, obuf
.buf
, obuf
.len
, &obuf
) |
3072 renormalize_buffer(idx
, path
, abuf
.buf
, abuf
.len
, &abuf
))
3073 ret
= (obuf
.len
== abuf
.len
&& !memcmp(obuf
.buf
, abuf
.buf
, obuf
.len
));
3076 strbuf_release(&obuf
);
3077 strbuf_release(&abuf
);
3081 static int handle_modify_delete(struct merge_options
*opt
,
3083 const struct diff_filespec
*o
,
3084 const struct diff_filespec
*a
,
3085 const struct diff_filespec
*b
)
3087 const char *modify_branch
, *delete_branch
;
3088 const struct diff_filespec
*changed
;
3091 modify_branch
= opt
->branch1
;
3092 delete_branch
= opt
->branch2
;
3095 modify_branch
= opt
->branch2
;
3096 delete_branch
= opt
->branch1
;
3100 return handle_change_delete(opt
,
3103 modify_branch
, delete_branch
,
3104 _("modify"), _("modified"));
3107 static int handle_content_merge(struct merge_file_info
*mfi
,
3108 struct merge_options
*opt
,
3111 const struct diff_filespec
*o
,
3112 const struct diff_filespec
*a
,
3113 const struct diff_filespec
*b
,
3114 struct rename_conflict_info
*ci
)
3116 const char *reason
= _("content");
3117 unsigned df_conflict_remains
= 0;
3120 reason
= _("add/add");
3122 assert(o
->path
&& a
->path
&& b
->path
);
3123 if (ci
&& dir_in_way(opt
->repo
->index
, path
, !opt
->priv
->call_depth
,
3124 S_ISGITLINK(ci
->ren1
->pair
->two
->mode
)))
3125 df_conflict_remains
= 1;
3127 if (merge_mode_and_contents(opt
, o
, a
, b
, path
,
3128 opt
->branch1
, opt
->branch2
,
3129 opt
->priv
->call_depth
* 2, mfi
))
3133 * We can skip updating the working tree file iff:
3134 * a) The merge is clean
3135 * b) The merge matches what was in HEAD (content, mode, pathname)
3136 * c) The target path is usable (i.e. not involved in D/F conflict)
3138 if (mfi
->clean
&& was_tracked_and_matches(opt
, path
, &mfi
->blob
) &&
3139 !df_conflict_remains
) {
3141 struct cache_entry
*ce
;
3143 output(opt
, 3, _("Skipped %s (merged same as existing)"), path
);
3144 if (add_cacheinfo(opt
, &mfi
->blob
, path
,
3145 0, (!opt
->priv
->call_depth
&& !is_dirty
), 0))
3148 * However, add_cacheinfo() will delete the old cache entry
3149 * and add a new one. We need to copy over any skip_worktree
3150 * flag to avoid making the file appear as if it were
3151 * deleted by the user.
3153 pos
= index_name_pos(&opt
->priv
->orig_index
, path
, strlen(path
));
3154 ce
= opt
->priv
->orig_index
.cache
[pos
];
3155 if (ce_skip_worktree(ce
)) {
3156 pos
= index_name_pos(opt
->repo
->index
, path
, strlen(path
));
3157 ce
= opt
->repo
->index
->cache
[pos
];
3158 ce
->ce_flags
|= CE_SKIP_WORKTREE
;
3164 if (S_ISGITLINK(mfi
->blob
.mode
))
3165 reason
= _("submodule");
3166 output(opt
, 1, _("CONFLICT (%s): Merge conflict in %s"),
3168 if (ci
&& !df_conflict_remains
)
3169 if (update_stages(opt
, path
, o
, a
, b
))
3173 if (df_conflict_remains
|| is_dirty
) {
3175 if (opt
->priv
->call_depth
) {
3176 remove_file_from_index(opt
->repo
->index
, path
);
3179 if (update_stages(opt
, path
, o
, a
, b
))
3182 int file_from_stage2
= was_tracked(opt
, path
);
3184 if (update_stages(opt
, path
, NULL
,
3185 file_from_stage2
? &mfi
->blob
: NULL
,
3186 file_from_stage2
? NULL
: &mfi
->blob
))
3191 new_path
= unique_path(opt
, path
, ci
->ren1
->branch
);
3193 output(opt
, 1, _("Refusing to lose dirty file at %s"),
3196 output(opt
, 1, _("Adding as %s instead"), new_path
);
3197 if (update_file(opt
, 0, &mfi
->blob
, new_path
)) {
3203 } else if (update_file(opt
, mfi
->clean
, &mfi
->blob
, path
))
3205 return !is_dirty
&& mfi
->clean
;
3208 static int handle_rename_normal(struct merge_options
*opt
,
3210 const struct diff_filespec
*o
,
3211 const struct diff_filespec
*a
,
3212 const struct diff_filespec
*b
,
3213 struct rename_conflict_info
*ci
)
3215 struct rename
*ren
= ci
->ren1
;
3216 struct merge_file_info mfi
;
3219 /* Merge the content and write it out */
3220 clean
= handle_content_merge(&mfi
, opt
, path
, was_dirty(opt
, path
),
3224 opt
->detect_directory_renames
== MERGE_DIRECTORY_RENAMES_CONFLICT
&&
3225 ren
->dir_rename_original_dest
) {
3226 if (update_stages(opt
, path
,
3227 &mfi
.blob
, &mfi
.blob
, &mfi
.blob
))
3229 clean
= 0; /* not clean, but conflicted */
3234 static void dir_rename_warning(const char *msg
,
3237 struct merge_options
*opt
,
3240 const char *other_branch
;
3241 other_branch
= (ren
->branch
== opt
->branch1
?
3242 opt
->branch2
: opt
->branch1
);
3244 output(opt
, clean
? 2 : 1, msg
,
3245 ren
->pair
->one
->path
, ren
->branch
,
3246 other_branch
, ren
->pair
->two
->path
);
3249 output(opt
, clean
? 2 : 1, msg
,
3250 ren
->pair
->one
->path
, ren
->dir_rename_original_dest
, ren
->branch
,
3251 other_branch
, ren
->pair
->two
->path
);
3253 static int warn_about_dir_renamed_entries(struct merge_options
*opt
,
3257 int clean
= 1, is_add
;
3262 /* Return early if ren was not affected/created by a directory rename */
3263 if (!ren
->dir_rename_original_dest
)
3267 assert(opt
->detect_directory_renames
> MERGE_DIRECTORY_RENAMES_NONE
);
3268 assert(ren
->dir_rename_original_type
== 'A' ||
3269 ren
->dir_rename_original_type
== 'R');
3271 /* Check whether to treat directory renames as a conflict */
3272 clean
= (opt
->detect_directory_renames
== MERGE_DIRECTORY_RENAMES_TRUE
);
3274 is_add
= (ren
->dir_rename_original_type
== 'A');
3275 if (ren
->dir_rename_original_type
== 'A' && clean
) {
3276 msg
= _("Path updated: %s added in %s inside a "
3277 "directory that was renamed in %s; moving it to %s.");
3278 } else if (ren
->dir_rename_original_type
== 'A' && !clean
) {
3279 msg
= _("CONFLICT (file location): %s added in %s "
3280 "inside a directory that was renamed in %s, "
3281 "suggesting it should perhaps be moved to %s.");
3282 } else if (ren
->dir_rename_original_type
== 'R' && clean
) {
3283 msg
= _("Path updated: %s renamed to %s in %s, inside a "
3284 "directory that was renamed in %s; moving it to %s.");
3285 } else if (ren
->dir_rename_original_type
== 'R' && !clean
) {
3286 msg
= _("CONFLICT (file location): %s renamed to %s in %s, "
3287 "inside a directory that was renamed in %s, "
3288 "suggesting it should perhaps be moved to %s.");
3290 BUG("Impossible dir_rename_original_type/clean combination");
3292 dir_rename_warning(msg
, is_add
, clean
, opt
, ren
);
3297 /* Per entry merge function */
3298 static int process_entry(struct merge_options
*opt
,
3299 const char *path
, struct stage_data
*entry
)
3301 int clean_merge
= 1;
3302 int normalize
= opt
->renormalize
;
3304 struct diff_filespec
*o
= &entry
->stages
[1];
3305 struct diff_filespec
*a
= &entry
->stages
[2];
3306 struct diff_filespec
*b
= &entry
->stages
[3];
3307 int o_valid
= is_valid(o
);
3308 int a_valid
= is_valid(a
);
3309 int b_valid
= is_valid(b
);
3310 o
->path
= a
->path
= b
->path
= (char*)path
;
3312 entry
->processed
= 1;
3313 if (entry
->rename_conflict_info
) {
3314 struct rename_conflict_info
*ci
= entry
->rename_conflict_info
;
3315 struct diff_filespec
*temp
;
3318 path_clean
= warn_about_dir_renamed_entries(opt
, ci
->ren1
);
3319 path_clean
&= warn_about_dir_renamed_entries(opt
, ci
->ren2
);
3322 * For cases with a single rename, {o,a,b}->path have all been
3323 * set to the rename target path; we need to set two of these
3324 * back to the rename source.
3325 * For rename/rename conflicts, we'll manually fix paths below.
3327 temp
= (opt
->branch1
== ci
->ren1
->branch
) ? b
: a
;
3328 o
->path
= temp
->path
= ci
->ren1
->pair
->one
->path
;
3330 assert(opt
->branch1
== ci
->ren1
->branch
);
3333 switch (ci
->rename_type
) {
3335 case RENAME_ONE_FILE_TO_ONE
:
3336 clean_merge
= handle_rename_normal(opt
, path
, o
, a
, b
,
3339 case RENAME_VIA_DIR
:
3340 clean_merge
= handle_rename_via_dir(opt
, ci
);
3344 * Probably unclean merge, but if the renamed file
3345 * merges cleanly and the result can then be
3346 * two-way merged cleanly with the added file, I
3347 * guess it's a clean merge?
3349 clean_merge
= handle_rename_add(opt
, ci
);
3353 if (handle_rename_delete(opt
, ci
))
3356 case RENAME_ONE_FILE_TO_TWO
:
3358 * Manually fix up paths; note:
3359 * ren[12]->pair->one->path are equal.
3361 o
->path
= ci
->ren1
->pair
->one
->path
;
3362 a
->path
= ci
->ren1
->pair
->two
->path
;
3363 b
->path
= ci
->ren2
->pair
->two
->path
;
3366 if (handle_rename_rename_1to2(opt
, ci
))
3369 case RENAME_TWO_FILES_TO_ONE
:
3371 * Manually fix up paths; note,
3372 * ren[12]->pair->two->path are actually equal.
3375 a
->path
= ci
->ren1
->pair
->two
->path
;
3376 b
->path
= ci
->ren2
->pair
->two
->path
;
3379 * Probably unclean merge, but if the two renamed
3380 * files merge cleanly and the two resulting files
3381 * can then be two-way merged cleanly, I guess it's
3384 clean_merge
= handle_rename_rename_2to1(opt
, ci
);
3387 entry
->processed
= 0;
3390 if (path_clean
< clean_merge
)
3391 clean_merge
= path_clean
;
3392 } else if (o_valid
&& (!a_valid
|| !b_valid
)) {
3393 /* Case A: Deleted in one */
3394 if ((!a_valid
&& !b_valid
) ||
3395 (!b_valid
&& blob_unchanged(opt
, o
, a
, normalize
, path
)) ||
3396 (!a_valid
&& blob_unchanged(opt
, o
, b
, normalize
, path
))) {
3397 /* Deleted in both or deleted in one and
3398 * unchanged in the other */
3400 output(opt
, 2, _("Removing %s"), path
);
3401 /* do not touch working file if it did not exist */
3402 remove_file(opt
, 1, path
, !a_valid
);
3404 /* Modify/delete; deleted side may have put a directory in the way */
3406 if (handle_modify_delete(opt
, path
, o
, a
, b
))
3409 } else if ((!o_valid
&& a_valid
&& !b_valid
) ||
3410 (!o_valid
&& !a_valid
&& b_valid
)) {
3411 /* Case B: Added in one. */
3412 /* [nothing|directory] -> ([nothing|directory], file) */
3414 const char *add_branch
;
3415 const char *other_branch
;
3417 const struct diff_filespec
*contents
;
3420 add_branch
= opt
->branch1
;
3421 other_branch
= opt
->branch2
;
3423 conf
= _("file/directory");
3425 add_branch
= opt
->branch2
;
3426 other_branch
= opt
->branch1
;
3428 conf
= _("directory/file");
3430 if (dir_in_way(opt
->repo
->index
, path
,
3431 !opt
->priv
->call_depth
&& !S_ISGITLINK(a
->mode
),
3433 char *new_path
= unique_path(opt
, path
, add_branch
);
3435 output(opt
, 1, _("CONFLICT (%s): There is a directory with name %s in %s. "
3437 conf
, path
, other_branch
, path
, new_path
);
3438 if (update_file(opt
, 0, contents
, new_path
))
3440 else if (opt
->priv
->call_depth
)
3441 remove_file_from_index(opt
->repo
->index
, path
);
3444 output(opt
, 2, _("Adding %s"), path
);
3445 /* do not overwrite file if already present */
3446 if (update_file_flags(opt
, contents
, path
, 1, !a_valid
))
3449 } else if (a_valid
&& b_valid
) {
3451 /* Case C: Added in both (check for same permissions) */
3453 _("CONFLICT (add/add): Merge conflict in %s"),
3455 clean_merge
= handle_file_collision(opt
,
3461 /* case D: Modified in both, but differently. */
3462 struct merge_file_info mfi
;
3463 int is_dirty
= 0; /* unpack_trees would have bailed if dirty */
3464 clean_merge
= handle_content_merge(&mfi
, opt
, path
,
3468 } else if (!o_valid
&& !a_valid
&& !b_valid
) {
3470 * this entry was deleted altogether. a_mode == 0 means
3471 * we had that path and want to actively remove it.
3473 remove_file(opt
, 1, path
, !a
->mode
);
3475 BUG("fatal merge failure, shouldn't happen.");
3480 static int merge_trees_internal(struct merge_options
*opt
,
3483 struct tree
*merge_base
,
3484 struct tree
**result
)
3486 struct index_state
*istate
= opt
->repo
->index
;
3489 if (opt
->subtree_shift
) {
3490 merge
= shift_tree_object(opt
->repo
, head
, merge
,
3491 opt
->subtree_shift
);
3492 merge_base
= shift_tree_object(opt
->repo
, head
, merge_base
,
3493 opt
->subtree_shift
);
3496 if (oideq(&merge_base
->object
.oid
, &merge
->object
.oid
)) {
3497 output(opt
, 0, _("Already up to date."));
3502 code
= unpack_trees_start(opt
, merge_base
, head
, merge
);
3505 if (show(opt
, 4) || opt
->priv
->call_depth
)
3506 err(opt
, _("merging of trees %s and %s failed"),
3507 oid_to_hex(&head
->object
.oid
),
3508 oid_to_hex(&merge
->object
.oid
));
3509 unpack_trees_finish(opt
);
3513 if (unmerged_index(istate
)) {
3514 struct string_list
*entries
;
3515 struct rename_info re_info
;
3518 * Only need the hashmap while processing entries, so
3519 * initialize it here and free it when we are done running
3520 * through the entries. Keeping it in the merge_options as
3521 * opposed to decaring a local hashmap is for convenience
3522 * so that we don't have to pass it to around.
3524 hashmap_init(&opt
->priv
->current_file_dir_set
, path_hashmap_cmp
,
3526 get_files_dirs(opt
, head
);
3527 get_files_dirs(opt
, merge
);
3529 entries
= get_unmerged(opt
->repo
->index
);
3530 clean
= detect_and_process_renames(opt
, merge_base
, head
, merge
,
3532 record_df_conflict_files(opt
, entries
);
3535 for (i
= entries
->nr
-1; 0 <= i
; i
--) {
3536 const char *path
= entries
->items
[i
].string
;
3537 struct stage_data
*e
= entries
->items
[i
].util
;
3538 if (!e
->processed
) {
3539 int ret
= process_entry(opt
, path
, e
);
3548 for (i
= 0; i
< entries
->nr
; i
++) {
3549 struct stage_data
*e
= entries
->items
[i
].util
;
3551 BUG("unprocessed path??? %s",
3552 entries
->items
[i
].string
);
3556 final_cleanup_renames(&re_info
);
3558 string_list_clear(entries
, 1);
3561 hashmap_clear_and_free(&opt
->priv
->current_file_dir_set
,
3562 struct path_hashmap_entry
, e
);
3565 unpack_trees_finish(opt
);
3572 unpack_trees_finish(opt
);
3574 if (opt
->priv
->call_depth
&&
3575 !(*result
= write_in_core_index_as_tree(opt
->repo
)))
3582 * Merge the commits h1 and h2, returning a flag (int) indicating the
3583 * cleanness of the merge. Also, if opt->priv->call_depth, create a
3584 * virtual commit and write its location to *result.
3586 static int merge_recursive_internal(struct merge_options
*opt
,
3589 struct commit_list
*merge_bases
,
3590 struct commit
**result
)
3592 struct commit_list
*iter
;
3593 struct commit
*merged_merge_bases
;
3594 struct tree
*result_tree
;
3596 const char *ancestor_name
;
3597 struct strbuf merge_base_abbrev
= STRBUF_INIT
;
3600 output(opt
, 4, _("Merging:"));
3601 output_commit_title(opt
, h1
);
3602 output_commit_title(opt
, h2
);
3606 merge_bases
= repo_get_merge_bases(the_repository
, h1
, h2
);
3607 merge_bases
= reverse_commit_list(merge_bases
);
3611 unsigned cnt
= commit_list_count(merge_bases
);
3613 output(opt
, 5, Q_("found %u common ancestor:",
3614 "found %u common ancestors:", cnt
), cnt
);
3615 for (iter
= merge_bases
; iter
; iter
= iter
->next
)
3616 output_commit_title(opt
, iter
->item
);
3619 merged_merge_bases
= pop_commit(&merge_bases
);
3620 if (!merged_merge_bases
) {
3621 /* if there is no common ancestor, use an empty tree */
3624 tree
= lookup_tree(opt
->repo
, opt
->repo
->hash_algo
->empty_tree
);
3625 merged_merge_bases
= make_virtual_commit(opt
->repo
, tree
,
3627 ancestor_name
= "empty tree";
3628 } else if (opt
->ancestor
&& !opt
->priv
->call_depth
) {
3629 ancestor_name
= opt
->ancestor
;
3630 } else if (merge_bases
) {
3631 ancestor_name
= "merged common ancestors";
3633 strbuf_add_unique_abbrev(&merge_base_abbrev
,
3634 &merged_merge_bases
->object
.oid
,
3636 ancestor_name
= merge_base_abbrev
.buf
;
3639 for (iter
= merge_bases
; iter
; iter
= iter
->next
) {
3640 const char *saved_b1
, *saved_b2
;
3641 opt
->priv
->call_depth
++;
3643 * When the merge fails, the result contains files
3644 * with conflict markers. The cleanness flag is
3645 * ignored (unless indicating an error), it was never
3646 * actually used, as result of merge_trees has always
3647 * overwritten it: the committed "conflicts" were
3650 discard_index(opt
->repo
->index
);
3651 saved_b1
= opt
->branch1
;
3652 saved_b2
= opt
->branch2
;
3653 opt
->branch1
= "Temporary merge branch 1";
3654 opt
->branch2
= "Temporary merge branch 2";
3655 if (merge_recursive_internal(opt
, merged_merge_bases
, iter
->item
,
3656 NULL
, &merged_merge_bases
) < 0)
3658 opt
->branch1
= saved_b1
;
3659 opt
->branch2
= saved_b2
;
3660 opt
->priv
->call_depth
--;
3662 if (!merged_merge_bases
)
3663 return err(opt
, _("merge returned no commit"));
3667 * FIXME: Since merge_recursive_internal() is only ever called by
3668 * places that ensure the index is loaded first
3669 * (e.g. builtin/merge.c, rebase/sequencer, etc.), in the common
3670 * case where the merge base was unique that means when we get here
3671 * we immediately discard the index and re-read it, which is a
3672 * complete waste of time. We should only be discarding and
3673 * re-reading if we were forced to recurse.
3675 discard_index(opt
->repo
->index
);
3676 if (!opt
->priv
->call_depth
)
3677 repo_read_index(opt
->repo
);
3679 opt
->ancestor
= ancestor_name
;
3680 clean
= merge_trees_internal(opt
,
3681 repo_get_commit_tree(opt
->repo
, h1
),
3682 repo_get_commit_tree(opt
->repo
, h2
),
3683 repo_get_commit_tree(opt
->repo
,
3684 merged_merge_bases
),
3686 strbuf_release(&merge_base_abbrev
);
3687 opt
->ancestor
= NULL
; /* avoid accidental re-use of opt->ancestor */
3693 if (opt
->priv
->call_depth
) {
3694 *result
= make_virtual_commit(opt
->repo
, result_tree
,
3696 commit_list_insert(h1
, &(*result
)->parents
);
3697 commit_list_insert(h2
, &(*result
)->parents
->next
);
3702 static int merge_start(struct merge_options
*opt
, struct tree
*head
)
3704 struct strbuf sb
= STRBUF_INIT
;
3706 /* Sanity checks on opt */
3709 assert(opt
->branch1
&& opt
->branch2
);
3711 assert(opt
->detect_renames
>= -1 &&
3712 opt
->detect_renames
<= DIFF_DETECT_COPY
);
3713 assert(opt
->detect_directory_renames
>= MERGE_DIRECTORY_RENAMES_NONE
&&
3714 opt
->detect_directory_renames
<= MERGE_DIRECTORY_RENAMES_TRUE
);
3715 assert(opt
->rename_limit
>= -1);
3716 assert(opt
->rename_score
>= 0 && opt
->rename_score
<= MAX_SCORE
);
3717 assert(opt
->show_rename_progress
>= 0 && opt
->show_rename_progress
<= 1);
3719 assert(opt
->xdl_opts
>= 0);
3720 assert(opt
->recursive_variant
>= MERGE_VARIANT_NORMAL
&&
3721 opt
->recursive_variant
<= MERGE_VARIANT_THEIRS
);
3723 assert(opt
->verbosity
>= 0 && opt
->verbosity
<= 5);
3724 assert(opt
->buffer_output
<= 2);
3725 assert(opt
->obuf
.len
== 0);
3727 assert(opt
->priv
== NULL
);
3729 /* Not supported; option specific to merge-ort */
3730 assert(!opt
->record_conflict_msgs_as_headers
);
3731 assert(!opt
->msg_header_prefix
);
3733 /* Sanity check on repo state; index must match head */
3734 if (repo_index_has_changes(opt
->repo
, head
, &sb
)) {
3735 err(opt
, _("Your local changes to the following files would be overwritten by merge:\n %s"),
3737 strbuf_release(&sb
);
3741 CALLOC_ARRAY(opt
->priv
, 1);
3742 string_list_init_dup(&opt
->priv
->df_conflict_file_set
);
3746 static void merge_finalize(struct merge_options
*opt
)
3749 if (!opt
->priv
->call_depth
&& opt
->buffer_output
< 2)
3750 strbuf_release(&opt
->obuf
);
3752 diff_warn_rename_limit("merge.renamelimit",
3753 opt
->priv
->needed_rename_limit
, 0);
3754 FREE_AND_NULL(opt
->priv
);
3757 int merge_trees(struct merge_options
*opt
,
3760 struct tree
*merge_base
)
3763 struct tree
*ignored
;
3765 assert(opt
->ancestor
!= NULL
);
3767 if (merge_start(opt
, head
))
3769 clean
= merge_trees_internal(opt
, head
, merge
, merge_base
, &ignored
);
3770 merge_finalize(opt
);
3775 int merge_recursive(struct merge_options
*opt
,
3778 struct commit_list
*merge_bases
,
3779 struct commit
**result
)
3783 assert(opt
->ancestor
== NULL
||
3784 !strcmp(opt
->ancestor
, "constructed merge base"));
3786 prepare_repo_settings(opt
->repo
);
3787 opt
->repo
->settings
.command_requires_full_index
= 1;
3789 if (merge_start(opt
, repo_get_commit_tree(opt
->repo
, h1
)))
3791 clean
= merge_recursive_internal(opt
, h1
, h2
, merge_bases
, result
);
3792 merge_finalize(opt
);
3797 static struct commit
*get_ref(struct repository
*repo
,
3798 const struct object_id
*oid
,
3801 struct object
*object
;
3803 object
= deref_tag(repo
, parse_object(repo
, oid
),
3804 name
, strlen(name
));
3807 if (object
->type
== OBJ_TREE
)
3808 return make_virtual_commit(repo
, (struct tree
*)object
, name
);
3809 if (object
->type
!= OBJ_COMMIT
)
3811 if (repo_parse_commit(repo
, (struct commit
*)object
))
3813 return (struct commit
*)object
;
3816 int merge_recursive_generic(struct merge_options
*opt
,
3817 const struct object_id
*head
,
3818 const struct object_id
*merge
,
3819 int num_merge_bases
,
3820 const struct object_id
**merge_bases
,
3821 struct commit
**result
)
3824 struct lock_file lock
= LOCK_INIT
;
3825 struct commit
*head_commit
= get_ref(opt
->repo
, head
, opt
->branch1
);
3826 struct commit
*next_commit
= get_ref(opt
->repo
, merge
, opt
->branch2
);
3827 struct commit_list
*ca
= NULL
;
3831 for (i
= 0; i
< num_merge_bases
; ++i
) {
3832 struct commit
*base
;
3833 if (!(base
= get_ref(opt
->repo
, merge_bases
[i
],
3834 oid_to_hex(merge_bases
[i
]))))
3835 return err(opt
, _("Could not parse object '%s'"),
3836 oid_to_hex(merge_bases
[i
]));
3837 commit_list_insert(base
, &ca
);
3839 if (num_merge_bases
== 1)
3840 opt
->ancestor
= "constructed merge base";
3843 repo_hold_locked_index(opt
->repo
, &lock
, LOCK_DIE_ON_ERROR
);
3844 clean
= merge_recursive(opt
, head_commit
, next_commit
, ca
,
3847 rollback_lock_file(&lock
);
3851 if (write_locked_index(opt
->repo
->index
, &lock
,
3852 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
3853 return err(opt
, _("Unable to write index."));
3855 return clean
? 0 : 1;
3858 static void merge_recursive_config(struct merge_options
*opt
)
3861 int renormalize
= 0;
3862 git_config_get_int("merge.verbosity", &opt
->verbosity
);
3863 git_config_get_int("diff.renamelimit", &opt
->rename_limit
);
3864 git_config_get_int("merge.renamelimit", &opt
->rename_limit
);
3865 git_config_get_bool("merge.renormalize", &renormalize
);
3866 opt
->renormalize
= renormalize
;
3867 if (!git_config_get_string("diff.renames", &value
)) {
3868 opt
->detect_renames
= git_config_rename("diff.renames", value
);
3871 if (!git_config_get_string("merge.renames", &value
)) {
3872 opt
->detect_renames
= git_config_rename("merge.renames", value
);
3875 if (!git_config_get_string("merge.directoryrenames", &value
)) {
3876 int boolval
= git_parse_maybe_bool(value
);
3878 opt
->detect_directory_renames
= boolval
?
3879 MERGE_DIRECTORY_RENAMES_TRUE
:
3880 MERGE_DIRECTORY_RENAMES_NONE
;
3881 } else if (!strcasecmp(value
, "conflict")) {
3882 opt
->detect_directory_renames
=
3883 MERGE_DIRECTORY_RENAMES_CONFLICT
;
3884 } /* avoid erroring on values from future versions of git */
3887 git_config(git_xmerge_config
, NULL
);
3890 void init_merge_options(struct merge_options
*opt
,
3891 struct repository
*repo
)
3893 const char *merge_verbosity
;
3894 memset(opt
, 0, sizeof(struct merge_options
));
3898 opt
->detect_renames
= -1;
3899 opt
->detect_directory_renames
= MERGE_DIRECTORY_RENAMES_CONFLICT
;
3900 opt
->rename_limit
= -1;
3903 opt
->buffer_output
= 1;
3904 strbuf_init(&opt
->obuf
, 0);
3906 opt
->renormalize
= 0;
3908 merge_recursive_config(opt
);
3909 merge_verbosity
= getenv("GIT_MERGE_VERBOSITY");
3910 if (merge_verbosity
)
3911 opt
->verbosity
= strtol(merge_verbosity
, NULL
, 10);
3912 if (opt
->verbosity
>= 5)
3913 opt
->buffer_output
= 0;
3916 int parse_merge_opt(struct merge_options
*opt
, const char *s
)
3922 if (!strcmp(s
, "ours"))
3923 opt
->recursive_variant
= MERGE_VARIANT_OURS
;
3924 else if (!strcmp(s
, "theirs"))
3925 opt
->recursive_variant
= MERGE_VARIANT_THEIRS
;
3926 else if (!strcmp(s
, "subtree"))
3927 opt
->subtree_shift
= "";
3928 else if (skip_prefix(s
, "subtree=", &arg
))
3929 opt
->subtree_shift
= arg
;
3930 else if (!strcmp(s
, "patience"))
3931 opt
->xdl_opts
= DIFF_WITH_ALG(opt
, PATIENCE_DIFF
);
3932 else if (!strcmp(s
, "histogram"))
3933 opt
->xdl_opts
= DIFF_WITH_ALG(opt
, HISTOGRAM_DIFF
);
3934 else if (skip_prefix(s
, "diff-algorithm=", &arg
)) {
3935 long value
= parse_algorithm_value(arg
);
3938 /* clear out previous settings */
3939 DIFF_XDL_CLR(opt
, NEED_MINIMAL
);
3940 opt
->xdl_opts
&= ~XDF_DIFF_ALGORITHM_MASK
;
3941 opt
->xdl_opts
|= value
;
3943 else if (!strcmp(s
, "ignore-space-change"))
3944 DIFF_XDL_SET(opt
, IGNORE_WHITESPACE_CHANGE
);
3945 else if (!strcmp(s
, "ignore-all-space"))
3946 DIFF_XDL_SET(opt
, IGNORE_WHITESPACE
);
3947 else if (!strcmp(s
, "ignore-space-at-eol"))
3948 DIFF_XDL_SET(opt
, IGNORE_WHITESPACE_AT_EOL
);
3949 else if (!strcmp(s
, "ignore-cr-at-eol"))
3950 DIFF_XDL_SET(opt
, IGNORE_CR_AT_EOL
);
3951 else if (!strcmp(s
, "renormalize"))
3952 opt
->renormalize
= 1;
3953 else if (!strcmp(s
, "no-renormalize"))
3954 opt
->renormalize
= 0;
3955 else if (!strcmp(s
, "no-renames"))
3956 opt
->detect_renames
= 0;
3957 else if (!strcmp(s
, "find-renames")) {
3958 opt
->detect_renames
= 1;
3959 opt
->rename_score
= 0;
3961 else if (skip_prefix(s
, "find-renames=", &arg
) ||
3962 skip_prefix(s
, "rename-threshold=", &arg
)) {
3963 if ((opt
->rename_score
= parse_rename_score(&arg
)) == -1 || *arg
!= 0)
3965 opt
->detect_renames
= 1;
3968 * Please update $__git_merge_strategy_options in
3969 * git-completion.bash when you add new options