-
Notifications
You must be signed in to change notification settings - Fork 1
/
walk.go
110 lines (97 loc) · 2.27 KB
/
walk.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"
"golang.org/x/tools/godoc/util"
"golang.org/x/tools/godoc/vfs"
"gopkg.in/src-d/go-billy.v4"
"gopkg.in/src-d/go-git.v4/plumbing/format/gitignore"
)
type Visitor func(cru *Cru, path string) error
type GitIgnorePatterns []gitignore.Pattern
func (i GitIgnorePatterns) Ignore(filename string, isDir bool) bool {
path := strings.Split(filename, "/")
for _, pattern := range i {
r := pattern.Match(path, isDir)
if r == gitignore.Exclude {
return true
}
}
return false
}
type BillyVFS struct {
fs billy.Filesystem
}
func (this BillyVFS) Open(name string) (vfs.ReadSeekCloser, error) {
return this.fs.Open(name)
}
func (c *Cru) walkFn(visitor Visitor, filename string, info os.FileInfo) error {
if info.IsDir() {
var patterns GitIgnorePatterns
patterns, err := gitignore.ReadPatterns(*c.filesystem, strings.Split(filename, "/"))
if err != nil {
return err
}
if patterns.Ignore(filename, info.IsDir()) {
return nil
}
if filepath.Base(filename) == ".git" {
return nil
}
dir, err := (*c.filesystem).ReadDir(filename)
if err != nil {
return err
}
for _, f := range dir {
childPath := path.Join(filename, f.Name())
childInfo, err := (*c.filesystem).Stat(childPath)
if err != nil {
return err
}
if !patterns.Ignore(childPath, childInfo.IsDir()) {
err = c.walkFn(visitor, childPath, childInfo)
if err != nil {
return err
}
}
}
} else {
fs := BillyVFS{*c.filesystem}
if util.IsTextFile(fs, filename) {
return visitor(c, filename)
}
}
return nil
}
func (c *Cru) AbsPath(filename string) string {
if filepath.IsAbs(filename) {
return filename
}
return filepath.Clean(filepath.Join(c.cwd, filename))
}
func (c *Cru) RelPath(filename string) string {
if !filepath.IsAbs(filename) {
return filename
}
if rel, err := filepath.Rel(c.cwd, filename); err == nil {
return rel
}
return filename
}
func (c *Cru) Walk(visitor Visitor) (err error) {
for _, p := range c.Path {
filename := c.AbsPath(p)
info, err := (*c.filesystem).Stat(filename)
if err != nil {
return fmt.Errorf("%s is not a valid path or is not readable, %s", p, err)
}
err = c.walkFn(visitor, filename, info)
if err != nil {
return err
}
}
return nil
}