Files
Willie 828ba93f27 fix(git): keep a caller's GIT_DIR out of skillshare's git commands (#854)
* fix(git): keep a caller's GIT_DIR out of skillshare's git commands

Git exports GIT_DIR, GIT_WORK_TREE, GIT_INDEX_FILE and related variables
to hooks that run in a linked worktree. Every git process skillshare
started inherited them, and those variables override the directory the
command runs in. Run from a post-checkout or post-merge hook, skillshare
therefore acted on the hook's repository instead of its own: install's
sparse clone turned on sparse checkout there (a later merge removed every
file outside the skill's path), the remote tree check's `git init --bare`
made it a bare repository, install's reconcile recorded its origin and
branch, and push staged and committed into it.

internal/gitenv builds git commands without the variables that
`git rev-parse --local-env-vars` lists for choosing a repository, work
tree, index, object store or config file. GIT_CONFIG_COUNT and
GIT_CONFIG_PARAMETERS stay, because users pass credentials through them
and install's token auth appends to the existing count. Every git call in
cmd/skillshare, internal/install, internal/git, internal/check and
internal/config now goes through it, and plugin runCommand starts native
CLIs from the same environment because they run git as well. The hooks
service keeps its own stricter filter.

Closes #852

* test(gitenv): fail on new git commands that bypass gitenv

Each git call site converted for #852 is safe only because it goes
through gitenv.Command, and about half have no test of their own. A
future exec.Command("git", ...) would silently bring the bug back, so a
syntactic guard now rejects one outside gitenv, hooks and testutil,
mirroring the sourcefs and sourcewalk ratchets.

The Environ test also checks a lowercase name, since Windows environment
names are case-insensitive, and compares against literal keys so that
removing one from repoVars fails it. The wiki records the rule.
2026-11-11 21:08:47 +08:00

84 lines
2.2 KiB
Go

package install
import (
"fmt"
"strconv"
"strings"
"skillshare/internal/gitenv"
)
func gitSupportsSparseCheckout() bool {
out, err := gitenv.Command("--version").Output()
if err != nil {
return false
}
return supportsSparseCheckoutVersion(strings.TrimSpace(string(out)))
}
func supportsSparseCheckoutVersion(versionOutput string) bool {
// Examples:
// git version 2.44.0
// git version 2.39.3 (Apple Git-146)
fields := strings.Fields(versionOutput)
if len(fields) < 3 {
return false
}
ver := fields[2]
parts := strings.Split(ver, ".")
if len(parts) < 2 {
return false
}
major, err := strconv.Atoi(parts[0])
if err != nil {
return false
}
minor, err := strconv.Atoi(parts[1])
if err != nil {
return false
}
if major > 2 {
return true
}
return major == 2 && minor >= 25
}
func sparseCloneSubdir(url, subdir, destPath, branch string, extraEnv []string, onProgress ProgressCallback) error {
subdir = strings.TrimSpace(subdir)
if subdir == "" {
return fmt.Errorf("sparse checkout requires non-empty subdir")
}
cloneArgs := []string{"clone", "--filter=blob:none", "--no-checkout", "--depth", "1"}
// clone --branch rejects a commit SHA: clone the default branch and fetch
// the commit below instead.
sha := IsCommitSHA(branch)
if branch != "" && !sha {
cloneArgs = append(cloneArgs, "--branch", branch)
}
if onProgress != nil {
cloneArgs = append(cloneArgs, "--progress")
} else {
cloneArgs = append(cloneArgs, "--quiet")
}
cloneArgs = append(cloneArgs, url, destPath)
if err := runGitCommandWithProgress(cloneArgs, "", extraEnv, onProgress); err != nil {
return err
}
if err := runGitCommandWithProgress([]string{"sparse-checkout", "set", subdir}, destPath, extraEnv, nil); err != nil {
return err
}
if sha {
// Servers only resolve a full SHA here; an abbreviated one fails and the
// caller falls back to a full clone.
if err := runGitCommandWithProgress([]string{"fetch", "--quiet", "--depth", "1", "--filter=blob:none", "origin", branch}, destPath, extraEnv, nil); err != nil {
return err
}
return runGitCommandWithProgress([]string{"checkout", "--quiet", "--detach", "FETCH_HEAD"}, destPath, extraEnv, nil)
}
return runGitCommandWithProgress([]string{"checkout"}, destPath, extraEnv, nil)
}