This repository has been archived by the owner on Dec 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjinja2_ignore.go
107 lines (89 loc) · 2.48 KB
/
jinja2_ignore.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
package jinja2
import (
"bufio"
"github.com/go-git/go-git/v5/plumbing/format/gitignore"
"os"
"path/filepath"
"strings"
)
const (
commentPrefix = "#"
templateIgnoreFile = ".templateignore"
)
// readIgnoreFile reads a specific git ignore file.
func (j *Jinja2) readIgnoreFile(path string, domainIn []string) ([]gitignore.Pattern, error) {
domain := make([]string, len(domainIn))
copy(domain, domainIn)
f, err := os.Open(path)
if err == nil {
defer f.Close()
var ret []gitignore.Pattern
scanner := bufio.NewScanner(f)
for scanner.Scan() {
s := scanner.Text()
if !strings.HasPrefix(s, commentPrefix) && len(strings.TrimSpace(s)) > 0 {
ret = append(ret, gitignore.ParsePattern(s, domain))
}
}
return ret, nil
} else if os.IsNotExist(err) {
return nil, nil
}
return nil, err
}
// readPatternsRecursive reads gitignore patterns recursively traversing through the directory
// structure. The result is in the ascending order of priority (last higher).
func (j *Jinja2) readPatternsRecursive(rootDir string, domain []string) ([]gitignore.Pattern, error) {
path := filepath.Join(rootDir, filepath.Join(domain...), templateIgnoreFile)
ps, _ := j.readIgnoreFile(path, domain)
pd := domain
if len(domain) == 0 {
pd = []string{"."}
}
fis, err := os.ReadDir(filepath.Join(rootDir, filepath.Join(pd...)))
if err != nil {
return nil, err
}
domain2 := make([]string, len(domain)+1)
copy(domain2, domain)
for _, fi := range fis {
if fi.IsDir() {
domain2[len(domain)] = fi.Name()
var subps []gitignore.Pattern
subps, err = j.readPatternsRecursive(rootDir, domain2)
if err != nil {
return nil, err
}
if len(subps) > 0 {
ps = append(ps, subps...)
}
}
}
return ps, nil
}
func (j *Jinja2) readAllIgnoreFiles(rootDir string, subdir string, excludePatterns []string) ([]gitignore.Pattern, error) {
var ret []gitignore.Pattern
var domain []string
var subDir2 string
if subdir != "" && subdir != "." {
for _, e := range strings.Split(subdir, string(filepath.Separator)) {
x, err := j.readIgnoreFile(filepath.Join(rootDir, subDir2, templateIgnoreFile), domain)
if err != nil {
return nil, err
}
ret = append(ret, x...)
subDir2 = filepath.Join(subDir2, e)
domain = append(domain, e)
}
}
x, err := j.readPatternsRecursive(rootDir, domain)
if err != nil {
return nil, err
}
ret = append(ret, x...)
for _, ep := range excludePatterns {
p := gitignore.ParsePattern(ep, domain)
ret = append(ret, p)
}
return ret, nil
}