Git fork

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>

authored by

Derrick Stolee and committed by
Junio C Hamano
c25651ae 1f2bc6be

+9 -17
+9 -17
midx-write.c
··· 1064 int bitmapped_packs_concat_len = 0; 1065 int pack_name_concat_len = 0; 1066 int dropped_packs = 0; 1067 - int result = 0; 1068 const char **keep_hashes = NULL; 1069 struct chunkfile *cf; 1070 ··· 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; 1121 goto cleanup; 1122 } 1123 ctx.num_multi_pack_indexes_before++; 1124 m = m->base_midx; 1125 } 1126 } else if (ctx.m && fill_packs_from_midx(&ctx)) { 1127 - result = 1; 1128 goto cleanup; 1129 } 1130 ··· 1160 */ 1161 if (!want_bitmap) 1162 clear_midx_files_ext(object_dir, "bitmap", NULL); 1163 goto cleanup; 1164 } 1165 } 1166 1167 - if (ctx.incremental && !ctx.nr) 1168 goto cleanup; /* nothing to do */ 1169 1170 if (preferred_pack_name) { 1171 ctx.preferred_pack_idx = NO_PREFERRED_PACK; ··· 1239 if (!preferred->num_objects) { 1240 error(_("cannot select preferred pack %s with no objects"), 1241 preferred->pack_name); 1242 - result = 1; 1243 goto cleanup; 1244 } 1245 } ··· 1278 } 1279 } 1280 1281 - if (missing_drops) { 1282 - result = 1; 1283 goto cleanup; 1284 - } 1285 } 1286 1287 /* ··· 1327 1328 if (ctx.nr - dropped_packs == 0) { 1329 error(_("no pack files to index.")); 1330 - result = 1; 1331 goto cleanup; 1332 } 1333 ··· 1347 incr = mks_tempfile_m(midx_name.buf, 0444); 1348 if (!incr) { 1349 error(_("unable to create temporary MIDX layer")); 1350 - result = -1; 1351 goto cleanup; 1352 } 1353 1354 if (adjust_shared_perm(r, get_tempfile_path(incr))) { 1355 error(_("unable to adjust shared permissions for '%s'"), 1356 get_tempfile_path(incr)); 1357 - result = -1; 1358 goto cleanup; 1359 } 1360 ··· 1432 midx_hash, &pdata, commits, commits_nr, 1433 flags) < 0) { 1434 error(_("could not write multi-pack bitmap")); 1435 - result = 1; 1436 clear_packing_data(&pdata); 1437 free(commits); 1438 goto cleanup; ··· 1458 1459 if (!chainf) { 1460 error_errno(_("unable to open multi-pack-index chain file")); 1461 - result = -1; 1462 goto cleanup; 1463 } 1464 1465 - if (link_midx_to_chain(ctx.base_midx) < 0) { 1466 - result = -1; 1467 goto cleanup; 1468 - } 1469 1470 get_split_midx_filename_ext(r->hash_algo, &final_midx_name, 1471 object_dir, midx_hash, MIDX_EXT_MIDX); 1472 1473 if (rename_tempfile(&incr, final_midx_name.buf) < 0) { 1474 error_errno(_("unable to rename new multi-pack-index layer")); 1475 - result = -1; 1476 goto cleanup; 1477 } 1478 ··· 1505 clear_midx_files(r, object_dir, keep_hashes, 1506 ctx.num_multi_pack_indexes_before + 1, 1507 ctx.incremental); 1508 1509 cleanup: 1510 for (size_t i = 0; i < ctx.nr; i++) {
··· 1064 int bitmapped_packs_concat_len = 0; 1065 int pack_name_concat_len = 0; 1066 int dropped_packs = 0; 1067 + int result = -1; 1068 const char **keep_hashes = NULL; 1069 struct chunkfile *cf; 1070 ··· 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 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)) { 1126 goto cleanup; 1127 } 1128 ··· 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) { 1168 + result = 0; 1169 goto cleanup; /* nothing to do */ 1170 + } 1171 1172 if (preferred_pack_name) { 1173 ctx.preferred_pack_idx = NO_PREFERRED_PACK; ··· 1241 if (!preferred->num_objects) { 1242 error(_("cannot select preferred pack %s with no objects"), 1243 preferred->pack_name); 1244 goto cleanup; 1245 } 1246 } ··· 1279 } 1280 } 1281 1282 + if (missing_drops) 1283 goto cleanup; 1284 } 1285 1286 /* ··· 1326 1327 if (ctx.nr - dropped_packs == 0) { 1328 error(_("no pack files to index.")); 1329 goto cleanup; 1330 } 1331 ··· 1345 incr = mks_tempfile_m(midx_name.buf, 0444); 1346 if (!incr) { 1347 error(_("unable to create temporary MIDX layer")); 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)); 1354 goto cleanup; 1355 } 1356 ··· 1428 midx_hash, &pdata, commits, commits_nr, 1429 flags) < 0) { 1430 error(_("could not write multi-pack bitmap")); 1431 clear_packing_data(&pdata); 1432 free(commits); 1433 goto cleanup; ··· 1453 1454 if (!chainf) { 1455 error_errno(_("unable to open multi-pack-index chain file")); 1456 goto cleanup; 1457 } 1458 1459 + if (link_midx_to_chain(ctx.base_midx) < 0) 1460 goto cleanup; 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")); 1467 goto cleanup; 1468 } 1469 ··· 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++) {