]> git.ipfire.org Git - thirdparty/git.git/blob - t/t7505-prepare-commit-msg-hook.sh
fix "git apply --index ..." not to deref NULL
[thirdparty/git.git] / t / t7505-prepare-commit-msg-hook.sh
1 #!/bin/sh
2
3 test_description='prepare-commit-msg hook'
4
5 . ./test-lib.sh
6
7 test_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
16 cat > fake-editor <<'EOF'
17 #!/bin/sh
18 exit 0
19 EOF
20 chmod +x fake-editor
21
22 ## Not using test_set_editor here so we can easily ensure the editor variable
23 ## is only set for the editor tests
24 FAKE_EDITOR="$(pwd)/fake-editor"
25 export FAKE_EDITOR
26
27 # now install hook that always succeeds and adds a message
28 HOOKDIR="$(git rev-parse --git-dir)/hooks"
29 HOOK="$HOOKDIR/prepare-commit-msg"
30 mkdir -p "$HOOKDIR"
31 echo "#!$SHELL_PATH" > "$HOOK"
32 cat >> "$HOOK" <<'EOF'
33
34 if test "$2" = commit; then
35 source=$(git rev-parse "$3")
36 else
37 source=${2-default}
38 fi
39 if test "$GIT_EDITOR" = :; then
40 sed -e "1s/.*/$source (no editor)/" "$1" > msg.tmp
41 else
42 sed -e "1s/.*/$source/" "$1" > msg.tmp
43 fi
44 mv msg.tmp "$1"
45 exit 0
46 EOF
47 chmod +x "$HOOK"
48
49 echo dummy template > "$(git rev-parse --git-dir)/template"
50
51 test_expect_success 'with hook (-m)' '
52
53 echo "more" >> file &&
54 git add file &&
55 git commit -m "more" &&
56 test "`git log -1 --pretty=format:%s`" = "message (no editor)"
57
58 '
59
60 test_expect_success 'with hook (-m editor)' '
61
62 echo "more" >> file &&
63 git add file &&
64 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -e -m "more more" &&
65 test "`git log -1 --pretty=format:%s`" = message
66
67 '
68
69 test_expect_success 'with hook (-t)' '
70
71 echo "more" >> file &&
72 git add file &&
73 git commit -t "$(git rev-parse --git-dir)/template" &&
74 test "`git log -1 --pretty=format:%s`" = template
75
76 '
77
78 test_expect_success 'with hook (-F)' '
79
80 echo "more" >> file &&
81 git add file &&
82 (echo more | git commit -F -) &&
83 test "`git log -1 --pretty=format:%s`" = "message (no editor)"
84
85 '
86
87 test_expect_success 'with hook (-F editor)' '
88
89 echo "more" >> file &&
90 git add file &&
91 (echo more more | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -e -F -) &&
92 test "`git log -1 --pretty=format:%s`" = message
93
94 '
95
96 test_expect_success 'with hook (-C)' '
97
98 head=`git rev-parse HEAD` &&
99 echo "more" >> file &&
100 git add file &&
101 git commit -C $head &&
102 test "`git log -1 --pretty=format:%s`" = "$head (no editor)"
103
104 '
105
106 test_expect_success 'with hook (editor)' '
107
108 echo "more more" >> file &&
109 git add file &&
110 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit &&
111 test "`git log -1 --pretty=format:%s`" = default
112
113 '
114
115 test_expect_success 'with hook (--amend)' '
116
117 head=`git rev-parse HEAD` &&
118 echo "more" >> file &&
119 git add file &&
120 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --amend &&
121 test "`git log -1 --pretty=format:%s`" = "$head"
122
123 '
124
125 test_expect_success 'with hook (-c)' '
126
127 head=`git rev-parse HEAD` &&
128 echo "more" >> file &&
129 git add file &&
130 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -c $head &&
131 test "`git log -1 --pretty=format:%s`" = "$head"
132
133 '
134
135 cat > "$HOOK" <<'EOF'
136 #!/bin/sh
137 exit 1
138 EOF
139
140 test_expect_success 'with failing hook' '
141
142 head=`git rev-parse HEAD` &&
143 echo "more" >> file &&
144 git add file &&
145 ! GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -c $head
146
147 '
148
149 test_expect_success 'with failing hook (--no-verify)' '
150
151 head=`git rev-parse HEAD` &&
152 echo "more" >> file &&
153 git add file &&
154 ! GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify -c $head
155
156 '
157
158
159 test_done