Skip to content

Commit d9fef9d

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

File tree

5 files changed

+23
-43
lines changed

5 files changed

+23
-43
lines changed

chunk-format.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,15 @@ int read_chunk(struct chunkfile *cf,
181181

182182
return CHUNK_NOT_FOUND;
183183
}
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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define CHUNK_FORMAT_H
33

44
#include "git-compat-util.h"
5+
#include "hash.h"
56

67
struct hashfile;
78
struct chunkfile;
@@ -65,4 +66,6 @@ int read_chunk(struct chunkfile *cf,
6566
chunk_read_fn fn,
6667
void *data);
6768

69+
uint8_t oid_version(const struct git_hash_algo *algop);
70+
6871
#endif

commit-graph.c

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -193,18 +193,6 @@ char *get_commit_graph_chain_filename(struct object_directory *odb)
193193
return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb->path);
194194
}
195195

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-
208196
static struct commit_graph *alloc_commit_graph(void)
209197
{
210198
struct commit_graph *g = xcalloc(1, sizeof(*g));
@@ -365,9 +353,9 @@ struct commit_graph *parse_commit_graph(struct repository *r,
365353
}
366354

367355
hash_version = *(unsigned char*)(data + 5);
368-
if (hash_version != oid_version()) {
356+
if (hash_version != oid_version(the_hash_algo)) {
369357
error(_("commit-graph hash version %X does not match version %X"),
370-
hash_version, oid_version());
358+
hash_version, oid_version(the_hash_algo));
371359
return NULL;
372360
}
373361

@@ -1921,7 +1909,7 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
19211909
hashwrite_be32(f, GRAPH_SIGNATURE);
19221910

19231911
hashwrite_u8(f, GRAPH_VERSION);
1924-
hashwrite_u8(f, oid_version());
1912+
hashwrite_u8(f, oid_version(the_hash_algo));
19251913
hashwrite_u8(f, get_num_chunks(cf));
19261914
hashwrite_u8(f, ctx->num_commit_graphs_after - 1);
19271915

midx.c

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,6 @@
4141

4242
#define PACK_EXPIRED UINT_MAX
4343

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-
5644
const unsigned char *get_midx_checksum(struct multi_pack_index *m)
5745
{
5846
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
134122
m->version);
135123

136124
hash_version = m->data[MIDX_BYTE_HASH_VERSION];
137-
if (hash_version != oid_version()) {
125+
if (hash_version != oid_version(the_hash_algo)) {
138126
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));
140128
goto cleanup_fail;
141129
}
142130
m->hash_len = the_hash_algo->rawsz;
@@ -420,7 +408,7 @@ static size_t write_midx_header(struct hashfile *f,
420408
{
421409
hashwrite_be32(f, MIDX_SIGNATURE);
422410
hashwrite_u8(f, MIDX_VERSION);
423-
hashwrite_u8(f, oid_version());
411+
hashwrite_u8(f, oid_version(the_hash_algo));
424412
hashwrite_u8(f, num_chunks);
425413
hashwrite_u8(f, 0); /* unused */
426414
hashwrite_be32(f, num_packs);

pack-write.c

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "pack.h"
33
#include "csum-file.h"
44
#include "remote.h"
5+
#include "chunk-format.h"
56

67
void reset_pack_idx_option(struct pack_idx_option *opts)
78
{
@@ -181,21 +182,9 @@ static int pack_order_cmp(const void *va, const void *vb, void *ctx)
181182

182183
static void write_rev_header(struct hashfile *f)
183184
{
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-
196185
hashwrite_be32(f, RIDX_SIGNATURE);
197186
hashwrite_be32(f, RIDX_VERSION);
198-
hashwrite_be32(f, oid_version);
187+
hashwrite_be32(f, oid_version(the_hash_algo));
199188
}
200189

201190
static void write_rev_index_positions(struct hashfile *f,

0 commit comments

Comments
 (0)