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 -40
View File
@@ -10,7 +10,6 @@ import (
"io"
"strings"
"code.gitea.io/gitea/modules/git/catfile"
"code.gitea.io/gitea/modules/git/gitcmd"
"code.gitea.io/gitea/modules/log"
)
@@ -37,26 +36,21 @@ func (repo *Repository) ResolveReference(name string) (string, error) {
// GetRefCommitID returns the last commit ID string of given reference (branch or tag).
func (repo *Repository) GetRefCommitID(name string) (string, error) {
batch, cancel, err := repo.CatFileBatchCheck(repo.Ctx)
batch, cancel, err := repo.CatFileBatch(repo.Ctx)
if err != nil {
return "", err
}
defer cancel()
_, err = batch.Writer().Write([]byte(name + "\n"))
if err != nil {
return "", err
}
shaBs, _, _, err := ReadBatchLine(batch.Reader())
info, err := batch.QueryInfo(name)
if IsErrNotExist(err) {
return "", ErrNotExist{name, ""}
}
return string(shaBs), nil
return info.ID, nil
}
// IsCommitExist returns true if given commit exists in current repository.
func (repo *Repository) IsCommitExist(name string) bool {
if err := catfile.EnsureValidGitRepository(repo.Ctx, repo.Path); err != nil {
if err := ensureValidGitRepository(repo.Ctx, repo.Path); err != nil {
log.Error("IsCommitExist: %v", err)
return false
}
@@ -73,15 +67,11 @@ func (repo *Repository) getCommit(id ObjectID) (*Commit, error) {
return nil, err
}
defer cancel()
_, _ = batch.Writer().Write([]byte(id.String() + "\n"))
return repo.getCommitFromBatchReader(batch, id)
return repo.getCommitWithBatch(batch, id)
}
func (repo *Repository) getCommitFromBatchReader(batch catfile.Batch, id ObjectID) (*Commit, error) {
rd := batch.Reader()
_, typ, size, err := ReadBatchLine(rd)
func (repo *Repository) getCommitWithBatch(batch CatFileBatch, id ObjectID) (*Commit, error) {
info, rd, err := batch.QueryContent(id.String())
if err != nil {
if errors.Is(err, io.EOF) || IsErrNotExist(err) {
return nil, ErrNotExist{ID: id.String()}
@@ -89,13 +79,13 @@ func (repo *Repository) getCommitFromBatchReader(batch catfile.Batch, id ObjectI
return nil, err
}
switch typ {
switch info.Type {
case "missing":
return nil, ErrNotExist{ID: id.String()}
case "tag":
// then we need to parse the tag
// and load the commit
data, err := io.ReadAll(io.LimitReader(rd, size))
data, err := io.ReadAll(io.LimitReader(rd, info.Size))
if err != nil {
return nil, err
}
@@ -107,19 +97,9 @@ func (repo *Repository) getCommitFromBatchReader(batch catfile.Batch, id ObjectI
if err != nil {
return nil, err
}
if _, err := batch.Writer().Write([]byte(tag.Object.String() + "\n")); err != nil {
return nil, err
}
commit, err := repo.getCommitFromBatchReader(batch, tag.Object)
if err != nil {
return nil, err
}
return commit, nil
return repo.getCommitWithBatch(batch, tag.Object)
case "commit":
commit, err := CommitFromReader(repo, id, io.LimitReader(rd, size))
commit, err := CommitFromReader(repo, id, io.LimitReader(rd, info.Size))
if err != nil {
return nil, err
}
@@ -130,8 +110,8 @@ func (repo *Repository) getCommitFromBatchReader(batch catfile.Batch, id ObjectI
return commit, nil
default:
log.Debug("Unknown typ: %s", typ)
if err := DiscardFull(rd, size+1); err != nil {
log.Debug("Unknown cat-file object type: %s", info.Type)
if err := DiscardFull(rd, info.Size+1); err != nil {
return nil, err
}
return nil, ErrNotExist{
@@ -153,16 +133,12 @@ func (repo *Repository) ConvertToGitID(commitID string) (ObjectID, error) {
}
}
batch, cancel, err := repo.CatFileBatchCheck(repo.Ctx)
batch, cancel, err := repo.CatFileBatch(repo.Ctx)
if err != nil {
return nil, err
}
defer cancel()
_, err = batch.Writer().Write([]byte(commitID + "\n"))
if err != nil {
return nil, err
}
sha, _, _, err := ReadBatchLine(batch.Reader())
info, err := batch.QueryInfo(commitID)
if err != nil {
if IsErrNotExist(err) {
return nil, ErrNotExist{commitID, ""}
@@ -170,5 +146,5 @@ func (repo *Repository) ConvertToGitID(commitID string) (ObjectID, error) {
return nil, err
}
return MustIDFromString(string(sha)), nil
return MustIDFromString(info.ID), nil
}