Skip to content

Commit

Permalink
Fix diff expansion is missing final line in a file (go-gitea#16222)
Browse files Browse the repository at this point in the history
* Fixed down offset.

* Fixed wrong line count result.
  • Loading branch information
KN4CK3R authored and AbdulrhmnGhanem committed Aug 10, 2021
1 parent c17db27 commit 85d4143
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
11 changes: 8 additions & 3 deletions modules/git/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,30 @@ func (b *Blob) GetBlobContent() (string, error) {
return string(buf), nil
}

// GetBlobLineCount gets line count of lob as raw text
// GetBlobLineCount gets line count of the blob
func (b *Blob) GetBlobLineCount() (int, error) {
reader, err := b.DataAsync()
if err != nil {
return 0, err
}
defer reader.Close()
buf := make([]byte, 32*1024)
count := 0
count := 1
lineSep := []byte{'\n'}

c, err := reader.Read(buf)
if c == 0 && err == io.EOF {
return 0, nil
}
for {
c, err := reader.Read(buf)
count += bytes.Count(buf[:c], lineSep)
switch {
case err == io.EOF:
return count, nil
case err != nil:
return count, err
}
c, err = reader.Read(buf)
}
}

Expand Down
6 changes: 5 additions & 1 deletion routers/web/repo/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,11 @@ func ExcerptBlob(ctx *context.Context) {
lastLeft += chunkSize
lastRight += chunkSize
} else {
section.Lines, err = getExcerptLines(commit, filePath, lastLeft, lastRight, idxRight-lastRight-1)
offset := -1
if direction == "down" {
offset = 0
}
section.Lines, err = getExcerptLines(commit, filePath, lastLeft, lastRight, idxRight-lastRight+offset)
leftHunkSize = 0
rightHunkSize = 0
idxLeft = lastLeft
Expand Down

0 comments on commit 85d4143

Please sign in to comment.