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
;
36 char name
[FLEX_ARRAY
];
40 static struct git_attr_check
*check_all_attr
;
41 static struct git_attr
*(git_attr_hash
[HASHSIZE
]);
43 char *git_attr_name(struct git_attr
*attr
)
48 static unsigned hash_name(const char *name
, int namelen
)
54 val
= ((val
<< 7) | (val
>> 22)) ^ c
;
59 static int invalid_attr_name(const char *name
, int namelen
)
62 * Attribute name cannot begin with '-' and must consist of
63 * characters from [-A-Za-z0-9_.].
65 if (namelen
<= 0 || *name
== '-')
69 if (! (ch
== '-' || ch
== '.' || ch
== '_' ||
70 ('0' <= ch
&& ch
<= '9') ||
71 ('a' <= ch
&& ch
<= 'z') ||
72 ('A' <= ch
&& ch
<= 'Z')) )
78 static struct git_attr
*git_attr_internal(const char *name
, int len
)
80 unsigned hval
= hash_name(name
, len
);
81 unsigned pos
= hval
% HASHSIZE
;
84 for (a
= git_attr_hash
[pos
]; a
; a
= a
->next
) {
86 !memcmp(a
->name
, name
, len
) && !a
->name
[len
])
90 if (invalid_attr_name(name
, len
))
93 a
= xmalloc(sizeof(*a
) + len
+ 1);
94 memcpy(a
->name
, name
, len
);
97 a
->next
= git_attr_hash
[pos
];
98 a
->attr_nr
= attr_nr
++;
100 git_attr_hash
[pos
] = a
;
102 REALLOC_ARRAY(check_all_attr
, attr_nr
);
103 check_all_attr
[a
->attr_nr
].attr
= a
;
104 check_all_attr
[a
->attr_nr
].value
= ATTR__UNKNOWN
;
108 struct git_attr
*git_attr(const char *name
)
110 return git_attr_internal(name
, strlen(name
));
113 /* What does a matched pattern decide? */
115 struct git_attr
*attr
;
123 int flags
; /* EXC_FLAG_* */
127 * One rule, as from a .gitattributes file.
129 * If is_macro is true, then u.attr is a pointer to the git_attr being
132 * If is_macro is false, then u.pattern points at the filename pattern
133 * to which the rule applies. (The memory pointed to is part of the
134 * memory block allocated for the match_attr instance.)
136 * In either case, num_attr is the number of attributes affected by
137 * this rule, and state is an array listing them. The attributes are
138 * listed as they appear in the file (macros unexpanded).
143 struct git_attr
*attr
;
147 struct attr_state state
[FLEX_ARRAY
];
150 static const char blank
[] = " \t\r\n";
153 * Parse a whitespace-delimited attribute state (i.e., "attr",
154 * "-attr", "!attr", or "attr=value") from the string starting at src.
155 * If e is not NULL, write the results to *e. Return a pointer to the
156 * remainder of the string (with leading whitespace removed), or NULL
157 * if there was an error.
159 static const char *parse_attr(const char *src
, int lineno
, const char *cp
,
160 struct attr_state
*e
)
162 const char *ep
, *equals
;
165 ep
= cp
+ strcspn(cp
, blank
);
166 equals
= strchr(cp
, '=');
167 if (equals
&& ep
< equals
)
174 if (*cp
== '-' || *cp
== '!') {
178 if (invalid_attr_name(cp
, len
)) {
180 "%.*s is not a valid attribute name: %s:%d\n",
181 len
, cp
, src
, lineno
);
185 if (*cp
== '-' || *cp
== '!') {
186 e
->setto
= (*cp
== '-') ? ATTR__FALSE
: ATTR__UNSET
;
191 e
->setto
= ATTR__TRUE
;
193 e
->setto
= xmemdupz(equals
+ 1, ep
- equals
- 1);
195 e
->attr
= git_attr_internal(cp
, len
);
197 return ep
+ strspn(ep
, blank
);
200 static struct match_attr
*parse_attr_line(const char *line
, const char *src
,
201 int lineno
, int macro_ok
)
205 const char *cp
, *name
, *states
;
206 struct match_attr
*res
= NULL
;
209 cp
= line
+ strspn(line
, blank
);
210 if (!*cp
|| *cp
== '#')
213 namelen
= strcspn(name
, blank
);
214 if (strlen(ATTRIBUTE_MACRO_PREFIX
) < namelen
&&
215 starts_with(name
, ATTRIBUTE_MACRO_PREFIX
)) {
217 fprintf(stderr
, "%s not allowed: %s:%d\n",
222 name
+= strlen(ATTRIBUTE_MACRO_PREFIX
);
223 name
+= strspn(name
, blank
);
224 namelen
= strcspn(name
, blank
);
225 if (invalid_attr_name(name
, namelen
)) {
227 "%.*s is not a valid attribute name: %s:%d\n",
228 namelen
, name
, src
, lineno
);
235 states
= name
+ namelen
;
236 states
+= strspn(states
, blank
);
238 /* First pass to count the attr_states */
239 for (cp
= states
, num_attr
= 0; *cp
; num_attr
++) {
240 cp
= parse_attr(src
, lineno
, cp
, NULL
);
247 sizeof(struct attr_state
) * num_attr
+
248 (is_macro
? 0 : namelen
+ 1));
250 res
->u
.attr
= git_attr_internal(name
, namelen
);
251 res
->u
.attr
->maybe_macro
= 1;
253 char *p
= (char *)&(res
->state
[num_attr
]);
254 memcpy(p
, name
, namelen
);
255 res
->u
.pat
.pattern
= p
;
256 parse_exclude_pattern(&res
->u
.pat
.pattern
,
257 &res
->u
.pat
.patternlen
,
259 &res
->u
.pat
.nowildcardlen
);
260 if (res
->u
.pat
.flags
& EXC_FLAG_NEGATIVE
) {
261 warning(_("Negative patterns are ignored in git attributes\n"
262 "Use '\\!' for literal leading exclamation."));
266 res
->is_macro
= is_macro
;
267 res
->num_attr
= num_attr
;
269 /* Second pass to fill the attr_states */
270 for (cp
= states
, i
= 0; *cp
; i
++) {
271 cp
= parse_attr(src
, lineno
, cp
, &(res
->state
[i
]));
278 * Like info/exclude and .gitignore, the attribute information can
279 * come from many places.
281 * (1) .gitattribute file of the same directory;
282 * (2) .gitattribute file of the parent directory if (1) does not have
283 * any match; this goes recursively upwards, just like .gitignore.
284 * (3) $GIT_DIR/info/attributes, which overrides both of the above.
286 * In the same file, later entries override the earlier match, so in the
287 * global list, we would have entries from info/attributes the earliest
288 * (reading the file from top to bottom), .gitattribute of the root
289 * directory (again, reading the file from top to bottom) down to the
290 * current directory, and then scan the list backwards to find the first match.
291 * This is exactly the same as what is_excluded() does in dir.c to deal with
295 static struct attr_stack
{
296 struct attr_stack
*prev
;
299 unsigned num_matches
;
301 struct match_attr
**attrs
;
304 static void free_attr_elem(struct attr_stack
*e
)
308 for (i
= 0; i
< e
->num_matches
; i
++) {
309 struct match_attr
*a
= e
->attrs
[i
];
311 for (j
= 0; j
< a
->num_attr
; j
++) {
312 const char *setto
= a
->state
[j
].setto
;
313 if (setto
== ATTR__TRUE
||
314 setto
== ATTR__FALSE
||
315 setto
== ATTR__UNSET
||
316 setto
== ATTR__UNKNOWN
)
319 free((char *) setto
);
327 static const char *builtin_attr
[] = {
328 "[attr]binary -diff -merge -text",
332 static void handle_attr_line(struct attr_stack
*res
,
338 struct match_attr
*a
;
340 a
= parse_attr_line(line
, src
, lineno
, macro_ok
);
343 ALLOC_GROW(res
->attrs
, res
->num_matches
+ 1, res
->alloc
);
344 res
->attrs
[res
->num_matches
++] = a
;
347 static struct attr_stack
*read_attr_from_array(const char **list
)
349 struct attr_stack
*res
;
353 res
= xcalloc(1, sizeof(*res
));
354 while ((line
= *(list
++)) != NULL
)
355 handle_attr_line(res
, line
, "[builtin]", ++lineno
, 1);
359 static enum git_attr_direction direction
;
360 static struct index_state
*use_index
;
362 static struct attr_stack
*read_attr_from_file(const char *path
, int macro_ok
)
364 FILE *fp
= fopen(path
, "r");
365 struct attr_stack
*res
;
370 if (errno
!= ENOENT
&& errno
!= ENOTDIR
)
371 warn_on_inaccessible(path
);
374 res
= xcalloc(1, sizeof(*res
));
375 while (fgets(buf
, sizeof(buf
), fp
))
376 handle_attr_line(res
, buf
, path
, ++lineno
, macro_ok
);
381 static struct attr_stack
*read_attr_from_index(const char *path
, int macro_ok
)
383 struct attr_stack
*res
;
387 buf
= read_blob_data_from_index(use_index
? use_index
: &the_index
, path
, NULL
);
391 res
= xcalloc(1, sizeof(*res
));
392 for (sp
= buf
; *sp
; ) {
395 for (ep
= sp
; *ep
&& *ep
!= '\n'; ep
++)
397 more
= (*ep
== '\n');
399 handle_attr_line(res
, sp
, path
, ++lineno
, macro_ok
);
406 static struct attr_stack
*read_attr(const char *path
, int macro_ok
)
408 struct attr_stack
*res
;
410 if (direction
== GIT_ATTR_CHECKOUT
) {
411 res
= read_attr_from_index(path
, macro_ok
);
413 res
= read_attr_from_file(path
, macro_ok
);
415 else if (direction
== GIT_ATTR_CHECKIN
) {
416 res
= read_attr_from_file(path
, macro_ok
);
419 * There is no checked out .gitattributes file there, but
420 * we might have it in the index. We allow operation in a
421 * sparsely checked out work tree, so read from it.
423 res
= read_attr_from_index(path
, macro_ok
);
426 res
= read_attr_from_index(path
, macro_ok
);
428 res
= xcalloc(1, sizeof(*res
));
433 static void debug_info(const char *what
, struct attr_stack
*elem
)
435 fprintf(stderr
, "%s: %s\n", what
, elem
->origin
? elem
->origin
: "()");
437 static void debug_set(const char *what
, const char *match
, struct git_attr
*attr
, const void *v
)
439 const char *value
= v
;
441 if (ATTR_TRUE(value
))
443 else if (ATTR_FALSE(value
))
445 else if (ATTR_UNSET(value
))
446 value
= "unspecified";
448 fprintf(stderr
, "%s: %s => %s (%s)\n",
449 what
, attr
->name
, (char *) value
, match
);
451 #define debug_push(a) debug_info("push", (a))
452 #define debug_pop(a) debug_info("pop", (a))
454 #define debug_push(a) do { ; } while (0)
455 #define debug_pop(a) do { ; } while (0)
456 #define debug_set(a,b,c,d) do { ; } while (0)
459 static void drop_attr_stack(void)
462 struct attr_stack
*elem
= attr_stack
;
463 attr_stack
= elem
->prev
;
464 free_attr_elem(elem
);
468 static const char *git_etc_gitattributes(void)
470 static const char *system_wide
;
472 system_wide
= system_path(ETC_GITATTRIBUTES
);
476 static int git_attr_system(void)
478 return !git_env_bool("GIT_ATTR_NOSYSTEM", 0);
481 static void bootstrap_attr_stack(void)
483 struct attr_stack
*elem
;
484 char *xdg_attributes_file
;
489 elem
= read_attr_from_array(builtin_attr
);
491 elem
->prev
= attr_stack
;
494 if (git_attr_system()) {
495 elem
= read_attr_from_file(git_etc_gitattributes(), 1);
498 elem
->prev
= attr_stack
;
503 if (!git_attributes_file
) {
504 home_config_paths(NULL
, &xdg_attributes_file
, "attributes");
505 git_attributes_file
= xdg_attributes_file
;
507 if (git_attributes_file
) {
508 elem
= read_attr_from_file(git_attributes_file
, 1);
511 elem
->prev
= attr_stack
;
516 if (!is_bare_repository() || direction
== GIT_ATTR_INDEX
) {
517 elem
= read_attr(GITATTRIBUTES_FILE
, 1);
518 elem
->origin
= xstrdup("");
520 elem
->prev
= attr_stack
;
525 elem
= read_attr_from_file(git_path(INFOATTRIBUTES_FILE
), 1);
527 elem
= xcalloc(1, sizeof(*elem
));
529 elem
->prev
= attr_stack
;
533 static void prepare_attr_stack(const char *path
, int dirlen
)
535 struct attr_stack
*elem
, *info
;
540 * At the bottom of the attribute stack is the built-in
541 * set of attribute definitions, followed by the contents
542 * of $(prefix)/etc/gitattributes and a file specified by
543 * core.attributesfile. Then, contents from
544 * .gitattribute files from directories closer to the
545 * root to the ones in deeper directories are pushed
546 * to the stack. Finally, at the very top of the stack
547 * we always keep the contents of $GIT_DIR/info/attributes.
549 * When checking, we use entries from near the top of the
550 * stack, preferring $GIT_DIR/info/attributes, then
551 * .gitattributes in deeper directories to shallower ones,
552 * and finally use the built-in set as the default.
554 bootstrap_attr_stack();
557 * Pop the "info" one that is always at the top of the stack.
560 attr_stack
= info
->prev
;
563 * Pop the ones from directories that are not the prefix of
564 * the path we are checking. Break out of the loop when we see
565 * the root one (whose origin is an empty string "") or the builtin
566 * one (whose origin is NULL) without popping it.
568 while (attr_stack
->origin
) {
569 int namelen
= strlen(attr_stack
->origin
);
572 if (namelen
<= dirlen
&&
573 !strncmp(elem
->origin
, path
, namelen
) &&
574 (!namelen
|| path
[namelen
] == '/'))
578 attr_stack
= elem
->prev
;
579 free_attr_elem(elem
);
583 * Read from parent directories and push them down
585 if (!is_bare_repository() || direction
== GIT_ATTR_INDEX
) {
587 * bootstrap_attr_stack() should have added, and the
588 * above loop should have stopped before popping, the
589 * root element whose attr_stack->origin is set to an
592 struct strbuf pathbuf
= STRBUF_INIT
;
594 assert(attr_stack
->origin
);
596 len
= strlen(attr_stack
->origin
);
599 cp
= memchr(path
+ len
+ 1, '/', dirlen
- len
- 1);
602 strbuf_add(&pathbuf
, path
, cp
- path
);
603 strbuf_addch(&pathbuf
, '/');
604 strbuf_addstr(&pathbuf
, GITATTRIBUTES_FILE
);
605 elem
= read_attr(pathbuf
.buf
, 0);
606 strbuf_setlen(&pathbuf
, cp
- path
);
607 elem
->origin
= strbuf_detach(&pathbuf
, &elem
->originlen
);
608 elem
->prev
= attr_stack
;
613 strbuf_release(&pathbuf
);
617 * Finally push the "info" one at the top of the stack.
619 info
->prev
= attr_stack
;
623 static int path_matches(const char *pathname
, int pathlen
,
625 const struct pattern
*pat
,
626 const char *base
, int baselen
)
628 const char *pattern
= pat
->pattern
;
629 int prefix
= pat
->nowildcardlen
;
630 int isdir
= (pathlen
&& pathname
[pathlen
- 1] == '/');
632 if ((pat
->flags
& EXC_FLAG_MUSTBEDIR
) && !isdir
)
635 if (pat
->flags
& EXC_FLAG_NODIR
) {
636 return match_basename(pathname
+ basename_offset
,
637 pathlen
- basename_offset
- isdir
,
639 pat
->patternlen
, pat
->flags
);
641 return match_pathname(pathname
, pathlen
- isdir
,
643 pattern
, prefix
, pat
->patternlen
, pat
->flags
);
646 static int macroexpand_one(int attr_nr
, int rem
);
648 static int fill_one(const char *what
, struct match_attr
*a
, int rem
)
650 struct git_attr_check
*check
= check_all_attr
;
653 for (i
= a
->num_attr
- 1; 0 < rem
&& 0 <= i
; i
--) {
654 struct git_attr
*attr
= a
->state
[i
].attr
;
655 const char **n
= &(check
[attr
->attr_nr
].value
);
656 const char *v
= a
->state
[i
].setto
;
658 if (*n
== ATTR__UNKNOWN
) {
660 a
->is_macro
? a
->u
.attr
->name
: a
->u
.pat
.pattern
,
664 rem
= macroexpand_one(attr
->attr_nr
, rem
);
670 static int fill(const char *path
, int pathlen
, int basename_offset
,
671 struct attr_stack
*stk
, int rem
)
674 const char *base
= stk
->origin
? stk
->origin
: "";
676 for (i
= stk
->num_matches
- 1; 0 < rem
&& 0 <= i
; i
--) {
677 struct match_attr
*a
= stk
->attrs
[i
];
680 if (path_matches(path
, pathlen
, basename_offset
,
681 &a
->u
.pat
, base
, stk
->originlen
))
682 rem
= fill_one("fill", a
, rem
);
687 static int macroexpand_one(int nr
, int rem
)
689 struct attr_stack
*stk
;
690 struct match_attr
*a
= NULL
;
693 if (check_all_attr
[nr
].value
!= ATTR__TRUE
||
694 !check_all_attr
[nr
].attr
->maybe_macro
)
697 for (stk
= attr_stack
; !a
&& stk
; stk
= stk
->prev
)
698 for (i
= stk
->num_matches
- 1; !a
&& 0 <= i
; i
--) {
699 struct match_attr
*ma
= stk
->attrs
[i
];
702 if (ma
->u
.attr
->attr_nr
== nr
)
707 rem
= fill_one("expand", a
, rem
);
713 * Collect all attributes for path into the array pointed to by
716 static void collect_all_attrs(const char *path
)
718 struct attr_stack
*stk
;
719 int i
, pathlen
, rem
, dirlen
;
720 const char *cp
, *last_slash
= NULL
;
723 for (cp
= path
; *cp
; cp
++) {
724 if (*cp
== '/' && cp
[1])
729 basename_offset
= last_slash
+ 1 - path
;
730 dirlen
= last_slash
- path
;
736 prepare_attr_stack(path
, dirlen
);
737 for (i
= 0; i
< attr_nr
; i
++)
738 check_all_attr
[i
].value
= ATTR__UNKNOWN
;
741 for (stk
= attr_stack
; 0 < rem
&& stk
; stk
= stk
->prev
)
742 rem
= fill(path
, pathlen
, basename_offset
, stk
, rem
);
745 int git_check_attr(const char *path
, int num
, struct git_attr_check
*check
)
749 collect_all_attrs(path
);
751 for (i
= 0; i
< num
; i
++) {
752 const char *value
= check_all_attr
[check
[i
].attr
->attr_nr
].value
;
753 if (value
== ATTR__UNKNOWN
)
755 check
[i
].value
= value
;
761 int git_all_attrs(const char *path
, int *num
, struct git_attr_check
**check
)
765 collect_all_attrs(path
);
767 /* Count the number of attributes that are set. */
769 for (i
= 0; i
< attr_nr
; i
++) {
770 const char *value
= check_all_attr
[i
].value
;
771 if (value
!= ATTR__UNSET
&& value
!= ATTR__UNKNOWN
)
775 *check
= xmalloc(sizeof(**check
) * count
);
777 for (i
= 0; i
< attr_nr
; i
++) {
778 const char *value
= check_all_attr
[i
].value
;
779 if (value
!= ATTR__UNSET
&& value
!= ATTR__UNKNOWN
) {
780 (*check
)[j
].attr
= check_all_attr
[i
].attr
;
781 (*check
)[j
].value
= value
;
789 void git_attr_set_direction(enum git_attr_direction
new, struct index_state
*istate
)
791 enum git_attr_direction old
= direction
;
793 if (is_bare_repository() && new != GIT_ATTR_INDEX
)
794 die("BUG: non-INDEX attr direction in a bare repo");