Merge branch 'jc/calloc-fix'
[git] / t / t5407-post-rewrite-hook.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2010 Thomas Rast
4 #
5
6 test_description='Test the post-rewrite hook.'
7 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
8 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
9
10 . ./test-lib.sh
11
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 &&
17         git checkout A^0 &&
18         test_commit E bar E &&
19         test_commit F foo F &&
20         git checkout main
21 '
22
23 mkdir .git/hooks
24
25 cat >.git/hooks/post-rewrite <<EOF
26 #!/bin/sh
27 echo \$@ > "$TRASH_DIRECTORY"/post-rewrite.args
28 cat > "$TRASH_DIRECTORY"/post-rewrite.data
29 EOF
30 chmod u+x .git/hooks/post-rewrite
31
32 clear_hook_input () {
33         rm -f post-rewrite.args post-rewrite.data
34 }
35
36 verify_hook_input () {
37         test_cmp expected.args "$TRASH_DIRECTORY"/post-rewrite.args &&
38         test_cmp expected.data "$TRASH_DIRECTORY"/post-rewrite.data
39 }
40
41 test_expect_success 'git commit --amend' '
42         clear_hook_input &&
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 &&
48         verify_hook_input
49 '
50
51 test_expect_success 'git commit --amend --no-post-rewrite' '
52         clear_hook_input &&
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
57 '
58
59 test_expect_success 'git rebase --apply' '
60         git reset --hard D &&
61         clear_hook_input &&
62         test_must_fail git rebase --apply --onto A B &&
63         echo C > foo &&
64         git add foo &&
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)
70         EOF
71         verify_hook_input
72 '
73
74 test_expect_success 'git rebase --apply --skip' '
75         git reset --hard D &&
76         clear_hook_input &&
77         test_must_fail git rebase --apply --onto A B &&
78         test_must_fail git rebase --skip &&
79         echo D > foo &&
80         git add foo &&
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)
86         EOF
87         verify_hook_input
88 '
89
90 test_expect_success 'git rebase --apply --skip the last one' '
91         git reset --hard F &&
92         clear_hook_input &&
93         test_must_fail git rebase --apply --onto D A &&
94         git rebase --skip &&
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)
99         EOF
100         verify_hook_input
101 '
102
103 test_expect_success 'git rebase -m' '
104         git reset --hard D &&
105         clear_hook_input &&
106         test_must_fail git rebase -m --onto A B &&
107         echo C > foo &&
108         git add foo &&
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)
114         EOF
115         verify_hook_input
116 '
117
118 test_expect_success 'git rebase -m --skip' '
119         git reset --hard D &&
120         clear_hook_input &&
121         test_must_fail git rebase -m --onto A B &&
122         test_must_fail git rebase --skip &&
123         echo D > foo &&
124         git add foo &&
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)
130         EOF
131         verify_hook_input
132 '
133
134 test_expect_success 'git rebase with implicit use of merge backend' '
135         git reset --hard D &&
136         clear_hook_input &&
137         test_must_fail git rebase --keep-empty --onto A B &&
138         echo C > foo &&
139         git add foo &&
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)
145         EOF
146         verify_hook_input
147 '
148
149 test_expect_success 'git rebase --skip with implicit use of merge backend' '
150         git reset --hard D &&
151         clear_hook_input &&
152         test_must_fail git rebase --keep-empty --onto A B &&
153         test_must_fail git rebase --skip &&
154         echo D > foo &&
155         git add foo &&
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)
161         EOF
162         verify_hook_input
163 '
164
165 . "$TEST_DIRECTORY"/lib-rebase.sh
166
167 set_fake_editor
168
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 () {
172         (
173                 FAKE_LINES="$1" &&
174                 shift &&
175                 export FAKE_LINES &&
176                 test_must_fail git rebase -i "$@"
177         )
178 }
179
180 test_expect_success 'git rebase -i (unchanged)' '
181         git reset --hard D &&
182         clear_hook_input &&
183         test_fail_interactive_rebase "1 2" --onto A B &&
184         echo C > foo &&
185         git add foo &&
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)
191         EOF
192         verify_hook_input
193 '
194
195 test_expect_success 'git rebase -i (skip)' '
196         git reset --hard D &&
197         clear_hook_input &&
198         test_fail_interactive_rebase "2" --onto A B &&
199         echo D > foo &&
200         git add foo &&
201         git rebase --continue &&
202         echo rebase >expected.args &&
203         cat >expected.data <<-EOF &&
204         $(git rev-parse D) $(git rev-parse HEAD)
205         EOF
206         verify_hook_input
207 '
208
209 test_expect_success 'git rebase -i (squash)' '
210         git reset --hard D &&
211         clear_hook_input &&
212         test_fail_interactive_rebase "1 squash 2" --onto A B &&
213         echo C > foo &&
214         git add foo &&
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)
220         EOF
221         verify_hook_input
222 '
223
224 test_expect_success 'git rebase -i (fixup without conflict)' '
225         git reset --hard D &&
226         clear_hook_input &&
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)
232         EOF
233         verify_hook_input
234 '
235
236 test_expect_success 'git rebase -i (double edit)' '
237         git reset --hard D &&
238         clear_hook_input &&
239         FAKE_LINES="edit 1 edit 2" git rebase -i B &&
240         git rebase --continue &&
241         echo something > foo &&
242         git add 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)
248         EOF
249         verify_hook_input
250 '
251
252 test_expect_success 'git rebase -i (exec)' '
253         git reset --hard D &&
254         clear_hook_input &&
255         FAKE_LINES="edit 1 exec_false 2" git rebase -i B &&
256         echo something >bar &&
257         git add 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)
265         EOF
266         verify_hook_input
267 '
268
269 test_done