This repository has been archived by the owner on Mar 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsync_ignore.go
86 lines (81 loc) · 1.62 KB
/
sync_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
package main
import (
"os"
"path/filepath"
"strings"
"io/ioutil"
"github.com/monochromegane/go-gitignore"
)
const IGNORE_FILENAME = ".syncignore"
type SyncIgnore struct {
rootDir string
base string
ignoreMatcher gitignore.IgnoreMatcher
}
func NewSyncIgnore(rootDir, base string) (*SyncIgnore, error) {
syncIgnore := &SyncIgnore{
rootDir: rootDir,
base: base,
}
err := syncIgnore.Load()
if err != nil {
return nil, err
}
return syncIgnore, nil
}
func (i SyncIgnore) getFile() (*os.File, error) {
rootDir, err := filepath.Abs(i.rootDir)
if err != nil {
return nil, err
}
if !strings.HasSuffix(rootDir, string(os.PathSeparator)) {
rootDir += string(os.PathSeparator)
}
pathIgnoreFile := rootDir + IGNORE_FILENAME
exists, err := FileExists(pathIgnoreFile)
if err != nil {
return nil, err
}
if exists {
return os.Open(pathIgnoreFile)
}
exists, err = FileExists(IGNORE_FILENAME)
if err != nil {
return nil, err
}
if !exists {
return nil, nil
}
f, err := os.Create(pathIgnoreFile)
if err != nil {
return nil, err
}
defer f.Close()
b, err := ioutil.ReadFile(IGNORE_FILENAME)
if err != nil {
return nil, err
}
_, err = f.WriteString(string(b))
if err != nil {
return nil, err
}
return os.Open(IGNORE_FILENAME)
}
func (i *SyncIgnore) Load() error {
f, err := i.getFile()
if err != nil {
return err
}
if f == nil {
return nil
}
defer f.Close()
i.ignoreMatcher = gitignore.NewGitIgnoreFromReader(i.base, f)
return nil
}
func (i SyncIgnore) Match(pathfile string, isDir bool) bool {
if i.ignoreMatcher == nil {
return false
}
return i.ignoreMatcher.Match(pathfile, isDir)
}