Skip to content

Commit

Permalink
feat: add env::git_shell() to obtain the shell Git would be using.
Browse files Browse the repository at this point in the history
This is particularly useful to execute Git hooks.
  • Loading branch information
Byron committed Jan 9, 2025
1 parent 7fd59cd commit a0c1e97
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
17 changes: 17 additions & 0 deletions gix-path/src/env/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,23 @@ pub fn installation_config_prefix() -> Option<&'static Path> {
installation_config().map(git::config_to_base_path)
}

/// Return the shell that Git would prefer as login shell, the shell to execute Git commands from.
///
/// On Windows, this is the `bash.exe` bundled with it, and on Unix it's the shell specified by `SHELL`,
/// or `None` if it is truly unspecified.
pub fn login_shell() -> Option<&'static Path> {
static PATH: Lazy<Option<PathBuf>> = Lazy::new(|| {
if cfg!(windows) {
dbg!(installation_config_prefix())
.and_then(|p| p.parent())
.map(|p| p.join("usr").join("bin").join("bash.exe"))
} else {
std::env::var_os("SHELL").map(PathBuf::from)
}
});
PATH.as_deref()
}

/// Return the name of the Git executable to invoke it.
/// If it's in the `PATH`, it will always be a short name.
///
Expand Down
7 changes: 7 additions & 0 deletions gix-path/tests/path/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ fn exe_invocation() {
);
}

#[test]
fn login_shell() {
assert!(gix_path::env::login_shell()
.expect("There should always be the notion of a shell used by git")
.exists());
}

#[test]
fn installation_config() {
assert_ne!(
Expand Down

0 comments on commit a0c1e97

Please sign in to comment.