Refactor cat-file batch operations and support --batch-command approach (#35775)

Replace #34651 and address more problems including fix framework bugs and changing to QueryInfo and QueryContent calls.

---------

Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
This commit is contained in:
wxiaoguang
2026-01-09 05:37:36 +08:00
committed by GitHub
parent ee9d8893a7
commit e226720cff
34 changed files with 832 additions and 812 deletions
+16 -35
View File
@@ -6,8 +6,6 @@
package git
import (
"bufio"
"bytes"
"io"
"code.gitea.io/gitea/modules/log"
@@ -25,39 +23,28 @@ type Blob struct {
// DataAsync gets a ReadCloser for the contents of a blob without reading it all.
// Calling the Close function on the result will discard all unread output.
func (b *Blob) DataAsync() (io.ReadCloser, error) {
func (b *Blob) DataAsync() (_ io.ReadCloser, retErr error) {
batch, cancel, err := b.repo.CatFileBatch(b.repo.Ctx)
if err != nil {
return nil, err
}
defer func() {
// if there was an error, cancel the batch right away,
// otherwise let the caller close it
if retErr != nil {
cancel()
}
}()
rd := batch.Reader()
_, err = batch.Writer().Write([]byte(b.ID.String() + "\n"))
info, contentReader, err := batch.QueryContent(b.ID.String())
if err != nil {
cancel()
return nil, err
}
_, _, size, err := ReadBatchLine(rd)
if err != nil {
cancel()
return nil, err
}
b.gotSize = true
b.size = size
if size < 4096 {
bs, err := io.ReadAll(io.LimitReader(rd, size))
defer cancel()
if err != nil {
return nil, err
}
_, err = rd.Discard(1)
return io.NopCloser(bytes.NewReader(bs)), err
}
b.size = info.Size
return &blobReader{
rd: rd,
n: size,
rd: contentReader,
n: info.Size,
cancel: cancel,
}, nil
}
@@ -68,30 +55,24 @@ func (b *Blob) Size() int64 {
return b.size
}
batch, cancel, err := b.repo.CatFileBatchCheck(b.repo.Ctx)
batch, cancel, err := b.repo.CatFileBatch(b.repo.Ctx)
if err != nil {
log.Debug("error whilst reading size for %s in %s. Error: %v", b.ID.String(), b.repo.Path, err)
return 0
}
defer cancel()
_, err = batch.Writer().Write([]byte(b.ID.String() + "\n"))
info, err := batch.QueryInfo(b.ID.String())
if err != nil {
log.Debug("error whilst reading size for %s in %s. Error: %v", b.ID.String(), b.repo.Path, err)
return 0
}
_, _, b.size, err = ReadBatchLine(batch.Reader())
if err != nil {
log.Debug("error whilst reading size for %s in %s. Error: %v", b.ID.String(), b.repo.Path, err)
return 0
}
b.gotSize = true
b.size = info.Size
return b.size
}
type blobReader struct {
rd *bufio.Reader
rd BufferedReader
n int64
cancel func()
}