midx-write: simplify error cases

The write_midx_internal() method uses gotos to jump to a cleanup section to clear memory before returning 'result'. Since these jumps are more common for error conditions, initialize 'result' to -1 and then only set it to 0 before returning with success. There are a couple places where we return with success via a jump. This has the added benefit that the method now returns -1 on error instead of an inconsistent 1 or -1. Signed-off-by: Derrick Stolee <stolee@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Sep 5, 2025 at 19:26 UTC c25651aefdf583ebb2dbb88437337947372de8f3
1 file changed +9 -17
midx-write.c
+9 -17
@@ -1064,7 +1064,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
1064 int bitmapped_packs_concat_len = 0;
1065 int pack_name_concat_len = 0;
1066 int dropped_packs = 0;
1067 - int result = 0;
1067 + int result = -1;
1068 const char **keep_hashes = NULL;
1069 struct chunkfile *cf;
1070
@@ -1117,14 +1117,12 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
1117 error(_("could not load reverse index for MIDX %s"),
1118 hash_to_hex_algop(get_midx_checksum(m),
1119 m->repo->hash_algo));
1120 - result = 1;
1120 goto cleanup;
1121 }
1122 ctx.num_multi_pack_indexes_before++;
1123 m = m->base_midx;
1124 }
1125 } else if (ctx.m && fill_packs_from_midx(&ctx)) {
1127 - result = 1;
1126 goto cleanup;
1127 }
1128
@@ -1160,12 +1158,16 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
1158 */
1159 if (!want_bitmap)
1160 clear_midx_files_ext(object_dir, "bitmap", NULL);
1161 +
1162 + result = 0;
1163 goto cleanup;
1164 }
1165 }
1166
1167 - if (ctx.incremental && !ctx.nr)
1167 + if (ctx.incremental && !ctx.nr) {
1168 + result = 0;
1169 goto cleanup; /* nothing to do */
1170 + }
1171
1172 if (preferred_pack_name) {
1173 ctx.preferred_pack_idx = NO_PREFERRED_PACK;
@@ -1239,7 +1241,6 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
1241 if (!preferred->num_objects) {
1242 error(_("cannot select preferred pack %s with no objects"),
1243 preferred->pack_name);
1242 - result = 1;
1244 goto cleanup;
1245 }
1246 }
@@ -1278,10 +1279,8 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
1279 }
1280 }
1281
1281 - if (missing_drops) {
1282 - result = 1;
1282 + if (missing_drops)
1283 goto cleanup;
1284 - }
1284 }
1285
1286 /*
@@ -1327,7 +1326,6 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
1326
1327 if (ctx.nr - dropped_packs == 0) {
1328 error(_("no pack files to index."));
1330 - result = 1;
1329 goto cleanup;
1330 }
1331
@@ -1347,14 +1345,12 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
1345 incr = mks_tempfile_m(midx_name.buf, 0444);
1346 if (!incr) {
1347 error(_("unable to create temporary MIDX layer"));
1350 - result = -1;
1348 goto cleanup;
1349 }
1350
1351 if (adjust_shared_perm(r, get_tempfile_path(incr))) {
1352 error(_("unable to adjust shared permissions for '%s'"),
1353 get_tempfile_path(incr));
1357 - result = -1;
1354 goto cleanup;
1355 }
1356
@@ -1432,7 +1428,6 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
1428 midx_hash, &pdata, commits, commits_nr,
1429 flags) < 0) {
1430 error(_("could not write multi-pack bitmap"));
1435 - result = 1;
1431 clear_packing_data(&pdata);
1432 free(commits);
1433 goto cleanup;
@@ -1458,21 +1453,17 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
1453
1454 if (!chainf) {
1455 error_errno(_("unable to open multi-pack-index chain file"));
1461 - result = -1;
1456 goto cleanup;
1457 }
1458
1465 - if (link_midx_to_chain(ctx.base_midx) < 0) {
1466 - result = -1;
1459 + if (link_midx_to_chain(ctx.base_midx) < 0)
1460 goto cleanup;
1468 - }
1461
1462 get_split_midx_filename_ext(r->hash_algo, &final_midx_name,
1463 object_dir, midx_hash, MIDX_EXT_MIDX);
1464
1465 if (rename_tempfile(&incr, final_midx_name.buf) < 0) {
1466 error_errno(_("unable to rename new multi-pack-index layer"));
1475 - result = -1;
1467 goto cleanup;
1468 }
1469
@@ -1505,6 +1496,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
1496 clear_midx_files(r, object_dir, keep_hashes,
1497 ctx.num_multi_pack_indexes_before + 1,
1498 ctx.incremental);
1499 + result = 0;
1500
1501 cleanup:
1502 for (size_t i = 0; i < ctx.nr; i++) {