From 6e28482706a2a7b9263b84c431777f6baa710236 Mon Sep 17 00:00:00 2001 From: SpringMaple Date: Sun, 14 Jul 2024 19:32:06 +0800 Subject: [PATCH] Fix git-bash finding in Windows not working properly --- Cargo.lock | 19 +++++++++++++++++++ Cargo.toml | 1 + crates/hyfetch/Cargo.toml | 1 + crates/hyfetch/src/bin/hyfetch.rs | 2 +- crates/hyfetch/src/neofetch_util.rs | 20 ++++++++++++-------- 5 files changed, 34 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3b7b009c..09a97343 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -172,6 +172,7 @@ dependencies = [ "normpath", "palette", "regex", + "same-file", "serde", "serde_json", "serde_path_to_error", @@ -448,6 +449,15 @@ version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + [[package]] name = "serde" version = "1.0.203" @@ -770,6 +780,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d4cc384e1e73b93bafa6fb4f1df8c41695c8a91cf9c4c64358067d15a7b6c6b" +dependencies = [ + "windows-sys 0.52.0", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index a50ceae0..e69bd631 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,6 +23,7 @@ indexmap = { version = "2.2.6", default-features = false } normpath = { version = "1.2.0", default-features = false } palette = { version = "0.7.6", default-features = false } regex = { version = "1.10.5", default-features = false } +same-file = { version = "1.0.6", default-features = false } serde = { version = "1.0.203", default-features = false } serde_json = { version = "1.0.118", default-features = false } serde_path_to_error = { version = "0.1.16", default-features = false } diff --git a/crates/hyfetch/Cargo.toml b/crates/hyfetch/Cargo.toml index 611aa98f..5829463b 100644 --- a/crates/hyfetch/Cargo.toml +++ b/crates/hyfetch/Cargo.toml @@ -18,6 +18,7 @@ deranged = { workspace = true, features = ["serde", "std"] } directories = { workspace = true, features = [] } indexmap = { workspace = true, features = ["serde", "std"] } palette = { workspace = true, features = ["std"] } +same-file = { workspace = true, features = [] } serde = { workspace = true, features = ["derive", "std"] } serde_json = { workspace = true, features = ["std"] } serde_path_to_error = { workspace = true, features = [] } diff --git a/crates/hyfetch/src/bin/hyfetch.rs b/crates/hyfetch/src/bin/hyfetch.rs index 02793ac4..391b85d4 100644 --- a/crates/hyfetch/src/bin/hyfetch.rs +++ b/crates/hyfetch/src/bin/hyfetch.rs @@ -41,7 +41,7 @@ _/\_\_ _/_/\_ fn main() -> Result<()> { #[cfg(windows)] if let Err(err) = enable_ansi_support::enable_ansi_support() { - debug!(err, "could not enable ANSI escape code support"); + debug!(%err, "could not enable ANSI escape code support"); } let options = options().run(); diff --git a/crates/hyfetch/src/neofetch_util.rs b/crates/hyfetch/src/neofetch_util.rs index e5340299..6bae21fe 100644 --- a/crates/hyfetch/src/neofetch_util.rs +++ b/crates/hyfetch/src/neofetch_util.rs @@ -1,6 +1,6 @@ use std::borrow::Cow; use std::ffi::OsStr; -use std::io::Write; +use std::io::{ErrorKind, Write}; use std::path::{Path, PathBuf}; use std::process::Command; use std::sync::OnceLock; @@ -11,6 +11,7 @@ use anyhow::{anyhow, Context, Result}; use indexmap::IndexMap; #[cfg(windows)] use normpath::PathExt as _; +use same_file::is_same_file; use serde::{Deserialize, Serialize}; use tempfile::NamedTempFile; use tracing::debug; @@ -317,7 +318,7 @@ pub fn neofetch_path() -> Result> { let neowofetch_path = if neowofetch_path.is_some() { neowofetch_path } else { - let current_exe_path = env::current_exe() + let current_exe_path: PathBuf = env::current_exe() .and_then(|p| { #[cfg(not(windows))] { @@ -358,10 +359,13 @@ pub fn ensure_git_bash() -> Result { // See https://github.com/hykilpikonna/hyfetch/issues/233 let windir = env::var_os("windir") .context("`windir` environment variable is not set or invalid")?; - if bash_path == Path::new(&windir).join(r"System32\bash.exe") { - None - } else { - Some(bash_path) + match is_same_file(&bash_path, Path::new(&windir).join(r"System32\bash.exe")) { + Ok(true) => None, + Ok(false) => Some(bash_path), + Err(err) if err.kind() == ErrorKind::NotFound => Some(bash_path), + Err(err) => { + return Err(err).context("failed to check if paths refer to the same file"); + }, } }, _ => bash_path, @@ -417,7 +421,7 @@ pub fn ensure_git_bash() -> Result { let git_bash_path = if git_bash_path.is_some() { git_bash_path } else { - let current_exe_path = env::current_exe() + let current_exe_path: PathBuf = env::current_exe() .and_then(|p| p.normalize().map(|p| p.into())) .context("failed to get path of current running executable")?; let bash_path = current_exe_path @@ -738,7 +742,7 @@ fn fastfetch_path() -> Result> { }; // Fall back to `fastfetch` in directory of current executable - let current_exe_path = env::current_exe() + let current_exe_path: PathBuf = env::current_exe() .and_then(|p| { #[cfg(not(windows))] {