- 🩺 `doctor --json`: structured check report for cron/monitoring; still exits non-zero on ❌ (collects checks into a slice, JSON path suppresses the human lines). - 🗑️ `conflicts --rm`: delete every `.conflict-` copy after review (lazy cleanup, not a resolver). Only touches conflict copies — real files untouched. - ⏱️ `status` shows per-mount last-sync age ("synced 12s ago" / "not synced yet") from a daemon-tracked timestamp (control.MountState.LastSyncUnix); also in --json. - 🙈 the reversed no-go: `settings.sync.ignore_defaults` (opt-in, default off) layers a built-in junk set (node_modules, .git, target, dist, build, .venv, __pycache__, .next, .DS_Store) onto the matcher — don't ship gigabytes of regenerable junk over the now-live WAN. Reuses round-1 --exclude plumbing; honored by daemon + publish. Tests: doctor --json valid+exit-code, conflicts --rm deletes only conflicts, ignore.Defaults match. go test ./... -race + vet clean.
176 lines
4.8 KiB
Go
176 lines
4.8 KiB
Go
// Package ignore implements a .devignore matcher with gitignore-compatible
|
|
// semantics (last-match-wins, negation, anchoring, ** globbing).
|
|
package ignore
|
|
|
|
import (
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
// Matcher matches forward-slash paths (relative to the project root) against a
|
|
// compiled set of .devignore patterns.
|
|
type Matcher struct {
|
|
rules []rule
|
|
}
|
|
|
|
type rule struct {
|
|
re *regexp.Regexp
|
|
negate bool
|
|
dirOnly bool
|
|
}
|
|
|
|
// Defaults are common regenerable / VCS directories that almost never belong in
|
|
// a sync share. Opt-in via settings.sync.ignore_defaults (off by default, so
|
|
// existing shares don't silently change what they sync). Gitignore syntax —
|
|
// trailing slash = directory-only — fed through the same --exclude layering.
|
|
var Defaults = []string{
|
|
".git/",
|
|
"node_modules/",
|
|
".venv/",
|
|
"__pycache__/",
|
|
"target/", // rust/java build output
|
|
"dist/",
|
|
"build/",
|
|
".next/",
|
|
".DS_Store",
|
|
}
|
|
|
|
// Compile parses the lines of a .devignore file into a Matcher (case-sensitive,
|
|
// matching gitignore semantics).
|
|
func Compile(patterns []string) (*Matcher, error) { return compile(patterns, false) }
|
|
|
|
// CompileFold is like Compile but matches case-insensitively. It exists for the
|
|
// secret guard: on case-insensitive filesystems (macOS/Windows) a file literally
|
|
// named .ENV / KEY.PEM / ID_RSA is the same secret as .env / key.pem / id_rsa and
|
|
// must also be blocked. .devignore keeps using Compile (case-sensitive).
|
|
func CompileFold(patterns []string) (*Matcher, error) { return compile(patterns, true) }
|
|
|
|
func compile(patterns []string, fold bool) (*Matcher, error) {
|
|
m := &Matcher{}
|
|
for _, p := range patterns {
|
|
r, ok, err := compilePattern(p, fold)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if ok {
|
|
m.rules = append(m.rules, r)
|
|
}
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
func compilePattern(line string, fold bool) (rule, bool, error) {
|
|
// Trim trailing whitespace; gitignore keeps leading whitespace literal.
|
|
line = strings.TrimRight(line, " \t\r\n")
|
|
if line == "" || strings.HasPrefix(line, "#") {
|
|
return rule{}, false, nil
|
|
}
|
|
|
|
var r rule
|
|
if strings.HasPrefix(line, "!") {
|
|
r.negate = true
|
|
line = line[1:]
|
|
}
|
|
if strings.HasSuffix(line, "/") {
|
|
r.dirOnly = true
|
|
line = strings.TrimSuffix(line, "/")
|
|
}
|
|
|
|
// A pattern is anchored to root if it contains a (non-trailing) slash or
|
|
// has a leading slash; otherwise a bare name matches at any depth.
|
|
anchored := strings.Contains(line, "/")
|
|
line = strings.TrimPrefix(line, "/")
|
|
|
|
expr := buildRegex(line, anchored)
|
|
if fold {
|
|
expr = "(?i)" + expr // case-insensitive match (secret guard only)
|
|
}
|
|
re, err := regexp.Compile(expr)
|
|
if err != nil {
|
|
return rule{}, false, err
|
|
}
|
|
r.re = re
|
|
return r, true, nil
|
|
}
|
|
|
|
// buildRegex translates a gitignore glob into an anchored regexp matching a
|
|
// whole path. When unanchored, the pattern may begin at any path segment.
|
|
func buildRegex(glob string, anchored bool) string {
|
|
var b strings.Builder
|
|
b.WriteString("^")
|
|
if anchored {
|
|
b.WriteString(globToRegex(glob))
|
|
} else {
|
|
// Bare name: match at root or after any "/".
|
|
b.WriteString("(?:.*/)?")
|
|
b.WriteString(globToRegex(glob))
|
|
}
|
|
b.WriteString("$")
|
|
return b.String()
|
|
}
|
|
|
|
// globToRegex converts gitignore glob metacharacters to a regexp fragment.
|
|
// - `**` spans segments (including slashes)
|
|
// - `*` matches within a single segment (no slash)
|
|
// - `?` matches one non-slash char
|
|
func globToRegex(g string) string {
|
|
var b strings.Builder
|
|
for i := 0; i < len(g); i++ {
|
|
switch c := g[i]; c {
|
|
case '*':
|
|
if i+1 < len(g) && g[i+1] == '*' {
|
|
b.WriteString(".*") // ponytail: treat ** as "anything incl. /"
|
|
i++
|
|
// swallow a slash directly after ** so a/**/b matches a/b
|
|
if i+1 < len(g) && g[i+1] == '/' {
|
|
i++
|
|
}
|
|
} else {
|
|
b.WriteString("[^/]*")
|
|
}
|
|
case '?':
|
|
b.WriteString("[^/]")
|
|
default:
|
|
b.WriteString(regexp.QuoteMeta(string(c)))
|
|
}
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
// Match reports whether path (forward-slash, relative to root) is ignored.
|
|
// isDir indicates whether path is a directory.
|
|
func (m *Matcher) Match(path string, isDir bool) bool {
|
|
path = strings.Trim(path, "/")
|
|
|
|
// .devbox is always ignored, including everything under it.
|
|
if path == ".devbox" || strings.HasPrefix(path, ".devbox/") {
|
|
return true
|
|
}
|
|
|
|
// A path is ignored if itself matches, or if any ancestor directory is
|
|
// ignored (last-match-wins is evaluated per candidate path).
|
|
if m.matchOne(path, isDir) {
|
|
return true
|
|
}
|
|
segs := strings.Split(path, "/")
|
|
for i := 1; i < len(segs); i++ {
|
|
if m.matchOne(strings.Join(segs[:i], "/"), true) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// matchOne applies last-match-wins to a single path (no ancestor walking).
|
|
func (m *Matcher) matchOne(path string, isDir bool) bool {
|
|
ignored := false
|
|
for _, r := range m.rules {
|
|
if r.dirOnly && !isDir {
|
|
continue
|
|
}
|
|
if r.re.MatchString(path) {
|
|
ignored = !r.negate
|
|
}
|
|
}
|
|
return ignored
|
|
}
|