From 9a1e656be1cb4ec64db34e596908c770e04472a3 Mon Sep 17 00:00:00 2001 From: Darko Draskovic Date: Thu, 28 Mar 2024 02:02:06 +0000 Subject: [PATCH] Add alternateObjectDirs param to get blob related funcs (#1157) --- git/api/blob.go | 3 ++- git/api/cat-file.go | 2 ++ git/api/commit.go | 2 +- git/api/match_files.go | 2 +- git/api/submodule.go | 2 +- git/api/tag.go | 2 +- git/blob.go | 8 +++++++- 7 files changed, 15 insertions(+), 6 deletions(-) diff --git a/git/api/blob.go b/git/api/blob.go index 2296569dd..75c5e5630 100644 --- a/git/api/blob.go +++ b/git/api/blob.go @@ -37,10 +37,11 @@ type BlobReader struct { func (g *Git) GetBlob( ctx context.Context, repoPath string, + alternateObjectDirs []string, sha sha.SHA, sizeLimit int64, ) (*BlobReader, error) { - stdIn, stdOut, cancel := CatFileBatch(ctx, repoPath) + stdIn, stdOut, cancel := CatFileBatch(ctx, repoPath, alternateObjectDirs) line := sha.String() + "\n" _, err := stdIn.Write([]byte(line)) if err != nil { diff --git a/git/api/cat-file.go b/git/api/cat-file.go index 95119c2b3..945dc966c 100644 --- a/git/api/cat-file.go +++ b/git/api/cat-file.go @@ -41,6 +41,7 @@ type WriteCloserError interface { func CatFileBatch( ctx context.Context, repoPath string, + alternateObjectDirs []string, flags ...command.CmdOptionFunc, ) (WriteCloserError, *bufio.Reader, func()) { const bufferSize = 32 * 1024 @@ -67,6 +68,7 @@ func CatFileBatch( stderr := bytes.Buffer{} cmd := command.New("cat-file", command.WithFlag("--batch"), + command.WithAlternateObjectDirs(alternateObjectDirs...), ) cmd.Add(flags...) err := cmd.Run(ctx, diff --git a/git/api/commit.go b/git/api/commit.go index c004669e2..2460d5385 100644 --- a/git/api/commit.go +++ b/git/api/commit.go @@ -758,7 +758,7 @@ func GetCommit( repoPath string, rev string, ) (*Commit, error) { - wr, rd, cancel := CatFileBatch(ctx, repoPath) + wr, rd, cancel := CatFileBatch(ctx, repoPath, nil) defer cancel() _, _ = wr.Write([]byte(rev + "\n")) diff --git a/git/api/match_files.go b/git/api/match_files.go index 7c7fbdd2d..f1e2c3bf7 100644 --- a/git/api/match_files.go +++ b/git/api/match_files.go @@ -40,7 +40,7 @@ func (g *Git) MatchFiles( 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() var files []FileContent diff --git a/git/api/submodule.go b/git/api/submodule.go index b44d48418..1ed775cba 100644 --- a/git/api/submodule.go +++ b/git/api/submodule.go @@ -50,7 +50,7 @@ func (g *Git) GetSubmodule( ref, commit) } - reader, err := g.GetBlob(ctx, repoPath, node.SHA, 0) + reader, err := g.GetBlob(ctx, repoPath, nil, node.SHA, 0) if err != nil { return nil, processGitErrorf(err, "error reading commit for ref '%s'", ref) } diff --git a/git/api/tag.go b/git/api/tag.go index cfa4827b8..df1b9849c 100644 --- a/git/api/tag.go +++ b/git/api/tag.go @@ -126,7 +126,7 @@ func getAnnotatedTags( return nil, ErrRepositoryPathEmpty } // The tag is an annotated tag with a message. - writer, reader, cancel := CatFileBatch(ctx, repoPath) + writer, reader, cancel := CatFileBatch(ctx, repoPath, nil) defer func() { cancel() _ = writer.Close() diff --git a/git/blob.go b/git/blob.go index bc3a5972e..759abd7c5 100644 --- a/git/blob.go +++ b/git/blob.go @@ -45,7 +45,13 @@ func (s *Service) GetBlob(ctx context.Context, params *GetBlobParams) (*GetBlobO repoPath := getFullPathForRepo(s.reposRoot, params.RepoUID) // 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 { return nil, err }