parse-opt: migrate git-merge-base.
Signed-off-by: Pierre Habouzit <madcoder@debian.org> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Pierre Habouzit committed
Oct 2, 2008 at 14:59 UTC
e5d1a4df56ad5ecffbc510c242d5acc5b15726cc
1 file changed
+16
-21
builtin-merge-base.c
+16
-21
@@ -1,6 +1,7 @@
1
#include "builtin.h"
2
#include "cache.h"
3
#include "commit.h"
4
+#include "parse-options.h"
5
6
static int show_merge_base(struct commit **rev, int rev_nr, int show_all)
7
{
@@ -21,8 +22,10 @@ static int show_merge_base(struct commit **rev, int rev_nr, int show_all)
22
return 0;
23
}
24
24
-static const char merge_base_usage[] =
25
-"git merge-base [--all] <commit-id> <commit-id>...";
25
+static const char * const merge_base_usage[] = {
26
+ "git merge-base [--all] <commit-id> <commit-id>...",
27
+ NULL
28
+};
29
30
static struct commit *get_commit_reference(const char *arg)
31
{
@@ -44,25 +47,17 @@ int cmd_merge_base(int argc, const char **argv, const char *prefix)
47
int rev_nr = 0;
48
int show_all = 0;
49
47
- git_config(git_default_config, NULL);
48
-
49
- while (1 < argc && argv[1][0] == '-') {
50
- const char *arg = argv[1];
51
- if (!strcmp(arg, "-a") || !strcmp(arg, "--all"))
52
- show_all = 1;
53
- else
54
- usage(merge_base_usage);
55
- argc--; argv++;
56
- }
57
- if (argc < 3)
58
- usage(merge_base_usage);
59
-
60
- rev = xmalloc((argc - 1) * sizeof(*rev));
61
-
62
- do {
63
- rev[rev_nr++] = get_commit_reference(argv[1]);
64
- argc--; argv++;
65
- } while (argc > 1);
50
+ struct option options[] = {
51
+ OPT_BOOLEAN('a', "all", &show_all, "outputs all common ancestors"),
52
+ OPT_END()
53
+ };
54
55
+ git_config(git_default_config, NULL);
56
+ argc = parse_options(argc, argv, options, merge_base_usage, 0);
57
+ if (argc < 2)
58
+ usage_with_options(merge_base_usage, options);
59
+ rev = xmalloc(argc * sizeof(*rev));
60
+ while (argc-- > 0)
61
+ rev[rev_nr++] = get_commit_reference(*argv++);
62
return show_merge_base(rev, rev_nr, show_all);
63
}