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
16 const char git_attr__true
[] = "(builtin)true";
17 const char git_attr__false
[] = "\0(builtin)false";
18 static const char git_attr__unknown
[] = "(builtin)unknown";
19 #define ATTR__TRUE git_attr__true
20 #define ATTR__FALSE git_attr__false
21 #define ATTR__UNSET NULL
22 #define ATTR__UNKNOWN git_attr__unknown
24 /* This is a randomly chosen prime. */
32 struct git_attr
*next
;
37 char name
[FLEX_ARRAY
];
40 static int cannot_trust_maybe_real
;
42 static struct git_attr_check
*check_all_attr
;
43 static struct git_attr
*(git_attr_hash
[HASHSIZE
]);
45 char *git_attr_name(struct git_attr
*attr
)
50 static unsigned hash_name(const char *name
, int namelen
)
56 val
= ((val
<< 7) | (val
>> 22)) ^ c
;
61 static int invalid_attr_name(const char *name
, int namelen
)
64 * Attribute name cannot begin with '-' and must consist of
65 * characters from [-A-Za-z0-9_.].
67 if (namelen
<= 0 || *name
== '-')
71 if (! (ch
== '-' || ch
== '.' || ch
== '_' ||
72 ('0' <= ch
&& ch
<= '9') ||
73 ('a' <= ch
&& ch
<= 'z') ||
74 ('A' <= ch
&& ch
<= 'Z')) )
80 static struct git_attr
*git_attr_internal(const char *name
, int len
)
82 unsigned hval
= hash_name(name
, len
);
83 unsigned pos
= hval
% HASHSIZE
;
86 for (a
= git_attr_hash
[pos
]; a
; a
= a
->next
) {
88 !memcmp(a
->name
, name
, len
) && !a
->name
[len
])
92 if (invalid_attr_name(name
, len
))
95 a
= xmalloc(sizeof(*a
) + len
+ 1);
96 memcpy(a
->name
, name
, len
);
99 a
->next
= git_attr_hash
[pos
];
100 a
->attr_nr
= attr_nr
++;
103 git_attr_hash
[pos
] = a
;
105 REALLOC_ARRAY(check_all_attr
, attr_nr
);
106 check_all_attr
[a
->attr_nr
].attr
= a
;
107 check_all_attr
[a
->attr_nr
].value
= ATTR__UNKNOWN
;
111 struct git_attr
*git_attr(const char *name
)
113 return git_attr_internal(name
, strlen(name
));
116 /* What does a matched pattern decide? */
118 struct git_attr
*attr
;
126 int flags
; /* EXC_FLAG_* */
130 * One rule, as from a .gitattributes file.
132 * If is_macro is true, then u.attr is a pointer to the git_attr being
135 * If is_macro is false, then u.pattern points at the filename pattern
136 * to which the rule applies. (The memory pointed to is part of the
137 * memory block allocated for the match_attr instance.)
139 * In either case, num_attr is the number of attributes affected by
140 * this rule, and state is an array listing them. The attributes are
141 * listed as they appear in the file (macros unexpanded).
146 struct git_attr
*attr
;
150 struct attr_state state
[FLEX_ARRAY
];
153 static const char blank
[] = " \t\r\n";
156 * Parse a whitespace-delimited attribute state (i.e., "attr",
157 * "-attr", "!attr", or "attr=value") from the string starting at src.
158 * If e is not NULL, write the results to *e. Return a pointer to the
159 * remainder of the string (with leading whitespace removed), or NULL
160 * if there was an error.
162 static const char *parse_attr(const char *src
, int lineno
, const char *cp
,
163 struct attr_state
*e
)
165 const char *ep
, *equals
;
168 ep
= cp
+ strcspn(cp
, blank
);
169 equals
= strchr(cp
, '=');
170 if (equals
&& ep
< equals
)
177 if (*cp
== '-' || *cp
== '!') {
181 if (invalid_attr_name(cp
, len
)) {
183 "%.*s is not a valid attribute name: %s:%d\n",
184 len
, cp
, src
, lineno
);
188 if (*cp
== '-' || *cp
== '!') {
189 e
->setto
= (*cp
== '-') ? ATTR__FALSE
: ATTR__UNSET
;
194 e
->setto
= ATTR__TRUE
;
196 e
->setto
= xmemdupz(equals
+ 1, ep
- equals
- 1);
198 e
->attr
= git_attr_internal(cp
, len
);
200 return ep
+ strspn(ep
, blank
);
203 static struct match_attr
*parse_attr_line(const char *line
, const char *src
,
204 int lineno
, int macro_ok
)
208 const char *cp
, *name
, *states
;
209 struct match_attr
*res
= NULL
;
212 cp
= line
+ strspn(line
, blank
);
213 if (!*cp
|| *cp
== '#')
216 namelen
= strcspn(name
, blank
);
217 if (strlen(ATTRIBUTE_MACRO_PREFIX
) < namelen
&&
218 starts_with(name
, ATTRIBUTE_MACRO_PREFIX
)) {
220 fprintf(stderr
, "%s not allowed: %s:%d\n",
225 name
+= strlen(ATTRIBUTE_MACRO_PREFIX
);
226 name
+= strspn(name
, blank
);
227 namelen
= strcspn(name
, blank
);
228 if (invalid_attr_name(name
, namelen
)) {
230 "%.*s is not a valid attribute name: %s:%d\n",
231 namelen
, name
, src
, lineno
);
238 states
= name
+ namelen
;
239 states
+= strspn(states
, blank
);
241 /* First pass to count the attr_states */
242 for (cp
= states
, num_attr
= 0; *cp
; num_attr
++) {
243 cp
= parse_attr(src
, lineno
, cp
, NULL
);
250 sizeof(struct attr_state
) * num_attr
+
251 (is_macro
? 0 : namelen
+ 1));
253 res
->u
.attr
= git_attr_internal(name
, namelen
);
254 res
->u
.attr
->maybe_macro
= 1;
256 char *p
= (char *)&(res
->state
[num_attr
]);
257 memcpy(p
, name
, namelen
);
258 res
->u
.pat
.pattern
= p
;
259 parse_exclude_pattern(&res
->u
.pat
.pattern
,
260 &res
->u
.pat
.patternlen
,
262 &res
->u
.pat
.nowildcardlen
);
263 if (res
->u
.pat
.flags
& EXC_FLAG_NEGATIVE
) {
264 warning(_("Negative patterns are ignored in git attributes\n"
265 "Use '\\!' for literal leading exclamation."));
269 res
->is_macro
= is_macro
;
270 res
->num_attr
= num_attr
;
272 /* Second pass to fill the attr_states */
273 for (cp
= states
, i
= 0; *cp
; i
++) {
274 cp
= parse_attr(src
, lineno
, cp
, &(res
->state
[i
]));
276 res
->state
[i
].attr
->maybe_real
= 1;
277 if (res
->state
[i
].attr
->maybe_macro
)
278 cannot_trust_maybe_real
= 1;
285 * Like info/exclude and .gitignore, the attribute information can
286 * come from many places.
288 * (1) .gitattribute file of the same directory;
289 * (2) .gitattribute file of the parent directory if (1) does not have
290 * any match; this goes recursively upwards, just like .gitignore.
291 * (3) $GIT_DIR/info/attributes, which overrides both of the above.
293 * In the same file, later entries override the earlier match, so in the
294 * global list, we would have entries from info/attributes the earliest
295 * (reading the file from top to bottom), .gitattribute of the root
296 * directory (again, reading the file from top to bottom) down to the
297 * current directory, and then scan the list backwards to find the first match.
298 * This is exactly the same as what is_excluded() does in dir.c to deal with
302 static struct attr_stack
{
303 struct attr_stack
*prev
;
306 unsigned num_matches
;
308 struct match_attr
**attrs
;
311 static void free_attr_elem(struct attr_stack
*e
)
315 for (i
= 0; i
< e
->num_matches
; i
++) {
316 struct match_attr
*a
= e
->attrs
[i
];
318 for (j
= 0; j
< a
->num_attr
; j
++) {
319 const char *setto
= a
->state
[j
].setto
;
320 if (setto
== ATTR__TRUE
||
321 setto
== ATTR__FALSE
||
322 setto
== ATTR__UNSET
||
323 setto
== ATTR__UNKNOWN
)
326 free((char *) setto
);
334 static const char *builtin_attr
[] = {
335 "[attr]binary -diff -merge -text",
339 static void handle_attr_line(struct attr_stack
*res
,
345 struct match_attr
*a
;
347 a
= parse_attr_line(line
, src
, lineno
, macro_ok
);
350 ALLOC_GROW(res
->attrs
, res
->num_matches
+ 1, res
->alloc
);
351 res
->attrs
[res
->num_matches
++] = a
;
354 static struct attr_stack
*read_attr_from_array(const char **list
)
356 struct attr_stack
*res
;
360 res
= xcalloc(1, sizeof(*res
));
361 while ((line
= *(list
++)) != NULL
)
362 handle_attr_line(res
, line
, "[builtin]", ++lineno
, 1);
366 static enum git_attr_direction direction
;
367 static struct index_state
*use_index
;
369 static struct attr_stack
*read_attr_from_file(const char *path
, int macro_ok
)
371 FILE *fp
= fopen(path
, "r");
372 struct attr_stack
*res
;
377 if (errno
!= ENOENT
&& errno
!= ENOTDIR
)
378 warn_on_inaccessible(path
);
381 res
= xcalloc(1, sizeof(*res
));
382 while (fgets(buf
, sizeof(buf
), fp
))
383 handle_attr_line(res
, buf
, path
, ++lineno
, macro_ok
);
388 static struct attr_stack
*read_attr_from_index(const char *path
, int macro_ok
)
390 struct attr_stack
*res
;
394 buf
= read_blob_data_from_index(use_index
? use_index
: &the_index
, path
, NULL
);
398 res
= xcalloc(1, sizeof(*res
));
399 for (sp
= buf
; *sp
; ) {
402 for (ep
= sp
; *ep
&& *ep
!= '\n'; ep
++)
404 more
= (*ep
== '\n');
406 handle_attr_line(res
, sp
, path
, ++lineno
, macro_ok
);
413 static struct attr_stack
*read_attr(const char *path
, int macro_ok
)
415 struct attr_stack
*res
;
417 if (direction
== GIT_ATTR_CHECKOUT
) {
418 res
= read_attr_from_index(path
, macro_ok
);
420 res
= read_attr_from_file(path
, macro_ok
);
422 else if (direction
== GIT_ATTR_CHECKIN
) {
423 res
= read_attr_from_file(path
, macro_ok
);
426 * There is no checked out .gitattributes file there, but
427 * we might have it in the index. We allow operation in a
428 * sparsely checked out work tree, so read from it.
430 res
= read_attr_from_index(path
, macro_ok
);
433 res
= read_attr_from_index(path
, macro_ok
);
435 res
= xcalloc(1, sizeof(*res
));
440 static void debug_info(const char *what
, struct attr_stack
*elem
)
442 fprintf(stderr
, "%s: %s\n", what
, elem
->origin
? elem
->origin
: "()");
444 static void debug_set(const char *what
, const char *match
, struct git_attr
*attr
, const void *v
)
446 const char *value
= v
;
448 if (ATTR_TRUE(value
))
450 else if (ATTR_FALSE(value
))
452 else if (ATTR_UNSET(value
))
453 value
= "unspecified";
455 fprintf(stderr
, "%s: %s => %s (%s)\n",
456 what
, attr
->name
, (char *) value
, match
);
458 #define debug_push(a) debug_info("push", (a))
459 #define debug_pop(a) debug_info("pop", (a))
461 #define debug_push(a) do { ; } while (0)
462 #define debug_pop(a) do { ; } while (0)
463 #define debug_set(a,b,c,d) do { ; } while (0)
466 static void drop_attr_stack(void)
469 struct attr_stack
*elem
= attr_stack
;
470 attr_stack
= elem
->prev
;
471 free_attr_elem(elem
);
475 static const char *git_etc_gitattributes(void)
477 static const char *system_wide
;
479 system_wide
= system_path(ETC_GITATTRIBUTES
);
483 static int git_attr_system(void)
485 return !git_env_bool("GIT_ATTR_NOSYSTEM", 0);
488 static void bootstrap_attr_stack(void)
490 struct attr_stack
*elem
;
491 char *xdg_attributes_file
;
496 elem
= read_attr_from_array(builtin_attr
);
498 elem
->prev
= attr_stack
;
501 if (git_attr_system()) {
502 elem
= read_attr_from_file(git_etc_gitattributes(), 1);
505 elem
->prev
= attr_stack
;
510 if (!git_attributes_file
) {
511 home_config_paths(NULL
, &xdg_attributes_file
, "attributes");
512 git_attributes_file
= xdg_attributes_file
;
514 if (git_attributes_file
) {
515 elem
= read_attr_from_file(git_attributes_file
, 1);
518 elem
->prev
= attr_stack
;
523 if (!is_bare_repository() || direction
== GIT_ATTR_INDEX
) {
524 elem
= read_attr(GITATTRIBUTES_FILE
, 1);
525 elem
->origin
= xstrdup("");
527 elem
->prev
= attr_stack
;
532 elem
= read_attr_from_file(git_path(INFOATTRIBUTES_FILE
), 1);
534 elem
= xcalloc(1, sizeof(*elem
));
536 elem
->prev
= attr_stack
;
540 static void prepare_attr_stack(const char *path
, int dirlen
)
542 struct attr_stack
*elem
, *info
;
547 * At the bottom of the attribute stack is the built-in
548 * set of attribute definitions, followed by the contents
549 * of $(prefix)/etc/gitattributes and a file specified by
550 * core.attributesfile. Then, contents from
551 * .gitattribute files from directories closer to the
552 * root to the ones in deeper directories are pushed
553 * to the stack. Finally, at the very top of the stack
554 * we always keep the contents of $GIT_DIR/info/attributes.
556 * When checking, we use entries from near the top of the
557 * stack, preferring $GIT_DIR/info/attributes, then
558 * .gitattributes in deeper directories to shallower ones,
559 * and finally use the built-in set as the default.
561 bootstrap_attr_stack();
564 * Pop the "info" one that is always at the top of the stack.
567 attr_stack
= info
->prev
;
570 * Pop the ones from directories that are not the prefix of
571 * the path we are checking. Break out of the loop when we see
572 * the root one (whose origin is an empty string "") or the builtin
573 * one (whose origin is NULL) without popping it.
575 while (attr_stack
->origin
) {
576 int namelen
= strlen(attr_stack
->origin
);
579 if (namelen
<= dirlen
&&
580 !strncmp(elem
->origin
, path
, namelen
) &&
581 (!namelen
|| path
[namelen
] == '/'))
585 attr_stack
= elem
->prev
;
586 free_attr_elem(elem
);
590 * Read from parent directories and push them down
592 if (!is_bare_repository() || direction
== GIT_ATTR_INDEX
) {
594 * bootstrap_attr_stack() should have added, and the
595 * above loop should have stopped before popping, the
596 * root element whose attr_stack->origin is set to an
599 struct strbuf pathbuf
= STRBUF_INIT
;
601 assert(attr_stack
->origin
);
603 len
= strlen(attr_stack
->origin
);
606 cp
= memchr(path
+ len
+ 1, '/', dirlen
- len
- 1);
609 strbuf_add(&pathbuf
, path
, cp
- path
);
610 strbuf_addch(&pathbuf
, '/');
611 strbuf_addstr(&pathbuf
, GITATTRIBUTES_FILE
);
612 elem
= read_attr(pathbuf
.buf
, 0);
613 strbuf_setlen(&pathbuf
, cp
- path
);
614 elem
->origin
= strbuf_detach(&pathbuf
, &elem
->originlen
);
615 elem
->prev
= attr_stack
;
620 strbuf_release(&pathbuf
);
624 * Finally push the "info" one at the top of the stack.
626 info
->prev
= attr_stack
;
630 static int path_matches(const char *pathname
, int pathlen
,
632 const struct pattern
*pat
,
633 const char *base
, int baselen
)
635 const char *pattern
= pat
->pattern
;
636 int prefix
= pat
->nowildcardlen
;
637 int isdir
= (pathlen
&& pathname
[pathlen
- 1] == '/');
639 if ((pat
->flags
& EXC_FLAG_MUSTBEDIR
) && !isdir
)
642 if (pat
->flags
& EXC_FLAG_NODIR
) {
643 return match_basename(pathname
+ basename_offset
,
644 pathlen
- basename_offset
- isdir
,
646 pat
->patternlen
, pat
->flags
);
648 return match_pathname(pathname
, pathlen
- isdir
,
650 pattern
, prefix
, pat
->patternlen
, pat
->flags
);
653 static int macroexpand_one(int attr_nr
, int rem
);
655 static int fill_one(const char *what
, struct match_attr
*a
, int rem
)
657 struct git_attr_check
*check
= check_all_attr
;
660 for (i
= a
->num_attr
- 1; 0 < rem
&& 0 <= i
; i
--) {
661 struct git_attr
*attr
= a
->state
[i
].attr
;
662 const char **n
= &(check
[attr
->attr_nr
].value
);
663 const char *v
= a
->state
[i
].setto
;
665 if (*n
== ATTR__UNKNOWN
) {
667 a
->is_macro
? a
->u
.attr
->name
: a
->u
.pat
.pattern
,
671 rem
= macroexpand_one(attr
->attr_nr
, rem
);
677 static int fill(const char *path
, int pathlen
, int basename_offset
,
678 struct attr_stack
*stk
, int rem
)
681 const char *base
= stk
->origin
? stk
->origin
: "";
683 for (i
= stk
->num_matches
- 1; 0 < rem
&& 0 <= i
; i
--) {
684 struct match_attr
*a
= stk
->attrs
[i
];
687 if (path_matches(path
, pathlen
, basename_offset
,
688 &a
->u
.pat
, base
, stk
->originlen
))
689 rem
= fill_one("fill", a
, rem
);
694 static int macroexpand_one(int nr
, int rem
)
696 struct attr_stack
*stk
;
697 struct match_attr
*a
= NULL
;
700 if (check_all_attr
[nr
].value
!= ATTR__TRUE
||
701 !check_all_attr
[nr
].attr
->maybe_macro
)
704 for (stk
= attr_stack
; !a
&& stk
; stk
= stk
->prev
)
705 for (i
= stk
->num_matches
- 1; !a
&& 0 <= i
; i
--) {
706 struct match_attr
*ma
= stk
->attrs
[i
];
709 if (ma
->u
.attr
->attr_nr
== nr
)
714 rem
= fill_one("expand", a
, rem
);
720 * Collect attributes for path into the array pointed to by
721 * check_all_attr. If num is non-zero, only attributes in check[] are
722 * collected. Otherwise all attributes are collected.
724 static void collect_some_attrs(const char *path
, int num
,
725 struct git_attr_check
*check
)
728 struct attr_stack
*stk
;
729 int i
, pathlen
, rem
, dirlen
;
730 const char *cp
, *last_slash
= NULL
;
733 for (cp
= path
; *cp
; cp
++) {
734 if (*cp
== '/' && cp
[1])
739 basename_offset
= last_slash
+ 1 - path
;
740 dirlen
= last_slash
- path
;
746 prepare_attr_stack(path
, dirlen
);
747 for (i
= 0; i
< attr_nr
; i
++)
748 check_all_attr
[i
].value
= ATTR__UNKNOWN
;
749 if (num
&& !cannot_trust_maybe_real
) {
751 for (i
= 0; i
< num
; i
++) {
752 if (!check
[i
].attr
->maybe_real
) {
753 struct git_attr_check
*c
;
754 c
= check_all_attr
+ check
[i
].attr
->attr_nr
;
755 c
->value
= ATTR__UNSET
;
764 for (stk
= attr_stack
; 0 < rem
&& stk
; stk
= stk
->prev
)
765 rem
= fill(path
, pathlen
, basename_offset
, stk
, rem
);
768 int git_check_attr(const char *path
, int num
, struct git_attr_check
*check
)
772 collect_some_attrs(path
, num
, check
);
774 for (i
= 0; i
< num
; i
++) {
775 const char *value
= check_all_attr
[check
[i
].attr
->attr_nr
].value
;
776 if (value
== ATTR__UNKNOWN
)
778 check
[i
].value
= value
;
784 int git_all_attrs(const char *path
, int *num
, struct git_attr_check
**check
)
788 collect_some_attrs(path
, 0, NULL
);
790 /* Count the number of attributes that are set. */
792 for (i
= 0; i
< attr_nr
; i
++) {
793 const char *value
= check_all_attr
[i
].value
;
794 if (value
!= ATTR__UNSET
&& value
!= ATTR__UNKNOWN
)
798 *check
= xmalloc(sizeof(**check
) * count
);
800 for (i
= 0; i
< attr_nr
; i
++) {
801 const char *value
= check_all_attr
[i
].value
;
802 if (value
!= ATTR__UNSET
&& value
!= ATTR__UNKNOWN
) {
803 (*check
)[j
].attr
= check_all_attr
[i
].attr
;
804 (*check
)[j
].value
= value
;
812 void git_attr_set_direction(enum git_attr_direction
new, struct index_state
*istate
)
814 enum git_attr_direction old
= direction
;
816 if (is_bare_repository() && new != GIT_ATTR_INDEX
)
817 die("BUG: non-INDEX attr direction in a bare repo");