Commit 0bc091ddb231b201ce22294cd529d37ae40b25cc

Josh Triplett 2020-05-23T15:35:38

git_packbuilder_write: Unify cleanup path Clean up and return via a single label, to avoid duplicate error handling before each return, and to make it easier to extend the set of cleanups needed.

diff --git a/src/pack-objects.c b/src/pack-objects.c
index 49b4e47..55a99e4 100644
--- a/src/pack-objects.c
+++ b/src/pack-objects.c
@@ -1384,8 +1384,9 @@ int git_packbuilder_write(
 	git_indexer_progress_cb progress_cb,
 	void *progress_cb_payload)
 {
+	int error = -1;
 	git_indexer_options opts = GIT_INDEXER_OPTIONS_INIT;
-	git_indexer *indexer;
+	git_indexer *indexer = NULL;
 	git_indexer_progress stats;
 	struct pack_write_context ctx;
 	int t;
@@ -1395,9 +1396,8 @@ int git_packbuilder_write(
 	opts.progress_cb = progress_cb;
 	opts.progress_cb_payload = progress_cb_payload;
 
-	if (git_indexer_new(
-		&indexer, path, mode, pb->odb, &opts) < 0)
-		return -1;
+	if ((error = git_indexer_new(&indexer, path, mode, pb->odb, &opts)) < 0)
+		goto cleanup;
 
 	if (!git_repository__configmap_lookup(&t, pb->repo, GIT_CONFIGMAP_FSYNCOBJECTFILES) && t)
 		git_indexer__set_fsync(indexer, 1);
@@ -1405,16 +1405,17 @@ int git_packbuilder_write(
 	ctx.indexer = indexer;
 	ctx.stats = &stats;
 
-	if (git_packbuilder_foreach(pb, write_cb, &ctx) < 0 ||
-		git_indexer_commit(indexer, &stats) < 0) {
-		git_indexer_free(indexer);
-		return -1;
-	}
+	if ((error = git_packbuilder_foreach(pb, write_cb, &ctx)) < 0)
+		goto cleanup;
+
+	if ((error = git_indexer_commit(indexer, &stats)) < 0)
+		goto cleanup;
 
 	git_oid_cpy(&pb->pack_oid, git_indexer_hash(indexer));
 
+cleanup:
 	git_indexer_free(indexer);
-	return 0;
+	return error;
 }
 
 #undef PREPARE_PACK