mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-20 12:23:22 +00:00
Users link a checkout directly under the skills source (e.g. skills/_dev-skills -> ~/code/dev-skills) and expect it to behave like any other first-level entry. sourcewalk resolved only the root, so these links were invisible to discovery, sync, update, and the dashboard. follow_source_links (global and project config, default false) enables a per-operation sourcewalk.Follow policy. Walk, WalkDir, and ReadDir report a followed link under its own name as a directory and descend into its target, so names, RelPath, FlatName, SourcePath, tracked-repo detection, and metadata keys stay logical. Only entries directly under the source root are followed. The policy is built from config and passed explicitly to every discovery caller in the CLI, server, and reconcile, so counts agree; the server keeps a per-instance snapshot refreshed on config load and save instead of a process-global flag. A link is skipped, with one warning per operation, when its target is missing or unreadable, is the source or a parent of it, or overlaps an active skills target. A missing or unreadable target marks the operation incomplete: sync then skips pruning and copy-orphan deletion, and reconcile keeps metadata, so an unmounted drive or a re-clone in progress cannot erase target entries or install records. A link to an existing file is not a directory link and stays an ordinary entry; treating it as incomplete would disable pruning for anyone sharing a file such as .skillignore. doctor keeps raw link identity: with the key off it still says the link is not followed and names follow_source_links: true; with it on it reports followed links and skipped links with the reason. The raw-walk allowlist gains entries for the policy path in Walk and WalkDir: the source root walk with a policy, and the walk of a validated link target whose paths are remapped to the logical link path. Both live in sourcewalk itself, the package that owns source traversal. Refs #274
220 lines
5.3 KiB
Go
220 lines
5.3 KiB
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"skillshare/internal/check"
|
|
"skillshare/internal/install"
|
|
)
|
|
|
|
// handleCheckStream serves an SSE endpoint that streams check progress in real time.
|
|
// Events:
|
|
// - "discovering" → {"phase":"..."} immediately on connect
|
|
// - "start" → {"total": N, "repos": R, "sources": S} after discovery (N = work units)
|
|
// - "progress" → {"checked": N} every 200ms
|
|
// - "done" → {"tracked_repos":…,"skills":…} final payload (same shape as GET /api/check)
|
|
//
|
|
// "total" counts actual work units (repos + remote URL groups), NOT individual skills.
|
|
// This ensures the progress bar advances evenly — one tick per network call.
|
|
func (s *Server) handleCheckStream(w http.ResponseWriter, r *http.Request) {
|
|
safeSend, ok := initSSE(w)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
ctx := r.Context()
|
|
|
|
// Snapshot config under RLock, then release before slow I/O.
|
|
s.mu.RLock()
|
|
sourceDir := s.skillsSource()
|
|
projectRoot := s.projectRoot
|
|
s.mu.RUnlock()
|
|
|
|
// Immediate feedback before the potentially slow discovery walk.
|
|
safeSend("discovering", map[string]string{"phase": "scanning source directory"})
|
|
|
|
repos, _ := install.GetTrackedRepos(sourceDir, s.skillsWalk())
|
|
skills, _ := install.GetUpdatableSkills(sourceDir)
|
|
|
|
// --- Pre-process: group skills by URL (fast, local only) ---
|
|
urlGroups := make(map[urlBranchGroup][]skillWithMetaEntry)
|
|
var localResults []skillCheckResult
|
|
|
|
for _, skill := range skills {
|
|
entry := s.skillEntry(skill)
|
|
if entry == nil || entry.RepoURL == "" {
|
|
localResults = append(localResults, localCheckResult(skill, entry, projectRoot))
|
|
continue
|
|
}
|
|
key := urlBranchGroup{url: entry.RepoURL, branch: entry.Branch}
|
|
urlGroups[key] = append(urlGroups[key], skillWithMetaEntry{
|
|
name: skill,
|
|
entry: entry,
|
|
})
|
|
}
|
|
|
|
// Total = repos + URL groups (the actual network-bound work units).
|
|
total := len(repos) + len(urlGroups)
|
|
safeSend("start", map[string]any{
|
|
"total": total,
|
|
"repos": len(repos),
|
|
"sources": len(urlGroups),
|
|
})
|
|
|
|
// Atomic counter + ticker for progress events
|
|
var checked atomic.Int64
|
|
done := make(chan struct{})
|
|
var wg sync.WaitGroup
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
defer wg.Done()
|
|
ticker := time.NewTicker(200 * time.Millisecond)
|
|
defer ticker.Stop()
|
|
for {
|
|
select {
|
|
case <-ticker.C:
|
|
safeSend("progress", map[string]int64{"checked": checked.Load()})
|
|
case <-done:
|
|
return
|
|
case <-ctx.Done():
|
|
return
|
|
}
|
|
}
|
|
}()
|
|
|
|
// --- Phase 1: Check tracked repos (1 work unit per repo) ---
|
|
var repoResults []repoCheckResult
|
|
for _, repo := range repos {
|
|
select {
|
|
case <-ctx.Done():
|
|
close(done)
|
|
wg.Wait()
|
|
return
|
|
default:
|
|
}
|
|
|
|
repoResults = append(repoResults, checkTrackedRepo(repo, filepath.Join(sourceDir, repo)))
|
|
checked.Add(1)
|
|
}
|
|
|
|
// --- Phase 2: Check skills by URL group (1 work unit per URL) ---
|
|
skillResults := append([]skillCheckResult{}, localResults...)
|
|
|
|
for key, group := range urlGroups {
|
|
select {
|
|
case <-ctx.Done():
|
|
close(done)
|
|
wg.Wait()
|
|
return
|
|
default:
|
|
}
|
|
|
|
remoteHash, err := key.remoteHash()
|
|
|
|
if err != nil {
|
|
for _, sw := range group {
|
|
r := skillCheckResult{
|
|
Name: sw.name,
|
|
Source: sw.entry.Source,
|
|
Version: sw.entry.Version,
|
|
Status: "error",
|
|
}
|
|
if !sw.entry.InstalledAt.IsZero() {
|
|
r.InstalledAt = sw.entry.InstalledAt.Format("2006-01-02")
|
|
}
|
|
skillResults = append(skillResults, r)
|
|
}
|
|
checked.Add(1)
|
|
continue
|
|
}
|
|
|
|
// Fast path: all commit hashes match
|
|
allMatch := true
|
|
for _, sw := range group {
|
|
if sw.entry.Version != remoteHash {
|
|
allMatch = false
|
|
break
|
|
}
|
|
}
|
|
if allMatch {
|
|
for _, sw := range group {
|
|
r := skillCheckResult{
|
|
Name: sw.name,
|
|
Source: sw.entry.Source,
|
|
Version: sw.entry.Version,
|
|
Status: "up_to_date",
|
|
}
|
|
if !sw.entry.InstalledAt.IsZero() {
|
|
r.InstalledAt = sw.entry.InstalledAt.Format("2006-01-02")
|
|
}
|
|
skillResults = append(skillResults, r)
|
|
}
|
|
checked.Add(1)
|
|
continue
|
|
}
|
|
|
|
// Slow path: tree hash comparison
|
|
var hasTreeHash bool
|
|
for _, sw := range group {
|
|
if sw.entry.TreeHash != "" && sw.entry.Subdir != "" {
|
|
hasTreeHash = true
|
|
break
|
|
}
|
|
}
|
|
|
|
var remoteTreeHashes map[string]string
|
|
if hasTreeHash {
|
|
remoteTreeHashes = check.FetchRemoteTreeHashesForRef(key.url, key.branch)
|
|
}
|
|
|
|
for _, sw := range group {
|
|
r := skillCheckResult{
|
|
Name: sw.name,
|
|
Source: sw.entry.Source,
|
|
Version: sw.entry.Version,
|
|
}
|
|
if !sw.entry.InstalledAt.IsZero() {
|
|
r.InstalledAt = sw.entry.InstalledAt.Format("2006-01-02")
|
|
}
|
|
|
|
if sw.entry.Version == remoteHash {
|
|
r.Status = "up_to_date"
|
|
} else if sw.entry.TreeHash != "" && sw.entry.Subdir != "" && remoteTreeHashes != nil {
|
|
normalizedSubdir := strings.TrimPrefix(sw.entry.Subdir, "/")
|
|
if rh, ok := remoteTreeHashes[normalizedSubdir]; ok && sw.entry.TreeHash == rh {
|
|
r.Status = "up_to_date"
|
|
} else {
|
|
r.Status = "update_available"
|
|
}
|
|
} else {
|
|
r.Status = "update_available"
|
|
}
|
|
|
|
skillResults = append(skillResults, r)
|
|
}
|
|
checked.Add(1)
|
|
}
|
|
|
|
// Stop ticker
|
|
close(done)
|
|
wg.Wait()
|
|
|
|
if repoResults == nil {
|
|
repoResults = []repoCheckResult{}
|
|
}
|
|
if skillResults == nil {
|
|
skillResults = []skillCheckResult{}
|
|
}
|
|
|
|
safeSend("done", map[string]any{
|
|
"tracked_repos": repoResults,
|
|
"skills": skillResults,
|
|
})
|
|
}
|