-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathgit_ignore.go
More file actions
72 lines (60 loc) · 1.6 KB
/
git_ignore.go
File metadata and controls
72 lines (60 loc) · 1.6 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Package utils
package utils
import (
"os"
"os/exec"
"path/filepath"
"strings"
ignore "github.com/sabhiram/go-gitignore"
)
type GitIgnore struct {
baseDir string
localGitIgnore *ignore.GitIgnore
globalGitIgnore *ignore.GitIgnore
gitInfoExclude *ignore.GitIgnore
}
func NewGitIgnore(baseDir string) GitIgnore {
return GitIgnore{
baseDir: baseDir,
localGitIgnore: compileIgnorer(filepath.Join(baseDir, ".gitignore")),
globalGitIgnore: compileIgnorer(getGlobalGitIgnorePath()),
gitInfoExclude: compileIgnorer(filepath.Join(baseDir, ".git", "info", "exclude")),
}
}
func (i GitIgnore) SkipFile(path string) (bool, error) {
// Never skip the .git directory or files inside it, even if matched by .gitignore patterns.
if path == ".git" ||
strings.HasPrefix(path, ".git/") ||
strings.HasSuffix(path, "/.git") ||
strings.Contains(path, "/.git/") {
return false, nil
}
for _, ignorer := range []*ignore.GitIgnore{i.localGitIgnore, i.globalGitIgnore, i.gitInfoExclude} {
if ignorer != nil && ignorer.MatchesPath(path) {
return true, nil
}
}
return false, nil
}
func compileIgnorer(path string) *ignore.GitIgnore {
ignorer, err := ignore.CompileIgnoreFile(path)
if err != nil {
return nil
}
return ignorer
}
func getGlobalGitIgnorePath() string {
output, err := exec.Command("git", "config", "--get", "core.excludesfile").Output()
if err != nil {
return ""
}
path := strings.TrimSpace(string(output))
if strings.HasPrefix(path, "~") {
homeDir, err := os.UserHomeDir()
if err != nil {
return ""
}
path = filepath.Join(homeDir, path[2:])
}
return path
}