mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-20 12:23:22 +00:00
Remove all "not yet supported in project mode" errors. Every command that supports agents in global mode now also supports project mode: - status -p agents: shows project agent source + per-target status - status -p all: shows both skills and agents - check -p agents: checks agent status from .skillshare/agents/ - diff -p agents: computes agent diffs (missing/orphan per target) - diff agents (global): also now fully implemented via diffGlobalAgents - collect -p agents: collects local agents from project targets - audit -p agents: scans .skillshare/agents/ for security patterns New file: diff_agents.go — shared agent diff logic for both modes (computeAgentDiff, diffProjectAgents, diffGlobalAgents). 11 new integration tests covering all 5 commands in project mode. Total agent tests: 56.
1083 lines
29 KiB
Go
1083 lines
29 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
gosync "sync"
|
|
"time"
|
|
|
|
"github.com/pterm/pterm"
|
|
|
|
"skillshare/internal/config"
|
|
"skillshare/internal/oplog"
|
|
"skillshare/internal/sync"
|
|
"skillshare/internal/ui"
|
|
"skillshare/internal/utils"
|
|
)
|
|
|
|
// diffRenderOpts controls diff output behavior.
|
|
type diffRenderOpts struct {
|
|
noTUI bool
|
|
showPatch bool
|
|
showStat bool
|
|
jsonOutput bool
|
|
}
|
|
|
|
// diffJSONOutput is the JSON representation for diff --json output.
|
|
type diffJSONOutput struct {
|
|
Targets []diffJSONTarget `json:"targets"`
|
|
Duration string `json:"duration"`
|
|
}
|
|
|
|
type diffJSONTarget struct {
|
|
Name string `json:"name"`
|
|
Mode string `json:"mode"`
|
|
Synced bool `json:"synced"`
|
|
Error string `json:"error,omitempty"`
|
|
Items []diffJSONItem `json:"items"`
|
|
Include []string `json:"include"`
|
|
Exclude []string `json:"exclude"`
|
|
}
|
|
|
|
type diffJSONItem struct {
|
|
Action string `json:"action"`
|
|
Name string `json:"name"`
|
|
Kind string `json:"kind,omitempty"` // "skill" or "agent"
|
|
Reason string `json:"reason"`
|
|
IsSync bool `json:"is_sync"`
|
|
}
|
|
|
|
func cmdDiff(args []string) error {
|
|
start := time.Now()
|
|
|
|
mode, rest, err := parseModeArgs(args)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
cwd, err := os.Getwd()
|
|
if err != nil {
|
|
return fmt.Errorf("cannot determine working directory: %w", err)
|
|
}
|
|
|
|
if mode == modeAuto {
|
|
if projectConfigExists(cwd) {
|
|
mode = modeProject
|
|
} else {
|
|
mode = modeGlobal
|
|
}
|
|
}
|
|
|
|
applyModeLabel(mode)
|
|
|
|
// Extract kind filter (e.g. "skillshare diff agents").
|
|
kind, rest := parseKindArg(rest)
|
|
|
|
scope := "global"
|
|
cfgPath := config.ConfigPath()
|
|
if mode == modeProject {
|
|
scope = "project"
|
|
cfgPath = config.ProjectConfigPath(cwd)
|
|
}
|
|
|
|
var targetName string
|
|
var opts diffRenderOpts
|
|
for i := 0; i < len(rest); i++ {
|
|
switch rest[i] {
|
|
case "--help", "-h":
|
|
printDiffHelp()
|
|
return nil
|
|
case "--no-tui":
|
|
opts.noTUI = true
|
|
case "--patch":
|
|
opts.showPatch = true
|
|
opts.noTUI = true // --patch implies no TUI
|
|
case "--stat":
|
|
opts.showStat = true
|
|
opts.noTUI = true // --stat implies no TUI
|
|
case "--json":
|
|
opts.jsonOutput = true
|
|
opts.noTUI = true // --json implies no TUI
|
|
default:
|
|
targetName = rest[i]
|
|
}
|
|
}
|
|
|
|
var cmdErr error
|
|
if mode == modeProject {
|
|
cmdErr = cmdDiffProject(cwd, targetName, kind, opts, start)
|
|
} else {
|
|
cmdErr = cmdDiffGlobal(targetName, kind, opts, start)
|
|
}
|
|
logDiffOp(cfgPath, targetName, scope, 0, start, cmdErr)
|
|
return cmdErr
|
|
}
|
|
|
|
func logDiffOp(cfgPath string, targetName, scope string, targetsShown int, start time.Time, cmdErr error) {
|
|
e := oplog.NewEntry("diff", statusFromErr(cmdErr), time.Since(start))
|
|
a := map[string]any{"scope": scope}
|
|
if targetName != "" {
|
|
a["target"] = targetName
|
|
}
|
|
if targetsShown > 0 {
|
|
a["targets_shown"] = targetsShown
|
|
}
|
|
e.Args = a
|
|
if cmdErr != nil {
|
|
e.Message = cmdErr.Error()
|
|
}
|
|
oplog.WriteWithLimit(cfgPath, oplog.OpsFile, e, logMaxEntries()) //nolint:errcheck
|
|
}
|
|
|
|
// targetDiffResult holds the diff outcome for one target.
|
|
type targetDiffResult struct {
|
|
name string
|
|
mode string // "merge", "copy", "symlink"
|
|
items []copyDiffEntry // reuse existing struct
|
|
syncCount int
|
|
localCount int
|
|
synced bool // true if fully synced
|
|
errMsg string // non-empty if target inaccessible
|
|
include []string
|
|
exclude []string
|
|
srcMtime time.Time // newest file mtime across source skills
|
|
dstMtime time.Time // newest file mtime in target dir
|
|
}
|
|
|
|
type copyDiffEntry struct {
|
|
action string // "add", "modify", "remove"
|
|
name string
|
|
kind string // "skill" or "agent" (empty defaults to "skill")
|
|
reason string
|
|
isSync bool // true = needs sync, false = local-only
|
|
files []fileDiffEntry // file-level diffs (nil until populated)
|
|
srcDir string // source directory path (for lazy diff)
|
|
dstDir string // target directory path (for lazy diff)
|
|
}
|
|
|
|
// ensureFiles lazily populates file-level diffs on first access.
|
|
// Works for items with srcDir (full diff) or only dstDir (file listing).
|
|
func (e *copyDiffEntry) ensureFiles() {
|
|
if e.files != nil {
|
|
return
|
|
}
|
|
if e.srcDir == "" && e.dstDir == "" {
|
|
return
|
|
}
|
|
e.files = diffSkillFiles(e.srcDir, e.dstDir)
|
|
}
|
|
|
|
// latestMtime returns the newest file mtime under dir.
|
|
func latestMtime(dir string) time.Time {
|
|
var latest time.Time
|
|
filepath.Walk(dir, func(_ string, info os.FileInfo, err error) error { //nolint:errcheck
|
|
if err != nil || info.IsDir() {
|
|
return nil
|
|
}
|
|
if info.ModTime().After(latest) {
|
|
latest = info.ModTime()
|
|
}
|
|
return nil
|
|
})
|
|
return latest
|
|
}
|
|
|
|
// diffProgress displays multi-target scanning progress.
|
|
// Target list (spinner/queued/done) + overall progress bar at the bottom.
|
|
type diffProgress struct {
|
|
names []string
|
|
states []string // "queued", "scanning", "done", "error"
|
|
details []string
|
|
totalSkills int
|
|
processedSkills int
|
|
area *pterm.AreaPrinter
|
|
mu gosync.Mutex
|
|
stopCh chan struct{}
|
|
frames []string
|
|
frame int
|
|
isTTY bool
|
|
}
|
|
|
|
// newDiffProgress creates a progress display for diff scanning.
|
|
// When showBar is false (no copy-mode targets), the progress bar is hidden
|
|
// because merge/symlink diffs are instant.
|
|
func newDiffProgress(names []string, totalSkills int, showBar bool) *diffProgress {
|
|
barTotal := totalSkills
|
|
if !showBar {
|
|
barTotal = 0
|
|
}
|
|
dp := &diffProgress{
|
|
names: names,
|
|
states: make([]string, len(names)),
|
|
details: make([]string, len(names)),
|
|
totalSkills: barTotal,
|
|
frames: []string{"⣾", "⣽", "⣻", "⢿", "⡿", "⣟", "⣯", "⣷"},
|
|
isTTY: ui.IsTTY(),
|
|
}
|
|
for i := range dp.states {
|
|
dp.states[i] = "queued"
|
|
}
|
|
if !dp.isTTY {
|
|
return dp
|
|
}
|
|
area, _ := pterm.DefaultArea.WithRemoveWhenDone(true).Start()
|
|
dp.area = area
|
|
dp.stopCh = make(chan struct{})
|
|
go func() {
|
|
ticker := time.NewTicker(80 * time.Millisecond)
|
|
defer ticker.Stop()
|
|
for {
|
|
select {
|
|
case <-dp.stopCh:
|
|
return
|
|
case <-ticker.C:
|
|
dp.mu.Lock()
|
|
dp.frame = (dp.frame + 1) % len(dp.frames)
|
|
dp.render()
|
|
dp.mu.Unlock()
|
|
}
|
|
}
|
|
}()
|
|
dp.render()
|
|
return dp
|
|
}
|
|
|
|
func (dp *diffProgress) render() {
|
|
if dp.area == nil {
|
|
return
|
|
}
|
|
var lines []string
|
|
for i, name := range dp.names {
|
|
var line string
|
|
switch dp.states[i] {
|
|
case "queued":
|
|
line = fmt.Sprintf(" %s %s", ui.DimText(name), ui.DimText("queued"))
|
|
case "scanning":
|
|
spin := pterm.Cyan(dp.frames[dp.frame])
|
|
line = fmt.Sprintf(" %s %s %s", spin, pterm.Cyan(name), ui.DimText(dp.details[i]))
|
|
case "done":
|
|
line = fmt.Sprintf(" %s %s %s", pterm.Green("✓"), name, ui.DimText(dp.details[i]))
|
|
case "error":
|
|
line = fmt.Sprintf(" %s %s %s", pterm.Red("✗"), name, ui.DimText(dp.details[i]))
|
|
}
|
|
lines = append(lines, line)
|
|
}
|
|
// Progress bar at bottom (with blank line separator)
|
|
if dp.totalSkills > 0 {
|
|
lines = append(lines, "", " "+dp.renderBar())
|
|
}
|
|
dp.area.Update(strings.Join(lines, "\n"))
|
|
}
|
|
|
|
func (dp *diffProgress) renderBar() string {
|
|
return ui.RenderInlineBar(dp.processedSkills, dp.totalSkills)
|
|
}
|
|
|
|
func (dp *diffProgress) startTarget(name string) {
|
|
if dp == nil {
|
|
return
|
|
}
|
|
dp.mu.Lock()
|
|
defer dp.mu.Unlock()
|
|
for i, n := range dp.names {
|
|
if n == name {
|
|
dp.states[i] = "scanning"
|
|
dp.details[i] = "comparing..."
|
|
break
|
|
}
|
|
}
|
|
if !dp.isTTY {
|
|
fmt.Printf(" %s: scanning...\n", name)
|
|
}
|
|
}
|
|
|
|
func (dp *diffProgress) update(targetName, skillName string) {
|
|
if dp == nil {
|
|
return
|
|
}
|
|
dp.mu.Lock()
|
|
defer dp.mu.Unlock()
|
|
dp.processedSkills++
|
|
for i, n := range dp.names {
|
|
if n == targetName {
|
|
dp.details[i] = skillName
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
func (dp *diffProgress) add(n int) {
|
|
if dp == nil {
|
|
return
|
|
}
|
|
dp.mu.Lock()
|
|
defer dp.mu.Unlock()
|
|
dp.processedSkills += n
|
|
}
|
|
|
|
func (dp *diffProgress) doneTarget(name string, r targetDiffResult) {
|
|
if dp == nil {
|
|
return
|
|
}
|
|
dp.mu.Lock()
|
|
defer dp.mu.Unlock()
|
|
for i, n := range dp.names {
|
|
if n != name {
|
|
continue
|
|
}
|
|
if r.errMsg != "" {
|
|
dp.states[i] = "error"
|
|
dp.details[i] = r.errMsg
|
|
} else if r.synced {
|
|
dp.states[i] = "done"
|
|
dp.details[i] = "fully synced"
|
|
} else {
|
|
dp.states[i] = "done"
|
|
dp.details[i] = fmt.Sprintf("%d difference(s)", r.syncCount+r.localCount)
|
|
}
|
|
break
|
|
}
|
|
if !dp.isTTY {
|
|
for i, n := range dp.names {
|
|
if n == name {
|
|
fmt.Printf(" %s: %s\n", name, dp.details[i])
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func (dp *diffProgress) stop() {
|
|
if dp == nil {
|
|
return
|
|
}
|
|
if dp.stopCh != nil {
|
|
close(dp.stopCh)
|
|
}
|
|
if dp.area != nil {
|
|
dp.area.Stop() //nolint:errcheck
|
|
}
|
|
}
|
|
|
|
func cmdDiffGlobal(targetName string, kind resourceKindFilter, opts diffRenderOpts, start time.Time) error {
|
|
cfg, err := config.Load()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Agent-only diff
|
|
if kind == kindAgents {
|
|
return diffGlobalAgents(cfg, targetName, opts, start)
|
|
}
|
|
|
|
var spinner *ui.Spinner
|
|
if !opts.jsonOutput {
|
|
spinner = ui.StartSpinner("Discovering skills")
|
|
}
|
|
discovered, discoverErr := sync.DiscoverSourceSkills(cfg.Source)
|
|
if discoverErr != nil {
|
|
if spinner != nil {
|
|
spinner.Fail("Discovery failed")
|
|
}
|
|
return fmt.Errorf("failed to discover skills: %w", discoverErr)
|
|
}
|
|
if spinner != nil {
|
|
spinner.Success(fmt.Sprintf("Discovered %d skills", len(discovered)))
|
|
}
|
|
|
|
targets := cfg.Targets
|
|
if targetName != "" {
|
|
if t, exists := cfg.Targets[targetName]; exists {
|
|
targets = map[string]config.TargetConfig{targetName: t}
|
|
} else {
|
|
return fmt.Errorf("target '%s' not found", targetName)
|
|
}
|
|
}
|
|
|
|
// Build sorted target list for deterministic progress display
|
|
type targetEntry struct {
|
|
name string
|
|
target config.TargetConfig
|
|
mode string
|
|
}
|
|
var entries []targetEntry
|
|
for name, target := range targets {
|
|
mode := target.SkillsConfig().Mode
|
|
if mode == "" {
|
|
mode = cfg.Mode
|
|
if mode == "" {
|
|
mode = "merge"
|
|
}
|
|
}
|
|
entries = append(entries, targetEntry{name, target, mode})
|
|
}
|
|
sort.Slice(entries, func(i, j int) bool {
|
|
return entries[i].name < entries[j].name
|
|
})
|
|
|
|
// Pre-filter skills per target and compute total for progress bar
|
|
type filteredEntry struct {
|
|
targetEntry
|
|
filtered []sync.DiscoveredSkill
|
|
}
|
|
var fentries []filteredEntry
|
|
totalSkills := 0
|
|
hasCopyMode := false
|
|
for _, e := range entries {
|
|
sc := e.target.SkillsConfig()
|
|
filtered, err := sync.FilterSkills(discovered, sc.Include, sc.Exclude)
|
|
if err != nil {
|
|
return fmt.Errorf("target %s has invalid include/exclude config: %w", e.name, err)
|
|
}
|
|
filtered = sync.FilterSkillsByTarget(filtered, e.name)
|
|
fentries = append(fentries, filteredEntry{e, filtered})
|
|
totalSkills += len(filtered)
|
|
if e.mode == "copy" {
|
|
hasCopyMode = true
|
|
}
|
|
}
|
|
|
|
names := make([]string, len(fentries))
|
|
for i, fe := range fentries {
|
|
names[i] = fe.name
|
|
}
|
|
|
|
var progress *diffProgress
|
|
if !opts.jsonOutput {
|
|
progress = newDiffProgress(names, totalSkills, hasCopyMode)
|
|
}
|
|
|
|
results := make([]targetDiffResult, len(fentries))
|
|
sem := make(chan struct{}, 8)
|
|
var wg gosync.WaitGroup
|
|
for i, fe := range fentries {
|
|
wg.Add(1)
|
|
sem <- struct{}{}
|
|
go func(idx int, fe filteredEntry) {
|
|
defer wg.Done()
|
|
defer func() { <-sem }()
|
|
progress.startTarget(fe.name)
|
|
r := collectTargetDiff(fe.name, fe.target, cfg.Source, fe.mode, fe.filtered, progress)
|
|
progress.doneTarget(fe.name, r)
|
|
results[idx] = r
|
|
}(i, fe)
|
|
}
|
|
wg.Wait()
|
|
|
|
if progress != nil {
|
|
progress.stop()
|
|
}
|
|
|
|
// Extras diff (always included when extras are configured)
|
|
var extrasResults []extraDiffResult
|
|
if len(cfg.Extras) > 0 {
|
|
extrasResults = collectExtrasDiff(cfg.Extras, func(extra config.ExtraConfig) string {
|
|
return config.ResolveExtrasSourceDir(extra, cfg.ExtrasSource, cfg.Source)
|
|
})
|
|
}
|
|
|
|
if opts.jsonOutput {
|
|
return diffOutputJSONWithExtras(results, extrasResults, start)
|
|
}
|
|
if shouldLaunchTUI(opts.noTUI, cfg) && len(results) > 0 {
|
|
return runDiffTUI(results, extrasResults)
|
|
}
|
|
renderGroupedDiffs(results, opts)
|
|
if len(extrasResults) > 0 {
|
|
renderExtrasDiffPlain(extrasResults)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func diffItemToJSON(item copyDiffEntry) diffJSONItem {
|
|
k := item.kind
|
|
if k == "" {
|
|
k = "skill"
|
|
}
|
|
return diffJSONItem{
|
|
Action: item.action,
|
|
Name: item.name,
|
|
Kind: k,
|
|
Reason: item.reason,
|
|
IsSync: item.isSync,
|
|
}
|
|
}
|
|
|
|
func diffOutputJSON(results []targetDiffResult, start time.Time) error {
|
|
output := diffJSONOutput{
|
|
Duration: formatDuration(start),
|
|
}
|
|
for _, r := range results {
|
|
jt := diffJSONTarget{
|
|
Name: r.name,
|
|
Mode: r.mode,
|
|
Synced: r.synced,
|
|
Error: r.errMsg,
|
|
Include: r.include,
|
|
Exclude: r.exclude,
|
|
}
|
|
for _, item := range r.items {
|
|
jt.Items = append(jt.Items, diffItemToJSON(item))
|
|
}
|
|
output.Targets = append(output.Targets, jt)
|
|
}
|
|
return writeJSON(&output)
|
|
}
|
|
|
|
func diffOutputJSONWithExtras(results []targetDiffResult, extrasResults []extraDiffResult, start time.Time) error {
|
|
type outputWithExtras struct {
|
|
Targets []diffJSONTarget `json:"targets"`
|
|
Extras []extraDiffJSONEntry `json:"extras,omitempty"`
|
|
Duration string `json:"duration"`
|
|
}
|
|
o := outputWithExtras{
|
|
Duration: formatDuration(start),
|
|
Extras: extrasDiffToJSON(extrasResults),
|
|
}
|
|
for _, r := range results {
|
|
jt := diffJSONTarget{
|
|
Name: r.name,
|
|
Mode: r.mode,
|
|
Synced: r.synced,
|
|
Error: r.errMsg,
|
|
Include: r.include,
|
|
Exclude: r.exclude,
|
|
}
|
|
for _, item := range r.items {
|
|
jt.Items = append(jt.Items, diffItemToJSON(item))
|
|
}
|
|
o.Targets = append(o.Targets, jt)
|
|
}
|
|
return writeJSON(&o)
|
|
}
|
|
|
|
func collectTargetDiff(name string, target config.TargetConfig, source, mode string, filtered []sync.DiscoveredSkill, dp *diffProgress) targetDiffResult {
|
|
sc := target.SkillsConfig()
|
|
r := targetDiffResult{
|
|
name: name,
|
|
mode: mode,
|
|
include: sc.Include,
|
|
exclude: sc.Exclude,
|
|
}
|
|
|
|
// Check if target is accessible
|
|
_, err := os.Lstat(sc.Path)
|
|
if err != nil {
|
|
r.errMsg = fmt.Sprintf("Cannot access target: %v", err)
|
|
dp.add(len(filtered))
|
|
return r
|
|
}
|
|
|
|
resolution, err := sync.ResolveTargetSkillsForTarget(name, config.ResourceTargetConfig{
|
|
Path: sc.Path,
|
|
TargetNaming: sc.TargetNaming,
|
|
}, filtered)
|
|
if err != nil {
|
|
r.errMsg = err.Error()
|
|
dp.add(len(filtered))
|
|
return r
|
|
}
|
|
|
|
sourceSkills := resolution.ValidTargetNames()
|
|
sourceMap := make(map[string]string, len(resolution.Skills))
|
|
for _, resolved := range resolution.Skills {
|
|
sourceMap[resolved.TargetName] = resolved.Skill.SourcePath
|
|
}
|
|
legacyNames := resolution.LegacyFlatNames()
|
|
|
|
if utils.IsSymlinkOrJunction(sc.Path) {
|
|
r.mode = "symlink"
|
|
collectSymlinkDiff(&r, sc.Path, source)
|
|
dp.add(len(filtered))
|
|
return r
|
|
}
|
|
|
|
if mode == "copy" {
|
|
manifest, _ := sync.ReadManifest(sc.Path)
|
|
collectCopyDiff(&r, name, sc.Path, resolution.Skills, sourceSkills, legacyNames, manifest, dp)
|
|
} else {
|
|
// Merge mode (instant)
|
|
collectMergeDiff(&r, sc.Path, sourceSkills, sourceMap, legacyNames)
|
|
dp.add(len(filtered))
|
|
}
|
|
|
|
// Compute mtime only for copy mode (merge uses symlinks, mtime is misleading)
|
|
if mode == "copy" {
|
|
r.dstMtime = latestMtime(sc.Path)
|
|
var srcLatest time.Time
|
|
for _, resolved := range resolution.Skills {
|
|
mt := latestMtime(resolved.Skill.SourcePath)
|
|
if mt.After(srcLatest) {
|
|
srcLatest = mt
|
|
}
|
|
}
|
|
r.srcMtime = srcLatest
|
|
}
|
|
|
|
return r
|
|
}
|
|
|
|
func collectSymlinkDiff(r *targetDiffResult, targetPath, source string) {
|
|
absLink, err := utils.ResolveLinkTarget(targetPath)
|
|
if err != nil {
|
|
r.errMsg = fmt.Sprintf("Unable to resolve symlink target: %v", err)
|
|
return
|
|
}
|
|
absSource, _ := filepath.Abs(source)
|
|
if utils.PathsEqual(absLink, absSource) {
|
|
r.synced = true
|
|
} else {
|
|
r.errMsg = fmt.Sprintf("Symlink points to different location: %s", absLink)
|
|
}
|
|
}
|
|
|
|
func collectCopyDiff(r *targetDiffResult, targetName, targetPath string, filtered []sync.ResolvedTargetSkill, sourceSkills map[string]bool, legacyNames map[string]sync.ResolvedTargetSkill, manifest *sync.Manifest, dp *diffProgress) {
|
|
for _, resolved := range filtered {
|
|
skill := resolved.Skill
|
|
dp.update(targetName, resolved.TargetName)
|
|
oldChecksum, isManaged := manifest.Managed[resolved.TargetName]
|
|
targetSkillPath := filepath.Join(targetPath, resolved.TargetName)
|
|
srcDir := skill.SourcePath
|
|
dstDir := targetSkillPath
|
|
if !isManaged {
|
|
if info, err := os.Stat(targetSkillPath); err == nil {
|
|
if info.IsDir() {
|
|
r.items = append(r.items, copyDiffEntry{action: "modify", name: resolved.TargetName, reason: "local copy (sync --force to replace)", isSync: true, srcDir: srcDir, dstDir: dstDir})
|
|
} else {
|
|
r.items = append(r.items, copyDiffEntry{action: "modify", name: resolved.TargetName, reason: "target entry is not a directory", isSync: true, srcDir: srcDir, dstDir: dstDir})
|
|
}
|
|
} else if os.IsNotExist(err) {
|
|
r.items = append(r.items, copyDiffEntry{action: "add", name: resolved.TargetName, reason: "source only", isSync: true, srcDir: srcDir, dstDir: dstDir})
|
|
} else {
|
|
r.items = append(r.items, copyDiffEntry{action: "modify", name: resolved.TargetName, reason: "cannot access target entry", isSync: true, srcDir: srcDir, dstDir: dstDir})
|
|
}
|
|
continue
|
|
}
|
|
targetInfo, err := os.Stat(targetSkillPath)
|
|
if os.IsNotExist(err) {
|
|
r.items = append(r.items, copyDiffEntry{action: "add", name: resolved.TargetName, reason: "deleted from target", isSync: true, srcDir: srcDir, dstDir: dstDir})
|
|
continue
|
|
}
|
|
if err != nil {
|
|
r.items = append(r.items, copyDiffEntry{action: "modify", name: resolved.TargetName, reason: "cannot access target entry", isSync: true, srcDir: srcDir, dstDir: dstDir})
|
|
continue
|
|
}
|
|
if !targetInfo.IsDir() {
|
|
r.items = append(r.items, copyDiffEntry{action: "modify", name: resolved.TargetName, reason: "target entry is not a directory", isSync: true, srcDir: srcDir, dstDir: dstDir})
|
|
continue
|
|
}
|
|
// mtime fast-path
|
|
oldMtime := manifest.Mtimes[resolved.TargetName]
|
|
currentMtime, mtimeErr := sync.DirMaxMtime(skill.SourcePath)
|
|
if mtimeErr == nil && oldMtime > 0 && currentMtime == oldMtime {
|
|
continue
|
|
}
|
|
srcChecksum, err := sync.DirChecksum(skill.SourcePath)
|
|
if err != nil {
|
|
r.items = append(r.items, copyDiffEntry{action: "modify", name: resolved.TargetName, reason: "cannot compute checksum", isSync: true, srcDir: srcDir, dstDir: dstDir})
|
|
continue
|
|
}
|
|
if srcChecksum != oldChecksum {
|
|
r.items = append(r.items, copyDiffEntry{action: "modify", name: resolved.TargetName, reason: "content changed", isSync: true, srcDir: srcDir, dstDir: dstDir})
|
|
}
|
|
}
|
|
|
|
// Orphan managed copies
|
|
for name := range manifest.Managed {
|
|
if _, keepLegacy := legacyNames[name]; keepLegacy {
|
|
continue
|
|
}
|
|
if !sourceSkills[name] {
|
|
r.items = append(r.items, copyDiffEntry{action: "remove", name: name, reason: "orphan (will be pruned)", isSync: true, dstDir: filepath.Join(targetPath, name)})
|
|
}
|
|
}
|
|
|
|
// Local directories
|
|
entries, _ := os.ReadDir(targetPath)
|
|
for _, e := range entries {
|
|
if utils.IsHidden(e.Name()) || !e.IsDir() {
|
|
continue
|
|
}
|
|
if sourceSkills[e.Name()] {
|
|
continue
|
|
}
|
|
if _, keepLegacy := legacyNames[e.Name()]; keepLegacy {
|
|
continue
|
|
}
|
|
if _, isManaged := manifest.Managed[e.Name()]; isManaged {
|
|
continue
|
|
}
|
|
r.items = append(r.items, copyDiffEntry{action: "remove", name: e.Name(), reason: "local only", isSync: false, dstDir: filepath.Join(targetPath, e.Name())})
|
|
}
|
|
|
|
// Compute counts
|
|
for _, item := range r.items {
|
|
if item.isSync {
|
|
r.syncCount++
|
|
} else {
|
|
r.localCount++
|
|
}
|
|
}
|
|
r.synced = r.syncCount == 0 && r.localCount == 0
|
|
}
|
|
|
|
func collectMergeDiff(r *targetDiffResult, targetPath string, sourceSkills map[string]bool, sourceMap map[string]string, legacyNames map[string]sync.ResolvedTargetSkill) {
|
|
targetSkills := make(map[string]bool)
|
|
targetSymlinks := make(map[string]bool)
|
|
entries, err := os.ReadDir(targetPath)
|
|
if err != nil {
|
|
r.errMsg = fmt.Sprintf("Cannot read target: %v", err)
|
|
return
|
|
}
|
|
|
|
for _, e := range entries {
|
|
if utils.IsHidden(e.Name()) {
|
|
continue
|
|
}
|
|
skillPath := filepath.Join(targetPath, e.Name())
|
|
if utils.IsSymlinkOrJunction(skillPath) {
|
|
targetSymlinks[e.Name()] = true
|
|
}
|
|
targetSkills[e.Name()] = true
|
|
}
|
|
|
|
// Skills only in source (not synced)
|
|
for skill := range sourceSkills {
|
|
srcDir := sourceMap[skill]
|
|
dstDir := filepath.Join(targetPath, skill)
|
|
if !targetSkills[skill] {
|
|
r.items = append(r.items, copyDiffEntry{action: "add", name: skill, reason: "source only", isSync: true, srcDir: srcDir, dstDir: dstDir})
|
|
r.syncCount++
|
|
} else if !targetSymlinks[skill] {
|
|
r.items = append(r.items, copyDiffEntry{action: "modify", name: skill, reason: "local copy (sync --force to replace)", isSync: true, srcDir: srcDir, dstDir: dstDir})
|
|
r.syncCount++
|
|
}
|
|
}
|
|
|
|
// Skills only in target (local only)
|
|
for skill := range targetSkills {
|
|
if _, keepLegacy := legacyNames[skill]; keepLegacy {
|
|
continue
|
|
}
|
|
if !sourceSkills[skill] && !targetSymlinks[skill] {
|
|
r.items = append(r.items, copyDiffEntry{action: "remove", name: skill, reason: "local only", isSync: false, dstDir: filepath.Join(targetPath, skill)})
|
|
r.localCount++
|
|
}
|
|
}
|
|
|
|
r.synced = r.syncCount == 0 && r.localCount == 0
|
|
}
|
|
|
|
// diffFingerprint generates a grouping key from diff items.
|
|
// Results with the same fingerprint are displayed together.
|
|
func diffFingerprint(items []copyDiffEntry) string {
|
|
sorted := make([]copyDiffEntry, len(items))
|
|
copy(sorted, items)
|
|
sort.Slice(sorted, func(i, j int) bool {
|
|
if sorted[i].name != sorted[j].name {
|
|
return sorted[i].name < sorted[j].name
|
|
}
|
|
return sorted[i].action < sorted[j].action
|
|
})
|
|
var b strings.Builder
|
|
for _, item := range sorted {
|
|
fmt.Fprintf(&b, "%s|%s|%s\n", item.action, item.name, item.reason)
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
// actionCategory groups diff items by the user action needed.
|
|
type actionCategory struct {
|
|
kind string // "new", "modified", "restore", "override", "orphan", "local", "warn"
|
|
label string // e.g. "New", "Modified", "Local Override"
|
|
names []string
|
|
expand bool // true = list skill names
|
|
}
|
|
|
|
// categorizeItems maps raw diff items to action-oriented categories.
|
|
func categorizeItems(items []copyDiffEntry) []actionCategory {
|
|
type bucket struct {
|
|
kind string
|
|
label string
|
|
names []string
|
|
}
|
|
buckets := map[string]*bucket{}
|
|
var order []string
|
|
|
|
add := func(key, kind, label, name string) {
|
|
if b, ok := buckets[key]; ok {
|
|
b.names = append(b.names, name)
|
|
} else {
|
|
buckets[key] = &bucket{kind: kind, label: label, names: []string{name}}
|
|
order = append(order, key)
|
|
}
|
|
}
|
|
|
|
for _, item := range items {
|
|
switch {
|
|
case item.reason == "source only":
|
|
add("new", "new", "New", item.name)
|
|
case item.reason == "deleted from target":
|
|
add("restore", "new", "Restore", item.name)
|
|
case item.reason == "content changed":
|
|
add("modified", "modified", "Modified", item.name)
|
|
case strings.Contains(item.reason, "local copy"):
|
|
add("override", "override", "Local Override", item.name)
|
|
case strings.Contains(item.reason, "orphan"):
|
|
add("orphan", "orphan", "Orphan", item.name)
|
|
case item.reason == "local only" || item.reason == "not in source":
|
|
add("local", "local", "Local Only", item.name)
|
|
default:
|
|
add("warn", "warn", item.reason, item.name)
|
|
}
|
|
}
|
|
|
|
var cats []actionCategory
|
|
for _, key := range order {
|
|
b := buckets[key]
|
|
expand := len(b.names) <= 15
|
|
cats = append(cats, actionCategory{
|
|
kind: b.kind,
|
|
label: b.label,
|
|
names: b.names,
|
|
expand: expand,
|
|
})
|
|
}
|
|
return cats
|
|
}
|
|
|
|
// renderGroupedDiffs groups targets with identical diff results and renders
|
|
// merged output. Targets with errors are always shown individually.
|
|
func renderGroupedDiffs(results []targetDiffResult, opts diffRenderOpts) {
|
|
sort.Slice(results, func(i, j int) bool {
|
|
return results[i].name < results[j].name
|
|
})
|
|
|
|
var errorResults []targetDiffResult
|
|
var syncedNames []string
|
|
type diffGroup struct {
|
|
names []string
|
|
result targetDiffResult
|
|
}
|
|
groups := make(map[string]*diffGroup)
|
|
var groupOrder []string
|
|
|
|
for _, r := range results {
|
|
if r.errMsg != "" {
|
|
errorResults = append(errorResults, r)
|
|
continue
|
|
}
|
|
if r.synced {
|
|
syncedNames = append(syncedNames, r.name)
|
|
continue
|
|
}
|
|
fp := diffFingerprint(r.items)
|
|
if g, exists := groups[fp]; exists {
|
|
g.names = append(g.names, r.name)
|
|
} else {
|
|
groups[fp] = &diffGroup{names: []string{r.name}, result: r}
|
|
groupOrder = append(groupOrder, fp)
|
|
}
|
|
}
|
|
|
|
// Overall summary (skip when all targets are fully synced — the ✓ line is enough)
|
|
needCount := 0
|
|
for _, g := range groups {
|
|
needCount += len(g.names)
|
|
}
|
|
if needCount > 0 || len(errorResults) > 0 {
|
|
renderOverallSummary(len(errorResults), needCount, len(syncedNames))
|
|
}
|
|
|
|
// Error targets
|
|
for _, r := range errorResults {
|
|
ui.Header(r.name)
|
|
ui.Warning("%s", r.errMsg)
|
|
}
|
|
|
|
// Grouped diffs
|
|
var anySyncNeeded, anyForceNeeded, anyCollectNeeded bool
|
|
for _, fp := range groupOrder {
|
|
g := groups[fp]
|
|
sort.Strings(g.names)
|
|
ui.Header(strings.Join(g.names, ", "))
|
|
|
|
items := make([]copyDiffEntry, len(g.result.items))
|
|
copy(items, g.result.items)
|
|
sort.Slice(items, func(i, j int) bool {
|
|
return items[i].name < items[j].name
|
|
})
|
|
|
|
cats := categorizeItems(items)
|
|
|
|
// Per-group stat line
|
|
var statParts []string
|
|
for _, cat := range cats {
|
|
n := len(cat.names)
|
|
statParts = append(statParts, fmt.Sprintf("%d %s", n, strings.ToLower(cat.label)))
|
|
}
|
|
if len(statParts) > 0 {
|
|
fmt.Printf(" %s%s%s\n", ui.Dim, strings.Join(statParts, ", "), ui.Reset)
|
|
}
|
|
|
|
for _, cat := range cats {
|
|
n := len(cat.names)
|
|
switch cat.kind {
|
|
case "new", "modified", "restore", "orphan":
|
|
anySyncNeeded = true
|
|
case "override":
|
|
anyForceNeeded = true
|
|
case "local":
|
|
anyCollectNeeded = true
|
|
}
|
|
|
|
skillWord := "skills"
|
|
if n == 1 {
|
|
skillWord = "skill"
|
|
}
|
|
if cat.expand && n > 0 {
|
|
ui.ActionLine(cat.kind, fmt.Sprintf("%s %d %s:", cat.label, n, skillWord))
|
|
for _, name := range cat.names {
|
|
fmt.Printf(" %s\n", name)
|
|
if opts.showStat || opts.showPatch || cat.kind == "modified" {
|
|
item := findDiffItem(items, name)
|
|
if item != nil {
|
|
item.ensureFiles()
|
|
if len(item.files) > 0 {
|
|
fmt.Print(renderFileStat(item.files))
|
|
}
|
|
if opts.showPatch {
|
|
for _, f := range item.files {
|
|
if f.Action == "modify" && item.srcDir != "" {
|
|
srcFile := filepath.Join(item.srcDir, f.RelPath)
|
|
dstFile := filepath.Join(item.dstDir, f.RelPath)
|
|
diffText := generateUnifiedDiff(srcFile, dstFile)
|
|
if diffText != "" {
|
|
fmt.Printf(" --- %s\n", f.RelPath)
|
|
fmt.Print(colorizePlainDiff(diffText))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
ui.ActionLine(cat.kind, fmt.Sprintf("%s %d %s", cat.label, n, skillWord))
|
|
}
|
|
}
|
|
|
|
// Time info
|
|
if !g.result.srcMtime.IsZero() || !g.result.dstMtime.IsZero() {
|
|
if !g.result.srcMtime.IsZero() {
|
|
fmt.Printf(" %sSource modified: %s%s\n", ui.Dim, g.result.srcMtime.Format("2006-01-02 15:04"), ui.Reset)
|
|
}
|
|
if !g.result.dstMtime.IsZero() {
|
|
fmt.Printf(" %sTarget modified: %s%s\n", ui.Dim, g.result.dstMtime.Format("2006-01-02 15:04"), ui.Reset)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Conditional hints
|
|
if anySyncNeeded || anyForceNeeded || anyCollectNeeded {
|
|
fmt.Println()
|
|
}
|
|
if anySyncNeeded || anyForceNeeded {
|
|
if anyForceNeeded {
|
|
ui.Info("Run 'skillshare sync' to apply changes, 'skillshare sync --force' to also replace local copies")
|
|
} else {
|
|
ui.Info("Run 'skillshare sync' to apply changes")
|
|
}
|
|
}
|
|
if anyCollectNeeded {
|
|
ui.Info("Run 'skillshare collect' to import local skills to source")
|
|
}
|
|
|
|
// Fully synced
|
|
if len(syncedNames) > 0 {
|
|
sort.Strings(syncedNames)
|
|
ui.Success("%s: fully synced", strings.Join(syncedNames, ", "))
|
|
}
|
|
}
|
|
|
|
func findDiffItem(items []copyDiffEntry, name string) *copyDiffEntry {
|
|
for i := range items {
|
|
if items[i].name == name {
|
|
return &items[i]
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func colorizePlainDiff(diff string) string {
|
|
if !ui.IsTTY() {
|
|
return diff
|
|
}
|
|
var b strings.Builder
|
|
for _, line := range strings.Split(strings.TrimRight(diff, "\n"), "\n") {
|
|
switch {
|
|
case strings.HasPrefix(line, "+ "):
|
|
b.WriteString(ui.Green + line + ui.Reset + "\n")
|
|
case strings.HasPrefix(line, "- "):
|
|
b.WriteString(ui.Red + line + ui.Reset + "\n")
|
|
default:
|
|
b.WriteString(line + "\n")
|
|
}
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
func renderOverallSummary(errCount, needCount, syncCount int) {
|
|
var parts []string
|
|
total := errCount + needCount + syncCount
|
|
if errCount > 0 {
|
|
parts = append(parts, fmt.Sprintf("%s%d error%s%s", ui.Red, errCount, pluralS(errCount), ui.Reset))
|
|
}
|
|
if needCount > 0 {
|
|
parts = append(parts, fmt.Sprintf("%s%d need sync%s", ui.Yellow, needCount, ui.Reset))
|
|
}
|
|
if syncCount > 0 {
|
|
parts = append(parts, fmt.Sprintf("%s%d synced%s", ui.Green, syncCount, ui.Reset))
|
|
}
|
|
if len(parts) > 0 {
|
|
fmt.Printf("\n%sSummary:%s %d target%s — %s\n", ui.Bold, ui.Reset, total, pluralS(total), strings.Join(parts, ", "))
|
|
}
|
|
}
|
|
|
|
func pluralS(n int) string {
|
|
if n == 1 {
|
|
return ""
|
|
}
|
|
return "s"
|
|
}
|
|
|
|
func printDiffHelp() {
|
|
fmt.Println(`Usage: skillshare diff [agents|all] [target] [options]
|
|
|
|
Show differences between source skills and target directories.
|
|
Previews what 'sync' would change without modifying anything.
|
|
|
|
Arguments:
|
|
target Target name to diff (optional; diffs all if omitted)
|
|
|
|
Options:
|
|
--project, -p Diff project-level skills (.skillshare/)
|
|
--global, -g Diff global skills (~/.config/skillshare)
|
|
--stat Show file-level changes (implies --no-tui)
|
|
--patch Show full unified diff (implies --no-tui)
|
|
--json Output results as JSON
|
|
--no-tui Plain text output (skip interactive TUI)
|
|
--help, -h Show this help
|
|
|
|
Examples:
|
|
skillshare diff # Diff all targets
|
|
skillshare diff claude # Diff a single target
|
|
skillshare diff -p # Diff project-mode targets
|
|
skillshare diff --stat # Show file-level stat
|
|
skillshare diff --patch # Show full text diff
|
|
skillshare diff agents # Diff agents targets only`)
|
|
}
|