git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/8] Change midx.c and midx-write.c to not use global variables
@ 2024-11-15 13:42 Karthik Nayak
  2024-11-15 13:42 ` [PATCH 1/8] builtin: pass repository to sub commands Karthik Nayak
                   ` (9 more replies)
  0 siblings, 10 replies; 70+ messages in thread
From: Karthik Nayak @ 2024-11-15 13:42 UTC (permalink / raw)
  To: git; +Cc: Karthik Nayak, me, peff

Similar to the earlier patch series on cleaning up packfile.c and
removing usage of global variables [1], we change the midx.c and
midx-write.c files to no longer use global variables.

This is done by the following:
  - Usage of repository variable already available in existing structs.
  - Passing down repository variable from other subsystems.
  - Modifying all subcommands to obtain repository variable from the
  command in `builtins/` and passing down the variable from there.

The biggest change is in the first commit, wherein we modify all
subcommands to add the repository variable. Since the subcommand
definition are not often changed, it shouldn't cause too many conflicts
with in flight topics.

Since the `packfile.c` cleanup is still in flight, this series is based
on top of master: b31fb630c0 (Merge https://github.com/j6t/git-gui,
2024-11-11) with those patches merged in.

[1]: https://lore.kernel.org/git/cover.1729504640.git.karthik.188@gmail.com/

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
Karthik Nayak (8):
      builtin: pass repository to sub commands
      midx-write: add repository field to `write_midx_context`
      midx-write: pass down repository to `write_midx_file[_only]`
      midx: cleanup internal usage of `the_repository` and `the_hash_algo`
      midx: pass `repository` to `load_multi_pack_index`
      midx: pass down `hash_algo` to `get_midx_filename[_ext]`
      midx: pass down `hash_algo` to `get_split_midx_filename_ext`
      midx: inline the `MIDX_MIN_SIZE` definition

 builtin/bisect.c              |  32 +++++++----
 builtin/bundle.c              |  16 ++++--
 builtin/commit-graph.c        |  10 ++--
 builtin/config.c              |  25 +++++---
 builtin/gc.c                  |  21 ++++---
 builtin/hook.c                |   7 ++-
 builtin/multi-pack-index.c    |  20 ++++---
 builtin/notes.c               |  36 +++++++-----
 builtin/reflog.c              |  17 ++++--
 builtin/refs.c                |  10 ++--
 builtin/remote.c              |  34 +++++++----
 builtin/repack.c              |   2 +-
 builtin/sparse-checkout.c     |  25 +++++---
 builtin/stash.c               |  39 ++++++++-----
 builtin/submodule--helper.c   |  46 +++++++++------
 builtin/worktree.c            |  28 +++++----
 midx-write.c                  | 129 +++++++++++++++++++++---------------------
 midx.c                        |  88 ++++++++++++++--------------
 midx.h                        |  24 ++++----
 pack-bitmap.c                 |   6 +-
 pack-revindex.c               |   2 +-
 parse-options.h               |   4 +-
 t/helper/test-parse-options.c |  11 +++-
 t/helper/test-read-midx.c     |   8 +--
 24 files changed, 380 insertions(+), 260 deletions(-)
---
base-commit: b31fb630c0fc6869a33ed717163e8a1210460d94
change-id: 20241111-374-refactor-midx-c-and-midx-write-c-to-not-depend-on-global-state-a88498c2590f
prerequisite-message-id: <cover.1731323350.git.karthik.188@gmail.com>
prerequisite-patch-id: 0c0b2f7ab45aa1c6a14b43577b527e388fbdbc38
prerequisite-patch-id: e3c455e9206c11da6df31b4fed1bb5a50f4b05a2
prerequisite-patch-id: d85a7bba34e100fe2fdcd69f9710d3791e5ea1bf
prerequisite-patch-id: d3ad99374f4cca79ab58862ba31f61970e18eec2
prerequisite-patch-id: bc4ede2ad40588cc2a65598e875da121251012f9
prerequisite-patch-id: 74b62a5d62b178284a60d376aa0425758f15f514
prerequisite-patch-id: 4a70e65446538fff399de3b019a5c187e6ee9895
prerequisite-patch-id: bf5a723ee69217d389fc735b19b1cd04fd7a389a
prerequisite-patch-id: 9d796bcf18fea156bd225787dbb4b8b0e2a7d23b

Best regards,
-- 
Karthik Nayak <karthik.188@gmail.com>


^ permalink raw reply	[flat|nested] 70+ messages in thread

* [PATCH 1/8] builtin: pass repository to sub commands
  2024-11-15 13:42 [PATCH 0/8] Change midx.c and midx-write.c to not use global variables Karthik Nayak
@ 2024-11-15 13:42 ` Karthik Nayak
  2024-11-16 12:49   ` shejialuo
  2024-11-15 13:42 ` [PATCH 2/8] midx-write: add repository field to `write_midx_context` Karthik Nayak
                   ` (8 subsequent siblings)
  9 siblings, 1 reply; 70+ messages in thread
From: Karthik Nayak @ 2024-11-15 13:42 UTC (permalink / raw)
  To: git; +Cc: Karthik Nayak, me, peff

In 9b1cb5070f (builtin: add a repository parameter for builtin
functions, 2024-09-13) we passed down the repository to all builtin
commands. This allowed us to pass down the repository to lower layers
without depending on the global `the_repository` variable.

To continue this work, we also pass down the repository parameter from
the command to sub-commands. In the upcoming commit, we'll utilize this
to remove global variable usage in `midx-write.c`.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 builtin/bisect.c              | 32 +++++++++++++++++++-----------
 builtin/bundle.c              | 16 +++++++++------
 builtin/commit-graph.c        | 10 ++++++----
 builtin/config.c              | 25 ++++++++++++++---------
 builtin/gc.c                  | 21 ++++++++++++--------
 builtin/hook.c                |  7 ++++---
 builtin/multi-pack-index.c    | 16 +++++++++------
 builtin/notes.c               | 36 +++++++++++++++++++++------------
 builtin/reflog.c              | 17 ++++++++++------
 builtin/refs.c                | 10 ++++++----
 builtin/remote.c              | 34 +++++++++++++++++++++-----------
 builtin/sparse-checkout.c     | 25 ++++++++++++++---------
 builtin/stash.c               | 39 +++++++++++++++++++++++-------------
 builtin/submodule--helper.c   | 46 ++++++++++++++++++++++++++++---------------
 builtin/worktree.c            | 28 ++++++++++++++++----------
 parse-options.h               |  4 +++-
 t/helper/test-parse-options.c | 11 ++++++++---
 17 files changed, 242 insertions(+), 135 deletions(-)

diff --git a/builtin/bisect.c b/builtin/bisect.c
index 21d17a6c1a83e51fb82b53703b95d89bd9028830..8166d4abf532fbc95aaac71198e9722684ccf7f5 100644
--- a/builtin/bisect.c
+++ b/builtin/bisect.c
@@ -1312,7 +1312,8 @@ static int bisect_run(struct bisect_terms *terms, int argc, const char **argv)
 	return res;
 }
 
-static int cmd_bisect__reset(int argc, const char **argv, const char *prefix UNUSED)
+static int cmd_bisect__reset(int argc, const char **argv, const char *prefix UNUSED,
+			     struct repository *repo UNUSED)
 {
 	if (argc > 1)
 		return error(_("'%s' requires either no argument or a commit"),
@@ -1320,7 +1321,8 @@ static int cmd_bisect__reset(int argc, const char **argv, const char *prefix UNU
 	return bisect_reset(argc ? argv[0] : NULL);
 }
 
-static int cmd_bisect__terms(int argc, const char **argv, const char *prefix UNUSED)
+static int cmd_bisect__terms(int argc, const char **argv, const char *prefix UNUSED,
+			     struct repository *repo UNUSED)
 {
 	int res;
 	struct bisect_terms terms = { 0 };
@@ -1333,7 +1335,8 @@ static int cmd_bisect__terms(int argc, const char **argv, const char *prefix UNU
 	return res;
 }
 
-static int cmd_bisect__start(int argc, const char **argv, const char *prefix UNUSED)
+static int cmd_bisect__start(int argc, const char **argv, const char *prefix UNUSED,
+			     struct repository *repo UNUSED)
 {
 	int res;
 	struct bisect_terms terms = { 0 };
@@ -1344,7 +1347,8 @@ static int cmd_bisect__start(int argc, const char **argv, const char *prefix UNU
 	return res;
 }
 
-static int cmd_bisect__next(int argc, const char **argv UNUSED, const char *prefix)
+static int cmd_bisect__next(int argc, const char **argv UNUSED, const char *prefix,
+			    struct repository *repo UNUSED)
 {
 	int res;
 	struct bisect_terms terms = { 0 };
@@ -1358,12 +1362,15 @@ static int cmd_bisect__next(int argc, const char **argv UNUSED, const char *pref
 	return res;
 }
 
-static int cmd_bisect__log(int argc UNUSED, const char **argv UNUSED, const char *prefix UNUSED)
+static int cmd_bisect__log(int argc UNUSED, const char **argv UNUSED,
+			   const char *prefix UNUSED,
+			   struct repository *repo UNUSED)
 {
 	return bisect_log();
 }
 
-static int cmd_bisect__replay(int argc, const char **argv, const char *prefix UNUSED)
+static int cmd_bisect__replay(int argc, const char **argv, const char *prefix UNUSED,
+			      struct repository *repo UNUSED)
 {
 	int res;
 	struct bisect_terms terms = { 0 };
@@ -1376,7 +1383,8 @@ static int cmd_bisect__replay(int argc, const char **argv, const char *prefix UN
 	return res;
 }
 
-static int cmd_bisect__skip(int argc, const char **argv, const char *prefix UNUSED)
+static int cmd_bisect__skip(int argc, const char **argv, const char *prefix UNUSED,
+			    struct repository *repo UNUSED)
 {
 	int res;
 	struct bisect_terms terms = { 0 };
@@ -1388,7 +1396,8 @@ static int cmd_bisect__skip(int argc, const char **argv, const char *prefix UNUS
 	return res;
 }
 
-static int cmd_bisect__visualize(int argc, const char **argv, const char *prefix UNUSED)
+static int cmd_bisect__visualize(int argc, const char **argv, const char *prefix UNUSED,
+				 struct repository *repo UNUSED)
 {
 	int res;
 	struct bisect_terms terms = { 0 };
@@ -1399,7 +1408,8 @@ static int cmd_bisect__visualize(int argc, const char **argv, const char *prefix
 	return res;
 }
 
-static int cmd_bisect__run(int argc, const char **argv, const char *prefix UNUSED)
+static int cmd_bisect__run(int argc, const char **argv, const char *prefix UNUSED,
+			   struct repository *repo UNUSED)
 {
 	int res;
 	struct bisect_terms terms = { 0 };
@@ -1415,7 +1425,7 @@ static int cmd_bisect__run(int argc, const char **argv, const char *prefix UNUSE
 int cmd_bisect(int argc,
 	       const char **argv,
 	       const char *prefix,
-	       struct repository *repo UNUSED)
+	       struct repository *repo)
 {
 	int res = 0;
 	parse_opt_subcommand_fn *fn = NULL;
@@ -1451,7 +1461,7 @@ int cmd_bisect(int argc,
 	} else {
 		argc--;
 		argv++;
-		res = fn(argc, argv, prefix);
+		res = fn(argc, argv, prefix, repo);
 	}
 
 	return is_bisect_success(res) ? 0 : -res;
diff --git a/builtin/bundle.c b/builtin/bundle.c
index 127518c2a8d3c4ec0bde62f1932e964ce9bcf66f..3f14754197c847e7a4e98b607c1a24df2b053daf 100644
--- a/builtin/bundle.c
+++ b/builtin/bundle.c
@@ -67,7 +67,8 @@ static int parse_options_cmd_bundle(int argc,
 	return argc;
 }
 
-static int cmd_bundle_create(int argc, const char **argv, const char *prefix) {
+static int cmd_bundle_create(int argc, const char **argv, const char *prefix,
+			     struct repository *repo UNUSED) {
 	struct strvec pack_opts = STRVEC_INIT;
 	int version = -1;
 	int ret;
@@ -123,7 +124,8 @@ static int open_bundle(const char *path, struct bundle_header *header,
 	return read_bundle_header(path, header);
 }
 
-static int cmd_bundle_verify(int argc, const char **argv, const char *prefix) {
+static int cmd_bundle_verify(int argc, const char **argv, const char *prefix,
+			     struct repository *repo UNUSED) {
 	struct bundle_header header = BUNDLE_HEADER_INIT;
 	int bundle_fd = -1;
 	int quiet = 0;
@@ -164,7 +166,8 @@ static int cmd_bundle_verify(int argc, const char **argv, const char *prefix) {
 	return ret;
 }
 
-static int cmd_bundle_list_heads(int argc, const char **argv, const char *prefix) {
+static int cmd_bundle_list_heads(int argc, const char **argv, const char *prefix,
+				 struct repository *repo UNUSED) {
 	struct bundle_header header = BUNDLE_HEADER_INIT;
 	int bundle_fd = -1;
 	int ret;
@@ -189,7 +192,8 @@ static int cmd_bundle_list_heads(int argc, const char **argv, const char *prefix
 	return ret;
 }
 
-static int cmd_bundle_unbundle(int argc, const char **argv, const char *prefix) {
+static int cmd_bundle_unbundle(int argc, const char **argv, const char *prefix,
+			       struct repository *repo UNUSED) {
 	struct bundle_header header = BUNDLE_HEADER_INIT;
 	int bundle_fd = -1;
 	int ret;
@@ -231,7 +235,7 @@ static int cmd_bundle_unbundle(int argc, const char **argv, const char *prefix)
 int cmd_bundle(int argc,
 	       const char **argv,
 	       const char *prefix,
-	       struct repository *repo UNUSED)
+	       struct repository *repo)
 {
 	parse_opt_subcommand_fn *fn = NULL;
 	struct option options[] = {
@@ -247,5 +251,5 @@ int cmd_bundle(int argc,
 
 	packet_trace_identity("bundle");
 
-	return !!fn(argc, argv, prefix);
+	return !!fn(argc, argv, prefix, repo);
 }
diff --git a/builtin/commit-graph.c b/builtin/commit-graph.c
index 7c991db6eb48ad6e935727a079abac02bb358f8a..bd70d052e706b6e34b5aaaceef158c63ea4863d5 100644
--- a/builtin/commit-graph.c
+++ b/builtin/commit-graph.c
@@ -62,7 +62,8 @@ static struct option *add_common_options(struct option *to)
 	return parse_options_concat(common_opts, to);
 }
 
-static int graph_verify(int argc, const char **argv, const char *prefix)
+static int graph_verify(int argc, const char **argv, const char *prefix,
+			struct repository *repo UNUSED)
 {
 	struct commit_graph *graph = NULL;
 	struct object_directory *odb = NULL;
@@ -214,7 +215,8 @@ static int git_commit_graph_write_config(const char *var, const char *value,
 	return 0;
 }
 
-static int graph_write(int argc, const char **argv, const char *prefix)
+static int graph_write(int argc, const char **argv, const char *prefix,
+		       struct repository *repo UNUSED)
 {
 	struct string_list pack_indexes = STRING_LIST_INIT_DUP;
 	struct strbuf buf = STRBUF_INIT;
@@ -333,7 +335,7 @@ static int graph_write(int argc, const char **argv, const char *prefix)
 int cmd_commit_graph(int argc,
 		     const char **argv,
 		     const char *prefix,
-		     struct repository *repo UNUSED)
+		     struct repository *repo)
 {
 	parse_opt_subcommand_fn *fn = NULL;
 	struct option builtin_commit_graph_options[] = {
@@ -352,5 +354,5 @@ int cmd_commit_graph(int argc,
 			     builtin_commit_graph_usage, 0);
 	FREE_AND_NULL(options);
 
-	return fn(argc, argv, prefix);
+	return fn(argc, argv, prefix, repo);
 }
diff --git a/builtin/config.c b/builtin/config.c
index cba702210815b716a5c6c93ebb69d8c485901e43..16e6e3055598f1355714dc6de458b662870214c0 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -826,7 +826,8 @@ static void display_options_init(struct config_display_options *opts)
 	}
 }
 
-static int cmd_config_list(int argc, const char **argv, const char *prefix)
+static int cmd_config_list(int argc, const char **argv, const char *prefix,
+			   struct repository *repo UNUSED)
 {
 	struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
 	struct config_display_options display_opts = CONFIG_DISPLAY_OPTIONS_INIT;
@@ -861,7 +862,8 @@ static int cmd_config_list(int argc, const char **argv, const char *prefix)
 	return 0;
 }
 
-static int cmd_config_get(int argc, const char **argv, const char *prefix)
+static int cmd_config_get(int argc, const char **argv, const char *prefix,
+			  struct repository *repo UNUSED)
 {
 	struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
 	struct config_display_options display_opts = CONFIG_DISPLAY_OPTIONS_INIT;
@@ -915,7 +917,8 @@ static int cmd_config_get(int argc, const char **argv, const char *prefix)
 	return ret;
 }
 
-static int cmd_config_set(int argc, const char **argv, const char *prefix)
+static int cmd_config_set(int argc, const char **argv, const char *prefix,
+			  struct repository *repo UNUSED)
 {
 	struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
 	const char *value_pattern = NULL, *comment_arg = NULL;
@@ -973,7 +976,8 @@ static int cmd_config_set(int argc, const char **argv, const char *prefix)
 	return ret;
 }
 
-static int cmd_config_unset(int argc, const char **argv, const char *prefix)
+static int cmd_config_unset(int argc, const char **argv, const char *prefix,
+			    struct repository *repo UNUSED)
 {
 	struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
 	const char *value_pattern = NULL;
@@ -1010,7 +1014,8 @@ static int cmd_config_unset(int argc, const char **argv, const char *prefix)
 	return ret;
 }
 
-static int cmd_config_rename_section(int argc, const char **argv, const char *prefix)
+static int cmd_config_rename_section(int argc, const char **argv, const char *prefix,
+				     struct repository *repo UNUSED)
 {
 	struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
 	struct option opts[] = {
@@ -1039,7 +1044,8 @@ static int cmd_config_rename_section(int argc, const char **argv, const char *pr
 	return ret;
 }
 
-static int cmd_config_remove_section(int argc, const char **argv, const char *prefix)
+static int cmd_config_remove_section(int argc, const char **argv, const char *prefix,
+				     struct repository *repo UNUSED)
 {
 	struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
 	struct option opts[] = {
@@ -1099,7 +1105,8 @@ static int show_editor(struct config_location_options *opts)
 	return 0;
 }
 
-static int cmd_config_edit(int argc, const char **argv, const char *prefix)
+static int cmd_config_edit(int argc, const char **argv, const char *prefix,
+			   struct repository *repo UNUSED)
 {
 	struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
 	struct option opts[] = {
@@ -1395,7 +1402,7 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
 int cmd_config(int argc,
 	       const char **argv,
 	       const char *prefix,
-	       struct repository *repo UNUSED)
+	       struct repository *repo)
 {
 	parse_opt_subcommand_fn *subcommand = NULL;
 	struct option subcommand_opts[] = {
@@ -1422,7 +1429,7 @@ int cmd_config(int argc,
 	if (subcommand) {
 		argc = parse_options(argc, argv, prefix, subcommand_opts, builtin_config_usage,
 		       PARSE_OPT_SUBCOMMAND_OPTIONAL|PARSE_OPT_KEEP_UNKNOWN_OPT);
-		return subcommand(argc, argv, prefix);
+		return subcommand(argc, argv, prefix, repo);
 	}
 
 	return cmd_config_actions(argc, argv, prefix);
diff --git a/builtin/gc.c b/builtin/gc.c
index 09802eb989471be242a4b2d74606b0998fdd2b11..50ccab172365d22c8f89e9f6fb822fca948878e7 100644
--- a/builtin/gc.c
+++ b/builtin/gc.c
@@ -1567,7 +1567,8 @@ static int task_option_parse(const struct option *opt UNUSED,
 	return 0;
 }
 
-static int maintenance_run(int argc, const char **argv, const char *prefix)
+static int maintenance_run(int argc, const char **argv, const char *prefix,
+			   struct repository *repo UNUSED)
 {
 	int i;
 	struct maintenance_run_opts opts = MAINTENANCE_RUN_OPTS_INIT;
@@ -1629,7 +1630,8 @@ static char const * const builtin_maintenance_register_usage[] = {
 	NULL
 };
 
-static int maintenance_register(int argc, const char **argv, const char *prefix)
+static int maintenance_register(int argc, const char **argv, const char *prefix,
+				struct repository *repo UNUSED)
 {
 	char *config_file = NULL;
 	struct option options[] = {
@@ -1693,7 +1695,8 @@ static char const * const builtin_maintenance_unregister_usage[] = {
 	NULL
 };
 
-static int maintenance_unregister(int argc, const char **argv, const char *prefix)
+static int maintenance_unregister(int argc, const char **argv, const char *prefix,
+				  struct repository *repo UNUSED)
 {
 	int force = 0;
 	char *config_file = NULL;
@@ -2923,7 +2926,8 @@ static const char *const builtin_maintenance_start_usage[] = {
 	NULL
 };
 
-static int maintenance_start(int argc, const char **argv, const char *prefix)
+static int maintenance_start(int argc, const char **argv, const char *prefix,
+			     struct repository *repo)
 {
 	struct maintenance_start_opts opts = { 0 };
 	struct option options[] = {
@@ -2946,7 +2950,7 @@ static int maintenance_start(int argc, const char **argv, const char *prefix)
 	if (update_background_schedule(&opts, 1))
 		die(_("failed to set up maintenance schedule"));
 
-	if (maintenance_register(ARRAY_SIZE(register_args)-1, register_args, NULL))
+	if (maintenance_register(ARRAY_SIZE(register_args)-1, register_args, NULL, repo))
 		warning(_("failed to add repo to global config"));
 	return 0;
 }
@@ -2956,7 +2960,8 @@ static const char *const builtin_maintenance_stop_usage[] = {
 	NULL
 };
 
-static int maintenance_stop(int argc, const char **argv, const char *prefix)
+static int maintenance_stop(int argc, const char **argv, const char *prefix,
+			    struct repository *repo UNUSED)
 {
 	struct option options[] = {
 		OPT_END()
@@ -2976,7 +2981,7 @@ static const char * const builtin_maintenance_usage[] = {
 int cmd_maintenance(int argc,
 		    const char **argv,
 		    const char *prefix,
-		    struct repository *repo UNUSED)
+		    struct repository *repo)
 {
 	parse_opt_subcommand_fn *fn = NULL;
 	struct option builtin_maintenance_options[] = {
@@ -2990,5 +2995,5 @@ int cmd_maintenance(int argc,
 
 	argc = parse_options(argc, argv, prefix, builtin_maintenance_options,
 			     builtin_maintenance_usage, 0);
-	return fn(argc, argv, prefix);
+	return fn(argc, argv, prefix, repo);
 }
diff --git a/builtin/hook.c b/builtin/hook.c
index 367ef3e0b893fa16756880395151a82ed053acd8..672d2e37e845a2118aca1c4417a837138c250590 100644
--- a/builtin/hook.c
+++ b/builtin/hook.c
@@ -19,7 +19,8 @@ static const char * const builtin_hook_run_usage[] = {
 	NULL
 };
 
-static int run(int argc, const char **argv, const char *prefix)
+static int run(int argc, const char **argv, const char *prefix,
+	       struct repository *repo UNUSED)
 {
 	int i;
 	struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
@@ -70,7 +71,7 @@ static int run(int argc, const char **argv, const char *prefix)
 int cmd_hook(int argc,
 	     const char **argv,
 	     const char *prefix,
-	     struct repository *repo UNUSED)
+	     struct repository *repo)
 {
 	parse_opt_subcommand_fn *fn = NULL;
 	struct option builtin_hook_options[] = {
@@ -81,5 +82,5 @@ int cmd_hook(int argc,
 	argc = parse_options(argc, argv, NULL, builtin_hook_options,
 			     builtin_hook_usage, 0);
 
-	return fn(argc, argv, prefix);
+	return fn(argc, argv, prefix, repo);
 }
diff --git a/builtin/multi-pack-index.c b/builtin/multi-pack-index.c
index d159ed1314d912a390ed725659b3abe7c821b83f..85e40a4b6d3e47e9ec1ec27c094455e5ba75b5b0 100644
--- a/builtin/multi-pack-index.c
+++ b/builtin/multi-pack-index.c
@@ -119,7 +119,8 @@ static void read_packs_from_stdin(struct string_list *to)
 }
 
 static int cmd_multi_pack_index_write(int argc, const char **argv,
-				      const char *prefix)
+				      const char *prefix,
+				      struct repository *repo UNUSED)
 {
 	struct option *options;
 	static struct option builtin_multi_pack_index_write_options[] = {
@@ -183,7 +184,8 @@ static int cmd_multi_pack_index_write(int argc, const char **argv,
 }
 
 static int cmd_multi_pack_index_verify(int argc, const char **argv,
-				       const char *prefix)
+				       const char *prefix,
+				       struct repository *repo UNUSED)
 {
 	struct option *options;
 	static struct option builtin_multi_pack_index_verify_options[] = {
@@ -210,7 +212,8 @@ static int cmd_multi_pack_index_verify(int argc, const char **argv,
 }
 
 static int cmd_multi_pack_index_expire(int argc, const char **argv,
-				       const char *prefix)
+				       const char *prefix,
+				       struct repository *repo UNUSED)
 {
 	struct option *options;
 	static struct option builtin_multi_pack_index_expire_options[] = {
@@ -237,7 +240,8 @@ static int cmd_multi_pack_index_expire(int argc, const char **argv,
 }
 
 static int cmd_multi_pack_index_repack(int argc, const char **argv,
-				       const char *prefix)
+				       const char *prefix,
+				       struct repository *repo UNUSED)
 {
 	struct option *options;
 	static struct option builtin_multi_pack_index_repack_options[] = {
@@ -271,7 +275,7 @@ static int cmd_multi_pack_index_repack(int argc, const char **argv,
 int cmd_multi_pack_index(int argc,
 			 const char **argv,
 			 const char *prefix,
-			 struct repository *repo UNUSED)
+			 struct repository *repo)
 {
 	int res;
 	parse_opt_subcommand_fn *fn = NULL;
@@ -297,7 +301,7 @@ int cmd_multi_pack_index(int argc,
 			     builtin_multi_pack_index_usage, 0);
 	FREE_AND_NULL(options);
 
-	res = fn(argc, argv, prefix);
+	res = fn(argc, argv, prefix, repo);
 
 	free(opts.object_dir);
 	return res;
diff --git a/builtin/notes.c b/builtin/notes.c
index 72c8a51cfacf72e1f115774487311b3d4464d204..d051abf6dff8f2bce193fd52b4beda5060af3972 100644
--- a/builtin/notes.c
+++ b/builtin/notes.c
@@ -431,7 +431,8 @@ static struct notes_tree *init_notes_check(const char *subcommand,
 	return t;
 }
 
-static int list(int argc, const char **argv, const char *prefix)
+static int list(int argc, const char **argv, const char *prefix,
+		struct repository *repo UNUSED)
 {
 	struct notes_tree *t;
 	struct object_id object;
@@ -468,9 +469,11 @@ static int list(int argc, const char **argv, const char *prefix)
 	return retval;
 }
 
-static int append_edit(int argc, const char **argv, const char *prefix);
+static int append_edit(int argc, const char **argv, const char *prefix,
+		       struct repository *repo UNUSED);
 
-static int add(int argc, const char **argv, const char *prefix)
+static int add(int argc, const char **argv, const char *prefix,
+	       struct repository *repo)
 {
 	int force = 0, allow_empty = 0;
 	const char *object_ref;
@@ -543,7 +546,7 @@ static int add(int argc, const char **argv, const char *prefix)
 			 * argv[0-1].
 			 */
 			argv[0] = "edit";
-			return append_edit(argc, argv, prefix);
+			return append_edit(argc, argv, prefix, repo);
 		}
 		fprintf(stderr, _("Overwriting existing notes for object %s\n"),
 			oid_to_hex(&object));
@@ -569,7 +572,8 @@ static int add(int argc, const char **argv, const char *prefix)
 	return 0;
 }
 
-static int copy(int argc, const char **argv, const char *prefix)
+static int copy(int argc, const char **argv, const char *prefix,
+		struct repository *repo UNUSED)
 {
 	int retval = 0, force = 0, from_stdin = 0;
 	const struct object_id *from_note, *note;
@@ -646,7 +650,8 @@ static int copy(int argc, const char **argv, const char *prefix)
 	return retval;
 }
 
-static int append_edit(int argc, const char **argv, const char *prefix)
+static int append_edit(int argc, const char **argv, const char *prefix,
+		       struct repository *repo UNUSED)
 {
 	int allow_empty = 0;
 	const char *object_ref;
@@ -749,7 +754,8 @@ static int append_edit(int argc, const char **argv, const char *prefix)
 	return 0;
 }
 
-static int show(int argc, const char **argv, const char *prefix)
+static int show(int argc, const char **argv, const char *prefix,
+		struct repository *repo UNUSED)
 {
 	const char *object_ref;
 	struct notes_tree *t;
@@ -875,7 +881,8 @@ static int git_config_get_notes_strategy(const char *key,
 	return 0;
 }
 
-static int merge(int argc, const char **argv, const char *prefix)
+static int merge(int argc, const char **argv, const char *prefix,
+		 struct repository *repo UNUSED)
 {
 	struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT;
 	struct object_id result_oid;
@@ -1016,7 +1023,8 @@ static int remove_one_note(struct notes_tree *t, const char *name, unsigned flag
 	return (flag & IGNORE_MISSING) ? 0 : status;
 }
 
-static int remove_cmd(int argc, const char **argv, const char *prefix)
+static int remove_cmd(int argc, const char **argv, const char *prefix,
+		      struct repository *repo UNUSED)
 {
 	unsigned flag = 0;
 	int from_stdin = 0;
@@ -1059,7 +1067,8 @@ static int remove_cmd(int argc, const char **argv, const char *prefix)
 	return retval;
 }
 
-static int prune(int argc, const char **argv, const char *prefix)
+static int prune(int argc, const char **argv, const char *prefix,
+		 struct repository *repo UNUSED)
 {
 	struct notes_tree *t;
 	int show_only = 0, verbose = 0;
@@ -1088,7 +1097,8 @@ static int prune(int argc, const char **argv, const char *prefix)
 	return 0;
 }
 
-static int get_ref(int argc, const char **argv, const char *prefix)
+static int get_ref(int argc, const char **argv, const char *prefix,
+		   struct repository *repo UNUSED)
 {
 	struct option options[] = { OPT_END() };
 	char *notes_ref;
@@ -1109,7 +1119,7 @@ static int get_ref(int argc, const char **argv, const char *prefix)
 int cmd_notes(int argc,
 	      const char **argv,
 	      const char *prefix,
-	      struct repository *repo UNUSED)
+	      struct repository *repo)
 {
 	const char *override_notes_ref = NULL;
 	parse_opt_subcommand_fn *fn = NULL;
@@ -1148,5 +1158,5 @@ int cmd_notes(int argc,
 		strbuf_release(&sb);
 	}
 
-	return !!fn(argc, argv, prefix);
+	return !!fn(argc, argv, prefix, repo);
 }
diff --git a/builtin/reflog.c b/builtin/reflog.c
index 22df6834f71098ab8378e4423967f0fb87858340..5a0c22f2f7e58733ce636619f3f19265b394bde5 100644
--- a/builtin/reflog.c
+++ b/builtin/reflog.c
@@ -234,7 +234,8 @@ static int expire_total_callback(const struct option *opt,
 	return 0;
 }
 
-static int cmd_reflog_show(int argc, const char **argv, const char *prefix)
+static int cmd_reflog_show(int argc, const char **argv, const char *prefix,
+			   struct repository *repo UNUSED)
 {
 	struct option options[] = {
 		OPT_END()
@@ -253,7 +254,8 @@ static int show_reflog(const char *refname, void *cb_data UNUSED)
 	return 0;
 }
 
-static int cmd_reflog_list(int argc, const char **argv, const char *prefix)
+static int cmd_reflog_list(int argc, const char **argv, const char *prefix,
+			   struct repository *repo UNUSED)
 {
 	struct option options[] = {
 		OPT_END()
@@ -270,7 +272,8 @@ static int cmd_reflog_list(int argc, const char **argv, const char *prefix)
 	return refs_for_each_reflog(ref_store, show_reflog, NULL);
 }
 
-static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
+static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,
+			     struct repository *repo UNUSED)
 {
 	struct cmd_reflog_expire_cb cmd = { 0 };
 	timestamp_t now = time(NULL);
@@ -394,7 +397,8 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
 	return status;
 }
 
-static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
+static int cmd_reflog_delete(int argc, const char **argv, const char *prefix,
+			     struct repository *repo UNUSED)
 {
 	int i, status = 0;
 	unsigned int flags = 0;
@@ -424,7 +428,8 @@ static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
 	return status;
 }
 
-static int cmd_reflog_exists(int argc, const char **argv, const char *prefix)
+static int cmd_reflog_exists(int argc, const char **argv, const char *prefix,
+			     struct repository *repo UNUSED)
 {
 	struct option options[] = {
 		OPT_END()
@@ -467,7 +472,7 @@ int cmd_reflog(int argc,
 			     PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0 |
 			     PARSE_OPT_KEEP_UNKNOWN_OPT);
 	if (fn)
-		return fn(argc - 1, argv + 1, prefix);
+		return fn(argc - 1, argv + 1, prefix, repository);
 	else
 		return cmd_log_reflog(argc, argv, prefix, repository);
 }
diff --git a/builtin/refs.c b/builtin/refs.c
index 24978a7b7b081ac6ed8ca99016f201d34c0639f8..3502980d21d055d8fc5ef36d2165aae61bc4db62 100644
--- a/builtin/refs.c
+++ b/builtin/refs.c
@@ -12,7 +12,8 @@
 #define REFS_VERIFY_USAGE \
 	N_("git refs verify [--strict] [--verbose]")
 
-static int cmd_refs_migrate(int argc, const char **argv, const char *prefix)
+static int cmd_refs_migrate(int argc, const char **argv, const char *prefix,
+			    struct repository *repo UNUSED)
 {
 	const char * const migrate_usage[] = {
 		REFS_MIGRATE_USAGE,
@@ -63,7 +64,8 @@ static int cmd_refs_migrate(int argc, const char **argv, const char *prefix)
 	return err;
 }
 
-static int cmd_refs_verify(int argc, const char **argv, const char *prefix)
+static int cmd_refs_verify(int argc, const char **argv, const char *prefix,
+			   struct repository *repo UNUSED)
 {
 	struct fsck_options fsck_refs_options = FSCK_REFS_OPTIONS_DEFAULT;
 	const char * const verify_usage[] = {
@@ -93,7 +95,7 @@ static int cmd_refs_verify(int argc, const char **argv, const char *prefix)
 int cmd_refs(int argc,
 	     const char **argv,
 	     const char *prefix,
-	     struct repository *repo UNUSED)
+	     struct repository *repo)
 {
 	const char * const refs_usage[] = {
 		REFS_MIGRATE_USAGE,
@@ -108,5 +110,5 @@ int cmd_refs(int argc,
 	};
 
 	argc = parse_options(argc, argv, prefix, opts, refs_usage, 0);
-	return fn(argc, argv, prefix);
+	return fn(argc, argv, prefix, repo);
 }
diff --git a/builtin/remote.c b/builtin/remote.c
index 76670ddd8b44e5b118c76522613c445ad92c5364..56214775df4ce86bf5d8e4ddb8d19140f696aeef 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -155,7 +155,8 @@ static int parse_mirror_opt(const struct option *opt, const char *arg, int not)
 	return 0;
 }
 
-static int add(int argc, const char **argv, const char *prefix)
+static int add(int argc, const char **argv, const char *prefix,
+	       struct repository *repo UNUSED)
 {
 	int fetch = 0, fetch_tags = TAGS_DEFAULT;
 	unsigned mirror = MIRROR_NONE;
@@ -706,7 +707,8 @@ static void handle_push_default(const char* old_name, const char* new_name)
 }
 
 
-static int mv(int argc, const char **argv, const char *prefix)
+static int mv(int argc, const char **argv, const char *prefix,
+	      struct repository *repo UNUSED)
 {
 	int show_progress = isatty(2);
 	struct option options[] = {
@@ -881,7 +883,8 @@ static int mv(int argc, const char **argv, const char *prefix)
 	return result;
 }
 
-static int rm(int argc, const char **argv, const char *prefix)
+static int rm(int argc, const char **argv, const char *prefix,
+	      struct repository *repo UNUSED)
 {
 	struct option options[] = {
 		OPT_END()
@@ -1303,7 +1306,8 @@ static int show_all(void)
 	return result;
 }
 
-static int show(int argc, const char **argv, const char *prefix)
+static int show(int argc, const char **argv, const char *prefix,
+		struct repository *repo UNUSED)
 {
 	int no_query = 0, result = 0, query_flag = 0;
 	struct option options[] = {
@@ -1399,7 +1403,8 @@ static int show(int argc, const char **argv, const char *prefix)
 	return result;
 }
 
-static int set_head(int argc, const char **argv, const char *prefix)
+static int set_head(int argc, const char **argv, const char *prefix,
+		    struct repository *repo UNUSED)
 {
 	int i, opt_a = 0, opt_d = 0, result = 0;
 	struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
@@ -1503,7 +1508,8 @@ static int prune_remote(const char *remote, int dry_run)
 	return result;
 }
 
-static int prune(int argc, const char **argv, const char *prefix)
+static int prune(int argc, const char **argv, const char *prefix,
+		 struct repository *repo UNUSED)
 {
 	int dry_run = 0, result = 0;
 	struct option options[] = {
@@ -1534,7 +1540,8 @@ static int get_remote_default(const char *key, const char *value UNUSED,
 	return 0;
 }
 
-static int update(int argc, const char **argv, const char *prefix)
+static int update(int argc, const char **argv, const char *prefix,
+		  struct repository *repo UNUSED)
 {
 	int i, prune = -1;
 	struct option options[] = {
@@ -1616,7 +1623,8 @@ static int set_remote_branches(const char *remotename, const char **branches,
 	return 0;
 }
 
-static int set_branches(int argc, const char **argv, const char *prefix)
+static int set_branches(int argc, const char **argv, const char *prefix,
+			struct repository *repo UNUSED)
 {
 	int add_mode = 0;
 	struct option options[] = {
@@ -1635,7 +1643,8 @@ static int set_branches(int argc, const char **argv, const char *prefix)
 	return set_remote_branches(argv[0], argv + 1, add_mode);
 }
 
-static int get_url(int argc, const char **argv, const char *prefix)
+static int get_url(int argc, const char **argv, const char *prefix,
+		   struct repository *repo UNUSED)
 {
 	int i, push_mode = 0, all_mode = 0;
 	const char *remotename = NULL;
@@ -1674,7 +1683,8 @@ static int get_url(int argc, const char **argv, const char *prefix)
 	return 0;
 }
 
-static int set_url(int argc, const char **argv, const char *prefix)
+static int set_url(int argc, const char **argv, const char *prefix,
+		   struct repository *repo UNUSED)
 {
 	int i, push_mode = 0, add_mode = 0, delete_mode = 0;
 	int matches = 0, negative_matches = 0;
@@ -1765,7 +1775,7 @@ static int set_url(int argc, const char **argv, const char *prefix)
 int cmd_remote(int argc,
 	       const char **argv,
 	       const char *prefix,
-	       struct repository *repo UNUSED)
+	       struct repository *repo)
 {
 	parse_opt_subcommand_fn *fn = NULL;
 	struct option options[] = {
@@ -1788,7 +1798,7 @@ int cmd_remote(int argc,
 			     PARSE_OPT_SUBCOMMAND_OPTIONAL);
 
 	if (fn) {
-		return !!fn(argc, argv, prefix);
+		return !!fn(argc, argv, prefix, repo);
 	} else {
 		if (argc) {
 			error(_("unknown subcommand: `%s'"), argv[0]);
diff --git a/builtin/sparse-checkout.c b/builtin/sparse-checkout.c
index 49aedc1de81a17b8b491cded7fa71b384e0e8be9..6f5fa5893b839fe21fef44ecbd837d132ebbac26 100644
--- a/builtin/sparse-checkout.c
+++ b/builtin/sparse-checkout.c
@@ -48,7 +48,8 @@ static char const * const builtin_sparse_checkout_list_usage[] = {
 	NULL
 };
 
-static int sparse_checkout_list(int argc, const char **argv, const char *prefix)
+static int sparse_checkout_list(int argc, const char **argv, const char *prefix,
+				struct repository *repo UNUSED)
 {
 	static struct option builtin_sparse_checkout_list_options[] = {
 		OPT_END(),
@@ -443,7 +444,8 @@ static struct sparse_checkout_init_opts {
 	int sparse_index;
 } init_opts;
 
-static int sparse_checkout_init(int argc, const char **argv, const char *prefix)
+static int sparse_checkout_init(int argc, const char **argv, const char *prefix,
+				struct repository *repo UNUSED)
 {
 	struct pattern_list pl;
 	char *sparse_filename;
@@ -770,7 +772,8 @@ static struct sparse_checkout_add_opts {
 	int use_stdin;
 } add_opts;
 
-static int sparse_checkout_add(int argc, const char **argv, const char *prefix)
+static int sparse_checkout_add(int argc, const char **argv, const char *prefix,
+			       struct repository *repo UNUSED)
 {
 	static struct option builtin_sparse_checkout_add_options[] = {
 		OPT_BOOL_F(0, "skip-checks", &add_opts.skip_checks,
@@ -808,7 +811,8 @@ static struct sparse_checkout_set_opts {
 	int use_stdin;
 } set_opts;
 
-static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
+static int sparse_checkout_set(int argc, const char **argv, const char *prefix,
+			       struct repository *repo UNUSED)
 {
 	int default_patterns_nr = 2;
 	const char *default_patterns[] = {"/*", "!/*/", NULL};
@@ -866,7 +870,8 @@ static struct sparse_checkout_reapply_opts {
 } reapply_opts;
 
 static int sparse_checkout_reapply(int argc, const char **argv,
-				   const char *prefix)
+				   const char *prefix,
+				   struct repository *repo UNUSED)
 {
 	static struct option builtin_sparse_checkout_reapply_options[] = {
 		OPT_BOOL(0, "cone", &reapply_opts.cone_mode,
@@ -901,7 +906,8 @@ static char const * const builtin_sparse_checkout_disable_usage[] = {
 };
 
 static int sparse_checkout_disable(int argc, const char **argv,
-				   const char *prefix)
+				   const char *prefix,
+				   struct repository *repo UNUSED)
 {
 	static struct option builtin_sparse_checkout_disable_options[] = {
 		OPT_END(),
@@ -989,7 +995,8 @@ static int check_rules(struct pattern_list *pl, int null_terminated) {
 	return 0;
 }
 
-static int sparse_checkout_check_rules(int argc, const char **argv, const char *prefix)
+static int sparse_checkout_check_rules(int argc, const char **argv, const char *prefix,
+				       struct repository *repo UNUSED)
 {
 	static struct option builtin_sparse_checkout_check_rules_options[] = {
 		OPT_BOOL('z', NULL, &check_rules_opts.null_termination,
@@ -1037,7 +1044,7 @@ static int sparse_checkout_check_rules(int argc, const char **argv, const char *
 int cmd_sparse_checkout(int argc,
 			const char **argv,
 			const char *prefix,
-			struct repository *repo UNUSED)
+			struct repository *repo)
 {
 	parse_opt_subcommand_fn *fn = NULL;
 	struct option builtin_sparse_checkout_options[] = {
@@ -1060,5 +1067,5 @@ int cmd_sparse_checkout(int argc,
 	prepare_repo_settings(the_repository);
 	the_repository->settings.command_requires_full_index = 0;
 
-	return fn(argc, argv, prefix);
+	return fn(argc, argv, prefix, repo);
 }
diff --git a/builtin/stash.c b/builtin/stash.c
index 1399a1bbe2c222ed3e1c33ac2dd17bd1148f709c..c212b1c0b2c7c55de2fdd5d7d7a247ce6f530960 100644
--- a/builtin/stash.c
+++ b/builtin/stash.c
@@ -249,7 +249,8 @@ static int do_clear_stash(void)
 			       ref_stash, &obj, 0);
 }
 
-static int clear_stash(int argc, const char **argv, const char *prefix)
+static int clear_stash(int argc, const char **argv, const char *prefix,
+		       struct repository *repo UNUSED)
 {
 	struct option options[] = {
 		OPT_END()
@@ -652,7 +653,8 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,
 	return ret;
 }
 
-static int apply_stash(int argc, const char **argv, const char *prefix)
+static int apply_stash(int argc, const char **argv, const char *prefix,
+		       struct repository *repo UNUSED)
 {
 	int ret = -1;
 	int quiet = 0;
@@ -726,7 +728,8 @@ static int get_stash_info_assert(struct stash_info *info, int argc,
 	return 0;
 }
 
-static int drop_stash(int argc, const char **argv, const char *prefix)
+static int drop_stash(int argc, const char **argv, const char *prefix,
+		      struct repository *repo UNUSED)
 {
 	int ret = -1;
 	int quiet = 0;
@@ -748,7 +751,8 @@ static int drop_stash(int argc, const char **argv, const char *prefix)
 	return ret;
 }
 
-static int pop_stash(int argc, const char **argv, const char *prefix)
+static int pop_stash(int argc, const char **argv, const char *prefix,
+		     struct repository *repo UNUSED)
 {
 	int ret = -1;
 	int index = 0;
@@ -778,7 +782,8 @@ static int pop_stash(int argc, const char **argv, const char *prefix)
 	return ret;
 }
 
-static int branch_stash(int argc, const char **argv, const char *prefix)
+static int branch_stash(int argc, const char **argv, const char *prefix,
+			struct repository *repo UNUSED)
 {
 	int ret = -1;
 	const char *branch = NULL;
@@ -816,7 +821,8 @@ static int branch_stash(int argc, const char **argv, const char *prefix)
 	return ret;
 }
 
-static int list_stash(int argc, const char **argv, const char *prefix)
+static int list_stash(int argc, const char **argv, const char *prefix,
+		      struct repository *repo UNUSED)
 {
 	struct child_process cp = CHILD_PROCESS_INIT;
 	struct option options[] = {
@@ -889,7 +895,8 @@ static void diff_include_untracked(const struct stash_info *info, struct diff_op
 	do_diff_cache(&info->b_commit, diff_opt);
 }
 
-static int show_stash(int argc, const char **argv, const char *prefix)
+static int show_stash(int argc, const char **argv, const char *prefix,
+		      struct repository *repo UNUSED)
 {
 	int i;
 	int ret = -1;
@@ -1017,7 +1024,8 @@ static int do_store_stash(const struct object_id *w_commit, const char *stash_ms
 	return 0;
 }
 
-static int store_stash(int argc, const char **argv, const char *prefix)
+static int store_stash(int argc, const char **argv, const char *prefix,
+		       struct repository *repo UNUSED)
 {
 	int quiet = 0;
 	const char *stash_msg = NULL;
@@ -1491,7 +1499,8 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b
 	return ret;
 }
 
-static int create_stash(int argc, const char **argv, const char *prefix UNUSED)
+static int create_stash(int argc, const char **argv, const char *prefix UNUSED,
+			struct repository *repo UNUSED)
 {
 	int ret;
 	struct strbuf stash_msg_buf = STRBUF_INIT;
@@ -1827,12 +1836,14 @@ static int push_stash(int argc, const char **argv, const char *prefix,
 	return ret;
 }
 
-static int push_stash_unassumed(int argc, const char **argv, const char *prefix)
+static int push_stash_unassumed(int argc, const char **argv, const char *prefix,
+				struct repository *repo UNUSED)
 {
 	return push_stash(argc, argv, prefix, 0);
 }
 
-static int save_stash(int argc, const char **argv, const char *prefix)
+static int save_stash(int argc, const char **argv, const char *prefix,
+		      struct repository *repo UNUSED)
 {
 	int keep_index = -1;
 	int only_staged = 0;
@@ -1878,7 +1889,7 @@ static int save_stash(int argc, const char **argv, const char *prefix)
 int cmd_stash(int argc,
 	      const char **argv,
 	      const char *prefix,
-	      struct repository *repo UNUSED)
+	      struct repository *repo)
 {
 	pid_t pid = getpid();
 	const char *index_file;
@@ -1916,9 +1927,9 @@ int cmd_stash(int argc,
 		    (uintmax_t)pid);
 
 	if (fn)
-		return !!fn(argc, argv, prefix);
+		return !!fn(argc, argv, prefix, repo);
 	else if (!argc)
-		return !!push_stash_unassumed(0, NULL, prefix);
+		return !!push_stash_unassumed(0, NULL, prefix, repo);
 
 	/* Assume 'stash push' */
 	strvec_push(&args, "push");
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index b6b5f1ebde7c2e4780af4097a0c4d6d838948aee..19e587838132f26e2e10dc4a9f93f7b1c78cd49d 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -399,7 +399,8 @@ static void runcommand_in_submodule_cb(const struct cache_entry *list_item,
 	free(displaypath);
 }
 
-static int module_foreach(int argc, const char **argv, const char *prefix)
+static int module_foreach(int argc, const char **argv, const char *prefix,
+			  struct repository *repo UNUSED)
 {
 	struct foreach_cb info = FOREACH_CB_INIT;
 	struct pathspec pathspec = { 0 };
@@ -544,7 +545,8 @@ static void init_submodule_cb(const struct cache_entry *list_item, void *cb_data
 		       info->flags);
 }
 
-static int module_init(int argc, const char **argv, const char *prefix)
+static int module_init(int argc, const char **argv, const char *prefix,
+		       struct repository *repo UNUSED)
 {
 	struct init_cb info = INIT_CB_INIT;
 	struct pathspec pathspec = { 0 };
@@ -738,7 +740,8 @@ static void status_submodule_cb(const struct cache_entry *list_item,
 			 info->prefix, info->super_prefix, info->flags);
 }
 
-static int module_status(int argc, const char **argv, const char *prefix)
+static int module_status(int argc, const char **argv, const char *prefix,
+			 struct repository *repo UNUSED)
 {
 	struct status_cb info = STATUS_CB_INIT;
 	struct pathspec pathspec = { 0 };
@@ -1163,7 +1166,8 @@ static int compute_summary_module_list(struct object_id *head_oid,
 	return ret;
 }
 
-static int module_summary(int argc, const char **argv, const char *prefix)
+static int module_summary(int argc, const char **argv, const char *prefix,
+			  struct repository *repo UNUSED)
 {
 	struct summary_cb info = SUMMARY_CB_INIT;
 	int cached = 0;
@@ -1339,7 +1343,8 @@ static void sync_submodule_cb(const struct cache_entry *list_item, void *cb_data
 		       info->flags);
 }
 
-static int module_sync(int argc, const char **argv, const char *prefix)
+static int module_sync(int argc, const char **argv, const char *prefix,
+		       struct repository *repo UNUSED)
 {
 	struct sync_cb info = SYNC_CB_INIT;
 	struct pathspec pathspec = { 0 };
@@ -1485,7 +1490,8 @@ static void deinit_submodule_cb(const struct cache_entry *list_item,
 	deinit_submodule(list_item->name, info->prefix, info->flags);
 }
 
-static int module_deinit(int argc, const char **argv, const char *prefix)
+static int module_deinit(int argc, const char **argv, const char *prefix,
+			 struct repository *repo UNUSED)
 {
 	struct deinit_cb info = DEINIT_CB_INIT;
 	struct pathspec pathspec = { 0 };
@@ -1842,7 +1848,8 @@ static int clone_submodule(const struct module_clone_data *clone_data,
 	return 0;
 }
 
-static int module_clone(int argc, const char **argv, const char *prefix)
+static int module_clone(int argc, const char **argv, const char *prefix,
+			struct repository *repo UNUSED)
 {
 	int dissociate = 0, quiet = 0, progress = 0, require_init = 0;
 	struct module_clone_data clone_data = MODULE_CLONE_DATA_INIT;
@@ -2779,7 +2786,8 @@ static int update_submodules(struct update_data *update_data)
 	return ret;
 }
 
-static int module_update(int argc, const char **argv, const char *prefix)
+static int module_update(int argc, const char **argv, const char *prefix,
+			 struct repository *repo UNUSED)
 {
 	struct pathspec pathspec = { 0 };
 	struct pathspec pathspec2 = { 0 };
@@ -2911,7 +2919,8 @@ static int module_update(int argc, const char **argv, const char *prefix)
 	return ret;
 }
 
-static int push_check(int argc, const char **argv, const char *prefix UNUSED)
+static int push_check(int argc, const char **argv, const char *prefix UNUSED,
+		      struct repository *repo UNUSED)
 {
 	struct remote *remote;
 	const char *superproject_head;
@@ -2991,7 +3000,8 @@ static int push_check(int argc, const char **argv, const char *prefix UNUSED)
 	return 0;
 }
 
-static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
+static int absorb_git_dirs(int argc, const char **argv, const char *prefix,
+			   struct repository *repo UNUSED)
 {
 	int i;
 	struct pathspec pathspec = { 0 };
@@ -3024,7 +3034,8 @@ static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
 	return ret;
 }
 
-static int module_set_url(int argc, const char **argv, const char *prefix)
+static int module_set_url(int argc, const char **argv, const char *prefix,
+			  struct repository *repo UNUSED)
 {
 	int quiet = 0, ret;
 	const char *newurl;
@@ -3063,7 +3074,8 @@ static int module_set_url(int argc, const char **argv, const char *prefix)
 	return !!ret;
 }
 
-static int module_set_branch(int argc, const char **argv, const char *prefix)
+static int module_set_branch(int argc, const char **argv, const char *prefix,
+			     struct repository *repo UNUSED)
 {
 	int opt_default = 0, ret;
 	const char *opt_branch = NULL;
@@ -3113,7 +3125,8 @@ static int module_set_branch(int argc, const char **argv, const char *prefix)
 	return !!ret;
 }
 
-static int module_create_branch(int argc, const char **argv, const char *prefix)
+static int module_create_branch(int argc, const char **argv, const char *prefix,
+				struct repository *repo UNUSED)
 {
 	enum branch_track track;
 	int quiet = 0, force = 0, reflog = 0, dry_run = 0;
@@ -3424,7 +3437,8 @@ static void die_on_repo_without_commits(const char *path)
 	strbuf_release(&sb);
 }
 
-static int module_add(int argc, const char **argv, const char *prefix)
+static int module_add(int argc, const char **argv, const char *prefix,
+		      struct repository *repo UNUSED)
 {
 	int force = 0, quiet = 0, progress = 0, dissociate = 0;
 	struct add_data add_data = ADD_DATA_INIT;
@@ -3557,7 +3571,7 @@ static int module_add(int argc, const char **argv, const char *prefix)
 int cmd_submodule__helper(int argc,
 			  const char **argv,
 			  const char *prefix,
-			  struct repository *repo UNUSED)
+			  struct repository *repo)
 {
 	parse_opt_subcommand_fn *fn = NULL;
 	const char *const usage[] = {
@@ -3583,5 +3597,5 @@ int cmd_submodule__helper(int argc,
 	};
 	argc = parse_options(argc, argv, prefix, options, usage, 0);
 
-	return fn(argc, argv, prefix);
+	return fn(argc, argv, prefix, repo);
 }
diff --git a/builtin/worktree.c b/builtin/worktree.c
index dae63dedf4cac2621f51f95a39aa456b33acd894..824dd71d643f112f384cde4de1373698c67de254 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -231,7 +231,8 @@ static void prune_worktrees(void)
 	strbuf_release(&reason);
 }
 
-static int prune(int ac, const char **av, const char *prefix)
+static int prune(int ac, const char **av, const char *prefix,
+		 struct repository *repo UNUSED)
 {
 	struct option options[] = {
 		OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
@@ -763,7 +764,8 @@ static char *dwim_branch(const char *path, char **new_branch)
 	return NULL;
 }
 
-static int add(int ac, const char **av, const char *prefix)
+static int add(int ac, const char **av, const char *prefix,
+	       struct repository *repo UNUSED)
 {
 	struct add_opts opts;
 	const char *new_branch_force = NULL;
@@ -1039,7 +1041,8 @@ static void pathsort(struct worktree **wt)
 	QSORT(wt, n, pathcmp);
 }
 
-static int list(int ac, const char **av, const char *prefix)
+static int list(int ac, const char **av, const char *prefix,
+		struct repository *repo UNUSED)
 {
 	int porcelain = 0;
 	int line_terminator = '\n';
@@ -1084,7 +1087,8 @@ static int list(int ac, const char **av, const char *prefix)
 	return 0;
 }
 
-static int lock_worktree(int ac, const char **av, const char *prefix)
+static int lock_worktree(int ac, const char **av, const char *prefix,
+			 struct repository *repo UNUSED)
 {
 	const char *reason = "", *old_reason;
 	struct option options[] = {
@@ -1119,7 +1123,8 @@ static int lock_worktree(int ac, const char **av, const char *prefix)
 	return 0;
 }
 
-static int unlock_worktree(int ac, const char **av, const char *prefix)
+static int unlock_worktree(int ac, const char **av, const char *prefix,
+			   struct repository *repo UNUSED)
 {
 	struct option options[] = {
 		OPT_END()
@@ -1182,7 +1187,8 @@ static void validate_no_submodules(const struct worktree *wt)
 		die(_("working trees containing submodules cannot be moved or removed"));
 }
 
-static int move_worktree(int ac, const char **av, const char *prefix)
+static int move_worktree(int ac, const char **av, const char *prefix,
+			 struct repository *repo UNUSED)
 {
 	int force = 0;
 	struct option options[] = {
@@ -1312,7 +1318,8 @@ static int delete_git_work_tree(struct worktree *wt)
 	return ret;
 }
 
-static int remove_worktree(int ac, const char **av, const char *prefix)
+static int remove_worktree(int ac, const char **av, const char *prefix,
+			   struct repository *repo UNUSED)
 {
 	int force = 0;
 	struct option options[] = {
@@ -1377,7 +1384,8 @@ static void report_repair(int iserr, const char *path, const char *msg, void *cb
 	}
 }
 
-static int repair(int ac, const char **av, const char *prefix)
+static int repair(int ac, const char **av, const char *prefix,
+		  struct repository *repo UNUSED)
 {
 	const char **p;
 	const char *self[] = { ".", NULL };
@@ -1397,7 +1405,7 @@ static int repair(int ac, const char **av, const char *prefix)
 int cmd_worktree(int ac,
 		 const char **av,
 		 const char *prefix,
-		 struct repository *repo UNUSED)
+		 struct repository *repo)
 {
 	parse_opt_subcommand_fn *fn = NULL;
 	struct option options[] = {
@@ -1422,5 +1430,5 @@ int cmd_worktree(int ac,
 	prepare_repo_settings(the_repository);
 	the_repository->settings.command_requires_full_index = 0;
 
-	return fn(ac, av, prefix);
+	return fn(ac, av, prefix, repo);
 }
diff --git a/parse-options.h b/parse-options.h
index ae15342390837c21503ab812648a7d8fd21432a8..f0801d4532a175b65783689f2a68fb56da2c8e87 100644
--- a/parse-options.h
+++ b/parse-options.h
@@ -3,6 +3,8 @@
 
 #include "gettext.h"
 
+struct repository;
+
 /**
  * Refer to Documentation/technical/api-parse-options.txt for the API doc.
  */
@@ -73,7 +75,7 @@ typedef enum parse_opt_result parse_opt_ll_cb(struct parse_opt_ctx_t *ctx,
 					      const char *arg, int unset);
 
 typedef int parse_opt_subcommand_fn(int argc, const char **argv,
-				    const char *prefix);
+				    const char *prefix, struct repository *repo);
 
 /*
  * `type`::
diff --git a/t/helper/test-parse-options.c b/t/helper/test-parse-options.c
index 5250913d99eba18a28878d3904cb7b2399670d02..e5b1fe287e3ec94f0d8a3a99adb68842d52992f6 100644
--- a/t/helper/test-parse-options.c
+++ b/t/helper/test-parse-options.c
@@ -1,8 +1,11 @@
+#define USE_THE_REPOSITORY_VARIABLE
+
 #include "test-tool.h"
 #include "parse-options.h"
 #include "strbuf.h"
 #include "string-list.h"
 #include "trace2.h"
+#include "repository.h"
 
 static int boolean = 0;
 static int integer = 0;
@@ -282,14 +285,16 @@ int cmd__parse_options_flags(int argc, const char **argv)
 	return parse_options_flags__cmd(argc, argv, test_flags);
 }
 
-static int subcmd_one(int argc, const char **argv, const char *prefix UNUSED)
+static int subcmd_one(int argc, const char **argv, const char *prefix UNUSED,
+		      struct repository *repo UNUSED)
 {
 	printf("fn: subcmd_one\n");
 	print_args(argc, argv);
 	return 0;
 }
 
-static int subcmd_two(int argc, const char **argv, const char *prefix UNUSED)
+static int subcmd_two(int argc, const char **argv, const char *prefix UNUSED,
+		      struct repository *repo UNUSED)
 {
 	printf("fn: subcmd_two\n");
 	print_args(argc, argv);
@@ -319,7 +324,7 @@ static int parse_subcommand__cmd(int argc, const char **argv,
 
 	printf("opt: %d\n", opt);
 
-	return fn(argc, argv, NULL);
+	return fn(argc, argv, NULL, the_repository);
 }
 
 int cmd__parse_subcommand(int argc, const char **argv)

-- 
2.47.0


^ permalink raw reply related	[flat|nested] 70+ messages in thread

* [PATCH 2/8] midx-write: add repository field to `write_midx_context`
  2024-11-15 13:42 [PATCH 0/8] Change midx.c and midx-write.c to not use global variables Karthik Nayak
  2024-11-15 13:42 ` [PATCH 1/8] builtin: pass repository to sub commands Karthik Nayak
@ 2024-11-15 13:42 ` Karthik Nayak
  2024-11-15 23:05   ` brian m. carlson
  2024-11-18  8:05   ` Patrick Steinhardt
  2024-11-15 13:42 ` [PATCH 3/8] midx-write: pass down repository to `write_midx_file[_only]` Karthik Nayak
                   ` (7 subsequent siblings)
  9 siblings, 2 replies; 70+ messages in thread
From: Karthik Nayak @ 2024-11-15 13:42 UTC (permalink / raw)
  To: git; +Cc: Karthik Nayak, me, peff

The struct `write_midx_context` is used to pass context for creating
MIDX files. Adding the repository field here ensures that most functions
within `midx-write.c` have access to the field and can use that instead
of the global `repository` variable. This involves passing the
`repository` field to `write_midx_internal`. To do this, we add
`the_repository` usage to two non-static functions, which we'll remove
in upcoming commits.

With this, modify the static functions in `midx-write.c` to not use
global variables. This means, either we use existing alternatives (like
`repository` struct), or we pass down required fields from other
functions.

Signed-off-by: default avatarKarthik Nayak <karthik.188@gmail.com>
---
 midx-write.c | 97 ++++++++++++++++++++++++++++++++----------------------------
 1 file changed, 52 insertions(+), 45 deletions(-)

diff --git a/midx-write.c b/midx-write.c
index c57726ef9475df693890d61627ce91409c1def7c..a384f7ddc8a396d0cffd528132bb8fcdc6b37e24 100644
--- a/midx-write.c
+++ b/midx-write.c
@@ -35,13 +35,13 @@ extern void clear_incremental_midx_files_ext(const char *object_dir,
 extern int cmp_idx_or_pack_name(const char *idx_or_pack_name,
 				const char *idx_name);
 
-static size_t write_midx_header(struct hashfile *f,
-				unsigned char num_chunks,
+static size_t write_midx_header(const struct git_hash_algo *hash_algo,
+				struct hashfile *f, unsigned char num_chunks,
 				uint32_t num_packs)
 {
 	hashwrite_be32(f, MIDX_SIGNATURE);
 	hashwrite_u8(f, MIDX_VERSION);
-	hashwrite_u8(f, oid_version(the_hash_algo));
+	hashwrite_u8(f, oid_version(hash_algo));
 	hashwrite_u8(f, num_chunks);
 	hashwrite_u8(f, 0); /* unused */
 	hashwrite_be32(f, num_packs);
@@ -110,6 +110,8 @@ struct write_midx_context {
 	uint32_t num_multi_pack_indexes_before;
 
 	struct string_list *to_include;
+
+	struct repository *repo;
 };
 
 static int should_include_pack(const struct write_midx_context *ctx,
@@ -154,7 +156,7 @@ static void add_pack_to_midx(const char *full_path, size_t full_path_len,
 			return;
 
 		ALLOC_GROW(ctx->info, ctx->nr + 1, ctx->alloc);
-		p = add_packed_git(the_repository, full_path, full_path_len, 0);
+		p = add_packed_git(ctx->repo, full_path, full_path_len, 0);
 		if (!p) {
 			warning(_("failed to add packfile '%s'"),
 				full_path);
@@ -480,7 +482,7 @@ static int write_midx_oid_lookup(struct hashfile *f,
 				 void *data)
 {
 	struct write_midx_context *ctx = data;
-	unsigned char hash_len = the_hash_algo->rawsz;
+	unsigned char hash_len = ctx->repo->hash_algo->rawsz;
 	struct pack_midx_entry *list = ctx->entries;
 	uint32_t i;
 
@@ -605,7 +607,7 @@ static uint32_t *midx_pack_order(struct write_midx_context *ctx)
 	uint32_t *pack_order, base_objects = 0;
 	uint32_t i;
 
-	trace2_region_enter("midx", "midx_pack_order", the_repository);
+	trace2_region_enter("midx", "midx_pack_order", ctx->repo);
 
 	if (ctx->incremental && ctx->base_midx)
 		base_objects = ctx->base_midx->num_objects +
@@ -640,7 +642,7 @@ static uint32_t *midx_pack_order(struct write_midx_context *ctx)
 	}
 	free(data);
 
-	trace2_region_leave("midx", "midx_pack_order", the_repository);
+	trace2_region_leave("midx", "midx_pack_order", ctx->repo);
 
 	return pack_order;
 }
@@ -651,9 +653,10 @@ static void write_midx_reverse_index(char *midx_name, unsigned char *midx_hash,
 	struct strbuf buf = STRBUF_INIT;
 	char *tmp_file;
 
-	trace2_region_enter("midx", "write_midx_reverse_index", the_repository);
+	trace2_region_enter("midx", "write_midx_reverse_index", ctx->repo);
 
-	strbuf_addf(&buf, "%s-%s.rev", midx_name, hash_to_hex(midx_hash));
+	strbuf_addf(&buf, "%s-%s.rev", midx_name, hash_to_hex_algop(midx_hash,
+								    ctx->repo->hash_algo));
 
 	tmp_file = write_rev_file_order(NULL, ctx->pack_order, ctx->entries_nr,
 					midx_hash, WRITE_REV);
@@ -664,7 +667,7 @@ static void write_midx_reverse_index(char *midx_name, unsigned char *midx_hash,
 	strbuf_release(&buf);
 	free(tmp_file);
 
-	trace2_region_leave("midx", "write_midx_reverse_index", the_repository);
+	trace2_region_leave("midx", "write_midx_reverse_index", ctx->repo);
 }
 
 static void prepare_midx_packing_data(struct packing_data *pdata,
@@ -672,10 +675,10 @@ static void prepare_midx_packing_data(struct packing_data *pdata,
 {
 	uint32_t i;
 
-	trace2_region_enter("midx", "prepare_midx_packing_data", the_repository);
+	trace2_region_enter("midx", "prepare_midx_packing_data", ctx->repo);
 
 	memset(pdata, 0, sizeof(struct packing_data));
-	prepare_packing_data(the_repository, pdata);
+	prepare_packing_data(ctx->repo, pdata);
 
 	for (i = 0; i < ctx->entries_nr; i++) {
 		uint32_t pos = ctx->pack_order[i];
@@ -686,7 +689,7 @@ static void prepare_midx_packing_data(struct packing_data *pdata,
 			       ctx->info[ctx->pack_perm[from->pack_int_id]].p);
 	}
 
-	trace2_region_leave("midx", "prepare_midx_packing_data", the_repository);
+	trace2_region_leave("midx", "prepare_midx_packing_data", ctx->repo);
 }
 
 static int add_ref_to_pending(const char *refname, const char *referent UNUSED,
@@ -702,7 +705,7 @@ static int add_ref_to_pending(const char *refname, const char *referent UNUSED,
 		return 0;
 	}
 
-	if (!peel_iterated_oid(the_repository, oid, &peeled))
+	if (!peel_iterated_oid(revs->repo, oid, &peeled))
 		oid = &peeled;
 
 	object = parse_object_or_die(oid, refname);
@@ -760,7 +763,7 @@ static int read_refs_snapshot(const char *refs_snapshot,
 			hex = &buf.buf[1];
 		}
 
-		if (parse_oid_hex(hex, &oid, &end) < 0)
+		if (parse_oid_hex_algop(hex, &oid, &end, revs->repo->hash_algo) < 0)
 			die(_("could not parse line: %s"), buf.buf);
 		if (*end)
 			die(_("malformed line: %s"), buf.buf);
@@ -783,17 +786,16 @@ static struct commit **find_commits_for_midx_bitmap(uint32_t *indexed_commits_nr
 	struct rev_info revs;
 	struct bitmap_commit_cb cb = {0};
 
-	trace2_region_enter("midx", "find_commits_for_midx_bitmap",
-			    the_repository);
+	trace2_region_enter("midx", "find_commits_for_midx_bitmap", ctx->repo);
 
 	cb.ctx = ctx;
 
-	repo_init_revisions(the_repository, &revs, NULL);
+	repo_init_revisions(ctx->repo, &revs, NULL);
 	if (refs_snapshot) {
 		read_refs_snapshot(refs_snapshot, &revs);
 	} else {
 		setup_revisions(0, NULL, &revs, NULL);
-		refs_for_each_ref(get_main_ref_store(the_repository),
+		refs_for_each_ref(get_main_ref_store(ctx->repo),
 				  add_ref_to_pending, &revs);
 	}
 
@@ -821,13 +823,12 @@ static struct commit **find_commits_for_midx_bitmap(uint32_t *indexed_commits_nr
 
 	release_revisions(&revs);
 
-	trace2_region_leave("midx", "find_commits_for_midx_bitmap",
-			    the_repository);
+	trace2_region_leave("midx", "find_commits_for_midx_bitmap", ctx->repo);
 
 	return cb.commits;
 }
 
-static int write_midx_bitmap(const char *midx_name,
+static int write_midx_bitmap(struct repository *r, const char *midx_name,
 			     const unsigned char *midx_hash,
 			     struct packing_data *pdata,
 			     struct commit **commits,
@@ -840,9 +841,9 @@ static int write_midx_bitmap(const char *midx_name,
 	struct bitmap_writer writer;
 	struct pack_idx_entry **index;
 	char *bitmap_name = xstrfmt("%s-%s.bitmap", midx_name,
-					hash_to_hex(midx_hash));
+				    hash_to_hex_algop(midx_hash, r->hash_algo));
 
-	trace2_region_enter("midx", "write_midx_bitmap", the_repository);
+	trace2_region_enter("midx", "write_midx_bitmap", r);
 
 	if (flags & MIDX_WRITE_BITMAP_HASH_CACHE)
 		options |= BITMAP_OPT_HASH_CACHE;
@@ -859,7 +860,7 @@ static int write_midx_bitmap(const char *midx_name,
 	for (i = 0; i < pdata->nr_objects; i++)
 		index[i] = &pdata->objects[i].idx;
 
-	bitmap_writer_init(&writer, the_repository, pdata);
+	bitmap_writer_init(&writer, r, pdata);
 	bitmap_writer_show_progress(&writer, flags & MIDX_PROGRESS);
 	bitmap_writer_build_type_index(&writer, index);
 
@@ -892,7 +893,7 @@ static int write_midx_bitmap(const char *midx_name,
 	free(bitmap_name);
 	bitmap_writer_free(&writer);
 
-	trace2_region_leave("midx", "write_midx_bitmap", the_repository);
+	trace2_region_leave("midx", "write_midx_bitmap", r);
 
 	return ret;
 }
@@ -944,7 +945,7 @@ static int fill_packs_from_midx(struct write_midx_context *ctx,
 			 */
 			if (flags & MIDX_WRITE_REV_INDEX ||
 			    preferred_pack_name) {
-				if (prepare_midx_pack(the_repository, m,
+				if (prepare_midx_pack(ctx->repo, m,
 						      m->num_packs_in_base + i)) {
 					error(_("could not load pack"));
 					return 1;
@@ -1049,7 +1050,7 @@ static void clear_midx_files(const char *object_dir,
 	strbuf_release(&buf);
 }
 
-static int write_midx_internal(const char *object_dir,
+static int write_midx_internal(struct repository *r, const char *object_dir,
 			       struct string_list *packs_to_include,
 			       struct string_list *packs_to_drop,
 			       const char *preferred_pack_name,
@@ -1070,7 +1071,9 @@ static int write_midx_internal(const char *object_dir,
 	const char **keep_hashes = NULL;
 	struct chunkfile *cf;
 
-	trace2_region_enter("midx", "write_midx_internal", the_repository);
+	trace2_region_enter("midx", "write_midx_internal", r);
+
+	ctx.repo = r;
 
 	ctx.incremental = !!(flags & MIDX_WRITE_INCREMENTAL);
 	if (ctx.incremental && (flags & MIDX_WRITE_BITMAP))
@@ -1087,8 +1090,7 @@ static int write_midx_internal(const char *object_dir,
 			  midx_name.buf);
 
 	if (!packs_to_include || ctx.incremental) {
-		struct multi_pack_index *m = lookup_multi_pack_index(the_repository,
-								     object_dir);
+		struct multi_pack_index *m = lookup_multi_pack_index(r, object_dir);
 		if (m && !midx_checksum_valid(m)) {
 			warning(_("ignoring existing multi-pack-index; checksum mismatch"));
 			m = NULL;
@@ -1351,7 +1353,7 @@ static int write_midx_internal(const char *object_dir,
 	add_chunk(cf, MIDX_CHUNKID_OIDFANOUT, MIDX_CHUNK_FANOUT_SIZE,
 		  write_midx_oid_fanout);
 	add_chunk(cf, MIDX_CHUNKID_OIDLOOKUP,
-		  st_mult(ctx.entries_nr, the_hash_algo->rawsz),
+		  st_mult(ctx.entries_nr, r->hash_algo->rawsz),
 		  write_midx_oid_lookup);
 	add_chunk(cf, MIDX_CHUNKID_OBJECTOFFSETS,
 		  st_mult(ctx.entries_nr, MIDX_CHUNK_OFFSET_WIDTH),
@@ -1373,7 +1375,8 @@ static int write_midx_internal(const char *object_dir,
 			  write_midx_bitmapped_packs);
 	}
 
-	write_midx_header(f, get_num_chunks(cf), ctx.nr - dropped_packs);
+	write_midx_header(r->hash_algo, f, get_num_chunks(cf),
+			  ctx.nr - dropped_packs);
 	write_chunkfile(cf, &ctx);
 
 	finalize_hashfile(f, midx_hash, FSYNC_COMPONENT_PACK_METADATA,
@@ -1405,7 +1408,7 @@ static int write_midx_internal(const char *object_dir,
 		FREE_AND_NULL(ctx.entries);
 		ctx.entries_nr = 0;
 
-		if (write_midx_bitmap(midx_name.buf, midx_hash, &pdata,
+		if (write_midx_bitmap(r, midx_name.buf, midx_hash, &pdata,
 				      commits, commits_nr, ctx.pack_order,
 				      flags) < 0) {
 			error(_("could not write multi-pack bitmap"));
@@ -1449,12 +1452,13 @@ static int write_midx_internal(const char *object_dir,
 		strbuf_release(&final_midx_name);
 
 		keep_hashes[ctx.num_multi_pack_indexes_before] =
-			xstrdup(hash_to_hex(midx_hash));
+			xstrdup(hash_to_hex_algop(midx_hash, r->hash_algo));
 
 		for (i = 0; i < ctx.num_multi_pack_indexes_before; i++) {
 			uint32_t j = ctx.num_multi_pack_indexes_before - i - 1;
 
-			keep_hashes[j] = xstrdup(hash_to_hex(get_midx_checksum(m)));
+			keep_hashes[j] = xstrdup(hash_to_hex_algop(get_midx_checksum(m),
+								   r->hash_algo));
 			m = m->base_midx;
 		}
 
@@ -1462,11 +1466,11 @@ static int write_midx_internal(const char *object_dir,
 			fprintf(get_lock_file_fp(&lk), "%s\n", keep_hashes[i]);
 	} else {
 		keep_hashes[ctx.num_multi_pack_indexes_before] =
-			xstrdup(hash_to_hex(midx_hash));
+			xstrdup(hash_to_hex_algop(midx_hash, r->hash_algo));
 	}
 
 	if (ctx.m || ctx.base_midx)
-		close_object_store(the_repository->objects);
+		close_object_store(ctx.repo->objects);
 
 	if (commit_lock_file(&lk) < 0)
 		die_errno(_("could not write multi-pack-index"));
@@ -1495,7 +1499,7 @@ static int write_midx_internal(const char *object_dir,
 	}
 	strbuf_release(&midx_name);
 
-	trace2_region_leave("midx", "write_midx_internal", the_repository);
+	trace2_region_leave("midx", "write_midx_internal", r);
 
 	return result;
 }
@@ -1505,8 +1509,8 @@ int write_midx_file(const char *object_dir,
 		    const char *refs_snapshot,
 		    unsigned flags)
 {
-	return write_midx_internal(object_dir, NULL, NULL, preferred_pack_name,
-				   refs_snapshot, flags);
+	return write_midx_internal(the_repository, object_dir, NULL, NULL,
+				   preferred_pack_name, refs_snapshot, flags);
 }
 
 int write_midx_file_only(const char *object_dir,
@@ -1515,8 +1519,9 @@ int write_midx_file_only(const char *object_dir,
 			 const char *refs_snapshot,
 			 unsigned flags)
 {
-	return write_midx_internal(object_dir, packs_to_include, NULL,
-				   preferred_pack_name, refs_snapshot, flags);
+	return write_midx_internal(the_repository, object_dir, packs_to_include,
+				   NULL, preferred_pack_name, refs_snapshot,
+				   flags);
 }
 
 int expire_midx_packs(struct repository *r, const char *object_dir, unsigned flags)
@@ -1572,7 +1577,8 @@ int expire_midx_packs(struct repository *r, const char *object_dir, unsigned fla
 	free(count);
 
 	if (packs_to_drop.nr)
-		result = write_midx_internal(object_dir, NULL, &packs_to_drop, NULL, NULL, flags);
+		result = write_midx_internal(r, object_dir, NULL,
+					     &packs_to_drop, NULL, NULL, flags);
 
 	string_list_clear(&packs_to_drop, 0);
 
@@ -1769,7 +1775,8 @@ int midx_repack(struct repository *r, const char *object_dir, size_t batch_size,
 		goto cleanup;
 	}
 
-	result = write_midx_internal(object_dir, NULL, NULL, NULL, NULL, flags);
+	result = write_midx_internal(r, object_dir, NULL, NULL, NULL, NULL,
+				     flags);
 
 cleanup:
 	free(include_pack);

-- 
2.47.0


^ permalink raw reply related	[flat|nested] 70+ messages in thread

* [PATCH 3/8] midx-write: pass down repository to `write_midx_file[_only]`
  2024-11-15 13:42 [PATCH 0/8] Change midx.c and midx-write.c to not use global variables Karthik Nayak
  2024-11-15 13:42 ` [PATCH 1/8] builtin: pass repository to sub commands Karthik Nayak
  2024-11-15 13:42 ` [PATCH 2/8] midx-write: add repository field to `write_midx_context` Karthik Nayak
@ 2024-11-15 13:42 ` Karthik Nayak
  2024-11-18  8:05   ` Patrick Steinhardt
  2024-11-15 13:42 ` [PATCH 4/8] midx: cleanup internal usage of `the_repository` and `the_hash_algo` Karthik Nayak
                   ` (6 subsequent siblings)
  9 siblings, 1 reply; 70+ messages in thread
From: Karthik Nayak @ 2024-11-15 13:42 UTC (permalink / raw)
  To: git; +Cc: Karthik Nayak, me, peff

In the previous commit, we passed the repository field to all
subcommands in the `builtin/` directory. We utilize this to pass the
repository field down to the `write_midx_file[_only]` functions to
remove the usage of `the_repository` global variables.

With this, we remove all usage of global variables in `midx-write.c` and
so we can remove the `USE_THE_REPOSITORY_VARIABLE` guard from the file.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 builtin/multi-pack-index.c |  6 +++---
 builtin/repack.c           |  2 +-
 midx-write.c               | 22 +++++++++-------------
 midx.h                     | 10 ++++------
 4 files changed, 17 insertions(+), 23 deletions(-)

diff --git a/builtin/multi-pack-index.c b/builtin/multi-pack-index.c
index 85e40a4b6d3e47e9ec1ec27c094455e5ba75b5b0..2a938466f53aaa11096170554fe11a4ed46a25e4 100644
--- a/builtin/multi-pack-index.c
+++ b/builtin/multi-pack-index.c
@@ -120,7 +120,7 @@ static void read_packs_from_stdin(struct string_list *to)
 
 static int cmd_multi_pack_index_write(int argc, const char **argv,
 				      const char *prefix,
-				      struct repository *repo UNUSED)
+				      struct repository *repo)
 {
 	struct option *options;
 	static struct option builtin_multi_pack_index_write_options[] = {
@@ -165,7 +165,7 @@ static int cmd_multi_pack_index_write(int argc, const char **argv,
 
 		read_packs_from_stdin(&packs);
 
-		ret = write_midx_file_only(opts.object_dir, &packs,
+		ret = write_midx_file_only(repo, opts.object_dir, &packs,
 					   opts.preferred_pack,
 					   opts.refs_snapshot, opts.flags);
 
@@ -176,7 +176,7 @@ static int cmd_multi_pack_index_write(int argc, const char **argv,
 
 	}
 
-	ret = write_midx_file(opts.object_dir, opts.preferred_pack,
+	ret = write_midx_file(repo, opts.object_dir, opts.preferred_pack,
 			      opts.refs_snapshot, opts.flags);
 
 	free(opts.refs_snapshot);
diff --git a/builtin/repack.c b/builtin/repack.c
index 96a4fa234bddfd2b63c8d9733379d9b1012a4014..9c21fc482dfb387c818c0d0a74f781848b5a0953 100644
--- a/builtin/repack.c
+++ b/builtin/repack.c
@@ -1569,7 +1569,7 @@ int cmd_repack(int argc,
 		unsigned flags = 0;
 		if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_INCREMENTAL, 0))
 			flags |= MIDX_WRITE_INCREMENTAL;
-		write_midx_file(repo_get_object_directory(the_repository),
+		write_midx_file(the_repository, repo_get_object_directory(the_repository),
 				NULL, NULL, flags);
 	}
 
diff --git a/midx-write.c b/midx-write.c
index a384f7ddc8a396d0cffd528132bb8fcdc6b37e24..5af29899bbe279c7c3ff4bc2c65330620ce37ee2 100644
--- a/midx-write.c
+++ b/midx-write.c
@@ -1,5 +1,3 @@
-#define USE_THE_REPOSITORY_VARIABLE
-
 #include "git-compat-util.h"
 #include "abspath.h"
 #include "config.h"
@@ -1504,24 +1502,22 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
 	return result;
 }
 
-int write_midx_file(const char *object_dir,
+int write_midx_file(struct repository *r, const char *object_dir,
 		    const char *preferred_pack_name,
-		    const char *refs_snapshot,
-		    unsigned flags)
+		    const char *refs_snapshot, unsigned flags)
 {
-	return write_midx_internal(the_repository, object_dir, NULL, NULL,
-				   preferred_pack_name, refs_snapshot, flags);
+	return write_midx_internal(r, object_dir, NULL, NULL,
+				   preferred_pack_name, refs_snapshot,
+				   flags);
 }
 
-int write_midx_file_only(const char *object_dir,
+int write_midx_file_only(struct repository *r, const char *object_dir,
 			 struct string_list *packs_to_include,
 			 const char *preferred_pack_name,
-			 const char *refs_snapshot,
-			 unsigned flags)
+			 const char *refs_snapshot, unsigned flags)
 {
-	return write_midx_internal(the_repository, object_dir, packs_to_include,
-				   NULL, preferred_pack_name, refs_snapshot,
-				   flags);
+	return write_midx_internal(r, object_dir, packs_to_include, NULL,
+				   preferred_pack_name, refs_snapshot, flags);
 }
 
 int expire_midx_packs(struct repository *r, const char *object_dir, unsigned flags)
diff --git a/midx.h b/midx.h
index 3b0ac4d8788b373c59fe69ca2d78e9d914702bc0..c37ad5b5242b56d21fd76bd59957a1bdb82786ec 100644
--- a/midx.h
+++ b/midx.h
@@ -123,15 +123,13 @@ int prepare_multi_pack_index_one(struct repository *r, const char *object_dir, i
  * Variant of write_midx_file which writes a MIDX containing only the packs
  * specified in packs_to_include.
  */
-int write_midx_file(const char *object_dir,
-		    const char *preferred_pack_name,
-		    const char *refs_snapshot,
+int write_midx_file(struct repository *r, const char *object_dir,
+		    const char *preferred_pack_name, const char *refs_snapshot,
 		    unsigned flags);
-int write_midx_file_only(const char *object_dir,
+int write_midx_file_only(struct repository *r, const char *object_dir,
 			 struct string_list *packs_to_include,
 			 const char *preferred_pack_name,
-			 const char *refs_snapshot,
-			 unsigned flags);
+			 const char *refs_snapshot, unsigned flags);
 void clear_midx_file(struct repository *r);
 int verify_midx_file(struct repository *r, const char *object_dir, unsigned flags);
 int expire_midx_packs(struct repository *r, const char *object_dir, unsigned flags);

-- 
2.47.0


^ permalink raw reply related	[flat|nested] 70+ messages in thread

* [PATCH 4/8] midx: cleanup internal usage of `the_repository` and `the_hash_algo`
  2024-11-15 13:42 [PATCH 0/8] Change midx.c and midx-write.c to not use global variables Karthik Nayak
                   ` (2 preceding siblings ...)
  2024-11-15 13:42 ` [PATCH 3/8] midx-write: pass down repository to `write_midx_file[_only]` Karthik Nayak
@ 2024-11-15 13:42 ` Karthik Nayak
  2024-11-18  8:06   ` Patrick Steinhardt
  2024-11-15 13:42 ` [PATCH 5/8] midx: pass `repository` to `load_multi_pack_index` Karthik Nayak
                   ` (5 subsequent siblings)
  9 siblings, 1 reply; 70+ messages in thread
From: Karthik Nayak @ 2024-11-15 13:42 UTC (permalink / raw)
  To: git; +Cc: Karthik Nayak, me, peff

In the `midx.c` file, there are multiple usages of `the_repository` and
`the_hash_algo` within static functions of the file. Some of the usages
can be simply swapped out with the available `repository` struct
available. While some of them can be swapped out by passing the
repository to the required functions.

This leaves out only some other usages of `the_repository` and
`the_hash_algo` in the file in non-static functions, which we'll tackle
in upcoming commits.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 midx.c | 49 +++++++++++++++++++++++++++----------------------
 1 file changed, 27 insertions(+), 22 deletions(-)

diff --git a/midx.c b/midx.c
index 079c45a1aafb658a7b887ac216cc6ecf5f0fb6ff..6f0fb8285af14843da132ef1b0c3a8bdd06732c9 100644
--- a/midx.c
+++ b/midx.c
@@ -25,7 +25,7 @@ int cmp_idx_or_pack_name(const char *idx_or_pack_name,
 
 const unsigned char *get_midx_checksum(struct multi_pack_index *m)
 {
-	return m->data + m->data_len - the_hash_algo->rawsz;
+	return m->data + m->data_len - m->repo->hash_algo->rawsz;
 }
 
 void get_midx_filename(struct strbuf *out, const char *object_dir)
@@ -94,7 +94,8 @@ static int midx_read_object_offsets(const unsigned char *chunk_start,
 
 #define MIDX_MIN_SIZE (MIDX_HEADER_SIZE + the_hash_algo->rawsz)
 
-static struct multi_pack_index *load_multi_pack_index_one(const char *object_dir,
+static struct multi_pack_index *load_multi_pack_index_one(struct repository *r,
+							  const char *object_dir,
 							  const char *midx_name,
 							  int local)
 {
@@ -131,7 +132,7 @@ static struct multi_pack_index *load_multi_pack_index_one(const char *object_dir
 	m->data = midx_map;
 	m->data_len = midx_size;
 	m->local = local;
-	m->repo = the_repository;
+	m->repo = r;
 
 	m->signature = get_be32(m->data);
 	if (m->signature != MIDX_SIGNATURE)
@@ -144,12 +145,12 @@ static struct multi_pack_index *load_multi_pack_index_one(const char *object_dir
 		      m->version);
 
 	hash_version = m->data[MIDX_BYTE_HASH_VERSION];
-	if (hash_version != oid_version(the_hash_algo)) {
+	if (hash_version != oid_version(r->hash_algo)) {
 		error(_("multi-pack-index hash version %u does not match version %u"),
-		      hash_version, oid_version(the_hash_algo));
+		      hash_version, oid_version(r->hash_algo));
 		goto cleanup_fail;
 	}
-	m->hash_len = the_hash_algo->rawsz;
+	m->hash_len = r->hash_algo->rawsz;
 
 	m->num_chunks = m->data[MIDX_BYTE_NUM_CHUNKS];
 
@@ -206,8 +207,8 @@ static struct multi_pack_index *load_multi_pack_index_one(const char *object_dir
 			      m->pack_names[i]);
 	}
 
-	trace2_data_intmax("midx", the_repository, "load/num_packs", m->num_packs);
-	trace2_data_intmax("midx", the_repository, "load/num_objects", m->num_objects);
+	trace2_data_intmax("midx", r, "load/num_packs", m->num_packs);
+	trace2_data_intmax("midx", r, "load/num_objects", m->num_objects);
 
 	free_chunkfile(cf);
 	return m;
@@ -240,8 +241,9 @@ void get_split_midx_filename_ext(struct strbuf *buf, const char *object_dir,
 	strbuf_addf(buf, "/multi-pack-index-%s.%s", hash_to_hex(hash), ext);
 }
 
-static int open_multi_pack_index_chain(const char *chain_file,
-				       int *fd, struct stat *st)
+static int open_multi_pack_index_chain(const struct git_hash_algo *hash_algo,
+				       const char *chain_file, int *fd,
+				       struct stat *st)
 {
 	*fd = git_open(chain_file);
 	if (*fd < 0)
@@ -250,7 +252,7 @@ static int open_multi_pack_index_chain(const char *chain_file,
 		close(*fd);
 		return 0;
 	}
-	if (st->st_size < the_hash_algo->hexsz) {
+	if (st->st_size < hash_algo->hexsz) {
 		close(*fd);
 		if (!st->st_size) {
 			/* treat empty files the same as missing */
@@ -292,7 +294,8 @@ static int add_midx_to_chain(struct multi_pack_index *midx,
 	return 1;
 }
 
-static struct multi_pack_index *load_midx_chain_fd_st(const char *object_dir,
+static struct multi_pack_index *load_midx_chain_fd_st(struct repository *r,
+						      const char *object_dir,
 						      int local,
 						      int fd, struct stat *st,
 						      int *incomplete_chain)
@@ -303,7 +306,7 @@ static struct multi_pack_index *load_midx_chain_fd_st(const char *object_dir,
 	uint32_t i, count;
 	FILE *fp = xfdopen(fd, "r");
 
-	count = st->st_size / (the_hash_algo->hexsz + 1);
+	count = st->st_size / (r->hash_algo->hexsz + 1);
 
 	for (i = 0; i < count; i++) {
 		struct multi_pack_index *m;
@@ -312,7 +315,7 @@ static struct multi_pack_index *load_midx_chain_fd_st(const char *object_dir,
 		if (strbuf_getline_lf(&buf, fp) == EOF)
 			break;
 
-		if (get_oid_hex(buf.buf, &layer)) {
+		if (get_oid_hex_algop(buf.buf, &layer, r->hash_algo)) {
 			warning(_("invalid multi-pack-index chain: line '%s' "
 				  "not a hash"),
 				buf.buf);
@@ -325,7 +328,7 @@ static struct multi_pack_index *load_midx_chain_fd_st(const char *object_dir,
 		strbuf_reset(&buf);
 		get_split_midx_filename_ext(&buf, object_dir, layer.hash,
 					    MIDX_EXT_MIDX);
-		m = load_multi_pack_index_one(object_dir, buf.buf, local);
+		m = load_multi_pack_index_one(r, object_dir, buf.buf, local);
 
 		if (m) {
 			if (add_midx_to_chain(m, midx_chain)) {
@@ -348,7 +351,8 @@ static struct multi_pack_index *load_midx_chain_fd_st(const char *object_dir,
 	return midx_chain;
 }
 
-static struct multi_pack_index *load_multi_pack_index_chain(const char *object_dir,
+static struct multi_pack_index *load_multi_pack_index_chain(struct repository *r,
+							    const char *object_dir,
 							    int local)
 {
 	struct strbuf chain_file = STRBUF_INIT;
@@ -357,10 +361,10 @@ static struct multi_pack_index *load_multi_pack_index_chain(const char *object_d
 	struct multi_pack_index *m = NULL;
 
 	get_midx_chain_filename(&chain_file, object_dir);
-	if (open_multi_pack_index_chain(chain_file.buf, &fd, &st)) {
+	if (open_multi_pack_index_chain(r->hash_algo, chain_file.buf, &fd, &st)) {
 		int incomplete;
 		/* ownership of fd is taken over by load function */
-		m = load_midx_chain_fd_st(object_dir, local, fd, &st,
+		m = load_midx_chain_fd_st(r, object_dir, local, fd, &st,
 					  &incomplete);
 	}
 
@@ -376,9 +380,10 @@ struct multi_pack_index *load_multi_pack_index(const char *object_dir,
 
 	get_midx_filename(&midx_name, object_dir);
 
-	m = load_multi_pack_index_one(object_dir, midx_name.buf, local);
+	m = load_multi_pack_index_one(the_repository, object_dir,
+				      midx_name.buf, local);
 	if (!m)
-		m = load_multi_pack_index_chain(object_dir, local);
+		m = load_multi_pack_index_chain(the_repository, object_dir, local);
 
 	strbuf_release(&midx_name);
 
@@ -520,7 +525,7 @@ int bsearch_one_midx(const struct object_id *oid, struct multi_pack_index *m,
 		     uint32_t *result)
 {
 	int ret = bsearch_hash(oid->hash, m->chunk_oid_fanout,
-			       m->chunk_oid_lookup, the_hash_algo->rawsz,
+			       m->chunk_oid_lookup, m->repo->hash_algo->rawsz,
 			       result);
 	if (result)
 		*result += m->num_objects_in_base;
@@ -551,7 +556,7 @@ struct object_id *nth_midxed_object_oid(struct object_id *oid,
 	n = midx_for_object(&m, n);
 
 	oidread(oid, m->chunk_oid_lookup + st_mult(m->hash_len, n),
-		the_repository->hash_algo);
+		m->repo->hash_algo);
 	return oid;
 }
 

-- 
2.47.0


^ permalink raw reply related	[flat|nested] 70+ messages in thread

* [PATCH 5/8] midx: pass `repository` to `load_multi_pack_index`
  2024-11-15 13:42 [PATCH 0/8] Change midx.c and midx-write.c to not use global variables Karthik Nayak
                   ` (3 preceding siblings ...)
  2024-11-15 13:42 ` [PATCH 4/8] midx: cleanup internal usage of `the_repository` and `the_hash_algo` Karthik Nayak
@ 2024-11-15 13:42 ` Karthik Nayak
  2024-11-15 13:42 ` [PATCH 6/8] midx: pass down `hash_algo` to `get_midx_filename[_ext]` Karthik Nayak
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 70+ messages in thread
From: Karthik Nayak @ 2024-11-15 13:42 UTC (permalink / raw)
  To: git; +Cc: Karthik Nayak, me, peff

The `load_multi_pack_index` function in midx uses `the_repository`
variable to access the `repository` struct. Let's modify the function
and its callee's to send the `repository` field.

This moves usage of `the_repository` to the `test-read-midx.c` file.
While that is not optimal, it is okay, since we'll slowly move the usage
of `the_repository` up the layers and eventually remove it entirely.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 midx.c                    | 11 ++++++-----
 midx.h                    |  4 +++-
 t/helper/test-read-midx.c |  8 ++++----
 3 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/midx.c b/midx.c
index 6f0fb8285af14843da132ef1b0c3a8bdd06732c9..98ee84d4a8bf388906634ad695ff39acdaa2c6d5 100644
--- a/midx.c
+++ b/midx.c
@@ -372,7 +372,8 @@ static struct multi_pack_index *load_multi_pack_index_chain(struct repository *r
 	return m;
 }
 
-struct multi_pack_index *load_multi_pack_index(const char *object_dir,
+struct multi_pack_index *load_multi_pack_index(struct repository *r,
+					       const char *object_dir,
 					       int local)
 {
 	struct strbuf midx_name = STRBUF_INIT;
@@ -380,10 +381,10 @@ struct multi_pack_index *load_multi_pack_index(const char *object_dir,
 
 	get_midx_filename(&midx_name, object_dir);
 
-	m = load_multi_pack_index_one(the_repository, object_dir,
+	m = load_multi_pack_index_one(r, object_dir,
 				      midx_name.buf, local);
 	if (!m)
-		m = load_multi_pack_index_chain(the_repository, object_dir, local);
+		m = load_multi_pack_index_chain(r, object_dir, local);
 
 	strbuf_release(&midx_name);
 
@@ -727,7 +728,7 @@ int prepare_multi_pack_index_one(struct repository *r, const char *object_dir, i
 		if (!strcmp(object_dir, m_search->object_dir))
 			return 1;
 
-	m = load_multi_pack_index(object_dir, local);
+	m = load_multi_pack_index(r, object_dir, local);
 
 	if (m) {
 		struct multi_pack_index *mp = r->objects->multi_pack_index;
@@ -881,7 +882,7 @@ int verify_midx_file(struct repository *r, const char *object_dir, unsigned flag
 	struct pair_pos_vs_id *pairs = NULL;
 	uint32_t i;
 	struct progress *progress = NULL;
-	struct multi_pack_index *m = load_multi_pack_index(object_dir, 1);
+	struct multi_pack_index *m = load_multi_pack_index(r, object_dir, 1);
 	struct multi_pack_index *curr;
 	verify_midx_error = 0;
 
diff --git a/midx.h b/midx.h
index c37ad5b5242b56d21fd76bd59957a1bdb82786ec..78efa28d35371795fa33c68660278182debb60ab 100644
--- a/midx.h
+++ b/midx.h
@@ -97,7 +97,9 @@ void get_midx_chain_filename(struct strbuf *buf, const char *object_dir);
 void get_split_midx_filename_ext(struct strbuf *buf, const char *object_dir,
 				 const unsigned char *hash, const char *ext);
 
-struct multi_pack_index *load_multi_pack_index(const char *object_dir, int local);
+struct multi_pack_index *load_multi_pack_index(struct repository *r,
+					       const char *object_dir,
+					       int local);
 int prepare_midx_pack(struct repository *r, struct multi_pack_index *m, uint32_t pack_int_id);
 struct packed_git *nth_midxed_pack(struct multi_pack_index *m,
 				   uint32_t pack_int_id);
diff --git a/t/helper/test-read-midx.c b/t/helper/test-read-midx.c
index 438fb9fc6197fc465f79d9a65b719ae315fed373..fc632369618917e2d8cdcb77bd9073c61e1544c1 100644
--- a/t/helper/test-read-midx.c
+++ b/t/helper/test-read-midx.c
@@ -18,7 +18,7 @@ static int read_midx_file(const char *object_dir, const char *checksum,
 	struct multi_pack_index *m;
 
 	setup_git_directory();
-	m = load_multi_pack_index(object_dir, 1);
+	m = load_multi_pack_index(the_repository, object_dir, 1);
 
 	if (!m)
 		return 1;
@@ -82,7 +82,7 @@ static int read_midx_checksum(const char *object_dir)
 	struct multi_pack_index *m;
 
 	setup_git_directory();
-	m = load_multi_pack_index(object_dir, 1);
+	m = load_multi_pack_index(the_repository, object_dir, 1);
 	if (!m)
 		return 1;
 	printf("%s\n", hash_to_hex(get_midx_checksum(m)));
@@ -98,7 +98,7 @@ static int read_midx_preferred_pack(const char *object_dir)
 
 	setup_git_directory();
 
-	midx = load_multi_pack_index(object_dir, 1);
+	midx = load_multi_pack_index(the_repository, object_dir, 1);
 	if (!midx)
 		return 1;
 
@@ -121,7 +121,7 @@ static int read_midx_bitmapped_packs(const char *object_dir)
 
 	setup_git_directory();
 
-	midx = load_multi_pack_index(object_dir, 1);
+	midx = load_multi_pack_index(the_repository, object_dir, 1);
 	if (!midx)
 		return 1;
 

-- 
2.47.0


^ permalink raw reply related	[flat|nested] 70+ messages in thread

* [PATCH 6/8] midx: pass down `hash_algo` to `get_midx_filename[_ext]`
  2024-11-15 13:42 [PATCH 0/8] Change midx.c and midx-write.c to not use global variables Karthik Nayak
                   ` (4 preceding siblings ...)
  2024-11-15 13:42 ` [PATCH 5/8] midx: pass `repository` to `load_multi_pack_index` Karthik Nayak
@ 2024-11-15 13:42 ` Karthik Nayak
  2024-11-16 13:16   ` shejialuo
  2024-11-15 13:42 ` [PATCH 7/8] midx: pass down `hash_algo` to `get_split_midx_filename_ext` Karthik Nayak
                   ` (3 subsequent siblings)
  9 siblings, 1 reply; 70+ messages in thread
From: Karthik Nayak @ 2024-11-15 13:42 UTC (permalink / raw)
  To: git; +Cc: Karthik Nayak, me, peff

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


^ permalink raw reply related	[flat|nested] 70+ messages in thread

* [PATCH 7/8] midx: pass down `hash_algo` to `get_split_midx_filename_ext`
  2024-11-15 13:42 [PATCH 0/8] Change midx.c and midx-write.c to not use global variables Karthik Nayak
                   ` (5 preceding siblings ...)
  2024-11-15 13:42 ` [PATCH 6/8] midx: pass down `hash_algo` to `get_midx_filename[_ext]` Karthik Nayak
@ 2024-11-15 13:42 ` Karthik Nayak
  2024-11-16 13:20   ` shejialuo
  2024-11-15 13:42 ` [PATCH 8/8] midx: inline the `MIDX_MIN_SIZE` definition Karthik Nayak
                   ` (2 subsequent siblings)
  9 siblings, 1 reply; 70+ messages in thread
From: Karthik Nayak @ 2024-11-15 13:42 UTC (permalink / raw)
  To: git; +Cc: Karthik Nayak, me, peff

Similar to the previous commit, pass down `hash_algo` to
`get_split_midx_filename_ext` so we can use `hash_to_hex_algop` and not
rely on the `the_repository` global variable.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 midx-write.c |  7 ++++---
 midx.c       | 10 ++++++----
 midx.h       |  3 ++-
 3 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/midx-write.c b/midx-write.c
index 7c1563845993075d622f59faeb25462180434abd..21a64f791cd8addf33437d85304dcf35bf17d904 100644
--- a/midx-write.c
+++ b/midx-write.c
@@ -992,7 +992,8 @@ static int link_midx_to_chain(struct multi_pack_index *m)
 
 		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,
+		get_split_midx_filename_ext(m->repo->hash_algo, &to,
+					    m->object_dir, hash,
 					    midx_exts[i].split);
 
 		if (link(from.buf, to.buf) < 0 && errno != ENOENT) {
@@ -1438,8 +1439,8 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
 		if (link_midx_to_chain(ctx.base_midx) < 0)
 			return -1;
 
-		get_split_midx_filename_ext(&final_midx_name, object_dir,
-					    midx_hash, MIDX_EXT_MIDX);
+		get_split_midx_filename_ext(r->hash_algo, &final_midx_name,
+					    object_dir, midx_hash, MIDX_EXT_MIDX);
 
 		if (rename_tempfile(&incr, final_midx_name.buf) < 0) {
 			error_errno(_("unable to rename new multi-pack-index layer"));
diff --git a/midx.c b/midx.c
index 9bed4185ff4d44602aedfe0329dd840ff9e85435..f45ea842cd6eda23d2eadc9deaae43839aef24c1 100644
--- a/midx.c
+++ b/midx.c
@@ -236,11 +236,13 @@ void get_midx_chain_filename(struct strbuf *buf, const char *object_dir)
 	strbuf_addstr(buf, "/multi-pack-index-chain");
 }
 
-void get_split_midx_filename_ext(struct strbuf *buf, const char *object_dir,
+void get_split_midx_filename_ext(const struct git_hash_algo *hash_algo,
+				 struct strbuf *buf, const char *object_dir,
 				 const unsigned char *hash, const char *ext)
 {
 	get_midx_chain_dirname(buf, object_dir);
-	strbuf_addf(buf, "/multi-pack-index-%s.%s", hash_to_hex(hash), ext);
+	strbuf_addf(buf, "/multi-pack-index-%s.%s",
+		    hash_to_hex_algop(hash, hash_algo), ext);
 }
 
 static int open_multi_pack_index_chain(const struct git_hash_algo *hash_algo,
@@ -328,8 +330,8 @@ static struct multi_pack_index *load_midx_chain_fd_st(struct repository *r,
 		valid = 0;
 
 		strbuf_reset(&buf);
-		get_split_midx_filename_ext(&buf, object_dir, layer.hash,
-					    MIDX_EXT_MIDX);
+		get_split_midx_filename_ext(r->hash_algo, &buf, object_dir,
+					    layer.hash, MIDX_EXT_MIDX);
 		m = load_multi_pack_index_one(r, object_dir, buf.buf, local);
 
 		if (m) {
diff --git a/midx.h b/midx.h
index 7620820d4d0272926af9e4eeb68bfb73404c7ec2..9d1374cbd58d016bb82338337b2a4e5ba7234092 100644
--- a/midx.h
+++ b/midx.h
@@ -97,7 +97,8 @@ void get_midx_filename_ext(const struct git_hash_algo *hash_algo,
 			   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);
-void get_split_midx_filename_ext(struct strbuf *buf, const char *object_dir,
+void get_split_midx_filename_ext(const struct git_hash_algo *hash_algo,
+				 struct strbuf *buf, const char *object_dir,
 				 const unsigned char *hash, const char *ext);
 
 struct multi_pack_index *load_multi_pack_index(struct repository *r,

-- 
2.47.0


^ permalink raw reply related	[flat|nested] 70+ messages in thread

* [PATCH 8/8] midx: inline the `MIDX_MIN_SIZE` definition
  2024-11-15 13:42 [PATCH 0/8] Change midx.c and midx-write.c to not use global variables Karthik Nayak
                   ` (6 preceding siblings ...)
  2024-11-15 13:42 ` [PATCH 7/8] midx: pass down `hash_algo` to `get_split_midx_filename_ext` Karthik Nayak
@ 2024-11-15 13:42 ` Karthik Nayak
  2024-11-15 14:13 ` [PATCH 0/8] Change midx.c and midx-write.c to not use global variables karthik nayak
  2024-11-19 15:36 ` [PATCH v2 00/10] " Karthik Nayak
  9 siblings, 0 replies; 70+ messages in thread
From: Karthik Nayak @ 2024-11-15 13:42 UTC (permalink / raw)
  To: git; +Cc: Karthik Nayak, me, peff

The `MIDX_MIN_SIZE` definition is used to check the midx_size in
`local_multi_pack_index_one`. This definitions relies on the
`the_hash_algo` global variable. Let's inline this and remove the global
variable usage.

With this, we can remove `USE_THE_REPOSITORY_VARIABLE` usage from
`midx.c`.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 midx.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/midx.c b/midx.c
index f45ea842cd6eda23d2eadc9deaae43839aef24c1..e0eae1c25ec91f7db5670ff9bacdf0e195c35795 100644
--- a/midx.c
+++ b/midx.c
@@ -1,5 +1,3 @@
-#define USE_THE_REPOSITORY_VARIABLE
-
 #include "git-compat-util.h"
 #include "config.h"
 #include "dir.h"
@@ -94,8 +92,6 @@ static int midx_read_object_offsets(const unsigned char *chunk_start,
 	return 0;
 }
 
-#define MIDX_MIN_SIZE (MIDX_HEADER_SIZE + the_hash_algo->rawsz)
-
 static struct multi_pack_index *load_multi_pack_index_one(struct repository *r,
 							  const char *object_dir,
 							  const char *midx_name,
@@ -122,7 +118,7 @@ static struct multi_pack_index *load_multi_pack_index_one(struct repository *r,
 
 	midx_size = xsize_t(st.st_size);
 
-	if (midx_size < MIDX_MIN_SIZE) {
+	if (midx_size < (MIDX_HEADER_SIZE + r->hash_algo->rawsz)) {
 		error(_("multi-pack-index file %s is too small"), midx_name);
 		goto cleanup_fail;
 	}

-- 
2.47.0


^ permalink raw reply related	[flat|nested] 70+ messages in thread

* Re: [PATCH 0/8] Change midx.c and midx-write.c to not use global variables
  2024-11-15 13:42 [PATCH 0/8] Change midx.c and midx-write.c to not use global variables Karthik Nayak
                   ` (7 preceding siblings ...)
  2024-11-15 13:42 ` [PATCH 8/8] midx: inline the `MIDX_MIN_SIZE` definition Karthik Nayak
@ 2024-11-15 14:13 ` karthik nayak
  2024-11-19 15:36 ` [PATCH v2 00/10] " Karthik Nayak
  9 siblings, 0 replies; 70+ messages in thread
From: karthik nayak @ 2024-11-15 14:13 UTC (permalink / raw)
  To: git; +Cc: me, peff

[-- Attachment #1: Type: text/plain, Size: 1144 bytes --]

Karthik Nayak <karthik.188@gmail.com> writes:

> Similar to the earlier patch series on cleaning up packfile.c and
> removing usage of global variables [1], we change the midx.c and
> midx-write.c files to no longer use global variables.
>
> This is done by the following:
>   - Usage of repository variable already available in existing structs.
>   - Passing down repository variable from other subsystems.
>   - Modifying all subcommands to obtain repository variable from the
>   command in `builtins/` and passing down the variable from there.
>
> The biggest change is in the first commit, wherein we modify all
> subcommands to add the repository variable. Since the subcommand
> definition are not often changed, it shouldn't cause too many conflicts
> with in flight topics.
>
> Since the `packfile.c` cleanup is still in flight, this series is based
> on top of master: b31fb630c0 (Merge https://github.com/j6t/git-gui,
> 2024-11-11) with those patches merged in.

There are some topics in `seen` which would conflict with this series. I
think the fixes should generally be trivial and I'll try and iron them
out in the next version.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 690 bytes --]

^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH 2/8] midx-write: add repository field to `write_midx_context`
  2024-11-15 13:42 ` [PATCH 2/8] midx-write: add repository field to `write_midx_context` Karthik Nayak
@ 2024-11-15 23:05   ` brian m. carlson
  2024-11-18 15:17     ` karthik nayak
  2024-11-18  8:05   ` Patrick Steinhardt
  1 sibling, 1 reply; 70+ messages in thread
From: brian m. carlson @ 2024-11-15 23:05 UTC (permalink / raw)
  To: Karthik Nayak; +Cc: git, me, peff

[-- Attachment #1: Type: text/plain, Size: 938 bytes --]

On 2024-11-15 at 13:42:15, Karthik Nayak wrote:
> The struct `write_midx_context` is used to pass context for creating
> MIDX files. Adding the repository field here ensures that most functions
> within `midx-write.c` have access to the field and can use that instead
> of the global `repository` variable. This involves passing the
> `repository` field to `write_midx_internal`. To do this, we add
> `the_repository` usage to two non-static functions, which we'll remove
> in upcoming commits.
> 
> With this, modify the static functions in `midx-write.c` to not use
> global variables. This means, either we use existing alternatives (like
> `repository` struct), or we pass down required fields from other
> functions.
> 
> Signed-off-by: default avatarKarthik Nayak <karthik.188@gmail.com>

It looks like the sign-off here may have gotten corrupted.
-- 
brian m. carlson (they/them or he/him)
Toronto, Ontario, CA

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 262 bytes --]

^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH 1/8] builtin: pass repository to sub commands
  2024-11-15 13:42 ` [PATCH 1/8] builtin: pass repository to sub commands Karthik Nayak
@ 2024-11-16 12:49   ` shejialuo
  2024-11-18  8:05     ` Patrick Steinhardt
  2024-11-18 15:17     ` karthik nayak
  0 siblings, 2 replies; 70+ messages in thread
From: shejialuo @ 2024-11-16 12:49 UTC (permalink / raw)
  To: Karthik Nayak; +Cc: git, me, peff

On Fri, Nov 15, 2024 at 02:42:14PM +0100, Karthik Nayak wrote:

[snip]

> diff --git a/t/helper/test-parse-options.c b/t/helper/test-parse-options.c
> index 5250913d99eba18a28878d3904cb7b2399670d02..e5b1fe287e3ec94f0d8a3a99adb68842d52992f6 100644
> --- a/t/helper/test-parse-options.c
> +++ b/t/helper/test-parse-options.c
> @@ -1,8 +1,11 @@
> +#define USE_THE_REPOSITORY_VARIABLE
> +
>  #include "test-tool.h"
>  #include "parse-options.h"
>  #include "strbuf.h"
>  #include "string-list.h"
>  #include "trace2.h"
> +#include "repository.h"
>  
>  static int boolean = 0;
>  static int integer = 0;
> @@ -282,14 +285,16 @@ int cmd__parse_options_flags(int argc, const char **argv)
>  	return parse_options_flags__cmd(argc, argv, test_flags);
>  }
>  
> -static int subcmd_one(int argc, const char **argv, const char *prefix UNUSED)
> +static int subcmd_one(int argc, const char **argv, const char *prefix UNUSED,
> +		      struct repository *repo UNUSED)
>  {
>  	printf("fn: subcmd_one\n");
>  	print_args(argc, argv);
>  	return 0;
>  }
>  
> -static int subcmd_two(int argc, const char **argv, const char *prefix UNUSED)
> +static int subcmd_two(int argc, const char **argv, const char *prefix UNUSED,
> +		      struct repository *repo UNUSED)
>  {
>  	printf("fn: subcmd_two\n");
>  	print_args(argc, argv);
> @@ -319,7 +324,7 @@ static int parse_subcommand__cmd(int argc, const char **argv,
>  
>  	printf("opt: %d\n", opt);
>  
> -	return fn(argc, argv, NULL);
> +	return fn(argc, argv, NULL, the_repository);

Do we need to pass the real "the_repository" variable here. From my
understanding, we could just pass `NULL` to avoid including the
"repository.h" and the macro. However, I am not familiar with the
"test-tool" helper. It my comment was wrong, please ignore.

Thanks,
Jialuo

^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH 6/8] midx: pass down `hash_algo` to `get_midx_filename[_ext]`
  2024-11-15 13:42 ` [PATCH 6/8] midx: pass down `hash_algo` to `get_midx_filename[_ext]` Karthik Nayak
@ 2024-11-16 13:16   ` shejialuo
  2024-11-18 16:25     ` karthik nayak
  0 siblings, 1 reply; 70+ messages in thread
From: shejialuo @ 2024-11-16 13:16 UTC (permalink / raw)
  To: Karthik Nayak; +Cc: git, me, peff

On Fri, Nov 15, 2024 at 02:42:19PM +0100, Karthik Nayak wrote:

[snip]

> 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);

I don't think it's a good idea to put "hash_algo" in the first argument,
we should put it at the last to align with the code style where we use
"git_hash_algo".

>  void get_midx_chain_dirname(struct strbuf *buf, const char *object_dir);
>  void get_midx_chain_filename(struct strbuf *buf, const char *object_dir);

^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH 7/8] midx: pass down `hash_algo` to `get_split_midx_filename_ext`
  2024-11-15 13:42 ` [PATCH 7/8] midx: pass down `hash_algo` to `get_split_midx_filename_ext` Karthik Nayak
@ 2024-11-16 13:20   ` shejialuo
  0 siblings, 0 replies; 70+ messages in thread
From: shejialuo @ 2024-11-16 13:20 UTC (permalink / raw)
  To: Karthik Nayak; +Cc: git, me, peff

On Fri, Nov 15, 2024 at 02:42:20PM +0100, Karthik Nayak wrote:

[snip]

> diff --git a/midx.h b/midx.h
> index 7620820d4d0272926af9e4eeb68bfb73404c7ec2..9d1374cbd58d016bb82338337b2a4e5ba7234092 100644
> --- a/midx.h
> +++ b/midx.h
> @@ -97,7 +97,8 @@ void get_midx_filename_ext(const struct git_hash_algo *hash_algo,
>  			   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);
> -void get_split_midx_filename_ext(struct strbuf *buf, const char *object_dir,
> +void get_split_midx_filename_ext(const struct git_hash_algo *hash_algo,
> +				 struct strbuf *buf, const char *object_dir,

Similar to comment for the previous patch, we should not put "hash_algo"
at the first.

>  				 const unsigned char *hash, const char *ext);
>  
>  struct multi_pack_index *load_multi_pack_index(struct repository *r,
> 
> -- 
> 2.47.0
> 

^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH 1/8] builtin: pass repository to sub commands
  2024-11-16 12:49   ` shejialuo
@ 2024-11-18  8:05     ` Patrick Steinhardt
  2024-11-18 15:17     ` karthik nayak
  1 sibling, 0 replies; 70+ messages in thread
From: Patrick Steinhardt @ 2024-11-18  8:05 UTC (permalink / raw)
  To: shejialuo; +Cc: Karthik Nayak, git, me, peff

On Sat, Nov 16, 2024 at 08:49:35PM +0800, shejialuo wrote:
> On Fri, Nov 15, 2024 at 02:42:14PM +0100, Karthik Nayak wrote:
> 
> [snip]
> 
> > diff --git a/t/helper/test-parse-options.c b/t/helper/test-parse-options.c
> > index 5250913d99eba18a28878d3904cb7b2399670d02..e5b1fe287e3ec94f0d8a3a99adb68842d52992f6 100644
> > --- a/t/helper/test-parse-options.c
> > +++ b/t/helper/test-parse-options.c
> > @@ -1,8 +1,11 @@
> > +#define USE_THE_REPOSITORY_VARIABLE
> > +
> >  #include "test-tool.h"
> >  #include "parse-options.h"
> >  #include "strbuf.h"
> >  #include "string-list.h"
> >  #include "trace2.h"
> > +#include "repository.h"
> >  
> >  static int boolean = 0;
> >  static int integer = 0;
> > @@ -282,14 +285,16 @@ int cmd__parse_options_flags(int argc, const char **argv)
> >  	return parse_options_flags__cmd(argc, argv, test_flags);
> >  }
> >  
> > -static int subcmd_one(int argc, const char **argv, const char *prefix UNUSED)
> > +static int subcmd_one(int argc, const char **argv, const char *prefix UNUSED,
> > +		      struct repository *repo UNUSED)
> >  {
> >  	printf("fn: subcmd_one\n");
> >  	print_args(argc, argv);
> >  	return 0;
> >  }
> >  
> > -static int subcmd_two(int argc, const char **argv, const char *prefix UNUSED)
> > +static int subcmd_two(int argc, const char **argv, const char *prefix UNUSED,
> > +		      struct repository *repo UNUSED)
> >  {
> >  	printf("fn: subcmd_two\n");
> >  	print_args(argc, argv);
> > @@ -319,7 +324,7 @@ static int parse_subcommand__cmd(int argc, const char **argv,
> >  
> >  	printf("opt: %d\n", opt);
> >  
> > -	return fn(argc, argv, NULL);
> > +	return fn(argc, argv, NULL, the_repository);
> 
> Do we need to pass the real "the_repository" variable here. From my
> understanding, we could just pass `NULL` to avoid including the
> "repository.h" and the macro. However, I am not familiar with the
> "test-tool" helper. It my comment was wrong, please ignore.

It certainly is surprising. If this really is required I'd expect an
explanation in the commit message.

Patrick

^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH 2/8] midx-write: add repository field to `write_midx_context`
  2024-11-15 13:42 ` [PATCH 2/8] midx-write: add repository field to `write_midx_context` Karthik Nayak
  2024-11-15 23:05   ` brian m. carlson
@ 2024-11-18  8:05   ` Patrick Steinhardt
  2024-11-18 15:37     ` karthik nayak
  1 sibling, 1 reply; 70+ messages in thread
From: Patrick Steinhardt @ 2024-11-18  8:05 UTC (permalink / raw)
  To: Karthik Nayak; +Cc: git, me, peff

On Fri, Nov 15, 2024 at 02:42:15PM +0100, Karthik Nayak wrote:
> @@ -651,9 +653,10 @@ static void write_midx_reverse_index(char *midx_name, unsigned char *midx_hash,
>  	struct strbuf buf = STRBUF_INIT;
>  	char *tmp_file;
>  
> -	trace2_region_enter("midx", "write_midx_reverse_index", the_repository);
> +	trace2_region_enter("midx", "write_midx_reverse_index", ctx->repo);
>  
> -	strbuf_addf(&buf, "%s-%s.rev", midx_name, hash_to_hex(midx_hash));
> +	strbuf_addf(&buf, "%s-%s.rev", midx_name, hash_to_hex_algop(midx_hash,
> +								    ctx->repo->hash_algo));
>  
>  	tmp_file = write_rev_file_order(NULL, ctx->pack_order, ctx->entries_nr,
>  					midx_hash, WRITE_REV);

I think it would be nice to split up the changes that start to pass down
a repo via the context and those that start to actually call a different
function like we have here. Otherwise it's hard to spot the callsites
where one actually has to think about what's happening.

Patrick

^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH 3/8] midx-write: pass down repository to `write_midx_file[_only]`
  2024-11-15 13:42 ` [PATCH 3/8] midx-write: pass down repository to `write_midx_file[_only]` Karthik Nayak
@ 2024-11-18  8:05   ` Patrick Steinhardt
  0 siblings, 0 replies; 70+ messages in thread
From: Patrick Steinhardt @ 2024-11-18  8:05 UTC (permalink / raw)
  To: Karthik Nayak; +Cc: git, me, peff

On Fri, Nov 15, 2024 at 02:42:16PM +0100, Karthik Nayak wrote:
> In the previous commit, we passed the repository field to all

Not quite accurate, since the preceding commit is instead passing the
repo via the context, not adapting the subcommands.

> subcommands in the `builtin/` directory. We utilize this to pass the
> repository field down to the `write_midx_file[_only]` functions to
> remove the usage of `the_repository` global variables.
> 
> With this, we remove all usage of global variables in `midx-write.c` and
> so we can remove the `USE_THE_REPOSITORY_VARIABLE` guard from the file.

Note that we typically use imperative commit messages, as if we were
instructing the code to change. So you should likely drop the "we" both
here and further up in "We utilize".

> diff --git a/builtin/multi-pack-index.c b/builtin/multi-pack-index.c
> index 85e40a4b6d3e47e9ec1ec27c094455e5ba75b5b0..2a938466f53aaa11096170554fe11a4ed46a25e4 100644
> --- a/builtin/multi-pack-index.c
> +++ b/builtin/multi-pack-index.c
> @@ -120,7 +120,7 @@ static void read_packs_from_stdin(struct string_list *to)
>  
>  static int cmd_multi_pack_index_write(int argc, const char **argv,
>  				      const char *prefix,
> -				      struct repository *repo UNUSED)
> +				      struct repository *repo)
>  {
>  	struct option *options;
>  	static struct option builtin_multi_pack_index_write_options[] = {

One thing I've briefly wondered: do we actually need the whole repo, or
do we only care about `packed_git`? I would have assumed that it should
be the latter as an MIDX only gets written for packfiles, but that is
likely only a naive assumption.

> diff --git a/midx-write.c b/midx-write.c
> index a384f7ddc8a396d0cffd528132bb8fcdc6b37e24..5af29899bbe279c7c3ff4bc2c65330620ce37ee2 100644
> --- a/midx-write.c
> +++ b/midx-write.c
> @@ -1,5 +1,3 @@
> -#define USE_THE_REPOSITORY_VARIABLE
> -
>  #include "git-compat-util.h"
>  #include "abspath.h"
>  #include "config.h"

Nice.

Patrick

^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH 4/8] midx: cleanup internal usage of `the_repository` and `the_hash_algo`
  2024-11-15 13:42 ` [PATCH 4/8] midx: cleanup internal usage of `the_repository` and `the_hash_algo` Karthik Nayak
@ 2024-11-18  8:06   ` Patrick Steinhardt
  2024-11-18 16:16     ` karthik nayak
  0 siblings, 1 reply; 70+ messages in thread
From: Patrick Steinhardt @ 2024-11-18  8:06 UTC (permalink / raw)
  To: Karthik Nayak; +Cc: git, me, peff

On Fri, Nov 15, 2024 at 02:42:17PM +0100, Karthik Nayak wrote:
> In the `midx.c` file, there are multiple usages of `the_repository` and
> `the_hash_algo` within static functions of the file. Some of the usages
> can be simply swapped out with the available `repository` struct
> available. While some of them can be swapped out by passing the

The second "available" should probably be "parameter" or "variable" or
something like this? Or it just needs to be dropped altogether.

Patrick

^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH 1/8] builtin: pass repository to sub commands
  2024-11-16 12:49   ` shejialuo
  2024-11-18  8:05     ` Patrick Steinhardt
@ 2024-11-18 15:17     ` karthik nayak
  1 sibling, 0 replies; 70+ messages in thread
From: karthik nayak @ 2024-11-18 15:17 UTC (permalink / raw)
  To: shejialuo; +Cc: git, me, peff

[-- Attachment #1: Type: text/plain, Size: 1972 bytes --]

shejialuo <shejialuo@gmail.com> writes:

> On Fri, Nov 15, 2024 at 02:42:14PM +0100, Karthik Nayak wrote:
>
> [snip]
>
>> diff --git a/t/helper/test-parse-options.c b/t/helper/test-parse-options.c
>> index 5250913d99eba18a28878d3904cb7b2399670d02..e5b1fe287e3ec94f0d8a3a99adb68842d52992f6 100644
>> --- a/t/helper/test-parse-options.c
>> +++ b/t/helper/test-parse-options.c
>> @@ -1,8 +1,11 @@
>> +#define USE_THE_REPOSITORY_VARIABLE
>> +
>>  #include "test-tool.h"
>>  #include "parse-options.h"
>>  #include "strbuf.h"
>>  #include "string-list.h"
>>  #include "trace2.h"
>> +#include "repository.h"
>>
>>  static int boolean = 0;
>>  static int integer = 0;
>> @@ -282,14 +285,16 @@ int cmd__parse_options_flags(int argc, const char **argv)
>>  	return parse_options_flags__cmd(argc, argv, test_flags);
>>  }
>>
>> -static int subcmd_one(int argc, const char **argv, const char *prefix UNUSED)
>> +static int subcmd_one(int argc, const char **argv, const char *prefix UNUSED,
>> +		      struct repository *repo UNUSED)
>>  {
>>  	printf("fn: subcmd_one\n");
>>  	print_args(argc, argv);
>>  	return 0;
>>  }
>>
>> -static int subcmd_two(int argc, const char **argv, const char *prefix UNUSED)
>> +static int subcmd_two(int argc, const char **argv, const char *prefix UNUSED,
>> +		      struct repository *repo UNUSED)
>>  {
>>  	printf("fn: subcmd_two\n");
>>  	print_args(argc, argv);
>> @@ -319,7 +324,7 @@ static int parse_subcommand__cmd(int argc, const char **argv,
>>
>>  	printf("opt: %d\n", opt);
>>
>> -	return fn(argc, argv, NULL);
>> +	return fn(argc, argv, NULL, the_repository);
>
> Do we need to pass the real "the_repository" variable here. From my
> understanding, we could just pass `NULL` to avoid including the
> "repository.h" and the macro. However, I am not familiar with the
> "test-tool" helper. It my comment was wrong, please ignore.
>

We don't _need_ to. The test should work with NULL too. Let me modify
this, thanks!

> Thanks,
> Jialuo

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 690 bytes --]

^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH 2/8] midx-write: add repository field to `write_midx_context`
  2024-11-15 23:05   ` brian m. carlson
@ 2024-11-18 15:17     ` karthik nayak
  0 siblings, 0 replies; 70+ messages in thread
From: karthik nayak @ 2024-11-18 15:17 UTC (permalink / raw)
  To: brian m. carlson; +Cc: git, me, peff

[-- Attachment #1: Type: text/plain, Size: 1026 bytes --]

"brian m. carlson" <sandals@crustytoothpaste.net> writes:

> On 2024-11-15 at 13:42:15, Karthik Nayak wrote:
>> The struct `write_midx_context` is used to pass context for creating
>> MIDX files. Adding the repository field here ensures that most functions
>> within `midx-write.c` have access to the field and can use that instead
>> of the global `repository` variable. This involves passing the
>> `repository` field to `write_midx_internal`. To do this, we add
>> `the_repository` usage to two non-static functions, which we'll remove
>> in upcoming commits.
>>
>> With this, modify the static functions in `midx-write.c` to not use
>> global variables. This means, either we use existing alternatives (like
>> `repository` struct), or we pass down required fields from other
>> functions.
>>
>> Signed-off-by: default avatarKarthik Nayak <karthik.188@gmail.com>
>
> It looks like the sign-off here may have gotten corrupted.
> --
> brian m. carlson (they/them or he/him)
> Toronto, Ontario, CA

Indeed, will fix. Thanks!

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 690 bytes --]

^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH 2/8] midx-write: add repository field to `write_midx_context`
  2024-11-18  8:05   ` Patrick Steinhardt
@ 2024-11-18 15:37     ` karthik nayak
  0 siblings, 0 replies; 70+ messages in thread
From: karthik nayak @ 2024-11-18 15:37 UTC (permalink / raw)
  To: Patrick Steinhardt; +Cc: git, me, peff

[-- Attachment #1: Type: text/plain, Size: 1289 bytes --]

Patrick Steinhardt <ps@pks.im> writes:

> On Fri, Nov 15, 2024 at 02:42:15PM +0100, Karthik Nayak wrote:
>> @@ -651,9 +653,10 @@ static void write_midx_reverse_index(char *midx_name, unsigned char *midx_hash,
>>  	struct strbuf buf = STRBUF_INIT;
>>  	char *tmp_file;
>>
>> -	trace2_region_enter("midx", "write_midx_reverse_index", the_repository);
>> +	trace2_region_enter("midx", "write_midx_reverse_index", ctx->repo);
>>
>> -	strbuf_addf(&buf, "%s-%s.rev", midx_name, hash_to_hex(midx_hash));
>> +	strbuf_addf(&buf, "%s-%s.rev", midx_name, hash_to_hex_algop(midx_hash,
>> +								    ctx->repo->hash_algo));
>>
>>  	tmp_file = write_rev_file_order(NULL, ctx->pack_order, ctx->entries_nr,
>>  					midx_hash, WRITE_REV);
>
> I think it would be nice to split up the changes that start to pass down
> a repo via the context and those that start to actually call a different
> function like we have here. Otherwise it's hard to spot the callsites
> where one actually has to think about what's happening.
>
> Patrick

Fair enough, I've split it into 3 separate commits for the next
version:

- To pass down `the_repository` to non-static functions
- To use `revs-repo` in `read_refs_snapshot`
- To add repository to `write_midx_context`

This should make it easier for reviewers. Thanks

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 690 bytes --]

^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH 4/8] midx: cleanup internal usage of `the_repository` and `the_hash_algo`
  2024-11-18  8:06   ` Patrick Steinhardt
@ 2024-11-18 16:16     ` karthik nayak
  0 siblings, 0 replies; 70+ messages in thread
From: karthik nayak @ 2024-11-18 16:16 UTC (permalink / raw)
  To: Patrick Steinhardt; +Cc: git, me, peff

[-- Attachment #1: Type: text/plain, Size: 579 bytes --]

Patrick Steinhardt <ps@pks.im> writes:

> On Fri, Nov 15, 2024 at 02:42:17PM +0100, Karthik Nayak wrote:
>> In the `midx.c` file, there are multiple usages of `the_repository` and
>> `the_hash_algo` within static functions of the file. Some of the usages
>> can be simply swapped out with the available `repository` struct
>> available. While some of them can be swapped out by passing the
>
> The second "available" should probably be "parameter" or "variable" or
> something like this? Or it just needs to be dropped altogether.
>
> Patrick

I think it is best dropped. Thanks

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 690 bytes --]

^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH 6/8] midx: pass down `hash_algo` to `get_midx_filename[_ext]`
  2024-11-16 13:16   ` shejialuo
@ 2024-11-18 16:25     ` karthik nayak
  2024-11-19 12:12       ` shejialuo
  0 siblings, 1 reply; 70+ messages in thread
From: karthik nayak @ 2024-11-18 16:25 UTC (permalink / raw)
  To: shejialuo; +Cc: git, me, peff

[-- Attachment #1: Type: text/plain, Size: 1861 bytes --]

shejialuo <shejialuo@gmail.com> writes:

> On Fri, Nov 15, 2024 at 02:42:19PM +0100, Karthik Nayak wrote:
>
> [snip]
>
>> 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);
>
> I don't think it's a good idea to put "hash_algo" in the first argument,
> we should put it at the last to align with the code style where we use
> "git_hash_algo".
>

Could you elaborate on why you think it is not a good idea?

I've mostly done this to stay consistent, because I see `struct
repository *repo` being passed as the first variable in our code base.

Roughly:

    $ grep -Iir "struct repository \*r" --include=\*.h | wc -l
    524

    $ grep -Iir "(struct repository \*r" --include=\*.h | wc -l
    327

Since `hash_algo` is similar, I thought it would be nicer to be
consistent.

>>  void get_midx_chain_dirname(struct strbuf *buf, const char *object_dir);
>>  void get_midx_chain_filename(struct strbuf *buf, const char *object_dir);

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 690 bytes --]

^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH 6/8] midx: pass down `hash_algo` to `get_midx_filename[_ext]`
  2024-11-18 16:25     ` karthik nayak
@ 2024-11-19 12:12       ` shejialuo
  0 siblings, 0 replies; 70+ messages in thread
From: shejialuo @ 2024-11-19 12:12 UTC (permalink / raw)
  To: karthik nayak; +Cc: git, me, peff

On Mon, Nov 18, 2024 at 10:25:09AM -0600, karthik nayak wrote:
> shejialuo <shejialuo@gmail.com> writes:
> 
> > On Fri, Nov 15, 2024 at 02:42:19PM +0100, Karthik Nayak wrote:
> >
> > [snip]
> >
> >> 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);
> >
> > I don't think it's a good idea to put "hash_algo" in the first argument,
> > we should put it at the last to align with the code style where we use
> > "git_hash_algo".
> >
> 
> Could you elaborate on why you think it is not a good idea?
> 
> I've mostly done this to stay consistent, because I see `struct
> repository *repo` being passed as the first variable in our code base.
> 
> Roughly:
> 
>     $ grep -Iir "struct repository \*r" --include=\*.h | wc -l
>     524
> 
>     $ grep -Iir "(struct repository \*r" --include=\*.h | wc -l
>     327
> 
> Since `hash_algo` is similar, I thought it would be nicer to be
> consistent.
> 

I will elaborate on this. The reason why I think this is not a good idea
comes from two aspects:

    1. I have thought that we will always put "struct git_hash_algo"
       to the end of the function definition. However, when I carefully
       inspect the code today, we could put it everywhere. So, I wrongly
       made above statement.
    2. Another aspect is that I think "struct git_hash_algo" is not the
       most important parameter for these functions. When the caller
       sees this function name "get_midx_filename_ext" without any
       knowledge, passing the "hash_algo" firstly is a little wired.

However, as 1 shows, we may not care about which position we put this
parameter into. So, I agree with you that we could just align with the
"struct repository *".

Thanks.

> >>  void get_midx_chain_dirname(struct strbuf *buf, const char *object_dir);
> >>  void get_midx_chain_filename(struct strbuf *buf, const char *object_dir);


^ permalink raw reply	[flat|nested] 70+ messages in thread

* [PATCH v2 00/10] Change midx.c and midx-write.c to not use global variables
  2024-11-15 13:42 [PATCH 0/8] Change midx.c and midx-write.c to not use global variables Karthik Nayak
                   ` (8 preceding siblings ...)
  2024-11-15 14:13 ` [PATCH 0/8] Change midx.c and midx-write.c to not use global variables karthik nayak
@ 2024-11-19 15:36 ` Karthik Nayak
  2024-11-19 15:36   ` [PATCH v2 01/10] builtin: pass repository to sub commands Karthik Nayak
                     ` (12 more replies)
  9 siblings, 13 replies; 70+ messages in thread
From: Karthik Nayak @ 2024-11-19 15:36 UTC (permalink / raw)
  To: git; +Cc: Karthik Nayak, ps, shejialuo

Similar to the earlier patch series on cleaning up packfile.c and
removing usage of global variables [1], we change the midx.c and
midx-write.c files to no longer use global variables.

This is done by the following:
  - Usage of repository variable already available in existing structs.
  - Passing down repository variable from other subsystems.
  - Modifying all subcommands to obtain repository variable from the
  command in `builtins/` and passing down the variable from there.

The biggest change is in the first commit, wherein we modify all
subcommands to add the repository variable. Since the subcommand
definition are not often changed, it shouldn't cause too many conflicts
with in flight topics.

Since the `packfile.c` cleanup is still in flight, this series is based
on top of master: b31fb630c0 (Merge https://github.com/j6t/git-gui,
2024-11-11) with those patches merged in. This applies cleanly on top 
of next, but conflicts with `bf/set-head-symref` in seen, the conflict
is mostly straight forward. I'll merge the topic in if it is merged into
next soon.

[1]: https://lore.kernel.org/git/cover.1729504640.git.karthik.188@gmail.com/

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
Changes in v2:
- Split commit modifying static functions in `midx-write.c` to multiple
commits, which makes it easier to review.
- Remove usage of `the_repository` in `test-parse-options.c` and use
NULL instead.
- Fix the commit messages to be imperative.
- Fix error in commit sign off.
- Link to v1: https://lore.kernel.org/r/20241115-374-refactor-midx-c-and-midx-write-c-to-not-depend-on-global-state-v1-0-761f8a2c7775@gmail.com

---
Karthik Nayak (10):
      builtin: pass repository to sub commands
      midx-write: pass down repository to static functions
      midx-write: use `revs->repo` inside `read_refs_snapshot`
      write-midx: add repository field to `write_midx_context`
      midx-write: pass down repository to `write_midx_file[_only]`
      midx: cleanup internal usage of `the_repository` and `the_hash_algo`
      midx: pass `repository` to `load_multi_pack_index`
      midx: pass down `hash_algo` to `get_midx_filename[_ext]`
      midx: pass down `hash_algo` to `get_split_midx_filename_ext`
      midx: inline the `MIDX_MIN_SIZE` definition

 builtin/bisect.c              |  32 +++++++----
 builtin/bundle.c              |  16 ++++--
 builtin/commit-graph.c        |  10 ++--
 builtin/config.c              |  25 +++++---
 builtin/gc.c                  |  21 ++++---
 builtin/hook.c                |   7 ++-
 builtin/multi-pack-index.c    |  20 ++++---
 builtin/notes.c               |  36 +++++++-----
 builtin/reflog.c              |  17 ++++--
 builtin/refs.c                |  10 ++--
 builtin/remote.c              |  34 +++++++----
 builtin/repack.c              |   2 +-
 builtin/sparse-checkout.c     |  25 +++++---
 builtin/stash.c               |  39 ++++++++-----
 builtin/submodule--helper.c   |  46 +++++++++------
 builtin/worktree.c            |  28 +++++----
 midx-write.c                  | 130 ++++++++++++++++++++++--------------------
 midx.c                        |  88 +++++++++++++++-------------
 midx.h                        |  24 ++++----
 pack-bitmap.c                 |   6 +-
 pack-revindex.c               |   2 +-
 parse-options.h               |   4 +-
 t/helper/test-parse-options.c |   8 ++-
 t/helper/test-read-midx.c     |   8 +--
 24 files changed, 378 insertions(+), 260 deletions(-)
---

Range-diff versus v1:

 1:  277db8a45a <  -:  ---------- packfile: add repository to struct `packed_git`
 2:  10ff177a40 <  -:  ---------- packfile: use `repository` from `packed_git` directly
 3:  c531a865d7 <  -:  ---------- packfile: pass `repository` to static function in the file
 4:  36cda6c7e9 <  -:  ---------- packfile: pass down repository to `odb_pack_name`
 5:  dbe2d93cbe <  -:  ---------- packfile: pass down repository to `has_object[_kept]_pack`
 6:  5b030a81c4 <  -:  ---------- packfile: pass down repository to `for_each_packed_object`
 7:  b4d6dda97c <  -:  ---------- config: make `delta_base_cache_limit` a non-global variable
 8:  bf13b948d4 <  -:  ---------- config: make `packed_git_(limit|window_size)` non-global variables
 9:  3ec55c737b <  -:  ---------- midx: add repository to `multi_pack_index` struct
10:  4df5b4fb30 !  1:  a423d7785c builtin: pass repository to sub commands
    @@ Commit message
         builtin: pass repository to sub commands
     
         In 9b1cb5070f (builtin: add a repository parameter for builtin
    -    functions, 2024-09-13) we passed down the repository to all builtin
    -    commands. This allowed us to pass down the repository to lower layers
    +    functions, 2024-09-13) the repository was passed down to all builtin
    +    commands. This allowed the repository to be passed down to lower layers
         without depending on the global `the_repository` variable.
     
    -    To continue this work, we also pass down the repository parameter from
    -    the command to sub-commands. In the upcoming commit, we'll utilize this
    -    to remove global variable usage in `midx-write.c`.
    +    Continue this work by also passing down the repository parameter from
    +    the command to sub-commands. In the upcoming commit, utilize this to
    +    remove global variable usage in `midx-write.c`.
     
         Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
     
    @@ parse-options.h: typedef enum parse_opt_result parse_opt_ll_cb(struct parse_opt_
       * `type`::
     
      ## t/helper/test-parse-options.c ##
    -@@
    -+#define USE_THE_REPOSITORY_VARIABLE
    -+
    - #include "test-tool.h"
    - #include "parse-options.h"
    - #include "strbuf.h"
    - #include "string-list.h"
    - #include "trace2.h"
    -+#include "repository.h"
    - 
    - static int boolean = 0;
    - static int integer = 0;
     @@ t/helper/test-parse-options.c: int cmd__parse_options_flags(int argc, const char **argv)
      	return parse_options_flags__cmd(argc, argv, test_flags);
      }
    @@ t/helper/test-parse-options.c: static int parse_subcommand__cmd(int argc, const
      	printf("opt: %d\n", opt);
      
     -	return fn(argc, argv, NULL);
    -+	return fn(argc, argv, NULL, the_repository);
    ++	return fn(argc, argv, NULL, NULL);
      }
      
      int cmd__parse_subcommand(int argc, const char **argv)
11:  3cfab22dc1 !  2:  3f39af60b4 midx-write: add repository field to `write_midx_context`
    @@ Metadata
     Author: Karthik Nayak <karthik.188@gmail.com>
     
      ## Commit message ##
    -    midx-write: add repository field to `write_midx_context`
    +    midx-write: pass down repository to static functions
     
    -    The struct `write_midx_context` is used to pass context for creating
    -    MIDX files. Adding the repository field here ensures that most functions
    -    within `midx-write.c` have access to the field and can use that instead
    -    of the global `repository` variable. This involves passing the
    -    `repository` field to `write_midx_internal`. To do this, we add
    -    `the_repository` usage to two non-static functions, which we'll remove
    -    in upcoming commits.
    +    In 'midx-write.c' there are a lot of static functions which use global
    +    variables `the_repository` or `the_hash_algo`. In a follow up commit,
    +    the repository variable will be added to `write_midx_context`, which
    +    some of the functions can use. But for functions which do not have
    +    access to this struct, pass down the required information from
    +    non-static functions `write_midx_file` and `write_midx_file_only`.
     
    -    With this, modify the static functions in `midx-write.c` to not use
    -    global variables. This means, either we use existing alternatives (like
    -    `repository` struct), or we pass down required fields from other
    -    functions.
    +    This ensures that the usage of global variables is limited to these
    +    non-static functions, which will be cleaned up in a follow up commits.
     
    -    Signed-off-by: default avatarKarthik Nayak <karthik.188@gmail.com>
    +    Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
     
      ## midx-write.c ##
     @@ midx-write.c: extern void clear_incremental_midx_files_ext(const char *object_dir,
    @@ midx-write.c: extern void clear_incremental_midx_files_ext(const char *object_di
      	hashwrite_u8(f, num_chunks);
      	hashwrite_u8(f, 0); /* unused */
      	hashwrite_be32(f, num_packs);
    -@@ midx-write.c: struct write_midx_context {
    - 	uint32_t num_multi_pack_indexes_before;
    - 
    - 	struct string_list *to_include;
    -+
    -+	struct repository *repo;
    - };
    - 
    - static int should_include_pack(const struct write_midx_context *ctx,
    -@@ midx-write.c: static void add_pack_to_midx(const char *full_path, size_t full_path_len,
    - 			return;
    - 
    - 		ALLOC_GROW(ctx->info, ctx->nr + 1, ctx->alloc);
    --		p = add_packed_git(the_repository, full_path, full_path_len, 0);
    -+		p = add_packed_git(ctx->repo, full_path, full_path_len, 0);
    - 		if (!p) {
    - 			warning(_("failed to add packfile '%s'"),
    - 				full_path);
    -@@ midx-write.c: static int write_midx_oid_lookup(struct hashfile *f,
    - 				 void *data)
    - {
    - 	struct write_midx_context *ctx = data;
    --	unsigned char hash_len = the_hash_algo->rawsz;
    -+	unsigned char hash_len = ctx->repo->hash_algo->rawsz;
    - 	struct pack_midx_entry *list = ctx->entries;
    - 	uint32_t i;
    - 
    -@@ midx-write.c: static uint32_t *midx_pack_order(struct write_midx_context *ctx)
    - 	uint32_t *pack_order, base_objects = 0;
    - 	uint32_t i;
    - 
    --	trace2_region_enter("midx", "midx_pack_order", the_repository);
    -+	trace2_region_enter("midx", "midx_pack_order", ctx->repo);
    - 
    - 	if (ctx->incremental && ctx->base_midx)
    - 		base_objects = ctx->base_midx->num_objects +
    -@@ midx-write.c: static uint32_t *midx_pack_order(struct write_midx_context *ctx)
    - 	}
    - 	free(data);
    - 
    --	trace2_region_leave("midx", "midx_pack_order", the_repository);
    -+	trace2_region_leave("midx", "midx_pack_order", ctx->repo);
    - 
    - 	return pack_order;
    - }
    -@@ midx-write.c: static void write_midx_reverse_index(char *midx_name, unsigned char *midx_hash,
    - 	struct strbuf buf = STRBUF_INIT;
    - 	char *tmp_file;
    - 
    --	trace2_region_enter("midx", "write_midx_reverse_index", the_repository);
    -+	trace2_region_enter("midx", "write_midx_reverse_index", ctx->repo);
    - 
    --	strbuf_addf(&buf, "%s-%s.rev", midx_name, hash_to_hex(midx_hash));
    -+	strbuf_addf(&buf, "%s-%s.rev", midx_name, hash_to_hex_algop(midx_hash,
    -+								    ctx->repo->hash_algo));
    - 
    - 	tmp_file = write_rev_file_order(NULL, ctx->pack_order, ctx->entries_nr,
    - 					midx_hash, WRITE_REV);
    -@@ midx-write.c: static void write_midx_reverse_index(char *midx_name, unsigned char *midx_hash,
    - 	strbuf_release(&buf);
    - 	free(tmp_file);
    - 
    --	trace2_region_leave("midx", "write_midx_reverse_index", the_repository);
    -+	trace2_region_leave("midx", "write_midx_reverse_index", ctx->repo);
    - }
    - 
    - static void prepare_midx_packing_data(struct packing_data *pdata,
    -@@ midx-write.c: static void prepare_midx_packing_data(struct packing_data *pdata,
    - {
    - 	uint32_t i;
    - 
    --	trace2_region_enter("midx", "prepare_midx_packing_data", the_repository);
    -+	trace2_region_enter("midx", "prepare_midx_packing_data", ctx->repo);
    - 
    - 	memset(pdata, 0, sizeof(struct packing_data));
    --	prepare_packing_data(the_repository, pdata);
    -+	prepare_packing_data(ctx->repo, pdata);
    - 
    - 	for (i = 0; i < ctx->entries_nr; i++) {
    - 		uint32_t pos = ctx->pack_order[i];
    -@@ midx-write.c: static void prepare_midx_packing_data(struct packing_data *pdata,
    - 			       ctx->info[ctx->pack_perm[from->pack_int_id]].p);
    - 	}
    - 
    --	trace2_region_leave("midx", "prepare_midx_packing_data", the_repository);
    -+	trace2_region_leave("midx", "prepare_midx_packing_data", ctx->repo);
    - }
    - 
    - static int add_ref_to_pending(const char *refname, const char *referent UNUSED,
     @@ midx-write.c: static int add_ref_to_pending(const char *refname, const char *referent UNUSED,
      		return 0;
      	}
    @@ midx-write.c: static int add_ref_to_pending(const char *refname, const char *ref
      		oid = &peeled;
      
      	object = parse_object_or_die(oid, refname);
    -@@ midx-write.c: static int read_refs_snapshot(const char *refs_snapshot,
    - 			hex = &buf.buf[1];
    - 		}
    - 
    --		if (parse_oid_hex(hex, &oid, &end) < 0)
    -+		if (parse_oid_hex_algop(hex, &oid, &end, revs->repo->hash_algo) < 0)
    - 			die(_("could not parse line: %s"), buf.buf);
    - 		if (*end)
    - 			die(_("malformed line: %s"), buf.buf);
    -@@ midx-write.c: static struct commit **find_commits_for_midx_bitmap(uint32_t *indexed_commits_nr
    - 	struct rev_info revs;
    - 	struct bitmap_commit_cb cb = {0};
    - 
    --	trace2_region_enter("midx", "find_commits_for_midx_bitmap",
    --			    the_repository);
    -+	trace2_region_enter("midx", "find_commits_for_midx_bitmap", ctx->repo);
    - 
    - 	cb.ctx = ctx;
    - 
    --	repo_init_revisions(the_repository, &revs, NULL);
    -+	repo_init_revisions(ctx->repo, &revs, NULL);
    - 	if (refs_snapshot) {
    - 		read_refs_snapshot(refs_snapshot, &revs);
    - 	} else {
    - 		setup_revisions(0, NULL, &revs, NULL);
    --		refs_for_each_ref(get_main_ref_store(the_repository),
    -+		refs_for_each_ref(get_main_ref_store(ctx->repo),
    - 				  add_ref_to_pending, &revs);
    - 	}
    - 
     @@ midx-write.c: static struct commit **find_commits_for_midx_bitmap(uint32_t *indexed_commits_nr
    - 
    - 	release_revisions(&revs);
    - 
    --	trace2_region_leave("midx", "find_commits_for_midx_bitmap",
    --			    the_repository);
    -+	trace2_region_leave("midx", "find_commits_for_midx_bitmap", ctx->repo);
    - 
      	return cb.commits;
      }
      
    @@ midx-write.c: static int write_midx_bitmap(const char *midx_name,
      
      	return ret;
      }
    -@@ midx-write.c: static int fill_packs_from_midx(struct write_midx_context *ctx,
    - 			 */
    - 			if (flags & MIDX_WRITE_REV_INDEX ||
    - 			    preferred_pack_name) {
    --				if (prepare_midx_pack(the_repository, m,
    -+				if (prepare_midx_pack(ctx->repo, m,
    - 						      m->num_packs_in_base + i)) {
    - 					error(_("could not load pack"));
    - 					return 1;
     @@ midx-write.c: static void clear_midx_files(const char *object_dir,
      	strbuf_release(&buf);
      }
    @@ midx-write.c: static int write_midx_internal(const char *object_dir,
     -	trace2_region_enter("midx", "write_midx_internal", the_repository);
     +	trace2_region_enter("midx", "write_midx_internal", r);
     +
    -+	ctx.repo = r;
      
      	ctx.incremental = !!(flags & MIDX_WRITE_INCREMENTAL);
      	if (ctx.incremental && (flags & MIDX_WRITE_BITMAP))
    @@ midx-write.c: static int write_midx_internal(const char *object_dir,
      	}
      
      	if (ctx.m || ctx.base_midx)
    --		close_object_store(the_repository->objects);
    -+		close_object_store(ctx.repo->objects);
    - 
    - 	if (commit_lock_file(&lk) < 0)
    - 		die_errno(_("could not write multi-pack-index"));
     @@ midx-write.c: static int write_midx_internal(const char *object_dir,
      	}
      	strbuf_release(&midx_name);
 -:  ---------- >  3:  774eea311a midx-write: use `revs->repo` inside `read_refs_snapshot`
 -:  ---------- >  4:  9ddfbdc0f8 write-midx: add repository field to `write_midx_context`
12:  5cdc724a0c !  5:  d25ea08541 midx-write: pass down repository to `write_midx_file[_only]`
    @@ Metadata
      ## Commit message ##
         midx-write: pass down repository to `write_midx_file[_only]`
     
    -    In the previous commit, we passed the repository field to all
    -    subcommands in the `builtin/` directory. We utilize this to pass the
    +    In a previous commit, we passed the repository field to all
    +    subcommands in the `builtin/` directory. Utilize this to pass the
         repository field down to the `write_midx_file[_only]` functions to
         remove the usage of `the_repository` global variables.
     
    -    With this, we remove all usage of global variables in `midx-write.c` and
    -    so we can remove the `USE_THE_REPOSITORY_VARIABLE` guard from the file.
    +    With this, all usage of global variables in `midx-write.c` is removed,
    +    hence, remove the `USE_THE_REPOSITORY_VARIABLE` guard from the file.
     
         Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
     
13:  c6964becef !  6:  ca123e8c73 midx: cleanup internal usage of `the_repository` and `the_hash_algo`
    @@ Commit message
     
         In the `midx.c` file, there are multiple usages of `the_repository` and
         `the_hash_algo` within static functions of the file. Some of the usages
    -    can be simply swapped out with the available `repository` struct
    -    available. While some of them can be swapped out by passing the
    -    repository to the required functions.
    +    can be simply swapped out with the available `repository` struct. While
    +    some of them can be swapped out by passing the repository to the
    +    required functions.
     
         This leaves out only some other usages of `the_repository` and
         `the_hash_algo` in the file in non-static functions, which we'll tackle
14:  adcb6f0c6c !  7:  7e941aa0ba midx: pass `repository` to `load_multi_pack_index`
    @@ Commit message
         midx: pass `repository` to `load_multi_pack_index`
     
         The `load_multi_pack_index` function in midx uses `the_repository`
    -    variable to access the `repository` struct. Let's modify the function
    -    and its callee's to send the `repository` field.
    +    variable to access the `repository` struct. Modify the function and its
    +    callee's to send the `repository` field.
     
         This moves usage of `the_repository` to the `test-read-midx.c` file.
    -    While that is not optimal, it is okay, since we'll slowly move the usage
    -    of `the_repository` up the layers and eventually remove it entirely.
    +    While that is not optimal, it is okay, since the upcoming commits will
    +    slowly move the usage of `the_repository` up the layers and remove it
    +    eventually.
     
         Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
     
15:  cea8e6c252 !  8:  3e6dd4e146 midx: pass down `hash_algo` to `get_midx_filename[_ext]`
    @@ Commit message
         midx: pass down `hash_algo` to `get_midx_filename[_ext]`
     
         The function `get_midx_filename_ext` uses `hash_to_hex` which internally
    -    uses the global variable `the_repository`. To remove this dependency, we
    +    uses the global variable `the_repository`. To remove this dependency,
         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.
    +    `get_midx_filename_ext`. This adds `the_repository` variable usage to
    +    `midx-write.c`, which will be resolved in a future commit.
     
         Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
     
16:  66dd34c862 !  9:  704a5a4392 midx: pass down `hash_algo` to `get_split_midx_filename_ext`
    @@ Commit message
         midx: pass down `hash_algo` to `get_split_midx_filename_ext`
     
         Similar to the previous commit, pass down `hash_algo` to
    -    `get_split_midx_filename_ext` so we can use `hash_to_hex_algop` and not
    -    rely on the `the_repository` global variable.
    +    `get_split_midx_filename_ext` and use `hash_to_hex_algop`.
     
         Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
     
17:  1f0602aec6 ! 10:  0a4a68bcff midx: inline the `MIDX_MIN_SIZE` definition
    @@ Commit message
     
         The `MIDX_MIN_SIZE` definition is used to check the midx_size in
         `local_multi_pack_index_one`. This definitions relies on the
    -    `the_hash_algo` global variable. Let's inline this and remove the global
    +    `the_hash_algo` global variable. Inline this and remove the global
         variable usage.
     
    -    With this, we can remove `USE_THE_REPOSITORY_VARIABLE` usage from
    -    `midx.c`.
    +    With this, remove `USE_THE_REPOSITORY_VARIABLE` usage from `midx.c`.
     
         Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
     


--- 

base-commit: b31fb630c0fc6869a33ed717163e8a1210460d94
change-id: 20241111-374-refactor-midx-c-and-midx-write-c-to-not-depend-on-global-state-a88498c2590f

Thanks
- Karthik


^ permalink raw reply	[flat|nested] 70+ messages in thread

* [PATCH v2 01/10] builtin: pass repository to sub commands
  2024-11-19 15:36 ` [PATCH v2 00/10] " Karthik Nayak
@ 2024-11-19 15:36   ` Karthik Nayak
  2024-11-19 15:36   ` [PATCH v2 02/10] midx-write: pass down repository to static functions Karthik Nayak
                     ` (11 subsequent siblings)
  12 siblings, 0 replies; 70+ messages in thread
From: Karthik Nayak @ 2024-11-19 15:36 UTC (permalink / raw)
  To: git; +Cc: Karthik Nayak, ps, shejialuo

In 9b1cb5070f (builtin: add a repository parameter for builtin
functions, 2024-09-13) the repository was passed down to all builtin
commands. This allowed the repository to be passed down to lower layers
without depending on the global `the_repository` variable.

Continue this work by also passing down the repository parameter from
the command to sub-commands. In the upcoming commit, utilize this to
remove global variable usage in `midx-write.c`.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 builtin/bisect.c              | 32 +++++++++++++++++++-----------
 builtin/bundle.c              | 16 +++++++++------
 builtin/commit-graph.c        | 10 ++++++----
 builtin/config.c              | 25 ++++++++++++++---------
 builtin/gc.c                  | 21 ++++++++++++--------
 builtin/hook.c                |  7 ++++---
 builtin/multi-pack-index.c    | 16 +++++++++------
 builtin/notes.c               | 36 +++++++++++++++++++++------------
 builtin/reflog.c              | 17 ++++++++++------
 builtin/refs.c                | 10 ++++++----
 builtin/remote.c              | 34 +++++++++++++++++++++-----------
 builtin/sparse-checkout.c     | 25 ++++++++++++++---------
 builtin/stash.c               | 39 +++++++++++++++++++++++-------------
 builtin/submodule--helper.c   | 46 ++++++++++++++++++++++++++++---------------
 builtin/worktree.c            | 28 ++++++++++++++++----------
 parse-options.h               |  4 +++-
 t/helper/test-parse-options.c |  8 +++++---
 17 files changed, 239 insertions(+), 135 deletions(-)

diff --git a/builtin/bisect.c b/builtin/bisect.c
index 21d17a6c1a83e51fb82b53703b95d89bd9028830..8166d4abf532fbc95aaac71198e9722684ccf7f5 100644
--- a/builtin/bisect.c
+++ b/builtin/bisect.c
@@ -1312,7 +1312,8 @@ static int bisect_run(struct bisect_terms *terms, int argc, const char **argv)
 	return res;
 }
 
-static int cmd_bisect__reset(int argc, const char **argv, const char *prefix UNUSED)
+static int cmd_bisect__reset(int argc, const char **argv, const char *prefix UNUSED,
+			     struct repository *repo UNUSED)
 {
 	if (argc > 1)
 		return error(_("'%s' requires either no argument or a commit"),
@@ -1320,7 +1321,8 @@ static int cmd_bisect__reset(int argc, const char **argv, const char *prefix UNU
 	return bisect_reset(argc ? argv[0] : NULL);
 }
 
-static int cmd_bisect__terms(int argc, const char **argv, const char *prefix UNUSED)
+static int cmd_bisect__terms(int argc, const char **argv, const char *prefix UNUSED,
+			     struct repository *repo UNUSED)
 {
 	int res;
 	struct bisect_terms terms = { 0 };
@@ -1333,7 +1335,8 @@ static int cmd_bisect__terms(int argc, const char **argv, const char *prefix UNU
 	return res;
 }
 
-static int cmd_bisect__start(int argc, const char **argv, const char *prefix UNUSED)
+static int cmd_bisect__start(int argc, const char **argv, const char *prefix UNUSED,
+			     struct repository *repo UNUSED)
 {
 	int res;
 	struct bisect_terms terms = { 0 };
@@ -1344,7 +1347,8 @@ static int cmd_bisect__start(int argc, const char **argv, const char *prefix UNU
 	return res;
 }
 
-static int cmd_bisect__next(int argc, const char **argv UNUSED, const char *prefix)
+static int cmd_bisect__next(int argc, const char **argv UNUSED, const char *prefix,
+			    struct repository *repo UNUSED)
 {
 	int res;
 	struct bisect_terms terms = { 0 };
@@ -1358,12 +1362,15 @@ static int cmd_bisect__next(int argc, const char **argv UNUSED, const char *pref
 	return res;
 }
 
-static int cmd_bisect__log(int argc UNUSED, const char **argv UNUSED, const char *prefix UNUSED)
+static int cmd_bisect__log(int argc UNUSED, const char **argv UNUSED,
+			   const char *prefix UNUSED,
+			   struct repository *repo UNUSED)
 {
 	return bisect_log();
 }
 
-static int cmd_bisect__replay(int argc, const char **argv, const char *prefix UNUSED)
+static int cmd_bisect__replay(int argc, const char **argv, const char *prefix UNUSED,
+			      struct repository *repo UNUSED)
 {
 	int res;
 	struct bisect_terms terms = { 0 };
@@ -1376,7 +1383,8 @@ static int cmd_bisect__replay(int argc, const char **argv, const char *prefix UN
 	return res;
 }
 
-static int cmd_bisect__skip(int argc, const char **argv, const char *prefix UNUSED)
+static int cmd_bisect__skip(int argc, const char **argv, const char *prefix UNUSED,
+			    struct repository *repo UNUSED)
 {
 	int res;
 	struct bisect_terms terms = { 0 };
@@ -1388,7 +1396,8 @@ static int cmd_bisect__skip(int argc, const char **argv, const char *prefix UNUS
 	return res;
 }
 
-static int cmd_bisect__visualize(int argc, const char **argv, const char *prefix UNUSED)
+static int cmd_bisect__visualize(int argc, const char **argv, const char *prefix UNUSED,
+				 struct repository *repo UNUSED)
 {
 	int res;
 	struct bisect_terms terms = { 0 };
@@ -1399,7 +1408,8 @@ static int cmd_bisect__visualize(int argc, const char **argv, const char *prefix
 	return res;
 }
 
-static int cmd_bisect__run(int argc, const char **argv, const char *prefix UNUSED)
+static int cmd_bisect__run(int argc, const char **argv, const char *prefix UNUSED,
+			   struct repository *repo UNUSED)
 {
 	int res;
 	struct bisect_terms terms = { 0 };
@@ -1415,7 +1425,7 @@ static int cmd_bisect__run(int argc, const char **argv, const char *prefix UNUSE
 int cmd_bisect(int argc,
 	       const char **argv,
 	       const char *prefix,
-	       struct repository *repo UNUSED)
+	       struct repository *repo)
 {
 	int res = 0;
 	parse_opt_subcommand_fn *fn = NULL;
@@ -1451,7 +1461,7 @@ int cmd_bisect(int argc,
 	} else {
 		argc--;
 		argv++;
-		res = fn(argc, argv, prefix);
+		res = fn(argc, argv, prefix, repo);
 	}
 
 	return is_bisect_success(res) ? 0 : -res;
diff --git a/builtin/bundle.c b/builtin/bundle.c
index 127518c2a8d3c4ec0bde62f1932e964ce9bcf66f..3f14754197c847e7a4e98b607c1a24df2b053daf 100644
--- a/builtin/bundle.c
+++ b/builtin/bundle.c
@@ -67,7 +67,8 @@ static int parse_options_cmd_bundle(int argc,
 	return argc;
 }
 
-static int cmd_bundle_create(int argc, const char **argv, const char *prefix) {
+static int cmd_bundle_create(int argc, const char **argv, const char *prefix,
+			     struct repository *repo UNUSED) {
 	struct strvec pack_opts = STRVEC_INIT;
 	int version = -1;
 	int ret;
@@ -123,7 +124,8 @@ static int open_bundle(const char *path, struct bundle_header *header,
 	return read_bundle_header(path, header);
 }
 
-static int cmd_bundle_verify(int argc, const char **argv, const char *prefix) {
+static int cmd_bundle_verify(int argc, const char **argv, const char *prefix,
+			     struct repository *repo UNUSED) {
 	struct bundle_header header = BUNDLE_HEADER_INIT;
 	int bundle_fd = -1;
 	int quiet = 0;
@@ -164,7 +166,8 @@ static int cmd_bundle_verify(int argc, const char **argv, const char *prefix) {
 	return ret;
 }
 
-static int cmd_bundle_list_heads(int argc, const char **argv, const char *prefix) {
+static int cmd_bundle_list_heads(int argc, const char **argv, const char *prefix,
+				 struct repository *repo UNUSED) {
 	struct bundle_header header = BUNDLE_HEADER_INIT;
 	int bundle_fd = -1;
 	int ret;
@@ -189,7 +192,8 @@ static int cmd_bundle_list_heads(int argc, const char **argv, const char *prefix
 	return ret;
 }
 
-static int cmd_bundle_unbundle(int argc, const char **argv, const char *prefix) {
+static int cmd_bundle_unbundle(int argc, const char **argv, const char *prefix,
+			       struct repository *repo UNUSED) {
 	struct bundle_header header = BUNDLE_HEADER_INIT;
 	int bundle_fd = -1;
 	int ret;
@@ -231,7 +235,7 @@ static int cmd_bundle_unbundle(int argc, const char **argv, const char *prefix)
 int cmd_bundle(int argc,
 	       const char **argv,
 	       const char *prefix,
-	       struct repository *repo UNUSED)
+	       struct repository *repo)
 {
 	parse_opt_subcommand_fn *fn = NULL;
 	struct option options[] = {
@@ -247,5 +251,5 @@ int cmd_bundle(int argc,
 
 	packet_trace_identity("bundle");
 
-	return !!fn(argc, argv, prefix);
+	return !!fn(argc, argv, prefix, repo);
 }
diff --git a/builtin/commit-graph.c b/builtin/commit-graph.c
index 7c991db6eb48ad6e935727a079abac02bb358f8a..bd70d052e706b6e34b5aaaceef158c63ea4863d5 100644
--- a/builtin/commit-graph.c
+++ b/builtin/commit-graph.c
@@ -62,7 +62,8 @@ static struct option *add_common_options(struct option *to)
 	return parse_options_concat(common_opts, to);
 }
 
-static int graph_verify(int argc, const char **argv, const char *prefix)
+static int graph_verify(int argc, const char **argv, const char *prefix,
+			struct repository *repo UNUSED)
 {
 	struct commit_graph *graph = NULL;
 	struct object_directory *odb = NULL;
@@ -214,7 +215,8 @@ static int git_commit_graph_write_config(const char *var, const char *value,
 	return 0;
 }
 
-static int graph_write(int argc, const char **argv, const char *prefix)
+static int graph_write(int argc, const char **argv, const char *prefix,
+		       struct repository *repo UNUSED)
 {
 	struct string_list pack_indexes = STRING_LIST_INIT_DUP;
 	struct strbuf buf = STRBUF_INIT;
@@ -333,7 +335,7 @@ static int graph_write(int argc, const char **argv, const char *prefix)
 int cmd_commit_graph(int argc,
 		     const char **argv,
 		     const char *prefix,
-		     struct repository *repo UNUSED)
+		     struct repository *repo)
 {
 	parse_opt_subcommand_fn *fn = NULL;
 	struct option builtin_commit_graph_options[] = {
@@ -352,5 +354,5 @@ int cmd_commit_graph(int argc,
 			     builtin_commit_graph_usage, 0);
 	FREE_AND_NULL(options);
 
-	return fn(argc, argv, prefix);
+	return fn(argc, argv, prefix, repo);
 }
diff --git a/builtin/config.c b/builtin/config.c
index cba702210815b716a5c6c93ebb69d8c485901e43..16e6e3055598f1355714dc6de458b662870214c0 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -826,7 +826,8 @@ static void display_options_init(struct config_display_options *opts)
 	}
 }
 
-static int cmd_config_list(int argc, const char **argv, const char *prefix)
+static int cmd_config_list(int argc, const char **argv, const char *prefix,
+			   struct repository *repo UNUSED)
 {
 	struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
 	struct config_display_options display_opts = CONFIG_DISPLAY_OPTIONS_INIT;
@@ -861,7 +862,8 @@ static int cmd_config_list(int argc, const char **argv, const char *prefix)
 	return 0;
 }
 
-static int cmd_config_get(int argc, const char **argv, const char *prefix)
+static int cmd_config_get(int argc, const char **argv, const char *prefix,
+			  struct repository *repo UNUSED)
 {
 	struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
 	struct config_display_options display_opts = CONFIG_DISPLAY_OPTIONS_INIT;
@@ -915,7 +917,8 @@ static int cmd_config_get(int argc, const char **argv, const char *prefix)
 	return ret;
 }
 
-static int cmd_config_set(int argc, const char **argv, const char *prefix)
+static int cmd_config_set(int argc, const char **argv, const char *prefix,
+			  struct repository *repo UNUSED)
 {
 	struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
 	const char *value_pattern = NULL, *comment_arg = NULL;
@@ -973,7 +976,8 @@ static int cmd_config_set(int argc, const char **argv, const char *prefix)
 	return ret;
 }
 
-static int cmd_config_unset(int argc, const char **argv, const char *prefix)
+static int cmd_config_unset(int argc, const char **argv, const char *prefix,
+			    struct repository *repo UNUSED)
 {
 	struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
 	const char *value_pattern = NULL;
@@ -1010,7 +1014,8 @@ static int cmd_config_unset(int argc, const char **argv, const char *prefix)
 	return ret;
 }
 
-static int cmd_config_rename_section(int argc, const char **argv, const char *prefix)
+static int cmd_config_rename_section(int argc, const char **argv, const char *prefix,
+				     struct repository *repo UNUSED)
 {
 	struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
 	struct option opts[] = {
@@ -1039,7 +1044,8 @@ static int cmd_config_rename_section(int argc, const char **argv, const char *pr
 	return ret;
 }
 
-static int cmd_config_remove_section(int argc, const char **argv, const char *prefix)
+static int cmd_config_remove_section(int argc, const char **argv, const char *prefix,
+				     struct repository *repo UNUSED)
 {
 	struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
 	struct option opts[] = {
@@ -1099,7 +1105,8 @@ static int show_editor(struct config_location_options *opts)
 	return 0;
 }
 
-static int cmd_config_edit(int argc, const char **argv, const char *prefix)
+static int cmd_config_edit(int argc, const char **argv, const char *prefix,
+			   struct repository *repo UNUSED)
 {
 	struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT;
 	struct option opts[] = {
@@ -1395,7 +1402,7 @@ static int cmd_config_actions(int argc, const char **argv, const char *prefix)
 int cmd_config(int argc,
 	       const char **argv,
 	       const char *prefix,
-	       struct repository *repo UNUSED)
+	       struct repository *repo)
 {
 	parse_opt_subcommand_fn *subcommand = NULL;
 	struct option subcommand_opts[] = {
@@ -1422,7 +1429,7 @@ int cmd_config(int argc,
 	if (subcommand) {
 		argc = parse_options(argc, argv, prefix, subcommand_opts, builtin_config_usage,
 		       PARSE_OPT_SUBCOMMAND_OPTIONAL|PARSE_OPT_KEEP_UNKNOWN_OPT);
-		return subcommand(argc, argv, prefix);
+		return subcommand(argc, argv, prefix, repo);
 	}
 
 	return cmd_config_actions(argc, argv, prefix);
diff --git a/builtin/gc.c b/builtin/gc.c
index 09802eb989471be242a4b2d74606b0998fdd2b11..50ccab172365d22c8f89e9f6fb822fca948878e7 100644
--- a/builtin/gc.c
+++ b/builtin/gc.c
@@ -1567,7 +1567,8 @@ static int task_option_parse(const struct option *opt UNUSED,
 	return 0;
 }
 
-static int maintenance_run(int argc, const char **argv, const char *prefix)
+static int maintenance_run(int argc, const char **argv, const char *prefix,
+			   struct repository *repo UNUSED)
 {
 	int i;
 	struct maintenance_run_opts opts = MAINTENANCE_RUN_OPTS_INIT;
@@ -1629,7 +1630,8 @@ static char const * const builtin_maintenance_register_usage[] = {
 	NULL
 };
 
-static int maintenance_register(int argc, const char **argv, const char *prefix)
+static int maintenance_register(int argc, const char **argv, const char *prefix,
+				struct repository *repo UNUSED)
 {
 	char *config_file = NULL;
 	struct option options[] = {
@@ -1693,7 +1695,8 @@ static char const * const builtin_maintenance_unregister_usage[] = {
 	NULL
 };
 
-static int maintenance_unregister(int argc, const char **argv, const char *prefix)
+static int maintenance_unregister(int argc, const char **argv, const char *prefix,
+				  struct repository *repo UNUSED)
 {
 	int force = 0;
 	char *config_file = NULL;
@@ -2923,7 +2926,8 @@ static const char *const builtin_maintenance_start_usage[] = {
 	NULL
 };
 
-static int maintenance_start(int argc, const char **argv, const char *prefix)
+static int maintenance_start(int argc, const char **argv, const char *prefix,
+			     struct repository *repo)
 {
 	struct maintenance_start_opts opts = { 0 };
 	struct option options[] = {
@@ -2946,7 +2950,7 @@ static int maintenance_start(int argc, const char **argv, const char *prefix)
 	if (update_background_schedule(&opts, 1))
 		die(_("failed to set up maintenance schedule"));
 
-	if (maintenance_register(ARRAY_SIZE(register_args)-1, register_args, NULL))
+	if (maintenance_register(ARRAY_SIZE(register_args)-1, register_args, NULL, repo))
 		warning(_("failed to add repo to global config"));
 	return 0;
 }
@@ -2956,7 +2960,8 @@ static const char *const builtin_maintenance_stop_usage[] = {
 	NULL
 };
 
-static int maintenance_stop(int argc, const char **argv, const char *prefix)
+static int maintenance_stop(int argc, const char **argv, const char *prefix,
+			    struct repository *repo UNUSED)
 {
 	struct option options[] = {
 		OPT_END()
@@ -2976,7 +2981,7 @@ static const char * const builtin_maintenance_usage[] = {
 int cmd_maintenance(int argc,
 		    const char **argv,
 		    const char *prefix,
-		    struct repository *repo UNUSED)
+		    struct repository *repo)
 {
 	parse_opt_subcommand_fn *fn = NULL;
 	struct option builtin_maintenance_options[] = {
@@ -2990,5 +2995,5 @@ int cmd_maintenance(int argc,
 
 	argc = parse_options(argc, argv, prefix, builtin_maintenance_options,
 			     builtin_maintenance_usage, 0);
-	return fn(argc, argv, prefix);
+	return fn(argc, argv, prefix, repo);
 }
diff --git a/builtin/hook.c b/builtin/hook.c
index 367ef3e0b893fa16756880395151a82ed053acd8..672d2e37e845a2118aca1c4417a837138c250590 100644
--- a/builtin/hook.c
+++ b/builtin/hook.c
@@ -19,7 +19,8 @@ static const char * const builtin_hook_run_usage[] = {
 	NULL
 };
 
-static int run(int argc, const char **argv, const char *prefix)
+static int run(int argc, const char **argv, const char *prefix,
+	       struct repository *repo UNUSED)
 {
 	int i;
 	struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
@@ -70,7 +71,7 @@ static int run(int argc, const char **argv, const char *prefix)
 int cmd_hook(int argc,
 	     const char **argv,
 	     const char *prefix,
-	     struct repository *repo UNUSED)
+	     struct repository *repo)
 {
 	parse_opt_subcommand_fn *fn = NULL;
 	struct option builtin_hook_options[] = {
@@ -81,5 +82,5 @@ int cmd_hook(int argc,
 	argc = parse_options(argc, argv, NULL, builtin_hook_options,
 			     builtin_hook_usage, 0);
 
-	return fn(argc, argv, prefix);
+	return fn(argc, argv, prefix, repo);
 }
diff --git a/builtin/multi-pack-index.c b/builtin/multi-pack-index.c
index d159ed1314d912a390ed725659b3abe7c821b83f..85e40a4b6d3e47e9ec1ec27c094455e5ba75b5b0 100644
--- a/builtin/multi-pack-index.c
+++ b/builtin/multi-pack-index.c
@@ -119,7 +119,8 @@ static void read_packs_from_stdin(struct string_list *to)
 }
 
 static int cmd_multi_pack_index_write(int argc, const char **argv,
-				      const char *prefix)
+				      const char *prefix,
+				      struct repository *repo UNUSED)
 {
 	struct option *options;
 	static struct option builtin_multi_pack_index_write_options[] = {
@@ -183,7 +184,8 @@ static int cmd_multi_pack_index_write(int argc, const char **argv,
 }
 
 static int cmd_multi_pack_index_verify(int argc, const char **argv,
-				       const char *prefix)
+				       const char *prefix,
+				       struct repository *repo UNUSED)
 {
 	struct option *options;
 	static struct option builtin_multi_pack_index_verify_options[] = {
@@ -210,7 +212,8 @@ static int cmd_multi_pack_index_verify(int argc, const char **argv,
 }
 
 static int cmd_multi_pack_index_expire(int argc, const char **argv,
-				       const char *prefix)
+				       const char *prefix,
+				       struct repository *repo UNUSED)
 {
 	struct option *options;
 	static struct option builtin_multi_pack_index_expire_options[] = {
@@ -237,7 +240,8 @@ static int cmd_multi_pack_index_expire(int argc, const char **argv,
 }
 
 static int cmd_multi_pack_index_repack(int argc, const char **argv,
-				       const char *prefix)
+				       const char *prefix,
+				       struct repository *repo UNUSED)
 {
 	struct option *options;
 	static struct option builtin_multi_pack_index_repack_options[] = {
@@ -271,7 +275,7 @@ static int cmd_multi_pack_index_repack(int argc, const char **argv,
 int cmd_multi_pack_index(int argc,
 			 const char **argv,
 			 const char *prefix,
-			 struct repository *repo UNUSED)
+			 struct repository *repo)
 {
 	int res;
 	parse_opt_subcommand_fn *fn = NULL;
@@ -297,7 +301,7 @@ int cmd_multi_pack_index(int argc,
 			     builtin_multi_pack_index_usage, 0);
 	FREE_AND_NULL(options);
 
-	res = fn(argc, argv, prefix);
+	res = fn(argc, argv, prefix, repo);
 
 	free(opts.object_dir);
 	return res;
diff --git a/builtin/notes.c b/builtin/notes.c
index 72c8a51cfacf72e1f115774487311b3d4464d204..d051abf6dff8f2bce193fd52b4beda5060af3972 100644
--- a/builtin/notes.c
+++ b/builtin/notes.c
@@ -431,7 +431,8 @@ static struct notes_tree *init_notes_check(const char *subcommand,
 	return t;
 }
 
-static int list(int argc, const char **argv, const char *prefix)
+static int list(int argc, const char **argv, const char *prefix,
+		struct repository *repo UNUSED)
 {
 	struct notes_tree *t;
 	struct object_id object;
@@ -468,9 +469,11 @@ static int list(int argc, const char **argv, const char *prefix)
 	return retval;
 }
 
-static int append_edit(int argc, const char **argv, const char *prefix);
+static int append_edit(int argc, const char **argv, const char *prefix,
+		       struct repository *repo UNUSED);
 
-static int add(int argc, const char **argv, const char *prefix)
+static int add(int argc, const char **argv, const char *prefix,
+	       struct repository *repo)
 {
 	int force = 0, allow_empty = 0;
 	const char *object_ref;
@@ -543,7 +546,7 @@ static int add(int argc, const char **argv, const char *prefix)
 			 * argv[0-1].
 			 */
 			argv[0] = "edit";
-			return append_edit(argc, argv, prefix);
+			return append_edit(argc, argv, prefix, repo);
 		}
 		fprintf(stderr, _("Overwriting existing notes for object %s\n"),
 			oid_to_hex(&object));
@@ -569,7 +572,8 @@ static int add(int argc, const char **argv, const char *prefix)
 	return 0;
 }
 
-static int copy(int argc, const char **argv, const char *prefix)
+static int copy(int argc, const char **argv, const char *prefix,
+		struct repository *repo UNUSED)
 {
 	int retval = 0, force = 0, from_stdin = 0;
 	const struct object_id *from_note, *note;
@@ -646,7 +650,8 @@ static int copy(int argc, const char **argv, const char *prefix)
 	return retval;
 }
 
-static int append_edit(int argc, const char **argv, const char *prefix)
+static int append_edit(int argc, const char **argv, const char *prefix,
+		       struct repository *repo UNUSED)
 {
 	int allow_empty = 0;
 	const char *object_ref;
@@ -749,7 +754,8 @@ static int append_edit(int argc, const char **argv, const char *prefix)
 	return 0;
 }
 
-static int show(int argc, const char **argv, const char *prefix)
+static int show(int argc, const char **argv, const char *prefix,
+		struct repository *repo UNUSED)
 {
 	const char *object_ref;
 	struct notes_tree *t;
@@ -875,7 +881,8 @@ static int git_config_get_notes_strategy(const char *key,
 	return 0;
 }
 
-static int merge(int argc, const char **argv, const char *prefix)
+static int merge(int argc, const char **argv, const char *prefix,
+		 struct repository *repo UNUSED)
 {
 	struct strbuf remote_ref = STRBUF_INIT, msg = STRBUF_INIT;
 	struct object_id result_oid;
@@ -1016,7 +1023,8 @@ static int remove_one_note(struct notes_tree *t, const char *name, unsigned flag
 	return (flag & IGNORE_MISSING) ? 0 : status;
 }
 
-static int remove_cmd(int argc, const char **argv, const char *prefix)
+static int remove_cmd(int argc, const char **argv, const char *prefix,
+		      struct repository *repo UNUSED)
 {
 	unsigned flag = 0;
 	int from_stdin = 0;
@@ -1059,7 +1067,8 @@ static int remove_cmd(int argc, const char **argv, const char *prefix)
 	return retval;
 }
 
-static int prune(int argc, const char **argv, const char *prefix)
+static int prune(int argc, const char **argv, const char *prefix,
+		 struct repository *repo UNUSED)
 {
 	struct notes_tree *t;
 	int show_only = 0, verbose = 0;
@@ -1088,7 +1097,8 @@ static int prune(int argc, const char **argv, const char *prefix)
 	return 0;
 }
 
-static int get_ref(int argc, const char **argv, const char *prefix)
+static int get_ref(int argc, const char **argv, const char *prefix,
+		   struct repository *repo UNUSED)
 {
 	struct option options[] = { OPT_END() };
 	char *notes_ref;
@@ -1109,7 +1119,7 @@ static int get_ref(int argc, const char **argv, const char *prefix)
 int cmd_notes(int argc,
 	      const char **argv,
 	      const char *prefix,
-	      struct repository *repo UNUSED)
+	      struct repository *repo)
 {
 	const char *override_notes_ref = NULL;
 	parse_opt_subcommand_fn *fn = NULL;
@@ -1148,5 +1158,5 @@ int cmd_notes(int argc,
 		strbuf_release(&sb);
 	}
 
-	return !!fn(argc, argv, prefix);
+	return !!fn(argc, argv, prefix, repo);
 }
diff --git a/builtin/reflog.c b/builtin/reflog.c
index 22df6834f71098ab8378e4423967f0fb87858340..5a0c22f2f7e58733ce636619f3f19265b394bde5 100644
--- a/builtin/reflog.c
+++ b/builtin/reflog.c
@@ -234,7 +234,8 @@ static int expire_total_callback(const struct option *opt,
 	return 0;
 }
 
-static int cmd_reflog_show(int argc, const char **argv, const char *prefix)
+static int cmd_reflog_show(int argc, const char **argv, const char *prefix,
+			   struct repository *repo UNUSED)
 {
 	struct option options[] = {
 		OPT_END()
@@ -253,7 +254,8 @@ static int show_reflog(const char *refname, void *cb_data UNUSED)
 	return 0;
 }
 
-static int cmd_reflog_list(int argc, const char **argv, const char *prefix)
+static int cmd_reflog_list(int argc, const char **argv, const char *prefix,
+			   struct repository *repo UNUSED)
 {
 	struct option options[] = {
 		OPT_END()
@@ -270,7 +272,8 @@ static int cmd_reflog_list(int argc, const char **argv, const char *prefix)
 	return refs_for_each_reflog(ref_store, show_reflog, NULL);
 }
 
-static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
+static int cmd_reflog_expire(int argc, const char **argv, const char *prefix,
+			     struct repository *repo UNUSED)
 {
 	struct cmd_reflog_expire_cb cmd = { 0 };
 	timestamp_t now = time(NULL);
@@ -394,7 +397,8 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
 	return status;
 }
 
-static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
+static int cmd_reflog_delete(int argc, const char **argv, const char *prefix,
+			     struct repository *repo UNUSED)
 {
 	int i, status = 0;
 	unsigned int flags = 0;
@@ -424,7 +428,8 @@ static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
 	return status;
 }
 
-static int cmd_reflog_exists(int argc, const char **argv, const char *prefix)
+static int cmd_reflog_exists(int argc, const char **argv, const char *prefix,
+			     struct repository *repo UNUSED)
 {
 	struct option options[] = {
 		OPT_END()
@@ -467,7 +472,7 @@ int cmd_reflog(int argc,
 			     PARSE_OPT_KEEP_DASHDASH | PARSE_OPT_KEEP_ARGV0 |
 			     PARSE_OPT_KEEP_UNKNOWN_OPT);
 	if (fn)
-		return fn(argc - 1, argv + 1, prefix);
+		return fn(argc - 1, argv + 1, prefix, repository);
 	else
 		return cmd_log_reflog(argc, argv, prefix, repository);
 }
diff --git a/builtin/refs.c b/builtin/refs.c
index 24978a7b7b081ac6ed8ca99016f201d34c0639f8..3502980d21d055d8fc5ef36d2165aae61bc4db62 100644
--- a/builtin/refs.c
+++ b/builtin/refs.c
@@ -12,7 +12,8 @@
 #define REFS_VERIFY_USAGE \
 	N_("git refs verify [--strict] [--verbose]")
 
-static int cmd_refs_migrate(int argc, const char **argv, const char *prefix)
+static int cmd_refs_migrate(int argc, const char **argv, const char *prefix,
+			    struct repository *repo UNUSED)
 {
 	const char * const migrate_usage[] = {
 		REFS_MIGRATE_USAGE,
@@ -63,7 +64,8 @@ static int cmd_refs_migrate(int argc, const char **argv, const char *prefix)
 	return err;
 }
 
-static int cmd_refs_verify(int argc, const char **argv, const char *prefix)
+static int cmd_refs_verify(int argc, const char **argv, const char *prefix,
+			   struct repository *repo UNUSED)
 {
 	struct fsck_options fsck_refs_options = FSCK_REFS_OPTIONS_DEFAULT;
 	const char * const verify_usage[] = {
@@ -93,7 +95,7 @@ static int cmd_refs_verify(int argc, const char **argv, const char *prefix)
 int cmd_refs(int argc,
 	     const char **argv,
 	     const char *prefix,
-	     struct repository *repo UNUSED)
+	     struct repository *repo)
 {
 	const char * const refs_usage[] = {
 		REFS_MIGRATE_USAGE,
@@ -108,5 +110,5 @@ int cmd_refs(int argc,
 	};
 
 	argc = parse_options(argc, argv, prefix, opts, refs_usage, 0);
-	return fn(argc, argv, prefix);
+	return fn(argc, argv, prefix, repo);
 }
diff --git a/builtin/remote.c b/builtin/remote.c
index 76670ddd8b44e5b118c76522613c445ad92c5364..56214775df4ce86bf5d8e4ddb8d19140f696aeef 100644
--- a/builtin/remote.c
+++ b/builtin/remote.c
@@ -155,7 +155,8 @@ static int parse_mirror_opt(const struct option *opt, const char *arg, int not)
 	return 0;
 }
 
-static int add(int argc, const char **argv, const char *prefix)
+static int add(int argc, const char **argv, const char *prefix,
+	       struct repository *repo UNUSED)
 {
 	int fetch = 0, fetch_tags = TAGS_DEFAULT;
 	unsigned mirror = MIRROR_NONE;
@@ -706,7 +707,8 @@ static void handle_push_default(const char* old_name, const char* new_name)
 }
 
 
-static int mv(int argc, const char **argv, const char *prefix)
+static int mv(int argc, const char **argv, const char *prefix,
+	      struct repository *repo UNUSED)
 {
 	int show_progress = isatty(2);
 	struct option options[] = {
@@ -881,7 +883,8 @@ static int mv(int argc, const char **argv, const char *prefix)
 	return result;
 }
 
-static int rm(int argc, const char **argv, const char *prefix)
+static int rm(int argc, const char **argv, const char *prefix,
+	      struct repository *repo UNUSED)
 {
 	struct option options[] = {
 		OPT_END()
@@ -1303,7 +1306,8 @@ static int show_all(void)
 	return result;
 }
 
-static int show(int argc, const char **argv, const char *prefix)
+static int show(int argc, const char **argv, const char *prefix,
+		struct repository *repo UNUSED)
 {
 	int no_query = 0, result = 0, query_flag = 0;
 	struct option options[] = {
@@ -1399,7 +1403,8 @@ static int show(int argc, const char **argv, const char *prefix)
 	return result;
 }
 
-static int set_head(int argc, const char **argv, const char *prefix)
+static int set_head(int argc, const char **argv, const char *prefix,
+		    struct repository *repo UNUSED)
 {
 	int i, opt_a = 0, opt_d = 0, result = 0;
 	struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
@@ -1503,7 +1508,8 @@ static int prune_remote(const char *remote, int dry_run)
 	return result;
 }
 
-static int prune(int argc, const char **argv, const char *prefix)
+static int prune(int argc, const char **argv, const char *prefix,
+		 struct repository *repo UNUSED)
 {
 	int dry_run = 0, result = 0;
 	struct option options[] = {
@@ -1534,7 +1540,8 @@ static int get_remote_default(const char *key, const char *value UNUSED,
 	return 0;
 }
 
-static int update(int argc, const char **argv, const char *prefix)
+static int update(int argc, const char **argv, const char *prefix,
+		  struct repository *repo UNUSED)
 {
 	int i, prune = -1;
 	struct option options[] = {
@@ -1616,7 +1623,8 @@ static int set_remote_branches(const char *remotename, const char **branches,
 	return 0;
 }
 
-static int set_branches(int argc, const char **argv, const char *prefix)
+static int set_branches(int argc, const char **argv, const char *prefix,
+			struct repository *repo UNUSED)
 {
 	int add_mode = 0;
 	struct option options[] = {
@@ -1635,7 +1643,8 @@ static int set_branches(int argc, const char **argv, const char *prefix)
 	return set_remote_branches(argv[0], argv + 1, add_mode);
 }
 
-static int get_url(int argc, const char **argv, const char *prefix)
+static int get_url(int argc, const char **argv, const char *prefix,
+		   struct repository *repo UNUSED)
 {
 	int i, push_mode = 0, all_mode = 0;
 	const char *remotename = NULL;
@@ -1674,7 +1683,8 @@ static int get_url(int argc, const char **argv, const char *prefix)
 	return 0;
 }
 
-static int set_url(int argc, const char **argv, const char *prefix)
+static int set_url(int argc, const char **argv, const char *prefix,
+		   struct repository *repo UNUSED)
 {
 	int i, push_mode = 0, add_mode = 0, delete_mode = 0;
 	int matches = 0, negative_matches = 0;
@@ -1765,7 +1775,7 @@ static int set_url(int argc, const char **argv, const char *prefix)
 int cmd_remote(int argc,
 	       const char **argv,
 	       const char *prefix,
-	       struct repository *repo UNUSED)
+	       struct repository *repo)
 {
 	parse_opt_subcommand_fn *fn = NULL;
 	struct option options[] = {
@@ -1788,7 +1798,7 @@ int cmd_remote(int argc,
 			     PARSE_OPT_SUBCOMMAND_OPTIONAL);
 
 	if (fn) {
-		return !!fn(argc, argv, prefix);
+		return !!fn(argc, argv, prefix, repo);
 	} else {
 		if (argc) {
 			error(_("unknown subcommand: `%s'"), argv[0]);
diff --git a/builtin/sparse-checkout.c b/builtin/sparse-checkout.c
index 49aedc1de81a17b8b491cded7fa71b384e0e8be9..6f5fa5893b839fe21fef44ecbd837d132ebbac26 100644
--- a/builtin/sparse-checkout.c
+++ b/builtin/sparse-checkout.c
@@ -48,7 +48,8 @@ static char const * const builtin_sparse_checkout_list_usage[] = {
 	NULL
 };
 
-static int sparse_checkout_list(int argc, const char **argv, const char *prefix)
+static int sparse_checkout_list(int argc, const char **argv, const char *prefix,
+				struct repository *repo UNUSED)
 {
 	static struct option builtin_sparse_checkout_list_options[] = {
 		OPT_END(),
@@ -443,7 +444,8 @@ static struct sparse_checkout_init_opts {
 	int sparse_index;
 } init_opts;
 
-static int sparse_checkout_init(int argc, const char **argv, const char *prefix)
+static int sparse_checkout_init(int argc, const char **argv, const char *prefix,
+				struct repository *repo UNUSED)
 {
 	struct pattern_list pl;
 	char *sparse_filename;
@@ -770,7 +772,8 @@ static struct sparse_checkout_add_opts {
 	int use_stdin;
 } add_opts;
 
-static int sparse_checkout_add(int argc, const char **argv, const char *prefix)
+static int sparse_checkout_add(int argc, const char **argv, const char *prefix,
+			       struct repository *repo UNUSED)
 {
 	static struct option builtin_sparse_checkout_add_options[] = {
 		OPT_BOOL_F(0, "skip-checks", &add_opts.skip_checks,
@@ -808,7 +811,8 @@ static struct sparse_checkout_set_opts {
 	int use_stdin;
 } set_opts;
 
-static int sparse_checkout_set(int argc, const char **argv, const char *prefix)
+static int sparse_checkout_set(int argc, const char **argv, const char *prefix,
+			       struct repository *repo UNUSED)
 {
 	int default_patterns_nr = 2;
 	const char *default_patterns[] = {"/*", "!/*/", NULL};
@@ -866,7 +870,8 @@ static struct sparse_checkout_reapply_opts {
 } reapply_opts;
 
 static int sparse_checkout_reapply(int argc, const char **argv,
-				   const char *prefix)
+				   const char *prefix,
+				   struct repository *repo UNUSED)
 {
 	static struct option builtin_sparse_checkout_reapply_options[] = {
 		OPT_BOOL(0, "cone", &reapply_opts.cone_mode,
@@ -901,7 +906,8 @@ static char const * const builtin_sparse_checkout_disable_usage[] = {
 };
 
 static int sparse_checkout_disable(int argc, const char **argv,
-				   const char *prefix)
+				   const char *prefix,
+				   struct repository *repo UNUSED)
 {
 	static struct option builtin_sparse_checkout_disable_options[] = {
 		OPT_END(),
@@ -989,7 +995,8 @@ static int check_rules(struct pattern_list *pl, int null_terminated) {
 	return 0;
 }
 
-static int sparse_checkout_check_rules(int argc, const char **argv, const char *prefix)
+static int sparse_checkout_check_rules(int argc, const char **argv, const char *prefix,
+				       struct repository *repo UNUSED)
 {
 	static struct option builtin_sparse_checkout_check_rules_options[] = {
 		OPT_BOOL('z', NULL, &check_rules_opts.null_termination,
@@ -1037,7 +1044,7 @@ static int sparse_checkout_check_rules(int argc, const char **argv, const char *
 int cmd_sparse_checkout(int argc,
 			const char **argv,
 			const char *prefix,
-			struct repository *repo UNUSED)
+			struct repository *repo)
 {
 	parse_opt_subcommand_fn *fn = NULL;
 	struct option builtin_sparse_checkout_options[] = {
@@ -1060,5 +1067,5 @@ int cmd_sparse_checkout(int argc,
 	prepare_repo_settings(the_repository);
 	the_repository->settings.command_requires_full_index = 0;
 
-	return fn(argc, argv, prefix);
+	return fn(argc, argv, prefix, repo);
 }
diff --git a/builtin/stash.c b/builtin/stash.c
index 1399a1bbe2c222ed3e1c33ac2dd17bd1148f709c..c212b1c0b2c7c55de2fdd5d7d7a247ce6f530960 100644
--- a/builtin/stash.c
+++ b/builtin/stash.c
@@ -249,7 +249,8 @@ static int do_clear_stash(void)
 			       ref_stash, &obj, 0);
 }
 
-static int clear_stash(int argc, const char **argv, const char *prefix)
+static int clear_stash(int argc, const char **argv, const char *prefix,
+		       struct repository *repo UNUSED)
 {
 	struct option options[] = {
 		OPT_END()
@@ -652,7 +653,8 @@ static int do_apply_stash(const char *prefix, struct stash_info *info,
 	return ret;
 }
 
-static int apply_stash(int argc, const char **argv, const char *prefix)
+static int apply_stash(int argc, const char **argv, const char *prefix,
+		       struct repository *repo UNUSED)
 {
 	int ret = -1;
 	int quiet = 0;
@@ -726,7 +728,8 @@ static int get_stash_info_assert(struct stash_info *info, int argc,
 	return 0;
 }
 
-static int drop_stash(int argc, const char **argv, const char *prefix)
+static int drop_stash(int argc, const char **argv, const char *prefix,
+		      struct repository *repo UNUSED)
 {
 	int ret = -1;
 	int quiet = 0;
@@ -748,7 +751,8 @@ static int drop_stash(int argc, const char **argv, const char *prefix)
 	return ret;
 }
 
-static int pop_stash(int argc, const char **argv, const char *prefix)
+static int pop_stash(int argc, const char **argv, const char *prefix,
+		     struct repository *repo UNUSED)
 {
 	int ret = -1;
 	int index = 0;
@@ -778,7 +782,8 @@ static int pop_stash(int argc, const char **argv, const char *prefix)
 	return ret;
 }
 
-static int branch_stash(int argc, const char **argv, const char *prefix)
+static int branch_stash(int argc, const char **argv, const char *prefix,
+			struct repository *repo UNUSED)
 {
 	int ret = -1;
 	const char *branch = NULL;
@@ -816,7 +821,8 @@ static int branch_stash(int argc, const char **argv, const char *prefix)
 	return ret;
 }
 
-static int list_stash(int argc, const char **argv, const char *prefix)
+static int list_stash(int argc, const char **argv, const char *prefix,
+		      struct repository *repo UNUSED)
 {
 	struct child_process cp = CHILD_PROCESS_INIT;
 	struct option options[] = {
@@ -889,7 +895,8 @@ static void diff_include_untracked(const struct stash_info *info, struct diff_op
 	do_diff_cache(&info->b_commit, diff_opt);
 }
 
-static int show_stash(int argc, const char **argv, const char *prefix)
+static int show_stash(int argc, const char **argv, const char *prefix,
+		      struct repository *repo UNUSED)
 {
 	int i;
 	int ret = -1;
@@ -1017,7 +1024,8 @@ static int do_store_stash(const struct object_id *w_commit, const char *stash_ms
 	return 0;
 }
 
-static int store_stash(int argc, const char **argv, const char *prefix)
+static int store_stash(int argc, const char **argv, const char *prefix,
+		       struct repository *repo UNUSED)
 {
 	int quiet = 0;
 	const char *stash_msg = NULL;
@@ -1491,7 +1499,8 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b
 	return ret;
 }
 
-static int create_stash(int argc, const char **argv, const char *prefix UNUSED)
+static int create_stash(int argc, const char **argv, const char *prefix UNUSED,
+			struct repository *repo UNUSED)
 {
 	int ret;
 	struct strbuf stash_msg_buf = STRBUF_INIT;
@@ -1827,12 +1836,14 @@ static int push_stash(int argc, const char **argv, const char *prefix,
 	return ret;
 }
 
-static int push_stash_unassumed(int argc, const char **argv, const char *prefix)
+static int push_stash_unassumed(int argc, const char **argv, const char *prefix,
+				struct repository *repo UNUSED)
 {
 	return push_stash(argc, argv, prefix, 0);
 }
 
-static int save_stash(int argc, const char **argv, const char *prefix)
+static int save_stash(int argc, const char **argv, const char *prefix,
+		      struct repository *repo UNUSED)
 {
 	int keep_index = -1;
 	int only_staged = 0;
@@ -1878,7 +1889,7 @@ static int save_stash(int argc, const char **argv, const char *prefix)
 int cmd_stash(int argc,
 	      const char **argv,
 	      const char *prefix,
-	      struct repository *repo UNUSED)
+	      struct repository *repo)
 {
 	pid_t pid = getpid();
 	const char *index_file;
@@ -1916,9 +1927,9 @@ int cmd_stash(int argc,
 		    (uintmax_t)pid);
 
 	if (fn)
-		return !!fn(argc, argv, prefix);
+		return !!fn(argc, argv, prefix, repo);
 	else if (!argc)
-		return !!push_stash_unassumed(0, NULL, prefix);
+		return !!push_stash_unassumed(0, NULL, prefix, repo);
 
 	/* Assume 'stash push' */
 	strvec_push(&args, "push");
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index b6b5f1ebde7c2e4780af4097a0c4d6d838948aee..19e587838132f26e2e10dc4a9f93f7b1c78cd49d 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -399,7 +399,8 @@ static void runcommand_in_submodule_cb(const struct cache_entry *list_item,
 	free(displaypath);
 }
 
-static int module_foreach(int argc, const char **argv, const char *prefix)
+static int module_foreach(int argc, const char **argv, const char *prefix,
+			  struct repository *repo UNUSED)
 {
 	struct foreach_cb info = FOREACH_CB_INIT;
 	struct pathspec pathspec = { 0 };
@@ -544,7 +545,8 @@ static void init_submodule_cb(const struct cache_entry *list_item, void *cb_data
 		       info->flags);
 }
 
-static int module_init(int argc, const char **argv, const char *prefix)
+static int module_init(int argc, const char **argv, const char *prefix,
+		       struct repository *repo UNUSED)
 {
 	struct init_cb info = INIT_CB_INIT;
 	struct pathspec pathspec = { 0 };
@@ -738,7 +740,8 @@ static void status_submodule_cb(const struct cache_entry *list_item,
 			 info->prefix, info->super_prefix, info->flags);
 }
 
-static int module_status(int argc, const char **argv, const char *prefix)
+static int module_status(int argc, const char **argv, const char *prefix,
+			 struct repository *repo UNUSED)
 {
 	struct status_cb info = STATUS_CB_INIT;
 	struct pathspec pathspec = { 0 };
@@ -1163,7 +1166,8 @@ static int compute_summary_module_list(struct object_id *head_oid,
 	return ret;
 }
 
-static int module_summary(int argc, const char **argv, const char *prefix)
+static int module_summary(int argc, const char **argv, const char *prefix,
+			  struct repository *repo UNUSED)
 {
 	struct summary_cb info = SUMMARY_CB_INIT;
 	int cached = 0;
@@ -1339,7 +1343,8 @@ static void sync_submodule_cb(const struct cache_entry *list_item, void *cb_data
 		       info->flags);
 }
 
-static int module_sync(int argc, const char **argv, const char *prefix)
+static int module_sync(int argc, const char **argv, const char *prefix,
+		       struct repository *repo UNUSED)
 {
 	struct sync_cb info = SYNC_CB_INIT;
 	struct pathspec pathspec = { 0 };
@@ -1485,7 +1490,8 @@ static void deinit_submodule_cb(const struct cache_entry *list_item,
 	deinit_submodule(list_item->name, info->prefix, info->flags);
 }
 
-static int module_deinit(int argc, const char **argv, const char *prefix)
+static int module_deinit(int argc, const char **argv, const char *prefix,
+			 struct repository *repo UNUSED)
 {
 	struct deinit_cb info = DEINIT_CB_INIT;
 	struct pathspec pathspec = { 0 };
@@ -1842,7 +1848,8 @@ static int clone_submodule(const struct module_clone_data *clone_data,
 	return 0;
 }
 
-static int module_clone(int argc, const char **argv, const char *prefix)
+static int module_clone(int argc, const char **argv, const char *prefix,
+			struct repository *repo UNUSED)
 {
 	int dissociate = 0, quiet = 0, progress = 0, require_init = 0;
 	struct module_clone_data clone_data = MODULE_CLONE_DATA_INIT;
@@ -2779,7 +2786,8 @@ static int update_submodules(struct update_data *update_data)
 	return ret;
 }
 
-static int module_update(int argc, const char **argv, const char *prefix)
+static int module_update(int argc, const char **argv, const char *prefix,
+			 struct repository *repo UNUSED)
 {
 	struct pathspec pathspec = { 0 };
 	struct pathspec pathspec2 = { 0 };
@@ -2911,7 +2919,8 @@ static int module_update(int argc, const char **argv, const char *prefix)
 	return ret;
 }
 
-static int push_check(int argc, const char **argv, const char *prefix UNUSED)
+static int push_check(int argc, const char **argv, const char *prefix UNUSED,
+		      struct repository *repo UNUSED)
 {
 	struct remote *remote;
 	const char *superproject_head;
@@ -2991,7 +3000,8 @@ static int push_check(int argc, const char **argv, const char *prefix UNUSED)
 	return 0;
 }
 
-static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
+static int absorb_git_dirs(int argc, const char **argv, const char *prefix,
+			   struct repository *repo UNUSED)
 {
 	int i;
 	struct pathspec pathspec = { 0 };
@@ -3024,7 +3034,8 @@ static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
 	return ret;
 }
 
-static int module_set_url(int argc, const char **argv, const char *prefix)
+static int module_set_url(int argc, const char **argv, const char *prefix,
+			  struct repository *repo UNUSED)
 {
 	int quiet = 0, ret;
 	const char *newurl;
@@ -3063,7 +3074,8 @@ static int module_set_url(int argc, const char **argv, const char *prefix)
 	return !!ret;
 }
 
-static int module_set_branch(int argc, const char **argv, const char *prefix)
+static int module_set_branch(int argc, const char **argv, const char *prefix,
+			     struct repository *repo UNUSED)
 {
 	int opt_default = 0, ret;
 	const char *opt_branch = NULL;
@@ -3113,7 +3125,8 @@ static int module_set_branch(int argc, const char **argv, const char *prefix)
 	return !!ret;
 }
 
-static int module_create_branch(int argc, const char **argv, const char *prefix)
+static int module_create_branch(int argc, const char **argv, const char *prefix,
+				struct repository *repo UNUSED)
 {
 	enum branch_track track;
 	int quiet = 0, force = 0, reflog = 0, dry_run = 0;
@@ -3424,7 +3437,8 @@ static void die_on_repo_without_commits(const char *path)
 	strbuf_release(&sb);
 }
 
-static int module_add(int argc, const char **argv, const char *prefix)
+static int module_add(int argc, const char **argv, const char *prefix,
+		      struct repository *repo UNUSED)
 {
 	int force = 0, quiet = 0, progress = 0, dissociate = 0;
 	struct add_data add_data = ADD_DATA_INIT;
@@ -3557,7 +3571,7 @@ static int module_add(int argc, const char **argv, const char *prefix)
 int cmd_submodule__helper(int argc,
 			  const char **argv,
 			  const char *prefix,
-			  struct repository *repo UNUSED)
+			  struct repository *repo)
 {
 	parse_opt_subcommand_fn *fn = NULL;
 	const char *const usage[] = {
@@ -3583,5 +3597,5 @@ int cmd_submodule__helper(int argc,
 	};
 	argc = parse_options(argc, argv, prefix, options, usage, 0);
 
-	return fn(argc, argv, prefix);
+	return fn(argc, argv, prefix, repo);
 }
diff --git a/builtin/worktree.c b/builtin/worktree.c
index dae63dedf4cac2621f51f95a39aa456b33acd894..824dd71d643f112f384cde4de1373698c67de254 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -231,7 +231,8 @@ static void prune_worktrees(void)
 	strbuf_release(&reason);
 }
 
-static int prune(int ac, const char **av, const char *prefix)
+static int prune(int ac, const char **av, const char *prefix,
+		 struct repository *repo UNUSED)
 {
 	struct option options[] = {
 		OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
@@ -763,7 +764,8 @@ static char *dwim_branch(const char *path, char **new_branch)
 	return NULL;
 }
 
-static int add(int ac, const char **av, const char *prefix)
+static int add(int ac, const char **av, const char *prefix,
+	       struct repository *repo UNUSED)
 {
 	struct add_opts opts;
 	const char *new_branch_force = NULL;
@@ -1039,7 +1041,8 @@ static void pathsort(struct worktree **wt)
 	QSORT(wt, n, pathcmp);
 }
 
-static int list(int ac, const char **av, const char *prefix)
+static int list(int ac, const char **av, const char *prefix,
+		struct repository *repo UNUSED)
 {
 	int porcelain = 0;
 	int line_terminator = '\n';
@@ -1084,7 +1087,8 @@ static int list(int ac, const char **av, const char *prefix)
 	return 0;
 }
 
-static int lock_worktree(int ac, const char **av, const char *prefix)
+static int lock_worktree(int ac, const char **av, const char *prefix,
+			 struct repository *repo UNUSED)
 {
 	const char *reason = "", *old_reason;
 	struct option options[] = {
@@ -1119,7 +1123,8 @@ static int lock_worktree(int ac, const char **av, const char *prefix)
 	return 0;
 }
 
-static int unlock_worktree(int ac, const char **av, const char *prefix)
+static int unlock_worktree(int ac, const char **av, const char *prefix,
+			   struct repository *repo UNUSED)
 {
 	struct option options[] = {
 		OPT_END()
@@ -1182,7 +1187,8 @@ static void validate_no_submodules(const struct worktree *wt)
 		die(_("working trees containing submodules cannot be moved or removed"));
 }
 
-static int move_worktree(int ac, const char **av, const char *prefix)
+static int move_worktree(int ac, const char **av, const char *prefix,
+			 struct repository *repo UNUSED)
 {
 	int force = 0;
 	struct option options[] = {
@@ -1312,7 +1318,8 @@ static int delete_git_work_tree(struct worktree *wt)
 	return ret;
 }
 
-static int remove_worktree(int ac, const char **av, const char *prefix)
+static int remove_worktree(int ac, const char **av, const char *prefix,
+			   struct repository *repo UNUSED)
 {
 	int force = 0;
 	struct option options[] = {
@@ -1377,7 +1384,8 @@ static void report_repair(int iserr, const char *path, const char *msg, void *cb
 	}
 }
 
-static int repair(int ac, const char **av, const char *prefix)
+static int repair(int ac, const char **av, const char *prefix,
+		  struct repository *repo UNUSED)
 {
 	const char **p;
 	const char *self[] = { ".", NULL };
@@ -1397,7 +1405,7 @@ static int repair(int ac, const char **av, const char *prefix)
 int cmd_worktree(int ac,
 		 const char **av,
 		 const char *prefix,
-		 struct repository *repo UNUSED)
+		 struct repository *repo)
 {
 	parse_opt_subcommand_fn *fn = NULL;
 	struct option options[] = {
@@ -1422,5 +1430,5 @@ int cmd_worktree(int ac,
 	prepare_repo_settings(the_repository);
 	the_repository->settings.command_requires_full_index = 0;
 
-	return fn(ac, av, prefix);
+	return fn(ac, av, prefix, repo);
 }
diff --git a/parse-options.h b/parse-options.h
index ae15342390837c21503ab812648a7d8fd21432a8..f0801d4532a175b65783689f2a68fb56da2c8e87 100644
--- a/parse-options.h
+++ b/parse-options.h
@@ -3,6 +3,8 @@
 
 #include "gettext.h"
 
+struct repository;
+
 /**
  * Refer to Documentation/technical/api-parse-options.txt for the API doc.
  */
@@ -73,7 +75,7 @@ typedef enum parse_opt_result parse_opt_ll_cb(struct parse_opt_ctx_t *ctx,
 					      const char *arg, int unset);
 
 typedef int parse_opt_subcommand_fn(int argc, const char **argv,
-				    const char *prefix);
+				    const char *prefix, struct repository *repo);
 
 /*
  * `type`::
diff --git a/t/helper/test-parse-options.c b/t/helper/test-parse-options.c
index 5250913d99eba18a28878d3904cb7b2399670d02..5da359486cdcb353b277653111bc201c5f82851e 100644
--- a/t/helper/test-parse-options.c
+++ b/t/helper/test-parse-options.c
@@ -282,14 +282,16 @@ int cmd__parse_options_flags(int argc, const char **argv)
 	return parse_options_flags__cmd(argc, argv, test_flags);
 }
 
-static int subcmd_one(int argc, const char **argv, const char *prefix UNUSED)
+static int subcmd_one(int argc, const char **argv, const char *prefix UNUSED,
+		      struct repository *repo UNUSED)
 {
 	printf("fn: subcmd_one\n");
 	print_args(argc, argv);
 	return 0;
 }
 
-static int subcmd_two(int argc, const char **argv, const char *prefix UNUSED)
+static int subcmd_two(int argc, const char **argv, const char *prefix UNUSED,
+		      struct repository *repo UNUSED)
 {
 	printf("fn: subcmd_two\n");
 	print_args(argc, argv);
@@ -319,7 +321,7 @@ static int parse_subcommand__cmd(int argc, const char **argv,
 
 	printf("opt: %d\n", opt);
 
-	return fn(argc, argv, NULL);
+	return fn(argc, argv, NULL, NULL);
 }
 
 int cmd__parse_subcommand(int argc, const char **argv)

-- 
2.47.0


^ permalink raw reply related	[flat|nested] 70+ messages in thread

* [PATCH v2 02/10] midx-write: pass down repository to static functions
  2024-11-19 15:36 ` [PATCH v2 00/10] " Karthik Nayak
  2024-11-19 15:36   ` [PATCH v2 01/10] builtin: pass repository to sub commands Karthik Nayak
@ 2024-11-19 15:36   ` Karthik Nayak
  2024-11-20 18:15     ` Christian Couder
  2024-11-20 19:43     ` Taylor Blau
  2024-11-19 15:36   ` [PATCH v2 03/10] midx-write: use `revs->repo` inside `read_refs_snapshot` Karthik Nayak
                     ` (10 subsequent siblings)
  12 siblings, 2 replies; 70+ messages in thread
From: Karthik Nayak @ 2024-11-19 15:36 UTC (permalink / raw)
  To: git; +Cc: Karthik Nayak, ps, shejialuo

In 'midx-write.c' there are a lot of static functions which use global
variables `the_repository` or `the_hash_algo`. In a follow up commit,
the repository variable will be added to `write_midx_context`, which
some of the functions can use. But for functions which do not have
access to this struct, pass down the required information from
non-static functions `write_midx_file` and `write_midx_file_only`.

This ensures that the usage of global variables is limited to these
non-static functions, which will be cleaned up in a follow up commits.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 midx-write.c | 57 +++++++++++++++++++++++++++++++--------------------------
 1 file changed, 31 insertions(+), 26 deletions(-)

diff --git a/midx-write.c b/midx-write.c
index c57726ef9475df693890d61627ce91409c1def7c..22b5233f51ec6c6d99b8f9613818f1581dca5982 100644
--- a/midx-write.c
+++ b/midx-write.c
@@ -35,13 +35,13 @@ extern void clear_incremental_midx_files_ext(const char *object_dir,
 extern int cmp_idx_or_pack_name(const char *idx_or_pack_name,
 				const char *idx_name);
 
-static size_t write_midx_header(struct hashfile *f,
-				unsigned char num_chunks,
+static size_t write_midx_header(const struct git_hash_algo *hash_algo,
+				struct hashfile *f, unsigned char num_chunks,
 				uint32_t num_packs)
 {
 	hashwrite_be32(f, MIDX_SIGNATURE);
 	hashwrite_u8(f, MIDX_VERSION);
-	hashwrite_u8(f, oid_version(the_hash_algo));
+	hashwrite_u8(f, oid_version(hash_algo));
 	hashwrite_u8(f, num_chunks);
 	hashwrite_u8(f, 0); /* unused */
 	hashwrite_be32(f, num_packs);
@@ -702,7 +702,7 @@ static int add_ref_to_pending(const char *refname, const char *referent UNUSED,
 		return 0;
 	}
 
-	if (!peel_iterated_oid(the_repository, oid, &peeled))
+	if (!peel_iterated_oid(revs->repo, oid, &peeled))
 		oid = &peeled;
 
 	object = parse_object_or_die(oid, refname);
@@ -827,7 +827,7 @@ static struct commit **find_commits_for_midx_bitmap(uint32_t *indexed_commits_nr
 	return cb.commits;
 }
 
-static int write_midx_bitmap(const char *midx_name,
+static int write_midx_bitmap(struct repository *r, const char *midx_name,
 			     const unsigned char *midx_hash,
 			     struct packing_data *pdata,
 			     struct commit **commits,
@@ -840,9 +840,9 @@ static int write_midx_bitmap(const char *midx_name,
 	struct bitmap_writer writer;
 	struct pack_idx_entry **index;
 	char *bitmap_name = xstrfmt("%s-%s.bitmap", midx_name,
-					hash_to_hex(midx_hash));
+				    hash_to_hex_algop(midx_hash, r->hash_algo));
 
-	trace2_region_enter("midx", "write_midx_bitmap", the_repository);
+	trace2_region_enter("midx", "write_midx_bitmap", r);
 
 	if (flags & MIDX_WRITE_BITMAP_HASH_CACHE)
 		options |= BITMAP_OPT_HASH_CACHE;
@@ -859,7 +859,7 @@ static int write_midx_bitmap(const char *midx_name,
 	for (i = 0; i < pdata->nr_objects; i++)
 		index[i] = &pdata->objects[i].idx;
 
-	bitmap_writer_init(&writer, the_repository, pdata);
+	bitmap_writer_init(&writer, r, pdata);
 	bitmap_writer_show_progress(&writer, flags & MIDX_PROGRESS);
 	bitmap_writer_build_type_index(&writer, index);
 
@@ -892,7 +892,7 @@ static int write_midx_bitmap(const char *midx_name,
 	free(bitmap_name);
 	bitmap_writer_free(&writer);
 
-	trace2_region_leave("midx", "write_midx_bitmap", the_repository);
+	trace2_region_leave("midx", "write_midx_bitmap", r);
 
 	return ret;
 }
@@ -1049,7 +1049,7 @@ static void clear_midx_files(const char *object_dir,
 	strbuf_release(&buf);
 }
 
-static int write_midx_internal(const char *object_dir,
+static int write_midx_internal(struct repository *r, const char *object_dir,
 			       struct string_list *packs_to_include,
 			       struct string_list *packs_to_drop,
 			       const char *preferred_pack_name,
@@ -1070,7 +1070,8 @@ static int write_midx_internal(const char *object_dir,
 	const char **keep_hashes = NULL;
 	struct chunkfile *cf;
 
-	trace2_region_enter("midx", "write_midx_internal", the_repository);
+	trace2_region_enter("midx", "write_midx_internal", r);
+
 
 	ctx.incremental = !!(flags & MIDX_WRITE_INCREMENTAL);
 	if (ctx.incremental && (flags & MIDX_WRITE_BITMAP))
@@ -1087,8 +1088,7 @@ static int write_midx_internal(const char *object_dir,
 			  midx_name.buf);
 
 	if (!packs_to_include || ctx.incremental) {
-		struct multi_pack_index *m = lookup_multi_pack_index(the_repository,
-								     object_dir);
+		struct multi_pack_index *m = lookup_multi_pack_index(r, object_dir);
 		if (m && !midx_checksum_valid(m)) {
 			warning(_("ignoring existing multi-pack-index; checksum mismatch"));
 			m = NULL;
@@ -1351,7 +1351,7 @@ static int write_midx_internal(const char *object_dir,
 	add_chunk(cf, MIDX_CHUNKID_OIDFANOUT, MIDX_CHUNK_FANOUT_SIZE,
 		  write_midx_oid_fanout);
 	add_chunk(cf, MIDX_CHUNKID_OIDLOOKUP,
-		  st_mult(ctx.entries_nr, the_hash_algo->rawsz),
+		  st_mult(ctx.entries_nr, r->hash_algo->rawsz),
 		  write_midx_oid_lookup);
 	add_chunk(cf, MIDX_CHUNKID_OBJECTOFFSETS,
 		  st_mult(ctx.entries_nr, MIDX_CHUNK_OFFSET_WIDTH),
@@ -1373,7 +1373,8 @@ static int write_midx_internal(const char *object_dir,
 			  write_midx_bitmapped_packs);
 	}
 
-	write_midx_header(f, get_num_chunks(cf), ctx.nr - dropped_packs);
+	write_midx_header(r->hash_algo, f, get_num_chunks(cf),
+			  ctx.nr - dropped_packs);
 	write_chunkfile(cf, &ctx);
 
 	finalize_hashfile(f, midx_hash, FSYNC_COMPONENT_PACK_METADATA,
@@ -1405,7 +1406,7 @@ static int write_midx_internal(const char *object_dir,
 		FREE_AND_NULL(ctx.entries);
 		ctx.entries_nr = 0;
 
-		if (write_midx_bitmap(midx_name.buf, midx_hash, &pdata,
+		if (write_midx_bitmap(r, midx_name.buf, midx_hash, &pdata,
 				      commits, commits_nr, ctx.pack_order,
 				      flags) < 0) {
 			error(_("could not write multi-pack bitmap"));
@@ -1449,12 +1450,13 @@ static int write_midx_internal(const char *object_dir,
 		strbuf_release(&final_midx_name);
 
 		keep_hashes[ctx.num_multi_pack_indexes_before] =
-			xstrdup(hash_to_hex(midx_hash));
+			xstrdup(hash_to_hex_algop(midx_hash, r->hash_algo));
 
 		for (i = 0; i < ctx.num_multi_pack_indexes_before; i++) {
 			uint32_t j = ctx.num_multi_pack_indexes_before - i - 1;
 
-			keep_hashes[j] = xstrdup(hash_to_hex(get_midx_checksum(m)));
+			keep_hashes[j] = xstrdup(hash_to_hex_algop(get_midx_checksum(m),
+								   r->hash_algo));
 			m = m->base_midx;
 		}
 
@@ -1462,7 +1464,7 @@ static int write_midx_internal(const char *object_dir,
 			fprintf(get_lock_file_fp(&lk), "%s\n", keep_hashes[i]);
 	} else {
 		keep_hashes[ctx.num_multi_pack_indexes_before] =
-			xstrdup(hash_to_hex(midx_hash));
+			xstrdup(hash_to_hex_algop(midx_hash, r->hash_algo));
 	}
 
 	if (ctx.m || ctx.base_midx)
@@ -1495,7 +1497,7 @@ static int write_midx_internal(const char *object_dir,
 	}
 	strbuf_release(&midx_name);
 
-	trace2_region_leave("midx", "write_midx_internal", the_repository);
+	trace2_region_leave("midx", "write_midx_internal", r);
 
 	return result;
 }
@@ -1505,8 +1507,8 @@ int write_midx_file(const char *object_dir,
 		    const char *refs_snapshot,
 		    unsigned flags)
 {
-	return write_midx_internal(object_dir, NULL, NULL, preferred_pack_name,
-				   refs_snapshot, flags);
+	return write_midx_internal(the_repository, object_dir, NULL, NULL,
+				   preferred_pack_name, refs_snapshot, flags);
 }
 
 int write_midx_file_only(const char *object_dir,
@@ -1515,8 +1517,9 @@ int write_midx_file_only(const char *object_dir,
 			 const char *refs_snapshot,
 			 unsigned flags)
 {
-	return write_midx_internal(object_dir, packs_to_include, NULL,
-				   preferred_pack_name, refs_snapshot, flags);
+	return write_midx_internal(the_repository, object_dir, packs_to_include,
+				   NULL, preferred_pack_name, refs_snapshot,
+				   flags);
 }
 
 int expire_midx_packs(struct repository *r, const char *object_dir, unsigned flags)
@@ -1572,7 +1575,8 @@ int expire_midx_packs(struct repository *r, const char *object_dir, unsigned fla
 	free(count);
 
 	if (packs_to_drop.nr)
-		result = write_midx_internal(object_dir, NULL, &packs_to_drop, NULL, NULL, flags);
+		result = write_midx_internal(r, object_dir, NULL,
+					     &packs_to_drop, NULL, NULL, flags);
 
 	string_list_clear(&packs_to_drop, 0);
 
@@ -1769,7 +1773,8 @@ int midx_repack(struct repository *r, const char *object_dir, size_t batch_size,
 		goto cleanup;
 	}
 
-	result = write_midx_internal(object_dir, NULL, NULL, NULL, NULL, flags);
+	result = write_midx_internal(r, object_dir, NULL, NULL, NULL, NULL,
+				     flags);
 
 cleanup:
 	free(include_pack);

-- 
2.47.0


^ permalink raw reply related	[flat|nested] 70+ messages in thread

* [PATCH v2 03/10] midx-write: use `revs->repo` inside `read_refs_snapshot`
  2024-11-19 15:36 ` [PATCH v2 00/10] " Karthik Nayak
  2024-11-19 15:36   ` [PATCH v2 01/10] builtin: pass repository to sub commands Karthik Nayak
  2024-11-19 15:36   ` [PATCH v2 02/10] midx-write: pass down repository to static functions Karthik Nayak
@ 2024-11-19 15:36   ` Karthik Nayak
  2024-11-20 12:58     ` shejialuo
  2024-11-20 19:44     ` Taylor Blau
  2024-11-19 15:36   ` [PATCH v2 04/10] write-midx: add repository field to `write_midx_context` Karthik Nayak
                     ` (9 subsequent siblings)
  12 siblings, 2 replies; 70+ messages in thread
From: Karthik Nayak @ 2024-11-19 15:36 UTC (permalink / raw)
  To: git; +Cc: Karthik Nayak, ps, shejialuo

The `read_refs_snapshot` uses the `parse_oid_hex` function which
internally uses global variables. Let's instead use
`parse_oid_hex_algop` and provide the hash algo via `revs->repo`.

Also, while here, fix a missing newline after the functions definition.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 midx-write.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/midx-write.c b/midx-write.c
index 22b5233f51ec6c6d99b8f9613818f1581dca5982..564438f616f59cd24edda956e4af0e0acf167138 100644
--- a/midx-write.c
+++ b/midx-write.c
@@ -760,7 +760,7 @@ static int read_refs_snapshot(const char *refs_snapshot,
 			hex = &buf.buf[1];
 		}
 
-		if (parse_oid_hex(hex, &oid, &end) < 0)
+		if (parse_oid_hex_algop(hex, &oid, &end, revs->repo->hash_algo) < 0)
 			die(_("could not parse line: %s"), buf.buf);
 		if (*end)
 			die(_("malformed line: %s"), buf.buf);
@@ -776,6 +776,7 @@ static int read_refs_snapshot(const char *refs_snapshot,
 	strbuf_release(&buf);
 	return 0;
 }
+
 static struct commit **find_commits_for_midx_bitmap(uint32_t *indexed_commits_nr_p,
 						    const char *refs_snapshot,
 						    struct write_midx_context *ctx)

-- 
2.47.0


^ permalink raw reply related	[flat|nested] 70+ messages in thread

* [PATCH v2 04/10] write-midx: add repository field to `write_midx_context`
  2024-11-19 15:36 ` [PATCH v2 00/10] " Karthik Nayak
                     ` (2 preceding siblings ...)
  2024-11-19 15:36   ` [PATCH v2 03/10] midx-write: use `revs->repo` inside `read_refs_snapshot` Karthik Nayak
@ 2024-11-19 15:36   ` Karthik Nayak
  2024-11-19 15:36   ` [PATCH v2 05/10] midx-write: pass down repository to `write_midx_file[_only]` Karthik Nayak
                     ` (8 subsequent siblings)
  12 siblings, 0 replies; 70+ messages in thread
From: Karthik Nayak @ 2024-11-19 15:36 UTC (permalink / raw)
  To: git; +Cc: Karthik Nayak, ps, shejialuo

The struct `write_midx_context` is used to pass context for creating
MIDX files. Add the repository field here to ensure that most functions
within `midx-write.c` have access to the field and can use that instead
of the global `the_repository` variable.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 midx-write.c | 38 ++++++++++++++++++++------------------
 1 file changed, 20 insertions(+), 18 deletions(-)

diff --git a/midx-write.c b/midx-write.c
index 564438f616f59cd24edda956e4af0e0acf167138..1c355cdf8db4e9fed61a4aabf61a237ad26181ce 100644
--- a/midx-write.c
+++ b/midx-write.c
@@ -110,6 +110,8 @@ struct write_midx_context {
 	uint32_t num_multi_pack_indexes_before;
 
 	struct string_list *to_include;
+
+	struct repository *repo;
 };
 
 static int should_include_pack(const struct write_midx_context *ctx,
@@ -154,7 +156,7 @@ static void add_pack_to_midx(const char *full_path, size_t full_path_len,
 			return;
 
 		ALLOC_GROW(ctx->info, ctx->nr + 1, ctx->alloc);
-		p = add_packed_git(the_repository, full_path, full_path_len, 0);
+		p = add_packed_git(ctx->repo, full_path, full_path_len, 0);
 		if (!p) {
 			warning(_("failed to add packfile '%s'"),
 				full_path);
@@ -480,7 +482,7 @@ static int write_midx_oid_lookup(struct hashfile *f,
 				 void *data)
 {
 	struct write_midx_context *ctx = data;
-	unsigned char hash_len = the_hash_algo->rawsz;
+	unsigned char hash_len = ctx->repo->hash_algo->rawsz;
 	struct pack_midx_entry *list = ctx->entries;
 	uint32_t i;
 
@@ -605,7 +607,7 @@ static uint32_t *midx_pack_order(struct write_midx_context *ctx)
 	uint32_t *pack_order, base_objects = 0;
 	uint32_t i;
 
-	trace2_region_enter("midx", "midx_pack_order", the_repository);
+	trace2_region_enter("midx", "midx_pack_order", ctx->repo);
 
 	if (ctx->incremental && ctx->base_midx)
 		base_objects = ctx->base_midx->num_objects +
@@ -640,7 +642,7 @@ static uint32_t *midx_pack_order(struct write_midx_context *ctx)
 	}
 	free(data);
 
-	trace2_region_leave("midx", "midx_pack_order", the_repository);
+	trace2_region_leave("midx", "midx_pack_order", ctx->repo);
 
 	return pack_order;
 }
@@ -651,9 +653,10 @@ static void write_midx_reverse_index(char *midx_name, unsigned char *midx_hash,
 	struct strbuf buf = STRBUF_INIT;
 	char *tmp_file;
 
-	trace2_region_enter("midx", "write_midx_reverse_index", the_repository);
+	trace2_region_enter("midx", "write_midx_reverse_index", ctx->repo);
 
-	strbuf_addf(&buf, "%s-%s.rev", midx_name, hash_to_hex(midx_hash));
+	strbuf_addf(&buf, "%s-%s.rev", midx_name, hash_to_hex_algop(midx_hash,
+								    ctx->repo->hash_algo));
 
 	tmp_file = write_rev_file_order(NULL, ctx->pack_order, ctx->entries_nr,
 					midx_hash, WRITE_REV);
@@ -664,7 +667,7 @@ static void write_midx_reverse_index(char *midx_name, unsigned char *midx_hash,
 	strbuf_release(&buf);
 	free(tmp_file);
 
-	trace2_region_leave("midx", "write_midx_reverse_index", the_repository);
+	trace2_region_leave("midx", "write_midx_reverse_index", ctx->repo);
 }
 
 static void prepare_midx_packing_data(struct packing_data *pdata,
@@ -672,10 +675,10 @@ static void prepare_midx_packing_data(struct packing_data *pdata,
 {
 	uint32_t i;
 
-	trace2_region_enter("midx", "prepare_midx_packing_data", the_repository);
+	trace2_region_enter("midx", "prepare_midx_packing_data", ctx->repo);
 
 	memset(pdata, 0, sizeof(struct packing_data));
-	prepare_packing_data(the_repository, pdata);
+	prepare_packing_data(ctx->repo, pdata);
 
 	for (i = 0; i < ctx->entries_nr; i++) {
 		uint32_t pos = ctx->pack_order[i];
@@ -686,7 +689,7 @@ static void prepare_midx_packing_data(struct packing_data *pdata,
 			       ctx->info[ctx->pack_perm[from->pack_int_id]].p);
 	}
 
-	trace2_region_leave("midx", "prepare_midx_packing_data", the_repository);
+	trace2_region_leave("midx", "prepare_midx_packing_data", ctx->repo);
 }
 
 static int add_ref_to_pending(const char *refname, const char *referent UNUSED,
@@ -784,17 +787,16 @@ static struct commit **find_commits_for_midx_bitmap(uint32_t *indexed_commits_nr
 	struct rev_info revs;
 	struct bitmap_commit_cb cb = {0};
 
-	trace2_region_enter("midx", "find_commits_for_midx_bitmap",
-			    the_repository);
+	trace2_region_enter("midx", "find_commits_for_midx_bitmap", ctx->repo);
 
 	cb.ctx = ctx;
 
-	repo_init_revisions(the_repository, &revs, NULL);
+	repo_init_revisions(ctx->repo, &revs, NULL);
 	if (refs_snapshot) {
 		read_refs_snapshot(refs_snapshot, &revs);
 	} else {
 		setup_revisions(0, NULL, &revs, NULL);
-		refs_for_each_ref(get_main_ref_store(the_repository),
+		refs_for_each_ref(get_main_ref_store(ctx->repo),
 				  add_ref_to_pending, &revs);
 	}
 
@@ -822,8 +824,7 @@ static struct commit **find_commits_for_midx_bitmap(uint32_t *indexed_commits_nr
 
 	release_revisions(&revs);
 
-	trace2_region_leave("midx", "find_commits_for_midx_bitmap",
-			    the_repository);
+	trace2_region_leave("midx", "find_commits_for_midx_bitmap", ctx->repo);
 
 	return cb.commits;
 }
@@ -945,7 +946,7 @@ static int fill_packs_from_midx(struct write_midx_context *ctx,
 			 */
 			if (flags & MIDX_WRITE_REV_INDEX ||
 			    preferred_pack_name) {
-				if (prepare_midx_pack(the_repository, m,
+				if (prepare_midx_pack(ctx->repo, m,
 						      m->num_packs_in_base + i)) {
 					error(_("could not load pack"));
 					return 1;
@@ -1073,6 +1074,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
 
 	trace2_region_enter("midx", "write_midx_internal", r);
 
+	ctx.repo = r;
 
 	ctx.incremental = !!(flags & MIDX_WRITE_INCREMENTAL);
 	if (ctx.incremental && (flags & MIDX_WRITE_BITMAP))
@@ -1469,7 +1471,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
 	}
 
 	if (ctx.m || ctx.base_midx)
-		close_object_store(the_repository->objects);
+		close_object_store(ctx.repo->objects);
 
 	if (commit_lock_file(&lk) < 0)
 		die_errno(_("could not write multi-pack-index"));

-- 
2.47.0


^ permalink raw reply related	[flat|nested] 70+ messages in thread

* [PATCH v2 05/10] midx-write: pass down repository to `write_midx_file[_only]`
  2024-11-19 15:36 ` [PATCH v2 00/10] " Karthik Nayak
                     ` (3 preceding siblings ...)
  2024-11-19 15:36   ` [PATCH v2 04/10] write-midx: add repository field to `write_midx_context` Karthik Nayak
@ 2024-11-19 15:36   ` Karthik Nayak
  2024-11-20 20:11     ` Taylor Blau
  2024-11-19 15:36   ` [PATCH v2 06/10] midx: cleanup internal usage of `the_repository` and `the_hash_algo` Karthik Nayak
                     ` (7 subsequent siblings)
  12 siblings, 1 reply; 70+ messages in thread
From: Karthik Nayak @ 2024-11-19 15:36 UTC (permalink / raw)
  To: git; +Cc: Karthik Nayak, ps, shejialuo

In a previous commit, we passed the repository field to all
subcommands in the `builtin/` directory. Utilize this to pass the
repository field down to the `write_midx_file[_only]` functions to
remove the usage of `the_repository` global variables.

With this, all usage of global variables in `midx-write.c` is removed,
hence, remove the `USE_THE_REPOSITORY_VARIABLE` guard from the file.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 builtin/multi-pack-index.c |  6 +++---
 builtin/repack.c           |  2 +-
 midx-write.c               | 22 +++++++++-------------
 midx.h                     | 10 ++++------
 4 files changed, 17 insertions(+), 23 deletions(-)

diff --git a/builtin/multi-pack-index.c b/builtin/multi-pack-index.c
index 85e40a4b6d3e47e9ec1ec27c094455e5ba75b5b0..2a938466f53aaa11096170554fe11a4ed46a25e4 100644
--- a/builtin/multi-pack-index.c
+++ b/builtin/multi-pack-index.c
@@ -120,7 +120,7 @@ static void read_packs_from_stdin(struct string_list *to)
 
 static int cmd_multi_pack_index_write(int argc, const char **argv,
 				      const char *prefix,
-				      struct repository *repo UNUSED)
+				      struct repository *repo)
 {
 	struct option *options;
 	static struct option builtin_multi_pack_index_write_options[] = {
@@ -165,7 +165,7 @@ static int cmd_multi_pack_index_write(int argc, const char **argv,
 
 		read_packs_from_stdin(&packs);
 
-		ret = write_midx_file_only(opts.object_dir, &packs,
+		ret = write_midx_file_only(repo, opts.object_dir, &packs,
 					   opts.preferred_pack,
 					   opts.refs_snapshot, opts.flags);
 
@@ -176,7 +176,7 @@ static int cmd_multi_pack_index_write(int argc, const char **argv,
 
 	}
 
-	ret = write_midx_file(opts.object_dir, opts.preferred_pack,
+	ret = write_midx_file(repo, opts.object_dir, opts.preferred_pack,
 			      opts.refs_snapshot, opts.flags);
 
 	free(opts.refs_snapshot);
diff --git a/builtin/repack.c b/builtin/repack.c
index 96a4fa234bddfd2b63c8d9733379d9b1012a4014..9c21fc482dfb387c818c0d0a74f781848b5a0953 100644
--- a/builtin/repack.c
+++ b/builtin/repack.c
@@ -1569,7 +1569,7 @@ int cmd_repack(int argc,
 		unsigned flags = 0;
 		if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_INCREMENTAL, 0))
 			flags |= MIDX_WRITE_INCREMENTAL;
-		write_midx_file(repo_get_object_directory(the_repository),
+		write_midx_file(the_repository, repo_get_object_directory(the_repository),
 				NULL, NULL, flags);
 	}
 
diff --git a/midx-write.c b/midx-write.c
index 1c355cdf8db4e9fed61a4aabf61a237ad26181ce..1bc2f5256916e69924245951f654c1047ffeab84 100644
--- a/midx-write.c
+++ b/midx-write.c
@@ -1,5 +1,3 @@
-#define USE_THE_REPOSITORY_VARIABLE
-
 #include "git-compat-util.h"
 #include "abspath.h"
 #include "config.h"
@@ -1505,24 +1503,22 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
 	return result;
 }
 
-int write_midx_file(const char *object_dir,
+int write_midx_file(struct repository *r, const char *object_dir,
 		    const char *preferred_pack_name,
-		    const char *refs_snapshot,
-		    unsigned flags)
+		    const char *refs_snapshot, unsigned flags)
 {
-	return write_midx_internal(the_repository, object_dir, NULL, NULL,
-				   preferred_pack_name, refs_snapshot, flags);
+	return write_midx_internal(r, object_dir, NULL, NULL,
+				   preferred_pack_name, refs_snapshot,
+				   flags);
 }
 
-int write_midx_file_only(const char *object_dir,
+int write_midx_file_only(struct repository *r, const char *object_dir,
 			 struct string_list *packs_to_include,
 			 const char *preferred_pack_name,
-			 const char *refs_snapshot,
-			 unsigned flags)
+			 const char *refs_snapshot, unsigned flags)
 {
-	return write_midx_internal(the_repository, object_dir, packs_to_include,
-				   NULL, preferred_pack_name, refs_snapshot,
-				   flags);
+	return write_midx_internal(r, object_dir, packs_to_include, NULL,
+				   preferred_pack_name, refs_snapshot, flags);
 }
 
 int expire_midx_packs(struct repository *r, const char *object_dir, unsigned flags)
diff --git a/midx.h b/midx.h
index 3b0ac4d8788b373c59fe69ca2d78e9d914702bc0..c37ad5b5242b56d21fd76bd59957a1bdb82786ec 100644
--- a/midx.h
+++ b/midx.h
@@ -123,15 +123,13 @@ int prepare_multi_pack_index_one(struct repository *r, const char *object_dir, i
  * Variant of write_midx_file which writes a MIDX containing only the packs
  * specified in packs_to_include.
  */
-int write_midx_file(const char *object_dir,
-		    const char *preferred_pack_name,
-		    const char *refs_snapshot,
+int write_midx_file(struct repository *r, const char *object_dir,
+		    const char *preferred_pack_name, const char *refs_snapshot,
 		    unsigned flags);
-int write_midx_file_only(const char *object_dir,
+int write_midx_file_only(struct repository *r, const char *object_dir,
 			 struct string_list *packs_to_include,
 			 const char *preferred_pack_name,
-			 const char *refs_snapshot,
-			 unsigned flags);
+			 const char *refs_snapshot, unsigned flags);
 void clear_midx_file(struct repository *r);
 int verify_midx_file(struct repository *r, const char *object_dir, unsigned flags);
 int expire_midx_packs(struct repository *r, const char *object_dir, unsigned flags);

-- 
2.47.0


^ permalink raw reply related	[flat|nested] 70+ messages in thread

* [PATCH v2 06/10] midx: cleanup internal usage of `the_repository` and `the_hash_algo`
  2024-11-19 15:36 ` [PATCH v2 00/10] " Karthik Nayak
                     ` (4 preceding siblings ...)
  2024-11-19 15:36   ` [PATCH v2 05/10] midx-write: pass down repository to `write_midx_file[_only]` Karthik Nayak
@ 2024-11-19 15:36   ` Karthik Nayak
  2024-11-19 15:36   ` [PATCH v2 07/10] midx: pass `repository` to `load_multi_pack_index` Karthik Nayak
                     ` (6 subsequent siblings)
  12 siblings, 0 replies; 70+ messages in thread
From: Karthik Nayak @ 2024-11-19 15:36 UTC (permalink / raw)
  To: git; +Cc: Karthik Nayak, ps, shejialuo

In the `midx.c` file, there are multiple usages of `the_repository` and
`the_hash_algo` within static functions of the file. Some of the usages
can be simply swapped out with the available `repository` struct. While
some of them can be swapped out by passing the repository to the
required functions.

This leaves out only some other usages of `the_repository` and
`the_hash_algo` in the file in non-static functions, which we'll tackle
in upcoming commits.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 midx.c | 49 +++++++++++++++++++++++++++----------------------
 1 file changed, 27 insertions(+), 22 deletions(-)

diff --git a/midx.c b/midx.c
index 079c45a1aafb658a7b887ac216cc6ecf5f0fb6ff..6f0fb8285af14843da132ef1b0c3a8bdd06732c9 100644
--- a/midx.c
+++ b/midx.c
@@ -25,7 +25,7 @@ int cmp_idx_or_pack_name(const char *idx_or_pack_name,
 
 const unsigned char *get_midx_checksum(struct multi_pack_index *m)
 {
-	return m->data + m->data_len - the_hash_algo->rawsz;
+	return m->data + m->data_len - m->repo->hash_algo->rawsz;
 }
 
 void get_midx_filename(struct strbuf *out, const char *object_dir)
@@ -94,7 +94,8 @@ static int midx_read_object_offsets(const unsigned char *chunk_start,
 
 #define MIDX_MIN_SIZE (MIDX_HEADER_SIZE + the_hash_algo->rawsz)
 
-static struct multi_pack_index *load_multi_pack_index_one(const char *object_dir,
+static struct multi_pack_index *load_multi_pack_index_one(struct repository *r,
+							  const char *object_dir,
 							  const char *midx_name,
 							  int local)
 {
@@ -131,7 +132,7 @@ static struct multi_pack_index *load_multi_pack_index_one(const char *object_dir
 	m->data = midx_map;
 	m->data_len = midx_size;
 	m->local = local;
-	m->repo = the_repository;
+	m->repo = r;
 
 	m->signature = get_be32(m->data);
 	if (m->signature != MIDX_SIGNATURE)
@@ -144,12 +145,12 @@ static struct multi_pack_index *load_multi_pack_index_one(const char *object_dir
 		      m->version);
 
 	hash_version = m->data[MIDX_BYTE_HASH_VERSION];
-	if (hash_version != oid_version(the_hash_algo)) {
+	if (hash_version != oid_version(r->hash_algo)) {
 		error(_("multi-pack-index hash version %u does not match version %u"),
-		      hash_version, oid_version(the_hash_algo));
+		      hash_version, oid_version(r->hash_algo));
 		goto cleanup_fail;
 	}
-	m->hash_len = the_hash_algo->rawsz;
+	m->hash_len = r->hash_algo->rawsz;
 
 	m->num_chunks = m->data[MIDX_BYTE_NUM_CHUNKS];
 
@@ -206,8 +207,8 @@ static struct multi_pack_index *load_multi_pack_index_one(const char *object_dir
 			      m->pack_names[i]);
 	}
 
-	trace2_data_intmax("midx", the_repository, "load/num_packs", m->num_packs);
-	trace2_data_intmax("midx", the_repository, "load/num_objects", m->num_objects);
+	trace2_data_intmax("midx", r, "load/num_packs", m->num_packs);
+	trace2_data_intmax("midx", r, "load/num_objects", m->num_objects);
 
 	free_chunkfile(cf);
 	return m;
@@ -240,8 +241,9 @@ void get_split_midx_filename_ext(struct strbuf *buf, const char *object_dir,
 	strbuf_addf(buf, "/multi-pack-index-%s.%s", hash_to_hex(hash), ext);
 }
 
-static int open_multi_pack_index_chain(const char *chain_file,
-				       int *fd, struct stat *st)
+static int open_multi_pack_index_chain(const struct git_hash_algo *hash_algo,
+				       const char *chain_file, int *fd,
+				       struct stat *st)
 {
 	*fd = git_open(chain_file);
 	if (*fd < 0)
@@ -250,7 +252,7 @@ static int open_multi_pack_index_chain(const char *chain_file,
 		close(*fd);
 		return 0;
 	}
-	if (st->st_size < the_hash_algo->hexsz) {
+	if (st->st_size < hash_algo->hexsz) {
 		close(*fd);
 		if (!st->st_size) {
 			/* treat empty files the same as missing */
@@ -292,7 +294,8 @@ static int add_midx_to_chain(struct multi_pack_index *midx,
 	return 1;
 }
 
-static struct multi_pack_index *load_midx_chain_fd_st(const char *object_dir,
+static struct multi_pack_index *load_midx_chain_fd_st(struct repository *r,
+						      const char *object_dir,
 						      int local,
 						      int fd, struct stat *st,
 						      int *incomplete_chain)
@@ -303,7 +306,7 @@ static struct multi_pack_index *load_midx_chain_fd_st(const char *object_dir,
 	uint32_t i, count;
 	FILE *fp = xfdopen(fd, "r");
 
-	count = st->st_size / (the_hash_algo->hexsz + 1);
+	count = st->st_size / (r->hash_algo->hexsz + 1);
 
 	for (i = 0; i < count; i++) {
 		struct multi_pack_index *m;
@@ -312,7 +315,7 @@ static struct multi_pack_index *load_midx_chain_fd_st(const char *object_dir,
 		if (strbuf_getline_lf(&buf, fp) == EOF)
 			break;
 
-		if (get_oid_hex(buf.buf, &layer)) {
+		if (get_oid_hex_algop(buf.buf, &layer, r->hash_algo)) {
 			warning(_("invalid multi-pack-index chain: line '%s' "
 				  "not a hash"),
 				buf.buf);
@@ -325,7 +328,7 @@ static struct multi_pack_index *load_midx_chain_fd_st(const char *object_dir,
 		strbuf_reset(&buf);
 		get_split_midx_filename_ext(&buf, object_dir, layer.hash,
 					    MIDX_EXT_MIDX);
-		m = load_multi_pack_index_one(object_dir, buf.buf, local);
+		m = load_multi_pack_index_one(r, object_dir, buf.buf, local);
 
 		if (m) {
 			if (add_midx_to_chain(m, midx_chain)) {
@@ -348,7 +351,8 @@ static struct multi_pack_index *load_midx_chain_fd_st(const char *object_dir,
 	return midx_chain;
 }
 
-static struct multi_pack_index *load_multi_pack_index_chain(const char *object_dir,
+static struct multi_pack_index *load_multi_pack_index_chain(struct repository *r,
+							    const char *object_dir,
 							    int local)
 {
 	struct strbuf chain_file = STRBUF_INIT;
@@ -357,10 +361,10 @@ static struct multi_pack_index *load_multi_pack_index_chain(const char *object_d
 	struct multi_pack_index *m = NULL;
 
 	get_midx_chain_filename(&chain_file, object_dir);
-	if (open_multi_pack_index_chain(chain_file.buf, &fd, &st)) {
+	if (open_multi_pack_index_chain(r->hash_algo, chain_file.buf, &fd, &st)) {
 		int incomplete;
 		/* ownership of fd is taken over by load function */
-		m = load_midx_chain_fd_st(object_dir, local, fd, &st,
+		m = load_midx_chain_fd_st(r, object_dir, local, fd, &st,
 					  &incomplete);
 	}
 
@@ -376,9 +380,10 @@ struct multi_pack_index *load_multi_pack_index(const char *object_dir,
 
 	get_midx_filename(&midx_name, object_dir);
 
-	m = load_multi_pack_index_one(object_dir, midx_name.buf, local);
+	m = load_multi_pack_index_one(the_repository, object_dir,
+				      midx_name.buf, local);
 	if (!m)
-		m = load_multi_pack_index_chain(object_dir, local);
+		m = load_multi_pack_index_chain(the_repository, object_dir, local);
 
 	strbuf_release(&midx_name);
 
@@ -520,7 +525,7 @@ int bsearch_one_midx(const struct object_id *oid, struct multi_pack_index *m,
 		     uint32_t *result)
 {
 	int ret = bsearch_hash(oid->hash, m->chunk_oid_fanout,
-			       m->chunk_oid_lookup, the_hash_algo->rawsz,
+			       m->chunk_oid_lookup, m->repo->hash_algo->rawsz,
 			       result);
 	if (result)
 		*result += m->num_objects_in_base;
@@ -551,7 +556,7 @@ struct object_id *nth_midxed_object_oid(struct object_id *oid,
 	n = midx_for_object(&m, n);
 
 	oidread(oid, m->chunk_oid_lookup + st_mult(m->hash_len, n),
-		the_repository->hash_algo);
+		m->repo->hash_algo);
 	return oid;
 }
 

-- 
2.47.0


^ permalink raw reply related	[flat|nested] 70+ messages in thread

* [PATCH v2 07/10] midx: pass `repository` to `load_multi_pack_index`
  2024-11-19 15:36 ` [PATCH v2 00/10] " Karthik Nayak
                     ` (5 preceding siblings ...)
  2024-11-19 15:36   ` [PATCH v2 06/10] midx: cleanup internal usage of `the_repository` and `the_hash_algo` Karthik Nayak
@ 2024-11-19 15:36   ` Karthik Nayak
  2024-11-19 15:36   ` [PATCH v2 08/10] midx: pass down `hash_algo` to `get_midx_filename[_ext]` Karthik Nayak
                     ` (5 subsequent siblings)
  12 siblings, 0 replies; 70+ messages in thread
From: Karthik Nayak @ 2024-11-19 15:36 UTC (permalink / raw)
  To: git; +Cc: Karthik Nayak, ps, shejialuo

The `load_multi_pack_index` function in midx uses `the_repository`
variable to access the `repository` struct. Modify the function and its
callee's to send the `repository` field.

This moves usage of `the_repository` to the `test-read-midx.c` file.
While that is not optimal, it is okay, since the upcoming commits will
slowly move the usage of `the_repository` up the layers and remove it
eventually.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 midx.c                    | 11 ++++++-----
 midx.h                    |  4 +++-
 t/helper/test-read-midx.c |  8 ++++----
 3 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/midx.c b/midx.c
index 6f0fb8285af14843da132ef1b0c3a8bdd06732c9..98ee84d4a8bf388906634ad695ff39acdaa2c6d5 100644
--- a/midx.c
+++ b/midx.c
@@ -372,7 +372,8 @@ static struct multi_pack_index *load_multi_pack_index_chain(struct repository *r
 	return m;
 }
 
-struct multi_pack_index *load_multi_pack_index(const char *object_dir,
+struct multi_pack_index *load_multi_pack_index(struct repository *r,
+					       const char *object_dir,
 					       int local)
 {
 	struct strbuf midx_name = STRBUF_INIT;
@@ -380,10 +381,10 @@ struct multi_pack_index *load_multi_pack_index(const char *object_dir,
 
 	get_midx_filename(&midx_name, object_dir);
 
-	m = load_multi_pack_index_one(the_repository, object_dir,
+	m = load_multi_pack_index_one(r, object_dir,
 				      midx_name.buf, local);
 	if (!m)
-		m = load_multi_pack_index_chain(the_repository, object_dir, local);
+		m = load_multi_pack_index_chain(r, object_dir, local);
 
 	strbuf_release(&midx_name);
 
@@ -727,7 +728,7 @@ int prepare_multi_pack_index_one(struct repository *r, const char *object_dir, i
 		if (!strcmp(object_dir, m_search->object_dir))
 			return 1;
 
-	m = load_multi_pack_index(object_dir, local);
+	m = load_multi_pack_index(r, object_dir, local);
 
 	if (m) {
 		struct multi_pack_index *mp = r->objects->multi_pack_index;
@@ -881,7 +882,7 @@ int verify_midx_file(struct repository *r, const char *object_dir, unsigned flag
 	struct pair_pos_vs_id *pairs = NULL;
 	uint32_t i;
 	struct progress *progress = NULL;
-	struct multi_pack_index *m = load_multi_pack_index(object_dir, 1);
+	struct multi_pack_index *m = load_multi_pack_index(r, object_dir, 1);
 	struct multi_pack_index *curr;
 	verify_midx_error = 0;
 
diff --git a/midx.h b/midx.h
index c37ad5b5242b56d21fd76bd59957a1bdb82786ec..78efa28d35371795fa33c68660278182debb60ab 100644
--- a/midx.h
+++ b/midx.h
@@ -97,7 +97,9 @@ void get_midx_chain_filename(struct strbuf *buf, const char *object_dir);
 void get_split_midx_filename_ext(struct strbuf *buf, const char *object_dir,
 				 const unsigned char *hash, const char *ext);
 
-struct multi_pack_index *load_multi_pack_index(const char *object_dir, int local);
+struct multi_pack_index *load_multi_pack_index(struct repository *r,
+					       const char *object_dir,
+					       int local);
 int prepare_midx_pack(struct repository *r, struct multi_pack_index *m, uint32_t pack_int_id);
 struct packed_git *nth_midxed_pack(struct multi_pack_index *m,
 				   uint32_t pack_int_id);
diff --git a/t/helper/test-read-midx.c b/t/helper/test-read-midx.c
index 438fb9fc6197fc465f79d9a65b719ae315fed373..fc632369618917e2d8cdcb77bd9073c61e1544c1 100644
--- a/t/helper/test-read-midx.c
+++ b/t/helper/test-read-midx.c
@@ -18,7 +18,7 @@ static int read_midx_file(const char *object_dir, const char *checksum,
 	struct multi_pack_index *m;
 
 	setup_git_directory();
-	m = load_multi_pack_index(object_dir, 1);
+	m = load_multi_pack_index(the_repository, object_dir, 1);
 
 	if (!m)
 		return 1;
@@ -82,7 +82,7 @@ static int read_midx_checksum(const char *object_dir)
 	struct multi_pack_index *m;
 
 	setup_git_directory();
-	m = load_multi_pack_index(object_dir, 1);
+	m = load_multi_pack_index(the_repository, object_dir, 1);
 	if (!m)
 		return 1;
 	printf("%s\n", hash_to_hex(get_midx_checksum(m)));
@@ -98,7 +98,7 @@ static int read_midx_preferred_pack(const char *object_dir)
 
 	setup_git_directory();
 
-	midx = load_multi_pack_index(object_dir, 1);
+	midx = load_multi_pack_index(the_repository, object_dir, 1);
 	if (!midx)
 		return 1;
 
@@ -121,7 +121,7 @@ static int read_midx_bitmapped_packs(const char *object_dir)
 
 	setup_git_directory();
 
-	midx = load_multi_pack_index(object_dir, 1);
+	midx = load_multi_pack_index(the_repository, object_dir, 1);
 	if (!midx)
 		return 1;
 

-- 
2.47.0


^ permalink raw reply related	[flat|nested] 70+ messages in thread

* [PATCH v2 08/10] midx: pass down `hash_algo` to `get_midx_filename[_ext]`
  2024-11-19 15:36 ` [PATCH v2 00/10] " Karthik Nayak
                     ` (6 preceding siblings ...)
  2024-11-19 15:36   ` [PATCH v2 07/10] midx: pass `repository` to `load_multi_pack_index` Karthik Nayak
@ 2024-11-19 15:36   ` Karthik Nayak
  2024-11-20 18:15     ` Christian Couder
  2024-11-19 15:36   ` [PATCH v2 09/10] midx: pass down `hash_algo` to `get_split_midx_filename_ext` Karthik Nayak
                     ` (4 subsequent siblings)
  12 siblings, 1 reply; 70+ messages in thread
From: Karthik Nayak @ 2024-11-19 15:36 UTC (permalink / raw)
  To: git; +Cc: Karthik Nayak, ps, shejialuo

The function `get_midx_filename_ext` uses `hash_to_hex` which internally
uses the global variable `the_repository`. To remove this dependency,
pass down the `hash_algo` to both `get_midx_filename` and
`get_midx_filename_ext`. This adds `the_repository` variable usage to
`midx-write.c`, which will be resolved 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 1bc2f5256916e69924245951f654c1047ffeab84..2f0c09211282fa651af8f9a342f0564729468306 100644
--- a/midx-write.c
+++ b/midx-write.c
@@ -991,8 +991,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);
 
@@ -1012,9 +1012,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)
 {
 	/*
@@ -1039,7 +1038,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);
 
@@ -1083,7 +1082,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);
@@ -1474,7 +1473,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


^ permalink raw reply related	[flat|nested] 70+ messages in thread

* [PATCH v2 09/10] midx: pass down `hash_algo` to `get_split_midx_filename_ext`
  2024-11-19 15:36 ` [PATCH v2 00/10] " Karthik Nayak
                     ` (7 preceding siblings ...)
  2024-11-19 15:36   ` [PATCH v2 08/10] midx: pass down `hash_algo` to `get_midx_filename[_ext]` Karthik Nayak
@ 2024-11-19 15:36   ` Karthik Nayak
  2024-11-20 18:15     ` Christian Couder
  2024-11-19 15:36   ` [PATCH v2 10/10] midx: inline the `MIDX_MIN_SIZE` definition Karthik Nayak
                     ` (3 subsequent siblings)
  12 siblings, 1 reply; 70+ messages in thread
From: Karthik Nayak @ 2024-11-19 15:36 UTC (permalink / raw)
  To: git; +Cc: Karthik Nayak, ps, shejialuo

Similar to the previous commit, pass down `hash_algo` to
`get_split_midx_filename_ext` and use `hash_to_hex_algop`.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 midx-write.c |  7 ++++---
 midx.c       | 10 ++++++----
 midx.h       |  3 ++-
 3 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/midx-write.c b/midx-write.c
index 2f0c09211282fa651af8f9a342f0564729468306..bcd1d50eb0f5c292c904a38f13279b1ebec9c855 100644
--- a/midx-write.c
+++ b/midx-write.c
@@ -993,7 +993,8 @@ static int link_midx_to_chain(struct multi_pack_index *m)
 
 		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,
+		get_split_midx_filename_ext(m->repo->hash_algo, &to,
+					    m->object_dir, hash,
 					    midx_exts[i].split);
 
 		if (link(from.buf, to.buf) < 0 && errno != ENOENT) {
@@ -1439,8 +1440,8 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
 		if (link_midx_to_chain(ctx.base_midx) < 0)
 			return -1;
 
-		get_split_midx_filename_ext(&final_midx_name, object_dir,
-					    midx_hash, MIDX_EXT_MIDX);
+		get_split_midx_filename_ext(r->hash_algo, &final_midx_name,
+					    object_dir, midx_hash, MIDX_EXT_MIDX);
 
 		if (rename_tempfile(&incr, final_midx_name.buf) < 0) {
 			error_errno(_("unable to rename new multi-pack-index layer"));
diff --git a/midx.c b/midx.c
index 9bed4185ff4d44602aedfe0329dd840ff9e85435..f45ea842cd6eda23d2eadc9deaae43839aef24c1 100644
--- a/midx.c
+++ b/midx.c
@@ -236,11 +236,13 @@ void get_midx_chain_filename(struct strbuf *buf, const char *object_dir)
 	strbuf_addstr(buf, "/multi-pack-index-chain");
 }
 
-void get_split_midx_filename_ext(struct strbuf *buf, const char *object_dir,
+void get_split_midx_filename_ext(const struct git_hash_algo *hash_algo,
+				 struct strbuf *buf, const char *object_dir,
 				 const unsigned char *hash, const char *ext)
 {
 	get_midx_chain_dirname(buf, object_dir);
-	strbuf_addf(buf, "/multi-pack-index-%s.%s", hash_to_hex(hash), ext);
+	strbuf_addf(buf, "/multi-pack-index-%s.%s",
+		    hash_to_hex_algop(hash, hash_algo), ext);
 }
 
 static int open_multi_pack_index_chain(const struct git_hash_algo *hash_algo,
@@ -328,8 +330,8 @@ static struct multi_pack_index *load_midx_chain_fd_st(struct repository *r,
 		valid = 0;
 
 		strbuf_reset(&buf);
-		get_split_midx_filename_ext(&buf, object_dir, layer.hash,
-					    MIDX_EXT_MIDX);
+		get_split_midx_filename_ext(r->hash_algo, &buf, object_dir,
+					    layer.hash, MIDX_EXT_MIDX);
 		m = load_multi_pack_index_one(r, object_dir, buf.buf, local);
 
 		if (m) {
diff --git a/midx.h b/midx.h
index 7620820d4d0272926af9e4eeb68bfb73404c7ec2..9d1374cbd58d016bb82338337b2a4e5ba7234092 100644
--- a/midx.h
+++ b/midx.h
@@ -97,7 +97,8 @@ void get_midx_filename_ext(const struct git_hash_algo *hash_algo,
 			   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);
-void get_split_midx_filename_ext(struct strbuf *buf, const char *object_dir,
+void get_split_midx_filename_ext(const struct git_hash_algo *hash_algo,
+				 struct strbuf *buf, const char *object_dir,
 				 const unsigned char *hash, const char *ext);
 
 struct multi_pack_index *load_multi_pack_index(struct repository *r,

-- 
2.47.0


^ permalink raw reply related	[flat|nested] 70+ messages in thread

* [PATCH v2 10/10] midx: inline the `MIDX_MIN_SIZE` definition
  2024-11-19 15:36 ` [PATCH v2 00/10] " Karthik Nayak
                     ` (8 preceding siblings ...)
  2024-11-19 15:36   ` [PATCH v2 09/10] midx: pass down `hash_algo` to `get_split_midx_filename_ext` Karthik Nayak
@ 2024-11-19 15:36   ` Karthik Nayak
  2024-11-20 13:13     ` shejialuo
  2024-11-20 18:20   ` [PATCH v2 00/10] Change midx.c and midx-write.c to not use global variables Christian Couder
                     ` (2 subsequent siblings)
  12 siblings, 1 reply; 70+ messages in thread
From: Karthik Nayak @ 2024-11-19 15:36 UTC (permalink / raw)
  To: git; +Cc: Karthik Nayak, ps, shejialuo

The `MIDX_MIN_SIZE` definition is used to check the midx_size in
`local_multi_pack_index_one`. This definitions relies on the
`the_hash_algo` global variable. Inline this and remove the global
variable usage.

With this, remove `USE_THE_REPOSITORY_VARIABLE` usage from `midx.c`.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 midx.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/midx.c b/midx.c
index f45ea842cd6eda23d2eadc9deaae43839aef24c1..e0eae1c25ec91f7db5670ff9bacdf0e195c35795 100644
--- a/midx.c
+++ b/midx.c
@@ -1,5 +1,3 @@
-#define USE_THE_REPOSITORY_VARIABLE
-
 #include "git-compat-util.h"
 #include "config.h"
 #include "dir.h"
@@ -94,8 +92,6 @@ static int midx_read_object_offsets(const unsigned char *chunk_start,
 	return 0;
 }
 
-#define MIDX_MIN_SIZE (MIDX_HEADER_SIZE + the_hash_algo->rawsz)
-
 static struct multi_pack_index *load_multi_pack_index_one(struct repository *r,
 							  const char *object_dir,
 							  const char *midx_name,
@@ -122,7 +118,7 @@ static struct multi_pack_index *load_multi_pack_index_one(struct repository *r,
 
 	midx_size = xsize_t(st.st_size);
 
-	if (midx_size < MIDX_MIN_SIZE) {
+	if (midx_size < (MIDX_HEADER_SIZE + r->hash_algo->rawsz)) {
 		error(_("multi-pack-index file %s is too small"), midx_name);
 		goto cleanup_fail;
 	}

-- 
2.47.0


^ permalink raw reply related	[flat|nested] 70+ messages in thread

* Re: [PATCH v2 03/10] midx-write: use `revs->repo` inside `read_refs_snapshot`
  2024-11-19 15:36   ` [PATCH v2 03/10] midx-write: use `revs->repo` inside `read_refs_snapshot` Karthik Nayak
@ 2024-11-20 12:58     ` shejialuo
  2024-11-20 14:26       ` Richard Kerry
  2024-11-20 19:44     ` Taylor Blau
  1 sibling, 1 reply; 70+ messages in thread
From: shejialuo @ 2024-11-20 12:58 UTC (permalink / raw)
  To: Karthik Nayak; +Cc: git, ps

On Tue, Nov 19, 2024 at 04:36:42PM +0100, Karthik Nayak wrote:
> The `read_refs_snapshot` uses the `parse_oid_hex` function which
> internally uses global variables. Let's instead use

Nit: s/variables/variable

> `parse_oid_hex_algop` and provide the hash algo via `revs->repo`.
> 
> Also, while here, fix a missing newline after the functions definition.
> 

Nit: s/functions/function

> Signed-off-by: Karthik Nayak <karthik.188@gmail.com>

^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v2 10/10] midx: inline the `MIDX_MIN_SIZE` definition
  2024-11-19 15:36   ` [PATCH v2 10/10] midx: inline the `MIDX_MIN_SIZE` definition Karthik Nayak
@ 2024-11-20 13:13     ` shejialuo
  2024-11-21 15:41       ` karthik nayak
  0 siblings, 1 reply; 70+ messages in thread
From: shejialuo @ 2024-11-20 13:13 UTC (permalink / raw)
  To: Karthik Nayak; +Cc: git, ps

On Tue, Nov 19, 2024 at 04:36:49PM +0100, Karthik Nayak wrote:
> The `MIDX_MIN_SIZE` definition is used to check the midx_size in
> `local_multi_pack_index_one`. This definitions relies on the

Nit: s/definitions/definition

> `the_hash_algo` global variable. Inline this and remove the global
> variable usage.
> 
> With this, remove `USE_THE_REPOSITORY_VARIABLE` usage from `midx.c`.
> 
> Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
> ---

^ permalink raw reply	[flat|nested] 70+ messages in thread

* RE: [PATCH v2 03/10] midx-write: use `revs->repo` inside `read_refs_snapshot`
  2024-11-20 12:58     ` shejialuo
@ 2024-11-20 14:26       ` Richard Kerry
  2024-11-20 19:46         ` Taylor Blau
  0 siblings, 1 reply; 70+ messages in thread
From: Richard Kerry @ 2024-11-20 14:26 UTC (permalink / raw)
  To: shejialuo, Karthik Nayak; +Cc: git@vger.kernel.org, ps@pks.im


>> The `read_refs_snapshot` uses the `parse_oid_hex` function which 
>> internally uses global variables. Let's instead use
>
>Nit: s/variables/variable

No, that's fine.
It's plural, so ends with 's'.
Unless it should be "uses a global variable"

>>
>> Also, while here, fix a missing newline after the functions definition.
>>
>
>Nit: s/functions/function

Maybe.
But it could be "the function's definition" as it could be seen as possessive.

Regards,
Richard.



^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v2 02/10] midx-write: pass down repository to static functions
  2024-11-19 15:36   ` [PATCH v2 02/10] midx-write: pass down repository to static functions Karthik Nayak
@ 2024-11-20 18:15     ` Christian Couder
  2024-11-20 19:41       ` Taylor Blau
  2024-11-21 14:57       ` karthik nayak
  2024-11-20 19:43     ` Taylor Blau
  1 sibling, 2 replies; 70+ messages in thread
From: Christian Couder @ 2024-11-20 18:15 UTC (permalink / raw)
  To: Karthik Nayak; +Cc: git, ps, shejialuo

On Tue, Nov 19, 2024 at 5:36 PM Karthik Nayak <karthik.188@gmail.com> wrote:
>
> In 'midx-write.c' there are a lot of static functions which use global
> variables `the_repository` or `the_hash_algo`. In a follow up commit,
> the repository variable will be added to `write_midx_context`, which
> some of the functions can use. But for functions which do not have
> access to this struct, pass down the required information from
> non-static functions `write_midx_file` and `write_midx_file_only`.
>
> This ensures that the usage of global variables is limited to these
> non-static functions, which will be cleaned up in a follow up commits.

s/commits/commit/

There are a few places in the patch where hash_to_hex() is replaced
with hash_to_hex_algop(). However hash_to_hex() is not quite a static
function and is not defined in 'midx-write.c'. So you might want to
mention this additional change in the commit message.

Thanks.

^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v2 08/10] midx: pass down `hash_algo` to `get_midx_filename[_ext]`
  2024-11-19 15:36   ` [PATCH v2 08/10] midx: pass down `hash_algo` to `get_midx_filename[_ext]` Karthik Nayak
@ 2024-11-20 18:15     ` Christian Couder
  2024-11-21 15:35       ` karthik nayak
  0 siblings, 1 reply; 70+ messages in thread
From: Christian Couder @ 2024-11-20 18:15 UTC (permalink / raw)
  To: Karthik Nayak; +Cc: git, ps, shejialuo

On Tue, Nov 19, 2024 at 4:39 PM Karthik Nayak <karthik.188@gmail.com> wrote:
>
> The function `get_midx_filename_ext` uses `hash_to_hex` which internally
> uses the global variable `the_repository`. To remove this dependency,
> pass down the `hash_algo` to both `get_midx_filename` and
> `get_midx_filename_ext`.

Technically passing down the `hash_algo` to both functions is not
enough to remove the dependency, as replacing `hash_to_hex()` with
`hash_to_hex_algop*()` is also needed. It's a bit unclear if
`hash_to_hex()` calls are replaced with other calls in this patch or
not though.

So you might want to add something like: "Replacing `hash_to_hex` with
functions that take an `hash_algo` as argument will be done in a
followup commit."


> This adds `the_repository` variable usage to
> `midx-write.c`, which will be resolved in a future commit.

^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v2 09/10] midx: pass down `hash_algo` to `get_split_midx_filename_ext`
  2024-11-19 15:36   ` [PATCH v2 09/10] midx: pass down `hash_algo` to `get_split_midx_filename_ext` Karthik Nayak
@ 2024-11-20 18:15     ` Christian Couder
  2024-11-20 22:23       ` Taylor Blau
  0 siblings, 1 reply; 70+ messages in thread
From: Christian Couder @ 2024-11-20 18:15 UTC (permalink / raw)
  To: Karthik Nayak; +Cc: git, ps, shejialuo

On Tue, Nov 19, 2024 at 4:47 PM Karthik Nayak <karthik.188@gmail.com> wrote:
>
> Similar to the previous commit, pass down `hash_algo` to
> `get_split_midx_filename_ext` and use `hash_to_hex_algop`.

I think we prefer explanations in commit messages to be a bit more
self-contained, and not to rely much only on other commit messages, or
links to a mailing list thread on a mailing list archive, or other
external content. It's better to err on the side of copying more.

^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v2 00/10] Change midx.c and midx-write.c to not use global variables
  2024-11-19 15:36 ` [PATCH v2 00/10] " Karthik Nayak
                     ` (9 preceding siblings ...)
  2024-11-19 15:36   ` [PATCH v2 10/10] midx: inline the `MIDX_MIN_SIZE` definition Karthik Nayak
@ 2024-11-20 18:20   ` Christian Couder
  2024-11-20 22:24     ` Taylor Blau
  2024-11-21  2:19   ` Junio C Hamano
  2024-11-27 16:28   ` [PATCH v3 0/8] " Karthik Nayak
  12 siblings, 1 reply; 70+ messages in thread
From: Christian Couder @ 2024-11-20 18:20 UTC (permalink / raw)
  To: Karthik Nayak; +Cc: git, ps, shejialuo

On Tue, Nov 19, 2024 at 4:38 PM Karthik Nayak <karthik.188@gmail.com> wrote:
>
> Similar to the earlier patch series on cleaning up packfile.c and
> removing usage of global variables [1], we change the midx.c and
> midx-write.c files to no longer use global variables.
>
> This is done by the following:
>   - Usage of repository variable already available in existing structs.
>   - Passing down repository variable from other subsystems.
>   - Modifying all subcommands to obtain repository variable from the
>   command in `builtins/` and passing down the variable from there.
>
> The biggest change is in the first commit, wherein we modify all
> subcommands to add the repository variable. Since the subcommand
> definition are not often changed, it shouldn't cause too many conflicts
> with in flight topics.

Overall I like the way it's done. I found a few improvements that
could be made to some commit messages though.

Thanks.

^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v2 02/10] midx-write: pass down repository to static functions
  2024-11-20 18:15     ` Christian Couder
@ 2024-11-20 19:41       ` Taylor Blau
  2024-11-21 14:57       ` karthik nayak
  1 sibling, 0 replies; 70+ messages in thread
From: Taylor Blau @ 2024-11-20 19:41 UTC (permalink / raw)
  To: Christian Couder; +Cc: Karthik Nayak, git, ps, shejialuo

On Wed, Nov 20, 2024 at 07:15:04PM +0100, Christian Couder wrote:
> On Tue, Nov 19, 2024 at 5:36 PM Karthik Nayak <karthik.188@gmail.com> wrote:
> >
> > In 'midx-write.c' there are a lot of static functions which use global
> > variables `the_repository` or `the_hash_algo`. In a follow up commit,
> > the repository variable will be added to `write_midx_context`, which
> > some of the functions can use. But for functions which do not have
> > access to this struct, pass down the required information from
> > non-static functions `write_midx_file` and `write_midx_file_only`.
> >
> > This ensures that the usage of global variables is limited to these
> > non-static functions, which will be cleaned up in a follow up commits.
>
> s/commits/commit/

Good eyes.

> There are a few places in the patch where hash_to_hex() is replaced
> with hash_to_hex_algop(). However hash_to_hex() is not quite a static
> function and is not defined in 'midx-write.c'. So you might want to
> mention this additional change in the commit message.

I think this is fine to leave out personally. Since hash_to_hex()
relies on the_hash_algo only, it's natural to expect that when a caller
has access to a new variable pointing at a 'struct git_hash_algo' that
they would convert that function to hash_to_hex_algop().

Thanks,
Taylor

^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v2 02/10] midx-write: pass down repository to static functions
  2024-11-19 15:36   ` [PATCH v2 02/10] midx-write: pass down repository to static functions Karthik Nayak
  2024-11-20 18:15     ` Christian Couder
@ 2024-11-20 19:43     ` Taylor Blau
  2024-11-21 15:06       ` karthik nayak
  1 sibling, 1 reply; 70+ messages in thread
From: Taylor Blau @ 2024-11-20 19:43 UTC (permalink / raw)
  To: Karthik Nayak; +Cc: git, ps, shejialuo

On Tue, Nov 19, 2024 at 04:36:41PM +0100, Karthik Nayak wrote:
> ---
>  midx-write.c | 57 +++++++++++++++++++++++++++++++--------------------------
>  1 file changed, 31 insertions(+), 26 deletions(-)

This patch all seems reasonable to me. When reviewing it in this way, it
is a little unclear to see which functions need just a repository, just
a hash_algo, or the whole write_midx_context structure.

I think I might have erred on the side of just passing the
write_midx_context structure throughout the midx-write.c internals. That
exposes more of the implementation details than is strictly necessary,
but it does so to the benefit of making the code (and this patch) easier
to follow.

I don't feel strongly about it, and I trust that you modified the right
functions to pass the right data. I just wanted to mention it in case
you hadn't considered an alternative way to structure this patch.

Thanks,
Taylor

^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v2 03/10] midx-write: use `revs->repo` inside `read_refs_snapshot`
  2024-11-19 15:36   ` [PATCH v2 03/10] midx-write: use `revs->repo` inside `read_refs_snapshot` Karthik Nayak
  2024-11-20 12:58     ` shejialuo
@ 2024-11-20 19:44     ` Taylor Blau
  1 sibling, 0 replies; 70+ messages in thread
From: Taylor Blau @ 2024-11-20 19:44 UTC (permalink / raw)
  To: Karthik Nayak; +Cc: git, ps, shejialuo

On Tue, Nov 19, 2024 at 04:36:42PM +0100, Karthik Nayak wrote:
> ---
>  midx-write.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/midx-write.c b/midx-write.c
> index 22b5233f51ec6c6d99b8f9613818f1581dca5982..564438f616f59cd24edda956e4af0e0acf167138 100644
> --- a/midx-write.c
> +++ b/midx-write.c
> @@ -760,7 +760,7 @@ static int read_refs_snapshot(const char *refs_snapshot,
>  			hex = &buf.buf[1];
>  		}
>
> -		if (parse_oid_hex(hex, &oid, &end) < 0)
> +		if (parse_oid_hex_algop(hex, &oid, &end, revs->repo->hash_algo) < 0)

Looks obviously correct.

>  			die(_("could not parse line: %s"), buf.buf);
>  		if (*end)
>  			die(_("malformed line: %s"), buf.buf);
> @@ -776,6 +776,7 @@ static int read_refs_snapshot(const char *refs_snapshot,
>  	strbuf_release(&buf);
>  	return 0;
>  }
> +

Good spotting :-).

Thanks,
Taylor

^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v2 03/10] midx-write: use `revs->repo` inside `read_refs_snapshot`
  2024-11-20 14:26       ` Richard Kerry
@ 2024-11-20 19:46         ` Taylor Blau
  2024-11-21 15:30           ` karthik nayak
  0 siblings, 1 reply; 70+ messages in thread
From: Taylor Blau @ 2024-11-20 19:46 UTC (permalink / raw)
  To: Richard Kerry; +Cc: shejialuo, Karthik Nayak, git@vger.kernel.org, ps@pks.im

On Wed, Nov 20, 2024 at 02:26:23PM +0000, Richard Kerry wrote:
>
> >> The `read_refs_snapshot` uses the `parse_oid_hex` function which
> >> internally uses global variables. Let's instead use
> >
> >Nit: s/variables/variable
>
> No, that's fine.
> It's plural, so ends with 's'.
> Unless it should be "uses a global variable"

The global variable in question here is just "the_hash_algo", so I think
shejialuo's suggestion to use "variable" is correct, but it would need
to be "uses a global variable" instead of "uses global variable"
(without the article).

But I think we're being unnecessarily vague here, and could instead say:

    The function `read_refs_snapshot()` uses `parse_oid_hex()`, which
    relies on the global `the_hash_algo` variable. Let's instead use
    [...]

> >> Also, while here, fix a missing newline after the functions definition.
> >>
> >
> >Nit: s/functions/function
>
> Maybe.
> But it could be "the function's definition" as it could be seen as possessive.

It should be "function's definition", as the possessive is the correct
form.

Thanks,
Taylor

^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v2 05/10] midx-write: pass down repository to `write_midx_file[_only]`
  2024-11-19 15:36   ` [PATCH v2 05/10] midx-write: pass down repository to `write_midx_file[_only]` Karthik Nayak
@ 2024-11-20 20:11     ` Taylor Blau
  0 siblings, 0 replies; 70+ messages in thread
From: Taylor Blau @ 2024-11-20 20:11 UTC (permalink / raw)
  To: Karthik Nayak; +Cc: git, ps, shejialuo

On Tue, Nov 19, 2024 at 04:36:44PM +0100, Karthik Nayak wrote:
> With this, all usage of global variables in `midx-write.c` is removed,
> hence, remove the `USE_THE_REPOSITORY_VARIABLE` guard from the file.

Very nicely done :-). Thanks for cleaning these up!

Thanks,
Taylor

^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v2 09/10] midx: pass down `hash_algo` to `get_split_midx_filename_ext`
  2024-11-20 18:15     ` Christian Couder
@ 2024-11-20 22:23       ` Taylor Blau
  2024-11-21 15:41         ` karthik nayak
  0 siblings, 1 reply; 70+ messages in thread
From: Taylor Blau @ 2024-11-20 22:23 UTC (permalink / raw)
  To: Christian Couder; +Cc: Karthik Nayak, git, ps, shejialuo

On Wed, Nov 20, 2024 at 07:15:40PM +0100, Christian Couder wrote:
> On Tue, Nov 19, 2024 at 4:47 PM Karthik Nayak <karthik.188@gmail.com> wrote:
> >
> > Similar to the previous commit, pass down `hash_algo` to
> > `get_split_midx_filename_ext` and use `hash_to_hex_algop`.
>
> I think we prefer explanations in commit messages to be a bit more
> self-contained, and not to rely much only on other commit messages, or
> links to a mailing list thread on a mailing list archive, or other
> external content. It's better to err on the side of copying more.

I agree, but in this instance think it would be better to simply
combine the two into one. I do not see a reason why these commits should
be split.

Thanks,
Taylor

^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v2 00/10] Change midx.c and midx-write.c to not use global variables
  2024-11-20 18:20   ` [PATCH v2 00/10] Change midx.c and midx-write.c to not use global variables Christian Couder
@ 2024-11-20 22:24     ` Taylor Blau
  2024-11-21  0:09       ` Junio C Hamano
  0 siblings, 1 reply; 70+ messages in thread
From: Taylor Blau @ 2024-11-20 22:24 UTC (permalink / raw)
  To: Christian Couder; +Cc: Karthik Nayak, git, ps, shejialuo

On Wed, Nov 20, 2024 at 07:20:33PM +0100, Christian Couder wrote:
> On Tue, Nov 19, 2024 at 4:38 PM Karthik Nayak <karthik.188@gmail.com> wrote:
> >
> > Similar to the earlier patch series on cleaning up packfile.c and
> > removing usage of global variables [1], we change the midx.c and
> > midx-write.c files to no longer use global variables.
> >
> > This is done by the following:
> >   - Usage of repository variable already available in existing structs.
> >   - Passing down repository variable from other subsystems.
> >   - Modifying all subcommands to obtain repository variable from the
> >   command in `builtins/` and passing down the variable from there.
> >
> > The biggest change is in the first commit, wherein we modify all
> > subcommands to add the repository variable. Since the subcommand
> > definition are not often changed, it shouldn't cause too many conflicts
> > with in flight topics.
>
> Overall I like the way it's done. I found a few improvements that
> could be made to some commit messages though.

Same here!

Thanks,
Taylor

^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v2 00/10] Change midx.c and midx-write.c to not use global variables
  2024-11-20 22:24     ` Taylor Blau
@ 2024-11-21  0:09       ` Junio C Hamano
  0 siblings, 0 replies; 70+ messages in thread
From: Junio C Hamano @ 2024-11-21  0:09 UTC (permalink / raw)
  To: Taylor Blau; +Cc: Christian Couder, Karthik Nayak, git, ps, shejialuo

Taylor Blau <me@ttaylorr.com> writes:

> On Wed, Nov 20, 2024 at 07:20:33PM +0100, Christian Couder wrote:
>
>> Overall I like the way it's done. I found a few improvements that
>> could be made to some commit messages though.
>
> Same here!

Thanks, all.  It seems to me that we are getting fairly close to the
goal post on this topic?


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v2 00/10] Change midx.c and midx-write.c to not use global variables
  2024-11-19 15:36 ` [PATCH v2 00/10] " Karthik Nayak
                     ` (10 preceding siblings ...)
  2024-11-20 18:20   ` [PATCH v2 00/10] Change midx.c and midx-write.c to not use global variables Christian Couder
@ 2024-11-21  2:19   ` Junio C Hamano
  2024-11-22 10:25     ` karthik nayak
  2024-11-27 16:28   ` [PATCH v3 0/8] " Karthik Nayak
  12 siblings, 1 reply; 70+ messages in thread
From: Junio C Hamano @ 2024-11-21  2:19 UTC (permalink / raw)
  To: Karthik Nayak; +Cc: git, ps, shejialuo, Taylor Blau

Karthik Nayak <karthik.188@gmail.com> writes:

> Since the `packfile.c` cleanup is still in flight, this series is based
> on top of master: b31fb630c0 (Merge https://github.com/j6t/git-gui,
> 2024-11-11) with those patches merged in. This applies cleanly on top 
> of next, but conflicts with `bf/set-head-symref` in seen, the conflict
> is mostly straight forward. I'll merge the topic in if it is merged into
> next soon.

I think set_head() part is fairly trivial.  But there seems to be a
much bigger conflicts with the incremental midx topic.

I actually do not understand offhand why doing anything to the midx
layer needs to change the calling convention of set_head() or many
other things that are unrelated to what midx layer does, and that do
*not* use the new parameter *anyway*.  Shouldn't the "subsubcommand
can inherit the repository from the builtin subcommand
implementation" be split out as a separate topic, which the midx
thing later builds on?

Thanks.

^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v2 02/10] midx-write: pass down repository to static functions
  2024-11-20 18:15     ` Christian Couder
  2024-11-20 19:41       ` Taylor Blau
@ 2024-11-21 14:57       ` karthik nayak
  1 sibling, 0 replies; 70+ messages in thread
From: karthik nayak @ 2024-11-21 14:57 UTC (permalink / raw)
  To: Christian Couder; +Cc: git, ps, shejialuo

[-- Attachment #1: Type: text/plain, Size: 1178 bytes --]

Christian Couder <christian.couder@gmail.com> writes:

> On Tue, Nov 19, 2024 at 5:36 PM Karthik Nayak <karthik.188@gmail.com> wrote:
>>
>> In 'midx-write.c' there are a lot of static functions which use global
>> variables `the_repository` or `the_hash_algo`. In a follow up commit,
>> the repository variable will be added to `write_midx_context`, which
>> some of the functions can use. But for functions which do not have
>> access to this struct, pass down the required information from
>> non-static functions `write_midx_file` and `write_midx_file_only`.
>>
>> This ensures that the usage of global variables is limited to these
>> non-static functions, which will be cleaned up in a follow up commits.
>
> s/commits/commit/
>

Will fix.

> There are a few places in the patch where hash_to_hex() is replaced
> with hash_to_hex_algop(). However hash_to_hex() is not quite a static
> function and is not defined in 'midx-write.c'. So you might want to
> mention this additional change in the commit message.
>

While I think it is obvious, that might not be the case, so I'll add in
a line to explain why we swap those functions.

> Thanks.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 690 bytes --]

^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v2 02/10] midx-write: pass down repository to static functions
  2024-11-20 19:43     ` Taylor Blau
@ 2024-11-21 15:06       ` karthik nayak
  0 siblings, 0 replies; 70+ messages in thread
From: karthik nayak @ 2024-11-21 15:06 UTC (permalink / raw)
  To: Taylor Blau; +Cc: git, ps, shejialuo

[-- Attachment #1: Type: text/plain, Size: 1441 bytes --]

Taylor Blau <me@ttaylorr.com> writes:

> On Tue, Nov 19, 2024 at 04:36:41PM +0100, Karthik Nayak wrote:
>> ---
>>  midx-write.c | 57 +++++++++++++++++++++++++++++++--------------------------
>>  1 file changed, 31 insertions(+), 26 deletions(-)
>
> This patch all seems reasonable to me. When reviewing it in this way, it
> is a little unclear to see which functions need just a repository, just
> a hash_algo, or the whole write_midx_context structure.
>
> I think I might have erred on the side of just passing the
> write_midx_context structure throughout the midx-write.c internals. That
> exposes more of the implementation details than is strictly necessary,
> but it does so to the benefit of making the code (and this patch) easier
> to follow.
>
> I don't feel strongly about it, and I trust that you modified the right
> functions to pass the right data. I just wanted to mention it in case
> you hadn't considered an alternative way to structure this patch.
>

I remember doing something like that and it seemed a lot more noisy than
the current iteration.

Regarding `write_midx_context`, I wonder if all these functions need to
receive it. Passing only what’s necessary might make the functions'
requirements clearer, instead of bundling everything into a large
struct.

But then again, if you feel otherwise, I'm happy to change it, I don't
feel so strongly about it :)

> Thanks,
> Taylor

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 690 bytes --]

^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v2 03/10] midx-write: use `revs->repo` inside `read_refs_snapshot`
  2024-11-20 19:46         ` Taylor Blau
@ 2024-11-21 15:30           ` karthik nayak
  0 siblings, 0 replies; 70+ messages in thread
From: karthik nayak @ 2024-11-21 15:30 UTC (permalink / raw)
  To: Taylor Blau, Richard Kerry; +Cc: shejialuo, git@vger.kernel.org, ps@pks.im

[-- Attachment #1: Type: text/plain, Size: 1237 bytes --]

Taylor Blau <me@ttaylorr.com> writes:

> On Wed, Nov 20, 2024 at 02:26:23PM +0000, Richard Kerry wrote:
>>
>> >> The `read_refs_snapshot` uses the `parse_oid_hex` function which
>> >> internally uses global variables. Let's instead use
>> >
>> >Nit: s/variables/variable
>>
>> No, that's fine.
>> It's plural, so ends with 's'.
>> Unless it should be "uses a global variable"
>
> The global variable in question here is just "the_hash_algo", so I think
> shejialuo's suggestion to use "variable" is correct, but it would need
> to be "uses a global variable" instead of "uses global variable"
> (without the article).
>
> But I think we're being unnecessarily vague here, and could instead say:
>
>     The function `read_refs_snapshot()` uses `parse_oid_hex()`, which
>     relies on the global `the_hash_algo` variable. Let's instead use
>     [...]
>
>> >> Also, while here, fix a missing newline after the functions definition.
>> >>
>> >
>> >Nit: s/functions/function
>>
>> Maybe.
>> But it could be "the function's definition" as it could be seen as possessive.
>
> It should be "function's definition", as the possessive is the correct
> form.
>

Will fix all of this and re-do the commit message, thanks all.

> Thanks,
> Taylor

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 690 bytes --]

^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v2 08/10] midx: pass down `hash_algo` to `get_midx_filename[_ext]`
  2024-11-20 18:15     ` Christian Couder
@ 2024-11-21 15:35       ` karthik nayak
  0 siblings, 0 replies; 70+ messages in thread
From: karthik nayak @ 2024-11-21 15:35 UTC (permalink / raw)
  To: Christian Couder; +Cc: git, ps, shejialuo

[-- Attachment #1: Type: text/plain, Size: 1287 bytes --]

Christian Couder <christian.couder@gmail.com> writes:

> On Tue, Nov 19, 2024 at 4:39 PM Karthik Nayak <karthik.188@gmail.com> wrote:
>>
>> The function `get_midx_filename_ext` uses `hash_to_hex` which internally
>> uses the global variable `the_repository`. To remove this dependency,
>> pass down the `hash_algo` to both `get_midx_filename` and
>> `get_midx_filename_ext`.
>
> Technically passing down the `hash_algo` to both functions is not
> enough to remove the dependency, as replacing `hash_to_hex()` with
> `hash_to_hex_algop*()` is also needed. It's a bit unclear if
> `hash_to_hex()` calls are replaced with other calls in this patch or
> not though.
>

I see what you mean, this patch itself swaps `hash_to_hex()` with
`hash_to_hex_algop()`, but only in `get_midx_filename[_ext]`.

> So you might want to add something like: "Replacing `hash_to_hex` with
> functions that take an `hash_algo` as argument will be done in a
> followup commit."

While we also do something similar in the next patch, I'll amend this
commit message to be less vague and clarify the intention. Thanks.

>
>> This adds `the_repository` variable usage to
>> `midx-write.c`, which will be resolved in a future commit.

I think this is stale too and will remove it ;)

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 690 bytes --]

^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v2 09/10] midx: pass down `hash_algo` to `get_split_midx_filename_ext`
  2024-11-20 22:23       ` Taylor Blau
@ 2024-11-21 15:41         ` karthik nayak
  0 siblings, 0 replies; 70+ messages in thread
From: karthik nayak @ 2024-11-21 15:41 UTC (permalink / raw)
  To: Taylor Blau, Christian Couder; +Cc: git, ps, shejialuo

[-- Attachment #1: Type: text/plain, Size: 873 bytes --]

Taylor Blau <me@ttaylorr.com> writes:

> On Wed, Nov 20, 2024 at 07:15:40PM +0100, Christian Couder wrote:
>> On Tue, Nov 19, 2024 at 4:47 PM Karthik Nayak <karthik.188@gmail.com> wrote:
>> >
>> > Similar to the previous commit, pass down `hash_algo` to
>> > `get_split_midx_filename_ext` and use `hash_to_hex_algop`.
>>
>> I think we prefer explanations in commit messages to be a bit more
>> self-contained, and not to rely much only on other commit messages, or
>> links to a mailing list thread on a mailing list archive, or other
>> external content. It's better to err on the side of copying more.
>
> I agree, but in this instance think it would be better to simply
> combine the two into one. I do not see a reason why these commits should
> be split.
>

Fair enough, let me combine the two commits and rewrite the message to
make it clearer.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 690 bytes --]

^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v2 10/10] midx: inline the `MIDX_MIN_SIZE` definition
  2024-11-20 13:13     ` shejialuo
@ 2024-11-21 15:41       ` karthik nayak
  0 siblings, 0 replies; 70+ messages in thread
From: karthik nayak @ 2024-11-21 15:41 UTC (permalink / raw)
  To: shejialuo; +Cc: git, ps

[-- Attachment #1: Type: text/plain, Size: 293 bytes --]

shejialuo <shejialuo@gmail.com> writes:

> On Tue, Nov 19, 2024 at 04:36:49PM +0100, Karthik Nayak wrote:
>> The `MIDX_MIN_SIZE` definition is used to check the midx_size in
>> `local_multi_pack_index_one`. This definitions relies on the
>
> Nit: s/definitions/definition
>

Thanks, will fix!

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 690 bytes --]

^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v2 00/10] Change midx.c and midx-write.c to not use global variables
  2024-11-21  2:19   ` Junio C Hamano
@ 2024-11-22 10:25     ` karthik nayak
  0 siblings, 0 replies; 70+ messages in thread
From: karthik nayak @ 2024-11-22 10:25 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, ps, shejialuo, Taylor Blau

[-- Attachment #1: Type: text/plain, Size: 1472 bytes --]

Junio C Hamano <gitster@pobox.com> writes:

> Karthik Nayak <karthik.188@gmail.com> writes:
>
>> Since the `packfile.c` cleanup is still in flight, this series is based
>> on top of master: b31fb630c0 (Merge https://github.com/j6t/git-gui,
>> 2024-11-11) with those patches merged in. This applies cleanly on top
>> of next, but conflicts with `bf/set-head-symref` in seen, the conflict
>> is mostly straight forward. I'll merge the topic in if it is merged into
>> next soon.
>
> I think set_head() part is fairly trivial.  But there seems to be a
> much bigger conflicts with the incremental midx topic.
>

I see that you've merged the topic to seen and the merge resolution
looks ok to me, I'll also merge in `tb/incremental-midx-part-2` for my
next version.

> I actually do not understand offhand why doing anything to the midx
> layer needs to change the calling convention of set_head() or many
> other things that are unrelated to what midx layer does, and that do
> *not* use the new parameter *anyway*.  Shouldn't the "subsubcommand
> can inherit the repository from the builtin subcommand
> implementation" be split out as a separate topic, which the midx
> thing later builds on?
>

That was just the progression of how I worked on these patches. My goal
was to remove global variables, the subsubcommand can inherit just was a
dependency which I bundled along, since it was a single patch.

There is no reason it cannot be split, Let me go ahead and do that.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 690 bytes --]

^ permalink raw reply	[flat|nested] 70+ messages in thread

* [PATCH v3 0/8] Change midx.c and midx-write.c to not use global variables
  2024-11-19 15:36 ` [PATCH v2 00/10] " Karthik Nayak
                     ` (11 preceding siblings ...)
  2024-11-21  2:19   ` Junio C Hamano
@ 2024-11-27 16:28   ` Karthik Nayak
  2024-11-27 16:28     ` [PATCH v3 1/8] midx-write: pass down repository to static functions Karthik Nayak
                       ` (8 more replies)
  12 siblings, 9 replies; 70+ messages in thread
From: Karthik Nayak @ 2024-11-27 16:28 UTC (permalink / raw)
  To: git; +Cc: Karthik Nayak, me, shejialuo, gitster, Christian Couder

Similar to the earlier patch series on cleaning up packfile.c and
removing usage of global variables [1], we change the midx.c and
midx-write.c files to no longer use global variables.

This is done by the following:
  - Usage of repository variable already available in existing structs.
  - Passing down repository variable from other subsystems.
  - Modifying all subcommands to obtain repository variable from the
  command in `builtins/` and passing down the variable from there.

The biggest change is in the first commit, wherein we modify all
subcommands to add the repository variable. Since the subcommand
definition are not often changed, it shouldn't cause too many conflicts
with in flight topics.

Since the `packfile.c` cleanup is still in flight, this series is based
on top of master: b31fb630c0 (Merge https://github.com/j6t/git-gui,
2024-11-11) with those patches merged in.

Since v3 this series also depends on
'kn/pass-repo-to-builtin-sub-sub-commands', which was split out from
this series.

[1]: https://lore.kernel.org/git/cover.1729504640.git.karthik.188@gmail.com/

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
Changes in v3:
- Split out the first commit into a separate series [1].
- Improved some of the commit messages to be more descriptive.
- Merged the 8th and 9th commits together, since they were similar.
- v2: https://lore.kernel.org/r/20241119-374-refactor-midx-c-and-midx-write-c-to-not-depend-on-global-state-v2-0-e2f607174efc@gmail.com

Changes in v2:
- Split commit modifying static functions in `midx-write.c` to multiple
commits, which makes it easier to review.
- Remove usage of `the_repository` in `test-parse-options.c` and use
NULL instead.
- Fix the commit messages to be imperative.
- Fix error in commit sign off.
- v1: https://lore.kernel.org/r/20241115-374-refactor-midx-c-and-midx-write-c-to-not-depend-on-global-state-v1-0-761f8a2c7775@gmail.com

[1]: https://lore.kernel.org/git/xmqq34jdyey3.fsf@gitster.g/T/#t

---
Karthik Nayak (8):
      midx-write: pass down repository to static functions
      midx-write: use `revs->repo` inside `read_refs_snapshot`
      write-midx: add repository field to `write_midx_context`
      midx-write: pass down repository to `write_midx_file[_only]`
      midx: cleanup internal usage of `the_repository` and `the_hash_algo`
      midx: pass `repository` to `load_multi_pack_index`
      midx: pass down `hash_algo` to functions using global variables
      midx: inline the `MIDX_MIN_SIZE` definition

 builtin/multi-pack-index.c |   6 +--
 builtin/repack.c           |   2 +-
 midx-write.c               | 130 +++++++++++++++++++++++----------------------
 midx.c                     |  88 ++++++++++++++++--------------
 midx.h                     |  24 +++++----
 pack-bitmap.c              |   6 +--
 pack-revindex.c            |   2 +-
 t/helper/test-read-midx.c  |   8 +--
 8 files changed, 140 insertions(+), 126 deletions(-)
---

Range-diff versus v2:

 1:  6c00e25c86 <  -:  ---------- packfile: add repository to struct `packed_git`
 2:  70fc8a79af <  -:  ---------- packfile: use `repository` from `packed_git` directly
 3:  167a1f3a11 <  -:  ---------- packfile: pass `repository` to static function in the file
 4:  b7cfe78217 <  -:  ---------- packfile: pass down repository to `odb_pack_name`
 5:  5566f5554c <  -:  ---------- packfile: pass down repository to `has_object[_kept]_pack`
 6:  1b26e45a9b <  -:  ---------- packfile: pass down repository to `for_each_packed_object`
 7:  1bdc34f4d8 <  -:  ---------- config: make `delta_base_cache_limit` a non-global variable
 8:  7b6baa89ac <  -:  ---------- config: make `packed_git_(limit|window_size)` non-global variables
 9:  a3667d87ec <  -:  ---------- midx: add repository to `multi_pack_index` struct
10:  91a35c4876 <  -:  ---------- builtin: pass repository to sub commands
11:  a916fe708f !  1:  d7c1056ebd midx-write: pass down repository to static functions
    @@ Commit message
         access to this struct, pass down the required information from
         non-static functions `write_midx_file` and `write_midx_file_only`.
     
    +    This requires that the function `hash_to_hex` is also replaced with
    +    `hash_to_hex_algop` since the former internally accesses the
    +    `the_hash_algo` global variable.
    +
         This ensures that the usage of global variables is limited to these
    -    non-static functions, which will be cleaned up in a follow up commits.
    +    non-static functions, which will be cleaned up in a follow up commit.
     
         Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
     
12:  a2fa59eca5 !  2:  11cd6c6bec midx-write: use `revs->repo` inside `read_refs_snapshot`
    @@ Metadata
      ## Commit message ##
         midx-write: use `revs->repo` inside `read_refs_snapshot`
     
    -    The `read_refs_snapshot` uses the `parse_oid_hex` function which
    -    internally uses global variables. Let's instead use
    -    `parse_oid_hex_algop` and provide the hash algo via `revs->repo`.
    +    The function `read_refs_snapshot()` uses `parse_oid_hex()`, which relies
    +    on the global `the_hash_algo` variable. Let's instead use
    +    `parse_oid_hex_algop()` and provide the hash algo via `revs->repo`.
     
    -    Also, while here, fix a missing newline after the functions definition.
    +    Also, while here, fix a missing newline after the function's definition.
     
         Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
     
13:  bc6ff987c5 =  3:  d74bf1fa51 write-midx: add repository field to `write_midx_context`
14:  27affa11fb =  4:  630bffe661 midx-write: pass down repository to `write_midx_file[_only]`
15:  02484793f6 =  5:  32d001bdd6 midx: cleanup internal usage of `the_repository` and `the_hash_algo`
16:  790215da01 =  6:  392a96de6a midx: pass `repository` to `load_multi_pack_index`
17:  8eac01e7ac !  7:  35bdc6d6b1 midx: pass down `hash_algo` to `get_midx_filename[_ext]`
    @@ Metadata
     Author: Karthik Nayak <karthik.188@gmail.com>
     
      ## Commit message ##
    -    midx: pass down `hash_algo` to `get_midx_filename[_ext]`
    +    midx: pass down `hash_algo` to functions using global variables
     
    -    The function `get_midx_filename_ext` uses `hash_to_hex` which internally
    -    uses the global variable `the_repository`. To remove this dependency,
    -    pass down the `hash_algo` to both `get_midx_filename` and
    -    `get_midx_filename_ext`. This adds `the_repository` variable usage to
    -    `midx-write.c`, which will be resolved in a future commit.
    +    The functions `get_split_midx_filename_ext()`, `get_midx_filename()` and
    +    `get_midx_filename_ext()` use `hash_to_hex()` which internally uses the
    +    `the_hash_algo` global variable.
    +
    +    Remove this dependency on global variables by passing down the
    +    `hash_algo` through to the functions mentioned and instead calling
    +    `hash_to_hex_algop()` along with the obtained `hash_algo`.
     
         Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
     
    @@ midx-write.c: static int link_midx_to_chain(struct multi_pack_index *m)
      
     -		get_midx_filename_ext(&from, m->object_dir, hash,
     -				      midx_exts[i].non_split);
    +-		get_split_midx_filename_ext(&to, m->object_dir, hash,
     +		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,
    ++		get_split_midx_filename_ext(m->repo->hash_algo, &to,
    ++					    m->object_dir, hash,
      					    midx_exts[i].split);
      
    + 		if (link(from.buf, to.buf) < 0 && errno != ENOENT) {
     @@ midx-write.c: static int link_midx_to_chain(struct multi_pack_index *m)
      	return ret;
      }
    @@ midx-write.c: static int write_midx_internal(struct repository *r, const char *o
      	if (safe_create_leading_directories(midx_name.buf))
      		die_errno(_("unable to create leading directories of %s"),
      			  midx_name.buf);
    +@@ midx-write.c: static int write_midx_internal(struct repository *r, const char *object_dir,
    + 		if (link_midx_to_chain(ctx.base_midx) < 0)
    + 			return -1;
    + 
    +-		get_split_midx_filename_ext(&final_midx_name, object_dir,
    +-					    midx_hash, MIDX_EXT_MIDX);
    ++		get_split_midx_filename_ext(r->hash_algo, &final_midx_name,
    ++					    object_dir, midx_hash, MIDX_EXT_MIDX);
    + 
    + 		if (rename_tempfile(&incr, final_midx_name.buf) < 0) {
    + 			error_errno(_("unable to rename new multi-pack-index layer"));
     @@ midx-write.c: 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"));
    @@ midx.c: const unsigned char *get_midx_checksum(struct multi_pack_index *m)
      }
      
      static int midx_read_oid_fanout(const unsigned char *chunk_start,
    +@@ midx.c: void get_midx_chain_filename(struct strbuf *buf, const char *object_dir)
    + 	strbuf_addstr(buf, "/multi-pack-index-chain");
    + }
    + 
    +-void get_split_midx_filename_ext(struct strbuf *buf, const char *object_dir,
    ++void get_split_midx_filename_ext(const struct git_hash_algo *hash_algo,
    ++				 struct strbuf *buf, const char *object_dir,
    + 				 const unsigned char *hash, const char *ext)
    + {
    + 	get_midx_chain_dirname(buf, object_dir);
    +-	strbuf_addf(buf, "/multi-pack-index-%s.%s", hash_to_hex(hash), ext);
    ++	strbuf_addf(buf, "/multi-pack-index-%s.%s",
    ++		    hash_to_hex_algop(hash, hash_algo), ext);
    + }
    + 
    + static int open_multi_pack_index_chain(const struct git_hash_algo *hash_algo,
    +@@ midx.c: static struct multi_pack_index *load_midx_chain_fd_st(struct repository *r,
    + 		valid = 0;
    + 
    + 		strbuf_reset(&buf);
    +-		get_split_midx_filename_ext(&buf, object_dir, layer.hash,
    +-					    MIDX_EXT_MIDX);
    ++		get_split_midx_filename_ext(r->hash_algo, &buf, object_dir,
    ++					    layer.hash, MIDX_EXT_MIDX);
    + 		m = load_multi_pack_index_one(r, object_dir, buf.buf, local);
    + 
    + 		if (m) {
     @@ midx.c: struct multi_pack_index *load_multi_pack_index(struct repository *r,
      	struct strbuf midx_name = STRBUF_INIT;
      	struct multi_pack_index *m;
    @@ midx.h: struct multi_pack_index {
      			   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);
    +-void get_split_midx_filename_ext(struct strbuf *buf, const char *object_dir,
    ++void get_split_midx_filename_ext(const struct git_hash_algo *hash_algo,
    ++				 struct strbuf *buf, const char *object_dir,
    + 				 const unsigned char *hash, const char *ext);
    + 
    + struct multi_pack_index *load_multi_pack_index(struct repository *r,
     
      ## pack-bitmap.c ##
     @@ pack-bitmap.c: static int load_bitmap_entries_v1(struct bitmap_index *index)
18:  3357d47ec0 <  -:  ---------- midx: pass down `hash_algo` to `get_split_midx_filename_ext`
19:  6a1ae9d11c !  8:  128f4f08b0 midx: inline the `MIDX_MIN_SIZE` definition
    @@ Commit message
         midx: inline the `MIDX_MIN_SIZE` definition
     
         The `MIDX_MIN_SIZE` definition is used to check the midx_size in
    -    `local_multi_pack_index_one`. This definitions relies on the
    +    `local_multi_pack_index_one`. This definition relies on the
         `the_hash_algo` global variable. Inline this and remove the global
         variable usage.
     


--- 

base-commit: b31fb630c0fc6869a33ed717163e8a1210460d94
change-id: 20241111-374-refactor-midx-c-and-midx-write-c-to-not-depend-on-global-state-a88498c2590f

Thanks
- Karthik


^ permalink raw reply	[flat|nested] 70+ messages in thread

* [PATCH v3 1/8] midx-write: pass down repository to static functions
  2024-11-27 16:28   ` [PATCH v3 0/8] " Karthik Nayak
@ 2024-11-27 16:28     ` Karthik Nayak
  2024-11-27 16:28     ` [PATCH v3 2/8] midx-write: use `revs->repo` inside `read_refs_snapshot` Karthik Nayak
                       ` (7 subsequent siblings)
  8 siblings, 0 replies; 70+ messages in thread
From: Karthik Nayak @ 2024-11-27 16:28 UTC (permalink / raw)
  To: git; +Cc: Karthik Nayak, me, shejialuo, gitster, Christian Couder

In 'midx-write.c' there are a lot of static functions which use global
variables `the_repository` or `the_hash_algo`. In a follow up commit,
the repository variable will be added to `write_midx_context`, which
some of the functions can use. But for functions which do not have
access to this struct, pass down the required information from
non-static functions `write_midx_file` and `write_midx_file_only`.

This requires that the function `hash_to_hex` is also replaced with
`hash_to_hex_algop` since the former internally accesses the
`the_hash_algo` global variable.

This ensures that the usage of global variables is limited to these
non-static functions, which will be cleaned up in a follow up commit.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 midx-write.c | 57 +++++++++++++++++++++++++++++++--------------------------
 1 file changed, 31 insertions(+), 26 deletions(-)

diff --git a/midx-write.c b/midx-write.c
index c57726ef9475df693890d61627ce91409c1def7c..22b5233f51ec6c6d99b8f9613818f1581dca5982 100644
--- a/midx-write.c
+++ b/midx-write.c
@@ -35,13 +35,13 @@ extern void clear_incremental_midx_files_ext(const char *object_dir,
 extern int cmp_idx_or_pack_name(const char *idx_or_pack_name,
 				const char *idx_name);
 
-static size_t write_midx_header(struct hashfile *f,
-				unsigned char num_chunks,
+static size_t write_midx_header(const struct git_hash_algo *hash_algo,
+				struct hashfile *f, unsigned char num_chunks,
 				uint32_t num_packs)
 {
 	hashwrite_be32(f, MIDX_SIGNATURE);
 	hashwrite_u8(f, MIDX_VERSION);
-	hashwrite_u8(f, oid_version(the_hash_algo));
+	hashwrite_u8(f, oid_version(hash_algo));
 	hashwrite_u8(f, num_chunks);
 	hashwrite_u8(f, 0); /* unused */
 	hashwrite_be32(f, num_packs);
@@ -702,7 +702,7 @@ static int add_ref_to_pending(const char *refname, const char *referent UNUSED,
 		return 0;
 	}
 
-	if (!peel_iterated_oid(the_repository, oid, &peeled))
+	if (!peel_iterated_oid(revs->repo, oid, &peeled))
 		oid = &peeled;
 
 	object = parse_object_or_die(oid, refname);
@@ -827,7 +827,7 @@ static struct commit **find_commits_for_midx_bitmap(uint32_t *indexed_commits_nr
 	return cb.commits;
 }
 
-static int write_midx_bitmap(const char *midx_name,
+static int write_midx_bitmap(struct repository *r, const char *midx_name,
 			     const unsigned char *midx_hash,
 			     struct packing_data *pdata,
 			     struct commit **commits,
@@ -840,9 +840,9 @@ static int write_midx_bitmap(const char *midx_name,
 	struct bitmap_writer writer;
 	struct pack_idx_entry **index;
 	char *bitmap_name = xstrfmt("%s-%s.bitmap", midx_name,
-					hash_to_hex(midx_hash));
+				    hash_to_hex_algop(midx_hash, r->hash_algo));
 
-	trace2_region_enter("midx", "write_midx_bitmap", the_repository);
+	trace2_region_enter("midx", "write_midx_bitmap", r);
 
 	if (flags & MIDX_WRITE_BITMAP_HASH_CACHE)
 		options |= BITMAP_OPT_HASH_CACHE;
@@ -859,7 +859,7 @@ static int write_midx_bitmap(const char *midx_name,
 	for (i = 0; i < pdata->nr_objects; i++)
 		index[i] = &pdata->objects[i].idx;
 
-	bitmap_writer_init(&writer, the_repository, pdata);
+	bitmap_writer_init(&writer, r, pdata);
 	bitmap_writer_show_progress(&writer, flags & MIDX_PROGRESS);
 	bitmap_writer_build_type_index(&writer, index);
 
@@ -892,7 +892,7 @@ static int write_midx_bitmap(const char *midx_name,
 	free(bitmap_name);
 	bitmap_writer_free(&writer);
 
-	trace2_region_leave("midx", "write_midx_bitmap", the_repository);
+	trace2_region_leave("midx", "write_midx_bitmap", r);
 
 	return ret;
 }
@@ -1049,7 +1049,7 @@ static void clear_midx_files(const char *object_dir,
 	strbuf_release(&buf);
 }
 
-static int write_midx_internal(const char *object_dir,
+static int write_midx_internal(struct repository *r, const char *object_dir,
 			       struct string_list *packs_to_include,
 			       struct string_list *packs_to_drop,
 			       const char *preferred_pack_name,
@@ -1070,7 +1070,8 @@ static int write_midx_internal(const char *object_dir,
 	const char **keep_hashes = NULL;
 	struct chunkfile *cf;
 
-	trace2_region_enter("midx", "write_midx_internal", the_repository);
+	trace2_region_enter("midx", "write_midx_internal", r);
+
 
 	ctx.incremental = !!(flags & MIDX_WRITE_INCREMENTAL);
 	if (ctx.incremental && (flags & MIDX_WRITE_BITMAP))
@@ -1087,8 +1088,7 @@ static int write_midx_internal(const char *object_dir,
 			  midx_name.buf);
 
 	if (!packs_to_include || ctx.incremental) {
-		struct multi_pack_index *m = lookup_multi_pack_index(the_repository,
-								     object_dir);
+		struct multi_pack_index *m = lookup_multi_pack_index(r, object_dir);
 		if (m && !midx_checksum_valid(m)) {
 			warning(_("ignoring existing multi-pack-index; checksum mismatch"));
 			m = NULL;
@@ -1351,7 +1351,7 @@ static int write_midx_internal(const char *object_dir,
 	add_chunk(cf, MIDX_CHUNKID_OIDFANOUT, MIDX_CHUNK_FANOUT_SIZE,
 		  write_midx_oid_fanout);
 	add_chunk(cf, MIDX_CHUNKID_OIDLOOKUP,
-		  st_mult(ctx.entries_nr, the_hash_algo->rawsz),
+		  st_mult(ctx.entries_nr, r->hash_algo->rawsz),
 		  write_midx_oid_lookup);
 	add_chunk(cf, MIDX_CHUNKID_OBJECTOFFSETS,
 		  st_mult(ctx.entries_nr, MIDX_CHUNK_OFFSET_WIDTH),
@@ -1373,7 +1373,8 @@ static int write_midx_internal(const char *object_dir,
 			  write_midx_bitmapped_packs);
 	}
 
-	write_midx_header(f, get_num_chunks(cf), ctx.nr - dropped_packs);
+	write_midx_header(r->hash_algo, f, get_num_chunks(cf),
+			  ctx.nr - dropped_packs);
 	write_chunkfile(cf, &ctx);
 
 	finalize_hashfile(f, midx_hash, FSYNC_COMPONENT_PACK_METADATA,
@@ -1405,7 +1406,7 @@ static int write_midx_internal(const char *object_dir,
 		FREE_AND_NULL(ctx.entries);
 		ctx.entries_nr = 0;
 
-		if (write_midx_bitmap(midx_name.buf, midx_hash, &pdata,
+		if (write_midx_bitmap(r, midx_name.buf, midx_hash, &pdata,
 				      commits, commits_nr, ctx.pack_order,
 				      flags) < 0) {
 			error(_("could not write multi-pack bitmap"));
@@ -1449,12 +1450,13 @@ static int write_midx_internal(const char *object_dir,
 		strbuf_release(&final_midx_name);
 
 		keep_hashes[ctx.num_multi_pack_indexes_before] =
-			xstrdup(hash_to_hex(midx_hash));
+			xstrdup(hash_to_hex_algop(midx_hash, r->hash_algo));
 
 		for (i = 0; i < ctx.num_multi_pack_indexes_before; i++) {
 			uint32_t j = ctx.num_multi_pack_indexes_before - i - 1;
 
-			keep_hashes[j] = xstrdup(hash_to_hex(get_midx_checksum(m)));
+			keep_hashes[j] = xstrdup(hash_to_hex_algop(get_midx_checksum(m),
+								   r->hash_algo));
 			m = m->base_midx;
 		}
 
@@ -1462,7 +1464,7 @@ static int write_midx_internal(const char *object_dir,
 			fprintf(get_lock_file_fp(&lk), "%s\n", keep_hashes[i]);
 	} else {
 		keep_hashes[ctx.num_multi_pack_indexes_before] =
-			xstrdup(hash_to_hex(midx_hash));
+			xstrdup(hash_to_hex_algop(midx_hash, r->hash_algo));
 	}
 
 	if (ctx.m || ctx.base_midx)
@@ -1495,7 +1497,7 @@ static int write_midx_internal(const char *object_dir,
 	}
 	strbuf_release(&midx_name);
 
-	trace2_region_leave("midx", "write_midx_internal", the_repository);
+	trace2_region_leave("midx", "write_midx_internal", r);
 
 	return result;
 }
@@ -1505,8 +1507,8 @@ int write_midx_file(const char *object_dir,
 		    const char *refs_snapshot,
 		    unsigned flags)
 {
-	return write_midx_internal(object_dir, NULL, NULL, preferred_pack_name,
-				   refs_snapshot, flags);
+	return write_midx_internal(the_repository, object_dir, NULL, NULL,
+				   preferred_pack_name, refs_snapshot, flags);
 }
 
 int write_midx_file_only(const char *object_dir,
@@ -1515,8 +1517,9 @@ int write_midx_file_only(const char *object_dir,
 			 const char *refs_snapshot,
 			 unsigned flags)
 {
-	return write_midx_internal(object_dir, packs_to_include, NULL,
-				   preferred_pack_name, refs_snapshot, flags);
+	return write_midx_internal(the_repository, object_dir, packs_to_include,
+				   NULL, preferred_pack_name, refs_snapshot,
+				   flags);
 }
 
 int expire_midx_packs(struct repository *r, const char *object_dir, unsigned flags)
@@ -1572,7 +1575,8 @@ int expire_midx_packs(struct repository *r, const char *object_dir, unsigned fla
 	free(count);
 
 	if (packs_to_drop.nr)
-		result = write_midx_internal(object_dir, NULL, &packs_to_drop, NULL, NULL, flags);
+		result = write_midx_internal(r, object_dir, NULL,
+					     &packs_to_drop, NULL, NULL, flags);
 
 	string_list_clear(&packs_to_drop, 0);
 
@@ -1769,7 +1773,8 @@ int midx_repack(struct repository *r, const char *object_dir, size_t batch_size,
 		goto cleanup;
 	}
 
-	result = write_midx_internal(object_dir, NULL, NULL, NULL, NULL, flags);
+	result = write_midx_internal(r, object_dir, NULL, NULL, NULL, NULL,
+				     flags);
 
 cleanup:
 	free(include_pack);

-- 
2.47.1


^ permalink raw reply related	[flat|nested] 70+ messages in thread

* [PATCH v3 2/8] midx-write: use `revs->repo` inside `read_refs_snapshot`
  2024-11-27 16:28   ` [PATCH v3 0/8] " Karthik Nayak
  2024-11-27 16:28     ` [PATCH v3 1/8] midx-write: pass down repository to static functions Karthik Nayak
@ 2024-11-27 16:28     ` Karthik Nayak
  2024-11-27 16:28     ` [PATCH v3 3/8] write-midx: add repository field to `write_midx_context` Karthik Nayak
                       ` (6 subsequent siblings)
  8 siblings, 0 replies; 70+ messages in thread
From: Karthik Nayak @ 2024-11-27 16:28 UTC (permalink / raw)
  To: git; +Cc: Karthik Nayak, me, shejialuo, gitster, Christian Couder

The function `read_refs_snapshot()` uses `parse_oid_hex()`, which relies
on the global `the_hash_algo` variable. Let's instead use
`parse_oid_hex_algop()` and provide the hash algo via `revs->repo`.

Also, while here, fix a missing newline after the function's definition.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 midx-write.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/midx-write.c b/midx-write.c
index 22b5233f51ec6c6d99b8f9613818f1581dca5982..564438f616f59cd24edda956e4af0e0acf167138 100644
--- a/midx-write.c
+++ b/midx-write.c
@@ -760,7 +760,7 @@ static int read_refs_snapshot(const char *refs_snapshot,
 			hex = &buf.buf[1];
 		}
 
-		if (parse_oid_hex(hex, &oid, &end) < 0)
+		if (parse_oid_hex_algop(hex, &oid, &end, revs->repo->hash_algo) < 0)
 			die(_("could not parse line: %s"), buf.buf);
 		if (*end)
 			die(_("malformed line: %s"), buf.buf);
@@ -776,6 +776,7 @@ static int read_refs_snapshot(const char *refs_snapshot,
 	strbuf_release(&buf);
 	return 0;
 }
+
 static struct commit **find_commits_for_midx_bitmap(uint32_t *indexed_commits_nr_p,
 						    const char *refs_snapshot,
 						    struct write_midx_context *ctx)

-- 
2.47.1


^ permalink raw reply related	[flat|nested] 70+ messages in thread

* [PATCH v3 3/8] write-midx: add repository field to `write_midx_context`
  2024-11-27 16:28   ` [PATCH v3 0/8] " Karthik Nayak
  2024-11-27 16:28     ` [PATCH v3 1/8] midx-write: pass down repository to static functions Karthik Nayak
  2024-11-27 16:28     ` [PATCH v3 2/8] midx-write: use `revs->repo` inside `read_refs_snapshot` Karthik Nayak
@ 2024-11-27 16:28     ` Karthik Nayak
  2024-11-27 16:28     ` [PATCH v3 4/8] midx-write: pass down repository to `write_midx_file[_only]` Karthik Nayak
                       ` (5 subsequent siblings)
  8 siblings, 0 replies; 70+ messages in thread
From: Karthik Nayak @ 2024-11-27 16:28 UTC (permalink / raw)
  To: git; +Cc: Karthik Nayak, me, shejialuo, gitster, Christian Couder

The struct `write_midx_context` is used to pass context for creating
MIDX files. Add the repository field here to ensure that most functions
within `midx-write.c` have access to the field and can use that instead
of the global `the_repository` variable.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 midx-write.c | 38 ++++++++++++++++++++------------------
 1 file changed, 20 insertions(+), 18 deletions(-)

diff --git a/midx-write.c b/midx-write.c
index 564438f616f59cd24edda956e4af0e0acf167138..1c355cdf8db4e9fed61a4aabf61a237ad26181ce 100644
--- a/midx-write.c
+++ b/midx-write.c
@@ -110,6 +110,8 @@ struct write_midx_context {
 	uint32_t num_multi_pack_indexes_before;
 
 	struct string_list *to_include;
+
+	struct repository *repo;
 };
 
 static int should_include_pack(const struct write_midx_context *ctx,
@@ -154,7 +156,7 @@ static void add_pack_to_midx(const char *full_path, size_t full_path_len,
 			return;
 
 		ALLOC_GROW(ctx->info, ctx->nr + 1, ctx->alloc);
-		p = add_packed_git(the_repository, full_path, full_path_len, 0);
+		p = add_packed_git(ctx->repo, full_path, full_path_len, 0);
 		if (!p) {
 			warning(_("failed to add packfile '%s'"),
 				full_path);
@@ -480,7 +482,7 @@ static int write_midx_oid_lookup(struct hashfile *f,
 				 void *data)
 {
 	struct write_midx_context *ctx = data;
-	unsigned char hash_len = the_hash_algo->rawsz;
+	unsigned char hash_len = ctx->repo->hash_algo->rawsz;
 	struct pack_midx_entry *list = ctx->entries;
 	uint32_t i;
 
@@ -605,7 +607,7 @@ static uint32_t *midx_pack_order(struct write_midx_context *ctx)
 	uint32_t *pack_order, base_objects = 0;
 	uint32_t i;
 
-	trace2_region_enter("midx", "midx_pack_order", the_repository);
+	trace2_region_enter("midx", "midx_pack_order", ctx->repo);
 
 	if (ctx->incremental && ctx->base_midx)
 		base_objects = ctx->base_midx->num_objects +
@@ -640,7 +642,7 @@ static uint32_t *midx_pack_order(struct write_midx_context *ctx)
 	}
 	free(data);
 
-	trace2_region_leave("midx", "midx_pack_order", the_repository);
+	trace2_region_leave("midx", "midx_pack_order", ctx->repo);
 
 	return pack_order;
 }
@@ -651,9 +653,10 @@ static void write_midx_reverse_index(char *midx_name, unsigned char *midx_hash,
 	struct strbuf buf = STRBUF_INIT;
 	char *tmp_file;
 
-	trace2_region_enter("midx", "write_midx_reverse_index", the_repository);
+	trace2_region_enter("midx", "write_midx_reverse_index", ctx->repo);
 
-	strbuf_addf(&buf, "%s-%s.rev", midx_name, hash_to_hex(midx_hash));
+	strbuf_addf(&buf, "%s-%s.rev", midx_name, hash_to_hex_algop(midx_hash,
+								    ctx->repo->hash_algo));
 
 	tmp_file = write_rev_file_order(NULL, ctx->pack_order, ctx->entries_nr,
 					midx_hash, WRITE_REV);
@@ -664,7 +667,7 @@ static void write_midx_reverse_index(char *midx_name, unsigned char *midx_hash,
 	strbuf_release(&buf);
 	free(tmp_file);
 
-	trace2_region_leave("midx", "write_midx_reverse_index", the_repository);
+	trace2_region_leave("midx", "write_midx_reverse_index", ctx->repo);
 }
 
 static void prepare_midx_packing_data(struct packing_data *pdata,
@@ -672,10 +675,10 @@ static void prepare_midx_packing_data(struct packing_data *pdata,
 {
 	uint32_t i;
 
-	trace2_region_enter("midx", "prepare_midx_packing_data", the_repository);
+	trace2_region_enter("midx", "prepare_midx_packing_data", ctx->repo);
 
 	memset(pdata, 0, sizeof(struct packing_data));
-	prepare_packing_data(the_repository, pdata);
+	prepare_packing_data(ctx->repo, pdata);
 
 	for (i = 0; i < ctx->entries_nr; i++) {
 		uint32_t pos = ctx->pack_order[i];
@@ -686,7 +689,7 @@ static void prepare_midx_packing_data(struct packing_data *pdata,
 			       ctx->info[ctx->pack_perm[from->pack_int_id]].p);
 	}
 
-	trace2_region_leave("midx", "prepare_midx_packing_data", the_repository);
+	trace2_region_leave("midx", "prepare_midx_packing_data", ctx->repo);
 }
 
 static int add_ref_to_pending(const char *refname, const char *referent UNUSED,
@@ -784,17 +787,16 @@ static struct commit **find_commits_for_midx_bitmap(uint32_t *indexed_commits_nr
 	struct rev_info revs;
 	struct bitmap_commit_cb cb = {0};
 
-	trace2_region_enter("midx", "find_commits_for_midx_bitmap",
-			    the_repository);
+	trace2_region_enter("midx", "find_commits_for_midx_bitmap", ctx->repo);
 
 	cb.ctx = ctx;
 
-	repo_init_revisions(the_repository, &revs, NULL);
+	repo_init_revisions(ctx->repo, &revs, NULL);
 	if (refs_snapshot) {
 		read_refs_snapshot(refs_snapshot, &revs);
 	} else {
 		setup_revisions(0, NULL, &revs, NULL);
-		refs_for_each_ref(get_main_ref_store(the_repository),
+		refs_for_each_ref(get_main_ref_store(ctx->repo),
 				  add_ref_to_pending, &revs);
 	}
 
@@ -822,8 +824,7 @@ static struct commit **find_commits_for_midx_bitmap(uint32_t *indexed_commits_nr
 
 	release_revisions(&revs);
 
-	trace2_region_leave("midx", "find_commits_for_midx_bitmap",
-			    the_repository);
+	trace2_region_leave("midx", "find_commits_for_midx_bitmap", ctx->repo);
 
 	return cb.commits;
 }
@@ -945,7 +946,7 @@ static int fill_packs_from_midx(struct write_midx_context *ctx,
 			 */
 			if (flags & MIDX_WRITE_REV_INDEX ||
 			    preferred_pack_name) {
-				if (prepare_midx_pack(the_repository, m,
+				if (prepare_midx_pack(ctx->repo, m,
 						      m->num_packs_in_base + i)) {
 					error(_("could not load pack"));
 					return 1;
@@ -1073,6 +1074,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
 
 	trace2_region_enter("midx", "write_midx_internal", r);
 
+	ctx.repo = r;
 
 	ctx.incremental = !!(flags & MIDX_WRITE_INCREMENTAL);
 	if (ctx.incremental && (flags & MIDX_WRITE_BITMAP))
@@ -1469,7 +1471,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
 	}
 
 	if (ctx.m || ctx.base_midx)
-		close_object_store(the_repository->objects);
+		close_object_store(ctx.repo->objects);
 
 	if (commit_lock_file(&lk) < 0)
 		die_errno(_("could not write multi-pack-index"));

-- 
2.47.1


^ permalink raw reply related	[flat|nested] 70+ messages in thread

* [PATCH v3 4/8] midx-write: pass down repository to `write_midx_file[_only]`
  2024-11-27 16:28   ` [PATCH v3 0/8] " Karthik Nayak
                       ` (2 preceding siblings ...)
  2024-11-27 16:28     ` [PATCH v3 3/8] write-midx: add repository field to `write_midx_context` Karthik Nayak
@ 2024-11-27 16:28     ` Karthik Nayak
  2024-11-27 16:28     ` [PATCH v3 5/8] midx: cleanup internal usage of `the_repository` and `the_hash_algo` Karthik Nayak
                       ` (4 subsequent siblings)
  8 siblings, 0 replies; 70+ messages in thread
From: Karthik Nayak @ 2024-11-27 16:28 UTC (permalink / raw)
  To: git; +Cc: Karthik Nayak, me, shejialuo, gitster, Christian Couder

In a previous commit, we passed the repository field to all
subcommands in the `builtin/` directory. Utilize this to pass the
repository field down to the `write_midx_file[_only]` functions to
remove the usage of `the_repository` global variables.

With this, all usage of global variables in `midx-write.c` is removed,
hence, remove the `USE_THE_REPOSITORY_VARIABLE` guard from the file.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 builtin/multi-pack-index.c |  6 +++---
 builtin/repack.c           |  2 +-
 midx-write.c               | 22 +++++++++-------------
 midx.h                     | 10 ++++------
 4 files changed, 17 insertions(+), 23 deletions(-)

diff --git a/builtin/multi-pack-index.c b/builtin/multi-pack-index.c
index 85e40a4b6d3e47e9ec1ec27c094455e5ba75b5b0..2a938466f53aaa11096170554fe11a4ed46a25e4 100644
--- a/builtin/multi-pack-index.c
+++ b/builtin/multi-pack-index.c
@@ -120,7 +120,7 @@ static void read_packs_from_stdin(struct string_list *to)
 
 static int cmd_multi_pack_index_write(int argc, const char **argv,
 				      const char *prefix,
-				      struct repository *repo UNUSED)
+				      struct repository *repo)
 {
 	struct option *options;
 	static struct option builtin_multi_pack_index_write_options[] = {
@@ -165,7 +165,7 @@ static int cmd_multi_pack_index_write(int argc, const char **argv,
 
 		read_packs_from_stdin(&packs);
 
-		ret = write_midx_file_only(opts.object_dir, &packs,
+		ret = write_midx_file_only(repo, opts.object_dir, &packs,
 					   opts.preferred_pack,
 					   opts.refs_snapshot, opts.flags);
 
@@ -176,7 +176,7 @@ static int cmd_multi_pack_index_write(int argc, const char **argv,
 
 	}
 
-	ret = write_midx_file(opts.object_dir, opts.preferred_pack,
+	ret = write_midx_file(repo, opts.object_dir, opts.preferred_pack,
 			      opts.refs_snapshot, opts.flags);
 
 	free(opts.refs_snapshot);
diff --git a/builtin/repack.c b/builtin/repack.c
index 96a4fa234bddfd2b63c8d9733379d9b1012a4014..9c21fc482dfb387c818c0d0a74f781848b5a0953 100644
--- a/builtin/repack.c
+++ b/builtin/repack.c
@@ -1569,7 +1569,7 @@ int cmd_repack(int argc,
 		unsigned flags = 0;
 		if (git_env_bool(GIT_TEST_MULTI_PACK_INDEX_WRITE_INCREMENTAL, 0))
 			flags |= MIDX_WRITE_INCREMENTAL;
-		write_midx_file(repo_get_object_directory(the_repository),
+		write_midx_file(the_repository, repo_get_object_directory(the_repository),
 				NULL, NULL, flags);
 	}
 
diff --git a/midx-write.c b/midx-write.c
index 1c355cdf8db4e9fed61a4aabf61a237ad26181ce..1bc2f5256916e69924245951f654c1047ffeab84 100644
--- a/midx-write.c
+++ b/midx-write.c
@@ -1,5 +1,3 @@
-#define USE_THE_REPOSITORY_VARIABLE
-
 #include "git-compat-util.h"
 #include "abspath.h"
 #include "config.h"
@@ -1505,24 +1503,22 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
 	return result;
 }
 
-int write_midx_file(const char *object_dir,
+int write_midx_file(struct repository *r, const char *object_dir,
 		    const char *preferred_pack_name,
-		    const char *refs_snapshot,
-		    unsigned flags)
+		    const char *refs_snapshot, unsigned flags)
 {
-	return write_midx_internal(the_repository, object_dir, NULL, NULL,
-				   preferred_pack_name, refs_snapshot, flags);
+	return write_midx_internal(r, object_dir, NULL, NULL,
+				   preferred_pack_name, refs_snapshot,
+				   flags);
 }
 
-int write_midx_file_only(const char *object_dir,
+int write_midx_file_only(struct repository *r, const char *object_dir,
 			 struct string_list *packs_to_include,
 			 const char *preferred_pack_name,
-			 const char *refs_snapshot,
-			 unsigned flags)
+			 const char *refs_snapshot, unsigned flags)
 {
-	return write_midx_internal(the_repository, object_dir, packs_to_include,
-				   NULL, preferred_pack_name, refs_snapshot,
-				   flags);
+	return write_midx_internal(r, object_dir, packs_to_include, NULL,
+				   preferred_pack_name, refs_snapshot, flags);
 }
 
 int expire_midx_packs(struct repository *r, const char *object_dir, unsigned flags)
diff --git a/midx.h b/midx.h
index 3b0ac4d8788b373c59fe69ca2d78e9d914702bc0..c37ad5b5242b56d21fd76bd59957a1bdb82786ec 100644
--- a/midx.h
+++ b/midx.h
@@ -123,15 +123,13 @@ int prepare_multi_pack_index_one(struct repository *r, const char *object_dir, i
  * Variant of write_midx_file which writes a MIDX containing only the packs
  * specified in packs_to_include.
  */
-int write_midx_file(const char *object_dir,
-		    const char *preferred_pack_name,
-		    const char *refs_snapshot,
+int write_midx_file(struct repository *r, const char *object_dir,
+		    const char *preferred_pack_name, const char *refs_snapshot,
 		    unsigned flags);
-int write_midx_file_only(const char *object_dir,
+int write_midx_file_only(struct repository *r, const char *object_dir,
 			 struct string_list *packs_to_include,
 			 const char *preferred_pack_name,
-			 const char *refs_snapshot,
-			 unsigned flags);
+			 const char *refs_snapshot, unsigned flags);
 void clear_midx_file(struct repository *r);
 int verify_midx_file(struct repository *r, const char *object_dir, unsigned flags);
 int expire_midx_packs(struct repository *r, const char *object_dir, unsigned flags);

-- 
2.47.1


^ permalink raw reply related	[flat|nested] 70+ messages in thread

* [PATCH v3 5/8] midx: cleanup internal usage of `the_repository` and `the_hash_algo`
  2024-11-27 16:28   ` [PATCH v3 0/8] " Karthik Nayak
                       ` (3 preceding siblings ...)
  2024-11-27 16:28     ` [PATCH v3 4/8] midx-write: pass down repository to `write_midx_file[_only]` Karthik Nayak
@ 2024-11-27 16:28     ` Karthik Nayak
  2024-11-27 16:28     ` [PATCH v3 6/8] midx: pass `repository` to `load_multi_pack_index` Karthik Nayak
                       ` (3 subsequent siblings)
  8 siblings, 0 replies; 70+ messages in thread
From: Karthik Nayak @ 2024-11-27 16:28 UTC (permalink / raw)
  To: git; +Cc: Karthik Nayak, me, shejialuo, gitster, Christian Couder

In the `midx.c` file, there are multiple usages of `the_repository` and
`the_hash_algo` within static functions of the file. Some of the usages
can be simply swapped out with the available `repository` struct. While
some of them can be swapped out by passing the repository to the
required functions.

This leaves out only some other usages of `the_repository` and
`the_hash_algo` in the file in non-static functions, which we'll tackle
in upcoming commits.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 midx.c | 49 +++++++++++++++++++++++++++----------------------
 1 file changed, 27 insertions(+), 22 deletions(-)

diff --git a/midx.c b/midx.c
index 079c45a1aafb658a7b887ac216cc6ecf5f0fb6ff..6f0fb8285af14843da132ef1b0c3a8bdd06732c9 100644
--- a/midx.c
+++ b/midx.c
@@ -25,7 +25,7 @@ int cmp_idx_or_pack_name(const char *idx_or_pack_name,
 
 const unsigned char *get_midx_checksum(struct multi_pack_index *m)
 {
-	return m->data + m->data_len - the_hash_algo->rawsz;
+	return m->data + m->data_len - m->repo->hash_algo->rawsz;
 }
 
 void get_midx_filename(struct strbuf *out, const char *object_dir)
@@ -94,7 +94,8 @@ static int midx_read_object_offsets(const unsigned char *chunk_start,
 
 #define MIDX_MIN_SIZE (MIDX_HEADER_SIZE + the_hash_algo->rawsz)
 
-static struct multi_pack_index *load_multi_pack_index_one(const char *object_dir,
+static struct multi_pack_index *load_multi_pack_index_one(struct repository *r,
+							  const char *object_dir,
 							  const char *midx_name,
 							  int local)
 {
@@ -131,7 +132,7 @@ static struct multi_pack_index *load_multi_pack_index_one(const char *object_dir
 	m->data = midx_map;
 	m->data_len = midx_size;
 	m->local = local;
-	m->repo = the_repository;
+	m->repo = r;
 
 	m->signature = get_be32(m->data);
 	if (m->signature != MIDX_SIGNATURE)
@@ -144,12 +145,12 @@ static struct multi_pack_index *load_multi_pack_index_one(const char *object_dir
 		      m->version);
 
 	hash_version = m->data[MIDX_BYTE_HASH_VERSION];
-	if (hash_version != oid_version(the_hash_algo)) {
+	if (hash_version != oid_version(r->hash_algo)) {
 		error(_("multi-pack-index hash version %u does not match version %u"),
-		      hash_version, oid_version(the_hash_algo));
+		      hash_version, oid_version(r->hash_algo));
 		goto cleanup_fail;
 	}
-	m->hash_len = the_hash_algo->rawsz;
+	m->hash_len = r->hash_algo->rawsz;
 
 	m->num_chunks = m->data[MIDX_BYTE_NUM_CHUNKS];
 
@@ -206,8 +207,8 @@ static struct multi_pack_index *load_multi_pack_index_one(const char *object_dir
 			      m->pack_names[i]);
 	}
 
-	trace2_data_intmax("midx", the_repository, "load/num_packs", m->num_packs);
-	trace2_data_intmax("midx", the_repository, "load/num_objects", m->num_objects);
+	trace2_data_intmax("midx", r, "load/num_packs", m->num_packs);
+	trace2_data_intmax("midx", r, "load/num_objects", m->num_objects);
 
 	free_chunkfile(cf);
 	return m;
@@ -240,8 +241,9 @@ void get_split_midx_filename_ext(struct strbuf *buf, const char *object_dir,
 	strbuf_addf(buf, "/multi-pack-index-%s.%s", hash_to_hex(hash), ext);
 }
 
-static int open_multi_pack_index_chain(const char *chain_file,
-				       int *fd, struct stat *st)
+static int open_multi_pack_index_chain(const struct git_hash_algo *hash_algo,
+				       const char *chain_file, int *fd,
+				       struct stat *st)
 {
 	*fd = git_open(chain_file);
 	if (*fd < 0)
@@ -250,7 +252,7 @@ static int open_multi_pack_index_chain(const char *chain_file,
 		close(*fd);
 		return 0;
 	}
-	if (st->st_size < the_hash_algo->hexsz) {
+	if (st->st_size < hash_algo->hexsz) {
 		close(*fd);
 		if (!st->st_size) {
 			/* treat empty files the same as missing */
@@ -292,7 +294,8 @@ static int add_midx_to_chain(struct multi_pack_index *midx,
 	return 1;
 }
 
-static struct multi_pack_index *load_midx_chain_fd_st(const char *object_dir,
+static struct multi_pack_index *load_midx_chain_fd_st(struct repository *r,
+						      const char *object_dir,
 						      int local,
 						      int fd, struct stat *st,
 						      int *incomplete_chain)
@@ -303,7 +306,7 @@ static struct multi_pack_index *load_midx_chain_fd_st(const char *object_dir,
 	uint32_t i, count;
 	FILE *fp = xfdopen(fd, "r");
 
-	count = st->st_size / (the_hash_algo->hexsz + 1);
+	count = st->st_size / (r->hash_algo->hexsz + 1);
 
 	for (i = 0; i < count; i++) {
 		struct multi_pack_index *m;
@@ -312,7 +315,7 @@ static struct multi_pack_index *load_midx_chain_fd_st(const char *object_dir,
 		if (strbuf_getline_lf(&buf, fp) == EOF)
 			break;
 
-		if (get_oid_hex(buf.buf, &layer)) {
+		if (get_oid_hex_algop(buf.buf, &layer, r->hash_algo)) {
 			warning(_("invalid multi-pack-index chain: line '%s' "
 				  "not a hash"),
 				buf.buf);
@@ -325,7 +328,7 @@ static struct multi_pack_index *load_midx_chain_fd_st(const char *object_dir,
 		strbuf_reset(&buf);
 		get_split_midx_filename_ext(&buf, object_dir, layer.hash,
 					    MIDX_EXT_MIDX);
-		m = load_multi_pack_index_one(object_dir, buf.buf, local);
+		m = load_multi_pack_index_one(r, object_dir, buf.buf, local);
 
 		if (m) {
 			if (add_midx_to_chain(m, midx_chain)) {
@@ -348,7 +351,8 @@ static struct multi_pack_index *load_midx_chain_fd_st(const char *object_dir,
 	return midx_chain;
 }
 
-static struct multi_pack_index *load_multi_pack_index_chain(const char *object_dir,
+static struct multi_pack_index *load_multi_pack_index_chain(struct repository *r,
+							    const char *object_dir,
 							    int local)
 {
 	struct strbuf chain_file = STRBUF_INIT;
@@ -357,10 +361,10 @@ static struct multi_pack_index *load_multi_pack_index_chain(const char *object_d
 	struct multi_pack_index *m = NULL;
 
 	get_midx_chain_filename(&chain_file, object_dir);
-	if (open_multi_pack_index_chain(chain_file.buf, &fd, &st)) {
+	if (open_multi_pack_index_chain(r->hash_algo, chain_file.buf, &fd, &st)) {
 		int incomplete;
 		/* ownership of fd is taken over by load function */
-		m = load_midx_chain_fd_st(object_dir, local, fd, &st,
+		m = load_midx_chain_fd_st(r, object_dir, local, fd, &st,
 					  &incomplete);
 	}
 
@@ -376,9 +380,10 @@ struct multi_pack_index *load_multi_pack_index(const char *object_dir,
 
 	get_midx_filename(&midx_name, object_dir);
 
-	m = load_multi_pack_index_one(object_dir, midx_name.buf, local);
+	m = load_multi_pack_index_one(the_repository, object_dir,
+				      midx_name.buf, local);
 	if (!m)
-		m = load_multi_pack_index_chain(object_dir, local);
+		m = load_multi_pack_index_chain(the_repository, object_dir, local);
 
 	strbuf_release(&midx_name);
 
@@ -520,7 +525,7 @@ int bsearch_one_midx(const struct object_id *oid, struct multi_pack_index *m,
 		     uint32_t *result)
 {
 	int ret = bsearch_hash(oid->hash, m->chunk_oid_fanout,
-			       m->chunk_oid_lookup, the_hash_algo->rawsz,
+			       m->chunk_oid_lookup, m->repo->hash_algo->rawsz,
 			       result);
 	if (result)
 		*result += m->num_objects_in_base;
@@ -551,7 +556,7 @@ struct object_id *nth_midxed_object_oid(struct object_id *oid,
 	n = midx_for_object(&m, n);
 
 	oidread(oid, m->chunk_oid_lookup + st_mult(m->hash_len, n),
-		the_repository->hash_algo);
+		m->repo->hash_algo);
 	return oid;
 }
 

-- 
2.47.1


^ permalink raw reply related	[flat|nested] 70+ messages in thread

* [PATCH v3 6/8] midx: pass `repository` to `load_multi_pack_index`
  2024-11-27 16:28   ` [PATCH v3 0/8] " Karthik Nayak
                       ` (4 preceding siblings ...)
  2024-11-27 16:28     ` [PATCH v3 5/8] midx: cleanup internal usage of `the_repository` and `the_hash_algo` Karthik Nayak
@ 2024-11-27 16:28     ` Karthik Nayak
  2024-11-27 16:28     ` [PATCH v3 7/8] midx: pass down `hash_algo` to functions using global variables Karthik Nayak
                       ` (2 subsequent siblings)
  8 siblings, 0 replies; 70+ messages in thread
From: Karthik Nayak @ 2024-11-27 16:28 UTC (permalink / raw)
  To: git; +Cc: Karthik Nayak, me, shejialuo, gitster, Christian Couder

The `load_multi_pack_index` function in midx uses `the_repository`
variable to access the `repository` struct. Modify the function and its
callee's to send the `repository` field.

This moves usage of `the_repository` to the `test-read-midx.c` file.
While that is not optimal, it is okay, since the upcoming commits will
slowly move the usage of `the_repository` up the layers and remove it
eventually.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 midx.c                    | 11 ++++++-----
 midx.h                    |  4 +++-
 t/helper/test-read-midx.c |  8 ++++----
 3 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/midx.c b/midx.c
index 6f0fb8285af14843da132ef1b0c3a8bdd06732c9..98ee84d4a8bf388906634ad695ff39acdaa2c6d5 100644
--- a/midx.c
+++ b/midx.c
@@ -372,7 +372,8 @@ static struct multi_pack_index *load_multi_pack_index_chain(struct repository *r
 	return m;
 }
 
-struct multi_pack_index *load_multi_pack_index(const char *object_dir,
+struct multi_pack_index *load_multi_pack_index(struct repository *r,
+					       const char *object_dir,
 					       int local)
 {
 	struct strbuf midx_name = STRBUF_INIT;
@@ -380,10 +381,10 @@ struct multi_pack_index *load_multi_pack_index(const char *object_dir,
 
 	get_midx_filename(&midx_name, object_dir);
 
-	m = load_multi_pack_index_one(the_repository, object_dir,
+	m = load_multi_pack_index_one(r, object_dir,
 				      midx_name.buf, local);
 	if (!m)
-		m = load_multi_pack_index_chain(the_repository, object_dir, local);
+		m = load_multi_pack_index_chain(r, object_dir, local);
 
 	strbuf_release(&midx_name);
 
@@ -727,7 +728,7 @@ int prepare_multi_pack_index_one(struct repository *r, const char *object_dir, i
 		if (!strcmp(object_dir, m_search->object_dir))
 			return 1;
 
-	m = load_multi_pack_index(object_dir, local);
+	m = load_multi_pack_index(r, object_dir, local);
 
 	if (m) {
 		struct multi_pack_index *mp = r->objects->multi_pack_index;
@@ -881,7 +882,7 @@ int verify_midx_file(struct repository *r, const char *object_dir, unsigned flag
 	struct pair_pos_vs_id *pairs = NULL;
 	uint32_t i;
 	struct progress *progress = NULL;
-	struct multi_pack_index *m = load_multi_pack_index(object_dir, 1);
+	struct multi_pack_index *m = load_multi_pack_index(r, object_dir, 1);
 	struct multi_pack_index *curr;
 	verify_midx_error = 0;
 
diff --git a/midx.h b/midx.h
index c37ad5b5242b56d21fd76bd59957a1bdb82786ec..78efa28d35371795fa33c68660278182debb60ab 100644
--- a/midx.h
+++ b/midx.h
@@ -97,7 +97,9 @@ void get_midx_chain_filename(struct strbuf *buf, const char *object_dir);
 void get_split_midx_filename_ext(struct strbuf *buf, const char *object_dir,
 				 const unsigned char *hash, const char *ext);
 
-struct multi_pack_index *load_multi_pack_index(const char *object_dir, int local);
+struct multi_pack_index *load_multi_pack_index(struct repository *r,
+					       const char *object_dir,
+					       int local);
 int prepare_midx_pack(struct repository *r, struct multi_pack_index *m, uint32_t pack_int_id);
 struct packed_git *nth_midxed_pack(struct multi_pack_index *m,
 				   uint32_t pack_int_id);
diff --git a/t/helper/test-read-midx.c b/t/helper/test-read-midx.c
index 438fb9fc6197fc465f79d9a65b719ae315fed373..fc632369618917e2d8cdcb77bd9073c61e1544c1 100644
--- a/t/helper/test-read-midx.c
+++ b/t/helper/test-read-midx.c
@@ -18,7 +18,7 @@ static int read_midx_file(const char *object_dir, const char *checksum,
 	struct multi_pack_index *m;
 
 	setup_git_directory();
-	m = load_multi_pack_index(object_dir, 1);
+	m = load_multi_pack_index(the_repository, object_dir, 1);
 
 	if (!m)
 		return 1;
@@ -82,7 +82,7 @@ static int read_midx_checksum(const char *object_dir)
 	struct multi_pack_index *m;
 
 	setup_git_directory();
-	m = load_multi_pack_index(object_dir, 1);
+	m = load_multi_pack_index(the_repository, object_dir, 1);
 	if (!m)
 		return 1;
 	printf("%s\n", hash_to_hex(get_midx_checksum(m)));
@@ -98,7 +98,7 @@ static int read_midx_preferred_pack(const char *object_dir)
 
 	setup_git_directory();
 
-	midx = load_multi_pack_index(object_dir, 1);
+	midx = load_multi_pack_index(the_repository, object_dir, 1);
 	if (!midx)
 		return 1;
 
@@ -121,7 +121,7 @@ static int read_midx_bitmapped_packs(const char *object_dir)
 
 	setup_git_directory();
 
-	midx = load_multi_pack_index(object_dir, 1);
+	midx = load_multi_pack_index(the_repository, object_dir, 1);
 	if (!midx)
 		return 1;
 

-- 
2.47.1


^ permalink raw reply related	[flat|nested] 70+ messages in thread

* [PATCH v3 7/8] midx: pass down `hash_algo` to functions using global variables
  2024-11-27 16:28   ` [PATCH v3 0/8] " Karthik Nayak
                       ` (5 preceding siblings ...)
  2024-11-27 16:28     ` [PATCH v3 6/8] midx: pass `repository` to `load_multi_pack_index` Karthik Nayak
@ 2024-11-27 16:28     ` Karthik Nayak
  2024-11-27 16:28     ` [PATCH v3 8/8] midx: inline the `MIDX_MIN_SIZE` definition Karthik Nayak
  2024-11-28  1:27     ` [PATCH v3 0/8] Change midx.c and midx-write.c to not use global variables Junio C Hamano
  8 siblings, 0 replies; 70+ messages in thread
From: Karthik Nayak @ 2024-11-27 16:28 UTC (permalink / raw)
  To: git; +Cc: Karthik Nayak, me, shejialuo, gitster, Christian Couder

The functions `get_split_midx_filename_ext()`, `get_midx_filename()` and
`get_midx_filename_ext()` use `hash_to_hex()` which internally uses the
`the_hash_algo` global variable.

Remove this dependency on global variables by passing down the
`hash_algo` through to the functions mentioned and instead calling
`hash_to_hex_algop()` along with the obtained `hash_algo`.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 midx-write.c    | 22 +++++++++++-----------
 midx.c          | 26 +++++++++++++++-----------
 midx.h          | 10 +++++++---
 pack-bitmap.c   |  6 +++---
 pack-revindex.c |  2 +-
 5 files changed, 37 insertions(+), 29 deletions(-)

diff --git a/midx-write.c b/midx-write.c
index 1bc2f5256916e69924245951f654c1047ffeab84..bcd1d50eb0f5c292c904a38f13279b1ebec9c855 100644
--- a/midx-write.c
+++ b/midx-write.c
@@ -991,9 +991,10 @@ 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_split_midx_filename_ext(&to, m->object_dir, hash,
+		get_midx_filename_ext(m->repo->hash_algo, &from, m->object_dir,
+				      hash, midx_exts[i].non_split);
+		get_split_midx_filename_ext(m->repo->hash_algo, &to,
+					    m->object_dir, hash,
 					    midx_exts[i].split);
 
 		if (link(from.buf, to.buf) < 0 && errno != ENOENT) {
@@ -1012,9 +1013,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)
 {
 	/*
@@ -1039,7 +1039,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);
 
@@ -1083,7 +1083,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);
@@ -1440,8 +1440,8 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
 		if (link_midx_to_chain(ctx.base_midx) < 0)
 			return -1;
 
-		get_split_midx_filename_ext(&final_midx_name, object_dir,
-					    midx_hash, MIDX_EXT_MIDX);
+		get_split_midx_filename_ext(r->hash_algo, &final_midx_name,
+					    object_dir, midx_hash, MIDX_EXT_MIDX);
 
 		if (rename_tempfile(&incr, final_midx_name.buf) < 0) {
 			error_errno(_("unable to rename new multi-pack-index layer"));
@@ -1474,7 +1474,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..f45ea842cd6eda23d2eadc9deaae43839aef24c1 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,
@@ -234,11 +236,13 @@ void get_midx_chain_filename(struct strbuf *buf, const char *object_dir)
 	strbuf_addstr(buf, "/multi-pack-index-chain");
 }
 
-void get_split_midx_filename_ext(struct strbuf *buf, const char *object_dir,
+void get_split_midx_filename_ext(const struct git_hash_algo *hash_algo,
+				 struct strbuf *buf, const char *object_dir,
 				 const unsigned char *hash, const char *ext)
 {
 	get_midx_chain_dirname(buf, object_dir);
-	strbuf_addf(buf, "/multi-pack-index-%s.%s", hash_to_hex(hash), ext);
+	strbuf_addf(buf, "/multi-pack-index-%s.%s",
+		    hash_to_hex_algop(hash, hash_algo), ext);
 }
 
 static int open_multi_pack_index_chain(const struct git_hash_algo *hash_algo,
@@ -326,8 +330,8 @@ static struct multi_pack_index *load_midx_chain_fd_st(struct repository *r,
 		valid = 0;
 
 		strbuf_reset(&buf);
-		get_split_midx_filename_ext(&buf, object_dir, layer.hash,
-					    MIDX_EXT_MIDX);
+		get_split_midx_filename_ext(r->hash_algo, &buf, object_dir,
+					    layer.hash, MIDX_EXT_MIDX);
 		m = load_multi_pack_index_one(r, object_dir, buf.buf, local);
 
 		if (m) {
@@ -379,7 +383,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 +826,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 +895,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..9d1374cbd58d016bb82338337b2a4e5ba7234092 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,12 +90,15 @@ 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);
-void get_split_midx_filename_ext(struct strbuf *buf, const char *object_dir,
+void get_split_midx_filename_ext(const struct git_hash_algo *hash_algo,
+				 struct strbuf *buf, const char *object_dir,
 				 const unsigned char *hash, const char *ext);
 
 struct multi_pack_index *load_multi_pack_index(struct repository *r,
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.1


^ permalink raw reply related	[flat|nested] 70+ messages in thread

* [PATCH v3 8/8] midx: inline the `MIDX_MIN_SIZE` definition
  2024-11-27 16:28   ` [PATCH v3 0/8] " Karthik Nayak
                       ` (6 preceding siblings ...)
  2024-11-27 16:28     ` [PATCH v3 7/8] midx: pass down `hash_algo` to functions using global variables Karthik Nayak
@ 2024-11-27 16:28     ` Karthik Nayak
  2024-11-28  1:27     ` [PATCH v3 0/8] Change midx.c and midx-write.c to not use global variables Junio C Hamano
  8 siblings, 0 replies; 70+ messages in thread
From: Karthik Nayak @ 2024-11-27 16:28 UTC (permalink / raw)
  To: git; +Cc: Karthik Nayak, me, shejialuo, gitster, Christian Couder

The `MIDX_MIN_SIZE` definition is used to check the midx_size in
`local_multi_pack_index_one`. This definition relies on the
`the_hash_algo` global variable. Inline this and remove the global
variable usage.

With this, remove `USE_THE_REPOSITORY_VARIABLE` usage from `midx.c`.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 midx.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/midx.c b/midx.c
index f45ea842cd6eda23d2eadc9deaae43839aef24c1..e0eae1c25ec91f7db5670ff9bacdf0e195c35795 100644
--- a/midx.c
+++ b/midx.c
@@ -1,5 +1,3 @@
-#define USE_THE_REPOSITORY_VARIABLE
-
 #include "git-compat-util.h"
 #include "config.h"
 #include "dir.h"
@@ -94,8 +92,6 @@ static int midx_read_object_offsets(const unsigned char *chunk_start,
 	return 0;
 }
 
-#define MIDX_MIN_SIZE (MIDX_HEADER_SIZE + the_hash_algo->rawsz)
-
 static struct multi_pack_index *load_multi_pack_index_one(struct repository *r,
 							  const char *object_dir,
 							  const char *midx_name,
@@ -122,7 +118,7 @@ static struct multi_pack_index *load_multi_pack_index_one(struct repository *r,
 
 	midx_size = xsize_t(st.st_size);
 
-	if (midx_size < MIDX_MIN_SIZE) {
+	if (midx_size < (MIDX_HEADER_SIZE + r->hash_algo->rawsz)) {
 		error(_("multi-pack-index file %s is too small"), midx_name);
 		goto cleanup_fail;
 	}

-- 
2.47.1


^ permalink raw reply related	[flat|nested] 70+ messages in thread

* Re: [PATCH v3 0/8] Change midx.c and midx-write.c to not use global variables
  2024-11-27 16:28   ` [PATCH v3 0/8] " Karthik Nayak
                       ` (7 preceding siblings ...)
  2024-11-27 16:28     ` [PATCH v3 8/8] midx: inline the `MIDX_MIN_SIZE` definition Karthik Nayak
@ 2024-11-28  1:27     ` Junio C Hamano
  2024-12-03  9:43       ` Patrick Steinhardt
  8 siblings, 1 reply; 70+ messages in thread
From: Junio C Hamano @ 2024-11-28  1:27 UTC (permalink / raw)
  To: Karthik Nayak; +Cc: git, me, shejialuo, Christian Couder

Karthik Nayak <karthik.188@gmail.com> writes:

> Changes in v3:
> - Split out the first commit into a separate series [1].
> - Improved some of the commit messages to be more descriptive.
> - Merged the 8th and 9th commits together, since they were similar.
> - v2: https://lore.kernel.org/r/20241119-374-refactor-midx-c-and-midx-write-c-to-not-depend-on-global-state-v2-0-e2f607174efc@gmail.com

Merging either v2 or v3 to 'master', together with their
prerequisite topics/patches, seems to result in identical trees,
which is fine if reviews on previous rounds did not find any bugs.

Will queue.  Thanks.


^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v3 0/8] Change midx.c and midx-write.c to not use global variables
  2024-11-28  1:27     ` [PATCH v3 0/8] Change midx.c and midx-write.c to not use global variables Junio C Hamano
@ 2024-12-03  9:43       ` Patrick Steinhardt
  2024-12-03 23:06         ` Junio C Hamano
  0 siblings, 1 reply; 70+ messages in thread
From: Patrick Steinhardt @ 2024-12-03  9:43 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Karthik Nayak, git, me, shejialuo, Christian Couder

On Thu, Nov 28, 2024 at 10:27:54AM +0900, Junio C Hamano wrote:
> Karthik Nayak <karthik.188@gmail.com> writes:
> 
> > Changes in v3:
> > - Split out the first commit into a separate series [1].
> > - Improved some of the commit messages to be more descriptive.
> > - Merged the 8th and 9th commits together, since they were similar.
> > - v2: https://lore.kernel.org/r/20241119-374-refactor-midx-c-and-midx-write-c-to-not-depend-on-global-state-v2-0-e2f607174efc@gmail.com
> 
> Merging either v2 or v3 to 'master', together with their
> prerequisite topics/patches, seems to result in identical trees,
> which is fine if reviews on previous rounds did not find any bugs.
> 
> Will queue.  Thanks.

I've had another read through the series and couldn't find any issues
with it. I also scanned through v2 to see whether there was any feedback
on code, but it seems like the only complaints there were about commit
messages. So I guess it is expected that the trees are identical.

So this looks good to me overall, thanks!

Patrick

^ permalink raw reply	[flat|nested] 70+ messages in thread

* Re: [PATCH v3 0/8] Change midx.c and midx-write.c to not use global variables
  2024-12-03  9:43       ` Patrick Steinhardt
@ 2024-12-03 23:06         ` Junio C Hamano
  0 siblings, 0 replies; 70+ messages in thread
From: Junio C Hamano @ 2024-12-03 23:06 UTC (permalink / raw)
  To: Patrick Steinhardt; +Cc: Karthik Nayak, git, me, shejialuo, Christian Couder

Patrick Steinhardt <ps@pks.im> writes:

> I've had another read through the series and couldn't find any issues
> with it. I also scanned through v2 to see whether there was any feedback
> on code, but it seems like the only complaints there were about commit
> messages. So I guess it is expected that the trees are identical.
>
> So this looks good to me overall, thanks!

Let's merge it down to 'next', then.  Thanks, all.

^ permalink raw reply	[flat|nested] 70+ messages in thread

end of thread, other threads:[~2024-12-03 23:06 UTC | newest]

Thread overview: 70+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-11-15 13:42 [PATCH 0/8] Change midx.c and midx-write.c to not use global variables Karthik Nayak
2024-11-15 13:42 ` [PATCH 1/8] builtin: pass repository to sub commands Karthik Nayak
2024-11-16 12:49   ` shejialuo
2024-11-18  8:05     ` Patrick Steinhardt
2024-11-18 15:17     ` karthik nayak
2024-11-15 13:42 ` [PATCH 2/8] midx-write: add repository field to `write_midx_context` Karthik Nayak
2024-11-15 23:05   ` brian m. carlson
2024-11-18 15:17     ` karthik nayak
2024-11-18  8:05   ` Patrick Steinhardt
2024-11-18 15:37     ` karthik nayak
2024-11-15 13:42 ` [PATCH 3/8] midx-write: pass down repository to `write_midx_file[_only]` Karthik Nayak
2024-11-18  8:05   ` Patrick Steinhardt
2024-11-15 13:42 ` [PATCH 4/8] midx: cleanup internal usage of `the_repository` and `the_hash_algo` Karthik Nayak
2024-11-18  8:06   ` Patrick Steinhardt
2024-11-18 16:16     ` karthik nayak
2024-11-15 13:42 ` [PATCH 5/8] midx: pass `repository` to `load_multi_pack_index` Karthik Nayak
2024-11-15 13:42 ` [PATCH 6/8] midx: pass down `hash_algo` to `get_midx_filename[_ext]` Karthik Nayak
2024-11-16 13:16   ` shejialuo
2024-11-18 16:25     ` karthik nayak
2024-11-19 12:12       ` shejialuo
2024-11-15 13:42 ` [PATCH 7/8] midx: pass down `hash_algo` to `get_split_midx_filename_ext` Karthik Nayak
2024-11-16 13:20   ` shejialuo
2024-11-15 13:42 ` [PATCH 8/8] midx: inline the `MIDX_MIN_SIZE` definition Karthik Nayak
2024-11-15 14:13 ` [PATCH 0/8] Change midx.c and midx-write.c to not use global variables karthik nayak
2024-11-19 15:36 ` [PATCH v2 00/10] " Karthik Nayak
2024-11-19 15:36   ` [PATCH v2 01/10] builtin: pass repository to sub commands Karthik Nayak
2024-11-19 15:36   ` [PATCH v2 02/10] midx-write: pass down repository to static functions Karthik Nayak
2024-11-20 18:15     ` Christian Couder
2024-11-20 19:41       ` Taylor Blau
2024-11-21 14:57       ` karthik nayak
2024-11-20 19:43     ` Taylor Blau
2024-11-21 15:06       ` karthik nayak
2024-11-19 15:36   ` [PATCH v2 03/10] midx-write: use `revs->repo` inside `read_refs_snapshot` Karthik Nayak
2024-11-20 12:58     ` shejialuo
2024-11-20 14:26       ` Richard Kerry
2024-11-20 19:46         ` Taylor Blau
2024-11-21 15:30           ` karthik nayak
2024-11-20 19:44     ` Taylor Blau
2024-11-19 15:36   ` [PATCH v2 04/10] write-midx: add repository field to `write_midx_context` Karthik Nayak
2024-11-19 15:36   ` [PATCH v2 05/10] midx-write: pass down repository to `write_midx_file[_only]` Karthik Nayak
2024-11-20 20:11     ` Taylor Blau
2024-11-19 15:36   ` [PATCH v2 06/10] midx: cleanup internal usage of `the_repository` and `the_hash_algo` Karthik Nayak
2024-11-19 15:36   ` [PATCH v2 07/10] midx: pass `repository` to `load_multi_pack_index` Karthik Nayak
2024-11-19 15:36   ` [PATCH v2 08/10] midx: pass down `hash_algo` to `get_midx_filename[_ext]` Karthik Nayak
2024-11-20 18:15     ` Christian Couder
2024-11-21 15:35       ` karthik nayak
2024-11-19 15:36   ` [PATCH v2 09/10] midx: pass down `hash_algo` to `get_split_midx_filename_ext` Karthik Nayak
2024-11-20 18:15     ` Christian Couder
2024-11-20 22:23       ` Taylor Blau
2024-11-21 15:41         ` karthik nayak
2024-11-19 15:36   ` [PATCH v2 10/10] midx: inline the `MIDX_MIN_SIZE` definition Karthik Nayak
2024-11-20 13:13     ` shejialuo
2024-11-21 15:41       ` karthik nayak
2024-11-20 18:20   ` [PATCH v2 00/10] Change midx.c and midx-write.c to not use global variables Christian Couder
2024-11-20 22:24     ` Taylor Blau
2024-11-21  0:09       ` Junio C Hamano
2024-11-21  2:19   ` Junio C Hamano
2024-11-22 10:25     ` karthik nayak
2024-11-27 16:28   ` [PATCH v3 0/8] " Karthik Nayak
2024-11-27 16:28     ` [PATCH v3 1/8] midx-write: pass down repository to static functions Karthik Nayak
2024-11-27 16:28     ` [PATCH v3 2/8] midx-write: use `revs->repo` inside `read_refs_snapshot` Karthik Nayak
2024-11-27 16:28     ` [PATCH v3 3/8] write-midx: add repository field to `write_midx_context` Karthik Nayak
2024-11-27 16:28     ` [PATCH v3 4/8] midx-write: pass down repository to `write_midx_file[_only]` Karthik Nayak
2024-11-27 16:28     ` [PATCH v3 5/8] midx: cleanup internal usage of `the_repository` and `the_hash_algo` Karthik Nayak
2024-11-27 16:28     ` [PATCH v3 6/8] midx: pass `repository` to `load_multi_pack_index` Karthik Nayak
2024-11-27 16:28     ` [PATCH v3 7/8] midx: pass down `hash_algo` to functions using global variables Karthik Nayak
2024-11-27 16:28     ` [PATCH v3 8/8] midx: inline the `MIDX_MIN_SIZE` definition Karthik Nayak
2024-11-28  1:27     ` [PATCH v3 0/8] Change midx.c and midx-write.c to not use global variables Junio C Hamano
2024-12-03  9:43       ` Patrick Steinhardt
2024-12-03 23:06         ` Junio C Hamano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).