Make builtin-name-rev.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 15, 2007 at 22:57 UTC
edefb1a23755505cc9b4e750c292cd236b1e8655
1 file changed
+25
-39
builtin-name-rev.c
+25
-39
@@ -3,12 +3,10 @@
3
#include "commit.h"
4
#include "tag.h"
5
#include "refs.h"
6
+#include "parse-options.h"
7
8
#define CUTOFF_DATE_SLOP 86400 /* one day */
9
9
-static const char name_rev_usage[] =
10
- "git-name-rev [--tags | --refs=<pattern>] ( --all | --stdin | committish [committish...] )\n";
11
-
10
typedef struct rev_name {
11
const char *tip_name;
12
int generation;
@@ -153,51 +151,41 @@ static const char* get_rev_name(struct object *o)
151
}
152
}
153
154
+static char const * const name_rev_usage[] = {
155
+ "git-name-rev [options] ( --all | --stdin | <commit>... )",
156
+ NULL
157
+};
158
+
159
int cmd_name_rev(int argc, const char **argv, const char *prefix)
160
{
161
struct object_array revs = { 0, 0, NULL };
159
- int as_is = 0, all = 0, transform_stdin = 0;
162
+ int all = 0, transform_stdin = 0;
163
struct name_ref_data data = { 0, 0, NULL };
164
+ struct option opts[] = {
165
+ OPT_BOOLEAN(0, "name-only", &data.name_only, "print only names (no SHA-1)"),
166
+ OPT_BOOLEAN(0, "tags", &data.tags_only, "only use tags to name the commits"),
167
+ OPT_STRING(0, "refs", &data.ref_filter, "pattern",
168
+ "only use refs matching <pattern>"),
169
+ OPT_GROUP(""),
170
+ OPT_BOOLEAN(0, "all", &all, "list all commits reachable from all refs"),
171
+ OPT_BOOLEAN(0, "stdin", &transform_stdin, "read from stdin"),
172
+ OPT_END(),
173
+ };
174
175
git_config(git_default_config);
176
+ argc = parse_options(argc, argv, opts, name_rev_usage, 0);
177
+ if (!!all + !!transform_stdin + !!argc > 1) {
178
+ error("Specify either a list, or --all, not both!");
179
+ usage_with_options(name_rev_usage, opts);
180
+ }
181
+ if (all || transform_stdin)
182
+ cutoff = 0;
183
164
- if (argc < 2)
165
- usage(name_rev_usage);
166
-
167
- for (--argc, ++argv; argc; --argc, ++argv) {
184
+ for (; argc; argc--, argv++) {
185
unsigned char sha1[20];
186
struct object *o;
187
struct commit *commit;
188
172
- if (!as_is && (*argv)[0] == '-') {
173
- if (!strcmp(*argv, "--")) {
174
- as_is = 1;
175
- continue;
176
- } else if (!strcmp(*argv, "--name-only")) {
177
- data.name_only = 1;
178
- continue;
179
- } else if (!strcmp(*argv, "--tags")) {
180
- data.tags_only = 1;
181
- continue;
182
- } else if (!prefixcmp(*argv, "--refs=")) {
183
- data.ref_filter = *argv + 7;
184
- continue;
185
- } else if (!strcmp(*argv, "--all")) {
186
- if (argc > 1)
187
- die("Specify either a list, or --all, not both!");
188
- all = 1;
189
- cutoff = 0;
190
- continue;
191
- } else if (!strcmp(*argv, "--stdin")) {
192
- if (argc > 1)
193
- die("Specify either a list, or --stdin, not both!");
194
- transform_stdin = 1;
195
- cutoff = 0;
196
- continue;
197
- }
198
- usage(name_rev_usage);
199
- }
200
-
189
if (get_sha1(*argv, sha1)) {
190
fprintf(stderr, "Could not get sha1 for %s. Skipping.\n",
191
*argv);
@@ -212,10 +200,8 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
200
}
201
202
commit = (struct commit *)o;
215
-
203
if (cutoff > commit->date)
204
cutoff = commit->date;
218
-
205
add_object_array((struct object *)commit, *argv, &revs);
206
}
207