Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/SantaClaas/gigit. git client
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156//! Builds throwaway repositories to test against.//!//! These shell out to `git` rather than writing objects with `gix`. Using the//! real thing to produce the fixtures means the tests check that we read what//! git actually writes, instead of only checking that gix agrees with itself.//!//! Behind the `testing` feature so it never ships in a release build, but is//! still usable from the crates above this one — Rust has no other tidy way to//! share test helpers across crate boundaries. Requires `git` on PATH.
use std::path::Path;use std::process::Command;
use tempfile::TempDir;
/// A temporary repository that deletes itself when dropped.pub struct Fixture { directory: TempDir,}
impl Fixture { /// An initialised repository with no commits. pub fn empty() -> Self { let directory = tempfile::tempdir().expect("could not create a temporary directory"); let fixture = Self { directory };
fixture.git(&["init", "--initial-branch=main"]); // Commits need an identity, and the machine running the tests may not // have one configured globally. fixture.git(&["config", "user.name", "Fixture"]); fixture.git(&["config", "user.email", "fixture@example.com"]); fixture.git(&["config", "commit.gpgsign", "false"]);
fixture }
/// A repository with a single commit on `main`. pub fn with_one_commit() -> Self { let fixture = Self::empty(); fixture.commit("first"); fixture }
pub fn path(&self) -> &Path { self.directory.path() }
/// Write a file and commit it, so each commit has real content. pub fn commit(&self, message: &str) { let file = self.path().join(format!("{message}.txt")); std::fs::write(&file, message).expect("could not write a fixture file");
self.git(&["add", "."]); self.git(&["commit", "--message", message]); }
pub fn branch(&self, name: &str) { self.git(&["branch", name]); }
/// Create a branch and switch to it. pub fn branch_off(&self, name: &str) { self.git(&["checkout", "-b", name]); }
pub fn checkout(&self, name: &str) { self.git(&["checkout", name]); }
/// Merge one or more branches, always as a real merge commit so the /// fixture's shape is the one the test asked for. Passing more than one /// branch makes it an octopus merge. pub fn merge(&self, branches: &[&str]) { let mut arguments = vec!["merge", "--no-ff", "--no-edit"]; arguments.extend_from_slice(branches);
self.git(&arguments); }
/// Commit with a committer date of your choosing, for testing what happens /// when the clock disagrees with the topology. /// /// `--date` only moves the author date, so the committer date — the one the /// graph orders by — has to come from the environment. pub fn commit_dated(&self, message: &str, date: &str) { let file = self.path().join(format!("{message}.txt")); std::fs::write(&file, message).expect("could not write a fixture file");
self.git(&["add", "."]); self.run( &["commit", "--message", message], &[("GIT_COMMITTER_DATE", date)], ); }
/// Ask git for a commit's parents, so tests can check the graph against /// git's own answer rather than against the graph's. pub fn parents_of(&self, id: &str) -> Vec<String> { let output = self.capture(&["log", "-1", "--format=%P", id]);
output.split_whitespace().map(ToOwned::to_owned).collect() }
pub fn tag(&self, name: &str) { self.git(&["tag", name]); }
/// Fabricate a remote-tracking ref without needing a real remote. pub fn remote_branch(&self, remote: &str, name: &str) { self.git(&[ "update-ref", &format!("refs/remotes/{remote}/{name}"), "HEAD", ]); }
pub fn detach_head(&self) { self.git(&["checkout", "--detach"]); }
pub fn git(&self, arguments: &[&str]) { self.run(arguments, &[]); }
/// Run git and return its standard output, trimmed. pub fn capture(&self, arguments: &[&str]) -> String { self.run(arguments, &[]) }
fn run(&self, arguments: &[&str], environment: &[(&str, &str)]) -> String { let mut command = Command::new("git"); command .args(arguments) .current_dir(self.path()) // Keep the developer's own git config from changing the outcome. .env("GIT_CONFIG_GLOBAL", "/dev/null") .env("GIT_CONFIG_SYSTEM", "/dev/null");
for (name, value) in environment { command.env(name, value); }
let output = command .output() .expect("could not run git — it must be installed to run these tests");
assert!( output.status.success(), "git {arguments:?} failed:\n{}", String::from_utf8_lossy(&output.stderr) );
String::from_utf8_lossy(&output.stdout).trim().to_owned() }}