fix: git diff blob excerpt (#38808)

1. refactor the legacy code and add more comments, remove the "+1/-1"
tricks, clarify the BuildBlobExcerptDiffSection behavior
2. fix a line-counting bug (see screenshot below)
This commit is contained in:
wxiaoguang
2026-08-07 10:44:55 +08:00
committed by GitHub
parent 9dac77fdc2
commit a7df0d5f41
5 changed files with 98 additions and 56 deletions
+18
View File
@@ -7,6 +7,7 @@ package git
import (
"io"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
@@ -56,3 +57,20 @@ func Benchmark_Blob_Data(b *testing.B) {
_ = r.Close()
}
}
func TestGetBlobLineCount(t *testing.T) {
size, count, err := getBlobLineCount(strings.NewReader(""), nil)
assert.NoError(t, err)
assert.EqualValues(t, 0, size)
assert.Equal(t, 0, count)
size, count, err = getBlobLineCount(strings.NewReader("\n"), nil)
assert.NoError(t, err)
assert.EqualValues(t, 1, size)
assert.Equal(t, 1, count)
size, count, err = getBlobLineCount(strings.NewReader("a\nb"), nil)
assert.NoError(t, err)
assert.EqualValues(t, 3, size)
assert.Equal(t, 2, count)
}