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
use std::{borrow::Cow, path::PathBuf};
use bstr::BStr;
use crate::Path;
pub mod interpolate {
use std::path::PathBuf;
#[derive(Clone, Copy)]
pub struct Context<'a> {
pub git_install_dir: Option<&'a std::path::Path>,
pub home_dir: Option<&'a std::path::Path>,
pub home_for_user: Option<fn(&str) -> Option<PathBuf>>,
}
impl Default for Context<'_> {
fn default() -> Self {
Context {
git_install_dir: None,
home_dir: None,
home_for_user: Some(home_for_user),
}
}
}
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error {
#[error("{} is missing", .what)]
Missing { what: &'static str },
#[error("Ill-formed UTF-8 in {}", .what)]
Utf8Conversion {
what: &'static str,
#[source]
err: git_path::Utf8Error,
},
#[error("Ill-formed UTF-8 in username")]
UsernameConversion(#[from] std::str::Utf8Error),
#[error("User interpolation is not available on this platform")]
UserInterpolationUnsupported,
}
#[cfg_attr(windows, allow(unused_variables))]
pub fn home_for_user(name: &str) -> Option<PathBuf> {
#[cfg(not(any(target_os = "android", target_os = "windows")))]
{
let cname = std::ffi::CString::new(name).ok()?;
#[allow(unsafe_code)]
let pwd = unsafe { libc::getpwnam(cname.as_ptr()) };
if pwd.is_null() {
None
} else {
use std::os::unix::ffi::OsStrExt;
#[allow(unsafe_code)]
let cstr = unsafe { std::ffi::CStr::from_ptr((*pwd).pw_dir) };
Some(std::ffi::OsStr::from_bytes(cstr.to_bytes()).into())
}
}
#[cfg(any(target_os = "android", target_os = "windows"))]
{
None
}
}
}
impl<'a> std::ops::Deref for Path<'a> {
type Target = BStr;
fn deref(&self) -> &Self::Target {
self.value.as_ref()
}
}
impl<'a> AsRef<[u8]> for Path<'a> {
fn as_ref(&self) -> &[u8] {
self.value.as_ref()
}
}
impl<'a> AsRef<BStr> for Path<'a> {
fn as_ref(&self) -> &BStr {
self.value.as_ref()
}
}
impl<'a> From<Cow<'a, BStr>> for Path<'a> {
fn from(value: Cow<'a, BStr>) -> Self {
Path { value }
}
}
impl<'a> Path<'a> {
pub fn interpolate(
self,
interpolate::Context {
git_install_dir,
home_dir,
home_for_user,
}: interpolate::Context<'_>,
) -> Result<Cow<'a, std::path::Path>, interpolate::Error> {
if self.is_empty() {
return Err(interpolate::Error::Missing { what: "path" });
}
const PREFIX: &[u8] = b"%(prefix)/";
const USER_HOME: &[u8] = b"~/";
if self.starts_with(PREFIX) {
let git_install_dir = git_install_dir.ok_or(interpolate::Error::Missing {
what: "git install dir",
})?;
let (_prefix, path_without_trailing_slash) = self.split_at(PREFIX.len());
let path_without_trailing_slash =
git_path::try_from_bstring(path_without_trailing_slash).map_err(|err| {
interpolate::Error::Utf8Conversion {
what: "path past %(prefix)",
err,
}
})?;
Ok(git_install_dir.join(path_without_trailing_slash).into())
} else if self.starts_with(USER_HOME) {
let home_path = home_dir.ok_or(interpolate::Error::Missing { what: "home dir" })?;
let (_prefix, val) = self.split_at(USER_HOME.len());
let val = git_path::try_from_byte_slice(val).map_err(|err| interpolate::Error::Utf8Conversion {
what: "path past ~/",
err,
})?;
Ok(home_path.join(val).into())
} else if self.starts_with(b"~") && self.contains(&b'/') {
self.interpolate_user(home_for_user.ok_or(interpolate::Error::Missing {
what: "home for user lookup",
})?)
} else {
Ok(git_path::from_bstr(self.value))
}
}
#[cfg(any(target_os = "windows", target_os = "android"))]
fn interpolate_user(
self,
_home_for_user: fn(&str) -> Option<PathBuf>,
) -> Result<Cow<'a, std::path::Path>, interpolate::Error> {
Err(interpolate::Error::UserInterpolationUnsupported)
}
#[cfg(not(any(target_os = "windows", target_os = "android")))]
fn interpolate_user(
self,
home_for_user: fn(&str) -> Option<PathBuf>,
) -> Result<Cow<'a, std::path::Path>, interpolate::Error> {
let (_prefix, val) = self.split_at("/".len());
let i = val
.iter()
.position(|&e| e == b'/')
.ok_or(interpolate::Error::Missing { what: "/" })?;
let (username, path_with_leading_slash) = val.split_at(i);
let username = std::str::from_utf8(username)?;
let home = home_for_user(username).ok_or(interpolate::Error::Missing { what: "pwd user info" })?;
let path_past_user_prefix =
git_path::try_from_byte_slice(&path_with_leading_slash["/".len()..]).map_err(|err| {
interpolate::Error::Utf8Conversion {
what: "path past ~user/",
err,
}
})?;
Ok(home.join(path_past_user_prefix).into())
}
}