Skip to content

Commit c25651a

Browse files
derrickstoleegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1f2bc6b commit c25651a

File tree

1 file changed

+9
-17
lines changed

1 file changed

+9
-17
lines changed

midx-write.c

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,7 +1064,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
10641064
int bitmapped_packs_concat_len = 0;
10651065
int pack_name_concat_len = 0;
10661066
int dropped_packs = 0;
1067-
int result = 0;
1067+
int result = -1;
10681068
const char **keep_hashes = NULL;
10691069
struct chunkfile *cf;
10701070

@@ -1117,14 +1117,12 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
11171117
error(_("could not load reverse index for MIDX %s"),
11181118
hash_to_hex_algop(get_midx_checksum(m),
11191119
m->repo->hash_algo));
1120-
result = 1;
11211120
goto cleanup;
11221121
}
11231122
ctx.num_multi_pack_indexes_before++;
11241123
m = m->base_midx;
11251124
}
11261125
} else if (ctx.m && fill_packs_from_midx(&ctx)) {
1127-
result = 1;
11281126
goto cleanup;
11291127
}
11301128

@@ -1160,12 +1158,16 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
11601158
*/
11611159
if (!want_bitmap)
11621160
clear_midx_files_ext(object_dir, "bitmap", NULL);
1161+
1162+
result = 0;
11631163
goto cleanup;
11641164
}
11651165
}
11661166

1167-
if (ctx.incremental && !ctx.nr)
1167+
if (ctx.incremental && !ctx.nr) {
1168+
result = 0;
11681169
goto cleanup; /* nothing to do */
1170+
}
11691171

11701172
if (preferred_pack_name) {
11711173
ctx.preferred_pack_idx = NO_PREFERRED_PACK;
@@ -1239,7 +1241,6 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
12391241
if (!preferred->num_objects) {
12401242
error(_("cannot select preferred pack %s with no objects"),
12411243
preferred->pack_name);
1242-
result = 1;
12431244
goto cleanup;
12441245
}
12451246
}
@@ -1278,10 +1279,8 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
12781279
}
12791280
}
12801281

1281-
if (missing_drops) {
1282-
result = 1;
1282+
if (missing_drops)
12831283
goto cleanup;
1284-
}
12851284
}
12861285

12871286
/*
@@ -1327,7 +1326,6 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
13271326

13281327
if (ctx.nr - dropped_packs == 0) {
13291328
error(_("no pack files to index."));
1330-
result = 1;
13311329
goto cleanup;
13321330
}
13331331

@@ -1347,14 +1345,12 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
13471345
incr = mks_tempfile_m(midx_name.buf, 0444);
13481346
if (!incr) {
13491347
error(_("unable to create temporary MIDX layer"));
1350-
result = -1;
13511348
goto cleanup;
13521349
}
13531350

13541351
if (adjust_shared_perm(r, get_tempfile_path(incr))) {
13551352
error(_("unable to adjust shared permissions for '%s'"),
13561353
get_tempfile_path(incr));
1357-
result = -1;
13581354
goto cleanup;
13591355
}
13601356

@@ -1432,7 +1428,6 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
14321428
midx_hash, &pdata, commits, commits_nr,
14331429
flags) < 0) {
14341430
error(_("could not write multi-pack bitmap"));
1435-
result = 1;
14361431
clear_packing_data(&pdata);
14371432
free(commits);
14381433
goto cleanup;
@@ -1458,21 +1453,17 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
14581453

14591454
if (!chainf) {
14601455
error_errno(_("unable to open multi-pack-index chain file"));
1461-
result = -1;
14621456
goto cleanup;
14631457
}
14641458

1465-
if (link_midx_to_chain(ctx.base_midx) < 0) {
1466-
result = -1;
1459+
if (link_midx_to_chain(ctx.base_midx) < 0)
14671460
goto cleanup;
1468-
}
14691461

14701462
get_split_midx_filename_ext(r->hash_algo, &final_midx_name,
14711463
object_dir, midx_hash, MIDX_EXT_MIDX);
14721464

14731465
if (rename_tempfile(&incr, final_midx_name.buf) < 0) {
14741466
error_errno(_("unable to rename new multi-pack-index layer"));
1475-
result = -1;
14761467
goto cleanup;
14771468
}
14781469

@@ -1505,6 +1496,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
15051496
clear_midx_files(r, object_dir, keep_hashes,
15061497
ctx.num_multi_pack_indexes_before + 1,
15071498
ctx.incremental);
1499+
result = 0;
15081500

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

0 commit comments

Comments
 (0)