Files
skillshare/internal/server/handler_skillignore.go
T
Willie 89fc4fbdf1 feat(sourcewalk): follow first-level source links when enabled
Users link a checkout directly under the skills source (e.g.
skills/_dev-skills -> ~/code/dev-skills) and expect it to behave like any
other first-level entry. sourcewalk resolved only the root, so these links
were invisible to discovery, sync, update, and the dashboard.

follow_source_links (global and project config, default false) enables a
per-operation sourcewalk.Follow policy. Walk, WalkDir, and ReadDir report a
followed link under its own name as a directory and descend into its
target, so names, RelPath, FlatName, SourcePath, tracked-repo detection, and
metadata keys stay logical. Only entries directly under the source root are
followed. The policy is built from config and passed explicitly to every
discovery caller in the CLI, server, and reconcile, so counts agree; the
server keeps a per-instance snapshot refreshed on config load and save
instead of a process-global flag.

A link is skipped, with one warning per operation, when its target is
missing or unreadable, is the source or a parent of it, or overlaps an
active skills target. A missing or unreadable target marks the operation
incomplete: sync then skips pruning and copy-orphan deletion, and reconcile
keeps metadata, so an unmounted drive or a re-clone in progress cannot
erase target entries or install records. A link to an existing file is not
a directory link and stays an ordinary entry; treating it as incomplete
would disable pruning for anyone sharing a file such as .skillignore.

doctor keeps raw link identity: with the key off it still says the link is
not followed and names follow_source_links: true; with it on it reports
followed links and skipped links with the reason.

The raw-walk allowlist gains entries for the policy path in Walk and
WalkDir: the source root walk with a policy, and the walk of a validated
link target whose paths are remapped to the logical link path. Both live
in sourcewalk itself, the package that owns source traversal.

Refs #274
2026-10-05 14:26:09 +08:00

120 lines
3.1 KiB
Go

package server
import (
"errors"
"net/http"
"os"
"path/filepath"
"time"
"skillshare/internal/sync"
)
type skillignoreStats struct {
PatternCount int `json:"pattern_count"`
IgnoredCount int `json:"ignored_count"`
Patterns []string `json:"patterns"`
IgnoredSkills []string `json:"ignored_skills"`
}
type skillignoreResponse struct {
Exists bool `json:"exists"`
Path string `json:"path"`
Raw string `json:"raw"`
Local *ignoreLocalFile `json:"local,omitempty"`
Stats *skillignoreStats `json:"stats,omitempty"`
}
// ignoreLocalFile is the machine-only .local companion of an ignore file.
// Its rules apply after the shared file and can override it.
type ignoreLocalFile struct {
Path string `json:"path"`
Raw string `json:"raw"`
}
// readIgnoreLocal returns ignorePath's .local companion, or nil when absent.
func readIgnoreLocal(ignorePath string) *ignoreLocalFile {
localPath := ignorePath + ".local"
raw, err := os.ReadFile(localPath)
if err != nil {
return nil
}
return &ignoreLocalFile{Path: localPath, Raw: string(raw)}
}
func (s *Server) handleGetSkillignore(w http.ResponseWriter, r *http.Request) {
// Snapshot source path under RLock, then release before I/O.
s.mu.RLock()
source := s.skillsSource()
s.mu.RUnlock()
ignorePath := filepath.Join(source, ".skillignore")
raw, err := os.ReadFile(ignorePath)
resp := skillignoreResponse{
Exists: err == nil,
Path: ignorePath,
Raw: string(raw), // empty if file doesn't exist
Local: readIgnoreLocal(ignorePath),
}
// Always discover stats — tracked repos may have their own .skillignore
_, stats, discoverErr := sync.DiscoverSourceSkillsWithStats(source, s.skillsWalk())
if discoverErr == nil && stats != nil {
patterns := stats.Patterns
if patterns == nil {
patterns = []string{}
}
ignoredSkills := stats.IgnoredSkills
if ignoredSkills == nil {
ignoredSkills = []string{}
}
resp.Stats = &skillignoreStats{
PatternCount: stats.PatternCount(),
IgnoredCount: stats.IgnoredCount(),
Patterns: patterns,
IgnoredSkills: ignoredSkills,
}
}
writeJSON(w, resp)
}
func (s *Server) handlePutSkillignore(w http.ResponseWriter, r *http.Request) {
start := time.Now()
s.mu.Lock()
defer s.mu.Unlock()
var body struct {
Raw string `json:"raw"`
}
if err := decodeJSON(w, r, &body, defaultJSONBodyLimit); err != nil {
if !errors.Is(err, errBodyTooLarge) {
writeError(w, http.StatusBadRequest, "invalid JSON body")
}
return
}
source := s.skillsSource()
ignorePath := filepath.Join(source, ".skillignore")
if body.Raw == "" {
if err := os.Remove(ignorePath); err != nil && !errors.Is(err, os.ErrNotExist) {
writeError(w, http.StatusInternalServerError, "failed to delete .skillignore: "+err.Error())
return
}
} else {
if err := os.WriteFile(ignorePath, []byte(body.Raw), 0644); err != nil {
writeError(w, http.StatusInternalServerError, "failed to write .skillignore: "+err.Error())
return
}
}
s.writeOpsLog("skillignore", "ok", start, map[string]any{
"scope": "ui",
}, "")
writeJSON(w, map[string]any{"success": true})
}