Skip to content

Commit 66b209b

Browse files
newrengitster
authored andcommitted
merge-ort: implement CE_SKIP_WORKTREE handling with conflicted entries
When merge conflicts occur in paths removed by a sparse-checkout, we need to unsparsify those paths (clear the SKIP_WORKTREE bit), and write out the conflicted file to the working copy. In the very unlikely case that someone manually put a file into the working copy at the location of the SKIP_WORKTREE file, we need to avoid overwriting whatever edits they have made and move that file to a different location first. Signed-off-by: Elijah Newren <[email protected]> Reviewed-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8ddc20b commit 66b209b

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

merge-ort.c

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3369,23 +3369,27 @@ static int checkout(struct merge_options *opt,
33693369
return ret;
33703370
}
33713371

3372-
static int record_conflicted_index_entries(struct merge_options *opt,
3373-
struct index_state *index,
3374-
struct strmap *paths,
3375-
struct strmap *conflicted)
3372+
static int record_conflicted_index_entries(struct merge_options *opt)
33763373
{
33773374
struct hashmap_iter iter;
33783375
struct strmap_entry *e;
3376+
struct index_state *index = opt->repo->index;
3377+
struct checkout state = CHECKOUT_INIT;
33793378
int errs = 0;
33803379
int original_cache_nr;
33813380

3382-
if (strmap_empty(conflicted))
3381+
if (strmap_empty(&opt->priv->conflicted))
33833382
return 0;
33843383

3384+
/* If any entries have skip_worktree set, we'll have to check 'em out */
3385+
state.force = 1;
3386+
state.quiet = 1;
3387+
state.refresh_cache = 1;
3388+
state.istate = index;
33853389
original_cache_nr = index->cache_nr;
33863390

33873391
/* Put every entry from paths into plist, then sort */
3388-
strmap_for_each_entry(conflicted, &iter, e) {
3392+
strmap_for_each_entry(&opt->priv->conflicted, &iter, e) {
33893393
const char *path = e->key;
33903394
struct conflict_info *ci = e->value;
33913395
int pos;
@@ -3426,9 +3430,23 @@ static int record_conflicted_index_entries(struct merge_options *opt,
34263430
* the higher order stages. Thus, we need override
34273431
* the CE_SKIP_WORKTREE bit and manually write those
34283432
* files to the working disk here.
3429-
*
3430-
* TODO: Implement this CE_SKIP_WORKTREE fixup.
34313433
*/
3434+
if (ce_skip_worktree(ce)) {
3435+
struct stat st;
3436+
3437+
if (!lstat(path, &st)) {
3438+
char *new_name = unique_path(&opt->priv->paths,
3439+
path,
3440+
"cruft");
3441+
3442+
path_msg(opt, path, 1,
3443+
_("Note: %s not up to date and in way of checking out conflicted version; old copy renamed to %s"),
3444+
path, new_name);
3445+
errs |= rename(path, new_name);
3446+
free(new_name);
3447+
}
3448+
errs |= checkout_entry(ce, &state, NULL, NULL);
3449+
}
34323450

34333451
/*
34343452
* Mark this cache entry for removal and instead add
@@ -3478,8 +3496,6 @@ void merge_switch_to_result(struct merge_options *opt,
34783496
{
34793497
assert(opt->priv == NULL);
34803498
if (result->clean >= 0 && update_worktree_and_index) {
3481-
struct merge_options_internal *opti = result->priv;
3482-
34833499
trace2_region_enter("merge", "checkout", opt->repo);
34843500
if (checkout(opt, head, result->tree)) {
34853501
/* failure to function */
@@ -3489,13 +3505,14 @@ void merge_switch_to_result(struct merge_options *opt,
34893505
trace2_region_leave("merge", "checkout", opt->repo);
34903506

34913507
trace2_region_enter("merge", "record_conflicted", opt->repo);
3492-
if (record_conflicted_index_entries(opt, opt->repo->index,
3493-
&opti->paths,
3494-
&opti->conflicted)) {
3508+
opt->priv = result->priv;
3509+
if (record_conflicted_index_entries(opt)) {
34953510
/* failure to function */
3511+
opt->priv = NULL;
34963512
result->clean = -1;
34973513
return;
34983514
}
3515+
opt->priv = NULL;
34993516
trace2_region_leave("merge", "record_conflicted", opt->repo);
35003517
}
35013518

t/t6428-merge-conflicts-sparse.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ test_setup_numerals () {
7676
)
7777
}
7878

79-
test_expect_merge_algorithm success failure 'conflicting entries written to worktree even if sparse' '
79+
test_expect_success 'conflicting entries written to worktree even if sparse' '
8080
test_setup_numerals plain &&
8181
(
8282
cd numerals_plain &&
@@ -112,7 +112,7 @@ test_expect_merge_algorithm success failure 'conflicting entries written to work
112112
)
113113
'
114114

115-
test_expect_merge_algorithm failure failure 'present-despite-SKIP_WORKTREE handled reasonably' '
115+
test_expect_merge_algorithm failure success 'present-despite-SKIP_WORKTREE handled reasonably' '
116116
test_setup_numerals in_the_way &&
117117
(
118118
cd numerals_in_the_way &&

0 commit comments

Comments
 (0)