1
0
Fork 0

Merge pull request 'fix: include last line in file previews with no trailing newline' (#5085) from solomonv/fix-previews-without-trailing-eol into forgejo

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/5085
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
This commit is contained in:
Gusted 2024-08-25 00:58:14 +00:00
commit d34d8ec2cf
6 changed files with 39 additions and 4 deletions

View file

@ -7,6 +7,7 @@ import (
"bufio"
"bytes"
"html/template"
"io"
"regexp"
"slices"
"strconv"
@ -184,10 +185,12 @@ func newFilePreview(ctx *RenderContext, node *html.Node, locale translation.Loca
lineBuffer := new(bytes.Buffer)
for i := 0; i < lineCount; i++ {
buf, err := reader.ReadBytes('\n')
if err == nil || err == io.EOF {
lineBuffer.Write(buf)
}
if err != nil {
break
}
lineBuffer.Write(buf)
}
// highlight the file...

View file

@ -688,10 +688,10 @@ func TestRender_FilePreview(t *testing.T) {
require.NoError(t, err)
defer gitRepo.Close()
commit, err := gitRepo.GetCommit("HEAD")
commit, err := gitRepo.GetCommit(commitSha)
require.NoError(t, err)
blob, err := commit.GetBlobByPath("path/to/file.go")
blob, err := commit.GetBlobByPath(filePath)
require.NoError(t, err)
return blob, nil
@ -780,6 +780,38 @@ func TestRender_FilePreview(t *testing.T) {
},
)
})
t.Run("single-line", func(t *testing.T) {
testRender(
util.URLJoin(markup.TestRepoURL, "src", "commit", "4c1aaf56bcb9f39dcf65f3f250726850aed13cd6", "single-line.txt")+"#L1",
`<p></p>`+
`<div class="file-preview-box">`+
`<div class="header">`+
`<div>`+
`<a href="http://localhost:3000/gogits/gogs/" rel="nofollow">gogits/gogs</a> `+
`<a href="http://localhost:3000/gogits/gogs/src/commit/4c1aaf56bcb9f39dcf65f3f250726850aed13cd6/single-line.txt#L1" class="muted" rel="nofollow">single-line.txt</a>`+
`</div>`+
`<span class="text small grey">`+
`Line 1 in <a href="http://localhost:3000/gogits/gogs/src/commit/4c1aaf56bcb9f39dcf65f3f250726850aed13cd6" class="text black" rel="nofollow">gogits/gogs@4c1aaf5</a>`+
`</span>`+
`</div>`+
`<div class="ui table">`+
`<table class="file-preview">`+
`<tbody>`+
`<tr>`+
`<td class="lines-num"><span data-line-number="1"></span></td>`+
`<td class="lines-code chroma"><code class="code-inner">A`+`</code></td>`+
`</tr>`+
`</tbody>`+
`</table>`+
`</div>`+
`</div>`+
`<p></p>`,
map[string]string{
"user": "gogits",
"repo": "gogs2",
},
)
})
t.Run("AppSubURL", func(t *testing.T) {
urlWithSub := util.URLJoin(markup.TestAppURL, "sub", markup.TestOrgRepo, "src", "commit", sha, "path", "to", "file.go") + "#L2-L3"

View file

@ -1 +1 @@
190d9492934af498c3f669d6a2431dc5459e5b20
4c1aaf56bcb9f39dcf65f3f250726850aed13cd6