Skip to content

Commit 4cc63e9

Browse files
authored
Fix diff expansion is missing final line in a file (#16222)
* Fixed down offset. * Fixed wrong line count result.
1 parent 71c5a8f commit 4cc63e9

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

modules/git/blob.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,30 @@ func (b *Blob) GetBlobContent() (string, error) {
3434
return string(buf), nil
3535
}
3636

37-
// GetBlobLineCount gets line count of lob as raw text
37+
// GetBlobLineCount gets line count of the blob
3838
func (b *Blob) GetBlobLineCount() (int, error) {
3939
reader, err := b.DataAsync()
4040
if err != nil {
4141
return 0, err
4242
}
4343
defer reader.Close()
4444
buf := make([]byte, 32*1024)
45-
count := 0
45+
count := 1
4646
lineSep := []byte{'\n'}
47+
48+
c, err := reader.Read(buf)
49+
if c == 0 && err == io.EOF {
50+
return 0, nil
51+
}
4752
for {
48-
c, err := reader.Read(buf)
4953
count += bytes.Count(buf[:c], lineSep)
5054
switch {
5155
case err == io.EOF:
5256
return count, nil
5357
case err != nil:
5458
return count, err
5559
}
60+
c, err = reader.Read(buf)
5661
}
5762
}
5863

routers/web/repo/compare.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,11 @@ func ExcerptBlob(ctx *context.Context) {
714714
lastLeft += chunkSize
715715
lastRight += chunkSize
716716
} else {
717-
section.Lines, err = getExcerptLines(commit, filePath, lastLeft, lastRight, idxRight-lastRight-1)
717+
offset := -1
718+
if direction == "down" {
719+
offset = 0
720+
}
721+
section.Lines, err = getExcerptLines(commit, filePath, lastLeft, lastRight, idxRight-lastRight+offset)
718722
leftHunkSize = 0
719723
rightHunkSize = 0
720724
idxLeft = lastLeft

0 commit comments

Comments
 (0)