[Outreachy PATCH] parse-options: convert check-ref-format
From: Ngezayo Bonheur <bonheurngezayo4@gmail.com>
Signed-off-by: Ngezayo Bonheur <bonheurngezayo4@gmail.com>
---
builtin/check-ref-format.c | 45 ++++++++++++++++++++++++--------------
1 file changed, 29 insertions(+), 16 deletions(-)
diff --git a/builtin/check-ref-format.c b/builtin/check-ref-format.c
index fd1c9c0e0c..a7ad3c8257 100644
--- a/builtin/check-ref-format.c
+++ b/builtin/check-ref-format.c
@@ -8,11 +8,18 @@
#include "refs.h"
#include "setup.h"
#include "strbuf.h"
+#include "parse-options.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_options[] = {
+ "git check-ref-format [--normalize] [<options>] <refname>",
+ "git check-ref-format --branch <branchname-shorthand>",
+ NULL
+};
+
/*
* Return a copy of refname but with leading slashes removed and runs
* of adjacent slashes replaced with single slashes.
@@ -58,12 +65,24 @@ 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 refname"), PARSE_OPT_NONEG),
+ OPT_BOOL_F(0, "print", &normalize,
+ N_("synonym for --normalize"), PARSE_OPT_NONEG),
+ OPT_BIT(0, "allow-onelevel", &flags,
+ N_("allow a one-level refname"),
+ REFNAME_ALLOW_ONELEVEL),
+ OPT_BIT_F(0, "refspec-pattern", &flags,
+ N_("allow a refspec pattern"),
+ REFNAME_REFSPEC_PATTERN, PARSE_OPT_NONEG),
+ OPT_END()
+ };
BUG_ON_NON_EMPTY_PREFIX(prefix);
@@ -73,22 +92,16 @@ int cmd_check_ref_format(int argc,
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_options,
+ PARSE_OPT_STOP_AT_NON_OPTION |
+ PARSE_OPT_KEEP_DASHDASH);
+
+ if (argc != 1)
+ usage_with_options(builtin_check_ref_format_usage_options,
+ options);
- refname = argv[i];
+ refname = argv[0];
if (normalize)
refname = to_free = collapse_slashes(refname);
if (check_refname_format(refname, flags))
--
2.43.0