loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request '[UI] Adjust trailing EOL behavior for empty file' (#5013) from gusted/forgejo-adjust-eol into forgejo

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/5013
Reviewed-by: Otto <otto@codeberg.org>

Gusted f28cde13 0d45ed0f

+33 -28
+5 -4
routers/web/repo/view.go
··· 562 562 // empty: 0 lines; "a": 1 line; "a\n": 1 line; "a\nb": 2 lines; 563 563 // When rendering, the last empty line is not rendered in U and isn't counted towards the number of lines. 564 564 // To tell users that the file not contains a trailing EOL, text with a tooltip is displayed in the file header. 565 + // Trailing EOL is only considered if the file has content. 565 566 // This NumLines is only used for the display on the UI: "xxx lines" 566 - hasTrailingEOL := bytes.HasSuffix(buf, []byte{'\n'}) 567 - ctx.Data["HasTrailingEOL"] = hasTrailingEOL 568 - ctx.Data["HasTrailingEOLSet"] = true 569 567 if len(buf) == 0 { 570 568 ctx.Data["NumLines"] = 0 571 569 } else { 570 + hasNoTrailingEOL := !bytes.HasSuffix(buf, []byte{'\n'}) 571 + ctx.Data["HasNoTrailingEOL"] = hasNoTrailingEOL 572 + 572 573 numLines := bytes.Count(buf, []byte{'\n'}) 573 - if !hasTrailingEOL { 574 + if hasNoTrailingEOL { 574 575 numLines++ 575 576 } 576 577 ctx.Data["NumLines"] = numLines
+1 -1
templates/repo/file_info.tmpl
··· 9 9 {{.NumLines}} {{ctx.Locale.TrN .NumLines "repo.line" "repo.lines"}} 10 10 </div> 11 11 {{end}} 12 - {{if and .HasTrailingEOLSet (not .HasTrailingEOL)}} 12 + {{if .HasNoTrailingEOL}} 13 13 <div class="file-info-entry" data-tooltip-content="{{ctx.Locale.Tr "repo.no_eol.tooltip"}}"> 14 14 {{ctx.Locale.Tr "repo.no_eol.text"}} 15 15 </div>
+27 -23
tests/integration/repo_view_test.go
··· 179 179 TreePath: "test-4", 180 180 ContentReader: strings.NewReader("Really two\nlines\n"), 181 181 }, 182 + { 183 + Operation: "create", 184 + TreePath: "empty", 185 + ContentReader: strings.NewReader(""), 186 + }, 187 + { 188 + Operation: "create", 189 + TreePath: "seemingly-empty", 190 + ContentReader: strings.NewReader("\n"), 191 + }, 182 192 }) 183 193 defer f() 184 194 185 - t.Run("No EOL", func(t *testing.T) { 186 - defer tests.PrintCurrentTest(t)() 187 - 188 - req := NewRequest(t, "GET", repo.Link()+"/src/branch/main/test-1") 195 + testEOL := func(t *testing.T, filename string, hasEOL bool) { 196 + t.Helper() 197 + req := NewRequestf(t, "GET", "%s/src/branch/main/%s", repo.Link(), filename) 189 198 resp := MakeRequest(t, req, http.StatusOK) 190 199 htmlDoc := NewHTMLParser(t, resp.Body) 191 200 192 201 fileInfo := htmlDoc.Find(".file-info").Text() 193 - assert.Contains(t, fileInfo, "No EOL") 202 + if hasEOL { 203 + assert.NotContains(t, fileInfo, "No EOL") 204 + } else { 205 + assert.Contains(t, fileInfo, "No EOL") 206 + } 207 + } 194 208 195 - req = NewRequest(t, "GET", repo.Link()+"/src/branch/main/test-3") 196 - resp = MakeRequest(t, req, http.StatusOK) 197 - htmlDoc = NewHTMLParser(t, resp.Body) 209 + t.Run("No EOL", func(t *testing.T) { 210 + defer tests.PrintCurrentTest(t)() 198 211 199 - fileInfo = htmlDoc.Find(".file-info").Text() 200 - assert.Contains(t, fileInfo, "No EOL") 212 + testEOL(t, "test-1", false) 213 + testEOL(t, "test-3", false) 201 214 }) 202 215 203 216 t.Run("With EOL", func(t *testing.T) { 204 217 defer tests.PrintCurrentTest(t)() 205 218 206 - req := NewRequest(t, "GET", repo.Link()+"/src/branch/main/test-2") 207 - resp := MakeRequest(t, req, http.StatusOK) 208 - htmlDoc := NewHTMLParser(t, resp.Body) 209 - 210 - fileInfo := htmlDoc.Find(".file-info").Text() 211 - assert.NotContains(t, fileInfo, "No EOL") 212 - 213 - req = NewRequest(t, "GET", repo.Link()+"/src/branch/main/test-4") 214 - resp = MakeRequest(t, req, http.StatusOK) 215 - htmlDoc = NewHTMLParser(t, resp.Body) 216 - 217 - fileInfo = htmlDoc.Find(".file-info").Text() 218 - assert.NotContains(t, fileInfo, "No EOL") 219 + testEOL(t, "test-2", true) 220 + testEOL(t, "test-4", true) 221 + testEOL(t, "empty", true) 222 + testEOL(t, "seemingly-empty", true) 219 223 }) 220 224 }) 221 225 }