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

[PATCH 08/12] builtin/show-ref: refactor options for patterns subcommand

From
Patrick Steinhardt <ps@pks.im>
Date
Oct 24, 2023, 13:11 UTC
Message-ID
<805889eda4cf89e6aebf8a408ceb2a5c60551526.1698152926.git.ps@pks.im>
In-Reply-To
<cover.1698152926.git.ps@pks.im>

The patterns subcommand is the last command that still uses global variables to track its options. Convert it to use a structure instead with the same motivation as preceding commits.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 builtin/show-ref.c | 35 ++++++++++++++++++++++-------------
 1 file changed, 22 insertions(+), 13 deletions(-)
diff --git a/builtin/show-ref.c b/builtin/show-ref.c
index 589a91f15b9..5d5d7d22ed1 100644
--- a/builtin/show-ref.c
+++ b/builtin/show-ref.c
@@ -18,8 +18,6 @@ static const char * const show_ref_usage[] = {
 	NULL
 };
 
-static int show_head, tags_only, heads_only, verify;
-
 struct show_one_options {
 	int quiet;
 	int hash_only;
@@ -59,6 +57,7 @@ struct show_ref_data {
 	const struct show_one_options *show_one_opts;
 	const char **patterns;
 	int found_match;
+	int show_head;
 };
 
 static int show_ref(const char *refname, const struct object_id *oid,
@@ -66,7 +65,7 @@ static int show_ref(const char *refname, const struct object_id *oid,
 {
 	struct show_ref_data *data = cbdata;
 
-	if (show_head && !strcmp(refname, "HEAD"))
+	if (data->show_head && !strcmp(refname, "HEAD"))
 		goto match;
 
 	if (data->patterns) {
@@ -180,20 +179,28 @@ static int cmd_show_ref__verify(const struct show_one_options *show_one_opts,
 	return 0;
 }
 
-static int cmd_show_ref__patterns(const struct show_one_options *show_one_opts,
+struct patterns_options {
+	int show_head;
+	int heads_only;
+	int tags_only;
+};
+
+static int cmd_show_ref__patterns(const struct patterns_options *opts,
+				  const struct show_one_options *show_one_opts,
 				  const char **patterns)
 {
 	struct show_ref_data show_ref_data = {
 		.show_one_opts = show_one_opts,
+		.show_head = opts->show_head,
 		.patterns = (patterns && *patterns) ? patterns : NULL,
 	};
 
-	if (show_head)
+	if (opts->show_head)
 		head_ref(show_ref, &show_ref_data);
-	if (heads_only || tags_only) {
-		if (heads_only)
+	if (opts->heads_only || opts->tags_only) {
+		if (opts->heads_only)
 			for_each_fullref_in("refs/heads/", show_ref, &show_ref_data);
-		if (tags_only)
+		if (opts->tags_only)
 			for_each_fullref_in("refs/tags/", show_ref, &show_ref_data);
 	} else {
 		for_each_ref(show_ref, &show_ref_data);
@@ -231,15 +238,17 @@ static int exclude_existing_callback(const struct option *opt, const char *arg,
 int cmd_show_ref(int argc, const char **argv, const char *prefix)
 {
 	struct exclude_existing_options exclude_existing_opts = {0};
+	struct patterns_options patterns_opts = {0};
 	struct show_one_options show_one_opts = {0};
+	int verify = 0;
 	const struct option show_ref_options[] = {
-		OPT_BOOL(0, "tags", &tags_only, N_("only show tags (can be combined with heads)")),
-		OPT_BOOL(0, "heads", &heads_only, N_("only show heads (can be combined with tags)")),
+		OPT_BOOL(0, "tags", &patterns_opts.tags_only, N_("only show tags (can be combined with heads)")),
+		OPT_BOOL(0, "heads", &patterns_opts.heads_only, N_("only show heads (can be combined with tags)")),
 		OPT_BOOL(0, "verify", &verify, N_("stricter reference checking, "
 			    "requires exact ref path")),
-		OPT_HIDDEN_BOOL('h', NULL, &show_head,
+		OPT_HIDDEN_BOOL('h', NULL, &patterns_opts.show_head,
 				N_("show the HEAD reference, even if it would be filtered out")),
-		OPT_BOOL(0, "head", &show_head,
+		OPT_BOOL(0, "head", &patterns_opts.show_head,
 		  N_("show the HEAD reference, even if it would be filtered out")),
 		OPT_BOOL('d', "dereference", &show_one_opts.deref_tags,
 			    N_("dereference tags into object IDs")),
@@ -265,5 +274,5 @@ int cmd_show_ref(int argc, const char **argv, const char *prefix)
 	else if (verify)
 		return cmd_show_ref__verify(&show_one_opts, argv);
 	else
-		return cmd_show_ref__patterns(&show_one_opts, argv);
+		return cmd_show_ref__patterns(&patterns_opts, &show_one_opts, argv);
 }
-- 
2.42.0
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 13 of 66 in “show-ref: introduce mode to check for ref existence”
  1. 00/12 show-ref: introduce mode to check for ref existencePatrick Steinhardt, Oct 24, 2023
  2. 01/12 builtin/show-ref: convert pattern to a local variablePatrick Steinhardt, Oct 24, 2023
  3. 02/12 builtin/show-ref: split up different subcommandsPatrick Steinhardt, Oct 24, 2023
  4. Eric SunshineOct 24, 2023
  5. 03/12 builtin/show-ref: fix leaking string bufferPatrick Steinhardt, Oct 24, 2023
  6. 04/12 builtin/show-ref: fix dead code when passing patternsPatrick Steinhardt, Oct 24, 2023
  7. Eric SunshineOct 24, 2023
  8. 05/12 builtin/show-ref: refactor `--exclude-existing` optionsPatrick Steinhardt, Oct 24, 2023
  9. Eric SunshineOct 24, 2023
  10. Patrick SteinhardtOct 25, 2023
  11. 06/12 builtin/show-ref: stop using global variable to count matchesPatrick Steinhardt, Oct 24, 2023
  12. 07/12 builtin/show-ref: stop using global vars for `show_one()`Patrick Steinhardt, Oct 24, 2023
  13. 08/12 builtin/show-ref: refactor options for patterns subcommandPatrick Steinhardt, Oct 24, 2023
  14. 09/12 builtin/show-ref: ensure mutual exclusiveness of subcommandsPatrick Steinhardt, Oct 24, 2023
  15. Eric SunshineOct 24, 2023
  16. 10/12 builtin/show-ref: explicitly spell out different modes in synopsisPatrick Steinhardt, Oct 24, 2023
  17. Eric SunshineOct 24, 2023
  18. Patrick SteinhardtOct 25, 2023
  19. 11/12 builtin/show-ref: add new mode to check for reference existencePatrick Steinhardt, Oct 24, 2023
  20. Eric SunshineOct 24, 2023
  21. Patrick SteinhardtOct 25, 2023
  22. 12/12 t: use git-show-ref(1) to check for ref existencePatrick Steinhardt, Oct 24, 2023
  23. Junio C HamanoOct 24, 2023
  24. Han-Wen NienhuysOct 25, 2023
  25. Phillip WoodOct 25, 2023
  26. Patrick SteinhardtOct 26, 2023
  27. Phillip WoodOct 27, 2023
  28. Patrick SteinhardtOct 26, 2023
  29. 00/12 show-ref: introduce mode to check for ref existencePatrick Steinhardt, Oct 26, 2023
  30. 01/12 builtin/show-ref: convert pattern to a local variablePatrick Steinhardt, Oct 26, 2023
  31. 02/12 builtin/show-ref: split up different subcommandsPatrick Steinhardt, Oct 26, 2023
  32. 03/12 builtin/show-ref: fix leaking string bufferPatrick Steinhardt, Oct 26, 2023
  33. Taylor BlauOct 30, 2023
  34. 04/12 builtin/show-ref: fix dead code when passing patternsPatrick Steinhardt, Oct 26, 2023
  35. Taylor BlauOct 30, 2023
  36. 05/12 builtin/show-ref: refactor `--exclude-existing` optionsPatrick Steinhardt, Oct 26, 2023
  37. Taylor BlauOct 30, 2023
  38. Patrick SteinhardtOct 31, 2023
  39. Taylor BlauOct 30, 2023
  40. Patrick SteinhardtOct 31, 2023
  41. 06/12 builtin/show-ref: stop using global variable to count matchesPatrick Steinhardt, Oct 26, 2023
  42. Taylor BlauOct 30, 2023
  43. 07/12 builtin/show-ref: stop using global vars for `show_one()`Patrick Steinhardt, Oct 26, 2023
  44. 08/12 builtin/show-ref: refactor options for patterns subcommandPatrick Steinhardt, Oct 26, 2023
  45. 09/12 builtin/show-ref: ensure mutual exclusiveness of subcommandsPatrick Steinhardt, Oct 26, 2023
  46. Taylor BlauOct 30, 2023
  47. Patrick SteinhardtOct 31, 2023
  48. 10/12 builtin/show-ref: explicitly spell out different modes in synopsisPatrick Steinhardt, Oct 26, 2023
  49. 11/12 builtin/show-ref: add new mode to check for reference existencePatrick Steinhardt, Oct 26, 2023
  50. 12/12 t: use git-show-ref(1) to check for ref existencePatrick Steinhardt, Oct 26, 2023
  51. Taylor BlauOct 30, 2023
  52. Junio C HamanoOct 31, 2023
  53. 00/12 builtin/show-ref: introduce mode to check for ref existencePatrick Steinhardt, Oct 31, 2023
  54. 01/12 builtin/show-ref: convert pattern to a local variablePatrick Steinhardt, Oct 31, 2023
  55. 02/12 builtin/show-ref: split up different subcommandsPatrick Steinhardt, Oct 31, 2023
  56. 03/12 builtin/show-ref: fix leaking string bufferPatrick Steinhardt, Oct 31, 2023
  57. 04/12 builtin/show-ref: fix dead code when passing patternsPatrick Steinhardt, Oct 31, 2023
  58. 05/12 builtin/show-ref: refactor `--exclude-existing` optionsPatrick Steinhardt, Oct 31, 2023
  59. 06/12 builtin/show-ref: stop using global variable to count matchesPatrick Steinhardt, Oct 31, 2023
  60. 07/12 builtin/show-ref: stop using global vars for `show_one()`Patrick Steinhardt, Oct 31, 2023
  61. 08/12 builtin/show-ref: refactor options for patterns subcommandPatrick Steinhardt, Oct 31, 2023
  62. 09/12 builtin/show-ref: ensure mutual exclusiveness of subcommandsPatrick Steinhardt, Oct 31, 2023
  63. 10/12 builtin/show-ref: explicitly spell out different modes in synopsisPatrick Steinhardt, Oct 31, 2023
  64. 11/12 builtin/show-ref: add new mode to check for reference existencePatrick Steinhardt, Oct 31, 2023
  65. 12/12 t: use git-show-ref(1) to check for ref existencePatrick Steinhardt, Oct 31, 2023
  66. Taylor BlauOct 31, 2023

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.