odb/source-inmemory: implement `find_abbrev_len()` callback

Implement the `find_abbrev_len()` callback function for the in-memory source. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Apr 10, 2026 at 14:12 UTC 3bd2856d3448943c4037d454f3e9cc0135330e73
1 file changed +39
odb/source-inmemory.c
+39
@@ -169,6 +169,44 @@ static int odb_source_inmemory_for_each_object(struct odb_source *source,
169 odb_source_inmemory_for_each_object_cb, &payload);
170 }
171
172 +struct find_abbrev_len_data {
173 + const struct object_id *oid;
174 + unsigned len;
175 +};
176 +
177 +static int find_abbrev_len_cb(const struct object_id *oid,
178 + struct object_info *oi UNUSED,
179 + void *cb_data)
180 +{
181 + struct find_abbrev_len_data *data = cb_data;
182 + unsigned len = oid_common_prefix_hexlen(oid, data->oid);
183 + if (len != hash_algos[oid->algo].hexsz && len >= data->len)
184 + data->len = len + 1;
185 + return 0;
186 +}
187 +
188 +static int odb_source_inmemory_find_abbrev_len(struct odb_source *source,
189 + const struct object_id *oid,
190 + unsigned min_len,
191 + unsigned *out)
192 +{
193 + struct odb_for_each_object_options opts = {
194 + .prefix = oid,
195 + .prefix_hex_len = min_len,
196 + };
197 + struct find_abbrev_len_data data = {
198 + .oid = oid,
199 + .len = min_len,
200 + };
201 + int ret;
202 +
203 + ret = odb_source_inmemory_for_each_object(source, NULL, find_abbrev_len_cb,
204 + &data, &opts);
205 + *out = data.len;
206 +
207 + return ret;
208 +}
209 +
210 static int odb_source_inmemory_write_object(struct odb_source *source,
211 const void *buf, unsigned long len,
212 enum object_type type,
@@ -275,6 +313,7 @@ struct odb_source_inmemory *odb_source_inmemory_new(struct object_database *odb)
313 source->base.read_object_info = odb_source_inmemory_read_object_info;
314 source->base.read_object_stream = odb_source_inmemory_read_object_stream;
315 source->base.for_each_object = odb_source_inmemory_for_each_object;
316 + source->base.find_abbrev_len = odb_source_inmemory_find_abbrev_len;
317 source->base.write_object = odb_source_inmemory_write_object;
318 source->base.write_object_stream = odb_source_inmemory_write_object_stream;
319