Skip to content

Commit 7fa5e35

Browse files
committed
Explain why we run git from a different directory
+ Copyedit other comments for readability.
1 parent 598c487 commit 7fa5e35

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

gix-path/src/env/git/mod.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,14 @@ fn git_cmd(executable: PathBuf) -> Command {
115115
const CREATE_NO_WINDOW: u32 = 0x08000000;
116116
cmd.creation_flags(CREATE_NO_WINDOW);
117117
}
118+
// We will try to run `git` from a location fairly high in the filesystem. This can be faster
119+
// than running it from our CWD, if we are deeply nested or on network storage. We try to pick
120+
// a place that exists, is unlikely to be a repo, and forbids unprivileged users from putting a
121+
// `.git` dir or other entries inside (so not `C:\` on Windows). But we will also be setting
122+
// `GIT_DIR` to a location `git` can't read config from, so this is mostly for performance.
118123
let cwd = if cfg!(windows) {
119124
env::var_os("SystemRoot") // Usually `C:\Windows`. Not to be confused with `C:\`.
120-
.or_else(|| env::var_os("windir")) // Same, in case our parent filtered out SystemRoot.
125+
.or_else(|| env::var_os("windir")) // Same. In case our parent filtered out SystemRoot.
121126
.map(PathBuf::from)
122127
.filter(|p| p.is_absolute())
123128
.unwrap_or_else(env::temp_dir)
@@ -126,19 +131,18 @@ fn git_cmd(executable: PathBuf) -> Command {
126131
};
127132
// Git 2.8.0 and higher support --show-origin. The -l, -z, and --name-only options were
128133
// supported even before that. In contrast, --show-scope was introduced later, in Git 2.26.0.
129-
// Low versions of git are still sometimes used, and this is sometimes reasonable because
134+
// Low versions of Git are still sometimes used, and this is sometimes reasonable because
130135
// downstream distributions often backport security patches without adding most new features.
131136
// So for now, we forgo the convenience of --show-scope for greater backward compatibility.
132137
//
133138
// Separately from that, we can't use --system here, because scopes treated higher than the
134139
// system scope are possible. This commonly happens on macOS with Apple Git, where the config
135-
// file under /Library is shown as an "unknown" scope but takes precedence over the system
136-
// scope. Although GIT_CONFIG_NOSYSTEM will suppress this as well, passing --system omits it.
137-
//
140+
// file under `/Library` is shown as an "unknown" scope but takes precedence over the system
141+
// scope. Although `GIT_CONFIG_NOSYSTEM` will suppress this as well, passing --system omits it.
138142
cmd.args(["config", "-lz", "--show-origin", "--name-only"])
139143
.current_dir(cwd)
140144
.env("GIT_DIR", NULL_DEVICE) // Avoid getting local-scope config.
141-
.env("GIT_WORK_TREE", NULL_DEVICE) // Just to avoid confusion when debugging.
145+
.env("GIT_WORK_TREE", NULL_DEVICE) // This is just to avoid confusion when debugging.
142146
.stdin(Stdio::null())
143147
.stderr(Stdio::null());
144148
cmd

0 commit comments

Comments
 (0)