[PATCH 3/7] hash: document function pointers and wrappers
From: Jeff King <hidden>
Date: 2026-07-07 05:05:58
Subsystem:
the rest · Maintainer:
Linus Torvalds
We want people to use the git_hash_*() wrappers rather than the bare
function pointers in the git_hash_algo struct. Let's document them
rather than the bare pointers, and warn people away from the pointers.
Coccinelle will eventually force the use of the wrappers, but it's
helpful to lead readers in the right direction from the start.
While we're here we can document a few other bits of wisdom I've turned
up while working in this area:
- You have to initialize the destination of a git_hash_clone(). This
is something we may eventually change for efficiency, but we should
definitely document the requirement for now.
- You must eventually finalize or discard a hash, since some backends
may allocate resources during initialization.
Signed-off-by: Jeff King <redacted>
---
hash.h | 43 ++++++++++++++++++++++++++++++++-----------
1 file changed, 32 insertions(+), 11 deletions(-)
diff --git a/hash.h b/hash.h
index 0a23ef4dfd..5686914b71 100644
--- a/hash.h
+++ b/hash.h@@ -309,22 +309,15 @@ struct git_hash_algo { /* The block size of the hash. */ size_t blksz; - /* The hash initialization function. */ + /* + * Low-level implementation hooks. Callers should use the git_hash_* + * wrappers below rather than invoking these directly. + */ git_hash_init_fn init_fn; - - /* The hash context cloning function. */ git_hash_clone_fn clone_fn; - - /* The hash update function. */ git_hash_update_fn update_fn; - - /* The hash finalization function. */ git_hash_final_fn final_fn; - - /* The hash finalization function for object IDs. */ git_hash_final_oid_fn final_oid_fn; - - /* Discard an initialized hash without finalizing. */ git_hash_discard_fn discard_fn; /* The OID of the empty tree. */
@@ -341,12 +334,40 @@ struct git_hash_algo { }; extern const struct git_hash_algo hash_algos[GIT_HASH_NALGOS]; +/* + * Prepare an uninitialized hash context for use. You must eventually release + * the context with with git_hash_final() (or final_oid()) or by calling + * git_hash_discard(). + */ void git_hash_init(struct git_hash_ctx *ctx, const struct git_hash_algo *algop); + +/* + * Clone the state of a hash. Both src and dst must have been initialized with + * git_hash_init(). + */ void git_hash_clone(struct git_hash_ctx *dst, const struct git_hash_ctx *src); + +/* + * Add more data to an initialized hash context. + */ void git_hash_update(struct git_hash_ctx *ctx, const void *in, size_t len); + +/* + * Retrieve the final hash value from a context, releasing any resources. + */ void git_hash_final(unsigned char *hash, struct git_hash_ctx *ctx); + +/* + * Like git_hash_final(), but write the result into an object_id. + */ void git_hash_final_oid(struct object_id *oid, struct git_hash_ctx *ctx); + +/* + * Discard a hash context without computing the final value, but still + * releasing any resources. + */ void git_hash_discard(struct git_hash_ctx *ctx); + const struct git_hash_algo *hash_algo_ptr_by_number(uint32_t algo); struct git_hash_ctx *git_hash_alloc(void); void git_hash_free(struct git_hash_ctx *ctx);
--
2.55.0.459.g1b256877c9