-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitignorer.go
39 lines (33 loc) · 1.05 KB
/
gitignorer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package gitutil
import (
"path/filepath"
"github.com/denormal/go-gitignore"
"github.com/iver-wharf/wharf-cmd/internal/ignorer"
)
// NewIgnorer creates a new ignorer that checks if a file should be ignored or
// not depending on the .gitignore files found inside the repository.
func NewIgnorer(currentDir, repoRoot string) (ignorer.Ignorer, error) {
repo, err := gitignore.NewRepository(repoRoot)
if err != nil {
return nil, err
}
return &gitIgnorer{currentDir, repo}, nil
}
type matcher interface {
Match(path string) gitignore.Match
}
type gitIgnorer struct {
currentDir string
matcher matcher
}
func (i *gitIgnorer) Ignore(relPath string) bool {
// NOTE: gitignore.GitIgnore has a .Ignore function, but that function
// isn't implemented on the repository level. Therefore we need to
// re-implement the gitignore.GitIgnore.Ignore() function here via the
// .Match function that is implemented on the repository level.
match := i.matcher.Match(filepath.Join(i.currentDir, relPath))
if match == nil {
return false
}
return match.Ignore()
}