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

[PATCH 5/9] builtin/pack-refs: stop using `the_repository`

From
Usman Akinyemi <usmanakinyemi202@gmail.com>
Date
Mar 18, 2025, 11:58 UTC
Message-ID
<20250318115912.2978992-6-usmanakinyemi202@gmail.com>
In-Reply-To
<20250318115912.2978992-1-usmanakinyemi202@gmail.com>

Remove the_repository global variable in favor of the repository argument that gets passed in "builtin/pack-refs.c".

When `-h` is passed to the command outside a Git repository, the `run_builtin()` will call the `cmd_pack_refs()` function with `repo` set to NULL and then early in the function, `parse_options()` call will give the options help and exit.

Mentored-by: Christian Couder <chriscool@tuxfamily.org>
Signed-off-by: Usman Akinyemi <usmanakinyemi202@gmail.com>
---
 builtin/pack-refs.c        | 8 +++-----
 t/t0610-reftable-basics.sh | 7 +++++++
 2 files changed, 10 insertions(+), 5 deletions(-)
diff --git a/builtin/pack-refs.c b/builtin/pack-refs.c
index 4fdd68880e..e47bae1c80 100644
--- a/builtin/pack-refs.c
+++ b/builtin/pack-refs.c
@@ -1,5 +1,3 @@
-#define USE_THE_REPOSITORY_VARIABLE
-
 #include "builtin.h"
 #include "config.h"
 #include "gettext.h"
@@ -15,7 +13,7 @@ static char const * const pack_refs_usage[] = {
 int cmd_pack_refs(int argc,
 		  const char **argv,
 		  const char *prefix,
-		  struct repository *repo UNUSED)
+		  struct repository *repo)
 {
 	struct ref_exclusions excludes = REF_EXCLUSIONS_INIT;
 	struct string_list included_refs = STRING_LIST_INIT_NODUP;
@@ -39,7 +37,7 @@ int cmd_pack_refs(int argc,
 			N_("references to exclude")),
 		OPT_END(),
 	};
-	git_config(git_default_config, NULL);
+	repo_config(repo, git_default_config, NULL);
 	if (parse_options(argc, argv, prefix, opts, pack_refs_usage, 0))
 		usage_with_options(pack_refs_usage, opts);
 
@@ -52,7 +50,7 @@ int cmd_pack_refs(int argc,
 	if (!pack_refs_opts.includes->nr)
 		string_list_append(pack_refs_opts.includes, "refs/tags/*");
 
-	ret = refs_pack_refs(get_main_ref_store(the_repository), &pack_refs_opts);
+	ret = refs_pack_refs(get_main_ref_store(repo), &pack_refs_opts);
 
 	clear_ref_exclusions(&excludes);
 	string_list_clear(&included_refs, 0);
diff --git a/t/t0610-reftable-basics.sh b/t/t0610-reftable-basics.sh
index 4618ffc108..002a75dee8 100755
--- a/t/t0610-reftable-basics.sh
+++ b/t/t0610-reftable-basics.sh
@@ -14,6 +14,13 @@ export GIT_TEST_DEFAULT_REF_FORMAT
 
 INVALID_OID=$(test_oid 001)
 
+test_expect_success 'pack-refs does not crash with -h' '
+	test_expect_code 129 git pack-refs -h >usage &&
+	test_grep "[Uu]sage: git pack-refs " usage &&
+	test_expect_code 129 nongit git pack-refs -h >usage &&
+	test_grep "[Uu]sage: git pack-refs " usage
+'
+
 test_expect_success 'init: creates basic reftable structures' '
 	test_when_finished "rm -rf repo" &&
 	git init repo &&
-- 
2.48.1
Previous: Usman AkinyemiNext: Usman Akinyemi
Message 9 of 17 in “remove unnecessary if statement”
  1. 0/9 remove unnecessary if statementUsman Akinyemi, Mar 18, 2025
  2. 1/9 config: teach repo_config to allow `repo` to be NULLUsman Akinyemi, Mar 18, 2025
  3. Patrick SteinhardtMar 20, 2025
  4. Usman AkinyemiMar 21, 2025
  5. 2/9 builtin/verify-tag: stop using `the_repository`Usman Akinyemi, Mar 18, 2025
  6. Patrick SteinhardtMar 20, 2025
  7. 3/9 builtin/verify-commit: stop using `the_repository`Usman Akinyemi, Mar 18, 2025
  8. 4/9 builtin/send-pack: stop using `the_repository`Usman Akinyemi, Mar 18, 2025
  9. 5/9 builtin/pack-refs: stop using `the_repository`Usman Akinyemi, Mar 18, 2025
  10. 6/9 builtin/ls-files: stop using `the_repository`Usman Akinyemi, Mar 18, 2025
  11. 7/9 builtin/for-each-ref: stop using `the_repository`Usman Akinyemi, Mar 18, 2025
  12. 8/9 builtin/checkout-index: stop using `the_repository`Usman Akinyemi, Mar 18, 2025
  13. 9/9 builtin/update-server-info: remove unnecessary if statementUsman Akinyemi, Mar 18, 2025
  14. Patrick SteinhardtMar 20, 2025
  15. Usman AkinyemiMar 21, 2025
  16. Junio C HamanoMar 18, 2025
  17. Usman AkinyemiMar 19, 2025

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

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