branch: simplify error handling for git_branch_move() The cleanup needs to happen anyway, so set the error code and jump there instead of copying the code.
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 43 44 45 46 47
diff --git a/src/branch.c b/src/branch.c
index 51df97c..c980cf0 100644
--- a/src/branch.c
+++ b/src/branch.c
@@ -185,32 +185,24 @@ int git_branch_move(git_repository *repo, const char *old_branch_name, const cha
{
git_reference *reference = NULL;
git_buf old_reference_name = GIT_BUF_INIT, new_reference_name = GIT_BUF_INIT;
- int error;
+ int error = 0;
- if (git_buf_joinpath(&old_reference_name, GIT_REFS_HEADS_DIR, old_branch_name) < 0)
- goto on_error;
+ if ((error = git_buf_joinpath(&old_reference_name, GIT_REFS_HEADS_DIR, old_branch_name)) < 0)
+ goto cleanup;
/* We need to be able to return GIT_ENOTFOUND */
- if ((error = git_reference_lookup(&reference, repo, git_buf_cstr(&old_reference_name))) < 0) {
- git_buf_free(&old_reference_name);
- return error;
- }
+ if ((error = git_reference_lookup(&reference, repo, git_buf_cstr(&old_reference_name))) < 0)
+ goto cleanup;
- if (git_buf_joinpath(&new_reference_name, GIT_REFS_HEADS_DIR, new_branch_name) < 0)
- goto on_error;
+ if ((error = git_buf_joinpath(&new_reference_name, GIT_REFS_HEADS_DIR, new_branch_name)) < 0)
+ goto cleanup;
- if (git_reference_rename(reference, git_buf_cstr(&new_reference_name), force) < 0)
- goto on_error;
+ error = git_reference_rename(reference, git_buf_cstr(&new_reference_name), force);
+cleanup:
git_reference_free(reference);
git_buf_free(&old_reference_name);
git_buf_free(&new_reference_name);
- return 0;
-
-on_error:
- git_reference_free(reference);
- git_buf_free(&old_reference_name);
- git_buf_free(&new_reference_name);
- return -1;
+ return error;
}