reflog exists: use parse_options() API

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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Mar 17, 2022 at 19:08 UTC a34393f5f8152ad8bdbf15bccffc2be12a9a19ca
2 files changed +21 -22
builtin/reflog.c
+16 -22
@@ -27,8 +27,10 @@ static const char *const reflog_delete_usage[] = {
27 NULL
28 };
29
30 -static const char reflog_exists_usage[] =
31 - BUILTIN_REFLOG_EXISTS_USAGE;
30 +static const char *const reflog_exists_usage[] = {
31 + BUILTIN_REFLOG_EXISTS_USAGE,
32 + NULL,
33 +};
34
35 static timestamp_t default_reflog_expire;
36 static timestamp_t default_reflog_expire_unreachable;
@@ -350,28 +352,20 @@ static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
352
353 static int cmd_reflog_exists(int argc, const char **argv, const char *prefix)
354 {
353 - int i, start = 0;
354 -
355 - for (i = 1; i < argc; i++) {
356 - const char *arg = argv[i];
357 - if (!strcmp(arg, "--")) {
358 - i++;
359 - break;
360 - }
361 - else if (arg[0] == '-')
362 - usage(_(reflog_exists_usage));
363 - else
364 - break;
365 - }
366 -
367 - start = i;
355 + struct option options[] = {
356 + OPT_END()
357 + };
358 + const char *refname;
359
369 - if (argc - start != 1)
370 - usage(_(reflog_exists_usage));
360 + argc = parse_options(argc, argv, prefix, options, reflog_exists_usage,
361 + 0);
362 + if (!argc)
363 + usage_with_options(reflog_exists_usage, options);
364
372 - if (check_refname_format(argv[start], REFNAME_ALLOW_ONELEVEL))
373 - die(_("invalid ref format: %s"), argv[start]);
374 - return !reflog_exists(argv[start]);
365 + refname = argv[0];
366 + if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL))
367 + die(_("invalid ref format: %s"), refname);
368 + return !reflog_exists(refname);
369 }
370
371 /*
t/t1418-reflog-exists.sh
+5
@@ -29,4 +29,9 @@ test_expect_success 'reflog exists works with a "--" delimiter' '
29 test_must_fail git reflog exists -- refs/heads/nonexistent
30 '
31
32 +test_expect_success 'reflog exists works with a "--end-of-options" delimiter' '
33 + git reflog exists --end-of-options refs/heads/main &&
34 + test_must_fail git reflog exists --end-of-options refs/heads/nonexistent
35 +'
36 +
37 test_done