1 #define NO_THE_INDEX_COMPATIBILITY_MACROS
5 const char git_attr__true
[] = "(builtin)true";
6 const char git_attr__false
[] = "\0(builtin)false";
7 static const char git_attr__unknown
[] = "(builtin)unknown";
8 #define ATTR__TRUE git_attr__true
9 #define ATTR__FALSE git_attr__false
10 #define ATTR__UNSET NULL
11 #define ATTR__UNKNOWN git_attr__unknown
14 * The basic design decision here is that we are not going to have
15 * insanely large number of attributes.
17 * This is a randomly chosen prime.
26 struct git_attr
*next
;
29 char name
[FLEX_ARRAY
];
33 static struct git_attr_check
*check_all_attr
;
34 static struct git_attr
*(git_attr_hash
[HASHSIZE
]);
36 static unsigned hash_name(const char *name
, int namelen
)
43 val
= ((val
<< 7) | (val
>> 22)) ^ c
;
48 static int invalid_attr_name(const char *name
, int namelen
)
51 * Attribute name cannot begin with '-' and from
52 * [-A-Za-z0-9_.]. We'd specifically exclude '=' for now,
53 * as we might later want to allow non-binary value for
54 * attributes, e.g. "*.svg merge=special-merge-program-for-svg"
60 if (! (ch
== '-' || ch
== '.' || ch
== '_' ||
61 ('0' <= ch
&& ch
<= '9') ||
62 ('a' <= ch
&& ch
<= 'z') ||
63 ('A' <= ch
&& ch
<= 'Z')) )
69 struct git_attr
*git_attr(const char *name
, int len
)
71 unsigned hval
= hash_name(name
, len
);
72 unsigned pos
= hval
% HASHSIZE
;
75 for (a
= git_attr_hash
[pos
]; a
; a
= a
->next
) {
77 !memcmp(a
->name
, name
, len
) && !a
->name
[len
])
81 if (invalid_attr_name(name
, len
))
84 a
= xmalloc(sizeof(*a
) + len
+ 1);
85 memcpy(a
->name
, name
, len
);
88 a
->next
= git_attr_hash
[pos
];
89 a
->attr_nr
= attr_nr
++;
90 git_attr_hash
[pos
] = a
;
92 check_all_attr
= xrealloc(check_all_attr
,
93 sizeof(*check_all_attr
) * attr_nr
);
94 check_all_attr
[a
->attr_nr
].attr
= a
;
95 check_all_attr
[a
->attr_nr
].value
= ATTR__UNKNOWN
;
100 * .gitattributes file is one line per record, each of which is
104 * (3) whitespace separated list of attribute names, each of which
105 * could be prefixed with '-' to mean "set to false", '!' to mean
109 /* What does a matched pattern decide? */
111 struct git_attr
*attr
;
118 struct git_attr
*attr
;
122 struct attr_state state
[FLEX_ARRAY
];
125 static const char blank
[] = " \t\r\n";
127 static const char *parse_attr(const char *src
, int lineno
, const char *cp
,
128 int *num_attr
, struct match_attr
*res
)
130 const char *ep
, *equals
;
133 ep
= cp
+ strcspn(cp
, blank
);
134 equals
= strchr(cp
, '=');
135 if (equals
&& ep
< equals
)
142 if (*cp
== '-' || *cp
== '!') {
146 if (invalid_attr_name(cp
, len
)) {
148 "%.*s is not a valid attribute name: %s:%d\n",
149 len
, cp
, src
, lineno
);
153 struct attr_state
*e
;
155 e
= &(res
->state
[*num_attr
]);
156 if (*cp
== '-' || *cp
== '!') {
157 e
->setto
= (*cp
== '-') ? ATTR__FALSE
: ATTR__UNSET
;
162 e
->setto
= ATTR__TRUE
;
164 e
->setto
= xmemdupz(equals
+ 1, ep
- equals
- 1);
166 e
->attr
= git_attr(cp
, len
);
169 return ep
+ strspn(ep
, blank
);
172 static struct match_attr
*parse_attr_line(const char *line
, const char *src
,
173 int lineno
, int macro_ok
)
177 const char *cp
, *name
;
178 struct match_attr
*res
= NULL
;
182 cp
= line
+ strspn(line
, blank
);
183 if (!*cp
|| *cp
== '#')
186 namelen
= strcspn(name
, blank
);
187 if (strlen(ATTRIBUTE_MACRO_PREFIX
) < namelen
&&
188 !prefixcmp(name
, ATTRIBUTE_MACRO_PREFIX
)) {
190 fprintf(stderr
, "%s not allowed: %s:%d\n",
195 name
+= strlen(ATTRIBUTE_MACRO_PREFIX
);
196 name
+= strspn(name
, blank
);
197 namelen
= strcspn(name
, blank
);
198 if (invalid_attr_name(name
, namelen
)) {
200 "%.*s is not a valid attribute name: %s:%d\n",
201 namelen
, name
, src
, lineno
);
208 for (pass
= 0; pass
< 2; pass
++) {
209 /* pass 0 counts and allocates, pass 1 fills */
212 cp
= cp
+ strspn(cp
, blank
);
214 cp
= parse_attr(src
, lineno
, cp
, &num_attr
, res
);
222 sizeof(struct attr_state
) * num_attr
+
223 (is_macro
? 0 : namelen
+ 1));
225 res
->u
.attr
= git_attr(name
, namelen
);
227 res
->u
.pattern
= (char *)&(res
->state
[num_attr
]);
228 memcpy(res
->u
.pattern
, name
, namelen
);
229 res
->u
.pattern
[namelen
] = 0;
231 res
->is_macro
= is_macro
;
232 res
->num_attr
= num_attr
;
238 * Like info/exclude and .gitignore, the attribute information can
239 * come from many places.
241 * (1) .gitattribute file of the same directory;
242 * (2) .gitattribute file of the parent directory if (1) does not have
243 * any match; this goes recursively upwards, just like .gitignore.
244 * (3) $GIT_DIR/info/attributes, which overrides both of the above.
246 * In the same file, later entries override the earlier match, so in the
247 * global list, we would have entries from info/attributes the earliest
248 * (reading the file from top to bottom), .gitattribute of the root
249 * directory (again, reading the file from top to bottom) down to the
250 * current directory, and then scan the list backwards to find the first match.
251 * This is exactly the same as what excluded() does in dir.c to deal with
255 static struct attr_stack
{
256 struct attr_stack
*prev
;
258 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
)
278 free((char *) setto
);
285 static const char *builtin_attr
[] = {
286 "[attr]binary -diff -crlf",
290 static void handle_attr_line(struct attr_stack
*res
,
296 struct match_attr
*a
;
298 a
= parse_attr_line(line
, src
, lineno
, macro_ok
);
301 if (res
->alloc
<= res
->num_matches
) {
302 res
->alloc
= alloc_nr(res
->num_matches
);
303 res
->attrs
= xrealloc(res
->attrs
,
304 sizeof(struct match_attr
*) *
307 res
->attrs
[res
->num_matches
++] = a
;
310 static struct attr_stack
*read_attr_from_array(const char **list
)
312 struct attr_stack
*res
;
316 res
= xcalloc(1, sizeof(*res
));
317 while ((line
= *(list
++)) != NULL
)
318 handle_attr_line(res
, line
, "[builtin]", ++lineno
, 1);
322 static enum git_attr_direction direction
;
323 static struct index_state
*use_index
;
325 static struct attr_stack
*read_attr_from_file(const char *path
, int macro_ok
)
327 FILE *fp
= fopen(path
, "r");
328 struct attr_stack
*res
;
334 res
= xcalloc(1, sizeof(*res
));
335 while (fgets(buf
, sizeof(buf
), fp
))
336 handle_attr_line(res
, buf
, path
, ++lineno
, macro_ok
);
341 static void *read_index_data(const char *path
)
345 enum object_type type
;
347 struct index_state
*istate
= use_index
? use_index
: &the_index
;
350 pos
= index_name_pos(istate
, path
, len
);
353 * We might be in the middle of a merge, in which
354 * case we would read stage #2 (ours).
358 (pos
< 0 && i
< istate
->cache_nr
&&
359 !strcmp(istate
->cache
[i
]->name
, path
));
361 if (ce_stage(istate
->cache
[i
]) == 2)
366 data
= read_sha1_file(istate
->cache
[pos
]->sha1
, &type
, &sz
);
367 if (!data
|| type
!= OBJ_BLOB
) {
374 static struct attr_stack
*read_attr_from_index(const char *path
, int macro_ok
)
376 struct attr_stack
*res
;
380 buf
= read_index_data(path
);
384 res
= xcalloc(1, sizeof(*res
));
385 for (sp
= buf
; *sp
; ) {
388 for (ep
= sp
; *ep
&& *ep
!= '\n'; ep
++)
390 more
= (*ep
== '\n');
392 handle_attr_line(res
, sp
, path
, ++lineno
, macro_ok
);
399 static struct attr_stack
*read_attr(const char *path
, int macro_ok
)
401 struct attr_stack
*res
;
403 if (direction
== GIT_ATTR_CHECKOUT
) {
404 res
= read_attr_from_index(path
, macro_ok
);
406 res
= read_attr_from_file(path
, macro_ok
);
408 else if (direction
== GIT_ATTR_CHECKIN
) {
409 res
= read_attr_from_file(path
, macro_ok
);
412 * There is no checked out .gitattributes file there, but
413 * we might have it in the index. We allow operation in a
414 * sparsely checked out work tree, so read from it.
416 res
= read_attr_from_index(path
, macro_ok
);
419 res
= read_attr_from_index(path
, macro_ok
);
421 res
= xcalloc(1, sizeof(*res
));
426 static void debug_info(const char *what
, struct attr_stack
*elem
)
428 fprintf(stderr
, "%s: %s\n", what
, elem
->origin
? elem
->origin
: "()");
430 static void debug_set(const char *what
, const char *match
, struct git_attr
*attr
, const void *v
)
432 const char *value
= v
;
434 if (ATTR_TRUE(value
))
436 else if (ATTR_FALSE(value
))
438 else if (ATTR_UNSET(value
))
439 value
= "unspecified";
441 fprintf(stderr
, "%s: %s => %s (%s)\n",
442 what
, attr
->name
, (char *) value
, match
);
444 #define debug_push(a) debug_info("push", (a))
445 #define debug_pop(a) debug_info("pop", (a))
447 #define debug_push(a) do { ; } while (0)
448 #define debug_pop(a) do { ; } while (0)
449 #define debug_set(a,b,c,d) do { ; } while (0)
452 static void drop_attr_stack(void)
455 struct attr_stack
*elem
= attr_stack
;
456 attr_stack
= elem
->prev
;
457 free_attr_elem(elem
);
461 static void bootstrap_attr_stack(void)
464 struct attr_stack
*elem
;
466 elem
= read_attr_from_array(builtin_attr
);
468 elem
->prev
= attr_stack
;
471 if (!is_bare_repository() || direction
== GIT_ATTR_INDEX
) {
472 elem
= read_attr(GITATTRIBUTES_FILE
, 1);
473 elem
->origin
= strdup("");
474 elem
->prev
= attr_stack
;
479 elem
= read_attr_from_file(git_path(INFOATTRIBUTES_FILE
), 1);
481 elem
= xcalloc(1, sizeof(*elem
));
483 elem
->prev
= attr_stack
;
488 static void prepare_attr_stack(const char *path
, int dirlen
)
490 struct attr_stack
*elem
, *info
;
492 struct strbuf pathbuf
;
494 strbuf_init(&pathbuf
, dirlen
+2+strlen(GITATTRIBUTES_FILE
));
497 * At the bottom of the attribute stack is the built-in
498 * set of attribute definitions. Then, contents from
499 * .gitattribute files from directories closer to the
500 * root to the ones in deeper directories are pushed
501 * to the stack. Finally, at the very top of the stack
502 * we always keep the contents of $GIT_DIR/info/attributes.
504 * When checking, we use entries from near the top of the
505 * stack, preferring $GIT_DIR/info/attributes, then
506 * .gitattributes in deeper directories to shallower ones,
507 * and finally use the built-in set as the default.
510 bootstrap_attr_stack();
513 * Pop the "info" one that is always at the top of the stack.
516 attr_stack
= info
->prev
;
519 * Pop the ones from directories that are not the prefix of
520 * the path we are checking.
522 while (attr_stack
&& attr_stack
->origin
) {
523 int namelen
= strlen(attr_stack
->origin
);
526 if (namelen
<= dirlen
&&
527 !strncmp(elem
->origin
, path
, namelen
))
531 attr_stack
= elem
->prev
;
532 free_attr_elem(elem
);
536 * Read from parent directories and push them down
538 if (!is_bare_repository() || direction
== GIT_ATTR_INDEX
) {
542 len
= strlen(attr_stack
->origin
);
545 strbuf_reset(&pathbuf
);
546 strbuf_add(&pathbuf
, path
, dirlen
);
547 strbuf_addch(&pathbuf
, '/');
548 cp
= strchr(pathbuf
.buf
+ len
+ 1, '/');
549 strcpy(cp
+ 1, GITATTRIBUTES_FILE
);
550 elem
= read_attr(pathbuf
.buf
, 0);
552 elem
->origin
= strdup(pathbuf
.buf
);
553 elem
->prev
= attr_stack
;
560 * Finally push the "info" one at the top of the stack.
562 info
->prev
= attr_stack
;
566 static int path_matches(const char *pathname
, int pathlen
,
568 const char *base
, int baselen
)
570 if (!strchr(pattern
, '/')) {
572 const char *basename
= strrchr(pathname
, '/');
573 basename
= basename
? basename
+ 1 : pathname
;
574 return (fnmatch(pattern
, basename
, 0) == 0);
577 * match with FNM_PATHNAME; the pattern has base implicitly
582 if (pathlen
< baselen
||
583 (baselen
&& pathname
[baselen
] != '/') ||
584 strncmp(pathname
, base
, baselen
))
588 return fnmatch(pattern
, pathname
+ baselen
, FNM_PATHNAME
) == 0;
591 static int fill_one(const char *what
, struct match_attr
*a
, int rem
)
593 struct git_attr_check
*check
= check_all_attr
;
596 for (i
= 0; 0 < rem
&& i
< a
->num_attr
; i
++) {
597 struct git_attr
*attr
= a
->state
[i
].attr
;
598 const char **n
= &(check
[attr
->attr_nr
].value
);
599 const char *v
= a
->state
[i
].setto
;
601 if (*n
== ATTR__UNKNOWN
) {
602 debug_set(what
, a
->u
.pattern
, attr
, v
);
610 static int fill(const char *path
, int pathlen
, struct attr_stack
*stk
, int rem
)
613 const char *base
= stk
->origin
? stk
->origin
: "";
615 for (i
= stk
->num_matches
- 1; 0 < rem
&& 0 <= i
; i
--) {
616 struct match_attr
*a
= stk
->attrs
[i
];
619 if (path_matches(path
, pathlen
,
620 a
->u
.pattern
, base
, strlen(base
)))
621 rem
= fill_one("fill", a
, rem
);
626 static int macroexpand(struct attr_stack
*stk
, int rem
)
629 struct git_attr_check
*check
= check_all_attr
;
631 for (i
= stk
->num_matches
- 1; 0 < rem
&& 0 <= i
; i
--) {
632 struct match_attr
*a
= stk
->attrs
[i
];
635 if (check
[a
->u
.attr
->attr_nr
].value
!= ATTR__TRUE
)
637 rem
= fill_one("expand", a
, rem
);
642 int git_checkattr(const char *path
, int num
, struct git_attr_check
*check
)
644 struct attr_stack
*stk
;
646 int dirlen
, pathlen
, i
, rem
;
648 bootstrap_attr_stack();
649 for (i
= 0; i
< attr_nr
; i
++)
650 check_all_attr
[i
].value
= ATTR__UNKNOWN
;
652 pathlen
= strlen(path
);
653 cp
= strrchr(path
, '/');
658 prepare_attr_stack(path
, dirlen
);
660 for (stk
= attr_stack
; 0 < rem
&& stk
; stk
= stk
->prev
)
661 rem
= fill(path
, pathlen
, stk
, rem
);
663 for (stk
= attr_stack
; 0 < rem
&& stk
; stk
= stk
->prev
)
664 rem
= macroexpand(stk
, rem
);
666 for (i
= 0; i
< num
; i
++) {
667 const char *value
= check_all_attr
[check
[i
].attr
->attr_nr
].value
;
668 if (value
== ATTR__UNKNOWN
)
670 check
[i
].value
= value
;
676 void git_attr_set_direction(enum git_attr_direction
new, struct index_state
*istate
)
678 enum git_attr_direction old
= direction
;
680 if (is_bare_repository() && new != GIT_ATTR_INDEX
)
681 die("BUG: non-INDEX attr direction in a bare repo");