1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//! Helpers for invoking `git` from fallow without inheriting ambient repo
//! state from the parent process.
//!
//! When fallow is invoked from a git hook (`pre-commit`, `pre-push`,
//! `commit-msg`, ...) or a tool that wraps git (lint-staged, husky, lefthook,
//! pre-commit framework, IDE git integrations, some CI runners), git exports a
//! handful of environment variables describing the *enclosing* operation:
//! `GIT_INDEX_FILE`, `GIT_DIR`, `GIT_WORK_TREE`, `GIT_OBJECT_DIRECTORY`,
//! `GIT_COMMON_DIR`, `GIT_PREFIX`. Several of these are written as paths
//! relative to the parent's working directory (e.g. `GIT_INDEX_FILE=.git/index`
//! during `git commit`). When fallow then spawns its own `git` subprocess from
//! a different working directory (notably `git worktree add` against a
//! temporary path), the inherited relative paths no longer resolve and the
//! call fails.
//!
//! Fallow always operates against the repository at `--root` (or the cwd) and
//! never wants to share index / object / work-tree state with an enclosing
//! `git` operation, so the safe default is to strip these vars before every
//! `git` invocation.
//!
//! Vars that are *not* stripped: `GIT_AUTHOR_*`, `GIT_COMMITTER_*`, `GIT_EDITOR`,
//! `GIT_EXEC_PATH`. Those are either harmless to fallow's read-only git
//! invocations or required for fallow's tests that depend on the parent shell's
//! git config.
use Command;
/// Environment variables that describe an enclosing git operation's
/// repository state, in the order they appear in `git`'s own environment
/// documentation. Hook subprocesses inherit some or all of these, often as
/// paths relative to the parent's cwd, and they break fallow's git invocations
/// when fallow runs from a different cwd.
pub const AMBIENT_GIT_ENV_VARS: & = &;
/// Strip ambient git repository-state environment variables from a `Command`.
///
/// Apply to every `git` subprocess fallow spawns from production code. The
/// strip is unconditional and idempotent: `Command::env_remove` is a no-op
/// when the variable is not present in the inherited environment.
///
/// Returns the `Command` for fluent chaining alongside `.args()`,
/// `.current_dir()`, and so on.