@matheusbsbs / matheus-bagliano / commits / e4aee10a2e
Fork 0

Change attribute negation marker from '!' to '-'.

At the same time, we do not want to allow arbitrary strings for attribute names, as we are likely to want to extend the syntax later. Allow only alnum, dash, underscore and dot for now. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Apr 15, 2007 at 14:56 UTC e4aee10a2eaf0937d86d046f85ee569a75cae9ac
3 files changed +52 -7
attr.c
+46 -5
@@ -36,6 +36,27 @@ static unsigned hash_name(const char *name, int namelen)
36 return val;
37 }
38
39 +static int invalid_attr_name(const char *name, int namelen)
40 +{
41 + /*
42 + * Attribute name cannot begin with '-' and from
43 + * [-A-Za-z0-9_.]. We'd specifically exclude '=' for now,
44 + * as we might later want to allow non-binary value for
45 + * attributes, e.g. "*.svg merge=special-merge-program-for-svg"
46 + */
47 + if (*name == '-')
48 + return -1;
49 + while (namelen--) {
50 + char ch = *name++;
51 + if (! (ch == '-' || ch == '.' || ch == '_' ||
52 + ('0' <= ch && ch <= '9') ||
53 + ('a' <= ch && ch <= 'z') ||
54 + ('A' <= ch && ch <= 'Z')) )
55 + return -1;
56 + }
57 + return 0;
58 +}
59 +
60 struct git_attr *git_attr(const char *name, int len)
61 {
62 unsigned hval = hash_name(name, len);
@@ -48,6 +69,9 @@ struct git_attr *git_attr(const char *name, int len)
69 return a;
70 }
71
72 + if (invalid_attr_name(name, len))
73 + return NULL;
74 +
75 a = xmalloc(sizeof(*a) + len + 1);
76 memcpy(a->name, name, len);
77 a->name[len] = 0;
@@ -68,7 +92,7 @@ struct git_attr *git_attr(const char *name, int len)
92 * (1) glob pattern.
93 * (2) whitespace
94 * (3) whitespace separated list of attribute names, each of which
71 - * could be prefixed with '!' to mean "not set".
95 + * could be prefixed with '-' to mean "not set".
96 */
97
98 struct attr_state {
@@ -114,6 +138,12 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,
138 name += strlen(ATTRIBUTE_MACRO_PREFIX);
139 name += strspn(name, blank);
140 namelen = strcspn(name, blank);
141 + if (invalid_attr_name(name, namelen)) {
142 + fprintf(stderr,
143 + "%.*s is not a valid attribute name: %s:%d\n",
144 + namelen, name, src, lineno);
145 + return NULL;
146 + }
147 }
148 else
149 is_macro = 0;
@@ -126,11 +156,21 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,
156 while (*cp) {
157 const char *ep;
158 ep = cp + strcspn(cp, blank);
129 - if (pass) {
159 + if (!pass) {
160 + if (*cp == '-')
161 + cp++;
162 + if (invalid_attr_name(cp, ep - cp)) {
163 + fprintf(stderr,
164 + "%.*s is not a valid attribute name: %s:%d\n",
165 + (int)(ep - cp), cp,
166 + src, lineno);
167 + return NULL;
168 + }
169 + } else {
170 struct attr_state *e;
171
172 e = &(res->state[num_attr]);
133 - if (*cp == '!') {
173 + if (*cp == '-') {
174 e->unset = 1;
175 cp++;
176 }
@@ -146,8 +186,9 @@ static struct match_attr *parse_attr_line(const char *line, const char *src,
186 sizeof(*res) +
187 sizeof(struct attr_state) * num_attr +
188 (is_macro ? 0 : namelen + 1));
149 - if (is_macro)
189 + if (is_macro) {
190 res->u.attr = git_attr(name, namelen);
191 + }
192 else {
193 res->u.pattern = (char*)&(res->state[num_attr]);
194 memcpy(res->u.pattern, name, namelen);
@@ -194,7 +235,7 @@ static void free_attr_elem(struct attr_stack *e)
235 }
236
237 static const char *builtin_attr[] = {
197 - "[attr]binary !diff !crlf",
238 + "[attr]binary -diff -crlf",
239 NULL,
240 };
241
builtin-check-attr.c
+5 -1
@@ -29,8 +29,12 @@ int cmd_check_attr(int argc, const char **argv, const char *prefix)
29 check = xcalloc(cnt, sizeof(*check));
30 for (i = 0; i < cnt; i++) {
31 const char *name;
32 + struct git_attr *a;
33 name = argv[i + 1];
33 - check[i].attr = git_attr(name, strlen(name));
34 + a = git_attr(name, strlen(name));
35 + if (!a)
36 + return error("%s: not a valid attribute name", name);
37 + check[i].attr = a;
38 }
39
40 for (i = doubledash; i < argc; i++) {
t/t0020-crlf.sh
+1 -1
@@ -216,7 +216,7 @@ test_expect_success 'apply patch --index (autocrlf=true)' '
216
217 test_expect_success '.gitattributes says two is binary' '
218
219 - echo "two !crlf" >.gitattributes &&
219 + echo "two -crlf" >.gitattributes &&
220 rm -f tmp one dir/two &&
221 git repo-config core.autocrlf true &&
222 git read-tree --reset -u HEAD &&