Skip to content

Commit e0ef849

Browse files
jrngitster
authored andcommitted
revert: do not rebuild argv on heap
Set options in struct rev_info directly so we can reuse the arguments collected from parse_options without modification. This is just a cleanup; no noticeable change is intended. Signed-off-by: Jonathan Nieder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 65281b7 commit e0ef849

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

builtin/revert.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,14 @@ static const char *strategy;
5050

5151
static char *get_encoding(const char *message);
5252

53+
static const char * const *revert_or_cherry_pick_usage(void)
54+
{
55+
return action == REVERT ? revert_usage : cherry_pick_usage;
56+
}
57+
5358
static void parse_args(int argc, const char **argv)
5459
{
55-
const char * const * usage_str =
56-
action == REVERT ? revert_usage : cherry_pick_usage;
60+
const char * const * usage_str = revert_or_cherry_pick_usage();
5761
int noop;
5862
struct option options[] = {
5963
OPT_BOOLEAN('n', "no-commit", &no_commit, "don't automatically commit"),
@@ -79,8 +83,9 @@ static void parse_args(int argc, const char **argv)
7983
}
8084

8185
commit_argc = parse_options(argc, argv, NULL, options, usage_str,
86+
PARSE_OPT_KEEP_ARGV0 |
8287
PARSE_OPT_KEEP_UNKNOWN);
83-
if (commit_argc < 1)
88+
if (commit_argc < 2)
8489
usage_with_options(usage_str, options);
8590

8691
commit_argv = argv;
@@ -526,27 +531,22 @@ static int do_pick_commit(void)
526531

527532
static void prepare_revs(struct rev_info *revs)
528533
{
529-
int argc = 0;
530-
int i;
531-
const char **argv = xmalloc((commit_argc + 4) * sizeof(*argv));
534+
int argc;
532535

533-
argv[argc++] = NULL;
534-
argv[argc++] = "--no-walk";
536+
init_revisions(revs, NULL);
537+
revs->no_walk = 1;
535538
if (action != REVERT)
536-
argv[argc++] = "--reverse";
537-
for (i = 0; i < commit_argc; i++)
538-
argv[argc++] = commit_argv[i];
539-
argv[argc++] = NULL;
539+
revs->reverse = 1;
540+
541+
argc = setup_revisions(commit_argc, commit_argv, revs, NULL);
542+
if (argc > 1)
543+
usage(*revert_or_cherry_pick_usage());
540544

541-
init_revisions(revs, NULL);
542-
setup_revisions(argc - 1, argv, revs, NULL);
543545
if (prepare_revision_walk(revs))
544546
die("revision walk setup failed");
545547

546548
if (!revs->commits)
547549
die("empty commit set passed");
548-
549-
free(argv);
550550
}
551551

552552
static int revert_or_cherry_pick(int argc, const char **argv)

t/t3501-revert-cherry-pick.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,24 @@ test_expect_success setup '
4141
git tag rename2
4242
'
4343

44+
test_expect_success 'cherry-pick --nonsense' '
45+
46+
pos=$(git rev-parse HEAD) &&
47+
git diff --exit-code HEAD &&
48+
test_must_fail git cherry-pick --nonsense 2>msg &&
49+
git diff --exit-code HEAD "$pos" &&
50+
grep '[Uu]sage:' msg
51+
'
52+
53+
test_expect_success 'revert --nonsense' '
54+
55+
pos=$(git rev-parse HEAD) &&
56+
git diff --exit-code HEAD &&
57+
test_must_fail git revert --nonsense 2>msg &&
58+
git diff --exit-code HEAD "$pos" &&
59+
grep '[Uu]sage:' msg
60+
'
61+
4462
test_expect_success 'cherry-pick after renaming branch' '
4563
4664
git checkout rename2 &&

0 commit comments

Comments
 (0)