Skip to content

Commit 11bd3dd

Browse files
committed
Merge branch 'jc/maint-checkout-fix'
* jc/maint-checkout-fix: checkout: do not check out unmerged higher stages randomly Conflicts: t/t7201-co.sh
2 parents af9ce1f + 8fdcf31 commit 11bd3dd

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

builtin-checkout.c

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ static int read_tree_some(struct tree *tree, const char **pathspec)
7676
return 0;
7777
}
7878

79+
static int skip_same_name(struct cache_entry *ce, int pos)
80+
{
81+
while (++pos < active_nr &&
82+
!strcmp(active_cache[pos]->name, ce->name))
83+
; /* skip */
84+
return pos;
85+
}
86+
87+
7988
static int checkout_paths(struct tree *source_tree, const char **pathspec)
8089
{
8190
int pos;
@@ -107,14 +116,32 @@ static int checkout_paths(struct tree *source_tree, const char **pathspec)
107116
if (report_path_error(ps_matched, pathspec, 0))
108117
return 1;
109118

119+
/* Any unmerged paths? */
120+
for (pos = 0; pos < active_nr; pos++) {
121+
struct cache_entry *ce = active_cache[pos];
122+
if (pathspec_match(pathspec, NULL, ce->name, 0)) {
123+
if (!ce_stage(ce))
124+
continue;
125+
errs = 1;
126+
error("path '%s' is unmerged", ce->name);
127+
pos = skip_same_name(ce, pos) - 1;
128+
}
129+
}
130+
if (errs)
131+
return 1;
132+
110133
/* Now we are committed to check them out */
111134
memset(&state, 0, sizeof(state));
112135
state.force = 1;
113136
state.refresh_cache = 1;
114137
for (pos = 0; pos < active_nr; pos++) {
115138
struct cache_entry *ce = active_cache[pos];
116139
if (pathspec_match(pathspec, NULL, ce->name, 0)) {
117-
errs |= checkout_entry(ce, &state, NULL);
140+
if (!ce_stage(ce)) {
141+
errs |= checkout_entry(ce, &state, NULL);
142+
continue;
143+
}
144+
pos = skip_same_name(ce, pos) - 1;
118145
}
119146
}
120147

t/t7201-co.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,4 +369,26 @@ test_expect_success \
369369
'checkout with --track, but without -b, fails with too short tracked name' '
370370
test_must_fail git checkout --track renamer'
371371

372+
test_expect_success 'checkout an unmerged path should fail' '
373+
rm -f .git/index &&
374+
O=$(echo original | git hash-object -w --stdin) &&
375+
A=$(echo ourside | git hash-object -w --stdin) &&
376+
B=$(echo theirside | git hash-object -w --stdin) &&
377+
(
378+
echo "100644 $A 0 fild" &&
379+
echo "100644 $O 1 file" &&
380+
echo "100644 $A 2 file" &&
381+
echo "100644 $B 3 file" &&
382+
echo "100644 $A 0 filf"
383+
) | git update-index --index-info &&
384+
echo "none of the above" >sample &&
385+
cat sample >fild &&
386+
cat sample >file &&
387+
cat sample >filf &&
388+
test_must_fail git checkout fild file filf &&
389+
test_cmp sample fild &&
390+
test_cmp sample filf &&
391+
test_cmp sample file
392+
'
393+
372394
test_done

0 commit comments

Comments
 (0)