mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-18 18:04:17 +00:00
ReadMatcher splits on "\n" and parseRule/Compile only trimmed " \t", so a
.skillignore saved with CRLF line endings compiled every rule with a
trailing \r that could never equal a path segment. status still reported
the patterns as loaded, so the file silently ignored nothing. The same
trim in write.go made HasPattern miss existing entries, AddPattern
duplicate them and RemovePattern fail to find them.
The pre-Matcher ReadPatterns used TrimSpace and handled CRLF; this
regressed in 4214cb10 (v0.17.4). Trim \r alongside the existing
whitespace, matching how git reads CRLF .gitignore files.
Refs #275
84 lines
1.9 KiB
Go
84 lines
1.9 KiB
Go
package skillignore
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
// AddPattern appends a pattern to a .skillignore file.
|
|
// Creates the file (and parent dirs) if it doesn't exist.
|
|
// Returns true if the pattern was added, false if it already existed.
|
|
func AddPattern(filePath, pattern string) (bool, error) {
|
|
if err := os.MkdirAll(filepath.Dir(filePath), 0755); err != nil {
|
|
return false, err
|
|
}
|
|
|
|
existing, _ := os.ReadFile(filePath)
|
|
content := string(existing)
|
|
|
|
// Check for duplicate in a single pass
|
|
for _, line := range strings.Split(content, "\n") {
|
|
if strings.TrimRight(line, " \t\r") == pattern {
|
|
return false, nil
|
|
}
|
|
}
|
|
|
|
if len(content) > 0 && !strings.HasSuffix(content, "\n") {
|
|
content += "\n"
|
|
}
|
|
content += pattern + "\n"
|
|
|
|
return true, os.WriteFile(filePath, []byte(content), 0644)
|
|
}
|
|
|
|
// RemovePattern removes all lines matching the exact pattern from a .skillignore file.
|
|
// Returns true if the pattern was found and removed, false if not found.
|
|
func RemovePattern(filePath, pattern string) (bool, error) {
|
|
data, err := os.ReadFile(filePath)
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
return false, nil
|
|
}
|
|
return false, err
|
|
}
|
|
|
|
lines := strings.Split(string(data), "\n")
|
|
var kept []string
|
|
found := false
|
|
for _, line := range lines {
|
|
trimmed := strings.TrimRight(line, " \t\r")
|
|
if trimmed == pattern {
|
|
found = true
|
|
continue
|
|
}
|
|
kept = append(kept, line)
|
|
}
|
|
|
|
if !found {
|
|
return false, nil
|
|
}
|
|
|
|
result := strings.Join(kept, "\n")
|
|
for strings.HasSuffix(result, "\n\n") {
|
|
result = strings.TrimSuffix(result, "\n")
|
|
}
|
|
|
|
return true, os.WriteFile(filePath, []byte(result), 0644)
|
|
}
|
|
|
|
// HasPattern returns true if the exact pattern exists in a .skillignore file.
|
|
func HasPattern(filePath, pattern string) bool {
|
|
data, err := os.ReadFile(filePath)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
for _, line := range strings.Split(string(data), "\n") {
|
|
if strings.TrimRight(line, " \t\r") == pattern {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|