Skip to content

Commit 96e1948

Browse files
trastgitster
authored andcommitted
rebase: invoke post-rewrite hook
We have to deal with two separate code paths: a normal rebase, which actually goes through git-am; and rebase {-m|-s}. The only small issue with both is that they need to remember the original sha1 across a possible conflict resolution. rebase -m already puts this information in $dotest/current, and we just introduce a similar file for git-am. Note that in git-am, the hook really only runs when coming from git-rebase: the code path that sets the $dotest/original-commit file is guarded by a test for $dotest/rebasing. Signed-off-by: Thomas Rast <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6f6bee3 commit 96e1948

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

git-am.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,13 +573,15 @@ do
573573
echo "Patch is empty. Was it split wrong?"
574574
stop_here $this
575575
}
576+
rm -f "$dotest/original-commit"
576577
if test -f "$dotest/rebasing" &&
577578
commit=$(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
578579
-e q "$dotest/$msgnum") &&
579580
test "$(git cat-file -t "$commit")" = commit
580581
then
581582
git cat-file commit "$commit" |
582583
sed -e '1,/^$/d' >"$dotest/msg-clean"
584+
echo "$commit" > "$dotest/original-commit"
583585
else
584586
{
585587
sed -n '/^Subject/ s/Subject: //p' "$dotest/info"
@@ -766,6 +768,10 @@ do
766768
git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
767769
stop_here $this
768770

771+
if test -f "$dotest/original-commit"; then
772+
echo "$(cat "$dotest/original-commit") $commit" >> "$dotest/rewritten"
773+
fi
774+
769775
if test -x "$GIT_DIR"/hooks/post-applypatch
770776
then
771777
"$GIT_DIR"/hooks/post-applypatch
@@ -774,6 +780,10 @@ do
774780
go_next
775781
done
776782

783+
if test -s "$dotest"/rewritten && test -x "$GIT_DIR"/hooks/post-rewrite; then
784+
"$GIT_DIR"/hooks/post-rewrite rebase < "$dotest"/rewritten
785+
fi
786+
777787
git gc --auto
778788

779789
rm -fr "$dotest"

git-rebase.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ continue_merge () {
7979
then
8080
printf "Committed: %0${prec}d " $msgnum
8181
fi
82+
echo "$cmt $(git rev-parse HEAD^0)" >> "$dotest/rewritten"
8283
else
8384
if test -z "$GIT_QUIET"
8485
then
@@ -153,6 +154,10 @@ move_to_original_branch () {
153154

154155
finish_rb_merge () {
155156
move_to_original_branch
157+
if test -x "$GIT_DIR"/hooks/post-rewrite &&
158+
test -s "$dotest"/rewritten; then
159+
"$GIT_DIR"/hooks/post-rewrite rebase < "$dotest"/rewritten
160+
fi
156161
rm -r "$dotest"
157162
say All done.
158163
}

t/t5407-post-rewrite-hook.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,34 @@ test_expect_success 'git commit --amend --no-post-rewrite' '
4949
test ! -f post-rewrite.data
5050
'
5151

52+
test_expect_success 'git rebase' '
53+
git reset --hard D &&
54+
clear_hook_input &&
55+
test_must_fail git rebase --onto A B &&
56+
echo C > foo &&
57+
git add foo &&
58+
git rebase --continue &&
59+
echo rebase >expected.args &&
60+
cat >expected.data <<EOF &&
61+
$(git rev-parse C) $(git rev-parse HEAD^)
62+
$(git rev-parse D) $(git rev-parse HEAD)
63+
EOF
64+
verify_hook_input
65+
'
66+
67+
test_expect_success 'git rebase --skip' '
68+
git reset --hard D &&
69+
clear_hook_input &&
70+
test_must_fail git rebase --onto A B &&
71+
test_must_fail git rebase --skip &&
72+
echo D > foo &&
73+
git add foo &&
74+
git rebase --continue &&
75+
echo rebase >expected.args &&
76+
cat >expected.data <<EOF &&
77+
$(git rev-parse D) $(git rev-parse HEAD)
78+
EOF
79+
verify_hook_input
80+
'
81+
5282
test_done

0 commit comments

Comments
 (0)