commit-graph: simplify write_commit_graph_file() #1
commitbb4d60e5d536009e0c97bfa7f464416c29c04c81
authorSZEDER Gábor <szeder.dev@gmail.com>
Fri, 5 Jun 2020 13:00:31 +0000 (5 13:00 +0000)
committerJunio C Hamano <gitster@pobox.com>
Mon, 8 Jun 2020 19:28:49 +0000 (8 12:28 -0700)
tree6b8f4d5d63a194fffca504f2929a0a2835b979f6
parent5cfa438a76f7f67f34f6f758b62943261ded1fb5
commit-graph: simplify write_commit_graph_file() #1

In write_commit_graph_file() one block of code fills the array of
chunk IDs, another block of code fills the array of chunk offsets,
then the chunk IDs and offsets are written to the Chunk Lookup table,
and finally a third block of code writes the actual chunks.  In case
of optional chunks like Extra Edge List and Base Graphs List there is
also a condition checking whether that chunk is necessary/desired, and
that same condition is repeated in all those three blocks of code.
This patch series is about to add more optional chunks, so there would
be even more repeated conditions.

Those chunk offsets are relative to the beginning of the file, so they
inherently depend on the size of the Chunk Lookup table, which in turn
depends on the number of chunks that are to be written to the
commit-graph file.  IOW at the time we set the first chunk's ID we
can't yet know its offset, because we don't yet know how many chunks
there are.

Simplify this by initially filling an array of chunk sizes, not
offsets, and calculate the offsets based on the chunk sizes only
later, while we are writing the Chunk Lookup table.  This way we can
fill the arrays of chunk IDs and sizes in one go, eliminating one set
of repeated conditions.

Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
commit-graph.c