Files
skillshare/cmd/skillshare/diff_project.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

144 lines
3.8 KiB
Go

package main
import (
"fmt"
gosync "sync"
"time"
"skillshare/internal/config"
"skillshare/internal/sync"
"skillshare/internal/ui"
)
func cmdDiffProject(root, targetName string, kind resourceKindFilter, opts diffRenderOpts, start time.Time) error {
if kind == kindAgents {
return diffProjectAgents(root, targetName, opts, start)
}
if err := ensureProjectConfig(root); err != nil {
return err
}
runtime, err := loadProjectRuntime(root)
if err != nil {
return err
}
var spinner *ui.Spinner
if !opts.jsonOutput {
spinner = ui.StartSpinner("Discovering skills")
}
discovered, err := sync.DiscoverSourceSkills(runtime.sourcePath, runtime.skillsWalk())
if err != nil {
if spinner != nil {
spinner.Fail("Discovery failed")
}
return fmt.Errorf("failed to discover skills: %w", err)
}
if spinner != nil {
spinner.Stop()
}
targets := make([]config.ProjectTargetEntry, len(runtime.config.Targets))
copy(targets, runtime.config.Targets)
if targetName != "" {
found := false
for _, entry := range runtime.config.Targets {
if entry.Name == targetName {
targets = []config.ProjectTargetEntry{entry}
found = true
break
}
}
if !found {
return fmt.Errorf("target '%s' not found", targetName)
}
}
// Pre-filter skills per target and compute total for progress bar
type resolvedTarget struct {
name string
target config.TargetConfig
mode string
filtered []sync.DiscoveredSkill
}
var resolved []resolvedTarget
totalSkills := 0
hasCopyMode := false
for _, entry := range targets {
target, ok := runtime.targets[entry.Name]
if !ok {
return fmt.Errorf("target '%s' not resolved", entry.Name)
}
sc := target.SkillsConfig()
// Skills off: nothing is synced, so there is nothing to compare.
if !sc.IsEnabled() {
continue
}
filtered, err := sync.FilterSkills(discovered, sc.Include, sc.Exclude)
if err != nil {
return fmt.Errorf("target %s has invalid include/exclude config: %w", entry.Name, err)
}
filtered = sync.FilterSkillsByTarget(filtered, entry.Name)
mode := sc.Mode
if mode == "" {
mode = "merge"
}
resolved = append(resolved, resolvedTarget{entry.Name, target, mode, filtered})
totalSkills += len(filtered)
if mode == "copy" {
hasCopyMode = true
}
}
names := make([]string, len(resolved))
for i, rt := range resolved {
names[i] = rt.name
}
var progress *diffProgress
if !opts.jsonOutput {
progress = newDiffProgress(names, totalSkills, hasCopyMode)
}
results := make([]targetDiffResult, len(resolved))
ignorePatterns := sync.EffectiveFileIgnorePatterns(runtime.config.Ignore)
sem := make(chan struct{}, 8)
var wg gosync.WaitGroup
for i, rt := range resolved {
wg.Add(1)
sem <- struct{}{}
go func(idx int, rt resolvedTarget) {
defer wg.Done()
defer func() { <-sem }()
progress.startTarget(rt.name)
r := collectTargetDiff(rt.name, rt.target, runtime.sourcePath, rt.mode, rt.filtered, ignorePatterns, progress)
progress.doneTarget(rt.name, r)
results[idx] = r
}(i, rt)
}
wg.Wait()
progress.stop()
// Extras diff (always included when extras are configured)
var extrasResults []extraDiffResult
if len(runtime.config.Extras) > 0 {
extrasResults = collectExtrasDiff(runtime.config.Extras, func(extra config.ExtraConfig) string {
return config.ResolveExtrasSourceDirProject(extra, runtime.config.EffectiveExtrasSource(root), root)
})
}
// Merge agent diffs into skill results so they appear together
results = mergeAgentDiffsProject(root, results, targetName)
if opts.jsonOutput {
return diffOutputJSONWithExtras(results, extrasResults, start)
}
if shouldLaunchTUI(opts.noTUI, nil) && len(results) > 0 {
return runDiffTUI(results, extrasResults)
}
opts.noTargets = len(runtime.config.Targets) == 0
renderGroupedDiffs(results, extrasResults, opts)
return nil
}