From: Jeff King <peff@peff.net>
To: git@vger.kernel.org
Cc: Patrick Steinhardt <ps@pks.im>,
"brian m. carlson" <sandals@crustytoothpaste.net>
Subject: [PATCH 3/7] hash: document function pointers and wrappers
Date: Tue, 7 Jul 2026 01:05:57 -0400 [thread overview]
Message-ID: <20260707050557.GC1288294@coredump.intra.peff.net> (raw)
In-Reply-To: <20260707045556.GA1288172@coredump.intra.peff.net>
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 <peff@peff.net>
---
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
next prev parent reply other threads:[~2026-07-07 5:05 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-07 4:55 [PATCH 0/7] git_hash_*() quality-of-life improvements Jeff King
2026-07-07 5:01 ` [PATCH 1/7] hash: use git_hash_init() consistently Jeff King
2026-07-07 14:39 ` Junio C Hamano
2026-07-07 20:13 ` Jeff King
2026-07-07 20:17 ` Junio C Hamano
2026-07-07 20:25 ` Jeff King
2026-07-07 21:25 ` brian m. carlson
2026-07-08 3:54 ` Jeff King
2026-07-07 5:04 ` [PATCH 2/7] hash: convert remaining direct function calls Jeff King
2026-07-07 16:15 ` Junio C Hamano
2026-07-07 5:05 ` Jeff King [this message]
2026-07-07 14:26 ` [PATCH 3/7] hash: document function pointers and wrappers Patrick Steinhardt
2026-07-07 20:05 ` Jeff King
2026-07-07 5:07 ` [PATCH 4/7] hash: make git_hash_discard() idempotent Jeff King
2026-07-07 16:22 ` Junio C Hamano
2026-07-07 20:18 ` Jeff King
2026-07-07 21:41 ` brian m. carlson
2026-07-07 22:25 ` Junio C Hamano
2026-07-07 5:07 ` [PATCH 5/7] csum-file: use idempotent git_hash_discard() Jeff King
2026-07-07 5:08 ` [PATCH 6/7] http: " Jeff King
2026-07-07 16:25 ` Junio C Hamano
2026-07-07 5:09 ` [PATCH 7/7] hash: check ctx->active flag in all wrapper functions Jeff King
2026-07-07 14:26 ` Patrick Steinhardt
2026-07-07 16:33 ` Junio C Hamano
2026-07-07 20:10 ` Jeff King
2026-07-07 14:26 ` [PATCH 0/7] git_hash_*() quality-of-life improvements Patrick Steinhardt
2026-07-08 3:52 ` [PATCH v2 " Jeff King
2026-07-08 3:52 ` [PATCH v2 1/7] hash: use git_hash_init() consistently Jeff King
2026-07-08 3:52 ` [PATCH v2 2/7] hash: convert remaining direct function calls Jeff King
2026-07-08 3:52 ` [PATCH v2 3/7] hash: document function pointers and wrappers Jeff King
2026-07-08 3:52 ` [PATCH v2 4/7] hash: make git_hash_discard() idempotent Jeff King
2026-07-08 3:53 ` [PATCH v2 5/7] csum-file: use idempotent git_hash_discard() Jeff King
2026-07-08 3:53 ` [PATCH v2 6/7] http: " Jeff King
2026-07-08 3:53 ` [PATCH v2 7/7] hash: check ctx->active flag in all wrapper functions Jeff King
2026-07-08 8:05 ` [PATCH v2 0/7] git_hash_*() quality-of-life improvements Patrick Steinhardt
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260707050557.GC1288294@coredump.intra.peff.net \
--to=peff@peff.net \
--cc=git@vger.kernel.org \
--cc=ps@pks.im \
--cc=sandals@crustytoothpaste.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox