Files
skillshare/internal/check/tree_check.go
T
Willie 117b319050 feat(check): per-skill tree hash comparison for precise update detection
Previously `skillshare check` compared repo-level commit hashes, causing
all skills from a registry repo to show "update_available" whenever any
commit was pushed — even if the individual skill's files were unchanged.

Now each skill records its git tree hash (subdir-level SHA) at install
time. During check, a three-tier comparison is used:

1. Fast path: commit hash matches → up_to_date (zero extra cost)
2. Tree hash path: blobless fetch (~150KB) + batch ls-tree → per-skill
   comparison; unchanged subdirs stay up_to_date
3. Fallback: no tree hash (old installs) → commit comparison (unchanged)

Also fixes pterm progress bar printing twice when it auto-stops at 100%.
2026-02-25 07:02:54 +08:00

82 lines
2.2 KiB
Go

package check
import (
"os"
"os/exec"
"strings"
"skillshare/internal/install"
)
// FetchRemoteTreeHashes performs a blobless fetch of the remote HEAD and
// returns a map of subdir paths to their git tree hashes. This allows
// per-skill version comparison without downloading full blobs.
//
// Returns nil on any error (unsupported git version, network failure, etc.)
// so callers can fall back to commit-level comparison.
func FetchRemoteTreeHashes(repoURL string) map[string]string {
tmpDir, err := os.MkdirTemp("", "skillshare-treecheck-*")
if err != nil {
return nil
}
defer os.RemoveAll(tmpDir)
// Init bare repo
initCmd := exec.Command("git", "init", "--bare")
initCmd.Dir = tmpDir
if out, err := initCmd.CombinedOutput(); err != nil {
_ = out
return nil
}
// Blobless fetch: downloads only tree + commit objects (~150-200KB)
fetchArgs := []string{"fetch", "--filter=blob:none", "--depth=1", repoURL, "HEAD"}
fetchCmd := exec.Command("git", fetchArgs...)
fetchCmd.Dir = tmpDir
// Inject auth env for private repos
if extraEnv := install.AuthEnvForURL(repoURL); len(extraEnv) > 0 {
fetchCmd.Env = append(os.Environ(), extraEnv...)
}
if out, err := fetchCmd.CombinedOutput(); err != nil {
_ = out
return nil
}
// List all tree objects recursively
// Output format: "040000 tree <hash>\t<path>"
lsCmd := exec.Command("git", "ls-tree", "-r", "-d", "FETCH_HEAD")
lsCmd.Dir = tmpDir
out, err := lsCmd.Output()
if err != nil {
return nil
}
return parseLsTreeOutput(string(out))
}
// parseLsTreeOutput parses `git ls-tree -r -d` output into a map[path]treeHash.
// Each line: "040000 tree <hash>\t<path>"
func parseLsTreeOutput(output string) map[string]string {
result := make(map[string]string)
for _, line := range strings.Split(output, "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
}
// Split on tab: left side has "mode type hash", right side is path
parts := strings.SplitN(line, "\t", 2)
if len(parts) != 2 {
continue
}
path := parts[1]
// Extract hash from "mode type hash"
fields := strings.Fields(parts[0])
if len(fields) != 3 {
continue
}
hash := fields[2]
result[path] = hash
}
return result
}