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

[PATCH 6/8] midx: pass down `hash_algo` to `get_midx_filename[_ext]`

From
Karthik Nayak <karthik.188@gmail.com>
Date
Nov 15, 2024, 13:42 UTC
Message-ID
<20241115-374-refactor-midx-c-and-midx-write-c-to-not-depend-on-global-state-v1-6-761f8a2c7775@gmail.com>
In-Reply-To
<20241115-374-refactor-midx-c-and-midx-write-c-to-not-depend-on-global-state-v1-0-761f8a2c7775@gmail.com>

The function `get_midx_filename_ext` uses `hash_to_hex` which internally uses the global variable `the_repository`. To remove this dependency, we pass down the `hash_algo` to both `get_midx_filename` and `get_midx_filename_ext`. While we add `the_repository` usage to `midx-write.c` for this reason, we'll resolve this in a future commit.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 midx-write.c    | 15 +++++++--------
 midx.c          | 16 +++++++++-------
 midx.h          |  7 +++++--
 pack-bitmap.c   |  6 +++---
 pack-revindex.c |  2 +-
 5 files changed, 25 insertions(+), 21 deletions(-)
diff --git a/midx-write.c b/midx-write.c
index 5af29899bbe279c7c3ff4bc2c65330620ce37ee2..7c1563845993075d622f59faeb25462180434abd 100644
--- a/midx-write.c
+++ b/midx-write.c
@@ -990,8 +990,8 @@ static int link_midx_to_chain(struct multi_pack_index *m)
 	for (i = 0; i < ARRAY_SIZE(midx_exts); i++) {
 		const unsigned char *hash = get_midx_checksum(m);
 
-		get_midx_filename_ext(&from, m->object_dir, hash,
-				      midx_exts[i].non_split);
+		get_midx_filename_ext(m->repo->hash_algo, &from, m->object_dir,
+				      hash, midx_exts[i].non_split);
 		get_split_midx_filename_ext(&to, m->object_dir, hash,
 					    midx_exts[i].split);
 
@@ -1011,9 +1011,8 @@ static int link_midx_to_chain(struct multi_pack_index *m)
 	return ret;
 }
 
-static void clear_midx_files(const char *object_dir,
-			     const char **hashes,
-			     uint32_t hashes_nr,
+static void clear_midx_files(struct repository *r, const char *object_dir,
+			     const char **hashes, uint32_t hashes_nr,
 			     unsigned incremental)
 {
 	/*
@@ -1038,7 +1037,7 @@ static void clear_midx_files(const char *object_dir,
 	}
 
 	if (incremental)
-		get_midx_filename(&buf, object_dir);
+		get_midx_filename(r->hash_algo, &buf, object_dir);
 	else
 		get_midx_chain_filename(&buf, object_dir);
 
@@ -1082,7 +1081,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
 			    "%s/pack/multi-pack-index.d/tmp_midx_XXXXXX",
 			    object_dir);
 	else
-		get_midx_filename(&midx_name, object_dir);
+		get_midx_filename(r->hash_algo, &midx_name, object_dir);
 	if (safe_create_leading_directories(midx_name.buf))
 		die_errno(_("unable to create leading directories of %s"),
 			  midx_name.buf);
@@ -1473,7 +1472,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
 	if (commit_lock_file(&lk) < 0)
 		die_errno(_("could not write multi-pack-index"));
 
-	clear_midx_files(object_dir, keep_hashes,
+	clear_midx_files(r, object_dir, keep_hashes,
 			 ctx.num_multi_pack_indexes_before + 1,
 			 ctx.incremental);
 
diff --git a/midx.c b/midx.c
index 98ee84d4a8bf388906634ad695ff39acdaa2c6d5..9bed4185ff4d44602aedfe0329dd840ff9e85435 100644
--- a/midx.c
+++ b/midx.c
@@ -28,17 +28,19 @@ const unsigned char *get_midx_checksum(struct multi_pack_index *m)
 	return m->data + m->data_len - m->repo->hash_algo->rawsz;
 }
 
-void get_midx_filename(struct strbuf *out, const char *object_dir)
+void get_midx_filename(const struct git_hash_algo *hash_algo,
+		       struct strbuf *out, const char *object_dir)
 {
-	get_midx_filename_ext(out, object_dir, NULL, NULL);
+	get_midx_filename_ext(hash_algo, out, object_dir, NULL, NULL);
 }
 
-void get_midx_filename_ext(struct strbuf *out, const char *object_dir,
+void get_midx_filename_ext(const struct git_hash_algo *hash_algo,
+			   struct strbuf *out, const char *object_dir,
 			   const unsigned char *hash, const char *ext)
 {
 	strbuf_addf(out, "%s/pack/multi-pack-index", object_dir);
 	if (ext)
-		strbuf_addf(out, "-%s.%s", hash_to_hex(hash), ext);
+		strbuf_addf(out, "-%s.%s", hash_to_hex_algop(hash, hash_algo), ext);
 }
 
 static int midx_read_oid_fanout(const unsigned char *chunk_start,
@@ -379,7 +381,7 @@ struct multi_pack_index *load_multi_pack_index(struct repository *r,
 	struct strbuf midx_name = STRBUF_INIT;
 	struct multi_pack_index *m;
 
-	get_midx_filename(&midx_name, object_dir);
+	get_midx_filename(r->hash_algo, &midx_name, object_dir);
 
 	m = load_multi_pack_index_one(r, object_dir,
 				      midx_name.buf, local);
@@ -822,7 +824,7 @@ void clear_midx_file(struct repository *r)
 {
 	struct strbuf midx = STRBUF_INIT;
 
-	get_midx_filename(&midx, r->objects->odb->path);
+	get_midx_filename(r->hash_algo, &midx, r->objects->odb->path);
 
 	if (r->objects && r->objects->multi_pack_index) {
 		close_midx(r->objects->multi_pack_index);
@@ -891,7 +893,7 @@ int verify_midx_file(struct repository *r, const char *object_dir, unsigned flag
 		struct stat sb;
 		struct strbuf filename = STRBUF_INIT;
 
-		get_midx_filename(&filename, object_dir);
+		get_midx_filename(r->hash_algo, &filename, object_dir);
 
 		if (!stat(filename.buf, &sb)) {
 			error(_("multi-pack-index file exists, but failed to parse"));
diff --git a/midx.h b/midx.h
index 78efa28d35371795fa33c68660278182debb60ab..7620820d4d0272926af9e4eeb68bfb73404c7ec2 100644
--- a/midx.h
+++ b/midx.h
@@ -7,6 +7,7 @@ struct object_id;
 struct pack_entry;
 struct repository;
 struct bitmapped_pack;
+struct git_hash_algo;
 
 #define MIDX_SIGNATURE 0x4d494458 /* "MIDX" */
 #define MIDX_VERSION 1
@@ -89,8 +90,10 @@ struct multi_pack_index {
 #define MIDX_EXT_MIDX "midx"
 
 const unsigned char *get_midx_checksum(struct multi_pack_index *m);
-void get_midx_filename(struct strbuf *out, const char *object_dir);
-void get_midx_filename_ext(struct strbuf *out, const char *object_dir,
+void get_midx_filename(const struct git_hash_algo *hash_algo,
+		       struct strbuf *out, const char *object_dir);
+void get_midx_filename_ext(const struct git_hash_algo *hash_algo,
+			   struct strbuf *out, const char *object_dir,
 			   const unsigned char *hash, const char *ext);
 void get_midx_chain_dirname(struct strbuf *buf, const char *object_dir);
 void get_midx_chain_filename(struct strbuf *buf, const char *object_dir);
diff --git a/pack-bitmap.c b/pack-bitmap.c
index 0cb1b56c9d5a55936ba53e2ff904ffe46cdcbafc..7b62d099cab5729a60a36b3ad15276fdc351aa97 100644
--- a/pack-bitmap.c
+++ b/pack-bitmap.c
@@ -375,8 +375,8 @@ static int load_bitmap_entries_v1(struct bitmap_index *index)
 char *midx_bitmap_filename(struct multi_pack_index *midx)
 {
 	struct strbuf buf = STRBUF_INIT;
-	get_midx_filename_ext(&buf, midx->object_dir, get_midx_checksum(midx),
-			      MIDX_EXT_BITMAP);
+	get_midx_filename_ext(midx->repo->hash_algo, &buf, midx->object_dir,
+			      get_midx_checksum(midx), MIDX_EXT_BITMAP);
 
 	return strbuf_detach(&buf, NULL);
 }
@@ -415,7 +415,7 @@ static int open_midx_bitmap_1(struct bitmap_index *bitmap_git,
 
 	if (bitmap_git->pack || bitmap_git->midx) {
 		struct strbuf buf = STRBUF_INIT;
-		get_midx_filename(&buf, midx->object_dir);
+		get_midx_filename(midx->repo->hash_algo, &buf, midx->object_dir);
 		trace2_data_string("bitmap", bitmap_repo(bitmap_git),
 				   "ignoring extra midx bitmap file", buf.buf);
 		close(fd);
diff --git a/pack-revindex.c b/pack-revindex.c
index 22d3c2346488de6279b6f26a69fe611106c1365a..d3832478d99edffae17db0bbe85aa981c1a3ad30 100644
--- a/pack-revindex.c
+++ b/pack-revindex.c
@@ -383,7 +383,7 @@ int load_midx_revindex(struct multi_pack_index *m)
 	trace2_data_string("load_midx_revindex", the_repository,
 			   "source", "rev");
 
-	get_midx_filename_ext(&revindex_name, m->object_dir,
+	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,
-- 
2.47.0
Previous: Karthik NayakNext: shejialuo
Message 17 of 70 in “Change midx.c and midx-write.c to not use global variables”
  1. 0/8 Change midx.c and midx-write.c to not use global variablesKarthik Nayak, Nov 15, 2024
  2. 1/8 builtin: pass repository to sub commandsKarthik Nayak, Nov 15, 2024
  3. shejialuoNov 16, 2024
  4. Patrick SteinhardtNov 18, 2024
  5. karthik nayakNov 18, 2024
  6. 2/8 midx-write: add repository field to `write_midx_context`Karthik Nayak, Nov 15, 2024
  7. brian m. carlsonNov 15, 2024
  8. karthik nayakNov 18, 2024
  9. Patrick SteinhardtNov 18, 2024
  10. karthik nayakNov 18, 2024
  11. 3/8 midx-write: pass down repository to `write_midx_file[_only]`Karthik Nayak, Nov 15, 2024
  12. Patrick SteinhardtNov 18, 2024
  13. 4/8 midx: cleanup internal usage of `the_repository` and `the_hash_algo`Karthik Nayak, Nov 15, 2024
  14. Patrick SteinhardtNov 18, 2024
  15. karthik nayakNov 18, 2024
  16. 5/8 midx: pass `repository` to `load_multi_pack_index`Karthik Nayak, Nov 15, 2024
  17. 6/8 midx: pass down `hash_algo` to `get_midx_filename[_ext]`Karthik Nayak, Nov 15, 2024
  18. shejialuoNov 16, 2024
  19. karthik nayakNov 18, 2024
  20. shejialuoNov 19, 2024
  21. 7/8 midx: pass down `hash_algo` to `get_split_midx_filename_ext`Karthik Nayak, Nov 15, 2024
  22. shejialuoNov 16, 2024
  23. 8/8 midx: inline the `MIDX_MIN_SIZE` definitionKarthik Nayak, Nov 15, 2024
  24. karthik nayakNov 15, 2024
  25. 00/10 Change midx.c and midx-write.c to not use global variablesKarthik Nayak, Nov 19, 2024
  26. 01/10 builtin: pass repository to sub commandsKarthik Nayak, Nov 19, 2024
  27. 03/10 midx-write: use `revs->repo` inside `read_refs_snapshot`Karthik Nayak, Nov 19, 2024
  28. shejialuoNov 20, 2024
  29. Richard KerryNov 20, 2024
  30. Taylor BlauNov 20, 2024
  31. karthik nayakNov 21, 2024
  32. Taylor BlauNov 20, 2024
  33. 05/10 midx-write: pass down repository to `write_midx_file[_only]`Karthik Nayak, Nov 19, 2024
  34. Taylor BlauNov 20, 2024
  35. 06/10 midx: cleanup internal usage of `the_repository` and `the_hash_algo`Karthik Nayak, Nov 19, 2024
  36. 07/10 midx: pass `repository` to `load_multi_pack_index`Karthik Nayak, Nov 19, 2024
  37. 08/10 midx: pass down `hash_algo` to `get_midx_filename[_ext]`Karthik Nayak, Nov 19, 2024
  38. Christian CouderNov 20, 2024
  39. karthik nayakNov 21, 2024
  40. 10/10 midx: inline the `MIDX_MIN_SIZE` definitionKarthik Nayak, Nov 19, 2024
  41. shejialuoNov 20, 2024
  42. karthik nayakNov 21, 2024
  43. 09/10 midx: pass down `hash_algo` to `get_split_midx_filename_ext`Karthik Nayak, Nov 19, 2024
  44. Christian CouderNov 20, 2024
  45. Taylor BlauNov 20, 2024
  46. karthik nayakNov 21, 2024
  47. 02/10 midx-write: pass down repository to static functionsKarthik Nayak, Nov 19, 2024
  48. Christian CouderNov 20, 2024
  49. Taylor BlauNov 20, 2024
  50. karthik nayakNov 21, 2024
  51. Taylor BlauNov 20, 2024
  52. karthik nayakNov 21, 2024
  53. 04/10 write-midx: add repository field to `write_midx_context`Karthik Nayak, Nov 19, 2024
  54. Christian CouderNov 20, 2024
  55. Taylor BlauNov 20, 2024
  56. Junio C HamanoNov 21, 2024
  57. Junio C HamanoNov 21, 2024
  58. karthik nayakNov 22, 2024
  59. 0/8 Change midx.c and midx-write.c to not use global variablesKarthik Nayak, Nov 27, 2024
  60. 1/8 midx-write: pass down repository to static functionsKarthik Nayak, Nov 27, 2024
  61. 2/8 midx-write: use `revs->repo` inside `read_refs_snapshot`Karthik Nayak, Nov 27, 2024
  62. 3/8 write-midx: add repository field to `write_midx_context`Karthik Nayak, Nov 27, 2024
  63. 4/8 midx-write: pass down repository to `write_midx_file[_only]`Karthik Nayak, Nov 27, 2024
  64. 5/8 midx: cleanup internal usage of `the_repository` and `the_hash_algo`Karthik Nayak, Nov 27, 2024
  65. 6/8 midx: pass `repository` to `load_multi_pack_index`Karthik Nayak, Nov 27, 2024
  66. 7/8 midx: pass down `hash_algo` to functions using global variablesKarthik Nayak, Nov 27, 2024
  67. 8/8 midx: inline the `MIDX_MIN_SIZE` definitionKarthik Nayak, Nov 27, 2024
  68. Junio C HamanoNov 28, 2024
  69. Patrick SteinhardtDec 3, 2024
  70. Junio C HamanoDec 3, 2024

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.