mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-18 18:04:17 +00:00
* 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.
62 lines
2.0 KiB
Go
62 lines
2.0 KiB
Go
// Package gitenv runs git without the caller's repository selection.
|
|
//
|
|
// Git exports GIT_DIR and related variables to hooks, so skillshare started
|
|
// from a hook would otherwise run its own git commands against the hook's
|
|
// repository instead of the directory it names (#852).
|
|
package gitenv
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
)
|
|
|
|
// repoVars are the variables `git rev-parse --local-env-vars` lists that pick
|
|
// a repository, work tree, index, object store, or config file.
|
|
// GIT_CONFIG_PARAMETERS and GIT_CONFIG_COUNT stay: they carry user settings
|
|
// such as credentials, and install's token auth appends to them.
|
|
var repoVars = map[string]bool{
|
|
"GIT_ALTERNATE_OBJECT_DIRECTORIES": true,
|
|
"GIT_COMMON_DIR": true,
|
|
"GIT_CONFIG": true,
|
|
"GIT_DIR": true,
|
|
"GIT_GRAFT_FILE": true,
|
|
"GIT_IMPLICIT_WORK_TREE": true,
|
|
"GIT_INDEX_FILE": true,
|
|
"GIT_NO_REPLACE_OBJECTS": true,
|
|
"GIT_OBJECT_DIRECTORY": true,
|
|
"GIT_PREFIX": true,
|
|
"GIT_REPLACE_REF_BASE": true,
|
|
"GIT_SHALLOW_FILE": true,
|
|
"GIT_WORK_TREE": true,
|
|
}
|
|
|
|
// Environ returns os.Environ() without the repository selection variables.
|
|
func Environ() []string {
|
|
env := os.Environ()
|
|
out := make([]string, 0, len(env))
|
|
for _, item := range env {
|
|
key, _, _ := strings.Cut(item, "=")
|
|
if !repoVars[strings.ToUpper(key)] { // Windows names are case-insensitive.
|
|
out = append(out, item)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// Command is exec.Command("git", args...) with Env set to Environ().
|
|
// Add variables with cmd.Env = append(cmd.Env, ...).
|
|
func Command(args ...string) *exec.Cmd {
|
|
cmd := exec.Command("git", args...)
|
|
cmd.Env = Environ()
|
|
return cmd
|
|
}
|
|
|
|
// CommandContext is the context-aware form of Command.
|
|
func CommandContext(ctx context.Context, args ...string) *exec.Cmd {
|
|
cmd := exec.CommandContext(ctx, "git", args...)
|
|
cmd.Env = Environ()
|
|
return cmd
|
|
}
|