2 * Copyright (C) the libgit2 contributors. All rights reserved.
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
10 #include "repository.h"
12 #include "attrcache.h"
14 #include "git2/blob.h"
15 #include "git2/tree.h"
18 #include "wildmatch.h"
21 static void attr_file_free(git_attr_file
*file
)
23 bool unlock
= !git_mutex_lock(&file
->lock
);
24 git_attr_file__clear_rules(file
, false);
25 git_pool_clear(&file
->pool
);
27 git_mutex_unlock(&file
->lock
);
28 git_mutex_free(&file
->lock
);
30 git__memzero(file
, sizeof(*file
));
34 int git_attr_file__new(
36 git_attr_file_entry
*entry
,
37 git_attr_file_source source
)
39 git_attr_file
*attrs
= git__calloc(1, sizeof(git_attr_file
));
40 GIT_ERROR_CHECK_ALLOC(attrs
);
42 if (git_mutex_init(&attrs
->lock
) < 0) {
43 git_error_set(GIT_ERROR_OS
, "failed to initialize lock");
47 if (git_pool_init(&attrs
->pool
, 1) < 0)
50 GIT_REFCOUNT_INC(attrs
);
52 attrs
->source
= source
;
61 int git_attr_file__clear_rules(git_attr_file
*file
, bool need_lock
)
66 if (need_lock
&& git_mutex_lock(&file
->lock
) < 0) {
67 git_error_set(GIT_ERROR_OS
, "failed to lock attribute file");
71 git_vector_foreach(&file
->rules
, i
, rule
)
72 git_attr_rule__free(rule
);
73 git_vector_free(&file
->rules
);
76 git_mutex_unlock(&file
->lock
);
81 void git_attr_file__free(git_attr_file
*file
)
85 GIT_REFCOUNT_DEC(file
, attr_file_free
);
88 static int attr_file_oid_from_index(
89 git_oid
*oid
, git_repository
*repo
, const char *path
)
94 const git_index_entry
*entry
;
96 if ((error
= git_repository_index__weakptr(&idx
, repo
)) < 0 ||
97 (error
= git_index__find_pos(&pos
, idx
, path
, 0, 0)) < 0)
100 if (!(entry
= git_index_get_byindex(idx
, pos
)))
101 return GIT_ENOTFOUND
;
107 int git_attr_file__load(
109 git_repository
*repo
,
110 git_attr_session
*attr_session
,
111 git_attr_file_entry
*entry
,
112 git_attr_file_source source
,
113 git_attr_file_parser parser
,
117 git_tree
*tree
= NULL
;
118 git_tree_entry
*tree_entry
= NULL
;
119 git_blob
*blob
= NULL
;
120 git_buf content
= GIT_BUF_INIT
;
121 const char *content_str
;
124 bool nonexistent
= false;
128 git_object_size_t blobsize
;
133 case GIT_ATTR_FILE__IN_MEMORY
:
134 /* in-memory attribute file doesn't need data */
136 case GIT_ATTR_FILE__FROM_INDEX
: {
137 if ((error
= attr_file_oid_from_index(&id
, repo
, entry
->path
)) < 0 ||
138 (error
= git_blob_lookup(&blob
, repo
, &id
)) < 0)
141 /* Do not assume that data straight from the ODB is NULL-terminated;
142 * copy the contents of a file to a buffer to work on */
143 blobsize
= git_blob_rawsize(blob
);
145 GIT_ERROR_CHECK_BLOBSIZE(blobsize
);
146 git_buf_put(&content
, git_blob_rawcontent(blob
), (size_t)blobsize
);
149 case GIT_ATTR_FILE__FROM_FILE
: {
152 /* For open or read errors, pretend that we got ENOTFOUND. */
153 /* TODO: issue warning when warning API is available */
155 if (p_stat(entry
->fullpath
, &st
) < 0 ||
156 S_ISDIR(st
.st_mode
) ||
157 (fd
= git_futils_open_ro(entry
->fullpath
)) < 0 ||
158 (error
= git_futils_readbuffer_fd(&content
, fd
, (size_t)st
.st_size
)) < 0)
166 case GIT_ATTR_FILE__FROM_HEAD
: {
167 if ((error
= git_repository_head_tree(&tree
, repo
)) < 0 ||
168 (error
= git_tree_entry_bypath(&tree_entry
, tree
, entry
->path
)) < 0 ||
169 (error
= git_blob_lookup(&blob
, repo
, git_tree_entry_id(tree_entry
))) < 0)
173 * Do not assume that data straight from the ODB is NULL-terminated;
174 * copy the contents of a file to a buffer to work on.
176 blobsize
= git_blob_rawsize(blob
);
178 GIT_ERROR_CHECK_BLOBSIZE(blobsize
);
179 if ((error
= git_buf_put(&content
,
180 git_blob_rawcontent(blob
), (size_t)blobsize
)) < 0)
186 git_error_set(GIT_ERROR_INVALID
, "unknown file source %d", source
);
190 if ((error
= git_attr_file__new(&file
, entry
, source
)) < 0)
193 /* advance over a UTF8 BOM */
194 content_str
= git_buf_cstr(&content
);
195 bom_offset
= git_buf_text_detect_bom(&bom
, &content
);
197 if (bom
== GIT_BOM_UTF8
)
198 content_str
+= bom_offset
;
200 /* store the key of the attr_reader; don't bother with cache
201 * invalidation during the same attr reader session.
204 file
->session_key
= attr_session
->key
;
206 if (parser
&& (error
= parser(repo
, file
, content_str
, allow_macros
)) < 0) {
207 git_attr_file__free(file
);
211 /* write cache breakers */
213 file
->nonexistent
= 1;
214 else if (source
== GIT_ATTR_FILE__FROM_INDEX
)
215 git_oid_cpy(&file
->cache_data
.oid
, git_blob_id(blob
));
216 else if (source
== GIT_ATTR_FILE__FROM_HEAD
)
217 git_oid_cpy(&file
->cache_data
.oid
, git_tree_id(tree
));
218 else if (source
== GIT_ATTR_FILE__FROM_FILE
)
219 git_futils_filestamp_set_from_stat(&file
->cache_data
.stamp
, &st
);
220 /* else always cacheable */
226 git_tree_entry_free(tree_entry
);
228 git_buf_dispose(&content
);
233 int git_attr_file__out_of_date(
234 git_repository
*repo
,
235 git_attr_session
*attr_session
,
241 /* we are never out of date if we just created this data in the same
242 * attr_session; otherwise, nonexistent files must be invalidated
244 if (attr_session
&& attr_session
->key
== file
->session_key
)
246 else if (file
->nonexistent
)
249 switch (file
->source
) {
250 case GIT_ATTR_FILE__IN_MEMORY
:
253 case GIT_ATTR_FILE__FROM_FILE
:
254 return git_futils_filestamp_check(
255 &file
->cache_data
.stamp
, file
->entry
->fullpath
);
257 case GIT_ATTR_FILE__FROM_INDEX
: {
261 if ((error
= attr_file_oid_from_index(
262 &id
, repo
, file
->entry
->path
)) < 0)
265 return (git_oid__cmp(&file
->cache_data
.oid
, &id
) != 0);
268 case GIT_ATTR_FILE__FROM_HEAD
: {
272 if ((error
= git_repository_head_tree(&tree
, repo
)) < 0)
275 error
= git_oid__cmp(&file
->cache_data
.oid
, git_tree_id(tree
));
282 git_error_set(GIT_ERROR_INVALID
, "invalid file type %d", file
->source
);
287 static int sort_by_hash_and_name(const void *a_raw
, const void *b_raw
);
288 static void git_attr_rule__clear(git_attr_rule
*rule
);
289 static bool parse_optimized_patterns(
290 git_attr_fnmatch
*spec
,
292 const char *pattern
);
294 int git_attr_file__parse_buffer(
295 git_repository
*repo
, git_attr_file
*attrs
, const char *data
, bool allow_macros
)
297 const char *scan
= data
, *context
= NULL
;
298 git_attr_rule
*rule
= NULL
;
301 /* If subdir file path, convert context for file paths */
302 if (attrs
->entry
&& git_path_root(attrs
->entry
->path
) < 0 &&
303 !git__suffixcmp(attrs
->entry
->path
, "/" GIT_ATTR_FILE
))
304 context
= attrs
->entry
->path
;
306 if (git_mutex_lock(&attrs
->lock
) < 0) {
307 git_error_set(GIT_ERROR_OS
, "failed to lock attribute file");
311 while (!error
&& *scan
) {
312 /* Allocate rule if needed, otherwise re-use previous rule */
314 rule
= git__calloc(1, sizeof(*rule
));
315 GIT_ERROR_CHECK_ALLOC(rule
);
317 git_attr_rule__clear(rule
);
319 rule
->match
.flags
= GIT_ATTR_FNMATCH_ALLOWNEG
| GIT_ATTR_FNMATCH_ALLOWMACRO
;
321 /* Parse the next "pattern attr attr attr" line */
322 if ((error
= git_attr_fnmatch__parse(&rule
->match
, &attrs
->pool
, context
, &scan
)) < 0 ||
323 (error
= git_attr_assignment__parse(repo
, &attrs
->pool
, &rule
->assigns
, &scan
)) < 0)
325 if (error
!= GIT_ENOTFOUND
)
331 if (rule
->match
.flags
& GIT_ATTR_FNMATCH_MACRO
) {
332 /* TODO: warning if macro found in file below repo root */
335 if ((error
= git_attr_cache__insert_macro(repo
, rule
)) < 0)
337 } else if ((error
= git_vector_insert(&attrs
->rules
, rule
)) < 0)
344 git_mutex_unlock(&attrs
->lock
);
345 git_attr_rule__free(rule
);
350 uint32_t git_attr_file__name_hash(const char *name
)
355 while ((c
= (int)*name
++) != 0)
356 h
= ((h
<< 5) + h
) + c
;
360 int git_attr_file__lookup_one(
373 name
.name_hash
= git_attr_file__name_hash(attr
);
375 git_attr_file__foreach_matching_rule(file
, path
, i
, rule
) {
378 if (!git_vector_bsearch(&pos
, &rule
->assigns
, &name
)) {
379 *value
= ((git_attr_assignment
*)
380 git_vector_get(&rule
->assigns
, pos
))->value
;
388 int git_attr_file__load_standalone(git_attr_file
**out
, const char *path
)
390 git_buf content
= GIT_BUF_INIT
;
391 git_attr_file
*file
= NULL
;
394 if ((error
= git_futils_readbuffer(&content
, path
)) < 0)
398 * Because the cache entry is allocated from the file's own pool, we
399 * don't have to free it - freeing file+pool will free cache entry, too.
402 if ((error
= git_attr_file__new(&file
, NULL
, GIT_ATTR_FILE__FROM_FILE
)) < 0 ||
403 (error
= git_attr_file__parse_buffer(NULL
, file
, content
.ptr
, true)) < 0 ||
404 (error
= git_attr_cache__alloc_file_entry(&file
->entry
, NULL
, path
, &file
->pool
)) < 0)
410 git_attr_file__free(file
);
411 git_buf_dispose(&content
);
416 bool git_attr_fnmatch__match(
417 git_attr_fnmatch
*match
,
420 const char *relpath
= path
->path
;
421 const char *filename
;
425 * If the rule was generated in a subdirectory, we must only
426 * use it for paths inside that directory. We can thus return
427 * a non-match if the prefixes don't match.
429 if (match
->containing_dir
) {
430 if (match
->flags
& GIT_ATTR_FNMATCH_ICASE
) {
431 if (git__strncasecmp(path
->path
, match
->containing_dir
, match
->containing_dir_length
))
434 if (git__prefixcmp(path
->path
, match
->containing_dir
))
438 relpath
+= match
->containing_dir_length
;
441 if (match
->flags
& GIT_ATTR_FNMATCH_ICASE
)
442 flags
|= WM_CASEFOLD
;
444 if (match
->flags
& GIT_ATTR_FNMATCH_FULLPATH
) {
446 flags
|= WM_PATHNAME
;
448 filename
= path
->basename
;
451 if ((match
->flags
& GIT_ATTR_FNMATCH_DIRECTORY
) && !path
->is_dir
) {
455 * for attribute checks or checks at the root of this match's
456 * containing_dir (or root of the repository if no containing_dir),
459 if (!(match
->flags
& GIT_ATTR_FNMATCH_IGNORE
) ||
460 path
->basename
== relpath
)
463 /* fail match if this is a file with same name as ignored folder */
464 samename
= (match
->flags
& GIT_ATTR_FNMATCH_ICASE
) ?
465 !strcasecmp(match
->pattern
, relpath
) :
466 !strcmp(match
->pattern
, relpath
);
471 return (wildmatch(match
->pattern
, relpath
, flags
) == WM_MATCH
);
474 return (wildmatch(match
->pattern
, filename
, flags
) == WM_MATCH
);
477 bool git_attr_rule__match(
481 bool matched
= git_attr_fnmatch__match(&rule
->match
, path
);
483 if (rule
->match
.flags
& GIT_ATTR_FNMATCH_NEGATIVE
)
489 git_attr_assignment
*git_attr_rule__lookup_assignment(
490 git_attr_rule
*rule
, const char *name
)
495 key
.name_hash
= git_attr_file__name_hash(name
);
497 if (git_vector_bsearch(&pos
, &rule
->assigns
, &key
))
500 return git_vector_get(&rule
->assigns
, pos
);
503 int git_attr_path__init(
504 git_attr_path
*info
, const char *path
, const char *base
, git_dir_flag dir_flag
)
508 /* build full path as best we can */
509 git_buf_init(&info
->full
, 0);
511 if (git_path_join_unrooted(&info
->full
, path
, base
, &root
) < 0)
514 info
->path
= info
->full
.ptr
+ root
;
516 /* remove trailing slashes */
517 while (info
->full
.size
> 0) {
518 if (info
->full
.ptr
[info
->full
.size
- 1] != '/')
522 info
->full
.ptr
[info
->full
.size
] = '\0';
524 /* skip leading slashes in path */
525 while (*info
->path
== '/')
528 /* find trailing basename component */
529 info
->basename
= strrchr(info
->path
, '/');
532 if (!info
->basename
|| !*info
->basename
)
533 info
->basename
= info
->path
;
537 case GIT_DIR_FLAG_FALSE
:
541 case GIT_DIR_FLAG_TRUE
:
545 case GIT_DIR_FLAG_UNKNOWN
:
547 info
->is_dir
= (int)git_path_isdir(info
->full
.ptr
);
554 void git_attr_path__free(git_attr_path
*info
)
556 git_buf_dispose(&info
->full
);
558 info
->basename
= NULL
;
562 * From gitattributes(5):
564 * Patterns have the following format:
566 * - A blank line matches no files, so it can serve as a separator for
569 * - A line starting with # serves as a comment.
571 * - An optional prefix ! which negates the pattern; any matching file
572 * excluded by a previous pattern will become included again. If a negated
573 * pattern matches, this will override lower precedence patterns sources.
575 * - If the pattern ends with a slash, it is removed for the purpose of the
576 * following description, but it would only find a match with a directory. In
577 * other words, foo/ will match a directory foo and paths underneath it, but
578 * will not match a regular file or a symbolic link foo (this is consistent
579 * with the way how pathspec works in general in git).
581 * - If the pattern does not contain a slash /, git treats it as a shell glob
582 * pattern and checks for a match against the pathname without leading
585 * - Otherwise, git treats the pattern as a shell glob suitable for consumption
586 * by fnmatch(3) with the FNM_PATHNAME flag: wildcards in the pattern will
587 * not match a / in the pathname. For example, "Documentation/\*.html" matches
588 * "Documentation/git.html" but not "Documentation/ppc/ppc.html". A leading
589 * slash matches the beginning of the pathname; for example, "/\*.c" matches
590 * "cat-file.c" but not "mozilla-sha1/sha1.c".
594 * Determine the length of trailing spaces. Escaped spaces do not count as
595 * trailing whitespace.
597 static size_t trailing_space_length(const char *p
, size_t len
)
600 for (n
= len
; n
; n
--) {
601 if (p
[n
-1] != ' ' && p
[n
-1] != '\t')
605 * Count escape-characters before space. In case where it's an
606 * even number of escape characters, then the escape char itself
607 * is escaped and the whitespace is an unescaped whitespace.
608 * Otherwise, the last escape char is not escaped and the
609 * whitespace in an escaped whitespace.
612 while (i
> 1 && p
[i
-2] == '\\')
620 static size_t unescape_spaces(char *str
)
622 char *scan
, *pos
= str
;
623 bool escaped
= false;
628 for (scan
= str
; *scan
; scan
++) {
629 if (!escaped
&& *scan
== '\\') {
634 /* Only insert the escape character for escaped non-spaces */
635 if (escaped
&& !git__isspace(*scan
))
649 * This will return 0 if the spec was filled out,
650 * GIT_ENOTFOUND if the fnmatch does not require matching, or
651 * another error code there was an actual problem.
653 int git_attr_fnmatch__parse(
654 git_attr_fnmatch
*spec
,
659 const char *pattern
, *scan
;
660 int slash_count
, allow_space
;
663 assert(spec
&& base
&& *base
);
665 if (parse_optimized_patterns(spec
, pool
, *base
))
668 spec
->flags
= (spec
->flags
& GIT_ATTR_FNMATCH__INCOMING
);
669 allow_space
= ((spec
->flags
& GIT_ATTR_FNMATCH_ALLOWSPACE
) != 0);
673 while (!allow_space
&& git__isspace(*pattern
))
676 if (!*pattern
|| *pattern
== '#' || *pattern
== '\n' ||
677 (*pattern
== '\r' && *(pattern
+ 1) == '\n')) {
678 *base
= git__next_line(pattern
);
679 return GIT_ENOTFOUND
;
682 if (*pattern
== '[' && (spec
->flags
& GIT_ATTR_FNMATCH_ALLOWMACRO
) != 0) {
683 if (strncmp(pattern
, "[attr]", 6) == 0) {
684 spec
->flags
= spec
->flags
| GIT_ATTR_FNMATCH_MACRO
;
687 /* else a character range like [a-e]* which is accepted */
690 if (*pattern
== '!' && (spec
->flags
& GIT_ATTR_FNMATCH_ALLOWNEG
) != 0) {
691 spec
->flags
= spec
->flags
| GIT_ATTR_FNMATCH_NEGATIVE
;
697 /* Scan until a non-escaped whitespace. */
698 for (scan
= pattern
; *scan
!= '\0'; ++scan
) {
701 if (c
== '\\' && !escaped
) {
704 } else if (git__isspace(c
) && !escaped
) {
705 if (!allow_space
|| (c
!= ' ' && c
!= '\t' && c
!= '\r'))
707 } else if (c
== '/') {
708 spec
->flags
= spec
->flags
| GIT_ATTR_FNMATCH_FULLPATH
;
711 if (slash_count
== 1 && pattern
== scan
)
713 } else if (git__iswildcard(c
) && !escaped
) {
714 /* remember if we see an unescaped wildcard in pattern */
715 spec
->flags
= spec
->flags
| GIT_ATTR_FNMATCH_HASWILD
;
723 if ((spec
->length
= scan
- pattern
) == 0)
724 return GIT_ENOTFOUND
;
727 * Remove one trailing \r in case this is a CRLF delimited
728 * file, in the case of Icon\r\r\n, we still leave the first
729 * \r there to match against.
731 if (pattern
[spec
->length
- 1] == '\r')
732 if (--spec
->length
== 0)
733 return GIT_ENOTFOUND
;
735 /* Remove trailing spaces. */
736 spec
->length
-= trailing_space_length(pattern
, spec
->length
);
738 if (spec
->length
== 0)
739 return GIT_ENOTFOUND
;
741 if (pattern
[spec
->length
- 1] == '/') {
743 spec
->flags
= spec
->flags
| GIT_ATTR_FNMATCH_DIRECTORY
;
744 if (--slash_count
<= 0)
745 spec
->flags
= spec
->flags
& ~GIT_ATTR_FNMATCH_FULLPATH
;
749 char *slash
= strrchr(context
, '/');
752 /* include the slash for easier matching */
753 len
= slash
- context
+ 1;
754 spec
->containing_dir
= git_pool_strndup(pool
, context
, len
);
755 spec
->containing_dir_length
= len
;
759 spec
->pattern
= git_pool_strndup(pool
, pattern
, spec
->length
);
761 if (!spec
->pattern
) {
762 *base
= git__next_line(pattern
);
765 /* strip '\' that might have been used for internal whitespace */
766 spec
->length
= unescape_spaces(spec
->pattern
);
772 static bool parse_optimized_patterns(
773 git_attr_fnmatch
*spec
,
777 if (!pattern
[1] && (pattern
[0] == '*' || pattern
[0] == '.')) {
778 spec
->flags
= GIT_ATTR_FNMATCH_MATCH_ALL
;
779 spec
->pattern
= git_pool_strndup(pool
, pattern
, 1);
788 static int sort_by_hash_and_name(const void *a_raw
, const void *b_raw
)
790 const git_attr_name
*a
= a_raw
;
791 const git_attr_name
*b
= b_raw
;
793 if (b
->name_hash
< a
->name_hash
)
795 else if (b
->name_hash
> a
->name_hash
)
798 return strcmp(b
->name
, a
->name
);
801 static void git_attr_assignment__free(git_attr_assignment
*assign
)
803 /* name and value are stored in a git_pool associated with the
804 * git_attr_file, so they do not need to be freed here
807 assign
->value
= NULL
;
811 static int merge_assignments(void **old_raw
, void *new_raw
)
813 git_attr_assignment
**old
= (git_attr_assignment
**)old_raw
;
814 git_attr_assignment
*new = (git_attr_assignment
*)new_raw
;
816 GIT_REFCOUNT_DEC(*old
, git_attr_assignment__free
);
821 int git_attr_assignment__parse(
822 git_repository
*repo
,
828 const char *scan
= *base
;
829 git_attr_assignment
*assign
= NULL
;
831 assert(assigns
&& !assigns
->length
);
833 git_vector_set_cmp(assigns
, sort_by_hash_and_name
);
835 while (*scan
&& *scan
!= '\n') {
836 const char *name_start
, *value_start
;
838 /* skip leading blanks */
839 while (git__isspace(*scan
) && *scan
!= '\n') scan
++;
841 /* allocate assign if needed */
843 assign
= git__calloc(1, sizeof(git_attr_assignment
));
844 GIT_ERROR_CHECK_ALLOC(assign
);
845 GIT_REFCOUNT_INC(assign
);
848 assign
->name_hash
= 5381;
849 assign
->value
= git_attr__true
;
851 /* look for magic name prefixes */
853 assign
->value
= git_attr__false
;
855 } else if (*scan
== '!') {
856 assign
->value
= git_attr__unset
; /* explicit unspecified state */
858 } else if (*scan
== '#') /* comment rest of line */
863 while (*scan
&& !git__isspace(*scan
) && *scan
!= '=') {
865 ((assign
->name_hash
<< 5) + assign
->name_hash
) + *scan
;
868 if (scan
== name_start
) {
869 /* must have found lone prefix (" - ") or leading = ("=foo")
870 * or end of buffer -- advance until whitespace and continue
872 while (*scan
&& !git__isspace(*scan
)) scan
++;
876 /* allocate permanent storage for name */
877 assign
->name
= git_pool_strndup(pool
, name_start
, scan
- name_start
);
878 GIT_ERROR_CHECK_ALLOC(assign
->name
);
880 /* if there is an equals sign, find the value */
882 for (value_start
= ++scan
; *scan
&& !git__isspace(*scan
); ++scan
);
884 /* if we found a value, allocate permanent storage for it */
885 if (scan
> value_start
) {
886 assign
->value
= git_pool_strndup(pool
, value_start
, scan
- value_start
);
887 GIT_ERROR_CHECK_ALLOC(assign
->value
);
891 /* expand macros (if given a repo with a macro cache) */
892 if (repo
!= NULL
&& assign
->value
== git_attr__true
) {
893 git_attr_rule
*macro
=
894 git_attr_cache__lookup_macro(repo
, assign
->name
);
898 git_attr_assignment
*massign
;
900 git_vector_foreach(¯o
->assigns
, i
, massign
) {
901 GIT_REFCOUNT_INC(massign
);
903 error
= git_vector_insert_sorted(
904 assigns
, massign
, &merge_assignments
);
905 if (error
< 0 && error
!= GIT_EEXISTS
) {
906 git_attr_assignment__free(assign
);
913 /* insert allocated assign into vector */
914 error
= git_vector_insert_sorted(assigns
, assign
, &merge_assignments
);
915 if (error
< 0 && error
!= GIT_EEXISTS
)
918 /* clear assign since it is now "owned" by the vector */
923 git_attr_assignment__free(assign
);
925 *base
= git__next_line(scan
);
927 return (assigns
->length
== 0) ? GIT_ENOTFOUND
: 0;
930 static void git_attr_rule__clear(git_attr_rule
*rule
)
933 git_attr_assignment
*assign
;
938 if (!(rule
->match
.flags
& GIT_ATTR_FNMATCH_IGNORE
)) {
939 git_vector_foreach(&rule
->assigns
, i
, assign
)
940 GIT_REFCOUNT_DEC(assign
, git_attr_assignment__free
);
941 git_vector_free(&rule
->assigns
);
944 /* match.pattern is stored in a git_pool, so no need to free */
945 rule
->match
.pattern
= NULL
;
946 rule
->match
.length
= 0;
949 void git_attr_rule__free(git_attr_rule
*rule
)
951 git_attr_rule__clear(rule
);
955 int git_attr_session__init(git_attr_session
*session
, git_repository
*repo
)
959 memset(session
, 0, sizeof(*session
));
960 session
->key
= git_atomic_inc(&repo
->attr_session_key
);
965 void git_attr_session__free(git_attr_session
*session
)
970 git_buf_dispose(&session
->sysdir
);
971 git_buf_dispose(&session
->tmp
);
973 memset(session
, 0, sizeof(git_attr_session
));