2 * Handle git attributes. See gitattributes(5) for a description of
3 * the file syntax, and attr.h for a description of the API.
5 * One basic design decision here is that we are not going to support
6 * an insanely large number of attributes.
9 #include "git-compat-util.h"
11 #include "environment.h"
19 #include "read-cache-ll.h"
22 #include "object-store-ll.h"
24 #include "thread-utils.h"
25 #include "tree-walk.h"
26 #include "object-name.h"
28 const char *git_attr_tree
;
30 const char git_attr__true
[] = "(builtin)true";
31 const char git_attr__false
[] = "\0(builtin)false";
32 static const char git_attr__unknown
[] = "(builtin)unknown";
33 #define ATTR__TRUE git_attr__true
34 #define ATTR__FALSE git_attr__false
35 #define ATTR__UNSET NULL
36 #define ATTR__UNKNOWN git_attr__unknown
39 unsigned int attr_nr
; /* unique attribute number */
40 char name
[FLEX_ARRAY
]; /* attribute name */
43 const char *git_attr_name(const struct git_attr
*attr
)
50 pthread_mutex_t mutex
;
53 static inline void hashmap_lock(struct attr_hashmap
*map
)
55 pthread_mutex_lock(&map
->mutex
);
58 static inline void hashmap_unlock(struct attr_hashmap
*map
)
60 pthread_mutex_unlock(&map
->mutex
);
63 /* The container for objects stored in "struct attr_hashmap" */
64 struct attr_hash_entry
{
65 struct hashmap_entry ent
;
66 const char *key
; /* the key; memory should be owned by value */
67 size_t keylen
; /* length of the key */
68 void *value
; /* the stored value */
71 /* attr_hashmap comparison function */
72 static int attr_hash_entry_cmp(const void *cmp_data UNUSED
,
73 const struct hashmap_entry
*eptr
,
74 const struct hashmap_entry
*entry_or_key
,
75 const void *keydata UNUSED
)
77 const struct attr_hash_entry
*a
, *b
;
79 a
= container_of(eptr
, const struct attr_hash_entry
, ent
);
80 b
= container_of(entry_or_key
, const struct attr_hash_entry
, ent
);
81 return (a
->keylen
!= b
->keylen
) || strncmp(a
->key
, b
->key
, a
->keylen
);
85 * The global dictionary of all interned attributes. This
86 * is a singleton object which is shared between threads.
87 * Access to this dictionary must be surrounded with a mutex.
89 static struct attr_hashmap g_attr_hashmap
= {
90 .map
= HASHMAP_INIT(attr_hash_entry_cmp
, NULL
),
94 * Retrieve the 'value' stored in a hashmap given the provided 'key'.
95 * If there is no matching entry, return NULL.
97 static void *attr_hashmap_get(struct attr_hashmap
*map
,
98 const char *key
, size_t keylen
)
100 struct attr_hash_entry k
;
101 struct attr_hash_entry
*e
;
103 hashmap_entry_init(&k
.ent
, memhash(key
, keylen
));
106 e
= hashmap_get_entry(&map
->map
, &k
, ent
, NULL
);
108 return e
? e
->value
: NULL
;
111 /* Add 'value' to a hashmap based on the provided 'key'. */
112 static void attr_hashmap_add(struct attr_hashmap
*map
,
113 const char *key
, size_t keylen
,
116 struct attr_hash_entry
*e
;
118 e
= xmalloc(sizeof(struct attr_hash_entry
));
119 hashmap_entry_init(&e
->ent
, memhash(key
, keylen
));
124 hashmap_add(&map
->map
, &e
->ent
);
127 struct all_attrs_item
{
128 const struct git_attr
*attr
;
131 * If 'macro' is non-NULL, indicates that 'attr' is a macro based on
132 * the current attribute stack and contains a pointer to the match_attr
133 * definition of the macro
135 const struct match_attr
*macro
;
139 * Reallocate and reinitialize the array of all attributes (which is used in
140 * the attribute collection process) in 'check' based on the global dictionary
143 static void all_attrs_init(struct attr_hashmap
*map
, struct attr_check
*check
)
150 size
= hashmap_get_size(&map
->map
);
151 if (size
< check
->all_attrs_nr
)
152 BUG("interned attributes shouldn't be deleted");
155 * If the number of attributes in the global dictionary has increased
156 * (or this attr_check instance doesn't have an initialized all_attrs
157 * field), reallocate the provided attr_check instance's all_attrs
158 * field and fill each entry with its corresponding git_attr.
160 if (size
!= check
->all_attrs_nr
) {
161 struct attr_hash_entry
*e
;
162 struct hashmap_iter iter
;
164 REALLOC_ARRAY(check
->all_attrs
, size
);
165 check
->all_attrs_nr
= size
;
167 hashmap_for_each_entry(&map
->map
, &iter
, e
,
168 ent
/* member name */) {
169 const struct git_attr
*a
= e
->value
;
170 check
->all_attrs
[a
->attr_nr
].attr
= a
;
177 * Re-initialize every entry in check->all_attrs.
178 * This re-initialization can live outside of the locked region since
179 * the attribute dictionary is no longer being accessed.
181 for (i
= 0; i
< check
->all_attrs_nr
; i
++) {
182 check
->all_attrs
[i
].value
= ATTR__UNKNOWN
;
183 check
->all_attrs
[i
].macro
= NULL
;
188 * Atribute name cannot begin with "builtin_" which
189 * is a reserved namespace for built in attributes values.
191 static int attr_name_reserved(const char *name
)
193 return starts_with(name
, "builtin_");
196 static int attr_name_valid(const char *name
, size_t namelen
)
199 * Attribute name cannot begin with '-' and must consist of
200 * characters from [-A-Za-z0-9_.].
202 if (namelen
<= 0 || *name
== '-')
206 if (! (ch
== '-' || ch
== '.' || ch
== '_' ||
207 ('0' <= ch
&& ch
<= '9') ||
208 ('a' <= ch
&& ch
<= 'z') ||
209 ('A' <= ch
&& ch
<= 'Z')) )
215 static void report_invalid_attr(const char *name
, size_t len
,
216 const char *src
, int lineno
)
218 struct strbuf err
= STRBUF_INIT
;
219 strbuf_addf(&err
, _("%.*s is not a valid attribute name"),
221 fprintf(stderr
, "%s: %s:%d\n", err
.buf
, src
, lineno
);
222 strbuf_release(&err
);
226 * Given a 'name', lookup and return the corresponding attribute in the global
227 * dictionary. If no entry is found, create a new attribute and store it in
230 static const struct git_attr
*git_attr_internal(const char *name
, size_t namelen
)
234 if (!attr_name_valid(name
, namelen
))
237 hashmap_lock(&g_attr_hashmap
);
239 a
= attr_hashmap_get(&g_attr_hashmap
, name
, namelen
);
242 FLEX_ALLOC_MEM(a
, name
, name
, namelen
);
243 a
->attr_nr
= hashmap_get_size(&g_attr_hashmap
.map
);
245 attr_hashmap_add(&g_attr_hashmap
, a
->name
, namelen
, a
);
246 if (a
->attr_nr
!= hashmap_get_size(&g_attr_hashmap
.map
) - 1)
247 die(_("unable to add additional attribute"));
250 hashmap_unlock(&g_attr_hashmap
);
255 const struct git_attr
*git_attr(const char *name
)
257 return git_attr_internal(name
, strlen(name
));
260 /* What does a matched pattern decide? */
262 const struct git_attr
*attr
;
270 unsigned flags
; /* PATTERN_FLAG_* */
274 * One rule, as from a .gitattributes file.
276 * If is_macro is true, then u.attr is a pointer to the git_attr being
279 * If is_macro is false, then u.pat is the filename pattern to which the
282 * In either case, num_attr is the number of attributes affected by
283 * this rule, and state is an array listing them. The attributes are
284 * listed as they appear in the file (macros unexpanded).
289 const struct git_attr
*attr
;
293 struct attr_state state
[FLEX_ARRAY
];
296 static const char blank
[] = " \t\r\n";
298 /* Flags usable in read_attr() and parse_attr_line() family of functions. */
299 #define READ_ATTR_MACRO_OK (1<<0)
300 #define READ_ATTR_NOFOLLOW (1<<1)
303 * Parse a whitespace-delimited attribute state (i.e., "attr",
304 * "-attr", "!attr", or "attr=value") from the string starting at src.
305 * If e is not NULL, write the results to *e. Return a pointer to the
306 * remainder of the string (with leading whitespace removed), or NULL
307 * if there was an error.
309 static const char *parse_attr(const char *src
, int lineno
, const char *cp
,
310 struct attr_state
*e
)
312 const char *ep
, *equals
;
315 ep
= cp
+ strcspn(cp
, blank
);
316 equals
= strchr(cp
, '=');
317 if (equals
&& ep
< equals
)
324 if (*cp
== '-' || *cp
== '!') {
328 if (!attr_name_valid(cp
, len
) || attr_name_reserved(cp
)) {
329 report_invalid_attr(cp
, len
, src
, lineno
);
334 * As this function is always called twice, once with
335 * e == NULL in the first pass and then e != NULL in
336 * the second pass, no need for attr_name_valid()
339 if (*cp
== '-' || *cp
== '!') {
340 e
->setto
= (*cp
== '-') ? ATTR__FALSE
: ATTR__UNSET
;
345 e
->setto
= ATTR__TRUE
;
347 e
->setto
= xmemdupz(equals
+ 1, ep
- equals
- 1);
349 e
->attr
= git_attr_internal(cp
, len
);
351 return ep
+ strspn(ep
, blank
);
354 static struct match_attr
*parse_attr_line(const char *line
, const char *src
,
355 int lineno
, unsigned flags
)
357 size_t namelen
, num_attr
, i
;
358 const char *cp
, *name
, *states
;
359 struct match_attr
*res
= NULL
;
361 struct strbuf pattern
= STRBUF_INIT
;
363 cp
= line
+ strspn(line
, blank
);
364 if (!*cp
|| *cp
== '#')
368 if (strlen(line
) >= ATTR_MAX_LINE_LENGTH
) {
369 warning(_("ignoring overly long attributes line %d"), lineno
);
373 if (*cp
== '"' && !unquote_c_style(&pattern
, name
, &states
)) {
375 namelen
= pattern
.len
;
377 namelen
= strcspn(name
, blank
);
378 states
= name
+ namelen
;
381 if (strlen(ATTRIBUTE_MACRO_PREFIX
) < namelen
&&
382 starts_with(name
, ATTRIBUTE_MACRO_PREFIX
)) {
383 if (!(flags
& READ_ATTR_MACRO_OK
)) {
384 fprintf_ln(stderr
, _("%s not allowed: %s:%d"),
389 name
+= strlen(ATTRIBUTE_MACRO_PREFIX
);
390 name
+= strspn(name
, blank
);
391 namelen
= strcspn(name
, blank
);
392 if (!attr_name_valid(name
, namelen
) || attr_name_reserved(name
)) {
393 report_invalid_attr(name
, namelen
, src
, lineno
);
400 states
+= strspn(states
, blank
);
402 /* First pass to count the attr_states */
403 for (cp
= states
, num_attr
= 0; *cp
; num_attr
++) {
404 cp
= parse_attr(src
, lineno
, cp
, NULL
);
409 res
= xcalloc(1, st_add3(sizeof(*res
),
410 st_mult(sizeof(struct attr_state
), num_attr
),
411 is_macro
? 0 : namelen
+ 1));
413 res
->u
.attr
= git_attr_internal(name
, namelen
);
415 char *p
= (char *)&(res
->state
[num_attr
]);
416 memcpy(p
, name
, namelen
);
417 res
->u
.pat
.pattern
= p
;
418 parse_path_pattern(&res
->u
.pat
.pattern
,
419 &res
->u
.pat
.patternlen
,
421 &res
->u
.pat
.nowildcardlen
);
422 if (res
->u
.pat
.flags
& PATTERN_FLAG_NEGATIVE
) {
423 warning(_("Negative patterns are ignored in git attributes\n"
424 "Use '\\!' for literal leading exclamation."));
428 res
->is_macro
= is_macro
;
429 res
->num_attr
= num_attr
;
431 /* Second pass to fill the attr_states */
432 for (cp
= states
, i
= 0; *cp
; i
++) {
433 cp
= parse_attr(src
, lineno
, cp
, &(res
->state
[i
]));
436 strbuf_release(&pattern
);
440 strbuf_release(&pattern
);
446 * Like info/exclude and .gitignore, the attribute information can
447 * come from many places.
449 * (1) .gitattributes file of the same directory;
450 * (2) .gitattributes file of the parent directory if (1) does not have
451 * any match; this goes recursively upwards, just like .gitignore.
452 * (3) $GIT_DIR/info/attributes, which overrides both of the above.
454 * In the same file, later entries override the earlier match, so in the
455 * global list, we would have entries from info/attributes the earliest
456 * (reading the file from top to bottom), .gitattributes of the root
457 * directory (again, reading the file from top to bottom) down to the
458 * current directory, and then scan the list backwards to find the first match.
459 * This is exactly the same as what is_excluded() does in dir.c to deal with
460 * .gitignore file and info/excludes file as a fallback.
464 struct attr_stack
*prev
;
467 unsigned num_matches
;
469 struct match_attr
**attrs
;
472 static void attr_stack_free(struct attr_stack
*e
)
476 for (i
= 0; i
< e
->num_matches
; i
++) {
477 struct match_attr
*a
= e
->attrs
[i
];
480 for (j
= 0; j
< a
->num_attr
; j
++) {
481 const char *setto
= a
->state
[j
].setto
;
482 if (setto
== ATTR__TRUE
||
483 setto
== ATTR__FALSE
||
484 setto
== ATTR__UNSET
||
485 setto
== ATTR__UNKNOWN
)
488 free((char *) setto
);
496 static void drop_attr_stack(struct attr_stack
**stack
)
499 struct attr_stack
*elem
= *stack
;
501 attr_stack_free(elem
);
505 /* List of all attr_check structs; access should be surrounded by mutex */
506 static struct check_vector
{
509 struct attr_check
**checks
;
510 pthread_mutex_t mutex
;
513 static inline void vector_lock(void)
515 pthread_mutex_lock(&check_vector
.mutex
);
518 static inline void vector_unlock(void)
520 pthread_mutex_unlock(&check_vector
.mutex
);
523 static void check_vector_add(struct attr_check
*c
)
527 ALLOC_GROW(check_vector
.checks
,
530 check_vector
.checks
[check_vector
.nr
++] = c
;
535 static void check_vector_remove(struct attr_check
*check
)
542 for (i
= 0; i
< check_vector
.nr
; i
++)
543 if (check_vector
.checks
[i
] == check
)
546 if (i
>= check_vector
.nr
)
547 BUG("no entry found");
549 /* shift entries over */
550 for (; i
< check_vector
.nr
- 1; i
++)
551 check_vector
.checks
[i
] = check_vector
.checks
[i
+ 1];
558 /* Iterate through all attr_check instances and drop their stacks */
559 static void drop_all_attr_stacks(void)
565 for (i
= 0; i
< check_vector
.nr
; i
++) {
566 drop_attr_stack(&check_vector
.checks
[i
]->stack
);
572 struct attr_check
*attr_check_alloc(void)
574 struct attr_check
*c
= xcalloc(1, sizeof(struct attr_check
));
576 /* save pointer to the check struct */
582 struct attr_check
*attr_check_initl(const char *one
, ...)
584 struct attr_check
*check
;
589 va_start(params
, one
);
590 for (cnt
= 1; (param
= va_arg(params
, const char *)) != NULL
; cnt
++)
594 check
= attr_check_alloc();
597 CALLOC_ARRAY(check
->items
, cnt
);
599 check
->items
[0].attr
= git_attr(one
);
600 va_start(params
, one
);
601 for (cnt
= 1; cnt
< check
->nr
; cnt
++) {
602 const struct git_attr
*attr
;
603 param
= va_arg(params
, const char *);
605 BUG("counted %d != ended at %d",
607 attr
= git_attr(param
);
609 BUG("%s: not a valid attribute name", param
);
610 check
->items
[cnt
].attr
= attr
;
616 struct attr_check
*attr_check_dup(const struct attr_check
*check
)
618 struct attr_check
*ret
;
623 ret
= attr_check_alloc();
626 ret
->alloc
= check
->alloc
;
627 DUP_ARRAY(ret
->items
, check
->items
, ret
->nr
);
632 struct attr_check_item
*attr_check_append(struct attr_check
*check
,
633 const struct git_attr
*attr
)
635 struct attr_check_item
*item
;
637 ALLOC_GROW(check
->items
, check
->nr
+ 1, check
->alloc
);
638 item
= &check
->items
[check
->nr
++];
643 void attr_check_reset(struct attr_check
*check
)
648 void attr_check_clear(struct attr_check
*check
)
650 FREE_AND_NULL(check
->items
);
654 FREE_AND_NULL(check
->all_attrs
);
655 check
->all_attrs_nr
= 0;
657 drop_attr_stack(&check
->stack
);
660 void attr_check_free(struct attr_check
*check
)
663 /* Remove check from the check vector */
664 check_vector_remove(check
);
666 attr_check_clear(check
);
671 static const char *builtin_attr
[] = {
672 "[attr]binary -diff -merge -text",
676 static void handle_attr_line(struct attr_stack
*res
,
682 struct match_attr
*a
;
684 a
= parse_attr_line(line
, src
, lineno
, flags
);
687 ALLOC_GROW_BY(res
->attrs
, res
->num_matches
, 1, res
->alloc
);
688 res
->attrs
[res
->num_matches
- 1] = a
;
691 static struct attr_stack
*read_attr_from_array(const char **list
)
693 struct attr_stack
*res
;
697 CALLOC_ARRAY(res
, 1);
698 while ((line
= *(list
++)) != NULL
)
699 handle_attr_line(res
, line
, "[builtin]", ++lineno
,
705 * Callers into the attribute system assume there is a single, system-wide
706 * global state where attributes are read from and when the state is flipped by
707 * calling git_attr_set_direction(), the stack frames that have been
708 * constructed need to be discarded so that subsequent calls into the
709 * attribute system will lazily read from the right place. Since changing
710 * direction causes a global paradigm shift, it should not ever be called while
711 * another thread could potentially be calling into the attribute system.
713 static enum git_attr_direction direction
;
715 void git_attr_set_direction(enum git_attr_direction new_direction
)
717 if (is_bare_repository() && new_direction
!= GIT_ATTR_INDEX
)
718 BUG("non-INDEX attr direction in a bare repo");
720 if (new_direction
!= direction
)
721 drop_all_attr_stacks();
723 direction
= new_direction
;
726 static struct attr_stack
*read_attr_from_file(const char *path
, unsigned flags
)
728 struct strbuf buf
= STRBUF_INIT
;
731 struct attr_stack
*res
;
735 if (flags
& READ_ATTR_NOFOLLOW
)
736 fd
= open_nofollow(path
, O_RDONLY
);
738 fd
= open(path
, O_RDONLY
);
741 warn_on_fopen_errors(path
);
744 fp
= xfdopen(fd
, "r");
745 if (fstat(fd
, &st
)) {
746 warning_errno(_("cannot fstat gitattributes file '%s'"), path
);
750 if (st
.st_size
>= ATTR_MAX_FILE_SIZE
) {
751 warning(_("ignoring overly large gitattributes file '%s'"), path
);
756 CALLOC_ARRAY(res
, 1);
757 while (strbuf_getline(&buf
, fp
) != EOF
) {
758 if (!lineno
&& starts_with(buf
.buf
, utf8_bom
))
759 strbuf_remove(&buf
, 0, strlen(utf8_bom
));
760 handle_attr_line(res
, buf
.buf
, path
, ++lineno
, flags
);
764 strbuf_release(&buf
);
768 static struct attr_stack
*read_attr_from_buf(char *buf
, const char *path
,
771 struct attr_stack
*res
;
778 CALLOC_ARRAY(res
, 1);
779 for (sp
= buf
; *sp
;) {
783 ep
= strchrnul(sp
, '\n');
784 more
= (*ep
== '\n');
786 handle_attr_line(res
, sp
, path
, ++lineno
, flags
);
794 static struct attr_stack
*read_attr_from_blob(struct index_state
*istate
,
795 const struct object_id
*tree_oid
,
796 const char *path
, unsigned flags
)
798 struct object_id oid
;
800 enum object_type type
;
807 if (get_tree_entry(istate
->repo
, tree_oid
, path
, &oid
, &mode
))
810 buf
= repo_read_object_file(istate
->repo
, &oid
, &type
, &sz
);
811 if (!buf
|| type
!= OBJ_BLOB
) {
816 return read_attr_from_buf(buf
, path
, flags
);
819 static struct attr_stack
*read_attr_from_index(struct index_state
*istate
,
820 const char *path
, unsigned flags
)
822 struct attr_stack
*stack
= NULL
;
825 int sparse_dir_pos
= -1;
831 * When handling sparse-checkouts, .gitattributes files
832 * may reside within a sparse directory. We distinguish
833 * whether a path exists directly in the index or not by
834 * evaluating if 'pos' is negative.
835 * If 'pos' is negative, the path is not directly present
836 * in the index and is likely within a sparse directory.
837 * For paths not in the index, The absolute value of 'pos'
838 * minus 1 gives us the position where the path would be
839 * inserted in lexicographic order within the index.
840 * We then subtract another 1 from this value
841 * (sparse_dir_pos = -pos - 2) to find the position of the
842 * last index entry which is lexicographically smaller than
843 * the path. This would be the sparse directory containing
844 * the path. By identifying the sparse directory containing
845 * the path, we can correctly read the attributes specified
846 * in the .gitattributes file from the tree object of the
849 if (!path_in_cone_mode_sparse_checkout(path
, istate
)) {
850 int pos
= index_name_pos_sparse(istate
, path
, strlen(path
));
853 sparse_dir_pos
= -pos
- 2;
856 if (sparse_dir_pos
>= 0 &&
857 S_ISSPARSEDIR(istate
->cache
[sparse_dir_pos
]->ce_mode
) &&
858 !strncmp(istate
->cache
[sparse_dir_pos
]->name
, path
, ce_namelen(istate
->cache
[sparse_dir_pos
]))) {
859 const char *relative_path
= path
+ ce_namelen(istate
->cache
[sparse_dir_pos
]);
860 stack
= read_attr_from_blob(istate
, &istate
->cache
[sparse_dir_pos
]->oid
, relative_path
, flags
);
862 buf
= read_blob_data_from_index(istate
, path
, &size
);
865 if (size
>= ATTR_MAX_FILE_SIZE
) {
866 warning(_("ignoring overly large gitattributes blob '%s'"), path
);
869 stack
= read_attr_from_buf(buf
, path
, flags
);
874 static struct attr_stack
*read_attr(struct index_state
*istate
,
875 const struct object_id
*tree_oid
,
876 const char *path
, unsigned flags
)
878 struct attr_stack
*res
= NULL
;
880 if (direction
== GIT_ATTR_INDEX
) {
881 res
= read_attr_from_index(istate
, path
, flags
);
882 } else if (tree_oid
) {
883 res
= read_attr_from_blob(istate
, tree_oid
, path
, flags
);
884 } else if (!is_bare_repository()) {
885 if (direction
== GIT_ATTR_CHECKOUT
) {
886 res
= read_attr_from_index(istate
, path
, flags
);
888 res
= read_attr_from_file(path
, flags
);
889 } else if (direction
== GIT_ATTR_CHECKIN
) {
890 res
= read_attr_from_file(path
, flags
);
893 * There is no checked out .gitattributes file
894 * there, but we might have it in the index.
895 * We allow operation in a sparsely checked out
896 * work tree, so read from it.
898 res
= read_attr_from_index(istate
, path
, flags
);
903 CALLOC_ARRAY(res
, 1);
907 const char *git_attr_system_file(void)
909 static const char *system_wide
;
911 system_wide
= system_path(ETC_GITATTRIBUTES
);
915 const char *git_attr_global_file(void)
917 if (!git_attributes_file
)
918 git_attributes_file
= xdg_config_home("attributes");
920 return git_attributes_file
;
923 int git_attr_system_is_enabled(void)
925 return !git_env_bool("GIT_ATTR_NOSYSTEM", 0);
928 static GIT_PATH_FUNC(git_path_info_attributes
, INFOATTRIBUTES_FILE
)
930 static void push_stack(struct attr_stack
**attr_stack_p
,
931 struct attr_stack
*elem
, char *origin
, size_t originlen
)
934 elem
->origin
= origin
;
936 elem
->originlen
= originlen
;
937 elem
->prev
= *attr_stack_p
;
938 *attr_stack_p
= elem
;
942 static void bootstrap_attr_stack(struct index_state
*istate
,
943 const struct object_id
*tree_oid
,
944 struct attr_stack
**stack
)
946 struct attr_stack
*e
;
947 unsigned flags
= READ_ATTR_MACRO_OK
;
953 e
= read_attr_from_array(builtin_attr
);
954 push_stack(stack
, e
, NULL
, 0);
956 /* system-wide frame */
957 if (git_attr_system_is_enabled()) {
958 e
= read_attr_from_file(git_attr_system_file(), flags
);
959 push_stack(stack
, e
, NULL
, 0);
963 if (git_attr_global_file()) {
964 e
= read_attr_from_file(git_attr_global_file(), flags
);
965 push_stack(stack
, e
, NULL
, 0);
969 e
= read_attr(istate
, tree_oid
, GITATTRIBUTES_FILE
, flags
| READ_ATTR_NOFOLLOW
);
970 push_stack(stack
, e
, xstrdup(""), 0);
973 if (startup_info
->have_repository
)
974 e
= read_attr_from_file(git_path_info_attributes(), flags
);
979 push_stack(stack
, e
, NULL
, 0);
982 static void prepare_attr_stack(struct index_state
*istate
,
983 const struct object_id
*tree_oid
,
984 const char *path
, int dirlen
,
985 struct attr_stack
**stack
)
987 struct attr_stack
*info
;
988 struct strbuf pathbuf
= STRBUF_INIT
;
991 * At the bottom of the attribute stack is the built-in
992 * set of attribute definitions, followed by the contents
993 * of $(prefix)/etc/gitattributes and a file specified by
994 * core.attributesfile. Then, contents from
995 * .gitattributes files from directories closer to the
996 * root to the ones in deeper directories are pushed
997 * to the stack. Finally, at the very top of the stack
998 * we always keep the contents of $GIT_DIR/info/attributes.
1000 * When checking, we use entries from near the top of the
1001 * stack, preferring $GIT_DIR/info/attributes, then
1002 * .gitattributes in deeper directories to shallower ones,
1003 * and finally use the built-in set as the default.
1005 bootstrap_attr_stack(istate
, tree_oid
, stack
);
1008 * Pop the "info" one that is always at the top of the stack.
1011 *stack
= info
->prev
;
1014 * Pop the ones from directories that are not the prefix of
1015 * the path we are checking. Break out of the loop when we see
1016 * the root one (whose origin is an empty string "") or the builtin
1017 * one (whose origin is NULL) without popping it.
1019 while ((*stack
)->origin
) {
1020 int namelen
= (*stack
)->originlen
;
1021 struct attr_stack
*elem
;
1024 if (namelen
<= dirlen
&&
1025 !strncmp(elem
->origin
, path
, namelen
) &&
1026 (!namelen
|| path
[namelen
] == '/'))
1029 *stack
= elem
->prev
;
1030 attr_stack_free(elem
);
1034 * bootstrap_attr_stack() should have added, and the
1035 * above loop should have stopped before popping, the
1036 * root element whose attr_stack->origin is set to an
1039 assert((*stack
)->origin
);
1041 strbuf_addstr(&pathbuf
, (*stack
)->origin
);
1042 /* Build up to the directory 'path' is in */
1043 while (pathbuf
.len
< dirlen
) {
1044 size_t len
= pathbuf
.len
;
1045 struct attr_stack
*next
;
1048 /* Skip path-separator */
1049 if (len
< dirlen
&& is_dir_sep(path
[len
]))
1051 /* Find the end of the next component */
1052 while (len
< dirlen
&& !is_dir_sep(path
[len
]))
1055 if (pathbuf
.len
> 0)
1056 strbuf_addch(&pathbuf
, '/');
1057 strbuf_add(&pathbuf
, path
+ pathbuf
.len
, (len
- pathbuf
.len
));
1058 strbuf_addf(&pathbuf
, "/%s", GITATTRIBUTES_FILE
);
1060 next
= read_attr(istate
, tree_oid
, pathbuf
.buf
, READ_ATTR_NOFOLLOW
);
1062 /* reset the pathbuf to not include "/.gitattributes" */
1063 strbuf_setlen(&pathbuf
, len
);
1065 origin
= xstrdup(pathbuf
.buf
);
1066 push_stack(stack
, next
, origin
, len
);
1070 * Finally push the "info" one at the top of the stack.
1072 push_stack(stack
, info
, NULL
, 0);
1074 strbuf_release(&pathbuf
);
1077 static int path_matches(const char *pathname
, int pathlen
,
1078 int basename_offset
,
1079 const struct pattern
*pat
,
1080 const char *base
, int baselen
)
1082 const char *pattern
= pat
->pattern
;
1083 int prefix
= pat
->nowildcardlen
;
1084 int isdir
= (pathlen
&& pathname
[pathlen
- 1] == '/');
1086 if ((pat
->flags
& PATTERN_FLAG_MUSTBEDIR
) && !isdir
)
1089 if (pat
->flags
& PATTERN_FLAG_NODIR
) {
1090 return match_basename(pathname
+ basename_offset
,
1091 pathlen
- basename_offset
- isdir
,
1093 pat
->patternlen
, pat
->flags
);
1095 return match_pathname(pathname
, pathlen
- isdir
,
1097 pattern
, prefix
, pat
->patternlen
);
1100 static int macroexpand_one(struct all_attrs_item
*all_attrs
, int nr
, int rem
);
1102 static int fill_one(struct all_attrs_item
*all_attrs
,
1103 const struct match_attr
*a
, int rem
)
1107 for (i
= a
->num_attr
; rem
> 0 && i
> 0; i
--) {
1108 const struct git_attr
*attr
= a
->state
[i
- 1].attr
;
1109 const char **n
= &(all_attrs
[attr
->attr_nr
].value
);
1110 const char *v
= a
->state
[i
- 1].setto
;
1112 if (*n
== ATTR__UNKNOWN
) {
1115 rem
= macroexpand_one(all_attrs
, attr
->attr_nr
, rem
);
1121 static int fill(const char *path
, int pathlen
, int basename_offset
,
1122 const struct attr_stack
*stack
,
1123 struct all_attrs_item
*all_attrs
, int rem
)
1125 for (; rem
> 0 && stack
; stack
= stack
->prev
) {
1127 const char *base
= stack
->origin
? stack
->origin
: "";
1129 for (i
= stack
->num_matches
; 0 < rem
&& 0 < i
; i
--) {
1130 const struct match_attr
*a
= stack
->attrs
[i
- 1];
1133 if (path_matches(path
, pathlen
, basename_offset
,
1134 &a
->u
.pat
, base
, stack
->originlen
))
1135 rem
= fill_one(all_attrs
, a
, rem
);
1142 static int macroexpand_one(struct all_attrs_item
*all_attrs
, int nr
, int rem
)
1144 const struct all_attrs_item
*item
= &all_attrs
[nr
];
1146 if (item
->macro
&& item
->value
== ATTR__TRUE
)
1147 return fill_one(all_attrs
, item
->macro
, rem
);
1153 * Marks the attributes which are macros based on the attribute stack.
1154 * This prevents having to search through the attribute stack each time
1155 * a macro needs to be expanded during the fill stage.
1157 static void determine_macros(struct all_attrs_item
*all_attrs
,
1158 const struct attr_stack
*stack
)
1160 for (; stack
; stack
= stack
->prev
) {
1162 for (i
= stack
->num_matches
; i
> 0; i
--) {
1163 const struct match_attr
*ma
= stack
->attrs
[i
- 1];
1165 unsigned int n
= ma
->u
.attr
->attr_nr
;
1166 if (!all_attrs
[n
].macro
) {
1167 all_attrs
[n
].macro
= ma
;
1175 * Collect attributes for path into the array pointed to by check->all_attrs.
1176 * If check->check_nr is non-zero, only attributes in check[] are collected.
1177 * Otherwise all attributes are collected.
1179 static void collect_some_attrs(struct index_state
*istate
,
1180 const struct object_id
*tree_oid
,
1181 const char *path
, struct attr_check
*check
)
1183 int pathlen
, rem
, dirlen
;
1184 const char *cp
, *last_slash
= NULL
;
1185 int basename_offset
;
1187 for (cp
= path
; *cp
; cp
++) {
1188 if (*cp
== '/' && cp
[1])
1191 pathlen
= cp
- path
;
1193 basename_offset
= last_slash
+ 1 - path
;
1194 dirlen
= last_slash
- path
;
1196 basename_offset
= 0;
1200 prepare_attr_stack(istate
, tree_oid
, path
, dirlen
, &check
->stack
);
1201 all_attrs_init(&g_attr_hashmap
, check
);
1202 determine_macros(check
->all_attrs
, check
->stack
);
1204 rem
= check
->all_attrs_nr
;
1205 fill(path
, pathlen
, basename_offset
, check
->stack
, check
->all_attrs
, rem
);
1208 static const char *default_attr_source_tree_object_name
;
1209 static int ignore_bad_attr_tree
;
1211 void set_git_attr_source(const char *tree_object_name
)
1213 default_attr_source_tree_object_name
= xstrdup(tree_object_name
);
1216 static void compute_default_attr_source(struct object_id
*attr_source
)
1218 if (!default_attr_source_tree_object_name
)
1219 default_attr_source_tree_object_name
= getenv(GIT_ATTR_SOURCE_ENVIRONMENT
);
1221 if (!default_attr_source_tree_object_name
&& git_attr_tree
) {
1222 default_attr_source_tree_object_name
= git_attr_tree
;
1223 ignore_bad_attr_tree
= 1;
1226 if (!default_attr_source_tree_object_name
&&
1227 startup_info
->have_repository
&&
1228 is_bare_repository()) {
1229 default_attr_source_tree_object_name
= "HEAD";
1230 ignore_bad_attr_tree
= 1;
1233 if (!default_attr_source_tree_object_name
|| !is_null_oid(attr_source
))
1236 if (repo_get_oid_treeish(the_repository
,
1237 default_attr_source_tree_object_name
,
1238 attr_source
) && !ignore_bad_attr_tree
)
1239 die(_("bad --attr-source or GIT_ATTR_SOURCE"));
1242 static struct object_id
*default_attr_source(void)
1244 static struct object_id attr_source
;
1246 if (is_null_oid(&attr_source
))
1247 compute_default_attr_source(&attr_source
);
1248 if (is_null_oid(&attr_source
))
1250 return &attr_source
;
1253 static const char *interned_mode_string(unsigned int mode
)
1267 for (i
= 0; i
< ARRAY_SIZE(mode_string
); i
++) {
1268 if (mode_string
[i
].val
!= mode
)
1270 if (!*mode_string
[i
].str
)
1271 snprintf(mode_string
[i
].str
, sizeof(mode_string
[i
].str
),
1273 return mode_string
[i
].str
;
1275 BUG("Unsupported mode 0%o", mode
);
1278 static const char *builtin_object_mode_attr(struct index_state
*istate
, const char *path
)
1282 if (direction
== GIT_ATTR_CHECKIN
) {
1283 struct object_id oid
;
1285 if (lstat(path
, &st
))
1286 die_errno(_("unable to stat '%s'"), path
);
1287 mode
= canon_mode(st
.st_mode
);
1288 if (S_ISDIR(mode
)) {
1290 *`path` is either a directory or it is a submodule,
1291 * in which case it is already indexed as submodule
1292 * or it does not exist in the index yet and we need to
1293 * check if we can resolve to a ref.
1295 int pos
= index_name_pos(istate
, path
, strlen(path
));
1297 if (S_ISGITLINK(istate
->cache
[pos
]->ce_mode
))
1298 mode
= istate
->cache
[pos
]->ce_mode
;
1299 } else if (resolve_gitlink_ref(path
, "HEAD", &oid
) == 0) {
1305 * For GIT_ATTR_CHECKOUT and GIT_ATTR_INDEX we only check
1306 * for mode in the index.
1308 int pos
= index_name_pos(istate
, path
, strlen(path
));
1310 mode
= istate
->cache
[pos
]->ce_mode
;
1315 return interned_mode_string(mode
);
1319 static const char *compute_builtin_attr(struct index_state
*istate
,
1321 const struct git_attr
*attr
) {
1322 static const struct git_attr
*object_mode_attr
;
1324 if (!object_mode_attr
)
1325 object_mode_attr
= git_attr("builtin_objectmode");
1327 if (attr
== object_mode_attr
)
1328 return builtin_object_mode_attr(istate
, path
);
1332 void git_check_attr(struct index_state
*istate
,
1334 struct attr_check
*check
)
1337 const struct object_id
*tree_oid
= default_attr_source();
1339 collect_some_attrs(istate
, tree_oid
, path
, check
);
1341 for (i
= 0; i
< check
->nr
; i
++) {
1342 unsigned int n
= check
->items
[i
].attr
->attr_nr
;
1343 const char *value
= check
->all_attrs
[n
].value
;
1344 if (value
== ATTR__UNKNOWN
)
1345 value
= compute_builtin_attr(istate
, path
, check
->all_attrs
[n
].attr
);
1346 check
->items
[i
].value
= value
;
1350 void git_all_attrs(struct index_state
*istate
,
1351 const char *path
, struct attr_check
*check
)
1354 const struct object_id
*tree_oid
= default_attr_source();
1356 attr_check_reset(check
);
1357 collect_some_attrs(istate
, tree_oid
, path
, check
);
1359 for (i
= 0; i
< check
->all_attrs_nr
; i
++) {
1360 const char *name
= check
->all_attrs
[i
].attr
->name
;
1361 const char *value
= check
->all_attrs
[i
].value
;
1362 struct attr_check_item
*item
;
1363 if (value
== ATTR__UNSET
|| value
== ATTR__UNKNOWN
)
1365 item
= attr_check_append(check
, git_attr(name
));
1366 item
->value
= value
;
1370 void attr_start(void)
1372 pthread_mutex_init(&g_attr_hashmap
.mutex
, NULL
);
1373 pthread_mutex_init(&check_vector
.mutex
, NULL
);