#![allow(dead_code)]
use std::path::Path;
use std::process::{Command, Output};
use tempfile::TempDir;
pub struct Repo {
dir: TempDir,
home: TempDir,
remote: TempDir,
gnupg: Option<TempDir>,
}
impl Repo {
pub fn new() -> Self {
let repo = Self::bare();
let init = repo.cw(&["init"]);
assert!(init.status.success(), "init failed: {}", text(&init));
repo.git(&["add", "-A"]);
repo.commit("chore: init");
repo
}
pub fn bare() -> Self {
let repo = Self {
dir: TempDir::new().unwrap(),
home: TempDir::new().unwrap(),
remote: TempDir::new().unwrap(),
gnupg: None,
};
repo.git(&["init", "-q", "-b", "main"]);
repo.git(&["config", "user.email", "test@example.com"]);
repo.git(&["config", "user.name", "test"]);
repo.git(&["config", "commit.gpgsign", "false"]);
repo.git(&["config", "tag.gpgsign", "false"]);
Command::new("git")
.args(["init", "-q", "--bare"])
.arg(repo.remote.path())
.status()
.unwrap();
repo.git(&[
"remote",
"add",
"origin",
repo.remote.path().to_str().unwrap(),
]);
repo
}
pub fn path(&self) -> &Path {
self.dir.path()
}
pub fn git(&self, args: &[&str]) -> String {
let out = Command::new("git")
.args(args)
.current_dir(self.path())
.env("HOME", self.home.path())
.env("XDG_CONFIG_HOME", self.home.path())
.env("APPDATA", self.home.path())
.env("LOCALAPPDATA", self.home.path())
.env("USERPROFILE", self.home.path())
.env("GIT_CONFIG_NOSYSTEM", "1")
.envs(self.gnupg.iter().map(|g| ("GNUPGHOME", g.path())))
.output()
.unwrap();
assert!(out.status.success(), "git {args:?}: {}", text(&out));
String::from_utf8_lossy(&out.stdout).trim().to_string()
}
pub fn commit(&self, message: &str) {
self.git(&["commit", "-q", "--allow-empty", "-m", message]);
}
pub fn commit_with_footer(&self, message: &str, footer: &str) {
self.git(&["commit", "-q", "--allow-empty", "-m", message, "-m", footer]);
}
pub fn cw(&self, args: &[&str]) -> Output {
Command::new(env!("CARGO_BIN_EXE_cw"))
.args(args)
.current_dir(self.path())
.env("HOME", self.home.path())
.env("XDG_CONFIG_HOME", self.home.path())
.env("APPDATA", self.home.path())
.env("LOCALAPPDATA", self.home.path())
.env("USERPROFILE", self.home.path())
.env("NO_COLOR", "1")
.envs(self.gnupg.iter().map(|g| ("GNUPGHOME", g.path())))
.stdin(std::process::Stdio::null())
.output()
.unwrap()
}
pub fn next_tag(&self) -> String {
let out = self.cw(&["bump", "--dry-run"]);
assert!(out.status.success(), "bump failed: {}", text(&out));
field(&text(&out), "next_tag")
}
pub fn tag(&self, args: &[&str]) -> Output {
let mut full = vec!["tag", "--yes"];
full.extend_from_slice(args);
self.cw(&full)
}
pub fn tags(&self) -> Vec<String> {
self.git(&["tag", "--list"])
.lines()
.map(str::to_string)
.collect()
}
}
pub fn file_url(dir: &Path) -> String {
let path = dir.display().to_string().replace('\\', "/");
if path.starts_with('/') {
format!("file://{path}")
} else {
format!("file:///{path}")
}
}
pub fn text(out: &Output) -> String {
format!(
"{}{}",
String::from_utf8_lossy(&out.stdout),
String::from_utf8_lossy(&out.stderr)
)
}
pub fn field(output: &str, key: &str) -> String {
output
.lines()
.find_map(|line| line.strip_prefix(&format!("{key}: ")))
.unwrap_or_else(|| panic!("no `{key}` in output:\n{output}"))
.trim()
.to_string()
}
impl Repo {
pub fn remote_branches(&self) -> Vec<String> {
self.remote_refs("refs/heads")
}
pub fn remote_tags(&self) -> Vec<String> {
self.remote_refs("refs/tags")
}
fn remote_refs(&self, prefix: &str) -> Vec<String> {
let out = Command::new("git")
.args(["for-each-ref", "--format=%(refname:short)", prefix])
.current_dir(self.remote.path())
.output()
.unwrap();
String::from_utf8_lossy(&out.stdout)
.lines()
.map(str::to_string)
.collect()
}
pub fn enable_signing(&mut self) -> bool {
let Ok(gnupg) = tempfile::Builder::new().prefix("cwg").tempdir_in("/tmp") else {
return false;
};
let made = Command::new("gpg")
.env("GNUPGHOME", gnupg.path())
.args([
"--batch",
"--pinentry-mode",
"loopback",
"--passphrase",
"",
"--quick-gen-key",
"cw-test@example.com",
"default",
"default",
"never",
])
.output();
if !matches!(made, Ok(ref out) if out.status.success()) {
return false;
}
self.gnupg = Some(gnupg);
self.git(&["config", "user.signingkey", "cw-test@example.com"]);
true
}
pub fn tag_is_signed(&self, tag: &str) -> bool {
Command::new("git")
.args(["tag", "-v", tag])
.current_dir(self.path())
.envs(self.gnupg.iter().map(|g| ("GNUPGHOME", g.path())))
.output()
.map(|out| out.status.success())
.unwrap_or(false)
}
}
pub fn released(repo: &Repo, version: &str) {
let out = repo.tag(&["--set-version", version]);
assert!(out.status.success(), "tag {version}: {}", text(&out));
}
#[cfg(unix)]
pub struct Pty {
session: expectrl::session::OsSession,
}
#[cfg(unix)]
impl Pty {
pub fn expect(&mut self, text: &str) {
use expectrl::Expect;
if let Err(err) = self.session.expect(text) {
let mut buf = [0u8; 8192];
let seen = self.session.try_read(&mut buf).unwrap_or(0);
panic!(
"waiting for {text:?}: {err}\nunmatched output: {:?}",
String::from_utf8_lossy(&buf[..seen])
);
}
}
pub fn send(&mut self, text: &str) {
use expectrl::Expect;
self.session.send(text).unwrap();
}
pub fn line(&mut self, text: &str) {
use expectrl::Expect;
self.session.send_line(text).unwrap();
}
pub fn finish(mut self) -> bool {
use expectrl::{Expect, process::unix::WaitStatus};
let _ = self.session.expect(expectrl::Eof);
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);
loop {
match self.session.get_process().status() {
Ok(WaitStatus::Exited(_, code)) => return code == 0,
Ok(WaitStatus::StillAlive) if std::time::Instant::now() < deadline => {
std::thread::sleep(std::time::Duration::from_millis(50));
}
_ => return false,
}
}
}
}
#[cfg(unix)]
impl Drop for Pty {
fn drop(&mut self) {
let _ = self.session.get_process_mut().exit(true);
}
}
#[cfg(unix)]
impl Repo {
pub fn cw_pty(&self, args: &[&str]) -> Pty {
let mut cmd = Command::new(env!("CARGO_BIN_EXE_cw"));
cmd.args(args)
.current_dir(self.path())
.env("HOME", self.home.path())
.env("XDG_CONFIG_HOME", self.home.path())
.env("APPDATA", self.home.path())
.env("LOCALAPPDATA", self.home.path())
.env("USERPROFILE", self.home.path())
.env("NO_COLOR", "1")
.env("TERM", "xterm")
.envs(self.gnupg.iter().map(|g| ("GNUPGHOME", g.path())));
let mut session = expectrl::Session::spawn(cmd).unwrap();
session.set_expect_timeout(Some(std::time::Duration::from_secs(10)));
Pty { session }
}
}
impl Repo {
pub fn subjects(&self) -> Vec<String> {
self.git(&["log", "--format=%s"])
.lines()
.map(str::to_string)
.collect()
}
pub fn subjects_or_empty(&self) -> Vec<String> {
let out = Command::new("git")
.args(["log", "--format=%s"])
.current_dir(self.path())
.output()
.unwrap();
String::from_utf8_lossy(&out.stdout)
.lines()
.map(str::to_string)
.collect()
}
pub fn write_global_config(&self, contents: &str) {
let dir = self.home.path().join("cwizard");
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join("config.toml"), contents).unwrap();
}
pub fn write_global_file(&self, name: &str, contents: &str) {
let dir = self.home.path().join("cwizard");
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join(name), contents).unwrap();
}
pub fn home_path(&self) -> &Path {
self.home.path()
}
pub fn write(&self, name: &str, contents: &str) {
std::fs::write(self.path().join(name), contents).unwrap();
}
}