Files
skillshare/internal/git/discard.go
T
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

46 lines
1.6 KiB
Go

package git
import (
"fmt"
"skillshare/internal/gitenv"
)
// DiscardChanges restores tracked files and the index to HEAD, and removes
// untracked files. Ignored files and nested repositories are kept. At root
// scope, the machine-specific config.yaml is also kept, even if tracked.
func DiscardChanges(dir string, preserveConfig bool) error {
paths := []string{"."}
cleanArgs := []string{"clean", "-fd"}
if preserveConfig {
paths = append(paths, ":(top,exclude)config.yaml")
cleanArgs = append(cleanArgs, "-e", "/config.yaml")
}
changed := false
// Check both diffs: staged and unstaged edits can cancel each other out.
// Skip restore when neither has changes, including an empty initial commit.
for _, args := range [][]string{{"diff", "--name-only", "HEAD"}, {"diff", "--cached", "--name-only", "HEAD"}} {
cmd := gitenv.Command(append(append(args, "--"), paths...)...)
cmd.Dir = dir
out, err := cmd.Output()
if err != nil {
return fmt.Errorf("failed to check changes: %w", err)
}
changed = changed || len(out) > 0
}
// Clean first, while the local .gitignore still defines ignored files.
// A single -f deliberately preserves nested repositories.
commands := [][]string{append(cleanArgs, "--", ".")}
if changed {
commands = append(commands, append([]string{"restore", "--source=HEAD", "--staged", "--worktree", "--no-recurse-submodules", "--"}, paths...))
}
for _, args := range commands {
cmd := gitenv.Command(args...)
cmd.Dir = dir
if out, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("failed to discard changes: %w: %s", err, out)
}
}
return nil
}