4 const char git_attr__true
[] = "(builtin)true";
5 const char git_attr__false
[] = "\0(builtin)false";
6 static const char git_attr__unknown
[] = "(builtin)unknown";
7 #define ATTR__TRUE git_attr__true
8 #define ATTR__FALSE git_attr__false
9 #define ATTR__UNSET NULL
10 #define ATTR__UNKNOWN git_attr__unknown
13 * The basic design decision here is that we are not going to have
14 * insanely large number of attributes.
16 * This is a randomly chosen prime.
25 struct git_attr
*next
;
28 char name
[FLEX_ARRAY
];
32 static struct git_attr_check
*check_all_attr
;
33 static struct git_attr
*(git_attr_hash
[HASHSIZE
]);
35 static unsigned hash_name(const char *name
, int namelen
)
42 val
= ((val
<< 7) | (val
>> 22)) ^ c
;
47 static int invalid_attr_name(const char *name
, int namelen
)
50 * Attribute name cannot begin with '-' and from
51 * [-A-Za-z0-9_.]. We'd specifically exclude '=' for now,
52 * as we might later want to allow non-binary value for
53 * attributes, e.g. "*.svg merge=special-merge-program-for-svg"
59 if (! (ch
== '-' || ch
== '.' || ch
== '_' ||
60 ('0' <= ch
&& ch
<= '9') ||
61 ('a' <= ch
&& ch
<= 'z') ||
62 ('A' <= ch
&& ch
<= 'Z')) )
68 struct git_attr
*git_attr(const char *name
, int len
)
70 unsigned hval
= hash_name(name
, len
);
71 unsigned pos
= hval
% HASHSIZE
;
74 for (a
= git_attr_hash
[pos
]; a
; a
= a
->next
) {
76 !memcmp(a
->name
, name
, len
) && !a
->name
[len
])
80 if (invalid_attr_name(name
, len
))
83 a
= xmalloc(sizeof(*a
) + len
+ 1);
84 memcpy(a
->name
, name
, len
);
87 a
->next
= git_attr_hash
[pos
];
88 a
->attr_nr
= attr_nr
++;
89 git_attr_hash
[pos
] = a
;
91 check_all_attr
= xrealloc(check_all_attr
,
92 sizeof(*check_all_attr
) * attr_nr
);
93 check_all_attr
[a
->attr_nr
].attr
= a
;
94 check_all_attr
[a
->attr_nr
].value
= ATTR__UNKNOWN
;
99 * .gitattributes file is one line per record, each of which is
103 * (3) whitespace separated list of attribute names, each of which
104 * could be prefixed with '-' to mean "set to false", '!' to mean
108 /* What does a matched pattern decide? */
110 struct git_attr
*attr
;
117 struct git_attr
*attr
;
121 struct attr_state state
[FLEX_ARRAY
];
124 static const char blank
[] = " \t\r\n";
126 static const char *parse_attr(const char *src
, int lineno
, const char *cp
,
127 int *num_attr
, struct match_attr
*res
)
129 const char *ep
, *equals
;
132 ep
= cp
+ strcspn(cp
, blank
);
133 equals
= strchr(cp
, '=');
134 if (equals
&& ep
< equals
)
141 if (*cp
== '-' || *cp
== '!') {
145 if (invalid_attr_name(cp
, len
)) {
147 "%.*s is not a valid attribute name: %s:%d\n",
148 len
, cp
, src
, lineno
);
152 struct attr_state
*e
;
154 e
= &(res
->state
[*num_attr
]);
155 if (*cp
== '-' || *cp
== '!') {
156 e
->setto
= (*cp
== '-') ? ATTR__FALSE
: ATTR__UNSET
;
161 e
->setto
= ATTR__TRUE
;
164 int vallen
= ep
- equals
;
165 value
= xmalloc(vallen
);
166 memcpy(value
, equals
+1, vallen
-1);
170 e
->attr
= git_attr(cp
, len
);
173 return ep
+ strspn(ep
, blank
);
176 static struct match_attr
*parse_attr_line(const char *line
, const char *src
,
177 int lineno
, int macro_ok
)
181 const char *cp
, *name
;
182 struct match_attr
*res
= NULL
;
186 cp
= line
+ strspn(line
, blank
);
187 if (!*cp
|| *cp
== '#')
190 namelen
= strcspn(name
, blank
);
191 if (strlen(ATTRIBUTE_MACRO_PREFIX
) < namelen
&&
192 !prefixcmp(name
, ATTRIBUTE_MACRO_PREFIX
)) {
194 fprintf(stderr
, "%s not allowed: %s:%d\n",
199 name
+= strlen(ATTRIBUTE_MACRO_PREFIX
);
200 name
+= strspn(name
, blank
);
201 namelen
= strcspn(name
, blank
);
202 if (invalid_attr_name(name
, namelen
)) {
204 "%.*s is not a valid attribute name: %s:%d\n",
205 namelen
, name
, src
, lineno
);
212 for (pass
= 0; pass
< 2; pass
++) {
213 /* pass 0 counts and allocates, pass 1 fills */
216 cp
= cp
+ strspn(cp
, blank
);
218 cp
= parse_attr(src
, lineno
, cp
, &num_attr
, res
);
223 sizeof(struct attr_state
) * num_attr
+
224 (is_macro
? 0 : namelen
+ 1));
226 res
->u
.attr
= git_attr(name
, namelen
);
228 res
->u
.pattern
= (char*)&(res
->state
[num_attr
]);
229 memcpy(res
->u
.pattern
, name
, namelen
);
230 res
->u
.pattern
[namelen
] = 0;
232 res
->is_macro
= is_macro
;
233 res
->num_attr
= num_attr
;
239 * Like info/exclude and .gitignore, the attribute information can
240 * come from many places.
242 * (1) .gitattribute file of the same directory;
243 * (2) .gitattribute file of the parent directory if (1) does not have
244 * any match; this goes recursively upwards, just like .gitignore.
245 * (3) $GIT_DIR/info/attributes, which overrides both of the above.
247 * In the same file, later entries override the earlier match, so in the
248 * global list, we would have entries from info/attributes the earliest
249 * (reading the file from top to bottom), .gitattribute of the root
250 * directory (again, reading the file from top to bottom) down to the
251 * current directory, and then scan the list backwards to find the first match.
252 * This is exactly the same as what excluded() does in dir.c to deal with
256 static struct attr_stack
{
257 struct attr_stack
*prev
;
259 unsigned num_matches
;
260 struct match_attr
**attrs
;
263 static void free_attr_elem(struct attr_stack
*e
)
267 for (i
= 0; i
< e
->num_matches
; i
++) {
268 struct match_attr
*a
= e
->attrs
[i
];
270 for (j
= 0; j
< a
->num_attr
; j
++) {
271 const char *setto
= a
->state
[j
].setto
;
272 if (setto
== ATTR__TRUE
||
273 setto
== ATTR__FALSE
||
274 setto
== ATTR__UNSET
||
275 setto
== ATTR__UNKNOWN
)
285 static const char *builtin_attr
[] = {
286 "[attr]binary -diff -crlf",
290 static struct attr_stack
*read_attr_from_array(const char **list
)
292 struct attr_stack
*res
;
296 res
= xcalloc(1, sizeof(*res
));
297 while ((line
= *(list
++)) != NULL
) {
298 struct match_attr
*a
;
300 a
= parse_attr_line(line
, "[builtin]", ++lineno
, 1);
303 res
->attrs
= xrealloc(res
->attrs
, res
->num_matches
+ 1);
304 res
->attrs
[res
->num_matches
++] = a
;
309 static struct attr_stack
*read_attr_from_file(const char *path
, int macro_ok
)
312 struct attr_stack
*res
;
316 res
= xcalloc(1, sizeof(*res
));
317 fp
= fopen(path
, "r");
321 while (fgets(buf
, sizeof(buf
), fp
)) {
322 struct match_attr
*a
;
324 a
= parse_attr_line(buf
, path
, ++lineno
, macro_ok
);
327 res
->attrs
= xrealloc(res
->attrs
, res
->num_matches
+ 1);
328 res
->attrs
[res
->num_matches
++] = a
;
335 static void debug_info(const char *what
, struct attr_stack
*elem
)
337 fprintf(stderr
, "%s: %s\n", what
, elem
->origin
? elem
->origin
: "()");
339 static void debug_set(const char *what
, const char *match
, struct git_attr
*attr
, void *v
)
341 const char *value
= v
;
343 if (ATTR_TRUE(value
))
345 else if (ATTR_FALSE(value
))
347 else if (ATTR_UNSET(value
))
348 value
= "unspecified";
350 fprintf(stderr
, "%s: %s => %s (%s)\n",
351 what
, attr
->name
, (char *) value
, match
);
353 #define debug_push(a) debug_info("push", (a))
354 #define debug_pop(a) debug_info("pop", (a))
356 #define debug_push(a) do { ; } while (0)
357 #define debug_pop(a) do { ; } while (0)
358 #define debug_set(a,b,c,d) do { ; } while (0)
361 static void bootstrap_attr_stack(void)
364 struct attr_stack
*elem
;
366 elem
= read_attr_from_array(builtin_attr
);
368 elem
->prev
= attr_stack
;
371 elem
= read_attr_from_file(GITATTRIBUTES_FILE
, 1);
372 elem
->origin
= strdup("");
373 elem
->prev
= attr_stack
;
377 elem
= read_attr_from_file(git_path(INFOATTRIBUTES_FILE
), 1);
379 elem
->prev
= attr_stack
;
384 static void prepare_attr_stack(const char *path
, int dirlen
)
386 struct attr_stack
*elem
, *info
;
388 char pathbuf
[PATH_MAX
];
391 * At the bottom of the attribute stack is the built-in
392 * set of attribute definitions. Then, contents from
393 * .gitattribute files from directories closer to the
394 * root to the ones in deeper directories are pushed
395 * to the stack. Finally, at the very top of the stack
396 * we always keep the contents of $GIT_DIR/info/attributes.
398 * When checking, we use entries from near the top of the
399 * stack, preferring $GIT_DIR/info/attributes, then
400 * .gitattributes in deeper directories to shallower ones,
401 * and finally use the built-in set as the default.
404 bootstrap_attr_stack();
407 * Pop the "info" one that is always at the top of the stack.
410 attr_stack
= info
->prev
;
413 * Pop the ones from directories that are not the prefix of
414 * the path we are checking.
416 while (attr_stack
&& attr_stack
->origin
) {
417 int namelen
= strlen(attr_stack
->origin
);
420 if (namelen
<= dirlen
&&
421 !strncmp(elem
->origin
, path
, namelen
))
425 attr_stack
= elem
->prev
;
426 free_attr_elem(elem
);
430 * Read from parent directories and push them down
435 len
= strlen(attr_stack
->origin
);
438 memcpy(pathbuf
, path
, dirlen
);
439 memcpy(pathbuf
+ dirlen
, "/", 2);
440 cp
= strchr(pathbuf
+ len
+ 1, '/');
441 strcpy(cp
+ 1, GITATTRIBUTES_FILE
);
442 elem
= read_attr_from_file(pathbuf
, 0);
444 elem
->origin
= strdup(pathbuf
);
445 elem
->prev
= attr_stack
;
451 * Finally push the "info" one at the top of the stack.
453 info
->prev
= attr_stack
;
457 static int path_matches(const char *pathname
, int pathlen
,
459 const char *base
, int baselen
)
461 if (!strchr(pattern
, '/')) {
463 const char *basename
= strrchr(pathname
, '/');
464 basename
= basename
? basename
+ 1 : pathname
;
465 return (fnmatch(pattern
, basename
, 0) == 0);
468 * match with FNM_PATHNAME; the pattern has base implicitly
473 if (pathlen
< baselen
||
474 (baselen
&& pathname
[baselen
- 1] != '/') ||
475 strncmp(pathname
, base
, baselen
))
477 return fnmatch(pattern
, pathname
+ baselen
, FNM_PATHNAME
) == 0;
480 static int fill_one(const char *what
, struct match_attr
*a
, int rem
)
482 struct git_attr_check
*check
= check_all_attr
;
485 for (i
= 0; 0 < rem
&& i
< a
->num_attr
; i
++) {
486 struct git_attr
*attr
= a
->state
[i
].attr
;
487 const char **n
= &(check
[attr
->attr_nr
].value
);
488 const char *v
= a
->state
[i
].setto
;
490 if (*n
== ATTR__UNKNOWN
) {
491 debug_set(what
, a
->u
.pattern
, attr
, v
);
499 static int fill(const char *path
, int pathlen
, struct attr_stack
*stk
, int rem
)
502 const char *base
= stk
->origin
? stk
->origin
: "";
504 for (i
= stk
->num_matches
- 1; 0 < rem
&& 0 <= i
; i
--) {
505 struct match_attr
*a
= stk
->attrs
[i
];
508 if (path_matches(path
, pathlen
,
509 a
->u
.pattern
, base
, strlen(base
)))
510 rem
= fill_one("fill", a
, rem
);
515 static int macroexpand(struct attr_stack
*stk
, int rem
)
518 struct git_attr_check
*check
= check_all_attr
;
520 for (i
= stk
->num_matches
- 1; 0 < rem
&& 0 <= i
; i
--) {
521 struct match_attr
*a
= stk
->attrs
[i
];
524 if (check
[a
->u
.attr
->attr_nr
].value
!= ATTR__TRUE
)
526 rem
= fill_one("expand", a
, rem
);
531 int git_checkattr(const char *path
, int num
, struct git_attr_check
*check
)
533 struct attr_stack
*stk
;
535 int dirlen
, pathlen
, i
, rem
;
537 bootstrap_attr_stack();
538 for (i
= 0; i
< attr_nr
; i
++)
539 check_all_attr
[i
].value
= ATTR__UNKNOWN
;
541 pathlen
= strlen(path
);
542 cp
= strrchr(path
, '/');
547 prepare_attr_stack(path
, dirlen
);
549 for (stk
= attr_stack
; 0 < rem
&& stk
; stk
= stk
->prev
)
550 rem
= fill(path
, pathlen
, stk
, rem
);
552 for (stk
= attr_stack
; 0 < rem
&& stk
; stk
= stk
->prev
)
553 rem
= macroexpand(stk
, rem
);
555 for (i
= 0; i
< num
; i
++) {
556 const char *value
= check_all_attr
[check
[i
].attr
->attr_nr
].value
;
557 if (value
== ATTR__UNKNOWN
)
559 check
[i
].value
= value
;