]> git.proxmox.com Git - libgit2.git/blob - src/attr_file.c
Merge pull request #2508 from libgit2/rb/fix-ignore-slash-star
[libgit2.git] / src / attr_file.c
1 #include "common.h"
2 #include "repository.h"
3 #include "filebuf.h"
4 #include "attr_file.h"
5 #include "attrcache.h"
6 #include "git2/blob.h"
7 #include "git2/tree.h"
8 #include "index.h"
9 #include <ctype.h>
10
11 static void attr_file_free(git_attr_file *file)
12 {
13 bool unlock = !git_mutex_lock(&file->lock);
14 git_attr_file__clear_rules(file, false);
15 git_pool_clear(&file->pool);
16 if (unlock)
17 git_mutex_unlock(&file->lock);
18 git_mutex_free(&file->lock);
19
20 git__memzero(file, sizeof(*file));
21 git__free(file);
22 }
23
24 int git_attr_file__new(
25 git_attr_file **out,
26 git_attr_file_entry *entry,
27 git_attr_file_source source)
28 {
29 git_attr_file *attrs = git__calloc(1, sizeof(git_attr_file));
30 GITERR_CHECK_ALLOC(attrs);
31
32 if (git_mutex_init(&attrs->lock) < 0) {
33 giterr_set(GITERR_OS, "Failed to initialize lock");
34 git__free(attrs);
35 return -1;
36 }
37
38 if (git_pool_init(&attrs->pool, 1, 0) < 0) {
39 attr_file_free(attrs);
40 return -1;
41 }
42
43 GIT_REFCOUNT_INC(attrs);
44 attrs->entry = entry;
45 attrs->source = source;
46 *out = attrs;
47 return 0;
48 }
49
50 int git_attr_file__clear_rules(git_attr_file *file, bool need_lock)
51 {
52 unsigned int i;
53 git_attr_rule *rule;
54
55 if (need_lock && git_mutex_lock(&file->lock) < 0) {
56 giterr_set(GITERR_OS, "Failed to lock attribute file");
57 return -1;
58 }
59
60 git_vector_foreach(&file->rules, i, rule)
61 git_attr_rule__free(rule);
62 git_vector_free(&file->rules);
63
64 if (need_lock)
65 git_mutex_unlock(&file->lock);
66
67 return 0;
68 }
69
70 void git_attr_file__free(git_attr_file *file)
71 {
72 if (!file)
73 return;
74 GIT_REFCOUNT_DEC(file, attr_file_free);
75 }
76
77 static int attr_file_oid_from_index(
78 git_oid *oid, git_repository *repo, const char *path)
79 {
80 int error;
81 git_index *idx;
82 size_t pos;
83 const git_index_entry *entry;
84
85 if ((error = git_repository_index__weakptr(&idx, repo)) < 0 ||
86 (error = git_index__find_pos(&pos, idx, path, 0, 0)) < 0)
87 return error;
88
89 if (!(entry = git_index_get_byindex(idx, pos)))
90 return GIT_ENOTFOUND;
91
92 *oid = entry->id;
93 return 0;
94 }
95
96 int git_attr_file__load(
97 git_attr_file **out,
98 git_repository *repo,
99 git_attr_file_entry *entry,
100 git_attr_file_source source,
101 git_attr_file_parser parser)
102 {
103 int error = 0;
104 git_blob *blob = NULL;
105 git_buf content = GIT_BUF_INIT;
106 const char *data = NULL;
107 git_attr_file *file;
108 struct stat st;
109
110 *out = NULL;
111
112 switch (source) {
113 case GIT_ATTR_FILE__IN_MEMORY:
114 /* in-memory attribute file doesn't need data */
115 break;
116 case GIT_ATTR_FILE__FROM_INDEX: {
117 git_oid id;
118
119 if ((error = attr_file_oid_from_index(&id, repo, entry->path)) < 0 ||
120 (error = git_blob_lookup(&blob, repo, &id)) < 0)
121 return error;
122
123 data = git_blob_rawcontent(blob);
124 break;
125 }
126 case GIT_ATTR_FILE__FROM_FILE: {
127 int fd;
128
129 if (p_stat(entry->fullpath, &st) < 0)
130 return git_path_set_error(errno, entry->fullpath, "stat");
131 if (S_ISDIR(st.st_mode))
132 return GIT_ENOTFOUND;
133
134 /* For open or read errors, return ENOTFOUND to skip item */
135 /* TODO: issue warning when warning API is available */
136
137 if ((fd = git_futils_open_ro(entry->fullpath)) < 0)
138 return GIT_ENOTFOUND;
139
140 error = git_futils_readbuffer_fd(&content, fd, (size_t)st.st_size);
141 p_close(fd);
142
143 if (error < 0)
144 return GIT_ENOTFOUND;
145
146 data = content.ptr;
147 break;
148 }
149 default:
150 giterr_set(GITERR_INVALID, "Unknown file source %d", source);
151 return -1;
152 }
153
154 if ((error = git_attr_file__new(&file, entry, source)) < 0)
155 goto cleanup;
156
157 if (parser && (error = parser(repo, file, data)) < 0) {
158 git_attr_file__free(file);
159 goto cleanup;
160 }
161
162 /* write cache breaker */
163 if (source == GIT_ATTR_FILE__FROM_INDEX)
164 git_oid_cpy(&file->cache_data.oid, git_blob_id(blob));
165 else if (source == GIT_ATTR_FILE__FROM_FILE)
166 git_futils_filestamp_set_from_stat(&file->cache_data.stamp, &st);
167 /* else always cacheable */
168
169 *out = file;
170
171 cleanup:
172 git_blob_free(blob);
173 git_buf_free(&content);
174
175 return error;
176 }
177
178 int git_attr_file__out_of_date(git_repository *repo, git_attr_file *file)
179 {
180 if (!file)
181 return 1;
182
183 switch (file->source) {
184 case GIT_ATTR_FILE__IN_MEMORY:
185 return 0;
186
187 case GIT_ATTR_FILE__FROM_FILE:
188 return git_futils_filestamp_check(
189 &file->cache_data.stamp, file->entry->fullpath);
190
191 case GIT_ATTR_FILE__FROM_INDEX: {
192 int error;
193 git_oid id;
194
195 if ((error = attr_file_oid_from_index(
196 &id, repo, file->entry->path)) < 0)
197 return error;
198
199 return (git_oid__cmp(&file->cache_data.oid, &id) != 0);
200 }
201
202 default:
203 giterr_set(GITERR_INVALID, "Invalid file type %d", file->source);
204 return -1;
205 }
206 }
207
208 static int sort_by_hash_and_name(const void *a_raw, const void *b_raw);
209 static void git_attr_rule__clear(git_attr_rule *rule);
210 static bool parse_optimized_patterns(
211 git_attr_fnmatch *spec,
212 git_pool *pool,
213 const char *pattern);
214
215 int git_attr_file__parse_buffer(
216 git_repository *repo, git_attr_file *attrs, const char *data)
217 {
218 int error = 0;
219 const char *scan = data, *context = NULL;
220 git_attr_rule *rule = NULL;
221
222 /* if subdir file path, convert context for file paths */
223 if (attrs->entry &&
224 git_path_root(attrs->entry->path) < 0 &&
225 !git__suffixcmp(attrs->entry->path, "/" GIT_ATTR_FILE))
226 context = attrs->entry->path;
227
228 if (git_mutex_lock(&attrs->lock) < 0) {
229 giterr_set(GITERR_OS, "Failed to lock attribute file");
230 return -1;
231 }
232
233 while (!error && *scan) {
234 /* allocate rule if needed */
235 if (!rule && !(rule = git__calloc(1, sizeof(*rule)))) {
236 error = -1;
237 break;
238 }
239
240 rule->match.flags =
241 GIT_ATTR_FNMATCH_ALLOWNEG | GIT_ATTR_FNMATCH_ALLOWMACRO;
242
243 /* parse the next "pattern attr attr attr" line */
244 if (!(error = git_attr_fnmatch__parse(
245 &rule->match, &attrs->pool, context, &scan)) &&
246 !(error = git_attr_assignment__parse(
247 repo, &attrs->pool, &rule->assigns, &scan)))
248 {
249 if (rule->match.flags & GIT_ATTR_FNMATCH_MACRO)
250 /* TODO: warning if macro found in file below repo root */
251 error = git_attr_cache__insert_macro(repo, rule);
252 else
253 error = git_vector_insert(&attrs->rules, rule);
254 }
255
256 /* if the rule wasn't a pattern, on to the next */
257 if (error < 0) {
258 git_attr_rule__clear(rule); /* reset rule contents */
259 if (error == GIT_ENOTFOUND)
260 error = 0;
261 } else {
262 rule = NULL; /* vector now "owns" the rule */
263 }
264 }
265
266 git_mutex_unlock(&attrs->lock);
267 git_attr_rule__free(rule);
268
269 return error;
270 }
271
272 uint32_t git_attr_file__name_hash(const char *name)
273 {
274 uint32_t h = 5381;
275 int c;
276 assert(name);
277 while ((c = (int)*name++) != 0)
278 h = ((h << 5) + h) + c;
279 return h;
280 }
281
282 int git_attr_file__lookup_one(
283 git_attr_file *file,
284 git_attr_path *path,
285 const char *attr,
286 const char **value)
287 {
288 size_t i;
289 git_attr_name name;
290 git_attr_rule *rule;
291
292 *value = NULL;
293
294 name.name = attr;
295 name.name_hash = git_attr_file__name_hash(attr);
296
297 git_attr_file__foreach_matching_rule(file, path, i, rule) {
298 size_t pos;
299
300 if (!git_vector_bsearch(&pos, &rule->assigns, &name)) {
301 *value = ((git_attr_assignment *)
302 git_vector_get(&rule->assigns, pos))->value;
303 break;
304 }
305 }
306
307 return 0;
308 }
309
310 int git_attr_file__load_standalone(git_attr_file **out, const char *path)
311 {
312 int error;
313 git_attr_file *file;
314 git_buf content = GIT_BUF_INIT;
315
316 error = git_attr_file__new(&file, NULL, GIT_ATTR_FILE__FROM_FILE);
317 if (error < 0)
318 return error;
319
320 error = git_attr_cache__alloc_file_entry(
321 &file->entry, NULL, path, &file->pool);
322 if (error < 0) {
323 git_attr_file__free(file);
324 return error;
325 }
326 /* because the cache entry is allocated from the file's own pool, we
327 * don't have to free it - freeing file+pool will free cache entry, too.
328 */
329
330 if (!(error = git_futils_readbuffer(&content, path))) {
331 error = git_attr_file__parse_buffer(NULL, file, content.ptr);
332 git_buf_free(&content);
333 }
334
335 if (error < 0)
336 git_attr_file__free(file);
337 else
338 *out = file;
339
340 return error;
341 }
342
343 bool git_attr_fnmatch__match(
344 git_attr_fnmatch *match,
345 git_attr_path *path)
346 {
347 const char *filename;
348 int flags = 0;
349
350 if (match->flags & GIT_ATTR_FNMATCH_ICASE)
351 flags |= FNM_CASEFOLD;
352 if (match->flags & GIT_ATTR_FNMATCH_LEADINGDIR)
353 flags |= FNM_LEADING_DIR;
354
355 if (match->flags & GIT_ATTR_FNMATCH_FULLPATH) {
356 filename = path->path;
357 flags |= FNM_PATHNAME;
358 } else {
359 filename = path->basename;
360
361 if (path->is_dir)
362 flags |= FNM_LEADING_DIR;
363 }
364
365 if ((match->flags & GIT_ATTR_FNMATCH_DIRECTORY) && !path->is_dir) {
366 int matchval;
367
368 /* for attribute checks or root ignore checks, fail match */
369 if (!(match->flags & GIT_ATTR_FNMATCH_IGNORE) ||
370 path->basename == path->path)
371 return false;
372
373 /* for ignore checks, use container of current item for check */
374 path->basename[-1] = '\0';
375 flags |= FNM_LEADING_DIR;
376 matchval = p_fnmatch(match->pattern, path->path, flags);
377 path->basename[-1] = '/';
378 return (matchval != FNM_NOMATCH);
379 }
380
381 /* if path is a directory prefix of a negated pattern, then match */
382 if ((match->flags & GIT_ATTR_FNMATCH_NEGATIVE) && path->is_dir) {
383 size_t pathlen = strlen(path->path);
384 bool prefixed = (pathlen <= match->length) &&
385 ((match->flags & GIT_ATTR_FNMATCH_ICASE) ?
386 !strncasecmp(match->pattern, path->path, pathlen) :
387 !strncmp(match->pattern, path->path, pathlen));
388
389 if (prefixed && git_path_at_end_of_segment(&match->pattern[pathlen]))
390 return true;
391 }
392
393 return (p_fnmatch(match->pattern, filename, flags) != FNM_NOMATCH);
394 }
395
396 bool git_attr_rule__match(
397 git_attr_rule *rule,
398 git_attr_path *path)
399 {
400 bool matched = git_attr_fnmatch__match(&rule->match, path);
401
402 if (rule->match.flags & GIT_ATTR_FNMATCH_NEGATIVE)
403 matched = !matched;
404
405 return matched;
406 }
407
408 git_attr_assignment *git_attr_rule__lookup_assignment(
409 git_attr_rule *rule, const char *name)
410 {
411 size_t pos;
412 git_attr_name key;
413 key.name = name;
414 key.name_hash = git_attr_file__name_hash(name);
415
416 if (git_vector_bsearch(&pos, &rule->assigns, &key))
417 return NULL;
418
419 return git_vector_get(&rule->assigns, pos);
420 }
421
422 int git_attr_path__init(
423 git_attr_path *info, const char *path, const char *base)
424 {
425 ssize_t root;
426
427 /* build full path as best we can */
428 git_buf_init(&info->full, 0);
429
430 if (git_path_join_unrooted(&info->full, path, base, &root) < 0)
431 return -1;
432
433 info->path = info->full.ptr + root;
434
435 /* remove trailing slashes */
436 while (info->full.size > 0) {
437 if (info->full.ptr[info->full.size - 1] != '/')
438 break;
439 info->full.size--;
440 }
441 info->full.ptr[info->full.size] = '\0';
442
443 /* skip leading slashes in path */
444 while (*info->path == '/')
445 info->path++;
446
447 /* find trailing basename component */
448 info->basename = strrchr(info->path, '/');
449 if (info->basename)
450 info->basename++;
451 if (!info->basename || !*info->basename)
452 info->basename = info->path;
453
454 info->is_dir = (int)git_path_isdir(info->full.ptr);
455
456 return 0;
457 }
458
459 void git_attr_path__free(git_attr_path *info)
460 {
461 git_buf_free(&info->full);
462 info->path = NULL;
463 info->basename = NULL;
464 }
465
466 /*
467 * From gitattributes(5):
468 *
469 * Patterns have the following format:
470 *
471 * - A blank line matches no files, so it can serve as a separator for
472 * readability.
473 *
474 * - A line starting with # serves as a comment.
475 *
476 * - An optional prefix ! which negates the pattern; any matching file
477 * excluded by a previous pattern will become included again. If a negated
478 * pattern matches, this will override lower precedence patterns sources.
479 *
480 * - If the pattern ends with a slash, it is removed for the purpose of the
481 * following description, but it would only find a match with a directory. In
482 * other words, foo/ will match a directory foo and paths underneath it, but
483 * will not match a regular file or a symbolic link foo (this is consistent
484 * with the way how pathspec works in general in git).
485 *
486 * - If the pattern does not contain a slash /, git treats it as a shell glob
487 * pattern and checks for a match against the pathname without leading
488 * directories.
489 *
490 * - Otherwise, git treats the pattern as a shell glob suitable for consumption
491 * by fnmatch(3) with the FNM_PATHNAME flag: wildcards in the pattern will
492 * not match a / in the pathname. For example, "Documentation/\*.html" matches
493 * "Documentation/git.html" but not "Documentation/ppc/ppc.html". A leading
494 * slash matches the beginning of the pathname; for example, "/\*.c" matches
495 * "cat-file.c" but not "mozilla-sha1/sha1.c".
496 */
497
498 /*
499 * This will return 0 if the spec was filled out,
500 * GIT_ENOTFOUND if the fnmatch does not require matching, or
501 * another error code there was an actual problem.
502 */
503 int git_attr_fnmatch__parse(
504 git_attr_fnmatch *spec,
505 git_pool *pool,
506 const char *context,
507 const char **base)
508 {
509 const char *pattern, *scan;
510 int slash_count, allow_space;
511
512 assert(spec && base && *base);
513
514 if (parse_optimized_patterns(spec, pool, *base))
515 return 0;
516
517 spec->flags = (spec->flags & GIT_ATTR_FNMATCH__INCOMING);
518 allow_space = ((spec->flags & GIT_ATTR_FNMATCH_ALLOWSPACE) != 0);
519
520 pattern = *base;
521
522 while (git__isspace(*pattern)) pattern++;
523 if (!*pattern || *pattern == '#') {
524 *base = git__next_line(pattern);
525 return GIT_ENOTFOUND;
526 }
527
528 if (*pattern == '[' && (spec->flags & GIT_ATTR_FNMATCH_ALLOWMACRO) != 0) {
529 if (strncmp(pattern, "[attr]", 6) == 0) {
530 spec->flags = spec->flags | GIT_ATTR_FNMATCH_MACRO;
531 pattern += 6;
532 }
533 /* else a character range like [a-e]* which is accepted */
534 }
535
536 if (*pattern == '!' && (spec->flags & GIT_ATTR_FNMATCH_ALLOWNEG) != 0) {
537 spec->flags = spec->flags |
538 GIT_ATTR_FNMATCH_NEGATIVE | GIT_ATTR_FNMATCH_LEADINGDIR;
539 pattern++;
540 }
541
542 slash_count = 0;
543 for (scan = pattern; *scan != '\0'; ++scan) {
544 /* scan until (non-escaped) white space */
545 if (git__isspace(*scan) && *(scan - 1) != '\\') {
546 if (!allow_space || (*scan != ' ' && *scan != '\t'))
547 break;
548 }
549
550 if (*scan == '/') {
551 spec->flags = spec->flags | GIT_ATTR_FNMATCH_FULLPATH;
552 slash_count++;
553 if (pattern == scan)
554 pattern++;
555 }
556 /* remember if we see an unescaped wildcard in pattern */
557 else if (git__iswildcard(*scan) &&
558 (scan == pattern || (*(scan - 1) != '\\')))
559 spec->flags = spec->flags | GIT_ATTR_FNMATCH_HASWILD;
560 }
561
562 *base = scan;
563
564 if ((spec->length = scan - pattern) == 0)
565 return GIT_ENOTFOUND;
566
567 if (pattern[spec->length - 1] == '/') {
568 spec->length--;
569 spec->flags = spec->flags | GIT_ATTR_FNMATCH_DIRECTORY;
570 if (--slash_count <= 0)
571 spec->flags = spec->flags & ~GIT_ATTR_FNMATCH_FULLPATH;
572 }
573 if ((spec->flags & GIT_ATTR_FNMATCH_NOLEADINGDIR) == 0 &&
574 spec->length >= 2 &&
575 pattern[spec->length - 1] == '*' &&
576 pattern[spec->length - 2] == '/') {
577 spec->length -= 2;
578 spec->flags = spec->flags | GIT_ATTR_FNMATCH_LEADINGDIR;
579 /* leave FULLPATH match on, however */
580 }
581
582 if ((spec->flags & GIT_ATTR_FNMATCH_FULLPATH) != 0 &&
583 context != NULL && git_path_root(pattern) < 0)
584 {
585 /* use context path minus the trailing filename */
586 char *slash = strrchr(context, '/');
587 size_t contextlen = slash ? slash - context + 1 : 0;
588
589 /* given an unrooted fullpath match from a file inside a repo,
590 * prefix the pattern with the relative directory of the source file
591 */
592 spec->pattern = git_pool_malloc(
593 pool, (uint32_t)(contextlen + spec->length + 1));
594 if (spec->pattern) {
595 memcpy(spec->pattern, context, contextlen);
596 memcpy(spec->pattern + contextlen, pattern, spec->length);
597 spec->length += contextlen;
598 spec->pattern[spec->length] = '\0';
599 }
600 } else {
601 spec->pattern = git_pool_strndup(pool, pattern, spec->length);
602 }
603
604 if (!spec->pattern) {
605 *base = git__next_line(pattern);
606 return -1;
607 } else {
608 /* strip '\' that might have be used for internal whitespace */
609 spec->length = git__unescape(spec->pattern);
610 /* TODO: convert remaining '\' into '/' for POSIX ??? */
611 }
612
613 return 0;
614 }
615
616 static bool parse_optimized_patterns(
617 git_attr_fnmatch *spec,
618 git_pool *pool,
619 const char *pattern)
620 {
621 if (!pattern[1] && (pattern[0] == '*' || pattern[0] == '.')) {
622 spec->flags = GIT_ATTR_FNMATCH_MATCH_ALL;
623 spec->pattern = git_pool_strndup(pool, pattern, 1);
624 spec->length = 1;
625
626 return true;
627 }
628
629 return false;
630 }
631
632 static int sort_by_hash_and_name(const void *a_raw, const void *b_raw)
633 {
634 const git_attr_name *a = a_raw;
635 const git_attr_name *b = b_raw;
636
637 if (b->name_hash < a->name_hash)
638 return 1;
639 else if (b->name_hash > a->name_hash)
640 return -1;
641 else
642 return strcmp(b->name, a->name);
643 }
644
645 static void git_attr_assignment__free(git_attr_assignment *assign)
646 {
647 /* name and value are stored in a git_pool associated with the
648 * git_attr_file, so they do not need to be freed here
649 */
650 assign->name = NULL;
651 assign->value = NULL;
652 git__free(assign);
653 }
654
655 static int merge_assignments(void **old_raw, void *new_raw)
656 {
657 git_attr_assignment **old = (git_attr_assignment **)old_raw;
658 git_attr_assignment *new = (git_attr_assignment *)new_raw;
659
660 GIT_REFCOUNT_DEC(*old, git_attr_assignment__free);
661 *old = new;
662 return GIT_EEXISTS;
663 }
664
665 int git_attr_assignment__parse(
666 git_repository *repo,
667 git_pool *pool,
668 git_vector *assigns,
669 const char **base)
670 {
671 int error;
672 const char *scan = *base;
673 git_attr_assignment *assign = NULL;
674
675 assert(assigns && !assigns->length);
676
677 git_vector_set_cmp(assigns, sort_by_hash_and_name);
678
679 while (*scan && *scan != '\n') {
680 const char *name_start, *value_start;
681
682 /* skip leading blanks */
683 while (git__isspace(*scan) && *scan != '\n') scan++;
684
685 /* allocate assign if needed */
686 if (!assign) {
687 assign = git__calloc(1, sizeof(git_attr_assignment));
688 GITERR_CHECK_ALLOC(assign);
689 GIT_REFCOUNT_INC(assign);
690 }
691
692 assign->name_hash = 5381;
693 assign->value = git_attr__true;
694
695 /* look for magic name prefixes */
696 if (*scan == '-') {
697 assign->value = git_attr__false;
698 scan++;
699 } else if (*scan == '!') {
700 assign->value = git_attr__unset; /* explicit unspecified state */
701 scan++;
702 } else if (*scan == '#') /* comment rest of line */
703 break;
704
705 /* find the name */
706 name_start = scan;
707 while (*scan && !git__isspace(*scan) && *scan != '=') {
708 assign->name_hash =
709 ((assign->name_hash << 5) + assign->name_hash) + *scan;
710 scan++;
711 }
712 if (scan == name_start) {
713 /* must have found lone prefix (" - ") or leading = ("=foo")
714 * or end of buffer -- advance until whitespace and continue
715 */
716 while (*scan && !git__isspace(*scan)) scan++;
717 continue;
718 }
719
720 /* allocate permanent storage for name */
721 assign->name = git_pool_strndup(pool, name_start, scan - name_start);
722 GITERR_CHECK_ALLOC(assign->name);
723
724 /* if there is an equals sign, find the value */
725 if (*scan == '=') {
726 for (value_start = ++scan; *scan && !git__isspace(*scan); ++scan);
727
728 /* if we found a value, allocate permanent storage for it */
729 if (scan > value_start) {
730 assign->value = git_pool_strndup(pool, value_start, scan - value_start);
731 GITERR_CHECK_ALLOC(assign->value);
732 }
733 }
734
735 /* expand macros (if given a repo with a macro cache) */
736 if (repo != NULL && assign->value == git_attr__true) {
737 git_attr_rule *macro =
738 git_attr_cache__lookup_macro(repo, assign->name);
739
740 if (macro != NULL) {
741 unsigned int i;
742 git_attr_assignment *massign;
743
744 git_vector_foreach(&macro->assigns, i, massign) {
745 GIT_REFCOUNT_INC(massign);
746
747 error = git_vector_insert_sorted(
748 assigns, massign, &merge_assignments);
749 if (error < 0 && error != GIT_EEXISTS)
750 return error;
751 }
752 }
753 }
754
755 /* insert allocated assign into vector */
756 error = git_vector_insert_sorted(assigns, assign, &merge_assignments);
757 if (error < 0 && error != GIT_EEXISTS)
758 return error;
759
760 /* clear assign since it is now "owned" by the vector */
761 assign = NULL;
762 }
763
764 if (assign != NULL)
765 git_attr_assignment__free(assign);
766
767 *base = git__next_line(scan);
768
769 return (assigns->length == 0) ? GIT_ENOTFOUND : 0;
770 }
771
772 static void git_attr_rule__clear(git_attr_rule *rule)
773 {
774 unsigned int i;
775 git_attr_assignment *assign;
776
777 if (!rule)
778 return;
779
780 if (!(rule->match.flags & GIT_ATTR_FNMATCH_IGNORE)) {
781 git_vector_foreach(&rule->assigns, i, assign)
782 GIT_REFCOUNT_DEC(assign, git_attr_assignment__free);
783 git_vector_free(&rule->assigns);
784 }
785
786 /* match.pattern is stored in a git_pool, so no need to free */
787 rule->match.pattern = NULL;
788 rule->match.length = 0;
789 }
790
791 void git_attr_rule__free(git_attr_rule *rule)
792 {
793 git_attr_rule__clear(rule);
794 git__free(rule);
795 }
796