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
|
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 two 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 -C pusher push -q ../scan/a.git HEAD:refs/heads/main &&
git -C pusher push -q ../scan/b.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 &&
echo writer >slots/0123abcd.lock &&
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/
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
'
shells=
for shell in sh dash ksh bash
do
command -v $shell >/dev/null && shells="$shells $shell"
done
for shell in $shells
do
test_expect_success "a push drops its own pages and the index under $shell" '
fresh_slots &&
(cd scan/a.git && $shell ../../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 ! "a.git" left &&
test_grep ! "1|/0|" left &&
test_path_is_file slots/0123abcd.lock &&
ls slots >left-slots &&
test_grep ! "^rc-" left-slots
'
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 && $shell ../../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 && $shell ../../named-hook 2>../../err) &&
test_must_be_empty err &&
test "$(ls slots)" = 0123abcd.lock
'
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 && $shell ../../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 && $shell "$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 && $shell "$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 && $shell "$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 && $shell "$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 && $shell "$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 && $shell "$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 && $shell "$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 && $shell "$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 && $shell "$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 && $shell "$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 && $shell "$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" '
cat graph-$shell.git/objects/info/commit-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 && $shell "$HOOK_DIRECTORY/post-receive.commit-graph" 2>../err) &&
test_must_be_empty err &&
! test_cmp chain-$shell graph-$shell.git/objects/info/commit-graphs/commit-graph-chain &&
git -C graph-$shell.git commit-graph verify
'
done
test_done
|