Subject: Re: [PATCH 01/14] oidtree: modernize the code a bit

From: Patrick Steinhardt <[email-masked]>
Date: Fri, 20 Mar 2026 07:40:10 +0100

On Thu, Mar 19, 2026 at 09:08:44AM -0700, Junio C Hamano wrote:
> Patrick Steinhardt <[email-masked]> writes:
> 
> > +void oidtree_each(struct oidtree *ot, const struct object_id *prefix,
> > +		  size_t prefix_hex_len, oidtree_each_cb cb, void *cb_data)
> >  {
> > -	size_t klen = oidhexsz / 2;
> > -	struct oidtree_iter_data x = { 0 };
> > -	assert(oidhexsz <= GIT_MAX_HEXSZ);
> > -
> > -	x.fn = fn;
> > -	x.arg = arg;
> > -	x.algo = oid->algo;
> > -	if (oidhexsz & 1) {
> > -		x.last_byte = oid->hash[klen];
> > -		x.last_nibble_at = &klen;
> > +	struct oidtree_each_data data = {
> > +		.cb = cb,
> > +		.cb_data = cb_data,
> > +		.algo = prefix->algo,
> > +	};
> > +	size_t klen = prefix_hex_len / 2;
> > +	assert(prefix_hex_len <= GIT_MAX_HEXSZ);
> 
> I know the original also used GIT_MAX_HEXSZ to clamp the length for
> sanity, but because we know what algorithm is in use, I wonder if we
> want to use the limit more specific to it.

That assumes that the passed prefix OID actually has an algorithm
attached to it, and that may not be the case. We could initialize the
overall oidtree with a hash algorithm in `oidtree_init()`, and if so we
can then become a bit more thorough with our asserts.

But I feel like that would go beyond the smallish cleanups that I'm
doing in this patch.

Patrick