Make builtin-describe.c use parse_options
Signed-off-by: Pierre Habouzit <madcoder@debian.org> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Pierre Habouzit committed
Oct 7, 2007 at 20:54 UTC
166185be7c730a25308b251508aa8f1a39ea476b
1 file changed
+29
-41
builtin-describe.c
+29
-41
@@ -4,12 +4,15 @@
4
#include "refs.h"
5
#include "builtin.h"
6
#include "exec_cmd.h"
7
+#include "parse-options.h"
8
9
#define SEEN (1u<<0)
10
#define MAX_TAGS (FLAG_BITS - 1)
11
11
-static const char describe_usage[] =
12
-"git-describe [--all] [--tags] [--abbrev=<n>] <committish>*";
12
+static const char * const describe_usage[] = {
13
+ "git-describe [options] <committish>*",
14
+ NULL
15
+};
16
17
static int debug; /* Display lots of verbose info */
18
static int all; /* Default to annotated tags only */
@@ -242,57 +245,42 @@ static void describe(const char *arg, int last_one)
245
246
int cmd_describe(int argc, const char **argv, const char *prefix)
247
{
245
- int i;
248
int contains = 0;
249
+ struct option options[] = {
250
+ OPT_BOOLEAN(0, "contains", &contains, "find the tag that comes after the commit"),
251
+ OPT_BOOLEAN(0, "debug", &debug, "debug search strategy on stderr"),
252
+ OPT_BOOLEAN(0, "all", &all, "use any ref in .git/refs"),
253
+ OPT_BOOLEAN(0, "tags", &tags, "use any tag in .git/refs/tags"),
254
+ OPT__ABBREV(&abbrev),
255
+ OPT_INTEGER(0, "candidates", &max_candidates,
256
+ "consider <n> most recent tags (default: 10)"),
257
+ OPT_END(),
258
+ };
259
248
- for (i = 1; i < argc; i++) {
249
- const char *arg = argv[i];
250
-
251
- if (*arg != '-')
252
- break;
253
- else if (!strcmp(arg, "--contains"))
254
- contains = 1;
255
- else if (!strcmp(arg, "--debug"))
256
- debug = 1;
257
- else if (!strcmp(arg, "--all"))
258
- all = 1;
259
- else if (!strcmp(arg, "--tags"))
260
- tags = 1;
261
- else if (!prefixcmp(arg, "--abbrev=")) {
262
- abbrev = strtoul(arg + 9, NULL, 10);
263
- if (abbrev != 0 && (abbrev < MINIMUM_ABBREV || 40 < abbrev))
264
- abbrev = DEFAULT_ABBREV;
265
- }
266
- else if (!prefixcmp(arg, "--candidates=")) {
267
- max_candidates = strtoul(arg + 13, NULL, 10);
268
- if (max_candidates < 1)
269
- max_candidates = 1;
270
- else if (max_candidates > MAX_TAGS)
271
- max_candidates = MAX_TAGS;
272
- }
273
- else
274
- usage(describe_usage);
275
- }
260
+ argc = parse_options(argc, argv, options, describe_usage, 0);
261
+ if (max_candidates < 1)
262
+ max_candidates = 1;
263
+ else if (max_candidates > MAX_TAGS)
264
+ max_candidates = MAX_TAGS;
265
266
save_commit_buffer = 0;
267
268
if (contains) {
280
- const char **args = xmalloc((4 + argc - i) * sizeof(char*));
269
+ const char **args = xmalloc((4 + argc) * sizeof(char*));
270
args[0] = "name-rev";
271
args[1] = "--name-only";
272
args[2] = "--tags";
284
- memcpy(args + 3, argv + i, (argc - i) * sizeof(char*));
285
- args[3 + argc - i] = NULL;
286
- return cmd_name_rev(3 + argc - i, args, prefix);
273
+ memcpy(args + 3, argv, argc * sizeof(char*));
274
+ args[3 + argc] = NULL;
275
+ return cmd_name_rev(3 + argc, args, prefix);
276
}
277
289
- if (argc <= i)
278
+ if (argc == 0) {
279
describe("HEAD", 1);
291
- else
292
- while (i < argc) {
293
- describe(argv[i], (i == argc - 1));
294
- i++;
280
+ } else {
281
+ while (argc-- > 0) {
282
+ describe(*argv++, argc == 0);
283
}
296
-
284
+ }
285
return 0;
286
}