5 * The basic design decision here is that we are not going to have
6 * insanely large number of attributes.
8 * This is a randomly chosen prime.
17 struct git_attr
*next
;
20 char name
[FLEX_ARRAY
];
24 static struct git_attr_check
*check_all_attr
;
25 static struct git_attr
*(git_attr_hash
[HASHSIZE
]);
27 static unsigned hash_name(const char *name
, int namelen
)
34 val
= ((val
<< 7) | (val
>> 22)) ^ c
;
39 static int invalid_attr_name(const char *name
, int namelen
)
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"
51 if (! (ch
== '-' || ch
== '.' || ch
== '_' ||
52 ('0' <= ch
&& ch
<= '9') ||
53 ('a' <= ch
&& ch
<= 'z') ||
54 ('A' <= ch
&& ch
<= 'Z')) )
60 struct git_attr
*git_attr(const char *name
, int len
)
62 unsigned hval
= hash_name(name
, len
);
63 unsigned pos
= hval
% HASHSIZE
;
66 for (a
= git_attr_hash
[pos
]; a
; a
= a
->next
) {
68 !memcmp(a
->name
, name
, len
) && !a
->name
[len
])
72 if (invalid_attr_name(name
, len
))
75 a
= xmalloc(sizeof(*a
) + len
+ 1);
76 memcpy(a
->name
, name
, len
);
79 a
->next
= git_attr_hash
[pos
];
80 a
->attr_nr
= attr_nr
++;
81 git_attr_hash
[pos
] = a
;
83 check_all_attr
= xrealloc(check_all_attr
,
84 sizeof(*check_all_attr
) * attr_nr
);
85 check_all_attr
[a
->attr_nr
].attr
= a
;
90 * .gitattributes file is one line per record, each of which is
94 * (3) whitespace separated list of attribute names, each of which
95 * could be prefixed with '-' to mean "not set".
100 struct git_attr
*attr
;
106 struct git_attr
*attr
;
110 struct attr_state state
[FLEX_ARRAY
];
113 static const char blank
[] = " \t\r\n";
115 static struct match_attr
*parse_attr_line(const char *line
, const char *src
,
116 int lineno
, int macro_ok
)
120 const char *cp
, *name
;
121 struct match_attr
*res
= res
;
125 cp
= line
+ strspn(line
, blank
);
126 if (!*cp
|| *cp
== '#')
129 namelen
= strcspn(name
, blank
);
130 if (strlen(ATTRIBUTE_MACRO_PREFIX
) < namelen
&&
131 !prefixcmp(name
, ATTRIBUTE_MACRO_PREFIX
)) {
133 fprintf(stderr
, "%s not allowed: %s:%d\n",
138 name
+= strlen(ATTRIBUTE_MACRO_PREFIX
);
139 name
+= strspn(name
, blank
);
140 namelen
= strcspn(name
, blank
);
141 if (invalid_attr_name(name
, namelen
)) {
143 "%.*s is not a valid attribute name: %s:%d\n",
144 namelen
, name
, src
, lineno
);
151 for (pass
= 0; pass
< 2; pass
++) {
152 /* pass 0 counts and allocates, pass 1 fills */
155 cp
= cp
+ strspn(cp
, blank
);
158 ep
= cp
+ strcspn(cp
, blank
);
162 if (invalid_attr_name(cp
, ep
- cp
)) {
164 "%.*s is not a valid attribute name: %s:%d\n",
170 struct attr_state
*e
;
172 e
= &(res
->state
[num_attr
]);
177 e
->attr
= git_attr(cp
, ep
- cp
);
180 cp
= ep
+ strspn(ep
, blank
);
187 sizeof(struct attr_state
) * num_attr
+
188 (is_macro
? 0 : namelen
+ 1));
190 res
->u
.attr
= git_attr(name
, namelen
);
193 res
->u
.pattern
= (char*)&(res
->state
[num_attr
]);
194 memcpy(res
->u
.pattern
, name
, namelen
);
195 res
->u
.pattern
[namelen
] = 0;
197 res
->is_macro
= is_macro
;
198 res
->num_attr
= num_attr
;
204 * Like info/exclude and .gitignore, the attribute information can
205 * come from many places.
207 * (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.
212 * In the same file, later entries override the earlier match, so in the
213 * global list, we would have entries from info/attributes the earliest
214 * (reading the file from top to bottom), .gitattribute of the root
215 * directory (again, reading the file from top to bottom) down to the
216 * current directory, and then scan the list backwards to find the first match.
217 * This is exactly the same as what excluded() does in dir.c to deal with
221 static struct attr_stack
{
222 struct attr_stack
*prev
;
224 unsigned num_matches
;
225 struct match_attr
**attrs
;
228 static void free_attr_elem(struct attr_stack
*e
)
232 for (i
= 0; i
< e
->num_matches
; i
++)
237 static const char *builtin_attr
[] = {
238 "[attr]binary -diff -crlf",
242 static struct attr_stack
*read_attr_from_array(const char **list
)
244 struct attr_stack
*res
;
248 res
= xcalloc(1, sizeof(*res
));
249 while ((line
= *(list
++)) != NULL
) {
250 struct match_attr
*a
;
252 a
= parse_attr_line(line
, "[builtin]", ++lineno
, 1);
255 res
->attrs
= xrealloc(res
->attrs
, res
->num_matches
+ 1);
256 res
->attrs
[res
->num_matches
++] = a
;
261 static struct attr_stack
*read_attr_from_file(const char *path
, int macro_ok
)
264 struct attr_stack
*res
;
268 res
= xcalloc(1, sizeof(*res
));
269 fp
= fopen(path
, "r");
273 while (fgets(buf
, sizeof(buf
), fp
)) {
274 struct match_attr
*a
;
276 a
= parse_attr_line(buf
, path
, ++lineno
, macro_ok
);
279 res
->attrs
= xrealloc(res
->attrs
, res
->num_matches
+ 1);
280 res
->attrs
[res
->num_matches
++] = a
;
287 static void debug_info(const char *what
, struct attr_stack
*elem
)
289 fprintf(stderr
, "%s: %s\n", what
, elem
->origin
? elem
->origin
: "()");
291 static void debug_set(const char *what
, const char *match
, struct git_attr
*attr
, int set
)
293 fprintf(stderr
, "%s: %s => %d (%s)\n",
294 what
, attr
->name
, set
, match
);
296 #define debug_push(a) debug_info("push", (a))
297 #define debug_pop(a) debug_info("pop", (a))
299 #define debug_push(a) do { ; } while (0)
300 #define debug_pop(a) do { ; } while (0)
301 #define debug_set(a,b,c,d) do { ; } while (0)
304 static void bootstrap_attr_stack(void)
307 struct attr_stack
*elem
;
309 elem
= read_attr_from_array(builtin_attr
);
311 elem
->prev
= attr_stack
;
314 elem
= read_attr_from_file(GITATTRIBUTES_FILE
, 1);
315 elem
->origin
= strdup("");
316 elem
->prev
= attr_stack
;
320 elem
= read_attr_from_file(git_path(INFOATTRIBUTES_FILE
), 1);
322 elem
->prev
= attr_stack
;
327 static void prepare_attr_stack(const char *path
, int dirlen
)
329 struct attr_stack
*elem
, *info
;
331 char pathbuf
[PATH_MAX
];
334 * At the bottom of the attribute stack is the built-in
335 * set of attribute definitions. Then, contents from
336 * .gitattribute files from directories closer to the
337 * root to the ones in deeper directories are pushed
338 * to the stack. Finally, at the very top of the stack
339 * we always keep the contents of $GIT_DIR/info/attributes.
341 * When checking, we use entries from near the top of the
342 * stack, preferring $GIT_DIR/info/attributes, then
343 * .gitattributes in deeper directories to shallower ones,
344 * and finally use the built-in set as the default.
347 bootstrap_attr_stack();
350 * Pop the "info" one that is always at the top of the stack.
353 attr_stack
= info
->prev
;
356 * Pop the ones from directories that are not the prefix of
357 * the path we are checking.
359 while (attr_stack
&& attr_stack
->origin
) {
360 int namelen
= strlen(attr_stack
->origin
);
363 if (namelen
<= dirlen
&&
364 !strncmp(elem
->origin
, path
, namelen
))
368 attr_stack
= elem
->prev
;
369 free_attr_elem(elem
);
373 * Read from parent directories and push them down
378 len
= strlen(attr_stack
->origin
);
381 memcpy(pathbuf
, path
, dirlen
);
382 memcpy(pathbuf
+ dirlen
, "/", 2);
383 cp
= strchr(pathbuf
+ len
+ 1, '/');
384 strcpy(cp
+ 1, GITATTRIBUTES_FILE
);
385 elem
= read_attr_from_file(pathbuf
, 0);
387 elem
->origin
= strdup(pathbuf
);
388 elem
->prev
= attr_stack
;
394 * Finally push the "info" one at the top of the stack.
396 info
->prev
= attr_stack
;
400 static int path_matches(const char *pathname
, int pathlen
,
402 const char *base
, int baselen
)
404 if (!strchr(pattern
, '/')) {
406 const char *basename
= strrchr(pathname
, '/');
407 basename
= basename
? basename
+ 1 : pathname
;
408 return (fnmatch(pattern
, basename
, 0) == 0);
411 * match with FNM_PATHNAME; the pattern has base implicitly
416 if (pathlen
< baselen
||
417 (baselen
&& pathname
[baselen
- 1] != '/') ||
418 strncmp(pathname
, base
, baselen
))
420 return fnmatch(pattern
, pathname
+ baselen
, FNM_PATHNAME
) == 0;
423 static int fill(const char *path
, int pathlen
, struct attr_stack
*stk
, int rem
)
425 const char *base
= stk
->origin
? stk
->origin
: "";
427 struct git_attr_check
*check
= check_all_attr
;
429 for (i
= stk
->num_matches
- 1; 0 < rem
&& 0 <= i
; i
--) {
430 struct match_attr
*a
= stk
->attrs
[i
];
433 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
);
441 debug_set("fill", a
->u
.pattern
, attr
, set
);
451 static int macroexpand(struct attr_stack
*stk
, int rem
)
454 struct git_attr_check
*check
= check_all_attr
;
456 for (i
= stk
->num_matches
- 1; 0 < rem
&& 0 <= i
; i
--) {
457 struct match_attr
*a
= stk
->attrs
[i
];
460 if (check
[a
->u
.attr
->attr_nr
].isset
< 0)
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
);
468 debug_set("expand", a
->u
.attr
->name
, attr
, set
);
477 int git_checkattr(const char *path
, int num
, struct git_attr_check
*check
)
479 struct attr_stack
*stk
;
481 int dirlen
, pathlen
, i
, rem
;
483 bootstrap_attr_stack();
484 for (i
= 0; i
< attr_nr
; i
++)
485 check_all_attr
[i
].isset
= -1;
487 pathlen
= strlen(path
);
488 cp
= strrchr(path
, '/');
493 prepare_attr_stack(path
, dirlen
);
495 for (stk
= attr_stack
; 0 < rem
&& stk
; stk
= stk
->prev
)
496 rem
= fill(path
, pathlen
, stk
, rem
);
498 for (stk
= attr_stack
; 0 < rem
&& stk
; stk
= stk
->prev
)
499 rem
= macroexpand(stk
, rem
);
501 for (i
= 0; i
< num
; i
++)
502 check
[i
].isset
= check_all_attr
[check
[i
].attr
->attr_nr
].isset
;