Files
skillshare/internal/check/parallel.go
T
Willie db69638e30 feat(install): project mode --branch support and branch display in check
- Set source.Branch in project install path
- Sync branch in project reconcile
- Add Branch to checkRepoResult and RepoCheckOutput for JSON/display
- Read current branch from tracked repos in check

Ref: #90
2026-04-01 00:32:49 +08:00

145 lines
3.0 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 }()
var hash string
var err error
if input.Branch != "" {
hash, err = git.GetRemoteRefHashWithAuth(input.RepoURL, input.Branch)
} else {
hash, err = git.GetRemoteHeadHashWithAuth(input.RepoURL)
}
outputs[idx] = URLCheckOutput{
RepoURL: input.RepoURL,
RemoteHash: hash,
Branch: input.Branch,
Err: err,
}
if onDone != nil {
onDone()
}
}(i, u)
}
wg.Wait()
return outputs
}