@matheusbsbs / matheus-bagliano / commits / f81037947c
Fork 0

Make builtin-revert.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 23:02 UTC f81037947c518703cefee2ada5a5d7f724191bc6
1 file changed +30 -37
builtin-revert.c
+30 -37
@@ -7,6 +7,7 @@
7 #include "run-command.h"
8 #include "exec_cmd.h"
9 #include "utf8.h"
10 +#include "parse-options.h"
11
12 /*
13 * This implements the builtins revert and cherry-pick.
@@ -19,51 +20,42 @@
20 * Copyright (c) 2005 Junio C Hamano
21 */
22
22 -static const char *revert_usage = "git-revert [--edit | --no-edit] [-n] <commit-ish>";
23 +static const char * const revert_usage[] = {
24 + "git-revert [options] <commit-ish>",
25 + NULL
26 +};
27
24 -static const char *cherry_pick_usage = "git-cherry-pick [--edit] [-n] [-r] [-x] <commit-ish>";
28 +static const char * const cherry_pick_usage[] = {
29 + "git-cherry-pick [options] <commit-ish>",
30 + NULL
31 +};
32
26 -static int edit;
27 -static int replay;
33 +static int edit, no_replay, no_commit, needed_deref;
34 static enum { REVERT, CHERRY_PICK } action;
29 -static int no_commit;
35 static struct commit *commit;
31 -static int needed_deref;
36
37 static const char *me;
38
39 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
40
37 -static void parse_options(int argc, const char **argv)
41 +static void parse_args(int argc, const char **argv)
42 {
39 - const char *usage_str = action == REVERT ?
40 - revert_usage : cherry_pick_usage;
43 + const char * const * usage_str =
44 + action == REVERT ? revert_usage : cherry_pick_usage;
45 unsigned char sha1[20];
46 const char *arg;
43 - int i;
44 -
45 - if (argc < 2)
46 - usage(usage_str);
47 -
48 - for (i = 1; i < argc; i++) {
49 - arg = argv[i];
50 - if (arg[0] != '-')
51 - break;
52 - if (!strcmp(arg, "-n") || !strcmp(arg, "--no-commit"))
53 - no_commit = 1;
54 - else if (!strcmp(arg, "-e") || !strcmp(arg, "--edit"))
55 - edit = 1;
56 - else if (!strcmp(arg, "--no-edit"))
57 - edit = 0;
58 - else if (!strcmp(arg, "-x") || !strcmp(arg, "--i-really-want-"
59 - "to-expose-my-private-commit-object-name"))
60 - replay = 0;
61 - else if (strcmp(arg, "-r"))
62 - usage(usage_str);
63 - }
64 - if (i != argc - 1)
65 - usage(usage_str);
66 - arg = argv[argc - 1];
47 + int noop;
48 + struct option options[] = {
49 + OPT_BOOLEAN('n', "no-commit", &no_commit, "don't automatically commit"),
50 + OPT_BOOLEAN('e', "edit", &edit, "edit the commit message"),
51 + OPT_BOOLEAN('x', NULL, &no_replay, "append commit name when cherry-picking"),
52 + OPT_BOOLEAN('r', NULL, &noop, "no-op (backward compatibility)"),
53 + OPT_END(),
54 + };
55 +
56 + if (parse_options(argc, argv, options, usage_str, 0) != 1)
57 + usage_with_options(usage_str, options);
58 + arg = argv[0];
59 if (get_sha1(arg, sha1))
60 die ("Cannot find '%s'", arg);
61 commit = (struct commit *)parse_object(sha1);
@@ -243,10 +235,10 @@ static int revert_or_cherry_pick(int argc, const char **argv)
235 git_config(git_default_config);
236 me = action == REVERT ? "revert" : "cherry-pick";
237 setenv(GIT_REFLOG_ACTION, me, 0);
246 - parse_options(argc, argv);
238 + parse_args(argc, argv);
239
240 /* this is copied from the shell script, but it's never triggered... */
249 - if (action == REVERT && replay)
241 + if (action == REVERT && !no_replay)
242 die("revert is incompatible with replay");
243
244 if (no_commit) {
@@ -310,7 +302,7 @@ static int revert_or_cherry_pick(int argc, const char **argv)
302 next = commit;
303 set_author_ident_env(message);
304 add_message_to_msg(message);
313 - if (!replay) {
305 + if (no_replay) {
306 add_to_msg("(cherry picked from commit ");
307 add_to_msg(sha1_to_hex(commit->object.sha1));
308 add_to_msg(")\n");
@@ -388,13 +380,14 @@ int cmd_revert(int argc, const char **argv, const char *prefix)
380 {
381 if (isatty(0))
382 edit = 1;
383 + no_replay = 1;
384 action = REVERT;
385 return revert_or_cherry_pick(argc, argv);
386 }
387
388 int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
389 {
397 - replay = 1;
390 + no_replay = 0;
391 action = CHERRY_PICK;
392 return revert_or_cherry_pick(argc, argv);
393 }