1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
|
test_description='Check the shipped post-receive hooks'
CGIT_TEST_NO_CREATE_REPOS=YesPlease
. ./setup.sh
HOOK_DIRECTORY="$TEST_OUTPUT_DIRECTORY/../custom/hooks"
cached_requests() {
test -d "$1" || return 1
for slot in "$1"/*
do
case $slot in
*.lock | */rc-*)
continue
;;
esac
dd if="$slot" bs=4096 count=1 2>/dev/null >slot-head &&
tr "\0" "\n" <slot-head | sed -n 1p || return 1
done
}
fresh_slots() {
rm -rf slots && cp -R pristine slots
}
test_expect_success 'set up a cache of three repositories and the index' '
test_create_repo pusher &&
test_commit -C pusher cached &&
mkdir scan slots &&
git init -q --bare scan/a.git &&
git init -q --bare scan/b.git &&
git init -q --bare scan/foo+bar.git &&
git -C pusher push -q ../scan/a.git HEAD:refs/heads/main &&
git -C pusher push -q ../scan/b.git HEAD:refs/heads/main &&
git -C pusher push -q ../scan/foo+bar.git HEAD:refs/heads/main &&
{
echo "cache-root=$PWD/slots" &&
echo "cache-size=1000" &&
echo "virtual-root=/" &&
echo "scan-path=$PWD/scan"
} >cachedrc &&
for page in / /a.git/ /a.git/log/ /b.git/ /b.git/log/
do
CGIT_CONFIG="$PWD/cachedrc" PATH_INFO="$page" QUERY_STRING= cgit >/dev/null ||
return 1
done &&
cgit_rc cachedrc "url=a.git/refs/" >/dev/null &&
cgit_rc cachedrc "url=b.git/refs/" >/dev/null &&
cgit_rc cachedrc "r=a.git&p=log" >/dev/null &&
cgit_rc cachedrc "r=b.git&p=log" >/dev/null &&
CGIT_CONFIG="$PWD/cachedrc" PATH_INFO=/foo+bar.git/log/ QUERY_STRING= cgit >/dev/null &&
cgit_rc cachedrc "url=foo%2bbar.git/refs/" >/dev/null &&
cgit_rc cachedrc "r=foo%2Bbar.git&p=log" >/dev/null &&
echo writer >slots/0123abcd.lock &&
printf "not a key\0page" >slots/00000000 &&
cp -R slots pristine &&
sort >expect-keys <<-\EOF &&
7|http://0|1|/0|
7|http://0|7|/a.git/0|
7|http://0|11|/a.git/log/0|
7|http://0|7|/b.git/0|
7|http://0|11|/b.git/log/0|
7|http://0|0|15|url=a.git/refs/
7|http://0|0|15|url=b.git/refs/
7|http://0|0|13|r=a.git&p=log
7|http://0|0|13|r=b.git&p=log
7|http://0|17|/foo+bar.git/log/0|
7|http://0|0|23|url=foo%2bbar.git/refs/
7|http://0|0|21|r=foo%2Bbar.git&p=log
not a key
EOF
cached_requests pristine >keys &&
sort keys >sorted-keys &&
test_cmp expect-keys sorted-keys &&
ls pristine >pristine-slots &&
grep "^rc-" pristine-slots &&
sed -e "s|^cache_root=.*|cache_root='\''$PWD/slots'\''|" \
-e "s|^scan_path=.*|scan_path='\''$PWD/scan'\''|" \
"$HOOK_DIRECTORY/post-receive.cgit-cache" >cache-hook &&
grep -Fx "cache_root='\''$PWD/slots'\''" cache-hook &&
grep -Fx "scan_path='\''$PWD/scan'\''" cache-hook
'
test_expect_success 'set up a cache of repositories served without the suffix' '
mkdir suffix-slots &&
{
echo "cache-root=$PWD/suffix-slots" &&
echo "cache-size=1000" &&
echo "virtual-root=/" &&
echo "remove-suffix=1" &&
echo "scan-path=$PWD/scan"
} >suffixrc &&
for page in / /a/ /a/log/ /b/ /b/log/
do
CGIT_CONFIG="$PWD/suffixrc" PATH_INFO="$page" QUERY_STRING= cgit >/dev/null ||
return 1
done &&
cp -R suffix-slots suffix-pristine &&
cached_requests suffix-pristine >keys &&
test_grep "|/a/log/0|" keys &&
test_grep "|/b/log/0|" keys &&
test_grep "|1|/0|" keys &&
sed -e "s|^cache_root=.*|cache_root='\''$PWD/suffix-slots'\''|" \
-e "s|^remove_suffix=.*|remove_suffix=1|" cache-hook >suffix-hook &&
grep -Fx "cache_root='\''$PWD/suffix-slots'\''" suffix-hook &&
grep -Fx "remove_suffix=1" suffix-hook
'
test_expect_success 'set up a cache of a scanned repository with a work tree' '
git clone -q pusher scan/c &&
mkdir worktree-slots &&
sed "s|^cache-root=.*|cache-root=$PWD/worktree-slots|" cachedrc >worktreerc &&
for page in / /a.git/ /c/ /c/log/
do
CGIT_CONFIG="$PWD/worktreerc" PATH_INFO="$page" QUERY_STRING= cgit >/dev/null ||
return 1
done &&
cp -R worktree-slots worktree-pristine &&
cached_requests worktree-pristine >keys &&
test_grep "|/c/0|" keys &&
test_grep "|/c/log/0|" keys &&
test_grep "|/a.git/0|" keys &&
sed "s|^cache_root=.*|cache_root='\''$PWD/worktree-slots'\''|" cache-hook >worktree-hook &&
grep -Fx "cache_root='\''$PWD/worktree-slots'\''" worktree-hook
'
shells=
shell_paths=
for shell in sh dash ksh mksh yash posh bash zsh busybox
do
shell_path=$(command -v $shell) || continue
for seen in $shell_paths
do
test "$seen" -ef "$shell_path" && continue 2
done
shell_paths="$shell_paths $shell_path"
shells="$shells $shell"
done
say "running the hooks under$shells"
for shell in $shells
do
case $shell in
zsh)
run="zsh --emulate sh"
;;
busybox)
run="busybox sh"
;;
*)
run=$shell
;;
esac
test_expect_success "a push drops its own pages and the index under $shell" '
fresh_slots &&
(cd scan/a.git && $run ../../cache-hook 2>../../err) &&
test_must_be_empty err &&
cached_requests slots >left &&
test_grep "|/b.git/log/" left &&
test_grep "url=b.git/refs/" left &&
test_grep "r=b.git&p=log" left &&
test_grep ! "a.git" left &&
test_grep ! "1|/0|" left &&
test_grep ! "not a key" left &&
test_path_is_file slots/0123abcd.lock &&
ls slots >left-slots &&
test_grep ! "^rc-" left-slots
'
test_expect_success "a push drops the pages of a repository served without the suffix under $shell" '
rm -rf suffix-slots && cp -R suffix-pristine suffix-slots &&
(cd scan/a.git && $run ../../suffix-hook 2>../../err) &&
test_must_be_empty err &&
cached_requests suffix-slots >left &&
test_grep "|/b/log/0|" left &&
test_grep ! "|/a/" left &&
test_grep ! "|1|/0|" left
'
test_expect_success "a push to a repository with a work tree drops its pages under $shell" '
rm -rf worktree-slots && cp -R worktree-pristine worktree-slots &&
(cd scan/c/.git && $run ../../../worktree-hook 2>../../../err) &&
test_must_be_empty err &&
cached_requests worktree-slots >left &&
test_grep "|/a.git/0|" left &&
test_grep ! "|/c/" left
'
test_expect_success "a push drops the pages of a repository whose url is escaped under $shell" '
fresh_slots &&
(cd scan/foo+bar.git && $run ../../cache-hook 2>../../err) &&
test_must_be_empty err &&
cached_requests slots >left &&
test_grep "|/b.git/log/" left &&
test_grep "url=b.git/refs/" left &&
test_grep ! "foo" left
'
test_expect_success "a repository named in cgitrc gives the hook its url under $shell" '
fresh_slots &&
git -C scan/b.git config hooks.cgiturl b.git &&
sed "s|^scan_path=.*|scan_path=/nonexistent|" cache-hook >named-hook &&
grep -Fx "scan_path=/nonexistent" named-hook &&
(cd scan/b.git && $run ../../named-hook 2>../../err) &&
test_must_be_empty err &&
cached_requests slots >left &&
test_grep "|/a.git/log/" left &&
test_grep ! "b.git" left
'
test_expect_success "with no url to go on the whole cache goes under $shell" '
fresh_slots &&
(cd scan/a.git && $run ../../named-hook 2>../../err) &&
test_must_be_empty err &&
test "$(ls slots)" = 0123abcd.lock
'
test_expect_success "the cache hook does nothing without a cache root under $shell" '
sed "s|^cache_root=.*|cache_root='\''$PWD/none'\''|" cache-hook >rootless-hook &&
grep -Fx "cache_root='\''$PWD/none'\''" rootless-hook &&
(cd scan/a.git && $run ../../rootless-hook 2>../../err) &&
test_must_be_empty err &&
test_path_is_missing none
'
test_expect_success SANITY "the cache hook shows a pusher no error under $shell" '
fresh_slots &&
test_when_finished "chmod 755 slots" &&
chmod 555 slots &&
(cd scan/a.git && $run ../../cache-hook 2>../../err) &&
test_must_be_empty err &&
ls slots >left &&
test_cmp pristine-slots left
'
done
test_expect_success 'set up a repository whose dates disagree' '
test_create_repo work &&
(
cd work &&
echo one >file &&
git add file &&
GIT_AUTHOR_DATE="2001-01-01 00:00:00 +0000" \
GIT_COMMITTER_DATE="2021-02-03 04:05:06 +0000" git commit -q -m newest &&
git branch -M main &&
git checkout -q -b old &&
echo two >file &&
GIT_AUTHOR_DATE="2029-01-01 00:00:00 +0000" \
GIT_COMMITTER_DATE="2019-01-01 00:00:00 +0000" git commit -q -a -m older &&
echo three >file &&
GIT_COMMITTER_DATE="2025-01-01 00:00:00 +0000" git commit -q -a -m loose &&
git tag loose &&
git reset -q --hard HEAD^ &&
GIT_COMMITTER_DATE="2027-01-01 00:00:00 +0000" git tag -a -m release release main
) &&
git init -q --bare plain.git &&
git -C work push -q ../plain.git main old loose release &&
echo "2021-02-03 04:05:06 +0000" >expect &&
echo last-modified >expect-entries &&
echo kept >kept
'
for shell in $shells
do
test_expect_success "the age hook writes the newest branch commit date under $shell" '
rm -f plain.git/info/web/last-modified &&
(cd plain.git && $run "$HOOK_DIRECTORY/post-receive.cgit-age" 2>../err) &&
test_must_be_empty err &&
test_cmp expect plain.git/info/web/last-modified &&
ls plain.git/info/web >entries &&
test_cmp expect-entries entries
'
test_expect_success "cgit shows the date the age hook wrote as the idle time under $shell" '
cgitrc_repo agerc plain "repo.path=$PWD/plain.git" &&
cgit_ok agerc "" >index &&
test_grep "<td class=.col-idle.><time [^>]*data-ut=.1612325106." index
'
test_expect_success SANITY "the age hook shows a pusher no error under $shell" '
cp kept plain.git/info/web/last-modified &&
test_when_finished "chmod 755 plain.git/info/web" &&
chmod 555 plain.git/info/web &&
(cd plain.git && $run "$HOOK_DIRECTORY/post-receive.cgit-age" 2>../err) &&
test_must_be_empty err &&
test_cmp kept plain.git/info/web/last-modified &&
ls plain.git/info/web >entries &&
test_cmp expect-entries entries
'
test_expect_success "the age hook replaces a file it cannot write in a directory it can under $shell" '
rm -f plain.git/info/web/last-modified &&
cp kept plain.git/info/web/last-modified &&
chmod 444 plain.git/info/web/last-modified &&
(cd plain.git && $run "$HOOK_DIRECTORY/post-receive.cgit-age" 2>../err) &&
test_must_be_empty err &&
test_cmp expect plain.git/info/web/last-modified
'
test_expect_success "the age hook leaves a repository with no branches as it was under $shell" '
git init -q --bare tags-$shell.git &&
git -C work push -q ../tags-$shell.git release &&
mkdir -p tags-$shell.git/info/web &&
echo kept >tags-$shell.git/info/web/last-modified &&
(cd tags-$shell.git && $run "$HOOK_DIRECTORY/post-receive.cgit-age" 2>../err) &&
test_must_be_empty err &&
test_cmp kept tags-$shell.git/info/web/last-modified
'
test_expect_success SANITY "the age hook shows no error when it cannot make its directory under $shell" '
git init -q --bare closed-$shell.git &&
git -C work push -q ../closed-$shell.git main &&
test_when_finished "chmod 755 closed-$shell.git/info" &&
chmod 555 closed-$shell.git/info &&
(cd closed-$shell.git && $run "$HOOK_DIRECTORY/post-receive.cgit-age" 2>../err) &&
test_must_be_empty err &&
test_path_is_missing closed-$shell.git/info/web
'
test_expect_success SANITY "the age hook keeps a shared directory group writable under $shell" '
git init -q --bare --shared=group shared-$shell.git &&
git -C work push -q ../shared-$shell.git main &&
(cd shared-$shell.git && umask 022 && $run "$HOOK_DIRECTORY/post-receive.cgit-age" 2>../err) &&
test_must_be_empty err &&
ls -ld shared-$shell.git/info/web >mode &&
test_grep "^drwxrw[xs]" mode
'
test_expect_success "the age hook writes a file anyone can read under a tight umask under $shell" '
git init -q --bare private-$shell.git &&
git -C work push -q ../private-$shell.git main &&
(cd private-$shell.git && umask 077 && $run "$HOOK_DIRECTORY/post-receive.cgit-age" 2>../err) &&
test_must_be_empty err &&
ls -ld private-$shell.git/info/web private-$shell.git/info/web/last-modified >mode &&
test_grep "^drwxr-xr-x" mode &&
test_grep "^-rw-r--r--" mode
'
test_expect_success "the age hook reads core.sharedRepository as git does under $shell" '
git -C private-$shell.git config core.sharedRepository FALSE &&
rm private-$shell.git/info/web/last-modified &&
(cd private-$shell.git && umask 077 && $run "$HOOK_DIRECTORY/post-receive.cgit-age" 2>../err) &&
test_must_be_empty err &&
ls -l private-$shell.git/info/web/last-modified >mode &&
test_grep "^-rw-r--r--" mode &&
git -C private-$shell.git config core.sharedRepository 0660 &&
rm private-$shell.git/info/web/last-modified &&
(cd private-$shell.git && umask 077 && $run "$HOOK_DIRECTORY/post-receive.cgit-age" 2>../err) &&
test_must_be_empty err &&
ls -l private-$shell.git/info/web/last-modified >mode &&
test_grep "^-rw-rw-r--" mode
'
test_expect_success "the commit-graph hook writes a graph with path filters under $shell" '
git init -q --bare graph-$shell.git &&
git -C work push -q ../graph-$shell.git main &&
(cd graph-$shell.git && $run "$HOOK_DIRECTORY/post-receive.commit-graph" 2>../err) &&
test_must_be_empty err &&
test_path_is_file graph-$shell.git/objects/info/commit-graphs/commit-graph-chain &&
grep -q BDAT graph-$shell.git/objects/info/commit-graphs/*.graph &&
git -C graph-$shell.git commit-graph verify
'
test_expect_success "the commit-graph hook writes a graph anyone can read under a tight umask under $shell" '
git init -q --bare private-graph-$shell.git &&
git -C work push -q ../private-graph-$shell.git main &&
(cd private-graph-$shell.git && umask 077 && $run "$HOOK_DIRECTORY/post-receive.commit-graph" 2>../err) &&
test_must_be_empty err &&
ls -l private-graph-$shell.git/objects/info/commit-graphs >modes &&
test_grep "commit-graph-chain" modes &&
test_grep "\.graph" modes &&
test_grep ! "^-r-------" modes
'
test_expect_success "a later push moves the graph on under $shell" '
graphs=graph-$shell.git/objects/info/commit-graphs &&
cp $graphs/commit-graph-chain chain-$shell &&
test_commit -C work graph-$shell &&
git -C work push -q ../graph-$shell.git HEAD:main &&
(cd graph-$shell.git && $run "$HOOK_DIRECTORY/post-receive.commit-graph" 2>../err) &&
test_must_be_empty err &&
newest=$(tail -n 1 $graphs/commit-graph-chain) &&
test -n "$newest" &&
test_path_is_file $graphs/graph-$newest.graph &&
test_grep ! "^$newest\$" chain-$shell &&
git -C graph-$shell.git commit-graph verify
'
test_expect_success "a git too old to write the graph leaves it quietly under $shell" '
mkdir -p old-git &&
write_script old-git/git <<-\EOF &&
if test "$1" = commit-graph
then
echo "error: unknown option" >&2
exit 129
fi
exec "$REAL_GIT" "$@"
EOF
real_git=$(command -v git) &&
shim="$PWD/old-git" &&
graphs=graph-$shell.git/objects/info/commit-graphs &&
cp $graphs/commit-graph-chain old-chain-$shell &&
test_commit -C work old-$shell &&
git -C work push -q ../graph-$shell.git HEAD:main &&
(
cd graph-$shell.git &&
REAL_GIT=$real_git PATH="$shim:$PATH" $run "$HOOK_DIRECTORY/post-receive.commit-graph" 2>../err
) &&
test_must_be_empty err &&
test_cmp old-chain-$shell $graphs/commit-graph-chain
'
done
test_expect_success 'the age hook works installed in a repository pushed to' '
git init -q --bare pushed-age.git &&
cp "$HOOK_DIRECTORY/post-receive.cgit-age" pushed-age.git/hooks/post-receive &&
git -C work push ../pushed-age.git main 2>push-err &&
test_grep "main -> main" push-err &&
test_grep ! "^remote:" push-err &&
test_cmp expect pushed-age.git/info/web/last-modified
'
test_expect_success 'the cache hook works installed in a repository pushed to' '
fresh_slots &&
cp cache-hook scan/a.git/hooks/post-receive &&
chmod +x scan/a.git/hooks/post-receive &&
test_when_finished "rm -f scan/a.git/hooks/post-receive" &&
test_commit -C pusher pushed &&
git -C pusher push ../scan/a.git HEAD:refs/heads/main 2>push-err &&
test_grep "HEAD -> main" push-err &&
test_grep ! "^remote:" push-err &&
cached_requests slots >left &&
test_grep "|/b.git/log/" left &&
test_grep ! "a.git" left
'
test_expect_success 'the commit-graph hook works installed in a repository pushed to' '
git init -q --bare pushed-graph.git &&
cp "$HOOK_DIRECTORY/post-receive.commit-graph" pushed-graph.git/hooks/post-receive &&
git -C work push ../pushed-graph.git main 2>push-err &&
test_grep "main -> main" push-err &&
test_grep ! "^remote:" push-err &&
test_path_is_file pushed-graph.git/objects/info/commit-graphs/commit-graph-chain &&
git -C pushed-graph.git commit-graph verify
'
test_done
|