Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/SantaClaas/gigit. git client
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495//! 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]); }
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]) { let output = Command::new("git") .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") .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) ); }}