Skip to content

Commit 69932bc

Browse files
bebarinogitster
authored andcommitted
show-ref: migrate to parse-options
Also make the docs more consistent with the usage message. While we're here remove the zero initializers from the static variables as they're unnecessary. Signed-off-by: Stephen Boyd <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c5764c0 commit 69932bc

File tree

2 files changed

+67
-81
lines changed

2 files changed

+67
-81
lines changed

Documentation/git-show-ref.txt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ SYNOPSIS
99
--------
1010
[verse]
1111
'git show-ref' [-q|--quiet] [--verify] [-h|--head] [-d|--dereference]
12-
[-s|--hash] [--abbrev] [--tags] [--heads] [--] <pattern>...
13-
'git show-ref' --exclude-existing[=pattern]
12+
[-s|--hash[=<n>]] [--abbrev[=<n>]] [--tags]
13+
[--heads] [--] <pattern>...
14+
'git show-ref' --exclude-existing[=<pattern>] < ref-list
1415

1516
DESCRIPTION
1617
-----------
@@ -48,7 +49,7 @@ OPTIONS
4849
appended.
4950

5051
-s::
51-
--hash::
52+
--hash[=<n>]::
5253

5354
Only show the SHA1 hash, not the reference name. When combined with
5455
--dereference the dereferenced tag will still be shown after the SHA1.
@@ -59,20 +60,18 @@ OPTIONS
5960
Aside from returning an error code of 1, it will also print an error
6061
message if '--quiet' was not specified.
6162

62-
--abbrev::
63-
--abbrev=len::
63+
--abbrev[=<n>]::
6464

6565
Abbreviate the object name. When using `--hash`, you do
66-
not have to say `--hash --abbrev`; `--hash=len` would do.
66+
not have to say `--hash --abbrev`; `--hash=n` would do.
6767

6868
-q::
6969
--quiet::
7070

7171
Do not print any results to stdout. When combined with '--verify' this
7272
can be used to silently check if a reference exists.
7373

74-
--exclude-existing::
75-
--exclude-existing=pattern::
74+
--exclude-existing[=<pattern>]::
7675

7776
Make 'git-show-ref' act as a filter that reads refs from stdin of the
7877
form "^(?:<anything>\s)?<refname>(?:\^\{\})?$" and performs the

builtin-show-ref.c

Lines changed: 60 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@
44
#include "object.h"
55
#include "tag.h"
66
#include "string-list.h"
7+
#include "parse-options.h"
78

8-
static const char show_ref_usage[] = "git show-ref [-q|--quiet] [--verify] [-h|--head] [-d|--dereference] [-s|--hash[=<length>]] [--abbrev[=<length>]] [--tags] [--heads] [--] [pattern*] < ref-list";
9+
static const char * const show_ref_usage[] = {
10+
"git show-ref [-q|--quiet] [--verify] [-h|--head] [-d|--dereference] [-s|--hash[=<n>]] [--abbrev[=<n>]] [--tags] [--heads] [--] [pattern*] ",
11+
"git show-ref --exclude-existing[=pattern] < ref-list",
12+
NULL
13+
};
914

10-
static int deref_tags = 0, show_head = 0, tags_only = 0, heads_only = 0,
11-
found_match = 0, verify = 0, quiet = 0, hash_only = 0, abbrev = 0;
15+
static int deref_tags, show_head, tags_only, heads_only, found_match, verify,
16+
quiet, hash_only, abbrev, exclude_arg;
1217
static const char **pattern;
18+
static const char *exclude_existing_arg;
1319

1420
static void show_one(const char *refname, const unsigned char *sha1)
1521
{
@@ -150,79 +156,60 @@ static int exclude_existing(const char *match)
150156
return 0;
151157
}
152158

159+
static int hash_callback(const struct option *opt, const char *arg, int unset)
160+
{
161+
hash_only = 1;
162+
/* Use full length SHA1 if no argument */
163+
if (!arg)
164+
return 0;
165+
return parse_opt_abbrev_cb(opt, arg, unset);
166+
}
167+
168+
static int exclude_existing_callback(const struct option *opt, const char *arg,
169+
int unset)
170+
{
171+
exclude_arg = 1;
172+
*(const char **)opt->value = arg;
173+
return 0;
174+
}
175+
176+
static int help_callback(const struct option *opt, const char *arg, int unset)
177+
{
178+
return -1;
179+
}
180+
181+
static const struct option show_ref_options[] = {
182+
OPT_BOOLEAN(0, "tags", &tags_only, "only show tags (can be combined with heads)"),
183+
OPT_BOOLEAN(0, "heads", &heads_only, "only show heads (can be combined with tags)"),
184+
OPT_BOOLEAN(0, "verify", &verify, "stricter reference checking, "
185+
"requires exact ref path"),
186+
OPT_BOOLEAN('h', "head", &show_head, "show the HEAD reference"),
187+
OPT_BOOLEAN('d', "dereference", &deref_tags,
188+
"dereference tags into object IDs"),
189+
{ OPTION_CALLBACK, 's', "hash", &abbrev, "n",
190+
"only show SHA1 hash using <n> digits",
191+
PARSE_OPT_OPTARG, &hash_callback },
192+
OPT__ABBREV(&abbrev),
193+
OPT__QUIET(&quiet),
194+
{ OPTION_CALLBACK, 0, "exclude-existing", &exclude_existing_arg,
195+
"pattern", "show refs from stdin that aren't in local repository",
196+
PARSE_OPT_OPTARG | PARSE_OPT_NONEG, exclude_existing_callback },
197+
{ OPTION_CALLBACK, 0, "help-all", NULL, NULL, "show usage",
198+
PARSE_OPT_HIDDEN | PARSE_OPT_NOARG, help_callback },
199+
OPT_END()
200+
};
201+
153202
int cmd_show_ref(int argc, const char **argv, const char *prefix)
154203
{
155-
int i;
204+
argc = parse_options(argc, argv, prefix, show_ref_options,
205+
show_ref_usage, PARSE_OPT_NO_INTERNAL_HELP);
156206

157-
for (i = 1; i < argc; i++) {
158-
const char *arg = argv[i];
159-
if (*arg != '-') {
160-
pattern = argv + i;
161-
break;
162-
}
163-
if (!strcmp(arg, "--")) {
164-
pattern = argv + i + 1;
165-
if (!*pattern)
166-
pattern = NULL;
167-
break;
168-
}
169-
if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet")) {
170-
quiet = 1;
171-
continue;
172-
}
173-
if (!strcmp(arg, "-h") || !strcmp(arg, "--head")) {
174-
show_head = 1;
175-
continue;
176-
}
177-
if (!strcmp(arg, "-d") || !strcmp(arg, "--dereference")) {
178-
deref_tags = 1;
179-
continue;
180-
}
181-
if (!strcmp(arg, "-s") || !strcmp(arg, "--hash")) {
182-
hash_only = 1;
183-
continue;
184-
}
185-
if (!prefixcmp(arg, "--hash=") ||
186-
(!prefixcmp(arg, "--abbrev") &&
187-
(arg[8] == '=' || arg[8] == '\0'))) {
188-
if (arg[2] != 'h' && !arg[8])
189-
/* --abbrev only */
190-
abbrev = DEFAULT_ABBREV;
191-
else {
192-
/* --hash= or --abbrev= */
193-
char *end;
194-
if (arg[2] == 'h') {
195-
hash_only = 1;
196-
arg += 7;
197-
}
198-
else
199-
arg += 9;
200-
abbrev = strtoul(arg, &end, 10);
201-
if (*end || abbrev > 40)
202-
usage(show_ref_usage);
203-
if (abbrev < MINIMUM_ABBREV)
204-
abbrev = MINIMUM_ABBREV;
205-
}
206-
continue;
207-
}
208-
if (!strcmp(arg, "--verify")) {
209-
verify = 1;
210-
continue;
211-
}
212-
if (!strcmp(arg, "--tags")) {
213-
tags_only = 1;
214-
continue;
215-
}
216-
if (!strcmp(arg, "--heads")) {
217-
heads_only = 1;
218-
continue;
219-
}
220-
if (!strcmp(arg, "--exclude-existing"))
221-
return exclude_existing(NULL);
222-
if (!prefixcmp(arg, "--exclude-existing="))
223-
return exclude_existing(arg + 19);
224-
usage(show_ref_usage);
225-
}
207+
if (exclude_arg)
208+
return exclude_existing(exclude_existing_arg);
209+
210+
pattern = argv;
211+
if (!*pattern)
212+
pattern = NULL;
226213

227214
if (verify) {
228215
if (!pattern)

0 commit comments

Comments
 (0)