Skip to content

Commit

Permalink
Refactor gitignore
Browse files Browse the repository at this point in the history
  • Loading branch information
caitlinelfring committed Aug 25, 2020
1 parent 4722767 commit 8b6b492
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 23 deletions.
32 changes: 9 additions & 23 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package config

import (
"io/ioutil"
"strings"

"github.com/caitlinelfring/woke/pkg/ignore"
"github.com/caitlinelfring/woke/pkg/rule"
"github.com/caitlinelfring/woke/pkg/util"

gitignore "github.com/sabhiram/go-gitignore"
"gopkg.in/yaml.v2"
)

Expand All @@ -16,15 +15,11 @@ type Config struct {
Rules []*rule.Rule `yaml:"rules"`
IgnoreFiles []string `yaml:"ignore_files"`

_gitIgnore *gitignore.GitIgnore
hasAbsolutePath bool

files []string
}
ignoreMatcherFunc func(string) bool

// DefaultIgnore is the default list of file globs that will be ignored
var DefaultIgnore = []string{
".git",
files []string
}

func NewConfig(filename string) (*Config, error) {
Expand All @@ -38,8 +33,11 @@ func NewConfig(filename string) (*Config, error) {
c.AddDefaultRules()

// Ignore the config filename, it will always match on its own rules
c.IgnoreFiles = append(c.IgnoreFiles, filename)
c.compileIgnoreGlobs()
ignorer, err := ignore.NewIgnore(append(c.IgnoreFiles, filename))
if err != nil {
return &c, err
}
c.ignoreMatcherFunc = ignorer.Match

return &c, nil
}
Expand All @@ -49,25 +47,13 @@ func (c *Config) SetFiles(fileGlobs []string) {
allFiles, hasAbsolutePath, _ := util.GetFilesInGlobs(fileGlobs)
c.hasAbsolutePath = hasAbsolutePath
for _, f := range allFiles {
if c._gitIgnore.MatchesPath(f) {
if c.ignoreMatcherFunc(f) {
continue
}
c.files = append(c.files, f)
}
}

// compileIgnoreGlobs pre-compiles globs
// See https://github.com/gobwas/glob#performance
func (c *Config) compileIgnoreGlobs() {
ignoreLines := []string{}
if buffer, err := ioutil.ReadFile(".gitignore"); err == nil {
ignoreLines = append(ignoreLines, strings.Split(string(buffer), "\n")...)
}
ignoreLines = append(ignoreLines, c.IgnoreFiles...)
ignoreLines = append(ignoreLines, DefaultIgnore...)
c._gitIgnore, _ = gitignore.CompileIgnoreLines(ignoreLines...)
}

// GetFiles returns files that may be parsed
func (c *Config) GetFiles() []string {
return c.files
Expand Down
42 changes: 42 additions & 0 deletions pkg/ignore/ignore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package ignore

import (
"io/ioutil"
"strings"

gitignore "github.com/sabhiram/go-gitignore"
)

// DefaultIgnores is the default list of file globs that will be ignored
var DefaultIgnores = []string{
".git",
}

type Ignore struct {
compiled *gitignore.GitIgnore
}

// NewIgnore produces an Ignore object, with compiled lines from .gitignore and DefaultIgnores
// which you can match files against
func NewIgnore(lines []string) (*Ignore, error) {
compiled, err := compileIgnoreLines(lines)
if err != nil {
return nil, err
}
return &Ignore{compiled: compiled}, nil
}

// Match returns true if the provided file matches any of the defined ignores
func (i *Ignore) Match(f string) bool {
return i.compiled.MatchesPath(f)
}

func compileIgnoreLines(lines []string) (*gitignore.GitIgnore, error) {
lines = append(lines, DefaultIgnores...)

if buffer, err := ioutil.ReadFile(".gitignore"); err == nil {
lines = append(lines, strings.Split(string(buffer), "\n")...)
}

return gitignore.CompileIgnoreLines(lines...)
}

0 comments on commit 8b6b492

Please sign in to comment.