Skip to content

Commit 6a143aa

Browse files
jrngitster
authored andcommitted
checkout -m: attempt merge when deletion of path was staged
twoway_merge() is missing an o->gently check in the case where a file that needs to be modified is missing from the index but present in the old and new trees. As a result, in this case 'git checkout -m' errors out instead of trying to perform a merge. Fix it by checking o->gently. While at it, inline the o->gently check into reject_merge to prevent future call sites from making the same mistake. Noticed by code inspection. The test for the motivating case was added by JC. Signed-off-by: Jonathan Nieder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6c1db1b commit 6a143aa

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

t/t7201-co.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,23 @@ test_expect_success 'checkout --merge --conflict=diff3 <branch>' '
223223
test_cmp two expect
224224
'
225225

226+
test_expect_success 'switch to another branch while carrying a deletion' '
227+
228+
git checkout -f master && git reset --hard && git clean -f &&
229+
git rm two &&
230+
231+
test_must_fail git checkout simple 2>errs &&
232+
test_i18ngrep overwritten errs &&
233+
234+
git checkout --merge simple 2>errs &&
235+
test_i18ngrep ! overwritten errs &&
236+
git ls-files -u &&
237+
test_must_fail git cat-file -t :0:two &&
238+
test "$(git cat-file -t :1:two)" = blob &&
239+
test "$(git cat-file -t :2:two)" = blob &&
240+
test_must_fail git cat-file -t :3:two
241+
'
242+
226243
test_expect_success 'checkout to detach HEAD (with advice declined)' '
227244
228245
git config advice.detachedHead false &&

unpack-trees.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,7 +1178,8 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
11781178
static int reject_merge(const struct cache_entry *ce,
11791179
struct unpack_trees_options *o)
11801180
{
1181-
return add_rejected_path(o, ERROR_WOULD_OVERWRITE, ce->name);
1181+
return o->gently ? -1 :
1182+
add_rejected_path(o, ERROR_WOULD_OVERWRITE, ce->name);
11821183
}
11831184

11841185
static int same(const struct cache_entry *a, const struct cache_entry *b)
@@ -1633,15 +1634,15 @@ int threeway_merge(const struct cache_entry * const *stages,
16331634
/* #14, #14ALT, #2ALT */
16341635
if (remote && !df_conflict_head && head_match && !remote_match) {
16351636
if (index && !same(index, remote) && !same(index, head))
1636-
return o->gently ? -1 : reject_merge(index, o);
1637+
return reject_merge(index, o);
16371638
return merged_entry(remote, index, o);
16381639
}
16391640
/*
16401641
* If we have an entry in the index cache, then we want to
16411642
* make sure that it matches head.
16421643
*/
16431644
if (index && !same(index, head))
1644-
return o->gently ? -1 : reject_merge(index, o);
1645+
return reject_merge(index, o);
16451646

16461647
if (head) {
16471648
/* #5ALT, #15 */
@@ -1770,7 +1771,7 @@ int twoway_merge(const struct cache_entry * const *src,
17701771
else
17711772
return merged_entry(newtree, current, o);
17721773
}
1773-
return o->gently ? -1 : reject_merge(current, o);
1774+
return reject_merge(current, o);
17741775
} else if ((!oldtree && !newtree) || /* 4 and 5 */
17751776
(!oldtree && newtree &&
17761777
same(current, newtree)) || /* 6 and 7 */
@@ -1788,7 +1789,7 @@ int twoway_merge(const struct cache_entry * const *src,
17881789
/* 20 or 21 */
17891790
return merged_entry(newtree, current, o);
17901791
} else
1791-
return o->gently ? -1 : reject_merge(current, o);
1792+
return reject_merge(current, o);
17921793
}
17931794
else if (newtree) {
17941795
if (oldtree && !o->initial_checkout) {

0 commit comments

Comments
 (0)