From 831244bd0df6871c618fc8becbb5c0f1fb8f5459 Mon Sep 17 00:00:00 2001 From: Christian Couder Date: Wed, 2 Jun 2010 07:58:34 +0200 Subject: revert: cleanup code for -x option There was some dead code and option -x appeared in the short help message of git revert (when running "git revert -h") which was wrong. Signed-off-by: Christian Couder Signed-off-by: Junio C Hamano --- builtin/revert.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/builtin/revert.c b/builtin/revert.c index 7976b5a329..5df0d690d9 100644 --- a/builtin/revert.c +++ b/builtin/revert.c @@ -58,7 +58,6 @@ static void parse_args(int argc, const char **argv) struct option options[] = { OPT_BOOLEAN('n', "no-commit", &no_commit, "don't automatically commit"), OPT_BOOLEAN('e', "edit", &edit, "edit the commit message"), - OPT_BOOLEAN('x', NULL, &no_replay, "append commit name when cherry-picking"), OPT_BOOLEAN('r', NULL, &noop, "no-op (backward compatibility)"), OPT_BOOLEAN('s', "signoff", &signoff, "add Signed-off-by:"), OPT_INTEGER('m', "mainline", &mainline, "parent number"), @@ -71,6 +70,7 @@ static void parse_args(int argc, const char **argv) if (action == CHERRY_PICK) { struct option cp_extra[] = { + OPT_BOOLEAN('x', NULL, &no_replay, "append commit name"), OPT_BOOLEAN(0, "ff", &allow_ff, "allow fast-forward"), OPT_END(), }; @@ -379,10 +379,6 @@ static int revert_or_cherry_pick(int argc, const char **argv) setenv(GIT_REFLOG_ACTION, me, 0); parse_args(argc, argv); - /* this is copied from the shell script, but it's never triggered... */ - if (action == REVERT && !no_replay) - die("revert is incompatible with replay"); - if (allow_ff) { if (signoff) die("cherry-pick --ff cannot be used with --signoff"); @@ -546,14 +542,12 @@ int cmd_revert(int argc, const char **argv, const char *prefix) { if (isatty(0)) edit = 1; - no_replay = 1; action = REVERT; return revert_or_cherry_pick(argc, argv); } int cmd_cherry_pick(int argc, const char **argv, const char *prefix) { - no_replay = 0; action = CHERRY_PICK; return revert_or_cherry_pick(argc, argv); } -- cgit v1.2.1 From 2fb0e14f4014a4f5027401bc7156929309449726 Mon Sep 17 00:00:00 2001 From: Christian Couder Date: Wed, 2 Jun 2010 07:58:35 +0200 Subject: revert: use run_command_v_opt() instead of execv_git_cmd() This is needed by the following commits, because we are going to cherry pick many commits instead of just one. Signed-off-by: Christian Couder Signed-off-by: Junio C Hamano --- builtin/revert.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/builtin/revert.c b/builtin/revert.c index 5df0d690d9..02f18c2208 100644 --- a/builtin/revert.c +++ b/builtin/revert.c @@ -508,6 +508,8 @@ static int revert_or_cherry_pick(int argc, const char **argv) } } + free_message(&msg); + /* * * If we are cherry-pick, and if the merge did not result in @@ -520,7 +522,9 @@ static int revert_or_cherry_pick(int argc, const char **argv) if (!no_commit) { /* 6 is max possible length of our args array including NULL */ const char *args[6]; + int res; int i = 0; + args[i++] = "commit"; args[i++] = "-n"; if (signoff) @@ -530,9 +534,12 @@ static int revert_or_cherry_pick(int argc, const char **argv) args[i++] = defmsg; } args[i] = NULL; - return execv_git_cmd(args); + res = run_command_v_opt(args, RUN_GIT_CMD); + free(defmsg); + + return res; } - free_message(&msg); + free(defmsg); return 0; -- cgit v1.2.1 From 7af46595b2667214e98da55ed2f82ce1ac2b404a Mon Sep 17 00:00:00 2001 From: Christian Couder Date: Wed, 2 Jun 2010 07:58:36 +0200 Subject: revert: refactor code into a do_pick_commit() function This is needed because we are going to make it possible to cherry-pick many commits instead of just one in the following commits. And we will be able to do that by just calling do_pick_commit() once for each commit to cherry-pick. Signed-off-by: Christian Couder Signed-off-by: Junio C Hamano --- builtin/revert.c | 44 +++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/builtin/revert.c b/builtin/revert.c index 02f18c2208..c2aee86d1c 100644 --- a/builtin/revert.c +++ b/builtin/revert.c @@ -365,7 +365,7 @@ static void do_recursive_merge(struct commit *base, struct commit *next, fprintf(stderr, "Finished one %s.\n", me); } -static int revert_or_cherry_pick(int argc, const char **argv) +static int do_pick_commit(void) { unsigned char head[20]; struct commit *base, *next, *parent; @@ -374,24 +374,6 @@ static int revert_or_cherry_pick(int argc, const char **argv) char *defmsg = NULL; struct strbuf msgbuf = STRBUF_INIT; - git_config(git_default_config, NULL); - me = action == REVERT ? "revert" : "cherry-pick"; - setenv(GIT_REFLOG_ACTION, me, 0); - parse_args(argc, argv); - - if (allow_ff) { - if (signoff) - die("cherry-pick --ff cannot be used with --signoff"); - if (no_commit) - die("cherry-pick --ff cannot be used with --no-commit"); - if (no_replay) - die("cherry-pick --ff cannot be used with -x"); - if (edit) - die("cherry-pick --ff cannot be used with --edit"); - } - - if (read_cache() < 0) - die("git %s: failed to read the index", me); if (no_commit) { /* * We do not intend to commit immediately. We just want to @@ -545,6 +527,30 @@ static int revert_or_cherry_pick(int argc, const char **argv) return 0; } +static int revert_or_cherry_pick(int argc, const char **argv) +{ + git_config(git_default_config, NULL); + me = action == REVERT ? "revert" : "cherry-pick"; + setenv(GIT_REFLOG_ACTION, me, 0); + parse_args(argc, argv); + + if (allow_ff) { + if (signoff) + die("cherry-pick --ff cannot be used with --signoff"); + if (no_commit) + die("cherry-pick --ff cannot be used with --no-commit"); + if (no_replay) + die("cherry-pick --ff cannot be used with -x"); + if (edit) + die("cherry-pick --ff cannot be used with --edit"); + } + + if (read_cache() < 0) + die("git %s: failed to read the index", me); + + return do_pick_commit(); +} + int cmd_revert(int argc, const char **argv, const char *prefix) { if (isatty(0)) -- cgit v1.2.1 From 4b2095622f61e6ab51f03601e5c3c8d71ace5fc7 Mon Sep 17 00:00:00 2001 From: Christian Couder Date: Wed, 2 Jun 2010 07:58:37 +0200 Subject: revert: change help_msg() to take no argument This is needed because the following commits will make it possible to cherry-pick many commits instead of just one. So it will be possible to pass for example ranges of commits to "git cherry-pick" and this means that it will not be possible to use the arguments passed to "git cherry-pick" in the help message. The help message will have to use the sha1 of the currently processed commit. Signed-off-by: Christian Couder Signed-off-by: Junio C Hamano --- builtin/revert.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/builtin/revert.c b/builtin/revert.c index c2aee86d1c..aee10692ba 100644 --- a/builtin/revert.c +++ b/builtin/revert.c @@ -239,7 +239,7 @@ static void set_author_ident_env(const char *message) sha1_to_hex(commit->object.sha1)); } -static char *help_msg(const char *name) +static char *help_msg(void) { struct strbuf helpbuf = STRBUF_INIT; char *msg = getenv("GIT_CHERRY_PICK_HELP"); @@ -255,7 +255,7 @@ static char *help_msg(const char *name) strbuf_addf(&helpbuf, " with: \n" "\n" " git commit -c %s\n", - name); + sha1_to_hex(commit->object.sha1)); } else strbuf_addch(&helpbuf, '.'); @@ -357,7 +357,7 @@ static void do_recursive_merge(struct commit *base, struct commit *next, } write_message(msgbuf, defmsg); fprintf(stderr, "Automatic %s failed.%s\n", - me, help_msg(commit_name)); + me, help_msg()); rerere(allow_rerere_auto); exit(1); } @@ -484,7 +484,7 @@ static int do_pick_commit(void) free_commit_list(remotes); if (res) { fprintf(stderr, "Automatic %s with strategy %s failed.%s\n", - me, strategy, help_msg(commit_name)); + me, strategy, help_msg()); rerere(allow_rerere_auto); exit(1); } -- cgit v1.2.1 From 7e2bfd3f99f8d6e89c7b855675919dd5404e47a2 Mon Sep 17 00:00:00 2001 From: Christian Couder Date: Wed, 2 Jun 2010 07:58:38 +0200 Subject: revert: allow cherry-picking more than one commit This makes it possible to pass many commits or ranges of commits to "git cherry-pick" and to "git revert" to process many commits instead of just one. In fact commits are now enumerated with an equivalent of git rev-list --no-walk "$@" so all the following are now possible: git cherry-pick master~2..master git cherry-pick ^master~2 master git cherry-pick master^ master The following should be possible but does not work: git cherry-pick -2 master because "git rev-list --no-walk -2 master" only outputs one commit as "--no-walk" seems to take over "-2". And there is currently no way to continue cherry-picking or reverting if there is a problem with one commit. It's also not possible to abort the whole process. Some future work should provide the --continue and --abort options to do just that. Signed-off-by: Christian Couder Signed-off-by: Junio C Hamano --- builtin/revert.c | 51 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/builtin/revert.c b/builtin/revert.c index aee10692ba..853e9e406c 100644 --- a/builtin/revert.c +++ b/builtin/revert.c @@ -39,7 +39,8 @@ static const char * const cherry_pick_usage[] = { static int edit, no_replay, no_commit, mainline, signoff, allow_ff; static enum { REVERT, CHERRY_PICK } action; static struct commit *commit; -static const char *commit_name; +static int commit_argc; +static const char **commit_argv; static int allow_rerere_auto; static const char *me; @@ -53,7 +54,6 @@ static void parse_args(int argc, const char **argv) { const char * const * usage_str = action == REVERT ? revert_usage : cherry_pick_usage; - unsigned char sha1[20]; int noop; struct option options[] = { OPT_BOOLEAN('n', "no-commit", &no_commit, "don't automatically commit"), @@ -78,15 +78,11 @@ static void parse_args(int argc, const char **argv) die("program error"); } - if (parse_options(argc, argv, NULL, options, usage_str, 0) != 1) + commit_argc = parse_options(argc, argv, NULL, options, usage_str, 0); + if (commit_argc < 1) usage_with_options(usage_str, options); - commit_name = argv[0]; - if (get_sha1(commit_name, sha1)) - die ("Cannot find '%s'", commit_name); - commit = lookup_commit_reference(sha1); - if (!commit) - exit(1); + commit_argv = argv; } struct commit_message { @@ -527,8 +523,35 @@ static int do_pick_commit(void) return 0; } +static void prepare_revs(struct rev_info *revs) +{ + int argc = 0; + int i; + const char **argv = xmalloc((commit_argc + 4) * sizeof(*argv)); + + argv[argc++] = NULL; + argv[argc++] = "--no-walk"; + if (action != REVERT) + argv[argc++] = "--reverse"; + for (i = 0; i < commit_argc; i++) + argv[argc++] = commit_argv[i]; + argv[argc++] = NULL; + + init_revisions(revs, NULL); + setup_revisions(argc - 1, argv, revs, NULL); + if (prepare_revision_walk(revs)) + die("revision walk setup failed"); + + if (!revs->commits) + die("empty commit set passed"); + + free(argv); +} + static int revert_or_cherry_pick(int argc, const char **argv) { + struct rev_info revs; + git_config(git_default_config, NULL); me = action == REVERT ? "revert" : "cherry-pick"; setenv(GIT_REFLOG_ACTION, me, 0); @@ -548,7 +571,15 @@ static int revert_or_cherry_pick(int argc, const char **argv) if (read_cache() < 0) die("git %s: failed to read the index", me); - return do_pick_commit(); + prepare_revs(&revs); + + while ((commit = get_revision(&revs))) { + int res = do_pick_commit(); + if (res) + return res; + } + + return 0; } int cmd_revert(int argc, const char **argv, const char *prefix) -- cgit v1.2.1 From aa29ccf4c07a6e1e249794e1fa3c7cd0bf0a9e46 Mon Sep 17 00:00:00 2001 From: Christian Couder Date: Wed, 2 Jun 2010 07:58:39 +0200 Subject: revert: add tests to check cherry-picking many commits Note that there is an expected failure when running: git cherry-pick -3 fourth that's because: git rev-list --no-walk -3 fourth produce only one commit and not 3 as "--no-walk" seems to take over "-3". Signed-off-by: Christian Couder Signed-off-by: Junio C Hamano --- t/t3508-cherry-pick-many-commits.sh | 95 +++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100755 t/t3508-cherry-pick-many-commits.sh diff --git a/t/t3508-cherry-pick-many-commits.sh b/t/t3508-cherry-pick-many-commits.sh new file mode 100755 index 0000000000..3b87efe3ad --- /dev/null +++ b/t/t3508-cherry-pick-many-commits.sh @@ -0,0 +1,95 @@ +#!/bin/sh + +test_description='test cherry-picking many commits' + +. ./test-lib.sh + +test_expect_success setup ' + echo first > file1 && + git add file1 && + test_tick && + git commit -m "first" && + git tag first && + + git checkout -b other && + for val in second third fourth + do + echo $val >> file1 && + git add file1 && + test_tick && + git commit -m "$val" && + git tag $val + done +' + +test_expect_success 'cherry-pick first..fourth works' ' + git checkout master && + git reset --hard first && + test_tick && + git cherry-pick first..fourth && + git diff --quiet other && + git diff --quiet HEAD other && + test "$(git rev-parse --verify HEAD)" != "$(git rev-parse --verify fourth)" +' + +test_expect_success 'cherry-pick --ff first..fourth works' ' + git checkout master && + git reset --hard first && + test_tick && + git cherry-pick --ff first..fourth && + git diff --quiet other && + git diff --quiet HEAD other && + test "$(git rev-parse --verify HEAD)" = "$(git rev-parse --verify fourth)" +' + +test_expect_success 'cherry-pick -n first..fourth works' ' + git checkout master && + git reset --hard first && + test_tick && + git cherry-pick -n first..fourth && + git diff --quiet other && + git diff --cached --quiet other && + git diff --quiet HEAD first +' + +test_expect_success 'revert first..fourth works' ' + git checkout master && + git reset --hard fourth && + test_tick && + git revert first..fourth && + git diff --quiet first && + git diff --cached --quiet first && + git diff --quiet HEAD first +' + +test_expect_success 'revert ^first fourth works' ' + git checkout master && + git reset --hard fourth && + test_tick && + git revert ^first fourth && + git diff --quiet first && + git diff --cached --quiet first && + git diff --quiet HEAD first +' + +test_expect_success 'revert fourth fourth~1 fourth~2 works' ' + git checkout master && + git reset --hard fourth && + test_tick && + git revert fourth fourth~1 fourth~2 && + git diff --quiet first && + git diff --cached --quiet first && + git diff --quiet HEAD first +' + +test_expect_failure 'cherry-pick -3 fourth works' ' + git checkout master && + git reset --hard first && + test_tick && + git cherry-pick -3 fourth && + git diff --quiet other && + git diff --quiet HEAD other && + test "$(git rev-parse --verify HEAD)" != "$(git rev-parse --verify fourth)" +' + +test_done -- cgit v1.2.1 From 89d32d33ae562afa789a7723de8d853364101610 Mon Sep 17 00:00:00 2001 From: Christian Couder Date: Wed, 2 Jun 2010 07:58:40 +0200 Subject: Documentation/cherry-pick: describe passing more than one commit And while at it, add an "EXAMPLES" section. Signed-off-by: Christian Couder Signed-off-by: Junio C Hamano --- Documentation/git-cherry-pick.txt | 64 ++++++++++++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 11 deletions(-) diff --git a/Documentation/git-cherry-pick.txt b/Documentation/git-cherry-pick.txt index d71607a85d..bcb4c758b7 100644 --- a/Documentation/git-cherry-pick.txt +++ b/Documentation/git-cherry-pick.txt @@ -3,24 +3,28 @@ git-cherry-pick(1) NAME ---- -git-cherry-pick - Apply the change introduced by an existing commit +git-cherry-pick - Apply the changes introduced by some existing commits SYNOPSIS -------- -'git cherry-pick' [--edit] [-n] [-m parent-number] [-s] [-x] [--ff] +'git cherry-pick' [--edit] [-n] [-m parent-number] [-s] [-x] [--ff] ... DESCRIPTION ----------- -Given one existing commit, apply the change the patch introduces, and record a -new commit that records it. This requires your working tree to be clean (no -modifications from the HEAD commit). + +Given one or more existing commits, apply the change each one +introduces, recording a new commit for each. This requires your +working tree to be clean (no modifications from the HEAD commit). OPTIONS ------- -:: - Commit to cherry-pick. +...:: + Commits to cherry-pick. For a more complete list of ways to spell commits, see the "SPECIFYING REVISIONS" section in linkgit:git-rev-parse[1]. + Sets of commits can be passed but no traversal is done by + default, as if the '--no-walk' option was specified, see + linkgit:git-rev-list[1]. -e:: --edit:: @@ -55,10 +59,10 @@ OPTIONS -n:: --no-commit:: - Usually the command automatically creates a commit. - This flag applies the change necessary to cherry-pick - the named commit to your working tree and the index, - but does not make the commit. In addition, when this + Usually the command automatically creates a sequence of commits. + This flag applies the changes necessary to cherry-pick + each named commit to your working tree and the index, + without making any commit. In addition, when this option is used, your index does not have to match the HEAD commit. The cherry-pick is done against the beginning state of your index. @@ -75,6 +79,40 @@ effect to your index in a row. cherry-pick'ed commit, then a fast forward to this commit will be performed. +EXAMPLES +-------- +git cherry-pick master:: + + Apply the change introduced by the commit at the tip of the + master branch and create a new commit with this change. + +git cherry-pick ..master:: +git cherry-pick ^HEAD master:: + + Apply the changes introduced by all commits that are ancestors + of master but not of HEAD to produce new commits. + +git cherry-pick master\~4 master~2:: + + Apply the changes introduced by the fifth and third last + commits pointed to by master and create 2 new commits with + these changes. + +git cherry-pick -n master~1 next:: + + Apply to the working tree and the index the changes introduced + by the second last commit pointed to by master and by the last + commit pointed to by next, but do not create any commit with + these changes. + +git cherry-pick --ff ..next:: + + If history is linear and HEAD is an ancestor of next, update + the working tree and advance the HEAD pointer to match next. + Otherwise, apply the changes introduced by those commits that + are in next but not HEAD to the current branch, creating a new + commit for each new change. + Author ------ Written by Junio C Hamano @@ -83,6 +121,10 @@ Documentation -------------- Documentation by Junio C Hamano and the git-list . +SEE ALSO +-------- +linkgit:git-revert[1] + GIT --- Part of the linkgit:git[1] suite -- cgit v1.2.1 From 86c7bb47c76948bd1240a0db5b8dd88cf9db855d Mon Sep 17 00:00:00 2001 From: Christian Couder Date: Wed, 2 Jun 2010 07:58:41 +0200 Subject: Documentation/revert: describe passing more than one commit And while at it, add an "EXAMPLES" section. Signed-off-by: Christian Couder Signed-off-by: Junio C Hamano --- Documentation/git-revert.txt | 51 ++++++++++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/Documentation/git-revert.txt b/Documentation/git-revert.txt index c66bf8072e..dea4f53522 100644 --- a/Documentation/git-revert.txt +++ b/Documentation/git-revert.txt @@ -3,20 +3,22 @@ git-revert(1) NAME ---- -git-revert - Revert an existing commit +git-revert - Revert some existing commits SYNOPSIS -------- -'git revert' [--edit | --no-edit] [-n] [-m parent-number] [-s] +'git revert' [--edit | --no-edit] [-n] [-m parent-number] [-s] ... DESCRIPTION ----------- -Given one existing commit, revert the change the patch introduces, and record a -new commit that records it. This requires your working tree to be clean (no -modifications from the HEAD commit). -Note: 'git revert' is used to record a new commit to reverse the -effect of an earlier commit (often a faulty one). If you want to +Given one or more existing commits, revert the changes that the +related patches introduce, and record some new commits that record +them. This requires your working tree to be clean (no modifications +from the HEAD commit). + +Note: 'git revert' is used to record some new commits to reverse the +effect of some earlier commits (often only a faulty one). If you want to throw away all uncommitted changes in your working directory, you should see linkgit:git-reset[1], particularly the '--hard' option. If you want to extract specific files as they were in another commit, you @@ -26,10 +28,13 @@ both will discard uncommitted changes in your working directory. OPTIONS ------- -:: - Commit to revert. +...:: + Commits to revert. For a more complete list of ways to spell commit names, see "SPECIFYING REVISIONS" section in linkgit:git-rev-parse[1]. + Sets of commits can also be given but no traversal is done by + default, see linkgit:git-rev-list[1] and its '--no-walk' + option. -e:: --edit:: @@ -59,11 +64,11 @@ more details. -n:: --no-commit:: - Usually the command automatically creates a commit with - a commit log message stating which commit was - reverted. This flag applies the change necessary - to revert the named commit to your working tree - and the index, but does not make the commit. In addition, + Usually the command automatically creates some commits with + commit log messages stating which commits were + reverted. This flag applies the changes necessary + to revert the named commits to your working tree + and the index, but does not make the commits. In addition, when this option is used, your index does not have to match the HEAD commit. The revert is done against the beginning state of your index. @@ -75,6 +80,20 @@ effect to your index in a row. --signoff:: Add Signed-off-by line at the end of the commit message. +EXAMPLES +-------- +git revert HEAD~3:: + + Revert the changes specified by the fourth last commit in HEAD + and create a new commit with the reverted changes. + +git revert -n master\~5..master~2:: + + Revert the changes done by commits from the fifth last commit + in master (included) to the third last commit in master + (included), but do not create any commit with the reverted + changes. The revert only modifies the working tree and the + index. Author ------ @@ -84,6 +103,10 @@ Documentation -------------- Documentation by Junio C Hamano and the git-list . +SEE ALSO +-------- +linkgit:git-cherry-pick[1] + GIT --- Part of the linkgit:git[1] suite -- cgit v1.2.1