1 use std::path::{Path, PathBuf};
2 use std::io;
3 #[cfg(unix)]
4 use std::ptr;
5 use tempdir::TempDir;
6 use url::Url;
7
8 use Repository;
9
10 macro_rules! t {
11 ($e:expr) => (match $e {
12 Ok(e) => e,
13 Err(e) => panic!("{} failed with {}", stringify!($e), e),
14 })
15 }
16
repo_init() -> (TempDir, Repository)17 pub fn repo_init() -> (TempDir, Repository) {
18 let td = TempDir::new("test").unwrap();
19 let repo = Repository::init(td.path()).unwrap();
20 {
21 let mut config = repo.config().unwrap();
22 config.set_str("user.name", "name").unwrap();
23 config.set_str("user.email", "email").unwrap();
24 let mut index = repo.index().unwrap();
25 let id = index.write_tree().unwrap();
26
27 let tree = repo.find_tree(id).unwrap();
28 let sig = repo.signature().unwrap();
29 repo.commit(Some("HEAD"), &sig, &sig, "initial",
30 &tree, &[]).unwrap();
31 }
32 (td, repo)
33 }
34
path2url(path: &Path) -> String35 pub fn path2url(path: &Path) -> String {
36 Url::from_file_path(path).unwrap().to_string()
37 }
38
39 #[cfg(windows)]
realpath(original: &Path) -> io::Result<PathBuf>40 pub fn realpath(original: &Path) -> io::Result<PathBuf> {
41 Ok(original.to_path_buf())
42 }
43 #[cfg(unix)]
realpath(original: &Path) -> io::Result<PathBuf>44 pub fn realpath(original: &Path) -> io::Result<PathBuf> {
45 use std::ffi::{CStr, OsString, CString};
46 use std::os::unix::prelude::*;
47 use libc::{self, c_char};
48 extern {
49 fn realpath(name: *const c_char, resolved: *mut c_char) -> *mut c_char;
50 }
51 unsafe {
52 let cstr = try!(CString::new(original.as_os_str().as_bytes()));
53 let ptr = realpath(cstr.as_ptr(), ptr::null_mut());
54 if ptr.is_null() {
55 return Err(io::Error::last_os_error())
56 }
57 let bytes = CStr::from_ptr(ptr).to_bytes().to_vec();
58 libc::free(ptr as *mut _);
59 Ok(PathBuf::from(OsString::from_vec(bytes)))
60 }
61 }
62