2 * Handle git attributes. See gitattributes(5) for a description of
3 * the file syntax, and Documentation/technical/api-gitattributes.txt
4 * for a description of the API.
6 * One basic design decision here is that we are not going to support
7 * an insanely large number of attributes.
10 #define NO_THE_INDEX_COMPATIBILITY_MACROS
17 const char git_attr__true
[] = "(builtin)true";
18 const char git_attr__false
[] = "\0(builtin)false";
19 static const char git_attr__unknown
[] = "(builtin)unknown";
20 #define ATTR__TRUE git_attr__true
21 #define ATTR__FALSE git_attr__false
22 #define ATTR__UNSET NULL
23 #define ATTR__UNKNOWN git_attr__unknown
25 /* This is a randomly chosen prime. */
33 struct git_attr
*next
;
38 char name
[FLEX_ARRAY
];
41 static int cannot_trust_maybe_real
;
43 static struct git_attr_check
*check_all_attr
;
44 static struct git_attr
*(git_attr_hash
[HASHSIZE
]);
46 char *git_attr_name(struct git_attr
*attr
)
51 static unsigned hash_name(const char *name
, int namelen
)
57 val
= ((val
<< 7) | (val
>> 22)) ^ c
;
62 static int invalid_attr_name(const char *name
, int namelen
)
65 * Attribute name cannot begin with '-' and must consist of
66 * characters from [-A-Za-z0-9_.].
68 if (namelen
<= 0 || *name
== '-')
72 if (! (ch
== '-' || ch
== '.' || ch
== '_' ||
73 ('0' <= ch
&& ch
<= '9') ||
74 ('a' <= ch
&& ch
<= 'z') ||
75 ('A' <= ch
&& ch
<= 'Z')) )
81 static struct git_attr
*git_attr_internal(const char *name
, int len
)
83 unsigned hval
= hash_name(name
, len
);
84 unsigned pos
= hval
% HASHSIZE
;
87 for (a
= git_attr_hash
[pos
]; a
; a
= a
->next
) {
89 !memcmp(a
->name
, name
, len
) && !a
->name
[len
])
93 if (invalid_attr_name(name
, len
))
96 a
= xmalloc(sizeof(*a
) + len
+ 1);
97 memcpy(a
->name
, name
, len
);
100 a
->next
= git_attr_hash
[pos
];
101 a
->attr_nr
= attr_nr
++;
104 git_attr_hash
[pos
] = a
;
106 REALLOC_ARRAY(check_all_attr
, attr_nr
);
107 check_all_attr
[a
->attr_nr
].attr
= a
;
108 check_all_attr
[a
->attr_nr
].value
= ATTR__UNKNOWN
;
112 struct git_attr
*git_attr(const char *name
)
114 return git_attr_internal(name
, strlen(name
));
117 /* What does a matched pattern decide? */
119 struct git_attr
*attr
;
127 int flags
; /* EXC_FLAG_* */
131 * One rule, as from a .gitattributes file.
133 * If is_macro is true, then u.attr is a pointer to the git_attr being
136 * If is_macro is false, then u.pattern points at the filename pattern
137 * to which the rule applies. (The memory pointed to is part of the
138 * memory block allocated for the match_attr instance.)
140 * In either case, num_attr is the number of attributes affected by
141 * this rule, and state is an array listing them. The attributes are
142 * listed as they appear in the file (macros unexpanded).
147 struct git_attr
*attr
;
151 struct attr_state state
[FLEX_ARRAY
];
154 static const char blank
[] = " \t\r\n";
157 * Parse a whitespace-delimited attribute state (i.e., "attr",
158 * "-attr", "!attr", or "attr=value") from the string starting at src.
159 * If e is not NULL, write the results to *e. Return a pointer to the
160 * remainder of the string (with leading whitespace removed), or NULL
161 * if there was an error.
163 static const char *parse_attr(const char *src
, int lineno
, const char *cp
,
164 struct attr_state
*e
)
166 const char *ep
, *equals
;
169 ep
= cp
+ strcspn(cp
, blank
);
170 equals
= strchr(cp
, '=');
171 if (equals
&& ep
< equals
)
178 if (*cp
== '-' || *cp
== '!') {
182 if (invalid_attr_name(cp
, len
)) {
184 "%.*s is not a valid attribute name: %s:%d\n",
185 len
, cp
, src
, lineno
);
189 if (*cp
== '-' || *cp
== '!') {
190 e
->setto
= (*cp
== '-') ? ATTR__FALSE
: ATTR__UNSET
;
195 e
->setto
= ATTR__TRUE
;
197 e
->setto
= xmemdupz(equals
+ 1, ep
- equals
- 1);
199 e
->attr
= git_attr_internal(cp
, len
);
201 return ep
+ strspn(ep
, blank
);
204 static struct match_attr
*parse_attr_line(const char *line
, const char *src
,
205 int lineno
, int macro_ok
)
209 const char *cp
, *name
, *states
;
210 struct match_attr
*res
= NULL
;
213 cp
= line
+ strspn(line
, blank
);
214 if (!*cp
|| *cp
== '#')
217 namelen
= strcspn(name
, blank
);
218 if (strlen(ATTRIBUTE_MACRO_PREFIX
) < namelen
&&
219 starts_with(name
, ATTRIBUTE_MACRO_PREFIX
)) {
221 fprintf(stderr
, "%s not allowed: %s:%d\n",
226 name
+= strlen(ATTRIBUTE_MACRO_PREFIX
);
227 name
+= strspn(name
, blank
);
228 namelen
= strcspn(name
, blank
);
229 if (invalid_attr_name(name
, namelen
)) {
231 "%.*s is not a valid attribute name: %s:%d\n",
232 namelen
, name
, src
, lineno
);
239 states
= name
+ namelen
;
240 states
+= strspn(states
, blank
);
242 /* First pass to count the attr_states */
243 for (cp
= states
, num_attr
= 0; *cp
; num_attr
++) {
244 cp
= parse_attr(src
, lineno
, cp
, NULL
);
251 sizeof(struct attr_state
) * num_attr
+
252 (is_macro
? 0 : namelen
+ 1));
254 res
->u
.attr
= git_attr_internal(name
, namelen
);
255 res
->u
.attr
->maybe_macro
= 1;
257 char *p
= (char *)&(res
->state
[num_attr
]);
258 memcpy(p
, name
, namelen
);
259 res
->u
.pat
.pattern
= p
;
260 parse_exclude_pattern(&res
->u
.pat
.pattern
,
261 &res
->u
.pat
.patternlen
,
263 &res
->u
.pat
.nowildcardlen
);
264 if (res
->u
.pat
.flags
& EXC_FLAG_NEGATIVE
) {
265 warning(_("Negative patterns are ignored in git attributes\n"
266 "Use '\\!' for literal leading exclamation."));
270 res
->is_macro
= is_macro
;
271 res
->num_attr
= num_attr
;
273 /* Second pass to fill the attr_states */
274 for (cp
= states
, i
= 0; *cp
; i
++) {
275 cp
= parse_attr(src
, lineno
, cp
, &(res
->state
[i
]));
277 res
->state
[i
].attr
->maybe_real
= 1;
278 if (res
->state
[i
].attr
->maybe_macro
)
279 cannot_trust_maybe_real
= 1;
286 * Like info/exclude and .gitignore, the attribute information can
287 * come from many places.
289 * (1) .gitattribute file of the same directory;
290 * (2) .gitattribute file of the parent directory if (1) does not have
291 * any match; this goes recursively upwards, just like .gitignore.
292 * (3) $GIT_DIR/info/attributes, which overrides both of the above.
294 * In the same file, later entries override the earlier match, so in the
295 * global list, we would have entries from info/attributes the earliest
296 * (reading the file from top to bottom), .gitattribute of the root
297 * directory (again, reading the file from top to bottom) down to the
298 * current directory, and then scan the list backwards to find the first match.
299 * This is exactly the same as what is_excluded() does in dir.c to deal with
303 static struct attr_stack
{
304 struct attr_stack
*prev
;
307 unsigned num_matches
;
309 struct match_attr
**attrs
;
312 static void free_attr_elem(struct attr_stack
*e
)
316 for (i
= 0; i
< e
->num_matches
; i
++) {
317 struct match_attr
*a
= e
->attrs
[i
];
319 for (j
= 0; j
< a
->num_attr
; j
++) {
320 const char *setto
= a
->state
[j
].setto
;
321 if (setto
== ATTR__TRUE
||
322 setto
== ATTR__FALSE
||
323 setto
== ATTR__UNSET
||
324 setto
== ATTR__UNKNOWN
)
327 free((char *) setto
);
335 static const char *builtin_attr
[] = {
336 "[attr]binary -diff -merge -text",
340 static void handle_attr_line(struct attr_stack
*res
,
346 struct match_attr
*a
;
348 a
= parse_attr_line(line
, src
, lineno
, macro_ok
);
351 ALLOC_GROW(res
->attrs
, res
->num_matches
+ 1, res
->alloc
);
352 res
->attrs
[res
->num_matches
++] = a
;
355 static struct attr_stack
*read_attr_from_array(const char **list
)
357 struct attr_stack
*res
;
361 res
= xcalloc(1, sizeof(*res
));
362 while ((line
= *(list
++)) != NULL
)
363 handle_attr_line(res
, line
, "[builtin]", ++lineno
, 1);
367 static enum git_attr_direction direction
;
368 static struct index_state
*use_index
;
370 static struct attr_stack
*read_attr_from_file(const char *path
, int macro_ok
)
372 FILE *fp
= fopen(path
, "r");
373 struct attr_stack
*res
;
378 if (errno
!= ENOENT
&& errno
!= ENOTDIR
)
379 warn_on_inaccessible(path
);
382 res
= xcalloc(1, sizeof(*res
));
383 while (fgets(buf
, sizeof(buf
), fp
)) {
386 skip_utf8_bom(&bufp
, strlen(bufp
));
387 handle_attr_line(res
, bufp
, path
, ++lineno
, macro_ok
);
393 static struct attr_stack
*read_attr_from_index(const char *path
, int macro_ok
)
395 struct attr_stack
*res
;
399 buf
= read_blob_data_from_index(use_index
? use_index
: &the_index
, path
, NULL
);
403 res
= xcalloc(1, sizeof(*res
));
404 for (sp
= buf
; *sp
; ) {
407 for (ep
= sp
; *ep
&& *ep
!= '\n'; ep
++)
409 more
= (*ep
== '\n');
411 handle_attr_line(res
, sp
, path
, ++lineno
, macro_ok
);
418 static struct attr_stack
*read_attr(const char *path
, int macro_ok
)
420 struct attr_stack
*res
;
422 if (direction
== GIT_ATTR_CHECKOUT
) {
423 res
= read_attr_from_index(path
, macro_ok
);
425 res
= read_attr_from_file(path
, macro_ok
);
427 else if (direction
== GIT_ATTR_CHECKIN
) {
428 res
= read_attr_from_file(path
, macro_ok
);
431 * There is no checked out .gitattributes file there, but
432 * we might have it in the index. We allow operation in a
433 * sparsely checked out work tree, so read from it.
435 res
= read_attr_from_index(path
, macro_ok
);
438 res
= read_attr_from_index(path
, macro_ok
);
440 res
= xcalloc(1, sizeof(*res
));
445 static void debug_info(const char *what
, struct attr_stack
*elem
)
447 fprintf(stderr
, "%s: %s\n", what
, elem
->origin
? elem
->origin
: "()");
449 static void debug_set(const char *what
, const char *match
, struct git_attr
*attr
, const void *v
)
451 const char *value
= v
;
453 if (ATTR_TRUE(value
))
455 else if (ATTR_FALSE(value
))
457 else if (ATTR_UNSET(value
))
458 value
= "unspecified";
460 fprintf(stderr
, "%s: %s => %s (%s)\n",
461 what
, attr
->name
, (char *) value
, match
);
463 #define debug_push(a) debug_info("push", (a))
464 #define debug_pop(a) debug_info("pop", (a))
466 #define debug_push(a) do { ; } while (0)
467 #define debug_pop(a) do { ; } while (0)
468 #define debug_set(a,b,c,d) do { ; } while (0)
471 static void drop_attr_stack(void)
474 struct attr_stack
*elem
= attr_stack
;
475 attr_stack
= elem
->prev
;
476 free_attr_elem(elem
);
480 static const char *git_etc_gitattributes(void)
482 static const char *system_wide
;
484 system_wide
= system_path(ETC_GITATTRIBUTES
);
488 static int git_attr_system(void)
490 return !git_env_bool("GIT_ATTR_NOSYSTEM", 0);
493 static void bootstrap_attr_stack(void)
495 struct attr_stack
*elem
;
496 char *xdg_attributes_file
;
501 elem
= read_attr_from_array(builtin_attr
);
503 elem
->prev
= attr_stack
;
506 if (git_attr_system()) {
507 elem
= read_attr_from_file(git_etc_gitattributes(), 1);
510 elem
->prev
= attr_stack
;
515 if (!git_attributes_file
) {
516 home_config_paths(NULL
, &xdg_attributes_file
, "attributes");
517 git_attributes_file
= xdg_attributes_file
;
519 if (git_attributes_file
) {
520 elem
= read_attr_from_file(git_attributes_file
, 1);
523 elem
->prev
= attr_stack
;
528 if (!is_bare_repository() || direction
== GIT_ATTR_INDEX
) {
529 elem
= read_attr(GITATTRIBUTES_FILE
, 1);
530 elem
->origin
= xstrdup("");
532 elem
->prev
= attr_stack
;
537 elem
= read_attr_from_file(git_path(INFOATTRIBUTES_FILE
), 1);
539 elem
= xcalloc(1, sizeof(*elem
));
541 elem
->prev
= attr_stack
;
545 static void prepare_attr_stack(const char *path
, int dirlen
)
547 struct attr_stack
*elem
, *info
;
552 * At the bottom of the attribute stack is the built-in
553 * set of attribute definitions, followed by the contents
554 * of $(prefix)/etc/gitattributes and a file specified by
555 * core.attributesfile. Then, contents from
556 * .gitattribute files from directories closer to the
557 * root to the ones in deeper directories are pushed
558 * to the stack. Finally, at the very top of the stack
559 * we always keep the contents of $GIT_DIR/info/attributes.
561 * When checking, we use entries from near the top of the
562 * stack, preferring $GIT_DIR/info/attributes, then
563 * .gitattributes in deeper directories to shallower ones,
564 * and finally use the built-in set as the default.
566 bootstrap_attr_stack();
569 * Pop the "info" one that is always at the top of the stack.
572 attr_stack
= info
->prev
;
575 * Pop the ones from directories that are not the prefix of
576 * the path we are checking. Break out of the loop when we see
577 * the root one (whose origin is an empty string "") or the builtin
578 * one (whose origin is NULL) without popping it.
580 while (attr_stack
->origin
) {
581 int namelen
= strlen(attr_stack
->origin
);
584 if (namelen
<= dirlen
&&
585 !strncmp(elem
->origin
, path
, namelen
) &&
586 (!namelen
|| path
[namelen
] == '/'))
590 attr_stack
= elem
->prev
;
591 free_attr_elem(elem
);
595 * Read from parent directories and push them down
597 if (!is_bare_repository() || direction
== GIT_ATTR_INDEX
) {
599 * bootstrap_attr_stack() should have added, and the
600 * above loop should have stopped before popping, the
601 * root element whose attr_stack->origin is set to an
604 struct strbuf pathbuf
= STRBUF_INIT
;
606 assert(attr_stack
->origin
);
608 len
= strlen(attr_stack
->origin
);
611 cp
= memchr(path
+ len
+ 1, '/', dirlen
- len
- 1);
614 strbuf_add(&pathbuf
, path
, cp
- path
);
615 strbuf_addch(&pathbuf
, '/');
616 strbuf_addstr(&pathbuf
, GITATTRIBUTES_FILE
);
617 elem
= read_attr(pathbuf
.buf
, 0);
618 strbuf_setlen(&pathbuf
, cp
- path
);
619 elem
->origin
= strbuf_detach(&pathbuf
, &elem
->originlen
);
620 elem
->prev
= attr_stack
;
625 strbuf_release(&pathbuf
);
629 * Finally push the "info" one at the top of the stack.
631 info
->prev
= attr_stack
;
635 static int path_matches(const char *pathname
, int pathlen
,
637 const struct pattern
*pat
,
638 const char *base
, int baselen
)
640 const char *pattern
= pat
->pattern
;
641 int prefix
= pat
->nowildcardlen
;
642 int isdir
= (pathlen
&& pathname
[pathlen
- 1] == '/');
644 if ((pat
->flags
& EXC_FLAG_MUSTBEDIR
) && !isdir
)
647 if (pat
->flags
& EXC_FLAG_NODIR
) {
648 return match_basename(pathname
+ basename_offset
,
649 pathlen
- basename_offset
- isdir
,
651 pat
->patternlen
, pat
->flags
);
653 return match_pathname(pathname
, pathlen
- isdir
,
655 pattern
, prefix
, pat
->patternlen
, pat
->flags
);
658 static int macroexpand_one(int attr_nr
, int rem
);
660 static int fill_one(const char *what
, struct match_attr
*a
, int rem
)
662 struct git_attr_check
*check
= check_all_attr
;
665 for (i
= a
->num_attr
- 1; 0 < rem
&& 0 <= i
; i
--) {
666 struct git_attr
*attr
= a
->state
[i
].attr
;
667 const char **n
= &(check
[attr
->attr_nr
].value
);
668 const char *v
= a
->state
[i
].setto
;
670 if (*n
== ATTR__UNKNOWN
) {
672 a
->is_macro
? a
->u
.attr
->name
: a
->u
.pat
.pattern
,
676 rem
= macroexpand_one(attr
->attr_nr
, rem
);
682 static int fill(const char *path
, int pathlen
, int basename_offset
,
683 struct attr_stack
*stk
, int rem
)
686 const char *base
= stk
->origin
? stk
->origin
: "";
688 for (i
= stk
->num_matches
- 1; 0 < rem
&& 0 <= i
; i
--) {
689 struct match_attr
*a
= stk
->attrs
[i
];
692 if (path_matches(path
, pathlen
, basename_offset
,
693 &a
->u
.pat
, base
, stk
->originlen
))
694 rem
= fill_one("fill", a
, rem
);
699 static int macroexpand_one(int nr
, int rem
)
701 struct attr_stack
*stk
;
702 struct match_attr
*a
= NULL
;
705 if (check_all_attr
[nr
].value
!= ATTR__TRUE
||
706 !check_all_attr
[nr
].attr
->maybe_macro
)
709 for (stk
= attr_stack
; !a
&& stk
; stk
= stk
->prev
)
710 for (i
= stk
->num_matches
- 1; !a
&& 0 <= i
; i
--) {
711 struct match_attr
*ma
= stk
->attrs
[i
];
714 if (ma
->u
.attr
->attr_nr
== nr
)
719 rem
= fill_one("expand", a
, rem
);
725 * Collect attributes for path into the array pointed to by
726 * check_all_attr. If num is non-zero, only attributes in check[] are
727 * collected. Otherwise all attributes are collected.
729 static void collect_some_attrs(const char *path
, int num
,
730 struct git_attr_check
*check
)
733 struct attr_stack
*stk
;
734 int i
, pathlen
, rem
, dirlen
;
735 const char *cp
, *last_slash
= NULL
;
738 for (cp
= path
; *cp
; cp
++) {
739 if (*cp
== '/' && cp
[1])
744 basename_offset
= last_slash
+ 1 - path
;
745 dirlen
= last_slash
- path
;
751 prepare_attr_stack(path
, dirlen
);
752 for (i
= 0; i
< attr_nr
; i
++)
753 check_all_attr
[i
].value
= ATTR__UNKNOWN
;
754 if (num
&& !cannot_trust_maybe_real
) {
756 for (i
= 0; i
< num
; i
++) {
757 if (!check
[i
].attr
->maybe_real
) {
758 struct git_attr_check
*c
;
759 c
= check_all_attr
+ check
[i
].attr
->attr_nr
;
760 c
->value
= ATTR__UNSET
;
769 for (stk
= attr_stack
; 0 < rem
&& stk
; stk
= stk
->prev
)
770 rem
= fill(path
, pathlen
, basename_offset
, stk
, rem
);
773 int git_check_attr(const char *path
, int num
, struct git_attr_check
*check
)
777 collect_some_attrs(path
, num
, check
);
779 for (i
= 0; i
< num
; i
++) {
780 const char *value
= check_all_attr
[check
[i
].attr
->attr_nr
].value
;
781 if (value
== ATTR__UNKNOWN
)
783 check
[i
].value
= value
;
789 int git_all_attrs(const char *path
, int *num
, struct git_attr_check
**check
)
793 collect_some_attrs(path
, 0, NULL
);
795 /* Count the number of attributes that are set. */
797 for (i
= 0; i
< attr_nr
; i
++) {
798 const char *value
= check_all_attr
[i
].value
;
799 if (value
!= ATTR__UNSET
&& value
!= ATTR__UNKNOWN
)
803 *check
= xmalloc(sizeof(**check
) * count
);
805 for (i
= 0; i
< attr_nr
; i
++) {
806 const char *value
= check_all_attr
[i
].value
;
807 if (value
!= ATTR__UNSET
&& value
!= ATTR__UNKNOWN
) {
808 (*check
)[j
].attr
= check_all_attr
[i
].attr
;
809 (*check
)[j
].value
= value
;
817 void git_attr_set_direction(enum git_attr_direction
new, struct index_state
*istate
)
819 enum git_attr_direction old
= direction
;
821 if (is_bare_repository() && new != GIT_ATTR_INDEX
)
822 die("BUG: non-INDEX attr direction in a bare repo");