Skip to content

Commit

Permalink
Use consistent git command name in gix-testtools
Browse files Browse the repository at this point in the history
In the test tools, this always runs the `git` program as `git.exe`
on Windows, while continuing always to run it as `git` on other
systems.

Prior to this change, on Windows `gix-testtools` used `git` in some
operations and `git.exe` in others:

- `parse_git_version` used `git.exe`.
- Other functions used `git`.
- For the git daemon, `git-daemon.exe` was used.

For the way `gix-testtools` uses the `git` program, it would be
fine to call it `git` on all platforms. For example, it does not
form full paths to the executable that have to be found to exist
in operations other than running it. (For running it, the `.exe`
suffix is allowed to be omitted.) So it would probably be fine to
use the even simpler logic of having it be `git` everywhere. But
since `git.exe` was sometimes used, `git-daemon.exe` was used, and
using `git.exe` is more similar to the behavior in `git-path`, it
is changed to use `git.exe` when the platform is Windows.

Because `gix-testtools` does not depend on `gix-path`, it doesn't
use `gix_path::env::exe_invocation` to decide how to call `git`.
That keeps it from finding `git` in some Windows environments
(those where `git` is in a standard/predictable location but not in
`PATH`). This change has no effect on that limitation.
  • Loading branch information
EliahKagan committed Nov 30, 2024
1 parent 01737ad commit e30c070
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions tests/tools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ impl Drop for GitDaemon {
}

static SCRIPT_IDENTITY: Lazy<Mutex<BTreeMap<PathBuf, u32>>> = Lazy::new(|| Mutex::new(BTreeMap::new()));

static EXCLUDE_LUT: Lazy<Mutex<Option<gix_worktree::Stack>>> = Lazy::new(|| {
let cache = (|| {
let (repo_path, _) = gix_discover::upwards(Path::new(".")).ok()?;
Expand Down Expand Up @@ -86,6 +87,12 @@ static EXCLUDE_LUT: Lazy<Mutex<Option<gix_worktree::Stack>>> = Lazy::new(|| {
})();
Mutex::new(cache)
});

#[cfg(windows)]
const GIT_PROGRAM: &str = "git.exe";
#[cfg(not(windows))]
const GIT_PROGRAM: &str = "git";

/// The major, minor and patch level of the git version on the system.
pub static GIT_VERSION: Lazy<(u8, u8, u8)> = Lazy::new(|| parse_git_version().expect("git version to be parsable"));

Expand Down Expand Up @@ -117,9 +124,7 @@ pub fn should_skip_as_git_version_is_smaller_than(major: u8, minor: u8, patch: u
}

fn parse_git_version() -> Result<(u8, u8, u8)> {
let git_program = cfg!(windows).then(|| "git.exe").unwrap_or("git");
let output = std::process::Command::new(git_program).arg("--version").output()?;

let output = std::process::Command::new(GIT_PROGRAM).arg("--version").output()?;
git_version_from_bytes(&output.stdout)
}

Expand Down Expand Up @@ -173,7 +178,7 @@ impl Drop for AutoRevertToPreviousCWD {

/// Run `git` in `working_dir` with all provided `args`.
pub fn run_git(working_dir: &Path, args: &[&str]) -> std::io::Result<std::process::ExitStatus> {
std::process::Command::new("git")
std::process::Command::new(GIT_PROGRAM)
.current_dir(working_dir)
.args(args)
.status()
Expand All @@ -182,7 +187,7 @@ pub fn run_git(working_dir: &Path, args: &[&str]) -> std::io::Result<std::proces
/// Spawn a git daemon process to host all repository at or below `working_dir`.
pub fn spawn_git_daemon(working_dir: impl AsRef<Path>) -> std::io::Result<GitDaemon> {
static EXEC_PATH: Lazy<PathBuf> = Lazy::new(|| {
let path = std::process::Command::new("git")
let path = std::process::Command::new(GIT_PROGRAM)
.arg("--exec-path")
.stderr(std::process::Stdio::null())
.output()
Expand Down Expand Up @@ -738,7 +743,7 @@ fn populate_meta_dir(destination_dir: &Path, script_identity: u32) -> std::io::R
)?;
std::fs::write(
meta_dir.join(META_GIT_VERSION),
std::process::Command::new("git").arg("--version").output()?.stdout,
std::process::Command::new(GIT_PROGRAM).arg("--version").output()?.stdout,
)?;
Ok(meta_dir)
}
Expand Down Expand Up @@ -951,7 +956,7 @@ mod tests {
let temp = tempfile::TempDir::new().expect("can create temp dir");
populate_ad_hoc_config_files(temp.path());

let mut cmd = std::process::Command::new("git");
let mut cmd = std::process::Command::new(GIT_PROGRAM);
cmd.env("GIT_CONFIG_SYSTEM", SCOPE_ENV_VALUE);
cmd.env("GIT_CONFIG_GLOBAL", SCOPE_ENV_VALUE);
configure_command(&mut cmd, ["config", "-l", "--show-origin"], temp.path());
Expand Down

0 comments on commit e30c070

Please sign in to comment.