fix line count for files that don't end in a newline

Signed-off-by: Derek Stevens <nilix@nilfm.cc>
This commit is contained in:
Derek Stevens 2023-02-01 22:08:04 -07:00 committed by Anirudh Oppiliappan
parent ee5ab32a40
commit ee800624f5
No known key found for this signature in database
GPG Key ID: 8A93F96F78C5D4C4
1 changed files with 8 additions and 0 deletions

View File

@ -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