sparse-checkout: write escaped patterns in cone mode
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>
This commit is contained in:
parent
4f52c2ce6c
commit
d585f0e799
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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"
|
||||
'
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user