use std::path::Path;
use git_url_parse::GitUrlParseError;
use thiserror::Error;
mod domain;
mod repository;
mod url;
pub use domain::{actor::Actor, commit::Commit};
pub use repository::{Local, Remote, Repository};
pub use url::GitUrl;
pub fn open_repository<P: AsRef<Path>>(p: P) -> Result<Repository<Local>, Error> {
Repository::<Local>::new(p)
}
pub fn clone_repository<P: AsRef<Path>>(
url: &str,
dest: Option<P>,
) -> Result<Repository<Local>, Error> {
Repository::<Remote>::new(url, dest)
}
#[derive(Debug, Error)]
pub enum Error {
#[error(transparent)]
Git(#[from] git2::Error),
#[error(transparent)]
GitUrlError(#[from] GitUrlParseError),
#[error("URL scheme was {0}, cannot clone URL.")]
UrlScheme(String),
#[error("{0}")]
PathError(String),
}
#[cfg(test)]
mod common {
use once_cell::sync::Lazy;
use std::{fs, path::Path};
use tempfile::TempDir;
use super::{Local, Repository};
pub const EXPECTED_MSG: &str = "commit msg";
pub const EXPECTED_ACTOR_NAME: &str = "test";
pub const EXPECTED_ACTOR_EMAIL: &str = "test@example.com";
fn make_repo(tmpdir: &TempDir) {
let repo = git2::Repository::init(tmpdir.path()).expect("Failed to init repo");
let fp = tmpdir.path().join("file.txt");
fs::write(&fp, "Hello World\n").expect("Failed to write to file");
let mut index = repo.index().expect("Failed to get index");
index
.add_path(Path::new("file.txt"))
.expect("Failed to add file to index");
index.write().expect("Failed to write index");
let tree_id = index.write_tree().expect("Failed to write tree");
let tree = repo.find_tree(tree_id).expect("Failed to find tree");
let sig = git2::Signature::now(EXPECTED_ACTOR_NAME, EXPECTED_ACTOR_EMAIL)
.expect("Failed to create actor signature");
repo.commit(Some("HEAD"), &sig, &sig, EXPECTED_MSG, &tree, &[])
.expect("Failed to create commit");
}
static TEST_DATA_DIR: Lazy<TempDir> = Lazy::new(|| {
let dir = TempDir::new().expect("Create temp dir");
make_repo(&dir);
dir
});
fn test_data_dir() -> &'static Path {
TEST_DATA_DIR.path()
}
pub fn init_repo() -> Repository<Local> {
Repository::<Local>::new(test_data_dir()).expect("Failed to init local repository")
}
}