Skip to content

Commit

Permalink
gitindex: respect flag -submodule (#208)
Browse files Browse the repository at this point in the history
With this change we don't set `RepoCache` if `-submodule=false`;

It turns out that we didn't respect the submodule flag, IE submodules was enabled by default.
This caused issues, e.g. if a repository had an invalid .gitmodules file.

I also wrapped some of the high-level calls that gitindex does to generate better error
messages and speed-up future debugging.
  • Loading branch information
stefanhengl authored Nov 12, 2021
1 parent 3c5cea3 commit 7ad19f6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
20 changes: 12 additions & 8 deletions gitindex/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,14 +360,17 @@ func IndexGitRepo(opts Options) error {
opts.BuildOptions.RepositoryDescription.Source = opts.RepoDir
repo, err := git.PlainOpen(opts.RepoDir)
if err != nil {
return err
return fmt.Errorf("git.PlainOpen: %w", err)
}

if err := setTemplatesFromConfig(&opts.BuildOptions.RepositoryDescription, opts.RepoDir); err != nil {
log.Printf("setTemplatesFromConfig(%s): %s", opts.RepoDir, err)
}

repoCache := NewRepoCache(opts.RepoCacheDir)
var repoCache *RepoCache
if opts.Submodules {
repoCache = NewRepoCache(opts.RepoCacheDir)
}

// branch => (path, sha1) => repo.
repos := map[fileKey]BlobLocation{}
Expand All @@ -380,7 +383,7 @@ func IndexGitRepo(opts Options) error {

branches, err := expandBranches(repo, opts.Branches, opts.BranchPrefix)
if err != nil {
return err
return fmt.Errorf("expandBranches: %w", err)
}
for _, b := range branches {
commit, err := getCommit(repo, opts.BranchPrefix, b)
Expand All @@ -389,7 +392,7 @@ func IndexGitRepo(opts Options) error {
continue
}

return err
return fmt.Errorf("getCommit: %w", err)
}

opts.BuildOptions.RepositoryDescription.Branches = append(opts.BuildOptions.RepositoryDescription.Branches, zoekt.RepositoryBranch{
Expand All @@ -403,17 +406,18 @@ func IndexGitRepo(opts Options) error {

tree, err := commit.Tree()
if err != nil {
return fmt.Errorf("commit.Tree: %w", err)
return err
}

ig, err := newIgnoreMatcher(tree)
if err != nil {
return err
return fmt.Errorf("newIgnoreMatcher: %w", err)
}

files, subVersions, err := TreeToFiles(repo, tree, opts.BuildOptions.RepositoryDescription.URL, repoCache)
if err != nil {
return err
return fmt.Errorf("TreeToFiles: %w", err)
}
for k, v := range files {
if ig.Match(k.Path) {
Expand Down Expand Up @@ -458,7 +462,7 @@ func IndexGitRepo(opts Options) error {

builder, err := build.NewBuilder(opts.BuildOptions)
if err != nil {
return err
return fmt.Errorf("build.NewBuilder: %w", err)
}
defer builder.Finish()

Expand Down Expand Up @@ -505,7 +509,7 @@ func IndexGitRepo(opts Options) error {
Content: contents,
Branches: brs,
}); err != nil {
return err
return fmt.Errorf("error adding document with name %s: %w", key.FullPath(), err)
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions gitindex/submodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package gitindex

import (
"bytes"
"fmt"
"io"

"github.com/go-git/go-git/v5/plumbing/format/config"
Expand All @@ -37,7 +38,7 @@ func ParseGitModules(content []byte) (map[string]*SubmoduleEntry, error) {
// https://stackoverflow.com/a/21375405
r, _, err := buf.ReadRune()
if err != nil && err != io.EOF {
return nil, err
return nil, fmt.Errorf("buf.ReadRune: %w", err)
}
if r != '\uFEFF' {
buf.UnreadRune()
Expand All @@ -46,7 +47,7 @@ func ParseGitModules(content []byte) (map[string]*SubmoduleEntry, error) {
cfg := &config.Config{}

if err := dec.Decode(cfg); err != nil {
return nil, err
return nil, fmt.Errorf("error decoding content %s: %w", string(content), err)
}

result := map[string]*SubmoduleEntry{}
Expand Down
11 changes: 7 additions & 4 deletions gitindex/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,18 @@ func newRepoWalker(r *git.Repository, repoURL string, repoCache *RepoCache) *rep

// parseModuleMap initializes rw.submodules.
func (rw *repoWalker) parseModuleMap(t *object.Tree) error {
if rw.repoCache == nil {
return nil
}
modEntry, _ := t.File(".gitmodules")
if modEntry != nil {
c, err := blobContents(&modEntry.Blob)
if err != nil {
return err
return fmt.Errorf("blobContents: %w", err)
}
mods, err := ParseGitModules(c)
if err != nil {
return err
return fmt.Errorf("ParseGitModules: %w", err)
}
rw.submodules = map[string]*SubmoduleEntry{}
for _, entry := range mods {
Expand All @@ -99,7 +102,7 @@ func TreeToFiles(r *git.Repository, t *object.Tree,
rw := newRepoWalker(r, repoURL, repoCache)

if err := rw.parseModuleMap(t); err != nil {
return nil, nil, err
return nil, nil, fmt.Errorf("parseModuleMap: %w", err)
}

tw := object.NewTreeWalker(t, true, make(map[plumbing.Hash]bool))
Expand All @@ -110,7 +113,7 @@ func TreeToFiles(r *git.Repository, t *object.Tree,
break
}
if err := rw.handleEntry(name, &entry); err != nil {
return nil, nil, err
return nil, nil, fmt.Errorf("handleEntry: %w", err)
}
}
return rw.tree, rw.subRepoVersions, nil
Expand Down

0 comments on commit 7ad19f6

Please sign in to comment.