repository: avoid swallowing error codes in `create_head` The error handling in `git_repository_create_head` completely swallows all error codes. While probably not too much of a problem, this also violates our usual coding style. Refactor the code to use a local `error` variable with the typical `goto out` statements.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
diff --git a/src/repository.c b/src/repository.c
index 71386d6..c40efa3 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -1360,10 +1360,11 @@ int git_repository_create_head(const char *git_dir, const char *ref_name)
git_buf ref_path = GIT_BUF_INIT;
git_filebuf ref = GIT_FILEBUF_INIT;
const char *fmt;
+ int error;
- if (git_buf_joinpath(&ref_path, git_dir, GIT_HEAD_FILE) < 0 ||
- git_filebuf_open(&ref, ref_path.ptr, 0, GIT_REFS_FILE_MODE) < 0)
- goto fail;
+ if ((error = git_buf_joinpath(&ref_path, git_dir, GIT_HEAD_FILE)) < 0 ||
+ (error = git_filebuf_open(&ref, ref_path.ptr, 0, GIT_REFS_FILE_MODE)) < 0)
+ goto out;
if (!ref_name)
ref_name = GIT_BRANCH_MASTER;
@@ -1373,17 +1374,14 @@ int git_repository_create_head(const char *git_dir, const char *ref_name)
else
fmt = "ref: " GIT_REFS_HEADS_DIR "%s\n";
- if (git_filebuf_printf(&ref, fmt, ref_name) < 0 ||
- git_filebuf_commit(&ref) < 0)
- goto fail;
-
- git_buf_dispose(&ref_path);
- return 0;
+ if ((error = git_filebuf_printf(&ref, fmt, ref_name)) < 0 ||
+ (error = git_filebuf_commit(&ref)) < 0)
+ goto out;
-fail:
+out:
git_buf_dispose(&ref_path);
git_filebuf_cleanup(&ref);
- return -1;
+ return error;
}
static bool is_chmod_supported(const char *file_path)