2 * Recursive Merge algorithm stolen from git-merge-recursive.py by
4 * The thieves were Alex Riesen and Johannes Schindelin, in June/July 2006
7 #include "merge-recursive.h"
14 #include "cache-tree.h"
16 #include "commit-reach.h"
23 #include "object-store.h"
24 #include "repository.h"
26 #include "string-list.h"
27 #include "submodule-config.h"
28 #include "submodule.h"
30 #include "tree-walk.h"
31 #include "unpack-trees.h"
32 #include "xdiff-interface.h"
34 struct merge_options_internal
{
36 int needed_rename_limit
;
37 struct hashmap current_file_dir_set
;
38 struct string_list df_conflict_file_set
;
39 struct unpack_trees_options unpack_opts
;
40 struct index_state orig_index
;
43 struct path_hashmap_entry
{
44 struct hashmap_entry e
;
45 char path
[FLEX_ARRAY
];
48 static int path_hashmap_cmp(const void *cmp_data
,
49 const struct hashmap_entry
*eptr
,
50 const struct hashmap_entry
*entry_or_key
,
53 const struct path_hashmap_entry
*a
, *b
;
54 const char *key
= keydata
;
56 a
= container_of(eptr
, const struct path_hashmap_entry
, e
);
57 b
= container_of(entry_or_key
, const struct path_hashmap_entry
, e
);
59 return fspathcmp(a
->path
, key
? key
: b
->path
);
63 * For dir_rename_entry, directory names are stored as a full path from the
64 * toplevel of the repository and do not include a trailing '/'. Also:
66 * dir: original name of directory being renamed
67 * non_unique_new_dir: if true, could not determine new_dir
68 * new_dir: final name of directory being renamed
69 * possible_new_dirs: temporary used to help determine new_dir; see comments
70 * in get_directory_renames() for details
72 struct dir_rename_entry
{
73 struct hashmap_entry ent
;
75 unsigned non_unique_new_dir
:1;
76 struct strbuf new_dir
;
77 struct string_list possible_new_dirs
;
80 static struct dir_rename_entry
*dir_rename_find_entry(struct hashmap
*hashmap
,
83 struct dir_rename_entry key
;
87 hashmap_entry_init(&key
.ent
, strhash(dir
));
89 return hashmap_get_entry(hashmap
, &key
, ent
, NULL
);
92 static int dir_rename_cmp(const void *unused_cmp_data
,
93 const struct hashmap_entry
*eptr
,
94 const struct hashmap_entry
*entry_or_key
,
95 const void *unused_keydata
)
97 const struct dir_rename_entry
*e1
, *e2
;
99 e1
= container_of(eptr
, const struct dir_rename_entry
, ent
);
100 e2
= container_of(entry_or_key
, const struct dir_rename_entry
, ent
);
102 return strcmp(e1
->dir
, e2
->dir
);
105 static void dir_rename_init(struct hashmap
*map
)
107 hashmap_init(map
, dir_rename_cmp
, NULL
, 0);
110 static void dir_rename_entry_init(struct dir_rename_entry
*entry
,
113 hashmap_entry_init(&entry
->ent
, strhash(directory
));
114 entry
->dir
= directory
;
115 entry
->non_unique_new_dir
= 0;
116 strbuf_init(&entry
->new_dir
, 0);
117 string_list_init_nodup(&entry
->possible_new_dirs
);
120 struct collision_entry
{
121 struct hashmap_entry ent
;
123 struct string_list source_files
;
124 unsigned reported_already
:1;
127 static struct collision_entry
*collision_find_entry(struct hashmap
*hashmap
,
130 struct collision_entry key
;
132 hashmap_entry_init(&key
.ent
, strhash(target_file
));
133 key
.target_file
= target_file
;
134 return hashmap_get_entry(hashmap
, &key
, ent
, NULL
);
137 static int collision_cmp(const void *unused_cmp_data
,
138 const struct hashmap_entry
*eptr
,
139 const struct hashmap_entry
*entry_or_key
,
140 const void *unused_keydata
)
142 const struct collision_entry
*e1
, *e2
;
144 e1
= container_of(eptr
, const struct collision_entry
, ent
);
145 e2
= container_of(entry_or_key
, const struct collision_entry
, ent
);
147 return strcmp(e1
->target_file
, e2
->target_file
);
150 static void collision_init(struct hashmap
*map
)
152 hashmap_init(map
, collision_cmp
, NULL
, 0);
155 static void flush_output(struct merge_options
*opt
)
157 if (opt
->buffer_output
< 2 && opt
->obuf
.len
) {
158 fputs(opt
->obuf
.buf
, stdout
);
159 strbuf_reset(&opt
->obuf
);
163 __attribute__((format (printf
, 2, 3)))
164 static int err(struct merge_options
*opt
, const char *err
, ...)
168 if (opt
->buffer_output
< 2)
171 strbuf_complete(&opt
->obuf
, '\n');
172 strbuf_addstr(&opt
->obuf
, "error: ");
174 va_start(params
, err
);
175 strbuf_vaddf(&opt
->obuf
, err
, params
);
177 if (opt
->buffer_output
> 1)
178 strbuf_addch(&opt
->obuf
, '\n');
180 error("%s", opt
->obuf
.buf
);
181 strbuf_reset(&opt
->obuf
);
187 static struct tree
*shift_tree_object(struct repository
*repo
,
188 struct tree
*one
, struct tree
*two
,
189 const char *subtree_shift
)
191 struct object_id shifted
;
193 if (!*subtree_shift
) {
194 shift_tree(repo
, &one
->object
.oid
, &two
->object
.oid
, &shifted
, 0);
196 shift_tree_by(repo
, &one
->object
.oid
, &two
->object
.oid
, &shifted
,
199 if (oideq(&two
->object
.oid
, &shifted
))
201 return lookup_tree(repo
, &shifted
);
204 static inline void set_commit_tree(struct commit
*c
, struct tree
*t
)
209 static struct commit
*make_virtual_commit(struct repository
*repo
,
213 struct commit
*commit
= alloc_commit_node(repo
);
215 set_merge_remote_desc(commit
, comment
, (struct object
*)commit
);
216 set_commit_tree(commit
, tree
);
217 commit
->object
.parsed
= 1;
226 RENAME_ONE_FILE_TO_ONE
,
227 RENAME_ONE_FILE_TO_TWO
,
228 RENAME_TWO_FILES_TO_ONE
232 * Since we want to write the index eventually, we cannot reuse the index
233 * for these (temporary) data.
236 struct diff_filespec stages
[4]; /* mostly for oid & mode; maybe path */
237 struct rename_conflict_info
*rename_conflict_info
;
238 unsigned processed
:1;
242 unsigned processed
:1;
243 struct diff_filepair
*pair
;
244 const char *branch
; /* branch that the rename occurred on */
246 * If directory rename detection affected this rename, what was its
247 * original type ('A' or 'R') and it's original destination before
248 * the directory rename (otherwise, '\0' and NULL for these two vars).
250 char dir_rename_original_type
;
251 char *dir_rename_original_dest
;
253 * Purpose of src_entry and dst_entry:
255 * If 'before' is renamed to 'after' then src_entry will contain
256 * the versions of 'before' from the merge_base, HEAD, and MERGE in
257 * stages 1, 2, and 3; dst_entry will contain the respective
258 * versions of 'after' in corresponding locations. Thus, we have a
259 * total of six modes and oids, though some will be null. (Stage 0
260 * is ignored; we're interested in handling conflicts.)
262 * Since we don't turn on break-rewrites by default, neither
263 * src_entry nor dst_entry can have all three of their stages have
264 * non-null oids, meaning at most four of the six will be non-null.
265 * Also, since this is a rename, both src_entry and dst_entry will
266 * have at least one non-null oid, meaning at least two will be
267 * non-null. Of the six oids, a typical rename will have three be
268 * non-null. Only two implies a rename/delete, and four implies a
271 struct stage_data
*src_entry
;
272 struct stage_data
*dst_entry
;
275 struct rename_conflict_info
{
276 enum rename_type rename_type
;
281 static inline void setup_rename_conflict_info(enum rename_type rename_type
,
282 struct merge_options
*opt
,
286 struct rename_conflict_info
*ci
;
289 * When we have two renames involved, it's easiest to get the
290 * correct things into stage 2 and 3, and to make sure that the
291 * content merge puts HEAD before the other branch if we just
292 * ensure that branch1 == opt->branch1. So, simply flip arguments
293 * around if we don't have that.
295 if (ren2
&& ren1
->branch
!= opt
->branch1
) {
296 setup_rename_conflict_info(rename_type
, opt
, ren2
, ren1
);
301 ci
->rename_type
= rename_type
;
305 ci
->ren1
->dst_entry
->processed
= 0;
306 ci
->ren1
->dst_entry
->rename_conflict_info
= ci
;
308 ci
->ren2
->dst_entry
->rename_conflict_info
= ci
;
312 static int show(struct merge_options
*opt
, int v
)
314 return (!opt
->priv
->call_depth
&& opt
->verbosity
>= v
) ||
318 __attribute__((format (printf
, 3, 4)))
319 static void output(struct merge_options
*opt
, int v
, const char *fmt
, ...)
326 strbuf_addchars(&opt
->obuf
, ' ', opt
->priv
->call_depth
* 2);
329 strbuf_vaddf(&opt
->obuf
, fmt
, ap
);
332 strbuf_addch(&opt
->obuf
, '\n');
333 if (!opt
->buffer_output
)
337 static void repo_output_commit_title(struct merge_options
*opt
,
338 struct repository
*repo
,
339 struct commit
*commit
)
341 struct merge_remote_desc
*desc
;
343 strbuf_addchars(&opt
->obuf
, ' ', opt
->priv
->call_depth
* 2);
344 desc
= merge_remote_util(commit
);
346 strbuf_addf(&opt
->obuf
, "virtual %s\n", desc
->name
);
348 strbuf_repo_add_unique_abbrev(&opt
->obuf
, repo
,
351 strbuf_addch(&opt
->obuf
, ' ');
352 if (repo_parse_commit(repo
, commit
) != 0)
353 strbuf_addstr(&opt
->obuf
, _("(bad commit)\n"));
356 const char *msg
= repo_get_commit_buffer(repo
, commit
, NULL
);
357 int len
= find_commit_subject(msg
, &title
);
359 strbuf_addf(&opt
->obuf
, "%.*s\n", len
, title
);
360 repo_unuse_commit_buffer(repo
, commit
, msg
);
366 static void output_commit_title(struct merge_options
*opt
, struct commit
*commit
)
368 repo_output_commit_title(opt
, the_repository
, commit
);
371 static int add_cacheinfo(struct merge_options
*opt
,
372 const struct diff_filespec
*blob
,
373 const char *path
, int stage
, int refresh
, int options
)
375 struct index_state
*istate
= opt
->repo
->index
;
376 struct cache_entry
*ce
;
379 ce
= make_cache_entry(istate
, blob
->mode
, &blob
->oid
, path
, stage
, 0);
381 return err(opt
, _("add_cacheinfo failed for path '%s'; merge aborting."), path
);
383 ret
= add_index_entry(istate
, ce
, options
);
385 struct cache_entry
*nce
;
387 nce
= refresh_cache_entry(istate
, ce
,
388 CE_MATCH_REFRESH
| CE_MATCH_IGNORE_MISSING
);
390 return err(opt
, _("add_cacheinfo failed to refresh for path '%s'; merge aborting."), path
);
392 ret
= add_index_entry(istate
, nce
, options
);
397 static inline int merge_detect_rename(struct merge_options
*opt
)
399 return (opt
->detect_renames
>= 0) ? opt
->detect_renames
: 1;
402 static void init_tree_desc_from_tree(struct tree_desc
*desc
, struct tree
*tree
)
405 init_tree_desc(desc
, tree
->buffer
, tree
->size
);
408 static int unpack_trees_start(struct merge_options
*opt
,
414 struct tree_desc t
[3];
415 struct index_state tmp_index
= { NULL
};
417 memset(&opt
->priv
->unpack_opts
, 0, sizeof(opt
->priv
->unpack_opts
));
418 if (opt
->priv
->call_depth
)
419 opt
->priv
->unpack_opts
.index_only
= 1;
421 opt
->priv
->unpack_opts
.update
= 1;
422 /* FIXME: should only do this if !overwrite_ignore */
423 opt
->priv
->unpack_opts
.preserve_ignored
= 0;
425 opt
->priv
->unpack_opts
.merge
= 1;
426 opt
->priv
->unpack_opts
.head_idx
= 2;
427 opt
->priv
->unpack_opts
.fn
= threeway_merge
;
428 opt
->priv
->unpack_opts
.src_index
= opt
->repo
->index
;
429 opt
->priv
->unpack_opts
.dst_index
= &tmp_index
;
430 opt
->priv
->unpack_opts
.aggressive
= !merge_detect_rename(opt
);
431 setup_unpack_trees_porcelain(&opt
->priv
->unpack_opts
, "merge");
433 init_tree_desc_from_tree(t
+0, common
);
434 init_tree_desc_from_tree(t
+1, head
);
435 init_tree_desc_from_tree(t
+2, merge
);
437 rc
= unpack_trees(3, t
, &opt
->priv
->unpack_opts
);
438 cache_tree_free(&opt
->repo
->index
->cache_tree
);
441 * Update opt->repo->index to match the new results, AFTER saving a
442 * copy in opt->priv->orig_index. Update src_index to point to the
443 * saved copy. (verify_uptodate() checks src_index, and the original
444 * index is the one that had the necessary modification timestamps.)
446 opt
->priv
->orig_index
= *opt
->repo
->index
;
447 *opt
->repo
->index
= tmp_index
;
448 opt
->priv
->unpack_opts
.src_index
= &opt
->priv
->orig_index
;
453 static void unpack_trees_finish(struct merge_options
*opt
)
455 discard_index(&opt
->priv
->orig_index
);
456 clear_unpack_trees_porcelain(&opt
->priv
->unpack_opts
);
459 static int save_files_dirs(const struct object_id
*oid
,
460 struct strbuf
*base
, const char *path
,
461 unsigned int mode
, void *context
)
463 struct path_hashmap_entry
*entry
;
464 int baselen
= base
->len
;
465 struct merge_options
*opt
= context
;
467 strbuf_addstr(base
, path
);
469 FLEX_ALLOC_MEM(entry
, path
, base
->buf
, base
->len
);
470 hashmap_entry_init(&entry
->e
, fspathhash(entry
->path
));
471 hashmap_add(&opt
->priv
->current_file_dir_set
, &entry
->e
);
473 strbuf_setlen(base
, baselen
);
474 return (S_ISDIR(mode
) ? READ_TREE_RECURSIVE
: 0);
477 static void get_files_dirs(struct merge_options
*opt
, struct tree
*tree
)
479 struct pathspec match_all
;
480 memset(&match_all
, 0, sizeof(match_all
));
481 read_tree(opt
->repo
, tree
,
482 &match_all
, save_files_dirs
, opt
);
485 static int get_tree_entry_if_blob(struct repository
*r
,
486 const struct object_id
*tree
,
488 struct diff_filespec
*dfs
)
492 ret
= get_tree_entry(r
, tree
, path
, &dfs
->oid
, &dfs
->mode
);
493 if (S_ISDIR(dfs
->mode
)) {
494 oidcpy(&dfs
->oid
, null_oid());
501 * Returns an index_entry instance which doesn't have to correspond to
502 * a real cache entry in Git's index.
504 static struct stage_data
*insert_stage_data(struct repository
*r
,
506 struct tree
*o
, struct tree
*a
, struct tree
*b
,
507 struct string_list
*entries
)
509 struct string_list_item
*item
;
510 struct stage_data
*e
= xcalloc(1, sizeof(struct stage_data
));
511 get_tree_entry_if_blob(r
, &o
->object
.oid
, path
, &e
->stages
[1]);
512 get_tree_entry_if_blob(r
, &a
->object
.oid
, path
, &e
->stages
[2]);
513 get_tree_entry_if_blob(r
, &b
->object
.oid
, path
, &e
->stages
[3]);
514 item
= string_list_insert(entries
, path
);
520 * Create a dictionary mapping file names to stage_data objects. The
521 * dictionary contains one entry for every path with a non-zero stage entry.
523 static struct string_list
*get_unmerged(struct index_state
*istate
)
525 struct string_list
*unmerged
= xcalloc(1, sizeof(struct string_list
));
528 unmerged
->strdup_strings
= 1;
530 /* TODO: audit for interaction with sparse-index. */
531 ensure_full_index(istate
);
532 for (i
= 0; i
< istate
->cache_nr
; i
++) {
533 struct string_list_item
*item
;
534 struct stage_data
*e
;
535 const struct cache_entry
*ce
= istate
->cache
[i
];
539 item
= string_list_lookup(unmerged
, ce
->name
);
541 item
= string_list_insert(unmerged
, ce
->name
);
542 item
->util
= xcalloc(1, sizeof(struct stage_data
));
545 e
->stages
[ce_stage(ce
)].mode
= ce
->ce_mode
;
546 oidcpy(&e
->stages
[ce_stage(ce
)].oid
, &ce
->oid
);
552 static int string_list_df_name_compare(const char *one
, const char *two
)
554 int onelen
= strlen(one
);
555 int twolen
= strlen(two
);
557 * Here we only care that entries for D/F conflicts are
558 * adjacent, in particular with the file of the D/F conflict
559 * appearing before files below the corresponding directory.
560 * The order of the rest of the list is irrelevant for us.
562 * To achieve this, we sort with df_name_compare and provide
563 * the mode S_IFDIR so that D/F conflicts will sort correctly.
564 * We use the mode S_IFDIR for everything else for simplicity,
565 * since in other cases any changes in their order due to
566 * sorting cause no problems for us.
568 int cmp
= df_name_compare(one
, onelen
, S_IFDIR
,
569 two
, twolen
, S_IFDIR
);
571 * Now that 'foo' and 'foo/bar' compare equal, we have to make sure
572 * that 'foo' comes before 'foo/bar'.
576 return onelen
- twolen
;
579 static void record_df_conflict_files(struct merge_options
*opt
,
580 struct string_list
*entries
)
582 /* If there is a D/F conflict and the file for such a conflict
583 * currently exists in the working tree, we want to allow it to be
584 * removed to make room for the corresponding directory if needed.
585 * The files underneath the directories of such D/F conflicts will
586 * be processed before the corresponding file involved in the D/F
587 * conflict. If the D/F directory ends up being removed by the
588 * merge, then we won't have to touch the D/F file. If the D/F
589 * directory needs to be written to the working copy, then the D/F
590 * file will simply be removed (in make_room_for_path()) to make
591 * room for the necessary paths. Note that if both the directory
592 * and the file need to be present, then the D/F file will be
593 * reinstated with a new unique name at the time it is processed.
595 struct string_list df_sorted_entries
= STRING_LIST_INIT_NODUP
;
596 const char *last_file
= NULL
;
601 * If we're merging merge-bases, we don't want to bother with
602 * any working directory changes.
604 if (opt
->priv
->call_depth
)
607 /* Ensure D/F conflicts are adjacent in the entries list. */
608 for (i
= 0; i
< entries
->nr
; i
++) {
609 struct string_list_item
*next
= &entries
->items
[i
];
610 string_list_append(&df_sorted_entries
, next
->string
)->util
=
613 df_sorted_entries
.cmp
= string_list_df_name_compare
;
614 string_list_sort(&df_sorted_entries
);
616 string_list_clear(&opt
->priv
->df_conflict_file_set
, 1);
617 for (i
= 0; i
< df_sorted_entries
.nr
; i
++) {
618 const char *path
= df_sorted_entries
.items
[i
].string
;
619 int len
= strlen(path
);
620 struct stage_data
*e
= df_sorted_entries
.items
[i
].util
;
623 * Check if last_file & path correspond to a D/F conflict;
624 * i.e. whether path is last_file+'/'+<something>.
625 * If so, record that it's okay to remove last_file to make
626 * room for path and friends if needed.
630 memcmp(path
, last_file
, last_len
) == 0 &&
631 path
[last_len
] == '/') {
632 string_list_insert(&opt
->priv
->df_conflict_file_set
, last_file
);
636 * Determine whether path could exist as a file in the
637 * working directory as a possible D/F conflict. This
638 * will only occur when it exists in stage 2 as a
641 if (S_ISREG(e
->stages
[2].mode
) || S_ISLNK(e
->stages
[2].mode
)) {
648 string_list_clear(&df_sorted_entries
, 0);
651 static int update_stages(struct merge_options
*opt
, const char *path
,
652 const struct diff_filespec
*o
,
653 const struct diff_filespec
*a
,
654 const struct diff_filespec
*b
)
658 * NOTE: It is usually a bad idea to call update_stages on a path
659 * before calling update_file on that same path, since it can
660 * sometimes lead to spurious "refusing to lose untracked file..."
661 * messages from update_file (via make_room_for path via
662 * would_lose_untracked). Instead, reverse the order of the calls
663 * (executing update_file first and then update_stages).
666 int options
= ADD_CACHE_OK_TO_ADD
| ADD_CACHE_SKIP_DFCHECK
;
668 if (remove_file_from_index(opt
->repo
->index
, path
))
671 if (add_cacheinfo(opt
, o
, path
, 1, 0, options
))
674 if (add_cacheinfo(opt
, a
, path
, 2, 0, options
))
677 if (add_cacheinfo(opt
, b
, path
, 3, 0, options
))
682 static void update_entry(struct stage_data
*entry
,
683 struct diff_filespec
*o
,
684 struct diff_filespec
*a
,
685 struct diff_filespec
*b
)
687 entry
->processed
= 0;
688 entry
->stages
[1].mode
= o
->mode
;
689 entry
->stages
[2].mode
= a
->mode
;
690 entry
->stages
[3].mode
= b
->mode
;
691 oidcpy(&entry
->stages
[1].oid
, &o
->oid
);
692 oidcpy(&entry
->stages
[2].oid
, &a
->oid
);
693 oidcpy(&entry
->stages
[3].oid
, &b
->oid
);
696 static int remove_file(struct merge_options
*opt
, int clean
,
697 const char *path
, int no_wd
)
699 int update_cache
= opt
->priv
->call_depth
|| clean
;
700 int update_working_directory
= !opt
->priv
->call_depth
&& !no_wd
;
703 if (remove_file_from_index(opt
->repo
->index
, path
))
706 if (update_working_directory
) {
708 struct cache_entry
*ce
;
709 ce
= index_file_exists(opt
->repo
->index
, path
, strlen(path
),
711 if (ce
&& ce_stage(ce
) == 0 && strcmp(path
, ce
->name
))
714 if (remove_path(path
))
720 /* add a string to a strbuf, but converting "/" to "_" */
721 static void add_flattened_path(struct strbuf
*out
, const char *s
)
724 strbuf_addstr(out
, s
);
725 for (; i
< out
->len
; i
++)
726 if (out
->buf
[i
] == '/')
730 static char *unique_path(struct merge_options
*opt
,
734 struct path_hashmap_entry
*entry
;
735 struct strbuf newpath
= STRBUF_INIT
;
739 strbuf_addf(&newpath
, "%s~", path
);
740 add_flattened_path(&newpath
, branch
);
742 base_len
= newpath
.len
;
743 while (hashmap_get_from_hash(&opt
->priv
->current_file_dir_set
,
744 fspathhash(newpath
.buf
), newpath
.buf
) ||
745 (!opt
->priv
->call_depth
&& file_exists(newpath
.buf
))) {
746 strbuf_setlen(&newpath
, base_len
);
747 strbuf_addf(&newpath
, "_%d", suffix
++);
750 FLEX_ALLOC_MEM(entry
, path
, newpath
.buf
, newpath
.len
);
751 hashmap_entry_init(&entry
->e
, fspathhash(entry
->path
));
752 hashmap_add(&opt
->priv
->current_file_dir_set
, &entry
->e
);
753 return strbuf_detach(&newpath
, NULL
);
757 * Check whether a directory in the index is in the way of an incoming
758 * file. Return 1 if so. If check_working_copy is non-zero, also
759 * check the working directory. If empty_ok is non-zero, also return
760 * 0 in the case where the working-tree dir exists but is empty.
762 static int dir_in_way(struct index_state
*istate
, const char *path
,
763 int check_working_copy
, int empty_ok
)
766 struct strbuf dirpath
= STRBUF_INIT
;
769 strbuf_addstr(&dirpath
, path
);
770 strbuf_addch(&dirpath
, '/');
772 pos
= index_name_pos(istate
, dirpath
.buf
, dirpath
.len
);
776 if (pos
< istate
->cache_nr
&&
777 !strncmp(dirpath
.buf
, istate
->cache
[pos
]->name
, dirpath
.len
)) {
778 strbuf_release(&dirpath
);
782 strbuf_release(&dirpath
);
783 return check_working_copy
&& !lstat(path
, &st
) && S_ISDIR(st
.st_mode
) &&
784 !(empty_ok
&& is_empty_dir(path
)) &&
785 !has_symlink_leading_path(path
, strlen(path
));
789 * Returns whether path was tracked in the index before the merge started,
790 * and its oid and mode match the specified values
792 static int was_tracked_and_matches(struct merge_options
*opt
, const char *path
,
793 const struct diff_filespec
*blob
)
795 int pos
= index_name_pos(&opt
->priv
->orig_index
, path
, strlen(path
));
796 struct cache_entry
*ce
;
799 /* we were not tracking this path before the merge */
802 /* See if the file we were tracking before matches */
803 ce
= opt
->priv
->orig_index
.cache
[pos
];
804 return (oideq(&ce
->oid
, &blob
->oid
) && ce
->ce_mode
== blob
->mode
);
808 * Returns whether path was tracked in the index before the merge started
810 static int was_tracked(struct merge_options
*opt
, const char *path
)
812 int pos
= index_name_pos(&opt
->priv
->orig_index
, path
, strlen(path
));
815 /* we were tracking this path before the merge */
821 static int would_lose_untracked(struct merge_options
*opt
, const char *path
)
823 struct index_state
*istate
= opt
->repo
->index
;
826 * This may look like it can be simplified to:
827 * return !was_tracked(opt, path) && file_exists(path)
828 * but it can't. This function needs to know whether path was in
829 * the working tree due to EITHER having been tracked in the index
830 * before the merge OR having been put into the working copy and
831 * index by unpack_trees(). Due to that either-or requirement, we
832 * check the current index instead of the original one.
834 * Note that we do not need to worry about merge-recursive itself
835 * updating the index after unpack_trees() and before calling this
836 * function, because we strictly require all code paths in
837 * merge-recursive to update the working tree first and the index
838 * second. Doing otherwise would break
839 * update_file()/would_lose_untracked(); see every comment in this
840 * file which mentions "update_stages".
842 int pos
= index_name_pos(istate
, path
, strlen(path
));
846 while (pos
< istate
->cache_nr
&&
847 !strcmp(path
, istate
->cache
[pos
]->name
)) {
849 * If stage #0, it is definitely tracked.
850 * If it has stage #2 then it was tracked
851 * before this merge started. All other
852 * cases the path was not tracked.
854 switch (ce_stage(istate
->cache
[pos
])) {
861 return file_exists(path
);
864 static int was_dirty(struct merge_options
*opt
, const char *path
)
866 struct cache_entry
*ce
;
869 if (opt
->priv
->call_depth
|| !was_tracked(opt
, path
))
872 ce
= index_file_exists(opt
->priv
->unpack_opts
.src_index
,
873 path
, strlen(path
), ignore_case
);
874 dirty
= verify_uptodate(ce
, &opt
->priv
->unpack_opts
) != 0;
878 static int make_room_for_path(struct merge_options
*opt
, const char *path
)
881 const char *msg
= _("failed to create path '%s'%s");
883 /* Unlink any D/F conflict files that are in the way */
884 for (i
= 0; i
< opt
->priv
->df_conflict_file_set
.nr
; i
++) {
885 const char *df_path
= opt
->priv
->df_conflict_file_set
.items
[i
].string
;
886 size_t pathlen
= strlen(path
);
887 size_t df_pathlen
= strlen(df_path
);
888 if (df_pathlen
< pathlen
&&
889 path
[df_pathlen
] == '/' &&
890 strncmp(path
, df_path
, df_pathlen
) == 0) {
892 _("Removing %s to make room for subdirectory\n"),
895 unsorted_string_list_delete_item(&opt
->priv
->df_conflict_file_set
,
901 /* Make sure leading directories are created */
902 status
= safe_create_leading_directories_const(path
);
904 if (status
== SCLD_EXISTS
)
905 /* something else exists */
906 return err(opt
, msg
, path
, _(": perhaps a D/F conflict?"));
907 return err(opt
, msg
, path
, "");
911 * Do not unlink a file in the work tree if we are not
914 if (would_lose_untracked(opt
, path
))
915 return err(opt
, _("refusing to lose untracked file at '%s'"),
918 /* Successful unlink is good.. */
921 /* .. and so is no existing file */
924 /* .. but not some other error (who really cares what?) */
925 return err(opt
, msg
, path
, _(": perhaps a D/F conflict?"));
928 static int update_file_flags(struct merge_options
*opt
,
929 const struct diff_filespec
*contents
,
936 if (opt
->priv
->call_depth
)
940 enum object_type type
;
944 if (S_ISGITLINK(contents
->mode
)) {
946 * We may later decide to recursively descend into
947 * the submodule directory and update its index
948 * and/or work tree, but we do not do that now.
954 buf
= read_object_file(&contents
->oid
, &type
, &size
);
956 ret
= err(opt
, _("cannot read object %s '%s'"),
957 oid_to_hex(&contents
->oid
), path
);
960 if (type
!= OBJ_BLOB
) {
961 ret
= err(opt
, _("blob expected for %s '%s'"),
962 oid_to_hex(&contents
->oid
), path
);
965 if (S_ISREG(contents
->mode
)) {
966 struct strbuf strbuf
= STRBUF_INIT
;
967 if (convert_to_working_tree(opt
->repo
->index
,
968 path
, buf
, size
, &strbuf
, NULL
)) {
971 buf
= strbuf_detach(&strbuf
, NULL
);
975 if (make_room_for_path(opt
, path
) < 0) {
979 if (S_ISREG(contents
->mode
) ||
980 (!has_symlinks
&& S_ISLNK(contents
->mode
))) {
982 int mode
= (contents
->mode
& 0100 ? 0777 : 0666);
984 fd
= open(path
, O_WRONLY
| O_TRUNC
| O_CREAT
, mode
);
986 ret
= err(opt
, _("failed to open '%s': %s"),
987 path
, strerror(errno
));
990 write_in_full(fd
, buf
, size
);
992 } else if (S_ISLNK(contents
->mode
)) {
993 char *lnk
= xmemdupz(buf
, size
);
994 safe_create_leading_directories_const(path
);
996 if (symlink(lnk
, path
))
997 ret
= err(opt
, _("failed to symlink '%s': %s"),
998 path
, strerror(errno
));
1002 _("do not know what to do with %06o %s '%s'"),
1003 contents
->mode
, oid_to_hex(&contents
->oid
), path
);
1008 if (!ret
&& update_cache
) {
1009 int refresh
= (!opt
->priv
->call_depth
&&
1010 contents
->mode
!= S_IFGITLINK
);
1011 if (add_cacheinfo(opt
, contents
, path
, 0, refresh
,
1012 ADD_CACHE_OK_TO_ADD
))
1018 static int update_file(struct merge_options
*opt
,
1020 const struct diff_filespec
*contents
,
1023 return update_file_flags(opt
, contents
, path
,
1024 opt
->priv
->call_depth
|| clean
, !opt
->priv
->call_depth
);
1027 /* Low level file merging, update and removal */
1029 struct merge_file_info
{
1030 struct diff_filespec blob
; /* mostly use oid & mode; sometimes path */
1035 static int merge_3way(struct merge_options
*opt
,
1036 mmbuffer_t
*result_buf
,
1037 const struct diff_filespec
*o
,
1038 const struct diff_filespec
*a
,
1039 const struct diff_filespec
*b
,
1040 const char *branch1
,
1041 const char *branch2
,
1042 const int extra_marker_size
)
1044 mmfile_t orig
, src1
, src2
;
1045 struct ll_merge_options ll_opts
= {0};
1046 char *base
, *name1
, *name2
;
1049 ll_opts
.renormalize
= opt
->renormalize
;
1050 ll_opts
.extra_marker_size
= extra_marker_size
;
1051 ll_opts
.xdl_opts
= opt
->xdl_opts
;
1053 if (opt
->priv
->call_depth
) {
1054 ll_opts
.virtual_ancestor
= 1;
1055 ll_opts
.variant
= 0;
1057 switch (opt
->recursive_variant
) {
1058 case MERGE_VARIANT_OURS
:
1059 ll_opts
.variant
= XDL_MERGE_FAVOR_OURS
;
1061 case MERGE_VARIANT_THEIRS
:
1062 ll_opts
.variant
= XDL_MERGE_FAVOR_THEIRS
;
1065 ll_opts
.variant
= 0;
1070 assert(a
->path
&& b
->path
&& o
->path
&& opt
->ancestor
);
1071 if (strcmp(a
->path
, b
->path
) || strcmp(a
->path
, o
->path
) != 0) {
1072 base
= mkpathdup("%s:%s", opt
->ancestor
, o
->path
);
1073 name1
= mkpathdup("%s:%s", branch1
, a
->path
);
1074 name2
= mkpathdup("%s:%s", branch2
, b
->path
);
1076 base
= mkpathdup("%s", opt
->ancestor
);
1077 name1
= mkpathdup("%s", branch1
);
1078 name2
= mkpathdup("%s", branch2
);
1081 read_mmblob(&orig
, &o
->oid
);
1082 read_mmblob(&src1
, &a
->oid
);
1083 read_mmblob(&src2
, &b
->oid
);
1086 * FIXME: Using a->path for normalization rules in ll_merge could be
1087 * wrong if we renamed from a->path to b->path. We should use the
1088 * target path for where the file will be written.
1090 merge_status
= ll_merge(result_buf
, a
->path
, &orig
, base
,
1091 &src1
, name1
, &src2
, name2
,
1092 opt
->repo
->index
, &ll_opts
);
1100 return merge_status
;
1103 static int find_first_merges(struct repository
*repo
,
1104 struct object_array
*result
, const char *path
,
1105 struct commit
*a
, struct commit
*b
)
1108 struct object_array merges
= OBJECT_ARRAY_INIT
;
1109 struct commit
*commit
;
1110 int contains_another
;
1112 char merged_revision
[GIT_MAX_HEXSZ
+ 2];
1113 const char *rev_args
[] = { "rev-list", "--merges", "--ancestry-path",
1114 "--all", merged_revision
, NULL
};
1115 struct rev_info revs
;
1116 struct setup_revision_opt rev_opts
;
1118 memset(result
, 0, sizeof(struct object_array
));
1119 memset(&rev_opts
, 0, sizeof(rev_opts
));
1121 /* get all revisions that merge commit a */
1122 xsnprintf(merged_revision
, sizeof(merged_revision
), "^%s",
1123 oid_to_hex(&a
->object
.oid
));
1124 repo_init_revisions(repo
, &revs
, NULL
);
1125 /* FIXME: can't handle linked worktrees in submodules yet */
1126 revs
.single_worktree
= path
!= NULL
;
1127 setup_revisions(ARRAY_SIZE(rev_args
)-1, rev_args
, &revs
, &rev_opts
);
1129 /* save all revisions from the above list that contain b */
1130 if (prepare_revision_walk(&revs
))
1131 die("revision walk setup failed");
1132 while ((commit
= get_revision(&revs
)) != NULL
) {
1133 struct object
*o
= &(commit
->object
);
1134 if (repo_in_merge_bases(repo
, b
, commit
))
1135 add_object_array(o
, NULL
, &merges
);
1137 reset_revision_walk();
1139 /* Now we've got all merges that contain a and b. Prune all
1140 * merges that contain another found merge and save them in
1143 for (i
= 0; i
< merges
.nr
; i
++) {
1144 struct commit
*m1
= (struct commit
*) merges
.objects
[i
].item
;
1146 contains_another
= 0;
1147 for (j
= 0; j
< merges
.nr
; j
++) {
1148 struct commit
*m2
= (struct commit
*) merges
.objects
[j
].item
;
1149 if (i
!= j
&& repo_in_merge_bases(repo
, m2
, m1
)) {
1150 contains_another
= 1;
1155 if (!contains_another
)
1156 add_object_array(merges
.objects
[i
].item
, NULL
, result
);
1159 object_array_clear(&merges
);
1163 static void print_commit(struct repository
*repo
, struct commit
*commit
)
1165 struct strbuf sb
= STRBUF_INIT
;
1166 struct pretty_print_context ctx
= {0};
1167 ctx
.date_mode
.type
= DATE_NORMAL
;
1168 /* FIXME: Merge this with output_commit_title() */
1169 assert(!merge_remote_util(commit
));
1170 repo_format_commit_message(repo
, commit
, " %h: %m %s", &sb
, &ctx
);
1171 fprintf(stderr
, "%s\n", sb
.buf
);
1172 strbuf_release(&sb
);
1175 static int is_valid(const struct diff_filespec
*dfs
)
1177 return dfs
->mode
!= 0 && !is_null_oid(&dfs
->oid
);
1180 static int merge_submodule(struct merge_options
*opt
,
1181 struct object_id
*result
, const char *path
,
1182 const struct object_id
*base
, const struct object_id
*a
,
1183 const struct object_id
*b
)
1185 struct repository subrepo
;
1187 struct commit
*commit_base
, *commit_a
, *commit_b
;
1189 struct object_array merges
;
1192 int search
= !opt
->priv
->call_depth
;
1194 /* store a in result in case we fail */
1195 /* FIXME: This is the WRONG resolution for the recursive case when
1196 * we need to be careful to avoid accidentally matching either side.
1197 * Should probably use o instead there, much like we do for merging
1202 /* we can not handle deletion conflicts */
1203 if (is_null_oid(base
))
1210 if (repo_submodule_init(&subrepo
, opt
->repo
, path
, null_oid())) {
1211 output(opt
, 1, _("Failed to merge submodule %s (not checked out)"), path
);
1215 if (!(commit_base
= lookup_commit_reference(&subrepo
, base
)) ||
1216 !(commit_a
= lookup_commit_reference(&subrepo
, a
)) ||
1217 !(commit_b
= lookup_commit_reference(&subrepo
, b
))) {
1218 output(opt
, 1, _("Failed to merge submodule %s (commits not present)"), path
);
1222 /* check whether both changes are forward */
1223 if (!repo_in_merge_bases(&subrepo
, commit_base
, commit_a
) ||
1224 !repo_in_merge_bases(&subrepo
, commit_base
, commit_b
)) {
1225 output(opt
, 1, _("Failed to merge submodule %s (commits don't follow merge-base)"), path
);
1229 /* Case #1: a is contained in b or vice versa */
1230 if (repo_in_merge_bases(&subrepo
, commit_a
, commit_b
)) {
1233 output(opt
, 3, _("Fast-forwarding submodule %s to the following commit:"), path
);
1234 repo_output_commit_title(opt
, &subrepo
, commit_b
);
1235 } else if (show(opt
, 2))
1236 output(opt
, 2, _("Fast-forwarding submodule %s"), path
);
1243 if (repo_in_merge_bases(&subrepo
, commit_b
, commit_a
)) {
1246 output(opt
, 3, _("Fast-forwarding submodule %s to the following commit:"), path
);
1247 repo_output_commit_title(opt
, &subrepo
, commit_a
);
1248 } else if (show(opt
, 2))
1249 output(opt
, 2, _("Fast-forwarding submodule %s"), path
);
1258 * Case #2: There are one or more merges that contain a and b in
1259 * the submodule. If there is only one, then present it as a
1260 * suggestion to the user, but leave it marked unmerged so the
1261 * user needs to confirm the resolution.
1264 /* Skip the search if makes no sense to the calling context. */
1268 /* find commit which merges them */
1269 parent_count
= find_first_merges(&subrepo
, &merges
, path
,
1270 commit_a
, commit_b
);
1271 switch (parent_count
) {
1273 output(opt
, 1, _("Failed to merge submodule %s (merge following commits not found)"), path
);
1277 output(opt
, 1, _("Failed to merge submodule %s (not fast-forward)"), path
);
1278 output(opt
, 2, _("Found a possible merge resolution for the submodule:\n"));
1279 print_commit(&subrepo
, (struct commit
*) merges
.objects
[0].item
);
1281 "If this is correct simply add it to the index "
1284 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
1285 "which will accept this suggestion.\n"),
1286 oid_to_hex(&merges
.objects
[0].item
->oid
), path
);
1290 output(opt
, 1, _("Failed to merge submodule %s (multiple merges found)"), path
);
1291 for (i
= 0; i
< merges
.nr
; i
++)
1292 print_commit(&subrepo
, (struct commit
*) merges
.objects
[i
].item
);
1295 object_array_clear(&merges
);
1297 repo_clear(&subrepo
);
1301 static int merge_mode_and_contents(struct merge_options
*opt
,
1302 const struct diff_filespec
*o
,
1303 const struct diff_filespec
*a
,
1304 const struct diff_filespec
*b
,
1305 const char *filename
,
1306 const char *branch1
,
1307 const char *branch2
,
1308 const int extra_marker_size
,
1309 struct merge_file_info
*result
)
1311 if (opt
->branch1
!= branch1
) {
1313 * It's weird getting a reverse merge with HEAD on the bottom
1314 * side of the conflict markers and the other branch on the
1317 return merge_mode_and_contents(opt
, o
, b
, a
,
1320 extra_marker_size
, result
);
1326 if ((S_IFMT
& a
->mode
) != (S_IFMT
& b
->mode
)) {
1329 * FIXME: This is a bad resolution for recursive case; for
1330 * the recursive case we want something that is unlikely to
1331 * accidentally match either side. Also, while it makes
1332 * sense to prefer regular files over symlinks, it doesn't
1333 * make sense to prefer regular files over submodules.
1335 if (S_ISREG(a
->mode
)) {
1336 result
->blob
.mode
= a
->mode
;
1337 oidcpy(&result
->blob
.oid
, &a
->oid
);
1339 result
->blob
.mode
= b
->mode
;
1340 oidcpy(&result
->blob
.oid
, &b
->oid
);
1343 if (!oideq(&a
->oid
, &o
->oid
) && !oideq(&b
->oid
, &o
->oid
))
1349 if (a
->mode
== b
->mode
|| a
->mode
== o
->mode
)
1350 result
->blob
.mode
= b
->mode
;
1352 result
->blob
.mode
= a
->mode
;
1353 if (b
->mode
!= o
->mode
) {
1359 if (oideq(&a
->oid
, &b
->oid
) || oideq(&a
->oid
, &o
->oid
))
1360 oidcpy(&result
->blob
.oid
, &b
->oid
);
1361 else if (oideq(&b
->oid
, &o
->oid
))
1362 oidcpy(&result
->blob
.oid
, &a
->oid
);
1363 else if (S_ISREG(a
->mode
)) {
1364 mmbuffer_t result_buf
;
1365 int ret
= 0, merge_status
;
1367 merge_status
= merge_3way(opt
, &result_buf
, o
, a
, b
,
1371 if ((merge_status
< 0) || !result_buf
.ptr
)
1372 ret
= err(opt
, _("Failed to execute internal merge"));
1375 write_object_file(result_buf
.ptr
, result_buf
.size
,
1376 blob_type
, &result
->blob
.oid
))
1377 ret
= err(opt
, _("Unable to add %s to database"),
1380 free(result_buf
.ptr
);
1383 /* FIXME: bug, what if modes didn't match? */
1384 result
->clean
= (merge_status
== 0);
1385 } else if (S_ISGITLINK(a
->mode
)) {
1386 result
->clean
= merge_submodule(opt
, &result
->blob
.oid
,
1391 } else if (S_ISLNK(a
->mode
)) {
1392 switch (opt
->recursive_variant
) {
1393 case MERGE_VARIANT_NORMAL
:
1394 oidcpy(&result
->blob
.oid
, &a
->oid
);
1395 if (!oideq(&a
->oid
, &b
->oid
))
1398 case MERGE_VARIANT_OURS
:
1399 oidcpy(&result
->blob
.oid
, &a
->oid
);
1401 case MERGE_VARIANT_THEIRS
:
1402 oidcpy(&result
->blob
.oid
, &b
->oid
);
1406 BUG("unsupported object type in the tree");
1410 output(opt
, 2, _("Auto-merging %s"), filename
);
1415 static int handle_rename_via_dir(struct merge_options
*opt
,
1416 struct rename_conflict_info
*ci
)
1419 * Handle file adds that need to be renamed due to directory rename
1420 * detection. This differs from handle_rename_normal, because
1421 * there is no content merge to do; just move the file into the
1422 * desired final location.
1424 const struct rename
*ren
= ci
->ren1
;
1425 const struct diff_filespec
*dest
= ren
->pair
->two
;
1426 char *file_path
= dest
->path
;
1427 int mark_conflicted
= (opt
->detect_directory_renames
==
1428 MERGE_DIRECTORY_RENAMES_CONFLICT
);
1429 assert(ren
->dir_rename_original_dest
);
1431 if (!opt
->priv
->call_depth
&& would_lose_untracked(opt
, dest
->path
)) {
1432 mark_conflicted
= 1;
1433 file_path
= unique_path(opt
, dest
->path
, ren
->branch
);
1434 output(opt
, 1, _("Error: Refusing to lose untracked file at %s; "
1435 "writing to %s instead."),
1436 dest
->path
, file_path
);
1439 if (mark_conflicted
) {
1441 * Write the file in worktree at file_path. In the index,
1442 * only record the file at dest->path in the appropriate
1445 if (update_file(opt
, 0, dest
, file_path
))
1447 if (file_path
!= dest
->path
)
1449 if (update_stages(opt
, dest
->path
, NULL
,
1450 ren
->branch
== opt
->branch1
? dest
: NULL
,
1451 ren
->branch
== opt
->branch1
? NULL
: dest
))
1453 return 0; /* not clean, but conflicted */
1455 /* Update dest->path both in index and in worktree */
1456 if (update_file(opt
, 1, dest
, dest
->path
))
1458 return 1; /* clean */
1462 static int handle_change_delete(struct merge_options
*opt
,
1463 const char *path
, const char *old_path
,
1464 const struct diff_filespec
*o
,
1465 const struct diff_filespec
*changed
,
1466 const char *change_branch
,
1467 const char *delete_branch
,
1468 const char *change
, const char *change_past
)
1470 char *alt_path
= NULL
;
1471 const char *update_path
= path
;
1474 if (dir_in_way(opt
->repo
->index
, path
, !opt
->priv
->call_depth
, 0) ||
1475 (!opt
->priv
->call_depth
&& would_lose_untracked(opt
, path
))) {
1476 update_path
= alt_path
= unique_path(opt
, path
, change_branch
);
1479 if (opt
->priv
->call_depth
) {
1481 * We cannot arbitrarily accept either a_sha or b_sha as
1482 * correct; since there is no true "middle point" between
1483 * them, simply reuse the base version for virtual merge base.
1485 ret
= remove_file_from_index(opt
->repo
->index
, path
);
1487 ret
= update_file(opt
, 0, o
, update_path
);
1490 * Despite the four nearly duplicate messages and argument
1491 * lists below and the ugliness of the nested if-statements,
1492 * having complete messages makes the job easier for
1495 * The slight variance among the cases is due to the fact
1497 * 1) directory/file conflicts (in effect if
1498 * !alt_path) could cause us to need to write the
1499 * file to a different path.
1500 * 2) renames (in effect if !old_path) could mean that
1501 * there are two names for the path that the user
1502 * may know the file by.
1506 output(opt
, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1507 "and %s in %s. Version %s of %s left in tree."),
1508 change
, path
, delete_branch
, change_past
,
1509 change_branch
, change_branch
, path
);
1511 output(opt
, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1512 "and %s to %s in %s. Version %s of %s left in tree."),
1513 change
, old_path
, delete_branch
, change_past
, path
,
1514 change_branch
, change_branch
, path
);
1518 output(opt
, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1519 "and %s in %s. Version %s of %s left in tree at %s."),
1520 change
, path
, delete_branch
, change_past
,
1521 change_branch
, change_branch
, path
, alt_path
);
1523 output(opt
, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1524 "and %s to %s in %s. Version %s of %s left in tree at %s."),
1525 change
, old_path
, delete_branch
, change_past
, path
,
1526 change_branch
, change_branch
, path
, alt_path
);
1530 * No need to call update_file() on path when change_branch ==
1531 * opt->branch1 && !alt_path, since that would needlessly touch
1532 * path. We could call update_file_flags() with update_cache=0
1533 * and update_wd=0, but that's a no-op.
1535 if (change_branch
!= opt
->branch1
|| alt_path
)
1536 ret
= update_file(opt
, 0, changed
, update_path
);
1543 static int handle_rename_delete(struct merge_options
*opt
,
1544 struct rename_conflict_info
*ci
)
1546 const struct rename
*ren
= ci
->ren1
;
1547 const struct diff_filespec
*orig
= ren
->pair
->one
;
1548 const struct diff_filespec
*dest
= ren
->pair
->two
;
1549 const char *rename_branch
= ren
->branch
;
1550 const char *delete_branch
= (opt
->branch1
== ren
->branch
?
1551 opt
->branch2
: opt
->branch1
);
1553 if (handle_change_delete(opt
,
1554 opt
->priv
->call_depth
? orig
->path
: dest
->path
,
1555 opt
->priv
->call_depth
? NULL
: orig
->path
,
1557 rename_branch
, delete_branch
,
1558 _("rename"), _("renamed")))
1561 if (opt
->priv
->call_depth
)
1562 return remove_file_from_index(opt
->repo
->index
, dest
->path
);
1564 return update_stages(opt
, dest
->path
, NULL
,
1565 rename_branch
== opt
->branch1
? dest
: NULL
,
1566 rename_branch
== opt
->branch1
? NULL
: dest
);
1569 static int handle_file_collision(struct merge_options
*opt
,
1570 const char *collide_path
,
1571 const char *prev_path1
,
1572 const char *prev_path2
,
1573 const char *branch1
, const char *branch2
,
1574 struct diff_filespec
*a
,
1575 struct diff_filespec
*b
)
1577 struct merge_file_info mfi
;
1578 struct diff_filespec null
;
1579 char *alt_path
= NULL
;
1580 const char *update_path
= collide_path
;
1583 * It's easiest to get the correct things into stage 2 and 3, and
1584 * to make sure that the content merge puts HEAD before the other
1585 * branch if we just ensure that branch1 == opt->branch1. So, simply
1586 * flip arguments around if we don't have that.
1588 if (branch1
!= opt
->branch1
) {
1589 return handle_file_collision(opt
, collide_path
,
1590 prev_path2
, prev_path1
,
1595 /* Remove rename sources if rename/add or rename/rename(2to1) */
1597 remove_file(opt
, 1, prev_path1
,
1598 opt
->priv
->call_depth
|| would_lose_untracked(opt
, prev_path1
));
1600 remove_file(opt
, 1, prev_path2
,
1601 opt
->priv
->call_depth
|| would_lose_untracked(opt
, prev_path2
));
1604 * Remove the collision path, if it wouldn't cause dirty contents
1605 * or an untracked file to get lost. We'll either overwrite with
1606 * merged contents, or just write out to differently named files.
1608 if (was_dirty(opt
, collide_path
)) {
1609 output(opt
, 1, _("Refusing to lose dirty file at %s"),
1611 update_path
= alt_path
= unique_path(opt
, collide_path
, "merged");
1612 } else if (would_lose_untracked(opt
, collide_path
)) {
1614 * Only way we get here is if both renames were from
1615 * a directory rename AND user had an untracked file
1616 * at the location where both files end up after the
1617 * two directory renames. See testcase 10d of t6043.
1619 output(opt
, 1, _("Refusing to lose untracked file at "
1620 "%s, even though it's in the way."),
1622 update_path
= alt_path
= unique_path(opt
, collide_path
, "merged");
1625 * FIXME: It's possible that the two files are identical
1626 * and that the current working copy happens to match, in
1627 * which case we are unnecessarily touching the working
1628 * tree file. It's not a likely enough scenario that I
1629 * want to code up the checks for it and a better fix is
1630 * available if we restructure how unpack_trees() and
1631 * merge-recursive interoperate anyway, so punting for
1634 remove_file(opt
, 0, collide_path
, 0);
1637 /* Store things in diff_filespecs for functions that need it */
1638 null
.path
= (char *)collide_path
;
1639 oidcpy(&null
.oid
, null_oid());
1642 if (merge_mode_and_contents(opt
, &null
, a
, b
, collide_path
,
1643 branch1
, branch2
, opt
->priv
->call_depth
* 2, &mfi
))
1645 mfi
.clean
&= !alt_path
;
1646 if (update_file(opt
, mfi
.clean
, &mfi
.blob
, update_path
))
1648 if (!mfi
.clean
&& !opt
->priv
->call_depth
&&
1649 update_stages(opt
, collide_path
, NULL
, a
, b
))
1653 * FIXME: If both a & b both started with conflicts (only possible
1654 * if they came from a rename/rename(2to1)), but had IDENTICAL
1655 * contents including those conflicts, then in the next line we claim
1656 * it was clean. If someone cares about this case, we should have the
1657 * caller notify us if we started with conflicts.
1662 static int handle_rename_add(struct merge_options
*opt
,
1663 struct rename_conflict_info
*ci
)
1665 /* a was renamed to c, and a separate c was added. */
1666 struct diff_filespec
*a
= ci
->ren1
->pair
->one
;
1667 struct diff_filespec
*c
= ci
->ren1
->pair
->two
;
1668 char *path
= c
->path
;
1669 char *prev_path_desc
;
1670 struct merge_file_info mfi
;
1672 const char *rename_branch
= ci
->ren1
->branch
;
1673 const char *add_branch
= (opt
->branch1
== rename_branch
?
1674 opt
->branch2
: opt
->branch1
);
1675 int other_stage
= (ci
->ren1
->branch
== opt
->branch1
? 3 : 2);
1677 output(opt
, 1, _("CONFLICT (rename/add): "
1678 "Rename %s->%s in %s. Added %s in %s"),
1679 a
->path
, c
->path
, rename_branch
,
1680 c
->path
, add_branch
);
1682 prev_path_desc
= xstrfmt("version of %s from %s", path
, a
->path
);
1683 ci
->ren1
->src_entry
->stages
[other_stage
].path
= a
->path
;
1684 if (merge_mode_and_contents(opt
, a
, c
,
1685 &ci
->ren1
->src_entry
->stages
[other_stage
],
1687 opt
->branch1
, opt
->branch2
,
1688 1 + opt
->priv
->call_depth
* 2, &mfi
))
1690 free(prev_path_desc
);
1692 ci
->ren1
->dst_entry
->stages
[other_stage
].path
= mfi
.blob
.path
= c
->path
;
1693 return handle_file_collision(opt
,
1694 c
->path
, a
->path
, NULL
,
1695 rename_branch
, add_branch
,
1697 &ci
->ren1
->dst_entry
->stages
[other_stage
]);
1700 static char *find_path_for_conflict(struct merge_options
*opt
,
1702 const char *branch1
,
1703 const char *branch2
)
1705 char *new_path
= NULL
;
1706 if (dir_in_way(opt
->repo
->index
, path
, !opt
->priv
->call_depth
, 0)) {
1707 new_path
= unique_path(opt
, path
, branch1
);
1708 output(opt
, 1, _("%s is a directory in %s adding "
1710 path
, branch2
, new_path
);
1711 } else if (would_lose_untracked(opt
, path
)) {
1712 new_path
= unique_path(opt
, path
, branch1
);
1713 output(opt
, 1, _("Refusing to lose untracked file"
1714 " at %s; adding as %s instead"),
1722 * Toggle the stage number between "ours" and "theirs" (2 and 3).
1724 static inline int flip_stage(int stage
)
1726 return (2 + 3) - stage
;
1729 static int handle_rename_rename_1to2(struct merge_options
*opt
,
1730 struct rename_conflict_info
*ci
)
1732 /* One file was renamed in both branches, but to different names. */
1733 struct merge_file_info mfi
;
1734 struct diff_filespec
*add
;
1735 struct diff_filespec
*o
= ci
->ren1
->pair
->one
;
1736 struct diff_filespec
*a
= ci
->ren1
->pair
->two
;
1737 struct diff_filespec
*b
= ci
->ren2
->pair
->two
;
1740 output(opt
, 1, _("CONFLICT (rename/rename): "
1741 "Rename \"%s\"->\"%s\" in branch \"%s\" "
1742 "rename \"%s\"->\"%s\" in \"%s\"%s"),
1743 o
->path
, a
->path
, ci
->ren1
->branch
,
1744 o
->path
, b
->path
, ci
->ren2
->branch
,
1745 opt
->priv
->call_depth
? _(" (left unresolved)") : "");
1747 path_desc
= xstrfmt("%s and %s, both renamed from %s",
1748 a
->path
, b
->path
, o
->path
);
1749 if (merge_mode_and_contents(opt
, o
, a
, b
, path_desc
,
1750 ci
->ren1
->branch
, ci
->ren2
->branch
,
1751 opt
->priv
->call_depth
* 2, &mfi
))
1755 if (opt
->priv
->call_depth
)
1756 remove_file_from_index(opt
->repo
->index
, o
->path
);
1759 * For each destination path, we need to see if there is a
1760 * rename/add collision. If not, we can write the file out
1761 * to the specified location.
1763 add
= &ci
->ren1
->dst_entry
->stages
[flip_stage(2)];
1764 if (is_valid(add
)) {
1765 add
->path
= mfi
.blob
.path
= a
->path
;
1766 if (handle_file_collision(opt
, a
->path
,
1770 &mfi
.blob
, add
) < 0)
1773 char *new_path
= find_path_for_conflict(opt
, a
->path
,
1776 if (update_file(opt
, 0, &mfi
.blob
,
1777 new_path
? new_path
: a
->path
))
1780 if (!opt
->priv
->call_depth
&&
1781 update_stages(opt
, a
->path
, NULL
, a
, NULL
))
1785 if (!mfi
.clean
&& mfi
.blob
.mode
== a
->mode
&&
1786 oideq(&mfi
.blob
.oid
, &a
->oid
)) {
1788 * Getting here means we were attempting to merge a binary
1789 * blob. Since we can't merge binaries, the merge algorithm
1790 * just takes one side. But we don't want to copy the
1791 * contents of one side to both paths; we'd rather use the
1792 * original content at the given path for each path.
1794 oidcpy(&mfi
.blob
.oid
, &b
->oid
);
1795 mfi
.blob
.mode
= b
->mode
;
1797 add
= &ci
->ren2
->dst_entry
->stages
[flip_stage(3)];
1798 if (is_valid(add
)) {
1799 add
->path
= mfi
.blob
.path
= b
->path
;
1800 if (handle_file_collision(opt
, b
->path
,
1804 add
, &mfi
.blob
) < 0)
1807 char *new_path
= find_path_for_conflict(opt
, b
->path
,
1810 if (update_file(opt
, 0, &mfi
.blob
,
1811 new_path
? new_path
: b
->path
))
1814 if (!opt
->priv
->call_depth
&&
1815 update_stages(opt
, b
->path
, NULL
, NULL
, b
))
1822 static int handle_rename_rename_2to1(struct merge_options
*opt
,
1823 struct rename_conflict_info
*ci
)
1825 /* Two files, a & b, were renamed to the same thing, c. */
1826 struct diff_filespec
*a
= ci
->ren1
->pair
->one
;
1827 struct diff_filespec
*b
= ci
->ren2
->pair
->one
;
1828 struct diff_filespec
*c1
= ci
->ren1
->pair
->two
;
1829 struct diff_filespec
*c2
= ci
->ren2
->pair
->two
;
1830 char *path
= c1
->path
; /* == c2->path */
1831 char *path_side_1_desc
;
1832 char *path_side_2_desc
;
1833 struct merge_file_info mfi_c1
;
1834 struct merge_file_info mfi_c2
;
1835 int ostage1
, ostage2
;
1837 output(opt
, 1, _("CONFLICT (rename/rename): "
1838 "Rename %s->%s in %s. "
1839 "Rename %s->%s in %s"),
1840 a
->path
, c1
->path
, ci
->ren1
->branch
,
1841 b
->path
, c2
->path
, ci
->ren2
->branch
);
1843 path_side_1_desc
= xstrfmt("version of %s from %s", path
, a
->path
);
1844 path_side_2_desc
= xstrfmt("version of %s from %s", path
, b
->path
);
1845 ostage1
= ci
->ren1
->branch
== opt
->branch1
? 3 : 2;
1846 ostage2
= flip_stage(ostage1
);
1847 ci
->ren1
->src_entry
->stages
[ostage1
].path
= a
->path
;
1848 ci
->ren2
->src_entry
->stages
[ostage2
].path
= b
->path
;
1849 if (merge_mode_and_contents(opt
, a
, c1
,
1850 &ci
->ren1
->src_entry
->stages
[ostage1
],
1852 opt
->branch1
, opt
->branch2
,
1853 1 + opt
->priv
->call_depth
* 2, &mfi_c1
) ||
1854 merge_mode_and_contents(opt
, b
,
1855 &ci
->ren2
->src_entry
->stages
[ostage2
],
1856 c2
, path_side_2_desc
,
1857 opt
->branch1
, opt
->branch2
,
1858 1 + opt
->priv
->call_depth
* 2, &mfi_c2
))
1860 free(path_side_1_desc
);
1861 free(path_side_2_desc
);
1862 mfi_c1
.blob
.path
= path
;
1863 mfi_c2
.blob
.path
= path
;
1865 return handle_file_collision(opt
, path
, a
->path
, b
->path
,
1866 ci
->ren1
->branch
, ci
->ren2
->branch
,
1867 &mfi_c1
.blob
, &mfi_c2
.blob
);
1871 * Get the diff_filepairs changed between o_tree and tree.
1873 static struct diff_queue_struct
*get_diffpairs(struct merge_options
*opt
,
1874 struct tree
*o_tree
,
1877 struct diff_queue_struct
*ret
;
1878 struct diff_options opts
;
1880 repo_diff_setup(opt
->repo
, &opts
);
1881 opts
.flags
.recursive
= 1;
1882 opts
.flags
.rename_empty
= 0;
1883 opts
.detect_rename
= merge_detect_rename(opt
);
1885 * We do not have logic to handle the detection of copies. In
1886 * fact, it may not even make sense to add such logic: would we
1887 * really want a change to a base file to be propagated through
1888 * multiple other files by a merge?
1890 if (opts
.detect_rename
> DIFF_DETECT_RENAME
)
1891 opts
.detect_rename
= DIFF_DETECT_RENAME
;
1892 opts
.rename_limit
= (opt
->rename_limit
>= 0) ? opt
->rename_limit
: 7000;
1893 opts
.rename_score
= opt
->rename_score
;
1894 opts
.show_rename_progress
= opt
->show_rename_progress
;
1895 opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1896 diff_setup_done(&opts
);
1897 diff_tree_oid(&o_tree
->object
.oid
, &tree
->object
.oid
, "", &opts
);
1898 diffcore_std(&opts
);
1899 if (opts
.needed_rename_limit
> opt
->priv
->needed_rename_limit
)
1900 opt
->priv
->needed_rename_limit
= opts
.needed_rename_limit
;
1902 ret
= xmalloc(sizeof(*ret
));
1903 *ret
= diff_queued_diff
;
1905 opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1906 diff_queued_diff
.nr
= 0;
1907 diff_queued_diff
.queue
= NULL
;
1912 static int tree_has_path(struct repository
*r
, struct tree
*tree
,
1915 struct object_id hashy
;
1916 unsigned short mode_o
;
1918 return !get_tree_entry(r
,
1919 &tree
->object
.oid
, path
,
1924 * Return a new string that replaces the beginning portion (which matches
1925 * entry->dir), with entry->new_dir. In perl-speak:
1926 * new_path_name = (old_path =~ s/entry->dir/entry->new_dir/);
1928 * Caller must ensure that old_path starts with entry->dir + '/'.
1930 static char *apply_dir_rename(struct dir_rename_entry
*entry
,
1931 const char *old_path
)
1933 struct strbuf new_path
= STRBUF_INIT
;
1936 if (entry
->non_unique_new_dir
)
1939 oldlen
= strlen(entry
->dir
);
1940 if (entry
->new_dir
.len
== 0)
1942 * If someone renamed/merged a subdirectory into the root
1943 * directory (e.g. 'some/subdir' -> ''), then we want to
1946 * as the rename; we need to make old_path + oldlen advance
1947 * past the '/' character.
1950 newlen
= entry
->new_dir
.len
+ (strlen(old_path
) - oldlen
) + 1;
1951 strbuf_grow(&new_path
, newlen
);
1952 strbuf_addbuf(&new_path
, &entry
->new_dir
);
1953 strbuf_addstr(&new_path
, &old_path
[oldlen
]);
1955 return strbuf_detach(&new_path
, NULL
);
1958 static void get_renamed_dir_portion(const char *old_path
, const char *new_path
,
1959 char **old_dir
, char **new_dir
)
1961 char *end_of_old
, *end_of_new
;
1963 /* Default return values: NULL, meaning no rename */
1969 * "a/b/c/d/e/foo.c" -> "a/b/some/thing/else/e/foo.c"
1970 * the "e/foo.c" part is the same, we just want to know that
1971 * "a/b/c/d" was renamed to "a/b/some/thing/else"
1972 * so, for this example, this function returns "a/b/c/d" in
1973 * *old_dir and "a/b/some/thing/else" in *new_dir.
1977 * If the basename of the file changed, we don't care. We want
1978 * to know which portion of the directory, if any, changed.
1980 end_of_old
= strrchr(old_path
, '/');
1981 end_of_new
= strrchr(new_path
, '/');
1984 * If end_of_old is NULL, old_path wasn't in a directory, so there
1985 * could not be a directory rename (our rule elsewhere that a
1986 * directory which still exists is not considered to have been
1987 * renamed means the root directory can never be renamed -- because
1988 * the root directory always exists).
1990 if (end_of_old
== NULL
)
1991 return; /* Note: *old_dir and *new_dir are still NULL */
1994 * If new_path contains no directory (end_of_new is NULL), then we
1995 * have a rename of old_path's directory to the root directory.
1997 if (end_of_new
== NULL
) {
1998 *old_dir
= xstrndup(old_path
, end_of_old
- old_path
);
1999 *new_dir
= xstrdup("");
2003 /* Find the first non-matching character traversing backwards */
2004 while (*--end_of_new
== *--end_of_old
&&
2005 end_of_old
!= old_path
&&
2006 end_of_new
!= new_path
)
2007 ; /* Do nothing; all in the while loop */
2010 * If both got back to the beginning of their strings, then the
2011 * directory didn't change at all, only the basename did.
2013 if (end_of_old
== old_path
&& end_of_new
== new_path
&&
2014 *end_of_old
== *end_of_new
)
2015 return; /* Note: *old_dir and *new_dir are still NULL */
2018 * If end_of_new got back to the beginning of its string, and
2019 * end_of_old got back to the beginning of some subdirectory, then
2020 * we have a rename/merge of a subdirectory into the root, which
2021 * needs slightly special handling.
2023 * Note: There is no need to consider the opposite case, with a
2024 * rename/merge of the root directory into some subdirectory
2025 * because as noted above the root directory always exists so it
2026 * cannot be considered to be renamed.
2028 if (end_of_new
== new_path
&&
2029 end_of_old
!= old_path
&& end_of_old
[-1] == '/') {
2030 *old_dir
= xstrndup(old_path
, --end_of_old
- old_path
);
2031 *new_dir
= xstrdup("");
2036 * We've found the first non-matching character in the directory
2037 * paths. That means the current characters we were looking at
2038 * were part of the first non-matching subdir name going back from
2039 * the end of the strings. Get the whole name by advancing both
2040 * end_of_old and end_of_new to the NEXT '/' character. That will
2041 * represent the entire directory rename.
2043 * The reason for the increment is cases like
2044 * a/b/star/foo/whatever.c -> a/b/tar/foo/random.c
2045 * After dropping the basename and going back to the first
2046 * non-matching character, we're now comparing:
2048 * and we want to be comparing:
2049 * a/b/star/ and a/b/tar/
2050 * but without the pre-increment, the one on the right would stay
2053 end_of_old
= strchr(++end_of_old
, '/');
2054 end_of_new
= strchr(++end_of_new
, '/');
2056 /* Copy the old and new directories into *old_dir and *new_dir. */
2057 *old_dir
= xstrndup(old_path
, end_of_old
- old_path
);
2058 *new_dir
= xstrndup(new_path
, end_of_new
- new_path
);
2061 static void remove_hashmap_entries(struct hashmap
*dir_renames
,
2062 struct string_list
*items_to_remove
)
2065 struct dir_rename_entry
*entry
;
2067 for (i
= 0; i
< items_to_remove
->nr
; i
++) {
2068 entry
= items_to_remove
->items
[i
].util
;
2069 hashmap_remove(dir_renames
, &entry
->ent
, NULL
);
2071 string_list_clear(items_to_remove
, 0);
2075 * See if there is a directory rename for path, and if there are any file
2076 * level conflicts for the renamed location. If there is a rename and
2077 * there are no conflicts, return the new name. Otherwise, return NULL.
2079 static char *handle_path_level_conflicts(struct merge_options
*opt
,
2081 struct dir_rename_entry
*entry
,
2082 struct hashmap
*collisions
,
2085 char *new_path
= NULL
;
2086 struct collision_entry
*collision_ent
;
2088 struct strbuf collision_paths
= STRBUF_INIT
;
2091 * entry has the mapping of old directory name to new directory name
2092 * that we want to apply to path.
2094 new_path
= apply_dir_rename(entry
, path
);
2097 /* This should only happen when entry->non_unique_new_dir set */
2098 if (!entry
->non_unique_new_dir
)
2099 BUG("entry->non_unqiue_dir not set and !new_path");
2100 output(opt
, 1, _("CONFLICT (directory rename split): "
2101 "Unclear where to place %s because directory "
2102 "%s was renamed to multiple other directories, "
2103 "with no destination getting a majority of the "
2111 * The caller needs to have ensured that it has pre-populated
2112 * collisions with all paths that map to new_path. Do a quick check
2113 * to ensure that's the case.
2115 collision_ent
= collision_find_entry(collisions
, new_path
);
2116 if (collision_ent
== NULL
)
2117 BUG("collision_ent is NULL");
2120 * Check for one-sided add/add/.../add conflicts, i.e.
2121 * where implicit renames from the other side doing
2122 * directory rename(s) can affect this side of history
2123 * to put multiple paths into the same location. Warn
2124 * and bail on directory renames for such paths.
2126 if (collision_ent
->reported_already
) {
2128 } else if (tree_has_path(opt
->repo
, tree
, new_path
)) {
2129 collision_ent
->reported_already
= 1;
2130 strbuf_add_separated_string_list(&collision_paths
, ", ",
2131 &collision_ent
->source_files
);
2132 output(opt
, 1, _("CONFLICT (implicit dir rename): Existing "
2133 "file/dir at %s in the way of implicit "
2134 "directory rename(s) putting the following "
2135 "path(s) there: %s."),
2136 new_path
, collision_paths
.buf
);
2138 } else if (collision_ent
->source_files
.nr
> 1) {
2139 collision_ent
->reported_already
= 1;
2140 strbuf_add_separated_string_list(&collision_paths
, ", ",
2141 &collision_ent
->source_files
);
2142 output(opt
, 1, _("CONFLICT (implicit dir rename): Cannot map "
2143 "more than one path to %s; implicit directory "
2144 "renames tried to put these paths there: %s"),
2145 new_path
, collision_paths
.buf
);
2149 /* Free memory we no longer need */
2150 strbuf_release(&collision_paths
);
2151 if (!clean
&& new_path
) {
2160 * There are a couple things we want to do at the directory level:
2161 * 1. Check for both sides renaming to the same thing, in order to avoid
2162 * implicit renaming of files that should be left in place. (See
2163 * testcase 6b in t6043 for details.)
2164 * 2. Prune directory renames if there are still files left in the
2165 * original directory. These represent a partial directory rename,
2166 * i.e. a rename where only some of the files within the directory
2167 * were renamed elsewhere. (Technically, this could be done earlier
2168 * in get_directory_renames(), except that would prevent us from
2169 * doing the previous check and thus failing testcase 6b.)
2170 * 3. Check for rename/rename(1to2) conflicts (at the directory level).
2171 * In the future, we could potentially record this info as well and
2172 * omit reporting rename/rename(1to2) conflicts for each path within
2173 * the affected directories, thus cleaning up the merge output.
2174 * NOTE: We do NOT check for rename/rename(2to1) conflicts at the
2175 * directory level, because merging directories is fine. If it
2176 * causes conflicts for files within those merged directories, then
2177 * that should be detected at the individual path level.
2179 static void handle_directory_level_conflicts(struct merge_options
*opt
,
2180 struct hashmap
*dir_re_head
,
2182 struct hashmap
*dir_re_merge
,
2185 struct hashmap_iter iter
;
2186 struct dir_rename_entry
*head_ent
;
2187 struct dir_rename_entry
*merge_ent
;
2189 struct string_list remove_from_head
= STRING_LIST_INIT_NODUP
;
2190 struct string_list remove_from_merge
= STRING_LIST_INIT_NODUP
;
2192 hashmap_for_each_entry(dir_re_head
, &iter
, head_ent
,
2193 ent
/* member name */) {
2194 merge_ent
= dir_rename_find_entry(dir_re_merge
, head_ent
->dir
);
2196 !head_ent
->non_unique_new_dir
&&
2197 !merge_ent
->non_unique_new_dir
&&
2198 !strbuf_cmp(&head_ent
->new_dir
, &merge_ent
->new_dir
)) {
2199 /* 1. Renamed identically; remove it from both sides */
2200 string_list_append(&remove_from_head
,
2201 head_ent
->dir
)->util
= head_ent
;
2202 strbuf_release(&head_ent
->new_dir
);
2203 string_list_append(&remove_from_merge
,
2204 merge_ent
->dir
)->util
= merge_ent
;
2205 strbuf_release(&merge_ent
->new_dir
);
2206 } else if (tree_has_path(opt
->repo
, head
, head_ent
->dir
)) {
2207 /* 2. This wasn't a directory rename after all */
2208 string_list_append(&remove_from_head
,
2209 head_ent
->dir
)->util
= head_ent
;
2210 strbuf_release(&head_ent
->new_dir
);
2214 remove_hashmap_entries(dir_re_head
, &remove_from_head
);
2215 remove_hashmap_entries(dir_re_merge
, &remove_from_merge
);
2217 hashmap_for_each_entry(dir_re_merge
, &iter
, merge_ent
,
2218 ent
/* member name */) {
2219 head_ent
= dir_rename_find_entry(dir_re_head
, merge_ent
->dir
);
2220 if (tree_has_path(opt
->repo
, merge
, merge_ent
->dir
)) {
2221 /* 2. This wasn't a directory rename after all */
2222 string_list_append(&remove_from_merge
,
2223 merge_ent
->dir
)->util
= merge_ent
;
2224 } else if (head_ent
&&
2225 !head_ent
->non_unique_new_dir
&&
2226 !merge_ent
->non_unique_new_dir
) {
2227 /* 3. rename/rename(1to2) */
2229 * We can assume it's not rename/rename(1to1) because
2230 * that was case (1), already checked above. So we
2231 * know that head_ent->new_dir and merge_ent->new_dir
2232 * are different strings.
2234 output(opt
, 1, _("CONFLICT (rename/rename): "
2235 "Rename directory %s->%s in %s. "
2236 "Rename directory %s->%s in %s"),
2237 head_ent
->dir
, head_ent
->new_dir
.buf
, opt
->branch1
,
2238 head_ent
->dir
, merge_ent
->new_dir
.buf
, opt
->branch2
);
2239 string_list_append(&remove_from_head
,
2240 head_ent
->dir
)->util
= head_ent
;
2241 strbuf_release(&head_ent
->new_dir
);
2242 string_list_append(&remove_from_merge
,
2243 merge_ent
->dir
)->util
= merge_ent
;
2244 strbuf_release(&merge_ent
->new_dir
);
2248 remove_hashmap_entries(dir_re_head
, &remove_from_head
);
2249 remove_hashmap_entries(dir_re_merge
, &remove_from_merge
);
2252 static struct hashmap
*get_directory_renames(struct diff_queue_struct
*pairs
)
2254 struct hashmap
*dir_renames
;
2255 struct hashmap_iter iter
;
2256 struct dir_rename_entry
*entry
;
2260 * Typically, we think of a directory rename as all files from a
2261 * certain directory being moved to a target directory. However,
2262 * what if someone first moved two files from the original
2263 * directory in one commit, and then renamed the directory
2264 * somewhere else in a later commit? At merge time, we just know
2265 * that files from the original directory went to two different
2266 * places, and that the bulk of them ended up in the same place.
2267 * We want each directory rename to represent where the bulk of the
2268 * files from that directory end up; this function exists to find
2269 * where the bulk of the files went.
2271 * The first loop below simply iterates through the list of file
2272 * renames, finding out how often each directory rename pair
2273 * possibility occurs.
2275 dir_renames
= xmalloc(sizeof(*dir_renames
));
2276 dir_rename_init(dir_renames
);
2277 for (i
= 0; i
< pairs
->nr
; ++i
) {
2278 struct string_list_item
*item
;
2280 struct diff_filepair
*pair
= pairs
->queue
[i
];
2281 char *old_dir
, *new_dir
;
2283 /* File not part of directory rename if it wasn't renamed */
2284 if (pair
->status
!= 'R')
2287 get_renamed_dir_portion(pair
->one
->path
, pair
->two
->path
,
2288 &old_dir
, &new_dir
);
2290 /* Directory didn't change at all; ignore this one. */
2293 entry
= dir_rename_find_entry(dir_renames
, old_dir
);
2295 entry
= xmalloc(sizeof(*entry
));
2296 dir_rename_entry_init(entry
, old_dir
);
2297 hashmap_put(dir_renames
, &entry
->ent
);
2301 item
= string_list_lookup(&entry
->possible_new_dirs
, new_dir
);
2303 item
= string_list_insert(&entry
->possible_new_dirs
,
2305 item
->util
= xcalloc(1, sizeof(int));
2314 * For each directory with files moved out of it, we find out which
2315 * target directory received the most files so we can declare it to
2316 * be the "winning" target location for the directory rename. This
2317 * winner gets recorded in new_dir. If there is no winner
2318 * (multiple target directories received the same number of files),
2319 * we set non_unique_new_dir. Once we've determined the winner (or
2320 * that there is no winner), we no longer need possible_new_dirs.
2322 hashmap_for_each_entry(dir_renames
, &iter
, entry
,
2323 ent
/* member name */) {
2328 for (i
= 0; i
< entry
->possible_new_dirs
.nr
; i
++) {
2329 int *count
= entry
->possible_new_dirs
.items
[i
].util
;
2333 else if (*count
> max
) {
2335 best
= entry
->possible_new_dirs
.items
[i
].string
;
2339 entry
->non_unique_new_dir
= 1;
2341 assert(entry
->new_dir
.len
== 0);
2342 strbuf_addstr(&entry
->new_dir
, best
);
2345 * The relevant directory sub-portion of the original full
2346 * filepaths were xstrndup'ed before inserting into
2347 * possible_new_dirs, and instead of manually iterating the
2348 * list and free'ing each, just lie and tell
2349 * possible_new_dirs that it did the strdup'ing so that it
2350 * will free them for us.
2352 entry
->possible_new_dirs
.strdup_strings
= 1;
2353 string_list_clear(&entry
->possible_new_dirs
, 1);
2359 static struct dir_rename_entry
*check_dir_renamed(const char *path
,
2360 struct hashmap
*dir_renames
)
2362 char *temp
= xstrdup(path
);
2364 struct dir_rename_entry
*entry
= NULL
;
2366 while ((end
= strrchr(temp
, '/'))) {
2368 entry
= dir_rename_find_entry(dir_renames
, temp
);
2376 static void compute_collisions(struct hashmap
*collisions
,
2377 struct hashmap
*dir_renames
,
2378 struct diff_queue_struct
*pairs
)
2383 * Multiple files can be mapped to the same path due to directory
2384 * renames done by the other side of history. Since that other
2385 * side of history could have merged multiple directories into one,
2386 * if our side of history added the same file basename to each of
2387 * those directories, then all N of them would get implicitly
2388 * renamed by the directory rename detection into the same path,
2389 * and we'd get an add/add/.../add conflict, and all those adds
2390 * from *this* side of history. This is not representable in the
2391 * index, and users aren't going to easily be able to make sense of
2392 * it. So we need to provide a good warning about what's
2393 * happening, and fall back to no-directory-rename detection
2394 * behavior for those paths.
2396 * See testcases 9e and all of section 5 from t6043 for examples.
2398 collision_init(collisions
);
2400 for (i
= 0; i
< pairs
->nr
; ++i
) {
2401 struct dir_rename_entry
*dir_rename_ent
;
2402 struct collision_entry
*collision_ent
;
2404 struct diff_filepair
*pair
= pairs
->queue
[i
];
2406 if (pair
->status
!= 'A' && pair
->status
!= 'R')
2408 dir_rename_ent
= check_dir_renamed(pair
->two
->path
,
2410 if (!dir_rename_ent
)
2413 new_path
= apply_dir_rename(dir_rename_ent
, pair
->two
->path
);
2416 * dir_rename_ent->non_unique_new_path is true, which
2417 * means there is no directory rename for us to use,
2418 * which means it won't cause us any additional
2422 collision_ent
= collision_find_entry(collisions
, new_path
);
2423 if (!collision_ent
) {
2424 CALLOC_ARRAY(collision_ent
, 1);
2425 hashmap_entry_init(&collision_ent
->ent
,
2427 hashmap_put(collisions
, &collision_ent
->ent
);
2428 collision_ent
->target_file
= new_path
;
2432 string_list_insert(&collision_ent
->source_files
,
2437 static char *check_for_directory_rename(struct merge_options
*opt
,
2440 struct hashmap
*dir_renames
,
2441 struct hashmap
*dir_rename_exclusions
,
2442 struct hashmap
*collisions
,
2445 char *new_path
= NULL
;
2446 struct dir_rename_entry
*entry
= check_dir_renamed(path
, dir_renames
);
2447 struct dir_rename_entry
*oentry
= NULL
;
2453 * This next part is a little weird. We do not want to do an
2454 * implicit rename into a directory we renamed on our side, because
2455 * that will result in a spurious rename/rename(1to2) conflict. An
2457 * Base commit: dumbdir/afile, otherdir/bfile
2458 * Side 1: smrtdir/afile, otherdir/bfile
2459 * Side 2: dumbdir/afile, dumbdir/bfile
2460 * Here, while working on Side 1, we could notice that otherdir was
2461 * renamed/merged to dumbdir, and change the diff_filepair for
2462 * otherdir/bfile into a rename into dumbdir/bfile. However, Side
2463 * 2 will notice the rename from dumbdir to smrtdir, and do the
2464 * transitive rename to move it from dumbdir/bfile to
2465 * smrtdir/bfile. That gives us bfile in dumbdir vs being in
2466 * smrtdir, a rename/rename(1to2) conflict. We really just want
2467 * the file to end up in smrtdir. And the way to achieve that is
2468 * to not let Side1 do the rename to dumbdir, since we know that is
2469 * the source of one of our directory renames.
2471 * That's why oentry and dir_rename_exclusions is here.
2473 * As it turns out, this also prevents N-way transient rename
2474 * confusion; See testcases 9c and 9d of t6043.
2476 oentry
= dir_rename_find_entry(dir_rename_exclusions
, entry
->new_dir
.buf
);
2478 output(opt
, 1, _("WARNING: Avoiding applying %s -> %s rename "
2479 "to %s, because %s itself was renamed."),
2480 entry
->dir
, entry
->new_dir
.buf
, path
, entry
->new_dir
.buf
);
2482 new_path
= handle_path_level_conflicts(opt
, path
, entry
,
2484 *clean_merge
&= (new_path
!= NULL
);
2490 static void apply_directory_rename_modifications(struct merge_options
*opt
,
2491 struct diff_filepair
*pair
,
2495 struct tree
*o_tree
,
2496 struct tree
*a_tree
,
2497 struct tree
*b_tree
,
2498 struct string_list
*entries
)
2500 struct string_list_item
*item
;
2501 int stage
= (tree
== a_tree
? 2 : 3);
2505 * In all cases where we can do directory rename detection,
2506 * unpack_trees() will have read pair->two->path into the
2507 * index and the working copy. We need to remove it so that
2508 * we can instead place it at new_path. It is guaranteed to
2509 * not be untracked (unpack_trees() would have errored out
2510 * saying the file would have been overwritten), but it might
2513 update_wd
= !was_dirty(opt
, pair
->two
->path
);
2515 output(opt
, 1, _("Refusing to lose dirty file at %s"),
2517 remove_file(opt
, 1, pair
->two
->path
, !update_wd
);
2519 /* Find or create a new re->dst_entry */
2520 item
= string_list_lookup(entries
, new_path
);
2523 * Since we're renaming on this side of history, and it's
2524 * due to a directory rename on the other side of history
2525 * (which we only allow when the directory in question no
2526 * longer exists on the other side of history), the
2527 * original entry for re->dst_entry is no longer
2530 re
->dst_entry
->processed
= 1;
2533 * ...because we'll be using this new one.
2535 re
->dst_entry
= item
->util
;
2538 * re->dst_entry is for the before-dir-rename path, and we
2539 * need it to hold information for the after-dir-rename
2540 * path. Before creating a new entry, we need to mark the
2541 * old one as unnecessary (...unless it is shared by
2542 * src_entry, i.e. this didn't use to be a rename, in which
2543 * case we can just allow the normal processing to happen
2546 if (pair
->status
== 'R')
2547 re
->dst_entry
->processed
= 1;
2549 re
->dst_entry
= insert_stage_data(opt
->repo
, new_path
,
2550 o_tree
, a_tree
, b_tree
,
2552 item
= string_list_insert(entries
, new_path
);
2553 item
->util
= re
->dst_entry
;
2557 * Update the stage_data with the information about the path we are
2558 * moving into place. That slot will be empty and available for us
2559 * to write to because of the collision checks in
2560 * handle_path_level_conflicts(). In other words,
2561 * re->dst_entry->stages[stage].oid will be the null_oid, so it's
2562 * open for us to write to.
2564 * It may be tempting to actually update the index at this point as
2565 * well, using update_stages_for_stage_data(), but as per the big
2566 * "NOTE" in update_stages(), doing so will modify the current
2567 * in-memory index which will break calls to would_lose_untracked()
2568 * that we need to make. Instead, we need to just make sure that
2569 * the various handle_rename_*() functions update the index
2570 * explicitly rather than relying on unpack_trees() to have done it.
2572 get_tree_entry(opt
->repo
,
2575 &re
->dst_entry
->stages
[stage
].oid
,
2576 &re
->dst_entry
->stages
[stage
].mode
);
2579 * Record the original change status (or 'type' of change). If it
2580 * was originally an add ('A'), this lets us differentiate later
2581 * between a RENAME_DELETE conflict and RENAME_VIA_DIR (they
2582 * otherwise look the same). If it was originally a rename ('R'),
2583 * this lets us remember and report accurately about the transitive
2584 * renaming that occurred via the directory rename detection. Also,
2585 * record the original destination name.
2587 re
->dir_rename_original_type
= pair
->status
;
2588 re
->dir_rename_original_dest
= pair
->two
->path
;
2591 * We don't actually look at pair->status again, but it seems
2592 * pedagogically correct to adjust it.
2597 * Finally, record the new location.
2599 pair
->two
->path
= new_path
;
2603 * Get information of all renames which occurred in 'pairs', making use of
2604 * any implicit directory renames inferred from the other side of history.
2605 * We need the three trees in the merge ('o_tree', 'a_tree' and 'b_tree')
2606 * to be able to associate the correct cache entries with the rename
2607 * information; tree is always equal to either a_tree or b_tree.
2609 static struct string_list
*get_renames(struct merge_options
*opt
,
2611 struct diff_queue_struct
*pairs
,
2612 struct hashmap
*dir_renames
,
2613 struct hashmap
*dir_rename_exclusions
,
2615 struct tree
*o_tree
,
2616 struct tree
*a_tree
,
2617 struct tree
*b_tree
,
2618 struct string_list
*entries
,
2622 struct hashmap collisions
;
2623 struct hashmap_iter iter
;
2624 struct collision_entry
*e
;
2625 struct string_list
*renames
;
2627 compute_collisions(&collisions
, dir_renames
, pairs
);
2628 CALLOC_ARRAY(renames
, 1);
2630 for (i
= 0; i
< pairs
->nr
; ++i
) {
2631 struct string_list_item
*item
;
2633 struct diff_filepair
*pair
= pairs
->queue
[i
];
2634 char *new_path
; /* non-NULL only with directory renames */
2636 if (pair
->status
!= 'A' && pair
->status
!= 'R') {
2637 diff_free_filepair(pair
);
2640 new_path
= check_for_directory_rename(opt
, pair
->two
->path
, tree
,
2642 dir_rename_exclusions
,
2645 if (pair
->status
!= 'R' && !new_path
) {
2646 diff_free_filepair(pair
);
2650 re
= xmalloc(sizeof(*re
));
2653 re
->branch
= branch
;
2654 re
->dir_rename_original_type
= '\0';
2655 re
->dir_rename_original_dest
= NULL
;
2656 item
= string_list_lookup(entries
, re
->pair
->one
->path
);
2658 re
->src_entry
= insert_stage_data(opt
->repo
,
2659 re
->pair
->one
->path
,
2660 o_tree
, a_tree
, b_tree
, entries
);
2662 re
->src_entry
= item
->util
;
2664 item
= string_list_lookup(entries
, re
->pair
->two
->path
);
2666 re
->dst_entry
= insert_stage_data(opt
->repo
,
2667 re
->pair
->two
->path
,
2668 o_tree
, a_tree
, b_tree
, entries
);
2670 re
->dst_entry
= item
->util
;
2671 item
= string_list_insert(renames
, pair
->one
->path
);
2674 apply_directory_rename_modifications(opt
, pair
, new_path
,
2680 hashmap_for_each_entry(&collisions
, &iter
, e
,
2681 ent
/* member name */) {
2682 free(e
->target_file
);
2683 string_list_clear(&e
->source_files
, 0);
2685 hashmap_clear_and_free(&collisions
, struct collision_entry
, ent
);
2689 static int process_renames(struct merge_options
*opt
,
2690 struct string_list
*a_renames
,
2691 struct string_list
*b_renames
)
2693 int clean_merge
= 1, i
, j
;
2694 struct string_list a_by_dst
= STRING_LIST_INIT_NODUP
;
2695 struct string_list b_by_dst
= STRING_LIST_INIT_NODUP
;
2696 const struct rename
*sre
;
2699 * FIXME: As string-list.h notes, it's O(n^2) to build a sorted
2700 * string_list one-by-one, but O(n log n) to build it unsorted and
2701 * then sort it. Note that as we build the list, we do not need to
2702 * check if the existing destination path is already in the list,
2703 * because the structure of diffcore_rename guarantees we won't
2706 for (i
= 0; i
< a_renames
->nr
; i
++) {
2707 sre
= a_renames
->items
[i
].util
;
2708 string_list_insert(&a_by_dst
, sre
->pair
->two
->path
)->util
2711 for (i
= 0; i
< b_renames
->nr
; i
++) {
2712 sre
= b_renames
->items
[i
].util
;
2713 string_list_insert(&b_by_dst
, sre
->pair
->two
->path
)->util
2717 for (i
= 0, j
= 0; i
< a_renames
->nr
|| j
< b_renames
->nr
;) {
2718 struct string_list
*renames1
, *renames2Dst
;
2719 struct rename
*ren1
= NULL
, *ren2
= NULL
;
2720 const char *ren1_src
, *ren1_dst
;
2721 struct string_list_item
*lookup
;
2723 if (i
>= a_renames
->nr
) {
2724 ren2
= b_renames
->items
[j
++].util
;
2725 } else if (j
>= b_renames
->nr
) {
2726 ren1
= a_renames
->items
[i
++].util
;
2728 int compare
= strcmp(a_renames
->items
[i
].string
,
2729 b_renames
->items
[j
].string
);
2731 ren1
= a_renames
->items
[i
++].util
;
2733 ren2
= b_renames
->items
[j
++].util
;
2736 /* TODO: refactor, so that 1/2 are not needed */
2738 renames1
= a_renames
;
2739 renames2Dst
= &b_by_dst
;
2741 renames1
= b_renames
;
2742 renames2Dst
= &a_by_dst
;
2746 if (ren1
->processed
)
2748 ren1
->processed
= 1;
2749 ren1
->dst_entry
->processed
= 1;
2750 /* BUG: We should only mark src_entry as processed if we
2751 * are not dealing with a rename + add-source case.
2753 ren1
->src_entry
->processed
= 1;
2755 ren1_src
= ren1
->pair
->one
->path
;
2756 ren1_dst
= ren1
->pair
->two
->path
;
2759 /* One file renamed on both sides */
2760 const char *ren2_src
= ren2
->pair
->one
->path
;
2761 const char *ren2_dst
= ren2
->pair
->two
->path
;
2762 enum rename_type rename_type
;
2763 if (strcmp(ren1_src
, ren2_src
) != 0)
2764 BUG("ren1_src != ren2_src");
2765 ren2
->dst_entry
->processed
= 1;
2766 ren2
->processed
= 1;
2767 if (strcmp(ren1_dst
, ren2_dst
) != 0) {
2768 rename_type
= RENAME_ONE_FILE_TO_TWO
;
2771 rename_type
= RENAME_ONE_FILE_TO_ONE
;
2772 /* BUG: We should only remove ren1_src in
2773 * the base stage (think of rename +
2774 * add-source cases).
2776 remove_file(opt
, 1, ren1_src
, 1);
2777 update_entry(ren1
->dst_entry
,
2782 setup_rename_conflict_info(rename_type
, opt
, ren1
, ren2
);
2783 } else if ((lookup
= string_list_lookup(renames2Dst
, ren1_dst
))) {
2784 /* Two different files renamed to the same thing */
2786 ren2
= lookup
->util
;
2787 ren2_dst
= ren2
->pair
->two
->path
;
2788 if (strcmp(ren1_dst
, ren2_dst
) != 0)
2789 BUG("ren1_dst != ren2_dst");
2792 ren2
->processed
= 1;
2794 * BUG: We should only mark src_entry as processed
2795 * if we are not dealing with a rename + add-source
2798 ren2
->src_entry
->processed
= 1;
2800 setup_rename_conflict_info(RENAME_TWO_FILES_TO_ONE
,
2803 /* Renamed in 1, maybe changed in 2 */
2804 /* we only use sha1 and mode of these */
2805 struct diff_filespec src_other
, dst_other
;
2809 * unpack_trees loads entries from common-commit
2810 * into stage 1, from head-commit into stage 2, and
2811 * from merge-commit into stage 3. We keep track
2812 * of which side corresponds to the rename.
2814 int renamed_stage
= a_renames
== renames1
? 2 : 3;
2815 int other_stage
= a_renames
== renames1
? 3 : 2;
2818 * Directory renames have a funny corner case...
2820 int renamed_to_self
= !strcmp(ren1_src
, ren1_dst
);
2822 /* BUG: We should only remove ren1_src in the base
2823 * stage and in other_stage (think of rename +
2826 if (!renamed_to_self
)
2827 remove_file(opt
, 1, ren1_src
,
2828 renamed_stage
== 2 ||
2829 !was_tracked(opt
, ren1_src
));
2831 oidcpy(&src_other
.oid
,
2832 &ren1
->src_entry
->stages
[other_stage
].oid
);
2833 src_other
.mode
= ren1
->src_entry
->stages
[other_stage
].mode
;
2834 oidcpy(&dst_other
.oid
,
2835 &ren1
->dst_entry
->stages
[other_stage
].oid
);
2836 dst_other
.mode
= ren1
->dst_entry
->stages
[other_stage
].mode
;
2839 if (oideq(&src_other
.oid
, null_oid()) &&
2840 ren1
->dir_rename_original_type
== 'A') {
2841 setup_rename_conflict_info(RENAME_VIA_DIR
,
2843 } else if (renamed_to_self
) {
2844 setup_rename_conflict_info(RENAME_NORMAL
,
2846 } else if (oideq(&src_other
.oid
, null_oid())) {
2847 setup_rename_conflict_info(RENAME_DELETE
,
2849 } else if ((dst_other
.mode
== ren1
->pair
->two
->mode
) &&
2850 oideq(&dst_other
.oid
, &ren1
->pair
->two
->oid
)) {
2852 * Added file on the other side identical to
2853 * the file being renamed: clean merge.
2854 * Also, there is no need to overwrite the
2855 * file already in the working copy, so call
2856 * update_file_flags() instead of
2859 if (update_file_flags(opt
,
2862 1, /* update_cache */
2865 } else if (!oideq(&dst_other
.oid
, null_oid())) {
2867 * Probably not a clean merge, but it's
2868 * premature to set clean_merge to 0 here,
2869 * because if the rename merges cleanly and
2870 * the merge exactly matches the newly added
2871 * file, then the merge will be clean.
2873 setup_rename_conflict_info(RENAME_ADD
,
2878 if (clean_merge
< 0)
2879 goto cleanup_and_return
;
2881 struct diff_filespec
*o
, *a
, *b
;
2882 src_other
.path
= (char *)ren1_src
;
2884 o
= ren1
->pair
->one
;
2885 if (a_renames
== renames1
) {
2886 a
= ren1
->pair
->two
;
2889 b
= ren1
->pair
->two
;
2892 update_entry(ren1
->dst_entry
, o
, a
, b
);
2893 setup_rename_conflict_info(RENAME_NORMAL
,
2899 string_list_clear(&a_by_dst
, 0);
2900 string_list_clear(&b_by_dst
, 0);
2905 struct rename_info
{
2906 struct string_list
*head_renames
;
2907 struct string_list
*merge_renames
;
2910 static void initial_cleanup_rename(struct diff_queue_struct
*pairs
,
2911 struct hashmap
*dir_renames
)
2913 struct hashmap_iter iter
;
2914 struct dir_rename_entry
*e
;
2916 hashmap_for_each_entry(dir_renames
, &iter
, e
,
2917 ent
/* member name */) {
2919 strbuf_release(&e
->new_dir
);
2920 /* possible_new_dirs already cleared in get_directory_renames */
2922 hashmap_clear_and_free(dir_renames
, struct dir_rename_entry
, ent
);
2929 static int detect_and_process_renames(struct merge_options
*opt
,
2930 struct tree
*common
,
2933 struct string_list
*entries
,
2934 struct rename_info
*ri
)
2936 struct diff_queue_struct
*head_pairs
, *merge_pairs
;
2937 struct hashmap
*dir_re_head
, *dir_re_merge
;
2940 ri
->head_renames
= NULL
;
2941 ri
->merge_renames
= NULL
;
2943 if (!merge_detect_rename(opt
))
2946 head_pairs
= get_diffpairs(opt
, common
, head
);
2947 merge_pairs
= get_diffpairs(opt
, common
, merge
);
2949 if ((opt
->detect_directory_renames
== MERGE_DIRECTORY_RENAMES_TRUE
) ||
2950 (opt
->detect_directory_renames
== MERGE_DIRECTORY_RENAMES_CONFLICT
&&
2951 !opt
->priv
->call_depth
)) {
2952 dir_re_head
= get_directory_renames(head_pairs
);
2953 dir_re_merge
= get_directory_renames(merge_pairs
);
2955 handle_directory_level_conflicts(opt
,
2957 dir_re_merge
, merge
);
2959 dir_re_head
= xmalloc(sizeof(*dir_re_head
));
2960 dir_re_merge
= xmalloc(sizeof(*dir_re_merge
));
2961 dir_rename_init(dir_re_head
);
2962 dir_rename_init(dir_re_merge
);
2965 ri
->head_renames
= get_renames(opt
, opt
->branch1
, head_pairs
,
2966 dir_re_merge
, dir_re_head
, head
,
2967 common
, head
, merge
, entries
,
2971 ri
->merge_renames
= get_renames(opt
, opt
->branch2
, merge_pairs
,
2972 dir_re_head
, dir_re_merge
, merge
,
2973 common
, head
, merge
, entries
,
2977 clean
&= process_renames(opt
, ri
->head_renames
, ri
->merge_renames
);
2981 * Some cleanup is deferred until cleanup_renames() because the
2982 * data structures are still needed and referenced in
2983 * process_entry(). But there are a few things we can free now.
2985 initial_cleanup_rename(head_pairs
, dir_re_head
);
2986 initial_cleanup_rename(merge_pairs
, dir_re_merge
);
2991 static void final_cleanup_rename(struct string_list
*rename
)
2993 const struct rename
*re
;
2999 for (i
= 0; i
< rename
->nr
; i
++) {
3000 re
= rename
->items
[i
].util
;
3001 diff_free_filepair(re
->pair
);
3003 string_list_clear(rename
, 1);
3007 static void final_cleanup_renames(struct rename_info
*re_info
)
3009 final_cleanup_rename(re_info
->head_renames
);
3010 final_cleanup_rename(re_info
->merge_renames
);
3013 static int read_oid_strbuf(struct merge_options
*opt
,
3014 const struct object_id
*oid
,
3018 enum object_type type
;
3020 buf
= read_object_file(oid
, &type
, &size
);
3022 return err(opt
, _("cannot read object %s"), oid_to_hex(oid
));
3023 if (type
!= OBJ_BLOB
) {
3025 return err(opt
, _("object %s is not a blob"), oid_to_hex(oid
));
3027 strbuf_attach(dst
, buf
, size
, size
+ 1);
3031 static int blob_unchanged(struct merge_options
*opt
,
3032 const struct diff_filespec
*o
,
3033 const struct diff_filespec
*a
,
3034 int renormalize
, const char *path
)
3036 struct strbuf obuf
= STRBUF_INIT
;
3037 struct strbuf abuf
= STRBUF_INIT
;
3038 int ret
= 0; /* assume changed for safety */
3039 struct index_state
*idx
= opt
->repo
->index
;
3041 if (a
->mode
!= o
->mode
)
3043 if (oideq(&o
->oid
, &a
->oid
))
3048 if (read_oid_strbuf(opt
, &o
->oid
, &obuf
) ||
3049 read_oid_strbuf(opt
, &a
->oid
, &abuf
))
3052 * Note: binary | is used so that both renormalizations are
3053 * performed. Comparison can be skipped if both files are
3054 * unchanged since their sha1s have already been compared.
3056 if (renormalize_buffer(idx
, path
, obuf
.buf
, obuf
.len
, &obuf
) |
3057 renormalize_buffer(idx
, path
, abuf
.buf
, abuf
.len
, &abuf
))
3058 ret
= (obuf
.len
== abuf
.len
&& !memcmp(obuf
.buf
, abuf
.buf
, obuf
.len
));
3061 strbuf_release(&obuf
);
3062 strbuf_release(&abuf
);
3066 static int handle_modify_delete(struct merge_options
*opt
,
3068 const struct diff_filespec
*o
,
3069 const struct diff_filespec
*a
,
3070 const struct diff_filespec
*b
)
3072 const char *modify_branch
, *delete_branch
;
3073 const struct diff_filespec
*changed
;
3076 modify_branch
= opt
->branch1
;
3077 delete_branch
= opt
->branch2
;
3080 modify_branch
= opt
->branch2
;
3081 delete_branch
= opt
->branch1
;
3085 return handle_change_delete(opt
,
3088 modify_branch
, delete_branch
,
3089 _("modify"), _("modified"));
3092 static int handle_content_merge(struct merge_file_info
*mfi
,
3093 struct merge_options
*opt
,
3096 const struct diff_filespec
*o
,
3097 const struct diff_filespec
*a
,
3098 const struct diff_filespec
*b
,
3099 struct rename_conflict_info
*ci
)
3101 const char *reason
= _("content");
3102 unsigned df_conflict_remains
= 0;
3105 reason
= _("add/add");
3107 assert(o
->path
&& a
->path
&& b
->path
);
3108 if (ci
&& dir_in_way(opt
->repo
->index
, path
, !opt
->priv
->call_depth
,
3109 S_ISGITLINK(ci
->ren1
->pair
->two
->mode
)))
3110 df_conflict_remains
= 1;
3112 if (merge_mode_and_contents(opt
, o
, a
, b
, path
,
3113 opt
->branch1
, opt
->branch2
,
3114 opt
->priv
->call_depth
* 2, mfi
))
3118 * We can skip updating the working tree file iff:
3119 * a) The merge is clean
3120 * b) The merge matches what was in HEAD (content, mode, pathname)
3121 * c) The target path is usable (i.e. not involved in D/F conflict)
3123 if (mfi
->clean
&& was_tracked_and_matches(opt
, path
, &mfi
->blob
) &&
3124 !df_conflict_remains
) {
3126 struct cache_entry
*ce
;
3128 output(opt
, 3, _("Skipped %s (merged same as existing)"), path
);
3129 if (add_cacheinfo(opt
, &mfi
->blob
, path
,
3130 0, (!opt
->priv
->call_depth
&& !is_dirty
), 0))
3133 * However, add_cacheinfo() will delete the old cache entry
3134 * and add a new one. We need to copy over any skip_worktree
3135 * flag to avoid making the file appear as if it were
3136 * deleted by the user.
3138 pos
= index_name_pos(&opt
->priv
->orig_index
, path
, strlen(path
));
3139 ce
= opt
->priv
->orig_index
.cache
[pos
];
3140 if (ce_skip_worktree(ce
)) {
3141 pos
= index_name_pos(opt
->repo
->index
, path
, strlen(path
));
3142 ce
= opt
->repo
->index
->cache
[pos
];
3143 ce
->ce_flags
|= CE_SKIP_WORKTREE
;
3149 if (S_ISGITLINK(mfi
->blob
.mode
))
3150 reason
= _("submodule");
3151 output(opt
, 1, _("CONFLICT (%s): Merge conflict in %s"),
3153 if (ci
&& !df_conflict_remains
)
3154 if (update_stages(opt
, path
, o
, a
, b
))
3158 if (df_conflict_remains
|| is_dirty
) {
3160 if (opt
->priv
->call_depth
) {
3161 remove_file_from_index(opt
->repo
->index
, path
);
3164 if (update_stages(opt
, path
, o
, a
, b
))
3167 int file_from_stage2
= was_tracked(opt
, path
);
3169 if (update_stages(opt
, path
, NULL
,
3170 file_from_stage2
? &mfi
->blob
: NULL
,
3171 file_from_stage2
? NULL
: &mfi
->blob
))
3176 new_path
= unique_path(opt
, path
, ci
->ren1
->branch
);
3178 output(opt
, 1, _("Refusing to lose dirty file at %s"),
3181 output(opt
, 1, _("Adding as %s instead"), new_path
);
3182 if (update_file(opt
, 0, &mfi
->blob
, new_path
)) {
3188 } else if (update_file(opt
, mfi
->clean
, &mfi
->blob
, path
))
3190 return !is_dirty
&& mfi
->clean
;
3193 static int handle_rename_normal(struct merge_options
*opt
,
3195 const struct diff_filespec
*o
,
3196 const struct diff_filespec
*a
,
3197 const struct diff_filespec
*b
,
3198 struct rename_conflict_info
*ci
)
3200 struct rename
*ren
= ci
->ren1
;
3201 struct merge_file_info mfi
;
3204 /* Merge the content and write it out */
3205 clean
= handle_content_merge(&mfi
, opt
, path
, was_dirty(opt
, path
),
3209 opt
->detect_directory_renames
== MERGE_DIRECTORY_RENAMES_CONFLICT
&&
3210 ren
->dir_rename_original_dest
) {
3211 if (update_stages(opt
, path
,
3212 &mfi
.blob
, &mfi
.blob
, &mfi
.blob
))
3214 clean
= 0; /* not clean, but conflicted */
3219 static void dir_rename_warning(const char *msg
,
3222 struct merge_options
*opt
,
3225 const char *other_branch
;
3226 other_branch
= (ren
->branch
== opt
->branch1
?
3227 opt
->branch2
: opt
->branch1
);
3229 output(opt
, clean
? 2 : 1, msg
,
3230 ren
->pair
->one
->path
, ren
->branch
,
3231 other_branch
, ren
->pair
->two
->path
);
3234 output(opt
, clean
? 2 : 1, msg
,
3235 ren
->pair
->one
->path
, ren
->dir_rename_original_dest
, ren
->branch
,
3236 other_branch
, ren
->pair
->two
->path
);
3238 static int warn_about_dir_renamed_entries(struct merge_options
*opt
,
3242 int clean
= 1, is_add
;
3247 /* Return early if ren was not affected/created by a directory rename */
3248 if (!ren
->dir_rename_original_dest
)
3252 assert(opt
->detect_directory_renames
> MERGE_DIRECTORY_RENAMES_NONE
);
3253 assert(ren
->dir_rename_original_type
== 'A' ||
3254 ren
->dir_rename_original_type
== 'R');
3256 /* Check whether to treat directory renames as a conflict */
3257 clean
= (opt
->detect_directory_renames
== MERGE_DIRECTORY_RENAMES_TRUE
);
3259 is_add
= (ren
->dir_rename_original_type
== 'A');
3260 if (ren
->dir_rename_original_type
== 'A' && clean
) {
3261 msg
= _("Path updated: %s added in %s inside a "
3262 "directory that was renamed in %s; moving it to %s.");
3263 } else if (ren
->dir_rename_original_type
== 'A' && !clean
) {
3264 msg
= _("CONFLICT (file location): %s added in %s "
3265 "inside a directory that was renamed in %s, "
3266 "suggesting it should perhaps be moved to %s.");
3267 } else if (ren
->dir_rename_original_type
== 'R' && clean
) {
3268 msg
= _("Path updated: %s renamed to %s in %s, inside a "
3269 "directory that was renamed in %s; moving it to %s.");
3270 } else if (ren
->dir_rename_original_type
== 'R' && !clean
) {
3271 msg
= _("CONFLICT (file location): %s renamed to %s in %s, "
3272 "inside a directory that was renamed in %s, "
3273 "suggesting it should perhaps be moved to %s.");
3275 BUG("Impossible dir_rename_original_type/clean combination");
3277 dir_rename_warning(msg
, is_add
, clean
, opt
, ren
);
3282 /* Per entry merge function */
3283 static int process_entry(struct merge_options
*opt
,
3284 const char *path
, struct stage_data
*entry
)
3286 int clean_merge
= 1;
3287 int normalize
= opt
->renormalize
;
3289 struct diff_filespec
*o
= &entry
->stages
[1];
3290 struct diff_filespec
*a
= &entry
->stages
[2];
3291 struct diff_filespec
*b
= &entry
->stages
[3];
3292 int o_valid
= is_valid(o
);
3293 int a_valid
= is_valid(a
);
3294 int b_valid
= is_valid(b
);
3295 o
->path
= a
->path
= b
->path
= (char*)path
;
3297 entry
->processed
= 1;
3298 if (entry
->rename_conflict_info
) {
3299 struct rename_conflict_info
*ci
= entry
->rename_conflict_info
;
3300 struct diff_filespec
*temp
;
3303 path_clean
= warn_about_dir_renamed_entries(opt
, ci
->ren1
);
3304 path_clean
&= warn_about_dir_renamed_entries(opt
, ci
->ren2
);
3307 * For cases with a single rename, {o,a,b}->path have all been
3308 * set to the rename target path; we need to set two of these
3309 * back to the rename source.
3310 * For rename/rename conflicts, we'll manually fix paths below.
3312 temp
= (opt
->branch1
== ci
->ren1
->branch
) ? b
: a
;
3313 o
->path
= temp
->path
= ci
->ren1
->pair
->one
->path
;
3315 assert(opt
->branch1
== ci
->ren1
->branch
);
3318 switch (ci
->rename_type
) {
3320 case RENAME_ONE_FILE_TO_ONE
:
3321 clean_merge
= handle_rename_normal(opt
, path
, o
, a
, b
,
3324 case RENAME_VIA_DIR
:
3325 clean_merge
= handle_rename_via_dir(opt
, ci
);
3329 * Probably unclean merge, but if the renamed file
3330 * merges cleanly and the result can then be
3331 * two-way merged cleanly with the added file, I
3332 * guess it's a clean merge?
3334 clean_merge
= handle_rename_add(opt
, ci
);
3338 if (handle_rename_delete(opt
, ci
))
3341 case RENAME_ONE_FILE_TO_TWO
:
3343 * Manually fix up paths; note:
3344 * ren[12]->pair->one->path are equal.
3346 o
->path
= ci
->ren1
->pair
->one
->path
;
3347 a
->path
= ci
->ren1
->pair
->two
->path
;
3348 b
->path
= ci
->ren2
->pair
->two
->path
;
3351 if (handle_rename_rename_1to2(opt
, ci
))
3354 case RENAME_TWO_FILES_TO_ONE
:
3356 * Manually fix up paths; note,
3357 * ren[12]->pair->two->path are actually equal.
3360 a
->path
= ci
->ren1
->pair
->two
->path
;
3361 b
->path
= ci
->ren2
->pair
->two
->path
;
3364 * Probably unclean merge, but if the two renamed
3365 * files merge cleanly and the two resulting files
3366 * can then be two-way merged cleanly, I guess it's
3369 clean_merge
= handle_rename_rename_2to1(opt
, ci
);
3372 entry
->processed
= 0;
3375 if (path_clean
< clean_merge
)
3376 clean_merge
= path_clean
;
3377 } else if (o_valid
&& (!a_valid
|| !b_valid
)) {
3378 /* Case A: Deleted in one */
3379 if ((!a_valid
&& !b_valid
) ||
3380 (!b_valid
&& blob_unchanged(opt
, o
, a
, normalize
, path
)) ||
3381 (!a_valid
&& blob_unchanged(opt
, o
, b
, normalize
, path
))) {
3382 /* Deleted in both or deleted in one and
3383 * unchanged in the other */
3385 output(opt
, 2, _("Removing %s"), path
);
3386 /* do not touch working file if it did not exist */
3387 remove_file(opt
, 1, path
, !a_valid
);
3389 /* Modify/delete; deleted side may have put a directory in the way */
3391 if (handle_modify_delete(opt
, path
, o
, a
, b
))
3394 } else if ((!o_valid
&& a_valid
&& !b_valid
) ||
3395 (!o_valid
&& !a_valid
&& b_valid
)) {
3396 /* Case B: Added in one. */
3397 /* [nothing|directory] -> ([nothing|directory], file) */
3399 const char *add_branch
;
3400 const char *other_branch
;
3402 const struct diff_filespec
*contents
;
3405 add_branch
= opt
->branch1
;
3406 other_branch
= opt
->branch2
;
3408 conf
= _("file/directory");
3410 add_branch
= opt
->branch2
;
3411 other_branch
= opt
->branch1
;
3413 conf
= _("directory/file");
3415 if (dir_in_way(opt
->repo
->index
, path
,
3416 !opt
->priv
->call_depth
&& !S_ISGITLINK(a
->mode
),
3418 char *new_path
= unique_path(opt
, path
, add_branch
);
3420 output(opt
, 1, _("CONFLICT (%s): There is a directory with name %s in %s. "
3422 conf
, path
, other_branch
, path
, new_path
);
3423 if (update_file(opt
, 0, contents
, new_path
))
3425 else if (opt
->priv
->call_depth
)
3426 remove_file_from_index(opt
->repo
->index
, path
);
3429 output(opt
, 2, _("Adding %s"), path
);
3430 /* do not overwrite file if already present */
3431 if (update_file_flags(opt
, contents
, path
, 1, !a_valid
))
3434 } else if (a_valid
&& b_valid
) {
3436 /* Case C: Added in both (check for same permissions) */
3438 _("CONFLICT (add/add): Merge conflict in %s"),
3440 clean_merge
= handle_file_collision(opt
,
3446 /* case D: Modified in both, but differently. */
3447 struct merge_file_info mfi
;
3448 int is_dirty
= 0; /* unpack_trees would have bailed if dirty */
3449 clean_merge
= handle_content_merge(&mfi
, opt
, path
,
3453 } else if (!o_valid
&& !a_valid
&& !b_valid
) {
3455 * this entry was deleted altogether. a_mode == 0 means
3456 * we had that path and want to actively remove it.
3458 remove_file(opt
, 1, path
, !a
->mode
);
3460 BUG("fatal merge failure, shouldn't happen.");
3465 static int merge_trees_internal(struct merge_options
*opt
,
3468 struct tree
*merge_base
,
3469 struct tree
**result
)
3471 struct index_state
*istate
= opt
->repo
->index
;
3474 if (opt
->subtree_shift
) {
3475 merge
= shift_tree_object(opt
->repo
, head
, merge
,
3476 opt
->subtree_shift
);
3477 merge_base
= shift_tree_object(opt
->repo
, head
, merge_base
,
3478 opt
->subtree_shift
);
3481 if (oideq(&merge_base
->object
.oid
, &merge
->object
.oid
)) {
3482 output(opt
, 0, _("Already up to date."));
3487 code
= unpack_trees_start(opt
, merge_base
, head
, merge
);
3490 if (show(opt
, 4) || opt
->priv
->call_depth
)
3491 err(opt
, _("merging of trees %s and %s failed"),
3492 oid_to_hex(&head
->object
.oid
),
3493 oid_to_hex(&merge
->object
.oid
));
3494 unpack_trees_finish(opt
);
3498 if (unmerged_index(istate
)) {
3499 struct string_list
*entries
;
3500 struct rename_info re_info
;
3503 * Only need the hashmap while processing entries, so
3504 * initialize it here and free it when we are done running
3505 * through the entries. Keeping it in the merge_options as
3506 * opposed to decaring a local hashmap is for convenience
3507 * so that we don't have to pass it to around.
3509 hashmap_init(&opt
->priv
->current_file_dir_set
, path_hashmap_cmp
,
3511 get_files_dirs(opt
, head
);
3512 get_files_dirs(opt
, merge
);
3514 entries
= get_unmerged(opt
->repo
->index
);
3515 clean
= detect_and_process_renames(opt
, merge_base
, head
, merge
,
3517 record_df_conflict_files(opt
, entries
);
3520 for (i
= entries
->nr
-1; 0 <= i
; i
--) {
3521 const char *path
= entries
->items
[i
].string
;
3522 struct stage_data
*e
= entries
->items
[i
].util
;
3523 if (!e
->processed
) {
3524 int ret
= process_entry(opt
, path
, e
);
3533 for (i
= 0; i
< entries
->nr
; i
++) {
3534 struct stage_data
*e
= entries
->items
[i
].util
;
3536 BUG("unprocessed path??? %s",
3537 entries
->items
[i
].string
);
3541 final_cleanup_renames(&re_info
);
3543 string_list_clear(entries
, 1);
3546 hashmap_clear_and_free(&opt
->priv
->current_file_dir_set
,
3547 struct path_hashmap_entry
, e
);
3550 unpack_trees_finish(opt
);
3557 unpack_trees_finish(opt
);
3559 if (opt
->priv
->call_depth
&&
3560 !(*result
= write_in_core_index_as_tree(opt
->repo
)))
3567 * Merge the commits h1 and h2, returning a flag (int) indicating the
3568 * cleanness of the merge. Also, if opt->priv->call_depth, create a
3569 * virtual commit and write its location to *result.
3571 static int merge_recursive_internal(struct merge_options
*opt
,
3574 struct commit_list
*merge_bases
,
3575 struct commit
**result
)
3577 struct commit_list
*iter
;
3578 struct commit
*merged_merge_bases
;
3579 struct tree
*result_tree
;
3581 const char *ancestor_name
;
3582 struct strbuf merge_base_abbrev
= STRBUF_INIT
;
3585 output(opt
, 4, _("Merging:"));
3586 output_commit_title(opt
, h1
);
3587 output_commit_title(opt
, h2
);
3591 merge_bases
= get_merge_bases(h1
, h2
);
3592 merge_bases
= reverse_commit_list(merge_bases
);
3596 unsigned cnt
= commit_list_count(merge_bases
);
3598 output(opt
, 5, Q_("found %u common ancestor:",
3599 "found %u common ancestors:", cnt
), cnt
);
3600 for (iter
= merge_bases
; iter
; iter
= iter
->next
)
3601 output_commit_title(opt
, iter
->item
);
3604 merged_merge_bases
= pop_commit(&merge_bases
);
3605 if (merged_merge_bases
== NULL
) {
3606 /* if there is no common ancestor, use an empty tree */
3609 tree
= lookup_tree(opt
->repo
, opt
->repo
->hash_algo
->empty_tree
);
3610 merged_merge_bases
= make_virtual_commit(opt
->repo
, tree
,
3612 ancestor_name
= "empty tree";
3613 } else if (opt
->ancestor
&& !opt
->priv
->call_depth
) {
3614 ancestor_name
= opt
->ancestor
;
3615 } else if (merge_bases
) {
3616 ancestor_name
= "merged common ancestors";
3618 strbuf_add_unique_abbrev(&merge_base_abbrev
,
3619 &merged_merge_bases
->object
.oid
,
3621 ancestor_name
= merge_base_abbrev
.buf
;
3624 for (iter
= merge_bases
; iter
; iter
= iter
->next
) {
3625 const char *saved_b1
, *saved_b2
;
3626 opt
->priv
->call_depth
++;
3628 * When the merge fails, the result contains files
3629 * with conflict markers. The cleanness flag is
3630 * ignored (unless indicating an error), it was never
3631 * actually used, as result of merge_trees has always
3632 * overwritten it: the committed "conflicts" were
3635 discard_index(opt
->repo
->index
);
3636 saved_b1
= opt
->branch1
;
3637 saved_b2
= opt
->branch2
;
3638 opt
->branch1
= "Temporary merge branch 1";
3639 opt
->branch2
= "Temporary merge branch 2";
3640 if (merge_recursive_internal(opt
, merged_merge_bases
, iter
->item
,
3641 NULL
, &merged_merge_bases
) < 0)
3643 opt
->branch1
= saved_b1
;
3644 opt
->branch2
= saved_b2
;
3645 opt
->priv
->call_depth
--;
3647 if (!merged_merge_bases
)
3648 return err(opt
, _("merge returned no commit"));
3652 * FIXME: Since merge_recursive_internal() is only ever called by
3653 * places that ensure the index is loaded first
3654 * (e.g. builtin/merge.c, rebase/sequencer, etc.), in the common
3655 * case where the merge base was unique that means when we get here
3656 * we immediately discard the index and re-read it, which is a
3657 * complete waste of time. We should only be discarding and
3658 * re-reading if we were forced to recurse.
3660 discard_index(opt
->repo
->index
);
3661 if (!opt
->priv
->call_depth
)
3662 repo_read_index(opt
->repo
);
3664 opt
->ancestor
= ancestor_name
;
3665 clean
= merge_trees_internal(opt
,
3666 repo_get_commit_tree(opt
->repo
, h1
),
3667 repo_get_commit_tree(opt
->repo
, h2
),
3668 repo_get_commit_tree(opt
->repo
,
3669 merged_merge_bases
),
3671 strbuf_release(&merge_base_abbrev
);
3672 opt
->ancestor
= NULL
; /* avoid accidental re-use of opt->ancestor */
3678 if (opt
->priv
->call_depth
) {
3679 *result
= make_virtual_commit(opt
->repo
, result_tree
,
3681 commit_list_insert(h1
, &(*result
)->parents
);
3682 commit_list_insert(h2
, &(*result
)->parents
->next
);
3687 static int merge_start(struct merge_options
*opt
, struct tree
*head
)
3689 struct strbuf sb
= STRBUF_INIT
;
3691 /* Sanity checks on opt */
3694 assert(opt
->branch1
&& opt
->branch2
);
3696 assert(opt
->detect_renames
>= -1 &&
3697 opt
->detect_renames
<= DIFF_DETECT_COPY
);
3698 assert(opt
->detect_directory_renames
>= MERGE_DIRECTORY_RENAMES_NONE
&&
3699 opt
->detect_directory_renames
<= MERGE_DIRECTORY_RENAMES_TRUE
);
3700 assert(opt
->rename_limit
>= -1);
3701 assert(opt
->rename_score
>= 0 && opt
->rename_score
<= MAX_SCORE
);
3702 assert(opt
->show_rename_progress
>= 0 && opt
->show_rename_progress
<= 1);
3704 assert(opt
->xdl_opts
>= 0);
3705 assert(opt
->recursive_variant
>= MERGE_VARIANT_NORMAL
&&
3706 opt
->recursive_variant
<= MERGE_VARIANT_THEIRS
);
3708 assert(opt
->verbosity
>= 0 && opt
->verbosity
<= 5);
3709 assert(opt
->buffer_output
<= 2);
3710 assert(opt
->obuf
.len
== 0);
3712 assert(opt
->priv
== NULL
);
3714 /* Sanity check on repo state; index must match head */
3715 if (repo_index_has_changes(opt
->repo
, head
, &sb
)) {
3716 err(opt
, _("Your local changes to the following files would be overwritten by merge:\n %s"),
3718 strbuf_release(&sb
);
3722 CALLOC_ARRAY(opt
->priv
, 1);
3723 string_list_init_dup(&opt
->priv
->df_conflict_file_set
);
3727 static void merge_finalize(struct merge_options
*opt
)
3730 if (!opt
->priv
->call_depth
&& opt
->buffer_output
< 2)
3731 strbuf_release(&opt
->obuf
);
3733 diff_warn_rename_limit("merge.renamelimit",
3734 opt
->priv
->needed_rename_limit
, 0);
3735 FREE_AND_NULL(opt
->priv
);
3738 int merge_trees(struct merge_options
*opt
,
3741 struct tree
*merge_base
)
3744 struct tree
*ignored
;
3746 assert(opt
->ancestor
!= NULL
);
3748 if (merge_start(opt
, head
))
3750 clean
= merge_trees_internal(opt
, head
, merge
, merge_base
, &ignored
);
3751 merge_finalize(opt
);
3756 int merge_recursive(struct merge_options
*opt
,
3759 struct commit_list
*merge_bases
,
3760 struct commit
**result
)
3764 assert(opt
->ancestor
== NULL
||
3765 !strcmp(opt
->ancestor
, "constructed merge base"));
3767 prepare_repo_settings(opt
->repo
);
3768 opt
->repo
->settings
.command_requires_full_index
= 1;
3770 if (merge_start(opt
, repo_get_commit_tree(opt
->repo
, h1
)))
3772 clean
= merge_recursive_internal(opt
, h1
, h2
, merge_bases
, result
);
3773 merge_finalize(opt
);
3778 static struct commit
*get_ref(struct repository
*repo
,
3779 const struct object_id
*oid
,
3782 struct object
*object
;
3784 object
= deref_tag(repo
, parse_object(repo
, oid
),
3785 name
, strlen(name
));
3788 if (object
->type
== OBJ_TREE
)
3789 return make_virtual_commit(repo
, (struct tree
*)object
, name
);
3790 if (object
->type
!= OBJ_COMMIT
)
3792 if (parse_commit((struct commit
*)object
))
3794 return (struct commit
*)object
;
3797 int merge_recursive_generic(struct merge_options
*opt
,
3798 const struct object_id
*head
,
3799 const struct object_id
*merge
,
3800 int num_merge_bases
,
3801 const struct object_id
**merge_bases
,
3802 struct commit
**result
)
3805 struct lock_file lock
= LOCK_INIT
;
3806 struct commit
*head_commit
= get_ref(opt
->repo
, head
, opt
->branch1
);
3807 struct commit
*next_commit
= get_ref(opt
->repo
, merge
, opt
->branch2
);
3808 struct commit_list
*ca
= NULL
;
3812 for (i
= 0; i
< num_merge_bases
; ++i
) {
3813 struct commit
*base
;
3814 if (!(base
= get_ref(opt
->repo
, merge_bases
[i
],
3815 oid_to_hex(merge_bases
[i
]))))
3816 return err(opt
, _("Could not parse object '%s'"),
3817 oid_to_hex(merge_bases
[i
]));
3818 commit_list_insert(base
, &ca
);
3820 if (num_merge_bases
== 1)
3821 opt
->ancestor
= "constructed merge base";
3824 repo_hold_locked_index(opt
->repo
, &lock
, LOCK_DIE_ON_ERROR
);
3825 clean
= merge_recursive(opt
, head_commit
, next_commit
, ca
,
3828 rollback_lock_file(&lock
);
3832 if (write_locked_index(opt
->repo
->index
, &lock
,
3833 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
3834 return err(opt
, _("Unable to write index."));
3836 return clean
? 0 : 1;
3839 static void merge_recursive_config(struct merge_options
*opt
)
3842 int renormalize
= 0;
3843 git_config_get_int("merge.verbosity", &opt
->verbosity
);
3844 git_config_get_int("diff.renamelimit", &opt
->rename_limit
);
3845 git_config_get_int("merge.renamelimit", &opt
->rename_limit
);
3846 git_config_get_bool("merge.renormalize", &renormalize
);
3847 opt
->renormalize
= renormalize
;
3848 if (!git_config_get_string("diff.renames", &value
)) {
3849 opt
->detect_renames
= git_config_rename("diff.renames", value
);
3852 if (!git_config_get_string("merge.renames", &value
)) {
3853 opt
->detect_renames
= git_config_rename("merge.renames", value
);
3856 if (!git_config_get_string("merge.directoryrenames", &value
)) {
3857 int boolval
= git_parse_maybe_bool(value
);
3859 opt
->detect_directory_renames
= boolval
?
3860 MERGE_DIRECTORY_RENAMES_TRUE
:
3861 MERGE_DIRECTORY_RENAMES_NONE
;
3862 } else if (!strcasecmp(value
, "conflict")) {
3863 opt
->detect_directory_renames
=
3864 MERGE_DIRECTORY_RENAMES_CONFLICT
;
3865 } /* avoid erroring on values from future versions of git */
3868 git_config(git_xmerge_config
, NULL
);
3871 void init_merge_options(struct merge_options
*opt
,
3872 struct repository
*repo
)
3874 const char *merge_verbosity
;
3875 memset(opt
, 0, sizeof(struct merge_options
));
3879 opt
->detect_renames
= -1;
3880 opt
->detect_directory_renames
= MERGE_DIRECTORY_RENAMES_CONFLICT
;
3881 opt
->rename_limit
= -1;
3884 opt
->buffer_output
= 1;
3885 strbuf_init(&opt
->obuf
, 0);
3887 opt
->renormalize
= 0;
3889 merge_recursive_config(opt
);
3890 merge_verbosity
= getenv("GIT_MERGE_VERBOSITY");
3891 if (merge_verbosity
)
3892 opt
->verbosity
= strtol(merge_verbosity
, NULL
, 10);
3893 if (opt
->verbosity
>= 5)
3894 opt
->buffer_output
= 0;
3897 int parse_merge_opt(struct merge_options
*opt
, const char *s
)
3903 if (!strcmp(s
, "ours"))
3904 opt
->recursive_variant
= MERGE_VARIANT_OURS
;
3905 else if (!strcmp(s
, "theirs"))
3906 opt
->recursive_variant
= MERGE_VARIANT_THEIRS
;
3907 else if (!strcmp(s
, "subtree"))
3908 opt
->subtree_shift
= "";
3909 else if (skip_prefix(s
, "subtree=", &arg
))
3910 opt
->subtree_shift
= arg
;
3911 else if (!strcmp(s
, "patience"))
3912 opt
->xdl_opts
= DIFF_WITH_ALG(opt
, PATIENCE_DIFF
);
3913 else if (!strcmp(s
, "histogram"))
3914 opt
->xdl_opts
= DIFF_WITH_ALG(opt
, HISTOGRAM_DIFF
);
3915 else if (skip_prefix(s
, "diff-algorithm=", &arg
)) {
3916 long value
= parse_algorithm_value(arg
);
3919 /* clear out previous settings */
3920 DIFF_XDL_CLR(opt
, NEED_MINIMAL
);
3921 opt
->xdl_opts
&= ~XDF_DIFF_ALGORITHM_MASK
;
3922 opt
->xdl_opts
|= value
;
3924 else if (!strcmp(s
, "ignore-space-change"))
3925 DIFF_XDL_SET(opt
, IGNORE_WHITESPACE_CHANGE
);
3926 else if (!strcmp(s
, "ignore-all-space"))
3927 DIFF_XDL_SET(opt
, IGNORE_WHITESPACE
);
3928 else if (!strcmp(s
, "ignore-space-at-eol"))
3929 DIFF_XDL_SET(opt
, IGNORE_WHITESPACE_AT_EOL
);
3930 else if (!strcmp(s
, "ignore-cr-at-eol"))
3931 DIFF_XDL_SET(opt
, IGNORE_CR_AT_EOL
);
3932 else if (!strcmp(s
, "renormalize"))
3933 opt
->renormalize
= 1;
3934 else if (!strcmp(s
, "no-renormalize"))
3935 opt
->renormalize
= 0;
3936 else if (!strcmp(s
, "no-renames"))
3937 opt
->detect_renames
= 0;
3938 else if (!strcmp(s
, "find-renames")) {
3939 opt
->detect_renames
= 1;
3940 opt
->rename_score
= 0;
3942 else if (skip_prefix(s
, "find-renames=", &arg
) ||
3943 skip_prefix(s
, "rename-threshold=", &arg
)) {
3944 if ((opt
->rename_score
= parse_rename_score(&arg
)) == -1 || *arg
!= 0)
3946 opt
->detect_renames
= 1;
3949 * Please update $__git_merge_strategy_options in
3950 * git-completion.bash when you add new options