All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Husain Sitamuwala via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Husain Sitamuwala <husainsitamuwala21@gmail.com>,
	Hussain Sitamouwala <husainsitamuwala21@gmail.com>
Subject: [PATCH v2] check-ref-format: use parse-options API
Date: Tue, 03 Nov 2026 08:33:33 +0000	[thread overview]
Message-ID: <pull.2470.v2.git.git.1793694813386.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.2470.git.git.1793298807397.gitgitgadget@gmail.com>

From: Hussain Sitamouwala <husainsitamuwala21@gmail.com>

The command parses its options by hand, comparing each argument
against string literals. Use the parse-options API instead so that
option handling is consistent with other builtins and the help
output is generated from the option table.

Keep the existing behavior: --print stays a hidden, deprecated
synonym for --normalize, only --allow-onelevel can be negated, and
parsing stops at the first non-option argument so that options
after <refname> are still rejected.

Drop the early show_usage_if_asked() call, so that -h is handled by
parse_options().

Two minor differences come with parse_options(): unambiguous
abbreviations of long options, such as --norm, are accepted, and
"--" may now precede <refname>.

Signed-off-by: Hussain Sitamouwala <husainsitamuwala21@gmail.com>
---
    check-ref-format: use parse-options API

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2470%2FCodeWithHusain%2Foutreachy-microproject-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2470/CodeWithHusain/outreachy-microproject-v2
Pull-Request: https://github.com/git/git/pull/2470

Range-diff vs v1:

 1:  9f05296454 ! 1:  991ca870fe check-ref-format: use parse-options API
     @@ Metadata
       ## Commit message ##
          check-ref-format: use parse-options API
      
     -    Use the parse-options API to handle command-line options
     -    consistently with other Git builtins.
     +    The command parses its options by hand, comparing each argument
     +    against string literals. Use the parse-options API instead so that
     +    option handling is consistent with other builtins and the help
     +    output is generated from the option table.
      
     -    The command currently parses options manually, duplicating
     -    argument-handling logic. Using parse-options makes option
     -    handling consistent with the rest of Git.
     +    Keep the existing behavior: --print stays a hidden, deprecated
     +    synonym for --normalize, only --allow-onelevel can be negated, and
     +    parsing stops at the first non-option argument so that options
     +    after <refname> are still rejected.
      
     -    Replace manual option parsing while preserving support for
     -    normalization, branch names, and refname validation.
     +    Drop the early show_usage_if_asked() call, so that -h is handled by
     +    parse_options().
     +
     +    Two minor differences come with parse_options(): unambiguous
     +    abbreviations of long options, such as --norm, are accepted, and
     +    "--" may now precede <refname>.
      
          Signed-off-by: Hussain Sitamouwala <husainsitamuwala21@gmail.com>
      
     @@ builtin/check-ref-format.c
       #include "setup.h"
       #include "strbuf.h"
       
     - static const char builtin_check_ref_format_usage[] =
     +-static const char builtin_check_ref_format_usage[] =
      -"git check-ref-format [--normalize] [<options>] <refname>\n"
      -"   or: git check-ref-format --branch <branchname-shorthand>";
     -+	"git check-ref-format [--normalize] [<options>] <refname>\n"
     -+	"   or: git check-ref-format --branch <branchname-shorthand>";
     -+
     -+static const char * const builtin_check_ref_format_usages[] = {
     -+	"git check-ref-format [--normalize] [<options>] <refname>",
     -+	"   or: git check-ref-format --branch <branchname-shorthand>",
     ++static const char * const builtin_check_ref_format_usage[] = {
     ++	N_("git check-ref-format [--normalize] [<options>] <refname>"),
     ++	N_("git check-ref-format --branch <branchname-shorthand>"),
      +	NULL
      +};
       
     @@ builtin/check-ref-format.c: int cmd_check_ref_format(int argc,
       	const char *refname;
       	char *to_free = NULL;
       	int ret = 1;
     - 
      +	struct option options[] = {
     -+		OPT_BOOL(0, "normalize", &normalize,
     -+			 N_("normalize refname")),
     -+		OPT_BOOL(0, "print", &normalize,
     -+			 N_("print normalized refname")),
     ++		OPT_BOOL_F(0, "normalize", &normalize,
     ++			   N_("normalize the refname and print it"),
     ++			   PARSE_OPT_NONEG),
     ++		OPT_BOOL_F(0, "print", &normalize,
     ++			   N_("deprecated synonym for --normalize"),
     ++			   PARSE_OPT_NONEG | PARSE_OPT_HIDDEN),
      +		OPT_BIT(0, "allow-onelevel", &flags,
     -+			N_("allow one-level refnames"),
     ++			N_("accept a refname with a single level"),
      +			REFNAME_ALLOW_ONELEVEL),
     -+		OPT_BIT(0, "refspec-pattern", &flags,
     -+			N_("allow refspec patterns"),
     -+			REFNAME_REFSPEC_PATTERN),
     ++		OPT_BIT_F(0, "refspec-pattern", &flags,
     ++			  N_("interpret <refname> as a reference pattern for a refspec"),
     ++			  REFNAME_REFSPEC_PATTERN, PARSE_OPT_NONEG),
      +		OPT_END()
      +	};
     -+
     + 
       	BUG_ON_NON_EMPTY_PREFIX(prefix);
       
     - 	show_usage_if_asked(argc, argv,
     -@@ builtin/check-ref-format.c: int cmd_check_ref_format(int argc,
     +-	show_usage_if_asked(argc, argv,
     +-			    builtin_check_ref_format_usage);
     +-
       	if (argc == 3 && !strcmp(argv[1], "--branch"))
       		return check_ref_format_branch(argv[2]);
       
     @@ builtin/check-ref-format.c: int cmd_check_ref_format(int argc,
      -			usage(builtin_check_ref_format_usage);
      -	}
      -	if (! (i == argc - 1))
     +-		usage(builtin_check_ref_format_usage);
      +	argc = parse_options(argc, argv, prefix, options,
     -+			     builtin_check_ref_format_usages, 0);
     -+
     ++			     builtin_check_ref_format_usage,
     ++			     PARSE_OPT_STOP_AT_NON_OPTION);
      +	if (argc != 1)
     - 		usage(builtin_check_ref_format_usage);
     ++		usage_with_options(builtin_check_ref_format_usage, options);
       
      -	refname = argv[i];
      +	refname = argv[0];


 builtin/check-ref-format.c | 49 +++++++++++++++++++++-----------------
 1 file changed, 27 insertions(+), 22 deletions(-)

diff --git a/builtin/check-ref-format.c b/builtin/check-ref-format.c
index fd1c9c0e0c..d698da99d9 100644
--- a/builtin/check-ref-format.c
+++ b/builtin/check-ref-format.c
@@ -5,13 +5,16 @@
 #define USE_THE_REPOSITORY_VARIABLE
 
 #include "builtin.h"
+#include "parse-options.h"
 #include "refs.h"
 #include "setup.h"
 #include "strbuf.h"
 
-static const char builtin_check_ref_format_usage[] =
-"git check-ref-format [--normalize] [<options>] <refname>\n"
-"   or: git check-ref-format --branch <branchname-shorthand>";
+static const char * const builtin_check_ref_format_usage[] = {
+	N_("git check-ref-format [--normalize] [<options>] <refname>"),
+	N_("git check-ref-format --branch <branchname-shorthand>"),
+	NULL
+};
 
 /*
  * Return a copy of refname but with leading slashes removed and runs
@@ -58,37 +61,39 @@ int cmd_check_ref_format(int argc,
 			 const char *prefix,
 			 struct repository *repo UNUSED)
 {
-	int i;
 	int normalize = 0;
 	int flags = 0;
 	const char *refname;
 	char *to_free = NULL;
 	int ret = 1;
+	struct option options[] = {
+		OPT_BOOL_F(0, "normalize", &normalize,
+			   N_("normalize the refname and print it"),
+			   PARSE_OPT_NONEG),
+		OPT_BOOL_F(0, "print", &normalize,
+			   N_("deprecated synonym for --normalize"),
+			   PARSE_OPT_NONEG | PARSE_OPT_HIDDEN),
+		OPT_BIT(0, "allow-onelevel", &flags,
+			N_("accept a refname with a single level"),
+			REFNAME_ALLOW_ONELEVEL),
+		OPT_BIT_F(0, "refspec-pattern", &flags,
+			  N_("interpret <refname> as a reference pattern for a refspec"),
+			  REFNAME_REFSPEC_PATTERN, PARSE_OPT_NONEG),
+		OPT_END()
+	};
 
 	BUG_ON_NON_EMPTY_PREFIX(prefix);
 
-	show_usage_if_asked(argc, argv,
-			    builtin_check_ref_format_usage);
-
 	if (argc == 3 && !strcmp(argv[1], "--branch"))
 		return check_ref_format_branch(argv[2]);
 
-	for (i = 1; i < argc && argv[i][0] == '-'; i++) {
-		if (!strcmp(argv[i], "--normalize") || !strcmp(argv[i], "--print"))
-			normalize = 1;
-		else if (!strcmp(argv[i], "--allow-onelevel"))
-			flags |= REFNAME_ALLOW_ONELEVEL;
-		else if (!strcmp(argv[i], "--no-allow-onelevel"))
-			flags &= ~REFNAME_ALLOW_ONELEVEL;
-		else if (!strcmp(argv[i], "--refspec-pattern"))
-			flags |= REFNAME_REFSPEC_PATTERN;
-		else
-			usage(builtin_check_ref_format_usage);
-	}
-	if (! (i == argc - 1))
-		usage(builtin_check_ref_format_usage);
+	argc = parse_options(argc, argv, prefix, options,
+			     builtin_check_ref_format_usage,
+			     PARSE_OPT_STOP_AT_NON_OPTION);
+	if (argc != 1)
+		usage_with_options(builtin_check_ref_format_usage, options);
 
-	refname = argv[i];
+	refname = argv[0];
 	if (normalize)
 		refname = to_free = collapse_slashes(refname);
 	if (check_refname_format(refname, flags))

base-commit: b9e5cff1803ab24582201ef059516eb16014b9d9
-- 
gitgitgadget

  parent reply	other threads:[~2026-11-03  8:33 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-29 18:33 [PATCH] check-ref-format: use parse-options API Husain Sitamuwala via GitGitGadget
2026-10-29 20:09 ` Junio C Hamano
2026-11-03  8:33 ` Husain Sitamuwala via GitGitGadget [this message]
2026-11-03 10:00   ` [PATCH v2] " Karthik Nayak
2026-11-03 18:46     ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=pull.2470.v2.git.git.1793694813386.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=husainsitamuwala21@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.