6426ec89919236c5d798eeb56e06d252119097c5
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 D) $(git rev-parse HEAD)
82 EOF
83 verify_hook_input
84'
85
86test_expect_success 'git rebase --skip the last one' '
87 git reset --hard F &&
88 clear_hook_input &&
89 test_must_fail git rebase --onto D A &&
90 git rebase --skip &&
91 echo rebase >expected.args &&
92 cat >expected.data <<-EOF &&
93 $(git rev-parse E) $(git rev-parse HEAD)
94 EOF
95 verify_hook_input
96'
97
98test_expect_success 'git rebase -m' '
99 git reset --hard D &&
100 clear_hook_input &&
101 test_must_fail git rebase -m --onto A B &&
102 echo C > foo &&
103 git add foo &&
104 git rebase --continue &&
105 echo rebase >expected.args &&
106 cat >expected.data <<-EOF &&
107 $(git rev-parse C) $(git rev-parse HEAD^)
108 $(git rev-parse D) $(git rev-parse HEAD)
109 EOF
110 verify_hook_input
111'
112
113test_expect_success 'git rebase -m --skip' '
114 git reset --hard D &&
115 clear_hook_input &&
116 test_must_fail git rebase -m --onto A B &&
117 test_must_fail git rebase --skip &&
118 echo D > foo &&
119 git add foo &&
120 git rebase --continue &&
121 echo rebase >expected.args &&
122 cat >expected.data <<-EOF &&
123 $(git rev-parse D) $(git rev-parse HEAD)
124 EOF
125 verify_hook_input
126'
127
128test_expect_success 'git rebase with implicit use of interactive backend' '
129 git reset --hard D &&
130 clear_hook_input &&
131 test_must_fail git rebase --keep --onto A B &&
132 echo C > foo &&
133 git add foo &&
134 git rebase --continue &&
135 echo rebase >expected.args &&
136 cat >expected.data <<-EOF &&
137 $(git rev-parse C) $(git rev-parse HEAD^)
138 $(git rev-parse D) $(git rev-parse HEAD)
139 EOF
140 verify_hook_input
141'
142
143test_expect_success 'git rebase --skip with implicit use of interactive backend' '
144 git reset --hard D &&
145 clear_hook_input &&
146 test_must_fail git rebase --keep --onto A B &&
147 test_must_fail git rebase --skip &&
148 echo D > foo &&
149 git add foo &&
150 git rebase --continue &&
151 echo rebase >expected.args &&
152 cat >expected.data <<-EOF &&
153 $(git rev-parse C) $(git rev-parse HEAD^)
154 $(git rev-parse D) $(git rev-parse HEAD)
155 EOF
156 verify_hook_input
157'
158
159. "$TEST_DIRECTORY"/lib-rebase.sh
160
161set_fake_editor
162
163# Helper to work around the lack of one-shot exporting for
164# test_must_fail (as it is a shell function)
165test_fail_interactive_rebase () {
166 (
167 FAKE_LINES="$1" &&
168 shift &&
169 export FAKE_LINES &&
170 test_must_fail git rebase -i "$@"
171 )
172}
173
174test_expect_success 'git rebase -i (unchanged)' '
175 git reset --hard D &&
176 clear_hook_input &&
177 test_fail_interactive_rebase "1 2" --onto A B &&
178 echo C > foo &&
179 git add foo &&
180 git rebase --continue &&
181 echo rebase >expected.args &&
182 cat >expected.data <<-EOF &&
183 $(git rev-parse C) $(git rev-parse HEAD^)
184 $(git rev-parse D) $(git rev-parse HEAD)
185 EOF
186 verify_hook_input
187'
188
189test_expect_success 'git rebase -i (skip)' '
190 git reset --hard D &&
191 clear_hook_input &&
192 test_fail_interactive_rebase "2" --onto A B &&
193 echo D > foo &&
194 git add foo &&
195 git rebase --continue &&
196 echo rebase >expected.args &&
197 cat >expected.data <<-EOF &&
198 $(git rev-parse D) $(git rev-parse HEAD)
199 EOF
200 verify_hook_input
201'
202
203test_expect_success 'git rebase -i (squash)' '
204 git reset --hard D &&
205 clear_hook_input &&
206 test_fail_interactive_rebase "1 squash 2" --onto A B &&
207 echo C > foo &&
208 git add foo &&
209 git rebase --continue &&
210 echo rebase >expected.args &&
211 cat >expected.data <<-EOF &&
212 $(git rev-parse C) $(git rev-parse HEAD)
213 $(git rev-parse D) $(git rev-parse HEAD)
214 EOF
215 verify_hook_input
216'
217
218test_expect_success 'git rebase -i (fixup without conflict)' '
219 git reset --hard D &&
220 clear_hook_input &&
221 FAKE_LINES="1 fixup 2" git rebase -i B &&
222 echo rebase >expected.args &&
223 cat >expected.data <<-EOF &&
224 $(git rev-parse C) $(git rev-parse HEAD)
225 $(git rev-parse D) $(git rev-parse HEAD)
226 EOF
227 verify_hook_input
228'
229
230test_expect_success 'git rebase -i (double edit)' '
231 git reset --hard D &&
232 clear_hook_input &&
233 FAKE_LINES="edit 1 edit 2" git rebase -i B &&
234 git rebase --continue &&
235 echo something > foo &&
236 git add foo &&
237 git rebase --continue &&
238 echo rebase >expected.args &&
239 cat >expected.data <<-EOF &&
240 $(git rev-parse C) $(git rev-parse HEAD^)
241 $(git rev-parse D) $(git rev-parse HEAD)
242 EOF
243 verify_hook_input
244'
245
246test_expect_success 'git rebase -i (exec)' '
247 git reset --hard D &&
248 clear_hook_input &&
249 FAKE_LINES="edit 1 exec_false 2" git rebase -i B &&
250 echo something >bar &&
251 git add bar &&
252 # Fails because of exec false
253 test_must_fail git rebase --continue &&
254 git rebase --continue &&
255 echo rebase >expected.args &&
256 cat >expected.data <<-EOF &&
257 $(git rev-parse C) $(git rev-parse HEAD^)
258 $(git rev-parse D) $(git rev-parse HEAD)
259 EOF
260 verify_hook_input
261'
262
263test_done