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 --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
128. "$TEST_DIRECTORY"/lib-rebase.sh
129
130set_fake_editor
131
132# Helper to work around the lack of one-shot exporting for
133# test_must_fail (as it is a shell function)
134test_fail_interactive_rebase () {
135 (
136 FAKE_LINES="$1" &&
137 shift &&
138 export FAKE_LINES &&
139 test_must_fail git rebase -i "$@"
140 )
141}
142
143test_expect_success 'git rebase -i (unchanged)' '
144 git reset --hard D &&
145 clear_hook_input &&
146 test_fail_interactive_rebase "1 2" --onto A B &&
147 echo C > foo &&
148 git add foo &&
149 git rebase --continue &&
150 echo rebase >expected.args &&
151 cat >expected.data <<-EOF &&
152 $(git rev-parse C) $(git rev-parse HEAD^)
153 $(git rev-parse D) $(git rev-parse HEAD)
154 EOF
155 verify_hook_input
156'
157
158test_expect_success 'git rebase -i (skip)' '
159 git reset --hard D &&
160 clear_hook_input &&
161 test_fail_interactive_rebase "2" --onto A B &&
162 echo D > foo &&
163 git add foo &&
164 git rebase --continue &&
165 echo rebase >expected.args &&
166 cat >expected.data <<-EOF &&
167 $(git rev-parse D) $(git rev-parse HEAD)
168 EOF
169 verify_hook_input
170'
171
172test_expect_success 'git rebase -i (squash)' '
173 git reset --hard D &&
174 clear_hook_input &&
175 test_fail_interactive_rebase "1 squash 2" --onto A B &&
176 echo C > foo &&
177 git add foo &&
178 git rebase --continue &&
179 echo rebase >expected.args &&
180 cat >expected.data <<-EOF &&
181 $(git rev-parse C) $(git rev-parse HEAD)
182 $(git rev-parse D) $(git rev-parse HEAD)
183 EOF
184 verify_hook_input
185'
186
187test_expect_success 'git rebase -i (fixup without conflict)' '
188 git reset --hard D &&
189 clear_hook_input &&
190 FAKE_LINES="1 fixup 2" git rebase -i B &&
191 echo rebase >expected.args &&
192 cat >expected.data <<-EOF &&
193 $(git rev-parse C) $(git rev-parse HEAD)
194 $(git rev-parse D) $(git rev-parse HEAD)
195 EOF
196 verify_hook_input
197'
198
199test_expect_success 'git rebase -i (double edit)' '
200 git reset --hard D &&
201 clear_hook_input &&
202 FAKE_LINES="edit 1 edit 2" git rebase -i B &&
203 git rebase --continue &&
204 echo something > foo &&
205 git add foo &&
206 git rebase --continue &&
207 echo rebase >expected.args &&
208 cat >expected.data <<-EOF &&
209 $(git rev-parse C) $(git rev-parse HEAD^)
210 $(git rev-parse D) $(git rev-parse HEAD)
211 EOF
212 verify_hook_input
213'
214
215test_expect_success 'git rebase -i (exec)' '
216 git reset --hard D &&
217 clear_hook_input &&
218 FAKE_LINES="edit 1 exec_false 2" git rebase -i B &&
219 echo something >bar &&
220 git add bar &&
221 # Fails because of exec false
222 test_must_fail git rebase --continue &&
223 git rebase --continue &&
224 echo rebase >expected.args &&
225 cat >expected.data <<-EOF &&
226 $(git rev-parse C) $(git rev-parse HEAD^)
227 $(git rev-parse D) $(git rev-parse HEAD)
228 EOF
229 verify_hook_input
230'
231
232test_done