use std::error::Error;
use std::path::{Path, PathBuf};
use std::process::Command;
use tempfile::TempDir;
use crate::types::project::{CommitId, GitUrl};
pub(super) type TestResult = Result<(), Box<dyn Error>>;
pub(super) const CRLF: &[u8] = b"a\r\nb\r\n";
pub(super) struct SourceRepo {
dir: TempDir,
pub(super) url: GitUrl,
pub(super) zero: CommitId,
pub(super) one: CommitId,
pub(super) two: CommitId,
pub(super) annotated: CommitId,
pub(super) crlf_blob: String,
}
impl SourceRepo {
pub(super) fn new() -> Result<Self, Box<dyn Error>> {
let dir = tempfile::tempdir()?;
run(dir.path(), &["init", "-q", "-b", "main", "work"], DAY_ZERO)?;
let work = dir.path().join("work");
let builder = Builder {
work: work.clone(),
scratch: dir.path().join("blob"),
};
builder.stage("100644", "zero.txt", b"zero\n")?;
let zero = builder.commit("zero", DAY_ZERO)?;
builder.stage("100644", "packs/core/pack.toml", b"name = \"core\"\n")?;
let crlf_blob = builder.stage("100644", "packs/core/crlf.md", CRLF)?;
builder.stage("100644", "packs/core/lf.md", b"a\nb\n")?;
builder.stage("120000", "packs/core/link", b"pack.toml")?;
builder.stage("100755", "packs/core/run.sh", b"#!/bin/sh\necho hi\n")?;
builder.stage("100644", "packs/core/nested/deeper/file.md", b"deep\n")?;
builder.gitlink("packs/core/sub", "0123456789abcdef0123456789abcdef01234567")?;
let one = builder.commit("one", DAY_ONE)?;
run(
&work,
&["tag", "-a", "packs-v0.1.0", "-m", "annotated 0.1.0"],
DAY_ONE,
)?;
let annotated = CommitId::new(&run(&work, &["rev-parse", "packs-v0.1.0"], DAY_ONE)?)?;
builder.stage("100644", "packs/core/two.md", b"two\n")?;
let two = builder.commit("two", DAY_TWO)?;
for tag in [
"packs-v0.2.0",
"packs-v0.9.0",
"packs-v0.10.0",
"other-v9.9.9",
] {
run(&work, &["tag", tag], DAY_TWO)?;
}
let url = GitUrl::new(&format!("file://{}", work.display()))?;
Ok(Self {
dir,
url,
zero,
one,
two,
annotated,
crlf_blob,
})
}
pub(super) fn path(&self) -> PathBuf {
self.dir.path().join("work")
}
pub(super) fn config(&self, key: &str, value: &str) -> Result<(), Box<dyn Error>> {
run(&self.path(), &["config", key, value], DAY_ZERO)?;
Ok(())
}
pub(super) fn delete(&self) -> Result<(), Box<dyn Error>> {
std::fs::remove_dir_all(self.path())?;
Ok(())
}
}
const DAY_ZERO: &str = "2026-01-01T00:00:00Z";
const DAY_ONE: &str = "2026-01-01T00:00:01Z";
const DAY_TWO: &str = "2026-01-02T00:00:00Z";
struct Builder {
work: PathBuf,
scratch: PathBuf,
}
impl Builder {
fn stage(&self, mode: &str, path: &str, bytes: &[u8]) -> Result<String, Box<dyn Error>> {
std::fs::write(&self.scratch, bytes)?;
let scratch = self.scratch.display().to_string();
let id = run(
&self.work,
&["hash-object", "-w", "--no-filters", &scratch],
DAY_ZERO,
)?;
let entry = format!("{mode},{id},{path}");
run(
&self.work,
&["update-index", "--add", "--cacheinfo", &entry],
DAY_ZERO,
)?;
Ok(id)
}
fn gitlink(&self, path: &str, commit: &str) -> Result<(), Box<dyn Error>> {
let entry = format!("160000,{commit},{path}");
run(
&self.work,
&["update-index", "--add", "--cacheinfo", &entry],
DAY_ZERO,
)?;
Ok(())
}
fn commit(&self, message: &str, date: &str) -> Result<CommitId, Box<dyn Error>> {
run(&self.work, &["commit", "-q", "-m", message], date)?;
Ok(CommitId::new(&run(
&self.work,
&["rev-parse", "HEAD"],
date,
)?)?)
}
}
fn run(cwd: &Path, args: &[&str], date: &str) -> Result<String, Box<dyn Error>> {
let output = Command::new("git")
.args(args)
.current_dir(cwd)
.env("GIT_CONFIG_GLOBAL", "/dev/null")
.env("GIT_CONFIG_NOSYSTEM", "1")
.env("GIT_AUTHOR_NAME", "tester")
.env("GIT_AUTHOR_EMAIL", "tester@example.invalid")
.env("GIT_COMMITTER_NAME", "tester")
.env("GIT_COMMITTER_EMAIL", "tester@example.invalid")
.env("GIT_AUTHOR_DATE", date)
.env("GIT_COMMITTER_DATE", date)
.output()?;
if !output.status.success() {
return Err(format!(
"git {args:?} failed: {}",
String::from_utf8_lossy(&output.stderr)
)
.into());
}
Ok(String::from_utf8(output.stdout)?.trim().to_owned())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_repository_has_the_three_commits() -> TestResult {
let repo = SourceRepo::new()?;
assert_ne!(repo.zero, repo.one);
assert_ne!(repo.one, repo.two);
assert_ne!(repo.annotated, repo.one);
assert!(repo.url.as_str().starts_with("file://"));
Ok(())
}
}