-
Notifications
You must be signed in to change notification settings - Fork 123
/
path_utils.go
92 lines (74 loc) · 1.98 KB
/
path_utils.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
88
89
90
91
92
package gitbase
import (
"path/filepath"
"regexp"
"strings"
git "gopkg.in/src-d/go-git.v4"
)
// RegMatchChars matches a string with a glob expression inside.
var RegMatchChars = regexp.MustCompile(`(^|[^\\])([*[?])`)
// StripPrefix removes the root path from the given path. Root may be a glob.
// The returned string has all backslashes replaced with slashes.
func StripPrefix(root, path string) (string, error) {
var err error
root, err = filepath.Abs(cleanGlob(root))
if err != nil {
return "", err
}
if !strings.HasSuffix(root, "/") {
root += string(filepath.Separator)
}
path, err = filepath.Abs(path)
if err != nil {
return "", err
}
return strings.TrimPrefix(filepath.ToSlash(path), root), nil
}
// cleanGlob removes all the parts of a glob that are not fixed. It also
// converts all slashes or backslashes to /.
func cleanGlob(pattern string) string {
pattern = filepath.ToSlash(pattern)
var parts []string
for _, part := range strings.Split(pattern, "/") {
if strings.ContainsAny(part, "*?[\\") {
break
}
parts = append(parts, part)
}
return strings.Join(parts, "/")
}
// PatternMatches returns the paths matched and any error found.
func PatternMatches(pattern string) ([]string, error) {
abs, err := filepath.Abs(pattern)
if err != nil {
return nil, err
}
matches, err := filepath.Glob(abs)
if err != nil {
return nil, err
}
return removeDsStore(matches), nil
}
func removeDsStore(matches []string) []string {
var result []string
for _, m := range matches {
if filepath.Base(m) != ".DS_Store" {
result = append(result, m)
}
}
return result
}
// IsGitRepo checks that the given path is a git repository.
func IsGitRepo(path string) (bool, error) {
if _, err := git.PlainOpen(path); err != nil {
if git.ErrRepositoryNotExists == err {
return false, nil
}
return false, err
}
return true, nil
}
//IsSivaFile checks that the given file is a siva file.
func IsSivaFile(file string) bool {
return strings.HasSuffix(file, ".siva")
}