2 #include "repository.h"
9 static int sort_by_hash_and_name(const void *a_raw
, const void *b_raw
);
10 static void git_attr_rule__clear(git_attr_rule
*rule
);
11 static bool parse_optimized_patterns(
12 git_attr_fnmatch
*spec
,
16 int git_attr_file__new(
17 git_attr_file
**attrs_ptr
,
18 git_attr_file_source from
,
22 git_attr_file
*attrs
= NULL
;
24 attrs
= git__calloc(1, sizeof(git_attr_file
));
25 GITERR_CHECK_ALLOC(attrs
);
30 attrs
->pool
= git__calloc(1, sizeof(git_pool
));
31 if (!attrs
->pool
|| git_pool_init(attrs
->pool
, 1, 0) < 0)
33 attrs
->pool_is_allocated
= true;
37 size_t len
= strlen(path
);
39 attrs
->key
= git_pool_malloc(attrs
->pool
, (uint32_t)len
+ 3);
40 GITERR_CHECK_ALLOC(attrs
->key
);
42 attrs
->key
[0] = '0' + from
;
44 memcpy(&attrs
->key
[2], path
, len
);
45 attrs
->key
[len
+ 2] = '\0';
48 if (git_vector_init(&attrs
->rules
, 4, NULL
) < 0)
55 git_attr_file__free(attrs
);
60 int git_attr_file__parse_buffer(
61 git_repository
*repo
, void *parsedata
, const char *buffer
, git_attr_file
*attrs
)
64 const char *scan
= NULL
;
66 git_attr_rule
*rule
= NULL
;
68 GIT_UNUSED(parsedata
);
70 assert(buffer
&& attrs
);
74 /* if subdir file path, convert context for file paths */
75 if (attrs
->key
&& git__suffixcmp(attrs
->key
, "/" GIT_ATTR_FILE
) == 0) {
76 context
= attrs
->key
+ 2;
77 context
[strlen(context
) - strlen(GIT_ATTR_FILE
)] = '\0';
80 while (!error
&& *scan
) {
81 /* allocate rule if needed */
83 if (!(rule
= git__calloc(1, sizeof(git_attr_rule
)))) {
87 rule
->match
.flags
= GIT_ATTR_FNMATCH_ALLOWNEG
|
88 GIT_ATTR_FNMATCH_ALLOWMACRO
;
91 /* parse the next "pattern attr attr attr" line */
92 if (!(error
= git_attr_fnmatch__parse(
93 &rule
->match
, attrs
->pool
, context
, &scan
)) &&
94 !(error
= git_attr_assignment__parse(
95 repo
, attrs
->pool
, &rule
->assigns
, &scan
)))
97 if (rule
->match
.flags
& GIT_ATTR_FNMATCH_MACRO
)
98 /* should generate error/warning if this is coming from any
99 * file other than .gitattributes at repo root.
101 error
= git_attr_cache__insert_macro(repo
, rule
);
103 error
= git_vector_insert(&attrs
->rules
, rule
);
106 /* if the rule wasn't a pattern, on to the next */
108 git_attr_rule__clear(rule
); /* reset rule contents */
109 if (error
== GIT_ENOTFOUND
)
112 rule
= NULL
; /* vector now "owns" the rule */
116 git_attr_rule__free(rule
);
118 /* restore file path used for context */
120 context
[strlen(context
)] = '.'; /* first char of GIT_ATTR_FILE */
125 int git_attr_file__new_and_load(
126 git_attr_file
**attrs_ptr
,
130 git_buf content
= GIT_BUF_INIT
;
132 if ((error
= git_attr_file__new(attrs_ptr
, 0, path
, NULL
)) < 0)
135 if (!(error
= git_futils_readbuffer(&content
, path
)))
136 error
= git_attr_file__parse_buffer(
137 NULL
, NULL
, git_buf_cstr(&content
), *attrs_ptr
);
139 git_buf_free(&content
);
142 git_attr_file__free(*attrs_ptr
);
149 void git_attr_file__clear_rules(git_attr_file
*file
)
154 git_vector_foreach(&file
->rules
, i
, rule
)
155 git_attr_rule__free(rule
);
157 git_vector_free(&file
->rules
);
160 void git_attr_file__free(git_attr_file
*file
)
165 git_attr_file__clear_rules(file
);
167 if (file
->pool_is_allocated
) {
168 git_pool_clear(file
->pool
);
169 git__free(file
->pool
);
176 uint32_t git_attr_file__name_hash(const char *name
)
181 while ((c
= (int)*name
++) != 0)
182 h
= ((h
<< 5) + h
) + c
;
187 int git_attr_file__lookup_one(
189 const git_attr_path
*path
,
200 name
.name_hash
= git_attr_file__name_hash(attr
);
202 git_attr_file__foreach_matching_rule(file
, path
, i
, rule
) {
205 if (!git_vector_bsearch(&pos
, &rule
->assigns
, &name
)) {
206 *value
= ((git_attr_assignment
*)
207 git_vector_get(&rule
->assigns
, pos
))->value
;
216 bool git_attr_fnmatch__match(
217 git_attr_fnmatch
*match
,
218 const git_attr_path
*path
)
221 int icase_flags
= (match
->flags
& GIT_ATTR_FNMATCH_ICASE
) ? FNM_CASEFOLD
: 0;
223 if (match
->flags
& GIT_ATTR_FNMATCH_DIRECTORY
&& !path
->is_dir
)
226 if (match
->flags
& GIT_ATTR_FNMATCH_FULLPATH
)
227 fnm
= p_fnmatch(match
->pattern
, path
->path
, FNM_PATHNAME
| icase_flags
);
228 else if (path
->is_dir
)
229 fnm
= p_fnmatch(match
->pattern
, path
->basename
, FNM_LEADING_DIR
| icase_flags
);
231 fnm
= p_fnmatch(match
->pattern
, path
->basename
, icase_flags
);
233 return (fnm
== FNM_NOMATCH
) ? false : true;
236 bool git_attr_rule__match(
238 const git_attr_path
*path
)
240 bool matched
= git_attr_fnmatch__match(&rule
->match
, path
);
242 if (rule
->match
.flags
& GIT_ATTR_FNMATCH_NEGATIVE
)
249 git_attr_assignment
*git_attr_rule__lookup_assignment(
250 git_attr_rule
*rule
, const char *name
)
255 key
.name_hash
= git_attr_file__name_hash(name
);
257 if (git_vector_bsearch(&pos
, &rule
->assigns
, &key
))
260 return git_vector_get(&rule
->assigns
, pos
);
263 int git_attr_path__init(
264 git_attr_path
*info
, const char *path
, const char *base
)
268 /* build full path as best we can */
269 git_buf_init(&info
->full
, 0);
271 if (git_path_join_unrooted(&info
->full
, path
, base
, &root
) < 0)
274 info
->path
= info
->full
.ptr
+ root
;
276 /* remove trailing slashes */
277 while (info
->full
.size
> 0) {
278 if (info
->full
.ptr
[info
->full
.size
- 1] != '/')
282 info
->full
.ptr
[info
->full
.size
] = '\0';
284 /* skip leading slashes in path */
285 while (*info
->path
== '/')
288 /* find trailing basename component */
289 info
->basename
= strrchr(info
->path
, '/');
292 if (!info
->basename
|| !*info
->basename
)
293 info
->basename
= info
->path
;
295 info
->is_dir
= (int)git_path_isdir(info
->full
.ptr
);
300 void git_attr_path__free(git_attr_path
*info
)
302 git_buf_free(&info
->full
);
304 info
->basename
= NULL
;
308 * From gitattributes(5):
310 * Patterns have the following format:
312 * - A blank line matches no files, so it can serve as a separator for
315 * - A line starting with # serves as a comment.
317 * - An optional prefix ! which negates the pattern; any matching file
318 * excluded by a previous pattern will become included again. If a negated
319 * pattern matches, this will override lower precedence patterns sources.
321 * - If the pattern ends with a slash, it is removed for the purpose of the
322 * following description, but it would only find a match with a directory. In
323 * other words, foo/ will match a directory foo and paths underneath it, but
324 * will not match a regular file or a symbolic link foo (this is consistent
325 * with the way how pathspec works in general in git).
327 * - If the pattern does not contain a slash /, git treats it as a shell glob
328 * pattern and checks for a match against the pathname without leading
331 * - Otherwise, git treats the pattern as a shell glob suitable for consumption
332 * by fnmatch(3) with the FNM_PATHNAME flag: wildcards in the pattern will
333 * not match a / in the pathname. For example, "Documentation/\*.html" matches
334 * "Documentation/git.html" but not "Documentation/ppc/ppc.html". A leading
335 * slash matches the beginning of the pathname; for example, "/\*.c" matches
336 * "cat-file.c" but not "mozilla-sha1/sha1.c".
340 * This will return 0 if the spec was filled out,
341 * GIT_ENOTFOUND if the fnmatch does not require matching, or
342 * another error code there was an actual problem.
344 int git_attr_fnmatch__parse(
345 git_attr_fnmatch
*spec
,
350 const char *pattern
, *scan
;
351 int slash_count
, allow_space
;
353 assert(spec
&& base
&& *base
);
355 if (parse_optimized_patterns(spec
, pool
, *base
))
358 spec
->flags
= (spec
->flags
& GIT_ATTR_FNMATCH__INCOMING
);
359 allow_space
= ((spec
->flags
& GIT_ATTR_FNMATCH_ALLOWSPACE
) != 0);
363 while (git__isspace(*pattern
)) pattern
++;
364 if (!*pattern
|| *pattern
== '#') {
365 *base
= git__next_line(pattern
);
366 return GIT_ENOTFOUND
;
369 if (*pattern
== '[' && (spec
->flags
& GIT_ATTR_FNMATCH_ALLOWMACRO
) != 0) {
370 if (strncmp(pattern
, "[attr]", 6) == 0) {
371 spec
->flags
= spec
->flags
| GIT_ATTR_FNMATCH_MACRO
;
374 /* else a character range like [a-e]* which is accepted */
377 if (*pattern
== '!' && (spec
->flags
& GIT_ATTR_FNMATCH_ALLOWNEG
) != 0) {
378 spec
->flags
= spec
->flags
| GIT_ATTR_FNMATCH_NEGATIVE
;
383 for (scan
= pattern
; *scan
!= '\0'; ++scan
) {
384 /* scan until (non-escaped) white space */
385 if (git__isspace(*scan
) && *(scan
- 1) != '\\') {
386 if (!allow_space
|| (*scan
!= ' ' && *scan
!= '\t'))
391 spec
->flags
= spec
->flags
| GIT_ATTR_FNMATCH_FULLPATH
;
396 /* remember if we see an unescaped wildcard in pattern */
397 else if (git__iswildcard(*scan
) &&
398 (scan
== pattern
|| (*(scan
- 1) != '\\')))
399 spec
->flags
= spec
->flags
| GIT_ATTR_FNMATCH_HASWILD
;
404 if ((spec
->length
= scan
- pattern
) == 0)
405 return GIT_ENOTFOUND
;
407 if (pattern
[spec
->length
- 1] == '/') {
409 spec
->flags
= spec
->flags
| GIT_ATTR_FNMATCH_DIRECTORY
;
410 if (--slash_count
<= 0)
411 spec
->flags
= spec
->flags
& ~GIT_ATTR_FNMATCH_FULLPATH
;
414 if ((spec
->flags
& GIT_ATTR_FNMATCH_FULLPATH
) != 0 &&
415 source
!= NULL
&& git_path_root(pattern
) < 0)
417 size_t sourcelen
= strlen(source
);
418 /* given an unrooted fullpath match from a file inside a repo,
419 * prefix the pattern with the relative directory of the source file
421 spec
->pattern
= git_pool_malloc(
422 pool
, (uint32_t)(sourcelen
+ spec
->length
+ 1));
424 memcpy(spec
->pattern
, source
, sourcelen
);
425 memcpy(spec
->pattern
+ sourcelen
, pattern
, spec
->length
);
426 spec
->length
+= sourcelen
;
427 spec
->pattern
[spec
->length
] = '\0';
430 spec
->pattern
= git_pool_strndup(pool
, pattern
, spec
->length
);
433 if (!spec
->pattern
) {
434 *base
= git__next_line(pattern
);
437 /* strip '\' that might have be used for internal whitespace */
438 spec
->length
= git__unescape(spec
->pattern
);
444 static bool parse_optimized_patterns(
445 git_attr_fnmatch
*spec
,
449 if (!pattern
[1] && (pattern
[0] == '*' || pattern
[0] == '.')) {
450 spec
->flags
= GIT_ATTR_FNMATCH_MATCH_ALL
;
451 spec
->pattern
= git_pool_strndup(pool
, pattern
, 1);
460 static int sort_by_hash_and_name(const void *a_raw
, const void *b_raw
)
462 const git_attr_name
*a
= a_raw
;
463 const git_attr_name
*b
= b_raw
;
465 if (b
->name_hash
< a
->name_hash
)
467 else if (b
->name_hash
> a
->name_hash
)
470 return strcmp(b
->name
, a
->name
);
473 static void git_attr_assignment__free(git_attr_assignment
*assign
)
475 /* name and value are stored in a git_pool associated with the
476 * git_attr_file, so they do not need to be freed here
479 assign
->value
= NULL
;
483 static int merge_assignments(void **old_raw
, void *new_raw
)
485 git_attr_assignment
**old
= (git_attr_assignment
**)old_raw
;
486 git_attr_assignment
*new = (git_attr_assignment
*)new_raw
;
488 GIT_REFCOUNT_DEC(*old
, git_attr_assignment__free
);
493 int git_attr_assignment__parse(
494 git_repository
*repo
,
500 const char *scan
= *base
;
501 git_attr_assignment
*assign
= NULL
;
503 assert(assigns
&& !assigns
->length
);
505 git_vector_set_cmp(assigns
, sort_by_hash_and_name
);
507 while (*scan
&& *scan
!= '\n') {
508 const char *name_start
, *value_start
;
510 /* skip leading blanks */
511 while (git__isspace(*scan
) && *scan
!= '\n') scan
++;
513 /* allocate assign if needed */
515 assign
= git__calloc(1, sizeof(git_attr_assignment
));
516 GITERR_CHECK_ALLOC(assign
);
517 GIT_REFCOUNT_INC(assign
);
520 assign
->name_hash
= 5381;
521 assign
->value
= git_attr__true
;
523 /* look for magic name prefixes */
525 assign
->value
= git_attr__false
;
527 } else if (*scan
== '!') {
528 assign
->value
= git_attr__unset
; /* explicit unspecified state */
530 } else if (*scan
== '#') /* comment rest of line */
535 while (*scan
&& !git__isspace(*scan
) && *scan
!= '=') {
537 ((assign
->name_hash
<< 5) + assign
->name_hash
) + *scan
;
540 if (scan
== name_start
) {
541 /* must have found lone prefix (" - ") or leading = ("=foo")
542 * or end of buffer -- advance until whitespace and continue
544 while (*scan
&& !git__isspace(*scan
)) scan
++;
548 /* allocate permanent storage for name */
549 assign
->name
= git_pool_strndup(pool
, name_start
, scan
- name_start
);
550 GITERR_CHECK_ALLOC(assign
->name
);
552 /* if there is an equals sign, find the value */
554 for (value_start
= ++scan
; *scan
&& !git__isspace(*scan
); ++scan
);
556 /* if we found a value, allocate permanent storage for it */
557 if (scan
> value_start
) {
558 assign
->value
= git_pool_strndup(pool
, value_start
, scan
- value_start
);
559 GITERR_CHECK_ALLOC(assign
->value
);
563 /* expand macros (if given a repo with a macro cache) */
564 if (repo
!= NULL
&& assign
->value
== git_attr__true
) {
565 git_attr_rule
*macro
=
566 git_attr_cache__lookup_macro(repo
, assign
->name
);
570 git_attr_assignment
*massign
;
572 git_vector_foreach(¯o
->assigns
, i
, massign
) {
573 GIT_REFCOUNT_INC(massign
);
575 error
= git_vector_insert_sorted(
576 assigns
, massign
, &merge_assignments
);
577 if (error
< 0 && error
!= GIT_EEXISTS
)
583 /* insert allocated assign into vector */
584 error
= git_vector_insert_sorted(assigns
, assign
, &merge_assignments
);
585 if (error
< 0 && error
!= GIT_EEXISTS
)
588 /* clear assign since it is now "owned" by the vector */
593 git_attr_assignment__free(assign
);
595 *base
= git__next_line(scan
);
597 return (assigns
->length
== 0) ? GIT_ENOTFOUND
: 0;
600 static void git_attr_rule__clear(git_attr_rule
*rule
)
603 git_attr_assignment
*assign
;
608 if (!(rule
->match
.flags
& GIT_ATTR_FNMATCH_IGNORE
)) {
609 git_vector_foreach(&rule
->assigns
, i
, assign
)
610 GIT_REFCOUNT_DEC(assign
, git_attr_assignment__free
);
611 git_vector_free(&rule
->assigns
);
614 /* match.pattern is stored in a git_pool, so no need to free */
615 rule
->match
.pattern
= NULL
;
616 rule
->match
.length
= 0;
619 void git_attr_rule__free(git_attr_rule
*rule
)
621 git_attr_rule__clear(rule
);