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