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
18 const char git_attr__true
[] = "(builtin)true";
19 const char git_attr__false
[] = "\0(builtin)false";
20 static const char git_attr__unknown
[] = "(builtin)unknown";
21 #define ATTR__TRUE git_attr__true
22 #define ATTR__FALSE git_attr__false
23 #define ATTR__UNSET NULL
24 #define ATTR__UNKNOWN git_attr__unknown
26 /* This is a randomly chosen prime. */
34 struct git_attr
*next
;
39 char name
[FLEX_ARRAY
];
42 static int cannot_trust_maybe_real
;
44 static struct git_attr_check
*check_all_attr
;
45 static struct git_attr
*(git_attr_hash
[HASHSIZE
]);
47 const char *git_attr_name(const struct git_attr
*attr
)
52 static unsigned hash_name(const char *name
, int namelen
)
58 val
= ((val
<< 7) | (val
>> 22)) ^ c
;
63 static int invalid_attr_name(const char *name
, int namelen
)
66 * Attribute name cannot begin with '-' and must consist of
67 * characters from [-A-Za-z0-9_.].
69 if (namelen
<= 0 || *name
== '-')
73 if (! (ch
== '-' || ch
== '.' || ch
== '_' ||
74 ('0' <= ch
&& ch
<= '9') ||
75 ('a' <= ch
&& ch
<= 'z') ||
76 ('A' <= ch
&& ch
<= 'Z')) )
82 static struct git_attr
*git_attr_internal(const char *name
, int len
)
84 unsigned hval
= hash_name(name
, len
);
85 unsigned pos
= hval
% HASHSIZE
;
88 for (a
= git_attr_hash
[pos
]; a
; a
= a
->next
) {
90 !memcmp(a
->name
, name
, len
) && !a
->name
[len
])
94 if (invalid_attr_name(name
, len
))
97 FLEX_ALLOC_MEM(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 unsigned 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.pat is the filename pattern to which the
138 * In either case, num_attr is the number of attributes affected by
139 * this rule, and state is an array listing them. The attributes are
140 * listed as they appear in the file (macros unexpanded).
145 struct git_attr
*attr
;
149 struct attr_state state
[FLEX_ARRAY
];
152 static const char blank
[] = " \t\r\n";
155 * Parse a whitespace-delimited attribute state (i.e., "attr",
156 * "-attr", "!attr", or "attr=value") from the string starting at src.
157 * If e is not NULL, write the results to *e. Return a pointer to the
158 * remainder of the string (with leading whitespace removed), or NULL
159 * if there was an error.
161 static const char *parse_attr(const char *src
, int lineno
, const char *cp
,
162 struct attr_state
*e
)
164 const char *ep
, *equals
;
167 ep
= cp
+ strcspn(cp
, blank
);
168 equals
= strchr(cp
, '=');
169 if (equals
&& ep
< equals
)
176 if (*cp
== '-' || *cp
== '!') {
180 if (invalid_attr_name(cp
, len
)) {
182 "%.*s is not a valid attribute name: %s:%d\n",
183 len
, cp
, src
, lineno
);
188 * As this function is always called twice, once with
189 * e == NULL in the first pass and then e != NULL in
190 * the second pass, no need for invalid_attr_name()
193 if (*cp
== '-' || *cp
== '!') {
194 e
->setto
= (*cp
== '-') ? ATTR__FALSE
: ATTR__UNSET
;
199 e
->setto
= ATTR__TRUE
;
201 e
->setto
= xmemdupz(equals
+ 1, ep
- equals
- 1);
203 e
->attr
= git_attr_internal(cp
, len
);
205 return ep
+ strspn(ep
, blank
);
208 static struct match_attr
*parse_attr_line(const char *line
, const char *src
,
209 int lineno
, int macro_ok
)
213 const char *cp
, *name
, *states
;
214 struct match_attr
*res
= NULL
;
216 struct strbuf pattern
= STRBUF_INIT
;
218 cp
= line
+ strspn(line
, blank
);
219 if (!*cp
|| *cp
== '#')
223 if (*cp
== '"' && !unquote_c_style(&pattern
, name
, &states
)) {
225 namelen
= pattern
.len
;
227 namelen
= strcspn(name
, blank
);
228 states
= name
+ namelen
;
231 if (strlen(ATTRIBUTE_MACRO_PREFIX
) < namelen
&&
232 starts_with(name
, ATTRIBUTE_MACRO_PREFIX
)) {
234 fprintf(stderr
, "%s not allowed: %s:%d\n",
239 name
+= strlen(ATTRIBUTE_MACRO_PREFIX
);
240 name
+= strspn(name
, blank
);
241 namelen
= strcspn(name
, blank
);
242 if (invalid_attr_name(name
, namelen
)) {
244 "%.*s is not a valid attribute name: %s:%d\n",
245 namelen
, name
, src
, lineno
);
252 states
+= strspn(states
, blank
);
254 /* First pass to count the attr_states */
255 for (cp
= states
, num_attr
= 0; *cp
; num_attr
++) {
256 cp
= parse_attr(src
, lineno
, cp
, NULL
);
263 sizeof(struct attr_state
) * num_attr
+
264 (is_macro
? 0 : namelen
+ 1));
266 res
->u
.attr
= git_attr_internal(name
, namelen
);
267 res
->u
.attr
->maybe_macro
= 1;
269 char *p
= (char *)&(res
->state
[num_attr
]);
270 memcpy(p
, name
, namelen
);
271 res
->u
.pat
.pattern
= p
;
272 parse_exclude_pattern(&res
->u
.pat
.pattern
,
273 &res
->u
.pat
.patternlen
,
275 &res
->u
.pat
.nowildcardlen
);
276 if (res
->u
.pat
.flags
& EXC_FLAG_NEGATIVE
) {
277 warning(_("Negative patterns are ignored in git attributes\n"
278 "Use '\\!' for literal leading exclamation."));
282 res
->is_macro
= is_macro
;
283 res
->num_attr
= num_attr
;
285 /* Second pass to fill the attr_states */
286 for (cp
= states
, i
= 0; *cp
; i
++) {
287 cp
= parse_attr(src
, lineno
, cp
, &(res
->state
[i
]));
289 res
->state
[i
].attr
->maybe_real
= 1;
290 if (res
->state
[i
].attr
->maybe_macro
)
291 cannot_trust_maybe_real
= 1;
294 strbuf_release(&pattern
);
298 strbuf_release(&pattern
);
304 * Like info/exclude and .gitignore, the attribute information can
305 * come from many places.
307 * (1) .gitattribute file of the same directory;
308 * (2) .gitattribute file of the parent directory if (1) does not have
309 * any match; this goes recursively upwards, just like .gitignore.
310 * (3) $GIT_DIR/info/attributes, which overrides both of the above.
312 * In the same file, later entries override the earlier match, so in the
313 * global list, we would have entries from info/attributes the earliest
314 * (reading the file from top to bottom), .gitattribute of the root
315 * directory (again, reading the file from top to bottom) down to the
316 * current directory, and then scan the list backwards to find the first match.
317 * This is exactly the same as what is_excluded() does in dir.c to deal with
318 * .gitignore file and info/excludes file as a fallback.
321 static struct attr_stack
{
322 struct attr_stack
*prev
;
325 unsigned num_matches
;
327 struct match_attr
**attrs
;
330 static void free_attr_elem(struct attr_stack
*e
)
334 for (i
= 0; i
< e
->num_matches
; i
++) {
335 struct match_attr
*a
= e
->attrs
[i
];
337 for (j
= 0; j
< a
->num_attr
; j
++) {
338 const char *setto
= a
->state
[j
].setto
;
339 if (setto
== ATTR__TRUE
||
340 setto
== ATTR__FALSE
||
341 setto
== ATTR__UNSET
||
342 setto
== ATTR__UNKNOWN
)
345 free((char *) setto
);
353 static const char *builtin_attr
[] = {
354 "[attr]binary -diff -merge -text",
358 static void handle_attr_line(struct attr_stack
*res
,
364 struct match_attr
*a
;
366 a
= parse_attr_line(line
, src
, lineno
, macro_ok
);
369 ALLOC_GROW(res
->attrs
, res
->num_matches
+ 1, res
->alloc
);
370 res
->attrs
[res
->num_matches
++] = a
;
373 static struct attr_stack
*read_attr_from_array(const char **list
)
375 struct attr_stack
*res
;
379 res
= xcalloc(1, sizeof(*res
));
380 while ((line
= *(list
++)) != NULL
)
381 handle_attr_line(res
, line
, "[builtin]", ++lineno
, 1);
385 static enum git_attr_direction direction
;
386 static struct index_state
*use_index
;
388 static struct attr_stack
*read_attr_from_file(const char *path
, int macro_ok
)
390 FILE *fp
= fopen(path
, "r");
391 struct attr_stack
*res
;
396 if (errno
!= ENOENT
&& errno
!= ENOTDIR
)
397 warn_on_inaccessible(path
);
400 res
= xcalloc(1, sizeof(*res
));
401 while (fgets(buf
, sizeof(buf
), fp
)) {
404 skip_utf8_bom(&bufp
, strlen(bufp
));
405 handle_attr_line(res
, bufp
, path
, ++lineno
, macro_ok
);
411 static struct attr_stack
*read_attr_from_index(const char *path
, int macro_ok
)
413 struct attr_stack
*res
;
417 buf
= read_blob_data_from_index(use_index
? use_index
: &the_index
, path
, NULL
);
421 res
= xcalloc(1, sizeof(*res
));
422 for (sp
= buf
; *sp
; ) {
426 ep
= strchrnul(sp
, '\n');
427 more
= (*ep
== '\n');
429 handle_attr_line(res
, sp
, path
, ++lineno
, macro_ok
);
436 static struct attr_stack
*read_attr(const char *path
, int macro_ok
)
438 struct attr_stack
*res
;
440 if (direction
== GIT_ATTR_CHECKOUT
) {
441 res
= read_attr_from_index(path
, macro_ok
);
443 res
= read_attr_from_file(path
, macro_ok
);
445 else if (direction
== GIT_ATTR_CHECKIN
) {
446 res
= read_attr_from_file(path
, macro_ok
);
449 * There is no checked out .gitattributes file there, but
450 * we might have it in the index. We allow operation in a
451 * sparsely checked out work tree, so read from it.
453 res
= read_attr_from_index(path
, macro_ok
);
456 res
= read_attr_from_index(path
, macro_ok
);
458 res
= xcalloc(1, sizeof(*res
));
463 static void debug_info(const char *what
, struct attr_stack
*elem
)
465 fprintf(stderr
, "%s: %s\n", what
, elem
->origin
? elem
->origin
: "()");
467 static void debug_set(const char *what
, const char *match
, struct git_attr
*attr
, const void *v
)
469 const char *value
= v
;
471 if (ATTR_TRUE(value
))
473 else if (ATTR_FALSE(value
))
475 else if (ATTR_UNSET(value
))
476 value
= "unspecified";
478 fprintf(stderr
, "%s: %s => %s (%s)\n",
479 what
, attr
->name
, (char *) value
, match
);
481 #define debug_push(a) debug_info("push", (a))
482 #define debug_pop(a) debug_info("pop", (a))
484 #define debug_push(a) do { ; } while (0)
485 #define debug_pop(a) do { ; } while (0)
486 #define debug_set(a,b,c,d) do { ; } while (0)
487 #endif /* DEBUG_ATTR */
489 static void drop_attr_stack(void)
492 struct attr_stack
*elem
= attr_stack
;
493 attr_stack
= elem
->prev
;
494 free_attr_elem(elem
);
498 static const char *git_etc_gitattributes(void)
500 static const char *system_wide
;
502 system_wide
= system_path(ETC_GITATTRIBUTES
);
506 static int git_attr_system(void)
508 return !git_env_bool("GIT_ATTR_NOSYSTEM", 0);
511 static GIT_PATH_FUNC(git_path_info_attributes
, INFOATTRIBUTES_FILE
)
513 static void push_stack(struct attr_stack
**attr_stack_p
,
514 struct attr_stack
*elem
, char *origin
, size_t originlen
)
517 elem
->origin
= origin
;
519 elem
->originlen
= originlen
;
520 elem
->prev
= *attr_stack_p
;
521 *attr_stack_p
= elem
;
525 static void bootstrap_attr_stack(void)
527 struct attr_stack
*elem
;
532 push_stack(&attr_stack
, read_attr_from_array(builtin_attr
), NULL
, 0);
534 if (git_attr_system())
535 push_stack(&attr_stack
,
536 read_attr_from_file(git_etc_gitattributes(), 1),
539 if (!git_attributes_file
)
540 git_attributes_file
= xdg_config_home("attributes");
541 if (git_attributes_file
)
542 push_stack(&attr_stack
,
543 read_attr_from_file(git_attributes_file
, 1),
546 if (!is_bare_repository() || direction
== GIT_ATTR_INDEX
) {
547 elem
= read_attr(GITATTRIBUTES_FILE
, 1);
548 push_stack(&attr_stack
, elem
, xstrdup(""), 0);
552 if (startup_info
->have_repository
)
553 elem
= read_attr_from_file(git_path_info_attributes(), 1);
558 elem
= xcalloc(1, sizeof(*elem
));
559 push_stack(&attr_stack
, elem
, NULL
, 0);
562 static void prepare_attr_stack(const char *path
, int dirlen
)
564 struct attr_stack
*elem
, *info
;
568 * At the bottom of the attribute stack is the built-in
569 * set of attribute definitions, followed by the contents
570 * of $(prefix)/etc/gitattributes and a file specified by
571 * core.attributesfile. Then, contents from
572 * .gitattribute files from directories closer to the
573 * root to the ones in deeper directories are pushed
574 * to the stack. Finally, at the very top of the stack
575 * we always keep the contents of $GIT_DIR/info/attributes.
577 * When checking, we use entries from near the top of the
578 * stack, preferring $GIT_DIR/info/attributes, then
579 * .gitattributes in deeper directories to shallower ones,
580 * and finally use the built-in set as the default.
582 bootstrap_attr_stack();
585 * Pop the "info" one that is always at the top of the stack.
588 attr_stack
= info
->prev
;
591 * Pop the ones from directories that are not the prefix of
592 * the path we are checking. Break out of the loop when we see
593 * the root one (whose origin is an empty string "") or the builtin
594 * one (whose origin is NULL) without popping it.
596 while (attr_stack
->origin
) {
597 int namelen
= strlen(attr_stack
->origin
);
600 if (namelen
<= dirlen
&&
601 !strncmp(elem
->origin
, path
, namelen
) &&
602 (!namelen
|| path
[namelen
] == '/'))
606 attr_stack
= elem
->prev
;
607 free_attr_elem(elem
);
611 * Read from parent directories and push them down
613 if (!is_bare_repository() || direction
== GIT_ATTR_INDEX
) {
615 * bootstrap_attr_stack() should have added, and the
616 * above loop should have stopped before popping, the
617 * root element whose attr_stack->origin is set to an
620 struct strbuf pathbuf
= STRBUF_INIT
;
622 assert(attr_stack
->origin
);
624 size_t len
= strlen(attr_stack
->origin
);
629 cp
= memchr(path
+ len
+ 1, '/', dirlen
- len
- 1);
632 strbuf_addf(&pathbuf
,
633 "%.*s/%s", (int)(cp
- path
), path
,
635 elem
= read_attr(pathbuf
.buf
, 0);
636 strbuf_setlen(&pathbuf
, cp
- path
);
637 origin
= strbuf_detach(&pathbuf
, &len
);
638 push_stack(&attr_stack
, elem
, origin
, len
);
642 strbuf_release(&pathbuf
);
646 * Finally push the "info" one at the top of the stack.
648 push_stack(&attr_stack
, info
, NULL
, 0);
651 static int path_matches(const char *pathname
, int pathlen
,
653 const struct pattern
*pat
,
654 const char *base
, int baselen
)
656 const char *pattern
= pat
->pattern
;
657 int prefix
= pat
->nowildcardlen
;
658 int isdir
= (pathlen
&& pathname
[pathlen
- 1] == '/');
660 if ((pat
->flags
& EXC_FLAG_MUSTBEDIR
) && !isdir
)
663 if (pat
->flags
& EXC_FLAG_NODIR
) {
664 return match_basename(pathname
+ basename_offset
,
665 pathlen
- basename_offset
- isdir
,
667 pat
->patternlen
, pat
->flags
);
669 return match_pathname(pathname
, pathlen
- isdir
,
671 pattern
, prefix
, pat
->patternlen
, pat
->flags
);
674 static int macroexpand_one(int attr_nr
, int rem
);
676 static int fill_one(const char *what
, struct match_attr
*a
, int rem
)
678 struct git_attr_check
*check
= check_all_attr
;
681 for (i
= a
->num_attr
- 1; 0 < rem
&& 0 <= i
; i
--) {
682 struct git_attr
*attr
= a
->state
[i
].attr
;
683 const char **n
= &(check
[attr
->attr_nr
].value
);
684 const char *v
= a
->state
[i
].setto
;
686 if (*n
== ATTR__UNKNOWN
) {
688 a
->is_macro
? a
->u
.attr
->name
: a
->u
.pat
.pattern
,
692 rem
= macroexpand_one(attr
->attr_nr
, rem
);
698 static int fill(const char *path
, int pathlen
, int basename_offset
,
699 struct attr_stack
*stk
, int rem
)
702 const char *base
= stk
->origin
? stk
->origin
: "";
704 for (i
= stk
->num_matches
- 1; 0 < rem
&& 0 <= i
; i
--) {
705 struct match_attr
*a
= stk
->attrs
[i
];
708 if (path_matches(path
, pathlen
, basename_offset
,
709 &a
->u
.pat
, base
, stk
->originlen
))
710 rem
= fill_one("fill", a
, rem
);
715 static int macroexpand_one(int nr
, int rem
)
717 struct attr_stack
*stk
;
720 if (check_all_attr
[nr
].value
!= ATTR__TRUE
||
721 !check_all_attr
[nr
].attr
->maybe_macro
)
724 for (stk
= attr_stack
; stk
; stk
= stk
->prev
) {
725 for (i
= stk
->num_matches
- 1; 0 <= i
; i
--) {
726 struct match_attr
*ma
= stk
->attrs
[i
];
729 if (ma
->u
.attr
->attr_nr
== nr
)
730 return fill_one("expand", ma
, rem
);
738 * Collect attributes for path into the array pointed to by
739 * check_all_attr. If num is non-zero, only attributes in check[] are
740 * collected. Otherwise all attributes are collected.
742 static void collect_some_attrs(const char *path
, int num
,
743 struct git_attr_check
*check
)
746 struct attr_stack
*stk
;
747 int i
, pathlen
, rem
, dirlen
;
748 const char *cp
, *last_slash
= NULL
;
751 for (cp
= path
; *cp
; cp
++) {
752 if (*cp
== '/' && cp
[1])
757 basename_offset
= last_slash
+ 1 - path
;
758 dirlen
= last_slash
- path
;
764 prepare_attr_stack(path
, dirlen
);
765 for (i
= 0; i
< attr_nr
; i
++)
766 check_all_attr
[i
].value
= ATTR__UNKNOWN
;
767 if (num
&& !cannot_trust_maybe_real
) {
769 for (i
= 0; i
< num
; i
++) {
770 if (!check
[i
].attr
->maybe_real
) {
771 struct git_attr_check
*c
;
772 c
= check_all_attr
+ check
[i
].attr
->attr_nr
;
773 c
->value
= ATTR__UNSET
;
782 for (stk
= attr_stack
; 0 < rem
&& stk
; stk
= stk
->prev
)
783 rem
= fill(path
, pathlen
, basename_offset
, stk
, rem
);
786 int git_check_attr(const char *path
, int num
, struct git_attr_check
*check
)
790 collect_some_attrs(path
, num
, check
);
792 for (i
= 0; i
< num
; i
++) {
793 const char *value
= check_all_attr
[check
[i
].attr
->attr_nr
].value
;
794 if (value
== ATTR__UNKNOWN
)
796 check
[i
].value
= value
;
802 int git_all_attrs(const char *path
, int *num
, struct git_attr_check
**check
)
806 collect_some_attrs(path
, 0, NULL
);
808 /* Count the number of attributes that are set. */
810 for (i
= 0; i
< attr_nr
; i
++) {
811 const char *value
= check_all_attr
[i
].value
;
812 if (value
!= ATTR__UNSET
&& value
!= ATTR__UNKNOWN
)
816 ALLOC_ARRAY(*check
, count
);
818 for (i
= 0; i
< attr_nr
; i
++) {
819 const char *value
= check_all_attr
[i
].value
;
820 if (value
!= ATTR__UNSET
&& value
!= ATTR__UNKNOWN
) {
821 (*check
)[j
].attr
= check_all_attr
[i
].attr
;
822 (*check
)[j
].value
= value
;
830 void git_attr_set_direction(enum git_attr_direction
new, struct index_state
*istate
)
832 enum git_attr_direction old
= direction
;
834 if (is_bare_repository() && new != GIT_ATTR_INDEX
)
835 die("BUG: non-INDEX attr direction in a bare repo");