Files
skillshare/internal/check/parallel.go
T
Willie 1bc009c668 refactor(check): resolve update status in internal/check
The CLI, GET /api/check and the check stream each wrote out the same
sequence: group skills by URL and branch, ask the remote hash, skip the
tree fetch when every skill matches, otherwise compare tree hashes and
assign a status. A fix such as branch handling had to be made three
times, and the sequence could only be tested against real repositories.

internal/check now owns it as Plan and Resolution.Run, with the remote
hash source injected and a per-remote progress callback. The CLI
progress bar and the SSE counter are adapters on that callback; the JSON
handler passes none.

The callers disagreed in two places, and both behaviours are kept: the
dashboard reports a skill removed upstream as update_available where the
CLI says stale, and gives no source or version for a skill without a
remote. The server adapter narrows the result accordingly.

Refs #444
2026-10-07 00:03:34 +08:00

139 lines
2.9 KiB
Go

package check
import (
"sync"
"skillshare/internal/git"
)
const maxWorkers = 8
// RepoCheckInput describes a tracked repo to check.
type RepoCheckInput struct {
Name string
RepoPath string
}
// RepoCheckOutput holds the result of checking a single repo.
type RepoCheckOutput struct {
Name string
Status string // "up_to_date", "behind", "dirty", "error"
Behind int
Branch string
Message string
}
// URLCheckInput describes a remote URL to probe.
type URLCheckInput struct {
RepoURL string
Branch string
}
// URLCheckOutput holds the result of probing a remote URL.
type URLCheckOutput struct {
RepoURL string
RemoteHash string
Branch string
Err error
}
// ParallelCheckRepos checks multiple repos concurrently using a bounded
// semaphore of maxWorkers goroutines. onDone (if non-nil) is called after
// each repo finishes, useful for progress updates.
func ParallelCheckRepos(repos []RepoCheckInput, onDone func()) []RepoCheckOutput {
outputs := make([]RepoCheckOutput, len(repos))
if len(repos) == 0 {
return outputs
}
sem := make(chan struct{}, maxWorkers)
var wg sync.WaitGroup
for i, repo := range repos {
wg.Add(1)
sem <- struct{}{}
go func(idx int, r RepoCheckInput) {
defer wg.Done()
defer func() { <-sem }()
outputs[idx] = checkOneRepo(r)
if onDone != nil {
onDone()
}
}(i, repo)
}
wg.Wait()
return outputs
}
func checkOneRepo(r RepoCheckInput) RepoCheckOutput {
out := RepoCheckOutput{Name: r.Name}
if branch, err := git.GetCurrentBranch(r.RepoPath); err == nil {
out.Branch = branch
}
isDirty, err := git.IsDirty(r.RepoPath)
if err != nil {
out.Status = "error"
out.Message = err.Error()
return out
}
if isDirty {
out.Status = "dirty"
out.Message = "has uncommitted changes"
return out
}
behind, err := git.GetBehindCountWithAuth(r.RepoPath)
if err != nil {
out.Status = "error"
out.Message = err.Error()
return out
}
if behind == 0 {
out.Status = "up_to_date"
} else {
out.Status = "behind"
out.Behind = behind
}
return out
}
// ParallelCheckURLs checks unique repo URLs concurrently with bounded workers.
// onDone is called after each URL completes (may be nil).
// Returns []URLCheckOutput aligned by index with the input slice.
func ParallelCheckURLs(urls []URLCheckInput, onDone func()) []URLCheckOutput {
outputs := make([]URLCheckOutput, len(urls))
if len(urls) == 0 {
return outputs
}
sem := make(chan struct{}, maxWorkers)
var wg sync.WaitGroup
for i, u := range urls {
wg.Add(1)
sem <- struct{}{}
go func(idx int, input URLCheckInput) {
defer wg.Done()
defer func() { <-sem }()
hash, err := GitRemote{}.Hash(input.RepoURL, input.Branch)
outputs[idx] = URLCheckOutput{
RepoURL: input.RepoURL,
RemoteHash: hash,
Branch: input.Branch,
Err: err,
}
if onDone != nil {
onDone()
}
}(i, u)
}
wg.Wait()
return outputs
}