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 <jrnieder@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jonathan Nieder committed
Aug 12, 2014 at 17:03 UTC
6a143aa2b23b97fa8363e2f4fd16f23b4c5b104d
2 files changed
+23
-5
t/t7201-co.sh
+17
@@ -223,6 +223,23 @@ test_expect_success 'checkout --merge --conflict=diff3 <branch>' '
223
test_cmp two expect
224
'
225
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
+
243
test_expect_success 'checkout to detach HEAD (with advice declined)' '
244
245
git config advice.detachedHead false &&
unpack-trees.c
+6
-5
@@ -1178,7 +1178,8 @@ return_failed:
1178
static int reject_merge(const struct cache_entry *ce,
1179
struct unpack_trees_options *o)
1180
{
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);
1183
}
1184
1185
static int same(const struct cache_entry *a, const struct cache_entry *b)
@@ -1633,7 +1634,7 @@ int threeway_merge(const struct cache_entry * const *stages,
1634
/* #14, #14ALT, #2ALT */
1635
if (remote && !df_conflict_head && head_match && !remote_match) {
1636
if (index && !same(index, remote) && !same(index, head))
1636
- return o->gently ? -1 : reject_merge(index, o);
1637
+ return reject_merge(index, o);
1638
return merged_entry(remote, index, o);
1639
}
1640
/*
@@ -1641,7 +1642,7 @@ int threeway_merge(const struct cache_entry * const *stages,
1642
* make sure that it matches head.
1643
*/
1644
if (index && !same(index, head))
1644
- return o->gently ? -1 : reject_merge(index, o);
1645
+ return reject_merge(index, o);
1646
1647
if (head) {
1648
/* #5ALT, #15 */
@@ -1770,7 +1771,7 @@ int twoway_merge(const struct cache_entry * const *src,
1771
else
1772
return merged_entry(newtree, current, o);
1773
}
1773
- return o->gently ? -1 : reject_merge(current, o);
1774
+ return reject_merge(current, o);
1775
} else if ((!oldtree && !newtree) || /* 4 and 5 */
1776
(!oldtree && newtree &&
1777
same(current, newtree)) || /* 6 and 7 */
@@ -1788,7 +1789,7 @@ int twoway_merge(const struct cache_entry * const *src,
1789
/* 20 or 21 */
1790
return merged_entry(newtree, current, o);
1791
} else
1791
- return o->gently ? -1 : reject_merge(current, o);
1792
+ return reject_merge(current, o);
1793
}
1794
else if (newtree) {
1795
if (oldtree && !o->initial_checkout) {