1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use std::collections::BTreeSet;
use crate::{bstr::ByteSlice, config};
impl crate::Repository {
pub fn config_snapshot(&self) -> config::Snapshot<'_> {
config::Snapshot { repo: self }
}
pub fn config_snapshot_mut(&mut self) -> config::SnapshotMut<'_> {
let config = self.config.resolved.as_ref().clone();
config::SnapshotMut {
repo: Some(self),
config,
}
}
pub fn open_options(&self) -> &crate::open::Options {
&self.options
}
#[cfg(feature = "blocking-network-client")]
pub fn ssh_connect_options(
&self,
) -> Result<git_protocol::transport::client::ssh::connect::Options, config::ssh_connect_options::Error> {
use crate::config::{
cache::util::ApplyLeniency,
tree::{gitoxide, Core, Ssh},
};
let config = &self.config.resolved;
let mut trusted = self.filter_config_section();
let mut fallback_active = false;
let ssh_command = config
.string_filter("core", None, Core::SSH_COMMAND.name, &mut trusted)
.or_else(|| {
fallback_active = true;
config.string_filter(
"gitoxide",
Some("ssh".into()),
gitoxide::Ssh::COMMAND_WITHOUT_SHELL_FALLBACK.name,
&mut trusted,
)
})
.map(|cmd| git_path::from_bstr(cmd).into_owned().into());
let opts = git_protocol::transport::client::ssh::connect::Options {
disallow_shell: fallback_active,
command: ssh_command,
kind: config
.string_filter_by_key("ssh.variant", &mut trusted)
.and_then(|variant| Ssh::VARIANT.try_into_variant(variant).transpose())
.transpose()
.with_leniency(self.options.lenient_config)?,
};
Ok(opts)
}
pub fn object_hash(&self) -> git_hash::Kind {
self.config.object_hash
}
}
#[cfg(any(feature = "blocking-network-client", feature = "async-network-client"))]
mod transport;
mod remote {
use std::{borrow::Cow, collections::BTreeSet};
use crate::{bstr::ByteSlice, remote};
impl crate::Repository {
pub fn remote_names(&self) -> BTreeSet<&str> {
self.subsection_names_of("remote")
}
pub fn remote_default_name(&self, direction: remote::Direction) -> Option<Cow<'_, str>> {
let name = (direction == remote::Direction::Push)
.then(|| {
self.config
.resolved
.string_filter("remote", None, "pushDefault", &mut self.filter_config_section())
.and_then(|s| match s {
Cow::Borrowed(s) => s.to_str().ok().map(Cow::Borrowed),
Cow::Owned(s) => s.to_str().ok().map(|s| Cow::Owned(s.into())),
})
})
.flatten();
name.or_else(|| {
let names = self.remote_names();
match names.len() {
0 => None,
1 => names.iter().next().copied().map(Cow::Borrowed),
_more_than_one => names.get("origin").copied().map(Cow::Borrowed),
}
})
}
}
}
mod branch {
use std::{borrow::Cow, collections::BTreeSet, convert::TryInto};
use git_ref::FullNameRef;
use git_validate::reference::name::Error as ValidateNameError;
use crate::bstr::BStr;
impl crate::Repository {
pub fn branch_names(&self) -> BTreeSet<&str> {
self.subsection_names_of("branch")
}
pub fn branch_remote_ref<'a>(
&self,
short_branch_name: impl Into<&'a BStr>,
) -> Option<Result<Cow<'_, FullNameRef>, ValidateNameError>> {
self.config
.resolved
.string("branch", Some(short_branch_name.into()), "merge")
.map(crate::config::tree::branch::Merge::try_into_fullrefname)
}
pub fn branch_remote_name<'a>(
&self,
short_branch_name: impl Into<&'a BStr>,
) -> Option<crate::remote::Name<'_>> {
self.config
.resolved
.string("branch", Some(short_branch_name.into()), "remote")
.and_then(|name| name.try_into().ok())
}
}
}
impl crate::Repository {
pub(crate) fn filter_config_section(&self) -> fn(&git_config::file::Metadata) -> bool {
self.options
.filter_config_section
.unwrap_or(config::section::is_trusted)
}
fn subsection_names_of<'a>(&'a self, header_name: &'a str) -> BTreeSet<&'a str> {
self.config
.resolved
.sections_by_name(header_name)
.map(|it| {
let filter = self.filter_config_section();
it.filter(move |s| filter(s.meta()))
.filter_map(|section| section.header().subsection_name().and_then(|b| b.to_str().ok()))
.collect()
})
.unwrap_or_default()
}
}