@matheusbsbs / matheus-bagliano / commits / 2f6c9760de
Fork 0

magic pathspec: futureproof shorthand form

The earlier design was to take whatever non-alnum that the short format parser happens to support, leaving the rest as part of the pattern, so a version of git that knows '*' magic and a version that does not would have behaved differently when given ":*Makefile". The former would have applied the '*' magic to the pattern "Makefile", while the latter would used no magic to the pattern "*Makefile". Instead, just reserve all non-alnum ASCII letters that are neither glob nor regexp special as potential magic signature, and when we see a magic that is not supported, die with an error message, just like the longhand codepath does. With this, ":%#!*Makefile" will always mean "%#!" magic applied to the pattern "*Makefile", no matter what version of git is used (it is a different matter if the version of git supports all of these three magic matching rules). Also make ':' without anything else to mean "there is no pathspec". This would allow differences between "git log" and "git log ." run from the top level of the working tree (the latter simplifies no-op commits away from the history) to be expressed from a subdirectory by saying "git log :". Helped-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Apr 8, 2011 at 16:18 UTC 2f6c9760debfb4705f6efb5862e2b3a23b2b951c
3 files changed +18 -8
ctype.c
+8 -7
@@ -10,17 +10,18 @@ enum {
10 A = GIT_ALPHA,
11 D = GIT_DIGIT,
12 G = GIT_GLOB_SPECIAL, /* *, ?, [, \\ */
13 - R = GIT_REGEX_SPECIAL /* $, (, ), +, ., ^, {, | */
13 + R = GIT_REGEX_SPECIAL, /* $, (, ), +, ., ^, {, | */
14 + P = GIT_PATHSPEC_MAGIC /* other non-alnum, except for ] and } */
15 };
16
17 unsigned char sane_ctype[256] = {
18 0, 0, 0, 0, 0, 0, 0, 0, 0, S, S, 0, 0, S, 0, 0, /* 0.. 15 */
19 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 16.. 31 */
19 - S, 0, 0, 0, R, 0, 0, 0, R, R, G, R, 0, 0, R, 0, /* 32.. 47 */
20 - D, D, D, D, D, D, D, D, D, D, 0, 0, 0, 0, 0, G, /* 48.. 63 */
21 - 0, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, /* 64.. 79 */
22 - A, A, A, A, A, A, A, A, A, A, A, G, G, 0, R, 0, /* 80.. 95 */
23 - 0, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, /* 96..111 */
24 - A, A, A, A, A, A, A, A, A, A, A, R, R, 0, 0, 0, /* 112..127 */
20 + S, P, P, P, R, P, P, P, R, R, G, R, P, P, R, P, /* 32.. 47 */
21 + D, D, D, D, D, D, D, D, D, D, P, P, P, P, P, G, /* 48.. 63 */
22 + P, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, /* 64.. 79 */
23 + A, A, A, A, A, A, A, A, A, A, A, G, G, 0, R, P, /* 80.. 95 */
24 + P, A, A, A, A, A, A, A, A, A, A, A, A, A, A, A, /* 96..111 */
25 + A, A, A, A, A, A, A, A, A, A, A, R, R, 0, P, 0, /* 112..127 */
26 /* Nothing in the 128.. range */
27 };
git-compat-util.h
+2
@@ -462,6 +462,7 @@ extern unsigned char sane_ctype[256];
462 #define GIT_ALPHA 0x04
463 #define GIT_GLOB_SPECIAL 0x08
464 #define GIT_REGEX_SPECIAL 0x10
465 +#define GIT_PATHSPEC_MAGIC 0x20
466 #define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
467 #define isascii(x) (((x) & ~0x7f) == 0)
468 #define isspace(x) sane_istest(x,GIT_SPACE)
@@ -472,6 +473,7 @@ extern unsigned char sane_ctype[256];
473 #define is_regex_special(x) sane_istest(x,GIT_GLOB_SPECIAL | GIT_REGEX_SPECIAL)
474 #define tolower(x) sane_case((unsigned char)(x), 0x20)
475 #define toupper(x) sane_case((unsigned char)(x), 0)
476 +#define is_pathspec_magic(x) sane_istest(x,GIT_PATHSPEC_MAGIC)
477
478 static inline int sane_case(int x, int high)
479 {
setup.c
+8 -1
@@ -197,19 +197,26 @@ const char *prefix_pathspec(const char *prefix, int prefixlen, const char *elt)
197 }
198 if (*copyfrom == ')')
199 copyfrom++;
200 + } else if (!elt[1]) {
201 + /* Just ':' -- no element! */
202 + return NULL;
203 } else {
204 /* shorthand */
205 for (copyfrom = elt + 1;
206 *copyfrom && *copyfrom != ':';
207 copyfrom++) {
208 char ch = *copyfrom;
209 +
210 + if (!is_pathspec_magic(ch))
211 + break;
212 for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)
213 if (pathspec_magic[i].mnemonic == ch) {
214 magic |= pathspec_magic[i].bit;
215 break;
216 }
217 if (ARRAY_SIZE(pathspec_magic) <= i)
212 - break;
218 + die("Unimplemented pathspec magic '%c' in '%s'",
219 + ch, elt);
220 }
221 if (*copyfrom == ':')
222 copyfrom++;