@matheusbsbs / matheus-bagliano / commits / 69932bc611
Fork 0

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

Stephen Boyd committed Jun 20, 2009 at 21:40 UTC 69932bc6117d7ec2689557693ad300756c961cdc
2 files changed +67 -81
Documentation/git-show-ref.txt
+7 -8
@@ -9,8 +9,9 @@ SYNOPSIS
9 --------
10 [verse]
11 '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
15
16 DESCRIPTION
17 -----------
@@ -48,7 +49,7 @@ OPTIONS
49 appended.
50
51 -s::
51 ---hash::
52 +--hash[=<n>]::
53
54 Only show the SHA1 hash, not the reference name. When combined with
55 --dereference the dereferenced tag will still be shown after the SHA1.
@@ -59,11 +60,10 @@ OPTIONS
60 Aside from returning an error code of 1, it will also print an error
61 message if '--quiet' was not specified.
62
62 ---abbrev::
63 ---abbrev=len::
63 +--abbrev[=<n>]::
64
65 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.
67
68 -q::
69 --quiet::
@@ -71,8 +71,7 @@ OPTIONS
71 Do not print any results to stdout. When combined with '--verify' this
72 can be used to silently check if a reference exists.
73
74 ---exclude-existing::
75 ---exclude-existing=pattern::
74 +--exclude-existing[=<pattern>]::
75
76 Make 'git-show-ref' act as a filter that reads refs from stdin of the
77 form "^(?:<anything>\s)?<refname>(?:\^\{\})?$" and performs the
builtin-show-ref.c
+60 -73
@@ -4,12 +4,18 @@
4 #include "object.h"
5 #include "tag.h"
6 #include "string-list.h"
7 +#include "parse-options.h"
8
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 +};
14
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;
17 static const char **pattern;
18 +static const char *exclude_existing_arg;
19
20 static void show_one(const char *refname, const unsigned char *sha1)
21 {
@@ -150,79 +156,60 @@ static int exclude_existing(const char *match)
156 return 0;
157 }
158
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 +
202 int cmd_show_ref(int argc, const char **argv, const char *prefix)
203 {
155 - int i;
204 + argc = parse_options(argc, argv, prefix, show_ref_options,
205 + show_ref_usage, PARSE_OPT_NO_INTERNAL_HELP);
206
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;
213
214 if (verify) {
215 if (!pattern)