Skip to content

Commit

Permalink
Dot files should not be excluded from indexing by default.
Browse files Browse the repository at this point in the history
 1. Add exclude-dot-files directive to config.
 2. Vcs plugins must now expose their "special" directories so they are not
indexed.
  • Loading branch information
kellegous committed Apr 15, 2015
1 parent 88a2be6 commit e113029
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 12 deletions.
3 changes: 2 additions & 1 deletion config-example.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
},
"AnotherGitRepo" : {
"url" : "https://www.github.com/YourOrganization/RepoOne.git",
"ms-between-poll": 10000
"ms-between-poll": 10000,
"excluded-dot-files": true
},
"SomeMercurialRepo" : {
"url" : "https://www.example.com/foo/hg",
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Repo struct {
Vcs string `json:"vcs"`
VcsConfigMessage *SecretMessage `json:"vcs-config"`
UrlPattern *UrlPattern `json:"url-pattern"`
ExcludeDotFiles bool `json:"exclude-dot-files"`
}

type Config struct {
Expand Down
53 changes: 46 additions & 7 deletions index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,23 @@ const (
manifestFilename = "metadata.gob"
)

const (
reasonDotFile = "Dot files are excluded."
reasonInvalidMode = "Invalid file mode."
reasonNotText = "Not a text file."
)

type Index struct {
Ref *IndexRef
idx *index.Index
lck sync.RWMutex
}

type IndexOptions struct {
ExcludeDotFiles bool
SpecialFiles []string
}

type SearchOptions struct {
IgnoreCase bool
LinesOfContext uint
Expand Down Expand Up @@ -270,7 +281,16 @@ func addDirToIndex(dst, src, path string) error {
return os.Mkdir(dup, os.ModePerm)
}

func indexAllFiles(dst, src string) error {
func isSpecialFile(specialFiles []string, name string) bool {
for _, file := range specialFiles {
if name == file {
return true
}
}
return false
}

func indexAllFiles(opt *IndexOptions, dst, src string) error {
ix := index.Create(filepath.Join(dst, "tri"))
var excluded []*ExcludedFile

Expand All @@ -288,11 +308,24 @@ func indexAllFiles(dst, src string) error {
return err
}

if name[0] == '.' {
// Is this file considered "special", this means it's not even a part
// of the source repository (like .git or .svn).
if isSpecialFile(opt.SpecialFiles, name) {
if info.IsDir() {
return filepath.SkipDir
}
excluded = append(excluded, &ExcludedFile{rel, "Dot file"})
return nil
}

if opt.ExcludeDotFiles && name[0] == '.' {
if info.IsDir() {
return filepath.SkipDir
}

excluded = append(excluded, &ExcludedFile{
rel,
reasonDotFile,
})
return nil
}

Expand All @@ -301,7 +334,10 @@ func indexAllFiles(dst, src string) error {
}

if info.Mode()&os.ModeType != 0 {
excluded = append(excluded, &ExcludedFile{rel, "Invalid Mode"})
excluded = append(excluded, &ExcludedFile{
rel,
reasonInvalidMode,
})
return nil
}

Expand All @@ -311,7 +347,10 @@ func indexAllFiles(dst, src string) error {
}

if !txt {
excluded = append(excluded, &ExcludedFile{rel, "Not a text file"})
excluded = append(excluded, &ExcludedFile{
rel,
reasonNotText,
})
return nil
}

Expand Down Expand Up @@ -359,7 +398,7 @@ func Read(dir string) (*IndexRef, error) {
return m, nil
}

func Build(dst, src, url, rev string) (*IndexRef, error) {
func Build(opt *IndexOptions, dst, src, url, rev string) (*IndexRef, error) {
if _, err := os.Stat(dst); err != nil {
if err := os.MkdirAll(dst, os.ModePerm); err != nil {
return nil, err
Expand All @@ -370,7 +409,7 @@ func Build(dst, src, url, rev string) (*IndexRef, error) {
return nil, err
}

if err := indexAllFiles(dst, src); err != nil {
if err := indexAllFiles(opt, dst, src); err != nil {
return nil, err
}

Expand Down
4 changes: 3 additions & 1 deletion index/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ func buildIndex(url, rev string) (*IndexRef, error) {
return nil, err
}

return Build(dir, thisDir(), url, rev)
var opt IndexOptions

return Build(&opt, dir, thisDir(), url, rev)
}

func TestSearch(t *testing.T) {
Expand Down
24 changes: 21 additions & 3 deletions searcher/searcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,15 @@ func findExistingRefs(dbpath string) (*foundRefs, error) {
// Open an index at the given path. If the idxDir is already present, it will
// simply open and use that index. If, however, the idxDir does not exist a new
// one will be built.
func buildAndOpenIndex(dbpath, vcsDir, idxDir, url, rev string) (*index.Index, error) {
func buildAndOpenIndex(
opt *index.IndexOptions,
dbpath,
vcsDir,
idxDir,
url,
rev string) (*index.Index, error) {
if _, err := os.Stat(idxDir); err != nil {
r, err := index.Build(idxDir, vcsDir, url, rev)
r, err := index.Build(opt, idxDir, vcsDir, url, rev)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -250,6 +256,11 @@ func newSearcher(dbpath, name string, repo *config.Repo, refs *foundRefs) (*Sear
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
Expand All @@ -264,7 +275,13 @@ func newSearcher(dbpath, name string, repo *config.Repo, refs *foundRefs) (*Sear
refs.claim(ref)
}

idx, err := buildAndOpenIndex(dbpath, vcsDir, idxDir, repo.Url, rev)
idx, err := buildAndOpenIndex(
opt,
dbpath,
vcsDir,
idxDir,
repo.Url,
rev)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -295,6 +312,7 @@ func newSearcher(dbpath, name string, repo *config.Repo, refs *foundRefs) (*Sear

log.Printf("Rebuilding %s for %s", name, newRev)
idx, err := buildAndOpenIndex(
opt,
dbpath,
vcsDir,
nextIndexDir(dbpath),
Expand Down
6 changes: 6 additions & 0 deletions vcs/bzr.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,9 @@ func (g *BzrDriver) Clone(dir, url string) (string, error) {

return g.HeadRev(dir)
}

func (g *BzrDriver) SpecialFiles() []string {
return []string{
".bzr",
}
}
6 changes: 6 additions & 0 deletions vcs/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,9 @@ func (g *GitDriver) Clone(dir, url string) (string, error) {

return g.HeadRev(dir)
}

func (g *GitDriver) SpecialFiles() []string {
return []string{
".git",
}
}
6 changes: 6 additions & 0 deletions vcs/hg.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,9 @@ func (g *MercurialDriver) Clone(dir, url string) (string, error) {

return g.HeadRev(dir)
}

func (g *MercurialDriver) SpecialFiles() []string {
return []string{
".hg",
}
}
6 changes: 6 additions & 0 deletions vcs/svn.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,9 @@ func (g *SVNDriver) Clone(dir, url string) (string, error) {

return g.HeadRev(dir)
}

func (g *SVNDriver) SpecialFiles() []string {
return []string{
".svn",
}
}
9 changes: 9 additions & 0 deletions vcs/vcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,18 @@ var drivers = make(map[string]func(c []byte) (Driver, error))
// A "plugin" for each vcs that supports the very limited set of vcs
// operations that hound needs.
type Driver interface {

// Clone a new working directory.
Clone(dir, url string) (string, error)

// Pull new changes from the server and update the working directory.
Pull(dir string) (string, error)

// Return the revision at the head of the vcs directory.
HeadRev(dir string) (string, error)

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

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

0 comments on commit e113029

Please sign in to comment.