aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/format/gitignore/dir.go
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2017-06-19 10:00:49 +0200
committerGitHub <noreply@github.com>2017-06-19 10:00:49 +0200
commit93633b5767b0d571bedc724364209d30f96a7b17 (patch)
treec53170875387107cbf9945986728f6fb115054b9 /plumbing/format/gitignore/dir.go
parent5d1c674ea818ea534476ed0ca5436e0b513f86a4 (diff)
parent2f4ac21bad4c14b860a7d5c9d761857cb8d4f89c (diff)
downloadgo-git-93633b5767b0d571bedc724364209d30f96a7b17.tar.gz
Merge pull request #429 from silvertern/gitignore
Adds .gitignore support
Diffstat (limited to 'plumbing/format/gitignore/dir.go')
-rw-r--r--plumbing/format/gitignore/dir.go50
1 files changed, 50 insertions, 0 deletions
diff --git a/plumbing/format/gitignore/dir.go b/plumbing/format/gitignore/dir.go
new file mode 100644
index 0000000..16e4617
--- /dev/null
+++ b/plumbing/format/gitignore/dir.go
@@ -0,0 +1,50 @@
+package gitignore
+
+import (
+ "io/ioutil"
+ "strings"
+
+ "gopkg.in/src-d/go-billy.v2"
+)
+
+const (
+ commentPrefix = "#"
+ eol = "\n"
+ gitDir = ".git"
+ gitignoreFile = ".gitignore"
+)
+
+// ReadPatterns reads gitignore patterns recursively traversing through the directory
+// structure. The result is in the ascending order of priority (last higher).
+func ReadPatterns(fs billy.Filesystem, path []string) (ps []Pattern, err error) {
+ if f, err := fs.Open(fs.Join(append(path, gitignoreFile)...)); err == nil {
+ defer f.Close()
+ if data, err := ioutil.ReadAll(f); err == nil {
+ for _, s := range strings.Split(string(data), eol) {
+ if !strings.HasPrefix(s, commentPrefix) && len(strings.TrimSpace(s)) > 0 {
+ ps = append(ps, ParsePattern(s, path))
+ }
+ }
+ }
+ }
+
+ var fis []billy.FileInfo
+ fis, err = fs.ReadDir(fs.Join(path...))
+ if err != nil {
+ return
+ }
+ for _, fi := range fis {
+ if fi.IsDir() && fi.Name() != gitDir {
+ var subps []Pattern
+ subps, err = ReadPatterns(fs, append(path, fi.Name()))
+ if err != nil {
+ return
+ }
+ if len(subps) > 0 {
+ ps = append(ps, subps...)
+ }
+ }
+ }
+
+ return
+}