summaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authorSolomon Victorino <git@solomonvictorino.com>2024-08-24 17:47:04 +0200
committerSolomon Victorino <git@solomonvictorino.com>2024-08-25 01:10:31 +0200
commit8f53a69c2e5be4284c04a0c8f8b7fa5589bfb37c (patch)
treebecb5fba083d37f198a6a2a402124ab0c6a7b9ac /modules
parentMerge pull request 'fix: improve the display of PR & issue short links' (#507... (diff)
downloadforgejo-8f53a69c2e5be4284c04a0c8f8b7fa5589bfb37c.tar.xz
forgejo-8f53a69c2e5be4284c04a0c8f8b7fa5589bfb37c.zip
fix: include last line in file previews with no trailing newline
Diffstat (limited to 'modules')
-rw-r--r--modules/markup/file_preview.go5
-rw-r--r--modules/markup/html_test.go36
-rw-r--r--modules/markup/tests/repo/repo1_filepreview/objects/3f/ed9bce8610a52048747f627b3863374642c85cbin0 -> 91 bytes
-rw-r--r--modules/markup/tests/repo/repo1_filepreview/objects/4c/1aaf56bcb9f39dcf65f3f250726850aed13cd6bin0 -> 187 bytes
-rw-r--r--modules/markup/tests/repo/repo1_filepreview/objects/8c/7e5a667f1b771847fe88c01c3de34413a1b220bin0 -> 16 bytes
-rw-r--r--modules/markup/tests/repo/repo1_filepreview/refs/heads/master2
6 files changed, 39 insertions, 4 deletions
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",
+ `<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"
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 0000000000..ebcf0765a5
--- /dev/null
+++ b/modules/markup/tests/repo/repo1_filepreview/objects/3f/ed9bce8610a52048747f627b3863374642c85c
Binary files differ
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 0000000000..b0857df8ab
--- /dev/null
+++ b/modules/markup/tests/repo/repo1_filepreview/objects/4c/1aaf56bcb9f39dcf65f3f250726850aed13cd6
Binary files differ
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 0000000000..c22450a204
--- /dev/null
+++ b/modules/markup/tests/repo/repo1_filepreview/objects/8c/7e5a667f1b771847fe88c01c3de34413a1b220
Binary files differ
diff --git a/modules/markup/tests/repo/repo1_filepreview/refs/heads/master b/modules/markup/tests/repo/repo1_filepreview/refs/heads/master
index 49c348b41c..df25bf45f0 100644
--- a/modules/markup/tests/repo/repo1_filepreview/refs/heads/master
+++ b/modules/markup/tests/repo/repo1_filepreview/refs/heads/master
@@ -1 +1 @@
-190d9492934af498c3f669d6a2431dc5459e5b20
+4c1aaf56bcb9f39dcf65f3f250726850aed13cd6