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

[PATCH v3 3/8] builtin/verify-commit: stop using `the_repository`

From
Usman Akinyemi <usmanakinyemi202@gmail.com>
Date
Mar 6, 2025, 14:35 UTC
Message-ID
<20250306143629.1267358-4-usmanakinyemi202@gmail.com>
In-Reply-To
<20250306143629.1267358-1-usmanakinyemi202@gmail.com>

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

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

Pass the repository available in the calling context to `verify_commit()` to remove it's dependency on the global `the_repository` variable.

Mentored-by: Christian Couder <chriscool@tuxfamily.org>
Signed-off-by: Usman Akinyemi <usmanakinyemi202@gmail.com>
---
 builtin/verify-commit.c  | 13 ++++++-------
 t/t7510-signed-commit.sh |  7 +++++++
 2 files changed, 13 insertions(+), 7 deletions(-)
diff --git a/builtin/verify-commit.c b/builtin/verify-commit.c
index 779b7988ca..5f749a30da 100644
--- a/builtin/verify-commit.c
+++ b/builtin/verify-commit.c
@@ -5,7 +5,6 @@
  *
  * Based on git-verify-tag
  */
-#define USE_THE_REPOSITORY_VARIABLE
 #include "builtin.h"
 #include "config.h"
 #include "gettext.h"
@@ -33,15 +32,15 @@ static int run_gpg_verify(struct commit *commit, unsigned flags)
 	return ret;
 }
 
-static int verify_commit(const char *name, unsigned flags)
+static int verify_commit(struct repository *repo, const char *name, unsigned flags)
 {
 	struct object_id oid;
 	struct object *obj;
 
-	if (repo_get_oid(the_repository, name, &oid))
+	if (repo_get_oid(repo, name, &oid))
 		return error("commit '%s' not found.", name);
 
-	obj = parse_object(the_repository, &oid);
+	obj = parse_object(repo, &oid);
 	if (!obj)
 		return error("%s: unable to read file.", name);
 	if (obj->type != OBJ_COMMIT)
@@ -54,7 +53,7 @@ static int verify_commit(const char *name, unsigned flags)
 int cmd_verify_commit(int argc,
 		      const char **argv,
 		      const char *prefix,
-		      struct repository *repo UNUSED)
+		      struct repository *repo)
 {
 	int i = 1, verbose = 0, had_error = 0;
 	unsigned flags = 0;
@@ -64,7 +63,7 @@ int cmd_verify_commit(int argc,
 		OPT_END()
 	};
 
-	git_config(git_default_config, NULL);
+	repo_config(repo, git_default_config, NULL);
 
 	argc = parse_options(argc, argv, prefix, verify_commit_options,
 			     verify_commit_usage, PARSE_OPT_KEEP_ARGV0);
@@ -78,7 +77,7 @@ int cmd_verify_commit(int argc,
 	 * was received in the process of writing the gpg input: */
 	signal(SIGPIPE, SIG_IGN);
 	while (i < argc)
-		if (verify_commit(argv[i++], flags))
+		if (verify_commit(repo, argv[i++], flags))
 			had_error = 1;
 	return had_error;
 }
diff --git a/t/t7510-signed-commit.sh b/t/t7510-signed-commit.sh
index 0d2dd29fe6..39677e859a 100755
--- a/t/t7510-signed-commit.sh
+++ b/t/t7510-signed-commit.sh
@@ -8,6 +8,13 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
 GNUPGHOME_NOT_USED=$GNUPGHOME
 . "$TEST_DIRECTORY/lib-gpg.sh"
 
+test_expect_success GPG 'verify-commit does not crash with -h' '
+	test_expect_code 129 git verify-commit -h >usage &&
+	test_grep "[Uu]sage: git verify-commit " usage &&
+	test_expect_code 129 nongit git verify-commit -h >usage &&
+	test_grep "[Uu]sage: git verify-commit " usage
+'
+
 test_expect_success GPG 'create signed commits' '
 	test_oid_cache <<-\EOF &&
 	header sha1:gpgsig
-- 
2.48.1
Previous: Junio C HamanoNext: Usman Akinyemi
Message 44 of 63 in “[Outreachy] stop using the_repository global variable.”
  1. 0/7 [Outreachy] stop using the_repository global variable.Usman Akinyemi, Feb 14, 2025
  2. 1/7 builtin/verify-tag: stop using `the_repository`Usman Akinyemi, Feb 14, 2025
  3. Patrick SteinhardtFeb 17, 2025
  4. Usman AkinyemiFeb 17, 2025
  5. Patrick SteinhardtFeb 17, 2025
  6. Usman AkinyemiFeb 17, 2025
  7. Patrick SteinhardtFeb 17, 2025
  8. 2/7 builtin/verify-commit.c: stop using `the_repository`Usman Akinyemi, Feb 14, 2025
  9. shejialuoFeb 16, 2025
  10. Usman AkinyemiFeb 17, 2025
  11. 3/7 builtin/send-pack.c: stop using `the_repository`Usman Akinyemi, Feb 14, 2025
  12. 4/7 builtin/pack-refs: stop using `the_repository`Usman Akinyemi, Feb 14, 2025
  13. 5/7 builtin/ls-files: stop using `the_repository`Usman Akinyemi, Feb 14, 2025
  14. Patrick SteinhardtFeb 17, 2025
  15. Usman AkinyemiFeb 17, 2025
  16. 6/7 builtin/for-each-ref: stop using `the_repository`Usman Akinyemi, Feb 14, 2025
  17. 7/7 builtin/checkout-index.c: stop using `the_repository`Usman Akinyemi, Feb 14, 2025
  18. shejialuoFeb 16, 2025
  19. Usman AkinyemiFeb 17, 2025
  20. 00/12 [Outreachy] stop using the_repository global variable.Usman Akinyemi, Feb 19, 2025
  21. 01/12 builtin/verify-tag: refactor `cmd_verify_tag()`Usman Akinyemi, Feb 19, 2025
  22. Junio C HamanoFeb 20, 2025
  23. 02/12 builtin/verify-tag: stop using `the_repository`Usman Akinyemi, Feb 19, 2025
  24. Junio C HamanoFeb 20, 2025
  25. Usman AkinyemiFeb 27, 2025
  26. Junio C HamanoFeb 27, 2025
  27. 03/12 builtin/verify-commit: refactor `cmd_verify_commit()`Usman Akinyemi, Feb 19, 2025
  28. 04/12 builtin/verify-commit: stop using `the_repository`Usman Akinyemi, Feb 19, 2025
  29. 05/12 builtin/send-pack: refactor `cmd_send_pack()`Usman Akinyemi, Feb 19, 2025
  30. 06/12 builtin/send-pack: stop using `the_repository`Usman Akinyemi, Feb 19, 2025
  31. 07/12 builtin/pack-refs: refactor `cmd_pack_refs()`Usman Akinyemi, Feb 19, 2025
  32. 08/12 builtin/pack-refs: stop using `the_repository`Usman Akinyemi, Feb 19, 2025
  33. 09/12 builtin/ls-files: stop using `the_repository`Usman Akinyemi, Feb 19, 2025
  34. 10/12 builtin/for-each-ref: refactor `cmd_for_each_ref()`Usman Akinyemi, Feb 19, 2025
  35. 11/12 builtin/for-each-ref: stop using `the_repository`Usman Akinyemi, Feb 19, 2025
  36. 12/12 builtin/checkout-index: stop using `the_repository`Usman Akinyemi, Feb 19, 2025
  37. 0/8 stop using the_repository global variable.Usman Akinyemi, Mar 6, 2025
  38. 1/8 config: teach repo_config to allow `repo` to be NULLUsman Akinyemi, Mar 6, 2025
  39. Junio C HamanoMar 6, 2025
  40. Usman AkinyemiMar 7, 2025
  41. Phillip WoodMar 7, 2025
  42. 2/8 builtin/verify-tag: stop using `the_repository`Usman Akinyemi, Mar 6, 2025
  43. Junio C HamanoMar 6, 2025
  44. 3/8 builtin/verify-commit: stop using `the_repository`Usman Akinyemi, Mar 6, 2025
  45. 4/8 builtin/send-pack: stop using `the_repository`Usman Akinyemi, Mar 6, 2025
  46. 5/8 builtin/pack-refs: stop using `the_repository`Usman Akinyemi, Mar 6, 2025
  47. 6/8 builtin/ls-files: stop using `the_repository`Usman Akinyemi, Mar 6, 2025
  48. Junio C HamanoMar 6, 2025
  49. 7/8 builtin/for-each-ref: stop using `the_repository`Usman Akinyemi, Mar 6, 2025
  50. 8/8 builtin/checkout-index: stop using `the_repository`Usman Akinyemi, Mar 6, 2025
  51. Junio C HamanoMar 6, 2025
  52. Usman AkinyemiMar 7, 2025
  53. Junio C HamanoMar 7, 2025
  54. Usman AkinyemiMar 7, 2025
  55. 0/8 stop using the_repository global variable.Usman Akinyemi, Mar 7, 2025
  56. 1/8 config: teach repo_config to allow `repo` to be NULLUsman Akinyemi, Mar 7, 2025
  57. 2/8 builtin/verify-tag: stop using `the_repository`Usman Akinyemi, Mar 7, 2025
  58. 3/8 builtin/verify-commit: stop using `the_repository`Usman Akinyemi, Mar 7, 2025
  59. 4/8 builtin/send-pack: stop using `the_repository`Usman Akinyemi, Mar 7, 2025
  60. 5/8 builtin/pack-refs: stop using `the_repository`Usman Akinyemi, Mar 7, 2025
  61. 6/8 builtin/ls-files: stop using `the_repository`Usman Akinyemi, Mar 7, 2025
  62. 7/8 builtin/for-each-ref: stop using `the_repository`Usman Akinyemi, Mar 7, 2025
  63. 8/8 builtin/checkout-index: stop using `the_repository`Usman Akinyemi, Mar 7, 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.