Thomas Rast | 6f6bee3 | 2010-03-12 18:04:28 +0100 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
| 3 | # Copyright (c) 2010 Thomas Rast |
| 4 | # |
| 5 | |
| 6 | test_description='Test the post-rewrite hook.' |
Johannes Schindelin | 966b4be | 2020-11-18 23:44:29 +0000 | [diff] [blame] | 7 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
Johannes Schindelin | 334afbc | 2020-11-18 23:44:19 +0000 | [diff] [blame] | 8 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
| 9 | |
Thomas Rast | 6f6bee3 | 2010-03-12 18:04:28 +0100 | [diff] [blame] | 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 && |
Junio C Hamano | ef88ad2 | 2010-12-22 16:50:28 -0800 | [diff] [blame] | 16 | test_commit D foo D && |
| 17 | git checkout A^0 && |
| 18 | test_commit E bar E && |
| 19 | test_commit F foo F && |
Phillip Wood | e032abd | 2023-09-06 15:22:49 +0000 | [diff] [blame] | 20 | git checkout B && |
| 21 | git merge E && |
| 22 | git tag merge-E && |
| 23 | test_commit G G && |
| 24 | test_commit H H && |
| 25 | test_commit I I && |
Ævar Arnfjörð Bjarmason | c36c628 | 2022-03-17 11:13:15 +0100 | [diff] [blame] | 26 | git checkout main && |
Thomas Rast | 6f6bee3 | 2010-03-12 18:04:28 +0100 | [diff] [blame] | 27 | |
Ævar Arnfjörð Bjarmason | c36c628 | 2022-03-17 11:13:15 +0100 | [diff] [blame] | 28 | test_hook --setup post-rewrite <<-EOF |
| 29 | echo \$@ > "$TRASH_DIRECTORY"/post-rewrite.args |
| 30 | cat > "$TRASH_DIRECTORY"/post-rewrite.data |
| 31 | EOF |
| 32 | ' |
Thomas Rast | 6f6bee3 | 2010-03-12 18:04:28 +0100 | [diff] [blame] | 33 | |
| 34 | clear_hook_input () { |
| 35 | rm -f post-rewrite.args post-rewrite.data |
| 36 | } |
| 37 | |
| 38 | verify_hook_input () { |
Felipe Contreras | 1fee89c | 2013-05-30 08:46:35 -0500 | [diff] [blame] | 39 | test_cmp expected.args "$TRASH_DIRECTORY"/post-rewrite.args && |
| 40 | test_cmp expected.data "$TRASH_DIRECTORY"/post-rewrite.data |
Thomas Rast | 6f6bee3 | 2010-03-12 18:04:28 +0100 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | test_expect_success 'git commit --amend' ' |
| 44 | clear_hook_input && |
| 45 | echo "D new message" > newmsg && |
| 46 | oldsha=$(git rev-parse HEAD^0) && |
| 47 | git commit -Fnewmsg --amend && |
| 48 | echo amend > expected.args && |
| 49 | echo $oldsha $(git rev-parse HEAD^0) > expected.data && |
| 50 | verify_hook_input |
| 51 | ' |
| 52 | |
| 53 | test_expect_success 'git commit --amend --no-post-rewrite' ' |
| 54 | clear_hook_input && |
| 55 | echo "D new message again" > newmsg && |
| 56 | git commit --no-post-rewrite -Fnewmsg --amend && |
| 57 | test ! -f post-rewrite.args && |
| 58 | test ! -f post-rewrite.data |
| 59 | ' |
| 60 | |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 61 | test_expect_success 'git rebase --apply' ' |
Thomas Rast | 96e1948 | 2010-03-12 18:04:29 +0100 | [diff] [blame] | 62 | git reset --hard D && |
| 63 | clear_hook_input && |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 64 | test_must_fail git rebase --apply --onto A B && |
Thomas Rast | 96e1948 | 2010-03-12 18:04:29 +0100 | [diff] [blame] | 65 | echo C > foo && |
| 66 | git add foo && |
| 67 | git rebase --continue && |
| 68 | echo rebase >expected.args && |
Junio C Hamano | 141ff8f | 2015-05-22 08:41:31 -0700 | [diff] [blame] | 69 | cat >expected.data <<-EOF && |
| 70 | $(git rev-parse C) $(git rev-parse HEAD^) |
| 71 | $(git rev-parse D) $(git rev-parse HEAD) |
| 72 | EOF |
Thomas Rast | 96e1948 | 2010-03-12 18:04:29 +0100 | [diff] [blame] | 73 | verify_hook_input |
| 74 | ' |
| 75 | |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 76 | test_expect_success 'git rebase --apply --skip' ' |
Thomas Rast | 96e1948 | 2010-03-12 18:04:29 +0100 | [diff] [blame] | 77 | git reset --hard D && |
| 78 | clear_hook_input && |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 79 | test_must_fail git rebase --apply --onto A B && |
Thomas Rast | 96e1948 | 2010-03-12 18:04:29 +0100 | [diff] [blame] | 80 | test_must_fail git rebase --skip && |
| 81 | echo D > foo && |
| 82 | git add foo && |
| 83 | git rebase --continue && |
| 84 | echo rebase >expected.args && |
Junio C Hamano | 141ff8f | 2015-05-22 08:41:31 -0700 | [diff] [blame] | 85 | cat >expected.data <<-EOF && |
Elijah Newren | 45339f7 | 2018-12-11 08:11:35 -0800 | [diff] [blame] | 86 | $(git rev-parse C) $(git rev-parse HEAD^) |
Junio C Hamano | 141ff8f | 2015-05-22 08:41:31 -0700 | [diff] [blame] | 87 | $(git rev-parse D) $(git rev-parse HEAD) |
| 88 | EOF |
Thomas Rast | 96e1948 | 2010-03-12 18:04:29 +0100 | [diff] [blame] | 89 | verify_hook_input |
| 90 | ' |
| 91 | |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 92 | test_expect_success 'git rebase --apply --skip the last one' ' |
Junio C Hamano | ef88ad2 | 2010-12-22 16:50:28 -0800 | [diff] [blame] | 93 | git reset --hard F && |
| 94 | clear_hook_input && |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 95 | test_must_fail git rebase --apply --onto D A && |
Junio C Hamano | ef88ad2 | 2010-12-22 16:50:28 -0800 | [diff] [blame] | 96 | git rebase --skip && |
| 97 | echo rebase >expected.args && |
Junio C Hamano | 141ff8f | 2015-05-22 08:41:31 -0700 | [diff] [blame] | 98 | cat >expected.data <<-EOF && |
| 99 | $(git rev-parse E) $(git rev-parse HEAD) |
Elijah Newren | 45339f7 | 2018-12-11 08:11:35 -0800 | [diff] [blame] | 100 | $(git rev-parse F) $(git rev-parse HEAD) |
Junio C Hamano | 141ff8f | 2015-05-22 08:41:31 -0700 | [diff] [blame] | 101 | EOF |
Junio C Hamano | ef88ad2 | 2010-12-22 16:50:28 -0800 | [diff] [blame] | 102 | verify_hook_input |
| 103 | ' |
| 104 | |
Thomas Rast | b079fee | 2010-03-12 18:04:30 +0100 | [diff] [blame] | 105 | test_expect_success 'git rebase -m' ' |
| 106 | git reset --hard D && |
| 107 | clear_hook_input && |
| 108 | test_must_fail git rebase -m --onto A B && |
| 109 | echo C > foo && |
| 110 | git add foo && |
| 111 | git rebase --continue && |
| 112 | echo rebase >expected.args && |
Junio C Hamano | 141ff8f | 2015-05-22 08:41:31 -0700 | [diff] [blame] | 113 | cat >expected.data <<-EOF && |
| 114 | $(git rev-parse C) $(git rev-parse HEAD^) |
| 115 | $(git rev-parse D) $(git rev-parse HEAD) |
| 116 | EOF |
Thomas Rast | b079fee | 2010-03-12 18:04:30 +0100 | [diff] [blame] | 117 | verify_hook_input |
| 118 | ' |
| 119 | |
| 120 | test_expect_success 'git rebase -m --skip' ' |
| 121 | git reset --hard D && |
| 122 | clear_hook_input && |
Elijah Newren | e951e8f | 2018-06-06 22:05:50 -0700 | [diff] [blame] | 123 | test_must_fail git rebase -m --onto A B && |
Thomas Rast | b079fee | 2010-03-12 18:04:30 +0100 | [diff] [blame] | 124 | test_must_fail git rebase --skip && |
| 125 | echo D > foo && |
| 126 | git add foo && |
| 127 | git rebase --continue && |
| 128 | echo rebase >expected.args && |
Junio C Hamano | 141ff8f | 2015-05-22 08:41:31 -0700 | [diff] [blame] | 129 | cat >expected.data <<-EOF && |
Elijah Newren | 45339f7 | 2018-12-11 08:11:35 -0800 | [diff] [blame] | 130 | $(git rev-parse C) $(git rev-parse HEAD^) |
Junio C Hamano | 141ff8f | 2015-05-22 08:41:31 -0700 | [diff] [blame] | 131 | $(git rev-parse D) $(git rev-parse HEAD) |
| 132 | EOF |
Thomas Rast | b079fee | 2010-03-12 18:04:30 +0100 | [diff] [blame] | 133 | verify_hook_input |
| 134 | ' |
| 135 | |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 136 | test_expect_success 'git rebase with implicit use of merge backend' ' |
Elijah Newren | 5400677 | 2018-12-11 08:11:34 -0800 | [diff] [blame] | 137 | git reset --hard D && |
| 138 | clear_hook_input && |
Johannes Schindelin | dd605e4 | 2019-03-25 11:14:18 -0700 | [diff] [blame] | 139 | test_must_fail git rebase --keep-empty --onto A B && |
Elijah Newren | 5400677 | 2018-12-11 08:11:34 -0800 | [diff] [blame] | 140 | echo C > foo && |
| 141 | git add foo && |
| 142 | git rebase --continue && |
| 143 | echo rebase >expected.args && |
| 144 | cat >expected.data <<-EOF && |
| 145 | $(git rev-parse C) $(git rev-parse HEAD^) |
| 146 | $(git rev-parse D) $(git rev-parse HEAD) |
| 147 | EOF |
| 148 | verify_hook_input |
| 149 | ' |
| 150 | |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 151 | test_expect_success 'git rebase --skip with implicit use of merge backend' ' |
Elijah Newren | 5400677 | 2018-12-11 08:11:34 -0800 | [diff] [blame] | 152 | git reset --hard D && |
| 153 | clear_hook_input && |
Johannes Schindelin | dd605e4 | 2019-03-25 11:14:18 -0700 | [diff] [blame] | 154 | test_must_fail git rebase --keep-empty --onto A B && |
Elijah Newren | 5400677 | 2018-12-11 08:11:34 -0800 | [diff] [blame] | 155 | test_must_fail git rebase --skip && |
| 156 | echo D > foo && |
| 157 | git add foo && |
| 158 | git rebase --continue && |
| 159 | echo rebase >expected.args && |
| 160 | cat >expected.data <<-EOF && |
| 161 | $(git rev-parse C) $(git rev-parse HEAD^) |
| 162 | $(git rev-parse D) $(git rev-parse HEAD) |
| 163 | EOF |
| 164 | verify_hook_input |
| 165 | ' |
| 166 | |
Thomas Rast | b079fee | 2010-03-12 18:04:30 +0100 | [diff] [blame] | 167 | . "$TEST_DIRECTORY"/lib-rebase.sh |
| 168 | |
| 169 | set_fake_editor |
| 170 | |
| 171 | # Helper to work around the lack of one-shot exporting for |
| 172 | # test_must_fail (as it is a shell function) |
| 173 | test_fail_interactive_rebase () { |
| 174 | ( |
| 175 | FAKE_LINES="$1" && |
| 176 | shift && |
| 177 | export FAKE_LINES && |
| 178 | test_must_fail git rebase -i "$@" |
| 179 | ) |
| 180 | } |
| 181 | |
Phillip Wood | e032abd | 2023-09-06 15:22:49 +0000 | [diff] [blame] | 182 | test_expect_success 'git rebase with failed pick' ' |
| 183 | clear_hook_input && |
| 184 | cat >todo <<-\EOF && |
| 185 | exec >bar |
| 186 | merge -C merge-E E |
| 187 | exec >G |
| 188 | pick G |
| 189 | exec >H 2>I |
| 190 | pick H |
| 191 | fixup I |
| 192 | EOF |
| 193 | |
| 194 | ( |
| 195 | set_replace_editor todo && |
| 196 | test_must_fail git rebase -i D D 2>err |
| 197 | ) && |
| 198 | grep "would be overwritten" err && |
| 199 | rm bar && |
| 200 | |
| 201 | test_must_fail git rebase --continue 2>err && |
| 202 | grep "would be overwritten" err && |
| 203 | rm G && |
| 204 | |
| 205 | test_must_fail git rebase --continue 2>err && |
| 206 | grep "would be overwritten" err && |
| 207 | rm H && |
| 208 | |
| 209 | test_must_fail git rebase --continue 2>err && |
| 210 | grep "would be overwritten" err && |
| 211 | rm I && |
| 212 | |
| 213 | git rebase --continue && |
| 214 | echo rebase >expected.args && |
| 215 | cat >expected.data <<-EOF && |
| 216 | $(git rev-parse merge-E) $(git rev-parse HEAD~2) |
| 217 | $(git rev-parse G) $(git rev-parse HEAD~1) |
| 218 | $(git rev-parse H) $(git rev-parse HEAD) |
| 219 | $(git rev-parse I) $(git rev-parse HEAD) |
| 220 | EOF |
| 221 | verify_hook_input |
| 222 | ' |
| 223 | |
Thomas Rast | b079fee | 2010-03-12 18:04:30 +0100 | [diff] [blame] | 224 | test_expect_success 'git rebase -i (unchanged)' ' |
| 225 | git reset --hard D && |
| 226 | clear_hook_input && |
| 227 | test_fail_interactive_rebase "1 2" --onto A B && |
| 228 | echo C > foo && |
| 229 | git add foo && |
| 230 | git rebase --continue && |
| 231 | echo rebase >expected.args && |
Junio C Hamano | 141ff8f | 2015-05-22 08:41:31 -0700 | [diff] [blame] | 232 | cat >expected.data <<-EOF && |
| 233 | $(git rev-parse C) $(git rev-parse HEAD^) |
| 234 | $(git rev-parse D) $(git rev-parse HEAD) |
| 235 | EOF |
Thomas Rast | b079fee | 2010-03-12 18:04:30 +0100 | [diff] [blame] | 236 | verify_hook_input |
| 237 | ' |
| 238 | |
| 239 | test_expect_success 'git rebase -i (skip)' ' |
| 240 | git reset --hard D && |
| 241 | clear_hook_input && |
| 242 | test_fail_interactive_rebase "2" --onto A B && |
| 243 | echo D > foo && |
| 244 | git add foo && |
| 245 | git rebase --continue && |
| 246 | echo rebase >expected.args && |
Junio C Hamano | 141ff8f | 2015-05-22 08:41:31 -0700 | [diff] [blame] | 247 | cat >expected.data <<-EOF && |
| 248 | $(git rev-parse D) $(git rev-parse HEAD) |
| 249 | EOF |
Thomas Rast | b079fee | 2010-03-12 18:04:30 +0100 | [diff] [blame] | 250 | verify_hook_input |
| 251 | ' |
| 252 | |
| 253 | test_expect_success 'git rebase -i (squash)' ' |
| 254 | git reset --hard D && |
| 255 | clear_hook_input && |
| 256 | test_fail_interactive_rebase "1 squash 2" --onto A B && |
| 257 | echo C > foo && |
| 258 | git add foo && |
| 259 | git rebase --continue && |
| 260 | echo rebase >expected.args && |
Junio C Hamano | 141ff8f | 2015-05-22 08:41:31 -0700 | [diff] [blame] | 261 | cat >expected.data <<-EOF && |
| 262 | $(git rev-parse C) $(git rev-parse HEAD) |
| 263 | $(git rev-parse D) $(git rev-parse HEAD) |
| 264 | EOF |
Thomas Rast | b079fee | 2010-03-12 18:04:30 +0100 | [diff] [blame] | 265 | verify_hook_input |
| 266 | ' |
| 267 | |
| 268 | test_expect_success 'git rebase -i (fixup without conflict)' ' |
| 269 | git reset --hard D && |
| 270 | clear_hook_input && |
| 271 | FAKE_LINES="1 fixup 2" git rebase -i B && |
| 272 | echo rebase >expected.args && |
Junio C Hamano | 141ff8f | 2015-05-22 08:41:31 -0700 | [diff] [blame] | 273 | cat >expected.data <<-EOF && |
| 274 | $(git rev-parse C) $(git rev-parse HEAD) |
| 275 | $(git rev-parse D) $(git rev-parse HEAD) |
| 276 | EOF |
Thomas Rast | b079fee | 2010-03-12 18:04:30 +0100 | [diff] [blame] | 277 | verify_hook_input |
| 278 | ' |
| 279 | |
Thomas Rast | 0acb62f | 2010-03-28 21:36:00 +0200 | [diff] [blame] | 280 | test_expect_success 'git rebase -i (double edit)' ' |
| 281 | git reset --hard D && |
| 282 | clear_hook_input && |
| 283 | FAKE_LINES="edit 1 edit 2" git rebase -i B && |
| 284 | git rebase --continue && |
| 285 | echo something > foo && |
| 286 | git add foo && |
| 287 | git rebase --continue && |
| 288 | echo rebase >expected.args && |
Junio C Hamano | 141ff8f | 2015-05-22 08:41:31 -0700 | [diff] [blame] | 289 | cat >expected.data <<-EOF && |
| 290 | $(git rev-parse C) $(git rev-parse HEAD^) |
| 291 | $(git rev-parse D) $(git rev-parse HEAD) |
| 292 | EOF |
Thomas Rast | 0acb62f | 2010-03-28 21:36:00 +0200 | [diff] [blame] | 293 | verify_hook_input |
| 294 | ' |
| 295 | |
Matthieu Moy | b12d3e9 | 2015-05-22 13:15:49 +0000 | [diff] [blame] | 296 | test_expect_success 'git rebase -i (exec)' ' |
Matthieu Moy | 1d968ca | 2015-05-22 13:15:50 +0000 | [diff] [blame] | 297 | git reset --hard D && |
| 298 | clear_hook_input && |
| 299 | FAKE_LINES="edit 1 exec_false 2" git rebase -i B && |
| 300 | echo something >bar && |
| 301 | git add bar && |
| 302 | # Fails because of exec false |
| 303 | test_must_fail git rebase --continue && |
| 304 | git rebase --continue && |
| 305 | echo rebase >expected.args && |
Junio C Hamano | 141ff8f | 2015-05-22 08:41:31 -0700 | [diff] [blame] | 306 | cat >expected.data <<-EOF && |
| 307 | $(git rev-parse C) $(git rev-parse HEAD^) |
| 308 | $(git rev-parse D) $(git rev-parse HEAD) |
| 309 | EOF |
Matthieu Moy | 1d968ca | 2015-05-22 13:15:50 +0000 | [diff] [blame] | 310 | verify_hook_input |
| 311 | ' |
| 312 | |
Thomas Rast | 6f6bee3 | 2010-03-12 18:04:28 +0100 | [diff] [blame] | 313 | test_done |