Add list of compiled regexps to /rest/ignores (fixes #942)

This commit is contained in:
Jakob Borg 2014-11-08 22:12:18 +01:00
parent ae0e56e98d
commit 2449723a1c
3 changed files with 27 additions and 7 deletions

View File

@ -476,13 +476,15 @@ func restGetIgnores(m *model.Model, w http.ResponseWriter, r *http.Request) {
qs := r.URL.Query()
w.Header().Set("Content-Type", "application/json; charset=utf-8")
ignores, err := m.GetIgnores(qs.Get("folder"))
ignores, patterns, err := m.GetIgnores(qs.Get("folder"))
if err != nil {
http.Error(w, err.Error(), 500)
return
}
json.NewEncoder(w).Encode(map[string][]string{
"ignore": ignores,
"patterns": patterns,
})
}

View File

@ -118,6 +118,19 @@ func (m *Matcher) Match(file string) (result bool) {
return false
}
// Patterns return a list of the loaded regexp patterns, as strings
func (m *Matcher) Patterns() []string {
patterns := make([]string, len(m.patterns))
for i, pat := range m.patterns {
if pat.include {
patterns[i] = pat.match.String()
} else {
patterns[i] = "(?exclude)" + pat.match.String()
}
}
return patterns
}
func loadIgnoreFile(file string, seen map[string]bool) (*Matcher, error) {
if seen[file] {
return nil, fmt.Errorf("Multiple include of ignore file %q", file)

View File

@ -730,23 +730,23 @@ func (m *Model) ConnectedTo(deviceID protocol.DeviceID) bool {
return ok
}
func (m *Model) GetIgnores(folder string) ([]string, error) {
func (m *Model) GetIgnores(folder string) ([]string, []string, error) {
var lines []string
m.fmut.RLock()
cfg, ok := m.folderCfgs[folder]
m.fmut.RUnlock()
if !ok {
return lines, fmt.Errorf("Folder %s does not exist", folder)
return lines, nil, fmt.Errorf("Folder %s does not exist", folder)
}
fd, err := os.Open(filepath.Join(cfg.Path, ".stignore"))
if err != nil {
if os.IsNotExist(err) {
return lines, nil
return lines, nil, nil
}
l.Warnln("Loading .stignore:", err)
return lines, err
return lines, nil, err
}
defer fd.Close()
@ -755,7 +755,12 @@ func (m *Model) GetIgnores(folder string) ([]string, error) {
lines = append(lines, strings.TrimSpace(scanner.Text()))
}
return lines, nil
var patterns []string
if matcher := m.folderIgnores[folder]; matcher != nil {
patterns = matcher.Patterns()
}
return lines, patterns, nil
}
func (m *Model) SetIgnores(folder string, content []string) error {