2 #include "repository.h"
11 static void attr_file_free(git_attr_file
*file
)
13 bool unlock
= !git_mutex_lock(&file
->lock
);
14 git_attr_file__clear_rules(file
, false);
15 git_pool_clear(&file
->pool
);
17 git_mutex_unlock(&file
->lock
);
18 git_mutex_free(&file
->lock
);
20 git__memzero(file
, sizeof(*file
));
24 int git_attr_file__new(
26 git_attr_file_entry
*entry
,
27 git_attr_file_source source
)
29 git_attr_file
*attrs
= git__calloc(1, sizeof(git_attr_file
));
30 GITERR_CHECK_ALLOC(attrs
);
32 if (git_mutex_init(&attrs
->lock
) < 0) {
33 giterr_set(GITERR_OS
, "Failed to initialize lock");
38 git_pool_init(&attrs
->pool
, 1);
39 GIT_REFCOUNT_INC(attrs
);
41 attrs
->source
= source
;
46 int git_attr_file__clear_rules(git_attr_file
*file
, bool need_lock
)
51 if (need_lock
&& git_mutex_lock(&file
->lock
) < 0) {
52 giterr_set(GITERR_OS
, "Failed to lock attribute file");
56 git_vector_foreach(&file
->rules
, i
, rule
)
57 git_attr_rule__free(rule
);
58 git_vector_free(&file
->rules
);
61 git_mutex_unlock(&file
->lock
);
66 void git_attr_file__free(git_attr_file
*file
)
70 GIT_REFCOUNT_DEC(file
, attr_file_free
);
73 static int attr_file_oid_from_index(
74 git_oid
*oid
, git_repository
*repo
, const char *path
)
79 const git_index_entry
*entry
;
81 if ((error
= git_repository_index__weakptr(&idx
, repo
)) < 0 ||
82 (error
= git_index__find_pos(&pos
, idx
, path
, 0, 0)) < 0)
85 if (!(entry
= git_index_get_byindex(idx
, pos
)))
92 int git_attr_file__load(
95 git_attr_session
*attr_session
,
96 git_attr_file_entry
*entry
,
97 git_attr_file_source source
,
98 git_attr_file_parser parser
)
101 git_blob
*blob
= NULL
;
102 git_buf content
= GIT_BUF_INIT
;
105 bool nonexistent
= false;
110 case GIT_ATTR_FILE__IN_MEMORY
:
111 /* in-memory attribute file doesn't need data */
113 case GIT_ATTR_FILE__FROM_INDEX
: {
116 if ((error
= attr_file_oid_from_index(&id
, repo
, entry
->path
)) < 0 ||
117 (error
= git_blob_lookup(&blob
, repo
, &id
)) < 0)
120 /* Do not assume that data straight from the ODB is NULL-terminated;
121 * copy the contents of a file to a buffer to work on */
122 git_buf_put(&content
, git_blob_rawcontent(blob
), git_blob_rawsize(blob
));
125 case GIT_ATTR_FILE__FROM_FILE
: {
128 /* For open or read errors, pretend that we got ENOTFOUND. */
129 /* TODO: issue warning when warning API is available */
131 if (p_stat(entry
->fullpath
, &st
) < 0 ||
132 S_ISDIR(st
.st_mode
) ||
133 (fd
= git_futils_open_ro(entry
->fullpath
)) < 0 ||
134 (error
= git_futils_readbuffer_fd(&content
, fd
, (size_t)st
.st_size
)) < 0)
143 giterr_set(GITERR_INVALID
, "Unknown file source %d", source
);
147 if ((error
= git_attr_file__new(&file
, entry
, source
)) < 0)
150 /* store the key of the attr_reader; don't bother with cache
151 * invalidation during the same attr reader session.
154 file
->session_key
= attr_session
->key
;
156 if (parser
&& (error
= parser(repo
, file
, git_buf_cstr(&content
))) < 0) {
157 git_attr_file__free(file
);
161 /* write cache breakers */
163 file
->nonexistent
= 1;
164 else if (source
== GIT_ATTR_FILE__FROM_INDEX
)
165 git_oid_cpy(&file
->cache_data
.oid
, git_blob_id(blob
));
166 else if (source
== GIT_ATTR_FILE__FROM_FILE
)
167 git_futils_filestamp_set_from_stat(&file
->cache_data
.stamp
, &st
);
168 /* else always cacheable */
174 git_buf_free(&content
);
179 int git_attr_file__out_of_date(
180 git_repository
*repo
,
181 git_attr_session
*attr_session
,
187 /* we are never out of date if we just created this data in the same
188 * attr_session; otherwise, nonexistent files must be invalidated
190 if (attr_session
&& attr_session
->key
== file
->session_key
)
192 else if (file
->nonexistent
)
195 switch (file
->source
) {
196 case GIT_ATTR_FILE__IN_MEMORY
:
199 case GIT_ATTR_FILE__FROM_FILE
:
200 return git_futils_filestamp_check(
201 &file
->cache_data
.stamp
, file
->entry
->fullpath
);
203 case GIT_ATTR_FILE__FROM_INDEX
: {
207 if ((error
= attr_file_oid_from_index(
208 &id
, repo
, file
->entry
->path
)) < 0)
211 return (git_oid__cmp(&file
->cache_data
.oid
, &id
) != 0);
215 giterr_set(GITERR_INVALID
, "Invalid file type %d", file
->source
);
220 static int sort_by_hash_and_name(const void *a_raw
, const void *b_raw
);
221 static void git_attr_rule__clear(git_attr_rule
*rule
);
222 static bool parse_optimized_patterns(
223 git_attr_fnmatch
*spec
,
225 const char *pattern
);
227 int git_attr_file__parse_buffer(
228 git_repository
*repo
, git_attr_file
*attrs
, const char *data
)
231 const char *scan
= data
, *context
= NULL
;
232 git_attr_rule
*rule
= NULL
;
234 /* if subdir file path, convert context for file paths */
236 git_path_root(attrs
->entry
->path
) < 0 &&
237 !git__suffixcmp(attrs
->entry
->path
, "/" GIT_ATTR_FILE
))
238 context
= attrs
->entry
->path
;
240 if (git_mutex_lock(&attrs
->lock
) < 0) {
241 giterr_set(GITERR_OS
, "Failed to lock attribute file");
245 while (!error
&& *scan
) {
246 /* allocate rule if needed */
247 if (!rule
&& !(rule
= git__calloc(1, sizeof(*rule
)))) {
253 GIT_ATTR_FNMATCH_ALLOWNEG
| GIT_ATTR_FNMATCH_ALLOWMACRO
;
255 /* parse the next "pattern attr attr attr" line */
256 if (!(error
= git_attr_fnmatch__parse(
257 &rule
->match
, &attrs
->pool
, context
, &scan
)) &&
258 !(error
= git_attr_assignment__parse(
259 repo
, &attrs
->pool
, &rule
->assigns
, &scan
)))
261 if (rule
->match
.flags
& GIT_ATTR_FNMATCH_MACRO
)
262 /* TODO: warning if macro found in file below repo root */
263 error
= git_attr_cache__insert_macro(repo
, rule
);
265 error
= git_vector_insert(&attrs
->rules
, rule
);
268 /* if the rule wasn't a pattern, on to the next */
270 git_attr_rule__clear(rule
); /* reset rule contents */
271 if (error
== GIT_ENOTFOUND
)
274 rule
= NULL
; /* vector now "owns" the rule */
278 git_mutex_unlock(&attrs
->lock
);
279 git_attr_rule__free(rule
);
284 uint32_t git_attr_file__name_hash(const char *name
)
289 while ((c
= (int)*name
++) != 0)
290 h
= ((h
<< 5) + h
) + c
;
294 int git_attr_file__lookup_one(
307 name
.name_hash
= git_attr_file__name_hash(attr
);
309 git_attr_file__foreach_matching_rule(file
, path
, i
, rule
) {
312 if (!git_vector_bsearch(&pos
, &rule
->assigns
, &name
)) {
313 *value
= ((git_attr_assignment
*)
314 git_vector_get(&rule
->assigns
, pos
))->value
;
322 int git_attr_file__load_standalone(git_attr_file
**out
, const char *path
)
326 git_buf content
= GIT_BUF_INIT
;
328 error
= git_attr_file__new(&file
, NULL
, GIT_ATTR_FILE__FROM_FILE
);
332 error
= git_attr_cache__alloc_file_entry(
333 &file
->entry
, NULL
, path
, &file
->pool
);
335 git_attr_file__free(file
);
338 /* because the cache entry is allocated from the file's own pool, we
339 * don't have to free it - freeing file+pool will free cache entry, too.
342 if (!(error
= git_futils_readbuffer(&content
, path
))) {
343 error
= git_attr_file__parse_buffer(NULL
, file
, content
.ptr
);
344 git_buf_free(&content
);
348 git_attr_file__free(file
);
355 bool git_attr_fnmatch__match(
356 git_attr_fnmatch
*match
,
359 const char *relpath
= path
->path
;
360 const char *filename
;
364 * If the rule was generated in a subdirectory, we must only
365 * use it for paths inside that directory. We can thus return
366 * a non-match if the prefixes don't match.
368 if (match
->containing_dir
) {
369 if (match
->flags
& GIT_ATTR_FNMATCH_ICASE
) {
370 if (git__strncasecmp(path
->path
, match
->containing_dir
, match
->containing_dir_length
))
373 if (git__prefixcmp(path
->path
, match
->containing_dir
))
377 relpath
+= match
->containing_dir_length
;
380 if (match
->flags
& GIT_ATTR_FNMATCH_ICASE
)
381 flags
|= FNM_CASEFOLD
;
382 if (match
->flags
& GIT_ATTR_FNMATCH_LEADINGDIR
)
383 flags
|= FNM_LEADING_DIR
;
385 if (match
->flags
& GIT_ATTR_FNMATCH_FULLPATH
) {
387 flags
|= FNM_PATHNAME
;
389 filename
= path
->basename
;
392 flags
|= FNM_LEADING_DIR
;
395 if ((match
->flags
& GIT_ATTR_FNMATCH_DIRECTORY
) && !path
->is_dir
) {
398 /* for attribute checks or root ignore checks, fail match */
399 if (!(match
->flags
& GIT_ATTR_FNMATCH_IGNORE
) ||
400 path
->basename
== path
->path
)
403 flags
|= FNM_LEADING_DIR
;
405 /* fail match if this is a file with same name as ignored folder */
406 samename
= (match
->flags
& GIT_ATTR_FNMATCH_ICASE
) ?
407 !strcasecmp(match
->pattern
, relpath
) :
408 !strcmp(match
->pattern
, relpath
);
413 return (p_fnmatch(match
->pattern
, relpath
, flags
) != FNM_NOMATCH
);
416 /* if path is a directory prefix of a negated pattern, then match */
417 if ((match
->flags
& GIT_ATTR_FNMATCH_NEGATIVE
) && path
->is_dir
) {
418 size_t pathlen
= strlen(relpath
);
419 bool prefixed
= (pathlen
<= match
->length
) &&
420 ((match
->flags
& GIT_ATTR_FNMATCH_ICASE
) ?
421 !strncasecmp(match
->pattern
, relpath
, pathlen
) :
422 !strncmp(match
->pattern
, relpath
, pathlen
));
424 if (prefixed
&& git_path_at_end_of_segment(&match
->pattern
[pathlen
]))
428 return (p_fnmatch(match
->pattern
, filename
, flags
) != FNM_NOMATCH
);
431 bool git_attr_rule__match(
435 bool matched
= git_attr_fnmatch__match(&rule
->match
, path
);
437 if (rule
->match
.flags
& GIT_ATTR_FNMATCH_NEGATIVE
)
443 git_attr_assignment
*git_attr_rule__lookup_assignment(
444 git_attr_rule
*rule
, const char *name
)
449 key
.name_hash
= git_attr_file__name_hash(name
);
451 if (git_vector_bsearch(&pos
, &rule
->assigns
, &key
))
454 return git_vector_get(&rule
->assigns
, pos
);
457 int git_attr_path__init(
458 git_attr_path
*info
, const char *path
, const char *base
, git_dir_flag dir_flag
)
462 /* build full path as best we can */
463 git_buf_init(&info
->full
, 0);
465 if (git_path_join_unrooted(&info
->full
, path
, base
, &root
) < 0)
468 info
->path
= info
->full
.ptr
+ root
;
470 /* remove trailing slashes */
471 while (info
->full
.size
> 0) {
472 if (info
->full
.ptr
[info
->full
.size
- 1] != '/')
476 info
->full
.ptr
[info
->full
.size
] = '\0';
478 /* skip leading slashes in path */
479 while (*info
->path
== '/')
482 /* find trailing basename component */
483 info
->basename
= strrchr(info
->path
, '/');
486 if (!info
->basename
|| !*info
->basename
)
487 info
->basename
= info
->path
;
491 case GIT_DIR_FLAG_FALSE
:
495 case GIT_DIR_FLAG_TRUE
:
499 case GIT_DIR_FLAG_UNKNOWN
:
501 info
->is_dir
= (int)git_path_isdir(info
->full
.ptr
);
508 void git_attr_path__free(git_attr_path
*info
)
510 git_buf_free(&info
->full
);
512 info
->basename
= NULL
;
516 * From gitattributes(5):
518 * Patterns have the following format:
520 * - A blank line matches no files, so it can serve as a separator for
523 * - A line starting with # serves as a comment.
525 * - An optional prefix ! which negates the pattern; any matching file
526 * excluded by a previous pattern will become included again. If a negated
527 * pattern matches, this will override lower precedence patterns sources.
529 * - If the pattern ends with a slash, it is removed for the purpose of the
530 * following description, but it would only find a match with a directory. In
531 * other words, foo/ will match a directory foo and paths underneath it, but
532 * will not match a regular file or a symbolic link foo (this is consistent
533 * with the way how pathspec works in general in git).
535 * - If the pattern does not contain a slash /, git treats it as a shell glob
536 * pattern and checks for a match against the pathname without leading
539 * - Otherwise, git treats the pattern as a shell glob suitable for consumption
540 * by fnmatch(3) with the FNM_PATHNAME flag: wildcards in the pattern will
541 * not match a / in the pathname. For example, "Documentation/\*.html" matches
542 * "Documentation/git.html" but not "Documentation/ppc/ppc.html". A leading
543 * slash matches the beginning of the pathname; for example, "/\*.c" matches
544 * "cat-file.c" but not "mozilla-sha1/sha1.c".
548 * This will return 0 if the spec was filled out,
549 * GIT_ENOTFOUND if the fnmatch does not require matching, or
550 * another error code there was an actual problem.
552 int git_attr_fnmatch__parse(
553 git_attr_fnmatch
*spec
,
558 const char *pattern
, *scan
;
559 int slash_count
, allow_space
;
561 assert(spec
&& base
&& *base
);
563 if (parse_optimized_patterns(spec
, pool
, *base
))
566 spec
->flags
= (spec
->flags
& GIT_ATTR_FNMATCH__INCOMING
);
567 allow_space
= ((spec
->flags
& GIT_ATTR_FNMATCH_ALLOWSPACE
) != 0);
571 while (git__isspace(*pattern
)) pattern
++;
572 if (!*pattern
|| *pattern
== '#') {
573 *base
= git__next_line(pattern
);
574 return GIT_ENOTFOUND
;
577 if (*pattern
== '[' && (spec
->flags
& GIT_ATTR_FNMATCH_ALLOWMACRO
) != 0) {
578 if (strncmp(pattern
, "[attr]", 6) == 0) {
579 spec
->flags
= spec
->flags
| GIT_ATTR_FNMATCH_MACRO
;
582 /* else a character range like [a-e]* which is accepted */
585 if (*pattern
== '!' && (spec
->flags
& GIT_ATTR_FNMATCH_ALLOWNEG
) != 0) {
586 spec
->flags
= spec
->flags
|
587 GIT_ATTR_FNMATCH_NEGATIVE
| GIT_ATTR_FNMATCH_LEADINGDIR
;
592 for (scan
= pattern
; *scan
!= '\0'; ++scan
) {
593 /* scan until (non-escaped) white space */
594 if (git__isspace(*scan
) && *(scan
- 1) != '\\') {
595 if (!allow_space
|| (*scan
!= ' ' && *scan
!= '\t' && *scan
!= '\r'))
600 spec
->flags
= spec
->flags
| GIT_ATTR_FNMATCH_FULLPATH
;
605 /* remember if we see an unescaped wildcard in pattern */
606 else if (git__iswildcard(*scan
) &&
607 (scan
== pattern
|| (*(scan
- 1) != '\\')))
608 spec
->flags
= spec
->flags
| GIT_ATTR_FNMATCH_HASWILD
;
613 if ((spec
->length
= scan
- pattern
) == 0)
614 return GIT_ENOTFOUND
;
617 * Remove one trailing \r in case this is a CRLF delimited
618 * file, in the case of Icon\r\r\n, we still leave the first
619 * \r there to match against.
621 if (pattern
[spec
->length
- 1] == '\r')
622 if (--spec
->length
== 0)
623 return GIT_ENOTFOUND
;
625 if (pattern
[spec
->length
- 1] == '/') {
627 spec
->flags
= spec
->flags
| GIT_ATTR_FNMATCH_DIRECTORY
;
628 if (--slash_count
<= 0)
629 spec
->flags
= spec
->flags
& ~GIT_ATTR_FNMATCH_FULLPATH
;
631 if ((spec
->flags
& GIT_ATTR_FNMATCH_NOLEADINGDIR
) == 0 &&
633 pattern
[spec
->length
- 1] == '*' &&
634 pattern
[spec
->length
- 2] == '/') {
636 spec
->flags
= spec
->flags
| GIT_ATTR_FNMATCH_LEADINGDIR
;
637 /* leave FULLPATH match on, however */
641 char *slash
= strrchr(context
, '/');
644 /* include the slash for easier matching */
645 len
= slash
- context
+ 1;
646 spec
->containing_dir
= git_pool_strndup(pool
, context
, len
);
647 spec
->containing_dir_length
= len
;
651 spec
->pattern
= git_pool_strndup(pool
, pattern
, spec
->length
);
653 if (!spec
->pattern
) {
654 *base
= git__next_line(pattern
);
657 /* strip '\' that might have be used for internal whitespace */
658 spec
->length
= git__unescape(spec
->pattern
);
659 /* TODO: convert remaining '\' into '/' for POSIX ??? */
665 static bool parse_optimized_patterns(
666 git_attr_fnmatch
*spec
,
670 if (!pattern
[1] && (pattern
[0] == '*' || pattern
[0] == '.')) {
671 spec
->flags
= GIT_ATTR_FNMATCH_MATCH_ALL
;
672 spec
->pattern
= git_pool_strndup(pool
, pattern
, 1);
681 static int sort_by_hash_and_name(const void *a_raw
, const void *b_raw
)
683 const git_attr_name
*a
= a_raw
;
684 const git_attr_name
*b
= b_raw
;
686 if (b
->name_hash
< a
->name_hash
)
688 else if (b
->name_hash
> a
->name_hash
)
691 return strcmp(b
->name
, a
->name
);
694 static void git_attr_assignment__free(git_attr_assignment
*assign
)
696 /* name and value are stored in a git_pool associated with the
697 * git_attr_file, so they do not need to be freed here
700 assign
->value
= NULL
;
704 static int merge_assignments(void **old_raw
, void *new_raw
)
706 git_attr_assignment
**old
= (git_attr_assignment
**)old_raw
;
707 git_attr_assignment
*new = (git_attr_assignment
*)new_raw
;
709 GIT_REFCOUNT_DEC(*old
, git_attr_assignment__free
);
714 int git_attr_assignment__parse(
715 git_repository
*repo
,
721 const char *scan
= *base
;
722 git_attr_assignment
*assign
= NULL
;
724 assert(assigns
&& !assigns
->length
);
726 git_vector_set_cmp(assigns
, sort_by_hash_and_name
);
728 while (*scan
&& *scan
!= '\n') {
729 const char *name_start
, *value_start
;
731 /* skip leading blanks */
732 while (git__isspace(*scan
) && *scan
!= '\n') scan
++;
734 /* allocate assign if needed */
736 assign
= git__calloc(1, sizeof(git_attr_assignment
));
737 GITERR_CHECK_ALLOC(assign
);
738 GIT_REFCOUNT_INC(assign
);
741 assign
->name_hash
= 5381;
742 assign
->value
= git_attr__true
;
744 /* look for magic name prefixes */
746 assign
->value
= git_attr__false
;
748 } else if (*scan
== '!') {
749 assign
->value
= git_attr__unset
; /* explicit unspecified state */
751 } else if (*scan
== '#') /* comment rest of line */
756 while (*scan
&& !git__isspace(*scan
) && *scan
!= '=') {
758 ((assign
->name_hash
<< 5) + assign
->name_hash
) + *scan
;
761 if (scan
== name_start
) {
762 /* must have found lone prefix (" - ") or leading = ("=foo")
763 * or end of buffer -- advance until whitespace and continue
765 while (*scan
&& !git__isspace(*scan
)) scan
++;
769 /* allocate permanent storage for name */
770 assign
->name
= git_pool_strndup(pool
, name_start
, scan
- name_start
);
771 GITERR_CHECK_ALLOC(assign
->name
);
773 /* if there is an equals sign, find the value */
775 for (value_start
= ++scan
; *scan
&& !git__isspace(*scan
); ++scan
);
777 /* if we found a value, allocate permanent storage for it */
778 if (scan
> value_start
) {
779 assign
->value
= git_pool_strndup(pool
, value_start
, scan
- value_start
);
780 GITERR_CHECK_ALLOC(assign
->value
);
784 /* expand macros (if given a repo with a macro cache) */
785 if (repo
!= NULL
&& assign
->value
== git_attr__true
) {
786 git_attr_rule
*macro
=
787 git_attr_cache__lookup_macro(repo
, assign
->name
);
791 git_attr_assignment
*massign
;
793 git_vector_foreach(¯o
->assigns
, i
, massign
) {
794 GIT_REFCOUNT_INC(massign
);
796 error
= git_vector_insert_sorted(
797 assigns
, massign
, &merge_assignments
);
798 if (error
< 0 && error
!= GIT_EEXISTS
) {
799 git_attr_assignment__free(assign
);
806 /* insert allocated assign into vector */
807 error
= git_vector_insert_sorted(assigns
, assign
, &merge_assignments
);
808 if (error
< 0 && error
!= GIT_EEXISTS
)
811 /* clear assign since it is now "owned" by the vector */
816 git_attr_assignment__free(assign
);
818 *base
= git__next_line(scan
);
820 return (assigns
->length
== 0) ? GIT_ENOTFOUND
: 0;
823 static void git_attr_rule__clear(git_attr_rule
*rule
)
826 git_attr_assignment
*assign
;
831 if (!(rule
->match
.flags
& GIT_ATTR_FNMATCH_IGNORE
)) {
832 git_vector_foreach(&rule
->assigns
, i
, assign
)
833 GIT_REFCOUNT_DEC(assign
, git_attr_assignment__free
);
834 git_vector_free(&rule
->assigns
);
837 /* match.pattern is stored in a git_pool, so no need to free */
838 rule
->match
.pattern
= NULL
;
839 rule
->match
.length
= 0;
842 void git_attr_rule__free(git_attr_rule
*rule
)
844 git_attr_rule__clear(rule
);
848 int git_attr_session__init(git_attr_session
*session
, git_repository
*repo
)
852 session
->key
= git_atomic_inc(&repo
->attr_session_key
);
857 void git_attr_session__free(git_attr_session
*session
)
862 git_buf_free(&session
->sysdir
);
863 git_buf_free(&session
->tmp
);
865 memset(session
, 0, sizeof(git_attr_session
));