Skip to content

Commit

Permalink
chunk-format.h: extract oid_version()
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
ttaylorr authored and gitster committed May 26, 2022
1 parent 1c573cd commit d9fef9d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 43 deletions.
12 changes: 12 additions & 0 deletions chunk-format.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,15 @@ int read_chunk(struct chunkfile *cf,

return CHUNK_NOT_FOUND;
}

uint8_t oid_version(const struct git_hash_algo *algop)
{
switch (hash_algo_by_ptr(algop)) {
case GIT_HASH_SHA1:
return 1;
case GIT_HASH_SHA256:
return 2;
default:
die(_("invalid hash version"));
}
}
3 changes: 3 additions & 0 deletions chunk-format.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define CHUNK_FORMAT_H

#include "git-compat-util.h"
#include "hash.h"

struct hashfile;
struct chunkfile;
Expand Down Expand Up @@ -65,4 +66,6 @@ int read_chunk(struct chunkfile *cf,
chunk_read_fn fn,
void *data);

uint8_t oid_version(const struct git_hash_algo *algop);

#endif
18 changes: 3 additions & 15 deletions commit-graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,18 +193,6 @@ char *get_commit_graph_chain_filename(struct object_directory *odb)
return xstrfmt("%s/info/commit-graphs/commit-graph-chain", odb->path);
}

static uint8_t oid_version(void)
{
switch (hash_algo_by_ptr(the_hash_algo)) {
case GIT_HASH_SHA1:
return 1;
case GIT_HASH_SHA256:
return 2;
default:
die(_("invalid hash version"));
}
}

static struct commit_graph *alloc_commit_graph(void)
{
struct commit_graph *g = xcalloc(1, sizeof(*g));
Expand Down Expand Up @@ -365,9 +353,9 @@ struct commit_graph *parse_commit_graph(struct repository *r,
}

hash_version = *(unsigned char*)(data + 5);
if (hash_version != oid_version()) {
if (hash_version != oid_version(the_hash_algo)) {
error(_("commit-graph hash version %X does not match version %X"),
hash_version, oid_version());
hash_version, oid_version(the_hash_algo));
return NULL;
}

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

hashwrite_u8(f, GRAPH_VERSION);
hashwrite_u8(f, oid_version());
hashwrite_u8(f, oid_version(the_hash_algo));
hashwrite_u8(f, get_num_chunks(cf));
hashwrite_u8(f, ctx->num_commit_graphs_after - 1);

Expand Down
18 changes: 3 additions & 15 deletions midx.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,6 @@

#define PACK_EXPIRED UINT_MAX

static uint8_t oid_version(void)
{
switch (hash_algo_by_ptr(the_hash_algo)) {
case GIT_HASH_SHA1:
return 1;
case GIT_HASH_SHA256:
return 2;
default:
die(_("invalid hash version"));
}
}

const unsigned char *get_midx_checksum(struct multi_pack_index *m)
{
return m->data + m->data_len - the_hash_algo->rawsz;
Expand Down Expand Up @@ -134,9 +122,9 @@ struct multi_pack_index *load_multi_pack_index(const char *object_dir, int local
m->version);

hash_version = m->data[MIDX_BYTE_HASH_VERSION];
if (hash_version != oid_version()) {
if (hash_version != oid_version(the_hash_algo)) {
error(_("multi-pack-index hash version %u does not match version %u"),
hash_version, oid_version());
hash_version, oid_version(the_hash_algo));
goto cleanup_fail;
}
m->hash_len = the_hash_algo->rawsz;
Expand Down Expand Up @@ -420,7 +408,7 @@ static size_t write_midx_header(struct hashfile *f,
{
hashwrite_be32(f, MIDX_SIGNATURE);
hashwrite_u8(f, MIDX_VERSION);
hashwrite_u8(f, oid_version());
hashwrite_u8(f, oid_version(the_hash_algo));
hashwrite_u8(f, num_chunks);
hashwrite_u8(f, 0); /* unused */
hashwrite_be32(f, num_packs);
Expand Down
15 changes: 2 additions & 13 deletions pack-write.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "pack.h"
#include "csum-file.h"
#include "remote.h"
#include "chunk-format.h"

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

static void write_rev_header(struct hashfile *f)
{
uint32_t oid_version;
switch (hash_algo_by_ptr(the_hash_algo)) {
case GIT_HASH_SHA1:
oid_version = 1;
break;
case GIT_HASH_SHA256:
oid_version = 2;
break;
default:
die("write_rev_header: unknown hash version");
}

hashwrite_be32(f, RIDX_SIGNATURE);
hashwrite_be32(f, RIDX_VERSION);
hashwrite_be32(f, oid_version);
hashwrite_be32(f, oid_version(the_hash_algo));
}

static void write_rev_index_positions(struct hashfile *f,
Expand Down

0 comments on commit d9fef9d

Please sign in to comment.