Skip to content

Commit d51e7c9

Browse files
committed
feat: Add permissions::Config::git_binary field (#450)
When true, default false, inject the git installation configuration file if present at the cost of one `git config` invocation. Note that we rely on the underlying `git-config` crate to not load duplicate files. We also currently lie about the scope which is actually unclear - have seen 'unknown' or normal scopes like `system`.
1 parent ea84e62 commit d51e7c9

File tree

2 files changed

+41
-25
lines changed

2 files changed

+41
-25
lines changed

git-repository/src/config/cache/init.rs

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ impl Cache {
2727
ssh_prefix: _,
2828
}: repository::permissions::Environment,
2929
repository::permissions::Config {
30+
git_binary: use_installation,
3031
system: use_system,
3132
git: use_git,
3233
user: use_user,
@@ -54,35 +55,43 @@ impl Cache {
5455
let home_env = &home_env;
5556
let xdg_config_home_env = &xdg_config_home_env;
5657
let git_prefix = &git_prefix;
58+
let mut install_path = use_installation
59+
.then(|| crate::env::git::install_config_path())
60+
.flatten();
5761
let metas = [git_config::source::Kind::System, git_config::source::Kind::Global]
5862
.iter()
5963
.flat_map(|kind| kind.sources())
60-
.filter_map(|source| {
61-
match source {
62-
git_config::Source::System if !use_system => return None,
63-
git_config::Source::Git if !use_git => return None,
64-
git_config::Source::User if !use_user => return None,
65-
_ => {}
64+
.filter_map(|source| match install_path.take() {
65+
Some(install_path) => (
66+
&git_config::Source::System,
67+
git_path::from_bstr(install_path).into_owned(),
68+
)
69+
.into(),
70+
None => {
71+
match source {
72+
git_config::Source::System if !use_system => return None,
73+
git_config::Source::Git if !use_git => return None,
74+
git_config::Source::User if !use_user => return None,
75+
_ => {}
76+
}
77+
source
78+
.storage_location(&mut |name| {
79+
match name {
80+
git_ if git_.starts_with("GIT_") => Some(git_prefix),
81+
"XDG_CONFIG_HOME" => Some(xdg_config_home_env),
82+
"HOME" => Some(home_env),
83+
_ => None,
84+
}
85+
.and_then(|perm| std::env::var_os(name).and_then(|val| perm.check(val).ok().flatten()))
86+
})
87+
.map(|p| (source, p.into_owned()))
6688
}
67-
let path = source
68-
.storage_location(&mut |name| {
69-
match name {
70-
git_ if git_.starts_with("GIT_") => Some(git_prefix),
71-
"XDG_CONFIG_HOME" => Some(xdg_config_home_env),
72-
"HOME" => Some(home_env),
73-
_ => None,
74-
}
75-
.and_then(|perm| std::env::var_os(name).and_then(|val| perm.check(val).ok().flatten()))
76-
})
77-
.map(|p| p.into_owned());
78-
79-
git_config::file::Metadata {
80-
path,
81-
source: *source,
82-
level: 0,
83-
trust: git_sec::Trust::Full,
84-
}
85-
.into()
89+
})
90+
.map(|(source, path)| git_config::file::Metadata {
91+
path: Some(path),
92+
source: *source,
93+
level: 0,
94+
trust: git_sec::Trust::Full,
8695
});
8796

8897
let err_on_nonexisting_paths = false;

git-repository/src/repository/permissions.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ pub struct Permissions {
1414
/// Configure security relevant options when loading a git configuration.
1515
#[derive(Copy, Clone, Ord, PartialOrd, PartialEq, Eq, Debug, Hash)]
1616
pub struct Config {
17+
/// The git binary may come with configuration as part of its configuration, and if this is true (default false)
18+
/// we will load the configuration of the git binary, if present and not a duplicate of the ones below.
19+
///
20+
/// It's disable by default as it involves executing the git binary once per execution of the application.
21+
pub git_binary: bool,
1722
/// Whether to use the system configuration.
1823
/// This is defined as `$(prefix)/etc/gitconfig` on unix.
1924
pub system: bool,
@@ -40,6 +45,7 @@ impl Config {
4045
/// Allow everything which usually relates to a fully trusted environment
4146
pub fn all() -> Self {
4247
Config {
48+
git_binary: false,
4349
system: true,
4450
git: true,
4551
user: true,
@@ -108,6 +114,7 @@ impl Permissions {
108114
pub fn isolated() -> Self {
109115
Permissions {
110116
config: Config {
117+
git_binary: false,
111118
system: false,
112119
git: false,
113120
user: false,

0 commit comments

Comments
 (0)