{"thread":{"id":"24115","subject":"[PATCH v2 3/3] revert: do not rebuild argv on heap","startedAt":"2010-06-15T03:29:51Z","lastAt":"2010-06-15T03:29:51Z","messageCount":1,"participants":["Christian Couder"],"isPatch":true,"patchVersion":2,"patchTotal":3},"messages":[{"id":"143717","messageId":"20100615032952.3977.88455.chriscool@tuxfamily.org","threadId":"24115","inReplyTo":null,"subject":"[PATCH v2 3/3] revert: do not rebuild argv on heap","fromName":"Christian Couder","fromEmail":"chriscool@tuxfamily.org","sentAt":"2010-06-15T03:29:51Z","receivedAt":"2010-06-15T03:29:51Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"From: Jonathan Nieder <jrnieder@gmail.com>\n\nSet options in struct rev_info directly so we can reuse the\narguments collected from parse_options without modification.\n\nThis is just a cleanup; no noticeable change is intended.\n\nSigned-off-by: Jonathan Nieder <jrnieder@gmail.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/revert.c              |   33 ++++++++++++++++-----------------\n t/t3501-revert-cherry-pick.sh |   18 ++++++++++++++++++\n 2 files changed, 34 insertions(+), 17 deletions(-)\n\ndiff --git a/builtin/revert.c b/builtin/revert.c\nindex 3f4a20e..b6b4152 100644\n--- a/builtin/revert.c\n+++ b/builtin/revert.c\n@@ -50,10 +50,14 @@ static const char *strategy;\n \n static char *get_encoding(const char *message);\n \n+static const char * const *revert_or_cherry_pick_usage(void)\n+{\n+\treturn action == REVERT ? revert_usage : cherry_pick_usage;\n+}\n+\n static void parse_args(int argc, const char **argv)\n {\n-\tconst char * const * usage_str =\n-\t\taction == REVERT ?  revert_usage : cherry_pick_usage;\n+\tconst char * const * usage_str = revert_or_cherry_pick_usage();\n \tint noop;\n \tstruct option options[] = {\n \t\tOPT_BOOLEAN('n', \"no-commit\", &no_commit, \"don't automatically commit\"),\n@@ -79,8 +83,8 @@ static void parse_args(int argc, const char **argv)\n \t}\n \n \tcommit_argc = parse_options(argc, argv, NULL, options, usage_str,\n-\t\t\t\t    PARSE_OPT_KEEP_UNKNOWN);\n-\tif (commit_argc < 1)\n+\t\t\t\t    PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN);\n+\tif (commit_argc < 2)\n \t\tusage_with_options(usage_str, options);\n \n \tcommit_argv = argv;\n@@ -526,27 +530,22 @@ static int do_pick_commit(void)\n \n static void prepare_revs(struct rev_info *revs)\n {\n-\tint argc = 0;\n-\tint i;\n-\tconst char **argv = xmalloc((commit_argc + 4) * sizeof(*argv));\n+\tint argc;\n \n-\targv[argc++] = NULL;\n-\targv[argc++] = \"--no-walk\";\n+\tinit_revisions(revs, NULL);\n+\trevs->no_walk = 1;\n \tif (action != REVERT)\n-\t\targv[argc++] = \"--reverse\";\n-\tfor (i = 0; i < commit_argc; i++)\n-\t\targv[argc++] = commit_argv[i];\n-\targv[argc++] = NULL;\n+\t\trevs->reverse = 1;\n+\n+\targc = setup_revisions(commit_argc, commit_argv, revs, NULL);\n+\tif (argc > 1)\n+\t\tusage(*revert_or_cherry_pick_usage());\n \n-\tinit_revisions(revs, NULL);\n-\tsetup_revisions(argc - 1, argv, revs, NULL);\n \tif (prepare_revision_walk(revs))\n \t\tdie(\"revision walk setup failed\");\n \n \tif (!revs->commits)\n \t\tdie(\"empty commit set passed\");\n-\n-\tfree(argv);\n }\n \n static int revert_or_cherry_pick(int argc, const char **argv)\ndiff --git a/t/t3501-revert-cherry-pick.sh b/t/t3501-revert-cherry-pick.sh\nindex e4fbf7a..bc7aedd 100755\n--- a/t/t3501-revert-cherry-pick.sh\n+++ b/t/t3501-revert-cherry-pick.sh\n@@ -41,6 +41,24 @@ test_expect_success setup '\n \tgit tag rename2\n '\n \n+test_expect_success 'cherry-pick --nonsense' '\n+\n+\tpos=$(git rev-parse HEAD) &&\n+\tgit diff --exit-code HEAD &&\n+\ttest_must_fail git cherry-pick --nonsense 2>msg &&\n+\tgit diff --exit-code HEAD \"$pos\" &&\n+\tgrep '[Uu]sage:' msg\n+'\n+\n+test_expect_success 'revert --nonsense' '\n+\n+\tpos=$(git rev-parse HEAD) &&\n+\tgit diff --exit-code HEAD &&\n+\ttest_must_fail git revert --nonsense 2>msg &&\n+\tgit diff --exit-code HEAD \"$pos\" &&\n+\tgrep '[Uu]sage:' msg\n+'\n+\n test_expect_success 'cherry-pick after renaming branch' '\n \n \tgit checkout rename2 &&\n-- \n1.7.1.530.g14dc.dirty\n"}]}