]> git.ipfire.org Git - thirdparty/git.git/blame - t/t7505-prepare-commit-msg-hook.sh
git-commit: add a prepare-commit-msg hook
[thirdparty/git.git] / t / t7505-prepare-commit-msg-hook.sh
CommitLineData
8089c85b
PB
1#!/bin/sh
2
3test_description='prepare-commit-msg hook'
4
5. ./test-lib.sh
6
7test_expect_success 'with no hook' '
8
9 echo "foo" > file &&
10 git add file &&
11 git commit -m "first"
12
13'
14
15# set up fake editor for interactive editing
16cat > fake-editor <<'EOF'
17#!/bin/sh
18exit 0
19EOF
20chmod +x fake-editor
21FAKE_EDITOR="$(pwd)/fake-editor"
22export FAKE_EDITOR
23
24# now install hook that always succeeds and adds a message
25HOOKDIR="$(git rev-parse --git-dir)/hooks"
26HOOK="$HOOKDIR/prepare-commit-msg"
27mkdir -p "$HOOKDIR"
28cat > "$HOOK" <<'EOF'
29#!/bin/sh
30if test "$2" = commit; then
31 source=$(git-rev-parse "$3")
32else
33 source=${2-default}
34fi
35if test "$GIT_EDITOR" = :; then
36 sed -e "1s/.*/$source (no editor)/" "$1" > msg.tmp
37else
38 sed -e "1s/.*/$source/" "$1" > msg.tmp
39fi
40mv msg.tmp "$1"
41exit 0
42EOF
43chmod +x "$HOOK"
44
45echo dummy template > "$(git rev-parse --git-dir)/template"
46
47test_expect_success 'with hook (-m)' '
48
49 echo "more" >> file &&
50 git add file &&
51 git commit -m "more" &&
52 test "`git log -1 --pretty=format:%s`" = "message (no editor)"
53
54'
55
56test_expect_success 'with hook (-m editor)' '
57
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
62
63'
64
65test_expect_success 'with hook (-t)' '
66
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
71
72'
73
74test_expect_success 'with hook (-F)' '
75
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)"
80
81'
82
83test_expect_success 'with hook (-F editor)' '
84
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
89
90'
91
92test_expect_success 'with hook (-C)' '
93
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)"
99
100'
101
102test_expect_success 'with hook (editor)' '
103
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
108
109'
110
111test_expect_success 'with hook (--amend)' '
112
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"
118
119'
120
121test_expect_success 'with hook (-c)' '
122
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"
128
129'
130
131cat > "$HOOK" <<'EOF'
132#!/bin/sh
133exit 1
134EOF
135
136test_expect_success 'with failing hook' '
137
138 head=`git rev-parse HEAD` &&
139 echo "more" >> file &&
140 git add file &&
141 ! GIT_EDITOR="$FAKE_EDITOR" git commit -c $head
142
143'
144
145test_expect_success 'with failing hook (--no-verify)' '
146
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
151
152'
153
154
155test_done