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
) {
398 /* for attribute checks or root ignore checks, fail match */
399 if (!(match
->flags
& GIT_ATTR_FNMATCH_IGNORE
) ||
400 path
->basename
== path
->path
)
403 /* for ignore checks, use container of current item for check */
404 path
->basename
[-1] = '\0';
405 flags
|= FNM_LEADING_DIR
;
406 matchval
= p_fnmatch(match
->pattern
, path
->path
, flags
);
407 path
->basename
[-1] = '/';
408 return (matchval
!= FNM_NOMATCH
);
411 /* if path is a directory prefix of a negated pattern, then match */
412 if ((match
->flags
& GIT_ATTR_FNMATCH_NEGATIVE
) && path
->is_dir
) {
413 size_t pathlen
= strlen(path
->path
);
414 bool prefixed
= (pathlen
<= match
->length
) &&
415 ((match
->flags
& GIT_ATTR_FNMATCH_ICASE
) ?
416 !strncasecmp(match
->pattern
, path
->path
, pathlen
) :
417 !strncmp(match
->pattern
, path
->path
, pathlen
));
419 if (prefixed
&& git_path_at_end_of_segment(&match
->pattern
[pathlen
]))
423 return (p_fnmatch(match
->pattern
, filename
, flags
) != FNM_NOMATCH
);
426 bool git_attr_rule__match(
430 bool matched
= git_attr_fnmatch__match(&rule
->match
, path
);
432 if (rule
->match
.flags
& GIT_ATTR_FNMATCH_NEGATIVE
)
438 git_attr_assignment
*git_attr_rule__lookup_assignment(
439 git_attr_rule
*rule
, const char *name
)
444 key
.name_hash
= git_attr_file__name_hash(name
);
446 if (git_vector_bsearch(&pos
, &rule
->assigns
, &key
))
449 return git_vector_get(&rule
->assigns
, pos
);
452 int git_attr_path__init(
453 git_attr_path
*info
, const char *path
, const char *base
)
457 /* build full path as best we can */
458 git_buf_init(&info
->full
, 0);
460 if (git_path_join_unrooted(&info
->full
, path
, base
, &root
) < 0)
463 info
->path
= info
->full
.ptr
+ root
;
465 /* remove trailing slashes */
466 while (info
->full
.size
> 0) {
467 if (info
->full
.ptr
[info
->full
.size
- 1] != '/')
471 info
->full
.ptr
[info
->full
.size
] = '\0';
473 /* skip leading slashes in path */
474 while (*info
->path
== '/')
477 /* find trailing basename component */
478 info
->basename
= strrchr(info
->path
, '/');
481 if (!info
->basename
|| !*info
->basename
)
482 info
->basename
= info
->path
;
484 info
->is_dir
= (int)git_path_isdir(info
->full
.ptr
);
489 void git_attr_path__free(git_attr_path
*info
)
491 git_buf_free(&info
->full
);
493 info
->basename
= NULL
;
497 * From gitattributes(5):
499 * Patterns have the following format:
501 * - A blank line matches no files, so it can serve as a separator for
504 * - A line starting with # serves as a comment.
506 * - An optional prefix ! which negates the pattern; any matching file
507 * excluded by a previous pattern will become included again. If a negated
508 * pattern matches, this will override lower precedence patterns sources.
510 * - If the pattern ends with a slash, it is removed for the purpose of the
511 * following description, but it would only find a match with a directory. In
512 * other words, foo/ will match a directory foo and paths underneath it, but
513 * will not match a regular file or a symbolic link foo (this is consistent
514 * with the way how pathspec works in general in git).
516 * - If the pattern does not contain a slash /, git treats it as a shell glob
517 * pattern and checks for a match against the pathname without leading
520 * - Otherwise, git treats the pattern as a shell glob suitable for consumption
521 * by fnmatch(3) with the FNM_PATHNAME flag: wildcards in the pattern will
522 * not match a / in the pathname. For example, "Documentation/\*.html" matches
523 * "Documentation/git.html" but not "Documentation/ppc/ppc.html". A leading
524 * slash matches the beginning of the pathname; for example, "/\*.c" matches
525 * "cat-file.c" but not "mozilla-sha1/sha1.c".
529 * This will return 0 if the spec was filled out,
530 * GIT_ENOTFOUND if the fnmatch does not require matching, or
531 * another error code there was an actual problem.
533 int git_attr_fnmatch__parse(
534 git_attr_fnmatch
*spec
,
539 const char *pattern
, *scan
;
540 int slash_count
, allow_space
;
542 assert(spec
&& base
&& *base
);
544 if (parse_optimized_patterns(spec
, pool
, *base
))
547 spec
->flags
= (spec
->flags
& GIT_ATTR_FNMATCH__INCOMING
);
548 allow_space
= ((spec
->flags
& GIT_ATTR_FNMATCH_ALLOWSPACE
) != 0);
552 while (git__isspace(*pattern
)) pattern
++;
553 if (!*pattern
|| *pattern
== '#') {
554 *base
= git__next_line(pattern
);
555 return GIT_ENOTFOUND
;
558 if (*pattern
== '[' && (spec
->flags
& GIT_ATTR_FNMATCH_ALLOWMACRO
) != 0) {
559 if (strncmp(pattern
, "[attr]", 6) == 0) {
560 spec
->flags
= spec
->flags
| GIT_ATTR_FNMATCH_MACRO
;
563 /* else a character range like [a-e]* which is accepted */
566 if (*pattern
== '!' && (spec
->flags
& GIT_ATTR_FNMATCH_ALLOWNEG
) != 0) {
567 spec
->flags
= spec
->flags
|
568 GIT_ATTR_FNMATCH_NEGATIVE
| GIT_ATTR_FNMATCH_LEADINGDIR
;
573 for (scan
= pattern
; *scan
!= '\0'; ++scan
) {
574 /* scan until (non-escaped) white space */
575 if (git__isspace(*scan
) && *(scan
- 1) != '\\') {
576 if (!allow_space
|| (*scan
!= ' ' && *scan
!= '\t' && *scan
!= '\r'))
581 spec
->flags
= spec
->flags
| GIT_ATTR_FNMATCH_FULLPATH
;
586 /* remember if we see an unescaped wildcard in pattern */
587 else if (git__iswildcard(*scan
) &&
588 (scan
== pattern
|| (*(scan
- 1) != '\\')))
589 spec
->flags
= spec
->flags
| GIT_ATTR_FNMATCH_HASWILD
;
594 if ((spec
->length
= scan
- pattern
) == 0)
595 return GIT_ENOTFOUND
;
598 * Remove one trailing \r in case this is a CRLF delimited
599 * file, in the case of Icon\r\r\n, we still leave the first
600 * \r there to match against.
602 if (pattern
[spec
->length
- 1] == '\r')
603 if (--spec
->length
== 0)
604 return GIT_ENOTFOUND
;
606 if (pattern
[spec
->length
- 1] == '/') {
608 spec
->flags
= spec
->flags
| GIT_ATTR_FNMATCH_DIRECTORY
;
609 if (--slash_count
<= 0)
610 spec
->flags
= spec
->flags
& ~GIT_ATTR_FNMATCH_FULLPATH
;
612 if ((spec
->flags
& GIT_ATTR_FNMATCH_NOLEADINGDIR
) == 0 &&
614 pattern
[spec
->length
- 1] == '*' &&
615 pattern
[spec
->length
- 2] == '/') {
617 spec
->flags
= spec
->flags
| GIT_ATTR_FNMATCH_LEADINGDIR
;
618 /* leave FULLPATH match on, however */
622 char *slash
= strchr(context
, '/');
625 /* include the slash for easier matching */
626 len
= slash
- context
+ 1;
627 spec
->containing_dir
= git_pool_strndup(pool
, context
, len
);
628 spec
->containing_dir_length
= len
;
632 if ((spec
->flags
& GIT_ATTR_FNMATCH_FULLPATH
) != 0 &&
633 context
!= NULL
&& git_path_root(pattern
) < 0)
635 /* use context path minus the trailing filename */
636 char *slash
= strrchr(context
, '/');
637 size_t contextlen
= slash
? slash
- context
+ 1 : 0;
639 /* given an unrooted fullpath match from a file inside a repo,
640 * prefix the pattern with the relative directory of the source file
642 spec
->pattern
= git_pool_malloc(
643 pool
, (uint32_t)(contextlen
+ spec
->length
+ 1));
645 memcpy(spec
->pattern
, context
, contextlen
);
646 memcpy(spec
->pattern
+ contextlen
, pattern
, spec
->length
);
647 spec
->length
+= contextlen
;
648 spec
->pattern
[spec
->length
] = '\0';
651 spec
->pattern
= git_pool_strndup(pool
, pattern
, spec
->length
);
654 if (!spec
->pattern
) {
655 *base
= git__next_line(pattern
);
658 /* strip '\' that might have be used for internal whitespace */
659 spec
->length
= git__unescape(spec
->pattern
);
660 /* TODO: convert remaining '\' into '/' for POSIX ??? */
666 static bool parse_optimized_patterns(
667 git_attr_fnmatch
*spec
,
671 if (!pattern
[1] && (pattern
[0] == '*' || pattern
[0] == '.')) {
672 spec
->flags
= GIT_ATTR_FNMATCH_MATCH_ALL
;
673 spec
->pattern
= git_pool_strndup(pool
, pattern
, 1);
682 static int sort_by_hash_and_name(const void *a_raw
, const void *b_raw
)
684 const git_attr_name
*a
= a_raw
;
685 const git_attr_name
*b
= b_raw
;
687 if (b
->name_hash
< a
->name_hash
)
689 else if (b
->name_hash
> a
->name_hash
)
692 return strcmp(b
->name
, a
->name
);
695 static void git_attr_assignment__free(git_attr_assignment
*assign
)
697 /* name and value are stored in a git_pool associated with the
698 * git_attr_file, so they do not need to be freed here
701 assign
->value
= NULL
;
705 static int merge_assignments(void **old_raw
, void *new_raw
)
707 git_attr_assignment
**old
= (git_attr_assignment
**)old_raw
;
708 git_attr_assignment
*new = (git_attr_assignment
*)new_raw
;
710 GIT_REFCOUNT_DEC(*old
, git_attr_assignment__free
);
715 int git_attr_assignment__parse(
716 git_repository
*repo
,
722 const char *scan
= *base
;
723 git_attr_assignment
*assign
= NULL
;
725 assert(assigns
&& !assigns
->length
);
727 git_vector_set_cmp(assigns
, sort_by_hash_and_name
);
729 while (*scan
&& *scan
!= '\n') {
730 const char *name_start
, *value_start
;
732 /* skip leading blanks */
733 while (git__isspace(*scan
) && *scan
!= '\n') scan
++;
735 /* allocate assign if needed */
737 assign
= git__calloc(1, sizeof(git_attr_assignment
));
738 GITERR_CHECK_ALLOC(assign
);
739 GIT_REFCOUNT_INC(assign
);
742 assign
->name_hash
= 5381;
743 assign
->value
= git_attr__true
;
745 /* look for magic name prefixes */
747 assign
->value
= git_attr__false
;
749 } else if (*scan
== '!') {
750 assign
->value
= git_attr__unset
; /* explicit unspecified state */
752 } else if (*scan
== '#') /* comment rest of line */
757 while (*scan
&& !git__isspace(*scan
) && *scan
!= '=') {
759 ((assign
->name_hash
<< 5) + assign
->name_hash
) + *scan
;
762 if (scan
== name_start
) {
763 /* must have found lone prefix (" - ") or leading = ("=foo")
764 * or end of buffer -- advance until whitespace and continue
766 while (*scan
&& !git__isspace(*scan
)) scan
++;
770 /* allocate permanent storage for name */
771 assign
->name
= git_pool_strndup(pool
, name_start
, scan
- name_start
);
772 GITERR_CHECK_ALLOC(assign
->name
);
774 /* if there is an equals sign, find the value */
776 for (value_start
= ++scan
; *scan
&& !git__isspace(*scan
); ++scan
);
778 /* if we found a value, allocate permanent storage for it */
779 if (scan
> value_start
) {
780 assign
->value
= git_pool_strndup(pool
, value_start
, scan
- value_start
);
781 GITERR_CHECK_ALLOC(assign
->value
);
785 /* expand macros (if given a repo with a macro cache) */
786 if (repo
!= NULL
&& assign
->value
== git_attr__true
) {
787 git_attr_rule
*macro
=
788 git_attr_cache__lookup_macro(repo
, assign
->name
);
792 git_attr_assignment
*massign
;
794 git_vector_foreach(¯o
->assigns
, i
, massign
) {
795 GIT_REFCOUNT_INC(massign
);
797 error
= git_vector_insert_sorted(
798 assigns
, massign
, &merge_assignments
);
799 if (error
< 0 && error
!= GIT_EEXISTS
) {
800 git_attr_assignment__free(assign
);
807 /* insert allocated assign into vector */
808 error
= git_vector_insert_sorted(assigns
, assign
, &merge_assignments
);
809 if (error
< 0 && error
!= GIT_EEXISTS
)
812 /* clear assign since it is now "owned" by the vector */
817 git_attr_assignment__free(assign
);
819 *base
= git__next_line(scan
);
821 return (assigns
->length
== 0) ? GIT_ENOTFOUND
: 0;
824 static void git_attr_rule__clear(git_attr_rule
*rule
)
827 git_attr_assignment
*assign
;
832 if (!(rule
->match
.flags
& GIT_ATTR_FNMATCH_IGNORE
)) {
833 git_vector_foreach(&rule
->assigns
, i
, assign
)
834 GIT_REFCOUNT_DEC(assign
, git_attr_assignment__free
);
835 git_vector_free(&rule
->assigns
);
838 /* match.pattern is stored in a git_pool, so no need to free */
839 rule
->match
.pattern
= NULL
;
840 rule
->match
.length
= 0;
843 void git_attr_rule__free(git_attr_rule
*rule
)
845 git_attr_rule__clear(rule
);
849 int git_attr_session__init(git_attr_session
*session
, git_repository
*repo
)
853 session
->key
= git_atomic_inc(&repo
->attr_session_key
);
858 void git_attr_session__free(git_attr_session
*session
)
863 git_buf_free(&session
->sysdir
);
864 git_buf_free(&session
->tmp
);
866 memset(session
, 0, sizeof(git_attr_session
));