chunk-format.h: extract oid_version()

There are three definitions of an identical function which converts `the_hash_algo` into either 1 (for SHA-1) or 2 (for SHA-256). There is a copy of this function for writing both the commit-graph and multi-pack-index file, and another inline definition used to write the .rev header. Consolidate these into a single definition in chunk-format.h. It's not clear that this is the best header to define this function in, but it should do for now. (Worth noting, the .rev caller expects a 4-byte unsigned, but the other two callers work with a single unsigned byte. The consolidated version uses the latter type, and lets the compiler widen it when required). Another caller will be added in a subsequent patch. Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Taylor Blau committed May 20, 2022 at 19:17 UTC d9fef9d90d27b6794350ec3bc622042b79397088
5 files changed +23 -43
chunk-format.c
+12
@@ -181,3 +181,15 @@ int read_chunk(struct chunkfile *cf,
181
182 return CHUNK_NOT_FOUND;
183 }
184 +
185 +uint8_t oid_version(const struct git_hash_algo *algop)
186 +{
187 + switch (hash_algo_by_ptr(algop)) {
188 + case GIT_HASH_SHA1:
189 + return 1;
190 + case GIT_HASH_SHA256:
191 + return 2;
192 + default:
193 + die(_("invalid hash version"));
194 + }
195 +}
chunk-format.h
+3
@@ -2,6 +2,7 @@
2 #define CHUNK_FORMAT_H
3
4 #include "git-compat-util.h"
5 +#include "hash.h"
6
7 struct hashfile;
8 struct chunkfile;
@@ -65,4 +66,6 @@ int read_chunk(struct chunkfile *cf,
66 chunk_read_fn fn,
67 void *data);
68
69 +uint8_t oid_version(const struct git_hash_algo *algop);
70 +
71 #endif
commit-graph.c
+3 -15
@@ -193,18 +193,6 @@ char *get_commit_graph_chain_filename(struct object_directory *odb)
193 return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb->path);
194 }
195
196 -static uint8_t oid_version(void)
197 -{
198 - switch (hash_algo_by_ptr(the_hash_algo)) {
199 - case GIT_HASH_SHA1:
200 - return 1;
201 - case GIT_HASH_SHA256:
202 - return 2;
203 - default:
204 - die(_("invalid hash version"));
205 - }
206 -}
207 -
196 static struct commit_graph *alloc_commit_graph(void)
197 {
198 struct commit_graph *g = xcalloc(1, sizeof(*g));
@@ -365,9 +353,9 @@ struct commit_graph *parse_commit_graph(struct repository *r,
353 }
354
355 hash_version = *(unsigned char*)(data + 5);
368 - if (hash_version != oid_version()) {
356 + if (hash_version != oid_version(the_hash_algo)) {
357 error(_("commit-graph hash version %X does not match version %X"),
370 - hash_version, oid_version());
358 + hash_version, oid_version(the_hash_algo));
359 return NULL;
360 }
361
@@ -1921,7 +1909,7 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
1909 hashwrite_be32(f, GRAPH_SIGNATURE);
1910
1911 hashwrite_u8(f, GRAPH_VERSION);
1924 - hashwrite_u8(f, oid_version());
1912 + hashwrite_u8(f, oid_version(the_hash_algo));
1913 hashwrite_u8(f, get_num_chunks(cf));
1914 hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
1915
midx.c
+3 -15
@@ -41,18 +41,6 @@
41
42 #define PACK_EXPIRED UINT_MAX
43
44 -static uint8_t oid_version(void)
45 -{
46 - switch (hash_algo_by_ptr(the_hash_algo)) {
47 - case GIT_HASH_SHA1:
48 - return 1;
49 - case GIT_HASH_SHA256:
50 - return 2;
51 - default:
52 - die(_("invalid hash version"));
53 - }
54 -}
55 -
44 const unsigned char *get_midx_checksum(struct multi_pack_index *m)
45 {
46 return m->data + m->data_len - the_hash_algo->rawsz;
@@ -134,9 +122,9 @@ struct multi_pack_index *load_multi_pack_index(const char *object_dir, int local
122 m->version);
123
124 hash_version = m->data[MIDX_BYTE_HASH_VERSION];
137 - if (hash_version != oid_version()) {
125 + if (hash_version != oid_version(the_hash_algo)) {
126 error(_("multi-pack-index hash version %u does not match version %u"),
139 - hash_version, oid_version());
127 + hash_version, oid_version(the_hash_algo));
128 goto cleanup_fail;
129 }
130 m->hash_len = the_hash_algo->rawsz;
@@ -420,7 +408,7 @@ static size_t write_midx_header(struct hashfile *f,
408 {
409 hashwrite_be32(f, MIDX_SIGNATURE);
410 hashwrite_u8(f, MIDX_VERSION);
423 - hashwrite_u8(f, oid_version());
411 + hashwrite_u8(f, oid_version(the_hash_algo));
412 hashwrite_u8(f, num_chunks);
413 hashwrite_u8(f, 0); /* unused */
414 hashwrite_be32(f, num_packs);
pack-write.c
+2 -13
@@ -2,6 +2,7 @@
2 #include "pack.h"
3 #include "csum-file.h"
4 #include "remote.h"
5 +#include "chunk-format.h"
6
7 void reset_pack_idx_option(struct pack_idx_option *opts)
8 {
@@ -181,21 +182,9 @@ static int pack_order_cmp(const void *va, const void *vb, void *ctx)
182
183 static void write_rev_header(struct hashfile *f)
184 {
184 - uint32_t oid_version;
185 - switch (hash_algo_by_ptr(the_hash_algo)) {
186 - case GIT_HASH_SHA1:
187 - oid_version = 1;
188 - break;
189 - case GIT_HASH_SHA256:
190 - oid_version = 2;
191 - break;
192 - default:
193 - die("write_rev_header: unknown hash version");
194 - }
195 -
185 hashwrite_be32(f, RIDX_SIGNATURE);
186 hashwrite_be32(f, RIDX_VERSION);
198 - hashwrite_be32(f, oid_version);
187 + hashwrite_be32(f, oid_version(the_hash_algo));
188 }
189
190 static void write_rev_index_positions(struct hashfile *f,