3 # Copyright (c) 2010 Thomas Rast
6 test_description='Test the post-rewrite hook.'
7 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
8 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
12 test_expect_success 'setup' '
13 test_commit A foo A &&
14 test_commit B foo B &&
15 test_commit C foo C &&
16 test_commit D foo D &&
18 test_commit E bar E &&
19 test_commit F foo F &&
25 cat >.git/hooks/post-rewrite <<EOF
27 echo \$@ > "$TRASH_DIRECTORY"/post-rewrite.args
28 cat > "$TRASH_DIRECTORY"/post-rewrite.data
30 chmod u+x .git/hooks/post-rewrite
33 rm -f post-rewrite.args post-rewrite.data
36 verify_hook_input () {
37 test_cmp expected.args "$TRASH_DIRECTORY"/post-rewrite.args &&
38 test_cmp expected.data "$TRASH_DIRECTORY"/post-rewrite.data
41 test_expect_success 'git commit --amend' '
43 echo "D new message" > newmsg &&
44 oldsha=$(git rev-parse HEAD^0) &&
45 git commit -Fnewmsg --amend &&
46 echo amend > expected.args &&
47 echo $oldsha $(git rev-parse HEAD^0) > expected.data &&
51 test_expect_success 'git commit --amend --no-post-rewrite' '
53 echo "D new message again" > newmsg &&
54 git commit --no-post-rewrite -Fnewmsg --amend &&
55 test ! -f post-rewrite.args &&
56 test ! -f post-rewrite.data
59 test_expect_success 'git rebase --apply' '
62 test_must_fail git rebase --apply --onto A B &&
65 git rebase --continue &&
66 echo rebase >expected.args &&
67 cat >expected.data <<-EOF &&
68 $(git rev-parse C) $(git rev-parse HEAD^)
69 $(git rev-parse D) $(git rev-parse HEAD)
74 test_expect_success 'git rebase --apply --skip' '
77 test_must_fail git rebase --apply --onto A B &&
78 test_must_fail git rebase --skip &&
81 git rebase --continue &&
82 echo rebase >expected.args &&
83 cat >expected.data <<-EOF &&
84 $(git rev-parse C) $(git rev-parse HEAD^)
85 $(git rev-parse D) $(git rev-parse HEAD)
90 test_expect_success 'git rebase --apply --skip the last one' '
93 test_must_fail git rebase --apply --onto D A &&
95 echo rebase >expected.args &&
96 cat >expected.data <<-EOF &&
97 $(git rev-parse E) $(git rev-parse HEAD)
98 $(git rev-parse F) $(git rev-parse HEAD)
103 test_expect_success 'git rebase -m' '
104 git reset --hard D &&
106 test_must_fail git rebase -m --onto A B &&
109 git rebase --continue &&
110 echo rebase >expected.args &&
111 cat >expected.data <<-EOF &&
112 $(git rev-parse C) $(git rev-parse HEAD^)
113 $(git rev-parse D) $(git rev-parse HEAD)
118 test_expect_success 'git rebase -m --skip' '
119 git reset --hard D &&
121 test_must_fail git rebase -m --onto A B &&
122 test_must_fail git rebase --skip &&
125 git rebase --continue &&
126 echo rebase >expected.args &&
127 cat >expected.data <<-EOF &&
128 $(git rev-parse C) $(git rev-parse HEAD^)
129 $(git rev-parse D) $(git rev-parse HEAD)
134 test_expect_success 'git rebase with implicit use of merge backend' '
135 git reset --hard D &&
137 test_must_fail git rebase --keep-empty --onto A B &&
140 git rebase --continue &&
141 echo rebase >expected.args &&
142 cat >expected.data <<-EOF &&
143 $(git rev-parse C) $(git rev-parse HEAD^)
144 $(git rev-parse D) $(git rev-parse HEAD)
149 test_expect_success 'git rebase --skip with implicit use of merge backend' '
150 git reset --hard D &&
152 test_must_fail git rebase --keep-empty --onto A B &&
153 test_must_fail git rebase --skip &&
156 git rebase --continue &&
157 echo rebase >expected.args &&
158 cat >expected.data <<-EOF &&
159 $(git rev-parse C) $(git rev-parse HEAD^)
160 $(git rev-parse D) $(git rev-parse HEAD)
165 . "$TEST_DIRECTORY"/lib-rebase.sh
169 # Helper to work around the lack of one-shot exporting for
170 # test_must_fail (as it is a shell function)
171 test_fail_interactive_rebase () {
176 test_must_fail git rebase -i "$@"
180 test_expect_success 'git rebase -i (unchanged)' '
181 git reset --hard D &&
183 test_fail_interactive_rebase "1 2" --onto A B &&
186 git rebase --continue &&
187 echo rebase >expected.args &&
188 cat >expected.data <<-EOF &&
189 $(git rev-parse C) $(git rev-parse HEAD^)
190 $(git rev-parse D) $(git rev-parse HEAD)
195 test_expect_success 'git rebase -i (skip)' '
196 git reset --hard D &&
198 test_fail_interactive_rebase "2" --onto A B &&
201 git rebase --continue &&
202 echo rebase >expected.args &&
203 cat >expected.data <<-EOF &&
204 $(git rev-parse D) $(git rev-parse HEAD)
209 test_expect_success 'git rebase -i (squash)' '
210 git reset --hard D &&
212 test_fail_interactive_rebase "1 squash 2" --onto A B &&
215 git rebase --continue &&
216 echo rebase >expected.args &&
217 cat >expected.data <<-EOF &&
218 $(git rev-parse C) $(git rev-parse HEAD)
219 $(git rev-parse D) $(git rev-parse HEAD)
224 test_expect_success 'git rebase -i (fixup without conflict)' '
225 git reset --hard D &&
227 FAKE_LINES="1 fixup 2" git rebase -i B &&
228 echo rebase >expected.args &&
229 cat >expected.data <<-EOF &&
230 $(git rev-parse C) $(git rev-parse HEAD)
231 $(git rev-parse D) $(git rev-parse HEAD)
236 test_expect_success 'git rebase -i (double edit)' '
237 git reset --hard D &&
239 FAKE_LINES="edit 1 edit 2" git rebase -i B &&
240 git rebase --continue &&
241 echo something > foo &&
243 git rebase --continue &&
244 echo rebase >expected.args &&
245 cat >expected.data <<-EOF &&
246 $(git rev-parse C) $(git rev-parse HEAD^)
247 $(git rev-parse D) $(git rev-parse HEAD)
252 test_expect_success 'git rebase -i (exec)' '
253 git reset --hard D &&
255 FAKE_LINES="edit 1 exec_false 2" git rebase -i B &&
256 echo something >bar &&
258 # Fails because of exec false
259 test_must_fail git rebase --continue &&
260 git rebase --continue &&
261 echo rebase >expected.args &&
262 cat >expected.data <<-EOF &&
263 $(git rev-parse C) $(git rev-parse HEAD^)
264 $(git rev-parse D) $(git rev-parse HEAD)