-
-
Notifications
You must be signed in to change notification settings - Fork 753
Expand file tree
/
Copy pathrepository.rs
More file actions
212 lines (188 loc) · 5.12 KB
/
Copy pathrepository.rs
File metadata and controls
212 lines (188 loc) · 5.12 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use std::{
cell::RefCell,
ffi::OsStr,
path::{Path, PathBuf},
};
use git2::{ConfigLevel, Repository, RepositoryOpenFlags};
use crate::error::Result;
///
pub type RepoPathRef = RefCell<RepoPath>;
///
#[derive(Clone, Debug)]
pub enum RepoPath {
///
Path(PathBuf),
///
Workdir {
///
gitdir: PathBuf,
///
workdir: PathBuf,
},
}
impl RepoPath {
///
pub fn gitpath(&self) -> &Path {
match self {
Self::Path(p) => p.as_path(),
Self::Workdir { gitdir, .. } => gitdir.as_path(),
}
}
///
pub fn workdir(&self) -> Option<&Path> {
match self {
Self::Path(_) => None,
Self::Workdir { workdir, .. } => Some(workdir.as_path()),
}
}
}
impl From<PathBuf> for RepoPath {
fn from(value: PathBuf) -> Self {
Self::Path(value)
}
}
impl From<&str> for RepoPath {
fn from(p: &str) -> Self {
Self::Path(PathBuf::from(p))
}
}
pub fn repo(repo_path: &RepoPath) -> Result<Repository> {
let repo = Repository::open_ext(
repo_path.gitpath(),
RepositoryOpenFlags::FROM_ENV,
Vec::<&Path>::new(),
)?;
if let Some(workdir) = repo_path.workdir() {
repo.set_workdir(workdir, false)?;
}
Ok(repo)
}
/// Works around repo opening failing under strict snap confinement.
///
/// Strict snap confinement denies libgit2 access to `/etc/gitconfig`, and
/// every repo open fails outright with something like `'/etc/gitconfig' is
/// locked: Permission denied` (see [#2984]), because opening a repo always
/// loads the system-level git config as part of the merged config.
///
/// This redirects libgit2's system-level config search path to the snap's
/// own (read-only, always accessible) install directory, which never
/// contains a `gitconfig` file, so libgit2 simply finds no system config
/// instead of failing to reach `/etc`. It mirrors what the test suite
/// already does to avoid touching real system config files, see
/// `sandbox_config_files` in `sync::tests`.
///
/// Must be called once, as early as possible at startup, before any repo
/// is opened.
///
/// [#2984]: https://github.com/gitui-org/gitui/issues/2984
#[allow(unsafe_code)]
pub fn sanitize_snap_config_search_path() {
if let Some(path) = snap_system_config_override(
std::env::var_os("SNAP").as_deref(),
std::env::var_os("SNAP_CONFINEMENT").as_deref(),
) {
// SAFETY: `set_search_path` mutates process-global libgit2 state
// and must not race with concurrent config reads. This function
// is documented to run once, before any repo is opened, so no
// other thread is touching libgit2 config state yet.
unsafe {
let _ = git2::opts::set_search_path(
ConfigLevel::System,
path,
);
}
}
}
/// Decides whether libgit2's system-level config search path needs to be
/// redirected away from `/etc` to work around strict snap confinement, and
/// if so, to what path.
///
/// Returns `None` when running unconfined, or under `classic`/`devmode`
/// confinement, both of which can reach the real filesystem, so the
/// default search path should be left untouched.
fn snap_system_config_override(
snap: Option<&OsStr>,
snap_confinement: Option<&OsStr>,
) -> Option<PathBuf> {
let snap = snap?;
// `classic` and `devmode` confinement have (near-)unrestricted
// filesystem access, so `/etc/gitconfig` is reachable there. Only
// `strict` confinement - or older snapd releases that predate the
// `SNAP_CONFINEMENT` variable and default to `strict` - need the
// workaround.
if matches!(
snap_confinement.and_then(OsStr::to_str),
Some("classic" | "devmode")
) {
return None;
}
Some(PathBuf::from(snap))
}
pub fn gix_repo(repo_path: &RepoPath) -> Result<gix::Repository> {
let mut repo: gix::Repository = gix::ThreadSafeRepository::discover_with_environment_overrides(
repo_path.gitpath(),
)
.map(Into::into)?;
if let Some(workdir) = repo_path.workdir() {
repo.set_workdir(Some(workdir.into()))?;
}
Ok(repo)
}
#[cfg(test)]
mod tests {
use super::snap_system_config_override;
use std::{ffi::OsStr, path::PathBuf};
#[test]
fn test_snap_override_not_applied_outside_snap() {
assert_eq!(snap_system_config_override(None, None), None);
assert_eq!(
snap_system_config_override(
None,
Some(OsStr::new("strict"))
),
None
);
}
#[test]
fn test_snap_override_applied_under_strict_confinement() {
assert_eq!(
snap_system_config_override(
Some(OsStr::new("/snap/gitui/380")),
Some(OsStr::new("strict"))
),
Some(PathBuf::from("/snap/gitui/380"))
);
}
#[test]
fn test_snap_override_applied_when_confinement_unknown() {
// Older snapd releases don't set `SNAP_CONFINEMENT`; assume the
// worst (strict) rather than risk failing to open the repo.
assert_eq!(
snap_system_config_override(
Some(OsStr::new("/snap/gitui/380")),
None
),
Some(PathBuf::from("/snap/gitui/380"))
);
}
#[test]
fn test_snap_override_not_applied_under_classic_confinement() {
assert_eq!(
snap_system_config_override(
Some(OsStr::new("/snap/gitui/380")),
Some(OsStr::new("classic"))
),
None
);
}
#[test]
fn test_snap_override_not_applied_under_devmode_confinement() {
assert_eq!(
snap_system_config_override(
Some(OsStr::new("/snap/gitui/380")),
Some(OsStr::new("devmode"))
),
None
);
}
}