Files
skillshare/internal/check/tree_check.go
T
Willie 0c69dad6c2 fix(update): stop trusting tree hashes for skills with symlinks (#573)
* fix(update): stop trusting tree hashes for skills with symlinks

Install copies a file symlink's target, but git stores the link as a blob
holding the target path, so a skill's subtree hash stays the same when a
target outside the folder changes. Batch update skipped such skills as
current and check reported them up to date, leaving stale content installed.

A subdir that holds a symlink now has no usable tree hash: install records
none, update falls back to a reinstall, and check reports the skill as
update_available. Check lists entries with -z so links with non-ASCII names
are not missed behind git's path quoting.

Closes #572

* fix(check): split ls-tree -z output on NUL only

parseLsTreeOutput also split on newlines so the old newline fixtures kept
passing, but with -z a newline belongs to the path. A directory or symlink
whose name holds a newline was cut in two, and a symlink under it was missed.
The fixtures now use NUL-terminated entries like real -z output.

Also writes both shortcut comments in the repo's one-line form and uses
testutil.RunGit in the new update test instead of a raw exec call.

* docs(update): note that skills with symlinks always reinstall

The update page says grouped updates leave skills whose folder did not
change alone. A skill with a symlink no longer has a usable tree hash, so it
is reinstalled whenever the upstream commit moves. State that exception on
the English page and its four translations.
2026-10-15 00:22:15 +08:00

103 lines
3.0 KiB
Go

package check
import (
"os"
"os/exec"
"path"
"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 {
return FetchRemoteTreeHashesForRef(repoURL, "")
}
// FetchRemoteTreeHashesForRef is FetchRemoteTreeHashes for a specific branch.
// An empty ref means the remote HEAD.
func FetchRemoteTreeHashesForRef(repoURL, ref string) map[string]string {
if ref == "" {
ref = "HEAD"
}
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, ref}
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 entries recursively, trees included; blob entries need no
// blob content, so the blobless fetch is enough. -z keeps paths unquoted.
// Output format: "040000 tree <hash>\t<path>\0"
lsCmd := exec.Command("git", "ls-tree", "-r", "-t", "-z", "FETCH_HEAD")
lsCmd.Dir = tmpDir
out, err := lsCmd.Output()
if err != nil {
return nil
}
return parseLsTreeOutput(string(out))
}
// parseLsTreeOutput parses `git ls-tree -r -t` output into a map[path]treeHash.
// Each entry: "<mode> <type> <hash>\t<path>", NUL-terminated. A directory
// that holds a symlink maps to "": install copies the link's target, whose
// changes its tree hash cannot see, so it must not count as unchanged.
func parseLsTreeOutput(output string) map[string]string {
result := make(map[string]string)
var symlinks []string
for _, line := range strings.Split(output, "\x00") {
// 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
}
if fields[0] == "120000" {
symlinks = append(symlinks, path)
}
if fields[1] != "tree" {
continue
}
result[path] = fields[2]
}
// shortcut: a link whose target stays inside the directory blanks it too, resolve link targets if skills report update_available too often.
for _, link := range symlinks {
for dir := path.Dir(link); dir != "."; dir = path.Dir(dir) {
result[dir] = ""
}
}
return result
}