1
#!/bin/sh
2
3
-test_description='pre-commit hook'
3
+test_description='pre-commit and pre-merge-commit hooks'
4
5
. ./test-lib.sh
6
7
HOOKDIR="$(git rev-parse --git-dir)/hooks"
8
PRECOMMIT="$HOOKDIR/pre-commit"
9
+PREMERGE="$HOOKDIR/pre-merge-commit"
10
11
# Prepare sample scripts that write their $0 to actual_hooks
12
test_expect_success 'sample script setup' '
35
EOF
36
'
37
38
+test_expect_success 'root commit' '
39
+ echo "root" >file &&
40
+ git add file &&
41
+ git commit -m "zeroth" &&
42
+ git checkout -b side &&
43
+ echo "foo" >foo &&
44
+ git add foo &&
45
+ git commit -m "make it non-ff" &&
46
+ git branch side-orig side &&
47
+ git checkout master
48
+'
49
+
50
+test_expect_success 'setup conflicting branches' '
51
+ test_when_finished "git checkout master" &&
52
+ git checkout -b conflicting-a master &&
53
+ echo a >conflicting &&
54
+ git add conflicting &&
55
+ git commit -m conflicting-a &&
56
+ git checkout -b conflicting-b master &&
57
+ echo b >conflicting &&
58
+ git add conflicting &&
59
+ git commit -m conflicting-b
60
+'
61
+
62
test_expect_success 'with no hook' '
63
test_when_finished "rm -f actual_hooks" &&
64
echo "foo" >file &&
67
test_path_is_missing actual_hooks
68
'
69
70
+test_expect_success 'with no hook (merge)' '
71
+ test_when_finished "rm -f actual_hooks" &&
72
+ git branch -f side side-orig &&
73
+ git checkout side &&
74
+ git merge -m "merge master" master &&
75
+ git checkout master &&
76
+ test_path_is_missing actual_hooks
77
+'
78
+
79
test_expect_success '--no-verify with no hook' '
80
test_when_finished "rm -f actual_hooks" &&
81
echo "bar" >file &&
94
test_cmp expected_hooks actual_hooks
95
'
96
97
+test_expect_success 'with succeeding hook (merge)' '
98
+ test_when_finished "rm -f \"$PREMERGE\" expected_hooks actual_hooks" &&
99
+ cp "$HOOKDIR/success.sample" "$PREMERGE" &&
100
+ echo "$PREMERGE" >expected_hooks &&
101
+ git checkout side &&
102
+ git merge -m "merge master" master &&
103
+ git checkout master &&
104
+ test_cmp expected_hooks actual_hooks
105
+'
106
+
107
+test_expect_success 'automatic merge fails; both hooks are available' '
108
+ test_when_finished "rm -f \"$PREMERGE\" \"$PRECOMMIT\"" &&
109
+ test_when_finished "rm -f expected_hooks actual_hooks" &&
110
+ test_when_finished "git checkout master" &&
111
+ cp "$HOOKDIR/success.sample" "$PREMERGE" &&
112
+ cp "$HOOKDIR/success.sample" "$PRECOMMIT" &&
113
+
114
+ git checkout conflicting-a &&
115
+ test_must_fail git merge -m "merge conflicting-b" conflicting-b &&
116
+ test_path_is_missing actual_hooks &&
117
+
118
+ echo "$PRECOMMIT" >expected_hooks &&
119
+ echo a+b >conflicting &&
120
+ git add conflicting &&
121
+ git commit -m "resolve conflict" &&
122
+ test_cmp expected_hooks actual_hooks
123
+'
124
+
125
test_expect_success '--no-verify with succeeding hook' '
126
test_when_finished "rm -f \"$PRECOMMIT\" actual_hooks" &&
127
cp "$HOOKDIR/success.sample" "$PRECOMMIT" &&
150
test_path_is_missing actual_hooks
151
'
152
153
+test_expect_success 'with failing hook (merge)' '
154
+ test_when_finished "rm -f \"$PREMERGE\" expected_hooks actual_hooks" &&
155
+ cp "$HOOKDIR/fail.sample" "$PREMERGE" &&
156
+ echo "$PREMERGE" >expected_hooks &&
157
+ git checkout side &&
158
+ test_must_fail git merge -m "merge master" master &&
159
+ git checkout master &&
160
+ test_cmp expected_hooks actual_hooks
161
+'
162
+
163
test_expect_success POSIXPERM 'with non-executable hook' '
164
test_when_finished "rm -f \"$PRECOMMIT\" actual_hooks" &&
165
cp "$HOOKDIR/non-exec.sample" "$PRECOMMIT" &&
178
test_path_is_missing actual_hooks
179
'
180
181
+test_expect_success POSIXPERM 'with non-executable hook (merge)' '
182
+ test_when_finished "rm -f \"$PREMERGE\" actual_hooks" &&
183
+ cp "$HOOKDIR/non-exec.sample" "$PREMERGE" &&
184
+ git branch -f side side-orig &&
185
+ git checkout side &&
186
+ git merge -m "merge master" master &&
187
+ git checkout master &&
188
+ test_path_is_missing actual_hooks
189
+'
190
+
191
test_expect_success 'with hook requiring GIT_PREFIX' '
192
test_when_finished "rm -rf \"$PRECOMMIT\" expected_hooks actual_hooks success" &&
193
cp "$HOOKDIR/require-prefix.sample" "$PRECOMMIT" &&