Skip to content

Commit 224be4e

Browse files
committed
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>
1 parent 7c68f25 commit 224be4e

1 file changed

Lines changed: 9 additions & 17 deletions

File tree

midx-write.c

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
10461046
int bitmapped_packs_concat_len = 0;
10471047
int pack_name_concat_len = 0;
10481048
int dropped_packs = 0;
1049-
int result = 0;
1049+
int result = -1;
10501050
const char **keep_hashes = NULL;
10511051
struct chunkfile *cf;
10521052

@@ -1099,14 +1099,12 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
10991099
error(_("could not load reverse index for MIDX %s"),
11001100
hash_to_hex_algop(get_midx_checksum(m),
11011101
m->repo->hash_algo));
1102-
result = 1;
11031102
goto cleanup;
11041103
}
11051104
ctx.num_multi_pack_indexes_before++;
11061105
m = m->base_midx;
11071106
}
11081107
} else if (ctx.m && fill_packs_from_midx(&ctx)) {
1109-
result = 1;
11101108
goto cleanup;
11111109
}
11121110

@@ -1142,12 +1140,16 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
11421140
*/
11431141
if (!want_bitmap)
11441142
clear_midx_files_ext(object_dir, "bitmap", NULL);
1143+
1144+
result = 0;
11451145
goto cleanup;
11461146
}
11471147
}
11481148

1149-
if (ctx.incremental && !ctx.nr)
1149+
if (ctx.incremental && !ctx.nr) {
1150+
result = 0;
11501151
goto cleanup; /* nothing to do */
1152+
}
11511153

11521154
if (preferred_pack_name) {
11531155
ctx.preferred_pack_idx = NO_PREFERRED_PACK;
@@ -1221,7 +1223,6 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
12211223
if (!preferred->num_objects) {
12221224
error(_("cannot select preferred pack %s with no objects"),
12231225
preferred->pack_name);
1224-
result = 1;
12251226
goto cleanup;
12261227
}
12271228
}
@@ -1260,10 +1261,8 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
12601261
}
12611262
}
12621263

1263-
if (missing_drops) {
1264-
result = 1;
1264+
if (missing_drops)
12651265
goto cleanup;
1266-
}
12671266
}
12681267

12691268
/*
@@ -1309,7 +1308,6 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
13091308

13101309
if (ctx.nr - dropped_packs == 0) {
13111310
error(_("no pack files to index."));
1312-
result = 1;
13131311
goto cleanup;
13141312
}
13151313

@@ -1329,14 +1327,12 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
13291327
incr = mks_tempfile_m(midx_name.buf, 0444);
13301328
if (!incr) {
13311329
error(_("unable to create temporary MIDX layer"));
1332-
result = -1;
13331330
goto cleanup;
13341331
}
13351332

13361333
if (adjust_shared_perm(r, get_tempfile_path(incr))) {
13371334
error(_("unable to adjust shared permissions for '%s'"),
13381335
get_tempfile_path(incr));
1339-
result = -1;
13401336
goto cleanup;
13411337
}
13421338

@@ -1414,7 +1410,6 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
14141410
midx_hash, &pdata, commits, commits_nr,
14151411
flags) < 0) {
14161412
error(_("could not write multi-pack bitmap"));
1417-
result = 1;
14181413
clear_packing_data(&pdata);
14191414
free(commits);
14201415
goto cleanup;
@@ -1440,21 +1435,17 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
14401435

14411436
if (!chainf) {
14421437
error_errno(_("unable to open multi-pack-index chain file"));
1443-
result = -1;
14441438
goto cleanup;
14451439
}
14461440

1447-
if (link_midx_to_chain(ctx.base_midx) < 0) {
1448-
result = -1;
1441+
if (link_midx_to_chain(ctx.base_midx) < 0)
14491442
goto cleanup;
1450-
}
14511443

14521444
get_split_midx_filename_ext(r->hash_algo, &final_midx_name,
14531445
object_dir, midx_hash, MIDX_EXT_MIDX);
14541446

14551447
if (rename_tempfile(&incr, final_midx_name.buf) < 0) {
14561448
error_errno(_("unable to rename new multi-pack-index layer"));
1457-
result = -1;
14581449
goto cleanup;
14591450
}
14601451

@@ -1487,6 +1478,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
14871478
clear_midx_files(r, object_dir, keep_hashes,
14881479
ctx.num_multi_pack_indexes_before + 1,
14891480
ctx.incremental);
1481+
result = 0;
14901482

14911483
cleanup:
14921484
for (size_t i = 0; i < ctx.nr; i++) {

0 commit comments

Comments
 (0)