mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-19 02:17:09 +00:00
132 lines
2.7 KiB
Go
132 lines
2.7 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
|
|
Message string
|
|
}
|
|
|
|
// URLCheckInput describes a remote URL to probe.
|
|
type URLCheckInput struct {
|
|
RepoURL string
|
|
}
|
|
|
|
// URLCheckOutput holds the result of probing a remote URL.
|
|
type URLCheckOutput struct {
|
|
RepoURL string
|
|
RemoteHash 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}
|
|
|
|
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 := git.GetRemoteHeadHashWithAuth(input.RepoURL)
|
|
outputs[idx] = URLCheckOutput{
|
|
RepoURL: input.RepoURL,
|
|
RemoteHash: hash,
|
|
Err: err,
|
|
}
|
|
if onDone != nil {
|
|
onDone()
|
|
}
|
|
}(i, u)
|
|
}
|
|
wg.Wait()
|
|
|
|
return outputs
|
|
}
|