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

Attempt auto-generated files being hidden by default #421

Merged
merged 6 commits into from
Aug 29, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ui/bindata.go linguist-generated=true
50 changes: 36 additions & 14 deletions index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ type Index struct {
}

type IndexOptions struct {
ExcludeDotFiles bool
SpecialFiles []string
ExcludeDotFiles bool
SpecialFiles []string
AutoGeneratedFilePatterns []string
}

type SearchOptions struct {
Expand Down Expand Up @@ -66,8 +67,9 @@ type SearchResponse struct {
}

type FileMatch struct {
Filename string
Matches []*Match
Filename string
Matches []*Match
AutoGenerated bool
}

type ExcludedFile struct {
Expand All @@ -76,10 +78,11 @@ type ExcludedFile struct {
}

type IndexRef struct {
Url string
Rev string
Time time.Time
dir string
Url string
Rev string
Time time.Time
dir string
AutoGeneratedFilePatterns []string
}

func (r *IndexRef) Dir() string {
Expand Down Expand Up @@ -182,6 +185,20 @@ func (n *Index) Search(pat string, opt *SearchOptions) (*SearchResponse, error)
}
}

var autoGeneratedFre *regexp.Regexp
if len(n.Ref.AutoGeneratedFilePatterns) > 0 {
var pattern, sep string
for _, fp := range n.Ref.AutoGeneratedFilePatterns {
pattern += sep + "(" + fp + ")"
sep = "|"
}
autoGeneratedFre, err = regexp.Compile(pattern)
if err != nil {
return nil, err
}
}


files := n.idx.PostingQuery(index.RegexpQuery(re.Syntax))
for _, file := range files {
var matches []*Match
Expand Down Expand Up @@ -231,9 +248,13 @@ func (n *Index) Search(pat string, opt *SearchOptions) (*SearchResponse, error)
filesFound++
if len(matches) > 0 {
filesCollected++

autoGenerated := autoGeneratedFre != nil && autoGeneratedFre.MatchString(name, true, true) > 0

results = append(results, &FileMatch{
Filename: name,
Matches: matches,
Filename: name,
Matches: matches,
AutoGenerated: autoGenerated,
})
}
}
Expand Down Expand Up @@ -485,10 +506,11 @@ func Build(opt *IndexOptions, dst, src, url, rev string) (*IndexRef, error) {
}

r := &IndexRef{
Url: url,
Rev: rev,
Time: time.Now(),
dir: dst,
Url: url,
Rev: rev,
Time: time.Now(),
dir: dst,
AutoGeneratedFilePatterns: opt.AutoGeneratedFilePatterns,
}

if err := r.writeManifest(); err != nil {
Expand Down
10 changes: 6 additions & 4 deletions searcher/searcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,16 +407,18 @@ func newSearcher(
return nil, err
}

opt := &index.IndexOptions{
ExcludeDotFiles: repo.ExcludeDotFiles,
SpecialFiles: wd.SpecialFiles(),
}

rev, err := wd.PullOrClone(vcsDir, repo.Url)
if err != nil {
return nil, err
}

opt := &index.IndexOptions{
ExcludeDotFiles: repo.ExcludeDotFiles,
SpecialFiles: wd.SpecialFiles(),
AutoGeneratedFilePatterns: wd.AutoGeneratedFilePatterns(vcsDir),
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to clarify, you swapped the order of this block so that we don't create opt if there's an error pulling?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah i swapped it because i needed wd.PullOrClone to be executed first so that when wd.AutoGeneratedFilePatterns(vcsDir) is called, it can find the .gitattributes file. wd.PullOrClone will populate the directory so without it, the first run of this flow will not see accurate results for autogenerated files

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh gotcha, that makes sense.


var idxDir string
ref := refs.find(repo.Url, rev)
if ref == nil {
Expand Down
8 changes: 5 additions & 3 deletions ui/assets/js/hound.js
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ var ContentFor = function(line, regexp) {

var FileContentView = React.createClass({
getInitialState: function() {
return { open: true };
return { open: !this.props.isAutoGenerated };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be nice to put an [AUTOGENERATED] badge next to the file path. That's not a blocker of course, but it would be a nice visual indication about why the file is collapsed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea, will do!

},
toggleContent: function() {
this.state.open ? this.closeContent(): this.openContent();
Expand Down Expand Up @@ -763,7 +763,8 @@ var FilesView = React.createClass({
rev={rev}
fileName={match.Filename}
blocks={CoalesceMatches(match.Matches)}
regexp={regexp}/>
regexp={regexp}
isAutoGenerated={match.AutoGenerated}/>
});


Expand Down Expand Up @@ -886,7 +887,8 @@ var ResultView = React.createClass({
rev={result.Rev}
repo={result.Repo}
regexp={regexp}
files={result.FilesWithMatch}/>
files={result.FilesWithMatch}
/>
);
});
var actions = '';
Expand Down
50 changes: 25 additions & 25 deletions ui/bindata.go

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions vcs/bzr.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,7 @@ func (g *BzrDriver) SpecialFiles() []string {
".bzr",
}
}

func (g *BzrDriver) AutoGeneratedFilePatterns(dir string) []string {
return []string{}
}
26 changes: 26 additions & 0 deletions vcs/git.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package vcs

import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
Expand All @@ -15,6 +17,7 @@ import (
const defaultRef = "master"

var headBranchRegexp = regexp.MustCompile(`HEAD branch: (?P<branch>.+)`)
var autoGeneratedFileRegexp = regexp.MustCompile(`(?P<path>.+) linguist-generated=true`)

func init() {
Register(newGit, "git")
Expand Down Expand Up @@ -151,6 +154,29 @@ func (g *GitDriver) SpecialFiles() []string {
}
}

func (g *GitDriver) AutoGeneratedFilePatterns(dir string) []string {
var filePatterns []string
path := filepath.Join(dir, ".gitattributes")

file, err := os.Open(path)
if err != nil {
return filePatterns
}
defer file.Close()

scanner := bufio.NewScanner(file)
for scanner.Scan() {
matches := autoGeneratedFileRegexp.FindStringSubmatch(scanner.Text())
if len(matches) == 2 {
pattern := strings.ReplaceAll(matches[1], "**", "*")
pattern = strings.ReplaceAll(pattern, "*", ".*")
filePatterns = append(filePatterns, pattern)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Could you add a test for this pattern-to-regex conversion?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah i might change this logic (see my comment about regexes and using direct file paths instead). but either way, i will add a test for the code added in this file!

}
}

return filePatterns
}

func (d *headBranchDetector) detectRef(dir string) string {
output, err := run("git show remote info", dir,
"git",
Expand Down
4 changes: 4 additions & 0 deletions vcs/hg.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,7 @@ func (g *MercurialDriver) SpecialFiles() []string {
".hg",
}
}

func (g *MercurialDriver) AutoGeneratedFilePatterns(dir string) []string {
return []string{}
}
4 changes: 4 additions & 0 deletions vcs/svn.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,7 @@ func (g *SVNDriver) SpecialFiles() []string {
".svn",
}
}

func (g *SVNDriver) AutoGeneratedFilePatterns(dir string) []string {
return []string{}
}
4 changes: 4 additions & 0 deletions vcs/vcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ type Driver interface {

// Return a list of special filenames that should not be indexed.
SpecialFiles() []string

// Return a list of file path patterns that are marked as auto-generated.
AutoGeneratedFilePatterns(dir string) []string

}

// An API to interact with a vcs working directory. This is
Expand Down