Merge branch 'es/worktree-repair-copied' into cw/worktrees-relative
[git/gitster.git] / t / t5407-post-rewrite-hook.sh
blobe99e728236ad07ed011d127fbfbb13126e7ea82c
1 #!/bin/sh
3 # Copyright (c) 2010 Thomas Rast
6 test_description='Test the post-rewrite hook.'
7 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
8 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10 TEST_PASSES_SANITIZE_LEAK=true
11 . ./test-lib.sh
13 test_expect_success 'setup' '
14 test_commit A foo A &&
15 test_commit B foo B &&
16 test_commit C foo C &&
17 test_commit D foo D &&
18 git checkout A^0 &&
19 test_commit E bar E &&
20 test_commit F foo F &&
21 git checkout B &&
22 git merge E &&
23 git tag merge-E &&
24 test_commit G G &&
25 test_commit H H &&
26 test_commit I I &&
27 git checkout main &&
29 test_hook --setup post-rewrite <<-EOF
30 echo \$@ > "$TRASH_DIRECTORY"/post-rewrite.args
31 cat > "$TRASH_DIRECTORY"/post-rewrite.data
32 EOF
35 clear_hook_input () {
36 rm -f post-rewrite.args post-rewrite.data
39 verify_hook_input () {
40 test_cmp expected.args "$TRASH_DIRECTORY"/post-rewrite.args &&
41 test_cmp expected.data "$TRASH_DIRECTORY"/post-rewrite.data
44 test_expect_success 'git commit --amend' '
45 clear_hook_input &&
46 echo "D new message" > newmsg &&
47 oldsha=$(git rev-parse HEAD^0) &&
48 git commit -Fnewmsg --amend &&
49 echo amend > expected.args &&
50 echo $oldsha $(git rev-parse HEAD^0) > expected.data &&
51 verify_hook_input
54 test_expect_success 'git commit --amend --no-post-rewrite' '
55 clear_hook_input &&
56 echo "D new message again" > newmsg &&
57 git commit --no-post-rewrite -Fnewmsg --amend &&
58 test ! -f post-rewrite.args &&
59 test ! -f post-rewrite.data
62 test_expect_success 'git rebase --apply' '
63 git reset --hard D &&
64 clear_hook_input &&
65 test_must_fail git rebase --apply --onto A B &&
66 echo C > foo &&
67 git add foo &&
68 git rebase --continue &&
69 echo rebase >expected.args &&
70 cat >expected.data <<-EOF &&
71 $(git rev-parse C) $(git rev-parse HEAD^)
72 $(git rev-parse D) $(git rev-parse HEAD)
73 EOF
74 verify_hook_input
77 test_expect_success 'git rebase --apply --skip' '
78 git reset --hard D &&
79 clear_hook_input &&
80 test_must_fail git rebase --apply --onto A B &&
81 test_must_fail git rebase --skip &&
82 echo D > foo &&
83 git add foo &&
84 git rebase --continue &&
85 echo rebase >expected.args &&
86 cat >expected.data <<-EOF &&
87 $(git rev-parse C) $(git rev-parse HEAD^)
88 $(git rev-parse D) $(git rev-parse HEAD)
89 EOF
90 verify_hook_input
93 test_expect_success 'git rebase --apply --skip the last one' '
94 git reset --hard F &&
95 clear_hook_input &&
96 test_must_fail git rebase --apply --onto D A &&
97 git rebase --skip &&
98 echo rebase >expected.args &&
99 cat >expected.data <<-EOF &&
100 $(git rev-parse E) $(git rev-parse HEAD)
101 $(git rev-parse F) $(git rev-parse HEAD)
103 verify_hook_input
106 test_expect_success 'git rebase -m' '
107 git reset --hard D &&
108 clear_hook_input &&
109 test_must_fail git rebase -m --onto A B &&
110 echo C > foo &&
111 git add foo &&
112 git rebase --continue &&
113 echo rebase >expected.args &&
114 cat >expected.data <<-EOF &&
115 $(git rev-parse C) $(git rev-parse HEAD^)
116 $(git rev-parse D) $(git rev-parse HEAD)
118 verify_hook_input
121 test_expect_success 'git rebase -m --skip' '
122 git reset --hard D &&
123 clear_hook_input &&
124 test_must_fail git rebase -m --onto A B &&
125 test_must_fail git rebase --skip &&
126 echo D > foo &&
127 git add foo &&
128 git rebase --continue &&
129 echo rebase >expected.args &&
130 cat >expected.data <<-EOF &&
131 $(git rev-parse C) $(git rev-parse HEAD^)
132 $(git rev-parse D) $(git rev-parse HEAD)
134 verify_hook_input
137 test_expect_success 'git rebase with implicit use of merge backend' '
138 git reset --hard D &&
139 clear_hook_input &&
140 test_must_fail git rebase --keep-empty --onto A B &&
141 echo C > foo &&
142 git add foo &&
143 git rebase --continue &&
144 echo rebase >expected.args &&
145 cat >expected.data <<-EOF &&
146 $(git rev-parse C) $(git rev-parse HEAD^)
147 $(git rev-parse D) $(git rev-parse HEAD)
149 verify_hook_input
152 test_expect_success 'git rebase --skip with implicit use of merge backend' '
153 git reset --hard D &&
154 clear_hook_input &&
155 test_must_fail git rebase --keep-empty --onto A B &&
156 test_must_fail git rebase --skip &&
157 echo D > foo &&
158 git add foo &&
159 git rebase --continue &&
160 echo rebase >expected.args &&
161 cat >expected.data <<-EOF &&
162 $(git rev-parse C) $(git rev-parse HEAD^)
163 $(git rev-parse D) $(git rev-parse HEAD)
165 verify_hook_input
168 . "$TEST_DIRECTORY"/lib-rebase.sh
170 set_fake_editor
172 # Helper to work around the lack of one-shot exporting for
173 # test_must_fail (as it is a shell function)
174 test_fail_interactive_rebase () {
176 FAKE_LINES="$1" &&
177 shift &&
178 export FAKE_LINES &&
179 test_must_fail git rebase -i "$@"
183 test_expect_success 'git rebase with failed pick' '
184 clear_hook_input &&
185 cat >todo <<-\EOF &&
186 exec >bar
187 merge -C merge-E E
188 exec >G
189 pick G
190 exec >H 2>I
191 pick H
192 fixup I
196 set_replace_editor todo &&
197 test_must_fail git rebase -i D D 2>err
198 ) &&
199 grep "would be overwritten" err &&
200 rm bar &&
202 test_must_fail git rebase --continue 2>err &&
203 grep "would be overwritten" err &&
204 rm G &&
206 test_must_fail git rebase --continue 2>err &&
207 grep "would be overwritten" err &&
208 rm H &&
210 test_must_fail git rebase --continue 2>err &&
211 grep "would be overwritten" err &&
212 rm I &&
214 git rebase --continue &&
215 echo rebase >expected.args &&
216 cat >expected.data <<-EOF &&
217 $(git rev-parse merge-E) $(git rev-parse HEAD~2)
218 $(git rev-parse G) $(git rev-parse HEAD~1)
219 $(git rev-parse H) $(git rev-parse HEAD)
220 $(git rev-parse I) $(git rev-parse HEAD)
222 verify_hook_input
225 test_expect_success 'git rebase -i (unchanged)' '
226 git reset --hard D &&
227 clear_hook_input &&
228 test_fail_interactive_rebase "1 2" --onto A B &&
229 echo C > foo &&
230 git add foo &&
231 git rebase --continue &&
232 echo rebase >expected.args &&
233 cat >expected.data <<-EOF &&
234 $(git rev-parse C) $(git rev-parse HEAD^)
235 $(git rev-parse D) $(git rev-parse HEAD)
237 verify_hook_input
240 test_expect_success 'git rebase -i (skip)' '
241 git reset --hard D &&
242 clear_hook_input &&
243 test_fail_interactive_rebase "2" --onto A B &&
244 echo D > foo &&
245 git add foo &&
246 git rebase --continue &&
247 echo rebase >expected.args &&
248 cat >expected.data <<-EOF &&
249 $(git rev-parse D) $(git rev-parse HEAD)
251 verify_hook_input
254 test_expect_success 'git rebase -i (squash)' '
255 git reset --hard D &&
256 clear_hook_input &&
257 test_fail_interactive_rebase "1 squash 2" --onto A B &&
258 echo C > foo &&
259 git add foo &&
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)
266 verify_hook_input
269 test_expect_success 'git rebase -i (fixup without conflict)' '
270 git reset --hard D &&
271 clear_hook_input &&
272 FAKE_LINES="1 fixup 2" git rebase -i B &&
273 echo rebase >expected.args &&
274 cat >expected.data <<-EOF &&
275 $(git rev-parse C) $(git rev-parse HEAD)
276 $(git rev-parse D) $(git rev-parse HEAD)
278 verify_hook_input
281 test_expect_success 'git rebase -i (double edit)' '
282 git reset --hard D &&
283 clear_hook_input &&
284 FAKE_LINES="edit 1 edit 2" git rebase -i B &&
285 git rebase --continue &&
286 echo something > foo &&
287 git add foo &&
288 git rebase --continue &&
289 echo rebase >expected.args &&
290 cat >expected.data <<-EOF &&
291 $(git rev-parse C) $(git rev-parse HEAD^)
292 $(git rev-parse D) $(git rev-parse HEAD)
294 verify_hook_input
297 test_expect_success 'git rebase -i (exec)' '
298 git reset --hard D &&
299 clear_hook_input &&
300 FAKE_LINES="edit 1 exec_false 2" git rebase -i B &&
301 echo something >bar &&
302 git add bar &&
303 # Fails because of exec false
304 test_must_fail git rebase --continue &&
305 git rebase --continue &&
306 echo rebase >expected.args &&
307 cat >expected.data <<-EOF &&
308 $(git rev-parse C) $(git rev-parse HEAD^)
309 $(git rev-parse D) $(git rev-parse HEAD)
311 verify_hook_input
314 test_done