Documentation
¶
Overview ¶
Package gitenv isolates test git invocations from the host's git config. It deliberately imports nothing from this repo so that internal tests of low-level packages (e.g. gitrepo, which testutil itself imports) can use it without an import cycle; testutil re-exports it for everyone else.
Index ¶
Constants ¶
const EnvHermetic = "ENTIRE_TEST_GIT_HERMETIC"
EnvHermetic, when set to a non-empty value, makes emptyConfigPath append per-host HTTP proxy config that routes git HTTPS transport to real external hosts (github.com, gitlab.com) through an unroutable loopback proxy. Any test whose git commands accidentally dial those hosts then fails fast (connection refused at 127.0.0.1:1) instead of reaching the network or prompting for credentials. It is opt-in per test process — the integration TestMain sets it — so unit test packages that don't set it are unaffected. Because Isolated strips all inherited GIT_CONFIG_* env, this config must live in the file GIT_CONFIG_GLOBAL points at (this one), not in GIT_CONFIG_* env entries.
A dead proxy (not url.insteadOf) is used deliberately: insteadOf rewrites the effective URL that git reports on read, which breaks production code that resolves the origin URL to detect the forge (e.g. `entire trail`). The proxy blocks transport only, leaving the configured URL string intact, and is scoped per host so loopback (127.0.0.1) test servers are never proxied.
Regression class: tests accidentally hitting live github.com / the macOS keychain (#1463, 53bc37a88).
Variables ¶
This section is empty.
Functions ¶
func EmptyConfigOverrides ¶ added in v0.11.0
func EmptyConfigOverrides() []string
EmptyConfigOverrides returns the GIT_CONFIG_GLOBAL / GIT_CONFIG_SYSTEM settings Isolated applies, for callers that build their own environment on a different base and still want the caller's git config kept out.
func IsolateMain ¶
func IsolateMain()
IsolateMain is IsolateProcess for a TestMain, which has no *testing.T to restore through: the isolation is set process-wide for the whole run via os.Setenv and inherited by every spawned binary and git hook. Inherited GIT_CONFIG_* overrides are neutralized (GIT_CONFIG_COUNT=0 disables the indexed KEY_/VALUE_ pairs) rather than unset.
func IsolateProcess ¶
IsolateProcess applies the same git config isolation to the current process. Use this in tests that exercise production code paths which invoke git with os.Environ(). All inherited GIT_CONFIG_* variables are cleared before the isolation overrides are set, so values such as GIT_CONFIG_PARAMETERS or indexed KEY_/VALUE_ overrides cannot leak into child git invocations.
func IsolateRepository ¶ added in v0.11.3
IsolateRepository isolates config and removes inherited repository/object-store selectors for a test that drives Git against temporary repositories. Unlike IsolateProcess, it must not be used by tests intentionally inheriting a hook's temporary index or another repository selector. It changes process-global state, so the test cannot run in parallel.
func Isolated ¶
func Isolated() []string
Isolated returns os.Environ() with git isolation variables set. This prevents user/system git config (global gitignore, aliases, etc.) from affecting test behavior. Use this for any exec.Command that runs git or the CLI binary in integration tests.
See https://git-scm.com/docs/git#Documentation/git.txt-GITCONFIGGLOBAL
Every inherited GIT_CONFIG_* entry is filtered out — including GIT_CONFIG_PARAMETERS and the indexed KEY_/VALUE_ pairs that can inject `git -c` overrides — so our explicit isolation overrides take effect regardless of parent env.
func Run ¶
Run runs one git command in dir with an isolated git config, failing the test on error and returning combined output.
func UnsetGlobalConfig ¶ added in v0.11.0
UnsetGlobalConfig removes GIT_CONFIG_GLOBAL for the duration of t so that git and go-git resolve global config from the caller's $HOME (then XDG) again.
Use it in a helper that points HOME at a fixture and then expects ~/.gitconfig to be read. GIT_CONFIG_GLOBAL, when set, replaces every standard path with the one file it names, so any inherited value — including the isolation file IsolateProcess and IsolateMain install — makes the fixture unreachable and the helper a no-op.
The variable must end up ABSENT, not empty: git and go-git both read an empty GIT_CONFIG_GLOBAL as "no global config at all". t.Setenv registers the restore for the end of the test; os.Unsetenv then clears it for the test's own duration.
Types ¶
This section is empty.