diff mbox series

[6/8] reflog exists: use parse_options() API

Message ID patch-6.8-f88acdc9702-20220317T180439Z-avarab@gmail.com (mailing list archive)
State Accepted
Commit a34393f5f8152ad8bdbf15bccffc2be12a9a19ca
Headers show
Series reflog: migrate fully to parse_options() | expand

Commit Message

Ævar Arnfjörð Bjarmason March 17, 2022, 6:08 p.m. UTC
Change the "reflog exists" command added in afcb2e7a3b8 (git-reflog:
add exists command, 2015-07-21) to use parse_options() instead of its
own custom command-line parser. This continues work started in
33d7bdd6459 (builtin/reflog.c: use parse-options api for expire,
delete subcommands, 2022-01-06).

As a result we'll understand the --end-of-options synonym for "--", so
let's test for that.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/reflog.c         | 38 ++++++++++++++++----------------------
 t/t1418-reflog-exists.sh |  5 +++++
 2 files changed, 21 insertions(+), 22 deletions(-)

Comments

Junio C Hamano March 18, 2022, 1:30 a.m. UTC | #1
Ævar Arnfjörð Bjarmason  <avarab@gmail.com> writes:

> Change the "reflog exists" command added in afcb2e7a3b8 (git-reflog:
> add exists command, 2015-07-21) to use parse_options() instead of its
> own custom command-line parser. This continues work started in
> 33d7bdd6459 (builtin/reflog.c: use parse-options api for expire,
> delete subcommands, 2022-01-06).
>
> As a result we'll understand the --end-of-options synonym for "--", so
> let's test for that.
>
> Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
> ---
>  builtin/reflog.c         | 38 ++++++++++++++++----------------------
>  t/t1418-reflog-exists.sh |  5 +++++
>  2 files changed, 21 insertions(+), 22 deletions(-)
>
> diff --git a/builtin/reflog.c b/builtin/reflog.c
> index 458764400b5..9847e9db3de 100644
> --- a/builtin/reflog.c
> +++ b/builtin/reflog.c
> @@ -27,8 +27,10 @@ static const char *const reflog_delete_usage[] = {
>  	NULL
>  };
>  
> -static const char reflog_exists_usage[] =
> -	BUILTIN_REFLOG_EXISTS_USAGE;
> +static const char *const reflog_exists_usage[] = {
> +	BUILTIN_REFLOG_EXISTS_USAGE,
> +	NULL,
> +};

Good.

The discrepanthy in the postimage of [PATCH 4/8] bothered me a lot.

This would immediately help by making it possible to feed it to
parse_options(), but the uniformity will also make it easier to
concatenate common and subcommand specific usage later, I would
presume, if we wanted to.

> +	struct option options[] = {
> +		OPT_END()
> +	};
> +	const char *refname;
>  
> +	argc = parse_options(argc, argv, prefix, options, reflog_exists_usage,
> +			     0);
> +	if (!argc)
> +		usage_with_options(reflog_exists_usage, options);
>  
> +	refname = argv[0];
> +	if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL))
> +		die(_("invalid ref format: %s"), refname);
> +	return !reflog_exists(refname);
>  }

Quite straight-forward.  Looking good.
diff mbox series

Patch

diff --git a/builtin/reflog.c b/builtin/reflog.c
index 458764400b5..9847e9db3de 100644
--- a/builtin/reflog.c
+++ b/builtin/reflog.c
@@ -27,8 +27,10 @@  static const char *const reflog_delete_usage[] = {
 	NULL
 };
 
-static const char reflog_exists_usage[] =
-	BUILTIN_REFLOG_EXISTS_USAGE;
+static const char *const reflog_exists_usage[] = {
+	BUILTIN_REFLOG_EXISTS_USAGE,
+	NULL,
+};
 
 static timestamp_t default_reflog_expire;
 static timestamp_t default_reflog_expire_unreachable;
@@ -350,28 +352,20 @@  static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
 
 static int cmd_reflog_exists(int argc, const char **argv, const char *prefix)
 {
-	int i, start = 0;
-
-	for (i = 1; i < argc; i++) {
-		const char *arg = argv[i];
-		if (!strcmp(arg, "--")) {
-			i++;
-			break;
-		}
-		else if (arg[0] == '-')
-			usage(_(reflog_exists_usage));
-		else
-			break;
-	}
-
-	start = i;
+	struct option options[] = {
+		OPT_END()
+	};
+	const char *refname;
 
-	if (argc - start != 1)
-		usage(_(reflog_exists_usage));
+	argc = parse_options(argc, argv, prefix, options, reflog_exists_usage,
+			     0);
+	if (!argc)
+		usage_with_options(reflog_exists_usage, options);
 
-	if (check_refname_format(argv[start], REFNAME_ALLOW_ONELEVEL))
-		die(_("invalid ref format: %s"), argv[start]);
-	return !reflog_exists(argv[start]);
+	refname = argv[0];
+	if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL))
+		die(_("invalid ref format: %s"), refname);
+	return !reflog_exists(refname);
 }
 
 /*
diff --git a/t/t1418-reflog-exists.sh b/t/t1418-reflog-exists.sh
index 60c6411ce3c..d51ecd5e925 100755
--- a/t/t1418-reflog-exists.sh
+++ b/t/t1418-reflog-exists.sh
@@ -29,4 +29,9 @@  test_expect_success 'reflog exists works with a "--" delimiter' '
 	test_must_fail git reflog exists -- refs/heads/nonexistent
 '
 
+test_expect_success 'reflog exists works with a "--end-of-options" delimiter' '
+	git reflog exists --end-of-options refs/heads/main &&
+	test_must_fail git reflog exists --end-of-options refs/heads/nonexistent
+'
+
 test_done