summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Derek Stevens <nilix@nilfm.cc>2023-02-01 22:08:04 -0700
committerLibravatar Anirudh Oppiliappan <x@icyphox.sh>2023-02-08 00:30:51 +0530
commitee800624f58b26204c1f846a524ccef5a1b4e386 (patch)
treeaae652ab3c0304cdcbc6a849ad9953a2d3637275
parentee5ab32a40e5ae4c0f629dc9313e9a1115d6a8ee (diff)
downloadlegit-ee800624f58b26204c1f846a524ccef5a1b4e386.tar.gz
legit-ee800624f58b26204c1f846a524ccef5a1b4e386.tar.bz2
legit-ee800624f58b26204c1f846a524ccef5a1b4e386.zip
fix line count for files that don't end in a newline
Signed-off-by: Derek Stevens <nilix@nilfm.cc>
-rw-r--r--routes/template.go8
1 files changed, 8 insertions, 0 deletions
diff --git a/routes/template.go b/routes/template.go
index d3e0cef..408841a 100644
--- a/routes/template.go
+++ b/routes/template.go
@@ -45,15 +45,23 @@ func (d *deps) listFiles(files []git.NiceTree, data map[string]any, w http.Respo
func countLines(r io.Reader) (int, error) {
buf := make([]byte, 32*1024)
+ bufLen := 0
count := 0
nl := []byte{'\n'}
for {
c, err := r.Read(buf)
+ if c > 0 {
+ bufLen += c
+ }
count += bytes.Count(buf[:c], nl)
switch {
case err == io.EOF:
+ /* handle last line not having a newline at the end */
+ if bufLen >= 1 && buf[bufLen-1] != '\n' {
+ count++
+ }
return count, nil
case err != nil:
return 0, err