1use std::path::Path;
2
3use git_url_parse::GitUrlParseError;
5use thiserror::Error;
6
7mod domain;
8mod repository;
9mod url;
10
11pub use domain::{actor::Actor, commit::Commit};
12pub use repository::{Local, Remote, Repository};
13pub use url::GitUrl;
14
15pub fn open_repository<P: AsRef<Path>>(p: P) -> Result<Repository<Local>, Error> {
17 Repository::<Local>::new(p)
18}
19
20pub fn clone_repository<P: AsRef<Path>>(
22 url: &str,
23 dest: Option<P>,
24) -> Result<Repository<Local>, Error> {
25 Repository::<Remote>::new(url, dest)
26}
27
28#[derive(Debug, Error)]
29pub enum Error {
30 #[error(transparent)]
32 Git(#[from] git2::Error),
33
34 #[error(transparent)]
36 GitUrlError(#[from] GitUrlParseError),
37
38 #[error("URL scheme was {0}, cannot clone URL.")]
40 UrlScheme(String),
41
42 #[error("{0}")]
44 PathError(String),
45}
46
47#[cfg(test)]
50mod common {
51 use once_cell::sync::Lazy;
52 use std::{fs, path::Path};
53 use tempfile::TempDir;
54
55 use super::{Local, Repository};
56
57 pub const EXPECTED_MSG: &str = "commit msg";
58 pub const EXPECTED_ACTOR_NAME: &str = "test";
59 pub const EXPECTED_ACTOR_EMAIL: &str = "test@example.com";
60
61 fn write_fp(root: &TempDir, path: &str, content: &str) {
63 let fp = root.path().join(path);
64 fs::write(&fp, content).expect("Failed to write to file");
65 }
66
67 fn write_to_index(index: &mut git2::Index, file: &str) {
69 index
70 .add_path(Path::new(file))
71 .expect("Failed to add file to index");
72 index.write().expect("Failed to write index");
73 }
74
75 fn write_tree<'a>(
77 repo: &'a git2::Repository,
78 index: &mut git2::Index,
79 file: &str,
80 ) -> git2::Tree<'a> {
81 write_to_index(index, file);
82
83 let tree_id = index.write_tree().expect("Failed to write tree");
84 repo.find_tree(tree_id).expect("Failed to find tree")
85 }
86
87 fn commit_file(
89 repo: &git2::Repository,
90 sig: &git2::Signature,
91 file: &str,
92 parent: Option<&git2::Commit<'_>>,
93 ) -> git2::Oid {
94 let mut index = repo.index().expect("Failed to get index");
96 let tree = write_tree(repo, &mut index, file);
97
98 let parents = match parent {
99 Some(v) => vec![v],
100 None => vec![],
101 };
102
103 repo.commit(Some("HEAD"), sig, sig, EXPECTED_MSG, &tree, &parents)
104 .expect("Failed to create commit")
105 }
106
107 fn make_repo(tmpdir: &TempDir) {
109 let repo = git2::Repository::init(tmpdir.path()).expect("Failed to init repo");
110 let sig = git2::Signature::now(EXPECTED_ACTOR_NAME, EXPECTED_ACTOR_EMAIL)
111 .expect("Failed to create actor signature");
112
113 let file = "file.txt";
114 write_fp(tmpdir, file, "Hello World\n");
115 let first_commit_id = commit_file(&repo, &sig, file, None);
116
117 write_fp(tmpdir, file, "Hello World\nFile Update\n");
118 let parent = repo
119 .find_commit(first_commit_id)
120 .expect("Failed to find first commit");
121 commit_file(&repo, &sig, file, Some(&parent));
122 }
123
124 static TEST_DATA_DIR: Lazy<TempDir> = Lazy::new(|| {
127 let dir = TempDir::new().expect("Create temp dir");
128 make_repo(&dir);
129 dir
130 });
131
132 fn test_data_dir() -> &'static Path {
134 TEST_DATA_DIR.path()
135 }
136
137 pub fn init_repo() -> Repository<Local> {
160 Repository::<Local>::new(test_data_dir()).expect("Failed to init local repository")
161 }
162}