Skip to content

Commit a9ac1c5

Browse files
committed
feat: add env::git_shell() to obtain the shell Git would be using.
This is particularly useful to execute Git hooks.
1 parent dbdc737 commit a9ac1c5

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

gix-path/src/env/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,23 @@ pub fn installation_config_prefix() -> Option<&'static Path> {
2828
installation_config().map(git::config_to_base_path)
2929
}
3030

31+
/// Return the shell that Git would prefer as login shell, the shell to execute Git commands from.
32+
///
33+
/// On Windows, this is the `bash.exe` bundled with it, and on Unix it's the shell specified by `SHELL`,
34+
/// or `None` if it is truly unspecified.
35+
pub fn login_shell() -> Option<&'static Path> {
36+
static PATH: Lazy<Option<PathBuf>> = Lazy::new(|| {
37+
if cfg!(windows) {
38+
installation_config_prefix()
39+
.and_then(|p| p.parent())
40+
.map(|p| p.join("usr").join("bin").join("bash.exe"))
41+
} else {
42+
std::env::var_os("SHELL").map(PathBuf::from)
43+
}
44+
});
45+
PATH.as_deref()
46+
}
47+
3148
/// Return the name of the Git executable to invoke it.
3249
/// If it's in the `PATH`, it will always be a short name.
3350
///

gix-path/tests/path/env.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ fn exe_invocation() {
77
);
88
}
99

10+
#[test]
11+
fn git_shell() {
12+
assert!(gix_path::env::login_shell()
13+
.expect("There should always be the notion of a shell used by git")
14+
.exists());
15+
}
16+
1017
#[test]
1118
fn installation_config() {
1219
assert_ne!(

0 commit comments

Comments
 (0)