diff --git a/gitindex/index.go b/gitindex/index.go index 7547bb78b..624f099cc 100644 --- a/gitindex/index.go +++ b/gitindex/index.go @@ -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{} @@ -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) @@ -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{ @@ -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) { @@ -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() @@ -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) } } } diff --git a/gitindex/submodule.go b/gitindex/submodule.go index 03c6e2caa..69a012ad5 100644 --- a/gitindex/submodule.go +++ b/gitindex/submodule.go @@ -16,6 +16,7 @@ package gitindex import ( "bytes" + "fmt" "io" "github.com/go-git/go-git/v5/plumbing/format/config" @@ -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() @@ -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{} diff --git a/gitindex/tree.go b/gitindex/tree.go index 145cfd69d..27a1319b0 100644 --- a/gitindex/tree.go +++ b/gitindex/tree.go @@ -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 { @@ -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)) @@ -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