Skip to content

Commit c7093ac

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 c7093ac

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

gix-path/src/env/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ 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 git_login_shell() -> Option<PathBuf> {
36+
if cfg!(windows) {
37+
installation_config_prefix()
38+
.and_then(|p| p.parent())
39+
.map(|p| p.join("usr").join("bin").join("bash.exe"))
40+
} else {
41+
std::env::var_os("SHELL").map(PathBuf::from)
42+
}
43+
}
44+
3145
/// Return the name of the Git executable to invoke it.
3246
/// If it's in the `PATH`, it will always be a short name.
3347
///

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::git_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)