use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use crate::index;
fn scratch(label: &str) -> PathBuf {
let dir = std::env::temp_dir().join(format!("rq-{}-{label}", std::process::id()));
let _ = fs::remove_dir_all(&dir);
fs::create_dir_all(&dir).unwrap();
dir
}
fn git(dir: &Path, args: &[&str]) {
let _ = Command::new("git").args(args).current_dir(dir).output();
}
fn rev_parse(dir: &Path, args: &[&str]) -> String {
let out = Command::new("git")
.args(args)
.current_dir(dir)
.output()
.expect("git");
String::from_utf8_lossy(&out.stdout).trim().to_string()
}
#[test]
fn git_metadata_read_from_disk_matches_git() {
let dir = scratch("gitmeta");
fs::write(dir.join("a.rb"), "class A\nend\n").unwrap();
git(&dir, &["init", "-q"]);
git(&dir, &["add", "-A"]);
git(
&dir,
&[
"-c",
"user.email=t@e.st",
"-c",
"user.name=test",
"commit",
"-qm",
"init",
],
);
let expected = rev_parse(&dir, &["rev-parse", "HEAD"]);
assert_eq!(
index::git_head(&dir).as_deref(),
Some(expected.as_str()),
"loose ref"
);
git(&dir, &["pack-refs", "--all"]);
assert_eq!(
index::git_head(&dir).as_deref(),
Some(expected.as_str()),
"packed ref"
);
git(&dir, &["checkout", "-q", "--detach"]);
assert_eq!(
index::git_head(&dir).as_deref(),
Some(expected.as_str()),
"detached HEAD"
);
assert!(index::branch_changed_files(&dir).is_empty());
}