Skip to content

Commit 9796852

Browse files
committed
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.
1 parent dbc7e4b commit 9796852

File tree

1 file changed

+29
-32
lines changed

1 file changed

+29
-32
lines changed

src/attr_file.c

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -251,14 +251,13 @@ static bool parse_optimized_patterns(
251251
int git_attr_file__parse_buffer(
252252
git_repository *repo, git_attr_file *attrs, const char *data)
253253
{
254-
int error = 0;
255254
const char *scan = data, *context = NULL;
256255
git_attr_rule *rule = NULL;
256+
int error = 0;
257257

258-
/* if subdir file path, convert context for file paths */
259-
if (attrs->entry &&
260-
git_path_root(attrs->entry->path) < 0 &&
261-
!git__suffixcmp(attrs->entry->path, "/" GIT_ATTR_FILE))
258+
/* If subdir file path, convert context for file paths */
259+
if (attrs->entry && git_path_root(attrs->entry->path) < 0 &&
260+
!git__suffixcmp(attrs->entry->path, "/" GIT_ATTR_FILE))
262261
context = attrs->entry->path;
263262

264263
if (git_mutex_lock(&attrs->lock) < 0) {
@@ -267,38 +266,36 @@ int git_attr_file__parse_buffer(
267266
}
268267

269268
while (!error && *scan) {
270-
/* allocate rule if needed */
271-
if (!rule && !(rule = git__calloc(1, sizeof(*rule)))) {
272-
error = -1;
273-
break;
274-
}
275-
276-
rule->match.flags =
277-
GIT_ATTR_FNMATCH_ALLOWNEG | GIT_ATTR_FNMATCH_ALLOWMACRO;
278-
279-
/* parse the next "pattern attr attr attr" line */
280-
if (!(error = git_attr_fnmatch__parse(
281-
&rule->match, &attrs->pool, context, &scan)) &&
282-
!(error = git_attr_assignment__parse(
283-
repo, &attrs->pool, &rule->assigns, &scan)))
269+
/* Allocate rule if needed, otherwise re-use previous rule */
270+
if (!rule) {
271+
rule = git__calloc(1, sizeof(*rule));
272+
GIT_ERROR_CHECK_ALLOC(rule);
273+
} else
274+
git_attr_rule__clear(rule);
275+
276+
rule->match.flags = GIT_ATTR_FNMATCH_ALLOWNEG | GIT_ATTR_FNMATCH_ALLOWMACRO;
277+
278+
/* Parse the next "pattern attr attr attr" line */
279+
if ((error = git_attr_fnmatch__parse(&rule->match, &attrs->pool, context, &scan)) < 0 ||
280+
(error = git_attr_assignment__parse(repo, &attrs->pool, &rule->assigns, &scan)) < 0)
284281
{
285-
if (rule->match.flags & GIT_ATTR_FNMATCH_MACRO)
286-
/* TODO: warning if macro found in file below repo root */
287-
error = git_attr_cache__insert_macro(repo, rule);
288-
else
289-
error = git_vector_insert(&attrs->rules, rule);
282+
if (error != GIT_ENOTFOUND)
283+
goto out;
284+
error = 0;
285+
continue;
290286
}
291287

292-
/* if the rule wasn't a pattern, on to the next */
293-
if (error < 0) {
294-
git_attr_rule__clear(rule); /* reset rule contents */
295-
if (error == GIT_ENOTFOUND)
296-
error = 0;
297-
} else {
298-
rule = NULL; /* vector now "owns" the rule */
299-
}
288+
if (rule->match.flags & GIT_ATTR_FNMATCH_MACRO) {
289+
/* TODO: warning if macro found in file below repo root */
290+
if ((error = git_attr_cache__insert_macro(repo, rule)) < 0)
291+
goto out;
292+
} else if ((error = git_vector_insert(&attrs->rules, rule)) < 0)
293+
goto out;
294+
295+
rule = NULL;
300296
}
301297

298+
out:
302299
git_mutex_unlock(&attrs->lock);
303300
git_attr_rule__free(rule);
304301

0 commit comments

Comments
 (0)