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"
10 #include "cache-tree.h"
12 #include "commit-reach.h"
17 #include "environment.h"
22 #include "match-trees.h"
23 #include "name-hash.h"
24 #include "object-file.h"
25 #include "object-name.h"
26 #include "object-store-ll.h"
28 #include "repository.h"
30 #include "sparse-index.h"
31 #include "string-list.h"
34 #include "tree-walk.h"
35 #include "unpack-trees.h"
36 #include "xdiff-interface.h"
38 struct merge_options_internal
{
40 int needed_rename_limit
;
41 struct hashmap current_file_dir_set
;
42 struct string_list df_conflict_file_set
;
43 struct unpack_trees_options unpack_opts
;
44 struct index_state orig_index
;
47 struct path_hashmap_entry
{
48 struct hashmap_entry e
;
49 char path
[FLEX_ARRAY
];
52 static int path_hashmap_cmp(const void *cmp_data UNUSED
,
53 const struct hashmap_entry
*eptr
,
54 const struct hashmap_entry
*entry_or_key
,
57 const struct path_hashmap_entry
*a
, *b
;
58 const char *key
= keydata
;
60 a
= container_of(eptr
, const struct path_hashmap_entry
, e
);
61 b
= container_of(entry_or_key
, const struct path_hashmap_entry
, e
);
63 return fspathcmp(a
->path
, key
? key
: b
->path
);
67 * For dir_rename_entry, directory names are stored as a full path from the
68 * toplevel of the repository and do not include a trailing '/'. Also:
70 * dir: original name of directory being renamed
71 * non_unique_new_dir: if true, could not determine new_dir
72 * new_dir: final name of directory being renamed
73 * possible_new_dirs: temporary used to help determine new_dir; see comments
74 * in get_directory_renames() for details
76 struct dir_rename_entry
{
77 struct hashmap_entry ent
;
79 unsigned non_unique_new_dir
:1;
80 struct strbuf new_dir
;
81 struct string_list possible_new_dirs
;
84 static struct dir_rename_entry
*dir_rename_find_entry(struct hashmap
*hashmap
,
87 struct dir_rename_entry key
;
91 hashmap_entry_init(&key
.ent
, strhash(dir
));
93 return hashmap_get_entry(hashmap
, &key
, ent
, NULL
);
96 static int dir_rename_cmp(const void *cmp_data UNUSED
,
97 const struct hashmap_entry
*eptr
,
98 const struct hashmap_entry
*entry_or_key
,
99 const void *keydata UNUSED
)
101 const struct dir_rename_entry
*e1
, *e2
;
103 e1
= container_of(eptr
, const struct dir_rename_entry
, ent
);
104 e2
= container_of(entry_or_key
, const struct dir_rename_entry
, ent
);
106 return strcmp(e1
->dir
, e2
->dir
);
109 static void dir_rename_init(struct hashmap
*map
)
111 hashmap_init(map
, dir_rename_cmp
, NULL
, 0);
114 static void dir_rename_entry_init(struct dir_rename_entry
*entry
,
117 hashmap_entry_init(&entry
->ent
, strhash(directory
));
118 entry
->dir
= directory
;
119 entry
->non_unique_new_dir
= 0;
120 strbuf_init(&entry
->new_dir
, 0);
121 string_list_init_nodup(&entry
->possible_new_dirs
);
124 struct collision_entry
{
125 struct hashmap_entry ent
;
127 struct string_list source_files
;
128 unsigned reported_already
:1;
131 static struct collision_entry
*collision_find_entry(struct hashmap
*hashmap
,
134 struct collision_entry key
;
136 hashmap_entry_init(&key
.ent
, strhash(target_file
));
137 key
.target_file
= target_file
;
138 return hashmap_get_entry(hashmap
, &key
, ent
, NULL
);
141 static int collision_cmp(const void *cmp_data UNUSED
,
142 const struct hashmap_entry
*eptr
,
143 const struct hashmap_entry
*entry_or_key
,
144 const void *keydata UNUSED
)
146 const struct collision_entry
*e1
, *e2
;
148 e1
= container_of(eptr
, const struct collision_entry
, ent
);
149 e2
= container_of(entry_or_key
, const struct collision_entry
, ent
);
151 return strcmp(e1
->target_file
, e2
->target_file
);
154 static void collision_init(struct hashmap
*map
)
156 hashmap_init(map
, collision_cmp
, NULL
, 0);
159 static void flush_output(struct merge_options
*opt
)
161 if (opt
->buffer_output
< 2 && opt
->obuf
.len
) {
162 fputs(opt
->obuf
.buf
, stdout
);
163 strbuf_reset(&opt
->obuf
);
167 __attribute__((format (printf
, 2, 3)))
168 static int err(struct merge_options
*opt
, const char *err
, ...)
172 if (opt
->buffer_output
< 2)
175 strbuf_complete(&opt
->obuf
, '\n');
176 strbuf_addstr(&opt
->obuf
, "error: ");
178 va_start(params
, err
);
179 strbuf_vaddf(&opt
->obuf
, err
, params
);
181 if (opt
->buffer_output
> 1)
182 strbuf_addch(&opt
->obuf
, '\n');
184 error("%s", opt
->obuf
.buf
);
185 strbuf_reset(&opt
->obuf
);
191 static struct tree
*shift_tree_object(struct repository
*repo
,
192 struct tree
*one
, struct tree
*two
,
193 const char *subtree_shift
)
195 struct object_id shifted
;
197 if (!*subtree_shift
) {
198 shift_tree(repo
, &one
->object
.oid
, &two
->object
.oid
, &shifted
, 0);
200 shift_tree_by(repo
, &one
->object
.oid
, &two
->object
.oid
, &shifted
,
203 if (oideq(&two
->object
.oid
, &shifted
))
205 return lookup_tree(repo
, &shifted
);
208 static inline void set_commit_tree(struct commit
*c
, struct tree
*t
)
213 static struct commit
*make_virtual_commit(struct repository
*repo
,
217 struct commit
*commit
= alloc_commit_node(repo
);
219 set_merge_remote_desc(commit
, comment
, (struct object
*)commit
);
220 set_commit_tree(commit
, tree
);
221 commit
->object
.parsed
= 1;
230 RENAME_ONE_FILE_TO_ONE
,
231 RENAME_ONE_FILE_TO_TWO
,
232 RENAME_TWO_FILES_TO_ONE
236 * Since we want to write the index eventually, we cannot reuse the index
237 * for these (temporary) data.
240 struct diff_filespec stages
[4]; /* mostly for oid & mode; maybe path */
241 struct rename_conflict_info
*rename_conflict_info
;
242 unsigned processed
:1;
246 unsigned processed
:1;
247 struct diff_filepair
*pair
;
248 const char *branch
; /* branch that the rename occurred on */
250 * If directory rename detection affected this rename, what was its
251 * original type ('A' or 'R') and it's original destination before
252 * the directory rename (otherwise, '\0' and NULL for these two vars).
254 char dir_rename_original_type
;
255 char *dir_rename_original_dest
;
257 * Purpose of src_entry and dst_entry:
259 * If 'before' is renamed to 'after' then src_entry will contain
260 * the versions of 'before' from the merge_base, HEAD, and MERGE in
261 * stages 1, 2, and 3; dst_entry will contain the respective
262 * versions of 'after' in corresponding locations. Thus, we have a
263 * total of six modes and oids, though some will be null. (Stage 0
264 * is ignored; we're interested in handling conflicts.)
266 * Since we don't turn on break-rewrites by default, neither
267 * src_entry nor dst_entry can have all three of their stages have
268 * non-null oids, meaning at most four of the six will be non-null.
269 * Also, since this is a rename, both src_entry and dst_entry will
270 * have at least one non-null oid, meaning at least two will be
271 * non-null. Of the six oids, a typical rename will have three be
272 * non-null. Only two implies a rename/delete, and four implies a
275 struct stage_data
*src_entry
;
276 struct stage_data
*dst_entry
;
279 struct rename_conflict_info
{
280 enum rename_type rename_type
;
285 static inline void setup_rename_conflict_info(enum rename_type rename_type
,
286 struct merge_options
*opt
,
290 struct rename_conflict_info
*ci
;
293 * When we have two renames involved, it's easiest to get the
294 * correct things into stage 2 and 3, and to make sure that the
295 * content merge puts HEAD before the other branch if we just
296 * ensure that branch1 == opt->branch1. So, simply flip arguments
297 * around if we don't have that.
299 if (ren2
&& ren1
->branch
!= opt
->branch1
) {
300 setup_rename_conflict_info(rename_type
, opt
, ren2
, ren1
);
305 ci
->rename_type
= rename_type
;
309 ci
->ren1
->dst_entry
->processed
= 0;
310 ci
->ren1
->dst_entry
->rename_conflict_info
= ci
;
312 ci
->ren2
->dst_entry
->rename_conflict_info
= ci
;
316 static int show(struct merge_options
*opt
, int v
)
318 return (!opt
->priv
->call_depth
&& opt
->verbosity
>= v
) ||
322 __attribute__((format (printf
, 3, 4)))
323 static void output(struct merge_options
*opt
, int v
, const char *fmt
, ...)
330 strbuf_addchars(&opt
->obuf
, ' ', opt
->priv
->call_depth
* 2);
333 strbuf_vaddf(&opt
->obuf
, fmt
, ap
);
336 strbuf_addch(&opt
->obuf
, '\n');
337 if (!opt
->buffer_output
)
341 static void repo_output_commit_title(struct merge_options
*opt
,
342 struct repository
*repo
,
343 struct commit
*commit
)
345 struct merge_remote_desc
*desc
;
347 strbuf_addchars(&opt
->obuf
, ' ', opt
->priv
->call_depth
* 2);
348 desc
= merge_remote_util(commit
);
350 strbuf_addf(&opt
->obuf
, "virtual %s\n", desc
->name
);
352 strbuf_repo_add_unique_abbrev(&opt
->obuf
, repo
,
355 strbuf_addch(&opt
->obuf
, ' ');
356 if (repo_parse_commit(repo
, commit
) != 0)
357 strbuf_addstr(&opt
->obuf
, _("(bad commit)\n"));
360 const char *msg
= repo_get_commit_buffer(repo
, commit
, NULL
);
361 int len
= find_commit_subject(msg
, &title
);
363 strbuf_addf(&opt
->obuf
, "%.*s\n", len
, title
);
364 repo_unuse_commit_buffer(repo
, commit
, msg
);
370 static void output_commit_title(struct merge_options
*opt
, struct commit
*commit
)
372 repo_output_commit_title(opt
, the_repository
, commit
);
375 static int add_cacheinfo(struct merge_options
*opt
,
376 const struct diff_filespec
*blob
,
377 const char *path
, int stage
, int refresh
, int options
)
379 struct index_state
*istate
= opt
->repo
->index
;
380 struct cache_entry
*ce
;
383 ce
= make_cache_entry(istate
, blob
->mode
, &blob
->oid
, path
, stage
, 0);
385 return err(opt
, _("add_cacheinfo failed for path '%s'; merge aborting."), path
);
387 ret
= add_index_entry(istate
, ce
, options
);
389 struct cache_entry
*nce
;
391 nce
= refresh_cache_entry(istate
, ce
,
392 CE_MATCH_REFRESH
| CE_MATCH_IGNORE_MISSING
);
394 return err(opt
, _("add_cacheinfo failed to refresh for path '%s'; merge aborting."), path
);
396 ret
= add_index_entry(istate
, nce
, options
);
401 static inline int merge_detect_rename(struct merge_options
*opt
)
403 return (opt
->detect_renames
>= 0) ? opt
->detect_renames
: 1;
406 static void init_tree_desc_from_tree(struct tree_desc
*desc
, struct tree
*tree
)
409 init_tree_desc(desc
, tree
->buffer
, tree
->size
);
412 static int unpack_trees_start(struct merge_options
*opt
,
418 struct tree_desc t
[3];
419 struct index_state tmp_index
= INDEX_STATE_INIT(opt
->repo
);
421 memset(&opt
->priv
->unpack_opts
, 0, sizeof(opt
->priv
->unpack_opts
));
422 if (opt
->priv
->call_depth
)
423 opt
->priv
->unpack_opts
.index_only
= 1;
425 opt
->priv
->unpack_opts
.update
= 1;
426 /* FIXME: should only do this if !overwrite_ignore */
427 opt
->priv
->unpack_opts
.preserve_ignored
= 0;
429 opt
->priv
->unpack_opts
.merge
= 1;
430 opt
->priv
->unpack_opts
.head_idx
= 2;
431 opt
->priv
->unpack_opts
.fn
= threeway_merge
;
432 opt
->priv
->unpack_opts
.src_index
= opt
->repo
->index
;
433 opt
->priv
->unpack_opts
.dst_index
= &tmp_index
;
434 opt
->priv
->unpack_opts
.aggressive
= !merge_detect_rename(opt
);
435 setup_unpack_trees_porcelain(&opt
->priv
->unpack_opts
, "merge");
437 init_tree_desc_from_tree(t
+0, common
);
438 init_tree_desc_from_tree(t
+1, head
);
439 init_tree_desc_from_tree(t
+2, merge
);
441 rc
= unpack_trees(3, t
, &opt
->priv
->unpack_opts
);
442 cache_tree_free(&opt
->repo
->index
->cache_tree
);
445 * Update opt->repo->index to match the new results, AFTER saving a
446 * copy in opt->priv->orig_index. Update src_index to point to the
447 * saved copy. (verify_uptodate() checks src_index, and the original
448 * index is the one that had the necessary modification timestamps.)
450 opt
->priv
->orig_index
= *opt
->repo
->index
;
451 *opt
->repo
->index
= tmp_index
;
452 opt
->priv
->unpack_opts
.src_index
= &opt
->priv
->orig_index
;
457 static void unpack_trees_finish(struct merge_options
*opt
)
459 discard_index(&opt
->priv
->orig_index
);
460 clear_unpack_trees_porcelain(&opt
->priv
->unpack_opts
);
463 static int save_files_dirs(const struct object_id
*oid UNUSED
,
464 struct strbuf
*base
, const char *path
,
465 unsigned int mode
, void *context
)
467 struct path_hashmap_entry
*entry
;
468 int baselen
= base
->len
;
469 struct merge_options
*opt
= context
;
471 strbuf_addstr(base
, path
);
473 FLEX_ALLOC_MEM(entry
, path
, base
->buf
, base
->len
);
474 hashmap_entry_init(&entry
->e
, fspathhash(entry
->path
));
475 hashmap_add(&opt
->priv
->current_file_dir_set
, &entry
->e
);
477 strbuf_setlen(base
, baselen
);
478 return (S_ISDIR(mode
) ? READ_TREE_RECURSIVE
: 0);
481 static void get_files_dirs(struct merge_options
*opt
, struct tree
*tree
)
483 struct pathspec match_all
;
484 memset(&match_all
, 0, sizeof(match_all
));
485 read_tree(opt
->repo
, tree
,
486 &match_all
, save_files_dirs
, opt
);
489 static int get_tree_entry_if_blob(struct repository
*r
,
490 const struct object_id
*tree
,
492 struct diff_filespec
*dfs
)
496 ret
= get_tree_entry(r
, tree
, path
, &dfs
->oid
, &dfs
->mode
);
497 if (S_ISDIR(dfs
->mode
)) {
498 oidcpy(&dfs
->oid
, null_oid());
505 * Returns an index_entry instance which doesn't have to correspond to
506 * a real cache entry in Git's index.
508 static struct stage_data
*insert_stage_data(struct repository
*r
,
510 struct tree
*o
, struct tree
*a
, struct tree
*b
,
511 struct string_list
*entries
)
513 struct string_list_item
*item
;
514 struct stage_data
*e
= xcalloc(1, sizeof(struct stage_data
));
515 get_tree_entry_if_blob(r
, &o
->object
.oid
, path
, &e
->stages
[1]);
516 get_tree_entry_if_blob(r
, &a
->object
.oid
, path
, &e
->stages
[2]);
517 get_tree_entry_if_blob(r
, &b
->object
.oid
, path
, &e
->stages
[3]);
518 item
= string_list_insert(entries
, path
);
524 * Create a dictionary mapping file names to stage_data objects. The
525 * dictionary contains one entry for every path with a non-zero stage entry.
527 static struct string_list
*get_unmerged(struct index_state
*istate
)
529 struct string_list
*unmerged
= xmalloc(sizeof(struct string_list
));
532 string_list_init_dup(unmerged
);
534 /* TODO: audit for interaction with sparse-index. */
535 ensure_full_index(istate
);
536 for (i
= 0; i
< istate
->cache_nr
; i
++) {
537 struct string_list_item
*item
;
538 struct stage_data
*e
;
539 const struct cache_entry
*ce
= istate
->cache
[i
];
543 item
= string_list_lookup(unmerged
, ce
->name
);
545 item
= string_list_insert(unmerged
, ce
->name
);
546 item
->util
= xcalloc(1, sizeof(struct stage_data
));
549 e
->stages
[ce_stage(ce
)].mode
= ce
->ce_mode
;
550 oidcpy(&e
->stages
[ce_stage(ce
)].oid
, &ce
->oid
);
556 static int string_list_df_name_compare(const char *one
, const char *two
)
558 int onelen
= strlen(one
);
559 int twolen
= strlen(two
);
561 * Here we only care that entries for D/F conflicts are
562 * adjacent, in particular with the file of the D/F conflict
563 * appearing before files below the corresponding directory.
564 * The order of the rest of the list is irrelevant for us.
566 * To achieve this, we sort with df_name_compare and provide
567 * the mode S_IFDIR so that D/F conflicts will sort correctly.
568 * We use the mode S_IFDIR for everything else for simplicity,
569 * since in other cases any changes in their order due to
570 * sorting cause no problems for us.
572 int cmp
= df_name_compare(one
, onelen
, S_IFDIR
,
573 two
, twolen
, S_IFDIR
);
575 * Now that 'foo' and 'foo/bar' compare equal, we have to make sure
576 * that 'foo' comes before 'foo/bar'.
580 return onelen
- twolen
;
583 static void record_df_conflict_files(struct merge_options
*opt
,
584 struct string_list
*entries
)
586 /* If there is a D/F conflict and the file for such a conflict
587 * currently exists in the working tree, we want to allow it to be
588 * removed to make room for the corresponding directory if needed.
589 * The files underneath the directories of such D/F conflicts will
590 * be processed before the corresponding file involved in the D/F
591 * conflict. If the D/F directory ends up being removed by the
592 * merge, then we won't have to touch the D/F file. If the D/F
593 * directory needs to be written to the working copy, then the D/F
594 * file will simply be removed (in make_room_for_path()) to make
595 * room for the necessary paths. Note that if both the directory
596 * and the file need to be present, then the D/F file will be
597 * reinstated with a new unique name at the time it is processed.
599 struct string_list df_sorted_entries
= STRING_LIST_INIT_NODUP
;
600 const char *last_file
= NULL
;
605 * If we're merging merge-bases, we don't want to bother with
606 * any working directory changes.
608 if (opt
->priv
->call_depth
)
611 /* Ensure D/F conflicts are adjacent in the entries list. */
612 for (i
= 0; i
< entries
->nr
; i
++) {
613 struct string_list_item
*next
= &entries
->items
[i
];
614 string_list_append(&df_sorted_entries
, next
->string
)->util
=
617 df_sorted_entries
.cmp
= string_list_df_name_compare
;
618 string_list_sort(&df_sorted_entries
);
620 string_list_clear(&opt
->priv
->df_conflict_file_set
, 1);
621 for (i
= 0; i
< df_sorted_entries
.nr
; i
++) {
622 const char *path
= df_sorted_entries
.items
[i
].string
;
623 int len
= strlen(path
);
624 struct stage_data
*e
= df_sorted_entries
.items
[i
].util
;
627 * Check if last_file & path correspond to a D/F conflict;
628 * i.e. whether path is last_file+'/'+<something>.
629 * If so, record that it's okay to remove last_file to make
630 * room for path and friends if needed.
634 memcmp(path
, last_file
, last_len
) == 0 &&
635 path
[last_len
] == '/') {
636 string_list_insert(&opt
->priv
->df_conflict_file_set
, last_file
);
640 * Determine whether path could exist as a file in the
641 * working directory as a possible D/F conflict. This
642 * will only occur when it exists in stage 2 as a
645 if (S_ISREG(e
->stages
[2].mode
) || S_ISLNK(e
->stages
[2].mode
)) {
652 string_list_clear(&df_sorted_entries
, 0);
655 static int update_stages(struct merge_options
*opt
, const char *path
,
656 const struct diff_filespec
*o
,
657 const struct diff_filespec
*a
,
658 const struct diff_filespec
*b
)
662 * NOTE: It is usually a bad idea to call update_stages on a path
663 * before calling update_file on that same path, since it can
664 * sometimes lead to spurious "refusing to lose untracked file..."
665 * messages from update_file (via make_room_for path via
666 * would_lose_untracked). Instead, reverse the order of the calls
667 * (executing update_file first and then update_stages).
670 int options
= ADD_CACHE_OK_TO_ADD
| ADD_CACHE_SKIP_DFCHECK
;
672 if (remove_file_from_index(opt
->repo
->index
, path
))
675 if (add_cacheinfo(opt
, o
, path
, 1, 0, options
))
678 if (add_cacheinfo(opt
, a
, path
, 2, 0, options
))
681 if (add_cacheinfo(opt
, b
, path
, 3, 0, options
))
686 static void update_entry(struct stage_data
*entry
,
687 struct diff_filespec
*o
,
688 struct diff_filespec
*a
,
689 struct diff_filespec
*b
)
691 entry
->processed
= 0;
692 entry
->stages
[1].mode
= o
->mode
;
693 entry
->stages
[2].mode
= a
->mode
;
694 entry
->stages
[3].mode
= b
->mode
;
695 oidcpy(&entry
->stages
[1].oid
, &o
->oid
);
696 oidcpy(&entry
->stages
[2].oid
, &a
->oid
);
697 oidcpy(&entry
->stages
[3].oid
, &b
->oid
);
700 static int remove_file(struct merge_options
*opt
, int clean
,
701 const char *path
, int no_wd
)
703 int update_cache
= opt
->priv
->call_depth
|| clean
;
704 int update_working_directory
= !opt
->priv
->call_depth
&& !no_wd
;
707 if (remove_file_from_index(opt
->repo
->index
, path
))
710 if (update_working_directory
) {
712 struct cache_entry
*ce
;
713 ce
= index_file_exists(opt
->repo
->index
, path
, strlen(path
),
715 if (ce
&& ce_stage(ce
) == 0 && strcmp(path
, ce
->name
))
718 if (remove_path(path
))
724 /* add a string to a strbuf, but converting "/" to "_" */
725 static void add_flattened_path(struct strbuf
*out
, const char *s
)
728 strbuf_addstr(out
, s
);
729 for (; i
< out
->len
; i
++)
730 if (out
->buf
[i
] == '/')
734 static char *unique_path(struct merge_options
*opt
,
738 struct path_hashmap_entry
*entry
;
739 struct strbuf newpath
= STRBUF_INIT
;
743 strbuf_addf(&newpath
, "%s~", path
);
744 add_flattened_path(&newpath
, branch
);
746 base_len
= newpath
.len
;
747 while (hashmap_get_from_hash(&opt
->priv
->current_file_dir_set
,
748 fspathhash(newpath
.buf
), newpath
.buf
) ||
749 (!opt
->priv
->call_depth
&& file_exists(newpath
.buf
))) {
750 strbuf_setlen(&newpath
, base_len
);
751 strbuf_addf(&newpath
, "_%d", suffix
++);
754 FLEX_ALLOC_MEM(entry
, path
, newpath
.buf
, newpath
.len
);
755 hashmap_entry_init(&entry
->e
, fspathhash(entry
->path
));
756 hashmap_add(&opt
->priv
->current_file_dir_set
, &entry
->e
);
757 return strbuf_detach(&newpath
, NULL
);
761 * Check whether a directory in the index is in the way of an incoming
762 * file. Return 1 if so. If check_working_copy is non-zero, also
763 * check the working directory. If empty_ok is non-zero, also return
764 * 0 in the case where the working-tree dir exists but is empty.
766 static int dir_in_way(struct index_state
*istate
, const char *path
,
767 int check_working_copy
, int empty_ok
)
770 struct strbuf dirpath
= STRBUF_INIT
;
773 strbuf_addstr(&dirpath
, path
);
774 strbuf_addch(&dirpath
, '/');
776 pos
= index_name_pos(istate
, dirpath
.buf
, dirpath
.len
);
780 if (pos
< istate
->cache_nr
&&
781 !strncmp(dirpath
.buf
, istate
->cache
[pos
]->name
, dirpath
.len
)) {
782 strbuf_release(&dirpath
);
786 strbuf_release(&dirpath
);
787 return check_working_copy
&& !lstat(path
, &st
) && S_ISDIR(st
.st_mode
) &&
788 !(empty_ok
&& is_empty_dir(path
)) &&
789 !has_symlink_leading_path(path
, strlen(path
));
793 * Returns whether path was tracked in the index before the merge started,
794 * and its oid and mode match the specified values
796 static int was_tracked_and_matches(struct merge_options
*opt
, const char *path
,
797 const struct diff_filespec
*blob
)
799 int pos
= index_name_pos(&opt
->priv
->orig_index
, path
, strlen(path
));
800 struct cache_entry
*ce
;
803 /* we were not tracking this path before the merge */
806 /* See if the file we were tracking before matches */
807 ce
= opt
->priv
->orig_index
.cache
[pos
];
808 return (oideq(&ce
->oid
, &blob
->oid
) && ce
->ce_mode
== blob
->mode
);
812 * Returns whether path was tracked in the index before the merge started
814 static int was_tracked(struct merge_options
*opt
, const char *path
)
816 int pos
= index_name_pos(&opt
->priv
->orig_index
, path
, strlen(path
));
819 /* we were tracking this path before the merge */
825 static int would_lose_untracked(struct merge_options
*opt
, const char *path
)
827 struct index_state
*istate
= opt
->repo
->index
;
830 * This may look like it can be simplified to:
831 * return !was_tracked(opt, path) && file_exists(path)
832 * but it can't. This function needs to know whether path was in
833 * the working tree due to EITHER having been tracked in the index
834 * before the merge OR having been put into the working copy and
835 * index by unpack_trees(). Due to that either-or requirement, we
836 * check the current index instead of the original one.
838 * Note that we do not need to worry about merge-recursive itself
839 * updating the index after unpack_trees() and before calling this
840 * function, because we strictly require all code paths in
841 * merge-recursive to update the working tree first and the index
842 * second. Doing otherwise would break
843 * update_file()/would_lose_untracked(); see every comment in this
844 * file which mentions "update_stages".
846 int pos
= index_name_pos(istate
, path
, strlen(path
));
850 while (pos
< istate
->cache_nr
&&
851 !strcmp(path
, istate
->cache
[pos
]->name
)) {
853 * If stage #0, it is definitely tracked.
854 * If it has stage #2 then it was tracked
855 * before this merge started. All other
856 * cases the path was not tracked.
858 switch (ce_stage(istate
->cache
[pos
])) {
865 return file_exists(path
);
868 static int was_dirty(struct merge_options
*opt
, const char *path
)
870 struct cache_entry
*ce
;
873 if (opt
->priv
->call_depth
|| !was_tracked(opt
, path
))
876 ce
= index_file_exists(opt
->priv
->unpack_opts
.src_index
,
877 path
, strlen(path
), ignore_case
);
878 dirty
= verify_uptodate(ce
, &opt
->priv
->unpack_opts
) != 0;
882 static int make_room_for_path(struct merge_options
*opt
, const char *path
)
885 const char *msg
= _("failed to create path '%s'%s");
887 /* Unlink any D/F conflict files that are in the way */
888 for (i
= 0; i
< opt
->priv
->df_conflict_file_set
.nr
; i
++) {
889 const char *df_path
= opt
->priv
->df_conflict_file_set
.items
[i
].string
;
890 size_t pathlen
= strlen(path
);
891 size_t df_pathlen
= strlen(df_path
);
892 if (df_pathlen
< pathlen
&&
893 path
[df_pathlen
] == '/' &&
894 strncmp(path
, df_path
, df_pathlen
) == 0) {
896 _("Removing %s to make room for subdirectory\n"),
899 unsorted_string_list_delete_item(&opt
->priv
->df_conflict_file_set
,
905 /* Make sure leading directories are created */
906 status
= safe_create_leading_directories_const(path
);
908 if (status
== SCLD_EXISTS
)
909 /* something else exists */
910 return err(opt
, msg
, path
, _(": perhaps a D/F conflict?"));
911 return err(opt
, msg
, path
, "");
915 * Do not unlink a file in the work tree if we are not
918 if (would_lose_untracked(opt
, path
))
919 return err(opt
, _("refusing to lose untracked file at '%s'"),
922 /* Successful unlink is good.. */
925 /* .. and so is no existing file */
928 /* .. but not some other error (who really cares what?) */
929 return err(opt
, msg
, path
, _(": perhaps a D/F conflict?"));
932 static int update_file_flags(struct merge_options
*opt
,
933 const struct diff_filespec
*contents
,
940 if (opt
->priv
->call_depth
)
944 enum object_type type
;
948 if (S_ISGITLINK(contents
->mode
)) {
950 * We may later decide to recursively descend into
951 * the submodule directory and update its index
952 * and/or work tree, but we do not do that now.
958 buf
= repo_read_object_file(the_repository
, &contents
->oid
,
961 ret
= err(opt
, _("cannot read object %s '%s'"),
962 oid_to_hex(&contents
->oid
), path
);
965 if (type
!= OBJ_BLOB
) {
966 ret
= err(opt
, _("blob expected for %s '%s'"),
967 oid_to_hex(&contents
->oid
), path
);
970 if (S_ISREG(contents
->mode
)) {
971 struct strbuf strbuf
= STRBUF_INIT
;
972 if (convert_to_working_tree(opt
->repo
->index
,
973 path
, buf
, size
, &strbuf
, NULL
)) {
976 buf
= strbuf_detach(&strbuf
, NULL
);
980 if (make_room_for_path(opt
, path
) < 0) {
984 if (S_ISREG(contents
->mode
) ||
985 (!has_symlinks
&& S_ISLNK(contents
->mode
))) {
987 int mode
= (contents
->mode
& 0100 ? 0777 : 0666);
989 fd
= open(path
, O_WRONLY
| O_TRUNC
| O_CREAT
, mode
);
991 ret
= err(opt
, _("failed to open '%s': %s"),
992 path
, strerror(errno
));
995 write_in_full(fd
, buf
, size
);
997 } else if (S_ISLNK(contents
->mode
)) {
998 char *lnk
= xmemdupz(buf
, size
);
999 safe_create_leading_directories_const(path
);
1001 if (symlink(lnk
, path
))
1002 ret
= err(opt
, _("failed to symlink '%s': %s"),
1003 path
, strerror(errno
));
1007 _("do not know what to do with %06o %s '%s'"),
1008 contents
->mode
, oid_to_hex(&contents
->oid
), path
);
1013 if (!ret
&& update_cache
) {
1014 int refresh
= (!opt
->priv
->call_depth
&&
1015 contents
->mode
!= S_IFGITLINK
);
1016 if (add_cacheinfo(opt
, contents
, path
, 0, refresh
,
1017 ADD_CACHE_OK_TO_ADD
))
1023 static int update_file(struct merge_options
*opt
,
1025 const struct diff_filespec
*contents
,
1028 return update_file_flags(opt
, contents
, path
,
1029 opt
->priv
->call_depth
|| clean
, !opt
->priv
->call_depth
);
1032 /* Low level file merging, update and removal */
1034 struct merge_file_info
{
1035 struct diff_filespec blob
; /* mostly use oid & mode; sometimes path */
1040 static int merge_3way(struct merge_options
*opt
,
1041 mmbuffer_t
*result_buf
,
1042 const struct diff_filespec
*o
,
1043 const struct diff_filespec
*a
,
1044 const struct diff_filespec
*b
,
1045 const char *branch1
,
1046 const char *branch2
,
1047 const int extra_marker_size
)
1049 mmfile_t orig
, src1
, src2
;
1050 struct ll_merge_options ll_opts
= {0};
1051 char *base
, *name1
, *name2
;
1052 enum ll_merge_result merge_status
;
1054 ll_opts
.renormalize
= opt
->renormalize
;
1055 ll_opts
.extra_marker_size
= extra_marker_size
;
1056 ll_opts
.xdl_opts
= opt
->xdl_opts
;
1058 if (opt
->priv
->call_depth
) {
1059 ll_opts
.virtual_ancestor
= 1;
1060 ll_opts
.variant
= 0;
1062 switch (opt
->recursive_variant
) {
1063 case MERGE_VARIANT_OURS
:
1064 ll_opts
.variant
= XDL_MERGE_FAVOR_OURS
;
1066 case MERGE_VARIANT_THEIRS
:
1067 ll_opts
.variant
= XDL_MERGE_FAVOR_THEIRS
;
1070 ll_opts
.variant
= 0;
1075 assert(a
->path
&& b
->path
&& o
->path
&& opt
->ancestor
);
1076 if (strcmp(a
->path
, b
->path
) || strcmp(a
->path
, o
->path
) != 0) {
1077 base
= mkpathdup("%s:%s", opt
->ancestor
, o
->path
);
1078 name1
= mkpathdup("%s:%s", branch1
, a
->path
);
1079 name2
= mkpathdup("%s:%s", branch2
, b
->path
);
1081 base
= mkpathdup("%s", opt
->ancestor
);
1082 name1
= mkpathdup("%s", branch1
);
1083 name2
= mkpathdup("%s", branch2
);
1086 read_mmblob(&orig
, &o
->oid
);
1087 read_mmblob(&src1
, &a
->oid
);
1088 read_mmblob(&src2
, &b
->oid
);
1091 * FIXME: Using a->path for normalization rules in ll_merge could be
1092 * wrong if we renamed from a->path to b->path. We should use the
1093 * target path for where the file will be written.
1095 merge_status
= ll_merge(result_buf
, a
->path
, &orig
, base
,
1096 &src1
, name1
, &src2
, name2
,
1097 opt
->repo
->index
, &ll_opts
);
1098 if (merge_status
== LL_MERGE_BINARY_CONFLICT
)
1099 warning("Cannot merge binary files: %s (%s vs. %s)",
1100 a
->path
, name1
, name2
);
1108 return merge_status
;
1111 static int find_first_merges(struct repository
*repo
,
1112 struct object_array
*result
, const char *path
,
1113 struct commit
*a
, struct commit
*b
)
1116 struct object_array merges
= OBJECT_ARRAY_INIT
;
1117 struct commit
*commit
;
1118 int contains_another
;
1120 char merged_revision
[GIT_MAX_HEXSZ
+ 2];
1121 const char *rev_args
[] = { "rev-list", "--merges", "--ancestry-path",
1122 "--all", merged_revision
, NULL
};
1123 struct rev_info revs
;
1124 struct setup_revision_opt rev_opts
;
1126 memset(result
, 0, sizeof(struct object_array
));
1127 memset(&rev_opts
, 0, sizeof(rev_opts
));
1129 /* get all revisions that merge commit a */
1130 xsnprintf(merged_revision
, sizeof(merged_revision
), "^%s",
1131 oid_to_hex(&a
->object
.oid
));
1132 repo_init_revisions(repo
, &revs
, NULL
);
1133 /* FIXME: can't handle linked worktrees in submodules yet */
1134 revs
.single_worktree
= path
!= NULL
;
1135 setup_revisions(ARRAY_SIZE(rev_args
)-1, rev_args
, &revs
, &rev_opts
);
1137 /* save all revisions from the above list that contain b */
1138 if (prepare_revision_walk(&revs
))
1139 die("revision walk setup failed");
1140 while ((commit
= get_revision(&revs
)) != NULL
) {
1141 struct object
*o
= &(commit
->object
);
1142 int ret
= repo_in_merge_bases(repo
, b
, commit
);
1144 object_array_clear(&merges
);
1145 release_revisions(&revs
);
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
;
1164 int ret
= repo_in_merge_bases(repo
, m2
, m1
);
1166 object_array_clear(&merges
);
1167 release_revisions(&revs
);
1171 contains_another
= 1;
1177 if (!contains_another
)
1178 add_object_array(merges
.objects
[i
].item
, NULL
, result
);
1181 object_array_clear(&merges
);
1182 release_revisions(&revs
);
1186 static void print_commit(struct repository
*repo
, struct commit
*commit
)
1188 struct strbuf sb
= STRBUF_INIT
;
1189 struct pretty_print_context ctx
= {0};
1190 ctx
.date_mode
.type
= DATE_NORMAL
;
1191 /* FIXME: Merge this with output_commit_title() */
1192 assert(!merge_remote_util(commit
));
1193 repo_format_commit_message(repo
, commit
, " %h: %m %s", &sb
, &ctx
);
1194 fprintf(stderr
, "%s\n", sb
.buf
);
1195 strbuf_release(&sb
);
1198 static int is_valid(const struct diff_filespec
*dfs
)
1200 return dfs
->mode
!= 0 && !is_null_oid(&dfs
->oid
);
1203 static int merge_submodule(struct merge_options
*opt
,
1204 struct object_id
*result
, const char *path
,
1205 const struct object_id
*base
, const struct object_id
*a
,
1206 const struct object_id
*b
)
1208 struct repository subrepo
;
1210 struct commit
*commit_base
, *commit_a
, *commit_b
;
1212 struct object_array merges
;
1215 int search
= !opt
->priv
->call_depth
;
1217 /* store a in result in case we fail */
1218 /* FIXME: This is the WRONG resolution for the recursive case when
1219 * we need to be careful to avoid accidentally matching either side.
1220 * Should probably use o instead there, much like we do for merging
1225 /* we can not handle deletion conflicts */
1226 if (is_null_oid(base
))
1233 if (repo_submodule_init(&subrepo
, opt
->repo
, path
, null_oid())) {
1234 output(opt
, 1, _("Failed to merge submodule %s (not checked out)"), path
);
1238 if (!(commit_base
= lookup_commit_reference(&subrepo
, base
)) ||
1239 !(commit_a
= lookup_commit_reference(&subrepo
, a
)) ||
1240 !(commit_b
= lookup_commit_reference(&subrepo
, b
))) {
1241 output(opt
, 1, _("Failed to merge submodule %s (commits not present)"), path
);
1245 /* check whether both changes are forward */
1246 ret2
= repo_in_merge_bases(&subrepo
, commit_base
, commit_a
);
1248 output(opt
, 1, _("Failed to merge submodule %s (repository corrupt)"), path
);
1252 ret2
= repo_in_merge_bases(&subrepo
, commit_base
, commit_b
);
1254 output(opt
, 1, _("Failed to merge submodule %s (repository corrupt)"), path
);
1258 output(opt
, 1, _("Failed to merge submodule %s (commits don't follow merge-base)"), path
);
1262 /* Case #1: a is contained in b or vice versa */
1263 ret2
= repo_in_merge_bases(&subrepo
, commit_a
, commit_b
);
1265 output(opt
, 1, _("Failed to merge submodule %s (repository corrupt)"), path
);
1271 output(opt
, 3, _("Fast-forwarding submodule %s to the following commit:"), path
);
1272 repo_output_commit_title(opt
, &subrepo
, commit_b
);
1273 } else if (show(opt
, 2))
1274 output(opt
, 2, _("Fast-forwarding submodule %s"), path
);
1281 ret2
= repo_in_merge_bases(&subrepo
, commit_b
, commit_a
);
1283 output(opt
, 1, _("Failed to merge submodule %s (repository corrupt)"), path
);
1289 output(opt
, 3, _("Fast-forwarding submodule %s to the following commit:"), path
);
1290 repo_output_commit_title(opt
, &subrepo
, commit_a
);
1291 } else if (show(opt
, 2))
1292 output(opt
, 2, _("Fast-forwarding submodule %s"), path
);
1301 * Case #2: There are one or more merges that contain a and b in
1302 * the submodule. If there is only one, then present it as a
1303 * suggestion to the user, but leave it marked unmerged so the
1304 * user needs to confirm the resolution.
1307 /* Skip the search if makes no sense to the calling context. */
1311 /* find commit which merges them */
1312 parent_count
= find_first_merges(&subrepo
, &merges
, path
,
1313 commit_a
, commit_b
);
1314 switch (parent_count
) {
1316 output(opt
, 1, _("Failed to merge submodule %s (merge following commits not found)"), path
);
1320 output(opt
, 1, _("Failed to merge submodule %s (not fast-forward)"), path
);
1321 output(opt
, 2, _("Found a possible merge resolution for the submodule:\n"));
1322 print_commit(&subrepo
, (struct commit
*) merges
.objects
[0].item
);
1324 "If this is correct simply add it to the index "
1327 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
1328 "which will accept this suggestion.\n"),
1329 oid_to_hex(&merges
.objects
[0].item
->oid
), path
);
1333 output(opt
, 1, _("Failed to merge submodule %s (multiple merges found)"), path
);
1334 for (i
= 0; i
< merges
.nr
; i
++)
1335 print_commit(&subrepo
, (struct commit
*) merges
.objects
[i
].item
);
1338 object_array_clear(&merges
);
1340 repo_clear(&subrepo
);
1344 static int merge_mode_and_contents(struct merge_options
*opt
,
1345 const struct diff_filespec
*o
,
1346 const struct diff_filespec
*a
,
1347 const struct diff_filespec
*b
,
1348 const char *filename
,
1349 const char *branch1
,
1350 const char *branch2
,
1351 const int extra_marker_size
,
1352 struct merge_file_info
*result
)
1354 if (opt
->branch1
!= branch1
) {
1356 * It's weird getting a reverse merge with HEAD on the bottom
1357 * side of the conflict markers and the other branch on the
1360 return merge_mode_and_contents(opt
, o
, b
, a
,
1363 extra_marker_size
, result
);
1369 if ((S_IFMT
& a
->mode
) != (S_IFMT
& b
->mode
)) {
1372 * FIXME: This is a bad resolution for recursive case; for
1373 * the recursive case we want something that is unlikely to
1374 * accidentally match either side. Also, while it makes
1375 * sense to prefer regular files over symlinks, it doesn't
1376 * make sense to prefer regular files over submodules.
1378 if (S_ISREG(a
->mode
)) {
1379 result
->blob
.mode
= a
->mode
;
1380 oidcpy(&result
->blob
.oid
, &a
->oid
);
1382 result
->blob
.mode
= b
->mode
;
1383 oidcpy(&result
->blob
.oid
, &b
->oid
);
1386 if (!oideq(&a
->oid
, &o
->oid
) && !oideq(&b
->oid
, &o
->oid
))
1392 if (a
->mode
== b
->mode
|| a
->mode
== o
->mode
)
1393 result
->blob
.mode
= b
->mode
;
1395 result
->blob
.mode
= a
->mode
;
1396 if (b
->mode
!= o
->mode
) {
1402 if (oideq(&a
->oid
, &b
->oid
) || oideq(&a
->oid
, &o
->oid
))
1403 oidcpy(&result
->blob
.oid
, &b
->oid
);
1404 else if (oideq(&b
->oid
, &o
->oid
))
1405 oidcpy(&result
->blob
.oid
, &a
->oid
);
1406 else if (S_ISREG(a
->mode
)) {
1407 mmbuffer_t result_buf
;
1408 int ret
= 0, merge_status
;
1410 merge_status
= merge_3way(opt
, &result_buf
, o
, a
, b
,
1414 if ((merge_status
< 0) || !result_buf
.ptr
)
1415 ret
= err(opt
, _("failed to execute internal merge"));
1418 write_object_file(result_buf
.ptr
, result_buf
.size
,
1419 OBJ_BLOB
, &result
->blob
.oid
))
1420 ret
= err(opt
, _("unable to add %s to database"),
1423 free(result_buf
.ptr
);
1426 /* FIXME: bug, what if modes didn't match? */
1427 result
->clean
= (merge_status
== 0);
1428 } else if (S_ISGITLINK(a
->mode
)) {
1429 result
->clean
= merge_submodule(opt
, &result
->blob
.oid
,
1434 if (result
->clean
< 0)
1436 } else if (S_ISLNK(a
->mode
)) {
1437 switch (opt
->recursive_variant
) {
1438 case MERGE_VARIANT_NORMAL
:
1439 oidcpy(&result
->blob
.oid
, &a
->oid
);
1440 if (!oideq(&a
->oid
, &b
->oid
))
1443 case MERGE_VARIANT_OURS
:
1444 oidcpy(&result
->blob
.oid
, &a
->oid
);
1446 case MERGE_VARIANT_THEIRS
:
1447 oidcpy(&result
->blob
.oid
, &b
->oid
);
1451 BUG("unsupported object type in the tree");
1455 output(opt
, 2, _("Auto-merging %s"), filename
);
1460 static int handle_rename_via_dir(struct merge_options
*opt
,
1461 struct rename_conflict_info
*ci
)
1464 * Handle file adds that need to be renamed due to directory rename
1465 * detection. This differs from handle_rename_normal, because
1466 * there is no content merge to do; just move the file into the
1467 * desired final location.
1469 const struct rename
*ren
= ci
->ren1
;
1470 const struct diff_filespec
*dest
= ren
->pair
->two
;
1471 char *file_path
= dest
->path
;
1472 int mark_conflicted
= (opt
->detect_directory_renames
==
1473 MERGE_DIRECTORY_RENAMES_CONFLICT
);
1474 assert(ren
->dir_rename_original_dest
);
1476 if (!opt
->priv
->call_depth
&& would_lose_untracked(opt
, dest
->path
)) {
1477 mark_conflicted
= 1;
1478 file_path
= unique_path(opt
, dest
->path
, ren
->branch
);
1479 output(opt
, 1, _("Error: Refusing to lose untracked file at %s; "
1480 "writing to %s instead."),
1481 dest
->path
, file_path
);
1484 if (mark_conflicted
) {
1486 * Write the file in worktree at file_path. In the index,
1487 * only record the file at dest->path in the appropriate
1490 if (update_file(opt
, 0, dest
, file_path
))
1492 if (file_path
!= dest
->path
)
1494 if (update_stages(opt
, dest
->path
, NULL
,
1495 ren
->branch
== opt
->branch1
? dest
: NULL
,
1496 ren
->branch
== opt
->branch1
? NULL
: dest
))
1498 return 0; /* not clean, but conflicted */
1500 /* Update dest->path both in index and in worktree */
1501 if (update_file(opt
, 1, dest
, dest
->path
))
1503 return 1; /* clean */
1507 static int handle_change_delete(struct merge_options
*opt
,
1508 const char *path
, const char *old_path
,
1509 const struct diff_filespec
*o
,
1510 const struct diff_filespec
*changed
,
1511 const char *change_branch
,
1512 const char *delete_branch
,
1513 const char *change
, const char *change_past
)
1515 char *alt_path
= NULL
;
1516 const char *update_path
= path
;
1519 if (dir_in_way(opt
->repo
->index
, path
, !opt
->priv
->call_depth
, 0) ||
1520 (!opt
->priv
->call_depth
&& would_lose_untracked(opt
, path
))) {
1521 update_path
= alt_path
= unique_path(opt
, path
, change_branch
);
1524 if (opt
->priv
->call_depth
) {
1526 * We cannot arbitrarily accept either a_sha or b_sha as
1527 * correct; since there is no true "middle point" between
1528 * them, simply reuse the base version for virtual merge base.
1530 ret
= remove_file_from_index(opt
->repo
->index
, path
);
1532 ret
= update_file(opt
, 0, o
, update_path
);
1535 * Despite the four nearly duplicate messages and argument
1536 * lists below and the ugliness of the nested if-statements,
1537 * having complete messages makes the job easier for
1540 * The slight variance among the cases is due to the fact
1542 * 1) directory/file conflicts (in effect if
1543 * !alt_path) could cause us to need to write the
1544 * file to a different path.
1545 * 2) renames (in effect if !old_path) could mean that
1546 * there are two names for the path that the user
1547 * may know the file by.
1551 output(opt
, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1552 "and %s in %s. Version %s of %s left in tree."),
1553 change
, path
, delete_branch
, change_past
,
1554 change_branch
, change_branch
, path
);
1556 output(opt
, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1557 "and %s to %s in %s. Version %s of %s left in tree."),
1558 change
, old_path
, delete_branch
, change_past
, path
,
1559 change_branch
, change_branch
, path
);
1563 output(opt
, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1564 "and %s in %s. Version %s of %s left in tree at %s."),
1565 change
, path
, delete_branch
, change_past
,
1566 change_branch
, change_branch
, path
, alt_path
);
1568 output(opt
, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1569 "and %s to %s in %s. Version %s of %s left in tree at %s."),
1570 change
, old_path
, delete_branch
, change_past
, path
,
1571 change_branch
, change_branch
, path
, alt_path
);
1575 * No need to call update_file() on path when change_branch ==
1576 * opt->branch1 && !alt_path, since that would needlessly touch
1577 * path. We could call update_file_flags() with update_cache=0
1578 * and update_wd=0, but that's a no-op.
1580 if (change_branch
!= opt
->branch1
|| alt_path
)
1581 ret
= update_file(opt
, 0, changed
, update_path
);
1588 static int handle_rename_delete(struct merge_options
*opt
,
1589 struct rename_conflict_info
*ci
)
1591 const struct rename
*ren
= ci
->ren1
;
1592 const struct diff_filespec
*orig
= ren
->pair
->one
;
1593 const struct diff_filespec
*dest
= ren
->pair
->two
;
1594 const char *rename_branch
= ren
->branch
;
1595 const char *delete_branch
= (opt
->branch1
== ren
->branch
?
1596 opt
->branch2
: opt
->branch1
);
1598 if (handle_change_delete(opt
,
1599 opt
->priv
->call_depth
? orig
->path
: dest
->path
,
1600 opt
->priv
->call_depth
? NULL
: orig
->path
,
1602 rename_branch
, delete_branch
,
1603 _("rename"), _("renamed")))
1606 if (opt
->priv
->call_depth
)
1607 return remove_file_from_index(opt
->repo
->index
, dest
->path
);
1609 return update_stages(opt
, dest
->path
, NULL
,
1610 rename_branch
== opt
->branch1
? dest
: NULL
,
1611 rename_branch
== opt
->branch1
? NULL
: dest
);
1614 static int handle_file_collision(struct merge_options
*opt
,
1615 const char *collide_path
,
1616 const char *prev_path1
,
1617 const char *prev_path2
,
1618 const char *branch1
, const char *branch2
,
1619 struct diff_filespec
*a
,
1620 struct diff_filespec
*b
)
1622 struct merge_file_info mfi
;
1623 struct diff_filespec null
;
1624 char *alt_path
= NULL
;
1625 const char *update_path
= collide_path
;
1628 * It's easiest to get the correct things into stage 2 and 3, and
1629 * to make sure that the content merge puts HEAD before the other
1630 * branch if we just ensure that branch1 == opt->branch1. So, simply
1631 * flip arguments around if we don't have that.
1633 if (branch1
!= opt
->branch1
) {
1634 return handle_file_collision(opt
, collide_path
,
1635 prev_path2
, prev_path1
,
1640 /* Remove rename sources if rename/add or rename/rename(2to1) */
1642 remove_file(opt
, 1, prev_path1
,
1643 opt
->priv
->call_depth
|| would_lose_untracked(opt
, prev_path1
));
1645 remove_file(opt
, 1, prev_path2
,
1646 opt
->priv
->call_depth
|| would_lose_untracked(opt
, prev_path2
));
1649 * Remove the collision path, if it wouldn't cause dirty contents
1650 * or an untracked file to get lost. We'll either overwrite with
1651 * merged contents, or just write out to differently named files.
1653 if (was_dirty(opt
, collide_path
)) {
1654 output(opt
, 1, _("Refusing to lose dirty file at %s"),
1656 update_path
= alt_path
= unique_path(opt
, collide_path
, "merged");
1657 } else if (would_lose_untracked(opt
, collide_path
)) {
1659 * Only way we get here is if both renames were from
1660 * a directory rename AND user had an untracked file
1661 * at the location where both files end up after the
1662 * two directory renames. See testcase 10d of t6043.
1664 output(opt
, 1, _("Refusing to lose untracked file at "
1665 "%s, even though it's in the way."),
1667 update_path
= alt_path
= unique_path(opt
, collide_path
, "merged");
1670 * FIXME: It's possible that the two files are identical
1671 * and that the current working copy happens to match, in
1672 * which case we are unnecessarily touching the working
1673 * tree file. It's not a likely enough scenario that I
1674 * want to code up the checks for it and a better fix is
1675 * available if we restructure how unpack_trees() and
1676 * merge-recursive interoperate anyway, so punting for
1679 remove_file(opt
, 0, collide_path
, 0);
1682 /* Store things in diff_filespecs for functions that need it */
1683 null
.path
= (char *)collide_path
;
1684 oidcpy(&null
.oid
, null_oid());
1687 if (merge_mode_and_contents(opt
, &null
, a
, b
, collide_path
,
1688 branch1
, branch2
, opt
->priv
->call_depth
* 2, &mfi
))
1690 mfi
.clean
&= !alt_path
;
1691 if (update_file(opt
, mfi
.clean
, &mfi
.blob
, update_path
))
1693 if (!mfi
.clean
&& !opt
->priv
->call_depth
&&
1694 update_stages(opt
, collide_path
, NULL
, a
, b
))
1698 * FIXME: If both a & b both started with conflicts (only possible
1699 * if they came from a rename/rename(2to1)), but had IDENTICAL
1700 * contents including those conflicts, then in the next line we claim
1701 * it was clean. If someone cares about this case, we should have the
1702 * caller notify us if we started with conflicts.
1707 static int handle_rename_add(struct merge_options
*opt
,
1708 struct rename_conflict_info
*ci
)
1710 /* a was renamed to c, and a separate c was added. */
1711 struct diff_filespec
*a
= ci
->ren1
->pair
->one
;
1712 struct diff_filespec
*c
= ci
->ren1
->pair
->two
;
1713 char *path
= c
->path
;
1714 char *prev_path_desc
;
1715 struct merge_file_info mfi
;
1717 const char *rename_branch
= ci
->ren1
->branch
;
1718 const char *add_branch
= (opt
->branch1
== rename_branch
?
1719 opt
->branch2
: opt
->branch1
);
1720 int other_stage
= (ci
->ren1
->branch
== opt
->branch1
? 3 : 2);
1722 output(opt
, 1, _("CONFLICT (rename/add): "
1723 "Rename %s->%s in %s. Added %s in %s"),
1724 a
->path
, c
->path
, rename_branch
,
1725 c
->path
, add_branch
);
1727 prev_path_desc
= xstrfmt("version of %s from %s", path
, a
->path
);
1728 ci
->ren1
->src_entry
->stages
[other_stage
].path
= a
->path
;
1729 if (merge_mode_and_contents(opt
, a
, c
,
1730 &ci
->ren1
->src_entry
->stages
[other_stage
],
1732 opt
->branch1
, opt
->branch2
,
1733 1 + opt
->priv
->call_depth
* 2, &mfi
))
1735 free(prev_path_desc
);
1737 ci
->ren1
->dst_entry
->stages
[other_stage
].path
= mfi
.blob
.path
= c
->path
;
1738 return handle_file_collision(opt
,
1739 c
->path
, a
->path
, NULL
,
1740 rename_branch
, add_branch
,
1742 &ci
->ren1
->dst_entry
->stages
[other_stage
]);
1745 static char *find_path_for_conflict(struct merge_options
*opt
,
1747 const char *branch1
,
1748 const char *branch2
)
1750 char *new_path
= NULL
;
1751 if (dir_in_way(opt
->repo
->index
, path
, !opt
->priv
->call_depth
, 0)) {
1752 new_path
= unique_path(opt
, path
, branch1
);
1753 output(opt
, 1, _("%s is a directory in %s adding "
1755 path
, branch2
, new_path
);
1756 } else if (would_lose_untracked(opt
, path
)) {
1757 new_path
= unique_path(opt
, path
, branch1
);
1758 output(opt
, 1, _("Refusing to lose untracked file"
1759 " at %s; adding as %s instead"),
1767 * Toggle the stage number between "ours" and "theirs" (2 and 3).
1769 static inline int flip_stage(int stage
)
1771 return (2 + 3) - stage
;
1774 static int handle_rename_rename_1to2(struct merge_options
*opt
,
1775 struct rename_conflict_info
*ci
)
1777 /* One file was renamed in both branches, but to different names. */
1778 struct merge_file_info mfi
;
1779 struct diff_filespec
*add
;
1780 struct diff_filespec
*o
= ci
->ren1
->pair
->one
;
1781 struct diff_filespec
*a
= ci
->ren1
->pair
->two
;
1782 struct diff_filespec
*b
= ci
->ren2
->pair
->two
;
1785 output(opt
, 1, _("CONFLICT (rename/rename): "
1786 "Rename \"%s\"->\"%s\" in branch \"%s\" "
1787 "rename \"%s\"->\"%s\" in \"%s\"%s"),
1788 o
->path
, a
->path
, ci
->ren1
->branch
,
1789 o
->path
, b
->path
, ci
->ren2
->branch
,
1790 opt
->priv
->call_depth
? _(" (left unresolved)") : "");
1792 path_desc
= xstrfmt("%s and %s, both renamed from %s",
1793 a
->path
, b
->path
, o
->path
);
1794 if (merge_mode_and_contents(opt
, o
, a
, b
, path_desc
,
1795 ci
->ren1
->branch
, ci
->ren2
->branch
,
1796 opt
->priv
->call_depth
* 2, &mfi
))
1800 if (opt
->priv
->call_depth
)
1801 remove_file_from_index(opt
->repo
->index
, o
->path
);
1804 * For each destination path, we need to see if there is a
1805 * rename/add collision. If not, we can write the file out
1806 * to the specified location.
1808 add
= &ci
->ren1
->dst_entry
->stages
[flip_stage(2)];
1809 if (is_valid(add
)) {
1810 add
->path
= mfi
.blob
.path
= a
->path
;
1811 if (handle_file_collision(opt
, a
->path
,
1815 &mfi
.blob
, add
) < 0)
1818 char *new_path
= find_path_for_conflict(opt
, a
->path
,
1821 if (update_file(opt
, 0, &mfi
.blob
,
1822 new_path
? new_path
: a
->path
))
1825 if (!opt
->priv
->call_depth
&&
1826 update_stages(opt
, a
->path
, NULL
, a
, NULL
))
1830 if (!mfi
.clean
&& mfi
.blob
.mode
== a
->mode
&&
1831 oideq(&mfi
.blob
.oid
, &a
->oid
)) {
1833 * Getting here means we were attempting to merge a binary
1834 * blob. Since we can't merge binaries, the merge algorithm
1835 * just takes one side. But we don't want to copy the
1836 * contents of one side to both paths; we'd rather use the
1837 * original content at the given path for each path.
1839 oidcpy(&mfi
.blob
.oid
, &b
->oid
);
1840 mfi
.blob
.mode
= b
->mode
;
1842 add
= &ci
->ren2
->dst_entry
->stages
[flip_stage(3)];
1843 if (is_valid(add
)) {
1844 add
->path
= mfi
.blob
.path
= b
->path
;
1845 if (handle_file_collision(opt
, b
->path
,
1849 add
, &mfi
.blob
) < 0)
1852 char *new_path
= find_path_for_conflict(opt
, b
->path
,
1855 if (update_file(opt
, 0, &mfi
.blob
,
1856 new_path
? new_path
: b
->path
))
1859 if (!opt
->priv
->call_depth
&&
1860 update_stages(opt
, b
->path
, NULL
, NULL
, b
))
1867 static int handle_rename_rename_2to1(struct merge_options
*opt
,
1868 struct rename_conflict_info
*ci
)
1870 /* Two files, a & b, were renamed to the same thing, c. */
1871 struct diff_filespec
*a
= ci
->ren1
->pair
->one
;
1872 struct diff_filespec
*b
= ci
->ren2
->pair
->one
;
1873 struct diff_filespec
*c1
= ci
->ren1
->pair
->two
;
1874 struct diff_filespec
*c2
= ci
->ren2
->pair
->two
;
1875 char *path
= c1
->path
; /* == c2->path */
1876 char *path_side_1_desc
;
1877 char *path_side_2_desc
;
1878 struct merge_file_info mfi_c1
;
1879 struct merge_file_info mfi_c2
;
1880 int ostage1
, ostage2
;
1882 output(opt
, 1, _("CONFLICT (rename/rename): "
1883 "Rename %s->%s in %s. "
1884 "Rename %s->%s in %s"),
1885 a
->path
, c1
->path
, ci
->ren1
->branch
,
1886 b
->path
, c2
->path
, ci
->ren2
->branch
);
1888 path_side_1_desc
= xstrfmt("version of %s from %s", path
, a
->path
);
1889 path_side_2_desc
= xstrfmt("version of %s from %s", path
, b
->path
);
1890 ostage1
= ci
->ren1
->branch
== opt
->branch1
? 3 : 2;
1891 ostage2
= flip_stage(ostage1
);
1892 ci
->ren1
->src_entry
->stages
[ostage1
].path
= a
->path
;
1893 ci
->ren2
->src_entry
->stages
[ostage2
].path
= b
->path
;
1894 if (merge_mode_and_contents(opt
, a
, c1
,
1895 &ci
->ren1
->src_entry
->stages
[ostage1
],
1897 opt
->branch1
, opt
->branch2
,
1898 1 + opt
->priv
->call_depth
* 2, &mfi_c1
) ||
1899 merge_mode_and_contents(opt
, b
,
1900 &ci
->ren2
->src_entry
->stages
[ostage2
],
1901 c2
, path_side_2_desc
,
1902 opt
->branch1
, opt
->branch2
,
1903 1 + opt
->priv
->call_depth
* 2, &mfi_c2
))
1905 free(path_side_1_desc
);
1906 free(path_side_2_desc
);
1907 mfi_c1
.blob
.path
= path
;
1908 mfi_c2
.blob
.path
= path
;
1910 return handle_file_collision(opt
, path
, a
->path
, b
->path
,
1911 ci
->ren1
->branch
, ci
->ren2
->branch
,
1912 &mfi_c1
.blob
, &mfi_c2
.blob
);
1916 * Get the diff_filepairs changed between o_tree and tree.
1918 static struct diff_queue_struct
*get_diffpairs(struct merge_options
*opt
,
1919 struct tree
*o_tree
,
1922 struct diff_queue_struct
*ret
;
1923 struct diff_options opts
;
1925 repo_diff_setup(opt
->repo
, &opts
);
1926 opts
.flags
.recursive
= 1;
1927 opts
.flags
.rename_empty
= 0;
1928 opts
.detect_rename
= merge_detect_rename(opt
);
1930 * We do not have logic to handle the detection of copies. In
1931 * fact, it may not even make sense to add such logic: would we
1932 * really want a change to a base file to be propagated through
1933 * multiple other files by a merge?
1935 if (opts
.detect_rename
> DIFF_DETECT_RENAME
)
1936 opts
.detect_rename
= DIFF_DETECT_RENAME
;
1937 opts
.rename_limit
= (opt
->rename_limit
>= 0) ? opt
->rename_limit
: 7000;
1938 opts
.rename_score
= opt
->rename_score
;
1939 opts
.show_rename_progress
= opt
->show_rename_progress
;
1940 opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1941 diff_setup_done(&opts
);
1942 diff_tree_oid(&o_tree
->object
.oid
, &tree
->object
.oid
, "", &opts
);
1943 diffcore_std(&opts
);
1944 if (opts
.needed_rename_limit
> opt
->priv
->needed_rename_limit
)
1945 opt
->priv
->needed_rename_limit
= opts
.needed_rename_limit
;
1947 ret
= xmalloc(sizeof(*ret
));
1948 *ret
= diff_queued_diff
;
1950 opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1951 diff_queued_diff
.nr
= 0;
1952 diff_queued_diff
.queue
= NULL
;
1957 static int tree_has_path(struct repository
*r
, struct tree
*tree
,
1960 struct object_id hashy
;
1961 unsigned short mode_o
;
1963 return !get_tree_entry(r
,
1964 &tree
->object
.oid
, path
,
1969 * Return a new string that replaces the beginning portion (which matches
1970 * entry->dir), with entry->new_dir. In perl-speak:
1971 * new_path_name = (old_path =~ s/entry->dir/entry->new_dir/);
1973 * Caller must ensure that old_path starts with entry->dir + '/'.
1975 static char *apply_dir_rename(struct dir_rename_entry
*entry
,
1976 const char *old_path
)
1978 struct strbuf new_path
= STRBUF_INIT
;
1981 if (entry
->non_unique_new_dir
)
1984 oldlen
= strlen(entry
->dir
);
1985 if (entry
->new_dir
.len
== 0)
1987 * If someone renamed/merged a subdirectory into the root
1988 * directory (e.g. 'some/subdir' -> ''), then we want to
1991 * as the rename; we need to make old_path + oldlen advance
1992 * past the '/' character.
1995 newlen
= entry
->new_dir
.len
+ (strlen(old_path
) - oldlen
) + 1;
1996 strbuf_grow(&new_path
, newlen
);
1997 strbuf_addbuf(&new_path
, &entry
->new_dir
);
1998 strbuf_addstr(&new_path
, &old_path
[oldlen
]);
2000 return strbuf_detach(&new_path
, NULL
);
2003 static void get_renamed_dir_portion(const char *old_path
, const char *new_path
,
2004 char **old_dir
, char **new_dir
)
2006 char *end_of_old
, *end_of_new
;
2008 /* Default return values: NULL, meaning no rename */
2014 * "a/b/c/d/e/foo.c" -> "a/b/some/thing/else/e/foo.c"
2015 * the "e/foo.c" part is the same, we just want to know that
2016 * "a/b/c/d" was renamed to "a/b/some/thing/else"
2017 * so, for this example, this function returns "a/b/c/d" in
2018 * *old_dir and "a/b/some/thing/else" in *new_dir.
2022 * If the basename of the file changed, we don't care. We want
2023 * to know which portion of the directory, if any, changed.
2025 end_of_old
= strrchr(old_path
, '/');
2026 end_of_new
= strrchr(new_path
, '/');
2029 * If end_of_old is NULL, old_path wasn't in a directory, so there
2030 * could not be a directory rename (our rule elsewhere that a
2031 * directory which still exists is not considered to have been
2032 * renamed means the root directory can never be renamed -- because
2033 * the root directory always exists).
2036 return; /* Note: *old_dir and *new_dir are still NULL */
2039 * If new_path contains no directory (end_of_new is NULL), then we
2040 * have a rename of old_path's directory to the root directory.
2043 *old_dir
= xstrndup(old_path
, end_of_old
- old_path
);
2044 *new_dir
= xstrdup("");
2048 /* Find the first non-matching character traversing backwards */
2049 while (*--end_of_new
== *--end_of_old
&&
2050 end_of_old
!= old_path
&&
2051 end_of_new
!= new_path
)
2052 ; /* Do nothing; all in the while loop */
2055 * If both got back to the beginning of their strings, then the
2056 * directory didn't change at all, only the basename did.
2058 if (end_of_old
== old_path
&& end_of_new
== new_path
&&
2059 *end_of_old
== *end_of_new
)
2060 return; /* Note: *old_dir and *new_dir are still NULL */
2063 * If end_of_new got back to the beginning of its string, and
2064 * end_of_old got back to the beginning of some subdirectory, then
2065 * we have a rename/merge of a subdirectory into the root, which
2066 * needs slightly special handling.
2068 * Note: There is no need to consider the opposite case, with a
2069 * rename/merge of the root directory into some subdirectory
2070 * because as noted above the root directory always exists so it
2071 * cannot be considered to be renamed.
2073 if (end_of_new
== new_path
&&
2074 end_of_old
!= old_path
&& end_of_old
[-1] == '/') {
2075 *old_dir
= xstrndup(old_path
, --end_of_old
- old_path
);
2076 *new_dir
= xstrdup("");
2081 * We've found the first non-matching character in the directory
2082 * paths. That means the current characters we were looking at
2083 * were part of the first non-matching subdir name going back from
2084 * the end of the strings. Get the whole name by advancing both
2085 * end_of_old and end_of_new to the NEXT '/' character. That will
2086 * represent the entire directory rename.
2088 * The reason for the increment is cases like
2089 * a/b/star/foo/whatever.c -> a/b/tar/foo/random.c
2090 * After dropping the basename and going back to the first
2091 * non-matching character, we're now comparing:
2093 * and we want to be comparing:
2094 * a/b/star/ and a/b/tar/
2095 * but without the pre-increment, the one on the right would stay
2098 end_of_old
= strchr(++end_of_old
, '/');
2099 end_of_new
= strchr(++end_of_new
, '/');
2101 /* Copy the old and new directories into *old_dir and *new_dir. */
2102 *old_dir
= xstrndup(old_path
, end_of_old
- old_path
);
2103 *new_dir
= xstrndup(new_path
, end_of_new
- new_path
);
2106 static void remove_hashmap_entries(struct hashmap
*dir_renames
,
2107 struct string_list
*items_to_remove
)
2110 struct dir_rename_entry
*entry
;
2112 for (i
= 0; i
< items_to_remove
->nr
; i
++) {
2113 entry
= items_to_remove
->items
[i
].util
;
2114 hashmap_remove(dir_renames
, &entry
->ent
, NULL
);
2116 string_list_clear(items_to_remove
, 0);
2120 * See if there is a directory rename for path, and if there are any file
2121 * level conflicts for the renamed location. If there is a rename and
2122 * there are no conflicts, return the new name. Otherwise, return NULL.
2124 static char *handle_path_level_conflicts(struct merge_options
*opt
,
2126 struct dir_rename_entry
*entry
,
2127 struct hashmap
*collisions
,
2130 char *new_path
= NULL
;
2131 struct collision_entry
*collision_ent
;
2133 struct strbuf collision_paths
= STRBUF_INIT
;
2136 * entry has the mapping of old directory name to new directory name
2137 * that we want to apply to path.
2139 new_path
= apply_dir_rename(entry
, path
);
2142 /* This should only happen when entry->non_unique_new_dir set */
2143 if (!entry
->non_unique_new_dir
)
2144 BUG("entry->non_unique_new_dir not set and !new_path");
2145 output(opt
, 1, _("CONFLICT (directory rename split): "
2146 "Unclear where to place %s because directory "
2147 "%s was renamed to multiple other directories, "
2148 "with no destination getting a majority of the "
2156 * The caller needs to have ensured that it has pre-populated
2157 * collisions with all paths that map to new_path. Do a quick check
2158 * to ensure that's the case.
2160 collision_ent
= collision_find_entry(collisions
, new_path
);
2162 BUG("collision_ent is NULL");
2165 * Check for one-sided add/add/.../add conflicts, i.e.
2166 * where implicit renames from the other side doing
2167 * directory rename(s) can affect this side of history
2168 * to put multiple paths into the same location. Warn
2169 * and bail on directory renames for such paths.
2171 if (collision_ent
->reported_already
) {
2173 } else if (tree_has_path(opt
->repo
, tree
, new_path
)) {
2174 collision_ent
->reported_already
= 1;
2175 strbuf_add_separated_string_list(&collision_paths
, ", ",
2176 &collision_ent
->source_files
);
2177 output(opt
, 1, _("CONFLICT (implicit dir rename): Existing "
2178 "file/dir at %s in the way of implicit "
2179 "directory rename(s) putting the following "
2180 "path(s) there: %s."),
2181 new_path
, collision_paths
.buf
);
2183 } else if (collision_ent
->source_files
.nr
> 1) {
2184 collision_ent
->reported_already
= 1;
2185 strbuf_add_separated_string_list(&collision_paths
, ", ",
2186 &collision_ent
->source_files
);
2187 output(opt
, 1, _("CONFLICT (implicit dir rename): Cannot map "
2188 "more than one path to %s; implicit directory "
2189 "renames tried to put these paths there: %s"),
2190 new_path
, collision_paths
.buf
);
2194 /* Free memory we no longer need */
2195 strbuf_release(&collision_paths
);
2196 if (!clean
&& new_path
) {
2205 * There are a couple things we want to do at the directory level:
2206 * 1. Check for both sides renaming to the same thing, in order to avoid
2207 * implicit renaming of files that should be left in place. (See
2208 * testcase 6b in t6043 for details.)
2209 * 2. Prune directory renames if there are still files left in the
2210 * original directory. These represent a partial directory rename,
2211 * i.e. a rename where only some of the files within the directory
2212 * were renamed elsewhere. (Technically, this could be done earlier
2213 * in get_directory_renames(), except that would prevent us from
2214 * doing the previous check and thus failing testcase 6b.)
2215 * 3. Check for rename/rename(1to2) conflicts (at the directory level).
2216 * In the future, we could potentially record this info as well and
2217 * omit reporting rename/rename(1to2) conflicts for each path within
2218 * the affected directories, thus cleaning up the merge output.
2219 * NOTE: We do NOT check for rename/rename(2to1) conflicts at the
2220 * directory level, because merging directories is fine. If it
2221 * causes conflicts for files within those merged directories, then
2222 * that should be detected at the individual path level.
2224 static void handle_directory_level_conflicts(struct merge_options
*opt
,
2225 struct hashmap
*dir_re_head
,
2227 struct hashmap
*dir_re_merge
,
2230 struct hashmap_iter iter
;
2231 struct dir_rename_entry
*head_ent
;
2232 struct dir_rename_entry
*merge_ent
;
2234 struct string_list remove_from_head
= STRING_LIST_INIT_NODUP
;
2235 struct string_list remove_from_merge
= STRING_LIST_INIT_NODUP
;
2237 hashmap_for_each_entry(dir_re_head
, &iter
, head_ent
,
2238 ent
/* member name */) {
2239 merge_ent
= dir_rename_find_entry(dir_re_merge
, head_ent
->dir
);
2241 !head_ent
->non_unique_new_dir
&&
2242 !merge_ent
->non_unique_new_dir
&&
2243 !strbuf_cmp(&head_ent
->new_dir
, &merge_ent
->new_dir
)) {
2244 /* 1. Renamed identically; remove it from both sides */
2245 string_list_append(&remove_from_head
,
2246 head_ent
->dir
)->util
= head_ent
;
2247 strbuf_release(&head_ent
->new_dir
);
2248 string_list_append(&remove_from_merge
,
2249 merge_ent
->dir
)->util
= merge_ent
;
2250 strbuf_release(&merge_ent
->new_dir
);
2251 } else if (tree_has_path(opt
->repo
, head
, head_ent
->dir
)) {
2252 /* 2. This wasn't a directory rename after all */
2253 string_list_append(&remove_from_head
,
2254 head_ent
->dir
)->util
= head_ent
;
2255 strbuf_release(&head_ent
->new_dir
);
2259 remove_hashmap_entries(dir_re_head
, &remove_from_head
);
2260 remove_hashmap_entries(dir_re_merge
, &remove_from_merge
);
2262 hashmap_for_each_entry(dir_re_merge
, &iter
, merge_ent
,
2263 ent
/* member name */) {
2264 head_ent
= dir_rename_find_entry(dir_re_head
, merge_ent
->dir
);
2265 if (tree_has_path(opt
->repo
, merge
, merge_ent
->dir
)) {
2266 /* 2. This wasn't a directory rename after all */
2267 string_list_append(&remove_from_merge
,
2268 merge_ent
->dir
)->util
= merge_ent
;
2269 } else if (head_ent
&&
2270 !head_ent
->non_unique_new_dir
&&
2271 !merge_ent
->non_unique_new_dir
) {
2272 /* 3. rename/rename(1to2) */
2274 * We can assume it's not rename/rename(1to1) because
2275 * that was case (1), already checked above. So we
2276 * know that head_ent->new_dir and merge_ent->new_dir
2277 * are different strings.
2279 output(opt
, 1, _("CONFLICT (rename/rename): "
2280 "Rename directory %s->%s in %s. "
2281 "Rename directory %s->%s in %s"),
2282 head_ent
->dir
, head_ent
->new_dir
.buf
, opt
->branch1
,
2283 head_ent
->dir
, merge_ent
->new_dir
.buf
, opt
->branch2
);
2284 string_list_append(&remove_from_head
,
2285 head_ent
->dir
)->util
= head_ent
;
2286 strbuf_release(&head_ent
->new_dir
);
2287 string_list_append(&remove_from_merge
,
2288 merge_ent
->dir
)->util
= merge_ent
;
2289 strbuf_release(&merge_ent
->new_dir
);
2293 remove_hashmap_entries(dir_re_head
, &remove_from_head
);
2294 remove_hashmap_entries(dir_re_merge
, &remove_from_merge
);
2297 static struct hashmap
*get_directory_renames(struct diff_queue_struct
*pairs
)
2299 struct hashmap
*dir_renames
;
2300 struct hashmap_iter iter
;
2301 struct dir_rename_entry
*entry
;
2305 * Typically, we think of a directory rename as all files from a
2306 * certain directory being moved to a target directory. However,
2307 * what if someone first moved two files from the original
2308 * directory in one commit, and then renamed the directory
2309 * somewhere else in a later commit? At merge time, we just know
2310 * that files from the original directory went to two different
2311 * places, and that the bulk of them ended up in the same place.
2312 * We want each directory rename to represent where the bulk of the
2313 * files from that directory end up; this function exists to find
2314 * where the bulk of the files went.
2316 * The first loop below simply iterates through the list of file
2317 * renames, finding out how often each directory rename pair
2318 * possibility occurs.
2320 dir_renames
= xmalloc(sizeof(*dir_renames
));
2321 dir_rename_init(dir_renames
);
2322 for (i
= 0; i
< pairs
->nr
; ++i
) {
2323 struct string_list_item
*item
;
2325 struct diff_filepair
*pair
= pairs
->queue
[i
];
2326 char *old_dir
, *new_dir
;
2328 /* File not part of directory rename if it wasn't renamed */
2329 if (pair
->status
!= 'R')
2332 get_renamed_dir_portion(pair
->one
->path
, pair
->two
->path
,
2333 &old_dir
, &new_dir
);
2335 /* Directory didn't change at all; ignore this one. */
2338 entry
= dir_rename_find_entry(dir_renames
, old_dir
);
2340 entry
= xmalloc(sizeof(*entry
));
2341 dir_rename_entry_init(entry
, old_dir
);
2342 hashmap_put(dir_renames
, &entry
->ent
);
2346 item
= string_list_lookup(&entry
->possible_new_dirs
, new_dir
);
2348 item
= string_list_insert(&entry
->possible_new_dirs
,
2350 item
->util
= xcalloc(1, sizeof(int));
2359 * For each directory with files moved out of it, we find out which
2360 * target directory received the most files so we can declare it to
2361 * be the "winning" target location for the directory rename. This
2362 * winner gets recorded in new_dir. If there is no winner
2363 * (multiple target directories received the same number of files),
2364 * we set non_unique_new_dir. Once we've determined the winner (or
2365 * that there is no winner), we no longer need possible_new_dirs.
2367 hashmap_for_each_entry(dir_renames
, &iter
, entry
,
2368 ent
/* member name */) {
2373 for (i
= 0; i
< entry
->possible_new_dirs
.nr
; i
++) {
2374 int *count
= entry
->possible_new_dirs
.items
[i
].util
;
2378 else if (*count
> max
) {
2380 best
= entry
->possible_new_dirs
.items
[i
].string
;
2384 entry
->non_unique_new_dir
= 1;
2386 assert(entry
->new_dir
.len
== 0);
2387 strbuf_addstr(&entry
->new_dir
, best
);
2390 * The relevant directory sub-portion of the original full
2391 * filepaths were xstrndup'ed before inserting into
2392 * possible_new_dirs, and instead of manually iterating the
2393 * list and free'ing each, just lie and tell
2394 * possible_new_dirs that it did the strdup'ing so that it
2395 * will free them for us.
2397 entry
->possible_new_dirs
.strdup_strings
= 1;
2398 string_list_clear(&entry
->possible_new_dirs
, 1);
2404 static struct dir_rename_entry
*check_dir_renamed(const char *path
,
2405 struct hashmap
*dir_renames
)
2407 char *temp
= xstrdup(path
);
2409 struct dir_rename_entry
*entry
= NULL
;
2411 while ((end
= strrchr(temp
, '/'))) {
2413 entry
= dir_rename_find_entry(dir_renames
, temp
);
2421 static void compute_collisions(struct hashmap
*collisions
,
2422 struct hashmap
*dir_renames
,
2423 struct diff_queue_struct
*pairs
)
2428 * Multiple files can be mapped to the same path due to directory
2429 * renames done by the other side of history. Since that other
2430 * side of history could have merged multiple directories into one,
2431 * if our side of history added the same file basename to each of
2432 * those directories, then all N of them would get implicitly
2433 * renamed by the directory rename detection into the same path,
2434 * and we'd get an add/add/.../add conflict, and all those adds
2435 * from *this* side of history. This is not representable in the
2436 * index, and users aren't going to easily be able to make sense of
2437 * it. So we need to provide a good warning about what's
2438 * happening, and fall back to no-directory-rename detection
2439 * behavior for those paths.
2441 * See testcases 9e and all of section 5 from t6043 for examples.
2443 collision_init(collisions
);
2445 for (i
= 0; i
< pairs
->nr
; ++i
) {
2446 struct dir_rename_entry
*dir_rename_ent
;
2447 struct collision_entry
*collision_ent
;
2449 struct diff_filepair
*pair
= pairs
->queue
[i
];
2451 if (pair
->status
!= 'A' && pair
->status
!= 'R')
2453 dir_rename_ent
= check_dir_renamed(pair
->two
->path
,
2455 if (!dir_rename_ent
)
2458 new_path
= apply_dir_rename(dir_rename_ent
, pair
->two
->path
);
2461 * dir_rename_ent->non_unique_new_path is true, which
2462 * means there is no directory rename for us to use,
2463 * which means it won't cause us any additional
2467 collision_ent
= collision_find_entry(collisions
, new_path
);
2468 if (!collision_ent
) {
2469 CALLOC_ARRAY(collision_ent
, 1);
2470 hashmap_entry_init(&collision_ent
->ent
,
2472 hashmap_put(collisions
, &collision_ent
->ent
);
2473 collision_ent
->target_file
= new_path
;
2477 string_list_insert(&collision_ent
->source_files
,
2482 static char *check_for_directory_rename(struct merge_options
*opt
,
2485 struct hashmap
*dir_renames
,
2486 struct hashmap
*dir_rename_exclusions
,
2487 struct hashmap
*collisions
,
2490 char *new_path
= NULL
;
2491 struct dir_rename_entry
*entry
= check_dir_renamed(path
, dir_renames
);
2492 struct dir_rename_entry
*oentry
= NULL
;
2498 * This next part is a little weird. We do not want to do an
2499 * implicit rename into a directory we renamed on our side, because
2500 * that will result in a spurious rename/rename(1to2) conflict. An
2502 * Base commit: dumbdir/afile, otherdir/bfile
2503 * Side 1: smrtdir/afile, otherdir/bfile
2504 * Side 2: dumbdir/afile, dumbdir/bfile
2505 * Here, while working on Side 1, we could notice that otherdir was
2506 * renamed/merged to dumbdir, and change the diff_filepair for
2507 * otherdir/bfile into a rename into dumbdir/bfile. However, Side
2508 * 2 will notice the rename from dumbdir to smrtdir, and do the
2509 * transitive rename to move it from dumbdir/bfile to
2510 * smrtdir/bfile. That gives us bfile in dumbdir vs being in
2511 * smrtdir, a rename/rename(1to2) conflict. We really just want
2512 * the file to end up in smrtdir. And the way to achieve that is
2513 * to not let Side1 do the rename to dumbdir, since we know that is
2514 * the source of one of our directory renames.
2516 * That's why oentry and dir_rename_exclusions is here.
2518 * As it turns out, this also prevents N-way transient rename
2519 * confusion; See testcases 9c and 9d of t6043.
2521 oentry
= dir_rename_find_entry(dir_rename_exclusions
, entry
->new_dir
.buf
);
2523 output(opt
, 1, _("WARNING: Avoiding applying %s -> %s rename "
2524 "to %s, because %s itself was renamed."),
2525 entry
->dir
, entry
->new_dir
.buf
, path
, entry
->new_dir
.buf
);
2527 new_path
= handle_path_level_conflicts(opt
, path
, entry
,
2529 *clean_merge
&= (new_path
!= NULL
);
2535 static void apply_directory_rename_modifications(struct merge_options
*opt
,
2536 struct diff_filepair
*pair
,
2540 struct tree
*o_tree
,
2541 struct tree
*a_tree
,
2542 struct tree
*b_tree
,
2543 struct string_list
*entries
)
2545 struct string_list_item
*item
;
2546 int stage
= (tree
== a_tree
? 2 : 3);
2550 * In all cases where we can do directory rename detection,
2551 * unpack_trees() will have read pair->two->path into the
2552 * index and the working copy. We need to remove it so that
2553 * we can instead place it at new_path. It is guaranteed to
2554 * not be untracked (unpack_trees() would have errored out
2555 * saying the file would have been overwritten), but it might
2558 update_wd
= !was_dirty(opt
, pair
->two
->path
);
2560 output(opt
, 1, _("Refusing to lose dirty file at %s"),
2562 remove_file(opt
, 1, pair
->two
->path
, !update_wd
);
2564 /* Find or create a new re->dst_entry */
2565 item
= string_list_lookup(entries
, new_path
);
2568 * Since we're renaming on this side of history, and it's
2569 * due to a directory rename on the other side of history
2570 * (which we only allow when the directory in question no
2571 * longer exists on the other side of history), the
2572 * original entry for re->dst_entry is no longer
2575 re
->dst_entry
->processed
= 1;
2578 * ...because we'll be using this new one.
2580 re
->dst_entry
= item
->util
;
2583 * re->dst_entry is for the before-dir-rename path, and we
2584 * need it to hold information for the after-dir-rename
2585 * path. Before creating a new entry, we need to mark the
2586 * old one as unnecessary (...unless it is shared by
2587 * src_entry, i.e. this didn't use to be a rename, in which
2588 * case we can just allow the normal processing to happen
2591 if (pair
->status
== 'R')
2592 re
->dst_entry
->processed
= 1;
2594 re
->dst_entry
= insert_stage_data(opt
->repo
, new_path
,
2595 o_tree
, a_tree
, b_tree
,
2597 item
= string_list_insert(entries
, new_path
);
2598 item
->util
= re
->dst_entry
;
2602 * Update the stage_data with the information about the path we are
2603 * moving into place. That slot will be empty and available for us
2604 * to write to because of the collision checks in
2605 * handle_path_level_conflicts(). In other words,
2606 * re->dst_entry->stages[stage].oid will be the null_oid, so it's
2607 * open for us to write to.
2609 * It may be tempting to actually update the index at this point as
2610 * well, using update_stages_for_stage_data(), but as per the big
2611 * "NOTE" in update_stages(), doing so will modify the current
2612 * in-memory index which will break calls to would_lose_untracked()
2613 * that we need to make. Instead, we need to just make sure that
2614 * the various handle_rename_*() functions update the index
2615 * explicitly rather than relying on unpack_trees() to have done it.
2617 get_tree_entry(opt
->repo
,
2620 &re
->dst_entry
->stages
[stage
].oid
,
2621 &re
->dst_entry
->stages
[stage
].mode
);
2624 * Record the original change status (or 'type' of change). If it
2625 * was originally an add ('A'), this lets us differentiate later
2626 * between a RENAME_DELETE conflict and RENAME_VIA_DIR (they
2627 * otherwise look the same). If it was originally a rename ('R'),
2628 * this lets us remember and report accurately about the transitive
2629 * renaming that occurred via the directory rename detection. Also,
2630 * record the original destination name.
2632 re
->dir_rename_original_type
= pair
->status
;
2633 re
->dir_rename_original_dest
= pair
->two
->path
;
2636 * We don't actually look at pair->status again, but it seems
2637 * pedagogically correct to adjust it.
2642 * Finally, record the new location.
2644 pair
->two
->path
= new_path
;
2648 * Get information of all renames which occurred in 'pairs', making use of
2649 * any implicit directory renames inferred from the other side of history.
2650 * We need the three trees in the merge ('o_tree', 'a_tree' and 'b_tree')
2651 * to be able to associate the correct cache entries with the rename
2652 * information; tree is always equal to either a_tree or b_tree.
2654 static struct string_list
*get_renames(struct merge_options
*opt
,
2656 struct diff_queue_struct
*pairs
,
2657 struct hashmap
*dir_renames
,
2658 struct hashmap
*dir_rename_exclusions
,
2660 struct tree
*o_tree
,
2661 struct tree
*a_tree
,
2662 struct tree
*b_tree
,
2663 struct string_list
*entries
,
2667 struct hashmap collisions
;
2668 struct hashmap_iter iter
;
2669 struct collision_entry
*e
;
2670 struct string_list
*renames
;
2672 compute_collisions(&collisions
, dir_renames
, pairs
);
2673 CALLOC_ARRAY(renames
, 1);
2675 for (i
= 0; i
< pairs
->nr
; ++i
) {
2676 struct string_list_item
*item
;
2678 struct diff_filepair
*pair
= pairs
->queue
[i
];
2679 char *new_path
; /* non-NULL only with directory renames */
2681 if (pair
->status
!= 'A' && pair
->status
!= 'R') {
2682 diff_free_filepair(pair
);
2685 new_path
= check_for_directory_rename(opt
, pair
->two
->path
, tree
,
2687 dir_rename_exclusions
,
2690 if (pair
->status
!= 'R' && !new_path
) {
2691 diff_free_filepair(pair
);
2695 re
= xmalloc(sizeof(*re
));
2698 re
->branch
= branch
;
2699 re
->dir_rename_original_type
= '\0';
2700 re
->dir_rename_original_dest
= NULL
;
2701 item
= string_list_lookup(entries
, re
->pair
->one
->path
);
2703 re
->src_entry
= insert_stage_data(opt
->repo
,
2704 re
->pair
->one
->path
,
2705 o_tree
, a_tree
, b_tree
, entries
);
2707 re
->src_entry
= item
->util
;
2709 item
= string_list_lookup(entries
, re
->pair
->two
->path
);
2711 re
->dst_entry
= insert_stage_data(opt
->repo
,
2712 re
->pair
->two
->path
,
2713 o_tree
, a_tree
, b_tree
, entries
);
2715 re
->dst_entry
= item
->util
;
2716 item
= string_list_insert(renames
, pair
->one
->path
);
2719 apply_directory_rename_modifications(opt
, pair
, new_path
,
2725 hashmap_for_each_entry(&collisions
, &iter
, e
,
2726 ent
/* member name */) {
2727 free(e
->target_file
);
2728 string_list_clear(&e
->source_files
, 0);
2730 hashmap_clear_and_free(&collisions
, struct collision_entry
, ent
);
2734 static int process_renames(struct merge_options
*opt
,
2735 struct string_list
*a_renames
,
2736 struct string_list
*b_renames
)
2738 int clean_merge
= 1, i
, j
;
2739 struct string_list a_by_dst
= STRING_LIST_INIT_NODUP
;
2740 struct string_list b_by_dst
= STRING_LIST_INIT_NODUP
;
2741 const struct rename
*sre
;
2744 * FIXME: As string-list.h notes, it's O(n^2) to build a sorted
2745 * string_list one-by-one, but O(n log n) to build it unsorted and
2746 * then sort it. Note that as we build the list, we do not need to
2747 * check if the existing destination path is already in the list,
2748 * because the structure of diffcore_rename guarantees we won't
2751 for (i
= 0; i
< a_renames
->nr
; i
++) {
2752 sre
= a_renames
->items
[i
].util
;
2753 string_list_insert(&a_by_dst
, sre
->pair
->two
->path
)->util
2756 for (i
= 0; i
< b_renames
->nr
; i
++) {
2757 sre
= b_renames
->items
[i
].util
;
2758 string_list_insert(&b_by_dst
, sre
->pair
->two
->path
)->util
2762 for (i
= 0, j
= 0; i
< a_renames
->nr
|| j
< b_renames
->nr
;) {
2763 struct string_list
*renames1
, *renames2Dst
;
2764 struct rename
*ren1
= NULL
, *ren2
= NULL
;
2765 const char *ren1_src
, *ren1_dst
;
2766 struct string_list_item
*lookup
;
2768 if (i
>= a_renames
->nr
) {
2769 ren2
= b_renames
->items
[j
++].util
;
2770 } else if (j
>= b_renames
->nr
) {
2771 ren1
= a_renames
->items
[i
++].util
;
2773 int compare
= strcmp(a_renames
->items
[i
].string
,
2774 b_renames
->items
[j
].string
);
2776 ren1
= a_renames
->items
[i
++].util
;
2778 ren2
= b_renames
->items
[j
++].util
;
2781 /* TODO: refactor, so that 1/2 are not needed */
2783 renames1
= a_renames
;
2784 renames2Dst
= &b_by_dst
;
2786 renames1
= b_renames
;
2787 renames2Dst
= &a_by_dst
;
2791 if (ren1
->processed
)
2793 ren1
->processed
= 1;
2794 ren1
->dst_entry
->processed
= 1;
2795 /* BUG: We should only mark src_entry as processed if we
2796 * are not dealing with a rename + add-source case.
2798 ren1
->src_entry
->processed
= 1;
2800 ren1_src
= ren1
->pair
->one
->path
;
2801 ren1_dst
= ren1
->pair
->two
->path
;
2804 /* One file renamed on both sides */
2805 const char *ren2_src
= ren2
->pair
->one
->path
;
2806 const char *ren2_dst
= ren2
->pair
->two
->path
;
2807 enum rename_type rename_type
;
2808 if (strcmp(ren1_src
, ren2_src
) != 0)
2809 BUG("ren1_src != ren2_src");
2810 ren2
->dst_entry
->processed
= 1;
2811 ren2
->processed
= 1;
2812 if (strcmp(ren1_dst
, ren2_dst
) != 0) {
2813 rename_type
= RENAME_ONE_FILE_TO_TWO
;
2816 rename_type
= RENAME_ONE_FILE_TO_ONE
;
2817 /* BUG: We should only remove ren1_src in
2818 * the base stage (think of rename +
2819 * add-source cases).
2821 remove_file(opt
, 1, ren1_src
, 1);
2822 update_entry(ren1
->dst_entry
,
2827 setup_rename_conflict_info(rename_type
, opt
, ren1
, ren2
);
2828 } else if ((lookup
= string_list_lookup(renames2Dst
, ren1_dst
))) {
2829 /* Two different files renamed to the same thing */
2831 ren2
= lookup
->util
;
2832 ren2_dst
= ren2
->pair
->two
->path
;
2833 if (strcmp(ren1_dst
, ren2_dst
) != 0)
2834 BUG("ren1_dst != ren2_dst");
2837 ren2
->processed
= 1;
2839 * BUG: We should only mark src_entry as processed
2840 * if we are not dealing with a rename + add-source
2843 ren2
->src_entry
->processed
= 1;
2845 setup_rename_conflict_info(RENAME_TWO_FILES_TO_ONE
,
2848 /* Renamed in 1, maybe changed in 2 */
2849 /* we only use sha1 and mode of these */
2850 struct diff_filespec src_other
, dst_other
;
2854 * unpack_trees loads entries from common-commit
2855 * into stage 1, from head-commit into stage 2, and
2856 * from merge-commit into stage 3. We keep track
2857 * of which side corresponds to the rename.
2859 int renamed_stage
= a_renames
== renames1
? 2 : 3;
2860 int other_stage
= a_renames
== renames1
? 3 : 2;
2863 * Directory renames have a funny corner case...
2865 int renamed_to_self
= !strcmp(ren1_src
, ren1_dst
);
2867 /* BUG: We should only remove ren1_src in the base
2868 * stage and in other_stage (think of rename +
2871 if (!renamed_to_self
)
2872 remove_file(opt
, 1, ren1_src
,
2873 renamed_stage
== 2 ||
2874 !was_tracked(opt
, ren1_src
));
2876 oidcpy(&src_other
.oid
,
2877 &ren1
->src_entry
->stages
[other_stage
].oid
);
2878 src_other
.mode
= ren1
->src_entry
->stages
[other_stage
].mode
;
2879 oidcpy(&dst_other
.oid
,
2880 &ren1
->dst_entry
->stages
[other_stage
].oid
);
2881 dst_other
.mode
= ren1
->dst_entry
->stages
[other_stage
].mode
;
2884 if (oideq(&src_other
.oid
, null_oid()) &&
2885 ren1
->dir_rename_original_type
== 'A') {
2886 setup_rename_conflict_info(RENAME_VIA_DIR
,
2888 } else if (renamed_to_self
) {
2889 setup_rename_conflict_info(RENAME_NORMAL
,
2891 } else if (oideq(&src_other
.oid
, null_oid())) {
2892 setup_rename_conflict_info(RENAME_DELETE
,
2894 } else if ((dst_other
.mode
== ren1
->pair
->two
->mode
) &&
2895 oideq(&dst_other
.oid
, &ren1
->pair
->two
->oid
)) {
2897 * Added file on the other side identical to
2898 * the file being renamed: clean merge.
2899 * Also, there is no need to overwrite the
2900 * file already in the working copy, so call
2901 * update_file_flags() instead of
2904 if (update_file_flags(opt
,
2907 1, /* update_cache */
2910 } else if (!oideq(&dst_other
.oid
, null_oid())) {
2912 * Probably not a clean merge, but it's
2913 * premature to set clean_merge to 0 here,
2914 * because if the rename merges cleanly and
2915 * the merge exactly matches the newly added
2916 * file, then the merge will be clean.
2918 setup_rename_conflict_info(RENAME_ADD
,
2923 if (clean_merge
< 0)
2924 goto cleanup_and_return
;
2926 struct diff_filespec
*o
, *a
, *b
;
2927 src_other
.path
= (char *)ren1_src
;
2929 o
= ren1
->pair
->one
;
2930 if (a_renames
== renames1
) {
2931 a
= ren1
->pair
->two
;
2934 b
= ren1
->pair
->two
;
2937 update_entry(ren1
->dst_entry
, o
, a
, b
);
2938 setup_rename_conflict_info(RENAME_NORMAL
,
2944 string_list_clear(&a_by_dst
, 0);
2945 string_list_clear(&b_by_dst
, 0);
2950 struct rename_info
{
2951 struct string_list
*head_renames
;
2952 struct string_list
*merge_renames
;
2955 static void initial_cleanup_rename(struct diff_queue_struct
*pairs
,
2956 struct hashmap
*dir_renames
)
2958 struct hashmap_iter iter
;
2959 struct dir_rename_entry
*e
;
2961 hashmap_for_each_entry(dir_renames
, &iter
, e
,
2962 ent
/* member name */) {
2964 strbuf_release(&e
->new_dir
);
2965 /* possible_new_dirs already cleared in get_directory_renames */
2967 hashmap_clear_and_free(dir_renames
, struct dir_rename_entry
, ent
);
2974 static int detect_and_process_renames(struct merge_options
*opt
,
2975 struct tree
*common
,
2978 struct string_list
*entries
,
2979 struct rename_info
*ri
)
2981 struct diff_queue_struct
*head_pairs
, *merge_pairs
;
2982 struct hashmap
*dir_re_head
, *dir_re_merge
;
2985 ri
->head_renames
= NULL
;
2986 ri
->merge_renames
= NULL
;
2988 if (!merge_detect_rename(opt
))
2991 head_pairs
= get_diffpairs(opt
, common
, head
);
2992 merge_pairs
= get_diffpairs(opt
, common
, merge
);
2994 if ((opt
->detect_directory_renames
== MERGE_DIRECTORY_RENAMES_TRUE
) ||
2995 (opt
->detect_directory_renames
== MERGE_DIRECTORY_RENAMES_CONFLICT
&&
2996 !opt
->priv
->call_depth
)) {
2997 dir_re_head
= get_directory_renames(head_pairs
);
2998 dir_re_merge
= get_directory_renames(merge_pairs
);
3000 handle_directory_level_conflicts(opt
,
3002 dir_re_merge
, merge
);
3004 dir_re_head
= xmalloc(sizeof(*dir_re_head
));
3005 dir_re_merge
= xmalloc(sizeof(*dir_re_merge
));
3006 dir_rename_init(dir_re_head
);
3007 dir_rename_init(dir_re_merge
);
3010 ri
->head_renames
= get_renames(opt
, opt
->branch1
, head_pairs
,
3011 dir_re_merge
, dir_re_head
, head
,
3012 common
, head
, merge
, entries
,
3016 ri
->merge_renames
= get_renames(opt
, opt
->branch2
, merge_pairs
,
3017 dir_re_head
, dir_re_merge
, merge
,
3018 common
, head
, merge
, entries
,
3022 clean
&= process_renames(opt
, ri
->head_renames
, ri
->merge_renames
);
3026 * Some cleanup is deferred until cleanup_renames() because the
3027 * data structures are still needed and referenced in
3028 * process_entry(). But there are a few things we can free now.
3030 initial_cleanup_rename(head_pairs
, dir_re_head
);
3031 initial_cleanup_rename(merge_pairs
, dir_re_merge
);
3036 static void final_cleanup_rename(struct string_list
*rename
)
3038 const struct rename
*re
;
3044 for (i
= 0; i
< rename
->nr
; i
++) {
3045 re
= rename
->items
[i
].util
;
3046 diff_free_filepair(re
->pair
);
3048 string_list_clear(rename
, 1);
3052 static void final_cleanup_renames(struct rename_info
*re_info
)
3054 final_cleanup_rename(re_info
->head_renames
);
3055 final_cleanup_rename(re_info
->merge_renames
);
3058 static int read_oid_strbuf(struct merge_options
*opt
,
3059 const struct object_id
*oid
,
3063 enum object_type type
;
3065 buf
= repo_read_object_file(the_repository
, oid
, &type
, &size
);
3067 return err(opt
, _("cannot read object %s"), oid_to_hex(oid
));
3068 if (type
!= OBJ_BLOB
) {
3070 return err(opt
, _("object %s is not a blob"), oid_to_hex(oid
));
3072 strbuf_attach(dst
, buf
, size
, size
+ 1);
3076 static int blob_unchanged(struct merge_options
*opt
,
3077 const struct diff_filespec
*o
,
3078 const struct diff_filespec
*a
,
3079 int renormalize
, const char *path
)
3081 struct strbuf obuf
= STRBUF_INIT
;
3082 struct strbuf abuf
= STRBUF_INIT
;
3083 int ret
= 0; /* assume changed for safety */
3084 struct index_state
*idx
= opt
->repo
->index
;
3086 if (a
->mode
!= o
->mode
)
3088 if (oideq(&o
->oid
, &a
->oid
))
3093 if (read_oid_strbuf(opt
, &o
->oid
, &obuf
) ||
3094 read_oid_strbuf(opt
, &a
->oid
, &abuf
))
3097 * Note: binary | is used so that both renormalizations are
3098 * performed. Comparison can be skipped if both files are
3099 * unchanged since their sha1s have already been compared.
3101 if (renormalize_buffer(idx
, path
, obuf
.buf
, obuf
.len
, &obuf
) |
3102 renormalize_buffer(idx
, path
, abuf
.buf
, abuf
.len
, &abuf
))
3103 ret
= (obuf
.len
== abuf
.len
&& !memcmp(obuf
.buf
, abuf
.buf
, obuf
.len
));
3106 strbuf_release(&obuf
);
3107 strbuf_release(&abuf
);
3111 static int handle_modify_delete(struct merge_options
*opt
,
3113 const struct diff_filespec
*o
,
3114 const struct diff_filespec
*a
,
3115 const struct diff_filespec
*b
)
3117 const char *modify_branch
, *delete_branch
;
3118 const struct diff_filespec
*changed
;
3121 modify_branch
= opt
->branch1
;
3122 delete_branch
= opt
->branch2
;
3125 modify_branch
= opt
->branch2
;
3126 delete_branch
= opt
->branch1
;
3130 return handle_change_delete(opt
,
3133 modify_branch
, delete_branch
,
3134 _("modify"), _("modified"));
3137 static int handle_content_merge(struct merge_file_info
*mfi
,
3138 struct merge_options
*opt
,
3141 const struct diff_filespec
*o
,
3142 const struct diff_filespec
*a
,
3143 const struct diff_filespec
*b
,
3144 struct rename_conflict_info
*ci
)
3146 const char *reason
= _("content");
3147 unsigned df_conflict_remains
= 0;
3150 reason
= _("add/add");
3152 assert(o
->path
&& a
->path
&& b
->path
);
3153 if (ci
&& dir_in_way(opt
->repo
->index
, path
, !opt
->priv
->call_depth
,
3154 S_ISGITLINK(ci
->ren1
->pair
->two
->mode
)))
3155 df_conflict_remains
= 1;
3157 if (merge_mode_and_contents(opt
, o
, a
, b
, path
,
3158 opt
->branch1
, opt
->branch2
,
3159 opt
->priv
->call_depth
* 2, mfi
))
3163 * We can skip updating the working tree file iff:
3164 * a) The merge is clean
3165 * b) The merge matches what was in HEAD (content, mode, pathname)
3166 * c) The target path is usable (i.e. not involved in D/F conflict)
3168 if (mfi
->clean
&& was_tracked_and_matches(opt
, path
, &mfi
->blob
) &&
3169 !df_conflict_remains
) {
3171 struct cache_entry
*ce
;
3173 output(opt
, 3, _("Skipped %s (merged same as existing)"), path
);
3174 if (add_cacheinfo(opt
, &mfi
->blob
, path
,
3175 0, (!opt
->priv
->call_depth
&& !is_dirty
), 0))
3178 * However, add_cacheinfo() will delete the old cache entry
3179 * and add a new one. We need to copy over any skip_worktree
3180 * flag to avoid making the file appear as if it were
3181 * deleted by the user.
3183 pos
= index_name_pos(&opt
->priv
->orig_index
, path
, strlen(path
));
3184 ce
= opt
->priv
->orig_index
.cache
[pos
];
3185 if (ce_skip_worktree(ce
)) {
3186 pos
= index_name_pos(opt
->repo
->index
, path
, strlen(path
));
3187 ce
= opt
->repo
->index
->cache
[pos
];
3188 ce
->ce_flags
|= CE_SKIP_WORKTREE
;
3194 if (S_ISGITLINK(mfi
->blob
.mode
))
3195 reason
= _("submodule");
3196 output(opt
, 1, _("CONFLICT (%s): Merge conflict in %s"),
3198 if (ci
&& !df_conflict_remains
)
3199 if (update_stages(opt
, path
, o
, a
, b
))
3203 if (df_conflict_remains
|| is_dirty
) {
3205 if (opt
->priv
->call_depth
) {
3206 remove_file_from_index(opt
->repo
->index
, path
);
3209 if (update_stages(opt
, path
, o
, a
, b
))
3212 int file_from_stage2
= was_tracked(opt
, path
);
3214 if (update_stages(opt
, path
, NULL
,
3215 file_from_stage2
? &mfi
->blob
: NULL
,
3216 file_from_stage2
? NULL
: &mfi
->blob
))
3221 new_path
= unique_path(opt
, path
, ci
->ren1
->branch
);
3223 output(opt
, 1, _("Refusing to lose dirty file at %s"),
3226 output(opt
, 1, _("Adding as %s instead"), new_path
);
3227 if (update_file(opt
, 0, &mfi
->blob
, new_path
)) {
3233 } else if (update_file(opt
, mfi
->clean
, &mfi
->blob
, path
))
3235 return !is_dirty
&& mfi
->clean
;
3238 static int handle_rename_normal(struct merge_options
*opt
,
3240 const struct diff_filespec
*o
,
3241 const struct diff_filespec
*a
,
3242 const struct diff_filespec
*b
,
3243 struct rename_conflict_info
*ci
)
3245 struct rename
*ren
= ci
->ren1
;
3246 struct merge_file_info mfi
;
3249 /* Merge the content and write it out */
3250 clean
= handle_content_merge(&mfi
, opt
, path
, was_dirty(opt
, path
),
3254 opt
->detect_directory_renames
== MERGE_DIRECTORY_RENAMES_CONFLICT
&&
3255 ren
->dir_rename_original_dest
) {
3256 if (update_stages(opt
, path
,
3257 &mfi
.blob
, &mfi
.blob
, &mfi
.blob
))
3259 clean
= 0; /* not clean, but conflicted */
3264 static void dir_rename_warning(const char *msg
,
3267 struct merge_options
*opt
,
3270 const char *other_branch
;
3271 other_branch
= (ren
->branch
== opt
->branch1
?
3272 opt
->branch2
: opt
->branch1
);
3274 output(opt
, clean
? 2 : 1, msg
,
3275 ren
->pair
->one
->path
, ren
->branch
,
3276 other_branch
, ren
->pair
->two
->path
);
3279 output(opt
, clean
? 2 : 1, msg
,
3280 ren
->pair
->one
->path
, ren
->dir_rename_original_dest
, ren
->branch
,
3281 other_branch
, ren
->pair
->two
->path
);
3283 static int warn_about_dir_renamed_entries(struct merge_options
*opt
,
3287 int clean
= 1, is_add
;
3292 /* Return early if ren was not affected/created by a directory rename */
3293 if (!ren
->dir_rename_original_dest
)
3297 assert(opt
->detect_directory_renames
> MERGE_DIRECTORY_RENAMES_NONE
);
3298 assert(ren
->dir_rename_original_type
== 'A' ||
3299 ren
->dir_rename_original_type
== 'R');
3301 /* Check whether to treat directory renames as a conflict */
3302 clean
= (opt
->detect_directory_renames
== MERGE_DIRECTORY_RENAMES_TRUE
);
3304 is_add
= (ren
->dir_rename_original_type
== 'A');
3305 if (ren
->dir_rename_original_type
== 'A' && clean
) {
3306 msg
= _("Path updated: %s added in %s inside a "
3307 "directory that was renamed in %s; moving it to %s.");
3308 } else if (ren
->dir_rename_original_type
== 'A' && !clean
) {
3309 msg
= _("CONFLICT (file location): %s added in %s "
3310 "inside a directory that was renamed in %s, "
3311 "suggesting it should perhaps be moved to %s.");
3312 } else if (ren
->dir_rename_original_type
== 'R' && clean
) {
3313 msg
= _("Path updated: %s renamed to %s in %s, inside a "
3314 "directory that was renamed in %s; moving it to %s.");
3315 } else if (ren
->dir_rename_original_type
== 'R' && !clean
) {
3316 msg
= _("CONFLICT (file location): %s renamed to %s in %s, "
3317 "inside a directory that was renamed in %s, "
3318 "suggesting it should perhaps be moved to %s.");
3320 BUG("Impossible dir_rename_original_type/clean combination");
3322 dir_rename_warning(msg
, is_add
, clean
, opt
, ren
);
3327 /* Per entry merge function */
3328 static int process_entry(struct merge_options
*opt
,
3329 const char *path
, struct stage_data
*entry
)
3331 int clean_merge
= 1;
3332 int normalize
= opt
->renormalize
;
3334 struct diff_filespec
*o
= &entry
->stages
[1];
3335 struct diff_filespec
*a
= &entry
->stages
[2];
3336 struct diff_filespec
*b
= &entry
->stages
[3];
3337 int o_valid
= is_valid(o
);
3338 int a_valid
= is_valid(a
);
3339 int b_valid
= is_valid(b
);
3340 o
->path
= a
->path
= b
->path
= (char*)path
;
3342 entry
->processed
= 1;
3343 if (entry
->rename_conflict_info
) {
3344 struct rename_conflict_info
*ci
= entry
->rename_conflict_info
;
3345 struct diff_filespec
*temp
;
3348 path_clean
= warn_about_dir_renamed_entries(opt
, ci
->ren1
);
3349 path_clean
&= warn_about_dir_renamed_entries(opt
, ci
->ren2
);
3352 * For cases with a single rename, {o,a,b}->path have all been
3353 * set to the rename target path; we need to set two of these
3354 * back to the rename source.
3355 * For rename/rename conflicts, we'll manually fix paths below.
3357 temp
= (opt
->branch1
== ci
->ren1
->branch
) ? b
: a
;
3358 o
->path
= temp
->path
= ci
->ren1
->pair
->one
->path
;
3360 assert(opt
->branch1
== ci
->ren1
->branch
);
3363 switch (ci
->rename_type
) {
3365 case RENAME_ONE_FILE_TO_ONE
:
3366 clean_merge
= handle_rename_normal(opt
, path
, o
, a
, b
,
3369 case RENAME_VIA_DIR
:
3370 clean_merge
= handle_rename_via_dir(opt
, ci
);
3374 * Probably unclean merge, but if the renamed file
3375 * merges cleanly and the result can then be
3376 * two-way merged cleanly with the added file, I
3377 * guess it's a clean merge?
3379 clean_merge
= handle_rename_add(opt
, ci
);
3383 if (handle_rename_delete(opt
, ci
))
3386 case RENAME_ONE_FILE_TO_TWO
:
3388 * Manually fix up paths; note:
3389 * ren[12]->pair->one->path are equal.
3391 o
->path
= ci
->ren1
->pair
->one
->path
;
3392 a
->path
= ci
->ren1
->pair
->two
->path
;
3393 b
->path
= ci
->ren2
->pair
->two
->path
;
3396 if (handle_rename_rename_1to2(opt
, ci
))
3399 case RENAME_TWO_FILES_TO_ONE
:
3401 * Manually fix up paths; note,
3402 * ren[12]->pair->two->path are actually equal.
3405 a
->path
= ci
->ren1
->pair
->two
->path
;
3406 b
->path
= ci
->ren2
->pair
->two
->path
;
3409 * Probably unclean merge, but if the two renamed
3410 * files merge cleanly and the two resulting files
3411 * can then be two-way merged cleanly, I guess it's
3414 clean_merge
= handle_rename_rename_2to1(opt
, ci
);
3417 entry
->processed
= 0;
3420 if (path_clean
< clean_merge
)
3421 clean_merge
= path_clean
;
3422 } else if (o_valid
&& (!a_valid
|| !b_valid
)) {
3423 /* Case A: Deleted in one */
3424 if ((!a_valid
&& !b_valid
) ||
3425 (!b_valid
&& blob_unchanged(opt
, o
, a
, normalize
, path
)) ||
3426 (!a_valid
&& blob_unchanged(opt
, o
, b
, normalize
, path
))) {
3427 /* Deleted in both or deleted in one and
3428 * unchanged in the other */
3430 output(opt
, 2, _("Removing %s"), path
);
3431 /* do not touch working file if it did not exist */
3432 remove_file(opt
, 1, path
, !a_valid
);
3434 /* Modify/delete; deleted side may have put a directory in the way */
3436 if (handle_modify_delete(opt
, path
, o
, a
, b
))
3439 } else if ((!o_valid
&& a_valid
&& !b_valid
) ||
3440 (!o_valid
&& !a_valid
&& b_valid
)) {
3441 /* Case B: Added in one. */
3442 /* [nothing|directory] -> ([nothing|directory], file) */
3444 const char *add_branch
;
3445 const char *other_branch
;
3447 const struct diff_filespec
*contents
;
3450 add_branch
= opt
->branch1
;
3451 other_branch
= opt
->branch2
;
3453 conf
= _("file/directory");
3455 add_branch
= opt
->branch2
;
3456 other_branch
= opt
->branch1
;
3458 conf
= _("directory/file");
3460 if (dir_in_way(opt
->repo
->index
, path
,
3461 !opt
->priv
->call_depth
&& !S_ISGITLINK(a
->mode
),
3463 char *new_path
= unique_path(opt
, path
, add_branch
);
3465 output(opt
, 1, _("CONFLICT (%s): There is a directory with name %s in %s. "
3467 conf
, path
, other_branch
, path
, new_path
);
3468 if (update_file(opt
, 0, contents
, new_path
))
3470 else if (opt
->priv
->call_depth
)
3471 remove_file_from_index(opt
->repo
->index
, path
);
3474 output(opt
, 2, _("Adding %s"), path
);
3475 /* do not overwrite file if already present */
3476 if (update_file_flags(opt
, contents
, path
, 1, !a_valid
))
3479 } else if (a_valid
&& b_valid
) {
3481 /* Case C: Added in both (check for same permissions) */
3483 _("CONFLICT (add/add): Merge conflict in %s"),
3485 clean_merge
= handle_file_collision(opt
,
3491 /* case D: Modified in both, but differently. */
3492 struct merge_file_info mfi
;
3493 int is_dirty
= 0; /* unpack_trees would have bailed if dirty */
3494 clean_merge
= handle_content_merge(&mfi
, opt
, path
,
3498 } else if (!o_valid
&& !a_valid
&& !b_valid
) {
3500 * this entry was deleted altogether. a_mode == 0 means
3501 * we had that path and want to actively remove it.
3503 remove_file(opt
, 1, path
, !a
->mode
);
3505 BUG("fatal merge failure, shouldn't happen.");
3510 static int merge_trees_internal(struct merge_options
*opt
,
3513 struct tree
*merge_base
,
3514 struct tree
**result
)
3516 struct index_state
*istate
= opt
->repo
->index
;
3519 if (opt
->subtree_shift
) {
3520 merge
= shift_tree_object(opt
->repo
, head
, merge
,
3521 opt
->subtree_shift
);
3522 merge_base
= shift_tree_object(opt
->repo
, head
, merge_base
,
3523 opt
->subtree_shift
);
3526 if (oideq(&merge_base
->object
.oid
, &merge
->object
.oid
)) {
3527 output(opt
, 0, _("Already up to date."));
3532 code
= unpack_trees_start(opt
, merge_base
, head
, merge
);
3535 if (show(opt
, 4) || opt
->priv
->call_depth
)
3536 err(opt
, _("merging of trees %s and %s failed"),
3537 oid_to_hex(&head
->object
.oid
),
3538 oid_to_hex(&merge
->object
.oid
));
3539 unpack_trees_finish(opt
);
3543 if (unmerged_index(istate
)) {
3544 struct string_list
*entries
;
3545 struct rename_info re_info
;
3548 * Only need the hashmap while processing entries, so
3549 * initialize it here and free it when we are done running
3550 * through the entries. Keeping it in the merge_options as
3551 * opposed to decaring a local hashmap is for convenience
3552 * so that we don't have to pass it to around.
3554 hashmap_init(&opt
->priv
->current_file_dir_set
, path_hashmap_cmp
,
3556 get_files_dirs(opt
, head
);
3557 get_files_dirs(opt
, merge
);
3559 entries
= get_unmerged(opt
->repo
->index
);
3560 clean
= detect_and_process_renames(opt
, merge_base
, head
, merge
,
3562 record_df_conflict_files(opt
, entries
);
3565 for (i
= entries
->nr
-1; 0 <= i
; i
--) {
3566 const char *path
= entries
->items
[i
].string
;
3567 struct stage_data
*e
= entries
->items
[i
].util
;
3568 if (!e
->processed
) {
3569 int ret
= process_entry(opt
, path
, e
);
3578 for (i
= 0; i
< entries
->nr
; i
++) {
3579 struct stage_data
*e
= entries
->items
[i
].util
;
3581 BUG("unprocessed path??? %s",
3582 entries
->items
[i
].string
);
3586 final_cleanup_renames(&re_info
);
3588 string_list_clear(entries
, 1);
3591 hashmap_clear_and_free(&opt
->priv
->current_file_dir_set
,
3592 struct path_hashmap_entry
, e
);
3595 unpack_trees_finish(opt
);
3602 unpack_trees_finish(opt
);
3604 if (opt
->priv
->call_depth
&&
3605 !(*result
= write_in_core_index_as_tree(opt
->repo
)))
3612 * Merge the commits h1 and h2, returning a flag (int) indicating the
3613 * cleanness of the merge. Also, if opt->priv->call_depth, create a
3614 * virtual commit and write its location to *result.
3616 static int merge_recursive_internal(struct merge_options
*opt
,
3619 struct commit_list
*merge_bases
,
3620 struct commit
**result
)
3622 struct commit_list
*iter
;
3623 struct commit
*merged_merge_bases
;
3624 struct tree
*result_tree
;
3626 const char *ancestor_name
;
3627 struct strbuf merge_base_abbrev
= STRBUF_INIT
;
3630 output(opt
, 4, _("Merging:"));
3631 output_commit_title(opt
, h1
);
3632 output_commit_title(opt
, h2
);
3636 merge_bases
= repo_get_merge_bases(the_repository
, h1
, h2
);
3637 merge_bases
= reverse_commit_list(merge_bases
);
3641 unsigned cnt
= commit_list_count(merge_bases
);
3643 output(opt
, 5, Q_("found %u common ancestor:",
3644 "found %u common ancestors:", cnt
), cnt
);
3645 for (iter
= merge_bases
; iter
; iter
= iter
->next
)
3646 output_commit_title(opt
, iter
->item
);
3649 merged_merge_bases
= pop_commit(&merge_bases
);
3650 if (!merged_merge_bases
) {
3651 /* if there is no common ancestor, use an empty tree */
3654 tree
= lookup_tree(opt
->repo
, opt
->repo
->hash_algo
->empty_tree
);
3655 merged_merge_bases
= make_virtual_commit(opt
->repo
, tree
,
3657 ancestor_name
= "empty tree";
3658 } else if (opt
->ancestor
&& !opt
->priv
->call_depth
) {
3659 ancestor_name
= opt
->ancestor
;
3660 } else if (merge_bases
) {
3661 ancestor_name
= "merged common ancestors";
3663 strbuf_add_unique_abbrev(&merge_base_abbrev
,
3664 &merged_merge_bases
->object
.oid
,
3666 ancestor_name
= merge_base_abbrev
.buf
;
3669 for (iter
= merge_bases
; iter
; iter
= iter
->next
) {
3670 const char *saved_b1
, *saved_b2
;
3671 opt
->priv
->call_depth
++;
3673 * When the merge fails, the result contains files
3674 * with conflict markers. The cleanness flag is
3675 * ignored (unless indicating an error), it was never
3676 * actually used, as result of merge_trees has always
3677 * overwritten it: the committed "conflicts" were
3680 discard_index(opt
->repo
->index
);
3681 saved_b1
= opt
->branch1
;
3682 saved_b2
= opt
->branch2
;
3683 opt
->branch1
= "Temporary merge branch 1";
3684 opt
->branch2
= "Temporary merge branch 2";
3685 if (merge_recursive_internal(opt
, merged_merge_bases
, iter
->item
,
3686 NULL
, &merged_merge_bases
) < 0)
3688 opt
->branch1
= saved_b1
;
3689 opt
->branch2
= saved_b2
;
3690 opt
->priv
->call_depth
--;
3692 if (!merged_merge_bases
)
3693 return err(opt
, _("merge returned no commit"));
3697 * FIXME: Since merge_recursive_internal() is only ever called by
3698 * places that ensure the index is loaded first
3699 * (e.g. builtin/merge.c, rebase/sequencer, etc.), in the common
3700 * case where the merge base was unique that means when we get here
3701 * we immediately discard the index and re-read it, which is a
3702 * complete waste of time. We should only be discarding and
3703 * re-reading if we were forced to recurse.
3705 discard_index(opt
->repo
->index
);
3706 if (!opt
->priv
->call_depth
)
3707 repo_read_index(opt
->repo
);
3709 opt
->ancestor
= ancestor_name
;
3710 clean
= merge_trees_internal(opt
,
3711 repo_get_commit_tree(opt
->repo
, h1
),
3712 repo_get_commit_tree(opt
->repo
, h2
),
3713 repo_get_commit_tree(opt
->repo
,
3714 merged_merge_bases
),
3716 strbuf_release(&merge_base_abbrev
);
3717 opt
->ancestor
= NULL
; /* avoid accidental re-use of opt->ancestor */
3723 if (opt
->priv
->call_depth
) {
3724 *result
= make_virtual_commit(opt
->repo
, result_tree
,
3726 commit_list_insert(h1
, &(*result
)->parents
);
3727 commit_list_insert(h2
, &(*result
)->parents
->next
);
3732 static int merge_start(struct merge_options
*opt
, struct tree
*head
)
3734 struct strbuf sb
= STRBUF_INIT
;
3736 /* Sanity checks on opt */
3739 assert(opt
->branch1
&& opt
->branch2
);
3741 assert(opt
->detect_renames
>= -1 &&
3742 opt
->detect_renames
<= DIFF_DETECT_COPY
);
3743 assert(opt
->detect_directory_renames
>= MERGE_DIRECTORY_RENAMES_NONE
&&
3744 opt
->detect_directory_renames
<= MERGE_DIRECTORY_RENAMES_TRUE
);
3745 assert(opt
->rename_limit
>= -1);
3746 assert(opt
->rename_score
>= 0 && opt
->rename_score
<= MAX_SCORE
);
3747 assert(opt
->show_rename_progress
>= 0 && opt
->show_rename_progress
<= 1);
3749 assert(opt
->xdl_opts
>= 0);
3750 assert(opt
->recursive_variant
>= MERGE_VARIANT_NORMAL
&&
3751 opt
->recursive_variant
<= MERGE_VARIANT_THEIRS
);
3753 assert(opt
->verbosity
>= 0 && opt
->verbosity
<= 5);
3754 assert(opt
->buffer_output
<= 2);
3755 assert(opt
->obuf
.len
== 0);
3757 assert(opt
->priv
== NULL
);
3759 /* Not supported; option specific to merge-ort */
3760 assert(!opt
->record_conflict_msgs_as_headers
);
3761 assert(!opt
->msg_header_prefix
);
3763 /* Sanity check on repo state; index must match head */
3764 if (repo_index_has_changes(opt
->repo
, head
, &sb
)) {
3765 err(opt
, _("Your local changes to the following files would be overwritten by merge:\n %s"),
3767 strbuf_release(&sb
);
3771 CALLOC_ARRAY(opt
->priv
, 1);
3772 string_list_init_dup(&opt
->priv
->df_conflict_file_set
);
3776 static void merge_finalize(struct merge_options
*opt
)
3779 if (!opt
->priv
->call_depth
&& opt
->buffer_output
< 2)
3780 strbuf_release(&opt
->obuf
);
3782 diff_warn_rename_limit("merge.renamelimit",
3783 opt
->priv
->needed_rename_limit
, 0);
3784 FREE_AND_NULL(opt
->priv
);
3787 int merge_trees(struct merge_options
*opt
,
3790 struct tree
*merge_base
)
3793 struct tree
*ignored
;
3795 assert(opt
->ancestor
!= NULL
);
3797 if (merge_start(opt
, head
))
3799 clean
= merge_trees_internal(opt
, head
, merge
, merge_base
, &ignored
);
3800 merge_finalize(opt
);
3805 int merge_recursive(struct merge_options
*opt
,
3808 struct commit_list
*merge_bases
,
3809 struct commit
**result
)
3813 assert(opt
->ancestor
== NULL
||
3814 !strcmp(opt
->ancestor
, "constructed merge base"));
3816 prepare_repo_settings(opt
->repo
);
3817 opt
->repo
->settings
.command_requires_full_index
= 1;
3819 if (merge_start(opt
, repo_get_commit_tree(opt
->repo
, h1
)))
3821 clean
= merge_recursive_internal(opt
, h1
, h2
, merge_bases
, result
);
3822 merge_finalize(opt
);
3827 static struct commit
*get_ref(struct repository
*repo
,
3828 const struct object_id
*oid
,
3831 struct object
*object
;
3833 object
= deref_tag(repo
, parse_object(repo
, oid
),
3834 name
, strlen(name
));
3837 if (object
->type
== OBJ_TREE
)
3838 return make_virtual_commit(repo
, (struct tree
*)object
, name
);
3839 if (object
->type
!= OBJ_COMMIT
)
3841 if (repo_parse_commit(repo
, (struct commit
*)object
))
3843 return (struct commit
*)object
;
3846 int merge_recursive_generic(struct merge_options
*opt
,
3847 const struct object_id
*head
,
3848 const struct object_id
*merge
,
3849 int num_merge_bases
,
3850 const struct object_id
**merge_bases
,
3851 struct commit
**result
)
3854 struct lock_file lock
= LOCK_INIT
;
3855 struct commit
*head_commit
= get_ref(opt
->repo
, head
, opt
->branch1
);
3856 struct commit
*next_commit
= get_ref(opt
->repo
, merge
, opt
->branch2
);
3857 struct commit_list
*ca
= NULL
;
3861 for (i
= 0; i
< num_merge_bases
; ++i
) {
3862 struct commit
*base
;
3863 if (!(base
= get_ref(opt
->repo
, merge_bases
[i
],
3864 oid_to_hex(merge_bases
[i
]))))
3865 return err(opt
, _("Could not parse object '%s'"),
3866 oid_to_hex(merge_bases
[i
]));
3867 commit_list_insert(base
, &ca
);
3869 if (num_merge_bases
== 1)
3870 opt
->ancestor
= "constructed merge base";
3873 repo_hold_locked_index(opt
->repo
, &lock
, LOCK_DIE_ON_ERROR
);
3874 clean
= merge_recursive(opt
, head_commit
, next_commit
, ca
,
3877 rollback_lock_file(&lock
);
3881 if (write_locked_index(opt
->repo
->index
, &lock
,
3882 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
3883 return err(opt
, _("Unable to write index."));
3885 return clean
? 0 : 1;
3888 static void merge_recursive_config(struct merge_options
*opt
)
3891 int renormalize
= 0;
3892 git_config_get_int("merge.verbosity", &opt
->verbosity
);
3893 git_config_get_int("diff.renamelimit", &opt
->rename_limit
);
3894 git_config_get_int("merge.renamelimit", &opt
->rename_limit
);
3895 git_config_get_bool("merge.renormalize", &renormalize
);
3896 opt
->renormalize
= renormalize
;
3897 if (!git_config_get_string("diff.renames", &value
)) {
3898 opt
->detect_renames
= git_config_rename("diff.renames", value
);
3901 if (!git_config_get_string("merge.renames", &value
)) {
3902 opt
->detect_renames
= git_config_rename("merge.renames", value
);
3905 if (!git_config_get_string("merge.directoryrenames", &value
)) {
3906 int boolval
= git_parse_maybe_bool(value
);
3908 opt
->detect_directory_renames
= boolval
?
3909 MERGE_DIRECTORY_RENAMES_TRUE
:
3910 MERGE_DIRECTORY_RENAMES_NONE
;
3911 } else if (!strcasecmp(value
, "conflict")) {
3912 opt
->detect_directory_renames
=
3913 MERGE_DIRECTORY_RENAMES_CONFLICT
;
3914 } /* avoid erroring on values from future versions of git */
3917 git_config(git_xmerge_config
, NULL
);
3920 void init_merge_options(struct merge_options
*opt
,
3921 struct repository
*repo
)
3923 const char *merge_verbosity
;
3924 memset(opt
, 0, sizeof(struct merge_options
));
3928 opt
->detect_renames
= -1;
3929 opt
->detect_directory_renames
= MERGE_DIRECTORY_RENAMES_CONFLICT
;
3930 opt
->rename_limit
= -1;
3933 opt
->buffer_output
= 1;
3934 strbuf_init(&opt
->obuf
, 0);
3936 opt
->renormalize
= 0;
3938 merge_recursive_config(opt
);
3939 merge_verbosity
= getenv("GIT_MERGE_VERBOSITY");
3940 if (merge_verbosity
)
3941 opt
->verbosity
= strtol(merge_verbosity
, NULL
, 10);
3942 if (opt
->verbosity
>= 5)
3943 opt
->buffer_output
= 0;
3947 * For now, members of merge_options do not need deep copying, but
3948 * it may change in the future, in which case we would need to update
3949 * this, and also make a matching change to clear_merge_options() to
3950 * release the resources held by a copied instance.
3952 void copy_merge_options(struct merge_options
*dst
, struct merge_options
*src
)
3957 void clear_merge_options(struct merge_options
*opt UNUSED
)
3959 ; /* no-op as our copy is shallow right now */
3962 int parse_merge_opt(struct merge_options
*opt
, const char *s
)
3968 if (!strcmp(s
, "ours"))
3969 opt
->recursive_variant
= MERGE_VARIANT_OURS
;
3970 else if (!strcmp(s
, "theirs"))
3971 opt
->recursive_variant
= MERGE_VARIANT_THEIRS
;
3972 else if (!strcmp(s
, "subtree"))
3973 opt
->subtree_shift
= "";
3974 else if (skip_prefix(s
, "subtree=", &arg
))
3975 opt
->subtree_shift
= arg
;
3976 else if (!strcmp(s
, "patience"))
3977 opt
->xdl_opts
= DIFF_WITH_ALG(opt
, PATIENCE_DIFF
);
3978 else if (!strcmp(s
, "histogram"))
3979 opt
->xdl_opts
= DIFF_WITH_ALG(opt
, HISTOGRAM_DIFF
);
3980 else if (skip_prefix(s
, "diff-algorithm=", &arg
)) {
3981 long value
= parse_algorithm_value(arg
);
3984 /* clear out previous settings */
3985 DIFF_XDL_CLR(opt
, NEED_MINIMAL
);
3986 opt
->xdl_opts
&= ~XDF_DIFF_ALGORITHM_MASK
;
3987 opt
->xdl_opts
|= value
;
3989 else if (!strcmp(s
, "ignore-space-change"))
3990 DIFF_XDL_SET(opt
, IGNORE_WHITESPACE_CHANGE
);
3991 else if (!strcmp(s
, "ignore-all-space"))
3992 DIFF_XDL_SET(opt
, IGNORE_WHITESPACE
);
3993 else if (!strcmp(s
, "ignore-space-at-eol"))
3994 DIFF_XDL_SET(opt
, IGNORE_WHITESPACE_AT_EOL
);
3995 else if (!strcmp(s
, "ignore-cr-at-eol"))
3996 DIFF_XDL_SET(opt
, IGNORE_CR_AT_EOL
);
3997 else if (!strcmp(s
, "renormalize"))
3998 opt
->renormalize
= 1;
3999 else if (!strcmp(s
, "no-renormalize"))
4000 opt
->renormalize
= 0;
4001 else if (!strcmp(s
, "no-renames"))
4002 opt
->detect_renames
= 0;
4003 else if (!strcmp(s
, "find-renames")) {
4004 opt
->detect_renames
= 1;
4005 opt
->rename_score
= 0;
4007 else if (skip_prefix(s
, "find-renames=", &arg
) ||
4008 skip_prefix(s
, "rename-threshold=", &arg
)) {
4009 if ((opt
->rename_score
= parse_rename_score(&arg
)) == -1 || *arg
!= 0)
4011 opt
->detect_renames
= 1;
4014 * Please update $__git_merge_strategy_options in
4015 * git-completion.bash when you add new options