@@ -7,37 +7,6 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
7
8
8
. ./test-lib.sh
9
9
10
- HOOKDIR=" $( git rev-parse --git-dir) /hooks"
11
- PRECOMMIT=" $HOOKDIR /pre-commit"
12
- PREMERGE=" $HOOKDIR /pre-merge-commit"
13
-
14
- # Prepare sample scripts that write their $0 to actual_hooks
15
- test_expect_success ' sample script setup' '
16
- mkdir -p "$HOOKDIR" &&
17
- write_script "$HOOKDIR/success.sample" <<-\EOF &&
18
- echo $0 >>actual_hooks
19
- exit 0
20
- EOF
21
- write_script "$HOOKDIR/fail.sample" <<-\EOF &&
22
- echo $0 >>actual_hooks
23
- exit 1
24
- EOF
25
- write_script "$HOOKDIR/non-exec.sample" <<-\EOF &&
26
- echo $0 >>actual_hooks
27
- exit 1
28
- EOF
29
- chmod -x "$HOOKDIR/non-exec.sample" &&
30
- write_script "$HOOKDIR/require-prefix.sample" <<-\EOF &&
31
- echo $0 >>actual_hooks
32
- test $GIT_PREFIX = "success/"
33
- EOF
34
- write_script "$HOOKDIR/check-author.sample" <<-\EOF
35
- echo $0 >>actual_hooks
36
- test "$GIT_AUTHOR_NAME" = "New Author" &&
37
- test "$GIT_AUTHOR_EMAIL" = "[email protected] "
38
- EOF
39
- '
40
-
41
10
test_expect_success ' root commit' '
42
11
echo "root" >file &&
43
12
git add file &&
@@ -96,123 +65,136 @@ test_expect_success '--no-verify with no hook (merge)' '
96
65
test_path_is_missing actual_hooks
97
66
'
98
67
68
+ setup_success_hook () {
69
+ test_when_finished " rm -f actual_hooks expected_hooks" &&
70
+ echo " $1 " > expected_hooks &&
71
+ test_hook " $1 " << -EOF
72
+ echo $1 >>actual_hooks
73
+ EOF
74
+ }
75
+
99
76
test_expect_success ' with succeeding hook' '
100
- test_when_finished "rm -f \"$PRECOMMIT\" expected_hooks actual_hooks" &&
101
- cp "$HOOKDIR/success.sample" "$PRECOMMIT" &&
102
- echo "$PRECOMMIT" >expected_hooks &&
77
+ setup_success_hook "pre-commit" &&
103
78
echo "more" >>file &&
104
79
git add file &&
105
80
git commit -m "more" &&
106
81
test_cmp expected_hooks actual_hooks
107
82
'
108
83
109
84
test_expect_success ' with succeeding hook (merge)' '
110
- test_when_finished "rm -f \"$PREMERGE\" expected_hooks actual_hooks" &&
111
- cp "$HOOKDIR/success.sample" "$PREMERGE" &&
112
- echo "$PREMERGE" >expected_hooks &&
85
+ setup_success_hook "pre-merge-commit" &&
113
86
git checkout side &&
114
87
git merge -m "merge main" main &&
115
88
git checkout main &&
116
89
test_cmp expected_hooks actual_hooks
117
90
'
118
91
119
92
test_expect_success ' automatic merge fails; both hooks are available' '
120
- test_when_finished "rm -f \"$PREMERGE\" \"$PRECOMMIT\"" &&
121
- test_when_finished "rm -f expected_hooks actual_hooks" &&
122
- test_when_finished "git checkout main" &&
123
- cp "$HOOKDIR/success.sample" "$PREMERGE" &&
124
- cp "$HOOKDIR/success.sample" "$PRECOMMIT" &&
93
+ setup_success_hook "pre-commit" &&
94
+ setup_success_hook "pre-merge-commit" &&
125
95
126
96
git checkout conflicting-a &&
127
97
test_must_fail git merge -m "merge conflicting-b" conflicting-b &&
128
98
test_path_is_missing actual_hooks &&
129
99
130
- echo "$PRECOMMIT " >expected_hooks &&
100
+ echo "pre-commit " >expected_hooks &&
131
101
echo a+b >conflicting &&
132
102
git add conflicting &&
133
103
git commit -m "resolve conflict" &&
134
104
test_cmp expected_hooks actual_hooks
135
105
'
136
106
137
107
test_expect_success ' --no-verify with succeeding hook' '
138
- test_when_finished "rm -f \"$PRECOMMIT\" actual_hooks" &&
139
- cp "$HOOKDIR/success.sample" "$PRECOMMIT" &&
108
+ setup_success_hook "pre-commit" &&
140
109
echo "even more" >>file &&
141
110
git add file &&
142
111
git commit --no-verify -m "even more" &&
143
112
test_path_is_missing actual_hooks
144
113
'
145
114
146
115
test_expect_success ' --no-verify with succeeding hook (merge)' '
147
- test_when_finished "rm -f \"$PREMERGE\" actual_hooks" &&
148
- cp "$HOOKDIR/success.sample" "$PREMERGE" &&
116
+ setup_success_hook "pre-merge-commit" &&
149
117
git branch -f side side-orig &&
150
118
git checkout side &&
151
119
git merge --no-verify -m "merge main" main &&
152
120
git checkout main &&
153
121
test_path_is_missing actual_hooks
154
122
'
155
123
124
+ setup_failing_hook () {
125
+ test_when_finished " rm -f actual_hooks" &&
126
+ test_hook " $1 " << -EOF
127
+ echo $1 -failing-hook >>actual_hooks
128
+ exit 1
129
+ EOF
130
+ }
131
+
156
132
test_expect_success ' with failing hook' '
157
- test_when_finished "rm -f \"$PRECOMMIT\" expected_hooks actual_hooks" &&
158
- cp "$HOOKDIR/fail.sample" "$PRECOMMIT" &&
159
- echo "$PRECOMMIT" >expected_hooks &&
133
+ setup_failing_hook "pre-commit" &&
134
+ test_when_finished "rm -f expected_hooks" &&
135
+ echo "pre-commit-failing-hook" >expected_hooks &&
136
+
160
137
echo "another" >>file &&
161
138
git add file &&
162
139
test_must_fail git commit -m "another" &&
163
140
test_cmp expected_hooks actual_hooks
164
141
'
165
142
166
143
test_expect_success ' --no-verify with failing hook' '
167
- test_when_finished "rm -f \"$PRECOMMIT\" actual_hooks" &&
168
- cp "$HOOKDIR/fail.sample" "$PRECOMMIT" &&
144
+ setup_failing_hook "pre-commit" &&
169
145
echo "stuff" >>file &&
170
146
git add file &&
171
147
git commit --no-verify -m "stuff" &&
172
148
test_path_is_missing actual_hooks
173
149
'
174
150
175
151
test_expect_success ' with failing hook (merge)' '
176
- test_when_finished "rm -f \"$PREMERGE\" expected_hooks actual_hooks" &&
177
- cp "$HOOKDIR/fail.sample" "$PREMERGE" &&
178
- echo "$PREMERGE" >expected_hooks &&
152
+ setup_failing_hook "pre-merge-commit" &&
153
+ echo "pre-merge-commit-failing-hook" >expected_hooks &&
179
154
git checkout side &&
180
155
test_must_fail git merge -m "merge main" main &&
181
156
git checkout main &&
182
157
test_cmp expected_hooks actual_hooks
183
158
'
184
159
185
160
test_expect_success ' --no-verify with failing hook (merge)' '
186
- test_when_finished "rm -f \"$PREMERGE\" actual_hooks " &&
187
- cp "$HOOKDIR/fail.sample" "$PREMERGE" &&
161
+ setup_failing_hook "pre-merge-commit " &&
162
+
188
163
git branch -f side side-orig &&
189
164
git checkout side &&
190
165
git merge --no-verify -m "merge main" main &&
191
166
git checkout main &&
192
167
test_path_is_missing actual_hooks
193
168
'
194
169
170
+ setup_non_exec_hook () {
171
+ test_when_finished " rm -f actual_hooks" &&
172
+ test_hook " $1 " << -\EOF &&
173
+ echo non-exec >>actual_hooks
174
+ exit 1
175
+ EOF
176
+ test_hook --disable " $1 "
177
+ }
178
+
179
+
195
180
test_expect_success POSIXPERM ' with non-executable hook' '
196
- test_when_finished "rm -f \"$PRECOMMIT\" actual_hooks" &&
197
- cp "$HOOKDIR/non-exec.sample" "$PRECOMMIT" &&
181
+ setup_non_exec_hook "pre-commit" &&
198
182
echo "content" >>file &&
199
183
git add file &&
200
184
git commit -m "content" &&
201
185
test_path_is_missing actual_hooks
202
186
'
203
187
204
188
test_expect_success POSIXPERM ' --no-verify with non-executable hook' '
205
- test_when_finished "rm -f \"$PRECOMMIT\" actual_hooks" &&
206
- cp "$HOOKDIR/non-exec.sample" "$PRECOMMIT" &&
189
+ setup_non_exec_hook "pre-commit" &&
207
190
echo "more content" >>file &&
208
191
git add file &&
209
192
git commit --no-verify -m "more content" &&
210
193
test_path_is_missing actual_hooks
211
194
'
212
195
213
196
test_expect_success POSIXPERM ' with non-executable hook (merge)' '
214
- test_when_finished "rm -f \"$PREMERGE\" actual_hooks" &&
215
- cp "$HOOKDIR/non-exec.sample" "$PREMERGE" &&
197
+ setup_non_exec_hook "pre-merge" &&
216
198
git branch -f side side-orig &&
217
199
git checkout side &&
218
200
git merge -m "merge main" main &&
@@ -221,19 +203,26 @@ test_expect_success POSIXPERM 'with non-executable hook (merge)' '
221
203
'
222
204
223
205
test_expect_success POSIXPERM ' --no-verify with non-executable hook (merge)' '
224
- test_when_finished "rm -f \"$PREMERGE\" actual_hooks" &&
225
- cp "$HOOKDIR/non-exec.sample" "$PREMERGE" &&
206
+ setup_non_exec_hook "pre-merge" &&
226
207
git branch -f side side-orig &&
227
208
git checkout side &&
228
209
git merge --no-verify -m "merge main" main &&
229
210
git checkout main &&
230
211
test_path_is_missing actual_hooks
231
212
'
232
213
214
+ setup_require_prefix_hook () {
215
+ test_when_finished " rm -f expected_hooks" &&
216
+ echo require-prefix > expected_hooks &&
217
+ test_hook pre-commit << -\EOF
218
+ echo require-prefix >>actual_hooks
219
+ test $GIT_PREFIX = "success/"
220
+ EOF
221
+ }
222
+
233
223
test_expect_success ' with hook requiring GIT_PREFIX' '
234
- test_when_finished "rm -rf \"$PRECOMMIT\" expected_hooks actual_hooks success" &&
235
- cp "$HOOKDIR/require-prefix.sample" "$PRECOMMIT" &&
236
- echo "$PRECOMMIT" >expected_hooks &&
224
+ test_when_finished "rm -rf actual_hooks success" &&
225
+ setup_require_prefix_hook &&
237
226
echo "more content" >>file &&
238
227
git add file &&
239
228
mkdir success &&
@@ -245,9 +234,8 @@ test_expect_success 'with hook requiring GIT_PREFIX' '
245
234
'
246
235
247
236
test_expect_success ' with failing hook requiring GIT_PREFIX' '
248
- test_when_finished "rm -rf \"$PRECOMMIT\" expected_hooks actual_hooks fail" &&
249
- cp "$HOOKDIR/require-prefix.sample" "$PRECOMMIT" &&
250
- echo "$PRECOMMIT" >expected_hooks &&
237
+ test_when_finished "rm -rf actual_hooks fail" &&
238
+ setup_require_prefix_hook &&
251
239
echo "more content" >>file &&
252
240
git add file &&
253
241
mkdir fail &&
@@ -259,13 +247,23 @@ test_expect_success 'with failing hook requiring GIT_PREFIX' '
259
247
test_cmp expected_hooks actual_hooks
260
248
'
261
249
250
+ setup_require_author_hook () {
251
+ test_when_finished " rm -f expected_hooks actual_hooks" &&
252
+ echo check-author > expected_hooks &&
253
+ test_hook pre-commit << -\EOF
254
+ echo check-author >>actual_hooks
255
+ test "$GIT_AUTHOR_NAME " = "New Author" &&
256
+ test "$GIT_AUTHOR_EMAIL " = "[email protected] "
257
+ EOF
258
+ }
259
+
260
+
262
261
test_expect_success ' check the author in hook' '
263
- test_when_finished "rm -f \"$PRECOMMIT\" expected_hooks actual_hooks" &&
264
- cp "$HOOKDIR/check-author.sample" "$PRECOMMIT" &&
262
+ setup_require_author_hook &&
265
263
cat >expected_hooks <<-EOF &&
266
- $PRECOMMIT
267
- $PRECOMMIT
268
- $PRECOMMIT
264
+ check-author
265
+ check-author
266
+ check-author
269
267
EOF
270
268
test_must_fail git commit --allow-empty -m "by a.u.thor" &&
271
269
(
0 commit comments