Skip to content

Commit

Permalink
add the ability to fetch blobs directly, for use by CLI and later on …
Browse files Browse the repository at this point in the history
…the frontend
  • Loading branch information
xvandish committed Jan 4, 2025
1 parent 8211224 commit aa2c01a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
24 changes: 24 additions & 0 deletions cmd/zoekt-fileviewer/fileview.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bytes"
"errors"
"fmt"
"io"
"log"
"net/url"
"os"
Expand Down Expand Up @@ -192,6 +193,15 @@ func gitCatBlob(obj string, repoPath string) (string, error) {
return string(out), nil
}

func gitCatBlobDirect(obj string, repoPath string, w io.Writer) error {
cmd := exec.Command("git", "-C", repoPath, "cat-file", "blob", obj)
cmd.Stdout = w
if err := cmd.Run(); err != nil {
return err
}
return nil
}

// used to get the "real" name of "HEAD"
func GitRevParseAbbrev(rev string, repoPath string) (string, error) {
out, err := exec.Command("git", "-C", repoPath, "rev-parse", "--abbrev-ref", rev).Output()
Expand Down Expand Up @@ -1189,6 +1199,20 @@ func GitBlameBlob(relativePath string, repo RepoConfig, commit string) (*BlameRe

var fileDoesNotExistError = errors.New("This file does not exist at this point in history")

func GetPlainBlob(relativePath, repoPath, repoName, commit string, w io.Writer) error {
commitHash := commit
out, err := gitCommitHash(commit, repoPath)
if err == nil {
commitHash = out[:strings.Index(out, "\n")]
}
cleanPath := path.Clean(relativePath)
if cleanPath == "." {
cleanPath = ""
}
obj := commitHash + ":" + cleanPath
return gitCatBlobDirect(obj, repoPath, w)
}

// Used to support zoekt "preview file" actions.
// Since we don't want to maintain repoConfig for zoekt
// repos, we tell this function explicitly where the repo
Expand Down
25 changes: 12 additions & 13 deletions cmd/zoekt-fileviewer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const (
GitListTagsRoute = V2 + "/getAllTagsForZoekt/:parent/:repo/+/"
GetSyntaxHighlightedFileForZoektRoute = V2 + "/getSyntaxHighlightedFileForZoekt/:parent/:repo/+/"
GitLsTreeRoute = V2 + "/getDirectoryTreeForZoekt/:parent/:repo/+/"
GitPlainBlobRoute = V2 + "/getPlainGitBlob/:parent/:repo/+/"
)

func main() {
Expand Down Expand Up @@ -111,12 +112,8 @@ func (s *Server) setupRoutes() error {
s.router.Get(GitListTagsRoute, http.HandlerFunc(s.ServeListAllTagsForZoekt))
s.router.Get(GetSyntaxHighlightedFileForZoektRoute, http.HandlerFunc(s.ServeSyntaxHighlightedFileForZoekt))
s.router.Get(GitLsTreeRoute, http.HandlerFunc(s.ServeGitLsTreeForZoekt))
s.router.Get(GitPlainBlobRoute, http.HandlerFunc(s.HandleFileReq))

// m.Add("GET", "/api/v2/getSyntaxHighlightedFileForZoekt/:parent/:repo/+/", srv.Handler(srv.ServeSyntaxHighlightedFileForZoekt))
// m.Add("GET", "/api/v2/getDirectoryTreeForZoekt/:parent/:repo/+/", srv.Handler(srv.ServeGitLsTreeForZoekt))
// m.Add("GET", "/api/v2/getGitLogForZoekt/:parent/:repo/+/", srv.Handler(srv.ServeGitLogForZoekt))
// m.Add("GET", "/api/v2/getAllBranchesForZoekt/:parent/:repo/+/", srv.Handler(srv.ServeListAllBranchesForZoekt))
// m.Add("GET", "/api/v2/getAllTagsForZoekt/:parent/:repo/+/", srv.Handler(srv.ServeListAllTagsForZoekt))
handler := requestIDMiddleware(logMiddleware(s.router))

s.server = &http.Server{
Expand Down Expand Up @@ -338,12 +335,14 @@ func (s *Server) ServeListAllTagsForZoekt(w http.ResponseWriter, r *http.Request
replyJSON(context.Background(), w, 200, tags)
}

// get all tags, get all branches, getgitlog, getdirectorytreeforzoekt,
// getsyntaxhighlightedfileforzoekt
func (s *Server) HandleFileReq(w http.ResponseWriter, r *http.Request) {
parent := r.URL.Query().Get(":parent")
repo := r.URL.Query().Get(":repo")
repoRevAndPath := pat.Tail(GitPlainBlobRoute, r.URL.Path)
revision, path := parseRevisionAndPath(repoRevAndPath)
repoPath := fmt.Sprintf("%s/%s/%s.git", s.config.ZoektRepoCache, parent, repo)

// m.Add("GET", "/api/v2/getSyntaxHighlightedFileForZoekt/:parent/:repo/+/", srv.Handler(srv.ServeSyntaxHighlightedFileForZoekt))
// m.Add("GET", "/api/v2/getDirectoryTreeForZoekt/:parent/:repo/+/", srv.Handler(srv.ServeGitLsTreeForZoekt))
// m.Add("GET", "/api/v2/getGitLogForZoekt/:parent/:repo/+/", srv.Handler(srv.ServeGitLogForZoekt))
// m.Add("GET", "/api/v2/getAllBranchesForZoekt/:parent/:repo/+/", srv.Handler(srv.ServeListAllBranchesForZoekt))
// m.Add("GET", "/api/v2/getAllTagsForZoekt/:parent/:repo/+/", srv.Handler(srv.ServeListAllTagsForZoekt))
// m.Add("GET", "/api/v2/getDirectoryTreeForZoekt/", srv.Handler(srv.TestHandler))
if err := GetPlainBlob(path, repoPath, parent+"/"+repo, revision, w); err != nil {
http.Error(w, "Failed to fetch git blob", 500)
}
}

0 comments on commit aa2c01a

Please sign in to comment.