Merge branch 'dc/format-pretty'
[git/spearce.git] / t / t7505-prepare-commit-msg-hook.sh
blob7ddec99a64273b056ff0f3e1fe1694315045f583
1 #!/bin/sh
3 test_description='prepare-commit-msg hook'
5 . ./test-lib.sh
7 test_expect_success 'with no hook' '
9 echo "foo" > file &&
10 git add file &&
11 git commit -m "first"
15 # set up fake editor for interactive editing
16 cat > fake-editor <<'EOF'
17 #!/bin/sh
18 exit 0
19 EOF
20 chmod +x fake-editor
21 FAKE_EDITOR="$(pwd)/fake-editor"
22 export FAKE_EDITOR
24 # now install hook that always succeeds and adds a message
25 HOOKDIR="$(git rev-parse --git-dir)/hooks"
26 HOOK="$HOOKDIR/prepare-commit-msg"
27 mkdir -p "$HOOKDIR"
28 cat > "$HOOK" <<'EOF'
29 #!/bin/sh
30 if test "$2" = commit; then
31 source=$(git-rev-parse "$3")
32 else
33 source=${2-default}
35 if test "$GIT_EDITOR" = :; then
36 sed -e "1s/.*/$source (no editor)/" "$1" > msg.tmp
37 else
38 sed -e "1s/.*/$source/" "$1" > msg.tmp
40 mv msg.tmp "$1"
41 exit 0
42 EOF
43 chmod +x "$HOOK"
45 echo dummy template > "$(git rev-parse --git-dir)/template"
47 test_expect_success 'with hook (-m)' '
49 echo "more" >> file &&
50 git add file &&
51 git commit -m "more" &&
52 test "`git log -1 --pretty=format:%s`" = "message (no editor)"
56 test_expect_success 'with hook (-m editor)' '
58 echo "more" >> file &&
59 git add file &&
60 GIT_EDITOR="$FAKE_EDITOR" git commit -e -m "more more" &&
61 test "`git log -1 --pretty=format:%s`" = message
65 test_expect_success 'with hook (-t)' '
67 echo "more" >> file &&
68 git add file &&
69 git commit -t "$(git rev-parse --git-dir)/template" &&
70 test "`git log -1 --pretty=format:%s`" = template
74 test_expect_success 'with hook (-F)' '
76 echo "more" >> file &&
77 git add file &&
78 (echo more | git commit -F -) &&
79 test "`git log -1 --pretty=format:%s`" = "message (no editor)"
83 test_expect_success 'with hook (-F editor)' '
85 echo "more" >> file &&
86 git add file &&
87 (echo more more | GIT_EDITOR="$FAKE_EDITOR" git commit -e -F -) &&
88 test "`git log -1 --pretty=format:%s`" = message
92 test_expect_success 'with hook (-C)' '
94 head=`git rev-parse HEAD` &&
95 echo "more" >> file &&
96 git add file &&
97 git commit -C $head &&
98 test "`git log -1 --pretty=format:%s`" = "$head (no editor)"
102 test_expect_success 'with hook (editor)' '
104 echo "more more" >> file &&
105 git add file &&
106 GIT_EDITOR="$FAKE_EDITOR" git commit &&
107 test "`git log -1 --pretty=format:%s`" = default
111 test_expect_success 'with hook (--amend)' '
113 head=`git rev-parse HEAD` &&
114 echo "more" >> file &&
115 git add file &&
116 GIT_EDITOR="$FAKE_EDITOR" git commit --amend &&
117 test "`git log -1 --pretty=format:%s`" = "$head"
121 test_expect_success 'with hook (-c)' '
123 head=`git rev-parse HEAD` &&
124 echo "more" >> file &&
125 git add file &&
126 GIT_EDITOR="$FAKE_EDITOR" git commit -c $head &&
127 test "`git log -1 --pretty=format:%s`" = "$head"
131 cat > "$HOOK" <<'EOF'
132 #!/bin/sh
133 exit 1
136 test_expect_success 'with failing hook' '
138 head=`git rev-parse HEAD` &&
139 echo "more" >> file &&
140 git add file &&
141 ! GIT_EDITOR="$FAKE_EDITOR" git commit -c $head
145 test_expect_success 'with failing hook (--no-verify)' '
147 head=`git rev-parse HEAD` &&
148 echo "more" >> file &&
149 git add file &&
150 ! GIT_EDITOR="$FAKE_EDITOR" git commit --no-verify -c $head
155 test_done