Forráskód Böngészése

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

Jakob Borg 11 éve
szülő
commit
2449723a1c
3 módosított fájl, 27 hozzáadás és 7 törlés
  1. 4 2
      cmd/syncthing/gui.go
  2. 13 0
      internal/ignore/ignore.go
  3. 10 5
      internal/model/model.go

+ 4 - 2
cmd/syncthing/gui.go

@@ -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,
+		"ignore":   ignores,
+		"patterns": patterns,
 	})
 }
 

+ 13 - 0
internal/ignore/ignore.go

@@ -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)

+ 10 - 5
internal/model/model.go

@@ -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 {