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