2 #include "repository.h"
8 static int sort_by_hash_and_name(const void *a_raw
, const void *b_raw
);
9 static void git_attr_rule__clear(git_attr_rule
*rule
);
11 int git_attr_file__new(
12 git_attr_file
**attrs_ptr
,
13 git_attr_file_source from
,
17 git_attr_file
*attrs
= NULL
;
19 attrs
= git__calloc(1, sizeof(git_attr_file
));
20 GITERR_CHECK_ALLOC(attrs
);
25 attrs
->pool
= git__calloc(1, sizeof(git_pool
));
26 if (!attrs
->pool
|| git_pool_init(attrs
->pool
, 1, 0) < 0)
28 attrs
->pool_is_allocated
= true;
32 size_t len
= strlen(path
);
34 attrs
->key
= git_pool_malloc(attrs
->pool
, (uint32_t)len
+ 3);
35 GITERR_CHECK_ALLOC(attrs
->key
);
37 attrs
->key
[0] = '0' + from
;
39 memcpy(&attrs
->key
[2], path
, len
);
40 attrs
->key
[len
+ 2] = '\0';
43 if (git_vector_init(&attrs
->rules
, 4, NULL
) < 0)
50 git_attr_file__free(attrs
);
55 int git_attr_file__parse_buffer(
56 git_repository
*repo
, void *parsedata
, const char *buffer
, git_attr_file
*attrs
)
59 const char *scan
= NULL
;
61 git_attr_rule
*rule
= NULL
;
63 GIT_UNUSED(parsedata
);
65 assert(buffer
&& attrs
);
69 /* if subdir file path, convert context for file paths */
70 if (attrs
->key
&& git__suffixcmp(attrs
->key
, "/" GIT_ATTR_FILE
) == 0) {
71 context
= attrs
->key
+ 2;
72 context
[strlen(context
) - strlen(GIT_ATTR_FILE
)] = '\0';
75 while (!error
&& *scan
) {
76 /* allocate rule if needed */
77 if (!rule
&& !(rule
= git__calloc(1, sizeof(git_attr_rule
)))) {
82 /* parse the next "pattern attr attr attr" line */
83 if (!(error
= git_attr_fnmatch__parse(
84 &rule
->match
, attrs
->pool
, context
, &scan
)) &&
85 !(error
= git_attr_assignment__parse(
86 repo
, attrs
->pool
, &rule
->assigns
, &scan
)))
88 if (rule
->match
.flags
& GIT_ATTR_FNMATCH_MACRO
)
89 /* should generate error/warning if this is coming from any
90 * file other than .gitattributes at repo root.
92 error
= git_attr_cache__insert_macro(repo
, rule
);
94 error
= git_vector_insert(&attrs
->rules
, rule
);
97 /* if the rule wasn't a pattern, on to the next */
99 git_attr_rule__clear(rule
); /* reset rule contents */
100 if (error
== GIT_ENOTFOUND
)
103 rule
= NULL
; /* vector now "owns" the rule */
107 git_attr_rule__free(rule
);
109 /* restore file path used for context */
111 context
[strlen(context
)] = '.'; /* first char of GIT_ATTR_FILE */
116 int git_attr_file__new_and_load(
117 git_attr_file
**attrs_ptr
,
121 git_buf content
= GIT_BUF_INIT
;
123 if ((error
= git_attr_file__new(attrs_ptr
, 0, path
, NULL
)) < 0)
126 if (!(error
= git_futils_readbuffer(&content
, path
)))
127 error
= git_attr_file__parse_buffer(
128 NULL
, NULL
, git_buf_cstr(&content
), *attrs_ptr
);
130 git_buf_free(&content
);
133 git_attr_file__free(*attrs_ptr
);
140 void git_attr_file__clear_rules(git_attr_file
*file
)
145 git_vector_foreach(&file
->rules
, i
, rule
)
146 git_attr_rule__free(rule
);
148 git_vector_free(&file
->rules
);
151 void git_attr_file__free(git_attr_file
*file
)
156 git_attr_file__clear_rules(file
);
158 if (file
->pool_is_allocated
) {
159 git_pool_clear(file
->pool
);
160 git__free(file
->pool
);
167 uint32_t git_attr_file__name_hash(const char *name
)
172 while ((c
= (int)*name
++) != 0)
173 h
= ((h
<< 5) + h
) + c
;
178 int git_attr_file__lookup_one(
180 const git_attr_path
*path
,
191 name
.name_hash
= git_attr_file__name_hash(attr
);
193 git_attr_file__foreach_matching_rule(file
, path
, i
, rule
) {
196 if (!git_vector_bsearch(&pos
, &rule
->assigns
, &name
)) {
197 *value
= ((git_attr_assignment
*)
198 git_vector_get(&rule
->assigns
, pos
))->value
;
207 bool git_attr_fnmatch__match(
208 git_attr_fnmatch
*match
,
209 const git_attr_path
*path
)
212 int icase_flags
= (match
->flags
& GIT_ATTR_FNMATCH_ICASE
) ? FNM_CASEFOLD
: 0;
214 if (match
->flags
& GIT_ATTR_FNMATCH_DIRECTORY
&& !path
->is_dir
)
217 if (match
->flags
& GIT_ATTR_FNMATCH_FULLPATH
)
218 fnm
= p_fnmatch(match
->pattern
, path
->path
, FNM_PATHNAME
| icase_flags
);
219 else if (path
->is_dir
)
220 fnm
= p_fnmatch(match
->pattern
, path
->basename
, FNM_LEADING_DIR
| icase_flags
);
222 fnm
= p_fnmatch(match
->pattern
, path
->basename
, icase_flags
);
224 return (fnm
== FNM_NOMATCH
) ? false : true;
227 bool git_attr_rule__match(
229 const git_attr_path
*path
)
231 bool matched
= git_attr_fnmatch__match(&rule
->match
, path
);
233 if (rule
->match
.flags
& GIT_ATTR_FNMATCH_NEGATIVE
)
240 git_attr_assignment
*git_attr_rule__lookup_assignment(
241 git_attr_rule
*rule
, const char *name
)
246 key
.name_hash
= git_attr_file__name_hash(name
);
248 if (git_vector_bsearch(&pos
, &rule
->assigns
, &key
))
251 return git_vector_get(&rule
->assigns
, pos
);
254 int git_attr_path__init(
255 git_attr_path
*info
, const char *path
, const char *base
)
259 /* build full path as best we can */
260 git_buf_init(&info
->full
, 0);
262 if (git_path_join_unrooted(&info
->full
, path
, base
, &root
) < 0)
265 info
->path
= info
->full
.ptr
+ root
;
267 /* remove trailing slashes */
268 while (info
->full
.size
> 0) {
269 if (info
->full
.ptr
[info
->full
.size
- 1] != '/')
273 info
->full
.ptr
[info
->full
.size
] = '\0';
275 /* skip leading slashes in path */
276 while (*info
->path
== '/')
279 /* find trailing basename component */
280 info
->basename
= strrchr(info
->path
, '/');
283 if (!info
->basename
|| !*info
->basename
)
284 info
->basename
= info
->path
;
286 info
->is_dir
= (int)git_path_isdir(info
->full
.ptr
);
291 void git_attr_path__free(git_attr_path
*info
)
293 git_buf_free(&info
->full
);
295 info
->basename
= NULL
;
300 * From gitattributes(5):
302 * Patterns have the following format:
304 * - A blank line matches no files, so it can serve as a separator for
307 * - A line starting with # serves as a comment.
309 * - An optional prefix ! which negates the pattern; any matching file
310 * excluded by a previous pattern will become included again. If a negated
311 * pattern matches, this will override lower precedence patterns sources.
313 * - If the pattern ends with a slash, it is removed for the purpose of the
314 * following description, but it would only find a match with a directory. In
315 * other words, foo/ will match a directory foo and paths underneath it, but
316 * will not match a regular file or a symbolic link foo (this is consistent
317 * with the way how pathspec works in general in git).
319 * - If the pattern does not contain a slash /, git treats it as a shell glob
320 * pattern and checks for a match against the pathname without leading
323 * - Otherwise, git treats the pattern as a shell glob suitable for consumption
324 * by fnmatch(3) with the FNM_PATHNAME flag: wildcards in the pattern will
325 * not match a / in the pathname. For example, "Documentation/\*.html" matches
326 * "Documentation/git.html" but not "Documentation/ppc/ppc.html". A leading
327 * slash matches the beginning of the pathname; for example, "/\*.c" matches
328 * "cat-file.c" but not "mozilla-sha1/sha1.c".
332 * This will return 0 if the spec was filled out,
333 * GIT_ENOTFOUND if the fnmatch does not require matching, or
334 * another error code there was an actual problem.
336 int git_attr_fnmatch__parse(
337 git_attr_fnmatch
*spec
,
342 const char *pattern
, *scan
;
343 int slash_count
, allow_space
;
345 assert(spec
&& base
&& *base
);
347 spec
->flags
= (spec
->flags
& GIT_ATTR_FNMATCH_ALLOWSPACE
);
348 allow_space
= (spec
->flags
!= 0);
352 while (git__isspace(*pattern
)) pattern
++;
353 if (!*pattern
|| *pattern
== '#') {
354 *base
= git__next_line(pattern
);
355 return GIT_ENOTFOUND
;
358 if (*pattern
== '[') {
359 if (strncmp(pattern
, "[attr]", 6) == 0) {
360 spec
->flags
= spec
->flags
| GIT_ATTR_FNMATCH_MACRO
;
363 /* else a character range like [a-e]* which is accepted */
366 if (*pattern
== '!') {
367 spec
->flags
= spec
->flags
| GIT_ATTR_FNMATCH_NEGATIVE
;
372 for (scan
= pattern
; *scan
!= '\0'; ++scan
) {
373 /* scan until (non-escaped) white space */
374 if (git__isspace(*scan
) && *(scan
- 1) != '\\') {
375 if (!allow_space
|| (*scan
!= ' ' && *scan
!= '\t'))
380 spec
->flags
= spec
->flags
| GIT_ATTR_FNMATCH_FULLPATH
;
385 /* remember if we see an unescaped wildcard in pattern */
386 else if (git__iswildcard(*scan
) &&
387 (scan
== pattern
|| (*(scan
- 1) != '\\')))
388 spec
->flags
= spec
->flags
| GIT_ATTR_FNMATCH_HASWILD
;
393 spec
->length
= scan
- pattern
;
395 if (pattern
[spec
->length
- 1] == '/') {
397 spec
->flags
= spec
->flags
| GIT_ATTR_FNMATCH_DIRECTORY
;
398 if (--slash_count
<= 0)
399 spec
->flags
= spec
->flags
& ~GIT_ATTR_FNMATCH_FULLPATH
;
402 if ((spec
->flags
& GIT_ATTR_FNMATCH_FULLPATH
) != 0 &&
403 source
!= NULL
&& git_path_root(pattern
) < 0)
405 size_t sourcelen
= strlen(source
);
406 /* given an unrooted fullpath match from a file inside a repo,
407 * prefix the pattern with the relative directory of the source file
409 spec
->pattern
= git_pool_malloc(
410 pool
, (uint32_t)(sourcelen
+ spec
->length
+ 1));
412 memcpy(spec
->pattern
, source
, sourcelen
);
413 memcpy(spec
->pattern
+ sourcelen
, pattern
, spec
->length
);
414 spec
->length
+= sourcelen
;
415 spec
->pattern
[spec
->length
] = '\0';
418 spec
->pattern
= git_pool_strndup(pool
, pattern
, spec
->length
);
421 if (!spec
->pattern
) {
422 *base
= git__next_line(pattern
);
425 /* strip '\' that might have be used for internal whitespace */
426 spec
->length
= git__unescape(spec
->pattern
);
432 static int sort_by_hash_and_name(const void *a_raw
, const void *b_raw
)
434 const git_attr_name
*a
= a_raw
;
435 const git_attr_name
*b
= b_raw
;
437 if (b
->name_hash
< a
->name_hash
)
439 else if (b
->name_hash
> a
->name_hash
)
442 return strcmp(b
->name
, a
->name
);
445 static void git_attr_assignment__free(git_attr_assignment
*assign
)
447 /* name and value are stored in a git_pool associated with the
448 * git_attr_file, so they do not need to be freed here
451 assign
->value
= NULL
;
455 static int merge_assignments(void **old_raw
, void *new_raw
)
457 git_attr_assignment
**old
= (git_attr_assignment
**)old_raw
;
458 git_attr_assignment
*new = (git_attr_assignment
*)new_raw
;
460 GIT_REFCOUNT_DEC(*old
, git_attr_assignment__free
);
465 int git_attr_assignment__parse(
466 git_repository
*repo
,
472 const char *scan
= *base
;
473 git_attr_assignment
*assign
= NULL
;
475 assert(assigns
&& !assigns
->length
);
477 assigns
->_cmp
= sort_by_hash_and_name
;
479 while (*scan
&& *scan
!= '\n') {
480 const char *name_start
, *value_start
;
482 /* skip leading blanks */
483 while (git__isspace(*scan
) && *scan
!= '\n') scan
++;
485 /* allocate assign if needed */
487 assign
= git__calloc(1, sizeof(git_attr_assignment
));
488 GITERR_CHECK_ALLOC(assign
);
489 GIT_REFCOUNT_INC(assign
);
492 assign
->name_hash
= 5381;
493 assign
->value
= git_attr__true
;
495 /* look for magic name prefixes */
497 assign
->value
= git_attr__false
;
499 } else if (*scan
== '!') {
500 assign
->value
= git_attr__unset
; /* explicit unspecified state */
502 } else if (*scan
== '#') /* comment rest of line */
507 while (*scan
&& !git__isspace(*scan
) && *scan
!= '=') {
509 ((assign
->name_hash
<< 5) + assign
->name_hash
) + *scan
;
512 if (scan
== name_start
) {
513 /* must have found lone prefix (" - ") or leading = ("=foo")
514 * or end of buffer -- advance until whitespace and continue
516 while (*scan
&& !git__isspace(*scan
)) scan
++;
520 /* allocate permanent storage for name */
521 assign
->name
= git_pool_strndup(pool
, name_start
, scan
- name_start
);
522 GITERR_CHECK_ALLOC(assign
->name
);
524 /* if there is an equals sign, find the value */
526 for (value_start
= ++scan
; *scan
&& !git__isspace(*scan
); ++scan
);
528 /* if we found a value, allocate permanent storage for it */
529 if (scan
> value_start
) {
530 assign
->value
= git_pool_strndup(pool
, value_start
, scan
- value_start
);
531 GITERR_CHECK_ALLOC(assign
->value
);
535 /* expand macros (if given a repo with a macro cache) */
536 if (repo
!= NULL
&& assign
->value
== git_attr__true
) {
537 git_attr_rule
*macro
=
538 git_attr_cache__lookup_macro(repo
, assign
->name
);
542 git_attr_assignment
*massign
;
544 git_vector_foreach(¯o
->assigns
, i
, massign
) {
545 GIT_REFCOUNT_INC(massign
);
547 error
= git_vector_insert_sorted(
548 assigns
, massign
, &merge_assignments
);
549 if (error
< 0 && error
!= GIT_EEXISTS
)
555 /* insert allocated assign into vector */
556 error
= git_vector_insert_sorted(assigns
, assign
, &merge_assignments
);
557 if (error
< 0 && error
!= GIT_EEXISTS
)
560 /* clear assign since it is now "owned" by the vector */
565 git_attr_assignment__free(assign
);
567 *base
= git__next_line(scan
);
569 return (assigns
->length
== 0) ? GIT_ENOTFOUND
: 0;
572 static void git_attr_rule__clear(git_attr_rule
*rule
)
575 git_attr_assignment
*assign
;
580 if (!(rule
->match
.flags
& GIT_ATTR_FNMATCH_IGNORE
)) {
581 git_vector_foreach(&rule
->assigns
, i
, assign
)
582 GIT_REFCOUNT_DEC(assign
, git_attr_assignment__free
);
583 git_vector_free(&rule
->assigns
);
586 /* match.pattern is stored in a git_pool, so no need to free */
587 rule
->match
.pattern
= NULL
;
588 rule
->match
.length
= 0;
591 void git_attr_rule__free(git_attr_rule
*rule
)
593 git_attr_rule__clear(rule
);