hash: expose hash context functions to Rust

We'd like to be able to hash our data in Rust using the same contexts as in C. However, we need our helper functions to not be inline so they can be linked into the binary appropriately. In addition, to avoid managing memory manually and since we don't know the size of the hash context structure, we want to have simple alloc and free functions we can use to make sure a context can be easily dynamically created. Expose the helper functions and create alloc, free, and init functions we can call. 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 9a1fa36fe3dbed82ae7f9906ba1ac6d391c18f17
2 files changed +42 -20
hash.c
+35
@@ -248,6 +248,41 @@ const struct git_hash_algo *hash_algo_ptr_by_number(uint32_t algo)
248 return &hash_algos[algo];
249 }
250
251 +struct git_hash_ctx *git_hash_alloc(void)
252 +{
253 + return xmalloc(sizeof(struct git_hash_ctx));
254 +}
255 +
256 +void git_hash_free(struct git_hash_ctx *ctx)
257 +{
258 + free(ctx);
259 +}
260 +
261 +void git_hash_init(struct git_hash_ctx *ctx, const struct git_hash_algo *algop)
262 +{
263 + algop->init_fn(ctx);
264 +}
265 +
266 +void git_hash_clone(struct git_hash_ctx *dst, const struct git_hash_ctx *src)
267 +{
268 + src->algop->clone_fn(dst, src);
269 +}
270 +
271 +void git_hash_update(struct git_hash_ctx *ctx, const void *in, size_t len)
272 +{
273 + ctx->algop->update_fn(ctx, in, len);
274 +}
275 +
276 +void git_hash_final(unsigned char *hash, struct git_hash_ctx *ctx)
277 +{
278 + ctx->algop->final_fn(hash, ctx);
279 +}
280 +
281 +void git_hash_final_oid(struct object_id *oid, struct git_hash_ctx *ctx)
282 +{
283 + ctx->algop->final_oid_fn(oid, ctx);
284 +}
285 +
286 uint32_t hash_algo_by_name(const char *name)
287 {
288 if (!name)
hash.h
+7 -20
@@ -320,27 +320,14 @@ struct git_hash_algo {
320 };
321 extern const struct git_hash_algo hash_algos[GIT_HASH_NALGOS];
322
323 -static inline void git_hash_clone(struct git_hash_ctx *dst, const struct git_hash_ctx *src)
324 -{
325 - src->algop->clone_fn(dst, src);
326 -}
327 -
328 -static inline void git_hash_update(struct git_hash_ctx *ctx, const void *in, size_t len)
329 -{
330 - ctx->algop->update_fn(ctx, in, len);
331 -}
332 -
333 -static inline void git_hash_final(unsigned char *hash, struct git_hash_ctx *ctx)
334 -{
335 - ctx->algop->final_fn(hash, ctx);
336 -}
337 -
338 -static inline void git_hash_final_oid(struct object_id *oid, struct git_hash_ctx *ctx)
339 -{
340 - ctx->algop->final_oid_fn(oid, ctx);
341 -}
342 -
323 +void git_hash_init(struct git_hash_ctx *ctx, const struct git_hash_algo *algop);
324 +void git_hash_clone(struct git_hash_ctx *dst, const struct git_hash_ctx *src);
325 +void git_hash_update(struct git_hash_ctx *ctx, const void *in, size_t len);
326 +void git_hash_final(unsigned char *hash, struct git_hash_ctx *ctx);
327 +void git_hash_final_oid(struct object_id *oid, struct git_hash_ctx *ctx);
328 const struct git_hash_algo *hash_algo_ptr_by_number(uint32_t algo);
329 +struct git_hash_ctx *git_hash_alloc(void);
330 +void git_hash_free(struct git_hash_ctx *ctx);
331 /*
332 * Return a GIT_HASH_* constant based on the name. Returns GIT_HASH_UNKNOWN if
333 * the name doesn't match a known algorithm.