From f391f3eae16c884e02acfe382822c198c768638c Mon Sep 17 00:00:00 2001 From: Ian Kerins Date: Thu, 9 May 2024 20:41:18 -0400 Subject: [PATCH] Make IndexGitRepo signal whether there were changes As described, we'd like this information to be externally available to drive metrics for incremental indexing. Closes #780. --- cmd/zoekt-git-index/main.go | 2 +- gitindex/index.go | 32 ++++++++++++++++++-------------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/cmd/zoekt-git-index/main.go b/cmd/zoekt-git-index/main.go index 1d7cfe88b..c1aa5d1ad 100644 --- a/cmd/zoekt-git-index/main.go +++ b/cmd/zoekt-git-index/main.go @@ -122,7 +122,7 @@ func run() int { DeltaShardNumberFallbackThreshold: *deltaShardNumberFallbackThreshold, } - if err := gitindex.IndexGitRepo(gitOpts); err != nil { + if _, err := gitindex.IndexGitRepo(gitOpts); err != nil { log.Printf("indexGitRepo(%s, delta=%t): %v", dir, gitOpts.BuildOptions.IsDelta, err) exitStatus = 1 } diff --git a/gitindex/index.go b/gitindex/index.go index eb2588b3c..5a295d05c 100644 --- a/gitindex/index.go +++ b/gitindex/index.go @@ -388,12 +388,16 @@ func expandBranches(repo *git.Repository, bs []string, prefix string) ([]string, } // IndexGitRepo indexes the git repository as specified by the options. -func IndexGitRepo(opts Options) error { +// The returned bool indicates whether the index was updated as a result. This +// can be informative if doing incremental indexing. +func IndexGitRepo(opts Options) (bool, error) { return indexGitRepo(opts, gitIndexConfig{}) } // indexGitRepo indexes the git repository as specified by the options and the provided gitIndexConfig. -func indexGitRepo(opts Options, config gitIndexConfig) error { +// The returned bool indicates whether the index was updated as a result. This +// can be informative if doing incremental indexing. +func indexGitRepo(opts Options, config gitIndexConfig) (bool, error) { prepareDeltaBuild := prepareDeltaBuild if config.prepareDeltaBuild != nil { prepareDeltaBuild = config.prepareDeltaBuild @@ -407,13 +411,13 @@ func indexGitRepo(opts Options, config gitIndexConfig) error { // Set max thresholds, since we use them in this function. opts.BuildOptions.SetDefaults() if opts.RepoDir == "" { - return fmt.Errorf("gitindex: must set RepoDir") + return false, fmt.Errorf("gitindex: must set RepoDir") } opts.BuildOptions.RepositoryDescription.Source = opts.RepoDir repo, err := git.PlainOpen(opts.RepoDir) if err != nil { - return fmt.Errorf("git.PlainOpen: %w", err) + return false, fmt.Errorf("git.PlainOpen: %w", err) } if err := setTemplatesFromConfig(&opts.BuildOptions.RepositoryDescription, opts.RepoDir); err != nil { @@ -422,7 +426,7 @@ func indexGitRepo(opts Options, config gitIndexConfig) error { branches, err := expandBranches(repo, opts.Branches, opts.BranchPrefix) if err != nil { - return fmt.Errorf("expandBranches: %w", err) + return false, fmt.Errorf("expandBranches: %w", err) } for _, b := range branches { commit, err := getCommit(repo, opts.BranchPrefix, b) @@ -431,7 +435,7 @@ func indexGitRepo(opts Options, config gitIndexConfig) error { continue } - return fmt.Errorf("getCommit(%q, %q): %w", opts.BranchPrefix, b, err) + return false, fmt.Errorf("getCommit(%q, %q): %w", opts.BranchPrefix, b, err) } opts.BuildOptions.RepositoryDescription.Branches = append(opts.BuildOptions.RepositoryDescription.Branches, zoekt.RepositoryBranch{ @@ -445,7 +449,7 @@ func indexGitRepo(opts Options, config gitIndexConfig) error { } if opts.Incremental && opts.BuildOptions.IncrementalSkipIndexing() { - return nil + return false, nil } // branch => (path, sha1) => repo. @@ -474,7 +478,7 @@ func indexGitRepo(opts Options, config gitIndexConfig) error { if !opts.BuildOptions.IsDelta { repos, branchMap, branchVersions, err = prepareNormalBuild(opts, repo) if err != nil { - return fmt.Errorf("preparing normal build: %w", err) + return false, fmt.Errorf("preparing normal build: %w", err) } } @@ -507,7 +511,7 @@ func indexGitRepo(opts Options, config gitIndexConfig) error { builder, err := build.NewBuilder(opts.BuildOptions) if err != nil { - return fmt.Errorf("build.NewBuilder: %w", err) + return false, fmt.Errorf("build.NewBuilder: %w", err) } var ranks repoPathRanks @@ -515,12 +519,12 @@ func indexGitRepo(opts Options, config gitIndexConfig) error { if opts.BuildOptions.DocumentRanksPath != "" { data, err := os.ReadFile(opts.BuildOptions.DocumentRanksPath) if err != nil { - return err + return false, err } err = json.Unmarshal(data, &ranks) if err != nil { - return err + return false, err } // Compute the mean rank for this repository. Note: we overwrite the rank @@ -564,16 +568,16 @@ func indexGitRepo(opts Options, config gitIndexConfig) error { for _, key := range keys { doc, err := createDocument(key, repos, branchMap, ranks, opts.BuildOptions) if err != nil { - return err + return false, err } if err := builder.Add(doc); err != nil { - return fmt.Errorf("error adding document with name %s: %w", key.FullPath(), err) + return false, fmt.Errorf("error adding document with name %s: %w", key.FullPath(), err) } } } - return builder.Finish() + return true, builder.Finish() } type repoPathRanks struct {