mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-20 12:23:22 +00:00
perf(cli): parallelize git dirty checks in status --json
Extract buildTrackedRepoJSON() that runs git.IsDirty concurrently via goroutines + WaitGroup instead of sequential subprocess spawns per repo. Also deduplicates the tracked-repo JSON loop between global and project status paths, and optimizes skill counting to a single pass with a map.
This commit is contained in:
@@ -305,15 +305,31 @@ func cmdInstall(args []string) error {
|
||||
|
||||
// In JSON mode, redirect all UI output to stderr early so the
|
||||
// logo, spinner, step, and handler output don't corrupt stdout.
|
||||
var restoreJSONUI func()
|
||||
restoreJSONUIIfNeeded := func() {
|
||||
if restoreJSONUI != nil {
|
||||
restoreJSONUI()
|
||||
restoreJSONUI = nil
|
||||
}
|
||||
}
|
||||
if parsed.jsonOutput {
|
||||
restoreUI := suppressUIToDevnull()
|
||||
defer restoreUI()
|
||||
restoreJSONUI = suppressUIToDevnull()
|
||||
}
|
||||
defer restoreJSONUIIfNeeded()
|
||||
|
||||
jsonWriteError := func(err error) error {
|
||||
restoreJSONUIIfNeeded()
|
||||
return writeJSONError(err)
|
||||
}
|
||||
jsonWriteResult := func(summary installLogSummary, cmdErr error) error {
|
||||
restoreJSONUIIfNeeded()
|
||||
return installOutputJSON(summary, start, cmdErr)
|
||||
}
|
||||
|
||||
cfg, err := config.Load()
|
||||
if err != nil {
|
||||
if parsed.jsonOutput {
|
||||
return writeJSONError(err)
|
||||
return jsonWriteError(err)
|
||||
}
|
||||
return fmt.Errorf("failed to load config: %w", err)
|
||||
}
|
||||
@@ -326,7 +342,7 @@ func cmdInstall(args []string) error {
|
||||
summary, err := installFromGlobalConfig(cfg, parsed.opts)
|
||||
logInstallOp(config.ConfigPath(), rest, start, err, summary)
|
||||
if parsed.jsonOutput {
|
||||
return installOutputJSON(summary, start, err)
|
||||
return jsonWriteResult(summary, err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -338,7 +354,7 @@ func cmdInstall(args []string) error {
|
||||
Mode: "global",
|
||||
})
|
||||
if parsed.jsonOutput {
|
||||
return writeJSONError(err)
|
||||
return jsonWriteError(err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -373,7 +389,7 @@ func cmdInstall(args []string) error {
|
||||
}
|
||||
logInstallOp(config.ConfigPath(), rest, start, err, summary)
|
||||
if parsed.jsonOutput {
|
||||
return installOutputJSON(summary, start, err)
|
||||
return jsonWriteResult(summary, err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -395,7 +411,7 @@ func cmdInstall(args []string) error {
|
||||
}
|
||||
logInstallOp(config.ConfigPath(), rest, start, err, summary)
|
||||
if parsed.jsonOutput {
|
||||
return installOutputJSON(summary, start, err)
|
||||
return jsonWriteResult(summary, err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
+39
-17
@@ -8,11 +8,12 @@ import (
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
gosync "sync"
|
||||
"time"
|
||||
|
||||
"skillshare/internal/audit"
|
||||
"skillshare/internal/git"
|
||||
"skillshare/internal/config"
|
||||
"skillshare/internal/git"
|
||||
"skillshare/internal/sync"
|
||||
"skillshare/internal/ui"
|
||||
)
|
||||
@@ -128,22 +129,8 @@ func cmdStatus(args []string) error {
|
||||
Version: version,
|
||||
}
|
||||
|
||||
// Tracked repos
|
||||
for _, repoName := range trackedRepos {
|
||||
repoPath := filepath.Join(cfg.Source, repoName)
|
||||
skillCount := 0
|
||||
for _, d := range discovered {
|
||||
if d.IsInRepo && strings.HasPrefix(d.RelPath, repoName+"/") {
|
||||
skillCount++
|
||||
}
|
||||
}
|
||||
dirty, _ := git.IsDirty(repoPath)
|
||||
output.TrackedRepos = append(output.TrackedRepos, statusJSONRepo{
|
||||
Name: repoName,
|
||||
SkillCount: skillCount,
|
||||
Dirty: dirty,
|
||||
})
|
||||
}
|
||||
// Tracked repos (parallel dirty checks)
|
||||
output.TrackedRepos = buildTrackedRepoJSON(cfg.Source, trackedRepos, discovered)
|
||||
|
||||
// Targets
|
||||
for name, target := range cfg.Targets {
|
||||
@@ -244,6 +231,41 @@ func extractTrackedRepos(discovered []sync.DiscoveredSkill) []string {
|
||||
return repos
|
||||
}
|
||||
|
||||
// buildTrackedRepoJSON builds statusJSONRepo entries with parallel git.IsDirty checks.
|
||||
func buildTrackedRepoJSON(sourcePath string, trackedRepos []string, discovered []sync.DiscoveredSkill) []statusJSONRepo {
|
||||
results := make([]statusJSONRepo, len(trackedRepos))
|
||||
|
||||
// Count skills per repo (single pass)
|
||||
repoSkillCount := make(map[string]int, len(trackedRepos))
|
||||
for _, d := range discovered {
|
||||
if !d.IsInRepo {
|
||||
continue
|
||||
}
|
||||
idx := strings.Index(d.RelPath, "/")
|
||||
if idx > 0 {
|
||||
repoSkillCount[d.RelPath[:idx]]++
|
||||
}
|
||||
}
|
||||
|
||||
// Parallel git.IsDirty checks
|
||||
var wg gosync.WaitGroup
|
||||
for i, repoName := range trackedRepos {
|
||||
wg.Add(1)
|
||||
go func(idx int, name string) {
|
||||
defer wg.Done()
|
||||
repoPath := filepath.Join(sourcePath, name)
|
||||
dirty, _ := git.IsDirty(repoPath)
|
||||
results[idx] = statusJSONRepo{
|
||||
Name: name,
|
||||
SkillCount: repoSkillCount[name],
|
||||
Dirty: dirty,
|
||||
}
|
||||
}(i, repoName)
|
||||
}
|
||||
wg.Wait()
|
||||
return results
|
||||
}
|
||||
|
||||
// targetStatusResult bundles status detail with synced count to avoid
|
||||
// duplicate CheckStatusMerge/Copy calls.
|
||||
type targetStatusResult struct {
|
||||
|
||||
@@ -66,22 +66,8 @@ func cmdStatusProjectJSON(root string) error {
|
||||
Version: version,
|
||||
}
|
||||
|
||||
// Tracked repos
|
||||
for _, repoName := range trackedRepos {
|
||||
repoPath := filepath.Join(runtime.sourcePath, repoName)
|
||||
skillCount := 0
|
||||
for _, d := range discovered {
|
||||
if d.IsInRepo && strings.HasPrefix(d.RelPath, repoName+"/") {
|
||||
skillCount++
|
||||
}
|
||||
}
|
||||
dirty, _ := git.IsDirty(repoPath)
|
||||
output.TrackedRepos = append(output.TrackedRepos, statusJSONRepo{
|
||||
Name: repoName,
|
||||
SkillCount: skillCount,
|
||||
Dirty: dirty,
|
||||
})
|
||||
}
|
||||
// Tracked repos (parallel dirty checks)
|
||||
output.TrackedRepos = buildTrackedRepoJSON(runtime.sourcePath, trackedRepos, discovered)
|
||||
|
||||
// Targets
|
||||
for _, entry := range runtime.config.Targets {
|
||||
|
||||
@@ -93,7 +93,7 @@ func cmdSync(args []string) error {
|
||||
dryRun, force, jsonOutput := parseSyncFlags(rest)
|
||||
|
||||
if mode == modeProject {
|
||||
if hasAll {
|
||||
if hasAll && !jsonOutput {
|
||||
ui.Warning("--all is not supported in project mode (extras are global only)")
|
||||
}
|
||||
stats, results, err := cmdSyncProject(cwd, dryRun, force, jsonOutput)
|
||||
|
||||
@@ -167,9 +167,25 @@ func cmdUpdate(args []string) error {
|
||||
|
||||
// In JSON mode, redirect all UI output to stderr early so the
|
||||
// header, step, spinner, and handler output don't corrupt stdout.
|
||||
var restoreJSONUI func()
|
||||
restoreJSONUIIfNeeded := func() {
|
||||
if restoreJSONUI != nil {
|
||||
restoreJSONUI()
|
||||
restoreJSONUI = nil
|
||||
}
|
||||
}
|
||||
if opts.jsonOutput {
|
||||
restoreUI := suppressUIToDevnull()
|
||||
defer restoreUI()
|
||||
restoreJSONUI = suppressUIToDevnull()
|
||||
}
|
||||
defer restoreJSONUIIfNeeded()
|
||||
|
||||
jsonWriteError := func(err error) error {
|
||||
restoreJSONUIIfNeeded()
|
||||
return writeJSONError(err)
|
||||
}
|
||||
jsonWriteResult := func(result *updateResult, cmdErr error) error {
|
||||
restoreJSONUIIfNeeded()
|
||||
return updateOutputJSON(result, opts.dryRun, start, cmdErr)
|
||||
}
|
||||
|
||||
ui.Header(ui.WithModeLabel("Updating"))
|
||||
@@ -224,7 +240,7 @@ func cmdUpdate(args []string) error {
|
||||
scanSpinner.Stop()
|
||||
if err != nil {
|
||||
if opts.jsonOutput {
|
||||
return writeJSONError(err)
|
||||
return jsonWriteError(err)
|
||||
}
|
||||
return fmt.Errorf("failed to scan skills: %w", err)
|
||||
}
|
||||
@@ -320,7 +336,7 @@ func cmdUpdate(args []string) error {
|
||||
if len(targets) == 0 {
|
||||
if opts.all {
|
||||
if opts.jsonOutput {
|
||||
return updateOutputJSON(nil, opts.dryRun, start, nil)
|
||||
return jsonWriteResult(nil, nil)
|
||||
}
|
||||
ui.UpdateSummary(ui.UpdateStats{})
|
||||
return nil
|
||||
@@ -330,7 +346,7 @@ func cmdUpdate(args []string) error {
|
||||
noTargetsErr = fmt.Errorf("no skills found")
|
||||
}
|
||||
if opts.jsonOutput {
|
||||
return writeJSONError(noTargetsErr)
|
||||
return jsonWriteError(noTargetsErr)
|
||||
}
|
||||
return noTargetsErr
|
||||
}
|
||||
@@ -358,7 +374,7 @@ func cmdUpdate(args []string) error {
|
||||
}
|
||||
logUpdateOp(config.ConfigPath(), opNames, opts, "global", start, updateErr, &r)
|
||||
if opts.jsonOutput {
|
||||
return updateOutputJSON(&r, opts.dryRun, start, updateErr)
|
||||
return jsonWriteResult(&r, updateErr)
|
||||
}
|
||||
return updateErr
|
||||
}
|
||||
@@ -383,7 +399,7 @@ func cmdUpdate(args []string) error {
|
||||
logUpdateOp(config.ConfigPath(), opNames, opts, "global", start, batchErr, &batchResult)
|
||||
|
||||
if opts.jsonOutput {
|
||||
return updateOutputJSON(&batchResult, opts.dryRun, start, batchErr)
|
||||
return jsonWriteResult(&batchResult, batchErr)
|
||||
}
|
||||
return batchErr
|
||||
}
|
||||
|
||||
@@ -666,6 +666,23 @@ func TestSync_JSON_All_PureJSON(t *testing.T) {
|
||||
assertPureJSON(t, stdout)
|
||||
}
|
||||
|
||||
func TestSync_Project_JSON_All_PureJSON(t *testing.T) {
|
||||
sb := testutil.NewSandbox(t)
|
||||
defer sb.Cleanup()
|
||||
|
||||
projectDir := sb.Root + "/sync-project"
|
||||
sb.WriteFile(projectDir+"/.skillshare/config.yaml",
|
||||
"targets:\n - name: claude\n path: "+projectDir+"/.claude/commands\n")
|
||||
sb.WriteFile(projectDir+"/.claude/commands/.gitkeep", "")
|
||||
sb.WriteFile(projectDir+"/.skillshare/skills/alpha/SKILL.md", "# Alpha")
|
||||
|
||||
result := sb.RunCLIInDir(projectDir, "sync", "--project", "--all", "--json")
|
||||
result.AssertSuccess(t)
|
||||
|
||||
stdout := strings.TrimSpace(result.Stdout)
|
||||
assertPureJSON(t, stdout)
|
||||
}
|
||||
|
||||
// --- test helpers ---
|
||||
|
||||
// assertPureJSON verifies that s is a valid JSON object with no leading/trailing noise.
|
||||
|
||||
Reference in New Issue
Block a user