Add alternateObjectDirs param to get blob related funcs (#1157)

This commit is contained in:
Darko Draskovic 2024-03-28 02:02:06 +00:00 committed by Harness
parent 4f6227bf1a
commit 9a1e656be1
7 changed files with 15 additions and 6 deletions

View File

@ -37,10 +37,11 @@ type BlobReader struct {
func (g *Git) GetBlob( func (g *Git) GetBlob(
ctx context.Context, ctx context.Context,
repoPath string, repoPath string,
alternateObjectDirs []string,
sha sha.SHA, sha sha.SHA,
sizeLimit int64, sizeLimit int64,
) (*BlobReader, error) { ) (*BlobReader, error) {
stdIn, stdOut, cancel := CatFileBatch(ctx, repoPath) stdIn, stdOut, cancel := CatFileBatch(ctx, repoPath, alternateObjectDirs)
line := sha.String() + "\n" line := sha.String() + "\n"
_, err := stdIn.Write([]byte(line)) _, err := stdIn.Write([]byte(line))
if err != nil { if err != nil {

View File

@ -41,6 +41,7 @@ type WriteCloserError interface {
func CatFileBatch( func CatFileBatch(
ctx context.Context, ctx context.Context,
repoPath string, repoPath string,
alternateObjectDirs []string,
flags ...command.CmdOptionFunc, flags ...command.CmdOptionFunc,
) (WriteCloserError, *bufio.Reader, func()) { ) (WriteCloserError, *bufio.Reader, func()) {
const bufferSize = 32 * 1024 const bufferSize = 32 * 1024
@ -67,6 +68,7 @@ func CatFileBatch(
stderr := bytes.Buffer{} stderr := bytes.Buffer{}
cmd := command.New("cat-file", cmd := command.New("cat-file",
command.WithFlag("--batch"), command.WithFlag("--batch"),
command.WithAlternateObjectDirs(alternateObjectDirs...),
) )
cmd.Add(flags...) cmd.Add(flags...)
err := cmd.Run(ctx, err := cmd.Run(ctx,

View File

@ -758,7 +758,7 @@ func GetCommit(
repoPath string, repoPath string,
rev string, rev string,
) (*Commit, error) { ) (*Commit, error) {
wr, rd, cancel := CatFileBatch(ctx, repoPath) wr, rd, cancel := CatFileBatch(ctx, repoPath, nil)
defer cancel() defer cancel()
_, _ = wr.Write([]byte(rev + "\n")) _, _ = wr.Write([]byte(rev + "\n"))

View File

@ -40,7 +40,7 @@ func (g *Git) MatchFiles(
return nil, fmt.Errorf("failed to list files in match files: %w", err) return nil, fmt.Errorf("failed to list files in match files: %w", err)
} }
catFileWriter, catFileReader, catFileStop := CatFileBatch(ctx, repoPath) catFileWriter, catFileReader, catFileStop := CatFileBatch(ctx, repoPath, nil)
defer catFileStop() defer catFileStop()
var files []FileContent var files []FileContent

View File

@ -50,7 +50,7 @@ func (g *Git) GetSubmodule(
ref, commit) ref, commit)
} }
reader, err := g.GetBlob(ctx, repoPath, node.SHA, 0) reader, err := g.GetBlob(ctx, repoPath, nil, node.SHA, 0)
if err != nil { if err != nil {
return nil, processGitErrorf(err, "error reading commit for ref '%s'", ref) return nil, processGitErrorf(err, "error reading commit for ref '%s'", ref)
} }

View File

@ -126,7 +126,7 @@ func getAnnotatedTags(
return nil, ErrRepositoryPathEmpty return nil, ErrRepositoryPathEmpty
} }
// The tag is an annotated tag with a message. // The tag is an annotated tag with a message.
writer, reader, cancel := CatFileBatch(ctx, repoPath) writer, reader, cancel := CatFileBatch(ctx, repoPath, nil)
defer func() { defer func() {
cancel() cancel()
_ = writer.Close() _ = writer.Close()

View File

@ -45,7 +45,13 @@ func (s *Service) GetBlob(ctx context.Context, params *GetBlobParams) (*GetBlobO
repoPath := getFullPathForRepo(s.reposRoot, params.RepoUID) repoPath := getFullPathForRepo(s.reposRoot, params.RepoUID)
// TODO: do we need to validate request for nil? // TODO: do we need to validate request for nil?
reader, err := s.git.GetBlob(ctx, repoPath, sha.Must(params.SHA), params.SizeLimit) reader, err := s.git.GetBlob(
ctx,
repoPath,
params.AlternateObjectDirs,
sha.Must(params.SHA),
params.SizeLimit,
)
if err != nil { if err != nil {
return nil, err return nil, err
} }