Commit 97968529 by Patrick Steinhardt

attr_file: refactor `parse_buffer` function

The gitattributes code is one of our oldest and most-untouched codebases
in libgit2, and as such its code style doesn't quite match our current
best practices. Refactor the function `git_attr_file__parse_buffer` to
better match them.
parent dbc7e4b1
...@@ -251,14 +251,13 @@ static bool parse_optimized_patterns( ...@@ -251,14 +251,13 @@ static bool parse_optimized_patterns(
int git_attr_file__parse_buffer( int git_attr_file__parse_buffer(
git_repository *repo, git_attr_file *attrs, const char *data) git_repository *repo, git_attr_file *attrs, const char *data)
{ {
int error = 0;
const char *scan = data, *context = NULL; const char *scan = data, *context = NULL;
git_attr_rule *rule = NULL; git_attr_rule *rule = NULL;
int error = 0;
/* if subdir file path, convert context for file paths */ /* If subdir file path, convert context for file paths */
if (attrs->entry && if (attrs->entry && git_path_root(attrs->entry->path) < 0 &&
git_path_root(attrs->entry->path) < 0 && !git__suffixcmp(attrs->entry->path, "/" GIT_ATTR_FILE))
!git__suffixcmp(attrs->entry->path, "/" GIT_ATTR_FILE))
context = attrs->entry->path; context = attrs->entry->path;
if (git_mutex_lock(&attrs->lock) < 0) { if (git_mutex_lock(&attrs->lock) < 0) {
...@@ -267,38 +266,36 @@ int git_attr_file__parse_buffer( ...@@ -267,38 +266,36 @@ int git_attr_file__parse_buffer(
} }
while (!error && *scan) { while (!error && *scan) {
/* allocate rule if needed */ /* Allocate rule if needed, otherwise re-use previous rule */
if (!rule && !(rule = git__calloc(1, sizeof(*rule)))) { if (!rule) {
error = -1; rule = git__calloc(1, sizeof(*rule));
break; GIT_ERROR_CHECK_ALLOC(rule);
} } else
git_attr_rule__clear(rule);
rule->match.flags =
GIT_ATTR_FNMATCH_ALLOWNEG | GIT_ATTR_FNMATCH_ALLOWMACRO; rule->match.flags = GIT_ATTR_FNMATCH_ALLOWNEG | GIT_ATTR_FNMATCH_ALLOWMACRO;
/* parse the next "pattern attr attr attr" line */ /* Parse the next "pattern attr attr attr" line */
if (!(error = git_attr_fnmatch__parse( if ((error = git_attr_fnmatch__parse(&rule->match, &attrs->pool, context, &scan)) < 0 ||
&rule->match, &attrs->pool, context, &scan)) && (error = git_attr_assignment__parse(repo, &attrs->pool, &rule->assigns, &scan)) < 0)
!(error = git_attr_assignment__parse(
repo, &attrs->pool, &rule->assigns, &scan)))
{ {
if (rule->match.flags & GIT_ATTR_FNMATCH_MACRO) if (error != GIT_ENOTFOUND)
/* TODO: warning if macro found in file below repo root */ goto out;
error = git_attr_cache__insert_macro(repo, rule); error = 0;
else continue;
error = git_vector_insert(&attrs->rules, rule);
} }
/* if the rule wasn't a pattern, on to the next */ if (rule->match.flags & GIT_ATTR_FNMATCH_MACRO) {
if (error < 0) { /* TODO: warning if macro found in file below repo root */
git_attr_rule__clear(rule); /* reset rule contents */ if ((error = git_attr_cache__insert_macro(repo, rule)) < 0)
if (error == GIT_ENOTFOUND) goto out;
error = 0; } else if ((error = git_vector_insert(&attrs->rules, rule)) < 0)
} else { goto out;
rule = NULL; /* vector now "owns" the rule */
} rule = NULL;
} }
out:
git_mutex_unlock(&attrs->lock); git_mutex_unlock(&attrs->lock);
git_attr_rule__free(rule); git_attr_rule__free(rule);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment