Merge branch 'jc/checkout-m-twoway' into maint
* jc/checkout-m-twoway: t/t2023-checkout-m.sh: fix use of test_must_fail checkout_merged(): squelch false warning from some gcc Test 'checkout -m -- path' checkout -m: no need to insist on having all 3 stages
This commit is contained in:
commit
9b0b0b4f45
@ -114,16 +114,21 @@ static int check_stage(int stage, struct cache_entry *ce, int pos)
|
|||||||
return error(_("path '%s' does not have their version"), ce->name);
|
return error(_("path '%s' does not have their version"), ce->name);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int check_all_stages(struct cache_entry *ce, int pos)
|
static int check_stages(unsigned stages, struct cache_entry *ce, int pos)
|
||||||
{
|
{
|
||||||
if (ce_stage(ce) != 1 ||
|
unsigned seen = 0;
|
||||||
active_nr <= pos + 2 ||
|
const char *name = ce->name;
|
||||||
strcmp(active_cache[pos+1]->name, ce->name) ||
|
|
||||||
ce_stage(active_cache[pos+1]) != 2 ||
|
while (pos < active_nr) {
|
||||||
strcmp(active_cache[pos+2]->name, ce->name) ||
|
ce = active_cache[pos];
|
||||||
ce_stage(active_cache[pos+2]) != 3)
|
if (strcmp(name, ce->name))
|
||||||
return error(_("path '%s' does not have all three versions"),
|
break;
|
||||||
ce->name);
|
seen |= (1 << ce_stage(ce));
|
||||||
|
pos++;
|
||||||
|
}
|
||||||
|
if ((stages & seen) != stages)
|
||||||
|
return error(_("path '%s' does not have all necessary versions"),
|
||||||
|
name);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -150,18 +155,27 @@ static int checkout_merged(int pos, struct checkout *state)
|
|||||||
int status;
|
int status;
|
||||||
unsigned char sha1[20];
|
unsigned char sha1[20];
|
||||||
mmbuffer_t result_buf;
|
mmbuffer_t result_buf;
|
||||||
|
unsigned char threeway[3][20];
|
||||||
|
unsigned mode = 0;
|
||||||
|
|
||||||
if (ce_stage(ce) != 1 ||
|
memset(threeway, 0, sizeof(threeway));
|
||||||
active_nr <= pos + 2 ||
|
while (pos < active_nr) {
|
||||||
strcmp(active_cache[pos+1]->name, path) ||
|
int stage;
|
||||||
ce_stage(active_cache[pos+1]) != 2 ||
|
stage = ce_stage(ce);
|
||||||
strcmp(active_cache[pos+2]->name, path) ||
|
if (!stage || strcmp(path, ce->name))
|
||||||
ce_stage(active_cache[pos+2]) != 3)
|
break;
|
||||||
return error(_("path '%s' does not have all 3 versions"), path);
|
hashcpy(threeway[stage - 1], ce->sha1);
|
||||||
|
if (stage == 2)
|
||||||
|
mode = create_ce_mode(ce->ce_mode);
|
||||||
|
pos++;
|
||||||
|
ce = active_cache[pos];
|
||||||
|
}
|
||||||
|
if (is_null_sha1(threeway[1]) || is_null_sha1(threeway[2]))
|
||||||
|
return error(_("path '%s' does not have necessary versions"), path);
|
||||||
|
|
||||||
read_mmblob(&ancestor, active_cache[pos]->sha1);
|
read_mmblob(&ancestor, threeway[0]);
|
||||||
read_mmblob(&ours, active_cache[pos+1]->sha1);
|
read_mmblob(&ours, threeway[1]);
|
||||||
read_mmblob(&theirs, active_cache[pos+2]->sha1);
|
read_mmblob(&theirs, threeway[2]);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* NEEDSWORK: re-create conflicts from merges with
|
* NEEDSWORK: re-create conflicts from merges with
|
||||||
@ -192,9 +206,7 @@ static int checkout_merged(int pos, struct checkout *state)
|
|||||||
if (write_sha1_file(result_buf.ptr, result_buf.size,
|
if (write_sha1_file(result_buf.ptr, result_buf.size,
|
||||||
blob_type, sha1))
|
blob_type, sha1))
|
||||||
die(_("Unable to add merge result for '%s'"), path);
|
die(_("Unable to add merge result for '%s'"), path);
|
||||||
ce = make_cache_entry(create_ce_mode(active_cache[pos+1]->ce_mode),
|
ce = make_cache_entry(mode, sha1, path, 2, 0);
|
||||||
sha1,
|
|
||||||
path, 2, 0);
|
|
||||||
if (!ce)
|
if (!ce)
|
||||||
die(_("make_cache_entry failed for path '%s'"), path);
|
die(_("make_cache_entry failed for path '%s'"), path);
|
||||||
status = checkout_entry(ce, state, NULL);
|
status = checkout_entry(ce, state, NULL);
|
||||||
@ -252,7 +264,7 @@ static int checkout_paths(struct tree *source_tree, const char **pathspec,
|
|||||||
} else if (stage) {
|
} else if (stage) {
|
||||||
errs |= check_stage(stage, ce, pos);
|
errs |= check_stage(stage, ce, pos);
|
||||||
} else if (opts->merge) {
|
} else if (opts->merge) {
|
||||||
errs |= check_all_stages(ce, pos);
|
errs |= check_stages((1<<2) | (1<<3), ce, pos);
|
||||||
} else {
|
} else {
|
||||||
errs = 1;
|
errs = 1;
|
||||||
error(_("path '%s' is unmerged"), ce->name);
|
error(_("path '%s' is unmerged"), ce->name);
|
||||||
|
49
t/t2023-checkout-m.sh
Executable file
49
t/t2023-checkout-m.sh
Executable file
@ -0,0 +1,49 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
test_description='checkout -m -- <conflicted path>
|
||||||
|
|
||||||
|
Ensures that checkout -m on a resolved file restores the conflicted file'
|
||||||
|
|
||||||
|
. ./test-lib.sh
|
||||||
|
|
||||||
|
test_expect_success setup '
|
||||||
|
test_tick &&
|
||||||
|
test_commit both.txt both.txt initial &&
|
||||||
|
git branch topic &&
|
||||||
|
test_commit modified_in_master both.txt in_master &&
|
||||||
|
test_commit added_in_master each.txt in_master &&
|
||||||
|
git checkout topic &&
|
||||||
|
test_commit modified_in_topic both.txt in_topic &&
|
||||||
|
test_commit added_in_topic each.txt in_topic
|
||||||
|
'
|
||||||
|
|
||||||
|
test_expect_success 'git merge master' '
|
||||||
|
test_must_fail git merge master
|
||||||
|
'
|
||||||
|
|
||||||
|
clean_branchnames () {
|
||||||
|
# Remove branch names after conflict lines
|
||||||
|
sed 's/^\([<>]\{5,\}\) .*$/\1/'
|
||||||
|
}
|
||||||
|
|
||||||
|
test_expect_success '-m restores 2-way conflicted+resolved file' '
|
||||||
|
cp each.txt each.txt.conflicted &&
|
||||||
|
echo resolved >each.txt &&
|
||||||
|
git add each.txt &&
|
||||||
|
git checkout -m -- each.txt &&
|
||||||
|
clean_branchnames <each.txt >each.txt.cleaned &&
|
||||||
|
clean_branchnames <each.txt.conflicted >each.txt.conflicted.cleaned &&
|
||||||
|
test_cmp each.txt.conflicted.cleaned each.txt.cleaned
|
||||||
|
'
|
||||||
|
|
||||||
|
test_expect_success '-m restores 3-way conflicted+resolved file' '
|
||||||
|
cp both.txt both.txt.conflicted &&
|
||||||
|
echo resolved >both.txt &&
|
||||||
|
git add both.txt &&
|
||||||
|
git checkout -m -- both.txt &&
|
||||||
|
clean_branchnames <both.txt >both.txt.cleaned &&
|
||||||
|
clean_branchnames <both.txt.conflicted >both.txt.conflicted.cleaned &&
|
||||||
|
test_cmp both.txt.conflicted.cleaned both.txt.cleaned
|
||||||
|
'
|
||||||
|
|
||||||
|
test_done
|
Loading…
Reference in New Issue
Block a user