revert: report success when using option --strategy

"git cherry-pick foo" has always reported success with
"Finished one cherry-pick" but "cherry-pick --strategy"
does not print anything. So move the code to write that
message from do_recursive_merge() to do_cherry_pick()
so other strategies can share it.

This patch also refactors the code that prints a message
like "Automatic cherry-pick failed. <help message>". This
code was duplicated in both do_recursive_merge() and
do_pick_commit().

To do that, now do_recursive_merge() returns an int to signal
success or failure. And in case of failure we just return 1
from do_pick_commit() instead of doing "exit(1)" from either
do_recursive_merge() or do_pick_commit().

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Christian Couder
2010-07-14 01:28:12 +02:00
committed by Junio C Hamano
parent 53b304224a
commit 7b53b92fdb
2 changed files with 54 additions and 23 deletions

View File

@ -311,10 +311,9 @@ static int fast_forward_to(const unsigned char *to, const unsigned char *from)
return write_ref_sha1(ref_lock, to, "cherry-pick"); return write_ref_sha1(ref_lock, to, "cherry-pick");
} }
static void do_recursive_merge(struct commit *base, struct commit *next, static int do_recursive_merge(struct commit *base, struct commit *next,
const char *base_label, const char *next_label, const char *base_label, const char *next_label,
unsigned char *head, struct strbuf *msgbuf, unsigned char *head, struct strbuf *msgbuf)
char *defmsg)
{ {
struct merge_options o; struct merge_options o;
struct tree *result, *next_tree, *base_tree, *head_tree; struct tree *result, *next_tree, *base_tree, *head_tree;
@ -357,14 +356,9 @@ static void do_recursive_merge(struct commit *base, struct commit *next,
i++; i++;
} }
} }
write_message(msgbuf, defmsg);
fprintf(stderr, "Automatic %s failed.%s\n",
me, help_msg());
rerere(allow_rerere_auto);
exit(1);
} }
write_message(msgbuf, defmsg);
fprintf(stderr, "Finished one %s.\n", me); return !clean;
} }
static int do_pick_commit(void) static int do_pick_commit(void)
@ -375,6 +369,8 @@ static int do_pick_commit(void)
struct commit_message msg = { NULL, NULL, NULL, NULL, NULL }; struct commit_message msg = { NULL, NULL, NULL, NULL, NULL };
char *defmsg = NULL; char *defmsg = NULL;
struct strbuf msgbuf = STRBUF_INIT; struct strbuf msgbuf = STRBUF_INIT;
struct strbuf mebuf = STRBUF_INIT;
int res;
if (no_commit) { if (no_commit) {
/* /*
@ -470,30 +466,41 @@ static int do_pick_commit(void)
} }
} }
if (!strategy || !strcmp(strategy, "recursive") || action == REVERT) strbuf_addstr(&mebuf, me);
do_recursive_merge(base, next, base_label, next_label,
head, &msgbuf, defmsg); if (!strategy || !strcmp(strategy, "recursive") || action == REVERT) {
else { res = do_recursive_merge(base, next, base_label, next_label,
int res; head, &msgbuf);
write_message(&msgbuf, defmsg);
} else {
struct commit_list *common = NULL; struct commit_list *common = NULL;
struct commit_list *remotes = NULL; struct commit_list *remotes = NULL;
strbuf_addf(&mebuf, " with strategy %s", strategy);
write_message(&msgbuf, defmsg); write_message(&msgbuf, defmsg);
commit_list_insert(base, &common); commit_list_insert(base, &common);
commit_list_insert(next, &remotes); commit_list_insert(next, &remotes);
res = try_merge_command(strategy, common, res = try_merge_command(strategy, common,
sha1_to_hex(head), remotes); sha1_to_hex(head), remotes);
free_commit_list(common); free_commit_list(common);
free_commit_list(remotes); free_commit_list(remotes);
if (res) {
fprintf(stderr, "Automatic %s with strategy %s failed.%s\n",
me, strategy, help_msg());
rerere(allow_rerere_auto);
exit(1);
}
} }
if (res) {
fprintf(stderr, "Automatic %s failed.%s\n",
mebuf.buf, help_msg());
rerere(allow_rerere_auto);
} else {
fprintf(stderr, "Finished one %s.\n", mebuf.buf);
}
strbuf_release(&mebuf);
free_message(&msg); free_message(&msg);
if (res)
return 1;
/* /*
* *
* If we are cherry-pick, and if the merge did not result in * If we are cherry-pick, and if the merge did not result in

View File

@ -23,12 +23,36 @@ test_expect_success setup '
' '
test_expect_success 'cherry-pick first..fourth works' ' test_expect_success 'cherry-pick first..fourth works' '
cat <<-\EOF >expected &&
Finished one cherry-pick.
Finished one cherry-pick.
Finished one cherry-pick.
EOF
git checkout -f master && git checkout -f master &&
git reset --hard first && git reset --hard first &&
test_tick && test_tick &&
git cherry-pick first..fourth && git cherry-pick first..fourth 2>actual &&
git diff --quiet other && git diff --quiet other &&
git diff --quiet HEAD other && git diff --quiet HEAD other &&
test_cmp expected actual &&
test "$(git rev-parse --verify HEAD)" != "$(git rev-parse --verify fourth)"
'
test_expect_success 'cherry-pick --strategy resolve first..fourth works' '
cat <<-\EOF >expected &&
Finished one cherry-pick with strategy resolve.
Finished one cherry-pick with strategy resolve.
Finished one cherry-pick with strategy resolve.
EOF
git checkout -f master &&
git reset --hard first &&
test_tick &&
git cherry-pick --strategy resolve first..fourth 2>actual &&
git diff --quiet other &&
git diff --quiet HEAD other &&
test_cmp expected actual &&
test "$(git rev-parse --verify HEAD)" != "$(git rev-parse --verify fourth)" test "$(git rev-parse --verify HEAD)" != "$(git rev-parse --verify fourth)"
' '