Files
upstream-gitea/modules/git/catfile_batch.go
upstream-mirror 52ae0c5802
Some checks failed
cache-seeder / prune (push) Has been skipped
cache-seeder / frontend (push) Failing after 3s
cache-seeder / python (push) Failing after 3s
cache-seeder / gobuild (push) Failing after 4s
cache-seeder / lint (bindata, lint-backend) (push) Failing after 3s
giteabot backport / giteabot (push) Has been skipped
giteabot / giteabot (push) Has been skipped
cache-prune / prune (push) Has been skipped
release-nightly-snapcraft / build-and-publish (ubuntu-24.04) (push) Has been cancelled
release-nightly-snapcraft / build-and-publish (ubuntu-24.04-arm) (push) Has been cancelled
release-nightly / nightly-binary (push) Has been cancelled
release-nightly / nightly-container (push) Has been cancelled
cron-renovate / cron-renovate (push) Has been skipped
snapshot of https://github.com/go-gitea/gitea.git (main, 2026-09-15; blobs > 131072B omitted)
2026-09-15 21:41:59 +05:00

62 lines
2.2 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package git
import (
"context"
"io"
"os"
"gitea.dev/modules/git/gitrepo"
"gitea.dev/modules/util"
)
type BufferedReader interface {
io.Reader
Buffered() int
Peek(n int) ([]byte, error)
Discard(n int) (int, error)
ReadString(sep byte) (string, error)
ReadSlice(sep byte) ([]byte, error)
ReadBytes(sep byte) ([]byte, error)
}
type CatFileObject struct {
ID string
Type string
Size int64
}
type CatFileBatch interface {
// QueryInfo queries the object info from the git repository by its object name using "git cat-file --batch" family commands.
// "git cat-file" accepts "<rev>" for the object name, it can be a ref name, object id, etc. https://git-scm.com/docs/gitrevisions
// In Gitea, we only use the simple ref name or object id, no other complex rev syntax like "suffix" or "git describe" although they are supported by git.
QueryInfo(obj string) (*CatFileObject, error)
// QueryContent is similar to QueryInfo, it queries the object info and additionally returns a reader for its content.
// FIXME: this design still follows the old pattern: the returned BufferedReader is very fragile,
// callers should carefully maintain its lifecycle and discard all unread data.
// TODO: It needs to be refactored to a fully managed Reader stream in the future, don't let callers manually Close or Discard
QueryContent(obj string) (*CatFileObject, BufferedReader, error)
}
type CatFileBatchCloser interface {
CatFileBatch
Context() context.Context
Close()
}
// NewBatch creates a "batch object provider (CatFileBatch)" for the given repository path to retrieve object info and content efficiently.
// The CatFileBatch and the readers create by it should only be used in the same goroutine.
func NewBatch(ctx context.Context, repo RepositoryFacade) (CatFileBatchCloser, error) {
repoPath := gitrepo.RepoLocalPath(repo)
if _, err := os.Stat(repoPath); err != nil {
return nil, util.NewNotExistErrorf("repo %q doesn't exist", repo.LogString())
}
if DefaultFeatures().SupportCatFileBatchCommand {
return newCatFileBatchCommand(ctx, repo), nil
}
return newCatFileBatchLegacy(ctx, repo), nil
}