|
4 | 4 | #include "object.h" |
5 | 5 | #include "tag.h" |
6 | 6 | #include "string-list.h" |
| 7 | +#include "parse-options.h" |
7 | 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 | +}; |
9 | 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; |
12 | 17 | static const char **pattern; |
| 18 | +static const char *exclude_existing_arg; |
13 | 19 |
|
14 | 20 | static void show_one(const char *refname, const unsigned char *sha1) |
15 | 21 | { |
@@ -150,79 +156,60 @@ static int exclude_existing(const char *match) |
150 | 156 | return 0; |
151 | 157 | } |
152 | 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 | + |
153 | 202 | int cmd_show_ref(int argc, const char **argv, const char *prefix) |
154 | 203 | { |
155 | | - int i; |
| 204 | + argc = parse_options(argc, argv, prefix, show_ref_options, |
| 205 | + show_ref_usage, PARSE_OPT_NO_INTERNAL_HELP); |
156 | 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; |
226 | 213 |
|
227 | 214 | if (verify) { |
228 | 215 | if (!pattern) |
|
0 commit comments