Files
skillshare/internal/install/sparse_checkout_test.go
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

118 lines
3.5 KiB
Go

package install
import (
"os"
"os/exec"
"path/filepath"
"testing"
)
func TestSupportsSparseCheckoutVersion(t *testing.T) {
tests := []struct {
version string
want bool
}{
{version: "git version 2.24.9", want: false},
{version: "git version 2.25.0", want: true},
{version: "git version 2.39.3 (Apple Git-146)", want: true},
{version: "git version 3.0.0", want: true},
{version: "invalid", want: false},
}
for _, tt := range tests {
got := supportsSparseCheckoutVersion(tt.version)
if got != tt.want {
t.Fatalf("supportsSparseCheckoutVersion(%q) = %v, want %v", tt.version, got, tt.want)
}
}
}
func TestSparseCloneSubdir_FileURL(t *testing.T) {
if _, err := exec.LookPath("git"); err != nil {
t.Skip("git not available")
}
if !gitSupportsSparseCheckout() {
t.Skip("git does not support sparse-checkout")
}
remote := sparseFixtureRemote(t)
dest := filepath.Join(t.TempDir(), "clone")
if err := sparseCloneSubdir("file://"+remote, "skills/one", dest, "", nil, nil); err != nil {
t.Fatalf("sparseCloneSubdir() error = %v", err)
}
if _, err := os.Stat(filepath.Join(dest, "skills", "one", "SKILL.md")); err != nil {
t.Fatalf("expected sparse path file to exist: %v", err)
}
if _, err := os.Stat(filepath.Join(dest, "other", "README.md")); err == nil {
t.Fatalf("expected non-sparse path to be absent")
}
}
// Git exports GIT_DIR to hooks in a linked worktree; a hook running install
// must not turn on sparse checkout in that repository (#852).
func TestSparseCloneSubdir_IgnoresCallerGitDir(t *testing.T) {
if _, err := exec.LookPath("git"); err != nil {
t.Skip("git not available")
}
if !gitSupportsSparseCheckout() {
t.Skip("git does not support sparse-checkout")
}
remote := sparseFixtureRemote(t)
host := filepath.Join(t.TempDir(), "host")
mustRunGit(t, "", "init", host)
mustRunGit(t, host, "-c", "user.email=test@test.com", "-c", "user.name=Test", "commit", "--allow-empty", "-m", "init")
dest := filepath.Join(t.TempDir(), "clone")
t.Setenv("GIT_DIR", filepath.Join(host, ".git"))
if err := sparseCloneSubdir("file://"+remote, "skills/one", dest, "", nil, nil); err != nil {
t.Fatalf("sparseCloneSubdir() error = %v", err)
}
if _, err := os.Stat(filepath.Join(host, ".git", "info", "sparse-checkout")); err == nil {
t.Fatal("sparse checkout was set up in the caller's repository")
}
}
// sparseFixtureRemote returns a bare repository holding skills/one and other.
func sparseFixtureRemote(t *testing.T) string {
t.Helper()
tmp := t.TempDir()
work := filepath.Join(tmp, "work")
remote := filepath.Join(tmp, "remote.git")
mustRunGit(t, "", "init", work)
mustRunGit(t, work, "config", "user.email", "test@test.com")
mustRunGit(t, work, "config", "user.name", "Test")
if err := os.MkdirAll(filepath.Join(work, "skills", "one"), 0755); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(filepath.Join(work, "other"), 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(work, "skills", "one", "SKILL.md"), []byte("# one"), 0644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(work, "other", "README.md"), []byte("other"), 0644); err != nil {
t.Fatal(err)
}
mustRunGit(t, work, "add", ".")
mustRunGit(t, work, "commit", "-m", "init")
mustRunGit(t, "", "clone", "--bare", work, remote)
return remote
}
func mustRunGit(t *testing.T, dir string, args ...string) {
t.Helper()
cmd := exec.Command("git", args...)
cmd.Dir = dir
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("git %v failed: %v\n%s", args, err, out)
}
}