Skip to content

Commit d47cebe

Browse files
committed
Merge branch 'pascalkuthe/main'
2 parents 02c4659 + d1e5e12 commit d47cebe

File tree

7 files changed

+63
-18
lines changed

7 files changed

+63
-18
lines changed

cargo-smart-release/src/crates_index.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fn default_path() -> PathBuf {
3636
.or_else(|| {
3737
std::env::var_os("CARGO_HOME")
3838
.map(PathBuf::from)
39-
.or_else(|| std::env::var_os("HOME").map(|dir| Path::new(&dir).join(".cargo")))
39+
.or_else(|| home::home_dir().map(|dir| Path::new(&dir).join(".cargo")))
4040
})
4141
.expect("one of these paths works")
4242
.join("registry/index")

gix-config/src/file/init/comfort.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::path::PathBuf;
2-
31
use crate::{
42
file::{init, Metadata},
53
path, source, File, Source,
@@ -30,7 +28,7 @@ impl File<'static> {
3028
.flat_map(|kind| kind.sources())
3129
.filter_map(|source| {
3230
let path = source
33-
.storage_location(&mut |name| std::env::var_os(name))
31+
.storage_location(&mut gix_path::env_var)
3432
.and_then(|p| p.is_file().then_some(p))
3533
.map(|p| p.into_owned());
3634

@@ -43,7 +41,7 @@ impl File<'static> {
4341
.into()
4442
});
4543

46-
let home = std::env::var("HOME").ok().map(PathBuf::from);
44+
let home = gix_path::home_dir();
4745
let options = init::Options {
4846
includes: init::includes::Options::follow_without_conditional(home.as_deref()),
4947
..Default::default()
@@ -59,7 +57,7 @@ impl File<'static> {
5957
///
6058
/// [`gix-config`'s documentation]: https://git-scm.com/docs/gix-config#Documentation/gix-config.txt-GITCONFIGCOUNT
6159
pub fn from_environment_overrides() -> Result<File<'static>, init::from_env::Error> {
62-
let home = std::env::var("HOME").ok().map(PathBuf::from);
60+
let home = gix_path::home_dir();
6361
let options = init::Options {
6462
includes: init::includes::Options::follow_without_conditional(home.as_deref()),
6563
..Default::default()
@@ -88,7 +86,7 @@ impl File<'static> {
8886
let mut path = dir.into();
8987
path.push(
9088
source
91-
.storage_location(&mut |n| std::env::var_os(n))
89+
.storage_location(&mut gix_path::env_var)
9290
.expect("location available for local"),
9391
);
9492
let local = Self::from_path_no_includes(&path, source)?;
@@ -101,7 +99,7 @@ impl File<'static> {
10199
let source = Source::Worktree;
102100
let path = git_dir.join(
103101
source
104-
.storage_location(&mut |n| std::env::var_os(n))
102+
.storage_location(&mut gix_path::env_var)
105103
.expect("location available for worktree"),
106104
);
107105
Self::from_path_no_includes(path, source)
@@ -110,7 +108,7 @@ impl File<'static> {
110108
}
111109
.transpose()?;
112110

113-
let home = std::env::var("HOME").ok().map(PathBuf::from);
111+
let home = gix_path::home_dir();
114112
let options = init::Options {
115113
includes: init::includes::Options::follow(
116114
path::interpolate::Context {

gix-path/src/lib.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@
5656
pub struct Spec(bstr::BString);
5757

5858
mod convert;
59+
use std::env::var_os;
60+
use std::ffi::OsString;
61+
use std::path::PathBuf;
62+
5963
pub use convert::*;
6064

6165
mod util;
@@ -66,3 +70,40 @@ mod spec;
6670
///
6771
pub mod realpath;
6872
pub use realpath::function::{realpath, realpath_opts};
73+
74+
/// Returns a platform independent home directory.
75+
///
76+
/// On unix this simply returns $HOME on windows this uses %HOMEDRIVE%\%HOMEPATH% or %USERPROFILE%
77+
pub fn home_dir() -> Option<PathBuf> {
78+
if let Some(home) = var_os("HOME") {
79+
return Some(home.into());
80+
}
81+
82+
// NOTE: technically we should also check HOMESHARE in case HOME is a UNC path
83+
// but git doesn't do this either so probably best to wait for an upstream fix.
84+
#[cfg(windows)]
85+
{
86+
if let Some(homedrive) = var_os("HOMEDRIVE") {
87+
if let Some(home_path) = var_os("HOMEPATH") {
88+
let home = PathBuf::from(homedrive).join(home_path);
89+
if home.metadata().map_or(false, |home| home.is_dir()) {
90+
return Some(home);
91+
}
92+
}
93+
}
94+
if let Some(userprofile) = var_os("USERPROFILE") {
95+
return Some(userprofile.into());
96+
}
97+
}
98+
None
99+
}
100+
101+
/// Returns the contents of an environment variable of `name` with some special handling
102+
/// for certain environment variables (like `HOME`) for platform compatibility.
103+
pub fn env_var(name: &str) -> Option<OsString> {
104+
if name == "HOME" {
105+
home_dir().map(PathBuf::into_os_string)
106+
} else {
107+
std::env::var_os(name)
108+
}
109+
}

gix-path/tests/path.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,15 @@ pub type Result<T = ()> = std::result::Result<T, Box<dyn std::error::Error>>;
22

33
mod convert;
44
mod realpath;
5+
mod home_dir {
6+
#[test]
7+
fn returns_existing_directory() {
8+
if let Some(home) = gix_path::home_dir() {
9+
assert!(
10+
home.is_dir(),
11+
"the home directory would typically exist, even though on unix we don't test for that."
12+
);
13+
}
14+
}
15+
}
516
mod util;

gix/src/config/cache/access.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,9 @@ impl Cache {
203203
std::env::var_os("XDG_CONFIG_HOME")
204204
.map(|path| (PathBuf::from(path), &self.xdg_config_home_env))
205205
.or_else(|| {
206-
std::env::var_os("HOME").map(|path| {
206+
gix_path::home_dir().map(|mut p| {
207207
(
208208
{
209-
let mut p = PathBuf::from(path);
210209
p.push(".config");
211210
p
212211
},
@@ -226,8 +225,6 @@ impl Cache {
226225
/// We never fail for here even if the permission is set to deny as we `gix-config` will fail later
227226
/// if it actually wants to use the home directory - we don't want to fail prematurely.
228227
pub(crate) fn home_dir(&self) -> Option<PathBuf> {
229-
std::env::var_os("HOME")
230-
.map(PathBuf::from)
231-
.and_then(|path| self.home_env.check_opt(path))
228+
gix_path::home_dir().and_then(|path| self.home_env.check_opt(path))
232229
}
233230
}

gix/src/config/cache/init.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl Cache {
9595
"HOME" => Some(home_env),
9696
_ => None,
9797
}
98-
.and_then(|perm| perm.check_opt(name).and_then(std::env::var_os))
98+
.and_then(|perm| perm.check_opt(name).and_then(gix_path::env_var))
9999
})
100100
.map(|p| (source, p.into_owned()))
101101
})

gix/src/open/repository.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,7 @@ impl ThreadSafeRepository {
180180
};
181181
let head = refs.find("HEAD").ok();
182182
let git_install_dir = crate::path::install_dir().ok();
183-
let home = std::env::var_os("HOME")
184-
.map(PathBuf::from)
185-
.and_then(|home| env.home.check_opt(home));
183+
let home = gix_path::home_dir().and_then(|home| env.home.check_opt(home));
186184

187185
let mut filter_config_section = filter_config_section.unwrap_or(config::section::is_trusted);
188186
let config = config::Cache::from_stage_one(

0 commit comments

Comments
 (0)