Skip to content

Commit a34393f

Browse files
avargitster
authored andcommitted
reflog exists: use parse_options() API
Change the "reflog exists" command added in afcb2e7 (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 33d7bdd (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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cbe4852 commit a34393f

File tree

2 files changed

+21
-22
lines changed

2 files changed

+21
-22
lines changed

builtin/reflog.c

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ static const char *const reflog_delete_usage[] = {
2727
NULL
2828
};
2929

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+
};
3234

3335
static timestamp_t default_reflog_expire;
3436
static timestamp_t default_reflog_expire_unreachable;
@@ -350,28 +352,20 @@ static int cmd_reflog_delete(int argc, const char **argv, const char *prefix)
350352

351353
static int cmd_reflog_exists(int argc, const char **argv, const char *prefix)
352354
{
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;
368359

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);
371364

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);
375369
}
376370

377371
/*

t/t1418-reflog-exists.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,9 @@ test_expect_success 'reflog exists works with a "--" delimiter' '
2929
test_must_fail git reflog exists -- refs/heads/nonexistent
3030
'
3131

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+
3237
test_done

0 commit comments

Comments
 (0)