Skip to content

Commit 869ae5a

Browse files
committed
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 parent 0d12b8d commit 869ae5a

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

src/repository.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,10 +1360,11 @@ int git_repository_create_head(const char *git_dir, const char *ref_name)
13601360
git_buf ref_path = GIT_BUF_INIT;
13611361
git_filebuf ref = GIT_FILEBUF_INIT;
13621362
const char *fmt;
1363+
int error;
13631364

1364-
if (git_buf_joinpath(&ref_path, git_dir, GIT_HEAD_FILE) < 0 ||
1365-
git_filebuf_open(&ref, ref_path.ptr, 0, GIT_REFS_FILE_MODE) < 0)
1366-
goto fail;
1365+
if ((error = git_buf_joinpath(&ref_path, git_dir, GIT_HEAD_FILE)) < 0 ||
1366+
(error = git_filebuf_open(&ref, ref_path.ptr, 0, GIT_REFS_FILE_MODE)) < 0)
1367+
goto out;
13671368

13681369
if (!ref_name)
13691370
ref_name = GIT_BRANCH_MASTER;
@@ -1373,17 +1374,14 @@ int git_repository_create_head(const char *git_dir, const char *ref_name)
13731374
else
13741375
fmt = "ref: " GIT_REFS_HEADS_DIR "%s\n";
13751376

1376-
if (git_filebuf_printf(&ref, fmt, ref_name) < 0 ||
1377-
git_filebuf_commit(&ref) < 0)
1378-
goto fail;
1379-
1380-
git_buf_dispose(&ref_path);
1381-
return 0;
1377+
if ((error = git_filebuf_printf(&ref, fmt, ref_name)) < 0 ||
1378+
(error = git_filebuf_commit(&ref)) < 0)
1379+
goto out;
13821380

1383-
fail:
1381+
out:
13841382
git_buf_dispose(&ref_path);
13851383
git_filebuf_cleanup(&ref);
1386-
return -1;
1384+
return error;
13871385
}
13881386

13891387
static bool is_chmod_supported(const char *file_path)

0 commit comments

Comments
 (0)