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 if (git_pool_init(&attrs
->pool
, 1, 0) < 0) {
39 attr_file_free(attrs
);
43 GIT_REFCOUNT_INC(attrs
);
45 attrs
->source
= source
;
50 int git_attr_file__clear_rules(git_attr_file
*file
, bool need_lock
)
55 if (need_lock
&& git_mutex_lock(&file
->lock
) < 0) {
56 giterr_set(GITERR_OS
, "Failed to lock attribute file");
60 git_vector_foreach(&file
->rules
, i
, rule
)
61 git_attr_rule__free(rule
);
62 git_vector_free(&file
->rules
);
65 git_mutex_unlock(&file
->lock
);
70 void git_attr_file__free(git_attr_file
*file
)
74 GIT_REFCOUNT_DEC(file
, attr_file_free
);
77 static int attr_file_oid_from_index(
78 git_oid
*oid
, git_repository
*repo
, const char *path
)
83 const git_index_entry
*entry
;
85 if ((error
= git_repository_index__weakptr(&idx
, repo
)) < 0 ||
86 (error
= git_index__find_pos(&pos
, idx
, path
, 0, 0)) < 0)
89 if (!(entry
= git_index_get_byindex(idx
, pos
)))
96 int git_attr_file__load(
99 git_attr_session
*attr_session
,
100 git_attr_file_entry
*entry
,
101 git_attr_file_source source
,
102 git_attr_file_parser parser
)
105 git_blob
*blob
= NULL
;
106 git_buf content
= GIT_BUF_INIT
;
109 bool nonexistent
= false;
114 case GIT_ATTR_FILE__IN_MEMORY
:
115 /* in-memory attribute file doesn't need data */
117 case GIT_ATTR_FILE__FROM_INDEX
: {
120 if ((error
= attr_file_oid_from_index(&id
, repo
, entry
->path
)) < 0 ||
121 (error
= git_blob_lookup(&blob
, repo
, &id
)) < 0)
124 /* Do not assume that data straight from the ODB is NULL-terminated;
125 * copy the contents of a file to a buffer to work on */
126 git_buf_put(&content
, git_blob_rawcontent(blob
), git_blob_rawsize(blob
));
129 case GIT_ATTR_FILE__FROM_FILE
: {
132 /* For open or read errors, pretend that we got ENOTFOUND. */
133 /* TODO: issue warning when warning API is available */
135 if (p_stat(entry
->fullpath
, &st
) < 0 ||
136 S_ISDIR(st
.st_mode
) ||
137 (fd
= git_futils_open_ro(entry
->fullpath
)) < 0 ||
138 (error
= git_futils_readbuffer_fd(&content
, fd
, (size_t)st
.st_size
)) < 0)
146 giterr_set(GITERR_INVALID
, "Unknown file source %d", source
);
150 if ((error
= git_attr_file__new(&file
, entry
, source
)) < 0)
153 /* store the key of the attr_reader; don't bother with cache
154 * invalidation during the same attr reader session.
157 file
->session_key
= attr_session
->key
;
159 if (parser
&& (error
= parser(repo
, file
, git_buf_cstr(&content
))) < 0) {
160 git_attr_file__free(file
);
164 /* write cache breakers */
166 file
->nonexistent
= 1;
167 else if (source
== GIT_ATTR_FILE__FROM_INDEX
)
168 git_oid_cpy(&file
->cache_data
.oid
, git_blob_id(blob
));
169 else if (source
== GIT_ATTR_FILE__FROM_FILE
)
170 git_futils_filestamp_set_from_stat(&file
->cache_data
.stamp
, &st
);
171 /* else always cacheable */
177 git_buf_free(&content
);
182 int git_attr_file__out_of_date(
183 git_repository
*repo
,
184 git_attr_session
*attr_session
,
190 /* we are never out of date if we just created this data in the same
191 * attr_session; otherwise, nonexistent files must be invalidated
193 if (attr_session
&& attr_session
->key
== file
->session_key
)
195 else if (file
->nonexistent
)
198 switch (file
->source
) {
199 case GIT_ATTR_FILE__IN_MEMORY
:
202 case GIT_ATTR_FILE__FROM_FILE
:
203 return git_futils_filestamp_check(
204 &file
->cache_data
.stamp
, file
->entry
->fullpath
);
206 case GIT_ATTR_FILE__FROM_INDEX
: {
210 if ((error
= attr_file_oid_from_index(
211 &id
, repo
, file
->entry
->path
)) < 0)
214 return (git_oid__cmp(&file
->cache_data
.oid
, &id
) != 0);
218 giterr_set(GITERR_INVALID
, "Invalid file type %d", file
->source
);
223 static int sort_by_hash_and_name(const void *a_raw
, const void *b_raw
);
224 static void git_attr_rule__clear(git_attr_rule
*rule
);
225 static bool parse_optimized_patterns(
226 git_attr_fnmatch
*spec
,
228 const char *pattern
);
230 int git_attr_file__parse_buffer(
231 git_repository
*repo
, git_attr_file
*attrs
, const char *data
)
234 const char *scan
= data
, *context
= NULL
;
235 git_attr_rule
*rule
= NULL
;
237 /* if subdir file path, convert context for file paths */
239 git_path_root(attrs
->entry
->path
) < 0 &&
240 !git__suffixcmp(attrs
->entry
->path
, "/" GIT_ATTR_FILE
))
241 context
= attrs
->entry
->path
;
243 if (git_mutex_lock(&attrs
->lock
) < 0) {
244 giterr_set(GITERR_OS
, "Failed to lock attribute file");
248 while (!error
&& *scan
) {
249 /* allocate rule if needed */
250 if (!rule
&& !(rule
= git__calloc(1, sizeof(*rule
)))) {
256 GIT_ATTR_FNMATCH_ALLOWNEG
| GIT_ATTR_FNMATCH_ALLOWMACRO
;
258 /* parse the next "pattern attr attr attr" line */
259 if (!(error
= git_attr_fnmatch__parse(
260 &rule
->match
, &attrs
->pool
, context
, &scan
)) &&
261 !(error
= git_attr_assignment__parse(
262 repo
, &attrs
->pool
, &rule
->assigns
, &scan
)))
264 if (rule
->match
.flags
& GIT_ATTR_FNMATCH_MACRO
)
265 /* TODO: warning if macro found in file below repo root */
266 error
= git_attr_cache__insert_macro(repo
, rule
);
268 error
= git_vector_insert(&attrs
->rules
, rule
);
271 /* if the rule wasn't a pattern, on to the next */
273 git_attr_rule__clear(rule
); /* reset rule contents */
274 if (error
== GIT_ENOTFOUND
)
277 rule
= NULL
; /* vector now "owns" the rule */
281 git_mutex_unlock(&attrs
->lock
);
282 git_attr_rule__free(rule
);
287 uint32_t git_attr_file__name_hash(const char *name
)
292 while ((c
= (int)*name
++) != 0)
293 h
= ((h
<< 5) + h
) + c
;
297 int git_attr_file__lookup_one(
310 name
.name_hash
= git_attr_file__name_hash(attr
);
312 git_attr_file__foreach_matching_rule(file
, path
, i
, rule
) {
315 if (!git_vector_bsearch(&pos
, &rule
->assigns
, &name
)) {
316 *value
= ((git_attr_assignment
*)
317 git_vector_get(&rule
->assigns
, pos
))->value
;
325 int git_attr_file__load_standalone(git_attr_file
**out
, const char *path
)
329 git_buf content
= GIT_BUF_INIT
;
331 error
= git_attr_file__new(&file
, NULL
, GIT_ATTR_FILE__FROM_FILE
);
335 error
= git_attr_cache__alloc_file_entry(
336 &file
->entry
, NULL
, path
, &file
->pool
);
338 git_attr_file__free(file
);
341 /* because the cache entry is allocated from the file's own pool, we
342 * don't have to free it - freeing file+pool will free cache entry, too.
345 if (!(error
= git_futils_readbuffer(&content
, path
))) {
346 error
= git_attr_file__parse_buffer(NULL
, file
, content
.ptr
);
347 git_buf_free(&content
);
351 git_attr_file__free(file
);
358 bool git_attr_fnmatch__match(
359 git_attr_fnmatch
*match
,
362 const char *filename
;
366 * If the rule was generated in a subdirectory, we must only
367 * use it for paths inside that directory. We can thus return
368 * a non-match if the prefixes don't match.
370 if (match
->containing_dir
) {
371 if (match
->flags
& GIT_ATTR_FNMATCH_ICASE
) {
372 if (git__strncasecmp(path
->path
, match
->containing_dir
, match
->containing_dir_length
))
375 if (git__prefixcmp(path
->path
, match
->containing_dir
))
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
) {
386 filename
= path
->path
;
387 flags
|= FNM_PATHNAME
;
389 filename
= path
->basename
;
392 flags
|= FNM_LEADING_DIR
;
395 if ((match
->flags
& GIT_ATTR_FNMATCH_DIRECTORY
) && !path
->is_dir
) {
399 /* for attribute checks or root ignore checks, fail match */
400 if (!(match
->flags
& GIT_ATTR_FNMATCH_IGNORE
) ||
401 path
->basename
== path
->path
)
404 /* for ignore checks, use container of current item for check */
405 path
->basename
[-1] = '\0';
406 flags
|= FNM_LEADING_DIR
;
408 if (match
->containing_dir
)
409 matchpath
= path
->basename
;
411 matchpath
= path
->path
;
413 matchval
= p_fnmatch(match
->pattern
, matchpath
, flags
);
414 path
->basename
[-1] = '/';
415 return (matchval
!= FNM_NOMATCH
);
418 /* if path is a directory prefix of a negated pattern, then match */
419 if ((match
->flags
& GIT_ATTR_FNMATCH_NEGATIVE
) && path
->is_dir
) {
420 size_t pathlen
= strlen(path
->path
);
421 bool prefixed
= (pathlen
<= match
->length
) &&
422 ((match
->flags
& GIT_ATTR_FNMATCH_ICASE
) ?
423 !strncasecmp(match
->pattern
, path
->path
, pathlen
) :
424 !strncmp(match
->pattern
, path
->path
, pathlen
));
426 if (prefixed
&& git_path_at_end_of_segment(&match
->pattern
[pathlen
]))
430 return (p_fnmatch(match
->pattern
, filename
, flags
) != FNM_NOMATCH
);
433 bool git_attr_rule__match(
437 bool matched
= git_attr_fnmatch__match(&rule
->match
, path
);
439 if (rule
->match
.flags
& GIT_ATTR_FNMATCH_NEGATIVE
)
445 git_attr_assignment
*git_attr_rule__lookup_assignment(
446 git_attr_rule
*rule
, const char *name
)
451 key
.name_hash
= git_attr_file__name_hash(name
);
453 if (git_vector_bsearch(&pos
, &rule
->assigns
, &key
))
456 return git_vector_get(&rule
->assigns
, pos
);
459 int git_attr_path__init(
460 git_attr_path
*info
, const char *path
, const char *base
)
464 /* build full path as best we can */
465 git_buf_init(&info
->full
, 0);
467 if (git_path_join_unrooted(&info
->full
, path
, base
, &root
) < 0)
470 info
->path
= info
->full
.ptr
+ root
;
472 /* remove trailing slashes */
473 while (info
->full
.size
> 0) {
474 if (info
->full
.ptr
[info
->full
.size
- 1] != '/')
478 info
->full
.ptr
[info
->full
.size
] = '\0';
480 /* skip leading slashes in path */
481 while (*info
->path
== '/')
484 /* find trailing basename component */
485 info
->basename
= strrchr(info
->path
, '/');
488 if (!info
->basename
|| !*info
->basename
)
489 info
->basename
= info
->path
;
491 info
->is_dir
= (int)git_path_isdir(info
->full
.ptr
);
496 void git_attr_path__free(git_attr_path
*info
)
498 git_buf_free(&info
->full
);
500 info
->basename
= NULL
;
504 * From gitattributes(5):
506 * Patterns have the following format:
508 * - A blank line matches no files, so it can serve as a separator for
511 * - A line starting with # serves as a comment.
513 * - An optional prefix ! which negates the pattern; any matching file
514 * excluded by a previous pattern will become included again. If a negated
515 * pattern matches, this will override lower precedence patterns sources.
517 * - If the pattern ends with a slash, it is removed for the purpose of the
518 * following description, but it would only find a match with a directory. In
519 * other words, foo/ will match a directory foo and paths underneath it, but
520 * will not match a regular file or a symbolic link foo (this is consistent
521 * with the way how pathspec works in general in git).
523 * - If the pattern does not contain a slash /, git treats it as a shell glob
524 * pattern and checks for a match against the pathname without leading
527 * - Otherwise, git treats the pattern as a shell glob suitable for consumption
528 * by fnmatch(3) with the FNM_PATHNAME flag: wildcards in the pattern will
529 * not match a / in the pathname. For example, "Documentation/\*.html" matches
530 * "Documentation/git.html" but not "Documentation/ppc/ppc.html". A leading
531 * slash matches the beginning of the pathname; for example, "/\*.c" matches
532 * "cat-file.c" but not "mozilla-sha1/sha1.c".
536 * This will return 0 if the spec was filled out,
537 * GIT_ENOTFOUND if the fnmatch does not require matching, or
538 * another error code there was an actual problem.
540 int git_attr_fnmatch__parse(
541 git_attr_fnmatch
*spec
,
546 const char *pattern
, *scan
;
547 int slash_count
, allow_space
;
549 assert(spec
&& base
&& *base
);
551 if (parse_optimized_patterns(spec
, pool
, *base
))
554 spec
->flags
= (spec
->flags
& GIT_ATTR_FNMATCH__INCOMING
);
555 allow_space
= ((spec
->flags
& GIT_ATTR_FNMATCH_ALLOWSPACE
) != 0);
559 while (git__isspace(*pattern
)) pattern
++;
560 if (!*pattern
|| *pattern
== '#') {
561 *base
= git__next_line(pattern
);
562 return GIT_ENOTFOUND
;
565 if (*pattern
== '[' && (spec
->flags
& GIT_ATTR_FNMATCH_ALLOWMACRO
) != 0) {
566 if (strncmp(pattern
, "[attr]", 6) == 0) {
567 spec
->flags
= spec
->flags
| GIT_ATTR_FNMATCH_MACRO
;
570 /* else a character range like [a-e]* which is accepted */
573 if (*pattern
== '!' && (spec
->flags
& GIT_ATTR_FNMATCH_ALLOWNEG
) != 0) {
574 spec
->flags
= spec
->flags
|
575 GIT_ATTR_FNMATCH_NEGATIVE
| GIT_ATTR_FNMATCH_LEADINGDIR
;
580 for (scan
= pattern
; *scan
!= '\0'; ++scan
) {
581 /* scan until (non-escaped) white space */
582 if (git__isspace(*scan
) && *(scan
- 1) != '\\') {
583 if (!allow_space
|| (*scan
!= ' ' && *scan
!= '\t' && *scan
!= '\r'))
588 spec
->flags
= spec
->flags
| GIT_ATTR_FNMATCH_FULLPATH
;
593 /* remember if we see an unescaped wildcard in pattern */
594 else if (git__iswildcard(*scan
) &&
595 (scan
== pattern
|| (*(scan
- 1) != '\\')))
596 spec
->flags
= spec
->flags
| GIT_ATTR_FNMATCH_HASWILD
;
601 if ((spec
->length
= scan
- pattern
) == 0)
602 return GIT_ENOTFOUND
;
605 * Remove one trailing \r in case this is a CRLF delimited
606 * file, in the case of Icon\r\r\n, we still leave the first
607 * \r there to match against.
609 if (pattern
[spec
->length
- 1] == '\r')
610 if (--spec
->length
== 0)
611 return GIT_ENOTFOUND
;
613 if (pattern
[spec
->length
- 1] == '/') {
615 spec
->flags
= spec
->flags
| GIT_ATTR_FNMATCH_DIRECTORY
;
616 if (--slash_count
<= 0)
617 spec
->flags
= spec
->flags
& ~GIT_ATTR_FNMATCH_FULLPATH
;
619 if ((spec
->flags
& GIT_ATTR_FNMATCH_NOLEADINGDIR
) == 0 &&
621 pattern
[spec
->length
- 1] == '*' &&
622 pattern
[spec
->length
- 2] == '/') {
624 spec
->flags
= spec
->flags
| GIT_ATTR_FNMATCH_LEADINGDIR
;
625 /* leave FULLPATH match on, however */
629 char *slash
= strchr(context
, '/');
632 /* include the slash for easier matching */
633 len
= slash
- context
+ 1;
634 spec
->containing_dir
= git_pool_strndup(pool
, context
, len
);
635 spec
->containing_dir_length
= len
;
639 if ((spec
->flags
& GIT_ATTR_FNMATCH_FULLPATH
) != 0 &&
640 context
!= NULL
&& git_path_root(pattern
) < 0)
642 /* use context path minus the trailing filename */
643 char *slash
= strrchr(context
, '/');
644 size_t contextlen
= slash
? slash
- context
+ 1 : 0;
646 /* given an unrooted fullpath match from a file inside a repo,
647 * prefix the pattern with the relative directory of the source file
649 spec
->pattern
= git_pool_malloc(
650 pool
, (uint32_t)(contextlen
+ spec
->length
+ 1));
652 memcpy(spec
->pattern
, context
, contextlen
);
653 memcpy(spec
->pattern
+ contextlen
, pattern
, spec
->length
);
654 spec
->length
+= contextlen
;
655 spec
->pattern
[spec
->length
] = '\0';
658 spec
->pattern
= git_pool_strndup(pool
, pattern
, spec
->length
);
661 if (!spec
->pattern
) {
662 *base
= git__next_line(pattern
);
665 /* strip '\' that might have be used for internal whitespace */
666 spec
->length
= git__unescape(spec
->pattern
);
667 /* TODO: convert remaining '\' into '/' for POSIX ??? */
673 static bool parse_optimized_patterns(
674 git_attr_fnmatch
*spec
,
678 if (!pattern
[1] && (pattern
[0] == '*' || pattern
[0] == '.')) {
679 spec
->flags
= GIT_ATTR_FNMATCH_MATCH_ALL
;
680 spec
->pattern
= git_pool_strndup(pool
, pattern
, 1);
689 static int sort_by_hash_and_name(const void *a_raw
, const void *b_raw
)
691 const git_attr_name
*a
= a_raw
;
692 const git_attr_name
*b
= b_raw
;
694 if (b
->name_hash
< a
->name_hash
)
696 else if (b
->name_hash
> a
->name_hash
)
699 return strcmp(b
->name
, a
->name
);
702 static void git_attr_assignment__free(git_attr_assignment
*assign
)
704 /* name and value are stored in a git_pool associated with the
705 * git_attr_file, so they do not need to be freed here
708 assign
->value
= NULL
;
712 static int merge_assignments(void **old_raw
, void *new_raw
)
714 git_attr_assignment
**old
= (git_attr_assignment
**)old_raw
;
715 git_attr_assignment
*new = (git_attr_assignment
*)new_raw
;
717 GIT_REFCOUNT_DEC(*old
, git_attr_assignment__free
);
722 int git_attr_assignment__parse(
723 git_repository
*repo
,
729 const char *scan
= *base
;
730 git_attr_assignment
*assign
= NULL
;
732 assert(assigns
&& !assigns
->length
);
734 git_vector_set_cmp(assigns
, sort_by_hash_and_name
);
736 while (*scan
&& *scan
!= '\n') {
737 const char *name_start
, *value_start
;
739 /* skip leading blanks */
740 while (git__isspace(*scan
) && *scan
!= '\n') scan
++;
742 /* allocate assign if needed */
744 assign
= git__calloc(1, sizeof(git_attr_assignment
));
745 GITERR_CHECK_ALLOC(assign
);
746 GIT_REFCOUNT_INC(assign
);
749 assign
->name_hash
= 5381;
750 assign
->value
= git_attr__true
;
752 /* look for magic name prefixes */
754 assign
->value
= git_attr__false
;
756 } else if (*scan
== '!') {
757 assign
->value
= git_attr__unset
; /* explicit unspecified state */
759 } else if (*scan
== '#') /* comment rest of line */
764 while (*scan
&& !git__isspace(*scan
) && *scan
!= '=') {
766 ((assign
->name_hash
<< 5) + assign
->name_hash
) + *scan
;
769 if (scan
== name_start
) {
770 /* must have found lone prefix (" - ") or leading = ("=foo")
771 * or end of buffer -- advance until whitespace and continue
773 while (*scan
&& !git__isspace(*scan
)) scan
++;
777 /* allocate permanent storage for name */
778 assign
->name
= git_pool_strndup(pool
, name_start
, scan
- name_start
);
779 GITERR_CHECK_ALLOC(assign
->name
);
781 /* if there is an equals sign, find the value */
783 for (value_start
= ++scan
; *scan
&& !git__isspace(*scan
); ++scan
);
785 /* if we found a value, allocate permanent storage for it */
786 if (scan
> value_start
) {
787 assign
->value
= git_pool_strndup(pool
, value_start
, scan
- value_start
);
788 GITERR_CHECK_ALLOC(assign
->value
);
792 /* expand macros (if given a repo with a macro cache) */
793 if (repo
!= NULL
&& assign
->value
== git_attr__true
) {
794 git_attr_rule
*macro
=
795 git_attr_cache__lookup_macro(repo
, assign
->name
);
799 git_attr_assignment
*massign
;
801 git_vector_foreach(¯o
->assigns
, i
, massign
) {
802 GIT_REFCOUNT_INC(massign
);
804 error
= git_vector_insert_sorted(
805 assigns
, massign
, &merge_assignments
);
806 if (error
< 0 && error
!= GIT_EEXISTS
) {
807 git_attr_assignment__free(assign
);
814 /* insert allocated assign into vector */
815 error
= git_vector_insert_sorted(assigns
, assign
, &merge_assignments
);
816 if (error
< 0 && error
!= GIT_EEXISTS
)
819 /* clear assign since it is now "owned" by the vector */
824 git_attr_assignment__free(assign
);
826 *base
= git__next_line(scan
);
828 return (assigns
->length
== 0) ? GIT_ENOTFOUND
: 0;
831 static void git_attr_rule__clear(git_attr_rule
*rule
)
834 git_attr_assignment
*assign
;
839 if (!(rule
->match
.flags
& GIT_ATTR_FNMATCH_IGNORE
)) {
840 git_vector_foreach(&rule
->assigns
, i
, assign
)
841 GIT_REFCOUNT_DEC(assign
, git_attr_assignment__free
);
842 git_vector_free(&rule
->assigns
);
845 /* match.pattern is stored in a git_pool, so no need to free */
846 rule
->match
.pattern
= NULL
;
847 rule
->match
.length
= 0;
850 void git_attr_rule__free(git_attr_rule
*rule
)
852 git_attr_rule__clear(rule
);
856 int git_attr_session__init(git_attr_session
*session
, git_repository
*repo
)
860 session
->key
= git_atomic_inc(&repo
->attr_session_key
);
865 void git_attr_session__free(git_attr_session
*session
)
870 git_buf_free(&session
->sysdir
);
871 git_buf_free(&session
->tmp
);
873 memset(session
, 0, sizeof(git_attr_session
));