hash: use uint32_t for object_id algorithm

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>

brian m. carlson committed Feb 7, 2026 at 20:04 UTC 67e526c33e025918c146c7cb61007cc2ffc46661
6 files changed +15 -15
hash.c
+3 -3
@@ -241,7 +241,7 @@ const char *empty_tree_oid_hex(const struct git_hash_algo *algop)
241 return oid_to_hex_r(buf, algop->empty_tree);
242 }
243
244 -int hash_algo_by_name(const char *name)
244 +uint32_t hash_algo_by_name(const char *name)
245 {
246 if (!name)
247 return GIT_HASH_UNKNOWN;
@@ -251,7 +251,7 @@ int hash_algo_by_name(const char *name)
251 return GIT_HASH_UNKNOWN;
252 }
253
254 -int hash_algo_by_id(uint32_t format_id)
254 +uint32_t hash_algo_by_id(uint32_t format_id)
255 {
256 for (size_t i = 1; i < GIT_HASH_NALGOS; i++)
257 if (format_id == hash_algos[i].format_id)
@@ -259,7 +259,7 @@ int hash_algo_by_id(uint32_t format_id)
259 return GIT_HASH_UNKNOWN;
260 }
261
262 -int hash_algo_by_length(size_t len)
262 +uint32_t hash_algo_by_length(size_t len)
263 {
264 for (size_t i = 1; i < GIT_HASH_NALGOS; i++)
265 if (len == hash_algos[i].rawsz)
hash.h
+5 -5
@@ -211,7 +211,7 @@ static inline void git_SHA256_Clone(git_SHA256_CTX *dst, const git_SHA256_CTX *s
211
212 struct object_id {
213 unsigned char hash[GIT_MAX_RAWSZ];
214 - int algo; /* XXX requires 4-byte alignment */
214 + uint32_t algo; /* XXX requires 4-byte alignment */
215 };
216
217 #define GET_OID_QUIETLY 01
@@ -344,13 +344,13 @@ static inline void git_hash_final_oid(struct object_id *oid, struct git_hash_ctx
344 * Return a GIT_HASH_* constant based on the name. Returns GIT_HASH_UNKNOWN if
345 * the name doesn't match a known algorithm.
346 */
347 -int hash_algo_by_name(const char *name);
347 +uint32_t hash_algo_by_name(const char *name);
348 /* Identical, except based on the format ID. */
349 -int hash_algo_by_id(uint32_t format_id);
349 +uint32_t hash_algo_by_id(uint32_t format_id);
350 /* Identical, except based on the length. */
351 -int hash_algo_by_length(size_t len);
351 +uint32_t hash_algo_by_length(size_t len);
352 /* Identical, except for a pointer to struct git_hash_algo. */
353 -static inline int hash_algo_by_ptr(const struct git_hash_algo *p)
353 +static inline uint32_t hash_algo_by_ptr(const struct git_hash_algo *p)
354 {
355 size_t i;
356 for (i = 0; i < GIT_HASH_NALGOS; i++) {
oidtree.c
+1 -1
@@ -10,7 +10,7 @@ struct oidtree_iter_data {
10 oidtree_iter fn;
11 void *arg;
12 size_t *last_nibble_at;
13 - int algo;
13 + uint32_t algo;
14 uint8_t last_byte;
15 };
16
repository.c
+3 -3
@@ -39,7 +39,7 @@ struct repository *the_repository = &the_repo;
39 static void set_default_hash_algo(struct repository *repo)
40 {
41 const char *hash_name;
42 - int algo;
42 + uint32_t algo;
43
44 hash_name = getenv("GIT_TEST_DEFAULT_HASH_ALGO");
45 if (!hash_name)
@@ -179,12 +179,12 @@ void repo_set_gitdir(struct repository *repo,
179 repo->gitdir, "index");
180 }
181
182 -void repo_set_hash_algo(struct repository *repo, int hash_algo)
182 +void repo_set_hash_algo(struct repository *repo, uint32_t hash_algo)
183 {
184 repo->hash_algo = &hash_algos[hash_algo];
185 }
186
187 -void repo_set_compat_hash_algo(struct repository *repo MAYBE_UNUSED, int algo)
187 +void repo_set_compat_hash_algo(struct repository *repo MAYBE_UNUSED, uint32_t algo)
188 {
189 #ifdef WITH_RUST
190 if (hash_algo_by_ptr(repo->hash_algo) == algo)
repository.h
+2 -2
@@ -202,8 +202,8 @@ struct set_gitdir_args {
202 void repo_set_gitdir(struct repository *repo, const char *root,
203 const struct set_gitdir_args *extra_args);
204 void repo_set_worktree(struct repository *repo, const char *path);
205 -void repo_set_hash_algo(struct repository *repo, int algo);
206 -void repo_set_compat_hash_algo(struct repository *repo, int compat_algo);
205 +void repo_set_hash_algo(struct repository *repo, uint32_t algo);
206 +void repo_set_compat_hash_algo(struct repository *repo, uint32_t compat_algo);
207 void repo_set_ref_storage_format(struct repository *repo,
208 enum ref_storage_format format);
209 void initialize_repository(struct repository *repo);
serve.c
+1 -1
@@ -14,7 +14,7 @@
14
15 static int advertise_sid = -1;
16 static int advertise_object_info = -1;
17 -static int client_hash_algo = GIT_HASH_SHA1_LEGACY;
17 +static uint32_t client_hash_algo = GIT_HASH_SHA1_LEGACY;
18
19 static int always_advertise(struct repository *r UNUSED,
20 struct strbuf *value UNUSED)