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
|
test_description='Check content on tree page'
. ./setup.sh
test_expect_success 'generate bar/tree' 'cgit_ok_url "bar/tree" >tmp'
test_expect_success 'find file-1' 'grep ">file-1<" tmp'
test_expect_success 'find file-50' 'grep ">file-50<" tmp'
test_expect_success 'the links column has a name' '
grep "<th class=.ls-links. scope=.col. aria-label=.Links.></th>" tmp
'
test_expect_success 'generate bar/tree/file-50' 'cgit_ok_url "bar/tree/file-50" >tmp'
test_expect_success 'find line 1' '
grep "<a id=.n1. href=.#n1.>1</a>" tmp
'
test_expect_success 'a one line file gets no second line number' '
cgit_ok_url "bar/tree/file-50" >tmp &&
test_grep "<a id=.n1. href=.#n1.>1</a>" tmp &&
test_grep ! "<a id=.n2. href=.#n2.>2</a>" tmp
'
test_expect_success 'the file table is read as layout' '
grep "<table class=.blob. role=.presentation.>" tmp
'
css_declarations() {
awk -v target="$1" -v properties="$2" '
BEGIN {
RS = "}"
n = split(properties, list, " ")
for (i = 1; i <= n; i++)
wanted[list[i]] = 1
}
{
n = split($0, part, "[{]")
if (n < 2)
next
named = 0
m = split(part[n - 1], selector, ",")
for (i = 1; i <= m; i++) {
s = selector[i]
gsub(/^[ \t\n]+|[ \t\n]+$/, "", s)
if (s == target)
named = 1
}
if (!named)
next
m = split(part[n], declaration, ";")
for (i = 1; i <= m; i++) {
d = declaration[i]
gsub(/^[ \t\n]+|[ \t\n]+$/, "", d)
name = substr(d, 1, index(d, ":") - 1)
if (name in wanted)
print d
}
}
' "$TEST_OUTPUT_DIRECTORY/../assets/cgit.css"
}
test_expect_success 'the code is set in the font of its line numbers' '
css_declarations "div#cgit table.blob code" "font font-family font-size" >decls &&
tail -n 1 decls >actual &&
echo "font: inherit" >expect &&
test_cmp expect actual
'
test_expect_success 'set up a file holding the characters a browser breaks at' '
test_create_repo repos/breaks &&
(
cd repos/breaks &&
printf "a\\rb\\vc\\fd\\302\\205e\\342\\200\\250f\\342\\200\\251g\\nh\\r\\ni\\r" >breaks.txt &&
git add breaks.txt &&
git commit -m breaks
) &&
cgitrc_repo breaksrc breaks enable-blame=1
'
test_expect_success 'each of them is shown as an escape in the tree' '
cgit_ok breaksrc "url=breaks/tree/breaks.txt" >tmp &&
grep "<code>a<span class=.escape.>\\\\r</span>b<span class=.escape.>\\\\v</span>c" tmp &&
grep "c<span class=.escape.>\\\\f</span>d<span class=.escape.>\\\\u0085</span>e" tmp &&
grep "e<span class=.escape.>\\\\u2028</span>f<span class=.escape.>\\\\u2029</span>g$" tmp
'
test_expect_success 'only a carriage return before a line feed is left as it is' '
cgit_ok breaksrc "url=breaks/tree/breaks.txt" >tmp &&
printf "h\\r\\n" >expect &&
grep "^h" tmp >actual &&
test_cmp expect actual &&
grep "^i<span class=.escape.>\\\\r</span></code>" tmp
'
test_expect_success 'the numbers count only the lines git counts' '
cgit_ok breaksrc "url=breaks/tree/breaks.txt" >tmp &&
test_grep "<a id=.n3. href=.#n3.>3</a>" tmp &&
test_grep ! "<a id=.n4. href=.#n4.>4</a>" tmp
'
test_expect_success 'the blame page escapes them the same way' '
cgit_ok breaksrc "url=breaks/blame/breaks.txt" >tmp &&
grep "<code>a<span class=.escape.>\\\\r</span>b<span class=.escape.>\\\\v</span>c" tmp &&
grep "c<span class=.escape.>\\\\f</span>d<span class=.escape.>\\\\u0085</span>e" tmp &&
grep "e<span class=.escape.>\\\\u2028</span>f<span class=.escape.>\\\\u2029</span>g$" tmp &&
grep "^i<span class=.escape.>\\\\r</span></code>" tmp
'
test_expect_success 'the blame stripes are as wide as the escaped line' '
cgit_ok breaksrc "url=breaks/blame/breaks.txt" >tmp &&
grep "^ \\{31\\}</pre></div>" tmp
'
test_expect_success 'blame is refused unless enabled' '
cgit_url "bar/blame/file-1" >tmp &&
test_grep "^Status: 403" tmp &&
test_grep "<div class=.error.>Blame is disabled</div>" tmp
'
test_expect_success 'the folder crumbs of a blame page lead to the tree' '
mkrepo repos/nested 1 &&
(
cd repos/nested &&
mkdir -p a/b &&
echo x >a/b/file.txt &&
git add a &&
git commit -m nested
) &&
cgitrc_repo nestedrc nested enable-blame=1 &&
cgit_ok nestedrc "url=nested/blame/a/b/file.txt" >tmp &&
grep "path: <a href=./nested/tree/.>root</a>/<a href=./nested/tree/a.>a</a>/<a href=./nested/tree/a/b.>b</a>/<a href=./nested/blame/a/b/file.txt.>file.txt</a>" tmp &&
cgit_ok nestedrc "url=nested/tree/a" >tmp &&
test_grep "href=./nested/tree/a/b.>b</a>" tmp &&
cgit_ok nestedrc "url=nested/tree/a/b" >tmp &&
test_grep "href=./nested/tree/a/b/file.txt.>file.txt</a>" tmp
'
test_expect_success 'a blame hash is titled with its commit' '
cgit_ok nestedrc "url=nested/blame/a/b/file.txt" >tmp &&
test_grep "<a title=.author A U Thor <author@example.com> 2005-04-01 13:14:15 +0200$" tmp &&
test_grep "^committer C O Mitter <committer@example.com> 2005-04-01 13:14:15 +0200$" tmp &&
test_grep "^nested. href=./nested/commit/a/b/file.txt?id=" tmp &&
cgitrc_repo noemailrc nested enable-blame=1 enable-plain-email=0 &&
cgit_ok noemailrc "url=nested/blame/a/b/file.txt" >tmp &&
test_grep "<a title=.author A U Thor 2005-04-01 13:14:15 +0200$" tmp &&
test_grep "^committer C O Mitter 2005-04-01 13:14:15 +0200$" tmp
'
test_expect_success 'blame links a commit to the file before it' '
mkrepo repos/renamed 1 &&
(
cd repos/renamed &&
test_seq 1 10 >a.txt &&
git add a.txt &&
git commit -m "add a.txt" &&
git mv a.txt b.txt &&
test_seq 1 9 >b.txt &&
echo ten >>b.txt &&
git commit -am "rename a.txt"
) &&
cgitrc_repo renamedrc renamed enable-blame=1 &&
cgit_ok renamedrc "url=renamed/blame/b.txt" >tmp &&
added=$(git -C repos/renamed rev-parse HEAD~1) &&
grep "title=.Blame the previous revision. href=./renamed/blame/a.txt?id=$added.>^</a>" tmp &&
test $(grep -c "Blame the previous revision" tmp) = 1 &&
cgit_ok renamedrc "url=renamed/blame/a.txt&id=$added" >tmp &&
test_grep "<a href=./renamed/plain/a.txt?id=$added.>plain</a>" tmp &&
test_grep "<table class=.blame blob.>" tmp
'
test_expect_success 'generate foo+bar/tree' 'cgit_ok_url "foo%2bbar/tree" >tmp'
test_expect_success 'verify a+b link' '
grep "/foo%2Bbar/tree/a%2Bb" tmp
'
test_expect_success 'generate foo+bar/tree?h=1+2' 'cgit_ok_url "foo%2bbar/tree&h=1%2b2" >tmp'
test_expect_success 'verify a+b?h=1+2 link' '
grep "/foo%2Bbar/tree/a%2Bb?h=1%2B2" tmp
'
test_expect_success 'set up a repo with a nested submodule' '
mkrepo repos/submodules 1 &&
(
cd repos/submodules &&
sub=$(git rev-parse HEAD) &&
git update-index --add --cacheinfo 160000,$sub,vendor/sub &&
git commit -m gitlink
) &&
cgitrc_repo submodrc submodules enable-blame=1
'
test_expect_success 'generate submodules/tree/vendor' '
cgit_ok submodrc "url=submodules/tree/vendor" >tmp
'
test_expect_success 'a submodule row shows the entry name alone' '
grep "class=.ls-mod.>sub</span>" tmp &&
test_grep ! ">vendor/sub</span>" tmp
'
test_expect_success 'the submodule hash is titled with the full id' '
sub=$(git -C repos/submodules rev-parse HEAD~1) &&
grep "class=.ls-mod-hash. title=.$sub.>" tmp
'
test_expect_success 'a submodule row shows no size' '
cgit_ok submodrc "url=submodules/tree/vendor" >tmp &&
grep "ls-mod" tmp >row &&
test_grep "class=.ls-size.>-</td>" row
'
test_expect_success 'a submodule row offers log but not blame or plain' '
cgit_ok submodrc "url=submodules/tree/vendor" >tmp &&
grep "ls-mod" tmp >row &&
test_grep ">log</a>" row &&
test_grep ! ">blame</a>" row &&
test_grep ! ">plain</a>" row
'
test_expect_success 'a file row still offers blame' '
cgit_ok submodrc "url=submodules/tree/" >tmp &&
grep ">blame</a>" tmp
'
test_expect_success 'a submodule path is answered with the submodule' '
sub=$(git -C repos/submodules rev-parse HEAD~1) &&
cgit_rc submodrc "url=submodules/tree/vendor/sub" >tmp &&
grep "^Status: 404" tmp &&
grep "<div class=.error.>This path is a submodule: <span class=.ls-mod.>sub</span> @ <span class=.ls-mod-hash. title=.$sub.>" tmp
'
test_expect_success 'only a link target inside the tree is linked' '
test_create_repo repos/links &&
(
cd repos/links &&
echo hi >target.txt &&
git add target.txt &&
rel=$(printf target.txt | git hash-object -w --stdin) &&
abs=$(printf /etc/passwd | git hash-object -w --stdin) &&
up=$(printf ../outside | git hash-object -w --stdin) &&
git update-index --add --cacheinfo 120000,$rel,rel &&
git update-index --add --cacheinfo 120000,$abs,abs &&
git update-index --add --cacheinfo 120000,$up,up &&
git commit -m links
) &&
cgitrc_repo linksrc links &&
cgit_ok linksrc "url=links/tree/" >tmp &&
grep " -> <a class=.ls-blob. href=./links/tree/target.txt.>target.txt</a>" tmp &&
grep " -> /etc/passwd</td>" tmp &&
test_grep ! "href=./links/tree/etc/passwd" tmp &&
test_grep " -> \.\./outside</td>" tmp
'
test_expect_success 'a symlink is not offered blame' '
cgitrc_repo linksblamerc links enable-blame=1 &&
cgit_ok linksblamerc "url=links/tree/" >tmp &&
grep "href=./links/blame/target.txt.>blame</a>" tmp &&
test_grep ! "href=./links/blame/rel.>blame</a>" tmp &&
cgit_ok linksblamerc "url=links/tree/rel" >tmp &&
grep "<div class=.blob-header.>" tmp &&
test_grep ! ">blame</a>" tmp
'
test_expect_success 'a long directory chain ends on a link' '
test_create_repo repos/deep &&
(
cd repos/deep &&
p=d1 &&
for i in $(test_seq 2 17)
do
p=$p/d$i || return 1
done &&
mkdir -p $p &&
echo deep >$p/file &&
git add . &&
git commit -m deep
) &&
cgitrc_repo deeprc deep &&
cgit_ok deeprc "url=deep/tree/" >tmp &&
grep ">d15</a></td>" tmp &&
test_grep ! " / </td>" tmp
'
test_expect_success 'a broken chain keeps the row links on the first folder' '
test_create_repo repos/cut &&
(
cd repos/cut &&
mkdir -p a/b &&
echo x >a/b/file &&
git add a &&
git commit -m base &&
odd=$(printf "tree %s\\nparent %s\\nauthor A <a@x> 1700000000 +0000\\ncommitter A <a@x> 1700000000 +0000\\n\\nodd\\n" \
$(git rev-parse HEAD^{tree}) $(git rev-parse HEAD:a/b) |
git hash-object -t commit -w --stdin --literally) &&
git update-ref refs/heads/odd $odd
) &&
cgitrc_repo cutrc cut &&
cgit_rc cutrc "url=cut/tree/&h=odd" >tmp 2>err &&
test_grep "^Status: 200" tmp &&
test_grep "object $(git -C repos/cut rev-parse HEAD:a/b) is a commit, not a tree" err &&
grep ">b</a></td>" tmp &&
grep "href=./cut/log/a?h=odd.>log</a>" tmp &&
test_grep "</html>" tmp
'
test_expect_success 'a binary file in the tree shows as a hex dump' '
test_create_repo repos/bin &&
(
cd repos/bin &&
printf "\\000\\001\\002bin" >blob.bin &&
git add blob.bin &&
git commit -m binary
) &&
cgitrc_repo binrc bin &&
cgit_ok binrc "url=bin/tree/blob.bin" >tmp &&
grep "<table class=.bin-blob. tabindex=.0. aria-label=.Hex dump.>" tmp &&
grep "<td class=.right.>0000</td><td class=.hex.> 00 01 02 62 69 6e" tmp &&
test_grep ! "class=.truncated." tmp
'
test_expect_success 'a focused hex dump shows the focus ring' '
ring="outline outline-style outline-width outline-color" &&
css_declarations "div#cgit a:focus-visible" "$ring" >decls &&
tail -n 1 decls >expect &&
echo "outline: solid 2px var(--link)" >ring &&
test_cmp ring expect &&
css_declarations "div#cgit table.bin-blob:focus-visible" "$ring" >decls &&
tail -n 1 decls >actual &&
test_cmp expect actual
'
test_expect_success 'a large binary is dumped only in part' '
(
cd repos/bin &&
printf "\\000" >big.bin &&
test_seq 1 20000 >>big.bin &&
git add big.bin &&
git commit -m "large binary"
) &&
cgit_ok binrc "url=bin/tree/big.bin" >tmp &&
grep "<td class=.right.>ffe0</td>" tmp &&
test_grep ! "<td class=.right.>10000</td>" tmp &&
grep "<div class=.truncated.>Only the first 64 KB of this file are shown. <a href=./bin/plain/big.bin.>View the whole file</a>.</div>" tmp
'
test_expect_success 'a listing offers blame only where it works' '
(
cd repos/bin &&
echo hello >small.txt &&
test_seq 1 2000 >large.txt &&
git add small.txt large.txt &&
git commit -m "text files"
) &&
cgitrc_repo binblamerc bin enable-blame=1 max-blob-size=4 &&
cgit_ok binblamerc "url=bin/tree/" >tmp &&
test_grep "href=./bin/tree/large.txt.>large.txt</a>" tmp &&
test_grep "href=./bin/tree/blob.bin.>blob.bin</a>" tmp &&
test_grep "href=./bin/tree/big.bin.>big.bin</a>" tmp &&
test_grep "href=./bin/blame/small.txt.>blame</a>" tmp &&
test_grep ! "href=./bin/blame/large.txt.>blame</a>" tmp &&
test_grep ! "href=./bin/blame/blob.bin.>blame</a>" tmp &&
test_grep ! "href=./bin/blame/big.bin.>blame</a>" tmp
'
test_expect_success 'blame says why it cannot blame a path' '
cgit_ok binblamerc "url=bin/blame/blob.bin" >tmp &&
test_grep "<div class=.error.>Blob is binary</div>" tmp &&
cgit_rc nestedrc "url=nested/blame/a" >tmp &&
test_grep "^Status: 404" tmp &&
test_grep "<div class=.error.>Blame is not available for a directory</div>" tmp &&
cgit_rc nestedrc "url=nested/blame/a/no-such-file" >tmp &&
test_grep "^Status: 404" tmp &&
test_grep "<div class=.error.>Not found</div>" tmp
'
test_expect_success 'the file view offers blame only for a text file' '
cgit_ok binblamerc "url=bin/tree/small.txt" >tmp &&
test_grep "(<a href=./bin/plain/small.txt.>plain</a>) (<a href=./bin/blame/small.txt.>blame</a>)</div>" tmp &&
cgit_ok binblamerc "url=bin/tree/blob.bin" >tmp &&
test_grep "(<a href=./bin/plain/blob.bin.>plain</a>)</div>" tmp &&
test_grep ! ">blame</a>" tmp
'
test_expect_success 'a NUL past the first 8000 bytes still gets a blame link' '
test_create_repo repos/late &&
(
cd repos/late &&
test_seq 1 2000 >late.txt &&
printf "\\000end\\n" >>late.txt &&
git add late.txt &&
git commit -m "late NUL"
) &&
cgitrc_repo laterc late enable-blame=1 &&
cgit_ok laterc "url=late/tree/" >tmp &&
test_grep "href=./late/blame/late.txt.>blame</a>" tmp &&
cgit_ok laterc "url=late/blame/late.txt" >tmp &&
test_grep "<div class=.error.>Blob is binary</div>" tmp &&
cgit_ok laterc "url=late/tree/late.txt" >tmp &&
test_grep "<table class=.bin-blob. tabindex=.0. aria-label=.Hex dump.>" tmp &&
test_grep ! ">blame</a>" tmp
'
test_expect_success 'tree groups directories before files' '
test_create_repo repos/grouped &&
mkdir repos/grouped/zsub &&
test_commit -C repos/grouped --no-tag inner zsub/inner &&
test_commit -C repos/grouped --no-tag top afile &&
cgitrc_repo groupedrc grouped enable-tree-group-dirs=1 &&
cgit_ok groupedrc "url=grouped/tree/" >tmp &&
dirline=$(grep -n "tree/zsub" tmp | head -n 1 | cut -d: -f1) &&
fileline=$(grep -n "tree/afile" tmp | head -n 1 | cut -d: -f1) &&
test -n "$dirline" &&
test -n "$fileline" &&
test "$dirline" -lt "$fileline"
'
test_done
|