Skip to content

Clear inherited GIT_DIR, GIT_INDEX_FILE and GIT_DIFF_OPTS before running git - #184

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/139-clear-inherited-git-env
Oct 7, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/139-clear-inherited-git-env

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #139

What changed

RunCommandGitProcessRunner.EnvironmentOverlay now sets these variables to null, which makes ktsu.RunCommand (1.5.0, the version pinned in Directory.Packages.props) remove them from the child process's environment:

GIT_DIR, GIT_WORK_TREE, GIT_INDEX_FILE, GIT_OBJECT_DIRECTORY, GIT_ALTERNATE_OBJECT_DIRECTORIES, GIT_COMMON_DIR, GIT_NAMESPACE, GIT_PREFIX, GIT_DIFF_OPTS

  • The repository-locating variables take priority over -C, and git exports them to hooks. A .NET tool running from a hook that opened a different repository read that hook's repository, and the mutating verbs would have written to it.
  • GIT_DIFF_OPTS takes priority over the -U that Patch() always passes. It could produce zero-context hunks, which Apply rejects.
  • GIT_EXTERNAL_DIFF is left alone, because --no-ext-diff already neutralises it.
  • The remarks on the overlay now explain why each group is removed.

Tests

  • New GitInheritedEnvironmentTests (integration tests, [DoNotParallelize] because they set variables on the shared test process and restore them afterwards):
    • InheritedGitDirAndIndexFileDoNotRedirectTheVerbsAsync: sets GIT_DIR and GIT_INDEX_FILE to point at another repository. Status() and Patch() still report only B.txt in the repository that was opened.
    • InheritedGitDiffOptsDoesNotStripPatchContextAsync: sets GIT_DIFF_OPTS=-u0. Patch().WithContext(3) still returns @@ -1,5 +1,5 @@, and Apply().ToIndex() succeeds.
  • New data-driven unit test EnvironmentOverlayRemovesVariablesThatOverrideTheArguments checks that each key is in the overlay with a null value.
  • To check the tests catch the bug, I stashed the overlay change and ran them against the old code. Both integration tests failed: Status returned A.txt and B.txt ("Sequence contains more than one element"), and the hunk started at line 3 rather than 1. Both passed again once the change was restored.
  • Full suite with KTSU_GIT_INTEGRATION_TESTS_REQUIRED=1: 757/757 passing locally, none skipped.

🤖 Generated with Claude Code

https://claude.ai/code/session_01B6YhFsbKVvV6YZVxUJzexE


Generated by Claude Code

…ing git [patch]

The environment overlay set only GIT_TERMINAL_PROMPT and LC_ALL, so a
process running inside a git hook passed the hook's GIT_DIR and
GIT_INDEX_FILE to every git child. Those beat -C, so every verb read (and
the mutating verbs wrote) the hook's repository. GIT_DIFF_OPTS likewise
beat the -U that Patch() always passes, producing zero-context hunks that
Apply rejects. The overlay now removes the repository-locating variables
and GIT_DIFF_OPTS.

Fixes #139

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01B6YhFsbKVvV6YZVxUJzexE
@sonarqubecloud

sonarqubecloud Bot commented Oct 7, 2026

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants