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

[PATCH v3 06/12] pack-revindex: stop depending on `the_repository`

From
Patrick Steinhardt <ps@pks.im>
Date
Mar 7, 2025, 14:18 UTC
Message-ID
<20250307-b4-pks-objects-without-the-repository-v3-6-7bccf408731e@pks.im>
In-Reply-To
<20250307-b4-pks-objects-without-the-repository-v3-0-7bccf408731e@pks.im>

There are multiple sites in "pack-revindex.c" where we use the global `the_repository` variable, either explicitly or implicitly by using `the_hash_algo`. In all of those cases we already have a repository available in the calling context though.

Refactor the code to instead use the caller-provided repository and remove the `USE_THE_REPOSITORY_VARIABLE` define.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 pack-revindex.c | 34 ++++++++++++++++++++--------------
 1 file changed, 20 insertions(+), 14 deletions(-)
diff --git a/pack-revindex.c b/pack-revindex.c
index 78139e3d7f5..038e0c96b1c 100644
--- a/pack-revindex.c
+++ b/pack-revindex.c
@@ -1,5 +1,3 @@
-#define USE_THE_REPOSITORY_VARIABLE
-
 #include "git-compat-util.h"
 #include "gettext.h"
 #include "pack-revindex.h"
@@ -9,6 +7,7 @@
 #include "strbuf.h"
 #include "trace2.h"
 #include "parse.h"
+#include "repository.h"
 #include "midx.h"
 #include "csum-file.h"
 
@@ -137,7 +136,7 @@ static void create_pack_revindex(struct packed_git *p)
 	const unsigned num_ent = p->num_objects;
 	unsigned i;
 	const char *index = p->index_data;
-	const unsigned hashsz = the_hash_algo->rawsz;
+	const unsigned hashsz = p->repo->hash_algo->rawsz;
 
 	ALLOC_ARRAY(p->revindex, num_ent + 1);
 	index += 4 * 256;
@@ -193,7 +192,11 @@ static char *pack_revindex_filename(struct packed_git *p)
 }
 
 #define RIDX_HEADER_SIZE (12)
-#define RIDX_MIN_SIZE (RIDX_HEADER_SIZE + (2 * the_hash_algo->rawsz))
+
+static size_t ridx_min_size(const struct git_hash_algo *algo)
+{
+	return RIDX_HEADER_SIZE + (2 * algo->rawsz);
+}
 
 struct revindex_header {
 	uint32_t signature;
@@ -201,7 +204,8 @@ struct revindex_header {
 	uint32_t hash_id;
 };
 
-static int load_revindex_from_disk(char *revindex_name,
+static int load_revindex_from_disk(const struct git_hash_algo *algo,
+				   char *revindex_name,
 				   uint32_t num_objects,
 				   const uint32_t **data_p, size_t *len_p)
 {
@@ -228,12 +232,12 @@ static int load_revindex_from_disk(char *revindex_name,
 
 	revindex_size = xsize_t(st.st_size);
 
-	if (revindex_size < RIDX_MIN_SIZE) {
+	if (revindex_size < ridx_min_size(algo)) {
 		ret = error(_("reverse-index file %s is too small"), revindex_name);
 		goto cleanup;
 	}
 
-	if (revindex_size - RIDX_MIN_SIZE != st_mult(sizeof(uint32_t), num_objects)) {
+	if (revindex_size - ridx_min_size(algo) != st_mult(sizeof(uint32_t), num_objects)) {
 		ret = error(_("reverse-index file %s is corrupt"), revindex_name);
 		goto cleanup;
 	}
@@ -279,7 +283,8 @@ int load_pack_revindex_from_disk(struct packed_git *p)
 
 	revindex_name = pack_revindex_filename(p);
 
-	ret = load_revindex_from_disk(revindex_name,
+	ret = load_revindex_from_disk(p->repo->hash_algo,
+				      revindex_name,
 				      p->num_objects,
 				      &p->revindex_map,
 				      &p->revindex_size);
@@ -322,7 +327,7 @@ int verify_pack_revindex(struct packed_git *p)
 	if (!p->revindex_map || !p->revindex_data)
 		return res;
 
-	if (!hashfile_checksum_valid(the_repository->hash_algo,
+	if (!hashfile_checksum_valid(p->repo->hash_algo,
 				     (const unsigned char *)p->revindex_map, p->revindex_size)) {
 		error(_("invalid checksum"));
 		res = -1;
@@ -375,19 +380,20 @@ int load_midx_revindex(struct multi_pack_index *m)
 		 * not want to accidentally call munmap() in the middle of the
 		 * MIDX.
 		 */
-		trace2_data_string("load_midx_revindex", the_repository,
+		trace2_data_string("load_midx_revindex", m->repo,
 				   "source", "midx");
 		m->revindex_data = (const uint32_t *)m->chunk_revindex;
 		return 0;
 	}
 
-	trace2_data_string("load_midx_revindex", the_repository,
+	trace2_data_string("load_midx_revindex", m->repo,
 			   "source", "rev");
 
 	get_midx_filename_ext(m->repo->hash_algo, &revindex_name, m->object_dir,
 			      get_midx_checksum(m), MIDX_EXT_REV);
 
-	ret = load_revindex_from_disk(revindex_name.buf,
+	ret = load_revindex_from_disk(m->repo->hash_algo,
+				      revindex_name.buf,
 				      m->num_objects,
 				      &m->revindex_map,
 				      &m->revindex_len);
@@ -419,7 +425,7 @@ int offset_to_pack_pos(struct packed_git *p, off_t ofs, uint32_t *pos)
 {
 	unsigned lo, hi;
 
-	if (load_pack_revindex(the_repository, p) < 0)
+	if (load_pack_revindex(p->repo, p) < 0)
 		return -1;
 
 	lo = 0;
@@ -465,7 +471,7 @@ off_t pack_pos_to_offset(struct packed_git *p, uint32_t pos)
 	if (p->revindex)
 		return p->revindex[pos].offset;
 	else if (pos == p->num_objects)
-		return p->pack_size - the_hash_algo->rawsz;
+		return p->pack_size - p->repo->hash_algo->rawsz;
 	else
 		return nth_packed_object_offset(p, pack_pos_to_index(p, pos));
 }
-- 
2.49.0.rc1.455.g4cd33545ba.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 48 of 73 in “Stop depending on `the_repository` in object-related subsystems”
  1. 00/12 Stop depending on `the_repository` in object-related subsystemsPatrick Steinhardt, Mar 3, 2025
  2. 01/12 csum-file: stop depending on `the_repository`Patrick Steinhardt, Mar 3, 2025
  3. Karthik NayakMar 6, 2025
  4. 03/12 pack-write: stop depending on `the_repository` and `the_hash_algo`Patrick Steinhardt, Mar 3, 2025
  5. Justin ToblerMar 4, 2025
  6. 02/12 object: stop depending on `the_repository`Patrick Steinhardt, Mar 3, 2025
  7. Karthik NayakMar 6, 2025
  8. Patrick SteinhardtMar 6, 2025
  9. 05/12 pack-check: stop depending on `the_repository`Patrick Steinhardt, Mar 3, 2025
  10. Karthik NayakMar 6, 2025
  11. 04/12 environment: move access to "core.bigFileThreshold" into repo settingsPatrick Steinhardt, Mar 3, 2025
  12. Justin ToblerMar 4, 2025
  13. Patrick SteinhardtMar 6, 2025
  14. 06/12 pack-revindex: stop depending on `the_repository`Patrick Steinhardt, Mar 3, 2025
  15. 07/12 pack-bitmap-write: stop depending on `the_repository`Patrick Steinhardt, Mar 3, 2025
  16. 08/12 object-file-convert: stop depending on `the_repository`Patrick Steinhardt, Mar 3, 2025
  17. Justin ToblerMar 4, 2025
  18. 09/12 delta-islands: stop depending on `the_repository`Patrick Steinhardt, Mar 3, 2025
  19. Justin ToblerMar 4, 2025
  20. 10/12 object-file: split out logic regarding hash algorithmsPatrick Steinhardt, Mar 3, 2025
  21. 11/12 hash: fix "-Wsign-compare" warningsPatrick Steinhardt, Mar 3, 2025
  22. 12/12 hash: stop depending on `the_repository` in `null_oid()`Patrick Steinhardt, Mar 3, 2025
  23. Justin ToblerMar 4, 2025
  24. Karthik NayakMar 6, 2025
  25. 00/12 Stop depending on `the_repository` in object-related subsystemsPatrick Steinhardt, Mar 6, 2025
  26. 01/12 csum-file: stop depending on `the_repository`Patrick Steinhardt, Mar 6, 2025
  27. 02/12 object: stop depending on `the_repository`Patrick Steinhardt, Mar 6, 2025
  28. 03/12 pack-write: stop depending on `the_repository` and `the_hash_algo`Patrick Steinhardt, Mar 6, 2025
  29. 04/12 environment: move access to "core.bigFileThreshold" into repo settingsPatrick Steinhardt, Mar 6, 2025
  30. 05/12 pack-check: stop depending on `the_repository`Patrick Steinhardt, Mar 6, 2025
  31. 06/12 pack-revindex: stop depending on `the_repository`Patrick Steinhardt, Mar 6, 2025
  32. 07/12 pack-bitmap-write: stop depending on `the_repository`Patrick Steinhardt, Mar 6, 2025
  33. 08/12 object-file-convert: stop depending on `the_repository`Patrick Steinhardt, Mar 6, 2025
  34. 09/12 delta-islands: stop depending on `the_repository`Patrick Steinhardt, Mar 6, 2025
  35. 11/12 hash: fix "-Wsign-compare" warningsPatrick Steinhardt, Mar 6, 2025
  36. 10/12 object-file: split out logic regarding hash algorithmsPatrick Steinhardt, Mar 6, 2025
  37. 12/12 hash: stop depending on `the_repository` in `null_oid()`Patrick Steinhardt, Mar 6, 2025
  38. Junio C HamanoMar 6, 2025
  39. Patrick SteinhardtMar 7, 2025
  40. Junio C HamanoMar 7, 2025
  41. Karthik NayakMar 6, 2025
  42. 00/12 Stop depending on `the_repository` in object-related subsystemsPatrick Steinhardt, Mar 7, 2025
  43. 02/12 object: stop depending on `the_repository`Patrick Steinhardt, Mar 7, 2025
  44. 01/12 csum-file: stop depending on `the_repository`Patrick Steinhardt, Mar 7, 2025
  45. 03/12 pack-write: stop depending on `the_repository` and `the_hash_algo`Patrick Steinhardt, Mar 7, 2025
  46. 05/12 pack-check: stop depending on `the_repository`Patrick Steinhardt, Mar 7, 2025
  47. 04/12 environment: move access to "core.bigFileThreshold" into repo settingsPatrick Steinhardt, Mar 7, 2025
  48. 06/12 pack-revindex: stop depending on `the_repository`Patrick Steinhardt, Mar 7, 2025
  49. 07/12 pack-bitmap-write: stop depending on `the_repository`Patrick Steinhardt, Mar 7, 2025
  50. 08/12 object-file-convert: stop depending on `the_repository`Patrick Steinhardt, Mar 7, 2025
  51. 09/12 delta-islands: stop depending on `the_repository`Patrick Steinhardt, Mar 7, 2025
  52. 10/12 object-file: split out logic regarding hash algorithmsPatrick Steinhardt, Mar 7, 2025
  53. 11/12 hash: fix "-Wsign-compare" warningsPatrick Steinhardt, Mar 7, 2025
  54. 12/12 hash: stop depending on `the_repository` in `null_oid()`Patrick Steinhardt, Mar 7, 2025
  55. Elijah NewrenMar 8, 2025
  56. Patrick SteinhardtMar 10, 2025
  57. Elijah NewrenMar 10, 2025
  58. Junio C HamanoMar 10, 2025
  59. Elijah NewrenMar 8, 2025
  60. 00/12 Stop depending on `the_repository` in object-related subsystemsPatrick Steinhardt, Mar 10, 2025
  61. 01/12 csum-file: stop depending on `the_repository`Patrick Steinhardt, Mar 10, 2025
  62. 02/12 object: stop depending on `the_repository`Patrick Steinhardt, Mar 10, 2025
  63. 03/12 pack-write: stop depending on `the_repository` and `the_hash_algo`Patrick Steinhardt, Mar 10, 2025
  64. 04/12 environment: move access to "core.bigFileThreshold" into repo settingsPatrick Steinhardt, Mar 10, 2025
  65. 05/12 pack-check: stop depending on `the_repository`Patrick Steinhardt, Mar 10, 2025
  66. 06/12 pack-revindex: stop depending on `the_repository`Patrick Steinhardt, Mar 10, 2025
  67. 07/12 pack-bitmap-write: stop depending on `the_repository`Patrick Steinhardt, Mar 10, 2025
  68. 08/12 object-file-convert: stop depending on `the_repository`Patrick Steinhardt, Mar 10, 2025
  69. 09/12 delta-islands: stop depending on `the_repository`Patrick Steinhardt, Mar 10, 2025
  70. 11/12 hash: fix "-Wsign-compare" warningsPatrick Steinhardt, Mar 10, 2025
  71. 10/12 object-file: split out logic regarding hash algorithmsPatrick Steinhardt, Mar 10, 2025
  72. 12/12 hash: stop depending on `the_repository` in `null_oid()`Patrick Steinhardt, Mar 10, 2025
  73. Elijah NewrenMar 10, 2025

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.