#![allow(dead_code)]
use git_meta_lib::Session;
pub fn setup_repo() -> (tempfile::TempDir, gix::Repository) {
let dir = tempfile::TempDir::new().unwrap();
let _init = gix::init(dir.path()).unwrap();
let output = std::process::Command::new("git")
.args([
"-C",
&dir.path().to_string_lossy(),
"config",
"user.email",
"test@example.com",
])
.output()
.unwrap();
assert!(output.status.success());
let output = std::process::Command::new("git")
.args([
"-C",
&dir.path().to_string_lossy(),
"config",
"user.name",
"Test User",
])
.output()
.unwrap();
assert!(output.status.success());
let repo = gix::open_opts(
dir.path(),
gix::open::Options::isolated()
.config_overrides(["user.name=Test User", "user.email=test@example.com"]),
)
.unwrap();
let tree_oid = repo.empty_tree().edit().unwrap().write().unwrap().detach();
let sig = gix::actor::Signature {
name: "Test User".into(),
email: "test@example.com".into(),
time: gix::date::Time::new(946684800, 0),
};
let commit = gix::objs::Commit {
message: "initial".into(),
tree: tree_oid,
author: sig.clone(),
committer: sig,
encoding: None,
parents: Default::default(),
extra_headers: Default::default(),
};
let commit_oid = repo.write_object(&commit).unwrap().detach();
repo.reference(
"refs/heads/main",
commit_oid,
gix::refs::transaction::PreviousValue::Any,
"",
)
.unwrap();
repo.reference(
"HEAD",
commit_oid,
gix::refs::transaction::PreviousValue::Any,
"",
)
.unwrap();
(dir, repo)
}
pub fn open_session(repo: gix::Repository) -> Session {
Session::open(repo.path()).unwrap().with_timestamp(1000)
}
pub fn head_sha(repo: &gix::Repository) -> String {
repo.head_id().unwrap().to_string()
}
pub fn copy_dir_contents(src: &std::path::Path, dst: &std::path::Path) {
if !src.exists() {
return;
}
for entry in std::fs::read_dir(src).unwrap() {
let entry = entry.unwrap();
let src_path = entry.path();
let dst_path = dst.join(entry.file_name());
if src_path.is_dir() {
std::fs::create_dir_all(&dst_path).ok();
copy_dir_contents(&src_path, &dst_path);
} else {
std::fs::copy(&src_path, &dst_path).ok();
}
}
}
pub fn inject_remote_ref(
src_objects_dir: &std::path::Path,
dst_dir: &std::path::Path,
oid: gix::ObjectId,
) {
let dst_objects = dst_dir.join(".git").join("objects");
copy_dir_contents(src_objects_dir, &dst_objects);
let dst_repo = gix::open_opts(
dst_dir,
gix::open::Options::isolated()
.config_overrides(["user.name=Test User", "user.email=test@example.com"]),
)
.unwrap();
dst_repo
.reference(
"refs/meta/origin",
oid,
gix::refs::transaction::PreviousValue::Any,
"simulated fetch",
)
.unwrap();
}
pub fn reopen_session(dir: &std::path::Path, timestamp: i64) -> Session {
let repo = gix::open_opts(
dir,
gix::open::Options::isolated()
.config_overrides(["user.name=Test User", "user.email=test@example.com"]),
)
.unwrap();
Session::open(repo.path())
.unwrap()
.with_timestamp(timestamp)
}
pub fn setup_three_way_base(
base_fn: impl FnOnce(&Session),
) -> (tempfile::TempDir, tempfile::TempDir, gix::ObjectId) {
let (dir_a, repo_a) = setup_repo();
let session_a = Session::open(repo_a.path()).unwrap().with_timestamp(1000);
base_fn(&session_a);
let _ = session_a.serialize().unwrap();
let repo_a_reopen = gix::open_opts(
dir_a.path(),
gix::open::Options::isolated()
.config_overrides(["user.name=Test User", "user.email=test@example.com"]),
)
.unwrap();
let base_oid = repo_a_reopen
.find_reference("refs/meta/local/main")
.unwrap()
.into_fully_peeled_id()
.unwrap()
.detach();
let (dir_c, _repo_c) = setup_repo();
let src_objects = dir_a.path().join(".git").join("objects");
let dst_objects = dir_c.path().join(".git").join("objects");
copy_dir_contents(&src_objects, &dst_objects);
let repo_c_reopen = gix::open_opts(
dir_c.path(),
gix::open::Options::isolated()
.config_overrides(["user.name=Test User", "user.email=test@example.com"]),
)
.unwrap();
repo_c_reopen
.reference(
"refs/meta/origin",
base_oid,
gix::refs::transaction::PreviousValue::Any,
"initial sync",
)
.unwrap();
let session_c = Session::open(repo_c_reopen.path())
.unwrap()
.with_timestamp(1500);
let _ = session_c.materialize(None).unwrap();
(dir_a, dir_c, base_oid)
}