]> git.ipfire.org Git - thirdparty/git.git/commitdiff
sparse-checkout: allow one-character directories in cone mode
authorDerrick Stolee <dstolee@microsoft.com>
Thu, 20 Feb 2020 20:07:06 +0000 (20:07 +0000)
committerJunio C Hamano <gitster@pobox.com>
Thu, 20 Feb 2020 22:43:36 +0000 (14:43 -0800)
In 9e6d3e64 (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 <dstolee@microsoft.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
dir.c
t/t1091-sparse-checkout-builtin.sh

diff --git a/dir.c b/dir.c
index 7ac0920b7135171a50c39a0912db35cf7c3fe68b..a87900d43a2ea8f80ce197c4d43aecf2936746dd 100644 (file)
--- a/dir.c
+++ b/dir.c
@@ -682,7 +682,7 @@ static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern
                return;
        }
 
-       if (given->patternlen <= 2 ||
+       if (given->patternlen < 2 ||
            *given->pattern == '*' ||
            strstr(given->pattern, "**")) {
                /* Not a cone pattern. */
index c35cbdef454858b943fa30fb58ac59c5694d0065..b4c9c32a0372516f020adbcac28e6290c7bbbd78 100755 (executable)
@@ -417,10 +417,20 @@ test_expect_success 'pattern-checks: too short' '
        cat >repo/.git/info/sparse-checkout <<-\EOF &&
        /*
        !/*/
-       /a
+       /
        EOF
        check_read_tree_errors repo "a" "disabling cone pattern matching"
 '
+test_expect_success 'pattern-checks: not too short' '
+       cat >repo/.git/info/sparse-checkout <<-\EOF &&
+       /*
+       !/*/
+       /b/
+       EOF
+       git -C repo read-tree -mu HEAD 2>err &&
+       test_must_be_empty err &&
+       check_files repo a
+'
 
 test_expect_success 'pattern-checks: trailing "*"' '
        cat >repo/.git/info/sparse-checkout <<-\EOF &&