Skip to content

Commit

Permalink
Make IndexGitRepo signal whether there were changes
Browse files Browse the repository at this point in the history
As described, we'd like this information to be externally available to
drive metrics for incremental indexing.

Closes #780.
  • Loading branch information
isker committed May 10, 2024
1 parent 4e674a4 commit f391f3e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion cmd/zoekt-git-index/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
32 changes: 18 additions & 14 deletions gitindex/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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{
Expand All @@ -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.
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -507,20 +511,20 @@ 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
var meanRank float64
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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit f391f3e

Please sign in to comment.