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

320 lines
8.9 KiB
Go

//go:build !online
package integration
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"skillshare/internal/testutil"
)
func TestPush_NoConfig_ReturnsError(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
os.Remove(sb.ConfigPath)
result := sb.RunCLI("push")
result.AssertFailure(t)
result.AssertAnyOutputContains(t, "config not found")
}
func TestPush_NoGitRepo_ShowsError(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets: {}
`)
result := sb.RunCLI("push")
result.AssertFailure(t)
result.AssertOutputContains(t, "not a git repository")
}
func TestPush_NoRemote_ShowsError(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets: {}
`)
// Initialize git but no remote
cmd := exec.Command("git", "init")
cmd.Dir = sb.SourcePath
if err := cmd.Run(); err != nil {
t.Skip("git not available")
}
result := sb.RunCLI("push")
result.AssertFailure(t)
result.AssertOutputContains(t, "No git remote")
}
func TestPush_DryRun_ShowsWhatWouldBePushed(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets: {}
`)
// Initialize git with remote
initGitWithRemote(t, sb)
// Create a skill (uncommitted)
sb.CreateSkill("new-skill", map[string]string{"SKILL.md": "# New"})
result := sb.RunCLI("push", "--dry-run")
result.AssertSuccess(t)
result.AssertOutputContains(t, "Dry run — nothing was written")
result.AssertOutputContains(t, "would commit 1 file")
}
func TestPush_NoChanges_ShowsNoChanges(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets: {}
`)
// Initialize git with remote and commit everything
initGitWithRemote(t, sb)
commitAll(t, sb.SourcePath, "initial")
result := sb.RunCLI("push", "--dry-run")
result.AssertSuccess(t)
result.AssertOutputContains(t, "nothing to commit")
}
func TestPush_CustomMessage(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets: {}
`)
initGitWithRemote(t, sb)
sb.CreateSkill("test-skill", map[string]string{"SKILL.md": "# Test"})
result := sb.RunCLI("push", "-m", "Custom commit message", "--dry-run")
result.AssertSuccess(t)
result.AssertOutputContains(t, "Custom commit message")
}
func TestPush_ActualPush_ToLocalBareRepo(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets: {}
`)
// Create bare repo as "remote"
bareRepo := filepath.Join(sb.Home, "remote.git")
cmd := exec.Command("git", "init", "--bare", bareRepo)
if err := cmd.Run(); err != nil {
t.Skip("git not available")
}
// Initialize git and add remote
cmd = exec.Command("git", "init")
cmd.Dir = sb.SourcePath
cmd.Run()
cmd = exec.Command("git", "remote", "add", "origin", bareRepo)
cmd.Dir = sb.SourcePath
cmd.Run()
testutil.ConfigureGitUser(t, sb.SourcePath)
// Initial commit and push to set up tracking
cmd = exec.Command("git", "commit", "--allow-empty", "-m", "initial")
cmd.Dir = sb.SourcePath
cmd.Run()
cmd = exec.Command("git", "push", "-u", "origin", "master")
cmd.Dir = sb.SourcePath
if err := cmd.Run(); err != nil {
// Try main branch
cmd = exec.Command("git", "push", "-u", "origin", "main")
cmd.Dir = sb.SourcePath
cmd.Run()
}
// Create a skill
sb.CreateSkill("pushed-skill", map[string]string{"SKILL.md": "# Pushed"})
result := sb.RunCLI("push", "-m", "Test push")
result.AssertSuccess(t)
result.AssertOutputContains(t, "✓ Push")
}
func TestPush_NoUpstream_RemoteMain_LocalMaster_PushesToMain_NoMasterCreated(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets: {}
`)
bareRepo := testutil.SetupBareRemoteRepo(t, sb.Home)
testutil.SeedRemoteBranch(t, sb.Home, bareRepo, "main", map[string]string{
"README.md": "# main",
})
testutil.RunGit(t, sb.SourcePath, "init")
testutil.RunGit(t, sb.SourcePath, "remote", "add", "origin", bareRepo)
testutil.ConfigureGitUser(t, sb.SourcePath)
testutil.RunGit(t, sb.SourcePath, "fetch", "origin")
testutil.RunGit(t, sb.SourcePath, "checkout", "-b", "master", "origin/main")
testutil.RunGit(t, sb.SourcePath, "branch", "--unset-upstream")
sb.CreateSkill("local-skill", map[string]string{"SKILL.md": "# Local"})
result := sb.RunCLI("push", "-m", "sync to main")
result.AssertSuccess(t)
result.AssertOutputContains(t, "✓ Push")
refs := testutil.RunGit(t, bareRepo, "for-each-ref", "--format=%(refname:short)", "refs/heads")
if !strings.Contains(refs, "main") {
t.Fatalf("expected remote to contain main branch, got: %s", refs)
}
if strings.Contains(refs, "master") {
t.Fatalf("expected no master branch to be created, got: %s", refs)
}
}
func TestPush_NoUpstream_RemoteCustomDefault_PushesToRemoteDefault(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets: {}
`)
bareRepo := testutil.SetupBareRemoteRepo(t, sb.Home)
testutil.SeedRemoteBranch(t, sb.Home, bareRepo, "trunk", map[string]string{
"README.md": "# trunk",
})
testutil.RunGit(t, sb.SourcePath, "init")
testutil.RunGit(t, sb.SourcePath, "remote", "add", "origin", bareRepo)
testutil.ConfigureGitUser(t, sb.SourcePath)
testutil.RunGit(t, sb.SourcePath, "fetch", "origin")
testutil.RunGit(t, sb.SourcePath, "checkout", "-b", "feature", "origin/trunk")
testutil.RunGit(t, sb.SourcePath, "branch", "--unset-upstream")
sb.CreateSkill("local-skill", map[string]string{"SKILL.md": "# Local"})
result := sb.RunCLI("push", "-m", "sync to trunk")
result.AssertSuccess(t)
result.AssertOutputContains(t, "✓ Push")
upstream := testutil.RunGit(t, sb.SourcePath, "rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}")
if upstream != "origin/trunk" {
t.Fatalf("expected upstream origin/trunk, got %q", upstream)
}
refs := testutil.RunGit(t, bareRepo, "for-each-ref", "--format=%(refname:short)", "refs/heads")
if !strings.Contains(refs, "trunk") {
t.Fatalf("expected remote to contain trunk branch, got: %s", refs)
}
if strings.Contains(refs, "feature") {
t.Fatalf("expected no feature branch to be created, got: %s", refs)
}
}
func TestPush_NoUpstream_RemoteEmpty_UsesLocalBranch(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets: {}
`)
bareRepo := testutil.SetupBareRemoteRepo(t, sb.Home)
testutil.RunGit(t, sb.SourcePath, "init")
testutil.RunGit(t, sb.SourcePath, "remote", "add", "origin", bareRepo)
testutil.ConfigureGitUser(t, sb.SourcePath)
testutil.RunGit(t, sb.SourcePath, "commit", "--allow-empty", "-m", "initial")
localBranch := testutil.RunGit(t, sb.SourcePath, "branch", "--show-current")
sb.CreateSkill("local-skill", map[string]string{"SKILL.md": "# Local"})
result := sb.RunCLI("push", "-m", "first push")
result.AssertSuccess(t)
result.AssertOutputContains(t, "✓ Push")
refs := testutil.RunGit(t, bareRepo, "for-each-ref", "--format=%(refname:short)", "refs/heads")
if !strings.Contains(refs, localBranch) {
t.Fatalf("expected remote to contain local branch %s, got: %s", localBranch, refs)
}
}
// Helper functions
func initGitWithRemote(t *testing.T, sb *testutil.Sandbox) {
cmd := exec.Command("git", "init")
cmd.Dir = sb.SourcePath
if err := cmd.Run(); err != nil {
t.Skip("git not available")
}
// Add a fake remote (won't actually push but passes remote check)
cmd = exec.Command("git", "remote", "add", "origin", "git@github.com:test/test.git")
cmd.Dir = sb.SourcePath
cmd.Run()
testutil.ConfigureGitUser(t, sb.SourcePath)
}
func commitAll(t *testing.T, dir, message string) {
testutil.RunGit(t, dir, "add", "-A")
testutil.RunGit(t, dir, "commit", "-m", message, "--allow-empty")
}
// Git exports GIT_DIR to hooks; push run from one must commit in the source
// repo, not the hook's repository (#852).
func TestPush_IgnoresCallerGitDir(t *testing.T) {
sb := testutil.NewSandbox(t)
defer sb.Cleanup()
sb.WriteConfig(`source: ` + sb.SourcePath + `
targets: {}
`)
bareRepo := filepath.Join(sb.Home, "remote.git")
testutil.RunGit(t, "", "init", "--bare", bareRepo)
testutil.RunGit(t, sb.SourcePath, "init")
testutil.RunGit(t, sb.SourcePath, "remote", "add", "origin", bareRepo)
testutil.ConfigureGitUser(t, sb.SourcePath)
caller := filepath.Join(sb.Home, "caller")
testutil.RunGit(t, "", "init", caller)
testutil.ConfigureGitUser(t, caller)
testutil.RunGit(t, caller, "remote", "add", "origin", filepath.Join(sb.Home, "caller-remote.git"))
testutil.RunGit(t, caller, "commit", "--allow-empty", "-m", "caller")
sb.CreateSkill("pushed-skill", map[string]string{"SKILL.md": "# Pushed"})
sb.RunCLIEnv(map[string]string{"GIT_DIR": filepath.Join(caller, ".git")}, "push", "-m", "Test push")
if got := testutil.RunGit(t, caller, "rev-list", "--count", "HEAD"); got != "1" {
t.Errorf("caller repository has %s commits, want 1", got)
}
}