Skip to content

Commit

Permalink
fix: rename env::login_shell() to shell() and explain what it is.
Browse files Browse the repository at this point in the history
This assures we don't suggest the usage of actual login shells to ever
be used to execute hooks.

Note that this is not marked as breaking change as no release was made
with the old name yet.
  • Loading branch information
Byron committed Jan 11, 2025
1 parent f18a312 commit 51bbb86
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
22 changes: 13 additions & 9 deletions gix-path/src/env/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::ffi::OsString;
use std::ffi::{OsStr, OsString};
use std::path::{Path, PathBuf};

use bstr::{BString, ByteSlice};
Expand Down Expand Up @@ -28,21 +28,25 @@ 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.
/// Return the shell that Git would use, the shell to execute 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(|| {
/// On Windows, this is the full path to `sh.exe` bundled with it, and on
/// Unix it's `/bin/sh` as posix compatible shell.
/// If the bundled shell on Windows cannot be found, `sh` is returned as the name of a shell
/// as it could possibly be found in `PATH`.
/// Note that the returned path might not be a path on disk.
pub fn shell() -> &'static OsStr {
static PATH: Lazy<Option<OsString>> = Lazy::new(|| {
if cfg!(windows) {
installation_config_prefix()
.and_then(|p| p.parent())
.map(|p| p.join("usr").join("bin").join("bash.exe"))
.map(|p| p.join("usr").join("bin").join("sh.exe"))
.map(Into::into)
} else {
std::env::var_os("SHELL").map(PathBuf::from)
Some("/bin/sh".into())
}
});
PATH.as_deref()
PATH.as_deref().unwrap_or(OsStr::new("sh"))
}

/// Return the name of the Git executable to invoke it.
Expand Down
12 changes: 5 additions & 7 deletions gix-path/tests/path/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ fn exe_invocation() {
}

#[test]
fn login_shell() {
// On CI, the $SHELL variable isn't necessarily set. Maybe other ways to get the login shell should be used then.
if !gix_testtools::is_ci::cached() {
assert!(gix_path::env::login_shell()
.expect("There should always be the notion of a shell used by git")
.exists());
}
fn shell() {
assert!(
std::path::Path::new(gix_path::env::shell()).exists(),
"On CI and on Unix we'd expect a full path to the shell that exists on disk"
);
}

#[test]
Expand Down

0 comments on commit 51bbb86

Please sign in to comment.