refactor(install): prefer sparse checkout over GitHub API for subdir install

Swap fast path order: sparse checkout is now fast path 1 (works for all
hosts including file://), GitHub Contents API is fast path 2 (fallback
when sparse is unavailable or fails). Also reset subdirPath on sparse
failure to ensure proper fallback chain.

Fix red team helpers to inject -g flag after subcommand, preventing
.skillshare/ auto-detection from hijacking global-mode tests.

Add blocked section assertions to TestUpdate_BatchAll_FailsOnMalicious.
This commit is contained in:
Willie
2026-02-25 07:02:54 +08:00
parent c321db5d9c
commit f8205a27a8
4 changed files with 56 additions and 42 deletions
+20 -18
View File
@@ -250,24 +250,9 @@ func installFromGitSubdir(source *Source, destPath string, result *InstallResult
var resolved string
var commitHash string
// Fast path 1: GitHub/GHE Contents API
if isGitHubAPISource(source) {
owner, repo := source.GitHubOwner(), source.GitHubRepo()
resolved = source.Subdir
subdirPath = filepath.Join(tempRepoPath, resolved)
hash, dlErr := downloadGitHubDir(owner, repo, source.Subdir, subdirPath, source, opts.OnProgress)
if dlErr == nil {
commitHash = hash
} else {
result.Warnings = append(result.Warnings, fmt.Sprintf("GitHub API install fallback: %v", dlErr))
subdirPath = ""
_ = os.RemoveAll(tempRepoPath)
}
}
// Fast path 2: sparse checkout fallback when API path is unavailable
// or unsupported (works for GitHub and non-GitHub hosts).
if subdirPath == "" && gitSupportsSparseCheckout() {
// Fast path 1: sparse checkout (preferred for speed if git is modern)
// Works for GitHub and non-GitHub hosts.
if gitSupportsSparseCheckout() {
resolved = source.Subdir
if err := sparseCloneSubdir(source.CloneURL, resolved, tempRepoPath, authEnv(source.CloneURL), opts.OnProgress); err == nil {
subdirPath = filepath.Join(tempRepoPath, resolved)
@@ -281,6 +266,23 @@ func installFromGitSubdir(source *Source, destPath string, result *InstallResult
} else {
result.Warnings = append(result.Warnings, fmt.Sprintf("sparse checkout install fallback: %v", err))
_ = os.RemoveAll(tempRepoPath)
subdirPath = ""
}
}
// Fast path 2: GitHub/GHE Contents API
// Fallback for when sparse checkout is unavailable or fails.
if subdirPath == "" && isGitHubAPISource(source) {
owner, repo := source.GitHubOwner(), source.GitHubRepo()
resolved = source.Subdir
subdirPath = filepath.Join(tempRepoPath, resolved)
hash, dlErr := downloadGitHubDir(owner, repo, source.Subdir, subdirPath, source, opts.OnProgress)
if dlErr == nil {
commitHash = hash
} else {
result.Warnings = append(result.Warnings, fmt.Sprintf("GitHub API install fallback: %v", dlErr))
subdirPath = ""
_ = os.RemoveAll(tempRepoPath)
}
}
+25 -21
View File
@@ -206,27 +206,8 @@ func discoverFromGitSubdirWithProgressImpl(source *Source, onProgress ProgressCa
var commitHash string
var subdirPath string
// Fast path 1: GitHub/GHE Contents API
if isGitHubAPISource(source) {
owner, repo := source.GitHubOwner(), source.GitHubRepo()
subdirPath = filepath.Join(repoPath, source.Subdir)
hash, dlErr := downloadGitHubDir(owner, repo, source.Subdir, subdirPath, source, onProgress)
if dlErr == nil {
commitHash = hash
skills := discoverSkills(subdirPath, true)
return &DiscoveryResult{
RepoPath: tempDir,
Skills: skills,
Source: source,
CommitHash: commitHash,
}, nil
}
warnings = append(warnings, fmt.Sprintf("GitHub API discovery fallback: %v", dlErr))
_ = os.RemoveAll(repoPath)
}
// Fast path 2: sparse checkout fallback when API path is unavailable
// or unsupported (works for GitHub and non-GitHub hosts).
// Fast path 1: sparse checkout (preferred for speed if git is modern)
// Works for GitHub and non-GitHub hosts.
if gitSupportsSparseCheckout() {
if err := sparseCloneSubdir(source.CloneURL, source.Subdir, repoPath, authEnv(source.CloneURL), onProgress); err == nil {
subdirPath = filepath.Join(repoPath, source.Subdir)
@@ -245,12 +226,35 @@ func discoverFromGitSubdirWithProgressImpl(source *Source, onProgress ProgressCa
}
warnings = append(warnings, "sparse checkout discovery fallback: subdirectory missing after checkout")
_ = os.RemoveAll(repoPath)
subdirPath = ""
} else {
warnings = append(warnings, fmt.Sprintf("sparse checkout discovery fallback: %v", err))
_ = os.RemoveAll(repoPath)
subdirPath = ""
}
}
// Fast path 2: GitHub/GHE Contents API
// Fallback for when sparse checkout is unavailable or fails.
if subdirPath == "" && isGitHubAPISource(source) {
owner, repo := source.GitHubOwner(), source.GitHubRepo()
subdirPath = filepath.Join(repoPath, source.Subdir)
hash, dlErr := downloadGitHubDir(owner, repo, source.Subdir, subdirPath, source, onProgress)
if dlErr == nil {
commitHash = hash
skills := discoverSkills(subdirPath, true)
return &DiscoveryResult{
RepoPath: tempDir,
Skills: skills,
Source: source,
CommitHash: commitHash,
}, nil
}
warnings = append(warnings, fmt.Sprintf("GitHub API discovery fallback: %v", dlErr))
_ = os.RemoveAll(repoPath)
subdirPath = ""
}
// Fallback: full clone + fuzzy subdir resolution
_ = os.RemoveAll(repoPath)
if onProgress != nil {
+7 -3
View File
@@ -49,17 +49,21 @@ $content
SKILL_EOF
}
# Run skillshare with isolated config
# Run skillshare with isolated config.
# -g is placed after the subcommand to force global mode,
# preventing auto-detection of .skillshare/ in the working directory.
ss() {
SKILLSHARE_CONFIG="$CONFIG_PATH" "$BIN" "$@" 2>&1
local cmd="$1"; shift
SKILLSHARE_CONFIG="$CONFIG_PATH" "$BIN" "$cmd" -g "$@" 2>&1
}
# Run skillshare, capture output + exit code
# Sets: SS_OUTPUT, SS_EXIT
ss_capture() {
local cmd="$1"; shift
SS_OUTPUT=""
SS_EXIT=0
SS_OUTPUT="$(SKILLSHARE_CONFIG="$CONFIG_PATH" "$BIN" "$@" 2>&1)" || SS_EXIT=$?
SS_OUTPUT="$(SKILLSHARE_CONFIG="$CONFIG_PATH" "$BIN" "$cmd" -g "$@" 2>&1)" || SS_EXIT=$?
}
# ── Assertion helpers ──────────────────────────────────────────────
+4
View File
@@ -421,6 +421,10 @@ func TestUpdate_BatchAll_FailsOnMalicious(t *testing.T) {
result.AssertFailure(t) // Must exit non-zero even though clean repo succeeded
result.AssertAnyOutputContains(t, "blocked by security audit")
// Blocked section shows details (not just a count)
result.AssertAnyOutputContains(t, "Blocked / Rolled Back")
result.AssertAnyOutputContains(t, maliciousName)
// Clean repo should be updated
cleanContent := sb.ReadFile(filepath.Join(sb.SourcePath, cleanName, "my-skill", "SKILL.md"))
if !contains(cleanContent, "Updated clean") {