Merge branch 'maint'
[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 FAKE_EDITOR="$(pwd)/fake-editor"
22 export FAKE_EDITOR
23
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}
34 fi
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
39 fi
40 mv msg.tmp "$1"
41 exit 0
42 EOF
43 chmod +x "$HOOK"
44
45 echo dummy template > "$(git rev-parse --git-dir)/template"
46
47 test_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
56 test_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
65 test_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
74 test_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
83 test_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
92 test_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
102 test_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
111 test_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
121 test_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
131 cat > "$HOOK" <<'EOF'
132 #!/bin/sh
133 exit 1
134 EOF
135
136 test_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
145 test_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
155 test_done