-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitignore.go
40 lines (34 loc) · 909 Bytes
/
gitignore.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
40
package gitignore
import (
"io/fs"
"os"
"path/filepath"
"strings"
gitignore "github.com/sabhiram/go-gitignore"
)
var alwaysIgnore = []string{
"node_modules",
".git",
".DS_Store",
}
var defaultIgnore = gitignore.CompileIgnoreLines(alwaysIgnore...).MatchesPath
func FromFS(fsys fs.FS) (ignore func(path string) bool) {
code, err := fs.ReadFile(fsys, ".gitignore")
if err != nil {
return defaultIgnore
}
lines := strings.Split(string(code), "\n")
lines = append(lines, alwaysIgnore...)
ignorer := gitignore.CompileIgnoreLines(lines...)
return ignorer.MatchesPath
}
func From(dir string) (ignore func(path string) bool) {
code, err := os.ReadFile(filepath.Join(dir, ".gitignore"))
if err != nil {
return defaultIgnore
}
lines := strings.Split(string(code), "\n")
lines = append(lines, alwaysIgnore...)
ignorer := gitignore.CompileIgnoreLines(lines...)
return ignorer.MatchesPath
}