]> git.ipfire.org Git - thirdparty/git.git/commitdiff
sparse-checkout: write escaped patterns in cone mode
authorDerrick Stolee <dstolee@microsoft.com>
Fri, 31 Jan 2020 20:16:10 +0000 (20:16 +0000)
committerJunio C Hamano <gitster@pobox.com>
Fri, 31 Jan 2020 21:05:29 +0000 (13:05 -0800)
If a user somehow creates a directory with an asterisk (*) or backslash
(\), then the "git sparse-checkout set" command will struggle to provide
the correct pattern in the sparse-checkout file. When not in cone mode,
the provided pattern is written directly into the sparse-checkout file.
However, in cone mode we expect a list of paths to directories and then
we convert those into patterns.

However, there is some care needed for the timing of these escapes. The
in-memory pattern list is used to update the working directory before
writing the patterns to disk. Thus, we need the command to have the
unescaped names in the hashsets for the cone comparisons, then escape
the patterns later.

Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/sparse-checkout.c
t/t1091-sparse-checkout-builtin.sh

index 3cee8ab46e548f0a93216f6b03eb875530f0c627..cc86b8a0147d74a0afcb12cf6b3940cb0d561a92 100644 (file)
@@ -13,6 +13,7 @@
 #include "resolve-undo.h"
 #include "unpack-trees.h"
 #include "wt-status.h"
+#include "quote.h"
 
 static const char *empty_base = "";
 
@@ -140,6 +141,22 @@ static int update_working_directory(struct pattern_list *pl)
        return result;
 }
 
+static char *escaped_pattern(char *pattern)
+{
+       char *p = pattern;
+       struct strbuf final = STRBUF_INIT;
+
+       while (*p) {
+               if (*p == '*' || *p == '\\')
+                       strbuf_addch(&final, '\\');
+
+               strbuf_addch(&final, *p);
+               p++;
+       }
+
+       return strbuf_detach(&final, NULL);
+}
+
 static void write_cone_to_file(FILE *fp, struct pattern_list *pl)
 {
        int i;
@@ -164,10 +181,11 @@ static void write_cone_to_file(FILE *fp, struct pattern_list *pl)
        fprintf(fp, "/*\n!/*/\n");
 
        for (i = 0; i < sl.nr; i++) {
-               char *pattern = sl.items[i].string;
+               char *pattern = escaped_pattern(sl.items[i].string);
 
                if (strlen(pattern))
                        fprintf(fp, "%s/\n!%s/*/\n", pattern, pattern);
+               free(pattern);
        }
 
        string_list_clear(&sl, 0);
@@ -185,8 +203,9 @@ static void write_cone_to_file(FILE *fp, struct pattern_list *pl)
        string_list_remove_duplicates(&sl, 0);
 
        for (i = 0; i < sl.nr; i++) {
-               char *pattern = sl.items[i].string;
+               char *pattern = escaped_pattern(sl.items[i].string);
                fprintf(fp, "%s/\n", pattern);
+               free(pattern);
        }
 }
 
index 9ea700896d831fc4ef537e747089bd84d04debb1..fb8718e64a49046fe3b74590b768e23c630a405b 100755 (executable)
@@ -309,6 +309,9 @@ check_read_tree_errors () {
        REPO=$1
        FILES=$2
        ERRORS=$3
+       git -C $REPO -c core.sparseCheckoutCone=false read-tree -mu HEAD 2>err &&
+       test_must_be_empty err &&
+       check_files $REPO "$FILES" &&
        git -C $REPO read-tree -mu HEAD 2>err &&
        if test -z "$ERRORS"
        then
@@ -391,14 +394,17 @@ test_expect_success BSLASHPSPEC 'pattern-checks: escaped "*"' '
        git -C escaped reset --hard $COMMIT &&
        check_files escaped "a deep folder1 folder2 zbad\\dir zdoes*exist" &&
        git -C escaped sparse-checkout init --cone &&
-       cat >escaped/.git/info/sparse-checkout <<-\EOF &&
+       git -C escaped sparse-checkout set zbad\\dir/bogus "zdoes*not*exist" "zdoes*exist" &&
+       cat >expect <<-\EOF &&
        /*
        !/*/
        /zbad\\dir/
        !/zbad\\dir/*/
-       /zdoes\*not\*exist/
+       /zbad\\dir/bogus/
        /zdoes\*exist/
+       /zdoes\*not\*exist/
        EOF
+       test_cmp expect escaped/.git/info/sparse-checkout &&
        check_read_tree_errors escaped "a zbad\\dir zdoes*exist"
 '