git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 05/14] object-name: move logic to iterate through packed prefixed objects

From
Patrick Steinhardt <ps@pks.im>
Date
Mar 20, 2026, 07:07 UTC
Message-ID
<20260320-b4-pks-odb-source-abbrev-v2-5-fe65dcd8c735@pks.im>
In-Reply-To
<20260320-b4-pks-odb-source-abbrev-v2-0-fe65dcd8c735@pks.im>

Similar to the preceding commit, move the logic to iterate through objects that have a given prefix into "packfile.c".

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 object-name.c |  94 +++----------------------------
 packfile.c    | 174 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 181 insertions(+), 87 deletions(-)
diff --git a/object-name.c b/object-name.c
index 929a68dbd0..ff0de06ff9 100644
--- a/object-name.c
+++ b/object-name.c
@@ -100,8 +100,6 @@ static void update_candidates(struct disambiguate_state *ds, const struct object
 	/* otherwise, current can be discarded and candidate is still good */
 }
 
-static int match_hash(unsigned, const unsigned char *, const unsigned char *);
-
 static int match_prefix(const struct object_id *oid, struct object_info *oi UNUSED, void *arg)
 {
 	struct disambiguate_state *ds = arg;
@@ -122,103 +120,25 @@ static void find_short_object_filename(struct disambiguate_state *ds)
 		odb_source_loose_for_each_object(source, NULL, match_prefix, ds, &opts);
 }
 
-static int match_hash(unsigned len, const unsigned char *a, const unsigned char *b)
-{
-	do {
-		if (*a != *b)
-			return 0;
-		a++;
-		b++;
-		len -= 2;
-	} while (len > 1);
-	if (len)
-		if ((*a ^ *b) & 0xf0)
-			return 0;
-	return 1;
-}
-
-static void unique_in_midx(struct multi_pack_index *m,
-			   struct disambiguate_state *ds)
-{
-	for (; m; m = m->base_midx) {
-		uint32_t num, i, first = 0;
-		const struct object_id *current = NULL;
-		int len = ds->len > ds->repo->hash_algo->hexsz ?
-			ds->repo->hash_algo->hexsz : ds->len;
-
-		if (!m->num_objects)
-			continue;
-
-		num = m->num_objects + m->num_objects_in_base;
-
-		bsearch_one_midx(&ds->bin_pfx, m, &first);
-
-		/*
-		 * At this point, "first" is the location of the lowest
-		 * object with an object name that could match
-		 * "bin_pfx".  See if we have 0, 1 or more objects that
-		 * actually match(es).
-		 */
-		for (i = first; i < num && !ds->ambiguous; i++) {
-			struct object_id oid;
-			current = nth_midxed_object_oid(&oid, m, i);
-			if (!match_hash(len, ds->bin_pfx.hash, current->hash))
-				break;
-			update_candidates(ds, current);
-		}
-	}
-}
-
-static void unique_in_pack(struct packed_git *p,
-			   struct disambiguate_state *ds)
-{
-	uint32_t num, i, first = 0;
-	int len = ds->len > ds->repo->hash_algo->hexsz ?
-		ds->repo->hash_algo->hexsz : ds->len;
-
-	if (p->multi_pack_index)
-		return;
-
-	if (open_pack_index(p) || !p->num_objects)
-		return;
-
-	num = p->num_objects;
-	bsearch_pack(&ds->bin_pfx, p, &first);
-
-	/*
-	 * At this point, "first" is the location of the lowest object
-	 * with an object name that could match "bin_pfx".  See if we have
-	 * 0, 1 or more objects that actually match(es).
-	 */
-	for (i = first; i < num && !ds->ambiguous; i++) {
-		struct object_id oid;
-		nth_packed_object_id(&oid, p, i);
-		if (!match_hash(len, ds->bin_pfx.hash, oid.hash))
-			break;
-		update_candidates(ds, &oid);
-	}
-}
-
 static void find_short_packed_object(struct disambiguate_state *ds)
 {
+	struct odb_for_each_object_options opts = {
+		.prefix = &ds->bin_pfx,
+		.prefix_hex_len = ds->len,
+	};
 	struct odb_source *source;
-	struct packed_git *p;
 
 	/* Skip, unless oids from the storage hash algorithm are wanted */
 	if (ds->bin_pfx.algo && (&hash_algos[ds->bin_pfx.algo] != ds->repo->hash_algo))
 		return;
 
 	odb_prepare_alternates(ds->repo->objects);
-	for (source = ds->repo->objects->sources; source && !ds->ambiguous; source = source->next) {
-		struct multi_pack_index *m = get_multi_pack_index(source);
-		if (m)
-			unique_in_midx(m, ds);
-	}
+	for (source = ds->repo->objects->sources; source; source = source->next) {
+		struct odb_source_files *files = odb_source_files_downcast(source);
 
-	repo_for_each_pack(ds->repo, p) {
+		packfile_store_for_each_object(files->packed, NULL, match_prefix, ds, &opts);
 		if (ds->ambiguous)
 			break;
-		unique_in_pack(p, ds);
 	}
 }
 
diff --git a/packfile.c b/packfile.c
index a6f3d2035d..2539a371c1 100644
--- a/packfile.c
+++ b/packfile.c
@@ -2371,6 +2371,177 @@ static int packfile_store_for_each_object_wrapper(const struct object_id *oid,
 	}
 }
 
+static int match_hash(unsigned len, const unsigned char *a, const unsigned char *b)
+{
+	do {
+		if (*a != *b)
+			return 0;
+		a++;
+		b++;
+		len -= 2;
+	} while (len > 1);
+	if (len)
+		if ((*a ^ *b) & 0xf0)
+			return 0;
+	return 1;
+}
+
+static int for_each_prefixed_object_in_midx(
+	struct packfile_store *store,
+	struct multi_pack_index *m,
+	const struct odb_for_each_object_options *opts,
+	struct packfile_store_for_each_object_wrapper_data *data)
+{
+	int ret;
+
+	for (; m; m = m->base_midx) {
+		uint32_t num, i, first = 0;
+		int len = opts->prefix_hex_len > m->source->odb->repo->hash_algo->hexsz ?
+			m->source->odb->repo->hash_algo->hexsz : opts->prefix_hex_len;
+
+		if (!m->num_objects)
+			continue;
+
+		num = m->num_objects + m->num_objects_in_base;
+
+		bsearch_one_midx(opts->prefix, m, &first);
+
+		/*
+		 * At this point, "first" is the location of the lowest
+		 * object with an object name that could match "opts->prefix".
+		 * See if we have 0, 1 or more objects that actually match(es).
+		 */
+		for (i = first; i < num; i++) {
+			const struct object_id *current = NULL;
+			struct object_id oid;
+
+			current = nth_midxed_object_oid(&oid, m, i);
+
+			if (!match_hash(len, opts->prefix->hash, current->hash))
+				break;
+
+			if (data->request) {
+				struct object_info oi = *data->request;
+
+				ret = packfile_store_read_object_info(store, current,
+								      &oi, 0);
+				if (ret)
+					goto out;
+
+				ret = data->cb(&oid, &oi, data->cb_data);
+				if (ret)
+					goto out;
+			} else {
+				ret = data->cb(&oid, NULL, data->cb_data);
+				if (ret)
+					goto out;
+			}
+		}
+	}
+
+	ret = 0;
+
+out:
+	return ret;
+}
+
+static int for_each_prefixed_object_in_pack(
+	struct packfile_store *store,
+	struct packed_git *p,
+	const struct odb_for_each_object_options *opts,
+	struct packfile_store_for_each_object_wrapper_data *data)
+{
+	uint32_t num, i, first = 0;
+	int len = opts->prefix_hex_len > p->repo->hash_algo->hexsz ?
+		p->repo->hash_algo->hexsz : opts->prefix_hex_len;
+	int ret;
+
+	num = p->num_objects;
+	bsearch_pack(opts->prefix, p, &first);
+
+	/*
+	 * At this point, "first" is the location of the lowest object
+	 * with an object name that could match "bin_pfx".  See if we have
+	 * 0, 1 or more objects that actually match(es).
+	 */
+	for (i = first; i < num; i++) {
+		struct object_id oid;
+
+		nth_packed_object_id(&oid, p, i);
+		if (!match_hash(len, opts->prefix->hash, oid.hash))
+			break;
+
+		if (data->request) {
+			struct object_info oi = *data->request;
+
+			ret = packfile_store_read_object_info(store, &oid, &oi, 0);
+			if (ret)
+				goto out;
+
+			ret = data->cb(&oid, &oi, data->cb_data);
+			if (ret)
+				goto out;
+		} else {
+			ret = data->cb(&oid, NULL, data->cb_data);
+			if (ret)
+				goto out;
+		}
+	}
+
+	ret = 0;
+
+out:
+	return ret;
+}
+
+static int packfile_store_for_each_prefixed_object(
+	struct packfile_store *store,
+	const struct odb_for_each_object_options *opts,
+	struct packfile_store_for_each_object_wrapper_data *data)
+{
+	struct packfile_list_entry *e;
+	struct multi_pack_index *m;
+	bool pack_errors = false;
+	int ret;
+
+	if (opts->flags)
+		BUG("flags unsupported");
+
+	store->skip_mru_updates = true;
+
+	m = get_multi_pack_index(store->source);
+	if (m) {
+		ret = for_each_prefixed_object_in_midx(store, m, opts, data);
+		if (ret)
+			goto out;
+	}
+
+	for (e = packfile_store_get_packs(store); e; e = e->next) {
+		if (e->pack->multi_pack_index)
+			continue;
+
+		if (open_pack_index(e->pack)) {
+			pack_errors = true;
+			continue;
+		}
+
+		if (!e->pack->num_objects)
+			continue;
+
+		ret = for_each_prefixed_object_in_pack(store, e->pack, opts, data);
+		if (ret)
+			goto out;
+	}
+
+	ret = 0;
+
+out:
+	store->skip_mru_updates = false;
+	if (!ret && pack_errors)
+		ret = -1;
+	return ret;
+}
+
 int packfile_store_for_each_object(struct packfile_store *store,
 				   const struct object_info *request,
 				   odb_for_each_object_cb cb,
@@ -2386,6 +2557,9 @@ int packfile_store_for_each_object(struct packfile_store *store,
 	struct packfile_list_entry *e;
 	int pack_errors = 0, ret;
 
+	if (opts->prefix)
+		return packfile_store_for_each_prefixed_object(store, opts, &data);
+
 	store->skip_mru_updates = true;
 
 	for (e = packfile_store_get_packs(store); e; e = e->next) {
-- 
2.53.0.1055.ga2ffed1127.dirty
Previous: Patrick SteinhardtNext: Toon Claes
Message 36 of 52 in “odb: generic object name handling”
  1. 00/14 odb: generic object name handlingPatrick Steinhardt, Mar 19, 2026
  2. 01/14 oidtree: modernize the code a bitPatrick Steinhardt, Mar 19, 2026
  3. Junio C HamanoMar 19, 2026
  4. Patrick SteinhardtMar 20, 2026
  5. brian m. carlsonMar 20, 2026
  6. Patrick SteinhardtMar 23, 2026
  7. 02/14 oidtree: extend iteration to allow for arbitrary return codesPatrick Steinhardt, Mar 19, 2026
  8. Junio C HamanoMar 19, 2026
  9. Patrick SteinhardtMar 20, 2026
  10. Karthik NayakMar 19, 2026
  11. 03/14 odb: introduce `struct odb_for_each_object_options`Patrick Steinhardt, Mar 19, 2026
  12. Junio C HamanoMar 19, 2026
  13. Patrick SteinhardtMar 19, 2026
  14. Karthik NayakMar 20, 2026
  15. 04/14 object-name: move logic to iterate through loose prefixed objectsPatrick Steinhardt, Mar 19, 2026
  16. 05/14 object-name: move logic to iterate through packed prefixed objectsPatrick Steinhardt, Mar 19, 2026
  17. 06/14 object-name: extract function to parse object ID prefixesPatrick Steinhardt, Mar 19, 2026
  18. 07/14 object-name: backend-generic `repo_collect_ambiguous()`Patrick Steinhardt, Mar 19, 2026
  19. Junio C HamanoMar 19, 2026
  20. Patrick SteinhardtMar 19, 2026
  21. Karthik NayakMar 20, 2026
  22. 08/14 object-name: backend-generic `get_short_oid()`Patrick Steinhardt, Mar 19, 2026
  23. 09/14 object-name: merge `update_candidates()` and `match_prefix()`Patrick Steinhardt, Mar 19, 2026
  24. 10/14 object-name: abbreviate loose object names without `disambiguate_state`Patrick Steinhardt, Mar 19, 2026
  25. 11/14 object-name: simplify computing common prefixesPatrick Steinhardt, Mar 19, 2026
  26. Karthik NayakMar 20, 2026
  27. Patrick SteinhardtMar 20, 2026
  28. 12/14 object-name: move logic to compute loose abbreviation lengthPatrick Steinhardt, Mar 19, 2026
  29. 13/14 object-file: move logic to compute packed abbreviation lengthPatrick Steinhardt, Mar 19, 2026
  30. 14/14 odb: introduce generic `odb_find_abbrev_len()`Patrick Steinhardt, Mar 19, 2026
  31. 00/14 odb: generic object name handlingPatrick Steinhardt, Mar 20, 2026
  32. 01/14 oidtree: modernize the code a bitPatrick Steinhardt, Mar 20, 2026
  33. 02/14 oidtree: extend iteration to allow for arbitrary return codesPatrick Steinhardt, Mar 20, 2026
  34. 03/14 odb: introduce `struct odb_for_each_object_options`Patrick Steinhardt, Mar 20, 2026
  35. 04/14 object-name: move logic to iterate through loose prefixed objectsPatrick Steinhardt, Mar 20, 2026
  36. 05/14 object-name: move logic to iterate through packed prefixed objectsPatrick Steinhardt, Mar 20, 2026
  37. Toon ClaesMar 30, 2026
  38. 06/14 object-name: extract function to parse object ID prefixesPatrick Steinhardt, Mar 20, 2026
  39. 07/14 object-name: backend-generic `repo_collect_ambiguous()`Patrick Steinhardt, Mar 20, 2026
  40. 08/14 object-name: backend-generic `get_short_oid()`Patrick Steinhardt, Mar 20, 2026
  41. Toon ClaesMar 30, 2026
  42. 09/14 object-name: merge `update_candidates()` and `match_prefix()`Patrick Steinhardt, Mar 20, 2026
  43. 10/14 object-name: abbreviate loose object names without `disambiguate_state`Patrick Steinhardt, Mar 20, 2026
  44. 11/14 object-name: simplify computing common prefixesPatrick Steinhardt, Mar 20, 2026
  45. 12/14 object-name: move logic to compute loose abbreviation lengthPatrick Steinhardt, Mar 20, 2026
  46. 13/14 object-file: move logic to compute packed abbreviation lengthPatrick Steinhardt, Mar 20, 2026
  47. 14/14 odb: introduce generic `odb_find_abbrev_len()`Patrick Steinhardt, Mar 20, 2026
  48. Karthik NayakMar 20, 2026
  49. Patrick SteinhardtMar 20, 2026
  50. Toon ClaesMar 30, 2026
  51. Junio C HamanoMar 30, 2026
  52. Patrick SteinhardtMar 31, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.