1
#include "cache.h"
2
#include "attr.h"
3
4
+#define ATTR__UNKNOWN ((void *) -2)
5
+
6
/*
7
* The basic design decision here is that we are not going to have
8
* insanely large number of attributes.
85
check_all_attr = xrealloc(check_all_attr,
86
sizeof(*check_all_attr) * attr_nr);
87
check_all_attr[a->attr_nr].attr = a;
88
+ check_all_attr[a->attr_nr].value = ATTR__UNKNOWN;
89
return a;
90
}
91
95
* (1) glob pattern.
96
* (2) whitespace
97
* (3) whitespace separated list of attribute names, each of which
95
- * could be prefixed with '-' to mean "not set".
98
+ * could be prefixed with '-' to mean "set to false", '!' to mean
99
+ * "unset".
100
*/
101
102
+/* What does a matched pattern decide? */
103
struct attr_state {
99
- int unset;
104
struct git_attr *attr;
105
+ void *setto;
106
};
107
108
struct match_attr {
117
118
static const char blank[] = " \t\r\n";
119
120
+static const char *parse_attr(const char *src, int lineno, const char *cp,
121
+ int *num_attr, struct match_attr *res)
122
+{
123
+ const char *ep, *equals;
124
+ int len;
125
+
126
+ ep = cp + strcspn(cp, blank);
127
+ equals = strchr(cp, '=');
128
+ if (equals && ep < equals)
129
+ equals = NULL;
130
+ if (equals)
131
+ len = equals - cp;
132
+ else
133
+ len = ep - cp;
134
+ if (!res) {
135
+ if (*cp == '-' || *cp == '!') {
136
+ cp++;
137
+ len--;
138
+ }
139
+ if (invalid_attr_name(cp, len)) {
140
+ fprintf(stderr,
141
+ "%.*s is not a valid attribute name: %s:%d\n",
142
+ len, cp, src, lineno);
143
+ return NULL;
144
+ }
145
+ } else {
146
+ struct attr_state *e;
147
+
148
+ e = &(res->state[*num_attr]);
149
+ if (*cp == '-' || *cp == '!') {
150
+ e->setto = (*cp == '-') ? ATTR__FALSE : ATTR__UNSET;
151
+ cp++;
152
+ len--;
153
+ }
154
+ else if (!equals)
155
+ e->setto = ATTR__TRUE;
156
+ else {
157
+ char *value;
158
+ int vallen = ep - equals;
159
+ value = xmalloc(vallen);
160
+ memcpy(value, equals+1, vallen-1);
161
+ value[vallen-1] = 0;
162
+ e->setto = value;
163
+ }
164
+ e->attr = git_attr(cp, len);
165
+ }
166
+ (*num_attr)++;
167
+ return ep + strspn(ep, blank);
168
+}
169
+
170
static struct match_attr *parse_attr_line(const char *line, const char *src,
171
int lineno, int macro_ok)
172
{
173
int namelen;
174
int num_attr;
175
const char *cp, *name;
121
- struct match_attr *res = res;
176
+ struct match_attr *res = NULL;
177
int pass;
178
int is_macro;
179
208
num_attr = 0;
209
cp = name + namelen;
210
cp = cp + strspn(cp, blank);
156
- while (*cp) {
157
- const char *ep;
158
- ep = cp + strcspn(cp, blank);
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]);
173
- if (*cp == '-') {
174
- e->unset = 1;
175
- cp++;
176
- }
177
- e->attr = git_attr(cp, ep - cp);
178
- }
179
- num_attr++;
180
- cp = ep + strspn(ep, blank);
181
- }
211
+ while (*cp)
212
+ cp = parse_attr(src, lineno, cp, &num_attr, res);
213
if (pass)
214
break;
184
-
215
res = xcalloc(1,
216
sizeof(*res) +
217
sizeof(struct attr_state) * num_attr +
218
(is_macro ? 0 : namelen + 1));
189
- if (is_macro) {
219
+ if (is_macro)
220
res->u.attr = git_attr(name, namelen);
191
- }
221
else {
222
res->u.pattern = (char*)&(res->state[num_attr]);
223
memcpy(res->u.pattern, name, namelen);
234
* come from many places.
235
*
236
* (1) .gitattribute file of the same directory;
208
- * (2) .gitattribute file of the parent directory if (1) does not have any match;
209
- * this goes recursively upwards, just like .gitignore
210
- * (3) perhaps $GIT_DIR/info/attributes, as the final fallback.
237
+ * (2) .gitattribute file of the parent directory if (1) does not have
238
+ * any match; this goes recursively upwards, just like .gitignore.
239
+ * (3) $GIT_DIR/info/attributes, which overrides both of the above.
240
*
241
* In the same file, later entries override the earlier match, so in the
242
* global list, we would have entries from info/attributes the earliest
258
{
259
int i;
260
free(e->origin);
232
- for (i = 0; i < e->num_matches; i++)
233
- free(e->attrs[i]);
261
+ for (i = 0; i < e->num_matches; i++) {
262
+ struct match_attr *a = e->attrs[i];
263
+ int j;
264
+ for (j = 0; j < a->num_attr; j++) {
265
+ void *setto = a->state[j].setto;
266
+ if (setto == ATTR__TRUE ||
267
+ setto == ATTR__FALSE ||
268
+ setto == ATTR__UNSET ||
269
+ setto == ATTR__UNKNOWN)
270
+ ;
271
+ else
272
+ free(setto);
273
+ }
274
+ free(a);
275
+ }
276
free(e);
277
}
278
330
{
331
fprintf(stderr, "%s: %s\n", what, elem->origin ? elem->origin : "()");
332
}
291
-static void debug_set(const char *what, const char *match, struct git_attr *attr, int set)
333
+static void debug_set(const char *what, const char *match, struct git_attr *attr, void *v)
334
{
293
- fprintf(stderr, "%s: %s => %d (%s)\n",
294
- what, attr->name, set, match);
335
+ const char *value = v;
336
+
337
+ if (ATTR_TRUE(value))
338
+ value = "set";
339
+ else if (ATTR_FALSE(value))
340
+ value = "unset";
341
+ else if (ATTR_UNSET(value))
342
+ value = "unspecified";
343
+
344
+ fprintf(stderr, "%s: %s => %s (%s)\n",
345
+ what, attr->name, (char *) value, match);
346
}
347
#define debug_push(a) debug_info("push", (a))
348
#define debug_pop(a) debug_info("pop", (a))
471
return fnmatch(pattern, pathname + baselen, FNM_PATHNAME) == 0;
472
}
473
474
+static int fill_one(const char *what, struct match_attr *a, int rem)
475
+{
476
+ struct git_attr_check *check = check_all_attr;
477
+ int i;
478
+
479
+ for (i = 0; 0 < rem && i < a->num_attr; i++) {
480
+ struct git_attr *attr = a->state[i].attr;
481
+ void **n = &(check[attr->attr_nr].value);
482
+ void *v = a->state[i].setto;
483
+
484
+ if (*n == ATTR__UNKNOWN) {
485
+ debug_set(what, a->u.pattern, attr, v);
486
+ *n = v;
487
+ rem--;
488
+ }
489
+ }
490
+ return rem;
491
+}
492
+
493
static int fill(const char *path, int pathlen, struct attr_stack *stk, int rem)
494
{
495
+ int i;
496
const char *base = stk->origin ? stk->origin : "";
426
- int i, j;
427
- struct git_attr_check *check = check_all_attr;
497
498
for (i = stk->num_matches - 1; 0 < rem && 0 <= i; i--) {
499
struct match_attr *a = stk->attrs[i];
500
if (a->is_macro)
501
continue;
502
if (path_matches(path, pathlen,
434
- a->u.pattern, base, strlen(base))) {
435
- for (j = 0; 0 < rem && j < a->num_attr; j++) {
436
- struct git_attr *attr = a->state[j].attr;
437
- int set = !a->state[j].unset;
438
- int *n = &(check[attr->attr_nr].isset);
439
-
440
- if (*n < 0) {
441
- debug_set("fill", a->u.pattern, attr, set);
442
- *n = set;
443
- rem--;
444
- }
445
- }
446
- }
503
+ a->u.pattern, base, strlen(base)))
504
+ rem = fill_one("fill", a, rem);
505
}
506
return rem;
507
}
508
509
static int macroexpand(struct attr_stack *stk, int rem)
510
{
453
- int i, j;
511
+ int i;
512
struct git_attr_check *check = check_all_attr;
513
514
for (i = stk->num_matches - 1; 0 < rem && 0 <= i; i--) {
515
struct match_attr *a = stk->attrs[i];
516
if (!a->is_macro)
517
continue;
460
- if (check[a->u.attr->attr_nr].isset < 0)
518
+ if (check[a->u.attr->attr_nr].value != ATTR__TRUE)
519
continue;
462
- for (j = 0; 0 < rem && j < a->num_attr; j++) {
463
- struct git_attr *attr = a->state[j].attr;
464
- int set = !a->state[j].unset;
465
- int *n = &(check[attr->attr_nr].isset);
466
-
467
- if (*n < 0) {
468
- debug_set("expand", a->u.attr->name, attr, set);
469
- *n = set;
470
- rem--;
471
- }
472
- }
520
+ rem = fill_one("expand", a, rem);
521
}
522
return rem;
523
}
530
531
bootstrap_attr_stack();
532
for (i = 0; i < attr_nr; i++)
485
- check_all_attr[i].isset = -1;
533
+ check_all_attr[i].value = ATTR__UNKNOWN;
534
535
pathlen = strlen(path);
536
cp = strrchr(path, '/');
546
for (stk = attr_stack; 0 < rem && stk; stk = stk->prev)
547
rem = macroexpand(stk, rem);
548
501
- for (i = 0; i < num; i++)
502
- check[i].isset = check_all_attr[check[i].attr->attr_nr].isset;
549
+ for (i = 0; i < num; i++) {
550
+ void *value = check_all_attr[check[i].attr->attr_nr].value;
551
+ if (value == ATTR__UNKNOWN)
552
+ value = ATTR__UNSET;
553
+ check[i].value = value;
554
+ }
555
556
return 0;
557
}