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
|
test_description='Check content on diff page'
. ./setup.sh
test_expect_success 'an added file shows its header, links and line' '
cgit_ok_url "foo/diff" >tmp &&
grep "a/file-5 b/file-5" tmp &&
grep "<a href=./foo/tree/file-5?id=" tmp &&
grep "new file mode 100644" tmp &&
grep "<div class=.hunk.>@@ -0,0 +1 @@</div>" tmp &&
grep "<div class=.add.>+5</div>" tmp
'
test_expect_success 'the unified diff table is read as layout' '
cgit_ok_url "foo/diff" >tmp &&
grep "<table class=.diff. role=.presentation.>" tmp
'
test_expect_success 'a tree on either side is named as the request gave it' '
tree=$(git -C repos/foo rev-parse --short HEAD^{tree}) &&
cgit_url "foo/diff/&id=$tree" >tmp &&
grep "^Status: 404" tmp &&
grep "<div class=.error.>Not a commit: $tree</div>" tmp &&
cgit_url "foo/diff/&id2=$tree" >tmp &&
grep "^Status: 404" tmp &&
grep "<div class=.error.>Not a commit: $tree</div>" tmp
'
test_expect_success 'a path limits the diff and the stat says so' '
cgit_ok_url "foo/diff/file-5" >tmp &&
grep "Diffstat</a> (limited to .file-5.)</div>" tmp &&
grep "diff --git a/file-5 b/file-5" tmp
'
test_expect_success 'a diff between two named commits spans both' '
id=$(git -C repos/foo rev-parse HEAD) &&
id2=$(git -C repos/foo rev-parse HEAD~2) &&
cgit_ok_url "foo/diff&id=$id&id2=$id2" >tmp &&
grep "diff --git a/file-4 b/file-4" tmp &&
grep "diff --git a/file-5 b/file-5" tmp &&
grep "<div class=.diffstat-summary.>2 files changed, 2 insertions, 0 deletions</div>" tmp
'
test_expect_success 'the root commit is diffed against nothing' '
root=$(git -C repos/foo rev-list --max-parents=0 HEAD) &&
cgit_ok_url "foo/diff&id=$root" >tmp &&
grep "diff --git a/file-1 b/file-1<br>new file mode 100644" tmp &&
grep "<div class=.diffstat-summary.>1 file changed, 1 insertion, 0 deletions</div>" tmp
'
test_expect_success 'set up a commit of every kind of change' '
mkrepo repos/kinds 1 &&
(
cd repos/kinds &&
one=$(git rev-parse HEAD) &&
echo gone >gone.txt &&
echo mode >mode.sh &&
printf "\000old" >bin.dat &&
git add gone.txt mode.sh bin.dat &&
git update-index --add --cacheinfo 160000,$one,sub &&
git commit -m "add the files" &&
two=$(git rev-parse HEAD) &&
git rm -q gone.txt &&
git update-index --chmod=+x mode.sh &&
printf "\000new" >bin.dat &&
git add bin.dat &&
git update-index --cacheinfo 160000,$two,sub &&
git commit -m "change the files"
) &&
cgitrc_repo kindsrc kinds
'
test_expect_success 'each kind of change is worded as git words it' '
one=$(git -C repos/kinds rev-parse HEAD~2) &&
two=$(git -C repos/kinds rev-parse HEAD~1) &&
cgit_ok kindsrc "url=kinds/diff/" >tmp &&
grep "diff --git a/gone.txt b/gone.txt<br>deleted file mode 100644" tmp &&
grep "<td class=.mode.>-rwxr-xr-x<span class=.modechange.>\[-rw-r--r--\]</span></td>" tmp &&
grep "<br>index [0-9a-f]*\.\.[0-9a-f]* 100644\.\.100755<br>" tmp &&
grep "<td class=.right.>bin</td><td class=.graph.>4 -> 4 bytes</td>" tmp &&
grep "Binary files differ" tmp &&
grep "<div class=.del.>-Subproject $one</div><div class=.add.>+Subproject $two</div>" tmp
'
test_expect_success 'set up a repo with single-line hunks' '
mkrepo repos/hunk1 1 &&
(
cd repos/hunk1 &&
printf "only\n" >one.txt &&
git add one.txt &&
git commit -m "add one.txt" &&
printf "ONLY\n" >one.txt &&
git commit -am "change one.txt"
) &&
cgitrc_repo hunk1rc hunk1 &&
git -C repos/hunk1 diff HEAD~1 HEAD >raw.diff &&
grep "^@@ -1 +1 @@" raw.diff
'
test_expect_success 'ssdiff numbers a comma-less hunk from its real line' '
old=$(git -C repos/hunk1 rev-parse HEAD~1) &&
new=$(git -C repos/hunk1 rev-parse HEAD) &&
cgit_ok hunk1rc "url=hunk1/diff/&dt=1" >tmp &&
grep "<a href=./hunk1/tree/one.txt?id=$old#n1.>1</a>" tmp &&
grep "<a href=./hunk1/tree/one.txt?id=$new#n1.>1</a>" tmp
'
test_expect_success 'set up a repo whose later hunk starts apart on each side' '
mkrepo repos/later 1 &&
(
cd repos/later &&
test_seq 1 20 >twenty.txt &&
git add twenty.txt &&
git commit -m "add twenty.txt" &&
test_seq 1 20 | sed -e 2d -e s/^15$/fifteen/ >twenty.txt &&
git commit -am "change twenty.txt"
) &&
cgitrc_repo laterrc later &&
git -C repos/later diff HEAD~1 HEAD >raw.diff &&
grep "^@@ -12,7 +11,7 @@" raw.diff
'
test_expect_success 'ssdiff numbers each side of a hunk from its own start' '
old=$(git -C repos/later rev-parse HEAD~1) &&
new=$(git -C repos/later rev-parse HEAD) &&
cgit_ok laterrc "url=later/diff/&dt=1" >tmp &&
grep -x "<td class=.lineno.><a href=./later/tree/twenty.txt?id=$old#n12.>12</a></td><td class=.ctx.>12</td>" tmp &&
grep -x "<td class=.lineno.><a href=./later/tree/twenty.txt?id=$new#n11.>11</a></td><td class=.ctx.>12</td></tr>" tmp &&
grep "<a href=./later/tree/twenty.txt?id=$old#n15.>15</a></td><td class=.changed.>" tmp &&
grep "<a href=./later/tree/twenty.txt?id=$new#n14.>14</a></td><td class=.changed.>" tmp
'
test_expect_success 'set up a repo with shrinking changed lines' '
mkrepo repos/lcs 1 &&
(
cd repos/lcs &&
awk "BEGIN{for(n=120;n>0;n-=3){s=\"\";
for(i=0;i<n;i++)s=s sprintf(\"%c\",97+(i*7+n)%26); print s}}" >lines.txt &&
git add lines.txt &&
git commit -m "add lines.txt" &&
awk "BEGIN{for(n=120;n>0;n-=3){s=\"\";
for(i=0;i<n;i++){c=97+(i*7+n)%26; if(i%7==0)c=97+(c-97+5)%26;
s=s sprintf(\"%c\",c)} print s}}" >lines.txt &&
git commit -am "change every line"
) &&
cgitrc_repo lcsrc lcs
'
test_expect_success 'every changed pair gets both a del and an add span' '
cgit_ok lcsrc "url=lcs/diff/&dt=1" >tmp &&
test $(grep -c "^<td class=.lineno.>.*<td class=.changed.>.*<span class=.del.>.*</td>$" tmp) -eq 40 &&
test $(grep -c "^<td class=.lineno.>.*<td class=.changed.>.*<span class=.add.>.*</td></tr>$" tmp) -eq 40
'
test_expect_success 'stripping the highlight leaves each side its own lines' '
cgit_ok lcsrc "url=lcs/diff/&dt=1" >tmp &&
sed -n "s/^<td class=.lineno.><a [^>]*>[0-9]*<\/a><\/td><td class=.changed.>\(.*\)<\/td>$/\1/p" tmp |
sed "s/<[^>]*>//g" >old.out &&
sed -n "s/^<td class=.lineno.><a [^>]*>[0-9]*<\/a><\/td><td class=.changed.>\(.*\)<\/td><\/tr>$/\1/p" tmp |
sed "s/<[^>]*>//g" >new.out &&
git -C repos/lcs show HEAD~1:lines.txt >expect &&
test_cmp expect old.out &&
git -C repos/lcs show HEAD:lines.txt >expect &&
test_cmp expect new.out
'
test_expect_success 'a short pair after a longer one marks its own characters' '
mkrepo repos/pair 1 &&
(
cd repos/pair &&
test_write_lines bcde xa >pair.txt &&
git add pair.txt &&
git commit -m "add pair.txt" &&
test_write_lines abcd ax >pair.txt &&
git commit -am "change pair.txt"
) &&
cgitrc_repo pairrc pair &&
cgit_ok pairrc "url=pair/diff/&dt=1" >tmp &&
sed -n "s/^<td class=.lineno.><a [^>]*>[0-9]*<\/a><\/td>//p" tmp | tr -d "\047" >actual &&
cat >expect <<-\EOF &&
<td class=changed>bcd<span class=del>e</span></td>
<td class=changed><span class=add>a</span>bcd</td></tr>
<td class=changed><span class=del>x</span>a</td>
<td class=changed>a<span class=add>x</span></td></tr>
EOF
test_cmp expect actual
'
test_expect_success 'each side of a row is classed by what it holds' '
cgit_ok_url "foo/diff&dt=1" >tmp &&
grep -x "<td class=.lineno.></td><td class=.add-dark.></td>" tmp &&
grep "<td class=.add.>5</td></tr>" tmp &&
cgit_ok hunk1rc "url=hunk1/diff/&dt=1" >tmp &&
grep "</a></td><td class=.changed.><span class=.del.>only</span></td>$" tmp &&
grep "</a></td><td class=.changed.><span class=.add.>ONLY</span></td></tr>$" tmp
'
test_expect_success 'forced colours mark the changed cells with a border' '
sed -n "/^@media (forced-colors: active) {$/,/^}/p" \
"$TEST_OUTPUT_DIRECTORY/../assets/cgit.css" >rule &&
sed -n "/^ div#cgit table.ssdiff td.add,$/,/^ }$/p" rule >cells &&
grep "^ div#cgit table.ssdiff td.del,$" cells &&
grep "^ div#cgit table.ssdiff td.changed {$" cells &&
grep "^ border-left-width: 4px;$" cells
'
test_expect_success 'set up a commit for the remaining side-by-side rows' '
mkrepo repos/cells 1 &&
(
cd repos/cells &&
test_write_lines gone >gone.txt &&
test_write_lines one two keep >uneven.txt &&
printf "%0130d\n" 0 >long.txt &&
printf "a\tb\nx\n" >tab.txt &&
printf "\000old" >bin.dat &&
git add . &&
git commit -m "add the files" &&
git rm -q gone.txt &&
test_write_lines ONE keep >uneven.txt &&
printf "%0129d1\n" 0 >long.txt &&
printf "a\tb\ny\n" >tab.txt &&
printf "\000new" >bin.dat &&
git commit -am "change the files"
) &&
cgitrc_repo cellsrc cells
'
test_expect_success 'the remaining side-by-side rows' '
cgit_ok cellsrc "url=cells/diff/&dt=1" >tmp &&
grep "</a></td><td class=.del.>gone</td>$" tmp &&
grep -x "<td class=.lineno.></td><td class=.del-dark.></td></tr>" tmp &&
grep "</a></td><td class=.changed.>one</td>$" tmp &&
grep "</a></td><td class=.changed.>ONE</td></tr>$" tmp &&
grep "</a></td><td class=.changed.>two</td>$" tmp &&
grep -x "<td class=.lineno.></td><td class=.changed-dark.></td></tr>" tmp &&
grep "</a></td><td class=.changed.>$(printf "%0130d" 0)</td>$" tmp &&
grep "</a></td><td class=.changed.>$(printf "%0129d1" 0)</td></tr>$" tmp &&
grep "</a></td><td class=.ctx.>a b</td>$" tmp &&
grep "<tr><td colspan=.4.>Binary files differ</td></tr>" tmp
'
test_expect_success 'a side-by-side diff past max-diff-lines is cut short' '
cgitrc_repo cellscaprc cells max-diff-lines=1 &&
cgit_ok cellscaprc "url=cells/diff/&dt=1" >tmp &&
grep "<tr><td colspan=.4. class=.truncated.>This diff is too large to be rendered inline. <a href=./cells/diff/long.txt?dt=1.>View it on its own page</a>.</td></tr>" tmp
'
test_expect_success 'dt=2 shows the diffstat alone' '
cgit_ok_url "foo/diff&dt=2" >tmp &&
grep "<td class=.add.><a href=./foo/diff/file-5.>file-5</a></td>" tmp &&
grep "<div class=.diffstat-summary.>1 file changed, 1 insertion, 0 deletions</div>" tmp &&
! grep "class=.hunk." tmp &&
! grep "<table class=.diff. role=.presentation.>" tmp
'
test_expect_success 'set up a change to whitespace alone' '
mkrepo repos/space 1 &&
(
cd repos/space &&
echo "a b" >space.txt &&
git add space.txt &&
git commit -m "add space.txt" &&
echo "a b" >space.txt &&
git commit -am "widen space.txt"
) &&
cgitrc_repo spacerc space
'
test_expect_success 'ignorews=1 hides a change to whitespace alone' '
cgit_ok spacerc "url=space/diff/" >tmp &&
grep "<div class=.hunk.>@@ -1 +1 @@</div>" tmp &&
cgit_ok spacerc "url=space/diff/&ignorews=1" >tmp &&
grep "diff --git a/space.txt b/space.txt" tmp &&
! grep "class=.hunk." tmp
'
test_expect_success 'context sets the lines around a change, up to a cap' '
mkrepo repos/wide 1 &&
(
cd repos/wide &&
test_seq 1 100 >wide.txt &&
git add wide.txt &&
git commit -m "add wide.txt" &&
test_seq 1 100 | sed s/^50$/fifty/ >wide.txt &&
git commit -am "change wide.txt"
) &&
cgitrc_repo widerc wide &&
cgit_ok widerc "url=wide/diff/" >tmp &&
grep "<div class=.hunk.>@@ -47,7 +47,7 @@</div>" tmp &&
cgit_ok widerc "url=wide/diff/&context=1" >tmp &&
grep "<div class=.hunk.>@@ -49,3 +49,3 @@</div>" tmp &&
cgit_ok widerc "url=wide/diff/&context=999" >tmp &&
grep "<div class=.hunk.>@@ -10,81 +10,81 @@</div>" tmp
'
test_expect_success 'side-by-side-diffs=1 makes side by side the default' '
cgitrc_repo ssrc wide side-by-side-diffs=1 &&
cgit_ok ssrc "url=wide/diff/" >tmp &&
grep "<table class=.ssdiff.>" tmp &&
cgit_ok ssrc "url=wide/diff/&dt=0" >tmp &&
grep "<table class=.diff. role=.presentation.>" tmp
'
test_expect_success 'set up two renames that are not exact' '
mkrepo repos/renames 1 &&
(
cd repos/renames &&
test_seq 1 10 >a.txt &&
test_seq 11 20 >b.txt &&
git add a.txt b.txt &&
git commit -m "add a.txt and b.txt" &&
git mv a.txt c.txt &&
git mv b.txt d.txt &&
echo more >>c.txt &&
echo more >>d.txt &&
git commit -am "rename both"
)
'
test_expect_success 'rename-limit bounds the renames a diff looks for' '
cgitrc_repo renamesrc renames &&
cgit_ok renamesrc "url=renames/diff/" >tmp &&
grep "</a> (renamed from a.txt)</td>" tmp &&
grep "</a> (renamed from b.txt)</td>" tmp &&
cgitrc_repo renamesrc renames rename-limit=1 &&
cgit_ok renamesrc "url=renames/diff/" >tmp &&
grep "diff --git a/c.txt b/c.txt<br>new file mode 100644" tmp &&
grep "diff --git a/a.txt b/a.txt<br>deleted file mode 100644" tmp &&
! grep "renamed from" tmp &&
cgitrc_repo renamesrc renames rename-limit=0 &&
cgit_ok renamesrc "url=renames/diff/" >tmp &&
grep "</a> (renamed from a.txt)</td>" tmp &&
grep "</a> (renamed from b.txt)</td>" tmp
'
test_expect_success 'set up a renamed file' '
mkrepo_renamed repos/moved "echo changed >file-1" &&
cgitrc_repo movedrc moved
'
test_expect_success 'the diff of a renamed file shows the rename alone' '
cgit_ok movedrc "url=moved/diff/new.txt" >tmp &&
grep "diff --git a/old.txt b/new.txt" tmp &&
grep "</a> (renamed from old.txt)</td>" tmp &&
grep "<div class=.diffstat-summary.>1 file changed, 0 insertions, 0 deletions</div>" tmp &&
! grep "new file mode" tmp &&
! grep "a/file-1 b/file-1" tmp
'
test_expect_success 'so does the commit page limited to the new name' '
cgit_ok movedrc "url=moved/commit/new.txt" >tmp &&
grep "diff --git a/old.txt b/new.txt" tmp &&
grep "<div class=.diffstat-summary.>1 file changed, 0 insertions, 0 deletions</div>" tmp &&
! grep "new file mode" tmp &&
! grep "a/file-1 b/file-1" tmp
'
test_expect_success 'a followed file is diffed as the rename alone' '
cgitrc_repo followrc moved enable-follow-links=1 &&
cgit_ok followrc "url=moved/diff/new.txt&follow=1" >tmp &&
grep "diff --git a/old.txt b/new.txt" tmp &&
grep "Diffstat</a> (limited to .new.txt.)</div>" tmp &&
grep "<div class=.diffstat-summary.>1 file changed, 0 insertions, 0 deletions</div>" tmp &&
! grep "a/file-1 b/file-1" tmp &&
cgit_ok followrc "url=moved/commit/new.txt&follow=1" >tmp &&
grep "diff --git a/old.txt b/new.txt" tmp &&
grep "Diffstat</a> (limited to .new.txt.)</div>" tmp &&
! grep "a/file-1 b/file-1" tmp
'
test_expect_success 'a followed path takes in no file it only prefixes' '
mkrepo repos/prefixed 1 &&
(
cd repos/prefixed &&
mkdir small.d &&
echo c >small.c &&
echo cpp >small.cpp &&
echo d >small.d/f &&
git add . &&
git commit -m "add the files" &&
echo C >small.c &&
echo CPP >small.cpp &&
echo D >small.d/f &&
git commit -am "change the files"
) &&
cgitrc_repo prefixedrc prefixed enable-follow-links=1 &&
cgit_ok prefixedrc "url=prefixed/diff/small.c&follow=1" >tmp &&
grep "diff --git a/small.c b/small.c" tmp &&
! grep "small.cpp" tmp &&
cgit_ok prefixedrc "url=prefixed/diff/small.d&follow=1" >tmp &&
grep "diff --git a/small.d/f b/small.d/f" tmp &&
! grep "small.c" tmp
'
test_expect_success 'following with no path limits nothing' '
cgit_ok followrc "url=moved/commit/&follow=1" >tmp &&
grep "diff --git a/file-1 b/file-1" tmp &&
grep "<div class=.diffstat-header.><a href=[^>]*>Diffstat</a></div>" tmp
'
test_expect_success 'a file added outright still shows as new' '
added=$(git -C repos/moved rev-parse HEAD~1) &&
cgit_ok movedrc "url=moved/diff/old.txt&id=$added" >tmp &&
grep "new file mode 100644" tmp &&
grep "<div class=.add.>+10</div>" tmp
'
test_done
|