Skip to content

Commit 144354b

Browse files
derrickstoleegitster
authored andcommitted
commit-graph: rearrange chunk count logic
The number of chunks in a commit-graph file can change depending on whether we need the Extra Edges Chunk. We are going to add more optional chunks, and it will be helpful to rearrange this logic around the chunk count before doing so. Specifically, we need to finalize the number of chunks before writing the commit-graph header. Further, we also need to fill out the chunk lookup table dynamically and using "num_chunks" as we add optional chunks is useful for adding optional chunks in the future. Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 118bd57 commit 144354b

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

commit-graph.c

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,7 +1213,7 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
12131213
uint64_t chunk_offsets[5];
12141214
const unsigned hashsz = the_hash_algo->rawsz;
12151215
struct strbuf progress_title = STRBUF_INIT;
1216-
int num_chunks = ctx->num_extra_edges ? 4 : 3;
1216+
int num_chunks = 3;
12171217

12181218
ctx->graph_name = get_commit_graph_filename(ctx->obj_dir);
12191219
if (safe_create_leading_directories(ctx->graph_name)) {
@@ -1226,27 +1226,34 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
12261226
hold_lock_file_for_update(&lk, ctx->graph_name, LOCK_DIE_ON_ERROR);
12271227
f = hashfd(lk.tempfile->fd, lk.tempfile->filename.buf);
12281228

1229-
hashwrite_be32(f, GRAPH_SIGNATURE);
1230-
1231-
hashwrite_u8(f, GRAPH_VERSION);
1232-
hashwrite_u8(f, oid_version());
1233-
hashwrite_u8(f, num_chunks);
1234-
hashwrite_u8(f, 0); /* unused padding byte */
1235-
12361229
chunk_ids[0] = GRAPH_CHUNKID_OIDFANOUT;
12371230
chunk_ids[1] = GRAPH_CHUNKID_OIDLOOKUP;
12381231
chunk_ids[2] = GRAPH_CHUNKID_DATA;
1239-
if (ctx->num_extra_edges)
1240-
chunk_ids[3] = GRAPH_CHUNKID_EXTRAEDGES;
1241-
else
1242-
chunk_ids[3] = 0;
1243-
chunk_ids[4] = 0;
1232+
if (ctx->num_extra_edges) {
1233+
chunk_ids[num_chunks] = GRAPH_CHUNKID_EXTRAEDGES;
1234+
num_chunks++;
1235+
}
1236+
1237+
chunk_ids[num_chunks] = 0;
12441238

12451239
chunk_offsets[0] = 8 + (num_chunks + 1) * GRAPH_CHUNKLOOKUP_WIDTH;
12461240
chunk_offsets[1] = chunk_offsets[0] + GRAPH_FANOUT_SIZE;
12471241
chunk_offsets[2] = chunk_offsets[1] + hashsz * ctx->commits.nr;
12481242
chunk_offsets[3] = chunk_offsets[2] + (hashsz + 16) * ctx->commits.nr;
1249-
chunk_offsets[4] = chunk_offsets[3] + 4 * ctx->num_extra_edges;
1243+
1244+
num_chunks = 3;
1245+
if (ctx->num_extra_edges) {
1246+
chunk_offsets[num_chunks + 1] = chunk_offsets[num_chunks] +
1247+
4 * ctx->num_extra_edges;
1248+
num_chunks++;
1249+
}
1250+
1251+
hashwrite_be32(f, GRAPH_SIGNATURE);
1252+
1253+
hashwrite_u8(f, GRAPH_VERSION);
1254+
hashwrite_u8(f, oid_version());
1255+
hashwrite_u8(f, num_chunks);
1256+
hashwrite_u8(f, 0);
12501257

12511258
for (i = 0; i <= num_chunks; i++) {
12521259
uint32_t chunk_write[3];

0 commit comments

Comments
 (0)