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 output_commit_title(struct merge_options
*opt
, struct commit
*commit
)
339 struct merge_remote_desc
*desc
;
341 strbuf_addchars(&opt
->obuf
, ' ', opt
->priv
->call_depth
* 2);
342 desc
= merge_remote_util(commit
);
344 strbuf_addf(&opt
->obuf
, "virtual %s\n", desc
->name
);
346 strbuf_add_unique_abbrev(&opt
->obuf
, &commit
->object
.oid
,
348 strbuf_addch(&opt
->obuf
, ' ');
349 if (parse_commit(commit
) != 0)
350 strbuf_addstr(&opt
->obuf
, _("(bad commit)\n"));
353 const char *msg
= get_commit_buffer(commit
, NULL
);
354 int len
= find_commit_subject(msg
, &title
);
356 strbuf_addf(&opt
->obuf
, "%.*s\n", len
, title
);
357 unuse_commit_buffer(commit
, msg
);
363 static int add_cacheinfo(struct merge_options
*opt
,
364 const struct diff_filespec
*blob
,
365 const char *path
, int stage
, int refresh
, int options
)
367 struct index_state
*istate
= opt
->repo
->index
;
368 struct cache_entry
*ce
;
371 ce
= make_cache_entry(istate
, blob
->mode
, &blob
->oid
, path
, stage
, 0);
373 return err(opt
, _("add_cacheinfo failed for path '%s'; merge aborting."), path
);
375 ret
= add_index_entry(istate
, ce
, options
);
377 struct cache_entry
*nce
;
379 nce
= refresh_cache_entry(istate
, ce
,
380 CE_MATCH_REFRESH
| CE_MATCH_IGNORE_MISSING
);
382 return err(opt
, _("add_cacheinfo failed to refresh for path '%s'; merge aborting."), path
);
384 ret
= add_index_entry(istate
, nce
, options
);
389 static inline int merge_detect_rename(struct merge_options
*opt
)
391 return (opt
->detect_renames
>= 0) ? opt
->detect_renames
: 1;
394 static void init_tree_desc_from_tree(struct tree_desc
*desc
, struct tree
*tree
)
397 init_tree_desc(desc
, tree
->buffer
, tree
->size
);
400 static int unpack_trees_start(struct merge_options
*opt
,
406 struct tree_desc t
[3];
407 struct index_state tmp_index
= { NULL
};
409 memset(&opt
->priv
->unpack_opts
, 0, sizeof(opt
->priv
->unpack_opts
));
410 if (opt
->priv
->call_depth
)
411 opt
->priv
->unpack_opts
.index_only
= 1;
413 opt
->priv
->unpack_opts
.update
= 1;
414 /* FIXME: should only do this if !overwrite_ignore */
415 opt
->priv
->unpack_opts
.preserve_ignored
= 0;
417 opt
->priv
->unpack_opts
.merge
= 1;
418 opt
->priv
->unpack_opts
.head_idx
= 2;
419 opt
->priv
->unpack_opts
.fn
= threeway_merge
;
420 opt
->priv
->unpack_opts
.src_index
= opt
->repo
->index
;
421 opt
->priv
->unpack_opts
.dst_index
= &tmp_index
;
422 opt
->priv
->unpack_opts
.aggressive
= !merge_detect_rename(opt
);
423 setup_unpack_trees_porcelain(&opt
->priv
->unpack_opts
, "merge");
425 init_tree_desc_from_tree(t
+0, common
);
426 init_tree_desc_from_tree(t
+1, head
);
427 init_tree_desc_from_tree(t
+2, merge
);
429 rc
= unpack_trees(3, t
, &opt
->priv
->unpack_opts
);
430 cache_tree_free(&opt
->repo
->index
->cache_tree
);
433 * Update opt->repo->index to match the new results, AFTER saving a
434 * copy in opt->priv->orig_index. Update src_index to point to the
435 * saved copy. (verify_uptodate() checks src_index, and the original
436 * index is the one that had the necessary modification timestamps.)
438 opt
->priv
->orig_index
= *opt
->repo
->index
;
439 *opt
->repo
->index
= tmp_index
;
440 opt
->priv
->unpack_opts
.src_index
= &opt
->priv
->orig_index
;
445 static void unpack_trees_finish(struct merge_options
*opt
)
447 discard_index(&opt
->priv
->orig_index
);
448 clear_unpack_trees_porcelain(&opt
->priv
->unpack_opts
);
451 static int save_files_dirs(const struct object_id
*oid
,
452 struct strbuf
*base
, const char *path
,
453 unsigned int mode
, void *context
)
455 struct path_hashmap_entry
*entry
;
456 int baselen
= base
->len
;
457 struct merge_options
*opt
= context
;
459 strbuf_addstr(base
, path
);
461 FLEX_ALLOC_MEM(entry
, path
, base
->buf
, base
->len
);
462 hashmap_entry_init(&entry
->e
, fspathhash(entry
->path
));
463 hashmap_add(&opt
->priv
->current_file_dir_set
, &entry
->e
);
465 strbuf_setlen(base
, baselen
);
466 return (S_ISDIR(mode
) ? READ_TREE_RECURSIVE
: 0);
469 static void get_files_dirs(struct merge_options
*opt
, struct tree
*tree
)
471 struct pathspec match_all
;
472 memset(&match_all
, 0, sizeof(match_all
));
473 read_tree(opt
->repo
, tree
,
474 &match_all
, save_files_dirs
, opt
);
477 static int get_tree_entry_if_blob(struct repository
*r
,
478 const struct object_id
*tree
,
480 struct diff_filespec
*dfs
)
484 ret
= get_tree_entry(r
, tree
, path
, &dfs
->oid
, &dfs
->mode
);
485 if (S_ISDIR(dfs
->mode
)) {
486 oidcpy(&dfs
->oid
, null_oid());
493 * Returns an index_entry instance which doesn't have to correspond to
494 * a real cache entry in Git's index.
496 static struct stage_data
*insert_stage_data(struct repository
*r
,
498 struct tree
*o
, struct tree
*a
, struct tree
*b
,
499 struct string_list
*entries
)
501 struct string_list_item
*item
;
502 struct stage_data
*e
= xcalloc(1, sizeof(struct stage_data
));
503 get_tree_entry_if_blob(r
, &o
->object
.oid
, path
, &e
->stages
[1]);
504 get_tree_entry_if_blob(r
, &a
->object
.oid
, path
, &e
->stages
[2]);
505 get_tree_entry_if_blob(r
, &b
->object
.oid
, path
, &e
->stages
[3]);
506 item
= string_list_insert(entries
, path
);
512 * Create a dictionary mapping file names to stage_data objects. The
513 * dictionary contains one entry for every path with a non-zero stage entry.
515 static struct string_list
*get_unmerged(struct index_state
*istate
)
517 struct string_list
*unmerged
= xcalloc(1, sizeof(struct string_list
));
520 unmerged
->strdup_strings
= 1;
522 /* TODO: audit for interaction with sparse-index. */
523 ensure_full_index(istate
);
524 for (i
= 0; i
< istate
->cache_nr
; i
++) {
525 struct string_list_item
*item
;
526 struct stage_data
*e
;
527 const struct cache_entry
*ce
= istate
->cache
[i
];
531 item
= string_list_lookup(unmerged
, ce
->name
);
533 item
= string_list_insert(unmerged
, ce
->name
);
534 item
->util
= xcalloc(1, sizeof(struct stage_data
));
537 e
->stages
[ce_stage(ce
)].mode
= ce
->ce_mode
;
538 oidcpy(&e
->stages
[ce_stage(ce
)].oid
, &ce
->oid
);
544 static int string_list_df_name_compare(const char *one
, const char *two
)
546 int onelen
= strlen(one
);
547 int twolen
= strlen(two
);
549 * Here we only care that entries for D/F conflicts are
550 * adjacent, in particular with the file of the D/F conflict
551 * appearing before files below the corresponding directory.
552 * The order of the rest of the list is irrelevant for us.
554 * To achieve this, we sort with df_name_compare and provide
555 * the mode S_IFDIR so that D/F conflicts will sort correctly.
556 * We use the mode S_IFDIR for everything else for simplicity,
557 * since in other cases any changes in their order due to
558 * sorting cause no problems for us.
560 int cmp
= df_name_compare(one
, onelen
, S_IFDIR
,
561 two
, twolen
, S_IFDIR
);
563 * Now that 'foo' and 'foo/bar' compare equal, we have to make sure
564 * that 'foo' comes before 'foo/bar'.
568 return onelen
- twolen
;
571 static void record_df_conflict_files(struct merge_options
*opt
,
572 struct string_list
*entries
)
574 /* If there is a D/F conflict and the file for such a conflict
575 * currently exists in the working tree, we want to allow it to be
576 * removed to make room for the corresponding directory if needed.
577 * The files underneath the directories of such D/F conflicts will
578 * be processed before the corresponding file involved in the D/F
579 * conflict. If the D/F directory ends up being removed by the
580 * merge, then we won't have to touch the D/F file. If the D/F
581 * directory needs to be written to the working copy, then the D/F
582 * file will simply be removed (in make_room_for_path()) to make
583 * room for the necessary paths. Note that if both the directory
584 * and the file need to be present, then the D/F file will be
585 * reinstated with a new unique name at the time it is processed.
587 struct string_list df_sorted_entries
= STRING_LIST_INIT_NODUP
;
588 const char *last_file
= NULL
;
593 * If we're merging merge-bases, we don't want to bother with
594 * any working directory changes.
596 if (opt
->priv
->call_depth
)
599 /* Ensure D/F conflicts are adjacent in the entries list. */
600 for (i
= 0; i
< entries
->nr
; i
++) {
601 struct string_list_item
*next
= &entries
->items
[i
];
602 string_list_append(&df_sorted_entries
, next
->string
)->util
=
605 df_sorted_entries
.cmp
= string_list_df_name_compare
;
606 string_list_sort(&df_sorted_entries
);
608 string_list_clear(&opt
->priv
->df_conflict_file_set
, 1);
609 for (i
= 0; i
< df_sorted_entries
.nr
; i
++) {
610 const char *path
= df_sorted_entries
.items
[i
].string
;
611 int len
= strlen(path
);
612 struct stage_data
*e
= df_sorted_entries
.items
[i
].util
;
615 * Check if last_file & path correspond to a D/F conflict;
616 * i.e. whether path is last_file+'/'+<something>.
617 * If so, record that it's okay to remove last_file to make
618 * room for path and friends if needed.
622 memcmp(path
, last_file
, last_len
) == 0 &&
623 path
[last_len
] == '/') {
624 string_list_insert(&opt
->priv
->df_conflict_file_set
, last_file
);
628 * Determine whether path could exist as a file in the
629 * working directory as a possible D/F conflict. This
630 * will only occur when it exists in stage 2 as a
633 if (S_ISREG(e
->stages
[2].mode
) || S_ISLNK(e
->stages
[2].mode
)) {
640 string_list_clear(&df_sorted_entries
, 0);
643 static int update_stages(struct merge_options
*opt
, const char *path
,
644 const struct diff_filespec
*o
,
645 const struct diff_filespec
*a
,
646 const struct diff_filespec
*b
)
650 * NOTE: It is usually a bad idea to call update_stages on a path
651 * before calling update_file on that same path, since it can
652 * sometimes lead to spurious "refusing to lose untracked file..."
653 * messages from update_file (via make_room_for path via
654 * would_lose_untracked). Instead, reverse the order of the calls
655 * (executing update_file first and then update_stages).
658 int options
= ADD_CACHE_OK_TO_ADD
| ADD_CACHE_SKIP_DFCHECK
;
660 if (remove_file_from_index(opt
->repo
->index
, path
))
663 if (add_cacheinfo(opt
, o
, path
, 1, 0, options
))
666 if (add_cacheinfo(opt
, a
, path
, 2, 0, options
))
669 if (add_cacheinfo(opt
, b
, path
, 3, 0, options
))
674 static void update_entry(struct stage_data
*entry
,
675 struct diff_filespec
*o
,
676 struct diff_filespec
*a
,
677 struct diff_filespec
*b
)
679 entry
->processed
= 0;
680 entry
->stages
[1].mode
= o
->mode
;
681 entry
->stages
[2].mode
= a
->mode
;
682 entry
->stages
[3].mode
= b
->mode
;
683 oidcpy(&entry
->stages
[1].oid
, &o
->oid
);
684 oidcpy(&entry
->stages
[2].oid
, &a
->oid
);
685 oidcpy(&entry
->stages
[3].oid
, &b
->oid
);
688 static int remove_file(struct merge_options
*opt
, int clean
,
689 const char *path
, int no_wd
)
691 int update_cache
= opt
->priv
->call_depth
|| clean
;
692 int update_working_directory
= !opt
->priv
->call_depth
&& !no_wd
;
695 if (remove_file_from_index(opt
->repo
->index
, path
))
698 if (update_working_directory
) {
700 struct cache_entry
*ce
;
701 ce
= index_file_exists(opt
->repo
->index
, path
, strlen(path
),
703 if (ce
&& ce_stage(ce
) == 0 && strcmp(path
, ce
->name
))
706 if (remove_path(path
))
712 /* add a string to a strbuf, but converting "/" to "_" */
713 static void add_flattened_path(struct strbuf
*out
, const char *s
)
716 strbuf_addstr(out
, s
);
717 for (; i
< out
->len
; i
++)
718 if (out
->buf
[i
] == '/')
722 static char *unique_path(struct merge_options
*opt
,
726 struct path_hashmap_entry
*entry
;
727 struct strbuf newpath
= STRBUF_INIT
;
731 strbuf_addf(&newpath
, "%s~", path
);
732 add_flattened_path(&newpath
, branch
);
734 base_len
= newpath
.len
;
735 while (hashmap_get_from_hash(&opt
->priv
->current_file_dir_set
,
736 fspathhash(newpath
.buf
), newpath
.buf
) ||
737 (!opt
->priv
->call_depth
&& file_exists(newpath
.buf
))) {
738 strbuf_setlen(&newpath
, base_len
);
739 strbuf_addf(&newpath
, "_%d", suffix
++);
742 FLEX_ALLOC_MEM(entry
, path
, newpath
.buf
, newpath
.len
);
743 hashmap_entry_init(&entry
->e
, fspathhash(entry
->path
));
744 hashmap_add(&opt
->priv
->current_file_dir_set
, &entry
->e
);
745 return strbuf_detach(&newpath
, NULL
);
749 * Check whether a directory in the index is in the way of an incoming
750 * file. Return 1 if so. If check_working_copy is non-zero, also
751 * check the working directory. If empty_ok is non-zero, also return
752 * 0 in the case where the working-tree dir exists but is empty.
754 static int dir_in_way(struct index_state
*istate
, const char *path
,
755 int check_working_copy
, int empty_ok
)
758 struct strbuf dirpath
= STRBUF_INIT
;
761 strbuf_addstr(&dirpath
, path
);
762 strbuf_addch(&dirpath
, '/');
764 pos
= index_name_pos(istate
, dirpath
.buf
, dirpath
.len
);
768 if (pos
< istate
->cache_nr
&&
769 !strncmp(dirpath
.buf
, istate
->cache
[pos
]->name
, dirpath
.len
)) {
770 strbuf_release(&dirpath
);
774 strbuf_release(&dirpath
);
775 return check_working_copy
&& !lstat(path
, &st
) && S_ISDIR(st
.st_mode
) &&
776 !(empty_ok
&& is_empty_dir(path
)) &&
777 !has_symlink_leading_path(path
, strlen(path
));
781 * Returns whether path was tracked in the index before the merge started,
782 * and its oid and mode match the specified values
784 static int was_tracked_and_matches(struct merge_options
*opt
, const char *path
,
785 const struct diff_filespec
*blob
)
787 int pos
= index_name_pos(&opt
->priv
->orig_index
, path
, strlen(path
));
788 struct cache_entry
*ce
;
791 /* we were not tracking this path before the merge */
794 /* See if the file we were tracking before matches */
795 ce
= opt
->priv
->orig_index
.cache
[pos
];
796 return (oideq(&ce
->oid
, &blob
->oid
) && ce
->ce_mode
== blob
->mode
);
800 * Returns whether path was tracked in the index before the merge started
802 static int was_tracked(struct merge_options
*opt
, const char *path
)
804 int pos
= index_name_pos(&opt
->priv
->orig_index
, path
, strlen(path
));
807 /* we were tracking this path before the merge */
813 static int would_lose_untracked(struct merge_options
*opt
, const char *path
)
815 struct index_state
*istate
= opt
->repo
->index
;
818 * This may look like it can be simplified to:
819 * return !was_tracked(opt, path) && file_exists(path)
820 * but it can't. This function needs to know whether path was in
821 * the working tree due to EITHER having been tracked in the index
822 * before the merge OR having been put into the working copy and
823 * index by unpack_trees(). Due to that either-or requirement, we
824 * check the current index instead of the original one.
826 * Note that we do not need to worry about merge-recursive itself
827 * updating the index after unpack_trees() and before calling this
828 * function, because we strictly require all code paths in
829 * merge-recursive to update the working tree first and the index
830 * second. Doing otherwise would break
831 * update_file()/would_lose_untracked(); see every comment in this
832 * file which mentions "update_stages".
834 int pos
= index_name_pos(istate
, path
, strlen(path
));
838 while (pos
< istate
->cache_nr
&&
839 !strcmp(path
, istate
->cache
[pos
]->name
)) {
841 * If stage #0, it is definitely tracked.
842 * If it has stage #2 then it was tracked
843 * before this merge started. All other
844 * cases the path was not tracked.
846 switch (ce_stage(istate
->cache
[pos
])) {
853 return file_exists(path
);
856 static int was_dirty(struct merge_options
*opt
, const char *path
)
858 struct cache_entry
*ce
;
861 if (opt
->priv
->call_depth
|| !was_tracked(opt
, path
))
864 ce
= index_file_exists(opt
->priv
->unpack_opts
.src_index
,
865 path
, strlen(path
), ignore_case
);
866 dirty
= verify_uptodate(ce
, &opt
->priv
->unpack_opts
) != 0;
870 static int make_room_for_path(struct merge_options
*opt
, const char *path
)
873 const char *msg
= _("failed to create path '%s'%s");
875 /* Unlink any D/F conflict files that are in the way */
876 for (i
= 0; i
< opt
->priv
->df_conflict_file_set
.nr
; i
++) {
877 const char *df_path
= opt
->priv
->df_conflict_file_set
.items
[i
].string
;
878 size_t pathlen
= strlen(path
);
879 size_t df_pathlen
= strlen(df_path
);
880 if (df_pathlen
< pathlen
&&
881 path
[df_pathlen
] == '/' &&
882 strncmp(path
, df_path
, df_pathlen
) == 0) {
884 _("Removing %s to make room for subdirectory\n"),
887 unsorted_string_list_delete_item(&opt
->priv
->df_conflict_file_set
,
893 /* Make sure leading directories are created */
894 status
= safe_create_leading_directories_const(path
);
896 if (status
== SCLD_EXISTS
)
897 /* something else exists */
898 return err(opt
, msg
, path
, _(": perhaps a D/F conflict?"));
899 return err(opt
, msg
, path
, "");
903 * Do not unlink a file in the work tree if we are not
906 if (would_lose_untracked(opt
, path
))
907 return err(opt
, _("refusing to lose untracked file at '%s'"),
910 /* Successful unlink is good.. */
913 /* .. and so is no existing file */
916 /* .. but not some other error (who really cares what?) */
917 return err(opt
, msg
, path
, _(": perhaps a D/F conflict?"));
920 static int update_file_flags(struct merge_options
*opt
,
921 const struct diff_filespec
*contents
,
928 if (opt
->priv
->call_depth
)
932 enum object_type type
;
936 if (S_ISGITLINK(contents
->mode
)) {
938 * We may later decide to recursively descend into
939 * the submodule directory and update its index
940 * and/or work tree, but we do not do that now.
946 buf
= read_object_file(&contents
->oid
, &type
, &size
);
948 ret
= err(opt
, _("cannot read object %s '%s'"),
949 oid_to_hex(&contents
->oid
), path
);
952 if (type
!= OBJ_BLOB
) {
953 ret
= err(opt
, _("blob expected for %s '%s'"),
954 oid_to_hex(&contents
->oid
), path
);
957 if (S_ISREG(contents
->mode
)) {
958 struct strbuf strbuf
= STRBUF_INIT
;
959 if (convert_to_working_tree(opt
->repo
->index
,
960 path
, buf
, size
, &strbuf
, NULL
)) {
963 buf
= strbuf_detach(&strbuf
, NULL
);
967 if (make_room_for_path(opt
, path
) < 0) {
971 if (S_ISREG(contents
->mode
) ||
972 (!has_symlinks
&& S_ISLNK(contents
->mode
))) {
974 int mode
= (contents
->mode
& 0100 ? 0777 : 0666);
976 fd
= open(path
, O_WRONLY
| O_TRUNC
| O_CREAT
, mode
);
978 ret
= err(opt
, _("failed to open '%s': %s"),
979 path
, strerror(errno
));
982 write_in_full(fd
, buf
, size
);
984 } else if (S_ISLNK(contents
->mode
)) {
985 char *lnk
= xmemdupz(buf
, size
);
986 safe_create_leading_directories_const(path
);
988 if (symlink(lnk
, path
))
989 ret
= err(opt
, _("failed to symlink '%s': %s"),
990 path
, strerror(errno
));
994 _("do not know what to do with %06o %s '%s'"),
995 contents
->mode
, oid_to_hex(&contents
->oid
), path
);
1000 if (!ret
&& update_cache
) {
1001 int refresh
= (!opt
->priv
->call_depth
&&
1002 contents
->mode
!= S_IFGITLINK
);
1003 if (add_cacheinfo(opt
, contents
, path
, 0, refresh
,
1004 ADD_CACHE_OK_TO_ADD
))
1010 static int update_file(struct merge_options
*opt
,
1012 const struct diff_filespec
*contents
,
1015 return update_file_flags(opt
, contents
, path
,
1016 opt
->priv
->call_depth
|| clean
, !opt
->priv
->call_depth
);
1019 /* Low level file merging, update and removal */
1021 struct merge_file_info
{
1022 struct diff_filespec blob
; /* mostly use oid & mode; sometimes path */
1027 static int merge_3way(struct merge_options
*opt
,
1028 mmbuffer_t
*result_buf
,
1029 const struct diff_filespec
*o
,
1030 const struct diff_filespec
*a
,
1031 const struct diff_filespec
*b
,
1032 const char *branch1
,
1033 const char *branch2
,
1034 const int extra_marker_size
)
1036 mmfile_t orig
, src1
, src2
;
1037 struct ll_merge_options ll_opts
= {0};
1038 char *base
, *name1
, *name2
;
1041 ll_opts
.renormalize
= opt
->renormalize
;
1042 ll_opts
.extra_marker_size
= extra_marker_size
;
1043 ll_opts
.xdl_opts
= opt
->xdl_opts
;
1045 if (opt
->priv
->call_depth
) {
1046 ll_opts
.virtual_ancestor
= 1;
1047 ll_opts
.variant
= 0;
1049 switch (opt
->recursive_variant
) {
1050 case MERGE_VARIANT_OURS
:
1051 ll_opts
.variant
= XDL_MERGE_FAVOR_OURS
;
1053 case MERGE_VARIANT_THEIRS
:
1054 ll_opts
.variant
= XDL_MERGE_FAVOR_THEIRS
;
1057 ll_opts
.variant
= 0;
1062 assert(a
->path
&& b
->path
&& o
->path
&& opt
->ancestor
);
1063 if (strcmp(a
->path
, b
->path
) || strcmp(a
->path
, o
->path
) != 0) {
1064 base
= mkpathdup("%s:%s", opt
->ancestor
, o
->path
);
1065 name1
= mkpathdup("%s:%s", branch1
, a
->path
);
1066 name2
= mkpathdup("%s:%s", branch2
, b
->path
);
1068 base
= mkpathdup("%s", opt
->ancestor
);
1069 name1
= mkpathdup("%s", branch1
);
1070 name2
= mkpathdup("%s", branch2
);
1073 read_mmblob(&orig
, &o
->oid
);
1074 read_mmblob(&src1
, &a
->oid
);
1075 read_mmblob(&src2
, &b
->oid
);
1078 * FIXME: Using a->path for normalization rules in ll_merge could be
1079 * wrong if we renamed from a->path to b->path. We should use the
1080 * target path for where the file will be written.
1082 merge_status
= ll_merge(result_buf
, a
->path
, &orig
, base
,
1083 &src1
, name1
, &src2
, name2
,
1084 opt
->repo
->index
, &ll_opts
);
1092 return merge_status
;
1095 static int find_first_merges(struct repository
*repo
,
1096 struct object_array
*result
, const char *path
,
1097 struct commit
*a
, struct commit
*b
)
1100 struct object_array merges
= OBJECT_ARRAY_INIT
;
1101 struct commit
*commit
;
1102 int contains_another
;
1104 char merged_revision
[GIT_MAX_HEXSZ
+ 2];
1105 const char *rev_args
[] = { "rev-list", "--merges", "--ancestry-path",
1106 "--all", merged_revision
, NULL
};
1107 struct rev_info revs
;
1108 struct setup_revision_opt rev_opts
;
1110 memset(result
, 0, sizeof(struct object_array
));
1111 memset(&rev_opts
, 0, sizeof(rev_opts
));
1113 /* get all revisions that merge commit a */
1114 xsnprintf(merged_revision
, sizeof(merged_revision
), "^%s",
1115 oid_to_hex(&a
->object
.oid
));
1116 repo_init_revisions(repo
, &revs
, NULL
);
1117 /* FIXME: can't handle linked worktrees in submodules yet */
1118 revs
.single_worktree
= path
!= NULL
;
1119 setup_revisions(ARRAY_SIZE(rev_args
)-1, rev_args
, &revs
, &rev_opts
);
1121 /* save all revisions from the above list that contain b */
1122 if (prepare_revision_walk(&revs
))
1123 die("revision walk setup failed");
1124 while ((commit
= get_revision(&revs
)) != NULL
) {
1125 struct object
*o
= &(commit
->object
);
1126 if (repo_in_merge_bases(repo
, b
, commit
))
1127 add_object_array(o
, NULL
, &merges
);
1129 reset_revision_walk();
1131 /* Now we've got all merges that contain a and b. Prune all
1132 * merges that contain another found merge and save them in
1135 for (i
= 0; i
< merges
.nr
; i
++) {
1136 struct commit
*m1
= (struct commit
*) merges
.objects
[i
].item
;
1138 contains_another
= 0;
1139 for (j
= 0; j
< merges
.nr
; j
++) {
1140 struct commit
*m2
= (struct commit
*) merges
.objects
[j
].item
;
1141 if (i
!= j
&& repo_in_merge_bases(repo
, m2
, m1
)) {
1142 contains_another
= 1;
1147 if (!contains_another
)
1148 add_object_array(merges
.objects
[i
].item
, NULL
, result
);
1151 object_array_clear(&merges
);
1155 static void print_commit(struct commit
*commit
)
1157 struct strbuf sb
= STRBUF_INIT
;
1158 struct pretty_print_context ctx
= {0};
1159 ctx
.date_mode
.type
= DATE_NORMAL
;
1160 /* FIXME: Merge this with output_commit_title() */
1161 assert(!merge_remote_util(commit
));
1162 format_commit_message(commit
, " %h: %m %s", &sb
, &ctx
);
1163 fprintf(stderr
, "%s\n", sb
.buf
);
1164 strbuf_release(&sb
);
1167 static int is_valid(const struct diff_filespec
*dfs
)
1169 return dfs
->mode
!= 0 && !is_null_oid(&dfs
->oid
);
1172 static int merge_submodule(struct merge_options
*opt
,
1173 struct object_id
*result
, const char *path
,
1174 const struct object_id
*base
, const struct object_id
*a
,
1175 const struct object_id
*b
)
1177 struct repository subrepo
;
1179 struct commit
*commit_base
, *commit_a
, *commit_b
;
1181 struct object_array merges
;
1184 int search
= !opt
->priv
->call_depth
;
1186 /* store a in result in case we fail */
1187 /* FIXME: This is the WRONG resolution for the recursive case when
1188 * we need to be careful to avoid accidentally matching either side.
1189 * Should probably use o instead there, much like we do for merging
1194 /* we can not handle deletion conflicts */
1195 if (is_null_oid(base
))
1203 * NEEDSWORK: Remove this when all submodule object accesses are
1204 * through explicitly specified repositores.
1206 if (add_submodule_odb(path
)) {
1207 output(opt
, 1, _("Failed to merge submodule %s (not checked out)"), path
);
1211 if (repo_submodule_init(&subrepo
, opt
->repo
, path
, null_oid())) {
1212 output(opt
, 1, _("Failed to merge submodule %s (not checked out)"), path
);
1216 if (!(commit_base
= lookup_commit_reference(&subrepo
, base
)) ||
1217 !(commit_a
= lookup_commit_reference(&subrepo
, a
)) ||
1218 !(commit_b
= lookup_commit_reference(&subrepo
, b
))) {
1219 output(opt
, 1, _("Failed to merge submodule %s (commits not present)"), path
);
1223 /* check whether both changes are forward */
1224 if (!repo_in_merge_bases(&subrepo
, commit_base
, commit_a
) ||
1225 !repo_in_merge_bases(&subrepo
, commit_base
, commit_b
)) {
1226 output(opt
, 1, _("Failed to merge submodule %s (commits don't follow merge-base)"), path
);
1230 /* Case #1: a is contained in b or vice versa */
1231 if (repo_in_merge_bases(&subrepo
, commit_a
, commit_b
)) {
1234 output(opt
, 3, _("Fast-forwarding submodule %s to the following commit:"), path
);
1235 output_commit_title(opt
, commit_b
);
1236 } else if (show(opt
, 2))
1237 output(opt
, 2, _("Fast-forwarding submodule %s"), path
);
1244 if (repo_in_merge_bases(&subrepo
, commit_b
, commit_a
)) {
1247 output(opt
, 3, _("Fast-forwarding submodule %s to the following commit:"), path
);
1248 output_commit_title(opt
, commit_a
);
1249 } else if (show(opt
, 2))
1250 output(opt
, 2, _("Fast-forwarding submodule %s"), path
);
1259 * Case #2: There are one or more merges that contain a and b in
1260 * the submodule. If there is only one, then present it as a
1261 * suggestion to the user, but leave it marked unmerged so the
1262 * user needs to confirm the resolution.
1265 /* Skip the search if makes no sense to the calling context. */
1269 /* find commit which merges them */
1270 parent_count
= find_first_merges(&subrepo
, &merges
, path
,
1271 commit_a
, commit_b
);
1272 switch (parent_count
) {
1274 output(opt
, 1, _("Failed to merge submodule %s (merge following commits not found)"), path
);
1278 output(opt
, 1, _("Failed to merge submodule %s (not fast-forward)"), path
);
1279 output(opt
, 2, _("Found a possible merge resolution for the submodule:\n"));
1280 print_commit((struct commit
*) merges
.objects
[0].item
);
1282 "If this is correct simply add it to the index "
1285 " git update-index --cacheinfo 160000 %s \"%s\"\n\n"
1286 "which will accept this suggestion.\n"),
1287 oid_to_hex(&merges
.objects
[0].item
->oid
), path
);
1291 output(opt
, 1, _("Failed to merge submodule %s (multiple merges found)"), path
);
1292 for (i
= 0; i
< merges
.nr
; i
++)
1293 print_commit((struct commit
*) merges
.objects
[i
].item
);
1296 object_array_clear(&merges
);
1298 repo_clear(&subrepo
);
1302 static int merge_mode_and_contents(struct merge_options
*opt
,
1303 const struct diff_filespec
*o
,
1304 const struct diff_filespec
*a
,
1305 const struct diff_filespec
*b
,
1306 const char *filename
,
1307 const char *branch1
,
1308 const char *branch2
,
1309 const int extra_marker_size
,
1310 struct merge_file_info
*result
)
1312 if (opt
->branch1
!= branch1
) {
1314 * It's weird getting a reverse merge with HEAD on the bottom
1315 * side of the conflict markers and the other branch on the
1318 return merge_mode_and_contents(opt
, o
, b
, a
,
1321 extra_marker_size
, result
);
1327 if ((S_IFMT
& a
->mode
) != (S_IFMT
& b
->mode
)) {
1330 * FIXME: This is a bad resolution for recursive case; for
1331 * the recursive case we want something that is unlikely to
1332 * accidentally match either side. Also, while it makes
1333 * sense to prefer regular files over symlinks, it doesn't
1334 * make sense to prefer regular files over submodules.
1336 if (S_ISREG(a
->mode
)) {
1337 result
->blob
.mode
= a
->mode
;
1338 oidcpy(&result
->blob
.oid
, &a
->oid
);
1340 result
->blob
.mode
= b
->mode
;
1341 oidcpy(&result
->blob
.oid
, &b
->oid
);
1344 if (!oideq(&a
->oid
, &o
->oid
) && !oideq(&b
->oid
, &o
->oid
))
1350 if (a
->mode
== b
->mode
|| a
->mode
== o
->mode
)
1351 result
->blob
.mode
= b
->mode
;
1353 result
->blob
.mode
= a
->mode
;
1354 if (b
->mode
!= o
->mode
) {
1360 if (oideq(&a
->oid
, &b
->oid
) || oideq(&a
->oid
, &o
->oid
))
1361 oidcpy(&result
->blob
.oid
, &b
->oid
);
1362 else if (oideq(&b
->oid
, &o
->oid
))
1363 oidcpy(&result
->blob
.oid
, &a
->oid
);
1364 else if (S_ISREG(a
->mode
)) {
1365 mmbuffer_t result_buf
;
1366 int ret
= 0, merge_status
;
1368 merge_status
= merge_3way(opt
, &result_buf
, o
, a
, b
,
1372 if ((merge_status
< 0) || !result_buf
.ptr
)
1373 ret
= err(opt
, _("Failed to execute internal merge"));
1376 write_object_file(result_buf
.ptr
, result_buf
.size
,
1377 blob_type
, &result
->blob
.oid
))
1378 ret
= err(opt
, _("Unable to add %s to database"),
1381 free(result_buf
.ptr
);
1384 /* FIXME: bug, what if modes didn't match? */
1385 result
->clean
= (merge_status
== 0);
1386 } else if (S_ISGITLINK(a
->mode
)) {
1387 result
->clean
= merge_submodule(opt
, &result
->blob
.oid
,
1392 } else if (S_ISLNK(a
->mode
)) {
1393 switch (opt
->recursive_variant
) {
1394 case MERGE_VARIANT_NORMAL
:
1395 oidcpy(&result
->blob
.oid
, &a
->oid
);
1396 if (!oideq(&a
->oid
, &b
->oid
))
1399 case MERGE_VARIANT_OURS
:
1400 oidcpy(&result
->blob
.oid
, &a
->oid
);
1402 case MERGE_VARIANT_THEIRS
:
1403 oidcpy(&result
->blob
.oid
, &b
->oid
);
1407 BUG("unsupported object type in the tree");
1411 output(opt
, 2, _("Auto-merging %s"), filename
);
1416 static int handle_rename_via_dir(struct merge_options
*opt
,
1417 struct rename_conflict_info
*ci
)
1420 * Handle file adds that need to be renamed due to directory rename
1421 * detection. This differs from handle_rename_normal, because
1422 * there is no content merge to do; just move the file into the
1423 * desired final location.
1425 const struct rename
*ren
= ci
->ren1
;
1426 const struct diff_filespec
*dest
= ren
->pair
->two
;
1427 char *file_path
= dest
->path
;
1428 int mark_conflicted
= (opt
->detect_directory_renames
==
1429 MERGE_DIRECTORY_RENAMES_CONFLICT
);
1430 assert(ren
->dir_rename_original_dest
);
1432 if (!opt
->priv
->call_depth
&& would_lose_untracked(opt
, dest
->path
)) {
1433 mark_conflicted
= 1;
1434 file_path
= unique_path(opt
, dest
->path
, ren
->branch
);
1435 output(opt
, 1, _("Error: Refusing to lose untracked file at %s; "
1436 "writing to %s instead."),
1437 dest
->path
, file_path
);
1440 if (mark_conflicted
) {
1442 * Write the file in worktree at file_path. In the index,
1443 * only record the file at dest->path in the appropriate
1446 if (update_file(opt
, 0, dest
, file_path
))
1448 if (file_path
!= dest
->path
)
1450 if (update_stages(opt
, dest
->path
, NULL
,
1451 ren
->branch
== opt
->branch1
? dest
: NULL
,
1452 ren
->branch
== opt
->branch1
? NULL
: dest
))
1454 return 0; /* not clean, but conflicted */
1456 /* Update dest->path both in index and in worktree */
1457 if (update_file(opt
, 1, dest
, dest
->path
))
1459 return 1; /* clean */
1463 static int handle_change_delete(struct merge_options
*opt
,
1464 const char *path
, const char *old_path
,
1465 const struct diff_filespec
*o
,
1466 const struct diff_filespec
*changed
,
1467 const char *change_branch
,
1468 const char *delete_branch
,
1469 const char *change
, const char *change_past
)
1471 char *alt_path
= NULL
;
1472 const char *update_path
= path
;
1475 if (dir_in_way(opt
->repo
->index
, path
, !opt
->priv
->call_depth
, 0) ||
1476 (!opt
->priv
->call_depth
&& would_lose_untracked(opt
, path
))) {
1477 update_path
= alt_path
= unique_path(opt
, path
, change_branch
);
1480 if (opt
->priv
->call_depth
) {
1482 * We cannot arbitrarily accept either a_sha or b_sha as
1483 * correct; since there is no true "middle point" between
1484 * them, simply reuse the base version for virtual merge base.
1486 ret
= remove_file_from_index(opt
->repo
->index
, path
);
1488 ret
= update_file(opt
, 0, o
, update_path
);
1491 * Despite the four nearly duplicate messages and argument
1492 * lists below and the ugliness of the nested if-statements,
1493 * having complete messages makes the job easier for
1496 * The slight variance among the cases is due to the fact
1498 * 1) directory/file conflicts (in effect if
1499 * !alt_path) could cause us to need to write the
1500 * file to a different path.
1501 * 2) renames (in effect if !old_path) could mean that
1502 * there are two names for the path that the user
1503 * may know the file by.
1507 output(opt
, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1508 "and %s in %s. Version %s of %s left in tree."),
1509 change
, path
, delete_branch
, change_past
,
1510 change_branch
, change_branch
, path
);
1512 output(opt
, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1513 "and %s to %s in %s. Version %s of %s left in tree."),
1514 change
, old_path
, delete_branch
, change_past
, path
,
1515 change_branch
, change_branch
, path
);
1519 output(opt
, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1520 "and %s in %s. Version %s of %s left in tree at %s."),
1521 change
, path
, delete_branch
, change_past
,
1522 change_branch
, change_branch
, path
, alt_path
);
1524 output(opt
, 1, _("CONFLICT (%s/delete): %s deleted in %s "
1525 "and %s to %s in %s. Version %s of %s left in tree at %s."),
1526 change
, old_path
, delete_branch
, change_past
, path
,
1527 change_branch
, change_branch
, path
, alt_path
);
1531 * No need to call update_file() on path when change_branch ==
1532 * opt->branch1 && !alt_path, since that would needlessly touch
1533 * path. We could call update_file_flags() with update_cache=0
1534 * and update_wd=0, but that's a no-op.
1536 if (change_branch
!= opt
->branch1
|| alt_path
)
1537 ret
= update_file(opt
, 0, changed
, update_path
);
1544 static int handle_rename_delete(struct merge_options
*opt
,
1545 struct rename_conflict_info
*ci
)
1547 const struct rename
*ren
= ci
->ren1
;
1548 const struct diff_filespec
*orig
= ren
->pair
->one
;
1549 const struct diff_filespec
*dest
= ren
->pair
->two
;
1550 const char *rename_branch
= ren
->branch
;
1551 const char *delete_branch
= (opt
->branch1
== ren
->branch
?
1552 opt
->branch2
: opt
->branch1
);
1554 if (handle_change_delete(opt
,
1555 opt
->priv
->call_depth
? orig
->path
: dest
->path
,
1556 opt
->priv
->call_depth
? NULL
: orig
->path
,
1558 rename_branch
, delete_branch
,
1559 _("rename"), _("renamed")))
1562 if (opt
->priv
->call_depth
)
1563 return remove_file_from_index(opt
->repo
->index
, dest
->path
);
1565 return update_stages(opt
, dest
->path
, NULL
,
1566 rename_branch
== opt
->branch1
? dest
: NULL
,
1567 rename_branch
== opt
->branch1
? NULL
: dest
);
1570 static int handle_file_collision(struct merge_options
*opt
,
1571 const char *collide_path
,
1572 const char *prev_path1
,
1573 const char *prev_path2
,
1574 const char *branch1
, const char *branch2
,
1575 struct diff_filespec
*a
,
1576 struct diff_filespec
*b
)
1578 struct merge_file_info mfi
;
1579 struct diff_filespec null
;
1580 char *alt_path
= NULL
;
1581 const char *update_path
= collide_path
;
1584 * It's easiest to get the correct things into stage 2 and 3, and
1585 * to make sure that the content merge puts HEAD before the other
1586 * branch if we just ensure that branch1 == opt->branch1. So, simply
1587 * flip arguments around if we don't have that.
1589 if (branch1
!= opt
->branch1
) {
1590 return handle_file_collision(opt
, collide_path
,
1591 prev_path2
, prev_path1
,
1596 /* Remove rename sources if rename/add or rename/rename(2to1) */
1598 remove_file(opt
, 1, prev_path1
,
1599 opt
->priv
->call_depth
|| would_lose_untracked(opt
, prev_path1
));
1601 remove_file(opt
, 1, prev_path2
,
1602 opt
->priv
->call_depth
|| would_lose_untracked(opt
, prev_path2
));
1605 * Remove the collision path, if it wouldn't cause dirty contents
1606 * or an untracked file to get lost. We'll either overwrite with
1607 * merged contents, or just write out to differently named files.
1609 if (was_dirty(opt
, collide_path
)) {
1610 output(opt
, 1, _("Refusing to lose dirty file at %s"),
1612 update_path
= alt_path
= unique_path(opt
, collide_path
, "merged");
1613 } else if (would_lose_untracked(opt
, collide_path
)) {
1615 * Only way we get here is if both renames were from
1616 * a directory rename AND user had an untracked file
1617 * at the location where both files end up after the
1618 * two directory renames. See testcase 10d of t6043.
1620 output(opt
, 1, _("Refusing to lose untracked file at "
1621 "%s, even though it's in the way."),
1623 update_path
= alt_path
= unique_path(opt
, collide_path
, "merged");
1626 * FIXME: It's possible that the two files are identical
1627 * and that the current working copy happens to match, in
1628 * which case we are unnecessarily touching the working
1629 * tree file. It's not a likely enough scenario that I
1630 * want to code up the checks for it and a better fix is
1631 * available if we restructure how unpack_trees() and
1632 * merge-recursive interoperate anyway, so punting for
1635 remove_file(opt
, 0, collide_path
, 0);
1638 /* Store things in diff_filespecs for functions that need it */
1639 null
.path
= (char *)collide_path
;
1640 oidcpy(&null
.oid
, null_oid());
1643 if (merge_mode_and_contents(opt
, &null
, a
, b
, collide_path
,
1644 branch1
, branch2
, opt
->priv
->call_depth
* 2, &mfi
))
1646 mfi
.clean
&= !alt_path
;
1647 if (update_file(opt
, mfi
.clean
, &mfi
.blob
, update_path
))
1649 if (!mfi
.clean
&& !opt
->priv
->call_depth
&&
1650 update_stages(opt
, collide_path
, NULL
, a
, b
))
1654 * FIXME: If both a & b both started with conflicts (only possible
1655 * if they came from a rename/rename(2to1)), but had IDENTICAL
1656 * contents including those conflicts, then in the next line we claim
1657 * it was clean. If someone cares about this case, we should have the
1658 * caller notify us if we started with conflicts.
1663 static int handle_rename_add(struct merge_options
*opt
,
1664 struct rename_conflict_info
*ci
)
1666 /* a was renamed to c, and a separate c was added. */
1667 struct diff_filespec
*a
= ci
->ren1
->pair
->one
;
1668 struct diff_filespec
*c
= ci
->ren1
->pair
->two
;
1669 char *path
= c
->path
;
1670 char *prev_path_desc
;
1671 struct merge_file_info mfi
;
1673 const char *rename_branch
= ci
->ren1
->branch
;
1674 const char *add_branch
= (opt
->branch1
== rename_branch
?
1675 opt
->branch2
: opt
->branch1
);
1676 int other_stage
= (ci
->ren1
->branch
== opt
->branch1
? 3 : 2);
1678 output(opt
, 1, _("CONFLICT (rename/add): "
1679 "Rename %s->%s in %s. Added %s in %s"),
1680 a
->path
, c
->path
, rename_branch
,
1681 c
->path
, add_branch
);
1683 prev_path_desc
= xstrfmt("version of %s from %s", path
, a
->path
);
1684 ci
->ren1
->src_entry
->stages
[other_stage
].path
= a
->path
;
1685 if (merge_mode_and_contents(opt
, a
, c
,
1686 &ci
->ren1
->src_entry
->stages
[other_stage
],
1688 opt
->branch1
, opt
->branch2
,
1689 1 + opt
->priv
->call_depth
* 2, &mfi
))
1691 free(prev_path_desc
);
1693 ci
->ren1
->dst_entry
->stages
[other_stage
].path
= mfi
.blob
.path
= c
->path
;
1694 return handle_file_collision(opt
,
1695 c
->path
, a
->path
, NULL
,
1696 rename_branch
, add_branch
,
1698 &ci
->ren1
->dst_entry
->stages
[other_stage
]);
1701 static char *find_path_for_conflict(struct merge_options
*opt
,
1703 const char *branch1
,
1704 const char *branch2
)
1706 char *new_path
= NULL
;
1707 if (dir_in_way(opt
->repo
->index
, path
, !opt
->priv
->call_depth
, 0)) {
1708 new_path
= unique_path(opt
, path
, branch1
);
1709 output(opt
, 1, _("%s is a directory in %s adding "
1711 path
, branch2
, new_path
);
1712 } else if (would_lose_untracked(opt
, path
)) {
1713 new_path
= unique_path(opt
, path
, branch1
);
1714 output(opt
, 1, _("Refusing to lose untracked file"
1715 " at %s; adding as %s instead"),
1723 * Toggle the stage number between "ours" and "theirs" (2 and 3).
1725 static inline int flip_stage(int stage
)
1727 return (2 + 3) - stage
;
1730 static int handle_rename_rename_1to2(struct merge_options
*opt
,
1731 struct rename_conflict_info
*ci
)
1733 /* One file was renamed in both branches, but to different names. */
1734 struct merge_file_info mfi
;
1735 struct diff_filespec
*add
;
1736 struct diff_filespec
*o
= ci
->ren1
->pair
->one
;
1737 struct diff_filespec
*a
= ci
->ren1
->pair
->two
;
1738 struct diff_filespec
*b
= ci
->ren2
->pair
->two
;
1741 output(opt
, 1, _("CONFLICT (rename/rename): "
1742 "Rename \"%s\"->\"%s\" in branch \"%s\" "
1743 "rename \"%s\"->\"%s\" in \"%s\"%s"),
1744 o
->path
, a
->path
, ci
->ren1
->branch
,
1745 o
->path
, b
->path
, ci
->ren2
->branch
,
1746 opt
->priv
->call_depth
? _(" (left unresolved)") : "");
1748 path_desc
= xstrfmt("%s and %s, both renamed from %s",
1749 a
->path
, b
->path
, o
->path
);
1750 if (merge_mode_and_contents(opt
, o
, a
, b
, path_desc
,
1751 ci
->ren1
->branch
, ci
->ren2
->branch
,
1752 opt
->priv
->call_depth
* 2, &mfi
))
1756 if (opt
->priv
->call_depth
)
1757 remove_file_from_index(opt
->repo
->index
, o
->path
);
1760 * For each destination path, we need to see if there is a
1761 * rename/add collision. If not, we can write the file out
1762 * to the specified location.
1764 add
= &ci
->ren1
->dst_entry
->stages
[flip_stage(2)];
1765 if (is_valid(add
)) {
1766 add
->path
= mfi
.blob
.path
= a
->path
;
1767 if (handle_file_collision(opt
, a
->path
,
1771 &mfi
.blob
, add
) < 0)
1774 char *new_path
= find_path_for_conflict(opt
, a
->path
,
1777 if (update_file(opt
, 0, &mfi
.blob
,
1778 new_path
? new_path
: a
->path
))
1781 if (!opt
->priv
->call_depth
&&
1782 update_stages(opt
, a
->path
, NULL
, a
, NULL
))
1786 if (!mfi
.clean
&& mfi
.blob
.mode
== a
->mode
&&
1787 oideq(&mfi
.blob
.oid
, &a
->oid
)) {
1789 * Getting here means we were attempting to merge a binary
1790 * blob. Since we can't merge binaries, the merge algorithm
1791 * just takes one side. But we don't want to copy the
1792 * contents of one side to both paths; we'd rather use the
1793 * original content at the given path for each path.
1795 oidcpy(&mfi
.blob
.oid
, &b
->oid
);
1796 mfi
.blob
.mode
= b
->mode
;
1798 add
= &ci
->ren2
->dst_entry
->stages
[flip_stage(3)];
1799 if (is_valid(add
)) {
1800 add
->path
= mfi
.blob
.path
= b
->path
;
1801 if (handle_file_collision(opt
, b
->path
,
1805 add
, &mfi
.blob
) < 0)
1808 char *new_path
= find_path_for_conflict(opt
, b
->path
,
1811 if (update_file(opt
, 0, &mfi
.blob
,
1812 new_path
? new_path
: b
->path
))
1815 if (!opt
->priv
->call_depth
&&
1816 update_stages(opt
, b
->path
, NULL
, NULL
, b
))
1823 static int handle_rename_rename_2to1(struct merge_options
*opt
,
1824 struct rename_conflict_info
*ci
)
1826 /* Two files, a & b, were renamed to the same thing, c. */
1827 struct diff_filespec
*a
= ci
->ren1
->pair
->one
;
1828 struct diff_filespec
*b
= ci
->ren2
->pair
->one
;
1829 struct diff_filespec
*c1
= ci
->ren1
->pair
->two
;
1830 struct diff_filespec
*c2
= ci
->ren2
->pair
->two
;
1831 char *path
= c1
->path
; /* == c2->path */
1832 char *path_side_1_desc
;
1833 char *path_side_2_desc
;
1834 struct merge_file_info mfi_c1
;
1835 struct merge_file_info mfi_c2
;
1836 int ostage1
, ostage2
;
1838 output(opt
, 1, _("CONFLICT (rename/rename): "
1839 "Rename %s->%s in %s. "
1840 "Rename %s->%s in %s"),
1841 a
->path
, c1
->path
, ci
->ren1
->branch
,
1842 b
->path
, c2
->path
, ci
->ren2
->branch
);
1844 path_side_1_desc
= xstrfmt("version of %s from %s", path
, a
->path
);
1845 path_side_2_desc
= xstrfmt("version of %s from %s", path
, b
->path
);
1846 ostage1
= ci
->ren1
->branch
== opt
->branch1
? 3 : 2;
1847 ostage2
= flip_stage(ostage1
);
1848 ci
->ren1
->src_entry
->stages
[ostage1
].path
= a
->path
;
1849 ci
->ren2
->src_entry
->stages
[ostage2
].path
= b
->path
;
1850 if (merge_mode_and_contents(opt
, a
, c1
,
1851 &ci
->ren1
->src_entry
->stages
[ostage1
],
1853 opt
->branch1
, opt
->branch2
,
1854 1 + opt
->priv
->call_depth
* 2, &mfi_c1
) ||
1855 merge_mode_and_contents(opt
, b
,
1856 &ci
->ren2
->src_entry
->stages
[ostage2
],
1857 c2
, path_side_2_desc
,
1858 opt
->branch1
, opt
->branch2
,
1859 1 + opt
->priv
->call_depth
* 2, &mfi_c2
))
1861 free(path_side_1_desc
);
1862 free(path_side_2_desc
);
1863 mfi_c1
.blob
.path
= path
;
1864 mfi_c2
.blob
.path
= path
;
1866 return handle_file_collision(opt
, path
, a
->path
, b
->path
,
1867 ci
->ren1
->branch
, ci
->ren2
->branch
,
1868 &mfi_c1
.blob
, &mfi_c2
.blob
);
1872 * Get the diff_filepairs changed between o_tree and tree.
1874 static struct diff_queue_struct
*get_diffpairs(struct merge_options
*opt
,
1875 struct tree
*o_tree
,
1878 struct diff_queue_struct
*ret
;
1879 struct diff_options opts
;
1881 repo_diff_setup(opt
->repo
, &opts
);
1882 opts
.flags
.recursive
= 1;
1883 opts
.flags
.rename_empty
= 0;
1884 opts
.detect_rename
= merge_detect_rename(opt
);
1886 * We do not have logic to handle the detection of copies. In
1887 * fact, it may not even make sense to add such logic: would we
1888 * really want a change to a base file to be propagated through
1889 * multiple other files by a merge?
1891 if (opts
.detect_rename
> DIFF_DETECT_RENAME
)
1892 opts
.detect_rename
= DIFF_DETECT_RENAME
;
1893 opts
.rename_limit
= (opt
->rename_limit
>= 0) ? opt
->rename_limit
: 7000;
1894 opts
.rename_score
= opt
->rename_score
;
1895 opts
.show_rename_progress
= opt
->show_rename_progress
;
1896 opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1897 diff_setup_done(&opts
);
1898 diff_tree_oid(&o_tree
->object
.oid
, &tree
->object
.oid
, "", &opts
);
1899 diffcore_std(&opts
);
1900 if (opts
.needed_rename_limit
> opt
->priv
->needed_rename_limit
)
1901 opt
->priv
->needed_rename_limit
= opts
.needed_rename_limit
;
1903 ret
= xmalloc(sizeof(*ret
));
1904 *ret
= diff_queued_diff
;
1906 opts
.output_format
= DIFF_FORMAT_NO_OUTPUT
;
1907 diff_queued_diff
.nr
= 0;
1908 diff_queued_diff
.queue
= NULL
;
1913 static int tree_has_path(struct repository
*r
, struct tree
*tree
,
1916 struct object_id hashy
;
1917 unsigned short mode_o
;
1919 return !get_tree_entry(r
,
1920 &tree
->object
.oid
, path
,
1925 * Return a new string that replaces the beginning portion (which matches
1926 * entry->dir), with entry->new_dir. In perl-speak:
1927 * new_path_name = (old_path =~ s/entry->dir/entry->new_dir/);
1929 * Caller must ensure that old_path starts with entry->dir + '/'.
1931 static char *apply_dir_rename(struct dir_rename_entry
*entry
,
1932 const char *old_path
)
1934 struct strbuf new_path
= STRBUF_INIT
;
1937 if (entry
->non_unique_new_dir
)
1940 oldlen
= strlen(entry
->dir
);
1941 if (entry
->new_dir
.len
== 0)
1943 * If someone renamed/merged a subdirectory into the root
1944 * directory (e.g. 'some/subdir' -> ''), then we want to
1947 * as the rename; we need to make old_path + oldlen advance
1948 * past the '/' character.
1951 newlen
= entry
->new_dir
.len
+ (strlen(old_path
) - oldlen
) + 1;
1952 strbuf_grow(&new_path
, newlen
);
1953 strbuf_addbuf(&new_path
, &entry
->new_dir
);
1954 strbuf_addstr(&new_path
, &old_path
[oldlen
]);
1956 return strbuf_detach(&new_path
, NULL
);
1959 static void get_renamed_dir_portion(const char *old_path
, const char *new_path
,
1960 char **old_dir
, char **new_dir
)
1962 char *end_of_old
, *end_of_new
;
1964 /* Default return values: NULL, meaning no rename */
1970 * "a/b/c/d/e/foo.c" -> "a/b/some/thing/else/e/foo.c"
1971 * the "e/foo.c" part is the same, we just want to know that
1972 * "a/b/c/d" was renamed to "a/b/some/thing/else"
1973 * so, for this example, this function returns "a/b/c/d" in
1974 * *old_dir and "a/b/some/thing/else" in *new_dir.
1978 * If the basename of the file changed, we don't care. We want
1979 * to know which portion of the directory, if any, changed.
1981 end_of_old
= strrchr(old_path
, '/');
1982 end_of_new
= strrchr(new_path
, '/');
1985 * If end_of_old is NULL, old_path wasn't in a directory, so there
1986 * could not be a directory rename (our rule elsewhere that a
1987 * directory which still exists is not considered to have been
1988 * renamed means the root directory can never be renamed -- because
1989 * the root directory always exists).
1991 if (end_of_old
== NULL
)
1992 return; /* Note: *old_dir and *new_dir are still NULL */
1995 * If new_path contains no directory (end_of_new is NULL), then we
1996 * have a rename of old_path's directory to the root directory.
1998 if (end_of_new
== NULL
) {
1999 *old_dir
= xstrndup(old_path
, end_of_old
- old_path
);
2000 *new_dir
= xstrdup("");
2004 /* Find the first non-matching character traversing backwards */
2005 while (*--end_of_new
== *--end_of_old
&&
2006 end_of_old
!= old_path
&&
2007 end_of_new
!= new_path
)
2008 ; /* Do nothing; all in the while loop */
2011 * If both got back to the beginning of their strings, then the
2012 * directory didn't change at all, only the basename did.
2014 if (end_of_old
== old_path
&& end_of_new
== new_path
&&
2015 *end_of_old
== *end_of_new
)
2016 return; /* Note: *old_dir and *new_dir are still NULL */
2019 * If end_of_new got back to the beginning of its string, and
2020 * end_of_old got back to the beginning of some subdirectory, then
2021 * we have a rename/merge of a subdirectory into the root, which
2022 * needs slightly special handling.
2024 * Note: There is no need to consider the opposite case, with a
2025 * rename/merge of the root directory into some subdirectory
2026 * because as noted above the root directory always exists so it
2027 * cannot be considered to be renamed.
2029 if (end_of_new
== new_path
&&
2030 end_of_old
!= old_path
&& end_of_old
[-1] == '/') {
2031 *old_dir
= xstrndup(old_path
, --end_of_old
- old_path
);
2032 *new_dir
= xstrdup("");
2037 * We've found the first non-matching character in the directory
2038 * paths. That means the current characters we were looking at
2039 * were part of the first non-matching subdir name going back from
2040 * the end of the strings. Get the whole name by advancing both
2041 * end_of_old and end_of_new to the NEXT '/' character. That will
2042 * represent the entire directory rename.
2044 * The reason for the increment is cases like
2045 * a/b/star/foo/whatever.c -> a/b/tar/foo/random.c
2046 * After dropping the basename and going back to the first
2047 * non-matching character, we're now comparing:
2049 * and we want to be comparing:
2050 * a/b/star/ and a/b/tar/
2051 * but without the pre-increment, the one on the right would stay
2054 end_of_old
= strchr(++end_of_old
, '/');
2055 end_of_new
= strchr(++end_of_new
, '/');
2057 /* Copy the old and new directories into *old_dir and *new_dir. */
2058 *old_dir
= xstrndup(old_path
, end_of_old
- old_path
);
2059 *new_dir
= xstrndup(new_path
, end_of_new
- new_path
);
2062 static void remove_hashmap_entries(struct hashmap
*dir_renames
,
2063 struct string_list
*items_to_remove
)
2066 struct dir_rename_entry
*entry
;
2068 for (i
= 0; i
< items_to_remove
->nr
; i
++) {
2069 entry
= items_to_remove
->items
[i
].util
;
2070 hashmap_remove(dir_renames
, &entry
->ent
, NULL
);
2072 string_list_clear(items_to_remove
, 0);
2076 * See if there is a directory rename for path, and if there are any file
2077 * level conflicts for the renamed location. If there is a rename and
2078 * there are no conflicts, return the new name. Otherwise, return NULL.
2080 static char *handle_path_level_conflicts(struct merge_options
*opt
,
2082 struct dir_rename_entry
*entry
,
2083 struct hashmap
*collisions
,
2086 char *new_path
= NULL
;
2087 struct collision_entry
*collision_ent
;
2089 struct strbuf collision_paths
= STRBUF_INIT
;
2092 * entry has the mapping of old directory name to new directory name
2093 * that we want to apply to path.
2095 new_path
= apply_dir_rename(entry
, path
);
2098 /* This should only happen when entry->non_unique_new_dir set */
2099 if (!entry
->non_unique_new_dir
)
2100 BUG("entry->non_unqiue_dir not set and !new_path");
2101 output(opt
, 1, _("CONFLICT (directory rename split): "
2102 "Unclear where to place %s because directory "
2103 "%s was renamed to multiple other directories, "
2104 "with no destination getting a majority of the "
2112 * The caller needs to have ensured that it has pre-populated
2113 * collisions with all paths that map to new_path. Do a quick check
2114 * to ensure that's the case.
2116 collision_ent
= collision_find_entry(collisions
, new_path
);
2117 if (collision_ent
== NULL
)
2118 BUG("collision_ent is NULL");
2121 * Check for one-sided add/add/.../add conflicts, i.e.
2122 * where implicit renames from the other side doing
2123 * directory rename(s) can affect this side of history
2124 * to put multiple paths into the same location. Warn
2125 * and bail on directory renames for such paths.
2127 if (collision_ent
->reported_already
) {
2129 } else if (tree_has_path(opt
->repo
, tree
, new_path
)) {
2130 collision_ent
->reported_already
= 1;
2131 strbuf_add_separated_string_list(&collision_paths
, ", ",
2132 &collision_ent
->source_files
);
2133 output(opt
, 1, _("CONFLICT (implicit dir rename): Existing "
2134 "file/dir at %s in the way of implicit "
2135 "directory rename(s) putting the following "
2136 "path(s) there: %s."),
2137 new_path
, collision_paths
.buf
);
2139 } else if (collision_ent
->source_files
.nr
> 1) {
2140 collision_ent
->reported_already
= 1;
2141 strbuf_add_separated_string_list(&collision_paths
, ", ",
2142 &collision_ent
->source_files
);
2143 output(opt
, 1, _("CONFLICT (implicit dir rename): Cannot map "
2144 "more than one path to %s; implicit directory "
2145 "renames tried to put these paths there: %s"),
2146 new_path
, collision_paths
.buf
);
2150 /* Free memory we no longer need */
2151 strbuf_release(&collision_paths
);
2152 if (!clean
&& new_path
) {
2161 * There are a couple things we want to do at the directory level:
2162 * 1. Check for both sides renaming to the same thing, in order to avoid
2163 * implicit renaming of files that should be left in place. (See
2164 * testcase 6b in t6043 for details.)
2165 * 2. Prune directory renames if there are still files left in the
2166 * original directory. These represent a partial directory rename,
2167 * i.e. a rename where only some of the files within the directory
2168 * were renamed elsewhere. (Technically, this could be done earlier
2169 * in get_directory_renames(), except that would prevent us from
2170 * doing the previous check and thus failing testcase 6b.)
2171 * 3. Check for rename/rename(1to2) conflicts (at the directory level).
2172 * In the future, we could potentially record this info as well and
2173 * omit reporting rename/rename(1to2) conflicts for each path within
2174 * the affected directories, thus cleaning up the merge output.
2175 * NOTE: We do NOT check for rename/rename(2to1) conflicts at the
2176 * directory level, because merging directories is fine. If it
2177 * causes conflicts for files within those merged directories, then
2178 * that should be detected at the individual path level.
2180 static void handle_directory_level_conflicts(struct merge_options
*opt
,
2181 struct hashmap
*dir_re_head
,
2183 struct hashmap
*dir_re_merge
,
2186 struct hashmap_iter iter
;
2187 struct dir_rename_entry
*head_ent
;
2188 struct dir_rename_entry
*merge_ent
;
2190 struct string_list remove_from_head
= STRING_LIST_INIT_NODUP
;
2191 struct string_list remove_from_merge
= STRING_LIST_INIT_NODUP
;
2193 hashmap_for_each_entry(dir_re_head
, &iter
, head_ent
,
2194 ent
/* member name */) {
2195 merge_ent
= dir_rename_find_entry(dir_re_merge
, head_ent
->dir
);
2197 !head_ent
->non_unique_new_dir
&&
2198 !merge_ent
->non_unique_new_dir
&&
2199 !strbuf_cmp(&head_ent
->new_dir
, &merge_ent
->new_dir
)) {
2200 /* 1. Renamed identically; remove it from both sides */
2201 string_list_append(&remove_from_head
,
2202 head_ent
->dir
)->util
= head_ent
;
2203 strbuf_release(&head_ent
->new_dir
);
2204 string_list_append(&remove_from_merge
,
2205 merge_ent
->dir
)->util
= merge_ent
;
2206 strbuf_release(&merge_ent
->new_dir
);
2207 } else if (tree_has_path(opt
->repo
, head
, head_ent
->dir
)) {
2208 /* 2. This wasn't a directory rename after all */
2209 string_list_append(&remove_from_head
,
2210 head_ent
->dir
)->util
= head_ent
;
2211 strbuf_release(&head_ent
->new_dir
);
2215 remove_hashmap_entries(dir_re_head
, &remove_from_head
);
2216 remove_hashmap_entries(dir_re_merge
, &remove_from_merge
);
2218 hashmap_for_each_entry(dir_re_merge
, &iter
, merge_ent
,
2219 ent
/* member name */) {
2220 head_ent
= dir_rename_find_entry(dir_re_head
, merge_ent
->dir
);
2221 if (tree_has_path(opt
->repo
, merge
, merge_ent
->dir
)) {
2222 /* 2. This wasn't a directory rename after all */
2223 string_list_append(&remove_from_merge
,
2224 merge_ent
->dir
)->util
= merge_ent
;
2225 } else if (head_ent
&&
2226 !head_ent
->non_unique_new_dir
&&
2227 !merge_ent
->non_unique_new_dir
) {
2228 /* 3. rename/rename(1to2) */
2230 * We can assume it's not rename/rename(1to1) because
2231 * that was case (1), already checked above. So we
2232 * know that head_ent->new_dir and merge_ent->new_dir
2233 * are different strings.
2235 output(opt
, 1, _("CONFLICT (rename/rename): "
2236 "Rename directory %s->%s in %s. "
2237 "Rename directory %s->%s in %s"),
2238 head_ent
->dir
, head_ent
->new_dir
.buf
, opt
->branch1
,
2239 head_ent
->dir
, merge_ent
->new_dir
.buf
, opt
->branch2
);
2240 string_list_append(&remove_from_head
,
2241 head_ent
->dir
)->util
= head_ent
;
2242 strbuf_release(&head_ent
->new_dir
);
2243 string_list_append(&remove_from_merge
,
2244 merge_ent
->dir
)->util
= merge_ent
;
2245 strbuf_release(&merge_ent
->new_dir
);
2249 remove_hashmap_entries(dir_re_head
, &remove_from_head
);
2250 remove_hashmap_entries(dir_re_merge
, &remove_from_merge
);
2253 static struct hashmap
*get_directory_renames(struct diff_queue_struct
*pairs
)
2255 struct hashmap
*dir_renames
;
2256 struct hashmap_iter iter
;
2257 struct dir_rename_entry
*entry
;
2261 * Typically, we think of a directory rename as all files from a
2262 * certain directory being moved to a target directory. However,
2263 * what if someone first moved two files from the original
2264 * directory in one commit, and then renamed the directory
2265 * somewhere else in a later commit? At merge time, we just know
2266 * that files from the original directory went to two different
2267 * places, and that the bulk of them ended up in the same place.
2268 * We want each directory rename to represent where the bulk of the
2269 * files from that directory end up; this function exists to find
2270 * where the bulk of the files went.
2272 * The first loop below simply iterates through the list of file
2273 * renames, finding out how often each directory rename pair
2274 * possibility occurs.
2276 dir_renames
= xmalloc(sizeof(*dir_renames
));
2277 dir_rename_init(dir_renames
);
2278 for (i
= 0; i
< pairs
->nr
; ++i
) {
2279 struct string_list_item
*item
;
2281 struct diff_filepair
*pair
= pairs
->queue
[i
];
2282 char *old_dir
, *new_dir
;
2284 /* File not part of directory rename if it wasn't renamed */
2285 if (pair
->status
!= 'R')
2288 get_renamed_dir_portion(pair
->one
->path
, pair
->two
->path
,
2289 &old_dir
, &new_dir
);
2291 /* Directory didn't change at all; ignore this one. */
2294 entry
= dir_rename_find_entry(dir_renames
, old_dir
);
2296 entry
= xmalloc(sizeof(*entry
));
2297 dir_rename_entry_init(entry
, old_dir
);
2298 hashmap_put(dir_renames
, &entry
->ent
);
2302 item
= string_list_lookup(&entry
->possible_new_dirs
, new_dir
);
2304 item
= string_list_insert(&entry
->possible_new_dirs
,
2306 item
->util
= xcalloc(1, sizeof(int));
2315 * For each directory with files moved out of it, we find out which
2316 * target directory received the most files so we can declare it to
2317 * be the "winning" target location for the directory rename. This
2318 * winner gets recorded in new_dir. If there is no winner
2319 * (multiple target directories received the same number of files),
2320 * we set non_unique_new_dir. Once we've determined the winner (or
2321 * that there is no winner), we no longer need possible_new_dirs.
2323 hashmap_for_each_entry(dir_renames
, &iter
, entry
,
2324 ent
/* member name */) {
2329 for (i
= 0; i
< entry
->possible_new_dirs
.nr
; i
++) {
2330 int *count
= entry
->possible_new_dirs
.items
[i
].util
;
2334 else if (*count
> max
) {
2336 best
= entry
->possible_new_dirs
.items
[i
].string
;
2340 entry
->non_unique_new_dir
= 1;
2342 assert(entry
->new_dir
.len
== 0);
2343 strbuf_addstr(&entry
->new_dir
, best
);
2346 * The relevant directory sub-portion of the original full
2347 * filepaths were xstrndup'ed before inserting into
2348 * possible_new_dirs, and instead of manually iterating the
2349 * list and free'ing each, just lie and tell
2350 * possible_new_dirs that it did the strdup'ing so that it
2351 * will free them for us.
2353 entry
->possible_new_dirs
.strdup_strings
= 1;
2354 string_list_clear(&entry
->possible_new_dirs
, 1);
2360 static struct dir_rename_entry
*check_dir_renamed(const char *path
,
2361 struct hashmap
*dir_renames
)
2363 char *temp
= xstrdup(path
);
2365 struct dir_rename_entry
*entry
= NULL
;
2367 while ((end
= strrchr(temp
, '/'))) {
2369 entry
= dir_rename_find_entry(dir_renames
, temp
);
2377 static void compute_collisions(struct hashmap
*collisions
,
2378 struct hashmap
*dir_renames
,
2379 struct diff_queue_struct
*pairs
)
2384 * Multiple files can be mapped to the same path due to directory
2385 * renames done by the other side of history. Since that other
2386 * side of history could have merged multiple directories into one,
2387 * if our side of history added the same file basename to each of
2388 * those directories, then all N of them would get implicitly
2389 * renamed by the directory rename detection into the same path,
2390 * and we'd get an add/add/.../add conflict, and all those adds
2391 * from *this* side of history. This is not representable in the
2392 * index, and users aren't going to easily be able to make sense of
2393 * it. So we need to provide a good warning about what's
2394 * happening, and fall back to no-directory-rename detection
2395 * behavior for those paths.
2397 * See testcases 9e and all of section 5 from t6043 for examples.
2399 collision_init(collisions
);
2401 for (i
= 0; i
< pairs
->nr
; ++i
) {
2402 struct dir_rename_entry
*dir_rename_ent
;
2403 struct collision_entry
*collision_ent
;
2405 struct diff_filepair
*pair
= pairs
->queue
[i
];
2407 if (pair
->status
!= 'A' && pair
->status
!= 'R')
2409 dir_rename_ent
= check_dir_renamed(pair
->two
->path
,
2411 if (!dir_rename_ent
)
2414 new_path
= apply_dir_rename(dir_rename_ent
, pair
->two
->path
);
2417 * dir_rename_ent->non_unique_new_path is true, which
2418 * means there is no directory rename for us to use,
2419 * which means it won't cause us any additional
2423 collision_ent
= collision_find_entry(collisions
, new_path
);
2424 if (!collision_ent
) {
2425 CALLOC_ARRAY(collision_ent
, 1);
2426 hashmap_entry_init(&collision_ent
->ent
,
2428 hashmap_put(collisions
, &collision_ent
->ent
);
2429 collision_ent
->target_file
= new_path
;
2433 string_list_insert(&collision_ent
->source_files
,
2438 static char *check_for_directory_rename(struct merge_options
*opt
,
2441 struct hashmap
*dir_renames
,
2442 struct hashmap
*dir_rename_exclusions
,
2443 struct hashmap
*collisions
,
2446 char *new_path
= NULL
;
2447 struct dir_rename_entry
*entry
= check_dir_renamed(path
, dir_renames
);
2448 struct dir_rename_entry
*oentry
= NULL
;
2454 * This next part is a little weird. We do not want to do an
2455 * implicit rename into a directory we renamed on our side, because
2456 * that will result in a spurious rename/rename(1to2) conflict. An
2458 * Base commit: dumbdir/afile, otherdir/bfile
2459 * Side 1: smrtdir/afile, otherdir/bfile
2460 * Side 2: dumbdir/afile, dumbdir/bfile
2461 * Here, while working on Side 1, we could notice that otherdir was
2462 * renamed/merged to dumbdir, and change the diff_filepair for
2463 * otherdir/bfile into a rename into dumbdir/bfile. However, Side
2464 * 2 will notice the rename from dumbdir to smrtdir, and do the
2465 * transitive rename to move it from dumbdir/bfile to
2466 * smrtdir/bfile. That gives us bfile in dumbdir vs being in
2467 * smrtdir, a rename/rename(1to2) conflict. We really just want
2468 * the file to end up in smrtdir. And the way to achieve that is
2469 * to not let Side1 do the rename to dumbdir, since we know that is
2470 * the source of one of our directory renames.
2472 * That's why oentry and dir_rename_exclusions is here.
2474 * As it turns out, this also prevents N-way transient rename
2475 * confusion; See testcases 9c and 9d of t6043.
2477 oentry
= dir_rename_find_entry(dir_rename_exclusions
, entry
->new_dir
.buf
);
2479 output(opt
, 1, _("WARNING: Avoiding applying %s -> %s rename "
2480 "to %s, because %s itself was renamed."),
2481 entry
->dir
, entry
->new_dir
.buf
, path
, entry
->new_dir
.buf
);
2483 new_path
= handle_path_level_conflicts(opt
, path
, entry
,
2485 *clean_merge
&= (new_path
!= NULL
);
2491 static void apply_directory_rename_modifications(struct merge_options
*opt
,
2492 struct diff_filepair
*pair
,
2496 struct tree
*o_tree
,
2497 struct tree
*a_tree
,
2498 struct tree
*b_tree
,
2499 struct string_list
*entries
)
2501 struct string_list_item
*item
;
2502 int stage
= (tree
== a_tree
? 2 : 3);
2506 * In all cases where we can do directory rename detection,
2507 * unpack_trees() will have read pair->two->path into the
2508 * index and the working copy. We need to remove it so that
2509 * we can instead place it at new_path. It is guaranteed to
2510 * not be untracked (unpack_trees() would have errored out
2511 * saying the file would have been overwritten), but it might
2514 update_wd
= !was_dirty(opt
, pair
->two
->path
);
2516 output(opt
, 1, _("Refusing to lose dirty file at %s"),
2518 remove_file(opt
, 1, pair
->two
->path
, !update_wd
);
2520 /* Find or create a new re->dst_entry */
2521 item
= string_list_lookup(entries
, new_path
);
2524 * Since we're renaming on this side of history, and it's
2525 * due to a directory rename on the other side of history
2526 * (which we only allow when the directory in question no
2527 * longer exists on the other side of history), the
2528 * original entry for re->dst_entry is no longer
2531 re
->dst_entry
->processed
= 1;
2534 * ...because we'll be using this new one.
2536 re
->dst_entry
= item
->util
;
2539 * re->dst_entry is for the before-dir-rename path, and we
2540 * need it to hold information for the after-dir-rename
2541 * path. Before creating a new entry, we need to mark the
2542 * old one as unnecessary (...unless it is shared by
2543 * src_entry, i.e. this didn't use to be a rename, in which
2544 * case we can just allow the normal processing to happen
2547 if (pair
->status
== 'R')
2548 re
->dst_entry
->processed
= 1;
2550 re
->dst_entry
= insert_stage_data(opt
->repo
, new_path
,
2551 o_tree
, a_tree
, b_tree
,
2553 item
= string_list_insert(entries
, new_path
);
2554 item
->util
= re
->dst_entry
;
2558 * Update the stage_data with the information about the path we are
2559 * moving into place. That slot will be empty and available for us
2560 * to write to because of the collision checks in
2561 * handle_path_level_conflicts(). In other words,
2562 * re->dst_entry->stages[stage].oid will be the null_oid, so it's
2563 * open for us to write to.
2565 * It may be tempting to actually update the index at this point as
2566 * well, using update_stages_for_stage_data(), but as per the big
2567 * "NOTE" in update_stages(), doing so will modify the current
2568 * in-memory index which will break calls to would_lose_untracked()
2569 * that we need to make. Instead, we need to just make sure that
2570 * the various handle_rename_*() functions update the index
2571 * explicitly rather than relying on unpack_trees() to have done it.
2573 get_tree_entry(opt
->repo
,
2576 &re
->dst_entry
->stages
[stage
].oid
,
2577 &re
->dst_entry
->stages
[stage
].mode
);
2580 * Record the original change status (or 'type' of change). If it
2581 * was originally an add ('A'), this lets us differentiate later
2582 * between a RENAME_DELETE conflict and RENAME_VIA_DIR (they
2583 * otherwise look the same). If it was originally a rename ('R'),
2584 * this lets us remember and report accurately about the transitive
2585 * renaming that occurred via the directory rename detection. Also,
2586 * record the original destination name.
2588 re
->dir_rename_original_type
= pair
->status
;
2589 re
->dir_rename_original_dest
= pair
->two
->path
;
2592 * We don't actually look at pair->status again, but it seems
2593 * pedagogically correct to adjust it.
2598 * Finally, record the new location.
2600 pair
->two
->path
= new_path
;
2604 * Get information of all renames which occurred in 'pairs', making use of
2605 * any implicit directory renames inferred from the other side of history.
2606 * We need the three trees in the merge ('o_tree', 'a_tree' and 'b_tree')
2607 * to be able to associate the correct cache entries with the rename
2608 * information; tree is always equal to either a_tree or b_tree.
2610 static struct string_list
*get_renames(struct merge_options
*opt
,
2612 struct diff_queue_struct
*pairs
,
2613 struct hashmap
*dir_renames
,
2614 struct hashmap
*dir_rename_exclusions
,
2616 struct tree
*o_tree
,
2617 struct tree
*a_tree
,
2618 struct tree
*b_tree
,
2619 struct string_list
*entries
,
2623 struct hashmap collisions
;
2624 struct hashmap_iter iter
;
2625 struct collision_entry
*e
;
2626 struct string_list
*renames
;
2628 compute_collisions(&collisions
, dir_renames
, pairs
);
2629 CALLOC_ARRAY(renames
, 1);
2631 for (i
= 0; i
< pairs
->nr
; ++i
) {
2632 struct string_list_item
*item
;
2634 struct diff_filepair
*pair
= pairs
->queue
[i
];
2635 char *new_path
; /* non-NULL only with directory renames */
2637 if (pair
->status
!= 'A' && pair
->status
!= 'R') {
2638 diff_free_filepair(pair
);
2641 new_path
= check_for_directory_rename(opt
, pair
->two
->path
, tree
,
2643 dir_rename_exclusions
,
2646 if (pair
->status
!= 'R' && !new_path
) {
2647 diff_free_filepair(pair
);
2651 re
= xmalloc(sizeof(*re
));
2654 re
->branch
= branch
;
2655 re
->dir_rename_original_type
= '\0';
2656 re
->dir_rename_original_dest
= NULL
;
2657 item
= string_list_lookup(entries
, re
->pair
->one
->path
);
2659 re
->src_entry
= insert_stage_data(opt
->repo
,
2660 re
->pair
->one
->path
,
2661 o_tree
, a_tree
, b_tree
, entries
);
2663 re
->src_entry
= item
->util
;
2665 item
= string_list_lookup(entries
, re
->pair
->two
->path
);
2667 re
->dst_entry
= insert_stage_data(opt
->repo
,
2668 re
->pair
->two
->path
,
2669 o_tree
, a_tree
, b_tree
, entries
);
2671 re
->dst_entry
= item
->util
;
2672 item
= string_list_insert(renames
, pair
->one
->path
);
2675 apply_directory_rename_modifications(opt
, pair
, new_path
,
2681 hashmap_for_each_entry(&collisions
, &iter
, e
,
2682 ent
/* member name */) {
2683 free(e
->target_file
);
2684 string_list_clear(&e
->source_files
, 0);
2686 hashmap_clear_and_free(&collisions
, struct collision_entry
, ent
);
2690 static int process_renames(struct merge_options
*opt
,
2691 struct string_list
*a_renames
,
2692 struct string_list
*b_renames
)
2694 int clean_merge
= 1, i
, j
;
2695 struct string_list a_by_dst
= STRING_LIST_INIT_NODUP
;
2696 struct string_list b_by_dst
= STRING_LIST_INIT_NODUP
;
2697 const struct rename
*sre
;
2700 * FIXME: As string-list.h notes, it's O(n^2) to build a sorted
2701 * string_list one-by-one, but O(n log n) to build it unsorted and
2702 * then sort it. Note that as we build the list, we do not need to
2703 * check if the existing destination path is already in the list,
2704 * because the structure of diffcore_rename guarantees we won't
2707 for (i
= 0; i
< a_renames
->nr
; i
++) {
2708 sre
= a_renames
->items
[i
].util
;
2709 string_list_insert(&a_by_dst
, sre
->pair
->two
->path
)->util
2712 for (i
= 0; i
< b_renames
->nr
; i
++) {
2713 sre
= b_renames
->items
[i
].util
;
2714 string_list_insert(&b_by_dst
, sre
->pair
->two
->path
)->util
2718 for (i
= 0, j
= 0; i
< a_renames
->nr
|| j
< b_renames
->nr
;) {
2719 struct string_list
*renames1
, *renames2Dst
;
2720 struct rename
*ren1
= NULL
, *ren2
= NULL
;
2721 const char *ren1_src
, *ren1_dst
;
2722 struct string_list_item
*lookup
;
2724 if (i
>= a_renames
->nr
) {
2725 ren2
= b_renames
->items
[j
++].util
;
2726 } else if (j
>= b_renames
->nr
) {
2727 ren1
= a_renames
->items
[i
++].util
;
2729 int compare
= strcmp(a_renames
->items
[i
].string
,
2730 b_renames
->items
[j
].string
);
2732 ren1
= a_renames
->items
[i
++].util
;
2734 ren2
= b_renames
->items
[j
++].util
;
2737 /* TODO: refactor, so that 1/2 are not needed */
2739 renames1
= a_renames
;
2740 renames2Dst
= &b_by_dst
;
2742 renames1
= b_renames
;
2743 renames2Dst
= &a_by_dst
;
2747 if (ren1
->processed
)
2749 ren1
->processed
= 1;
2750 ren1
->dst_entry
->processed
= 1;
2751 /* BUG: We should only mark src_entry as processed if we
2752 * are not dealing with a rename + add-source case.
2754 ren1
->src_entry
->processed
= 1;
2756 ren1_src
= ren1
->pair
->one
->path
;
2757 ren1_dst
= ren1
->pair
->two
->path
;
2760 /* One file renamed on both sides */
2761 const char *ren2_src
= ren2
->pair
->one
->path
;
2762 const char *ren2_dst
= ren2
->pair
->two
->path
;
2763 enum rename_type rename_type
;
2764 if (strcmp(ren1_src
, ren2_src
) != 0)
2765 BUG("ren1_src != ren2_src");
2766 ren2
->dst_entry
->processed
= 1;
2767 ren2
->processed
= 1;
2768 if (strcmp(ren1_dst
, ren2_dst
) != 0) {
2769 rename_type
= RENAME_ONE_FILE_TO_TWO
;
2772 rename_type
= RENAME_ONE_FILE_TO_ONE
;
2773 /* BUG: We should only remove ren1_src in
2774 * the base stage (think of rename +
2775 * add-source cases).
2777 remove_file(opt
, 1, ren1_src
, 1);
2778 update_entry(ren1
->dst_entry
,
2783 setup_rename_conflict_info(rename_type
, opt
, ren1
, ren2
);
2784 } else if ((lookup
= string_list_lookup(renames2Dst
, ren1_dst
))) {
2785 /* Two different files renamed to the same thing */
2787 ren2
= lookup
->util
;
2788 ren2_dst
= ren2
->pair
->two
->path
;
2789 if (strcmp(ren1_dst
, ren2_dst
) != 0)
2790 BUG("ren1_dst != ren2_dst");
2793 ren2
->processed
= 1;
2795 * BUG: We should only mark src_entry as processed
2796 * if we are not dealing with a rename + add-source
2799 ren2
->src_entry
->processed
= 1;
2801 setup_rename_conflict_info(RENAME_TWO_FILES_TO_ONE
,
2804 /* Renamed in 1, maybe changed in 2 */
2805 /* we only use sha1 and mode of these */
2806 struct diff_filespec src_other
, dst_other
;
2810 * unpack_trees loads entries from common-commit
2811 * into stage 1, from head-commit into stage 2, and
2812 * from merge-commit into stage 3. We keep track
2813 * of which side corresponds to the rename.
2815 int renamed_stage
= a_renames
== renames1
? 2 : 3;
2816 int other_stage
= a_renames
== renames1
? 3 : 2;
2819 * Directory renames have a funny corner case...
2821 int renamed_to_self
= !strcmp(ren1_src
, ren1_dst
);
2823 /* BUG: We should only remove ren1_src in the base
2824 * stage and in other_stage (think of rename +
2827 if (!renamed_to_self
)
2828 remove_file(opt
, 1, ren1_src
,
2829 renamed_stage
== 2 ||
2830 !was_tracked(opt
, ren1_src
));
2832 oidcpy(&src_other
.oid
,
2833 &ren1
->src_entry
->stages
[other_stage
].oid
);
2834 src_other
.mode
= ren1
->src_entry
->stages
[other_stage
].mode
;
2835 oidcpy(&dst_other
.oid
,
2836 &ren1
->dst_entry
->stages
[other_stage
].oid
);
2837 dst_other
.mode
= ren1
->dst_entry
->stages
[other_stage
].mode
;
2840 if (oideq(&src_other
.oid
, null_oid()) &&
2841 ren1
->dir_rename_original_type
== 'A') {
2842 setup_rename_conflict_info(RENAME_VIA_DIR
,
2844 } else if (renamed_to_self
) {
2845 setup_rename_conflict_info(RENAME_NORMAL
,
2847 } else if (oideq(&src_other
.oid
, null_oid())) {
2848 setup_rename_conflict_info(RENAME_DELETE
,
2850 } else if ((dst_other
.mode
== ren1
->pair
->two
->mode
) &&
2851 oideq(&dst_other
.oid
, &ren1
->pair
->two
->oid
)) {
2853 * Added file on the other side identical to
2854 * the file being renamed: clean merge.
2855 * Also, there is no need to overwrite the
2856 * file already in the working copy, so call
2857 * update_file_flags() instead of
2860 if (update_file_flags(opt
,
2863 1, /* update_cache */
2866 } else if (!oideq(&dst_other
.oid
, null_oid())) {
2868 * Probably not a clean merge, but it's
2869 * premature to set clean_merge to 0 here,
2870 * because if the rename merges cleanly and
2871 * the merge exactly matches the newly added
2872 * file, then the merge will be clean.
2874 setup_rename_conflict_info(RENAME_ADD
,
2879 if (clean_merge
< 0)
2880 goto cleanup_and_return
;
2882 struct diff_filespec
*o
, *a
, *b
;
2883 src_other
.path
= (char *)ren1_src
;
2885 o
= ren1
->pair
->one
;
2886 if (a_renames
== renames1
) {
2887 a
= ren1
->pair
->two
;
2890 b
= ren1
->pair
->two
;
2893 update_entry(ren1
->dst_entry
, o
, a
, b
);
2894 setup_rename_conflict_info(RENAME_NORMAL
,
2900 string_list_clear(&a_by_dst
, 0);
2901 string_list_clear(&b_by_dst
, 0);
2906 struct rename_info
{
2907 struct string_list
*head_renames
;
2908 struct string_list
*merge_renames
;
2911 static void initial_cleanup_rename(struct diff_queue_struct
*pairs
,
2912 struct hashmap
*dir_renames
)
2914 struct hashmap_iter iter
;
2915 struct dir_rename_entry
*e
;
2917 hashmap_for_each_entry(dir_renames
, &iter
, e
,
2918 ent
/* member name */) {
2920 strbuf_release(&e
->new_dir
);
2921 /* possible_new_dirs already cleared in get_directory_renames */
2923 hashmap_clear_and_free(dir_renames
, struct dir_rename_entry
, ent
);
2930 static int detect_and_process_renames(struct merge_options
*opt
,
2931 struct tree
*common
,
2934 struct string_list
*entries
,
2935 struct rename_info
*ri
)
2937 struct diff_queue_struct
*head_pairs
, *merge_pairs
;
2938 struct hashmap
*dir_re_head
, *dir_re_merge
;
2941 ri
->head_renames
= NULL
;
2942 ri
->merge_renames
= NULL
;
2944 if (!merge_detect_rename(opt
))
2947 head_pairs
= get_diffpairs(opt
, common
, head
);
2948 merge_pairs
= get_diffpairs(opt
, common
, merge
);
2950 if ((opt
->detect_directory_renames
== MERGE_DIRECTORY_RENAMES_TRUE
) ||
2951 (opt
->detect_directory_renames
== MERGE_DIRECTORY_RENAMES_CONFLICT
&&
2952 !opt
->priv
->call_depth
)) {
2953 dir_re_head
= get_directory_renames(head_pairs
);
2954 dir_re_merge
= get_directory_renames(merge_pairs
);
2956 handle_directory_level_conflicts(opt
,
2958 dir_re_merge
, merge
);
2960 dir_re_head
= xmalloc(sizeof(*dir_re_head
));
2961 dir_re_merge
= xmalloc(sizeof(*dir_re_merge
));
2962 dir_rename_init(dir_re_head
);
2963 dir_rename_init(dir_re_merge
);
2966 ri
->head_renames
= get_renames(opt
, opt
->branch1
, head_pairs
,
2967 dir_re_merge
, dir_re_head
, head
,
2968 common
, head
, merge
, entries
,
2972 ri
->merge_renames
= get_renames(opt
, opt
->branch2
, merge_pairs
,
2973 dir_re_head
, dir_re_merge
, merge
,
2974 common
, head
, merge
, entries
,
2978 clean
&= process_renames(opt
, ri
->head_renames
, ri
->merge_renames
);
2982 * Some cleanup is deferred until cleanup_renames() because the
2983 * data structures are still needed and referenced in
2984 * process_entry(). But there are a few things we can free now.
2986 initial_cleanup_rename(head_pairs
, dir_re_head
);
2987 initial_cleanup_rename(merge_pairs
, dir_re_merge
);
2992 static void final_cleanup_rename(struct string_list
*rename
)
2994 const struct rename
*re
;
3000 for (i
= 0; i
< rename
->nr
; i
++) {
3001 re
= rename
->items
[i
].util
;
3002 diff_free_filepair(re
->pair
);
3004 string_list_clear(rename
, 1);
3008 static void final_cleanup_renames(struct rename_info
*re_info
)
3010 final_cleanup_rename(re_info
->head_renames
);
3011 final_cleanup_rename(re_info
->merge_renames
);
3014 static int read_oid_strbuf(struct merge_options
*opt
,
3015 const struct object_id
*oid
,
3019 enum object_type type
;
3021 buf
= read_object_file(oid
, &type
, &size
);
3023 return err(opt
, _("cannot read object %s"), oid_to_hex(oid
));
3024 if (type
!= OBJ_BLOB
) {
3026 return err(opt
, _("object %s is not a blob"), oid_to_hex(oid
));
3028 strbuf_attach(dst
, buf
, size
, size
+ 1);
3032 static int blob_unchanged(struct merge_options
*opt
,
3033 const struct diff_filespec
*o
,
3034 const struct diff_filespec
*a
,
3035 int renormalize
, const char *path
)
3037 struct strbuf obuf
= STRBUF_INIT
;
3038 struct strbuf abuf
= STRBUF_INIT
;
3039 int ret
= 0; /* assume changed for safety */
3040 struct index_state
*idx
= opt
->repo
->index
;
3042 if (a
->mode
!= o
->mode
)
3044 if (oideq(&o
->oid
, &a
->oid
))
3049 if (read_oid_strbuf(opt
, &o
->oid
, &obuf
) ||
3050 read_oid_strbuf(opt
, &a
->oid
, &abuf
))
3053 * Note: binary | is used so that both renormalizations are
3054 * performed. Comparison can be skipped if both files are
3055 * unchanged since their sha1s have already been compared.
3057 if (renormalize_buffer(idx
, path
, obuf
.buf
, obuf
.len
, &obuf
) |
3058 renormalize_buffer(idx
, path
, abuf
.buf
, abuf
.len
, &abuf
))
3059 ret
= (obuf
.len
== abuf
.len
&& !memcmp(obuf
.buf
, abuf
.buf
, obuf
.len
));
3062 strbuf_release(&obuf
);
3063 strbuf_release(&abuf
);
3067 static int handle_modify_delete(struct merge_options
*opt
,
3069 const struct diff_filespec
*o
,
3070 const struct diff_filespec
*a
,
3071 const struct diff_filespec
*b
)
3073 const char *modify_branch
, *delete_branch
;
3074 const struct diff_filespec
*changed
;
3077 modify_branch
= opt
->branch1
;
3078 delete_branch
= opt
->branch2
;
3081 modify_branch
= opt
->branch2
;
3082 delete_branch
= opt
->branch1
;
3086 return handle_change_delete(opt
,
3089 modify_branch
, delete_branch
,
3090 _("modify"), _("modified"));
3093 static int handle_content_merge(struct merge_file_info
*mfi
,
3094 struct merge_options
*opt
,
3097 const struct diff_filespec
*o
,
3098 const struct diff_filespec
*a
,
3099 const struct diff_filespec
*b
,
3100 struct rename_conflict_info
*ci
)
3102 const char *reason
= _("content");
3103 unsigned df_conflict_remains
= 0;
3106 reason
= _("add/add");
3108 assert(o
->path
&& a
->path
&& b
->path
);
3109 if (ci
&& dir_in_way(opt
->repo
->index
, path
, !opt
->priv
->call_depth
,
3110 S_ISGITLINK(ci
->ren1
->pair
->two
->mode
)))
3111 df_conflict_remains
= 1;
3113 if (merge_mode_and_contents(opt
, o
, a
, b
, path
,
3114 opt
->branch1
, opt
->branch2
,
3115 opt
->priv
->call_depth
* 2, mfi
))
3119 * We can skip updating the working tree file iff:
3120 * a) The merge is clean
3121 * b) The merge matches what was in HEAD (content, mode, pathname)
3122 * c) The target path is usable (i.e. not involved in D/F conflict)
3124 if (mfi
->clean
&& was_tracked_and_matches(opt
, path
, &mfi
->blob
) &&
3125 !df_conflict_remains
) {
3127 struct cache_entry
*ce
;
3129 output(opt
, 3, _("Skipped %s (merged same as existing)"), path
);
3130 if (add_cacheinfo(opt
, &mfi
->blob
, path
,
3131 0, (!opt
->priv
->call_depth
&& !is_dirty
), 0))
3134 * However, add_cacheinfo() will delete the old cache entry
3135 * and add a new one. We need to copy over any skip_worktree
3136 * flag to avoid making the file appear as if it were
3137 * deleted by the user.
3139 pos
= index_name_pos(&opt
->priv
->orig_index
, path
, strlen(path
));
3140 ce
= opt
->priv
->orig_index
.cache
[pos
];
3141 if (ce_skip_worktree(ce
)) {
3142 pos
= index_name_pos(opt
->repo
->index
, path
, strlen(path
));
3143 ce
= opt
->repo
->index
->cache
[pos
];
3144 ce
->ce_flags
|= CE_SKIP_WORKTREE
;
3150 if (S_ISGITLINK(mfi
->blob
.mode
))
3151 reason
= _("submodule");
3152 output(opt
, 1, _("CONFLICT (%s): Merge conflict in %s"),
3154 if (ci
&& !df_conflict_remains
)
3155 if (update_stages(opt
, path
, o
, a
, b
))
3159 if (df_conflict_remains
|| is_dirty
) {
3161 if (opt
->priv
->call_depth
) {
3162 remove_file_from_index(opt
->repo
->index
, path
);
3165 if (update_stages(opt
, path
, o
, a
, b
))
3168 int file_from_stage2
= was_tracked(opt
, path
);
3170 if (update_stages(opt
, path
, NULL
,
3171 file_from_stage2
? &mfi
->blob
: NULL
,
3172 file_from_stage2
? NULL
: &mfi
->blob
))
3177 new_path
= unique_path(opt
, path
, ci
->ren1
->branch
);
3179 output(opt
, 1, _("Refusing to lose dirty file at %s"),
3182 output(opt
, 1, _("Adding as %s instead"), new_path
);
3183 if (update_file(opt
, 0, &mfi
->blob
, new_path
)) {
3189 } else if (update_file(opt
, mfi
->clean
, &mfi
->blob
, path
))
3191 return !is_dirty
&& mfi
->clean
;
3194 static int handle_rename_normal(struct merge_options
*opt
,
3196 const struct diff_filespec
*o
,
3197 const struct diff_filespec
*a
,
3198 const struct diff_filespec
*b
,
3199 struct rename_conflict_info
*ci
)
3201 struct rename
*ren
= ci
->ren1
;
3202 struct merge_file_info mfi
;
3205 /* Merge the content and write it out */
3206 clean
= handle_content_merge(&mfi
, opt
, path
, was_dirty(opt
, path
),
3210 opt
->detect_directory_renames
== MERGE_DIRECTORY_RENAMES_CONFLICT
&&
3211 ren
->dir_rename_original_dest
) {
3212 if (update_stages(opt
, path
,
3213 &mfi
.blob
, &mfi
.blob
, &mfi
.blob
))
3215 clean
= 0; /* not clean, but conflicted */
3220 static void dir_rename_warning(const char *msg
,
3223 struct merge_options
*opt
,
3226 const char *other_branch
;
3227 other_branch
= (ren
->branch
== opt
->branch1
?
3228 opt
->branch2
: opt
->branch1
);
3230 output(opt
, clean
? 2 : 1, msg
,
3231 ren
->pair
->one
->path
, ren
->branch
,
3232 other_branch
, ren
->pair
->two
->path
);
3235 output(opt
, clean
? 2 : 1, msg
,
3236 ren
->pair
->one
->path
, ren
->dir_rename_original_dest
, ren
->branch
,
3237 other_branch
, ren
->pair
->two
->path
);
3239 static int warn_about_dir_renamed_entries(struct merge_options
*opt
,
3243 int clean
= 1, is_add
;
3248 /* Return early if ren was not affected/created by a directory rename */
3249 if (!ren
->dir_rename_original_dest
)
3253 assert(opt
->detect_directory_renames
> MERGE_DIRECTORY_RENAMES_NONE
);
3254 assert(ren
->dir_rename_original_type
== 'A' ||
3255 ren
->dir_rename_original_type
== 'R');
3257 /* Check whether to treat directory renames as a conflict */
3258 clean
= (opt
->detect_directory_renames
== MERGE_DIRECTORY_RENAMES_TRUE
);
3260 is_add
= (ren
->dir_rename_original_type
== 'A');
3261 if (ren
->dir_rename_original_type
== 'A' && clean
) {
3262 msg
= _("Path updated: %s added in %s inside a "
3263 "directory that was renamed in %s; moving it to %s.");
3264 } else if (ren
->dir_rename_original_type
== 'A' && !clean
) {
3265 msg
= _("CONFLICT (file location): %s added in %s "
3266 "inside a directory that was renamed in %s, "
3267 "suggesting it should perhaps be moved to %s.");
3268 } else if (ren
->dir_rename_original_type
== 'R' && clean
) {
3269 msg
= _("Path updated: %s renamed to %s in %s, inside a "
3270 "directory that was renamed in %s; moving it to %s.");
3271 } else if (ren
->dir_rename_original_type
== 'R' && !clean
) {
3272 msg
= _("CONFLICT (file location): %s renamed to %s in %s, "
3273 "inside a directory that was renamed in %s, "
3274 "suggesting it should perhaps be moved to %s.");
3276 BUG("Impossible dir_rename_original_type/clean combination");
3278 dir_rename_warning(msg
, is_add
, clean
, opt
, ren
);
3283 /* Per entry merge function */
3284 static int process_entry(struct merge_options
*opt
,
3285 const char *path
, struct stage_data
*entry
)
3287 int clean_merge
= 1;
3288 int normalize
= opt
->renormalize
;
3290 struct diff_filespec
*o
= &entry
->stages
[1];
3291 struct diff_filespec
*a
= &entry
->stages
[2];
3292 struct diff_filespec
*b
= &entry
->stages
[3];
3293 int o_valid
= is_valid(o
);
3294 int a_valid
= is_valid(a
);
3295 int b_valid
= is_valid(b
);
3296 o
->path
= a
->path
= b
->path
= (char*)path
;
3298 entry
->processed
= 1;
3299 if (entry
->rename_conflict_info
) {
3300 struct rename_conflict_info
*ci
= entry
->rename_conflict_info
;
3301 struct diff_filespec
*temp
;
3304 path_clean
= warn_about_dir_renamed_entries(opt
, ci
->ren1
);
3305 path_clean
&= warn_about_dir_renamed_entries(opt
, ci
->ren2
);
3308 * For cases with a single rename, {o,a,b}->path have all been
3309 * set to the rename target path; we need to set two of these
3310 * back to the rename source.
3311 * For rename/rename conflicts, we'll manually fix paths below.
3313 temp
= (opt
->branch1
== ci
->ren1
->branch
) ? b
: a
;
3314 o
->path
= temp
->path
= ci
->ren1
->pair
->one
->path
;
3316 assert(opt
->branch1
== ci
->ren1
->branch
);
3319 switch (ci
->rename_type
) {
3321 case RENAME_ONE_FILE_TO_ONE
:
3322 clean_merge
= handle_rename_normal(opt
, path
, o
, a
, b
,
3325 case RENAME_VIA_DIR
:
3326 clean_merge
= handle_rename_via_dir(opt
, ci
);
3330 * Probably unclean merge, but if the renamed file
3331 * merges cleanly and the result can then be
3332 * two-way merged cleanly with the added file, I
3333 * guess it's a clean merge?
3335 clean_merge
= handle_rename_add(opt
, ci
);
3339 if (handle_rename_delete(opt
, ci
))
3342 case RENAME_ONE_FILE_TO_TWO
:
3344 * Manually fix up paths; note:
3345 * ren[12]->pair->one->path are equal.
3347 o
->path
= ci
->ren1
->pair
->one
->path
;
3348 a
->path
= ci
->ren1
->pair
->two
->path
;
3349 b
->path
= ci
->ren2
->pair
->two
->path
;
3352 if (handle_rename_rename_1to2(opt
, ci
))
3355 case RENAME_TWO_FILES_TO_ONE
:
3357 * Manually fix up paths; note,
3358 * ren[12]->pair->two->path are actually equal.
3361 a
->path
= ci
->ren1
->pair
->two
->path
;
3362 b
->path
= ci
->ren2
->pair
->two
->path
;
3365 * Probably unclean merge, but if the two renamed
3366 * files merge cleanly and the two resulting files
3367 * can then be two-way merged cleanly, I guess it's
3370 clean_merge
= handle_rename_rename_2to1(opt
, ci
);
3373 entry
->processed
= 0;
3376 if (path_clean
< clean_merge
)
3377 clean_merge
= path_clean
;
3378 } else if (o_valid
&& (!a_valid
|| !b_valid
)) {
3379 /* Case A: Deleted in one */
3380 if ((!a_valid
&& !b_valid
) ||
3381 (!b_valid
&& blob_unchanged(opt
, o
, a
, normalize
, path
)) ||
3382 (!a_valid
&& blob_unchanged(opt
, o
, b
, normalize
, path
))) {
3383 /* Deleted in both or deleted in one and
3384 * unchanged in the other */
3386 output(opt
, 2, _("Removing %s"), path
);
3387 /* do not touch working file if it did not exist */
3388 remove_file(opt
, 1, path
, !a_valid
);
3390 /* Modify/delete; deleted side may have put a directory in the way */
3392 if (handle_modify_delete(opt
, path
, o
, a
, b
))
3395 } else if ((!o_valid
&& a_valid
&& !b_valid
) ||
3396 (!o_valid
&& !a_valid
&& b_valid
)) {
3397 /* Case B: Added in one. */
3398 /* [nothing|directory] -> ([nothing|directory], file) */
3400 const char *add_branch
;
3401 const char *other_branch
;
3403 const struct diff_filespec
*contents
;
3406 add_branch
= opt
->branch1
;
3407 other_branch
= opt
->branch2
;
3409 conf
= _("file/directory");
3411 add_branch
= opt
->branch2
;
3412 other_branch
= opt
->branch1
;
3414 conf
= _("directory/file");
3416 if (dir_in_way(opt
->repo
->index
, path
,
3417 !opt
->priv
->call_depth
&& !S_ISGITLINK(a
->mode
),
3419 char *new_path
= unique_path(opt
, path
, add_branch
);
3421 output(opt
, 1, _("CONFLICT (%s): There is a directory with name %s in %s. "
3423 conf
, path
, other_branch
, path
, new_path
);
3424 if (update_file(opt
, 0, contents
, new_path
))
3426 else if (opt
->priv
->call_depth
)
3427 remove_file_from_index(opt
->repo
->index
, path
);
3430 output(opt
, 2, _("Adding %s"), path
);
3431 /* do not overwrite file if already present */
3432 if (update_file_flags(opt
, contents
, path
, 1, !a_valid
))
3435 } else if (a_valid
&& b_valid
) {
3437 /* Case C: Added in both (check for same permissions) */
3439 _("CONFLICT (add/add): Merge conflict in %s"),
3441 clean_merge
= handle_file_collision(opt
,
3447 /* case D: Modified in both, but differently. */
3448 struct merge_file_info mfi
;
3449 int is_dirty
= 0; /* unpack_trees would have bailed if dirty */
3450 clean_merge
= handle_content_merge(&mfi
, opt
, path
,
3454 } else if (!o_valid
&& !a_valid
&& !b_valid
) {
3456 * this entry was deleted altogether. a_mode == 0 means
3457 * we had that path and want to actively remove it.
3459 remove_file(opt
, 1, path
, !a
->mode
);
3461 BUG("fatal merge failure, shouldn't happen.");
3466 static int merge_trees_internal(struct merge_options
*opt
,
3469 struct tree
*merge_base
,
3470 struct tree
**result
)
3472 struct index_state
*istate
= opt
->repo
->index
;
3475 if (opt
->subtree_shift
) {
3476 merge
= shift_tree_object(opt
->repo
, head
, merge
,
3477 opt
->subtree_shift
);
3478 merge_base
= shift_tree_object(opt
->repo
, head
, merge_base
,
3479 opt
->subtree_shift
);
3482 if (oideq(&merge_base
->object
.oid
, &merge
->object
.oid
)) {
3483 output(opt
, 0, _("Already up to date."));
3488 code
= unpack_trees_start(opt
, merge_base
, head
, merge
);
3491 if (show(opt
, 4) || opt
->priv
->call_depth
)
3492 err(opt
, _("merging of trees %s and %s failed"),
3493 oid_to_hex(&head
->object
.oid
),
3494 oid_to_hex(&merge
->object
.oid
));
3495 unpack_trees_finish(opt
);
3499 if (unmerged_index(istate
)) {
3500 struct string_list
*entries
;
3501 struct rename_info re_info
;
3504 * Only need the hashmap while processing entries, so
3505 * initialize it here and free it when we are done running
3506 * through the entries. Keeping it in the merge_options as
3507 * opposed to decaring a local hashmap is for convenience
3508 * so that we don't have to pass it to around.
3510 hashmap_init(&opt
->priv
->current_file_dir_set
, path_hashmap_cmp
,
3512 get_files_dirs(opt
, head
);
3513 get_files_dirs(opt
, merge
);
3515 entries
= get_unmerged(opt
->repo
->index
);
3516 clean
= detect_and_process_renames(opt
, merge_base
, head
, merge
,
3518 record_df_conflict_files(opt
, entries
);
3521 for (i
= entries
->nr
-1; 0 <= i
; i
--) {
3522 const char *path
= entries
->items
[i
].string
;
3523 struct stage_data
*e
= entries
->items
[i
].util
;
3524 if (!e
->processed
) {
3525 int ret
= process_entry(opt
, path
, e
);
3534 for (i
= 0; i
< entries
->nr
; i
++) {
3535 struct stage_data
*e
= entries
->items
[i
].util
;
3537 BUG("unprocessed path??? %s",
3538 entries
->items
[i
].string
);
3542 final_cleanup_renames(&re_info
);
3544 string_list_clear(entries
, 1);
3547 hashmap_clear_and_free(&opt
->priv
->current_file_dir_set
,
3548 struct path_hashmap_entry
, e
);
3551 unpack_trees_finish(opt
);
3558 unpack_trees_finish(opt
);
3560 if (opt
->priv
->call_depth
&&
3561 !(*result
= write_in_core_index_as_tree(opt
->repo
)))
3568 * Merge the commits h1 and h2, returning a flag (int) indicating the
3569 * cleanness of the merge. Also, if opt->priv->call_depth, create a
3570 * virtual commit and write its location to *result.
3572 static int merge_recursive_internal(struct merge_options
*opt
,
3575 struct commit_list
*merge_bases
,
3576 struct commit
**result
)
3578 struct commit_list
*iter
;
3579 struct commit
*merged_merge_bases
;
3580 struct tree
*result_tree
;
3582 const char *ancestor_name
;
3583 struct strbuf merge_base_abbrev
= STRBUF_INIT
;
3586 output(opt
, 4, _("Merging:"));
3587 output_commit_title(opt
, h1
);
3588 output_commit_title(opt
, h2
);
3592 merge_bases
= get_merge_bases(h1
, h2
);
3593 merge_bases
= reverse_commit_list(merge_bases
);
3597 unsigned cnt
= commit_list_count(merge_bases
);
3599 output(opt
, 5, Q_("found %u common ancestor:",
3600 "found %u common ancestors:", cnt
), cnt
);
3601 for (iter
= merge_bases
; iter
; iter
= iter
->next
)
3602 output_commit_title(opt
, iter
->item
);
3605 merged_merge_bases
= pop_commit(&merge_bases
);
3606 if (merged_merge_bases
== NULL
) {
3607 /* if there is no common ancestor, use an empty tree */
3610 tree
= lookup_tree(opt
->repo
, opt
->repo
->hash_algo
->empty_tree
);
3611 merged_merge_bases
= make_virtual_commit(opt
->repo
, tree
,
3613 ancestor_name
= "empty tree";
3614 } else if (opt
->ancestor
&& !opt
->priv
->call_depth
) {
3615 ancestor_name
= opt
->ancestor
;
3616 } else if (merge_bases
) {
3617 ancestor_name
= "merged common ancestors";
3619 strbuf_add_unique_abbrev(&merge_base_abbrev
,
3620 &merged_merge_bases
->object
.oid
,
3622 ancestor_name
= merge_base_abbrev
.buf
;
3625 for (iter
= merge_bases
; iter
; iter
= iter
->next
) {
3626 const char *saved_b1
, *saved_b2
;
3627 opt
->priv
->call_depth
++;
3629 * When the merge fails, the result contains files
3630 * with conflict markers. The cleanness flag is
3631 * ignored (unless indicating an error), it was never
3632 * actually used, as result of merge_trees has always
3633 * overwritten it: the committed "conflicts" were
3636 discard_index(opt
->repo
->index
);
3637 saved_b1
= opt
->branch1
;
3638 saved_b2
= opt
->branch2
;
3639 opt
->branch1
= "Temporary merge branch 1";
3640 opt
->branch2
= "Temporary merge branch 2";
3641 if (merge_recursive_internal(opt
, merged_merge_bases
, iter
->item
,
3642 NULL
, &merged_merge_bases
) < 0)
3644 opt
->branch1
= saved_b1
;
3645 opt
->branch2
= saved_b2
;
3646 opt
->priv
->call_depth
--;
3648 if (!merged_merge_bases
)
3649 return err(opt
, _("merge returned no commit"));
3653 * FIXME: Since merge_recursive_internal() is only ever called by
3654 * places that ensure the index is loaded first
3655 * (e.g. builtin/merge.c, rebase/sequencer, etc.), in the common
3656 * case where the merge base was unique that means when we get here
3657 * we immediately discard the index and re-read it, which is a
3658 * complete waste of time. We should only be discarding and
3659 * re-reading if we were forced to recurse.
3661 discard_index(opt
->repo
->index
);
3662 if (!opt
->priv
->call_depth
)
3663 repo_read_index(opt
->repo
);
3665 opt
->ancestor
= ancestor_name
;
3666 clean
= merge_trees_internal(opt
,
3667 repo_get_commit_tree(opt
->repo
, h1
),
3668 repo_get_commit_tree(opt
->repo
, h2
),
3669 repo_get_commit_tree(opt
->repo
,
3670 merged_merge_bases
),
3672 strbuf_release(&merge_base_abbrev
);
3673 opt
->ancestor
= NULL
; /* avoid accidental re-use of opt->ancestor */
3679 if (opt
->priv
->call_depth
) {
3680 *result
= make_virtual_commit(opt
->repo
, result_tree
,
3682 commit_list_insert(h1
, &(*result
)->parents
);
3683 commit_list_insert(h2
, &(*result
)->parents
->next
);
3688 static int merge_start(struct merge_options
*opt
, struct tree
*head
)
3690 struct strbuf sb
= STRBUF_INIT
;
3692 /* Sanity checks on opt */
3695 assert(opt
->branch1
&& opt
->branch2
);
3697 assert(opt
->detect_renames
>= -1 &&
3698 opt
->detect_renames
<= DIFF_DETECT_COPY
);
3699 assert(opt
->detect_directory_renames
>= MERGE_DIRECTORY_RENAMES_NONE
&&
3700 opt
->detect_directory_renames
<= MERGE_DIRECTORY_RENAMES_TRUE
);
3701 assert(opt
->rename_limit
>= -1);
3702 assert(opt
->rename_score
>= 0 && opt
->rename_score
<= MAX_SCORE
);
3703 assert(opt
->show_rename_progress
>= 0 && opt
->show_rename_progress
<= 1);
3705 assert(opt
->xdl_opts
>= 0);
3706 assert(opt
->recursive_variant
>= MERGE_VARIANT_NORMAL
&&
3707 opt
->recursive_variant
<= MERGE_VARIANT_THEIRS
);
3709 assert(opt
->verbosity
>= 0 && opt
->verbosity
<= 5);
3710 assert(opt
->buffer_output
<= 2);
3711 assert(opt
->obuf
.len
== 0);
3713 assert(opt
->priv
== NULL
);
3715 /* Sanity check on repo state; index must match head */
3716 if (repo_index_has_changes(opt
->repo
, head
, &sb
)) {
3717 err(opt
, _("Your local changes to the following files would be overwritten by merge:\n %s"),
3719 strbuf_release(&sb
);
3723 CALLOC_ARRAY(opt
->priv
, 1);
3724 string_list_init_dup(&opt
->priv
->df_conflict_file_set
);
3728 static void merge_finalize(struct merge_options
*opt
)
3731 if (!opt
->priv
->call_depth
&& opt
->buffer_output
< 2)
3732 strbuf_release(&opt
->obuf
);
3734 diff_warn_rename_limit("merge.renamelimit",
3735 opt
->priv
->needed_rename_limit
, 0);
3736 FREE_AND_NULL(opt
->priv
);
3739 int merge_trees(struct merge_options
*opt
,
3742 struct tree
*merge_base
)
3745 struct tree
*ignored
;
3747 assert(opt
->ancestor
!= NULL
);
3749 if (merge_start(opt
, head
))
3751 clean
= merge_trees_internal(opt
, head
, merge
, merge_base
, &ignored
);
3752 merge_finalize(opt
);
3757 int merge_recursive(struct merge_options
*opt
,
3760 struct commit_list
*merge_bases
,
3761 struct commit
**result
)
3765 assert(opt
->ancestor
== NULL
||
3766 !strcmp(opt
->ancestor
, "constructed merge base"));
3768 prepare_repo_settings(opt
->repo
);
3769 opt
->repo
->settings
.command_requires_full_index
= 1;
3771 if (merge_start(opt
, repo_get_commit_tree(opt
->repo
, h1
)))
3773 clean
= merge_recursive_internal(opt
, h1
, h2
, merge_bases
, result
);
3774 merge_finalize(opt
);
3779 static struct commit
*get_ref(struct repository
*repo
,
3780 const struct object_id
*oid
,
3783 struct object
*object
;
3785 object
= deref_tag(repo
, parse_object(repo
, oid
),
3786 name
, strlen(name
));
3789 if (object
->type
== OBJ_TREE
)
3790 return make_virtual_commit(repo
, (struct tree
*)object
, name
);
3791 if (object
->type
!= OBJ_COMMIT
)
3793 if (parse_commit((struct commit
*)object
))
3795 return (struct commit
*)object
;
3798 int merge_recursive_generic(struct merge_options
*opt
,
3799 const struct object_id
*head
,
3800 const struct object_id
*merge
,
3801 int num_merge_bases
,
3802 const struct object_id
**merge_bases
,
3803 struct commit
**result
)
3806 struct lock_file lock
= LOCK_INIT
;
3807 struct commit
*head_commit
= get_ref(opt
->repo
, head
, opt
->branch1
);
3808 struct commit
*next_commit
= get_ref(opt
->repo
, merge
, opt
->branch2
);
3809 struct commit_list
*ca
= NULL
;
3813 for (i
= 0; i
< num_merge_bases
; ++i
) {
3814 struct commit
*base
;
3815 if (!(base
= get_ref(opt
->repo
, merge_bases
[i
],
3816 oid_to_hex(merge_bases
[i
]))))
3817 return err(opt
, _("Could not parse object '%s'"),
3818 oid_to_hex(merge_bases
[i
]));
3819 commit_list_insert(base
, &ca
);
3821 if (num_merge_bases
== 1)
3822 opt
->ancestor
= "constructed merge base";
3825 repo_hold_locked_index(opt
->repo
, &lock
, LOCK_DIE_ON_ERROR
);
3826 clean
= merge_recursive(opt
, head_commit
, next_commit
, ca
,
3829 rollback_lock_file(&lock
);
3833 if (write_locked_index(opt
->repo
->index
, &lock
,
3834 COMMIT_LOCK
| SKIP_IF_UNCHANGED
))
3835 return err(opt
, _("Unable to write index."));
3837 return clean
? 0 : 1;
3840 static void merge_recursive_config(struct merge_options
*opt
)
3843 int renormalize
= 0;
3844 git_config_get_int("merge.verbosity", &opt
->verbosity
);
3845 git_config_get_int("diff.renamelimit", &opt
->rename_limit
);
3846 git_config_get_int("merge.renamelimit", &opt
->rename_limit
);
3847 git_config_get_bool("merge.renormalize", &renormalize
);
3848 opt
->renormalize
= renormalize
;
3849 if (!git_config_get_string("diff.renames", &value
)) {
3850 opt
->detect_renames
= git_config_rename("diff.renames", value
);
3853 if (!git_config_get_string("merge.renames", &value
)) {
3854 opt
->detect_renames
= git_config_rename("merge.renames", value
);
3857 if (!git_config_get_string("merge.directoryrenames", &value
)) {
3858 int boolval
= git_parse_maybe_bool(value
);
3860 opt
->detect_directory_renames
= boolval
?
3861 MERGE_DIRECTORY_RENAMES_TRUE
:
3862 MERGE_DIRECTORY_RENAMES_NONE
;
3863 } else if (!strcasecmp(value
, "conflict")) {
3864 opt
->detect_directory_renames
=
3865 MERGE_DIRECTORY_RENAMES_CONFLICT
;
3866 } /* avoid erroring on values from future versions of git */
3869 git_config(git_xmerge_config
, NULL
);
3872 void init_merge_options(struct merge_options
*opt
,
3873 struct repository
*repo
)
3875 const char *merge_verbosity
;
3876 memset(opt
, 0, sizeof(struct merge_options
));
3880 opt
->detect_renames
= -1;
3881 opt
->detect_directory_renames
= MERGE_DIRECTORY_RENAMES_CONFLICT
;
3882 opt
->rename_limit
= -1;
3885 opt
->buffer_output
= 1;
3886 strbuf_init(&opt
->obuf
, 0);
3888 opt
->renormalize
= 0;
3890 merge_recursive_config(opt
);
3891 merge_verbosity
= getenv("GIT_MERGE_VERBOSITY");
3892 if (merge_verbosity
)
3893 opt
->verbosity
= strtol(merge_verbosity
, NULL
, 10);
3894 if (opt
->verbosity
>= 5)
3895 opt
->buffer_output
= 0;
3898 int parse_merge_opt(struct merge_options
*opt
, const char *s
)
3904 if (!strcmp(s
, "ours"))
3905 opt
->recursive_variant
= MERGE_VARIANT_OURS
;
3906 else if (!strcmp(s
, "theirs"))
3907 opt
->recursive_variant
= MERGE_VARIANT_THEIRS
;
3908 else if (!strcmp(s
, "subtree"))
3909 opt
->subtree_shift
= "";
3910 else if (skip_prefix(s
, "subtree=", &arg
))
3911 opt
->subtree_shift
= arg
;
3912 else if (!strcmp(s
, "patience"))
3913 opt
->xdl_opts
= DIFF_WITH_ALG(opt
, PATIENCE_DIFF
);
3914 else if (!strcmp(s
, "histogram"))
3915 opt
->xdl_opts
= DIFF_WITH_ALG(opt
, HISTOGRAM_DIFF
);
3916 else if (skip_prefix(s
, "diff-algorithm=", &arg
)) {
3917 long value
= parse_algorithm_value(arg
);
3920 /* clear out previous settings */
3921 DIFF_XDL_CLR(opt
, NEED_MINIMAL
);
3922 opt
->xdl_opts
&= ~XDF_DIFF_ALGORITHM_MASK
;
3923 opt
->xdl_opts
|= value
;
3925 else if (!strcmp(s
, "ignore-space-change"))
3926 DIFF_XDL_SET(opt
, IGNORE_WHITESPACE_CHANGE
);
3927 else if (!strcmp(s
, "ignore-all-space"))
3928 DIFF_XDL_SET(opt
, IGNORE_WHITESPACE
);
3929 else if (!strcmp(s
, "ignore-space-at-eol"))
3930 DIFF_XDL_SET(opt
, IGNORE_WHITESPACE_AT_EOL
);
3931 else if (!strcmp(s
, "ignore-cr-at-eol"))
3932 DIFF_XDL_SET(opt
, IGNORE_CR_AT_EOL
);
3933 else if (!strcmp(s
, "renormalize"))
3934 opt
->renormalize
= 1;
3935 else if (!strcmp(s
, "no-renormalize"))
3936 opt
->renormalize
= 0;
3937 else if (!strcmp(s
, "no-renames"))
3938 opt
->detect_renames
= 0;
3939 else if (!strcmp(s
, "find-renames")) {
3940 opt
->detect_renames
= 1;
3941 opt
->rename_score
= 0;
3943 else if (skip_prefix(s
, "find-renames=", &arg
) ||
3944 skip_prefix(s
, "rename-threshold=", &arg
)) {
3945 if ((opt
->rename_score
= parse_rename_score(&arg
)) == -1 || *arg
!= 0)
3947 opt
->detect_renames
= 1;
3950 * Please update $__git_merge_strategy_options in
3951 * git-completion.bash when you add new options