This repository was archived by the owner on Jan 16, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 140
/
Copy pathparseignore.go
87 lines (74 loc) · 2.13 KB
/
parseignore.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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package parsecmd
import (
"bytes"
"os"
"path/filepath"
"strings"
"github.com/ParsePlatform/parse-cli/parsecli"
"github.com/facebookgo/parseignore"
"github.com/facebookgo/symwalk"
)
func ignoreErrors(errors []error, e *parsecli.Env) string {
l := len(errors)
var b bytes.Buffer
for n, err := range errors {
b.WriteString(parsecli.ErrorString(e, err))
if n != l-1 {
b.WriteString("\n")
}
}
return b.String()
}
type legacyRulesMatcher struct{}
func (l legacyRulesMatcher) Match(path string, fi os.FileInfo) (parseignore.Decision, error) {
parts := strings.Split(path, string(filepath.Separator))
for _, part := range parts {
if strings.HasPrefix(part, ".") ||
strings.HasPrefix(part, "#") ||
strings.HasSuffix(part, ".swp") ||
strings.HasSuffix(part, "~") {
return parseignore.Exclude, nil
}
}
return parseignore.Pass, nil
}
func parseIgnoreMatcher(content []byte) (parseignore.Matcher, []error) {
if content != nil {
matcher, errors := parseignore.CompilePatterns(content)
return parseignore.MultiMatcher(matcher, legacyRulesMatcher{}), errors
}
return legacyRulesMatcher{}, nil
}
// parseIgnoreWalk is a helper function user by the Parse CLI. It walks the given
// root path (traversing symbolic links if any) and calls walkFn only
// on files that were not ignored by the given Matcher.
// Note: It ignores any errors encountered during matching
func parseIgnoreWalk(matcher parseignore.Matcher,
root string,
walkFn filepath.WalkFunc) ([]error, error) {
var errors []error
ignoresWalkFn := func(path string, info os.FileInfo, err error) error {
// if root==path relPath="." which is ignored by legacyRule
// hence the special handling of root
if err != nil || root == path {
return walkFn(path, info, err)
}
relPath, err := filepath.Rel(root, path)
if err != nil {
return err
}
exclude, err := matcher.Match(relPath, info)
if err != nil {
errors = append(errors, err)
return nil
}
if exclude == parseignore.Exclude {
if info.IsDir() {
return filepath.SkipDir
}
return nil
}
return walkFn(path, info, err)
}
return errors, symwalk.Walk(root, ignoresWalkFn)
}