mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-08-03 00:45:22 +02:00
git/blob: refactor line-counting logic (#8270)
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8270 Reviewed-by: Gusted <gusted@noreply.codeberg.org>
This commit is contained in:
commit
30bfa13308
4 changed files with 29 additions and 36 deletions
|
@ -172,33 +172,6 @@ func (b *Blob) GetBlobContent(limit int64) (string, error) {
|
|||
return string(buf), err
|
||||
}
|
||||
|
||||
// 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 := 1
|
||||
lineSep := []byte{'\n'}
|
||||
|
||||
c, err := reader.Read(buf)
|
||||
if c == 0 && err == io.EOF {
|
||||
return 0, nil
|
||||
}
|
||||
for {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
// GetBlobContentBase64 Reads the content of the blob with a base64 encode and returns the encoded string
|
||||
func (b *Blob) GetBlobContentBase64() (string, error) {
|
||||
dataRc, err := b.DataAsync()
|
||||
|
|
|
@ -82,19 +82,19 @@ func RefBlame(ctx *context.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
ctx.Data["NumLinesSet"] = true
|
||||
ctx.Data["NumLines"], err = blob.GetBlobLineCount()
|
||||
if err != nil {
|
||||
ctx.ServerError("GetBlobLineCount", err)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := performBlame(ctx, ctx.Repo.Commit, ctx.Repo.TreePath, ctx.FormBool("bypass-blame-ignore"))
|
||||
if err != nil {
|
||||
ctx.ServerError("performBlame", err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Data["NumLinesSet"] = true
|
||||
numLines := 0
|
||||
for _, p := range result.Parts {
|
||||
numLines += len(p.Lines)
|
||||
}
|
||||
ctx.Data["NumLines"] = numLines
|
||||
|
||||
ctx.Data["UsesIgnoreRevs"] = result.UsesIgnoreRevs
|
||||
ctx.Data["FaultyIgnoreRevsFile"] = result.FaultyIgnoreRevsFile
|
||||
|
||||
|
|
|
@ -440,11 +440,29 @@ func getCommitFileLineCount(commit *git.Commit, filePath string) int {
|
|||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
lineCount, err := blob.GetBlobLineCount()
|
||||
reader, err := blob.DataAsync()
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return lineCount
|
||||
defer reader.Close()
|
||||
buf := make([]byte, 32*1024)
|
||||
count := 1
|
||||
lineSep := []byte{'\n'}
|
||||
|
||||
c, err := reader.Read(buf)
|
||||
if c == 0 && err == io.EOF {
|
||||
return 0
|
||||
}
|
||||
for {
|
||||
count += bytes.Count(buf[:c], lineSep)
|
||||
switch {
|
||||
case err == io.EOF:
|
||||
return count
|
||||
case err != nil:
|
||||
return count
|
||||
}
|
||||
c, err = reader.Read(buf)
|
||||
}
|
||||
}
|
||||
|
||||
// Diff represents a difference between two git trees.
|
||||
|
|
|
@ -712,6 +712,8 @@ func TestGetDiffFull(t *testing.T) {
|
|||
|
||||
assert.Equal(t, ".gitattributes", diff.Files[0].Name)
|
||||
assert.Equal(t, "24139dae656713ba861751fb2c2ac38839349a7a", diff.Files[0].NameHash)
|
||||
assert.Len(t, diff.Files[0].Sections, 2)
|
||||
assert.Equal(t, 4, diff.Files[0].Sections[1].Lines[0].SectionInfo.LeftIdx)
|
||||
})
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue