Git 2.32.7
[alt-git.git] / t / t5407-post-rewrite-hook.sh
blob5bb23cc3a4e519a282a88d87ca6c1d4ddc6dca29
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-lib.sh
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
23 mkdir .git/hooks
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
32 clear_hook_input () {
33 rm -f post-rewrite.args post-rewrite.data
36 verify_hook_input () {
37 test_cmp expected.args "$TRASH_DIRECTORY"/post-rewrite.args &&
38 test_cmp expected.data "$TRASH_DIRECTORY"/post-rewrite.data
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
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
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
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
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
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)
115 verify_hook_input
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)
131 verify_hook_input
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)
146 verify_hook_input
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)
162 verify_hook_input
165 . "$TEST_DIRECTORY"/lib-rebase.sh
167 set_fake_editor
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 () {
173 FAKE_LINES="$1" &&
174 shift &&
175 export FAKE_LINES &&
176 test_must_fail git rebase -i "$@"
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)
192 verify_hook_input
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)
206 verify_hook_input
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)
221 verify_hook_input
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)
233 verify_hook_input
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)
249 verify_hook_input
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)
266 verify_hook_input
269 test_done