]> git.ipfire.org Git - thirdparty/git.git/commitdiff
hash: use uint32_t for object_id algorithm
authorbrian m. carlson <sandals@crustytoothpaste.net>
Mon, 17 Nov 2025 22:16:09 +0000 (22:16 +0000)
committerJunio C Hamano <gitster@pobox.com>
Mon, 17 Nov 2025 22:24:14 +0000 (14:24 -0800)
We currently use an int for this value, but we'll define this structure
from Rust in a future commit and we want to ensure that our data types
are exactly identical.  To make that possible, use a uint32_t for the
hash algorithm.

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
hash.c
hash.h
oidtree.c
repository.c
repository.h
serve.c

diff --git a/hash.c b/hash.c
index 4a04ecb50e8a0bf3298a6d6ab26bd0475b814dca..81b4f87027ebd2b01e134a48d98af2e350bf52eb 100644 (file)
--- a/hash.c
+++ b/hash.c
@@ -241,7 +241,7 @@ const char *empty_tree_oid_hex(const struct git_hash_algo *algop)
        return oid_to_hex_r(buf, algop->empty_tree);
 }
 
-int hash_algo_by_name(const char *name)
+uint32_t hash_algo_by_name(const char *name)
 {
        if (!name)
                return GIT_HASH_UNKNOWN;
@@ -251,7 +251,7 @@ int hash_algo_by_name(const char *name)
        return GIT_HASH_UNKNOWN;
 }
 
-int hash_algo_by_id(uint32_t format_id)
+uint32_t hash_algo_by_id(uint32_t format_id)
 {
        for (size_t i = 1; i < GIT_HASH_NALGOS; i++)
                if (format_id == hash_algos[i].format_id)
@@ -259,7 +259,7 @@ int hash_algo_by_id(uint32_t format_id)
        return GIT_HASH_UNKNOWN;
 }
 
-int hash_algo_by_length(size_t len)
+uint32_t hash_algo_by_length(size_t len)
 {
        for (size_t i = 1; i < GIT_HASH_NALGOS; i++)
                if (len == hash_algos[i].rawsz)
diff --git a/hash.h b/hash.h
index fae966b23c7508492cbfd94db9b872e38a5405b4..99c9c2a0a8624c3c0fadefc5b75c42ed76e753eb 100644 (file)
--- a/hash.h
+++ b/hash.h
@@ -211,7 +211,7 @@ static inline void git_SHA256_Clone(git_SHA256_CTX *dst, const git_SHA256_CTX *s
 
 struct object_id {
        unsigned char hash[GIT_MAX_RAWSZ];
-       int algo;       /* XXX requires 4-byte alignment */
+       uint32_t algo;  /* XXX requires 4-byte alignment */
 };
 
 #define GET_OID_QUIETLY                  01
@@ -344,13 +344,13 @@ static inline void git_hash_final_oid(struct object_id *oid, struct git_hash_ctx
  * Return a GIT_HASH_* constant based on the name.  Returns GIT_HASH_UNKNOWN if
  * the name doesn't match a known algorithm.
  */
-int hash_algo_by_name(const char *name);
+uint32_t hash_algo_by_name(const char *name);
 /* Identical, except based on the format ID. */
-int hash_algo_by_id(uint32_t format_id);
+uint32_t hash_algo_by_id(uint32_t format_id);
 /* Identical, except based on the length. */
-int hash_algo_by_length(size_t len);
+uint32_t hash_algo_by_length(size_t len);
 /* Identical, except for a pointer to struct git_hash_algo. */
-static inline int hash_algo_by_ptr(const struct git_hash_algo *p)
+static inline uint32_t hash_algo_by_ptr(const struct git_hash_algo *p)
 {
        size_t i;
        for (i = 0; i < GIT_HASH_NALGOS; i++) {
index 151568f74fbf13a53451a91b476946d0e08ed3dd..324de949344e69c6430d2b46f16b396205cb6177 100644 (file)
--- a/oidtree.c
+++ b/oidtree.c
@@ -10,7 +10,7 @@ struct oidtree_iter_data {
        oidtree_iter fn;
        void *arg;
        size_t *last_nibble_at;
-       int algo;
+       uint32_t algo;
        uint8_t last_byte;
 };
 
index 186d2c10288d670e005b0cc8b6cdff0239d53778..ebe719de3cf048c6488d351d89ee222f6546625e 100644 (file)
@@ -39,7 +39,7 @@ struct repository *the_repository = &the_repo;
 static void set_default_hash_algo(struct repository *repo)
 {
        const char *hash_name;
-       int algo;
+       uint32_t algo;
 
        hash_name = getenv("GIT_TEST_DEFAULT_HASH_ALGO");
        if (!hash_name)
@@ -186,12 +186,12 @@ void repo_set_gitdir(struct repository *repo,
                        repo->gitdir, "index");
 }
 
-void repo_set_hash_algo(struct repository *repo, int hash_algo)
+void repo_set_hash_algo(struct repository *repo, uint32_t hash_algo)
 {
        repo->hash_algo = &hash_algos[hash_algo];
 }
 
-void repo_set_compat_hash_algo(struct repository *repo MAYBE_UNUSED, int algo)
+void repo_set_compat_hash_algo(struct repository *repo MAYBE_UNUSED, uint32_t algo)
 {
 #ifdef WITH_RUST
        if (hash_algo_by_ptr(repo->hash_algo) == algo)
index 5808a5d610846a0e42233f66e56dcbcebbd3ecd0..c0a3543b24ad2672b1e01ae2a6cf657e6985a1d1 100644 (file)
@@ -193,8 +193,8 @@ struct set_gitdir_args {
 void repo_set_gitdir(struct repository *repo, const char *root,
                     const struct set_gitdir_args *extra_args);
 void repo_set_worktree(struct repository *repo, const char *path);
-void repo_set_hash_algo(struct repository *repo, int algo);
-void repo_set_compat_hash_algo(struct repository *repo, int compat_algo);
+void repo_set_hash_algo(struct repository *repo, uint32_t algo);
+void repo_set_compat_hash_algo(struct repository *repo, uint32_t compat_algo);
 void repo_set_ref_storage_format(struct repository *repo,
                                 enum ref_storage_format format);
 void initialize_repository(struct repository *repo);
diff --git a/serve.c b/serve.c
index 53ecab3b42b44fd6aa96eea1e1fdfeba9336e644..49a6e39b1dd25cc4341f5fef2ef5c7d623a73801 100644 (file)
--- a/serve.c
+++ b/serve.c
@@ -14,7 +14,7 @@
 
 static int advertise_sid = -1;
 static int advertise_object_info = -1;
-static int client_hash_algo = GIT_HASH_SHA1_LEGACY;
+static uint32_t client_hash_algo = GIT_HASH_SHA1_LEGACY;
 
 static int always_advertise(struct repository *r UNUSED,
                            struct strbuf *value UNUSED)