Merge tag 'v2.3.0'
[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 . ./test-lib.sh
8
9 test_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
20 mkdir .git/hooks
21
22 cat >.git/hooks/post-rewrite <<EOF
23 #!/bin/sh
24 echo \$@ > "$TRASH_DIRECTORY"/post-rewrite.args
25 cat > "$TRASH_DIRECTORY"/post-rewrite.data
26 EOF
27 chmod u+x .git/hooks/post-rewrite
28
29 clear_hook_input () {
30         rm -f post-rewrite.args post-rewrite.data
31 }
32
33 verify_hook_input () {
34         test_cmp expected.args "$TRASH_DIRECTORY"/post-rewrite.args &&
35         test_cmp expected.data "$TRASH_DIRECTORY"/post-rewrite.data
36 }
37
38 test_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
48 test_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
56 test_expect_success 'git cherry-pick' '
57         git reset --hard D &&
58         clear_hook_input &&
59         git checkout A &&
60         test_must_fail git cherry-pick B..D &&
61         echo C > foo &&
62         git add foo &&
63         git cherry-pick --continue &&
64         echo cherry-pick >expected.args &&
65         cat >expected.data <<EOF &&
66 $(git rev-parse C) $(git rev-parse HEAD^)
67 $(git rev-parse D) $(git rev-parse HEAD)
68 EOF
69         verify_hook_input
70 '
71
72 test_expect_success 'git rebase' '
73         git reset --hard D &&
74         clear_hook_input &&
75         test_must_fail git rebase --onto A B &&
76         echo C > 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
87 test_expect_success 'git rebase --skip' '
88         git reset --hard D &&
89         clear_hook_input &&
90         test_must_fail git rebase --onto A B &&
91         test_must_fail git rebase --skip &&
92         echo D > foo &&
93         git add foo &&
94         git rebase --continue &&
95         echo rebase >expected.args &&
96         cat >expected.data <<EOF &&
97 $(git rev-parse D) $(git rev-parse HEAD)
98 EOF
99         verify_hook_input
100 '
101
102 test_expect_success 'git rebase --skip the last one' '
103         git reset --hard F &&
104         clear_hook_input &&
105         test_must_fail git rebase --onto D A &&
106         git rebase --skip &&
107         echo rebase >expected.args &&
108         cat >expected.data <<EOF &&
109 $(git rev-parse E) $(git rev-parse HEAD)
110 EOF
111         verify_hook_input
112 '
113
114 test_expect_success 'git rebase -m' '
115         git reset --hard D &&
116         clear_hook_input &&
117         test_must_fail git rebase -m --onto A B &&
118         echo C > foo &&
119         git add foo &&
120         git rebase --continue &&
121         echo rebase >expected.args &&
122         cat >expected.data <<EOF &&
123 $(git rev-parse C) $(git rev-parse HEAD^)
124 $(git rev-parse D) $(git rev-parse HEAD)
125 EOF
126         verify_hook_input
127 '
128
129 test_expect_success 'git rebase -m --skip' '
130         git reset --hard D &&
131         clear_hook_input &&
132         test_must_fail git rebase --onto A B &&
133         test_must_fail git rebase --skip &&
134         echo D > foo &&
135         git add foo &&
136         git rebase --continue &&
137         echo rebase >expected.args &&
138         cat >expected.data <<EOF &&
139 $(git rev-parse D) $(git rev-parse HEAD)
140 EOF
141         verify_hook_input
142 '
143
144 . "$TEST_DIRECTORY"/lib-rebase.sh
145
146 set_fake_editor
147
148 # Helper to work around the lack of one-shot exporting for
149 # test_must_fail (as it is a shell function)
150 test_fail_interactive_rebase () {
151         (
152                 FAKE_LINES="$1" &&
153                 shift &&
154                 export FAKE_LINES &&
155                 test_must_fail git rebase -i "$@"
156         )
157 }
158
159 test_expect_success 'git rebase -i (unchanged)' '
160         git reset --hard D &&
161         clear_hook_input &&
162         test_fail_interactive_rebase "1 2" --onto A B &&
163         echo C > foo &&
164         git add foo &&
165         git rebase --continue &&
166         echo rebase >expected.args &&
167         cat >expected.data <<EOF &&
168 $(git rev-parse C) $(git rev-parse HEAD^)
169 $(git rev-parse D) $(git rev-parse HEAD)
170 EOF
171         verify_hook_input
172 '
173
174 test_expect_success 'git rebase -i (skip)' '
175         git reset --hard D &&
176         clear_hook_input &&
177         test_fail_interactive_rebase "2" --onto A B &&
178         echo D > foo &&
179         git add foo &&
180         git rebase --continue &&
181         echo rebase >expected.args &&
182         cat >expected.data <<EOF &&
183 $(git rev-parse D) $(git rev-parse HEAD)
184 EOF
185         verify_hook_input
186 '
187
188 test_expect_success 'git rebase -i (squash)' '
189         git reset --hard D &&
190         clear_hook_input &&
191         test_fail_interactive_rebase "1 squash 2" --onto A B &&
192         echo C > foo &&
193         git add foo &&
194         git rebase --continue &&
195         echo rebase >expected.args &&
196         cat >expected.data <<EOF &&
197 $(git rev-parse C) $(git rev-parse HEAD)
198 $(git rev-parse D) $(git rev-parse HEAD)
199 EOF
200         verify_hook_input
201 '
202
203 test_expect_success 'git rebase -i (fixup without conflict)' '
204         git reset --hard D &&
205         clear_hook_input &&
206         FAKE_LINES="1 fixup 2" git rebase -i B &&
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
215 test_expect_success 'git rebase -i (double edit)' '
216         git reset --hard D &&
217         clear_hook_input &&
218         FAKE_LINES="edit 1 edit 2" git rebase -i B &&
219         git rebase --continue &&
220         echo something > foo &&
221         git add foo &&
222         git rebase --continue &&
223         echo rebase >expected.args &&
224         cat >expected.data <<EOF &&
225 $(git rev-parse C) $(git rev-parse HEAD^)
226 $(git rev-parse D) $(git rev-parse HEAD)
227 EOF
228         verify_hook_input
229 '
230
231 test_done