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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
use libc::{c_char, c_int, size_t};
use std::cmp::Ordering;
use std::ffi::{CString, OsStr, OsString};
use std::iter::IntoIterator;
use std::path::{Component, Path, PathBuf};

use crate::{raw, Error};

#[doc(hidden)]
pub trait IsNull {
    fn is_ptr_null(&self) -> bool;
}
impl<T> IsNull for *const T {
    fn is_ptr_null(&self) -> bool {
        self.is_null()
    }
}
impl<T> IsNull for *mut T {
    fn is_ptr_null(&self) -> bool {
        self.is_null()
    }
}

#[doc(hidden)]
pub trait Binding: Sized {
    type Raw;

    unsafe fn from_raw(raw: Self::Raw) -> Self;
    fn raw(&self) -> Self::Raw;

    unsafe fn from_raw_opt<T>(raw: T) -> Option<Self>
    where
        T: Copy + IsNull,
        Self: Binding<Raw = T>,
    {
        if raw.is_ptr_null() {
            None
        } else {
            Some(Binding::from_raw(raw))
        }
    }
}

/// Converts an iterator of repo paths into a git2-compatible array of cstrings.
///
/// Only use this for repo-relative paths or pathspecs.
///
/// See `iter2cstrs` for more details.
pub fn iter2cstrs_paths<T, I>(
    iter: I,
) -> Result<(Vec<CString>, Vec<*const c_char>, raw::git_strarray), Error>
where
    T: IntoCString,
    I: IntoIterator<Item = T>,
{
    let cstrs = iter
        .into_iter()
        .map(|i| fixup_windows_path(i.into_c_string()?))
        .collect::<Result<Vec<CString>, _>>()?;
    iter2cstrs(cstrs)
}

/// Converts an iterator of things into a git array of c-strings.
///
/// Returns a tuple `(cstrings, pointers, git_strarray)`. The first two values
/// should not be dropped before `git_strarray`.
pub fn iter2cstrs<T, I>(
    iter: I,
) -> Result<(Vec<CString>, Vec<*const c_char>, raw::git_strarray), Error>
where
    T: IntoCString,
    I: IntoIterator<Item = T>,
{
    let cstrs = iter
        .into_iter()
        .map(|i| i.into_c_string())
        .collect::<Result<Vec<CString>, _>>()?;
    let ptrs = cstrs.iter().map(|i| i.as_ptr()).collect::<Vec<_>>();
    let raw = raw::git_strarray {
        strings: ptrs.as_ptr() as *mut _,
        count: ptrs.len() as size_t,
    };
    Ok((cstrs, ptrs, raw))
}

#[cfg(unix)]
pub fn bytes2path(b: &[u8]) -> &Path {
    use std::os::unix::prelude::*;
    Path::new(OsStr::from_bytes(b))
}
#[cfg(windows)]
pub fn bytes2path(b: &[u8]) -> &Path {
    use std::str;
    Path::new(str::from_utf8(b).unwrap())
}

/// A class of types that can be converted to C strings.
///
/// These types are represented internally as byte slices and it is quite rare
/// for them to contain an interior 0 byte.
pub trait IntoCString {
    /// Consume this container, converting it into a CString
    fn into_c_string(self) -> Result<CString, Error>;
}

impl<'a, T: IntoCString + Clone> IntoCString for &'a T {
    fn into_c_string(self) -> Result<CString, Error> {
        self.clone().into_c_string()
    }
}

impl<'a> IntoCString for &'a str {
    fn into_c_string(self) -> Result<CString, Error> {
        Ok(CString::new(self)?)
    }
}

impl IntoCString for String {
    fn into_c_string(self) -> Result<CString, Error> {
        Ok(CString::new(self.into_bytes())?)
    }
}

impl IntoCString for CString {
    fn into_c_string(self) -> Result<CString, Error> {
        Ok(self)
    }
}

impl<'a> IntoCString for &'a Path {
    fn into_c_string(self) -> Result<CString, Error> {
        let s: &OsStr = self.as_ref();
        s.into_c_string()
    }
}

impl IntoCString for PathBuf {
    fn into_c_string(self) -> Result<CString, Error> {
        let s: OsString = self.into();
        s.into_c_string()
    }
}

impl<'a> IntoCString for &'a OsStr {
    fn into_c_string(self) -> Result<CString, Error> {
        self.to_os_string().into_c_string()
    }
}

impl IntoCString for OsString {
    #[cfg(unix)]
    fn into_c_string(self) -> Result<CString, Error> {
        use std::os::unix::prelude::*;
        let s: &OsStr = self.as_ref();
        Ok(CString::new(s.as_bytes())?)
    }
    #[cfg(windows)]
    fn into_c_string(self) -> Result<CString, Error> {
        match self.to_str() {
            Some(s) => s.into_c_string(),
            None => Err(Error::from_str(
                "only valid unicode paths are accepted on windows",
            )),
        }
    }
}

impl<'a> IntoCString for &'a [u8] {
    fn into_c_string(self) -> Result<CString, Error> {
        Ok(CString::new(self)?)
    }
}

impl IntoCString for Vec<u8> {
    fn into_c_string(self) -> Result<CString, Error> {
        Ok(CString::new(self)?)
    }
}

pub fn into_opt_c_string<S>(opt_s: Option<S>) -> Result<Option<CString>, Error>
where
    S: IntoCString,
{
    match opt_s {
        None => Ok(None),
        Some(s) => Ok(Some(s.into_c_string()?)),
    }
}

pub fn c_cmp_to_ordering(cmp: c_int) -> Ordering {
    match cmp {
        0 => Ordering::Equal,
        n if n < 0 => Ordering::Less,
        _ => Ordering::Greater,
    }
}

/// Converts a path to a CString that is usable by the libgit2 API.
///
/// Checks if it is a relative path.
///
/// On Windows, this also requires the path to be valid unicode, and translates
/// back slashes to forward slashes.
pub fn path_to_repo_path(path: &Path) -> Result<CString, Error> {
    macro_rules! err {
        ($msg:literal, $path:expr) => {
            return Err(Error::from_str(&format!($msg, $path.display())))
        };
    }
    match path.components().next() {
        None => return Err(Error::from_str("repo path should not be empty")),
        Some(Component::Prefix(_)) => err!(
            "repo path `{}` should be relative, not a windows prefix",
            path
        ),
        Some(Component::RootDir) => err!("repo path `{}` should be relative", path),
        Some(Component::CurDir) => err!("repo path `{}` should not start with `.`", path),
        Some(Component::ParentDir) => err!("repo path `{}` should not start with `..`", path),
        Some(Component::Normal(_)) => {}
    }
    #[cfg(windows)]
    {
        match path.to_str() {
            None => {
                return Err(Error::from_str(
                    "only valid unicode paths are accepted on windows",
                ))
            }
            Some(s) => return fixup_windows_path(s),
        }
    }
    #[cfg(not(windows))]
    {
        path.into_c_string()
    }
}

pub fn cstring_to_repo_path<T: IntoCString>(path: T) -> Result<CString, Error> {
    fixup_windows_path(path.into_c_string()?)
}

#[cfg(windows)]
fn fixup_windows_path<P: Into<Vec<u8>>>(path: P) -> Result<CString, Error> {
    let mut bytes: Vec<u8> = path.into();
    for i in 0..bytes.len() {
        if bytes[i] == b'\\' {
            bytes[i] = b'/';
        }
    }
    Ok(CString::new(bytes)?)
}

#[cfg(not(windows))]
fn fixup_windows_path(path: CString) -> Result<CString, Error> {
    Ok(path)
}

#[cfg(test)]
mod tests {
    use super::*;

    macro_rules! assert_err {
        ($path:expr, $msg:expr) => {
            match path_to_repo_path(Path::new($path)) {
                Ok(_) => panic!("expected `{}` to err", $path),
                Err(e) => assert_eq!(e.message(), $msg),
            }
        };
    }

    macro_rules! assert_repo_path_ok {
        ($path:expr) => {
            assert_repo_path_ok!($path, $path)
        };
        ($path:expr, $expect:expr) => {
            assert_eq!(
                path_to_repo_path(Path::new($path)),
                Ok(CString::new($expect).unwrap())
            );
        };
    }

    #[test]
    #[cfg(windows)]
    fn path_to_repo_path_translate() {
        assert_repo_path_ok!("foo");
        assert_repo_path_ok!("foo/bar");
        assert_repo_path_ok!(r"foo\bar", "foo/bar");
        assert_repo_path_ok!(r"foo\bar\", "foo/bar/");
    }

    #[test]
    fn path_to_repo_path_no_weird() {
        assert_err!("", "repo path should not be empty");
        assert_err!("./foo", "repo path `./foo` should not start with `.`");
        assert_err!("../foo", "repo path `../foo` should not start with `..`");
    }

    #[test]
    #[cfg(not(windows))]
    fn path_to_repo_path_no_absolute() {
        assert_err!("/", "repo path `/` should be relative");
        assert_repo_path_ok!("foo/bar");
    }

    #[test]
    #[cfg(windows)]
    fn path_to_repo_path_no_absolute() {
        assert_err!(
            r"c:",
            r"repo path `c:` should be relative, not a windows prefix"
        );
        assert_err!(
            r"c:\",
            r"repo path `c:\` should be relative, not a windows prefix"
        );
        assert_err!(
            r"c:temp",
            r"repo path `c:temp` should be relative, not a windows prefix"
        );
        assert_err!(
            r"\\?\UNC\a\b\c",
            r"repo path `\\?\UNC\a\b\c` should be relative, not a windows prefix"
        );
        assert_err!(
            r"\\?\c:\foo",
            r"repo path `\\?\c:\foo` should be relative, not a windows prefix"
        );
        assert_err!(
            r"\\.\COM42",
            r"repo path `\\.\COM42` should be relative, not a windows prefix"
        );
        assert_err!(
            r"\\a\b",
            r"repo path `\\a\b` should be relative, not a windows prefix"
        );
        assert_err!(r"\", r"repo path `\` should be relative");
        assert_err!(r"/", r"repo path `/` should be relative");
        assert_err!(r"\foo", r"repo path `\foo` should be relative");
        assert_err!(r"/foo", r"repo path `/foo` should be relative");
    }
}