]> git.ipfire.org Git - thirdparty/git.git/commitdiff
chunk-format.h: extract oid_version()
authorTaylor Blau <me@ttaylorr.com>
Fri, 20 May 2022 23:17:41 +0000 (19:17 -0400)
committerJunio C Hamano <gitster@pobox.com>
Thu, 26 May 2022 22:48:26 +0000 (15:48 -0700)
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>
chunk-format.c
chunk-format.h
commit-graph.c
midx.c
pack-write.c

index 1c3dca62e2057bbdb8339e13e10f962befd26857..0275b74a895a17bb62b7fe12299e6f12aa503d92 100644 (file)
@@ -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"));
+       }
+}
index 9ccbe0037792c36620644996de66f5f779f93e98..7885aa084878dde2f2786545b7edc05e7d3fee24 100644 (file)
@@ -2,6 +2,7 @@
 #define CHUNK_FORMAT_H
 
 #include "git-compat-util.h"
+#include "hash.h"
 
 struct hashfile;
 struct chunkfile;
@@ -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
index 441b36016ba796c13665603b4c523c9baa52c12d..f430d5058bf50b111379a9b45aa33694acb46dbf 100644 (file)
@@ -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));
@@ -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;
        }
 
@@ -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);
 
diff --git a/midx.c b/midx.c
index 107365d2114ce2945d4221ff853df97829b22d05..d7459fd6164b1da4ccd1b9941eb944adb6dd69c1 100644 (file)
--- a/midx.c
+++ b/midx.c
 
 #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;
@@ -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;
@@ -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);
index a2adc565f450335850b3e636278560a8302ec392..27b171e440bfcb5ccf7c24bc41731846af5c5497 100644 (file)
@@ -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)
 {
@@ -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,