Skip to content

Commit d1e5e12

Browse files
committed
refactor
- move `env_var` to path as it's the smaller crate that is easier to pull in from other plumbing crates. - use fully qualified paths to make clearer what is what. - add at least 'some' test for `gix_path::home_dir()`
1 parent d1bd513 commit d1e5e12

File tree

7 files changed

+47
-45
lines changed

7 files changed

+47
-45
lines changed

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
use std::path::PathBuf;
2-
3-
use gix_path::home;
4-
51
use crate::{
62
file::{init, Metadata},
73
path, source, File, Source,
@@ -32,7 +28,7 @@ impl File<'static> {
3228
.flat_map(|kind| kind.sources())
3329
.filter_map(|source| {
3430
let path = source
35-
.storage_location(&mut crate::env_var)
31+
.storage_location(&mut gix_path::env_var)
3632
.and_then(|p| p.is_file().then_some(p))
3733
.map(|p| p.into_owned());
3834

@@ -45,7 +41,7 @@ impl File<'static> {
4541
.into()
4642
});
4743

48-
let home = home();
44+
let home = gix_path::home_dir();
4945
let options = init::Options {
5046
includes: init::includes::Options::follow_without_conditional(home.as_deref()),
5147
..Default::default()
@@ -61,7 +57,7 @@ impl File<'static> {
6157
///
6258
/// [`gix-config`'s documentation]: https://git-scm.com/docs/gix-config#Documentation/gix-config.txt-GITCONFIGCOUNT
6359
pub fn from_environment_overrides() -> Result<File<'static>, init::from_env::Error> {
64-
let home = home();
60+
let home = gix_path::home_dir();
6561
let options = init::Options {
6662
includes: init::includes::Options::follow_without_conditional(home.as_deref()),
6763
..Default::default()
@@ -90,7 +86,7 @@ impl File<'static> {
9086
let mut path = dir.into();
9187
path.push(
9288
source
93-
.storage_location(&mut crate::env_var)
89+
.storage_location(&mut gix_path::env_var)
9490
.expect("location available for local"),
9591
);
9692
let local = Self::from_path_no_includes(&path, source)?;
@@ -103,7 +99,7 @@ impl File<'static> {
10399
let source = Source::Worktree;
104100
let path = git_dir.join(
105101
source
106-
.storage_location(&mut crate::env_var)
102+
.storage_location(&mut gix_path::env_var)
107103
.expect("location available for worktree"),
108104
);
109105
Self::from_path_no_includes(path, source)
@@ -112,7 +108,7 @@ impl File<'static> {
112108
}
113109
.transpose()?;
114110

115-
let home = home();
111+
let home = gix_path::home_dir();
116112
let options = init::Options {
117113
includes: init::includes::Options::follow(
118114
path::interpolate::Context {

gix-config/src/lib.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,9 @@ pub mod lookup;
4444
pub mod parse;
4545
///
4646
pub mod value;
47-
use std::ffi::OsString;
48-
4947
pub use gix_config_value::{color, integer, path, Boolean, Color, Integer, Path};
5048

5149
mod types;
5250
pub use types::{File, Source};
5351
///
5452
pub mod source;
55-
56-
/// Returns the contents of an eviorment variable with some specical handeling
57-
/// for certain enviorment variables (like $HOME) for platform compatability
58-
pub fn env_var(var: &str) -> Option<OsString> {
59-
if var == "HOME" {
60-
gix_path::home().map(|home| home.into_os_string())
61-
} else {
62-
std::env::var_os(var)
63-
}
64-
}

gix-path/src/lib.rs

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ pub struct Spec(bstr::BString);
5757

5858
mod convert;
5959
use std::env::var_os;
60+
use std::ffi::OsString;
6061
use std::path::PathBuf;
6162

6263
pub use convert::*;
@@ -70,31 +71,39 @@ mod spec;
7071
pub mod realpath;
7172
pub use realpath::function::{realpath, realpath_opts};
7273

73-
/// Returns a platform independent home directory
74+
/// Returns a platform independent home directory.
75+
///
7476
/// On unix this simply returns $HOME on windows this uses %HOMEDRIVE%\%HOMEPATH% or %USERPROFILE%
75-
pub fn home() -> Option<PathBuf> {
77+
pub fn home_dir() -> Option<PathBuf> {
7678
if let Some(home) = var_os("HOME") {
7779
return Some(home.into());
7880
}
7981

80-
// TODO cache env access on windows? git does this by setting HOME but
81-
// setting enviorment variables is a *really* bad idea in library
82-
83-
// FIXME: technically we should also check HOMESHARE in case HOME is a UNC path
84-
// but git doesn't do this either so probably best to wait for an upstream fix
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.
8584
#[cfg(windows)]
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);
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+
}
9192
}
9293
}
94+
if let Some(userprofile) = var_os("USERPROFILE") {
95+
return Some(userprofile.into());
96+
}
9397
}
94-
#[cfg(windows)]
95-
if let Some(userprofile) = var_os("USERPROFILE") {
96-
return Some(userprofile.into());
97-
}
98-
9998
None
10099
}
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 & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
use std::{borrow::Cow, path::PathBuf, time::Duration};
33

44
use gix_lock::acquire::Fail;
5-
use gix_path::home;
65

76
use crate::{
87
bstr::BStr,
@@ -204,7 +203,7 @@ impl Cache {
204203
std::env::var_os("XDG_CONFIG_HOME")
205204
.map(|path| (PathBuf::from(path), &self.xdg_config_home_env))
206205
.or_else(|| {
207-
home().map(|mut p| {
206+
gix_path::home_dir().map(|mut p| {
208207
(
209208
{
210209
p.push(".config");
@@ -226,6 +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-
home().and_then(|path| self.home_env.check_opt(path))
228+
gix_path::home_dir().and_then(|path| self.home_env.check_opt(path))
230229
}
231230
}

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(gix_config::env_var))
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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
use std::{borrow::Cow, path::PathBuf};
33

44
use gix_features::threading::OwnShared;
5-
use gix_path::home;
65

76
use super::{Error, Options};
87
use crate::{
@@ -181,7 +180,7 @@ impl ThreadSafeRepository {
181180
};
182181
let head = refs.find("HEAD").ok();
183182
let git_install_dir = crate::path::install_dir().ok();
184-
let home = home().and_then(|home| env.home.check_opt(home));
183+
let home = gix_path::home_dir().and_then(|home| env.home.check_opt(home));
185184

186185
let mut filter_config_section = filter_config_section.unwrap_or(config::section::is_trusted);
187186
let config = config::Cache::from_stage_one(

0 commit comments

Comments
 (0)