Skip to content

Commit dbc7e4b

Browse files
committed
attr_file: refactor load_standalone 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__lookup_standalone` to better match them.
1 parent be8f9bb commit dbc7e4b

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

src/attr_file.c

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -345,33 +345,28 @@ int git_attr_file__lookup_one(
345345

346346
int git_attr_file__load_standalone(git_attr_file **out, const char *path)
347347
{
348-
int error;
349-
git_attr_file *file;
350348
git_buf content = GIT_BUF_INIT;
349+
git_attr_file *file = NULL;
350+
int error;
351351

352-
error = git_attr_file__new(&file, NULL, GIT_ATTR_FILE__FROM_FILE);
353-
if (error < 0)
354-
return error;
352+
if ((error = git_futils_readbuffer(&content, path)) < 0)
353+
goto out;
355354

356-
error = git_attr_cache__alloc_file_entry(
357-
&file->entry, NULL, path, &file->pool);
358-
if (error < 0) {
359-
git_attr_file__free(file);
360-
return error;
361-
}
362-
/* because the cache entry is allocated from the file's own pool, we
355+
/*
356+
* Because the cache entry is allocated from the file's own pool, we
363357
* don't have to free it - freeing file+pool will free cache entry, too.
364358
*/
365359

366-
if (!(error = git_futils_readbuffer(&content, path))) {
367-
error = git_attr_file__parse_buffer(NULL, file, content.ptr);
368-
git_buf_dispose(&content);
369-
}
360+
if ((error = git_attr_file__new(&file, NULL, GIT_ATTR_FILE__FROM_FILE)) < 0 ||
361+
(error = git_attr_file__parse_buffer(NULL, file, content.ptr)) < 0 ||
362+
(error = git_attr_cache__alloc_file_entry(&file->entry, NULL, path, &file->pool)) < 0)
363+
goto out;
370364

365+
*out = file;
366+
out:
371367
if (error < 0)
372368
git_attr_file__free(file);
373-
else
374-
*out = file;
369+
git_buf_dispose(&content);
375370

376371
return error;
377372
}

0 commit comments

Comments
 (0)