2 * Recursive Merge algorithm stolen from git-merge-recursive.py by
4 * The thieves were Alex Riesen and Johannes Schindelin, in June/July 2006
7 #define USE_THE_REPOSITORY_VARIABLE
9 #include "git-compat-util.h"
10 #include "merge-recursive.h"
13 #include "cache-tree.h"
15 #include "commit-reach.h"
20 #include "environment.h"
25 #include "match-trees.h"
26 #include "name-hash.h"
27 #include "object-file.h"
28 #include "object-name.h"
29 #include "object-store-ll.h"
31 #include "repository.h"
33 #include "sparse-index.h"
34 #include "string-list.h"
37 #include "tree-walk.h"
38 #include "unpack-trees.h"
39 #include "xdiff-interface.h"
41 struct merge_options_internal
{
43 int needed_rename_limit
;
44 struct hashmap current_file_dir_set
;
45 struct string_list df_conflict_file_set
;
46 struct unpack_trees_options unpack_opts
;
47 struct index_state orig_index
;
50 struct path_hashmap_entry
{
51 struct hashmap_entry e
;
52 char path
[FLEX_ARRAY
];
55 static int path_hashmap_cmp(const void *cmp_data UNUSED
,
56 const struct hashmap_entry
*eptr
,
57 const struct hashmap_entry
*entry_or_key
,
60 const struct path_hashmap_entry
*a
, *b
;
61 const char *key
= keydata
;
63 a
= container_of(eptr
, const struct path_hashmap_entry
, e
);
64 b
= container_of(entry_or_key
, const struct path_hashmap_entry
, e
);
66 return fspathcmp(a
->path
, key
? key
: b
->path
);
70 * For dir_rename_entry, directory names are stored as a full path from the
71 * toplevel of the repository and do not include a trailing '/'. Also:
73 * dir: original name of directory being renamed
74 * non_unique_new_dir: if true, could not determine new_dir
75 * new_dir: final name of directory being renamed
76 * possible_new_dirs: temporary used to help determine new_dir; see comments
77 * in get_directory_renames() for details
79 struct dir_rename_entry
{
80 struct hashmap_entry ent
;
82 unsigned non_unique_new_dir
:1;
83 struct strbuf new_dir
;
84 struct string_list possible_new_dirs
;
87 static struct dir_rename_entry
*dir_rename_find_entry(struct hashmap
*hashmap
,
90 struct dir_rename_entry key
;
94 hashmap_entry_init(&key
.ent
, strhash(dir
));
96 return hashmap_get_entry(hashmap
, &key
, ent
, NULL
);
99 static int dir_rename_cmp(const void *cmp_data UNUSED
,
100 const struct hashmap_entry
*eptr
,
101 const struct hashmap_entry
*entry_or_key
,
102 const void *keydata UNUSED
)
104 const struct dir_rename_entry
*e1
, *e2
;
106 e1
= container_of(eptr
, const struct dir_rename_entry
, ent
);
107 e2
= container_of(entry_or_key
, const struct dir_rename_entry
, ent
);
109 return strcmp(e1
->dir
, e2
->dir
);
112 static void dir_rename_init(struct hashmap
*map
)
114 hashmap_init(map
, dir_rename_cmp
, NULL
, 0);
117 static void dir_rename_entry_init(struct dir_rename_entry
*entry
,
120 hashmap_entry_init(&entry
->ent
, strhash(directory
));
121 entry
->dir
= directory
;
122 entry
->non_unique_new_dir
= 0;
123 strbuf_init(&entry
->new_dir
, 0);
124 string_list_init_nodup(&entry
->possible_new_dirs
);
127 struct collision_entry
{
128 struct hashmap_entry ent
;
130 struct string_list source_files
;
131 unsigned reported_already
:1;
134 static struct collision_entry
*collision_find_entry(struct hashmap
*hashmap
,
137 struct collision_entry key
;
139 hashmap_entry_init(&key
.ent
, strhash(target_file
));
140 key
.target_file
= target_file
;
141 return hashmap_get_entry(hashmap
, &key
, ent
, NULL
);
144 static int collision_cmp(const void *cmp_data UNUSED
,
145 const struct hashmap_entry
*eptr
,
146 const struct hashmap_entry
*entry_or_key
,
147 const void *keydata UNUSED
)
149 const struct collision_entry
*e1
, *e2
;
151 e1
= container_of(eptr
, const struct collision_entry
, ent
);
152 e2
= container_of(entry_or_key
, const struct collision_entry
, ent
);
154 return strcmp(e1
->target_file
, e2
->target_file
);
157 static void collision_init(struct hashmap
*map
)
159 hashmap_init(map
, collision_cmp
, NULL
, 0);
162 static void flush_output(struct merge_options
*opt
)
164 if (opt
->buffer_output
< 2 && opt
->obuf
.len
) {
165 fputs(opt
->obuf
.buf
, stdout
);
166 strbuf_reset(&opt
->obuf
);
170 __attribute__((format (printf
, 2, 3)))
171 static int err(struct merge_options
*opt
, const char *err
, ...)
175 if (opt
->buffer_output
< 2)
178 strbuf_complete(&opt
->obuf
, '\n');
179 strbuf_addstr(&opt
->obuf
, "error: ");
181 va_start(params
, err
);
182 strbuf_vaddf(&opt
->obuf
, err
, params
);
184 if (opt
->buffer_output
> 1)
185 strbuf_addch(&opt
->obuf
, '\n');
187 error("%s", opt
->obuf
.buf
);
188 strbuf_reset(&opt
->obuf
);
194 static struct tree
*shift_tree_object(struct repository
*repo
,
195 struct tree
*one
, struct tree
*two
,
196 const char *subtree_shift
)
198 struct object_id shifted
;
200 if (!*subtree_shift
) {
201 shift_tree(repo
, &one
->object
.oid
, &two
->object
.oid
, &shifted
, 0);
203 shift_tree_by(repo
, &one
->object
.oid
, &two
->object
.oid
, &shifted
,
206 if (oideq(&two
->object
.oid
, &shifted
))
208 return lookup_tree(repo
, &shifted
);
211 static inline void set_commit_tree(struct commit
*c
, struct tree
*t
)
216 static struct commit
*make_virtual_commit(struct repository
*repo
,
220 struct commit
*commit
= alloc_commit_node(repo
);
222 set_merge_remote_desc(commit
, comment
, (struct object
*)commit
);
223 set_commit_tree(commit
, tree
);
224 commit
->object
.parsed
= 1;
233 RENAME_ONE_FILE_TO_ONE
,
234 RENAME_ONE_FILE_TO_TWO
,
235 RENAME_TWO_FILES_TO_ONE
239 * Since we want to write the index eventually, we cannot reuse the index
240 * for these (temporary) data.
243 struct diff_filespec stages
[4]; /* mostly for oid & mode; maybe path */
244 struct rename_conflict_info
*rename_conflict_info
;
245 unsigned processed
:1;
249 unsigned processed
:1;
250 struct diff_filepair
*pair
;
251 const char *branch
; /* branch that the rename occurred on */
253 * If directory rename detection affected this rename, what was its
254 * original type ('A' or 'R') and it's original destination before
255 * the directory rename (otherwise, '\0' and NULL for these two vars).
257 char dir_rename_original_type
;
258 char *dir_rename_original_dest
;
260 * Purpose of src_entry and dst_entry:
262 * If 'before' is renamed to 'after' then src_entry will contain
263 * the versions of 'before' from the merge_base, HEAD, and MERGE in
264 * stages 1, 2, and 3; dst_entry will contain the respective
265 * versions of 'after' in corresponding locations. Thus, we have a
266 * total of six modes and oids, though some will be null. (Stage 0
267 * is ignored; we're interested in handling conflicts.)
269 * Since we don't turn on break-rewrites by default, neither
270 * src_entry nor dst_entry can have all three of their stages have
271 * non-null oids, meaning at most four of the six will be non-null.
272 * Also, since this is a rename, both src_entry and dst_entry will
273 * have at least one non-null oid, meaning at least two will be
274 * non-null. Of the six oids, a typical rename will have three be
275 * non-null. Only two implies a rename/delete, and four implies a
278 struct stage_data
*src_entry
;
279 struct stage_data
*dst_entry
;
282 struct rename_conflict_info
{
283 enum rename_type rename_type
;
288 static inline void setup_rename_conflict_info(enum rename_type rename_type
,
289 struct merge_options
*opt
,
293 struct rename_conflict_info
*ci
;
296 * When we have two renames involved, it's easiest to get the
297 * correct things into stage 2 and 3, and to make sure that the
298 * content merge puts HEAD before the other branch if we just
299 * ensure that branch1 == opt->branch1. So, simply flip arguments
300 * around if we don't have that.
302 if (ren2
&& ren1
->branch
!= opt
->branch1
) {
303 setup_rename_conflict_info(rename_type
, opt
, ren2
, ren1
);
308 ci
->rename_type
= rename_type
;
312 ci
->ren1
->dst_entry
->processed
= 0;
313 ci
->ren1
->dst_entry
->rename_conflict_info
= ci
;
315 ci
->ren2
->dst_entry
->rename_conflict_info
= ci
;
319 static int show(struct merge_options
*opt
, int v
)
321 return (!opt
->priv
->call_depth
&& opt
->verbosity
>= v
) ||
325 __attribute__((format (printf
, 3, 4)))
326 static void output(struct merge_options
*opt
, int v
, const char *fmt
, ...)
333 strbuf_addchars(&opt
->obuf
, ' ', opt
->priv
->call_depth
* 2);
336 strbuf_vaddf(&opt
->obuf
, fmt
, ap
);
339 strbuf_addch(&opt
->obuf
, '\n');
340 if (!opt
->buffer_output
)
344 static void repo_output_commit_title(struct merge_options
*opt
,
345 struct repository
*repo
,
346 struct commit
*commit
)
348 struct merge_remote_desc
*desc
;
350 strbuf_addchars(&opt
->obuf
, ' ', opt
->priv
->call_depth
* 2);
351 desc
= merge_remote_util(commit
);
353 strbuf_addf(&opt
->obuf
, "virtual %s\n", desc
->name
);
355 strbuf_repo_add_unique_abbrev(&opt
->obuf
, repo
,
358 strbuf_addch(&opt
->obuf
, ' ');
359 if (repo_parse_commit(repo
, commit
) != 0)
360 strbuf_addstr(&opt
->obuf
, _("(bad commit)\n"));
363 const char *msg
= repo_get_commit_buffer(repo
, commit
, NULL
);
364 int len
= find_commit_subject(msg
, &title
);
366 strbuf_addf(&opt
->obuf
, "%.*s\n", len
, title
);
367 repo_unuse_commit_buffer(repo
, commit
, msg
);
373 static void output_commit_title(struct merge_options
*opt
, struct commit
*commit
)
375 repo_output_commit_title(opt
, the_repository
, commit
);
378 static int add_cacheinfo(struct merge_options
*opt
,
379 const struct diff_filespec
*blob
,
380 const char *path
, int stage
, int refresh
, int options
)
382 struct index_state
*istate
= opt
->repo
->index
;
383 struct cache_entry
*ce
;
386 ce
= make_cache_entry(istate
, blob
->mode
, &blob
->oid
, path
, stage
, 0);
388 return err(opt
, _("add_cacheinfo failed for path '%s'; merge aborting."), path
);
390 ret
= add_index_entry(istate
, ce
, options
);
392 struct cache_entry
*nce
;
394 nce
= refresh_cache_entry(istate
, ce
,
395 CE_MATCH_REFRESH
| CE_MATCH_IGNORE_MISSING
);
397 return err(opt
, _("add_cacheinfo failed to refresh for path '%s'; merge aborting."), path
);
399 ret
= add_index_entry(istate
, nce
, options
);
404 static inline int merge_detect_rename(struct merge_options
*opt
)
406 return (opt
->detect_renames
>= 0) ? opt
->detect_renames
: 1;
409 static void init_tree_desc_from_tree(struct tree_desc
*desc
, struct tree
*tree
)
411 if (parse_tree(tree
) < 0)
413 init_tree_desc(desc
, &tree
->object
.oid
, tree
->buffer
, tree
->size
);
416 static int unpack_trees_start(struct merge_options
*opt
,
422 struct tree_desc t
[3];
423 struct index_state tmp_index
= INDEX_STATE_INIT(opt
->repo
);
425 memset(&opt
->priv
->unpack_opts
, 0, sizeof(opt
->priv
->unpack_opts
));
426 if (opt
->priv
->call_depth
)
427 opt
->priv
->unpack_opts
.index_only
= 1;
429 opt
->priv
->unpack_opts
.update
= 1;
430 /* FIXME: should only do this if !overwrite_ignore */
431 opt
->priv
->unpack_opts
.preserve_ignored
= 0;
433 opt
->priv
->unpack_opts
.merge
= 1;
434 opt
->priv
->unpack_opts
.head_idx
= 2;
435 opt
->priv
->unpack_opts
.fn
= threeway_merge
;
436 opt
->priv
->unpack_opts
.src_index
= opt
->repo
->index
;
437 opt
->priv
->unpack_opts
.dst_index
= &tmp_index
;
438 opt
->priv
->unpack_opts
.aggressive
= !merge_detect_rename(opt
);
439 setup_unpack_trees_porcelain(&opt
->priv
->unpack_opts
, "merge");
441 init_tree_desc_from_tree(t
+0, common
);
442 init_tree_desc_from_tree(t
+1, head
);
443 init_tree_desc_from_tree(t
+2, merge
);
445 rc
= unpack_trees(3, t
, &opt
->priv
->unpack_opts
);
446 cache_tree_free(&opt
->repo
->index
->cache_tree
);
449 * Update opt->repo->index to match the new results, AFTER saving a
450 * copy in opt->priv->orig_index. Update src_index to point to the
451 * saved copy. (verify_uptodate() checks src_index, and the original
452 * index is the one that had the necessary modification timestamps.)
454 opt
->priv
->orig_index
= *opt
->repo
->index
;
455 *opt
->repo
->index
= tmp_index
;
456 opt
->priv
->unpack_opts
.src_index
= &opt
->priv
->orig_index
;
461 static void unpack_trees_finish(struct merge_options
*opt
)
463 discard_index(&opt
->priv
->orig_index
);
464 clear_unpack_trees_porcelain(&opt
->priv
->unpack_opts
);
467 static int save_files_dirs(const struct object_id
*oid UNUSED
,
468 struct strbuf
*base
, const char *path
,
469 unsigned int mode
, void *context
)
471 struct path_hashmap_entry
*entry
;
472 int baselen
= base
->len
;
473 struct merge_options
*opt
= context
;
475 strbuf_addstr(base
, path
);
477 FLEX_ALLOC_MEM(entry
, path
, base
->buf
, base
->len
);
478 hashmap_entry_init(&entry
->e
, fspathhash(entry
->path
));
479 hashmap_add(&opt
->priv
->current_file_dir_set
, &entry
->e
);
481 strbuf_setlen(base
, baselen
);
482 return (S_ISDIR(mode
) ? READ_TREE_RECURSIVE
: 0);
485 static void get_files_dirs(struct merge_options
*opt
, struct tree
*tree
)
487 struct pathspec match_all
;
488 memset(&match_all
, 0, sizeof(match_all
));
489 read_tree(opt
->repo
, tree
,
490 &match_all
, save_files_dirs
, opt
);
493 static int get_tree_entry_if_blob(struct repository
*r
,
494 const struct object_id
*tree
,
496 struct diff_filespec
*dfs
)
500 ret
= get_tree_entry(r
, tree
, path
, &dfs
->oid
, &dfs
->mode
);
501 if (S_ISDIR(dfs
->mode
)) {
502 oidcpy(&dfs
->oid
, null_oid());
509 * Returns an index_entry instance which doesn't have to correspond to
510 * a real cache entry in Git's index.
512 static struct stage_data
*insert_stage_data(struct repository
*r
,
514 struct tree
*o
, struct tree
*a
, struct tree
*b
,
515 struct string_list
*entries
)
517 struct string_list_item
*item
;
518 struct stage_data
*e
= xcalloc(1, sizeof(struct stage_data
));
519 get_tree_entry_if_blob(r
, &o
->object
.oid
, path
, &e
->stages
[1]);
520 get_tree_entry_if_blob(r
, &a
->object
.oid
, path
, &e
->stages
[2]);
521 get_tree_entry_if_blob(r
, &b
->object
.oid
, path
, &e
->stages
[3]);
522 item
= string_list_insert(entries
, path
);
528 * Create a dictionary mapping file names to stage_data objects. The
529 * dictionary contains one entry for every path with a non-zero stage entry.
531 static struct string_list
*get_unmerged(struct index_state
*istate
)
533 struct string_list
*unmerged
= xmalloc(sizeof(struct string_list
));
536 string_list_init_dup(unmerged
);
538 /* TODO: audit for interaction with sparse-index. */
539 ensure_full_index(istate
);
540 for (i
= 0; i
< istate
->cache_nr
; i
++) {
541 struct string_list_item
*item
;
542 struct stage_data
*e
;
543 const struct cache_entry
*ce
= istate
->cache
[i
];
547 item
= string_list_lookup(unmerged
, ce
->name
);
549 item
= string_list_insert(unmerged
, ce
->name
);
550 item
->util
= xcalloc(1, sizeof(struct stage_data
));
553 e
->stages
[ce_stage(ce
)].mode
= ce
->ce_mode
;
554 oidcpy(&e
->stages
[ce_stage(ce
)].oid
, &ce
->oid
);
560 static int string_list_df_name_compare(const char *one
, const char *two
)
562 int onelen
= strlen(one
);
563 int twolen
= strlen(two
);
565 * Here we only care that entries for D/F conflicts are
566 * adjacent, in particular with the file of the D/F conflict
567 * appearing before files below the corresponding directory.
568 * The order of the rest of the list is irrelevant for us.
570 * To achieve this, we sort with df_name_compare and provide
571 * the mode S_IFDIR so that D/F conflicts will sort correctly.
572 * We use the mode S_IFDIR for everything else for simplicity,
573 * since in other cases any changes in their order due to
574 * sorting cause no problems for us.
576 int cmp
= df_name_compare(one
, onelen
, S_IFDIR
,
577 two
, twolen
, S_IFDIR
);
579 * Now that 'foo' and 'foo/bar' compare equal, we have to make sure
580 * that 'foo' comes before 'foo/bar'.
584 return onelen
- twolen
;
587 static void record_df_conflict_files(struct merge_options
*opt
,
588 struct string_list
*entries
)
590 /* If there is a D/F conflict and the file for such a conflict
591 * currently exists in the working tree, we want to allow it to be
592 * removed to make room for the corresponding directory if needed.
593 * The files underneath the directories of such D/F conflicts will
594 * be processed before the corresponding file involved in the D/F
595 * conflict. If the D/F directory ends up being removed by the
596 * merge, then we won't have to touch the D/F file. If the D/F
597 * directory needs to be written to the working copy, then the D/F
598 * file will simply be removed (in make_room_for_path()) to make
599 * room for the necessary paths. Note that if both the directory
600 * and the file need to be present, then the D/F file will be
601 * reinstated with a new unique name at the time it is processed.
603 struct string_list df_sorted_entries
= STRING_LIST_INIT_NODUP
;
604 const char *last_file
= NULL
;
609 * If we're merging merge-bases, we don't want to bother with
610 * any working directory changes.
612 if (opt
->priv
->call_depth
)
615 /* Ensure D/F conflicts are adjacent in the entries list. */
616 for (i
= 0; i
< entries
->nr
; i
++) {
617 struct string_list_item
*next
= &entries
->items
[i
];
618 string_list_append(&df_sorted_entries
, next
->string
)->util
=
621 df_sorted_entries
.cmp
= string_list_df_name_compare
;
622 string_list_sort(&df_sorted_entries
);
624 string_list_clear(&opt
->priv
->df_conflict_file_set
, 1);
625 for (i
= 0; i
< df_sorted_entries
.nr
; i
++) {
626 const char *path
= df_sorted_entries
.items
[i
].string
;
627 int len
= strlen(path
);
628 struct stage_data
*e
= df_sorted_entries
.items
[i
].util
;
631 * Check if last_file & path correspond to a D/F conflict;
632 * i.e. whether path is last_file+'/'+<something>.
633 * If so, record that it's okay to remove last_file to make
634 * room for path and friends if needed.
638 memcmp(path
, last_file
, last_len
) == 0 &&
639 path
[last_len
] == '/') {
640 string_list_insert(&opt
->priv
->df_conflict_file_set
, last_file
);
644 * Determine whether path could exist as a file in the
645 * working directory as a possible D/F conflict. This
646 * will only occur when it exists in stage 2 as a
649 if (S_ISREG(e
->stages
[2].mode
) || S_ISLNK(e
->stages
[2].mode
)) {
656 string_list_clear(&df_sorted_entries
, 0);
659 static int update_stages(struct merge_options
*opt
, const char *path
,
660 const struct diff_filespec
*o
,
661 const struct diff_filespec
*a
,
662 const struct diff_filespec
*b
)
666 * NOTE: It is usually a bad idea to call update_stages on a path
667 * before calling update_file on that same path, since it can
668 * sometimes lead to spurious "refusing to lose untracked file..."
669 * messages from update_file (via make_room_for path via
670 * would_lose_untracked). Instead, reverse the order of the calls
671 * (executing update_file first and then update_stages).
674 int options
= ADD_CACHE_OK_TO_ADD
| ADD_CACHE_SKIP_DFCHECK
;
676 if (remove_file_from_index(opt
->repo
->index
, path
))
679 if (add_cacheinfo(opt
, o
, path
, 1, 0, options
))
682 if (add_cacheinfo(opt
, a
, path
, 2, 0, options
))
685 if (add_cacheinfo(opt
, b
, path
, 3, 0, options
))
690 static void update_entry(struct stage_data
*entry
,
691 struct diff_filespec
*o
,
692 struct diff_filespec
*a
,
693 struct diff_filespec
*b
)
695 entry
->processed
= 0;
696 entry
->stages
[1].mode
= o
->mode
;
697 entry
->stages
[2].mode
= a
->mode
;
698 entry
->stages
[3].mode
= b
->mode
;
699 oidcpy(&entry
->stages
[1].oid
, &o
->oid
);
700 oidcpy(&entry
->stages
[2].oid
, &a
->oid
);
701 oidcpy(&entry
->stages
[3].oid
, &b
->oid
);
704 static int remove_file(struct merge_options
*opt
, int clean
,
705 const char *path
, int no_wd
)
707 int update_cache
= opt
->priv
->call_depth
|| clean
;
708 int update_working_directory
= !opt
->priv
->call_depth
&& !no_wd
;
711 if (remove_file_from_index(opt
->repo
->index
, path
))
714 if (update_working_directory
) {
716 struct cache_entry
*ce
;
717 ce
= index_file_exists(opt
->repo
->index
, path
, strlen(path
),
719 if (ce
&& ce_stage(ce
) == 0 && strcmp(path
, ce
->name
))
722 if (remove_path(path
))
728 /* add a string to a strbuf, but converting "/" to "_" */
729 static void add_flattened_path(struct strbuf
*out
, const char *s
)
732 strbuf_addstr(out
, s
);
733 for (; i
< out
->len
; i
++)
734 if (out
->buf
[i
] == '/')
738 static char *unique_path(struct merge_options
*opt
,
742 struct path_hashmap_entry
*entry
;
743 struct strbuf newpath
= STRBUF_INIT
;
747 strbuf_addf(&newpath
, "%s~", path
);
748 add_flattened_path(&newpath
, branch
);
750 base_len
= newpath
.len
;
751 while (hashmap_get_from_hash(&opt
->priv
->current_file_dir_set
,
752 fspathhash(newpath
.buf
), newpath
.buf
) ||
753 (!opt
->priv
->call_depth
&& file_exists(newpath
.buf
))) {
754 strbuf_setlen(&newpath
, base_len
);
755 strbuf_addf(&newpath
, "_%d", suffix
++);
758 FLEX_ALLOC_MEM(entry
, path
, newpath
.buf
, newpath
.len
);
759 hashmap_entry_init(&entry
->e
, fspathhash(entry
->path
));
760 hashmap_add(&opt
->priv
->current_file_dir_set
, &entry
->e
);
761 return strbuf_detach(&newpath
, NULL
);
765 * Check whether a directory in the index is in the way of an incoming
766 * file. Return 1 if so. If check_working_copy is non-zero, also
767 * check the working directory. If empty_ok is non-zero, also return
768 * 0 in the case where the working-tree dir exists but is empty.
770 static int dir_in_way(struct index_state
*istate
, const char *path
,
771 int check_working_copy
, int empty_ok
)
774 struct strbuf dirpath
= STRBUF_INIT
;
777 strbuf_addstr(&dirpath
, path
);
778 strbuf_addch(&dirpath
, '/');
780 pos
= index_name_pos(istate
, dirpath
.buf
, dirpath
.len
);
784 if (pos
< istate
->cache_nr
&&
785 !strncmp(dirpath
.buf
, istate
->cache
[pos
]->name
, dirpath
.len
)) {
786 strbuf_release(&dirpath
);
790 strbuf_release(&dirpath
);
791 return check_working_copy
&& !lstat(path
, &st
) && S_ISDIR(st
.st_mode
) &&
792 !(empty_ok
&& is_empty_dir(path
)) &&
793 !has_symlink_leading_path(path
, strlen(path
));
797 * Returns whether path was tracked in the index before the merge started,
798 * and its oid and mode match the specified values
800 static int was_tracked_and_matches(struct merge_options
*opt
, const char *path
,
801 const struct diff_filespec
*blob
)
803 int pos
= index_name_pos(&opt
->priv
->orig_index
, path
, strlen(path
));
804 struct cache_entry
*ce
;
807 /* we were not tracking this path before the merge */
810 /* See if the file we were tracking before matches */
811 ce
= opt
->priv
->orig_index
.cache
[pos
];
812 return (oideq(&ce
->oid
, &blob
->oid
) && ce
->ce_mode
== blob
->mode
);
816 * Returns whether path was tracked in the index before the merge started
818 static int was_tracked(struct merge_options
*opt
, const char *path
)
820 int pos
= index_name_pos(&opt
->priv
->orig_index
, path
, strlen(path
));
823 /* we were tracking this path before the merge */
829 static int would_lose_untracked(struct merge_options
*opt
, const char *path
)
831 struct index_state
*istate
= opt
->repo
->index
;
834 * This may look like it can be simplified to:
835 * return !was_tracked(opt, path) && file_exists(path)
836 * but it can't. This function needs to know whether path was in
837 * the working tree due to EITHER having been tracked in the index
838 * before the merge OR having been put into the working copy and
839 * index by unpack_trees(). Due to that either-or requirement, we
840 * check the current index instead of the original one.
842 * Note that we do not need to worry about merge-recursive itself
843 * updating the index after unpack_trees() and before calling this
844 * function, because we strictly require all code paths in
845 * merge-recursive to update the working tree first and the index
846 * second. Doing otherwise would break
847 * update_file()/would_lose_untracked(); see every comment in this
848 * file which mentions "update_stages".
850 int pos
= index_name_pos(istate
, path
, strlen(path
));
854 while (pos
< istate
->cache_nr
&&
855 !strcmp(path
, istate
->cache
[pos
]->name
)) {
857 * If stage #0, it is definitely tracked.
858 * If it has stage #2 then it was tracked
859 * before this merge started. All other
860 * cases the path was not tracked.
862 switch (ce_stage(istate
->cache
[pos
])) {
869 return file_exists(path
);
872 static int was_dirty(struct merge_options
*opt
, const char *path
)
874 struct cache_entry
*ce
;
877 if (opt
->priv
->call_depth
|| !was_tracked(opt
, path
))
880 ce
= index_file_exists(opt
->priv
->unpack_opts
.src_index
,
881 path
, strlen(path
), ignore_case
);
882 dirty
= verify_uptodate(ce
, &opt
->priv
->unpack_opts
) != 0;
886 static int make_room_for_path(struct merge_options
*opt
, const char *path
)
889 const char *msg
= _("failed to create path '%s'%s");
891 /* Unlink any D/F conflict files that are in the way */
892 for (i
= 0; i
< opt
->priv
->df_conflict_file_set
.nr
; i
++) {
893 const char *df_path
= opt
->priv
->df_conflict_file_set
.items
[i
].string
;
894 size_t pathlen
= strlen(path
);
895 size_t df_pathlen
= strlen(df_path
);
896 if (df_pathlen
< pathlen
&&
897 path
[df_pathlen
] == '/' &&
898 strncmp(path
, df_path
, df_pathlen
) == 0) {
900 _("Removing %s to make room for subdirectory\n"),
903 unsorted_string_list_delete_item(&opt
->priv
->df_conflict_file_set
,
909 /* Make sure leading directories are created */
910 status
= safe_create_leading_directories_const(path
);
912 if (status
== SCLD_EXISTS
)
913 /* something else exists */
914 return err(opt
, msg
, path
, _(": perhaps a D/F conflict?"));
915 return err(opt
, msg
, path
, "");
919 * Do not unlink a file in the work tree if we are not
922 if (would_lose_untracked(opt
, path
))
923 return err(opt
, _("refusing to lose untracked file at '%s'"),
926 /* Successful unlink is good.. */
929 /* .. and so is no existing file */
932 /* .. but not some other error (who really cares what?) */
933 return err(opt
, msg
, path
, _(": perhaps a D/F conflict?"));
936 static int update_file_flags(struct merge_options
*opt
,
937 const struct diff_filespec
*contents
,
944 if (opt
->priv
->call_depth
)
948 enum object_type type
;
952 if (S_ISGITLINK(contents
->mode
)) {
954 * We may later decide to recursively descend into
955 * the submodule directory and update its index
956 * and/or work tree, but we do not do that now.
962 buf
= repo_read_object_file(the_repository
, &contents
->oid
,
965 ret
= err(opt
, _("cannot read object %s '%s'"),
966 oid_to_hex(&contents
->oid
), path
);
969 if (type
!= OBJ_BLOB
) {
970 ret
= err(opt
, _("blob expected for %s '%s'"),
971 oid_to_hex(&contents
->oid
), path
);
974 if (S_ISREG(contents
->mode
)) {
975 struct strbuf strbuf
= STRBUF_INIT
;
976 if (convert_to_working_tree(opt
->repo
->index
,
977 path
, buf
, size
, &strbuf
, NULL
)) {
980 buf
= strbuf_detach(&strbuf
, NULL
);
984 if (make_room_for_path(opt
, path
) < 0) {
988 if (S_ISREG(contents
->mode
) ||
989 (!has_symlinks
&& S_ISLNK(contents
->mode
))) {
991 int mode
= (contents
->mode
& 0100 ? 0777 : 0666);
993 fd
= open(path
, O_WRONLY
| O_TRUNC
| O_CREAT
, mode
);
995 ret
= err(opt
, _("failed to open '%s': %s"),
996 path
, strerror(errno
));
999 write_in_full(fd
, buf
, size
);
1001 } else if (S_ISLNK(contents
->mode
)) {
1002 char *lnk
= xmemdupz(buf
, size
);
1003 safe_create_leading_directories_const(path
);
1005 if (symlink(lnk
, path
))
1006 ret
= err(opt
, _("failed to symlink '%s': %s"),
1007 path
, strerror(errno
));
1011 _("do not know what to do with %06o %s '%s'"),
1012 contents
->mode
, oid_to_hex(&contents
->oid
), path
);
1017 if (!ret
&& update_cache
) {
1018 int refresh
= (!opt
->priv
->call_depth
&&
1019 contents
->mode
!= S_IFGITLINK
);
1020 if (add_cacheinfo(opt
, contents
, path
, 0, refresh
,
1021 ADD_CACHE_OK_TO_ADD
))
1027 static int update_file(struct merge_options
*opt
,
1029 const struct diff_filespec
*contents
,
1032 return update_file_flags(opt
, contents
, path
,
1033 opt
->priv
->call_depth
|| clean
, !opt
->priv
->call_depth
);
1036 /* Low level file merging, update and removal */
1038 struct merge_file_info
{
1039 struct diff_filespec blob
; /* mostly use oid & mode; sometimes path */
1044 static int merge_3way(struct merge_options
*opt
,
1045 mmbuffer_t
*result_buf
,
1046 const struct diff_filespec
*o
,
1047 const struct diff_filespec
*a
,
1048 const struct diff_filespec
*b
,
1049 const char *branch1
,
1050 const char *branch2
,
1051 const int extra_marker_size
)
1053 mmfile_t orig
, src1
, src2
;
1054 struct ll_merge_options ll_opts
= LL_MERGE_OPTIONS_INIT
;
1055 char *base
, *name1
, *name2
;
1056 enum ll_merge_result merge_status
;
1058 ll_opts
.renormalize
= opt
->renormalize
;
1059 ll_opts
.extra_marker_size
= extra_marker_size
;
1060 ll_opts
.xdl_opts
= opt
->xdl_opts
;
1061 ll_opts
.conflict_style
= opt
->conflict_style
;
1063 if (opt
->priv
->call_depth
) {
1064 ll_opts
.virtual_ancestor
= 1;
1065 ll_opts
.variant
= 0;
1067 switch (opt
->recursive_variant
) {
1068 case MERGE_VARIANT_OURS
:
1069 ll_opts
.variant
= XDL_MERGE_FAVOR_OURS
;
1071 case MERGE_VARIANT_THEIRS
:
1072 ll_opts
.variant
= XDL_MERGE_FAVOR_THEIRS
;
1075 ll_opts
.variant
= 0;
1080 assert(a
->path
&& b
->path
&& o
->path
&& opt
->ancestor
);
1081 if (strcmp(a
->path
, b
->path
) || strcmp(a
->path
, o
->path
) != 0) {
1082 base
= mkpathdup("%s:%s", opt
->ancestor
, o
->path
);
1083 name1
= mkpathdup("%s:%s", branch1
, a
->path
);
1084 name2
= mkpathdup("%s:%s", branch2
, b
->path
);
1086 base
= mkpathdup("%s", opt
->ancestor
);
1087 name1
= mkpathdup("%s", branch1
);
1088 name2
= mkpathdup("%s", branch2
);
1091 read_mmblob(&orig
, &o
->oid
);
1092 read_mmblob(&src1
, &a
->oid
);
1093 read_mmblob(&src2
, &b
->oid
);
1096 * FIXME: Using a->path for normalization rules in ll_merge could be
1097 * wrong if we renamed from a->path to b->path. We should use the
1098 * target path for where the file will be written.
1100 merge_status
= ll_merge(result_buf
, a
->path
, &orig
, base
,
1101 &src1
, name1
, &src2
, name2
,
1102 opt
->repo
->index
, &ll_opts
);
1103 if (merge_status
== LL_MERGE_BINARY_CONFLICT
)
1104 warning("Cannot merge binary files: %s (%s vs. %s)",
1105 a
->path
, name1
, name2
);
1113 return merge_status
;
1116 static int find_first_merges(struct repository
*repo
,
1117 struct object_array
*result
, const char *path
,
1118 struct commit
*a
, struct commit
*b
)
1121 struct object_array merges
= OBJECT_ARRAY_INIT
;
1122 struct commit
*commit
;
1123 int contains_another
;
1125 char merged_revision
[GIT_MAX_HEXSZ
+ 2];
1126 const char *rev_args
[] = { "rev-list", "--merges", "--ancestry-path",
1127 "--all", merged_revision
, NULL
};
1128 struct rev_info revs
;
1129 struct setup_revision_opt rev_opts
;
1131 memset(result
, 0, sizeof(struct object_array
));
1132 memset(&rev_opts
, 0, sizeof(rev_opts
));
1134 /* get all revisions that merge commit a */
1135 xsnprintf(merged_revision
, sizeof(merged_revision
), "^%s",
1136 oid_to_hex(&a
->object
.oid
));
1137 repo_init_revisions(repo
, &revs
, NULL
);
1138 /* FIXME: can't handle linked worktrees in submodules yet */
1139 revs
.single_worktree
= path
!= NULL
;
1140 setup_revisions(ARRAY_SIZE(rev_args
)-1, rev_args
, &revs
, &rev_opts
);
1142 /* save all revisions from the above list that contain b */
1143 if (prepare_revision_walk(&revs
))
1144 die("revision walk setup failed");
1145 while ((commit
= get_revision(&revs
)) != NULL
) {
1146 struct object
*o
= &(commit
->object
);
1147 int ret
= repo_in_merge_bases(repo
, b
, commit
);
1149 object_array_clear(&merges
);
1150 release_revisions(&revs
);
1154 add_object_array(o
, NULL
, &merges
);
1156 reset_revision_walk();
1158 /* Now we've got all merges that contain a and b. Prune all
1159 * merges that contain another found merge and save them in
1162 for (i
= 0; i
< merges
.nr
; i
++) {
1163 struct commit
*m1
= (struct commit
*) merges
.objects
[i
].item
;
1165 contains_another
= 0;
1166 for (j
= 0; j
< merges
.nr
; j
++) {
1167 struct commit
*m2
= (struct commit
*) merges
.objects
[j
].item
;
1169 int ret
= repo_in_merge_bases(repo
, m2
, m1
);
1171 object_array_clear(&merges
);
1172 release_revisions(&revs
);
1176 contains_another
= 1;
1182 if (!contains_another
)
1183 add_object_array(merges
.objects
[i
].item
, NULL
, result
);
1186 object_array_clear(&merges
);
1187 release_revisions(&revs
);
1191 static void print_commit(struct repository
*repo
, struct commit
*commit
)
1193 struct strbuf sb
= STRBUF_INIT
;
1194 struct pretty_print_context ctx
= {0};
1195 ctx
.date_mode
.type
= DATE_NORMAL
;
1196 /* FIXME: Merge this with output_commit_title() */
1197 assert(!merge_remote_util(commit
));
1198 repo_format_commit_message(repo
, commit
, " %h: %m %s", &sb
, &ctx
);
1199 fprintf(stderr
, "%s\n", sb
.buf
);
1200 strbuf_release(&sb
);
1203 static int is_valid(const struct diff_filespec
*dfs
)
1205 return dfs
->mode
!= 0 && !is_null_oid(&dfs
->oid
);
1208 static int merge_submodule(struct merge_options
*opt
,
1209 struct object_id
*result
, const char *path
,
1210 const struct object_id
*base
, const struct object_id
*a
,
1211 const struct object_id
*b
)
1213 struct repository subrepo
;
1215 struct commit
*commit_base
, *commit_a
, *commit_b
;
1217 struct object_array merges
;
1220 int search
= !opt
->priv
->call_depth
;
1222 /* store a in result in case we fail */
1223 /* FIXME: This is the WRONG resolution for the recursive case when
1224 * we need to be careful to avoid accidentally matching either side.
1225 * Should probably use o instead there, much like we do for merging
1230 /* we can not handle deletion conflicts */
1231 if (is_null_oid(base
))
1238 if (repo_submodule_init(&subrepo
, opt
->repo
, path
, null_oid())) {
1239 output(opt
, 1, _("Failed to merge submodule %s (not checked out)"), path
);
1243 if (!(commit_base
= lookup_commit_reference(&subrepo
, base
)) ||
1244 !(commit_a
= lookup_commit_reference(&subrepo
, a
)) ||
1245 !(commit_b
= lookup_commit_reference(&subrepo
, b
))) {
1246 output(opt
, 1, _("Failed to merge submodule %s (commits not present)"), path
);
1250 /* check whether both changes are forward */
1251 ret2
= repo_in_merge_bases(&subrepo
, commit_base
, commit_a
);
1253 output(opt
, 1, _("Failed to merge submodule %s (repository corrupt)"), path
);
1258 ret2
= repo_in_merge_bases(&subrepo
, commit_base
, commit_b
);
1260 output(opt
, 1, _("Failed to merge submodule %s (repository corrupt)"), path
);
1265 output(opt
, 1, _("Failed to merge submodule %s (commits don't follow merge-base)"), path
);
1269 /* Case #1: a is contained in b or vice versa */
1270 ret2
= repo_in_merge_bases(&subrepo
, commit_a
, commit_b
);
1272 output(opt
, 1, _("Failed to merge submodule %s (repository corrupt)"), path
);
1279 output(opt
, 3, _("Fast-forwarding submodule %s to the following commit:"), path
);
1280 repo_output_commit_title(opt
, &subrepo
, commit_b
);
1281 } else if (show(opt
, 2))
1282 output(opt
, 2, _("Fast-forwarding submodule %s"), path
);
1289 ret2
= repo_in_merge_bases(&subrepo
, commit_b
, commit_a
);
1291 output(opt
, 1, _("Failed to merge submodule %s (repository corrupt)"), path
);
1298 output(opt
, 3, _("Fast-forwarding submodule %s to the following commit:"), path
);
1299 repo_output_commit_title(opt
, &subrepo
, commit_a
);
1300 } else if (show(opt
, 2))
1301 output(opt
, 2, _("Fast-forwarding submodule %s"), path
);
1310 * Case #2: There are one or more merges that contain a and b in
1311 * the submodule. If there is only one, then present it as a
1312 * suggestion to the user, but leave it marked unmerged so the
1313 * user needs to confirm the resolution.
1316 /* Skip the search if makes no sense to the calling context. */
1320 /* find commit which merges them */
1321 parent_count
= find_first_merges(&subrepo
, &merges
, path
,
1322 commit_a
, commit_b
);
1323 switch (parent_count
) {
1325 output(opt
, 1,_("Failed to merge submodule %s (repository corrupt)"), path
);
1329 output(opt
, 1, _("Failed to merge submodule %s (merge following commits not found)"), path
);
1333 output(opt
, 1, _("Failed to merge submodule %s (not fast-forward)"), path
);
1334 output(opt
, 2, _("Found a possible merge resolution for the submodule:\n"));
1335 print_commit(&subrepo
, (struct commit
*) merges
.objects
[0].item
);
1337 "If this is correct simply add it to the index "
1340 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
1341 "which will accept this suggestion.\n"),
1342 oid_to_hex(&merges
.objects
[0].item
->oid
), path
);
1346 output(opt
, 1, _("Failed to merge submodule %s (multiple merges found)"), path
);
1347 for (i
= 0; i
< merges
.nr
; i
++)
1348 print_commit(&subrepo
, (struct commit
*) merges
.objects
[i
].item
);
1351 object_array_clear(&merges
);
1353 repo_clear(&subrepo
);
1357 static int merge_mode_and_contents(struct merge_options
*opt
,
1358 const struct diff_filespec
*o
,
1359 const struct diff_filespec
*a
,
1360 const struct diff_filespec
*b
,
1361 const char *filename
,
1362 const char *branch1
,
1363 const char *branch2
,
1364 const int extra_marker_size
,
1365 struct merge_file_info
*result
)
1367 if (opt
->branch1
!= branch1
) {
1369 * It's weird getting a reverse merge with HEAD on the bottom
1370 * side of the conflict markers and the other branch on the
1373 return merge_mode_and_contents(opt
, o
, b
, a
,
1376 extra_marker_size
, result
);
1382 if ((S_IFMT
& a
->mode
) != (S_IFMT
& b
->mode
)) {
1385 * FIXME: This is a bad resolution for recursive case; for
1386 * the recursive case we want something that is unlikely to
1387 * accidentally match either side. Also, while it makes
1388 * sense to prefer regular files over symlinks, it doesn't
1389 * make sense to prefer regular files over submodules.
1391 if (S_ISREG(a
->mode
)) {
1392 result
->blob
.mode
= a
->mode
;
1393 oidcpy(&result
->blob
.oid
, &a
->oid
);
1395 result
->blob
.mode
= b
->mode
;
1396 oidcpy(&result
->blob
.oid
, &b
->oid
);
1399 if (!oideq(&a
->oid
, &o
->oid
) && !oideq(&b
->oid
, &o
->oid
))
1405 if (a
->mode
== b
->mode
|| a
->mode
== o
->mode
)
1406 result
->blob
.mode
= b
->mode
;
1408 result
->blob
.mode
= a
->mode
;
1409 if (b
->mode
!= o
->mode
) {
1415 if (oideq(&a
->oid
, &b
->oid
) || oideq(&a
->oid
, &o
->oid
))
1416 oidcpy(&result
->blob
.oid
, &b
->oid
);
1417 else if (oideq(&b
->oid
, &o
->oid
))
1418 oidcpy(&result
->blob
.oid
, &a
->oid
);
1419 else if (S_ISREG(a
->mode
)) {
1420 mmbuffer_t result_buf
;
1421 int ret
= 0, merge_status
;
1423 merge_status
= merge_3way(opt
, &result_buf
, o
, a
, b
,
1427 if ((merge_status
< 0) || !result_buf
.ptr
)
1428 ret
= err(opt
, _("failed to execute internal merge"));
1431 write_object_file(result_buf
.ptr
, result_buf
.size
,
1432 OBJ_BLOB
, &result
->blob
.oid
))
1433 ret
= err(opt
, _("unable to add %s to database"),
1436 free(result_buf
.ptr
);
1439 /* FIXME: bug, what if modes didn't match? */
1440 result
->clean
= (merge_status
== 0);
1441 } else if (S_ISGITLINK(a
->mode
)) {
1442 int clean
= merge_submodule(opt
, &result
->blob
.oid
,
1449 result
->clean
= clean
;
1450 } else if (S_ISLNK(a
->mode
)) {
1451 switch (opt
->recursive_variant
) {
1452 case MERGE_VARIANT_NORMAL
:
1453 oidcpy(&result
->blob
.oid
, &a
->oid
);
1454 if (!oideq(&a
->oid
, &b
->oid
))
1457 case MERGE_VARIANT_OURS
:
1458 oidcpy(&result
->blob
.oid
, &a
->oid
);
1460 case MERGE_VARIANT_THEIRS
:
1461 oidcpy(&result
->blob
.oid
, &b
->oid
);
1465 BUG("unsupported object type in the tree");
1469 output(opt
, 2, _("Auto-merging %s"), filename
);
1474 static int handle_rename_via_dir(struct merge_options
*opt
,
1475 struct rename_conflict_info
*ci
)
1478 * Handle file adds that need to be renamed due to directory rename
1479 * detection. This differs from handle_rename_normal, because
1480 * there is no content merge to do; just move the file into the
1481 * desired final location.
1483 const struct rename
*ren
= ci
->ren1
;
1484 const struct diff_filespec
*dest
= ren
->pair
->two
;
1485 char *file_path
= dest
->path
;
1486 int mark_conflicted
= (opt
->detect_directory_renames
==
1487 MERGE_DIRECTORY_RENAMES_CONFLICT
);
1488 assert(ren
->dir_rename_original_dest
);
1490 if (!opt
->priv
->call_depth
&& would_lose_untracked(opt
, dest
->path
)) {
1491 mark_conflicted
= 1;
1492 file_path
= unique_path(opt
, dest
->path
, ren
->branch
);
1493 output(opt
, 1, _("Error: Refusing to lose untracked file at %s; "
1494 "writing to %s instead."),
1495 dest
->path
, file_path
);
1498 if (mark_conflicted
) {
1500 * Write the file in worktree at file_path. In the index,
1501 * only record the file at dest->path in the appropriate
1504 if (update_file(opt
, 0, dest
, file_path
))
1506 if (file_path
!= dest
->path
)
1508 if (update_stages(opt
, dest
->path
, NULL
,
1509 ren
->branch
== opt
->branch1
? dest
: NULL
,
1510 ren
->branch
== opt
->branch1
? NULL
: dest
))
1512 return 0; /* not clean, but conflicted */
1514 /* Update dest->path both in index and in worktree */
1515 if (update_file(opt
, 1, dest
, dest
->path
))
1517 return 1; /* clean */
1521 static int handle_change_delete(struct merge_options
*opt
,
1522 const char *path
, const char *old_path
,
1523 const struct diff_filespec
*o
,
1524 const struct diff_filespec
*changed
,
1525 const char *change_branch
,
1526 const char *delete_branch
,
1527 const char *change
, const char *change_past
)
1529 char *alt_path
= NULL
;
1530 const char *update_path
= path
;
1533 if (dir_in_way(opt
->repo
->index
, path
, !opt
->priv
->call_depth
, 0) ||
1534 (!opt
->priv
->call_depth
&& would_lose_untracked(opt
, path
))) {
1535 update_path
= alt_path
= unique_path(opt
, path
, change_branch
);
1538 if (opt
->priv
->call_depth
) {
1540 * We cannot arbitrarily accept either a_sha or b_sha as
1541 * correct; since there is no true "middle point" between
1542 * them, simply reuse the base version for virtual merge base.
1544 ret
= remove_file_from_index(opt
->repo
->index
, path
);
1546 ret
= update_file(opt
, 0, o
, update_path
);
1549 * Despite the four nearly duplicate messages and argument
1550 * lists below and the ugliness of the nested if-statements,
1551 * having complete messages makes the job easier for
1554 * The slight variance among the cases is due to the fact
1556 * 1) directory/file conflicts (in effect if
1557 * !alt_path) could cause us to need to write the
1558 * file to a different path.
1559 * 2) renames (in effect if !old_path) could mean that
1560 * there are two names for the path that the user
1561 * may know the file by.
1565 output(opt
, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1566 "and %s in %s. Version %s of %s left in tree."),
1567 change
, path
, delete_branch
, change_past
,
1568 change_branch
, change_branch
, path
);
1570 output(opt
, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1571 "and %s to %s in %s. Version %s of %s left in tree."),
1572 change
, old_path
, delete_branch
, change_past
, path
,
1573 change_branch
, change_branch
, path
);
1577 output(opt
, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1578 "and %s in %s. Version %s of %s left in tree at %s."),
1579 change
, path
, delete_branch
, change_past
,
1580 change_branch
, change_branch
, path
, alt_path
);
1582 output(opt
, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1583 "and %s to %s in %s. Version %s of %s left in tree at %s."),
1584 change
, old_path
, delete_branch
, change_past
, path
,
1585 change_branch
, change_branch
, path
, alt_path
);
1589 * No need to call update_file() on path when change_branch ==
1590 * opt->branch1 && !alt_path, since that would needlessly touch
1591 * path. We could call update_file_flags() with update_cache=0
1592 * and update_wd=0, but that's a no-op.
1594 if (change_branch
!= opt
->branch1
|| alt_path
)
1595 ret
= update_file(opt
, 0, changed
, update_path
);
1602 static int handle_rename_delete(struct merge_options
*opt
,
1603 struct rename_conflict_info
*ci
)
1605 const struct rename
*ren
= ci
->ren1
;
1606 const struct diff_filespec
*orig
= ren
->pair
->one
;
1607 const struct diff_filespec
*dest
= ren
->pair
->two
;
1608 const char *rename_branch
= ren
->branch
;
1609 const char *delete_branch
= (opt
->branch1
== ren
->branch
?
1610 opt
->branch2
: opt
->branch1
);
1612 if (handle_change_delete(opt
,
1613 opt
->priv
->call_depth
? orig
->path
: dest
->path
,
1614 opt
->priv
->call_depth
? NULL
: orig
->path
,
1616 rename_branch
, delete_branch
,
1617 _("rename"), _("renamed")))
1620 if (opt
->priv
->call_depth
)
1621 return remove_file_from_index(opt
->repo
->index
, dest
->path
);
1623 return update_stages(opt
, dest
->path
, NULL
,
1624 rename_branch
== opt
->branch1
? dest
: NULL
,
1625 rename_branch
== opt
->branch1
? NULL
: dest
);
1628 static int handle_file_collision(struct merge_options
*opt
,
1629 const char *collide_path
,
1630 const char *prev_path1
,
1631 const char *prev_path2
,
1632 const char *branch1
, const char *branch2
,
1633 struct diff_filespec
*a
,
1634 struct diff_filespec
*b
)
1636 struct merge_file_info mfi
;
1637 struct diff_filespec null
;
1638 char *alt_path
= NULL
;
1639 const char *update_path
= collide_path
;
1642 * It's easiest to get the correct things into stage 2 and 3, and
1643 * to make sure that the content merge puts HEAD before the other
1644 * branch if we just ensure that branch1 == opt->branch1. So, simply
1645 * flip arguments around if we don't have that.
1647 if (branch1
!= opt
->branch1
) {
1648 return handle_file_collision(opt
, collide_path
,
1649 prev_path2
, prev_path1
,
1654 /* Remove rename sources if rename/add or rename/rename(2to1) */
1656 remove_file(opt
, 1, prev_path1
,
1657 opt
->priv
->call_depth
|| would_lose_untracked(opt
, prev_path1
));
1659 remove_file(opt
, 1, prev_path2
,
1660 opt
->priv
->call_depth
|| would_lose_untracked(opt
, prev_path2
));
1663 * Remove the collision path, if it wouldn't cause dirty contents
1664 * or an untracked file to get lost. We'll either overwrite with
1665 * merged contents, or just write out to differently named files.
1667 if (was_dirty(opt
, collide_path
)) {
1668 output(opt
, 1, _("Refusing to lose dirty file at %s"),
1670 update_path
= alt_path
= unique_path(opt
, collide_path
, "merged");
1671 } else if (would_lose_untracked(opt
, collide_path
)) {
1673 * Only way we get here is if both renames were from
1674 * a directory rename AND user had an untracked file
1675 * at the location where both files end up after the
1676 * two directory renames. See testcase 10d of t6043.
1678 output(opt
, 1, _("Refusing to lose untracked file at "
1679 "%s, even though it's in the way."),
1681 update_path
= alt_path
= unique_path(opt
, collide_path
, "merged");
1684 * FIXME: It's possible that the two files are identical
1685 * and that the current working copy happens to match, in
1686 * which case we are unnecessarily touching the working
1687 * tree file. It's not a likely enough scenario that I
1688 * want to code up the checks for it and a better fix is
1689 * available if we restructure how unpack_trees() and
1690 * merge-recursive interoperate anyway, so punting for
1693 remove_file(opt
, 0, collide_path
, 0);
1696 /* Store things in diff_filespecs for functions that need it */
1697 null
.path
= (char *)collide_path
;
1698 oidcpy(&null
.oid
, null_oid());
1701 if (merge_mode_and_contents(opt
, &null
, a
, b
, collide_path
,
1702 branch1
, branch2
, opt
->priv
->call_depth
* 2, &mfi
))
1704 mfi
.clean
&= !alt_path
;
1705 if (update_file(opt
, mfi
.clean
, &mfi
.blob
, update_path
))
1707 if (!mfi
.clean
&& !opt
->priv
->call_depth
&&
1708 update_stages(opt
, collide_path
, NULL
, a
, b
))
1712 * FIXME: If both a & b both started with conflicts (only possible
1713 * if they came from a rename/rename(2to1)), but had IDENTICAL
1714 * contents including those conflicts, then in the next line we claim
1715 * it was clean. If someone cares about this case, we should have the
1716 * caller notify us if we started with conflicts.
1721 static int handle_rename_add(struct merge_options
*opt
,
1722 struct rename_conflict_info
*ci
)
1724 /* a was renamed to c, and a separate c was added. */
1725 struct diff_filespec
*a
= ci
->ren1
->pair
->one
;
1726 struct diff_filespec
*c
= ci
->ren1
->pair
->two
;
1727 char *path
= c
->path
;
1728 char *prev_path_desc
;
1729 struct merge_file_info mfi
;
1731 const char *rename_branch
= ci
->ren1
->branch
;
1732 const char *add_branch
= (opt
->branch1
== rename_branch
?
1733 opt
->branch2
: opt
->branch1
);
1734 int other_stage
= (ci
->ren1
->branch
== opt
->branch1
? 3 : 2);
1736 output(opt
, 1, _("CONFLICT (rename/add): "
1737 "Rename %s->%s in %s. Added %s in %s"),
1738 a
->path
, c
->path
, rename_branch
,
1739 c
->path
, add_branch
);
1741 prev_path_desc
= xstrfmt("version of %s from %s", path
, a
->path
);
1742 ci
->ren1
->src_entry
->stages
[other_stage
].path
= a
->path
;
1743 if (merge_mode_and_contents(opt
, a
, c
,
1744 &ci
->ren1
->src_entry
->stages
[other_stage
],
1746 opt
->branch1
, opt
->branch2
,
1747 1 + opt
->priv
->call_depth
* 2, &mfi
))
1749 free(prev_path_desc
);
1751 ci
->ren1
->dst_entry
->stages
[other_stage
].path
= mfi
.blob
.path
= c
->path
;
1752 return handle_file_collision(opt
,
1753 c
->path
, a
->path
, NULL
,
1754 rename_branch
, add_branch
,
1756 &ci
->ren1
->dst_entry
->stages
[other_stage
]);
1759 static char *find_path_for_conflict(struct merge_options
*opt
,
1761 const char *branch1
,
1762 const char *branch2
)
1764 char *new_path
= NULL
;
1765 if (dir_in_way(opt
->repo
->index
, path
, !opt
->priv
->call_depth
, 0)) {
1766 new_path
= unique_path(opt
, path
, branch1
);
1767 output(opt
, 1, _("%s is a directory in %s adding "
1769 path
, branch2
, new_path
);
1770 } else if (would_lose_untracked(opt
, path
)) {
1771 new_path
= unique_path(opt
, path
, branch1
);
1772 output(opt
, 1, _("Refusing to lose untracked file"
1773 " at %s; adding as %s instead"),
1781 * Toggle the stage number between "ours" and "theirs" (2 and 3).
1783 static inline int flip_stage(int stage
)
1785 return (2 + 3) - stage
;
1788 static int handle_rename_rename_1to2(struct merge_options
*opt
,
1789 struct rename_conflict_info
*ci
)
1791 /* One file was renamed in both branches, but to different names. */
1792 struct merge_file_info mfi
;
1793 struct diff_filespec
*add
;
1794 struct diff_filespec
*o
= ci
->ren1
->pair
->one
;
1795 struct diff_filespec
*a
= ci
->ren1
->pair
->two
;
1796 struct diff_filespec
*b
= ci
->ren2
->pair
->two
;
1799 output(opt
, 1, _("CONFLICT (rename/rename): "
1800 "Rename \"%s\"->\"%s\" in branch \"%s\" "
1801 "rename \"%s\"->\"%s\" in \"%s\"%s"),
1802 o
->path
, a
->path
, ci
->ren1
->branch
,
1803 o
->path
, b
->path
, ci
->ren2
->branch
,
1804 opt
->priv
->call_depth
? _(" (left unresolved)") : "");
1806 path_desc
= xstrfmt("%s and %s, both renamed from %s",
1807 a
->path
, b
->path
, o
->path
);
1808 if (merge_mode_and_contents(opt
, o
, a
, b
, path_desc
,
1809 ci
->ren1
->branch
, ci
->ren2
->branch
,
1810 opt
->priv
->call_depth
* 2, &mfi
))
1814 if (opt
->priv
->call_depth
)
1815 remove_file_from_index(opt
->repo
->index
, o
->path
);
1818 * For each destination path, we need to see if there is a
1819 * rename/add collision. If not, we can write the file out
1820 * to the specified location.
1822 add
= &ci
->ren1
->dst_entry
->stages
[flip_stage(2)];
1823 if (is_valid(add
)) {
1824 add
->path
= mfi
.blob
.path
= a
->path
;
1825 if (handle_file_collision(opt
, a
->path
,
1829 &mfi
.blob
, add
) < 0)
1832 char *new_path
= find_path_for_conflict(opt
, a
->path
,
1835 if (update_file(opt
, 0, &mfi
.blob
,
1836 new_path
? new_path
: a
->path
))
1839 if (!opt
->priv
->call_depth
&&
1840 update_stages(opt
, a
->path
, NULL
, a
, NULL
))
1844 if (!mfi
.clean
&& mfi
.blob
.mode
== a
->mode
&&
1845 oideq(&mfi
.blob
.oid
, &a
->oid
)) {
1847 * Getting here means we were attempting to merge a binary
1848 * blob. Since we can't merge binaries, the merge algorithm
1849 * just takes one side. But we don't want to copy the
1850 * contents of one side to both paths; we'd rather use the
1851 * original content at the given path for each path.
1853 oidcpy(&mfi
.blob
.oid
, &b
->oid
);
1854 mfi
.blob
.mode
= b
->mode
;
1856 add
= &ci
->ren2
->dst_entry
->stages
[flip_stage(3)];
1857 if (is_valid(add
)) {
1858 add
->path
= mfi
.blob
.path
= b
->path
;
1859 if (handle_file_collision(opt
, b
->path
,
1863 add
, &mfi
.blob
) < 0)
1866 char *new_path
= find_path_for_conflict(opt
, b
->path
,
1869 if (update_file(opt
, 0, &mfi
.blob
,
1870 new_path
? new_path
: b
->path
))
1873 if (!opt
->priv
->call_depth
&&
1874 update_stages(opt
, b
->path
, NULL
, NULL
, b
))
1881 static int handle_rename_rename_2to1(struct merge_options
*opt
,
1882 struct rename_conflict_info
*ci
)
1884 /* Two files, a & b, were renamed to the same thing, c. */
1885 struct diff_filespec
*a
= ci
->ren1
->pair
->one
;
1886 struct diff_filespec
*b
= ci
->ren2
->pair
->one
;
1887 struct diff_filespec
*c1
= ci
->ren1
->pair
->two
;
1888 struct diff_filespec
*c2
= ci
->ren2
->pair
->two
;
1889 char *path
= c1
->path
; /* == c2->path */
1890 char *path_side_1_desc
;
1891 char *path_side_2_desc
;
1892 struct merge_file_info mfi_c1
;
1893 struct merge_file_info mfi_c2
;
1894 int ostage1
, ostage2
;
1896 output(opt
, 1, _("CONFLICT (rename/rename): "
1897 "Rename %s->%s in %s. "
1898 "Rename %s->%s in %s"),
1899 a
->path
, c1
->path
, ci
->ren1
->branch
,
1900 b
->path
, c2
->path
, ci
->ren2
->branch
);
1902 path_side_1_desc
= xstrfmt("version of %s from %s", path
, a
->path
);
1903 path_side_2_desc
= xstrfmt("version of %s from %s", path
, b
->path
);
1904 ostage1
= ci
->ren1
->branch
== opt
->branch1
? 3 : 2;
1905 ostage2
= flip_stage(ostage1
);
1906 ci
->ren1
->src_entry
->stages
[ostage1
].path
= a
->path
;
1907 ci
->ren2
->src_entry
->stages
[ostage2
].path
= b
->path
;
1908 if (merge_mode_and_contents(opt
, a
, c1
,
1909 &ci
->ren1
->src_entry
->stages
[ostage1
],
1911 opt
->branch1
, opt
->branch2
,
1912 1 + opt
->priv
->call_depth
* 2, &mfi_c1
) ||
1913 merge_mode_and_contents(opt
, b
,
1914 &ci
->ren2
->src_entry
->stages
[ostage2
],
1915 c2
, path_side_2_desc
,
1916 opt
->branch1
, opt
->branch2
,
1917 1 + opt
->priv
->call_depth
* 2, &mfi_c2
))
1919 free(path_side_1_desc
);
1920 free(path_side_2_desc
);
1921 mfi_c1
.blob
.path
= path
;
1922 mfi_c2
.blob
.path
= path
;
1924 return handle_file_collision(opt
, path
, a
->path
, b
->path
,
1925 ci
->ren1
->branch
, ci
->ren2
->branch
,
1926 &mfi_c1
.blob
, &mfi_c2
.blob
);
1930 * Get the diff_filepairs changed between o_tree and tree.
1932 static struct diff_queue_struct
*get_diffpairs(struct merge_options
*opt
,
1933 struct tree
*o_tree
,
1936 struct diff_queue_struct
*ret
;
1937 struct diff_options opts
;
1939 repo_diff_setup(opt
->repo
, &opts
);
1940 opts
.flags
.recursive
= 1;
1941 opts
.flags
.rename_empty
= 0;
1942 opts
.detect_rename
= merge_detect_rename(opt
);
1944 * We do not have logic to handle the detection of copies. In
1945 * fact, it may not even make sense to add such logic: would we
1946 * really want a change to a base file to be propagated through
1947 * multiple other files by a merge?
1949 if (opts
.detect_rename
> DIFF_DETECT_RENAME
)
1950 opts
.detect_rename
= DIFF_DETECT_RENAME
;
1951 opts
.rename_limit
= (opt
->rename_limit
>= 0) ? opt
->rename_limit
: 7000;
1952 opts
.rename_score
= opt
->rename_score
;
1953 opts
.show_rename_progress
= opt
->show_rename_progress
;
1954 opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1955 diff_setup_done(&opts
);
1956 diff_tree_oid(&o_tree
->object
.oid
, &tree
->object
.oid
, "", &opts
);
1957 diffcore_std(&opts
);
1958 if (opts
.needed_rename_limit
> opt
->priv
->needed_rename_limit
)
1959 opt
->priv
->needed_rename_limit
= opts
.needed_rename_limit
;
1961 ret
= xmalloc(sizeof(*ret
));
1962 *ret
= diff_queued_diff
;
1964 opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1965 diff_queued_diff
.nr
= 0;
1966 diff_queued_diff
.queue
= NULL
;
1971 static int tree_has_path(struct repository
*r
, struct tree
*tree
,
1974 struct object_id hashy
;
1975 unsigned short mode_o
;
1977 return !get_tree_entry(r
,
1978 &tree
->object
.oid
, path
,
1983 * Return a new string that replaces the beginning portion (which matches
1984 * entry->dir), with entry->new_dir. In perl-speak:
1985 * new_path_name = (old_path =~ s/entry->dir/entry->new_dir/);
1987 * Caller must ensure that old_path starts with entry->dir + '/'.
1989 static char *apply_dir_rename(struct dir_rename_entry
*entry
,
1990 const char *old_path
)
1992 struct strbuf new_path
= STRBUF_INIT
;
1995 if (entry
->non_unique_new_dir
)
1998 oldlen
= strlen(entry
->dir
);
1999 if (entry
->new_dir
.len
== 0)
2001 * If someone renamed/merged a subdirectory into the root
2002 * directory (e.g. 'some/subdir' -> ''), then we want to
2005 * as the rename; we need to make old_path + oldlen advance
2006 * past the '/' character.
2009 newlen
= entry
->new_dir
.len
+ (strlen(old_path
) - oldlen
) + 1;
2010 strbuf_grow(&new_path
, newlen
);
2011 strbuf_addbuf(&new_path
, &entry
->new_dir
);
2012 strbuf_addstr(&new_path
, &old_path
[oldlen
]);
2014 return strbuf_detach(&new_path
, NULL
);
2017 static void get_renamed_dir_portion(const char *old_path
, const char *new_path
,
2018 char **old_dir
, char **new_dir
)
2020 char *end_of_old
, *end_of_new
;
2022 /* Default return values: NULL, meaning no rename */
2028 * "a/b/c/d/e/foo.c" -> "a/b/some/thing/else/e/foo.c"
2029 * the "e/foo.c" part is the same, we just want to know that
2030 * "a/b/c/d" was renamed to "a/b/some/thing/else"
2031 * so, for this example, this function returns "a/b/c/d" in
2032 * *old_dir and "a/b/some/thing/else" in *new_dir.
2036 * If the basename of the file changed, we don't care. We want
2037 * to know which portion of the directory, if any, changed.
2039 end_of_old
= strrchr(old_path
, '/');
2040 end_of_new
= strrchr(new_path
, '/');
2043 * If end_of_old is NULL, old_path wasn't in a directory, so there
2044 * could not be a directory rename (our rule elsewhere that a
2045 * directory which still exists is not considered to have been
2046 * renamed means the root directory can never be renamed -- because
2047 * the root directory always exists).
2050 return; /* Note: *old_dir and *new_dir are still NULL */
2053 * If new_path contains no directory (end_of_new is NULL), then we
2054 * have a rename of old_path's directory to the root directory.
2057 *old_dir
= xstrndup(old_path
, end_of_old
- old_path
);
2058 *new_dir
= xstrdup("");
2062 /* Find the first non-matching character traversing backwards */
2063 while (*--end_of_new
== *--end_of_old
&&
2064 end_of_old
!= old_path
&&
2065 end_of_new
!= new_path
)
2066 ; /* Do nothing; all in the while loop */
2069 * If both got back to the beginning of their strings, then the
2070 * directory didn't change at all, only the basename did.
2072 if (end_of_old
== old_path
&& end_of_new
== new_path
&&
2073 *end_of_old
== *end_of_new
)
2074 return; /* Note: *old_dir and *new_dir are still NULL */
2077 * If end_of_new got back to the beginning of its string, and
2078 * end_of_old got back to the beginning of some subdirectory, then
2079 * we have a rename/merge of a subdirectory into the root, which
2080 * needs slightly special handling.
2082 * Note: There is no need to consider the opposite case, with a
2083 * rename/merge of the root directory into some subdirectory
2084 * because as noted above the root directory always exists so it
2085 * cannot be considered to be renamed.
2087 if (end_of_new
== new_path
&&
2088 end_of_old
!= old_path
&& end_of_old
[-1] == '/') {
2089 *old_dir
= xstrndup(old_path
, --end_of_old
- old_path
);
2090 *new_dir
= xstrdup("");
2095 * We've found the first non-matching character in the directory
2096 * paths. That means the current characters we were looking at
2097 * were part of the first non-matching subdir name going back from
2098 * the end of the strings. Get the whole name by advancing both
2099 * end_of_old and end_of_new to the NEXT '/' character. That will
2100 * represent the entire directory rename.
2102 * The reason for the increment is cases like
2103 * a/b/star/foo/whatever.c -> a/b/tar/foo/random.c
2104 * After dropping the basename and going back to the first
2105 * non-matching character, we're now comparing:
2107 * and we want to be comparing:
2108 * a/b/star/ and a/b/tar/
2109 * but without the pre-increment, the one on the right would stay
2112 end_of_old
= strchr(++end_of_old
, '/');
2113 end_of_new
= strchr(++end_of_new
, '/');
2115 /* Copy the old and new directories into *old_dir and *new_dir. */
2116 *old_dir
= xstrndup(old_path
, end_of_old
- old_path
);
2117 *new_dir
= xstrndup(new_path
, end_of_new
- new_path
);
2120 static void remove_hashmap_entries(struct hashmap
*dir_renames
,
2121 struct string_list
*items_to_remove
)
2124 struct dir_rename_entry
*entry
;
2126 for (i
= 0; i
< items_to_remove
->nr
; i
++) {
2127 entry
= items_to_remove
->items
[i
].util
;
2128 hashmap_remove(dir_renames
, &entry
->ent
, NULL
);
2130 string_list_clear(items_to_remove
, 0);
2134 * See if there is a directory rename for path, and if there are any file
2135 * level conflicts for the renamed location. If there is a rename and
2136 * there are no conflicts, return the new name. Otherwise, return NULL.
2138 static char *handle_path_level_conflicts(struct merge_options
*opt
,
2140 struct dir_rename_entry
*entry
,
2141 struct hashmap
*collisions
,
2144 char *new_path
= NULL
;
2145 struct collision_entry
*collision_ent
;
2147 struct strbuf collision_paths
= STRBUF_INIT
;
2150 * entry has the mapping of old directory name to new directory name
2151 * that we want to apply to path.
2153 new_path
= apply_dir_rename(entry
, path
);
2156 /* This should only happen when entry->non_unique_new_dir set */
2157 if (!entry
->non_unique_new_dir
)
2158 BUG("entry->non_unique_new_dir not set and !new_path");
2159 output(opt
, 1, _("CONFLICT (directory rename split): "
2160 "Unclear where to place %s because directory "
2161 "%s was renamed to multiple other directories, "
2162 "with no destination getting a majority of the "
2170 * The caller needs to have ensured that it has pre-populated
2171 * collisions with all paths that map to new_path. Do a quick check
2172 * to ensure that's the case.
2174 collision_ent
= collision_find_entry(collisions
, new_path
);
2176 BUG("collision_ent is NULL");
2179 * Check for one-sided add/add/.../add conflicts, i.e.
2180 * where implicit renames from the other side doing
2181 * directory rename(s) can affect this side of history
2182 * to put multiple paths into the same location. Warn
2183 * and bail on directory renames for such paths.
2185 if (collision_ent
->reported_already
) {
2187 } else if (tree_has_path(opt
->repo
, tree
, new_path
)) {
2188 collision_ent
->reported_already
= 1;
2189 strbuf_add_separated_string_list(&collision_paths
, ", ",
2190 &collision_ent
->source_files
);
2191 output(opt
, 1, _("CONFLICT (implicit dir rename): Existing "
2192 "file/dir at %s in the way of implicit "
2193 "directory rename(s) putting the following "
2194 "path(s) there: %s."),
2195 new_path
, collision_paths
.buf
);
2197 } else if (collision_ent
->source_files
.nr
> 1) {
2198 collision_ent
->reported_already
= 1;
2199 strbuf_add_separated_string_list(&collision_paths
, ", ",
2200 &collision_ent
->source_files
);
2201 output(opt
, 1, _("CONFLICT (implicit dir rename): Cannot map "
2202 "more than one path to %s; implicit directory "
2203 "renames tried to put these paths there: %s"),
2204 new_path
, collision_paths
.buf
);
2208 /* Free memory we no longer need */
2209 strbuf_release(&collision_paths
);
2210 if (!clean
&& new_path
) {
2219 * There are a couple things we want to do at the directory level:
2220 * 1. Check for both sides renaming to the same thing, in order to avoid
2221 * implicit renaming of files that should be left in place. (See
2222 * testcase 6b in t6043 for details.)
2223 * 2. Prune directory renames if there are still files left in the
2224 * original directory. These represent a partial directory rename,
2225 * i.e. a rename where only some of the files within the directory
2226 * were renamed elsewhere. (Technically, this could be done earlier
2227 * in get_directory_renames(), except that would prevent us from
2228 * doing the previous check and thus failing testcase 6b.)
2229 * 3. Check for rename/rename(1to2) conflicts (at the directory level).
2230 * In the future, we could potentially record this info as well and
2231 * omit reporting rename/rename(1to2) conflicts for each path within
2232 * the affected directories, thus cleaning up the merge output.
2233 * NOTE: We do NOT check for rename/rename(2to1) conflicts at the
2234 * directory level, because merging directories is fine. If it
2235 * causes conflicts for files within those merged directories, then
2236 * that should be detected at the individual path level.
2238 static void handle_directory_level_conflicts(struct merge_options
*opt
,
2239 struct hashmap
*dir_re_head
,
2241 struct hashmap
*dir_re_merge
,
2244 struct hashmap_iter iter
;
2245 struct dir_rename_entry
*head_ent
;
2246 struct dir_rename_entry
*merge_ent
;
2248 struct string_list remove_from_head
= STRING_LIST_INIT_NODUP
;
2249 struct string_list remove_from_merge
= STRING_LIST_INIT_NODUP
;
2251 hashmap_for_each_entry(dir_re_head
, &iter
, head_ent
,
2252 ent
/* member name */) {
2253 merge_ent
= dir_rename_find_entry(dir_re_merge
, head_ent
->dir
);
2255 !head_ent
->non_unique_new_dir
&&
2256 !merge_ent
->non_unique_new_dir
&&
2257 !strbuf_cmp(&head_ent
->new_dir
, &merge_ent
->new_dir
)) {
2258 /* 1. Renamed identically; remove it from both sides */
2259 string_list_append(&remove_from_head
,
2260 head_ent
->dir
)->util
= head_ent
;
2261 strbuf_release(&head_ent
->new_dir
);
2262 string_list_append(&remove_from_merge
,
2263 merge_ent
->dir
)->util
= merge_ent
;
2264 strbuf_release(&merge_ent
->new_dir
);
2265 } else if (tree_has_path(opt
->repo
, head
, head_ent
->dir
)) {
2266 /* 2. This wasn't a directory rename after all */
2267 string_list_append(&remove_from_head
,
2268 head_ent
->dir
)->util
= head_ent
;
2269 strbuf_release(&head_ent
->new_dir
);
2273 remove_hashmap_entries(dir_re_head
, &remove_from_head
);
2274 remove_hashmap_entries(dir_re_merge
, &remove_from_merge
);
2276 hashmap_for_each_entry(dir_re_merge
, &iter
, merge_ent
,
2277 ent
/* member name */) {
2278 head_ent
= dir_rename_find_entry(dir_re_head
, merge_ent
->dir
);
2279 if (tree_has_path(opt
->repo
, merge
, merge_ent
->dir
)) {
2280 /* 2. This wasn't a directory rename after all */
2281 string_list_append(&remove_from_merge
,
2282 merge_ent
->dir
)->util
= merge_ent
;
2283 } else if (head_ent
&&
2284 !head_ent
->non_unique_new_dir
&&
2285 !merge_ent
->non_unique_new_dir
) {
2286 /* 3. rename/rename(1to2) */
2288 * We can assume it's not rename/rename(1to1) because
2289 * that was case (1), already checked above. So we
2290 * know that head_ent->new_dir and merge_ent->new_dir
2291 * are different strings.
2293 output(opt
, 1, _("CONFLICT (rename/rename): "
2294 "Rename directory %s->%s in %s. "
2295 "Rename directory %s->%s in %s"),
2296 head_ent
->dir
, head_ent
->new_dir
.buf
, opt
->branch1
,
2297 head_ent
->dir
, merge_ent
->new_dir
.buf
, opt
->branch2
);
2298 string_list_append(&remove_from_head
,
2299 head_ent
->dir
)->util
= head_ent
;
2300 strbuf_release(&head_ent
->new_dir
);
2301 string_list_append(&remove_from_merge
,
2302 merge_ent
->dir
)->util
= merge_ent
;
2303 strbuf_release(&merge_ent
->new_dir
);
2307 remove_hashmap_entries(dir_re_head
, &remove_from_head
);
2308 remove_hashmap_entries(dir_re_merge
, &remove_from_merge
);
2311 static struct hashmap
*get_directory_renames(struct diff_queue_struct
*pairs
)
2313 struct hashmap
*dir_renames
;
2314 struct hashmap_iter iter
;
2315 struct dir_rename_entry
*entry
;
2319 * Typically, we think of a directory rename as all files from a
2320 * certain directory being moved to a target directory. However,
2321 * what if someone first moved two files from the original
2322 * directory in one commit, and then renamed the directory
2323 * somewhere else in a later commit? At merge time, we just know
2324 * that files from the original directory went to two different
2325 * places, and that the bulk of them ended up in the same place.
2326 * We want each directory rename to represent where the bulk of the
2327 * files from that directory end up; this function exists to find
2328 * where the bulk of the files went.
2330 * The first loop below simply iterates through the list of file
2331 * renames, finding out how often each directory rename pair
2332 * possibility occurs.
2334 dir_renames
= xmalloc(sizeof(*dir_renames
));
2335 dir_rename_init(dir_renames
);
2336 for (i
= 0; i
< pairs
->nr
; ++i
) {
2337 struct string_list_item
*item
;
2339 struct diff_filepair
*pair
= pairs
->queue
[i
];
2340 char *old_dir
, *new_dir
;
2342 /* File not part of directory rename if it wasn't renamed */
2343 if (pair
->status
!= 'R')
2346 get_renamed_dir_portion(pair
->one
->path
, pair
->two
->path
,
2347 &old_dir
, &new_dir
);
2349 /* Directory didn't change at all; ignore this one. */
2352 entry
= dir_rename_find_entry(dir_renames
, old_dir
);
2354 entry
= xmalloc(sizeof(*entry
));
2355 dir_rename_entry_init(entry
, old_dir
);
2356 hashmap_put(dir_renames
, &entry
->ent
);
2360 item
= string_list_lookup(&entry
->possible_new_dirs
, new_dir
);
2362 item
= string_list_insert(&entry
->possible_new_dirs
,
2364 item
->util
= xcalloc(1, sizeof(int));
2373 * For each directory with files moved out of it, we find out which
2374 * target directory received the most files so we can declare it to
2375 * be the "winning" target location for the directory rename. This
2376 * winner gets recorded in new_dir. If there is no winner
2377 * (multiple target directories received the same number of files),
2378 * we set non_unique_new_dir. Once we've determined the winner (or
2379 * that there is no winner), we no longer need possible_new_dirs.
2381 hashmap_for_each_entry(dir_renames
, &iter
, entry
,
2382 ent
/* member name */) {
2387 for (i
= 0; i
< entry
->possible_new_dirs
.nr
; i
++) {
2388 int *count
= entry
->possible_new_dirs
.items
[i
].util
;
2392 else if (*count
> max
) {
2394 best
= entry
->possible_new_dirs
.items
[i
].string
;
2398 entry
->non_unique_new_dir
= 1;
2400 assert(entry
->new_dir
.len
== 0);
2401 strbuf_addstr(&entry
->new_dir
, best
);
2404 * The relevant directory sub-portion of the original full
2405 * filepaths were xstrndup'ed before inserting into
2406 * possible_new_dirs, and instead of manually iterating the
2407 * list and free'ing each, just lie and tell
2408 * possible_new_dirs that it did the strdup'ing so that it
2409 * will free them for us.
2411 entry
->possible_new_dirs
.strdup_strings
= 1;
2412 string_list_clear(&entry
->possible_new_dirs
, 1);
2418 static struct dir_rename_entry
*check_dir_renamed(const char *path
,
2419 struct hashmap
*dir_renames
)
2421 char *temp
= xstrdup(path
);
2423 struct dir_rename_entry
*entry
= NULL
;
2425 while ((end
= strrchr(temp
, '/'))) {
2427 entry
= dir_rename_find_entry(dir_renames
, temp
);
2435 static void compute_collisions(struct hashmap
*collisions
,
2436 struct hashmap
*dir_renames
,
2437 struct diff_queue_struct
*pairs
)
2442 * Multiple files can be mapped to the same path due to directory
2443 * renames done by the other side of history. Since that other
2444 * side of history could have merged multiple directories into one,
2445 * if our side of history added the same file basename to each of
2446 * those directories, then all N of them would get implicitly
2447 * renamed by the directory rename detection into the same path,
2448 * and we'd get an add/add/.../add conflict, and all those adds
2449 * from *this* side of history. This is not representable in the
2450 * index, and users aren't going to easily be able to make sense of
2451 * it. So we need to provide a good warning about what's
2452 * happening, and fall back to no-directory-rename detection
2453 * behavior for those paths.
2455 * See testcases 9e and all of section 5 from t6043 for examples.
2457 collision_init(collisions
);
2459 for (i
= 0; i
< pairs
->nr
; ++i
) {
2460 struct dir_rename_entry
*dir_rename_ent
;
2461 struct collision_entry
*collision_ent
;
2463 struct diff_filepair
*pair
= pairs
->queue
[i
];
2465 if (pair
->status
!= 'A' && pair
->status
!= 'R')
2467 dir_rename_ent
= check_dir_renamed(pair
->two
->path
,
2469 if (!dir_rename_ent
)
2472 new_path
= apply_dir_rename(dir_rename_ent
, pair
->two
->path
);
2475 * dir_rename_ent->non_unique_new_path is true, which
2476 * means there is no directory rename for us to use,
2477 * which means it won't cause us any additional
2481 collision_ent
= collision_find_entry(collisions
, new_path
);
2482 if (!collision_ent
) {
2483 CALLOC_ARRAY(collision_ent
, 1);
2484 hashmap_entry_init(&collision_ent
->ent
,
2486 hashmap_put(collisions
, &collision_ent
->ent
);
2487 collision_ent
->target_file
= new_path
;
2491 string_list_insert(&collision_ent
->source_files
,
2496 static char *check_for_directory_rename(struct merge_options
*opt
,
2499 struct hashmap
*dir_renames
,
2500 struct hashmap
*dir_rename_exclusions
,
2501 struct hashmap
*collisions
,
2504 char *new_path
= NULL
;
2505 struct dir_rename_entry
*entry
= check_dir_renamed(path
, dir_renames
);
2506 struct dir_rename_entry
*oentry
= NULL
;
2512 * This next part is a little weird. We do not want to do an
2513 * implicit rename into a directory we renamed on our side, because
2514 * that will result in a spurious rename/rename(1to2) conflict. An
2516 * Base commit: dumbdir/afile, otherdir/bfile
2517 * Side 1: smrtdir/afile, otherdir/bfile
2518 * Side 2: dumbdir/afile, dumbdir/bfile
2519 * Here, while working on Side 1, we could notice that otherdir was
2520 * renamed/merged to dumbdir, and change the diff_filepair for
2521 * otherdir/bfile into a rename into dumbdir/bfile. However, Side
2522 * 2 will notice the rename from dumbdir to smrtdir, and do the
2523 * transitive rename to move it from dumbdir/bfile to
2524 * smrtdir/bfile. That gives us bfile in dumbdir vs being in
2525 * smrtdir, a rename/rename(1to2) conflict. We really just want
2526 * the file to end up in smrtdir. And the way to achieve that is
2527 * to not let Side1 do the rename to dumbdir, since we know that is
2528 * the source of one of our directory renames.
2530 * That's why oentry and dir_rename_exclusions is here.
2532 * As it turns out, this also prevents N-way transient rename
2533 * confusion; See testcases 9c and 9d of t6043.
2535 oentry
= dir_rename_find_entry(dir_rename_exclusions
, entry
->new_dir
.buf
);
2537 output(opt
, 1, _("WARNING: Avoiding applying %s -> %s rename "
2538 "to %s, because %s itself was renamed."),
2539 entry
->dir
, entry
->new_dir
.buf
, path
, entry
->new_dir
.buf
);
2541 new_path
= handle_path_level_conflicts(opt
, path
, entry
,
2543 *clean_merge
&= (new_path
!= NULL
);
2549 static void apply_directory_rename_modifications(struct merge_options
*opt
,
2550 struct diff_filepair
*pair
,
2554 struct tree
*o_tree
,
2555 struct tree
*a_tree
,
2556 struct tree
*b_tree
,
2557 struct string_list
*entries
)
2559 struct string_list_item
*item
;
2560 int stage
= (tree
== a_tree
? 2 : 3);
2564 * In all cases where we can do directory rename detection,
2565 * unpack_trees() will have read pair->two->path into the
2566 * index and the working copy. We need to remove it so that
2567 * we can instead place it at new_path. It is guaranteed to
2568 * not be untracked (unpack_trees() would have errored out
2569 * saying the file would have been overwritten), but it might
2572 update_wd
= !was_dirty(opt
, pair
->two
->path
);
2574 output(opt
, 1, _("Refusing to lose dirty file at %s"),
2576 remove_file(opt
, 1, pair
->two
->path
, !update_wd
);
2578 /* Find or create a new re->dst_entry */
2579 item
= string_list_lookup(entries
, new_path
);
2582 * Since we're renaming on this side of history, and it's
2583 * due to a directory rename on the other side of history
2584 * (which we only allow when the directory in question no
2585 * longer exists on the other side of history), the
2586 * original entry for re->dst_entry is no longer
2589 re
->dst_entry
->processed
= 1;
2592 * ...because we'll be using this new one.
2594 re
->dst_entry
= item
->util
;
2597 * re->dst_entry is for the before-dir-rename path, and we
2598 * need it to hold information for the after-dir-rename
2599 * path. Before creating a new entry, we need to mark the
2600 * old one as unnecessary (...unless it is shared by
2601 * src_entry, i.e. this didn't use to be a rename, in which
2602 * case we can just allow the normal processing to happen
2605 if (pair
->status
== 'R')
2606 re
->dst_entry
->processed
= 1;
2608 re
->dst_entry
= insert_stage_data(opt
->repo
, new_path
,
2609 o_tree
, a_tree
, b_tree
,
2611 item
= string_list_insert(entries
, new_path
);
2612 item
->util
= re
->dst_entry
;
2616 * Update the stage_data with the information about the path we are
2617 * moving into place. That slot will be empty and available for us
2618 * to write to because of the collision checks in
2619 * handle_path_level_conflicts(). In other words,
2620 * re->dst_entry->stages[stage].oid will be the null_oid, so it's
2621 * open for us to write to.
2623 * It may be tempting to actually update the index at this point as
2624 * well, using update_stages_for_stage_data(), but as per the big
2625 * "NOTE" in update_stages(), doing so will modify the current
2626 * in-memory index which will break calls to would_lose_untracked()
2627 * that we need to make. Instead, we need to just make sure that
2628 * the various handle_rename_*() functions update the index
2629 * explicitly rather than relying on unpack_trees() to have done it.
2631 get_tree_entry(opt
->repo
,
2634 &re
->dst_entry
->stages
[stage
].oid
,
2635 &re
->dst_entry
->stages
[stage
].mode
);
2638 * Record the original change status (or 'type' of change). If it
2639 * was originally an add ('A'), this lets us differentiate later
2640 * between a RENAME_DELETE conflict and RENAME_VIA_DIR (they
2641 * otherwise look the same). If it was originally a rename ('R'),
2642 * this lets us remember and report accurately about the transitive
2643 * renaming that occurred via the directory rename detection. Also,
2644 * record the original destination name.
2646 re
->dir_rename_original_type
= pair
->status
;
2647 re
->dir_rename_original_dest
= pair
->two
->path
;
2650 * We don't actually look at pair->status again, but it seems
2651 * pedagogically correct to adjust it.
2656 * Finally, record the new location.
2658 pair
->two
->path
= new_path
;
2662 * Get information of all renames which occurred in 'pairs', making use of
2663 * any implicit directory renames inferred from the other side of history.
2664 * We need the three trees in the merge ('o_tree', 'a_tree' and 'b_tree')
2665 * to be able to associate the correct cache entries with the rename
2666 * information; tree is always equal to either a_tree or b_tree.
2668 static struct string_list
*get_renames(struct merge_options
*opt
,
2670 struct diff_queue_struct
*pairs
,
2671 struct hashmap
*dir_renames
,
2672 struct hashmap
*dir_rename_exclusions
,
2674 struct tree
*o_tree
,
2675 struct tree
*a_tree
,
2676 struct tree
*b_tree
,
2677 struct string_list
*entries
,
2681 struct hashmap collisions
;
2682 struct hashmap_iter iter
;
2683 struct collision_entry
*e
;
2684 struct string_list
*renames
;
2686 compute_collisions(&collisions
, dir_renames
, pairs
);
2687 CALLOC_ARRAY(renames
, 1);
2689 for (i
= 0; i
< pairs
->nr
; ++i
) {
2690 struct string_list_item
*item
;
2692 struct diff_filepair
*pair
= pairs
->queue
[i
];
2693 char *new_path
; /* non-NULL only with directory renames */
2695 if (pair
->status
!= 'A' && pair
->status
!= 'R') {
2696 diff_free_filepair(pair
);
2699 new_path
= check_for_directory_rename(opt
, pair
->two
->path
, tree
,
2701 dir_rename_exclusions
,
2704 if (pair
->status
!= 'R' && !new_path
) {
2705 diff_free_filepair(pair
);
2709 re
= xmalloc(sizeof(*re
));
2712 re
->branch
= branch
;
2713 re
->dir_rename_original_type
= '\0';
2714 re
->dir_rename_original_dest
= NULL
;
2715 item
= string_list_lookup(entries
, re
->pair
->one
->path
);
2717 re
->src_entry
= insert_stage_data(opt
->repo
,
2718 re
->pair
->one
->path
,
2719 o_tree
, a_tree
, b_tree
, entries
);
2721 re
->src_entry
= item
->util
;
2723 item
= string_list_lookup(entries
, re
->pair
->two
->path
);
2725 re
->dst_entry
= insert_stage_data(opt
->repo
,
2726 re
->pair
->two
->path
,
2727 o_tree
, a_tree
, b_tree
, entries
);
2729 re
->dst_entry
= item
->util
;
2730 item
= string_list_insert(renames
, pair
->one
->path
);
2733 apply_directory_rename_modifications(opt
, pair
, new_path
,
2739 hashmap_for_each_entry(&collisions
, &iter
, e
,
2740 ent
/* member name */) {
2741 free(e
->target_file
);
2742 string_list_clear(&e
->source_files
, 0);
2744 hashmap_clear_and_free(&collisions
, struct collision_entry
, ent
);
2748 static int process_renames(struct merge_options
*opt
,
2749 struct string_list
*a_renames
,
2750 struct string_list
*b_renames
)
2752 int clean_merge
= 1, i
, j
;
2753 struct string_list a_by_dst
= STRING_LIST_INIT_NODUP
;
2754 struct string_list b_by_dst
= STRING_LIST_INIT_NODUP
;
2755 const struct rename
*sre
;
2758 * FIXME: As string-list.h notes, it's O(n^2) to build a sorted
2759 * string_list one-by-one, but O(n log n) to build it unsorted and
2760 * then sort it. Note that as we build the list, we do not need to
2761 * check if the existing destination path is already in the list,
2762 * because the structure of diffcore_rename guarantees we won't
2765 for (i
= 0; i
< a_renames
->nr
; i
++) {
2766 sre
= a_renames
->items
[i
].util
;
2767 string_list_insert(&a_by_dst
, sre
->pair
->two
->path
)->util
2770 for (i
= 0; i
< b_renames
->nr
; i
++) {
2771 sre
= b_renames
->items
[i
].util
;
2772 string_list_insert(&b_by_dst
, sre
->pair
->two
->path
)->util
2776 for (i
= 0, j
= 0; i
< a_renames
->nr
|| j
< b_renames
->nr
;) {
2777 struct string_list
*renames1
, *renames2Dst
;
2778 struct rename
*ren1
= NULL
, *ren2
= NULL
;
2779 const char *ren1_src
, *ren1_dst
;
2780 struct string_list_item
*lookup
;
2782 if (i
>= a_renames
->nr
) {
2783 ren2
= b_renames
->items
[j
++].util
;
2784 } else if (j
>= b_renames
->nr
) {
2785 ren1
= a_renames
->items
[i
++].util
;
2787 int compare
= strcmp(a_renames
->items
[i
].string
,
2788 b_renames
->items
[j
].string
);
2790 ren1
= a_renames
->items
[i
++].util
;
2792 ren2
= b_renames
->items
[j
++].util
;
2795 /* TODO: refactor, so that 1/2 are not needed */
2797 renames1
= a_renames
;
2798 renames2Dst
= &b_by_dst
;
2800 renames1
= b_renames
;
2801 renames2Dst
= &a_by_dst
;
2805 if (ren1
->processed
)
2807 ren1
->processed
= 1;
2808 ren1
->dst_entry
->processed
= 1;
2809 /* BUG: We should only mark src_entry as processed if we
2810 * are not dealing with a rename + add-source case.
2812 ren1
->src_entry
->processed
= 1;
2814 ren1_src
= ren1
->pair
->one
->path
;
2815 ren1_dst
= ren1
->pair
->two
->path
;
2818 /* One file renamed on both sides */
2819 const char *ren2_src
= ren2
->pair
->one
->path
;
2820 const char *ren2_dst
= ren2
->pair
->two
->path
;
2821 enum rename_type rename_type
;
2822 if (strcmp(ren1_src
, ren2_src
) != 0)
2823 BUG("ren1_src != ren2_src");
2824 ren2
->dst_entry
->processed
= 1;
2825 ren2
->processed
= 1;
2826 if (strcmp(ren1_dst
, ren2_dst
) != 0) {
2827 rename_type
= RENAME_ONE_FILE_TO_TWO
;
2830 rename_type
= RENAME_ONE_FILE_TO_ONE
;
2831 /* BUG: We should only remove ren1_src in
2832 * the base stage (think of rename +
2833 * add-source cases).
2835 remove_file(opt
, 1, ren1_src
, 1);
2836 update_entry(ren1
->dst_entry
,
2841 setup_rename_conflict_info(rename_type
, opt
, ren1
, ren2
);
2842 } else if ((lookup
= string_list_lookup(renames2Dst
, ren1_dst
))) {
2843 /* Two different files renamed to the same thing */
2845 ren2
= lookup
->util
;
2846 ren2_dst
= ren2
->pair
->two
->path
;
2847 if (strcmp(ren1_dst
, ren2_dst
) != 0)
2848 BUG("ren1_dst != ren2_dst");
2851 ren2
->processed
= 1;
2853 * BUG: We should only mark src_entry as processed
2854 * if we are not dealing with a rename + add-source
2857 ren2
->src_entry
->processed
= 1;
2859 setup_rename_conflict_info(RENAME_TWO_FILES_TO_ONE
,
2862 /* Renamed in 1, maybe changed in 2 */
2863 /* we only use sha1 and mode of these */
2864 struct diff_filespec src_other
, dst_other
;
2868 * unpack_trees loads entries from common-commit
2869 * into stage 1, from head-commit into stage 2, and
2870 * from merge-commit into stage 3. We keep track
2871 * of which side corresponds to the rename.
2873 int renamed_stage
= a_renames
== renames1
? 2 : 3;
2874 int other_stage
= a_renames
== renames1
? 3 : 2;
2877 * Directory renames have a funny corner case...
2879 int renamed_to_self
= !strcmp(ren1_src
, ren1_dst
);
2881 /* BUG: We should only remove ren1_src in the base
2882 * stage and in other_stage (think of rename +
2885 if (!renamed_to_self
)
2886 remove_file(opt
, 1, ren1_src
,
2887 renamed_stage
== 2 ||
2888 !was_tracked(opt
, ren1_src
));
2890 oidcpy(&src_other
.oid
,
2891 &ren1
->src_entry
->stages
[other_stage
].oid
);
2892 src_other
.mode
= ren1
->src_entry
->stages
[other_stage
].mode
;
2893 oidcpy(&dst_other
.oid
,
2894 &ren1
->dst_entry
->stages
[other_stage
].oid
);
2895 dst_other
.mode
= ren1
->dst_entry
->stages
[other_stage
].mode
;
2898 if (oideq(&src_other
.oid
, null_oid()) &&
2899 ren1
->dir_rename_original_type
== 'A') {
2900 setup_rename_conflict_info(RENAME_VIA_DIR
,
2902 } else if (renamed_to_self
) {
2903 setup_rename_conflict_info(RENAME_NORMAL
,
2905 } else if (oideq(&src_other
.oid
, null_oid())) {
2906 setup_rename_conflict_info(RENAME_DELETE
,
2908 } else if ((dst_other
.mode
== ren1
->pair
->two
->mode
) &&
2909 oideq(&dst_other
.oid
, &ren1
->pair
->two
->oid
)) {
2911 * Added file on the other side identical to
2912 * the file being renamed: clean merge.
2913 * Also, there is no need to overwrite the
2914 * file already in the working copy, so call
2915 * update_file_flags() instead of
2918 if (update_file_flags(opt
,
2921 1, /* update_cache */
2924 } else if (!oideq(&dst_other
.oid
, null_oid())) {
2926 * Probably not a clean merge, but it's
2927 * premature to set clean_merge to 0 here,
2928 * because if the rename merges cleanly and
2929 * the merge exactly matches the newly added
2930 * file, then the merge will be clean.
2932 setup_rename_conflict_info(RENAME_ADD
,
2937 if (clean_merge
< 0)
2938 goto cleanup_and_return
;
2940 struct diff_filespec
*o
, *a
, *b
;
2941 src_other
.path
= (char *)ren1_src
;
2943 o
= ren1
->pair
->one
;
2944 if (a_renames
== renames1
) {
2945 a
= ren1
->pair
->two
;
2948 b
= ren1
->pair
->two
;
2951 update_entry(ren1
->dst_entry
, o
, a
, b
);
2952 setup_rename_conflict_info(RENAME_NORMAL
,
2958 string_list_clear(&a_by_dst
, 0);
2959 string_list_clear(&b_by_dst
, 0);
2964 struct rename_info
{
2965 struct string_list
*head_renames
;
2966 struct string_list
*merge_renames
;
2969 static void initial_cleanup_rename(struct diff_queue_struct
*pairs
,
2970 struct hashmap
*dir_renames
)
2972 struct hashmap_iter iter
;
2973 struct dir_rename_entry
*e
;
2975 hashmap_for_each_entry(dir_renames
, &iter
, e
,
2976 ent
/* member name */) {
2978 strbuf_release(&e
->new_dir
);
2979 /* possible_new_dirs already cleared in get_directory_renames */
2981 hashmap_clear_and_free(dir_renames
, struct dir_rename_entry
, ent
);
2988 static int detect_and_process_renames(struct merge_options
*opt
,
2989 struct tree
*common
,
2992 struct string_list
*entries
,
2993 struct rename_info
*ri
)
2995 struct diff_queue_struct
*head_pairs
, *merge_pairs
;
2996 struct hashmap
*dir_re_head
, *dir_re_merge
;
2999 ri
->head_renames
= NULL
;
3000 ri
->merge_renames
= NULL
;
3002 if (!merge_detect_rename(opt
))
3005 head_pairs
= get_diffpairs(opt
, common
, head
);
3006 merge_pairs
= get_diffpairs(opt
, common
, merge
);
3008 if ((opt
->detect_directory_renames
== MERGE_DIRECTORY_RENAMES_TRUE
) ||
3009 (opt
->detect_directory_renames
== MERGE_DIRECTORY_RENAMES_CONFLICT
&&
3010 !opt
->priv
->call_depth
)) {
3011 dir_re_head
= get_directory_renames(head_pairs
);
3012 dir_re_merge
= get_directory_renames(merge_pairs
);
3014 handle_directory_level_conflicts(opt
,
3016 dir_re_merge
, merge
);
3018 dir_re_head
= xmalloc(sizeof(*dir_re_head
));
3019 dir_re_merge
= xmalloc(sizeof(*dir_re_merge
));
3020 dir_rename_init(dir_re_head
);
3021 dir_rename_init(dir_re_merge
);
3024 ri
->head_renames
= get_renames(opt
, opt
->branch1
, head_pairs
,
3025 dir_re_merge
, dir_re_head
, head
,
3026 common
, head
, merge
, entries
,
3030 ri
->merge_renames
= get_renames(opt
, opt
->branch2
, merge_pairs
,
3031 dir_re_head
, dir_re_merge
, merge
,
3032 common
, head
, merge
, entries
,
3036 clean
&= process_renames(opt
, ri
->head_renames
, ri
->merge_renames
);
3040 * Some cleanup is deferred until cleanup_renames() because the
3041 * data structures are still needed and referenced in
3042 * process_entry(). But there are a few things we can free now.
3044 initial_cleanup_rename(head_pairs
, dir_re_head
);
3045 initial_cleanup_rename(merge_pairs
, dir_re_merge
);
3050 static void final_cleanup_rename(struct string_list
*rename
)
3052 const struct rename
*re
;
3058 for (i
= 0; i
< rename
->nr
; i
++) {
3059 re
= rename
->items
[i
].util
;
3060 diff_free_filepair(re
->pair
);
3062 string_list_clear(rename
, 1);
3066 static void final_cleanup_renames(struct rename_info
*re_info
)
3068 final_cleanup_rename(re_info
->head_renames
);
3069 final_cleanup_rename(re_info
->merge_renames
);
3072 static int read_oid_strbuf(struct merge_options
*opt
,
3073 const struct object_id
*oid
,
3077 enum object_type type
;
3079 buf
= repo_read_object_file(the_repository
, oid
, &type
, &size
);
3081 return err(opt
, _("cannot read object %s"), oid_to_hex(oid
));
3082 if (type
!= OBJ_BLOB
) {
3084 return err(opt
, _("object %s is not a blob"), oid_to_hex(oid
));
3086 strbuf_attach(dst
, buf
, size
, size
+ 1);
3090 static int blob_unchanged(struct merge_options
*opt
,
3091 const struct diff_filespec
*o
,
3092 const struct diff_filespec
*a
,
3093 int renormalize
, const char *path
)
3095 struct strbuf obuf
= STRBUF_INIT
;
3096 struct strbuf abuf
= STRBUF_INIT
;
3097 int ret
= 0; /* assume changed for safety */
3098 struct index_state
*idx
= opt
->repo
->index
;
3100 if (a
->mode
!= o
->mode
)
3102 if (oideq(&o
->oid
, &a
->oid
))
3107 if (read_oid_strbuf(opt
, &o
->oid
, &obuf
) ||
3108 read_oid_strbuf(opt
, &a
->oid
, &abuf
))
3111 * Note: binary | is used so that both renormalizations are
3112 * performed. Comparison can be skipped if both files are
3113 * unchanged since their sha1s have already been compared.
3115 if (renormalize_buffer(idx
, path
, obuf
.buf
, obuf
.len
, &obuf
) |
3116 renormalize_buffer(idx
, path
, abuf
.buf
, abuf
.len
, &abuf
))
3117 ret
= (obuf
.len
== abuf
.len
&& !memcmp(obuf
.buf
, abuf
.buf
, obuf
.len
));
3120 strbuf_release(&obuf
);
3121 strbuf_release(&abuf
);
3125 static int handle_modify_delete(struct merge_options
*opt
,
3127 const struct diff_filespec
*o
,
3128 const struct diff_filespec
*a
,
3129 const struct diff_filespec
*b
)
3131 const char *modify_branch
, *delete_branch
;
3132 const struct diff_filespec
*changed
;
3135 modify_branch
= opt
->branch1
;
3136 delete_branch
= opt
->branch2
;
3139 modify_branch
= opt
->branch2
;
3140 delete_branch
= opt
->branch1
;
3144 return handle_change_delete(opt
,
3147 modify_branch
, delete_branch
,
3148 _("modify"), _("modified"));
3151 static int handle_content_merge(struct merge_file_info
*mfi
,
3152 struct merge_options
*opt
,
3155 const struct diff_filespec
*o
,
3156 const struct diff_filespec
*a
,
3157 const struct diff_filespec
*b
,
3158 struct rename_conflict_info
*ci
)
3160 const char *reason
= _("content");
3161 unsigned df_conflict_remains
= 0;
3164 reason
= _("add/add");
3166 assert(o
->path
&& a
->path
&& b
->path
);
3167 if (ci
&& dir_in_way(opt
->repo
->index
, path
, !opt
->priv
->call_depth
,
3168 S_ISGITLINK(ci
->ren1
->pair
->two
->mode
)))
3169 df_conflict_remains
= 1;
3171 if (merge_mode_and_contents(opt
, o
, a
, b
, path
,
3172 opt
->branch1
, opt
->branch2
,
3173 opt
->priv
->call_depth
* 2, mfi
))
3177 * We can skip updating the working tree file iff:
3178 * a) The merge is clean
3179 * b) The merge matches what was in HEAD (content, mode, pathname)
3180 * c) The target path is usable (i.e. not involved in D/F conflict)
3182 if (mfi
->clean
&& was_tracked_and_matches(opt
, path
, &mfi
->blob
) &&
3183 !df_conflict_remains
) {
3185 struct cache_entry
*ce
;
3187 output(opt
, 3, _("Skipped %s (merged same as existing)"), path
);
3188 if (add_cacheinfo(opt
, &mfi
->blob
, path
,
3189 0, (!opt
->priv
->call_depth
&& !is_dirty
), 0))
3192 * However, add_cacheinfo() will delete the old cache entry
3193 * and add a new one. We need to copy over any skip_worktree
3194 * flag to avoid making the file appear as if it were
3195 * deleted by the user.
3197 pos
= index_name_pos(&opt
->priv
->orig_index
, path
, strlen(path
));
3198 ce
= opt
->priv
->orig_index
.cache
[pos
];
3199 if (ce_skip_worktree(ce
)) {
3200 pos
= index_name_pos(opt
->repo
->index
, path
, strlen(path
));
3201 ce
= opt
->repo
->index
->cache
[pos
];
3202 ce
->ce_flags
|= CE_SKIP_WORKTREE
;
3208 if (S_ISGITLINK(mfi
->blob
.mode
))
3209 reason
= _("submodule");
3210 output(opt
, 1, _("CONFLICT (%s): Merge conflict in %s"),
3212 if (ci
&& !df_conflict_remains
)
3213 if (update_stages(opt
, path
, o
, a
, b
))
3217 if (df_conflict_remains
|| is_dirty
) {
3219 if (opt
->priv
->call_depth
) {
3220 remove_file_from_index(opt
->repo
->index
, path
);
3223 if (update_stages(opt
, path
, o
, a
, b
))
3226 int file_from_stage2
= was_tracked(opt
, path
);
3228 if (update_stages(opt
, path
, NULL
,
3229 file_from_stage2
? &mfi
->blob
: NULL
,
3230 file_from_stage2
? NULL
: &mfi
->blob
))
3235 new_path
= unique_path(opt
, path
, ci
->ren1
->branch
);
3237 output(opt
, 1, _("Refusing to lose dirty file at %s"),
3240 output(opt
, 1, _("Adding as %s instead"), new_path
);
3241 if (update_file(opt
, 0, &mfi
->blob
, new_path
)) {
3247 } else if (update_file(opt
, mfi
->clean
, &mfi
->blob
, path
))
3249 return !is_dirty
&& mfi
->clean
;
3252 static int handle_rename_normal(struct merge_options
*opt
,
3254 const struct diff_filespec
*o
,
3255 const struct diff_filespec
*a
,
3256 const struct diff_filespec
*b
,
3257 struct rename_conflict_info
*ci
)
3259 struct rename
*ren
= ci
->ren1
;
3260 struct merge_file_info mfi
;
3263 /* Merge the content and write it out */
3264 clean
= handle_content_merge(&mfi
, opt
, path
, was_dirty(opt
, path
),
3268 opt
->detect_directory_renames
== MERGE_DIRECTORY_RENAMES_CONFLICT
&&
3269 ren
->dir_rename_original_dest
) {
3270 if (update_stages(opt
, path
,
3271 &mfi
.blob
, &mfi
.blob
, &mfi
.blob
))
3273 clean
= 0; /* not clean, but conflicted */
3278 static void dir_rename_warning(const char *msg
,
3281 struct merge_options
*opt
,
3284 const char *other_branch
;
3285 other_branch
= (ren
->branch
== opt
->branch1
?
3286 opt
->branch2
: opt
->branch1
);
3288 output(opt
, clean
? 2 : 1, msg
,
3289 ren
->pair
->one
->path
, ren
->branch
,
3290 other_branch
, ren
->pair
->two
->path
);
3293 output(opt
, clean
? 2 : 1, msg
,
3294 ren
->pair
->one
->path
, ren
->dir_rename_original_dest
, ren
->branch
,
3295 other_branch
, ren
->pair
->two
->path
);
3297 static int warn_about_dir_renamed_entries(struct merge_options
*opt
,
3301 int clean
= 1, is_add
;
3306 /* Return early if ren was not affected/created by a directory rename */
3307 if (!ren
->dir_rename_original_dest
)
3311 assert(opt
->detect_directory_renames
> MERGE_DIRECTORY_RENAMES_NONE
);
3312 assert(ren
->dir_rename_original_type
== 'A' ||
3313 ren
->dir_rename_original_type
== 'R');
3315 /* Check whether to treat directory renames as a conflict */
3316 clean
= (opt
->detect_directory_renames
== MERGE_DIRECTORY_RENAMES_TRUE
);
3318 is_add
= (ren
->dir_rename_original_type
== 'A');
3319 if (ren
->dir_rename_original_type
== 'A' && clean
) {
3320 msg
= _("Path updated: %s added in %s inside a "
3321 "directory that was renamed in %s; moving it to %s.");
3322 } else if (ren
->dir_rename_original_type
== 'A' && !clean
) {
3323 msg
= _("CONFLICT (file location): %s added in %s "
3324 "inside a directory that was renamed in %s, "
3325 "suggesting it should perhaps be moved to %s.");
3326 } else if (ren
->dir_rename_original_type
== 'R' && clean
) {
3327 msg
= _("Path updated: %s renamed to %s in %s, inside a "
3328 "directory that was renamed in %s; moving it to %s.");
3329 } else if (ren
->dir_rename_original_type
== 'R' && !clean
) {
3330 msg
= _("CONFLICT (file location): %s renamed to %s in %s, "
3331 "inside a directory that was renamed in %s, "
3332 "suggesting it should perhaps be moved to %s.");
3334 BUG("Impossible dir_rename_original_type/clean combination");
3336 dir_rename_warning(msg
, is_add
, clean
, opt
, ren
);
3341 /* Per entry merge function */
3342 static int process_entry(struct merge_options
*opt
,
3343 const char *path
, struct stage_data
*entry
)
3345 int clean_merge
= 1;
3346 int normalize
= opt
->renormalize
;
3348 struct diff_filespec
*o
= &entry
->stages
[1];
3349 struct diff_filespec
*a
= &entry
->stages
[2];
3350 struct diff_filespec
*b
= &entry
->stages
[3];
3351 int o_valid
= is_valid(o
);
3352 int a_valid
= is_valid(a
);
3353 int b_valid
= is_valid(b
);
3354 o
->path
= a
->path
= b
->path
= (char*)path
;
3356 entry
->processed
= 1;
3357 if (entry
->rename_conflict_info
) {
3358 struct rename_conflict_info
*ci
= entry
->rename_conflict_info
;
3359 struct diff_filespec
*temp
;
3362 path_clean
= warn_about_dir_renamed_entries(opt
, ci
->ren1
);
3363 path_clean
&= warn_about_dir_renamed_entries(opt
, ci
->ren2
);
3366 * For cases with a single rename, {o,a,b}->path have all been
3367 * set to the rename target path; we need to set two of these
3368 * back to the rename source.
3369 * For rename/rename conflicts, we'll manually fix paths below.
3371 temp
= (opt
->branch1
== ci
->ren1
->branch
) ? b
: a
;
3372 o
->path
= temp
->path
= ci
->ren1
->pair
->one
->path
;
3374 assert(opt
->branch1
== ci
->ren1
->branch
);
3377 switch (ci
->rename_type
) {
3379 case RENAME_ONE_FILE_TO_ONE
:
3380 clean_merge
= handle_rename_normal(opt
, path
, o
, a
, b
,
3383 case RENAME_VIA_DIR
:
3384 clean_merge
= handle_rename_via_dir(opt
, ci
);
3388 * Probably unclean merge, but if the renamed file
3389 * merges cleanly and the result can then be
3390 * two-way merged cleanly with the added file, I
3391 * guess it's a clean merge?
3393 clean_merge
= handle_rename_add(opt
, ci
);
3397 if (handle_rename_delete(opt
, ci
))
3400 case RENAME_ONE_FILE_TO_TWO
:
3402 * Manually fix up paths; note:
3403 * ren[12]->pair->one->path are equal.
3405 o
->path
= ci
->ren1
->pair
->one
->path
;
3406 a
->path
= ci
->ren1
->pair
->two
->path
;
3407 b
->path
= ci
->ren2
->pair
->two
->path
;
3410 if (handle_rename_rename_1to2(opt
, ci
))
3413 case RENAME_TWO_FILES_TO_ONE
:
3415 * Manually fix up paths; note,
3416 * ren[12]->pair->two->path are actually equal.
3419 a
->path
= ci
->ren1
->pair
->two
->path
;
3420 b
->path
= ci
->ren2
->pair
->two
->path
;
3423 * Probably unclean merge, but if the two renamed
3424 * files merge cleanly and the two resulting files
3425 * can then be two-way merged cleanly, I guess it's
3428 clean_merge
= handle_rename_rename_2to1(opt
, ci
);
3431 entry
->processed
= 0;
3434 if (path_clean
< clean_merge
)
3435 clean_merge
= path_clean
;
3436 } else if (o_valid
&& (!a_valid
|| !b_valid
)) {
3437 /* Case A: Deleted in one */
3438 if ((!a_valid
&& !b_valid
) ||
3439 (!b_valid
&& blob_unchanged(opt
, o
, a
, normalize
, path
)) ||
3440 (!a_valid
&& blob_unchanged(opt
, o
, b
, normalize
, path
))) {
3441 /* Deleted in both or deleted in one and
3442 * unchanged in the other */
3444 output(opt
, 2, _("Removing %s"), path
);
3445 /* do not touch working file if it did not exist */
3446 remove_file(opt
, 1, path
, !a_valid
);
3448 /* Modify/delete; deleted side may have put a directory in the way */
3450 if (handle_modify_delete(opt
, path
, o
, a
, b
))
3453 } else if ((!o_valid
&& a_valid
&& !b_valid
) ||
3454 (!o_valid
&& !a_valid
&& b_valid
)) {
3455 /* Case B: Added in one. */
3456 /* [nothing|directory] -> ([nothing|directory], file) */
3458 const char *add_branch
;
3459 const char *other_branch
;
3461 const struct diff_filespec
*contents
;
3464 add_branch
= opt
->branch1
;
3465 other_branch
= opt
->branch2
;
3467 conf
= _("file/directory");
3469 add_branch
= opt
->branch2
;
3470 other_branch
= opt
->branch1
;
3472 conf
= _("directory/file");
3474 if (dir_in_way(opt
->repo
->index
, path
,
3475 !opt
->priv
->call_depth
&& !S_ISGITLINK(a
->mode
),
3477 char *new_path
= unique_path(opt
, path
, add_branch
);
3479 output(opt
, 1, _("CONFLICT (%s): There is a directory with name %s in %s. "
3481 conf
, path
, other_branch
, path
, new_path
);
3482 if (update_file(opt
, 0, contents
, new_path
))
3484 else if (opt
->priv
->call_depth
)
3485 remove_file_from_index(opt
->repo
->index
, path
);
3488 output(opt
, 2, _("Adding %s"), path
);
3489 /* do not overwrite file if already present */
3490 if (update_file_flags(opt
, contents
, path
, 1, !a_valid
))
3493 } else if (a_valid
&& b_valid
) {
3495 /* Case C: Added in both (check for same permissions) */
3497 _("CONFLICT (add/add): Merge conflict in %s"),
3499 clean_merge
= handle_file_collision(opt
,
3505 /* case D: Modified in both, but differently. */
3506 struct merge_file_info mfi
;
3507 int is_dirty
= 0; /* unpack_trees would have bailed if dirty */
3508 clean_merge
= handle_content_merge(&mfi
, opt
, path
,
3512 } else if (!o_valid
&& !a_valid
&& !b_valid
) {
3514 * this entry was deleted altogether. a_mode == 0 means
3515 * we had that path and want to actively remove it.
3517 remove_file(opt
, 1, path
, !a
->mode
);
3519 BUG("fatal merge failure, shouldn't happen.");
3524 static int merge_trees_internal(struct merge_options
*opt
,
3527 struct tree
*merge_base
,
3528 struct tree
**result
)
3530 struct index_state
*istate
= opt
->repo
->index
;
3533 if (opt
->subtree_shift
) {
3534 merge
= shift_tree_object(opt
->repo
, head
, merge
,
3535 opt
->subtree_shift
);
3536 merge_base
= shift_tree_object(opt
->repo
, head
, merge_base
,
3537 opt
->subtree_shift
);
3540 if (oideq(&merge_base
->object
.oid
, &merge
->object
.oid
)) {
3541 output(opt
, 0, _("Already up to date."));
3546 code
= unpack_trees_start(opt
, merge_base
, head
, merge
);
3549 if (show(opt
, 4) || opt
->priv
->call_depth
)
3550 err(opt
, _("merging of trees %s and %s failed"),
3551 oid_to_hex(&head
->object
.oid
),
3552 oid_to_hex(&merge
->object
.oid
));
3553 unpack_trees_finish(opt
);
3557 if (unmerged_index(istate
)) {
3558 struct string_list
*entries
;
3559 struct rename_info re_info
;
3562 * Only need the hashmap while processing entries, so
3563 * initialize it here and free it when we are done running
3564 * through the entries. Keeping it in the merge_options as
3565 * opposed to decaring a local hashmap is for convenience
3566 * so that we don't have to pass it to around.
3568 hashmap_init(&opt
->priv
->current_file_dir_set
, path_hashmap_cmp
,
3570 get_files_dirs(opt
, head
);
3571 get_files_dirs(opt
, merge
);
3573 entries
= get_unmerged(opt
->repo
->index
);
3574 clean
= detect_and_process_renames(opt
, merge_base
, head
, merge
,
3576 record_df_conflict_files(opt
, entries
);
3579 for (i
= entries
->nr
-1; 0 <= i
; i
--) {
3580 const char *path
= entries
->items
[i
].string
;
3581 struct stage_data
*e
= entries
->items
[i
].util
;
3582 if (!e
->processed
) {
3583 int ret
= process_entry(opt
, path
, e
);
3592 for (i
= 0; i
< entries
->nr
; i
++) {
3593 struct stage_data
*e
= entries
->items
[i
].util
;
3595 BUG("unprocessed path??? %s",
3596 entries
->items
[i
].string
);
3600 final_cleanup_renames(&re_info
);
3602 string_list_clear(entries
, 1);
3605 hashmap_clear_and_free(&opt
->priv
->current_file_dir_set
,
3606 struct path_hashmap_entry
, e
);
3609 unpack_trees_finish(opt
);
3616 unpack_trees_finish(opt
);
3618 if (opt
->priv
->call_depth
&&
3619 !(*result
= write_in_core_index_as_tree(opt
->repo
)))
3626 * Merge the commits h1 and h2, returning a flag (int) indicating the
3627 * cleanness of the merge. Also, if opt->priv->call_depth, create a
3628 * virtual commit and write its location to *result.
3630 static int merge_recursive_internal(struct merge_options
*opt
,
3633 struct commit_list
*merge_bases
,
3634 struct commit
**result
)
3636 struct commit_list
*iter
;
3637 struct commit
*merged_merge_bases
;
3638 struct tree
*result_tree
;
3640 const char *ancestor_name
;
3641 struct strbuf merge_base_abbrev
= STRBUF_INIT
;
3644 output(opt
, 4, _("Merging:"));
3645 output_commit_title(opt
, h1
);
3646 output_commit_title(opt
, h2
);
3650 if (repo_get_merge_bases(the_repository
, h1
, h2
,
3653 merge_bases
= reverse_commit_list(merge_bases
);
3657 unsigned cnt
= commit_list_count(merge_bases
);
3659 output(opt
, 5, Q_("found %u common ancestor:",
3660 "found %u common ancestors:", cnt
), cnt
);
3661 for (iter
= merge_bases
; iter
; iter
= iter
->next
)
3662 output_commit_title(opt
, iter
->item
);
3665 merged_merge_bases
= pop_commit(&merge_bases
);
3666 if (!merged_merge_bases
) {
3667 /* if there is no common ancestor, use an empty tree */
3670 tree
= lookup_tree(opt
->repo
, opt
->repo
->hash_algo
->empty_tree
);
3671 merged_merge_bases
= make_virtual_commit(opt
->repo
, tree
,
3673 ancestor_name
= "empty tree";
3674 } else if (opt
->ancestor
&& !opt
->priv
->call_depth
) {
3675 ancestor_name
= opt
->ancestor
;
3676 } else if (merge_bases
) {
3677 ancestor_name
= "merged common ancestors";
3679 strbuf_add_unique_abbrev(&merge_base_abbrev
,
3680 &merged_merge_bases
->object
.oid
,
3682 ancestor_name
= merge_base_abbrev
.buf
;
3685 for (iter
= merge_bases
; iter
; iter
= iter
->next
) {
3686 const char *saved_b1
, *saved_b2
;
3687 opt
->priv
->call_depth
++;
3689 * When the merge fails, the result contains files
3690 * with conflict markers. The cleanness flag is
3691 * ignored (unless indicating an error), it was never
3692 * actually used, as result of merge_trees has always
3693 * overwritten it: the committed "conflicts" were
3696 discard_index(opt
->repo
->index
);
3697 saved_b1
= opt
->branch1
;
3698 saved_b2
= opt
->branch2
;
3699 opt
->branch1
= "Temporary merge branch 1";
3700 opt
->branch2
= "Temporary merge branch 2";
3701 if (merge_recursive_internal(opt
, merged_merge_bases
, iter
->item
,
3702 NULL
, &merged_merge_bases
) < 0)
3704 opt
->branch1
= saved_b1
;
3705 opt
->branch2
= saved_b2
;
3706 opt
->priv
->call_depth
--;
3708 if (!merged_merge_bases
)
3709 return err(opt
, _("merge returned no commit"));
3713 * FIXME: Since merge_recursive_internal() is only ever called by
3714 * places that ensure the index is loaded first
3715 * (e.g. builtin/merge.c, rebase/sequencer, etc.), in the common
3716 * case where the merge base was unique that means when we get here
3717 * we immediately discard the index and re-read it, which is a
3718 * complete waste of time. We should only be discarding and
3719 * re-reading if we were forced to recurse.
3721 discard_index(opt
->repo
->index
);
3722 if (!opt
->priv
->call_depth
)
3723 repo_read_index(opt
->repo
);
3725 opt
->ancestor
= ancestor_name
;
3726 clean
= merge_trees_internal(opt
,
3727 repo_get_commit_tree(opt
->repo
, h1
),
3728 repo_get_commit_tree(opt
->repo
, h2
),
3729 repo_get_commit_tree(opt
->repo
,
3730 merged_merge_bases
),
3732 strbuf_release(&merge_base_abbrev
);
3733 opt
->ancestor
= NULL
; /* avoid accidental re-use of opt->ancestor */
3739 if (opt
->priv
->call_depth
) {
3740 *result
= make_virtual_commit(opt
->repo
, result_tree
,
3742 commit_list_insert(h1
, &(*result
)->parents
);
3743 commit_list_insert(h2
, &(*result
)->parents
->next
);
3748 static int merge_start(struct merge_options
*opt
, struct tree
*head
)
3750 struct strbuf sb
= STRBUF_INIT
;
3752 /* Sanity checks on opt */
3755 assert(opt
->branch1
&& opt
->branch2
);
3757 assert(opt
->detect_renames
>= -1 &&
3758 opt
->detect_renames
<= DIFF_DETECT_COPY
);
3759 assert(opt
->detect_directory_renames
>= MERGE_DIRECTORY_RENAMES_NONE
&&
3760 opt
->detect_directory_renames
<= MERGE_DIRECTORY_RENAMES_TRUE
);
3761 assert(opt
->rename_limit
>= -1);
3762 assert(opt
->rename_score
>= 0 && opt
->rename_score
<= MAX_SCORE
);
3763 assert(opt
->show_rename_progress
>= 0 && opt
->show_rename_progress
<= 1);
3765 assert(opt
->xdl_opts
>= 0);
3766 assert(opt
->recursive_variant
>= MERGE_VARIANT_NORMAL
&&
3767 opt
->recursive_variant
<= MERGE_VARIANT_THEIRS
);
3769 assert(opt
->verbosity
>= 0 && opt
->verbosity
<= 5);
3770 assert(opt
->buffer_output
<= 2);
3771 assert(opt
->obuf
.len
== 0);
3773 assert(opt
->priv
== NULL
);
3775 /* Not supported; option specific to merge-ort */
3776 assert(!opt
->record_conflict_msgs_as_headers
);
3777 assert(!opt
->msg_header_prefix
);
3779 /* Sanity check on repo state; index must match head */
3780 if (repo_index_has_changes(opt
->repo
, head
, &sb
)) {
3781 err(opt
, _("Your local changes to the following files would be overwritten by merge:\n %s"),
3783 strbuf_release(&sb
);
3787 CALLOC_ARRAY(opt
->priv
, 1);
3788 string_list_init_dup(&opt
->priv
->df_conflict_file_set
);
3792 static void merge_finalize(struct merge_options
*opt
)
3795 if (!opt
->priv
->call_depth
&& opt
->buffer_output
< 2)
3796 strbuf_release(&opt
->obuf
);
3798 diff_warn_rename_limit("merge.renamelimit",
3799 opt
->priv
->needed_rename_limit
, 0);
3800 FREE_AND_NULL(opt
->priv
);
3803 int merge_trees(struct merge_options
*opt
,
3806 struct tree
*merge_base
)
3809 struct tree
*ignored
;
3811 assert(opt
->ancestor
!= NULL
);
3813 if (merge_start(opt
, head
))
3815 clean
= merge_trees_internal(opt
, head
, merge
, merge_base
, &ignored
);
3816 merge_finalize(opt
);
3821 int merge_recursive(struct merge_options
*opt
,
3824 struct commit_list
*merge_bases
,
3825 struct commit
**result
)
3829 assert(opt
->ancestor
== NULL
||
3830 !strcmp(opt
->ancestor
, "constructed merge base"));
3832 prepare_repo_settings(opt
->repo
);
3833 opt
->repo
->settings
.command_requires_full_index
= 1;
3835 if (merge_start(opt
, repo_get_commit_tree(opt
->repo
, h1
)))
3837 clean
= merge_recursive_internal(opt
, h1
, h2
, merge_bases
, result
);
3838 merge_finalize(opt
);
3843 static struct commit
*get_ref(struct repository
*repo
,
3844 const struct object_id
*oid
,
3847 struct object
*object
;
3849 object
= deref_tag(repo
, parse_object(repo
, oid
),
3850 name
, strlen(name
));
3853 if (object
->type
== OBJ_TREE
)
3854 return make_virtual_commit(repo
, (struct tree
*)object
, name
);
3855 if (object
->type
!= OBJ_COMMIT
)
3857 if (repo_parse_commit(repo
, (struct commit
*)object
))
3859 return (struct commit
*)object
;
3862 int merge_recursive_generic(struct merge_options
*opt
,
3863 const struct object_id
*head
,
3864 const struct object_id
*merge
,
3865 int num_merge_bases
,
3866 const struct object_id
**merge_bases
,
3867 struct commit
**result
)
3870 struct lock_file lock
= LOCK_INIT
;
3871 struct commit
*head_commit
= get_ref(opt
->repo
, head
, opt
->branch1
);
3872 struct commit
*next_commit
= get_ref(opt
->repo
, merge
, opt
->branch2
);
3873 struct commit_list
*ca
= NULL
;
3877 for (i
= 0; i
< num_merge_bases
; ++i
) {
3878 struct commit
*base
;
3879 if (!(base
= get_ref(opt
->repo
, merge_bases
[i
],
3880 oid_to_hex(merge_bases
[i
]))))
3881 return err(opt
, _("Could not parse object '%s'"),
3882 oid_to_hex(merge_bases
[i
]));
3883 commit_list_insert(base
, &ca
);
3885 if (num_merge_bases
== 1)
3886 opt
->ancestor
= "constructed merge base";
3889 repo_hold_locked_index(opt
->repo
, &lock
, LOCK_DIE_ON_ERROR
);
3890 clean
= merge_recursive(opt
, head_commit
, next_commit
, ca
,
3893 rollback_lock_file(&lock
);
3897 if (write_locked_index(opt
->repo
->index
, &lock
,
3898 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
3899 return err(opt
, _("Unable to write index."));
3901 return clean
? 0 : 1;
3904 static void merge_recursive_config(struct merge_options
*opt
)
3907 int renormalize
= 0;
3908 git_config_get_int("merge.verbosity", &opt
->verbosity
);
3909 git_config_get_int("diff.renamelimit", &opt
->rename_limit
);
3910 git_config_get_int("merge.renamelimit", &opt
->rename_limit
);
3911 git_config_get_bool("merge.renormalize", &renormalize
);
3912 opt
->renormalize
= renormalize
;
3913 if (!git_config_get_string("diff.renames", &value
)) {
3914 opt
->detect_renames
= git_config_rename("diff.renames", value
);
3917 if (!git_config_get_string("merge.renames", &value
)) {
3918 opt
->detect_renames
= git_config_rename("merge.renames", value
);
3921 if (!git_config_get_string("merge.directoryrenames", &value
)) {
3922 int boolval
= git_parse_maybe_bool(value
);
3924 opt
->detect_directory_renames
= boolval
?
3925 MERGE_DIRECTORY_RENAMES_TRUE
:
3926 MERGE_DIRECTORY_RENAMES_NONE
;
3927 } else if (!strcasecmp(value
, "conflict")) {
3928 opt
->detect_directory_renames
=
3929 MERGE_DIRECTORY_RENAMES_CONFLICT
;
3930 } /* avoid erroring on values from future versions of git */
3933 git_config(git_xmerge_config
, NULL
);
3936 void init_merge_options(struct merge_options
*opt
,
3937 struct repository
*repo
)
3939 const char *merge_verbosity
;
3940 memset(opt
, 0, sizeof(struct merge_options
));
3944 opt
->detect_renames
= -1;
3945 opt
->detect_directory_renames
= MERGE_DIRECTORY_RENAMES_CONFLICT
;
3946 opt
->rename_limit
= -1;
3949 opt
->buffer_output
= 1;
3950 strbuf_init(&opt
->obuf
, 0);
3952 opt
->renormalize
= 0;
3954 opt
->conflict_style
= -1;
3956 merge_recursive_config(opt
);
3957 merge_verbosity
= getenv("GIT_MERGE_VERBOSITY");
3958 if (merge_verbosity
)
3959 opt
->verbosity
= strtol(merge_verbosity
, NULL
, 10);
3960 if (opt
->verbosity
>= 5)
3961 opt
->buffer_output
= 0;
3965 * For now, members of merge_options do not need deep copying, but
3966 * it may change in the future, in which case we would need to update
3967 * this, and also make a matching change to clear_merge_options() to
3968 * release the resources held by a copied instance.
3970 void copy_merge_options(struct merge_options
*dst
, struct merge_options
*src
)
3975 void clear_merge_options(struct merge_options
*opt UNUSED
)
3977 ; /* no-op as our copy is shallow right now */
3980 int parse_merge_opt(struct merge_options
*opt
, const char *s
)
3986 if (!strcmp(s
, "ours"))
3987 opt
->recursive_variant
= MERGE_VARIANT_OURS
;
3988 else if (!strcmp(s
, "theirs"))
3989 opt
->recursive_variant
= MERGE_VARIANT_THEIRS
;
3990 else if (!strcmp(s
, "subtree"))
3991 opt
->subtree_shift
= "";
3992 else if (skip_prefix(s
, "subtree=", &arg
))
3993 opt
->subtree_shift
= arg
;
3994 else if (!strcmp(s
, "patience"))
3995 opt
->xdl_opts
= DIFF_WITH_ALG(opt
, PATIENCE_DIFF
);
3996 else if (!strcmp(s
, "histogram"))
3997 opt
->xdl_opts
= DIFF_WITH_ALG(opt
, HISTOGRAM_DIFF
);
3998 else if (skip_prefix(s
, "diff-algorithm=", &arg
)) {
3999 long value
= parse_algorithm_value(arg
);
4002 /* clear out previous settings */
4003 DIFF_XDL_CLR(opt
, NEED_MINIMAL
);
4004 opt
->xdl_opts
&= ~XDF_DIFF_ALGORITHM_MASK
;
4005 opt
->xdl_opts
|= value
;
4007 else if (!strcmp(s
, "ignore-space-change"))
4008 DIFF_XDL_SET(opt
, IGNORE_WHITESPACE_CHANGE
);
4009 else if (!strcmp(s
, "ignore-all-space"))
4010 DIFF_XDL_SET(opt
, IGNORE_WHITESPACE
);
4011 else if (!strcmp(s
, "ignore-space-at-eol"))
4012 DIFF_XDL_SET(opt
, IGNORE_WHITESPACE_AT_EOL
);
4013 else if (!strcmp(s
, "ignore-cr-at-eol"))
4014 DIFF_XDL_SET(opt
, IGNORE_CR_AT_EOL
);
4015 else if (!strcmp(s
, "renormalize"))
4016 opt
->renormalize
= 1;
4017 else if (!strcmp(s
, "no-renormalize"))
4018 opt
->renormalize
= 0;
4019 else if (!strcmp(s
, "no-renames"))
4020 opt
->detect_renames
= 0;
4021 else if (!strcmp(s
, "find-renames")) {
4022 opt
->detect_renames
= 1;
4023 opt
->rename_score
= 0;
4025 else if (skip_prefix(s
, "find-renames=", &arg
) ||
4026 skip_prefix(s
, "rename-threshold=", &arg
)) {
4027 if ((opt
->rename_score
= parse_rename_score(&arg
)) == -1 || *arg
!= 0)
4029 opt
->detect_renames
= 1;
4032 * Please update $__git_merge_strategy_options in
4033 * git-completion.bash when you add new options