Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='prepare-commit-msg hook' |
| 4 | |
| 5 | . ./test-lib.sh |
| 6 | |
Phillip Wood | 15cd6d3 | 2018-01-24 12:34:21 +0000 | [diff] [blame] | 7 | test_expect_success 'set up commits for rebasing' ' |
| 8 | test_commit root && |
| 9 | test_commit a a a && |
| 10 | test_commit b b b && |
| 11 | git checkout -b rebase-me root && |
| 12 | test_commit rebase-a a aa && |
| 13 | test_commit rebase-b b bb && |
| 14 | for i in $(test_seq 1 13) |
| 15 | do |
| 16 | test_commit rebase-$i c $i |
| 17 | done && |
| 18 | git checkout master && |
| 19 | |
| 20 | cat >rebase-todo <<-EOF |
| 21 | pick $(git rev-parse rebase-a) |
| 22 | pick $(git rev-parse rebase-b) |
| 23 | fixup $(git rev-parse rebase-1) |
| 24 | fixup $(git rev-parse rebase-2) |
| 25 | pick $(git rev-parse rebase-3) |
| 26 | fixup $(git rev-parse rebase-4) |
| 27 | squash $(git rev-parse rebase-5) |
| 28 | reword $(git rev-parse rebase-6) |
| 29 | squash $(git rev-parse rebase-7) |
| 30 | fixup $(git rev-parse rebase-8) |
| 31 | fixup $(git rev-parse rebase-9) |
| 32 | edit $(git rev-parse rebase-10) |
| 33 | squash $(git rev-parse rebase-11) |
| 34 | squash $(git rev-parse rebase-12) |
| 35 | edit $(git rev-parse rebase-13) |
| 36 | EOF |
| 37 | ' |
| 38 | |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 39 | test_expect_success 'with no hook' ' |
| 40 | |
| 41 | echo "foo" > file && |
| 42 | git add file && |
| 43 | git commit -m "first" |
| 44 | |
| 45 | ' |
| 46 | |
| 47 | # set up fake editor for interactive editing |
| 48 | cat > fake-editor <<'EOF' |
| 49 | #!/bin/sh |
| 50 | exit 0 |
| 51 | EOF |
| 52 | chmod +x fake-editor |
Bryan Donlan | f69e836 | 2008-05-04 01:37:59 -0400 | [diff] [blame] | 53 | |
| 54 | ## Not using test_set_editor here so we can easily ensure the editor variable |
| 55 | ## is only set for the editor tests |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 56 | FAKE_EDITOR="$(pwd)/fake-editor" |
| 57 | export FAKE_EDITOR |
| 58 | |
| 59 | # now install hook that always succeeds and adds a message |
| 60 | HOOKDIR="$(git rev-parse --git-dir)/hooks" |
| 61 | HOOK="$HOOKDIR/prepare-commit-msg" |
| 62 | mkdir -p "$HOOKDIR" |
Jeff King | 462f8ca | 2008-03-12 17:42:43 -0400 | [diff] [blame] | 63 | echo "#!$SHELL_PATH" > "$HOOK" |
| 64 | cat >> "$HOOK" <<'EOF' |
| 65 | |
Phillip Wood | 15cd6d3 | 2018-01-24 12:34:21 +0000 | [diff] [blame] | 66 | GIT_DIR=$(git rev-parse --git-dir) |
| 67 | if test -d "$GIT_DIR/rebase-merge" |
| 68 | then |
| 69 | rebasing=1 |
| 70 | else |
| 71 | rebasing=0 |
| 72 | fi |
| 73 | |
| 74 | get_last_cmd () { |
| 75 | tail -n1 "$GIT_DIR/rebase-merge/done" | { |
| 76 | read cmd id _ |
| 77 | git log --pretty="[$cmd %s]" -n1 $id |
| 78 | } |
| 79 | } |
| 80 | |
Phillip Wood | 4f8cbf2 | 2018-01-24 12:34:20 +0000 | [diff] [blame] | 81 | if test "$2" = commit |
| 82 | then |
Phillip Wood | 15cd6d3 | 2018-01-24 12:34:21 +0000 | [diff] [blame] | 83 | if test $rebasing = 1 |
| 84 | then |
| 85 | source="$3" |
| 86 | else |
| 87 | source=$(git rev-parse "$3") |
| 88 | fi |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 89 | else |
Phillip Wood | 4f8cbf2 | 2018-01-24 12:34:20 +0000 | [diff] [blame] | 90 | source=${2-default} |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 91 | fi |
Phillip Wood | 15cd6d3 | 2018-01-24 12:34:21 +0000 | [diff] [blame] | 92 | test "$GIT_EDITOR" = : && source="$source (no editor)" |
| 93 | |
| 94 | if test $rebasing = 1 |
Phillip Wood | 4f8cbf2 | 2018-01-24 12:34:20 +0000 | [diff] [blame] | 95 | then |
Phillip Wood | 15cd6d3 | 2018-01-24 12:34:21 +0000 | [diff] [blame] | 96 | echo "$source $(get_last_cmd)" >"$1" |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 97 | else |
Phillip Wood | 4f8cbf2 | 2018-01-24 12:34:20 +0000 | [diff] [blame] | 98 | sed -e "1s/.*/$source/" "$1" >msg.tmp |
Phillip Wood | 15cd6d3 | 2018-01-24 12:34:21 +0000 | [diff] [blame] | 99 | mv msg.tmp "$1" |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 100 | fi |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 101 | exit 0 |
| 102 | EOF |
| 103 | chmod +x "$HOOK" |
| 104 | |
| 105 | echo dummy template > "$(git rev-parse --git-dir)/template" |
| 106 | |
| 107 | test_expect_success 'with hook (-m)' ' |
| 108 | |
| 109 | echo "more" >> file && |
| 110 | git add file && |
| 111 | git commit -m "more" && |
Elia Pinto | 0c92325 | 2016-01-08 12:06:23 +0100 | [diff] [blame] | 112 | test "$(git log -1 --pretty=format:%s)" = "message (no editor)" |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 113 | |
| 114 | ' |
| 115 | |
| 116 | test_expect_success 'with hook (-m editor)' ' |
| 117 | |
| 118 | echo "more" >> file && |
| 119 | git add file && |
Bryan Donlan | f69e836 | 2008-05-04 01:37:59 -0400 | [diff] [blame] | 120 | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -e -m "more more" && |
Elia Pinto | 0c92325 | 2016-01-08 12:06:23 +0100 | [diff] [blame] | 121 | test "$(git log -1 --pretty=format:%s)" = message |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 122 | |
| 123 | ' |
| 124 | |
| 125 | test_expect_success 'with hook (-t)' ' |
| 126 | |
| 127 | echo "more" >> file && |
| 128 | git add file && |
| 129 | git commit -t "$(git rev-parse --git-dir)/template" && |
Elia Pinto | 0c92325 | 2016-01-08 12:06:23 +0100 | [diff] [blame] | 130 | test "$(git log -1 --pretty=format:%s)" = template |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 131 | |
| 132 | ' |
| 133 | |
| 134 | test_expect_success 'with hook (-F)' ' |
| 135 | |
| 136 | echo "more" >> file && |
| 137 | git add file && |
| 138 | (echo more | git commit -F -) && |
Elia Pinto | 0c92325 | 2016-01-08 12:06:23 +0100 | [diff] [blame] | 139 | test "$(git log -1 --pretty=format:%s)" = "message (no editor)" |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 140 | |
| 141 | ' |
| 142 | |
| 143 | test_expect_success 'with hook (-F editor)' ' |
| 144 | |
| 145 | echo "more" >> file && |
| 146 | git add file && |
Bryan Donlan | f69e836 | 2008-05-04 01:37:59 -0400 | [diff] [blame] | 147 | (echo more more | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -e -F -) && |
Elia Pinto | 0c92325 | 2016-01-08 12:06:23 +0100 | [diff] [blame] | 148 | test "$(git log -1 --pretty=format:%s)" = message |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 149 | |
| 150 | ' |
| 151 | |
| 152 | test_expect_success 'with hook (-C)' ' |
| 153 | |
Elia Pinto | 0c92325 | 2016-01-08 12:06:23 +0100 | [diff] [blame] | 154 | head=$(git rev-parse HEAD) && |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 155 | echo "more" >> file && |
| 156 | git add file && |
| 157 | git commit -C $head && |
Elia Pinto | 0c92325 | 2016-01-08 12:06:23 +0100 | [diff] [blame] | 158 | test "$(git log -1 --pretty=format:%s)" = "$head (no editor)" |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 159 | |
| 160 | ' |
| 161 | |
| 162 | test_expect_success 'with hook (editor)' ' |
| 163 | |
| 164 | echo "more more" >> file && |
| 165 | git add file && |
Bryan Donlan | f69e836 | 2008-05-04 01:37:59 -0400 | [diff] [blame] | 166 | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit && |
Elia Pinto | 0c92325 | 2016-01-08 12:06:23 +0100 | [diff] [blame] | 167 | test "$(git log -1 --pretty=format:%s)" = default |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 168 | |
| 169 | ' |
| 170 | |
| 171 | test_expect_success 'with hook (--amend)' ' |
| 172 | |
Elia Pinto | 0c92325 | 2016-01-08 12:06:23 +0100 | [diff] [blame] | 173 | head=$(git rev-parse HEAD) && |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 174 | echo "more" >> file && |
| 175 | git add file && |
Bryan Donlan | f69e836 | 2008-05-04 01:37:59 -0400 | [diff] [blame] | 176 | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --amend && |
Elia Pinto | 0c92325 | 2016-01-08 12:06:23 +0100 | [diff] [blame] | 177 | test "$(git log -1 --pretty=format:%s)" = "$head" |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 178 | |
| 179 | ' |
| 180 | |
| 181 | test_expect_success 'with hook (-c)' ' |
| 182 | |
Elia Pinto | 0c92325 | 2016-01-08 12:06:23 +0100 | [diff] [blame] | 183 | head=$(git rev-parse HEAD) && |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 184 | echo "more" >> file && |
| 185 | git add file && |
Bryan Donlan | f69e836 | 2008-05-04 01:37:59 -0400 | [diff] [blame] | 186 | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -c $head && |
Elia Pinto | 0c92325 | 2016-01-08 12:06:23 +0100 | [diff] [blame] | 187 | test "$(git log -1 --pretty=format:%s)" = "$head" |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 188 | |
| 189 | ' |
| 190 | |
Jay Soffian | 65969d4 | 2011-02-14 20:07:50 -0500 | [diff] [blame] | 191 | test_expect_success 'with hook (merge)' ' |
| 192 | |
Benoit Pierre | 1fc4f97 | 2014-03-18 11:00:55 +0100 | [diff] [blame] | 193 | test_when_finished "git checkout -f master" && |
| 194 | git checkout -B other HEAD@{1} && |
| 195 | echo "more" >>file && |
Jay Soffian | 65969d4 | 2011-02-14 20:07:50 -0500 | [diff] [blame] | 196 | git add file && |
| 197 | git commit -m other && |
| 198 | git checkout - && |
Benoit Pierre | 1fc4f97 | 2014-03-18 11:00:55 +0100 | [diff] [blame] | 199 | git merge --no-ff other && |
Elia Pinto | 0c92325 | 2016-01-08 12:06:23 +0100 | [diff] [blame] | 200 | test "$(git log -1 --pretty=format:%s)" = "merge (no editor)" |
Benoit Pierre | 1fc4f97 | 2014-03-18 11:00:55 +0100 | [diff] [blame] | 201 | ' |
| 202 | |
| 203 | test_expect_success 'with hook and editor (merge)' ' |
| 204 | |
| 205 | test_when_finished "git checkout -f master" && |
| 206 | git checkout -B other HEAD@{1} && |
| 207 | echo "more" >>file && |
| 208 | git add file && |
| 209 | git commit -m other && |
| 210 | git checkout - && |
| 211 | env GIT_EDITOR="\"\$FAKE_EDITOR\"" git merge --no-ff -e other && |
Elia Pinto | 0c92325 | 2016-01-08 12:06:23 +0100 | [diff] [blame] | 212 | test "$(git log -1 --pretty=format:%s)" = "merge" |
Jay Soffian | 65969d4 | 2011-02-14 20:07:50 -0500 | [diff] [blame] | 213 | ' |
| 214 | |
Phillip Wood | 15cd6d3 | 2018-01-24 12:34:21 +0000 | [diff] [blame] | 215 | test_rebase () { |
| 216 | expect=$1 && |
| 217 | mode=$2 && |
| 218 | test_expect_$expect C_LOCALE_OUTPUT "with hook (rebase $mode)" ' |
| 219 | test_when_finished "\ |
| 220 | git rebase --abort |
| 221 | git checkout -f master |
| 222 | git branch -D tmp" && |
| 223 | git checkout -b tmp rebase-me && |
| 224 | GIT_SEQUENCE_EDITOR="cp rebase-todo" && |
| 225 | GIT_EDITOR="\"$FAKE_EDITOR\"" && |
| 226 | ( |
| 227 | export GIT_SEQUENCE_EDITOR GIT_EDITOR && |
| 228 | test_must_fail git rebase $mode b && |
| 229 | echo x >a && |
| 230 | git add a && |
| 231 | test_must_fail git rebase --continue && |
| 232 | echo x >b && |
| 233 | git add b && |
| 234 | git commit && |
| 235 | git rebase --continue && |
| 236 | echo y >a && |
| 237 | git add a && |
| 238 | git commit && |
| 239 | git rebase --continue && |
| 240 | echo y >b && |
| 241 | git add b && |
| 242 | git rebase --continue |
| 243 | ) && |
| 244 | if test $mode = -p # reword amended after pick |
| 245 | then |
| 246 | n=18 |
| 247 | else |
| 248 | n=17 |
| 249 | fi && |
| 250 | git log --pretty=%s -g -n$n HEAD@{1} >actual && |
| 251 | test_cmp "$TEST_DIRECTORY/t7505/expected-rebase$mode" actual |
| 252 | ' |
| 253 | } |
| 254 | |
Phillip Wood | 66618a5 | 2018-01-24 12:34:22 +0000 | [diff] [blame] | 255 | test_rebase success -i |
| 256 | test_rebase success -p |
Phillip Wood | 15cd6d3 | 2018-01-24 12:34:21 +0000 | [diff] [blame] | 257 | |
Phillip Wood | 66618a5 | 2018-01-24 12:34:22 +0000 | [diff] [blame] | 258 | test_expect_success 'with hook (cherry-pick)' ' |
Phillip Wood | 15cd6d3 | 2018-01-24 12:34:21 +0000 | [diff] [blame] | 259 | test_when_finished "git checkout -f master" && |
| 260 | git checkout -B other b && |
| 261 | git cherry-pick rebase-1 && |
| 262 | test "$(git log -1 --pretty=format:%s)" = "message (no editor)" |
| 263 | ' |
| 264 | |
| 265 | test_expect_success 'with hook and editor (cherry-pick)' ' |
| 266 | test_when_finished "git checkout -f master" && |
| 267 | git checkout -B other b && |
| 268 | git cherry-pick -e rebase-1 && |
| 269 | test "$(git log -1 --pretty=format:%s)" = merge |
| 270 | ' |
| 271 | |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 272 | cat > "$HOOK" <<'EOF' |
| 273 | #!/bin/sh |
| 274 | exit 1 |
| 275 | EOF |
| 276 | |
| 277 | test_expect_success 'with failing hook' ' |
| 278 | |
Benoit Pierre | 1fc4f97 | 2014-03-18 11:00:55 +0100 | [diff] [blame] | 279 | test_when_finished "git checkout -f master" && |
Elia Pinto | 0c92325 | 2016-01-08 12:06:23 +0100 | [diff] [blame] | 280 | head=$(git rev-parse HEAD) && |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 281 | echo "more" >> file && |
| 282 | git add file && |
Benoit Pierre | b7ae141 | 2014-03-10 19:49:32 +0100 | [diff] [blame] | 283 | test_must_fail env GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -c $head |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 284 | |
| 285 | ' |
| 286 | |
| 287 | test_expect_success 'with failing hook (--no-verify)' ' |
| 288 | |
Benoit Pierre | 1fc4f97 | 2014-03-18 11:00:55 +0100 | [diff] [blame] | 289 | test_when_finished "git checkout -f master" && |
Elia Pinto | 0c92325 | 2016-01-08 12:06:23 +0100 | [diff] [blame] | 290 | head=$(git rev-parse HEAD) && |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 291 | echo "more" >> file && |
| 292 | git add file && |
Benoit Pierre | b7ae141 | 2014-03-10 19:49:32 +0100 | [diff] [blame] | 293 | test_must_fail env GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify -c $head |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 294 | |
| 295 | ' |
| 296 | |
Antoine Pelisse | 3e4141d | 2013-01-02 19:42:50 +0100 | [diff] [blame] | 297 | test_expect_success 'with failing hook (merge)' ' |
| 298 | |
Benoit Pierre | 1fc4f97 | 2014-03-18 11:00:55 +0100 | [diff] [blame] | 299 | test_when_finished "git checkout -f master" && |
Antoine Pelisse | 3e4141d | 2013-01-02 19:42:50 +0100 | [diff] [blame] | 300 | git checkout -B other HEAD@{1} && |
| 301 | echo "more" >> file && |
| 302 | git add file && |
| 303 | rm -f "$HOOK" && |
| 304 | git commit -m other && |
Benoit Pierre | 3219bad | 2014-03-10 19:49:31 +0100 | [diff] [blame] | 305 | write_script "$HOOK" <<-EOF && |
Antoine Pelisse | 3e4141d | 2013-01-02 19:42:50 +0100 | [diff] [blame] | 306 | exit 1 |
| 307 | EOF |
| 308 | git checkout - && |
Benoit Pierre | 1fc4f97 | 2014-03-18 11:00:55 +0100 | [diff] [blame] | 309 | test_must_fail git merge --no-ff other |
Antoine Pelisse | 3e4141d | 2013-01-02 19:42:50 +0100 | [diff] [blame] | 310 | |
| 311 | ' |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 312 | |
Phillip Wood | 66618a5 | 2018-01-24 12:34:22 +0000 | [diff] [blame] | 313 | test_expect_success C_LOCALE_OUTPUT 'with failing hook (cherry-pick)' ' |
Phillip Wood | 15cd6d3 | 2018-01-24 12:34:21 +0000 | [diff] [blame] | 314 | test_when_finished "git checkout -f master" && |
| 315 | git checkout -B other b && |
| 316 | test_must_fail git cherry-pick rebase-1 2>actual && |
| 317 | test $(grep -c prepare-commit-msg actual) = 1 |
| 318 | ' |
| 319 | |
Paolo Bonzini | 8089c85 | 2008-02-05 08:04:18 +0100 | [diff] [blame] | 320 | test_done |