1#!/bin/sh
2#
3# Copyright (c) 2010 Thomas Rast
4#
5
6test_description='Test the post-rewrite hook.'
7. ./test-lib.sh
8
9test_expect_success 'setup' '
10 test_commit A foo A &&
11 test_commit B foo B &&
12 test_commit C foo C &&
13 test_commit D foo D &&
14 git checkout A^0 &&
15 test_commit E bar E &&
16 test_commit F foo F &&
17 git checkout master
18'
19
20mkdir .git/hooks
21
22cat >.git/hooks/post-rewrite <<EOF
23#!/bin/sh
24echo \$@ > "$TRASH_DIRECTORY"/post-rewrite.args
25cat > "$TRASH_DIRECTORY"/post-rewrite.data
26EOF
27chmod u+x .git/hooks/post-rewrite
28
29clear_hook_input () {
30 rm -f post-rewrite.args post-rewrite.data
31}
32
33verify_hook_input () {
34 test_cmp expected.args "$TRASH_DIRECTORY"/post-rewrite.args &&
35 test_cmp expected.data "$TRASH_DIRECTORY"/post-rewrite.data
36}
37
38test_expect_success 'git commit --amend' '
39 clear_hook_input &&
40 echo "D new message" > newmsg &&
41 oldsha=$(git rev-parse HEAD^0) &&
42 git commit -Fnewmsg --amend &&
43 echo amend > expected.args &&
44 echo $oldsha $(git rev-parse HEAD^0) > expected.data &&
45 verify_hook_input
46'
47
48test_expect_success 'git commit --amend --no-post-rewrite' '
49 clear_hook_input &&
50 echo "D new message again" > newmsg &&
51 git commit --no-post-rewrite -Fnewmsg --amend &&
52 test ! -f post-rewrite.args &&
53 test ! -f post-rewrite.data
54'
55
56test_expect_success 'git rebase' '
57 git reset --hard D &&
58 clear_hook_input &&
59 test_must_fail git rebase --onto A B &&
60 echo C > foo &&
61 git add foo &&
62 git rebase --continue &&
63 echo rebase >expected.args &&
64 cat >expected.data <<-EOF &&
65 $(git rev-parse C) $(git rev-parse HEAD^)
66 $(git rev-parse D) $(git rev-parse HEAD)
67 EOF
68 verify_hook_input
69'
70
71test_expect_success 'git rebase --skip' '
72 git reset --hard D &&
73 clear_hook_input &&
74 test_must_fail git rebase --onto A B &&
75 test_must_fail git rebase --skip &&
76 echo D > foo &&
77 git add foo &&
78 git rebase --continue &&
79 echo rebase >expected.args &&
80 cat >expected.data <<-EOF &&
81 $(git rev-parse C) $(git rev-parse HEAD^)
82 $(git rev-parse D) $(git rev-parse HEAD)
83 EOF
84 verify_hook_input
85'
86
87test_expect_success 'git rebase --skip the last one' '
88 git reset --hard F &&
89 clear_hook_input &&
90 test_must_fail git rebase --onto D A &&
91 git rebase --skip &&
92 echo rebase >expected.args &&
93 cat >expected.data <<-EOF &&
94 $(git rev-parse E) $(git rev-parse HEAD)
95 $(git rev-parse F) $(git rev-parse HEAD)
96 EOF
97 verify_hook_input
98'
99
100test_expect_success 'git rebase -m' '
101 git reset --hard D &&
102 clear_hook_input &&
103 test_must_fail git rebase -m --onto A B &&
104 echo C > foo &&
105 git add foo &&
106 git rebase --continue &&
107 echo rebase >expected.args &&
108 cat >expected.data <<-EOF &&
109 $(git rev-parse C) $(git rev-parse HEAD^)
110 $(git rev-parse D) $(git rev-parse HEAD)
111 EOF
112 verify_hook_input
113'
114
115test_expect_success 'git rebase -m --skip' '
116 git reset --hard D &&
117 clear_hook_input &&
118 test_must_fail git rebase -m --onto A B &&
119 test_must_fail git rebase --skip &&
120 echo D > foo &&
121 git add foo &&
122 git rebase --continue &&
123 echo rebase >expected.args &&
124 cat >expected.data <<-EOF &&
125 $(git rev-parse C) $(git rev-parse HEAD^)
126 $(git rev-parse D) $(git rev-parse HEAD)
127 EOF
128 verify_hook_input
129'
130
131test_expect_success 'git rebase with implicit use of interactive backend' '
132 git reset --hard D &&
133 clear_hook_input &&
134 test_must_fail git rebase --keep-empty --onto A B &&
135 echo C > foo &&
136 git add foo &&
137 git rebase --continue &&
138 echo rebase >expected.args &&
139 cat >expected.data <<-EOF &&
140 $(git rev-parse C) $(git rev-parse HEAD^)
141 $(git rev-parse D) $(git rev-parse HEAD)
142 EOF
143 verify_hook_input
144'
145
146test_expect_success 'git rebase --skip with implicit use of interactive backend' '
147 git reset --hard D &&
148 clear_hook_input &&
149 test_must_fail git rebase --keep-empty --onto A B &&
150 test_must_fail git rebase --skip &&
151 echo D > foo &&
152 git add foo &&
153 git rebase --continue &&
154 echo rebase >expected.args &&
155 cat >expected.data <<-EOF &&
156 $(git rev-parse C) $(git rev-parse HEAD^)
157 $(git rev-parse D) $(git rev-parse HEAD)
158 EOF
159 verify_hook_input
160'
161
162. "$TEST_DIRECTORY"/lib-rebase.sh
163
164set_fake_editor
165
166# Helper to work around the lack of one-shot exporting for
167# test_must_fail (as it is a shell function)
168test_fail_interactive_rebase () {
169 (
170 FAKE_LINES="$1" &&
171 shift &&
172 export FAKE_LINES &&
173 test_must_fail git rebase -i "$@"
174 )
175}
176
177test_expect_success 'git rebase -i (unchanged)' '
178 git reset --hard D &&
179 clear_hook_input &&
180 test_fail_interactive_rebase "1 2" --onto A B &&
181 echo C > foo &&
182 git add foo &&
183 git rebase --continue &&
184 echo rebase >expected.args &&
185 cat >expected.data <<-EOF &&
186 $(git rev-parse C) $(git rev-parse HEAD^)
187 $(git rev-parse D) $(git rev-parse HEAD)
188 EOF
189 verify_hook_input
190'
191
192test_expect_success 'git rebase -i (skip)' '
193 git reset --hard D &&
194 clear_hook_input &&
195 test_fail_interactive_rebase "2" --onto A B &&
196 echo D > foo &&
197 git add foo &&
198 git rebase --continue &&
199 echo rebase >expected.args &&
200 cat >expected.data <<-EOF &&
201 $(git rev-parse D) $(git rev-parse HEAD)
202 EOF
203 verify_hook_input
204'
205
206test_expect_success 'git rebase -i (squash)' '
207 git reset --hard D &&
208 clear_hook_input &&
209 test_fail_interactive_rebase "1 squash 2" --onto A B &&
210 echo C > foo &&
211 git add foo &&
212 git rebase --continue &&
213 echo rebase >expected.args &&
214 cat >expected.data <<-EOF &&
215 $(git rev-parse C) $(git rev-parse HEAD)
216 $(git rev-parse D) $(git rev-parse HEAD)
217 EOF
218 verify_hook_input
219'
220
221test_expect_success 'git rebase -i (fixup without conflict)' '
222 git reset --hard D &&
223 clear_hook_input &&
224 FAKE_LINES="1 fixup 2" git rebase -i B &&
225 echo rebase >expected.args &&
226 cat >expected.data <<-EOF &&
227 $(git rev-parse C) $(git rev-parse HEAD)
228 $(git rev-parse D) $(git rev-parse HEAD)
229 EOF
230 verify_hook_input
231'
232
233test_expect_success 'git rebase -i (double edit)' '
234 git reset --hard D &&
235 clear_hook_input &&
236 FAKE_LINES="edit 1 edit 2" git rebase -i B &&
237 git rebase --continue &&
238 echo something > foo &&
239 git add foo &&
240 git rebase --continue &&
241 echo rebase >expected.args &&
242 cat >expected.data <<-EOF &&
243 $(git rev-parse C) $(git rev-parse HEAD^)
244 $(git rev-parse D) $(git rev-parse HEAD)
245 EOF
246 verify_hook_input
247'
248
249test_expect_success 'git rebase -i (exec)' '
250 git reset --hard D &&
251 clear_hook_input &&
252 FAKE_LINES="edit 1 exec_false 2" git rebase -i B &&
253 echo something >bar &&
254 git add bar &&
255 # Fails because of exec false
256 test_must_fail git rebase --continue &&
257 git rebase --continue &&
258 echo rebase >expected.args &&
259 cat >expected.data <<-EOF &&
260 $(git rev-parse C) $(git rev-parse HEAD^)
261 $(git rev-parse D) $(git rev-parse HEAD)
262 EOF
263 verify_hook_input
264'
265
266test_done