Skip to content

Commit 6c11c6a

Browse files
derrickstoleegitster
authored andcommitted
sparse-checkout: allow one-character directories in cone mode
In 9e6d3e6 (sparse-checkout: detect short patterns, 2020-01-24), a condition on the minimum length of a cone-mode pattern was introduced. However, this condition was off-by-one. If we have a directory with a single character, say "b", then the command git sparse-checkout set b will correctly add the pattern "/b/" to the sparse-checkout file. When this is interpeted in dir.c, the pattern is "/b" with the PATTERN_FLAG_MUSTBEDIR flag. This string has length two, which satisfies our inclusive inequality (<= 2). The reason for this inequality is that we will start to read the pattern string character-by-character using three char pointers: prev, cur, next. In particular, next is set to the current pattern plus two. The mistake was that next will still be a valid pointer when the pattern length is two, since the string is null-terminated. Make this inequality strict so these patterns work. Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ef07659 commit 6c11c6a

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

dir.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern
682682
return;
683683
}
684684

685-
if (given->patternlen <= 2 ||
685+
if (given->patternlen < 2 ||
686686
*given->pattern == '*' ||
687687
strstr(given->pattern, "**")) {
688688
/* Not a cone pattern. */

t/t1091-sparse-checkout-builtin.sh

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,10 +417,20 @@ test_expect_success 'pattern-checks: too short' '
417417
cat >repo/.git/info/sparse-checkout <<-\EOF &&
418418
/*
419419
!/*/
420-
/a
420+
/
421421
EOF
422422
check_read_tree_errors repo "a" "disabling cone pattern matching"
423423
'
424+
test_expect_success 'pattern-checks: not too short' '
425+
cat >repo/.git/info/sparse-checkout <<-\EOF &&
426+
/*
427+
!/*/
428+
/b/
429+
EOF
430+
git -C repo read-tree -mu HEAD 2>err &&
431+
test_must_be_empty err &&
432+
check_files repo a
433+
'
424434

425435
test_expect_success 'pattern-checks: trailing "*"' '
426436
cat >repo/.git/info/sparse-checkout <<-\EOF &&

0 commit comments

Comments
 (0)