gitbutler/packages/tauri/tests/common.rs

115 lines
3.6 KiB
Rust
Raw Normal View History

2023-10-12 11:36:48 +03:00
use gitbutler::git;
pub fn temp_dir() -> std::path::PathBuf {
tempfile::tempdir()
.expect("failed to create temp dir")
.into_path()
}
2023-10-12 15:25:01 +03:00
pub struct TestProject {
local_repository: git::Repository,
}
impl Default for TestProject {
fn default() -> Self {
let path = temp_dir();
let local_repository =
git::Repository::init(path.clone()).expect("failed to init repository");
let mut index = local_repository.index().expect("failed to get index");
let oid = index.write_tree().expect("failed to write tree");
let signature = git::Signature::now("test", "test@email.com").unwrap();
local_repository
.commit(
Some("HEAD"),
&signature,
&signature,
"Initial commit",
&local_repository
.find_tree(oid)
.expect("failed to find tree"),
&[],
)
.expect("failed to commit");
let remote_path = temp_dir();
2023-10-12 16:35:50 +03:00
let remote_repository = git::Repository::init_opts(
remote_path,
git2::RepositoryInitOptions::new()
.bare(true)
.external_template(false),
)
.expect("failed to init repository");
2023-10-12 15:25:01 +03:00
{
let mut remote = local_repository
.remote(
"origin",
remote_repository
.path()
.to_str()
.expect("failed to convert path to str"),
)
.expect("failed to add remote");
remote
.push(&["refs/heads/master:refs/heads/master"], None)
.expect("failed to push");
}
2023-10-12 16:14:02 +03:00
Self { local_repository }
2023-10-12 15:25:01 +03:00
}
}
impl TestProject {
pub fn path(&self) -> &std::path::Path {
self.local_repository.workdir().unwrap()
}
pub fn push(&self) {
let mut origin = self.local_repository.find_remote("origin").unwrap();
origin
.push(&["refs/heads/master:refs/heads/master"], None)
.unwrap();
}
pub fn reset_hard(&self, oid: git::Oid) {
let commit = self.local_repository.find_commit(oid).unwrap();
self.local_repository
.reset(&commit, git2::ResetType::Hard, None)
.unwrap();
}
2023-10-12 16:14:02 +03:00
pub fn find_commit(&self, oid: git::Oid) -> Result<git::Commit, git::Error> {
self.local_repository.find_commit(oid)
}
2023-10-12 15:25:01 +03:00
pub fn commit_all(&self, message: &str) -> git::Oid {
let mut index = self.local_repository.index().expect("failed to get index");
index
.add_all(["."], git2::IndexAddOption::DEFAULT, None)
.expect("failed to add all");
index.write().expect("failed to write index");
let oid = index.write_tree().expect("failed to write tree");
let signature = git::Signature::now("test", "test@email.com").unwrap();
self.local_repository
.commit(
Some("HEAD"),
&signature,
&signature,
message,
&self
.local_repository
.find_tree(oid)
.expect("failed to find tree"),
&[&self
.local_repository
.find_commit(
self.local_repository
.refname_to_id("HEAD")
.expect("failed to get head"),
)
.expect("failed to find commit")],
)
.expect("failed to commit")
}
2023-10-12 11:36:48 +03:00
}