Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull out document creation into its own method #693

Merged
merged 3 commits into from
Nov 15, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 47 additions & 36 deletions gitindex/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,46 +546,13 @@ func indexGitRepo(opts Options, config gitIndexConfig) error {
keys := fileKeys[name]

for _, key := range keys {
brs := branchMap[key]
blob, err := repos[key].Repo.BlobObject(key.ID)
doc, err := createDocument(key, repos, branchMap, ranks, opts)
if err != nil {
return err
}

keyFullPath := key.FullPath()

if blob.Size > int64(opts.BuildOptions.SizeMax) && !opts.BuildOptions.IgnoreSizeMax(keyFullPath) {
if err := builder.Add(zoekt.Document{
SkipReason: fmt.Sprintf("file size %d exceeds maximum size %d", blob.Size, opts.BuildOptions.SizeMax),
Name: keyFullPath,
Branches: brs,
SubRepositoryPath: key.SubRepoPath,
}); err != nil {
return err
}
continue
}

contents, err := blobContents(blob)
if err != nil {
return err
}

var pathRanks []float64
if len(ranks.Paths) > 0 {
// If the repository has ranking data, then store the file's rank.
pathRank := ranks.rank(keyFullPath)
pathRanks = []float64{pathRank}
}

if err := builder.Add(zoekt.Document{
SubRepositoryPath: key.SubRepoPath,
Name: keyFullPath,
Content: contents,
Branches: brs,
Ranks: pathRanks,
}); err != nil {
return fmt.Errorf("error adding document with name %s: %w", keyFullPath, err)
if err := builder.Add(doc); err != nil {
return fmt.Errorf("error adding document with name %s: %w", key.FullPath(), err)
}
}
}
Expand Down Expand Up @@ -893,6 +860,50 @@ func prepareNormalBuild(options Options, repository *git.Repository) (repos map[
return repos, branchMap, branchVersions, nil
}

func createDocument(
key fileKey,
repos map[fileKey]BlobLocation,
branchMap map[fileKey][]string,
ranks repoPathRanks,
opts Options,
jtibshirani marked this conversation as resolved.
Show resolved Hide resolved
) (zoekt.Document, error) {
blob, err := repos[key].Repo.BlobObject(key.ID)
if err != nil {
return zoekt.Document{}, err
}

keyFullPath := key.FullPath()

if blob.Size > int64(opts.BuildOptions.SizeMax) && !opts.BuildOptions.IgnoreSizeMax(keyFullPath) {
return zoekt.Document{
SkipReason: fmt.Sprintf("file size %d exceeds maximum size %d", blob.Size, opts.BuildOptions.SizeMax),
Name: key.FullPath(),
Branches: branchMap[key],
SubRepositoryPath: key.SubRepoPath,
}, nil
}

contents, err := blobContents(blob)
if err != nil {
return zoekt.Document{}, err
}

var pathRanks []float64
if len(ranks.Paths) > 0 {
// If the repository has ranking data, then store the file's rank.
pathRank := ranks.rank(keyFullPath)
pathRanks = []float64{pathRank}
}

return zoekt.Document{
SubRepositoryPath: key.SubRepoPath,
Name: keyFullPath,
Content: contents,
Branches: branchMap[key],
Ranks: pathRanks,
}, nil
}

func blobContents(blob *object.Blob) ([]byte, error) {
r, err := blob.Reader()
if err != nil {
Expand Down
Loading