Skip to content

fix(git): keep a caller's GIT_DIR out of skillshare's git commands - #854

Merged
runkids merged 2 commits into
mainfrom
runkids/git-env-isolation
Nov 11, 2026
Merged

runkids merged 2 commits into
mainfrom
runkids/git-env-isolation

Conversation

@runkids

@runkids runkids commented Nov 11, 2026

Copy link
Copy Markdown
Owner

Type

  • Bug fix
  • Small improvement (docs, typo, minor refactor)
  • Feature proposal
  • Approved feature or large change

Summary

Git exports GIT_DIR, GIT_WORK_TREE, GIT_INDEX_FILE and related variables to hooks in a linked worktree, and they override the directory a git command runs in. Every git process skillshare started inherited them, so skillshare run from a post-checkout / post-merge hook acted on the hook's repository:

Path Effect on the caller's repository
install sparse subdirectory clone (#852) sparse checkout turned on; the next checkout or merge removes every file outside the skill's path
remote tree check (git init --bare + fetch) core.bare=true
install reconcile of tracked repos its origin URL and branch recorded in metadata
push add -A, commit and push land in it
  • New internal/gitenv: Command / CommandContext / Environ drop the variables git rev-parse --local-env-vars lists for choosing a repository, work tree, index, object store or config file.
  • GIT_CONFIG_COUNT / GIT_CONFIG_PARAMETERS (and GIT_CONFIG_KEY_n / VALUE_n) are kept: 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 goes through it. internal/plugin runCommand starts native CLIs from the same environment, since they run git too.
  • internal/hooks keeps its own stricter sanitizedGitEnv (it also drops config variables and forces LC_ALL=C).

Linked Issue

Closes #852

Verification

  • Before: the issue's reproduction, with an absolute GIT_DIR as git exports to worktree hooks, against an origin/main binary turned on sparse checkout in the host repo, and a forced checkout removed x/b.
  • After: the same reproduction with this branch leaves the host repo untouched and installs the skill.
  • Regression tests, each confirmed failing before the fix: TestSparseCloneSubdir_IgnoresCallerGitDir, TestGetCurrentFullHash_IgnoresCallerGitDir, TestFetchRemoteTreeHashes_IgnoresCallerGitDir, TestGitRemoteOrigin_IgnoresCallerGitDir, TestRunCommandDropsTheCallersRepository, and the integration test TestPush_IgnoresCallerGitDir; plus gitenv unit tests for dropped and kept variables.
  • make check passed in the devcontainer image; GOOS=windows go build ./... passed.
  • Five-pass local review (correctness, rules, spec, consumers, cleanup), twice; confirmed findings are fixed.
  • Not covered: user-defined commands skillshare launches (sync extension run scripts, mcp check --live server probes) still inherit the caller's environment on purpose, matching what a hook running them directly would see. Windows runtime was not exercised.

Merge Danger

Low. Environment handling only; no config, file format or flag changes. A user who deliberately set GIT_DIR / GIT_WORK_TREE to steer skillshare's git commands would no longer have that effect; skillshare always names the repository it works on.

Checklist

  • I've read CONTRIBUTING.md, including its AI-assisted contributions section
  • I've reviewed every line of the diff and can explain it
  • Tests included and passing (make check)
  • No unrelated changes in the diff
  • Scope is focused — one concern per PR

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
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-11T10:15:05.754938Z d1d7bbb PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

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.
@runkids
runkids merged commit 828ba93 into main Nov 11, 2026
9 checks passed
@runkids
runkids deleted the runkids/git-env-isolation branch November 11, 2026 13:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

install: sparse clone inherits GIT_DIR and turns on sparse-checkout in the caller's repository

1 participant