use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
const TMP_DIR: &str = env!("CARGO_TARGET_TMPDIR");
pub fn init(dir: &str) -> PathBuf {
let dir = PathBuf::from(TMP_DIR).join(dir);
println!("git init: '{}'.", dir.display());
if dir.exists() {
fs::remove_dir_all(&dir).unwrap();
}
fs::create_dir(&dir).unwrap();
Command::new("git")
.arg("init")
.arg("--initial-branch=main")
.current_dir(&dir)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.unwrap();
Command::new("git")
.arg("config")
.arg("--local")
.arg("user.name")
.arg("Git Slides")
.current_dir(&dir)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.unwrap();
Command::new("git")
.arg("config")
.arg("--local")
.arg("user.email")
.arg("git@slides")
.current_dir(&dir)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.unwrap();
dir
}
pub fn commit(dir: &Path, message: &str) {
Command::new("git")
.arg("commit")
.arg("--allow-empty")
.arg("--message")
.arg(message)
.current_dir(dir)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.unwrap();
}
pub fn status(dir: &Path) -> String {
let output = Command::new("git")
.arg("rev-list")
.arg("--max-count=1")
.arg("--no-commit-header")
.arg("--format=%s")
.arg("HEAD")
.current_dir(dir)
.output()
.unwrap();
String::from_utf8_lossy(&output.stdout).trim().to_owned()
}
pub fn checkout(dir: &Path, ref_: &str) {
Command::new("git")
.arg("checkout")
.arg(ref_)
.current_dir(dir)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.unwrap();
}
pub fn add(dir: &Path, file: &Path) {
Command::new("git")
.arg("add")
.arg(file)
.current_dir(dir)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.unwrap();
}
pub fn has_stashed_changes(dir: &Path) -> bool {
let output = Command::new("git")
.arg("stash")
.arg("list")
.current_dir(dir)
.output()
.unwrap();
!String::from_utf8_lossy(&output.stdout).trim().is_empty()
}