summaryrefslogtreecommitdiff
path: root/builtin/sparse-checkout.c
diff options
context:
space:
mode:
authorDerrick Stolee <dstolee@microsoft.com>2020-01-31 20:16:10 (GMT)
committerJunio C Hamano <gitster@pobox.com>2020-01-31 21:05:29 (GMT)
commitd585f0e7992ea7f025a5a91f46f2baa9e88f19f6 (patch)
treed311a04ddf853f4784a28e590c6eeb76c9dd91c5 /builtin/sparse-checkout.c
parent4f52c2ce6c578896964e960f6017510f0efd3f46 (diff)
downloadgit-d585f0e7992ea7f025a5a91f46f2baa9e88f19f6.zip
git-d585f0e7992ea7f025a5a91f46f2baa9e88f19f6.tar.gz
git-d585f0e7992ea7f025a5a91f46f2baa9e88f19f6.tar.bz2
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>
Diffstat (limited to 'builtin/sparse-checkout.c')
-rw-r--r--builtin/sparse-checkout.c23
1 files changed, 21 insertions, 2 deletions
diff --git a/builtin/sparse-checkout.c b/builtin/sparse-checkout.c
index 3cee8ab..cc86b8a 100644
--- a/builtin/sparse-checkout.c
+++ b/builtin/sparse-checkout.c
@@ -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);
}
}