From 8f53a69c2e5be4284c04a0c8f8b7fa5589bfb37c Mon Sep 17 00:00:00 2001 From: Solomon Victorino Date: Sat, 24 Aug 2024 09:47:04 -0600 Subject: [PATCH] fix: include last line in file previews with no trailing newline --- modules/markup/file_preview.go | 5 ++- modules/markup/html_test.go | 36 +++++++++++++++++- .../3f/ed9bce8610a52048747f627b3863374642c85c | Bin 0 -> 91 bytes .../4c/1aaf56bcb9f39dcf65f3f250726850aed13cd6 | Bin 0 -> 187 bytes .../8c/7e5a667f1b771847fe88c01c3de34413a1b220 | Bin 0 -> 16 bytes .../repo/repo1_filepreview/refs/heads/master | 2 +- 6 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 modules/markup/tests/repo/repo1_filepreview/objects/3f/ed9bce8610a52048747f627b3863374642c85c create mode 100644 modules/markup/tests/repo/repo1_filepreview/objects/4c/1aaf56bcb9f39dcf65f3f250726850aed13cd6 create mode 100644 modules/markup/tests/repo/repo1_filepreview/objects/8c/7e5a667f1b771847fe88c01c3de34413a1b220 diff --git a/modules/markup/file_preview.go b/modules/markup/file_preview.go index 993df717e1..49a5f1e8ba 100644 --- a/modules/markup/file_preview.go +++ b/modules/markup/file_preview.go @@ -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... diff --git a/modules/markup/html_test.go b/modules/markup/html_test.go index 42ce99903d..68d1ada5b3 100644 --- a/modules/markup/html_test.go +++ b/modules/markup/html_test.go @@ -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", + `

`+ + `
`+ + `
`+ + `
`+ + `gogits/gogs – `+ + `single-line.txt`+ + `
`+ + ``+ + `Line 1 in gogits/gogs@4c1aaf5`+ + ``+ + `
`+ + `
`+ + ``+ + ``+ + ``+ + ``+ + ``+ + ``+ + ``+ + `
A`+`
`+ + `
`+ + `
`+ + `

`, + 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" diff --git a/modules/markup/tests/repo/repo1_filepreview/objects/3f/ed9bce8610a52048747f627b3863374642c85c b/modules/markup/tests/repo/repo1_filepreview/objects/3f/ed9bce8610a52048747f627b3863374642c85c new file mode 100644 index 0000000000000000000000000000000000000000..ebcf0765a533592eb098c67017db99e7eb270f0c GIT binary patch literal 91 zcmV-h0HptT0V^p=O;s>AVK6Z;FfdRkNG!=wI$;?aDE2$`9=&6fJtCud9aR1kFK*sj5i}1ot3IN^uA5@udDV_iT literal 0 HcmV?d00001 diff --git a/modules/markup/tests/repo/repo1_filepreview/objects/4c/1aaf56bcb9f39dcf65f3f250726850aed13cd6 b/modules/markup/tests/repo/repo1_filepreview/objects/4c/1aaf56bcb9f39dcf65f3f250726850aed13cd6 new file mode 100644 index 0000000000000000000000000000000000000000..b0857df8abb3ba69aa6a10195d804b6d842d9aa4 GIT binary patch literal 187 zcmV;s07UIB0&GqGi)xDhW2H}5(7 zXbDYDjvR|ytyb|qKU#;kA>F|F{*CJ>Q%$0Z)IM`uYUa5Q)!YK8&3+$ze`U1J{N;W@?TD|}P literal 0 HcmV?d00001 diff --git a/modules/markup/tests/repo/repo1_filepreview/objects/8c/7e5a667f1b771847fe88c01c3de34413a1b220 b/modules/markup/tests/repo/repo1_filepreview/objects/8c/7e5a667f1b771847fe88c01c3de34413a1b220 new file mode 100644 index 0000000000000000000000000000000000000000..c22450a204bdaab6550b4cff751d972550843662 GIT binary patch literal 16 Xcmb