odb/source-loose: wire up `find_abbrev_len()` callback

Move `odb_source_loose_find_abbrev_len()` and its associated helpers from "object-file.c" into "odb/source-loose.c" and wire it up as the `find_abbrev_len` callback of the loose source. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Jun 1, 2026 at 10:20 UTC 8a6da81cc113607bdc1ac08395f6e7121cd652e9
4 files changed +41 -52
object-file.c
-39
@@ -1662,45 +1662,6 @@ out:
1662 return ret;
1663 }
1664
1665 -struct find_abbrev_len_data {
1666 - const struct object_id *oid;
1667 - unsigned len;
1668 -};
1669 -
1670 -static int find_abbrev_len_cb(const struct object_id *oid,
1671 - struct object_info *oi UNUSED,
1672 - void *cb_data)
1673 -{
1674 - struct find_abbrev_len_data *data = cb_data;
1675 - unsigned len = oid_common_prefix_hexlen(oid, data->oid);
1676 - if (len != hash_algos[oid->algo].hexsz && len >= data->len)
1677 - data->len = len + 1;
1678 - return 0;
1679 -}
1680 -
1681 -int odb_source_loose_find_abbrev_len(struct odb_source *source,
1682 - const struct object_id *oid,
1683 - unsigned min_len,
1684 - unsigned *out)
1685 -{
1686 - struct odb_source_files *files = odb_source_files_downcast(source);
1687 - struct odb_for_each_object_options opts = {
1688 - .prefix = oid,
1689 - .prefix_hex_len = min_len,
1690 - };
1691 - struct find_abbrev_len_data data = {
1692 - .oid = oid,
1693 - .len = min_len,
1694 - };
1695 - int ret;
1696 -
1697 - ret = odb_source_for_each_object(&files->loose->base, NULL, find_abbrev_len_cb,
1698 - &data, &opts);
1699 - *out = data.len;
1700 -
1701 - return ret;
1702 -}
1703 -
1665 static int check_stream_oid(git_zstream *stream,
1666 const char *hdr,
1667 unsigned long size,
object-file.h
-12
@@ -110,18 +110,6 @@ int odb_source_loose_count_objects(struct odb_source *source,
110 enum odb_count_objects_flags flags,
111 unsigned long *out);
112
113 -/*
114 - * Find the shortest unique prefix for the given object ID, where `min_len` is
115 - * the minimum length that the prefix should have.
116 - *
117 - * Returns 0 on success, in which case the computed length will be written to
118 - * `out`. Otherwise, a negative error code is returned.
119 - */
120 -int odb_source_loose_find_abbrev_len(struct odb_source *source,
121 - const struct object_id *oid,
122 - unsigned min_len,
123 - unsigned *out);
124 -
113 /**
114 * format_object_header() is a thin wrapper around s xsnprintf() that
115 * writes the initial "<type> <obj-len>" part of the loose object
odb/source-files.c
+1 -1
@@ -136,7 +136,7 @@ static int odb_source_files_find_abbrev_len(struct odb_source *source,
136 if (ret < 0)
137 goto out;
138
139 - ret = odb_source_loose_find_abbrev_len(source, oid, len, &len);
139 + ret = odb_source_find_abbrev_len(&files->loose->base, oid, len, &len);
140 if (ret < 0)
141 goto out;
142
odb/source-loose.c
+40
@@ -481,6 +481,45 @@ static int odb_source_loose_for_each_object(struct odb_source *source,
481 NULL, NULL, &data);
482 }
483
484 +struct find_abbrev_len_data {
485 + const struct object_id *oid;
486 + unsigned len;
487 +};
488 +
489 +static int find_abbrev_len_cb(const struct object_id *oid,
490 + struct object_info *oi UNUSED,
491 + void *cb_data)
492 +{
493 + struct find_abbrev_len_data *data = cb_data;
494 + unsigned len = oid_common_prefix_hexlen(oid, data->oid);
495 + if (len != hash_algos[oid->algo].hexsz && len >= data->len)
496 + data->len = len + 1;
497 + return 0;
498 +}
499 +
500 +static int odb_source_loose_find_abbrev_len(struct odb_source *source,
501 + const struct object_id *oid,
502 + unsigned min_len,
503 + unsigned *out)
504 +{
505 + struct odb_source_loose *loose = odb_source_loose_downcast(source);
506 + struct odb_for_each_object_options opts = {
507 + .prefix = oid,
508 + .prefix_hex_len = min_len,
509 + };
510 + struct find_abbrev_len_data data = {
511 + .oid = oid,
512 + .len = min_len,
513 + };
514 + int ret;
515 +
516 + ret = odb_source_for_each_object(&loose->base, NULL, find_abbrev_len_cb,
517 + &data, &opts);
518 + *out = data.len;
519 +
520 + return ret;
521 +}
522 +
523 static void odb_source_loose_clear_cache(struct odb_source_loose *loose)
524 {
525 oidtree_clear(loose->cache);
@@ -537,6 +576,7 @@ struct odb_source_loose *odb_source_loose_new(struct odb_source_files *files)
576 loose->base.read_object_info = odb_source_loose_read_object_info;
577 loose->base.read_object_stream = odb_source_loose_read_object_stream;
578 loose->base.for_each_object = odb_source_loose_for_each_object;
579 + loose->base.find_abbrev_len = odb_source_loose_find_abbrev_len;
580
581 if (!is_absolute_path(loose->base.path))
582 chdir_notify_register(NULL, odb_source_loose_reparent, loose);