From 0bd6b893897ce1446a3486aaf4d8eaa216334afd Mon Sep 17 00:00:00 2001 From: Willie Date: Thu, 26 Feb 2026 13:18:58 +0800 Subject: [PATCH] perf(sync): cache discovery + add progress UI for sync operations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit DiscoverSourceSkills was called 1+2N times (N = number of targets), each doing a full filepath.Walk + SKILL.md frontmatter parsing. Now discovered once and passed to all targets via WithSkills variants. Changes: - Add SyncTargetMergeWithSkills, PruneOrphanLinksWithSkills (PruneOptions struct) - Add SyncTargetCopyWithSkills with onProgress callback, PruneOrphanCopiesWithSkills - Discovery spinner + per-target reporting + summary line in CLI - Copy mode shows per-skill spinner progress during slow checksum+copy - Collision report uses dim gray text to avoid confusion with sync results - Server handlers (handleSync, handlePull) use single discovery pass - Fix pre-existing TestLog_StatsOutput assertion ("Success rate" → "OK:") --- cmd/skillshare/sync.go | 165 ++++++++++++++++++++++++++++---- cmd/skillshare/sync_project.go | 33 ++++++- internal/server/handler_git.go | 31 +++++- internal/server/handler_sync.go | 19 +++- internal/sync/copy.go | 38 +++++--- internal/sync/sync.go | 62 +++++++++--- internal/ui/pterm.go | 56 +++-------- tests/integration/log_test.go | 2 +- 8 files changed, 304 insertions(+), 102 deletions(-) diff --git a/cmd/skillshare/sync.go b/cmd/skillshare/sync.go index eef1fbb1c..eb88ccdad 100644 --- a/cmd/skillshare/sync.go +++ b/cmd/skillshare/sync.go @@ -23,6 +23,11 @@ type syncLogStats struct { ProjectScope bool } +// syncModeStats aggregates per-target sync results for UI summary. +type syncModeStats struct { + linked, local, updated, pruned int +} + func cmdSync(args []string) error { start := time.Now() @@ -70,29 +75,51 @@ func cmdSync(args []string) error { backupTargetsBeforeSync(cfg) } - // Check for name collisions before syncing (per-target aware) + // Phase 1: Discovery — spinner + spinner := ui.StartSpinner("Discovering skills") discoveredSkills, discoverErr := sync.DiscoverSourceSkills(cfg.Source) - if discoverErr == nil { - reportCollisions(discoveredSkills, cfg.Targets) + if discoverErr != nil { + spinner.Fail("Discovery failed") + return discoverErr } + spinner.Success(fmt.Sprintf("Discovered %d skills", len(discoveredSkills))) + reportCollisions(discoveredSkills, cfg.Targets) + // Phase 2: Per-target sync ui.Header("Syncing skills") if dryRun { ui.Warning("Dry run mode - no changes will be made") } failedTargets := 0 + var totals syncModeStats for name, target := range cfg.Targets { - if err := syncTarget(name, target, cfg, dryRun, force); err != nil { + stats, err := syncTargetWithSkillsStats(name, target, cfg, discoveredSkills, dryRun, force) + if err != nil { ui.Error("%s: %v", name, err) failedTargets++ } + totals.linked += stats.linked + totals.local += stats.local + totals.updated += stats.updated + totals.pruned += stats.pruned } + var syncErr error if failedTargets > 0 { syncErr = fmt.Errorf("some targets failed to sync") } + // Phase 3: Summary + ui.SyncSummary(ui.SyncStats{ + Targets: len(cfg.Targets), + Linked: totals.linked, + Local: totals.local, + Updated: totals.updated, + Pruned: totals.pruned, + Duration: time.Since(start), + }) + // Opportunistic cleanup of expired trash items if !dryRun { if n, _ := trash.Cleanup(trash.TrashDir(), 0); n > 0 { @@ -179,19 +206,66 @@ func syncTarget(name string, target config.TargetConfig, cfg *config.Config, dry } } +func syncTargetWithSkills(name string, target config.TargetConfig, cfg *config.Config, skills []sync.DiscoveredSkill, dryRun, force bool) error { + _, err := syncTargetWithSkillsStats(name, target, cfg, skills, dryRun, force) + return err +} + +func syncTargetWithSkillsStats(name string, target config.TargetConfig, cfg *config.Config, skills []sync.DiscoveredSkill, dryRun, force bool) (syncModeStats, error) { + mode := target.Mode + if mode == "" { + mode = cfg.Mode + } + if mode == "" { + mode = "merge" + } + + switch mode { + case "merge": + return syncMergeModeWithSkills(name, target, cfg.Source, skills, dryRun, force) + case "copy": + return syncCopyModeWithSkills(name, target, cfg.Source, skills, dryRun, force) + default: + err := syncSymlinkMode(name, target, cfg.Source, dryRun, force) + return syncModeStats{}, err + } +} + func syncMergeMode(name string, target config.TargetConfig, source string, dryRun, force bool) error { result, err := sync.SyncTargetMerge(name, target, source, dryRun, force) if err != nil { return err } - // Prune orphan links (skills that no longer exist in source) pruneResult, pruneErr := sync.PruneOrphanLinks(target.Path, source, target.Include, target.Exclude, name, dryRun, force) if pruneErr != nil { ui.Warning("%s: prune failed: %v", name, pruneErr) } - // Report results + reportMergeResult(name, target, result, pruneResult) + return nil +} + +func syncMergeModeWithSkills(name string, target config.TargetConfig, source string, skills []sync.DiscoveredSkill, dryRun, force bool) (syncModeStats, error) { + result, err := sync.SyncTargetMergeWithSkills(name, target, skills, dryRun, force) + if err != nil { + return syncModeStats{}, err + } + + pruneResult, pruneErr := sync.PruneOrphanLinksWithSkills(sync.PruneOptions{ + TargetPath: target.Path, SourcePath: source, Skills: skills, + Include: target.Include, Exclude: target.Exclude, TargetName: name, + DryRun: dryRun, Force: force, + }) + if pruneErr != nil { + ui.Warning("%s: prune failed: %v", name, pruneErr) + } + + reportMergeResult(name, target, result, pruneResult) + return mergeStats(result, pruneResult), nil +} + +func reportMergeResult(name string, target config.TargetConfig, result *sync.MergeResult, pruneResult *sync.PruneResult) { linkedCount := len(result.Linked) updatedCount := len(result.Updated) skippedCount := len(result.Skipped) @@ -210,7 +284,6 @@ func syncMergeMode(name string, target config.TargetConfig, source string, dryRu ui.Success("%s: merged (no skills)", name) } - // Show filter summary (omit empty, human-readable) if len(target.Include) > 0 { ui.Info(" include: %s", strings.Join(target.Include, ", ")) } @@ -218,14 +291,11 @@ func syncMergeMode(name string, target config.TargetConfig, source string, dryRu ui.Info(" exclude: %s", strings.Join(target.Exclude, ", ")) } - // Show prune warnings if pruneResult != nil { for _, warn := range pruneResult.Warnings { ui.Warning(" %s", warn) } } - - return nil } func syncCopyMode(name string, target config.TargetConfig, source string, dryRun, force bool) error { @@ -234,13 +304,64 @@ func syncCopyMode(name string, target config.TargetConfig, source string, dryRun return err } - // Prune orphan copies pruneResult, pruneErr := sync.PruneOrphanCopies(target.Path, source, target.Include, target.Exclude, name, dryRun) if pruneErr != nil { ui.Warning("%s: prune failed: %v", name, pruneErr) } - // Report results + reportCopyResult(name, target, result, pruneResult) + return nil +} + +func syncCopyModeWithSkills(name string, target config.TargetConfig, source string, skills []sync.DiscoveredSkill, dryRun, force bool) (syncModeStats, error) { + // Copy mode is slow (checksum + file copy per skill) — show a spinner with progress + spinner := ui.StartSpinner(fmt.Sprintf("%s: copying skills", name)) + onProgress := func(cur, total int, skill string) { + spinner.Update(fmt.Sprintf("%s: %d/%d %s", name, cur, total, skill)) + } + + result, err := sync.SyncTargetCopyWithSkills(name, target, skills, dryRun, force, onProgress) + if err != nil { + spinner.Fail(fmt.Sprintf("%s: copy failed", name)) + return syncModeStats{}, err + } + spinner.Stop() + + pruneResult, pruneErr := sync.PruneOrphanCopiesWithSkills(target.Path, skills, target.Include, target.Exclude, name, dryRun) + if pruneErr != nil { + ui.Warning("%s: prune failed: %v", name, pruneErr) + } + + reportCopyResult(name, target, result, pruneResult) + return copyStats(result, pruneResult), nil +} + +func mergeStats(result *sync.MergeResult, prune *sync.PruneResult) syncModeStats { + s := syncModeStats{ + linked: len(result.Linked), + local: len(result.Skipped), + updated: len(result.Updated), + } + if prune != nil { + s.pruned = len(prune.Removed) + s.local += len(prune.LocalDirs) + } + return s +} + +func copyStats(result *sync.CopyResult, prune *sync.PruneResult) syncModeStats { + s := syncModeStats{ + linked: len(result.Copied), + local: len(result.Skipped), + updated: len(result.Updated), + } + if prune != nil { + s.pruned = len(prune.Removed) + } + return s +} + +func reportCopyResult(name string, target config.TargetConfig, result *sync.CopyResult, pruneResult *sync.PruneResult) { copiedCount := len(result.Copied) updatedCount := len(result.Updated) skippedCount := len(result.Skipped) @@ -258,7 +379,6 @@ func syncCopyMode(name string, target config.TargetConfig, source string, dryRun ui.Success("%s: copied (no skills)", name) } - // Show filter summary if len(target.Include) > 0 { ui.Info(" include: %s", strings.Join(target.Include, ", ")) } @@ -266,14 +386,11 @@ func syncCopyMode(name string, target config.TargetConfig, source string, dryRun ui.Info(" exclude: %s", strings.Join(target.Exclude, ", ")) } - // Show prune warnings if pruneResult != nil { for _, warn := range pruneResult.Warnings { ui.Warning(" %s", warn) } } - - return nil } func reportCollisions(skills []sync.DiscoveredSkill, targets map[string]config.TargetConfig) { @@ -296,12 +413,18 @@ func reportCollisions(skills []sync.DiscoveredSkill, targets map[string]config.T ui.Info("Rename one in SKILL.md or adjust include/exclude filters") fmt.Println() } else { - // Global collision exists but filters isolate them — informational - ui.Info("Duplicate skill names exist but are isolated by target filters:") - for _, c := range global { - ui.Info(" '%s' (%d definitions)", c.Name, len(c.Paths)) + // Global collision exists but filters isolate them — dim list + const maxShow = 50 + fmt.Printf("\n%s%d duplicate skill names (isolated by target filters)%s\n", + ui.Gray, len(global), ui.Reset) + fmt.Println(ui.Gray + "─────────────────────────────────────────" + ui.Reset) + for i, c := range global { + if i >= maxShow { + fmt.Printf("%s ... and %d more%s\n", ui.Gray, len(global)-maxShow, ui.Reset) + break + } + fmt.Printf("%s '%s' (%d definitions)%s\n", ui.Gray, c.Name, len(c.Paths), ui.Reset) } - fmt.Println() } } diff --git a/cmd/skillshare/sync_project.go b/cmd/skillshare/sync_project.go index a06a47557..a1feff761 100644 --- a/cmd/skillshare/sync_project.go +++ b/cmd/skillshare/sync_project.go @@ -3,6 +3,7 @@ package main import ( "fmt" "os" + "time" "skillshare/internal/config" "skillshare/internal/sync" @@ -11,6 +12,7 @@ import ( ) func cmdSyncProject(root string, dryRun, force bool) (syncLogStats, error) { + start := time.Now() stats := syncLogStats{ DryRun: dryRun, Force: force, @@ -33,17 +35,24 @@ func cmdSyncProject(root string, dryRun, force bool) (syncLogStats, error) { return stats, fmt.Errorf("source directory does not exist: %s", runtime.sourcePath) } + // Phase 1: Discovery — spinner + spinner := ui.StartSpinner("Discovering skills") discoveredSkills, discoverErr := sync.DiscoverSourceSkills(runtime.sourcePath) - if discoverErr == nil { - reportCollisions(discoveredSkills, runtime.targets) + if discoverErr != nil { + spinner.Fail("Discovery failed") + return stats, discoverErr } + spinner.Success(fmt.Sprintf("Discovered %d skills", len(discoveredSkills))) + reportCollisions(discoveredSkills, runtime.targets) + // Phase 2: Per-target sync ui.Header("Syncing skills (project)") if dryRun { ui.Warning("Dry run mode - no changes will be made") } failedTargets := 0 + var totals syncModeStats for _, entry := range runtime.config.Targets { name := entry.Name target, ok := runtime.targets[name] @@ -58,21 +67,37 @@ func cmdSyncProject(root string, dryRun, force bool) (syncLogStats, error) { mode = "merge" } + var s syncModeStats var syncErr error switch mode { case "symlink": syncErr = syncSymlinkMode(name, target, runtime.sourcePath, dryRun, force) case "copy": - syncErr = syncCopyMode(name, target, runtime.sourcePath, dryRun, force) + s, syncErr = syncCopyModeWithSkills(name, target, runtime.sourcePath, discoveredSkills, dryRun, force) default: - syncErr = syncMergeMode(name, target, runtime.sourcePath, dryRun, force) + s, syncErr = syncMergeModeWithSkills(name, target, runtime.sourcePath, discoveredSkills, dryRun, force) } if syncErr != nil { ui.Error("%s: %v", name, syncErr) failedTargets++ } + totals.linked += s.linked + totals.local += s.local + totals.updated += s.updated + totals.pruned += s.pruned } stats.Failed = failedTargets + + // Phase 3: Summary + ui.SyncSummary(ui.SyncStats{ + Targets: len(runtime.config.Targets), + Linked: totals.linked, + Local: totals.local, + Updated: totals.updated, + Pruned: totals.pruned, + Duration: time.Since(start), + }) + if failedTargets > 0 { return stats, fmt.Errorf("some targets failed to sync") } diff --git a/internal/server/handler_git.go b/internal/server/handler_git.go index 61b0e07f9..c778aa72e 100644 --- a/internal/server/handler_git.go +++ b/internal/server/handler_git.go @@ -218,6 +218,9 @@ func (s *Server) handlePull(w http.ResponseWriter, r *http.Request) { globalMode = "merge" } + // Discover skills once for all targets + allSkills, discoverErr := ssync.DiscoverSourceSkills(src) + for name, target := range s.cfg.Targets { mode := target.Mode if mode == "" { @@ -232,18 +235,38 @@ func (s *Server) handlePull(w http.ResponseWriter, r *http.Request) { Pruned: make([]string, 0), } - if mode == "merge" { - mergeResult, err := ssync.SyncTargetMerge(name, target, src, false, false) + if discoverErr != nil { + resp.SyncResults = append(resp.SyncResults, res) + continue + } + + switch mode { + case "merge": + mergeResult, err := ssync.SyncTargetMergeWithSkills(name, target, allSkills, false, false) if err == nil { res.Linked = mergeResult.Linked res.Updated = mergeResult.Updated res.Skipped = mergeResult.Skipped } - pruneResult, err := ssync.PruneOrphanLinks(target.Path, src, target.Include, target.Exclude, name, false, false) + pruneResult, err := ssync.PruneOrphanLinksWithSkills(ssync.PruneOptions{ + TargetPath: target.Path, SourcePath: src, Skills: allSkills, + Include: target.Include, Exclude: target.Exclude, TargetName: name, + }) if err == nil { res.Pruned = pruneResult.Removed } - } else { + case "copy": + copyResult, err := ssync.SyncTargetCopyWithSkills(name, target, allSkills, false, false, nil) + if err == nil { + res.Linked = copyResult.Copied + res.Updated = copyResult.Updated + res.Skipped = copyResult.Skipped + } + pruneResult, err := ssync.PruneOrphanCopiesWithSkills(target.Path, allSkills, target.Include, target.Exclude, name, false) + if err == nil { + res.Pruned = pruneResult.Removed + } + default: ssync.SyncTarget(name, target, src, false) res.Linked = []string{"(symlink mode)"} } diff --git a/internal/server/handler_sync.go b/internal/server/handler_sync.go index c82c75b8b..d2632e749 100644 --- a/internal/server/handler_sync.go +++ b/internal/server/handler_sync.go @@ -37,6 +37,13 @@ func (s *Server) handleSync(w http.ResponseWriter, r *http.Request) { globalMode = "merge" } + // Discover skills once for all targets + allSkills, err := ssync.DiscoverSourceSkills(s.cfg.Source) + if err != nil { + writeError(w, http.StatusInternalServerError, "failed to discover skills: "+err.Error()) + return + } + results := make([]syncTargetResult, 0) for name, target := range s.cfg.Targets { @@ -64,7 +71,7 @@ func (s *Server) handleSync(w http.ResponseWriter, r *http.Request) { switch mode { case "merge": - mergeResult, err := ssync.SyncTargetMerge(name, target, s.cfg.Source, body.DryRun, body.Force) + mergeResult, err := ssync.SyncTargetMergeWithSkills(name, target, allSkills, body.DryRun, body.Force) if err != nil { s.writeOpsLog("sync", "error", start, syncErrArgs, err.Error()) writeError(w, http.StatusInternalServerError, "sync failed for "+name+": "+err.Error()) @@ -74,13 +81,17 @@ func (s *Server) handleSync(w http.ResponseWriter, r *http.Request) { res.Updated = mergeResult.Updated res.Skipped = mergeResult.Skipped - pruneResult, err := ssync.PruneOrphanLinks(target.Path, s.cfg.Source, target.Include, target.Exclude, name, body.DryRun, body.Force) + pruneResult, err := ssync.PruneOrphanLinksWithSkills(ssync.PruneOptions{ + TargetPath: target.Path, SourcePath: s.cfg.Source, Skills: allSkills, + Include: target.Include, Exclude: target.Exclude, TargetName: name, + DryRun: body.DryRun, Force: body.Force, + }) if err == nil { res.Pruned = pruneResult.Removed } case "copy": - copyResult, err := ssync.SyncTargetCopy(name, target, s.cfg.Source, body.DryRun, body.Force) + copyResult, err := ssync.SyncTargetCopyWithSkills(name, target, allSkills, body.DryRun, body.Force, nil) if err != nil { s.writeOpsLog("sync", "error", start, syncErrArgs, err.Error()) writeError(w, http.StatusInternalServerError, "sync failed for "+name+": "+err.Error()) @@ -90,7 +101,7 @@ func (s *Server) handleSync(w http.ResponseWriter, r *http.Request) { res.Updated = copyResult.Updated res.Skipped = copyResult.Skipped - pruneResult, err := ssync.PruneOrphanCopies(target.Path, s.cfg.Source, target.Include, target.Exclude, name, body.DryRun) + pruneResult, err := ssync.PruneOrphanCopiesWithSkills(target.Path, allSkills, target.Include, target.Exclude, name, body.DryRun) if err == nil { res.Pruned = pruneResult.Removed } diff --git a/internal/sync/copy.go b/internal/sync/copy.go index e8984e047..23c02c6dc 100644 --- a/internal/sync/copy.go +++ b/internal/sync/copy.go @@ -23,6 +23,16 @@ type CopyResult struct { // SyncTargetCopy performs copy mode sync — copies each skill individually // while preserving target-specific (unmanaged) skills. func SyncTargetCopy(name string, target config.TargetConfig, sourcePath string, dryRun, force bool) (*CopyResult, error) { + skills, err := DiscoverSourceSkills(sourcePath) + if err != nil { + return nil, fmt.Errorf("failed to discover skills: %w", err) + } + return SyncTargetCopyWithSkills(name, target, skills, dryRun, force, nil) +} + +// SyncTargetCopyWithSkills is like SyncTargetCopy but accepts pre-discovered skills +// and an optional progress callback for per-skill UI updates. +func SyncTargetCopyWithSkills(name string, target config.TargetConfig, allSkills []DiscoveredSkill, dryRun, force bool, onProgress func(current, total int, skill string)) (*CopyResult, error) { result := &CopyResult{} // If target is currently a symlink (symlink mode), remove it to convert @@ -44,12 +54,8 @@ func SyncTargetCopy(name string, target config.TargetConfig, sourcePath string, } } - // Discover and filter source skills - discoveredSkills, err := DiscoverSourceSkills(sourcePath) - if err != nil { - return nil, fmt.Errorf("failed to discover skills: %w", err) - } - discoveredSkills, err = FilterSkills(discoveredSkills, target.Include, target.Exclude) + // Filter skills for this target + discoveredSkills, err := FilterSkills(allSkills, target.Include, target.Exclude) if err != nil { return nil, fmt.Errorf("failed to apply filters for target %s: %w", name, err) } @@ -61,7 +67,11 @@ func SyncTargetCopy(name string, target config.TargetConfig, sourcePath string, return nil, fmt.Errorf("failed to read manifest: %w", err) } - for _, skill := range discoveredSkills { + for i, skill := range discoveredSkills { + if onProgress != nil { + onProgress(i+1, len(discoveredSkills), skill.FlatName) + } + targetSkillPath := filepath.Join(target.Path, skill.FlatName) // Compute source checksum @@ -165,6 +175,15 @@ func SyncTargetCopy(name string, target config.TargetConfig, sourcePath string, // PruneOrphanCopies removes managed copies that no longer exist in source. func PruneOrphanCopies(targetPath, sourcePath string, include, exclude []string, targetName string, dryRun bool) (*PruneResult, error) { + allSourceSkills, err := DiscoverSourceSkills(sourcePath) + if err != nil { + return nil, fmt.Errorf("failed to discover skills for pruning: %w", err) + } + return PruneOrphanCopiesWithSkills(targetPath, allSourceSkills, include, exclude, targetName, dryRun) +} + +// PruneOrphanCopiesWithSkills is like PruneOrphanCopies but accepts pre-discovered skills. +func PruneOrphanCopiesWithSkills(targetPath string, allSourceSkills []DiscoveredSkill, include, exclude []string, targetName string, dryRun bool) (*PruneResult, error) { result := &PruneResult{} manifest, err := ReadManifest(targetPath) @@ -172,11 +191,6 @@ func PruneOrphanCopies(targetPath, sourcePath string, include, exclude []string, return nil, fmt.Errorf("failed to read manifest: %w", err) } - // Discover current source skills - allSourceSkills, err := DiscoverSourceSkills(sourcePath) - if err != nil { - return nil, fmt.Errorf("failed to discover skills for pruning: %w", err) - } managedSkills, err := FilterSkills(allSourceSkills, include, exclude) if err != nil { return nil, fmt.Errorf("failed to apply filters for pruning: %w", err) diff --git a/internal/sync/sync.go b/internal/sync/sync.go index 534dfd7d3..7ce72ed17 100644 --- a/internal/sync/sync.go +++ b/internal/sync/sync.go @@ -403,6 +403,16 @@ type MergeResult struct { // Supports nested skills: source path "personal/writing/email" becomes target symlink "personal__writing__email" // If force is true, local copies will be replaced with symlinks. func SyncTargetMerge(name string, target config.TargetConfig, sourcePath string, dryRun, force bool) (*MergeResult, error) { + skills, err := DiscoverSourceSkills(sourcePath) + if err != nil { + return nil, fmt.Errorf("failed to discover skills: %w", err) + } + return SyncTargetMergeWithSkills(name, target, skills, dryRun, force) +} + +// SyncTargetMergeWithSkills is like SyncTargetMerge but accepts pre-discovered skills, +// avoiding redundant filesystem walks when syncing multiple targets. +func SyncTargetMergeWithSkills(name string, target config.TargetConfig, allSkills []DiscoveredSkill, dryRun, force bool) (*MergeResult, error) { result := &MergeResult{} // Check if target is currently a symlink/junction (symlink mode) - need to convert to merge mode @@ -425,12 +435,8 @@ func SyncTargetMerge(name string, target config.TargetConfig, sourcePath string, } } - // Discover all skills recursively from source - discoveredSkills, err := DiscoverSourceSkills(sourcePath) - if err != nil { - return nil, fmt.Errorf("failed to discover skills: %w", err) - } - discoveredSkills, err = FilterSkills(discoveredSkills, target.Include, target.Exclude) + // Filter skills for this target + discoveredSkills, err := FilterSkills(allSkills, target.Include, target.Exclude) if err != nil { return nil, fmt.Errorf("failed to apply filters for target %s: %w", name, err) } @@ -526,23 +532,57 @@ type PruneResult struct { LocalDirs []string // User-created directories not managed by skillshare } +// PruneOptions holds parameters for PruneOrphanLinks / PruneOrphanCopies. +type PruneOptions struct { + TargetPath string + SourcePath string + Skills []DiscoveredSkill // pre-discovered; if nil, will be discovered from SourcePath + Include []string + Exclude []string + TargetName string + DryRun bool + Force bool +} + // PruneOrphanLinks removes target entries that are no longer managed by sync. // This includes: // 1. Source-linked entries excluded by include/exclude filters (remove from target) // 2. Orphan links/directories that no longer exist in source // 3. Unknown local directories (kept with warning) func PruneOrphanLinks(targetPath, sourcePath string, include, exclude []string, targetName string, dryRun, force bool) (*PruneResult, error) { + allSourceSkills, err := DiscoverSourceSkills(sourcePath) + if err != nil { + return nil, fmt.Errorf("failed to discover skills for pruning: %w", err) + } + return PruneOrphanLinksWithSkills(PruneOptions{ + TargetPath: targetPath, + SourcePath: sourcePath, + Skills: allSourceSkills, + Include: include, + Exclude: exclude, + TargetName: targetName, + DryRun: dryRun, + Force: force, + }) +} + +// PruneOrphanLinksWithSkills is like PruneOrphanLinks but accepts pre-discovered skills +// via PruneOptions, avoiding redundant filesystem walks. +func PruneOrphanLinksWithSkills(opts PruneOptions) (*PruneResult, error) { + targetPath := opts.TargetPath + sourcePath := opts.SourcePath + allSourceSkills := opts.Skills + include := opts.Include + exclude := opts.Exclude + targetName := opts.TargetName + dryRun := opts.DryRun + force := opts.Force result := &PruneResult{} // Read manifest for managed-directory detection (may be empty/absent) manifest, _ := ReadManifest(targetPath) manifestChanged := false - // Discover all skills from source, then filter to target-managed skills. - allSourceSkills, err := DiscoverSourceSkills(sourcePath) - if err != nil { - return nil, fmt.Errorf("failed to discover skills for pruning: %w", err) - } managedSkills, err := FilterSkills(allSourceSkills, include, exclude) if err != nil { return nil, fmt.Errorf("failed to apply filters for pruning: %w", err) diff --git a/internal/ui/pterm.go b/internal/ui/pterm.go index a268fa5bc..951f51c24 100644 --- a/internal/ui/pterm.go +++ b/internal/ui/pterm.go @@ -422,67 +422,33 @@ func UpdateNotification(currentVersion, latestVersion string) { box.Println(content) } -// SyncSummary prints a beautiful sync summary box +// SyncSummary prints a sync summary line. func SyncSummary(stats SyncStats) { + fmt.Println() // spacing + if !IsTTY() { - fmt.Printf("\n─── Sync Complete ───\n") - fmt.Printf(" Targets: %d Linked: %d Local: %d Updated: %d Pruned: %d\n", + fmt.Printf("Sync complete: %d targets, %d linked, %d local, %d updated, %d pruned", stats.Targets, stats.Linked, stats.Local, stats.Updated, stats.Pruned) if stats.Duration > 0 { - fmt.Printf(" Duration: %.1fs\n", stats.Duration.Seconds()) + fmt.Printf(" (%.1fs)", stats.Duration.Seconds()) } + fmt.Println() return } - // Build stats line with colors - statsLine := fmt.Sprintf( - " %s targets %s linked %s local %s updated %s pruned", + line := fmt.Sprintf( + "%s %s targets %s linked %s local %s updated %s pruned", + pterm.Green("✓ Sync complete"), pterm.Cyan(fmt.Sprint(stats.Targets)), pterm.Green(fmt.Sprint(stats.Linked)), pterm.Blue(fmt.Sprint(stats.Local)), pterm.Yellow(fmt.Sprint(stats.Updated)), pterm.Gray(fmt.Sprint(stats.Pruned)), ) - - var durationLine string if stats.Duration > 0 { - durationLine = fmt.Sprintf(" Completed in %s", pterm.Gray(fmt.Sprintf("%.1fs", stats.Duration.Seconds()))) + line += fmt.Sprintf(" %s", pterm.Gray(fmt.Sprintf("(%.1fs)", stats.Duration.Seconds()))) } - - // Build content with proper padding - lines := []string{"", statsLine} - if durationLine != "" { - lines = append(lines, durationLine) - } - lines = append(lines, "") - - // Find max display width - maxLen := 0 - for _, line := range lines { - w := displayWidth(line) - if w > maxLen { - maxLen = w - } - } - - // Pad lines - var content strings.Builder - for i, line := range lines { - padded := line - w := displayWidth(line) - if w < maxLen { - padded = line + strings.Repeat(" ", maxLen-w) - } - content.WriteString(padded) - if i < len(lines)-1 { - content.WriteString("\n") - } - } - - box := pterm.DefaultBox. - WithTitle(pterm.Green("✓ Sync Complete")). - WithBoxStyle(pterm.NewStyle(pterm.FgGreen)) - box.Println(content.String()) + fmt.Println(line) } // SyncStats holds statistics for sync summary diff --git a/tests/integration/log_test.go b/tests/integration/log_test.go index a10f9ab7b..d20f7ddef 100644 --- a/tests/integration/log_test.go +++ b/tests/integration/log_test.go @@ -461,7 +461,7 @@ targets: result.AssertSuccess(t) result.AssertOutputContains(t, "Operation Log Summary") result.AssertOutputContains(t, "sync") - result.AssertOutputContains(t, "Success rate") + result.AssertOutputContains(t, "OK:") } func TestLog_CheckCreatesEntry(t *testing.T) {