Skip to content

Commit 0f9833a

Browse files
committed
adjust to changes in git-config (#450)
This also fixes a bug that may cause the actual System location to be ignored and replaced with the git installation location. Now it's additive.
1 parent 27fb1ce commit 0f9833a

File tree

2 files changed

+32
-94
lines changed

2 files changed

+32
-94
lines changed

Diff for: git-repository/src/config/cache/init.rs

+32-35
Original file line numberDiff line numberDiff line change
@@ -55,42 +55,39 @@ impl Cache {
5555
let home_env = &home_env;
5656
let xdg_config_home_env = &xdg_config_home_env;
5757
let git_prefix = &git_prefix;
58-
let mut install_path = use_installation.then(crate::env::git::install_config_path).flatten();
59-
let metas = [git_config::source::Kind::System, git_config::source::Kind::Global]
60-
.iter()
61-
.flat_map(|kind| kind.sources())
62-
.filter_map(|source| match install_path.take() {
63-
Some(install_path) => (
64-
&git_config::Source::System,
65-
git_path::from_bstr(install_path).into_owned(),
66-
)
67-
.into(),
68-
None => {
69-
match source {
70-
git_config::Source::System if !use_system => return None,
71-
git_config::Source::Git if !use_git => return None,
72-
git_config::Source::User if !use_user => return None,
73-
_ => {}
58+
let metas = [
59+
git_config::source::Kind::GitInstallation,
60+
git_config::source::Kind::System,
61+
git_config::source::Kind::Global,
62+
]
63+
.iter()
64+
.flat_map(|kind| kind.sources())
65+
.filter_map(|source| {
66+
match source {
67+
git_config::Source::GitInstallation if !use_installation => return None,
68+
git_config::Source::System if !use_system => return None,
69+
git_config::Source::Git if !use_git => return None,
70+
git_config::Source::User if !use_user => return None,
71+
_ => {}
72+
}
73+
source
74+
.storage_location(&mut |name| {
75+
match name {
76+
git_ if git_.starts_with("GIT_") => Some(git_prefix),
77+
"XDG_CONFIG_HOME" => Some(xdg_config_home_env),
78+
"HOME" => Some(home_env),
79+
_ => None,
7480
}
75-
source
76-
.storage_location(&mut |name| {
77-
match name {
78-
git_ if git_.starts_with("GIT_") => Some(git_prefix),
79-
"XDG_CONFIG_HOME" => Some(xdg_config_home_env),
80-
"HOME" => Some(home_env),
81-
_ => None,
82-
}
83-
.and_then(|perm| std::env::var_os(name).and_then(|val| perm.check_opt(val)))
84-
})
85-
.map(|p| (source, p.into_owned()))
86-
}
87-
})
88-
.map(|(source, path)| git_config::file::Metadata {
89-
path: Some(path),
90-
source: *source,
91-
level: 0,
92-
trust: git_sec::Trust::Full,
93-
});
81+
.and_then(|perm| std::env::var_os(name).and_then(|val| perm.check_opt(val)))
82+
})
83+
.map(|p| (source, p.into_owned()))
84+
})
85+
.map(|(source, path)| git_config::file::Metadata {
86+
path: Some(path),
87+
source: *source,
88+
level: 0,
89+
trust: git_sec::Trust::Full,
90+
});
9491

9592
let err_on_nonexisting_paths = false;
9693
let mut globals = git_config::File::from_paths_metadata_buf(

Diff for: git-repository/src/env.rs

-59
Original file line numberDiff line numberDiff line change
@@ -26,62 +26,3 @@ pub fn os_str_to_bstring(input: &OsStr) -> Result<BString, String> {
2626
.map(Into::into)
2727
.map_err(|_| input.to_string_lossy().into_owned())
2828
}
29-
30-
/// Environment information involving the `git` program itself.
31-
pub mod git {
32-
use std::process::{Command, Stdio};
33-
34-
use crate::bstr::{BStr, BString, ByteSlice};
35-
36-
/// Returns the file that contains git configuration coming with the installation of the `git` file in the current `PATH`, or `None`
37-
/// if no `git` executable was found or there were other errors during execution.
38-
pub fn install_config_path() -> Option<&'static BStr> {
39-
static PATH: once_cell::sync::Lazy<Option<BString>> = once_cell::sync::Lazy::new(|| {
40-
let mut cmd = Command::new(if cfg!(windows) { "git.exe" } else { "git" });
41-
cmd.args(["config", "-l", "--show-origin"])
42-
.stdin(Stdio::null())
43-
.stderr(Stdio::null());
44-
first_file_from_config_with_origin(cmd.output().ok()?.stdout.as_slice().into()).map(ToOwned::to_owned)
45-
});
46-
PATH.as_ref().map(|b| b.as_ref())
47-
}
48-
49-
fn first_file_from_config_with_origin(source: &BStr) -> Option<&BStr> {
50-
let file = source.strip_prefix(b"file:")?;
51-
let end_pos = file.find_byte(b'\t')?;
52-
file[..end_pos].as_bstr().into()
53-
}
54-
55-
#[cfg(test)]
56-
mod tests {
57-
use crate::env::git;
58-
59-
#[test]
60-
fn first_file_from_config_with_origin() {
61-
let macos = "file:/Applications/Xcode.app/Contents/Developer/usr/share/git-core/gitconfig credential.helper=osxkeychain\nfile:/Users/byron/.gitconfig push.default=simple\n";
62-
let win_msys =
63-
"file:C:/git-sdk-64/etc/gitconfig core.symlinks=false\r\nfile:C:/git-sdk-64/etc/gitconfig core.autocrlf=true";
64-
let win_cmd = "file:C:/Program Files/Git/etc/gitconfig diff.astextplain.textconv=astextplain\r\nfile:C:/Program Files/Git/etc/gitconfig filter.lfs.clean=git-lfs clean -- %f\r\n";
65-
let linux = "file:/home/parallels/.gitconfig core.excludesfile=~/.gitignore\n";
66-
let bogus = "something unexpected";
67-
let empty = "";
68-
69-
for (source, expected) in [
70-
(
71-
macos,
72-
Some("/Applications/Xcode.app/Contents/Developer/usr/share/git-core/gitconfig"),
73-
),
74-
(win_msys, Some("C:/git-sdk-64/etc/gitconfig")),
75-
(win_cmd, Some("C:/Program Files/Git/etc/gitconfig")),
76-
(linux, Some("/home/parallels/.gitconfig")),
77-
(bogus, None),
78-
(empty, None),
79-
] {
80-
assert_eq!(
81-
git::first_file_from_config_with_origin(source.into()),
82-
expected.map(Into::into)
83-
);
84-
}
85-
}
86-
}
87-
}

0 commit comments

Comments
 (0)