|
6 | 6 | #include "string-list.h" |
7 | 7 |
|
8 | 8 | /* |
9 | | - * Make sure "ref" is something reasonable to have under ".git/refs/"; |
10 | | - * We do not like it if: |
| 9 | + * How to handle various characters in refnames: |
| 10 | + * 0: An acceptable character for refs |
| 11 | + * 1: End-of-component |
| 12 | + * 2: ., look for a preceding . to reject .. in refs |
| 13 | + * 3: {, look for a preceding @ to reject @{ in refs |
| 14 | + * 4: A bad character: ASCII control characters, "~", "^", ":" or SP |
| 15 | + */ |
| 16 | +static unsigned char refname_disposition[256] = { |
| 17 | + 1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, |
| 18 | + 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, |
| 19 | + 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 2, 1, |
| 20 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 4, |
| 21 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 22 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 0, 4, 0, |
| 23 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 24 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 4, 4 |
| 25 | +}; |
| 26 | + |
| 27 | +/* |
| 28 | + * Try to read one refname component from the front of refname. |
| 29 | + * Return the length of the component found, or -1 if the component is |
| 30 | + * not legal. It is legal if it is something reasonable to have under |
| 31 | + * ".git/refs/"; We do not like it if: |
11 | 32 | * |
12 | 33 | * - any path component of it begins with ".", or |
13 | 34 | * - it has double dots "..", or |
|
16 | 37 | * - it ends with ".lock" |
17 | 38 | * - it contains a "\" (backslash) |
18 | 39 | */ |
19 | | - |
20 | | -/* Return true iff ch is not allowed in reference names. */ |
21 | | -static inline int bad_ref_char(int ch) |
22 | | -{ |
23 | | - if (((unsigned) ch) <= ' ' || ch == 0x7f || |
24 | | - ch == '~' || ch == '^' || ch == ':' || ch == '\\') |
25 | | - return 1; |
26 | | - /* 2.13 Pattern Matching Notation */ |
27 | | - if (ch == '*' || ch == '?' || ch == '[') /* Unsupported */ |
28 | | - return 1; |
29 | | - return 0; |
30 | | -} |
31 | | - |
32 | | -/* |
33 | | - * Try to read one refname component from the front of refname. Return |
34 | | - * the length of the component found, or -1 if the component is not |
35 | | - * legal. |
36 | | - */ |
37 | 40 | static int check_refname_component(const char *refname, int flags) |
38 | 41 | { |
39 | 42 | const char *cp; |
40 | 43 | char last = '\0'; |
41 | 44 |
|
42 | 45 | for (cp = refname; ; cp++) { |
43 | | - char ch = *cp; |
44 | | - if (ch == '\0' || ch == '/') |
| 46 | + int ch = *cp & 255; |
| 47 | + unsigned char disp = refname_disposition[ch]; |
| 48 | + switch (disp) { |
| 49 | + case 1: |
| 50 | + goto out; |
| 51 | + case 2: |
| 52 | + if (last == '.') |
| 53 | + return -1; /* Refname contains "..". */ |
| 54 | + break; |
| 55 | + case 3: |
| 56 | + if (last == '@') |
| 57 | + return -1; /* Refname contains "@{". */ |
45 | 58 | break; |
46 | | - if (bad_ref_char(ch)) |
47 | | - return -1; /* Illegal character in refname. */ |
48 | | - if (last == '.' && ch == '.') |
49 | | - return -1; /* Refname contains "..". */ |
50 | | - if (last == '@' && ch == '{') |
51 | | - return -1; /* Refname contains "@{". */ |
| 59 | + case 4: |
| 60 | + return -1; |
| 61 | + } |
52 | 62 | last = ch; |
53 | 63 | } |
| 64 | +out: |
54 | 65 | if (cp == refname) |
55 | 66 | return 0; /* Component has zero length. */ |
56 | 67 | if (refname[0] == '.') { |
|
0 commit comments