Skip to content

Commit f727be7

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 e17cd99 commit f727be7

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

gix-path/src/env/mod.rs

+17
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

+10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ fn exe_invocation() {
77
);
88
}
99

10+
#[test]
11+
fn login_shell() {
12+
// On CI, the $SHELL variable isn't necessarily set. Maybe other ways to get the login shell should be used then.
13+
if !gix_testtools::is_ci::cached() {
14+
assert!(gix_path::env::login_shell()
15+
.expect("There should always be the notion of a shell used by git")
16+
.exists());
17+
}
18+
}
19+
1020
#[test]
1121
fn installation_config() {
1222
assert_ne!(

0 commit comments

Comments
 (0)