Debian Codegraph @

src/test.rs

raw
1 use std::fs::File;
2 use std::io;
3 use std::path::{Path, PathBuf};
4 #[cfg(unix)]
5 use std::ptr;
6 use tempfile::TempDir;
7 use url::Url;
8
9 use crate::{Branch, ObjectFormat, Oid, Repository, RepositoryInitOptions};
10
11 macro_rules! t {
12 ($e:expr) => {
13 match $e {
14 Ok(e) => e,
15 Err(e) => panic!("{} failed with {}", stringify!($e), e),
16 }
17 };
18 }
19
20 fn repo_init_ext(format: ObjectFormat) -> (TempDir, Repository) {
21 let td = TempDir::new().unwrap();
22 let mut opts = RepositoryInitOptions::new();
23 opts.initial_head("main");
24 opts.object_format(format);
25 let repo = Repository::init_opts(td.path(), &opts).unwrap();
26 {
27 let mut config = repo.config().unwrap();
28 config.set_str("user.name", "name").unwrap();
29 config.set_str("user.email", "email").unwrap();
30 let mut index = repo.index().unwrap();
31 let id = index.write_tree().unwrap();
32
33 let tree = repo.find_tree(id).unwrap();
34 let sig = repo.signature().unwrap();
35 repo.commit(Some("HEAD"), &sig, &sig, "initial\n\nbody", &tree, &[])
36 .unwrap();
37 }
38 (td, repo)
39 }
40
41 pub fn repo_init() -> (TempDir, Repository) {
42 repo_init_ext(ObjectFormat::Sha1)
43 }
44
45 #[cfg(feature = "unstable-sha256")]
46 pub fn repo_init_sha256() -> (TempDir, Repository) {
47 repo_init_ext(ObjectFormat::Sha256)
48 }
49
50 pub fn commit(repo: &Repository) -> (Oid, Oid) {
51 let mut index = t!(repo.index());
52 let root = repo.path().parent().unwrap();
53 t!(File::create(&root.join("foo")));
54 t!(index.add_path(Path::new("foo")));
55
56 let tree_id = t!(index.write_tree());
57 let tree = t!(repo.find_tree(tree_id));
58 let sig = t!(repo.signature());
59 let head_id = t!(repo.refname_to_id("HEAD"));
60 let parent = t!(repo.find_commit(head_id));
61 let commit = t!(repo.commit(Some("HEAD"), &sig, &sig, "commit", &tree, &[&parent]));
62 (commit, tree_id)
63 }
64
65 pub fn path2url(path: &Path) -> String {
66 Url::from_file_path(path).unwrap().to_string()
67 }
68
69 pub fn worktrees_env_init(repo: &Repository) -> (TempDir, Branch<'_>) {
70 let oid = repo.head().unwrap().target().unwrap();
71 let commit = repo.find_commit(oid).unwrap();
72 let branch = repo.branch("wt-branch", &commit, true).unwrap();
73 let wtdir = TempDir::new().unwrap();
74 (wtdir, branch)
75 }
76
77 #[cfg(windows)]
78 pub fn realpath(original: &Path) -> io::Result<PathBuf> {
79 Ok(original.canonicalize()?.to_path_buf())
80 }
81 #[cfg(unix)]
82 pub fn realpath(original: &Path) -> io::Result<PathBuf> {
83 use libc::c_char;
84 use std::ffi::{CStr, CString, OsString};
85 use std::os::unix::prelude::*;
86 extern "C" {
87 fn realpath(name: *const c_char, resolved: *mut c_char) -> *mut c_char;
88 }
89 unsafe {
90 let cstr = CString::new(original.as_os_str().as_bytes())?;
91 let ptr = realpath(cstr.as_ptr(), ptr::null_mut());
92 if ptr.is_null() {
93 return Err(io::Error::last_os_error());
94 }
95 let bytes = CStr::from_ptr(ptr).to_bytes().to_vec();
96 libc::free(ptr as *mut _);
97 Ok(PathBuf::from(OsString::from_vec(bytes)))
98 }
99 }