1 #define NO_THE_INDEX_COMPATIBILITY_MACROS
6 const char git_attr__true
[] = "(builtin)true";
7 const char git_attr__false
[] = "\0(builtin)false";
8 static const char git_attr__unknown
[] = "(builtin)unknown";
9 #define ATTR__TRUE git_attr__true
10 #define ATTR__FALSE git_attr__false
11 #define ATTR__UNSET NULL
12 #define ATTR__UNKNOWN git_attr__unknown
14 static const char *attributes_file
;
17 * The basic design decision here is that we are not going to have
18 * insanely large number of attributes.
20 * This is a randomly chosen prime.
29 struct git_attr
*next
;
32 char name
[FLEX_ARRAY
];
36 static struct git_attr_check
*check_all_attr
;
37 static struct git_attr
*(git_attr_hash
[HASHSIZE
]);
39 static unsigned hash_name(const char *name
, int namelen
)
45 val
= ((val
<< 7) | (val
>> 22)) ^ c
;
50 static int invalid_attr_name(const char *name
, int namelen
)
53 * Attribute name cannot begin with '-' and must consist of
54 * characters from [-A-Za-z0-9_.].
60 if (! (ch
== '-' || ch
== '.' || ch
== '_' ||
61 ('0' <= ch
&& ch
<= '9') ||
62 ('a' <= ch
&& ch
<= 'z') ||
63 ('A' <= ch
&& ch
<= 'Z')) )
69 static struct git_attr
*git_attr_internal(const char *name
, int len
)
71 unsigned hval
= hash_name(name
, len
);
72 unsigned pos
= hval
% HASHSIZE
;
75 for (a
= git_attr_hash
[pos
]; a
; a
= a
->next
) {
77 !memcmp(a
->name
, name
, len
) && !a
->name
[len
])
81 if (invalid_attr_name(name
, len
))
84 a
= xmalloc(sizeof(*a
) + len
+ 1);
85 memcpy(a
->name
, name
, len
);
88 a
->next
= git_attr_hash
[pos
];
89 a
->attr_nr
= attr_nr
++;
90 git_attr_hash
[pos
] = a
;
92 check_all_attr
= xrealloc(check_all_attr
,
93 sizeof(*check_all_attr
) * attr_nr
);
94 check_all_attr
[a
->attr_nr
].attr
= a
;
95 check_all_attr
[a
->attr_nr
].value
= ATTR__UNKNOWN
;
99 struct git_attr
*git_attr(const char *name
)
101 return git_attr_internal(name
, strlen(name
));
105 * .gitattributes file is one line per record, each of which is
109 * (3) whitespace separated list of attribute names, each of which
110 * could be prefixed with '-' to mean "set to false", '!' to mean
114 /* What does a matched pattern decide? */
116 struct git_attr
*attr
;
123 struct git_attr
*attr
;
127 struct attr_state state
[FLEX_ARRAY
];
130 static const char blank
[] = " \t\r\n";
132 static const char *parse_attr(const char *src
, int lineno
, const char *cp
,
133 int *num_attr
, struct match_attr
*res
)
135 const char *ep
, *equals
;
138 ep
= cp
+ strcspn(cp
, blank
);
139 equals
= strchr(cp
, '=');
140 if (equals
&& ep
< equals
)
147 if (*cp
== '-' || *cp
== '!') {
151 if (invalid_attr_name(cp
, len
)) {
153 "%.*s is not a valid attribute name: %s:%d\n",
154 len
, cp
, src
, lineno
);
158 struct attr_state
*e
;
160 e
= &(res
->state
[*num_attr
]);
161 if (*cp
== '-' || *cp
== '!') {
162 e
->setto
= (*cp
== '-') ? ATTR__FALSE
: ATTR__UNSET
;
167 e
->setto
= ATTR__TRUE
;
169 e
->setto
= xmemdupz(equals
+ 1, ep
- equals
- 1);
171 e
->attr
= git_attr_internal(cp
, len
);
174 return ep
+ strspn(ep
, blank
);
177 static struct match_attr
*parse_attr_line(const char *line
, const char *src
,
178 int lineno
, int macro_ok
)
182 const char *cp
, *name
;
183 struct match_attr
*res
= NULL
;
187 cp
= line
+ strspn(line
, blank
);
188 if (!*cp
|| *cp
== '#')
191 namelen
= strcspn(name
, blank
);
192 if (strlen(ATTRIBUTE_MACRO_PREFIX
) < namelen
&&
193 !prefixcmp(name
, ATTRIBUTE_MACRO_PREFIX
)) {
195 fprintf(stderr
, "%s not allowed: %s:%d\n",
200 name
+= strlen(ATTRIBUTE_MACRO_PREFIX
);
201 name
+= strspn(name
, blank
);
202 namelen
= strcspn(name
, blank
);
203 if (invalid_attr_name(name
, namelen
)) {
205 "%.*s is not a valid attribute name: %s:%d\n",
206 namelen
, name
, src
, lineno
);
213 for (pass
= 0; pass
< 2; pass
++) {
214 /* pass 0 counts and allocates, pass 1 fills */
217 cp
= cp
+ strspn(cp
, blank
);
219 cp
= parse_attr(src
, lineno
, cp
, &num_attr
, res
);
227 sizeof(struct attr_state
) * num_attr
+
228 (is_macro
? 0 : namelen
+ 1));
230 res
->u
.attr
= git_attr_internal(name
, namelen
);
232 res
->u
.pattern
= (char *)&(res
->state
[num_attr
]);
233 memcpy(res
->u
.pattern
, name
, namelen
);
234 res
->u
.pattern
[namelen
] = 0;
236 res
->is_macro
= is_macro
;
237 res
->num_attr
= num_attr
;
243 * Like info/exclude and .gitignore, the attribute information can
244 * come from many places.
246 * (1) .gitattribute file of the same directory;
247 * (2) .gitattribute file of the parent directory if (1) does not have
248 * any match; this goes recursively upwards, just like .gitignore.
249 * (3) $GIT_DIR/info/attributes, which overrides both of the above.
251 * In the same file, later entries override the earlier match, so in the
252 * global list, we would have entries from info/attributes the earliest
253 * (reading the file from top to bottom), .gitattribute of the root
254 * directory (again, reading the file from top to bottom) down to the
255 * current directory, and then scan the list backwards to find the first match.
256 * This is exactly the same as what excluded() does in dir.c to deal with
260 static struct attr_stack
{
261 struct attr_stack
*prev
;
263 unsigned num_matches
;
265 struct match_attr
**attrs
;
268 static void free_attr_elem(struct attr_stack
*e
)
272 for (i
= 0; i
< e
->num_matches
; i
++) {
273 struct match_attr
*a
= e
->attrs
[i
];
275 for (j
= 0; j
< a
->num_attr
; j
++) {
276 const char *setto
= a
->state
[j
].setto
;
277 if (setto
== ATTR__TRUE
||
278 setto
== ATTR__FALSE
||
279 setto
== ATTR__UNSET
||
280 setto
== ATTR__UNKNOWN
)
283 free((char *) setto
);
290 static const char *builtin_attr
[] = {
291 "[attr]binary -diff -text",
295 static void handle_attr_line(struct attr_stack
*res
,
301 struct match_attr
*a
;
303 a
= parse_attr_line(line
, src
, lineno
, macro_ok
);
306 if (res
->alloc
<= res
->num_matches
) {
307 res
->alloc
= alloc_nr(res
->num_matches
);
308 res
->attrs
= xrealloc(res
->attrs
,
309 sizeof(struct match_attr
*) *
312 res
->attrs
[res
->num_matches
++] = a
;
315 static struct attr_stack
*read_attr_from_array(const char **list
)
317 struct attr_stack
*res
;
321 res
= xcalloc(1, sizeof(*res
));
322 while ((line
= *(list
++)) != NULL
)
323 handle_attr_line(res
, line
, "[builtin]", ++lineno
, 1);
327 static enum git_attr_direction direction
;
328 static struct index_state
*use_index
;
330 static struct attr_stack
*read_attr_from_file(const char *path
, int macro_ok
)
332 FILE *fp
= fopen(path
, "r");
333 struct attr_stack
*res
;
339 res
= xcalloc(1, sizeof(*res
));
340 while (fgets(buf
, sizeof(buf
), fp
))
341 handle_attr_line(res
, buf
, path
, ++lineno
, macro_ok
);
346 static void *read_index_data(const char *path
)
350 enum object_type type
;
352 struct index_state
*istate
= use_index
? use_index
: &the_index
;
355 pos
= index_name_pos(istate
, path
, len
);
358 * We might be in the middle of a merge, in which
359 * case we would read stage #2 (ours).
363 (pos
< 0 && i
< istate
->cache_nr
&&
364 !strcmp(istate
->cache
[i
]->name
, path
));
366 if (ce_stage(istate
->cache
[i
]) == 2)
371 data
= read_sha1_file(istate
->cache
[pos
]->sha1
, &type
, &sz
);
372 if (!data
|| type
!= OBJ_BLOB
) {
379 static struct attr_stack
*read_attr_from_index(const char *path
, int macro_ok
)
381 struct attr_stack
*res
;
385 buf
= read_index_data(path
);
389 res
= xcalloc(1, sizeof(*res
));
390 for (sp
= buf
; *sp
; ) {
393 for (ep
= sp
; *ep
&& *ep
!= '\n'; ep
++)
395 more
= (*ep
== '\n');
397 handle_attr_line(res
, sp
, path
, ++lineno
, macro_ok
);
404 static struct attr_stack
*read_attr(const char *path
, int macro_ok
)
406 struct attr_stack
*res
;
408 if (direction
== GIT_ATTR_CHECKOUT
) {
409 res
= read_attr_from_index(path
, macro_ok
);
411 res
= read_attr_from_file(path
, macro_ok
);
413 else if (direction
== GIT_ATTR_CHECKIN
) {
414 res
= read_attr_from_file(path
, macro_ok
);
417 * There is no checked out .gitattributes file there, but
418 * we might have it in the index. We allow operation in a
419 * sparsely checked out work tree, so read from it.
421 res
= read_attr_from_index(path
, macro_ok
);
424 res
= read_attr_from_index(path
, macro_ok
);
426 res
= xcalloc(1, sizeof(*res
));
431 static void debug_info(const char *what
, struct attr_stack
*elem
)
433 fprintf(stderr
, "%s: %s\n", what
, elem
->origin
? elem
->origin
: "()");
435 static void debug_set(const char *what
, const char *match
, struct git_attr
*attr
, const void *v
)
437 const char *value
= v
;
439 if (ATTR_TRUE(value
))
441 else if (ATTR_FALSE(value
))
443 else if (ATTR_UNSET(value
))
444 value
= "unspecified";
446 fprintf(stderr
, "%s: %s => %s (%s)\n",
447 what
, attr
->name
, (char *) value
, match
);
449 #define debug_push(a) debug_info("push", (a))
450 #define debug_pop(a) debug_info("pop", (a))
452 #define debug_push(a) do { ; } while (0)
453 #define debug_pop(a) do { ; } while (0)
454 #define debug_set(a,b,c,d) do { ; } while (0)
457 static void drop_attr_stack(void)
460 struct attr_stack
*elem
= attr_stack
;
461 attr_stack
= elem
->prev
;
462 free_attr_elem(elem
);
466 static const char *git_etc_gitattributes(void)
468 static const char *system_wide
;
470 system_wide
= system_path(ETC_GITATTRIBUTES
);
474 static int git_attr_system(void)
476 return !git_env_bool("GIT_ATTR_NOSYSTEM", 0);
479 static int git_attr_config(const char *var
, const char *value
, void *dummy
)
481 if (!strcmp(var
, "core.attributesfile"))
482 return git_config_pathname(&attributes_file
, var
, value
);
487 static void bootstrap_attr_stack(void)
490 struct attr_stack
*elem
;
492 elem
= read_attr_from_array(builtin_attr
);
494 elem
->prev
= attr_stack
;
497 if (git_attr_system()) {
498 elem
= read_attr_from_file(git_etc_gitattributes(), 1);
501 elem
->prev
= attr_stack
;
506 git_config(git_attr_config
, NULL
);
507 if (attributes_file
) {
508 elem
= read_attr_from_file(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
= strdup("");
519 elem
->prev
= attr_stack
;
524 elem
= read_attr_from_file(git_path(INFOATTRIBUTES_FILE
), 1);
526 elem
= xcalloc(1, sizeof(*elem
));
528 elem
->prev
= attr_stack
;
533 static void prepare_attr_stack(const char *path
, int dirlen
)
535 struct attr_stack
*elem
, *info
;
537 struct strbuf pathbuf
;
539 strbuf_init(&pathbuf
, dirlen
+2+strlen(GITATTRIBUTES_FILE
));
542 * At the bottom of the attribute stack is the built-in
543 * set of attribute definitions, followed by the contents
544 * of $(prefix)/etc/gitattributes and a file specified by
545 * core.attributesfile. Then, contents from
546 * .gitattribute files from directories closer to the
547 * root to the ones in deeper directories are pushed
548 * to the stack. Finally, at the very top of the stack
549 * we always keep the contents of $GIT_DIR/info/attributes.
551 * When checking, we use entries from near the top of the
552 * stack, preferring $GIT_DIR/info/attributes, then
553 * .gitattributes in deeper directories to shallower ones,
554 * and finally use the built-in set as the default.
557 bootstrap_attr_stack();
560 * Pop the "info" one that is always at the top of the stack.
563 attr_stack
= info
->prev
;
566 * Pop the ones from directories that are not the prefix of
567 * the path we are checking.
569 while (attr_stack
&& attr_stack
->origin
) {
570 int namelen
= strlen(attr_stack
->origin
);
573 if (namelen
<= dirlen
&&
574 !strncmp(elem
->origin
, 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
) {
589 len
= strlen(attr_stack
->origin
);
592 strbuf_reset(&pathbuf
);
593 strbuf_add(&pathbuf
, path
, dirlen
);
594 strbuf_addch(&pathbuf
, '/');
595 cp
= strchr(pathbuf
.buf
+ len
+ 1, '/');
596 strcpy(cp
+ 1, GITATTRIBUTES_FILE
);
597 elem
= read_attr(pathbuf
.buf
, 0);
599 elem
->origin
= strdup(pathbuf
.buf
);
600 elem
->prev
= attr_stack
;
606 strbuf_release(&pathbuf
);
609 * Finally push the "info" one at the top of the stack.
611 info
->prev
= attr_stack
;
615 static int path_matches(const char *pathname
, int pathlen
,
617 const char *base
, int baselen
)
619 if (!strchr(pattern
, '/')) {
621 const char *basename
= strrchr(pathname
, '/');
622 basename
= basename
? basename
+ 1 : pathname
;
623 return (fnmatch(pattern
, basename
, 0) == 0);
626 * match with FNM_PATHNAME; the pattern has base implicitly
631 if (pathlen
< baselen
||
632 (baselen
&& pathname
[baselen
] != '/') ||
633 strncmp(pathname
, base
, baselen
))
637 return fnmatch(pattern
, pathname
+ baselen
, FNM_PATHNAME
) == 0;
640 static int macroexpand_one(int attr_nr
, int rem
);
642 static int fill_one(const char *what
, struct match_attr
*a
, int rem
)
644 struct git_attr_check
*check
= check_all_attr
;
647 for (i
= a
->num_attr
- 1; 0 < rem
&& 0 <= i
; i
--) {
648 struct git_attr
*attr
= a
->state
[i
].attr
;
649 const char **n
= &(check
[attr
->attr_nr
].value
);
650 const char *v
= a
->state
[i
].setto
;
652 if (*n
== ATTR__UNKNOWN
) {
654 a
->is_macro
? a
->u
.attr
->name
: a
->u
.pattern
,
658 rem
= macroexpand_one(attr
->attr_nr
, rem
);
664 static int fill(const char *path
, int pathlen
, struct attr_stack
*stk
, int rem
)
667 const char *base
= stk
->origin
? stk
->origin
: "";
669 for (i
= stk
->num_matches
- 1; 0 < rem
&& 0 <= i
; i
--) {
670 struct match_attr
*a
= stk
->attrs
[i
];
673 if (path_matches(path
, pathlen
,
674 a
->u
.pattern
, base
, strlen(base
)))
675 rem
= fill_one("fill", a
, rem
);
680 static int macroexpand_one(int attr_nr
, int rem
)
682 struct attr_stack
*stk
;
683 struct match_attr
*a
= NULL
;
686 if (check_all_attr
[attr_nr
].value
!= ATTR__TRUE
)
689 for (stk
= attr_stack
; !a
&& stk
; stk
= stk
->prev
)
690 for (i
= stk
->num_matches
- 1; !a
&& 0 <= i
; i
--) {
691 struct match_attr
*ma
= stk
->attrs
[i
];
694 if (ma
->u
.attr
->attr_nr
== attr_nr
)
699 rem
= fill_one("expand", a
, rem
);
704 int git_checkattr(const char *path
, int num
, struct git_attr_check
*check
)
706 struct attr_stack
*stk
;
708 int dirlen
, pathlen
, i
, rem
;
710 bootstrap_attr_stack();
711 for (i
= 0; i
< attr_nr
; i
++)
712 check_all_attr
[i
].value
= ATTR__UNKNOWN
;
714 pathlen
= strlen(path
);
715 cp
= strrchr(path
, '/');
720 prepare_attr_stack(path
, dirlen
);
722 for (stk
= attr_stack
; 0 < rem
&& stk
; stk
= stk
->prev
)
723 rem
= fill(path
, pathlen
, stk
, rem
);
725 for (i
= 0; i
< num
; i
++) {
726 const char *value
= check_all_attr
[check
[i
].attr
->attr_nr
].value
;
727 if (value
== ATTR__UNKNOWN
)
729 check
[i
].value
= value
;
735 void git_attr_set_direction(enum git_attr_direction
new, struct index_state
*istate
)
737 enum git_attr_direction old
= direction
;
739 if (is_bare_repository() && new != GIT_ATTR_INDEX
)
740 die("BUG: non-INDEX attr direction in a bare repo");