Skip to content

Commit 77a514f

Browse files
committed
sparse-checkout: warn on incorrect '*' in patterns
In cone mode, the sparse-checkout commmand will write patterns that allow faster pattern matching. This matching only works if the patterns in the sparse-checkout file are those written by that command. Users can edit the sparse-checkout file and create patterns that cause the cone mode matching to fail. The cone mode patterns may end in "/*" but otherwise an un-escaped asterisk is invalid. Add checks to disable cone mode when seeing these values. A later change will properly handle escaped asterisks. Signed-off-by: Derrick Stolee <[email protected]>
1 parent 9be4990 commit 77a514f

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

dir.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,7 @@ static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern
635635
struct pattern_entry *translated;
636636
char *truncated;
637637
char *data = NULL;
638+
const char *prev, *cur, *next;
638639

639640
if (!pl->use_cone_patterns)
640641
return;
@@ -652,13 +653,42 @@ static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern
652653
}
653654

654655
if (given->patternlen <= 2 ||
656+
*given->pattern == '*' ||
655657
strstr(given->pattern, "**")) {
656658
/* Not a cone pattern. */
657659
pl->use_cone_patterns = 0;
658660
warning(_("unrecognized pattern: '%s'"), given->pattern);
659661
goto clear_hashmaps;
660662
}
661663

664+
prev = given->pattern;
665+
cur = given->pattern + 1;
666+
next = given->pattern + 2;
667+
668+
while (*cur) {
669+
/* We care about *cur == '*' */
670+
if (*cur != '*')
671+
goto increment;
672+
673+
/* But only if *prev != '\\' */
674+
if (*prev == '\\')
675+
goto increment;
676+
677+
/* But a trailing '/' then '*' is fine */
678+
if (*prev == '/' && *next == 0)
679+
goto increment;
680+
681+
/* Not a cone pattern. */
682+
pl->use_cone_patterns = 0;
683+
warning(_("unrecognized pattern: '%s'"), given->pattern);
684+
goto clear_hashmaps;
685+
686+
increment:
687+
prev++;
688+
cur++;
689+
next++;
690+
}
691+
662692
if (given->patternlen > 2 &&
663693
!strcmp(given->pattern + given->patternlen - 2, "/*")) {
664694
if (!(given->flags & PATTERN_FLAG_NEGATIVE)) {

t/t1091-sparse-checkout-builtin.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,4 +348,31 @@ test_expect_success 'pattern-checks: too short' '
348348
check_read_tree_errors repo "a" "disabling cone pattern matching"
349349
'
350350

351+
test_expect_success 'pattern-checks: trailing "*"' '
352+
cat >repo/.git/info/sparse-checkout <<-\EOF &&
353+
/*
354+
!/*/
355+
/a*
356+
EOF
357+
check_read_tree_errors repo "a" "disabling cone pattern matching"
358+
'
359+
360+
test_expect_success 'pattern-checks: starting "*"' '
361+
cat >repo/.git/info/sparse-checkout <<-\EOF &&
362+
/*
363+
!/*/
364+
*eep/
365+
EOF
366+
check_read_tree_errors repo "a deep" "disabling cone pattern matching"
367+
'
368+
369+
test_expect_success 'pattern-checks: escaped "*"' '
370+
cat >repo/.git/info/sparse-checkout <<-\EOF &&
371+
/*
372+
!/*/
373+
/does\*not\*exist/
374+
EOF
375+
check_read_tree_errors repo "a" ""
376+
'
377+
351378
test_done

0 commit comments

Comments
 (0)