]> git.proxmox.com Git - libgit2.git/blob - src/attr_file.c
attr_session: keep a temp buffer
[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_session *attr_session,
100 git_attr_file_entry *entry,
101 git_attr_file_source source,
102 git_attr_file_parser parser)
103 {
104 int error = 0;
105 git_blob *blob = NULL;
106 git_buf content = GIT_BUF_INIT;
107 git_attr_file *file;
108 struct stat st;
109 bool nonexistent = false;
110
111 *out = NULL;
112
113 switch (source) {
114 case GIT_ATTR_FILE__IN_MEMORY:
115 /* in-memory attribute file doesn't need data */
116 break;
117 case GIT_ATTR_FILE__FROM_INDEX: {
118 git_oid id;
119
120 if ((error = attr_file_oid_from_index(&id, repo, entry->path)) < 0 ||
121 (error = git_blob_lookup(&blob, repo, &id)) < 0)
122 return error;
123
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));
127 break;
128 }
129 case GIT_ATTR_FILE__FROM_FILE: {
130 int fd;
131
132 /* For open or read errors, pretend that we got ENOTFOUND. */
133 /* TODO: issue warning when warning API is available */
134
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)
139 nonexistent = true;
140 else
141 p_close(fd);
142
143 break;
144 }
145 default:
146 giterr_set(GITERR_INVALID, "Unknown file source %d", source);
147 return -1;
148 }
149
150 if ((error = git_attr_file__new(&file, entry, source)) < 0)
151 goto cleanup;
152
153 /* store the key of the attr_reader; don't bother with cache
154 * invalidation during the same attr reader session.
155 */
156 if (attr_session)
157 file->session_key = attr_session->key;
158
159 if (parser && (error = parser(repo, file, git_buf_cstr(&content))) < 0) {
160 git_attr_file__free(file);
161 goto cleanup;
162 }
163
164 /* write cache breakers */
165 if (nonexistent)
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 */
172
173 *out = file;
174
175 cleanup:
176 git_blob_free(blob);
177 git_buf_free(&content);
178
179 return error;
180 }
181
182 int git_attr_file__out_of_date(
183 git_repository *repo,
184 git_attr_session *attr_session,
185 git_attr_file *file)
186 {
187 if (!file)
188 return 1;
189
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
192 */
193 if (attr_session && attr_session->key == file->session_key)
194 return 0;
195 else if (file->nonexistent)
196 return 1;
197
198 switch (file->source) {
199 case GIT_ATTR_FILE__IN_MEMORY:
200 return 0;
201
202 case GIT_ATTR_FILE__FROM_FILE:
203 return git_futils_filestamp_check(
204 &file->cache_data.stamp, file->entry->fullpath);
205
206 case GIT_ATTR_FILE__FROM_INDEX: {
207 int error;
208 git_oid id;
209
210 if ((error = attr_file_oid_from_index(
211 &id, repo, file->entry->path)) < 0)
212 return error;
213
214 return (git_oid__cmp(&file->cache_data.oid, &id) != 0);
215 }
216
217 default:
218 giterr_set(GITERR_INVALID, "Invalid file type %d", file->source);
219 return -1;
220 }
221 }
222
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,
227 git_pool *pool,
228 const char *pattern);
229
230 int git_attr_file__parse_buffer(
231 git_repository *repo, git_attr_file *attrs, const char *data)
232 {
233 int error = 0;
234 const char *scan = data, *context = NULL;
235 git_attr_rule *rule = NULL;
236
237 /* if subdir file path, convert context for file paths */
238 if (attrs->entry &&
239 git_path_root(attrs->entry->path) < 0 &&
240 !git__suffixcmp(attrs->entry->path, "/" GIT_ATTR_FILE))
241 context = attrs->entry->path;
242
243 if (git_mutex_lock(&attrs->lock) < 0) {
244 giterr_set(GITERR_OS, "Failed to lock attribute file");
245 return -1;
246 }
247
248 while (!error && *scan) {
249 /* allocate rule if needed */
250 if (!rule && !(rule = git__calloc(1, sizeof(*rule)))) {
251 error = -1;
252 break;
253 }
254
255 rule->match.flags =
256 GIT_ATTR_FNMATCH_ALLOWNEG | GIT_ATTR_FNMATCH_ALLOWMACRO;
257
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)))
263 {
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);
267 else
268 error = git_vector_insert(&attrs->rules, rule);
269 }
270
271 /* if the rule wasn't a pattern, on to the next */
272 if (error < 0) {
273 git_attr_rule__clear(rule); /* reset rule contents */
274 if (error == GIT_ENOTFOUND)
275 error = 0;
276 } else {
277 rule = NULL; /* vector now "owns" the rule */
278 }
279 }
280
281 git_mutex_unlock(&attrs->lock);
282 git_attr_rule__free(rule);
283
284 return error;
285 }
286
287 uint32_t git_attr_file__name_hash(const char *name)
288 {
289 uint32_t h = 5381;
290 int c;
291 assert(name);
292 while ((c = (int)*name++) != 0)
293 h = ((h << 5) + h) + c;
294 return h;
295 }
296
297 int git_attr_file__lookup_one(
298 git_attr_file *file,
299 git_attr_path *path,
300 const char *attr,
301 const char **value)
302 {
303 size_t i;
304 git_attr_name name;
305 git_attr_rule *rule;
306
307 *value = NULL;
308
309 name.name = attr;
310 name.name_hash = git_attr_file__name_hash(attr);
311
312 git_attr_file__foreach_matching_rule(file, path, i, rule) {
313 size_t pos;
314
315 if (!git_vector_bsearch(&pos, &rule->assigns, &name)) {
316 *value = ((git_attr_assignment *)
317 git_vector_get(&rule->assigns, pos))->value;
318 break;
319 }
320 }
321
322 return 0;
323 }
324
325 int git_attr_file__load_standalone(git_attr_file **out, const char *path)
326 {
327 int error;
328 git_attr_file *file;
329 git_buf content = GIT_BUF_INIT;
330
331 error = git_attr_file__new(&file, NULL, GIT_ATTR_FILE__FROM_FILE);
332 if (error < 0)
333 return error;
334
335 error = git_attr_cache__alloc_file_entry(
336 &file->entry, NULL, path, &file->pool);
337 if (error < 0) {
338 git_attr_file__free(file);
339 return error;
340 }
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.
343 */
344
345 if (!(error = git_futils_readbuffer(&content, path))) {
346 error = git_attr_file__parse_buffer(NULL, file, content.ptr);
347 git_buf_free(&content);
348 }
349
350 if (error < 0)
351 git_attr_file__free(file);
352 else
353 *out = file;
354
355 return error;
356 }
357
358 bool git_attr_fnmatch__match(
359 git_attr_fnmatch *match,
360 git_attr_path *path)
361 {
362 const char *filename;
363 int flags = 0;
364
365 /*
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.
369 */
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))
373 return 0;
374 } else {
375 if (git__prefixcmp(path->path, match->containing_dir))
376 return 0;
377 }
378 }
379
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;
384
385 if (match->flags & GIT_ATTR_FNMATCH_FULLPATH) {
386 filename = path->path;
387 flags |= FNM_PATHNAME;
388 } else {
389 filename = path->basename;
390
391 if (path->is_dir)
392 flags |= FNM_LEADING_DIR;
393 }
394
395 if ((match->flags & GIT_ATTR_FNMATCH_DIRECTORY) && !path->is_dir) {
396 int matchval;
397
398 /* for attribute checks or root ignore checks, fail match */
399 if (!(match->flags & GIT_ATTR_FNMATCH_IGNORE) ||
400 path->basename == path->path)
401 return false;
402
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);
409 }
410
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));
418
419 if (prefixed && git_path_at_end_of_segment(&match->pattern[pathlen]))
420 return true;
421 }
422
423 return (p_fnmatch(match->pattern, filename, flags) != FNM_NOMATCH);
424 }
425
426 bool git_attr_rule__match(
427 git_attr_rule *rule,
428 git_attr_path *path)
429 {
430 bool matched = git_attr_fnmatch__match(&rule->match, path);
431
432 if (rule->match.flags & GIT_ATTR_FNMATCH_NEGATIVE)
433 matched = !matched;
434
435 return matched;
436 }
437
438 git_attr_assignment *git_attr_rule__lookup_assignment(
439 git_attr_rule *rule, const char *name)
440 {
441 size_t pos;
442 git_attr_name key;
443 key.name = name;
444 key.name_hash = git_attr_file__name_hash(name);
445
446 if (git_vector_bsearch(&pos, &rule->assigns, &key))
447 return NULL;
448
449 return git_vector_get(&rule->assigns, pos);
450 }
451
452 int git_attr_path__init(
453 git_attr_path *info, const char *path, const char *base)
454 {
455 ssize_t root;
456
457 /* build full path as best we can */
458 git_buf_init(&info->full, 0);
459
460 if (git_path_join_unrooted(&info->full, path, base, &root) < 0)
461 return -1;
462
463 info->path = info->full.ptr + root;
464
465 /* remove trailing slashes */
466 while (info->full.size > 0) {
467 if (info->full.ptr[info->full.size - 1] != '/')
468 break;
469 info->full.size--;
470 }
471 info->full.ptr[info->full.size] = '\0';
472
473 /* skip leading slashes in path */
474 while (*info->path == '/')
475 info->path++;
476
477 /* find trailing basename component */
478 info->basename = strrchr(info->path, '/');
479 if (info->basename)
480 info->basename++;
481 if (!info->basename || !*info->basename)
482 info->basename = info->path;
483
484 info->is_dir = (int)git_path_isdir(info->full.ptr);
485
486 return 0;
487 }
488
489 void git_attr_path__free(git_attr_path *info)
490 {
491 git_buf_free(&info->full);
492 info->path = NULL;
493 info->basename = NULL;
494 }
495
496 /*
497 * From gitattributes(5):
498 *
499 * Patterns have the following format:
500 *
501 * - A blank line matches no files, so it can serve as a separator for
502 * readability.
503 *
504 * - A line starting with # serves as a comment.
505 *
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.
509 *
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).
515 *
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
518 * directories.
519 *
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".
526 */
527
528 /*
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.
532 */
533 int git_attr_fnmatch__parse(
534 git_attr_fnmatch *spec,
535 git_pool *pool,
536 const char *context,
537 const char **base)
538 {
539 const char *pattern, *scan;
540 int slash_count, allow_space;
541
542 assert(spec && base && *base);
543
544 if (parse_optimized_patterns(spec, pool, *base))
545 return 0;
546
547 spec->flags = (spec->flags & GIT_ATTR_FNMATCH__INCOMING);
548 allow_space = ((spec->flags & GIT_ATTR_FNMATCH_ALLOWSPACE) != 0);
549
550 pattern = *base;
551
552 while (git__isspace(*pattern)) pattern++;
553 if (!*pattern || *pattern == '#') {
554 *base = git__next_line(pattern);
555 return GIT_ENOTFOUND;
556 }
557
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;
561 pattern += 6;
562 }
563 /* else a character range like [a-e]* which is accepted */
564 }
565
566 if (*pattern == '!' && (spec->flags & GIT_ATTR_FNMATCH_ALLOWNEG) != 0) {
567 spec->flags = spec->flags |
568 GIT_ATTR_FNMATCH_NEGATIVE | GIT_ATTR_FNMATCH_LEADINGDIR;
569 pattern++;
570 }
571
572 slash_count = 0;
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'))
577 break;
578 }
579
580 if (*scan == '/') {
581 spec->flags = spec->flags | GIT_ATTR_FNMATCH_FULLPATH;
582 slash_count++;
583 if (pattern == scan)
584 pattern++;
585 }
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;
590 }
591
592 *base = scan;
593
594 if ((spec->length = scan - pattern) == 0)
595 return GIT_ENOTFOUND;
596
597 /*
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.
601 */
602 if (pattern[spec->length - 1] == '\r')
603 if (--spec->length == 0)
604 return GIT_ENOTFOUND;
605
606 if (pattern[spec->length - 1] == '/') {
607 spec->length--;
608 spec->flags = spec->flags | GIT_ATTR_FNMATCH_DIRECTORY;
609 if (--slash_count <= 0)
610 spec->flags = spec->flags & ~GIT_ATTR_FNMATCH_FULLPATH;
611 }
612 if ((spec->flags & GIT_ATTR_FNMATCH_NOLEADINGDIR) == 0 &&
613 spec->length >= 2 &&
614 pattern[spec->length - 1] == '*' &&
615 pattern[spec->length - 2] == '/') {
616 spec->length -= 2;
617 spec->flags = spec->flags | GIT_ATTR_FNMATCH_LEADINGDIR;
618 /* leave FULLPATH match on, however */
619 }
620
621 if (context) {
622 char *slash = strchr(context, '/');
623 size_t len;
624 if (slash) {
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;
629 }
630 }
631
632 if ((spec->flags & GIT_ATTR_FNMATCH_FULLPATH) != 0 &&
633 context != NULL && git_path_root(pattern) < 0)
634 {
635 /* use context path minus the trailing filename */
636 char *slash = strrchr(context, '/');
637 size_t contextlen = slash ? slash - context + 1 : 0;
638
639 /* given an unrooted fullpath match from a file inside a repo,
640 * prefix the pattern with the relative directory of the source file
641 */
642 spec->pattern = git_pool_malloc(
643 pool, (uint32_t)(contextlen + spec->length + 1));
644 if (spec->pattern) {
645 memcpy(spec->pattern, context, contextlen);
646 memcpy(spec->pattern + contextlen, pattern, spec->length);
647 spec->length += contextlen;
648 spec->pattern[spec->length] = '\0';
649 }
650 } else {
651 spec->pattern = git_pool_strndup(pool, pattern, spec->length);
652 }
653
654 if (!spec->pattern) {
655 *base = git__next_line(pattern);
656 return -1;
657 } else {
658 /* strip '\' that might have be used for internal whitespace */
659 spec->length = git__unescape(spec->pattern);
660 /* TODO: convert remaining '\' into '/' for POSIX ??? */
661 }
662
663 return 0;
664 }
665
666 static bool parse_optimized_patterns(
667 git_attr_fnmatch *spec,
668 git_pool *pool,
669 const char *pattern)
670 {
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);
674 spec->length = 1;
675
676 return true;
677 }
678
679 return false;
680 }
681
682 static int sort_by_hash_and_name(const void *a_raw, const void *b_raw)
683 {
684 const git_attr_name *a = a_raw;
685 const git_attr_name *b = b_raw;
686
687 if (b->name_hash < a->name_hash)
688 return 1;
689 else if (b->name_hash > a->name_hash)
690 return -1;
691 else
692 return strcmp(b->name, a->name);
693 }
694
695 static void git_attr_assignment__free(git_attr_assignment *assign)
696 {
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
699 */
700 assign->name = NULL;
701 assign->value = NULL;
702 git__free(assign);
703 }
704
705 static int merge_assignments(void **old_raw, void *new_raw)
706 {
707 git_attr_assignment **old = (git_attr_assignment **)old_raw;
708 git_attr_assignment *new = (git_attr_assignment *)new_raw;
709
710 GIT_REFCOUNT_DEC(*old, git_attr_assignment__free);
711 *old = new;
712 return GIT_EEXISTS;
713 }
714
715 int git_attr_assignment__parse(
716 git_repository *repo,
717 git_pool *pool,
718 git_vector *assigns,
719 const char **base)
720 {
721 int error;
722 const char *scan = *base;
723 git_attr_assignment *assign = NULL;
724
725 assert(assigns && !assigns->length);
726
727 git_vector_set_cmp(assigns, sort_by_hash_and_name);
728
729 while (*scan && *scan != '\n') {
730 const char *name_start, *value_start;
731
732 /* skip leading blanks */
733 while (git__isspace(*scan) && *scan != '\n') scan++;
734
735 /* allocate assign if needed */
736 if (!assign) {
737 assign = git__calloc(1, sizeof(git_attr_assignment));
738 GITERR_CHECK_ALLOC(assign);
739 GIT_REFCOUNT_INC(assign);
740 }
741
742 assign->name_hash = 5381;
743 assign->value = git_attr__true;
744
745 /* look for magic name prefixes */
746 if (*scan == '-') {
747 assign->value = git_attr__false;
748 scan++;
749 } else if (*scan == '!') {
750 assign->value = git_attr__unset; /* explicit unspecified state */
751 scan++;
752 } else if (*scan == '#') /* comment rest of line */
753 break;
754
755 /* find the name */
756 name_start = scan;
757 while (*scan && !git__isspace(*scan) && *scan != '=') {
758 assign->name_hash =
759 ((assign->name_hash << 5) + assign->name_hash) + *scan;
760 scan++;
761 }
762 if (scan == name_start) {
763 /* must have found lone prefix (" - ") or leading = ("=foo")
764 * or end of buffer -- advance until whitespace and continue
765 */
766 while (*scan && !git__isspace(*scan)) scan++;
767 continue;
768 }
769
770 /* allocate permanent storage for name */
771 assign->name = git_pool_strndup(pool, name_start, scan - name_start);
772 GITERR_CHECK_ALLOC(assign->name);
773
774 /* if there is an equals sign, find the value */
775 if (*scan == '=') {
776 for (value_start = ++scan; *scan && !git__isspace(*scan); ++scan);
777
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);
782 }
783 }
784
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);
789
790 if (macro != NULL) {
791 unsigned int i;
792 git_attr_assignment *massign;
793
794 git_vector_foreach(&macro->assigns, i, massign) {
795 GIT_REFCOUNT_INC(massign);
796
797 error = git_vector_insert_sorted(
798 assigns, massign, &merge_assignments);
799 if (error < 0 && error != GIT_EEXISTS) {
800 git_attr_assignment__free(assign);
801 return error;
802 }
803 }
804 }
805 }
806
807 /* insert allocated assign into vector */
808 error = git_vector_insert_sorted(assigns, assign, &merge_assignments);
809 if (error < 0 && error != GIT_EEXISTS)
810 return error;
811
812 /* clear assign since it is now "owned" by the vector */
813 assign = NULL;
814 }
815
816 if (assign != NULL)
817 git_attr_assignment__free(assign);
818
819 *base = git__next_line(scan);
820
821 return (assigns->length == 0) ? GIT_ENOTFOUND : 0;
822 }
823
824 static void git_attr_rule__clear(git_attr_rule *rule)
825 {
826 unsigned int i;
827 git_attr_assignment *assign;
828
829 if (!rule)
830 return;
831
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);
836 }
837
838 /* match.pattern is stored in a git_pool, so no need to free */
839 rule->match.pattern = NULL;
840 rule->match.length = 0;
841 }
842
843 void git_attr_rule__free(git_attr_rule *rule)
844 {
845 git_attr_rule__clear(rule);
846 git__free(rule);
847 }
848
849 int git_attr_session__init(git_attr_session *session, git_repository *repo)
850 {
851 assert(repo);
852
853 session->key = git_atomic_inc(&repo->attr_session_key);
854
855 return 0;
856 }
857
858 void git_attr_session__free(git_attr_session *session)
859 {
860 if (!session)
861 return;
862
863 git_buf_free(&session->sysdir);
864 git_buf_free(&session->tmp);
865
866 memset(session, 0, sizeof(git_attr_session));
867 }