threads / patch / 39399

patch, 2 partsfix post-rewrite hook with 'git rebase -i --exec'

Subject: [PATCH 0/2] fix post-rewrite hook with 'git rebase -i --exec'

## tl;dr

5 messages between May 21, 2015 and May 22, 2015. Diffs are folded; open one to read it.

replies: 4people: 3as markdown or json

Matthieu Moy· May 21, 2015, 18:13 UTC · lore
Matthieu Moy (2):
  rebase -i: demonstrate incorrect behavior of post-rewrite hook with
    exec
  rebase -i: fix post-rewrite hook with failed exec command
 git-rebase--interactive.sh   | 10 +++++-----
 t/t5407-post-rewrite-hook.sh | 17 +++++++++++++++++
 2 files changed, 22 insertions(+), 5 deletions(-)
-- 
2.4.1.171.g060e6ae.dirty
Matthieu Moy· May 21, 2015, 18:13 UTC · re: Matthieu Moy · lore

[PATCH 1/2] rebase -i: demonstrate incorrect behavior of post-rewrite hook with exec

The 'exec' command is sending the current commit to stopped-sha, which is supposed to contain the original commit (before rebase). As a result, if an 'exec' command fails, the next 'git rebase --continue' will send the current commit as <old-sha1> to the post-rewrite hook.

The test currently fails with :

--- expected.data 2015-05-21 17:55:29.000000000 +0000 +++ [...]post-rewrite.data 2015-05-21 17:55:29.000000000 +0000

Show changes to t/t5407-post-rewrite-hook.sh +18 −0
@@ -1,2 +1,3 @@
 2362ae8e1b1b865e6161e6f0e165ffb974abf018 488028e9fac0b598b70cbeb594258a917e3f6fab
+488028e9fac0b598b70cbeb594258a917e3f6fab 488028e9fac0b598b70cbeb594258a917e3f6fab
 babc8a4c7470895886fc129f1a015c486d05a351 8edffcc4e69a4e696a1d4bab047df450caf99507

Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
---
 t/t5407-post-rewrite-hook.sh | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/t/t5407-post-rewrite-hook.sh b/t/t5407-post-rewrite-hook.sh
index ea2e0d4..ecef820 100755
--- a/t/t5407-post-rewrite-hook.sh
+++ b/t/t5407-post-rewrite-hook.sh
@@ -212,4 +212,21 @@ EOF
 	verify_hook_input
 '
 
+test_expect_failure 'git rebase -i (exec)' '
+	git reset --hard D &&
+	clear_hook_input &&
+	FAKE_LINES="edit 1 exec_false 2" git rebase -i B
+	echo something >bar &&
+	git add bar &&
+	# Fail because of exec false
+	test_must_fail git rebase --continue &&
+	git rebase --continue &&
+	echo rebase >expected.args &&
+	cat >expected.data <<EOF &&
+$(git rev-parse C) $(git rev-parse HEAD^)
+$(git rev-parse D) $(git rev-parse HEAD)
+EOF
+	verify_hook_input
+'
+
 test_done
-- 
2.4.1.171.g060e6ae.dirty
Eric Sunshine· May 21, 2015, 21:12 UTC · re: Matthieu Moy · lore

Re: [PATCH 1/2] rebase -i: demonstrate incorrect behavior of post-rewrite hook with exec

On Thu, May 21, 2015 at 2:13 PM, Matthieu Moy <Matthieu.Moy@imag.fr> wrote:
Show 20 quoted lines
> The 'exec' command is sending the current commit to stopped-sha, which is
> supposed to contain the original commit (before rebase). As a result, if
> an 'exec' command fails, the next 'git rebase --continue' will send the
> current commit as <old-sha1> to the post-rewrite hook.
> [...]
>
> Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
> ---
> diff --git a/t/t5407-post-rewrite-hook.sh b/t/t5407-post-rewrite-hook.sh
> index ea2e0d4..ecef820 100755
> --- a/t/t5407-post-rewrite-hook.sh
> +++ b/t/t5407-post-rewrite-hook.sh
> @@ -212,4 +212,21 @@ EOF
>         verify_hook_input
>  '
>
> +test_expect_failure 'git rebase -i (exec)' '
> +       git reset --hard D &&
> +       clear_hook_input &&
> +       FAKE_LINES="edit 1 exec_false 2" git rebase -i B
Broken &&-chain.
Show 16 quoted lines
> +       echo something >bar &&
> +       git add bar &&
> +       # Fail because of exec false
> +       test_must_fail git rebase --continue &&
> +       git rebase --continue &&
> +       echo rebase >expected.args &&
> +       cat >expected.data <<EOF &&
> +$(git rev-parse C) $(git rev-parse HEAD^)
> +$(git rev-parse D) $(git rev-parse HEAD)
> +EOF
> +       verify_hook_input
> +'
> +
>  test_done
> --
> 2.4.1.171.g060e6ae.dirty
Matthieu Moy· May 22, 2015, 06:35 UTC · re: Eric Sunshine · lore

Re: [PATCH 1/2] rebase -i: demonstrate incorrect behavior of post-rewrite hook with exec

Eric Sunshine <sunshine@sunshineco.com> writes:
Show 6 quoted lines
>> +test_expect_failure 'git rebase -i (exec)' '
>> +       git reset --hard D &&
>> +       clear_hook_input &&
>> +       FAKE_LINES="edit 1 exec_false 2" git rebase -i B
>
> Broken &&-chain.
Thanks, will add in v2.
-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/
Matthieu Moy· May 21, 2015, 18:13 UTC · re: Matthieu Moy · lore

[PATCH 2/2] rebase -i: fix post-rewrite hook with failed exec command

Usually, when 'git rebase' stops before completing the rebase, it is to give the user an opportunity to edit a commit (e.g. with the 'edit' command). In such cases, 'git rebase' leaves the sha1 of the commit being rewritten in "$state_dir"/stopped-sha, and subsequent 'git rebase --continue' will call the post-rewrite hook with this sha1 as <old-sha1> argument to the post-rewrite hook.

The case of 'git rebase' stopping because of a failed 'exec' command is different: it gives the opportunity to the user to examine or fix the failure, but does not stop saying "here's a commit to edit, use --continue when you're done". So, there's no reason to call the post-rewrite hook for 'exec' commands. If the user did rewrite the commit, it would be with 'git commit --amend' which already called the post-rewrite hook.

Fix the behavior to leave no stopped-sha file in case of failed exec command, and teach 'git rebase --continue' to skip record_in_rewritten if no stopped-sha file is found.

Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
---
I'm afraid I'm the one to blame for the bug :-(.
 git-rebase--interactive.sh   | 10 +++++-----
 t/t5407-post-rewrite-hook.sh |  2 +-
 2 files changed, 6 insertions(+), 6 deletions(-)
Show changes to 2 files +6 −6

git-rebase--interactive.sh, t/t5407-post-rewrite-hook.sh

diff --git a/git-rebase--interactive.sh b/git-rebase--interactive.sh
index 08e5d86..1c321e4 100644
--- a/git-rebase--interactive.sh
+++ b/git-rebase--interactive.sh
@@ -486,7 +486,7 @@ do_pick () {
 }
 
 do_next () {
-	rm -f "$msg" "$author_script" "$amend" || exit
+	rm -f "$msg" "$author_script" "$amend" "$state_dir"/stopped-sha || exit
 	read -r command sha1 rest < "$todo"
 	case "$command" in
 	"$comment_char"*|''|noop)
@@ -576,9 +576,6 @@ do_next () {
 		read -r command rest < "$todo"
 		mark_action_done
 		printf 'Executing: %s\n' "$rest"
-		# "exec" command doesn't take a sha1 in the todo-list.
-		# => can't just use $sha1 here.
-		git rev-parse --verify HEAD > "$state_dir"/stopped-sha
 		${SHELL:-@SHELL_PATH@} -c "$rest" # Actual execution
 		status=$?
 		# Run in subshell because require_clean_work_tree can die.
@@ -874,7 +871,10 @@ first and then run 'git rebase --continue' again."
 		fi
 	fi
 
-	record_in_rewritten "$(cat "$state_dir"/stopped-sha)"
+	if test -r "$state_dir"/stopped-sha
+	then
+		record_in_rewritten "$(cat "$state_dir"/stopped-sha)"
+	fi
 
 	require_clean_work_tree "rebase"
 	do_rest
diff --git a/t/t5407-post-rewrite-hook.sh b/t/t5407-post-rewrite-hook.sh
index ecef820..24ba796 100755
--- a/t/t5407-post-rewrite-hook.sh
+++ b/t/t5407-post-rewrite-hook.sh
@@ -212,7 +212,7 @@ EOF
 	verify_hook_input
 '
 
-test_expect_failure 'git rebase -i (exec)' '
+test_expect_success 'git rebase -i (exec)' '
 	git reset --hard D &&
 	clear_hook_input &&
 	FAKE_LINES="edit 1 exec_false 2" git rebase -i B
-- 
2.4.1.171.g060e6ae.dirty

← back to recent threads