summaryrefslogtreecommitdiff
path: root/dir.c
diff options
context:
space:
mode:
authorDerrick Stolee <dstolee@microsoft.com>2020-01-31 20:16:08 (GMT)
committerJunio C Hamano <gitster@pobox.com>2020-01-31 21:05:29 (GMT)
commit9abc60f8015d060d3f3433b105648a4725c97bd1 (patch)
tree367534ff77b191bf1edbfa24ef61496733332b40 /dir.c
parent9e6d3e64175713bc0007f3012ea288f4dfc0a399 (diff)
downloadgit-9abc60f8015d060d3f3433b105648a4725c97bd1.zip
git-9abc60f8015d060d3f3433b105648a4725c97bd1.tar.gz
git-9abc60f8015d060d3f3433b105648a4725c97bd1.tar.bz2
sparse-checkout: warn on globs in cone 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 or other glob character is invalid. Add checks to disable cone mode when seeing these values. A later change will properly handle escaped globs. Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'dir.c')
-rw-r--r--dir.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/dir.c b/dir.c
index c2e5856..71d2833 100644
--- a/dir.c
+++ b/dir.c
@@ -635,6 +635,7 @@ static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern
struct pattern_entry *translated;
char *truncated;
char *data = NULL;
+ const char *prev, *cur, *next;
if (!pl->use_cone_patterns)
return;
@@ -652,12 +653,47 @@ static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern
}
if (given->patternlen <= 2 ||
+ *given->pattern == '*' ||
strstr(given->pattern, "**")) {
/* Not a cone pattern. */
warning(_("unrecognized pattern: '%s'"), given->pattern);
goto clear_hashmaps;
}
+ prev = given->pattern;
+ cur = given->pattern + 1;
+ next = given->pattern + 2;
+
+ while (*cur) {
+ /* Watch for glob characters '*', '\', '[', '?' */
+ if (!is_glob_special(*cur))
+ goto increment;
+
+ /* But only if *prev != '\\' */
+ if (*prev == '\\')
+ goto increment;
+
+ /* But allow the initial '\' */
+ if (*cur == '\\' &&
+ is_glob_special(*next))
+ goto increment;
+
+ /* But a trailing '/' then '*' is fine */
+ if (*prev == '/' &&
+ *cur == '*' &&
+ *next == 0)
+ goto increment;
+
+ /* Not a cone pattern. */
+ warning(_("unrecognized pattern: '%s'"), given->pattern);
+ goto clear_hashmaps;
+
+ increment:
+ prev++;
+ cur++;
+ next++;
+ }
+
if (given->patternlen > 2 &&
!strcmp(given->pattern + given->patternlen - 2, "/*")) {
if (!(given->flags & PATTERN_FLAG_NEGATIVE)) {