use anyhow::{Context, Result, bail};
use std::path::{Path, PathBuf};
use tokio::process::Command;
const HARDEN: &[&str] = &["-c", "core.fsmonitor=false", "-c", "core.hooksPath=/dev/null", "-c", "protocol.ext.allow=never"];
pub async fn git(dir: &Path, args: &[&str]) -> Result<String> {
let out = Command::new("git")
.args(HARDEN)
.arg("-C")
.arg(dir)
.args(args)
.env("GIT_TERMINAL_PROMPT", "0")
.env("GIT_EDITOR", "true")
.kill_on_drop(true)
.output()
.await
.context("run git")?;
if !out.status.success() {
bail!("git {}: {}", redact(&args.join(" ")), redact(String::from_utf8_lossy(&out.stderr).trim()));
}
Ok(String::from_utf8_lossy(&out.stdout).trim_end().to_string())
}
pub fn redact(s: &str) -> String {
static RE: std::sync::LazyLock<regex::Regex> = std::sync::LazyLock::new(|| regex::Regex::new(r"://[^/@\s]+@").expect("regex"));
RE.replace_all(s, "://***@").into_owned()
}
pub fn branch_name(target: crate::forge::Target, n: u64) -> String {
match target {
crate::forge::Target::Pr => format!("nightporter/pr-{n}"),
crate::forge::Target::Issue => format!("nightporter/issue-{n}"),
}
}
pub async fn prepare_worktree(clone: &Path, wt: &Path, url: &str, fetch_ref: &str, branch: &str) -> Result<(String, String)> {
let tmp_ref = format!("refs/nightporter/{}", branch.replace('/', "-"));
git(clone, &["fetch", "--no-tags", "--quiet", "--end-of-options", url, &format!("+{fetch_ref}:{tmp_ref}")]).await?;
let sha = git(clone, &["rev-parse", &tmp_ref]).await?;
if wt.exists() && !gitfile_points_into(clone, wt).await {
std::fs::remove_dir_all(wt).with_context(|| format!("remove tampered worktree {}", wt.display()))?;
}
if wt.join(".git").exists() {
git(wt, &["checkout", "--quiet", "-B", branch, &sha]).await?;
git(wt, &["reset", "--hard", "--quiet", &sha]).await?;
git(wt, &["clean", "-fd", "--quiet"]).await?;
} else {
if let Some(p) = wt.parent() {
std::fs::create_dir_all(p)?;
}
git(clone, &["worktree", "prune"]).await?;
git(clone, &["worktree", "add", "--quiet", "-B", branch, &wt.to_string_lossy(), &sha]).await?;
}
let gitfile = std::fs::read_to_string(wt.join(".git")).context("worktree has no .git file")?;
Ok((sha, gitfile))
}
async fn gitfile_points_into(clone: &Path, wt: &Path) -> bool {
let Ok(meta) = std::fs::symlink_metadata(wt.join(".git")) else { return false };
let Ok(text) = std::fs::read_to_string(wt.join(".git")) else { return false };
let Some(dir) = text.trim_end().strip_prefix("gitdir: ") else { return false };
let Ok(common) = git(clone, &["rev-parse", "--path-format=absolute", "--git-common-dir"]).await else { return false };
let (Ok(dir), Ok(worktrees)) = (Path::new(dir).canonicalize(), Path::new(&common).join("worktrees").canonicalize()) else {
return false;
};
meta.is_file() && dir.parent() == Some(worktrees.as_path())
}
pub fn verify_gitfile(wt: &Path, expected: &str) -> Result<()> {
let meta = std::fs::symlink_metadata(wt.join(".git"))?;
let now = std::fs::read_to_string(wt.join(".git")).unwrap_or_default();
if !meta.is_file() || now != expected {
bail!("the worktree's .git was modified during the job; refusing to continue");
}
Ok(())
}
pub fn base_ref(branch: &str) -> String {
format!("refs/nightporter/base-{}", branch.replace('/', "-"))
}
pub async fn remove_worktree(clone: &Path, wt: &Path, branch: &str) -> Result<()> {
if wt.exists() {
git(clone, &["worktree", "remove", "--force", &wt.to_string_lossy()]).await.ok();
if wt.exists() {
std::fs::remove_dir_all(wt)?;
}
}
git(clone, &["worktree", "prune"]).await.ok();
git(clone, &["branch", "-D", branch]).await.ok();
git(clone, &["update-ref", "-d", &format!("refs/nightporter/{}", branch.replace('/', "-"))]).await.ok();
git(clone, &["update-ref", "-d", &base_ref(branch)]).await.ok();
Ok(())
}
pub async fn remote_head(clone: &Path, url: &str, branch: &str) -> Result<Option<String>> {
let out = git(clone, &["ls-remote", "--end-of-options", url, &format!("refs/heads/{branch}")]).await?;
Ok(out.split_whitespace().next().map(String::from))
}
pub async fn push(clone: &Path, url: &str, sha: &str, remote_branch: &str) -> Result<()> {
git(clone, &["push", "--quiet", "--no-verify", "--end-of-options", url, &format!("{sha}:refs/heads/{remote_branch}")]).await.map(|_| ())
}
pub async fn head(wt: &Path) -> Result<String> {
git(wt, &["rev-parse", "HEAD"]).await
}
pub async fn is_ancestor(wt: &Path, a: &str, b: &str) -> bool {
git(wt, &["merge-base", "--is-ancestor", a, b]).await.is_ok()
}
pub fn remote_url(clone: &Path, repo: &crate::forge::RepoId, accounts: &[crate::config::Account]) -> Result<String> {
let cfg = std::fs::read_to_string(clone.join(".git/config")).with_context(|| format!("read {}/.git/config", clone.display()))?;
crate::repos::remote_urls(&cfg)
.into_iter()
.find(|u| crate::repos::parse_remote(u, accounts).as_ref() == Some(repo))
.with_context(|| format!("no remote for {repo} in {}", clone.display()))
}
pub fn same_scheme(clone_url: &str, ssh: &str, https: &str) -> String {
let https_like = clone_url.starts_with("http");
if (https_like && !https.is_empty()) || ssh.is_empty() { https.into() } else { ssh.into() }
}
pub fn free_gb(p: &Path) -> Result<u64> {
let mut probe = PathBuf::from(p);
while !probe.exists() {
if !probe.pop() {
break;
}
}
let out = std::process::Command::new("df").arg("-Pk").arg(&probe).output().context("run df")?;
let s = String::from_utf8_lossy(&out.stdout);
let avail_kb: u64 =
s.lines().nth(1).and_then(|l| l.split_whitespace().nth(3)).and_then(|v| v.parse().ok()).context("parse df output")?;
Ok(avail_kb / 1024 / 1024)
}
#[cfg(test)]
pub(crate) fn tests_head(dir: &Path) -> String {
let o = std::process::Command::new("git").args(["rev-parse", "HEAD"]).current_dir(dir).output().unwrap();
String::from_utf8_lossy(&o.stdout).trim().to_string()
}
#[cfg(test)]
pub(crate) mod tests {
use super::*;
pub(crate) fn repo(dir: &Path) {
let run = |args: &[&str]| {
let ok = std::process::Command::new("git")
.args(["-c", "user.name=t", "-c", "user.email=t@t", "-c", "commit.gpgsign=false", "-c", "init.defaultBranch=main"])
.args(args)
.current_dir(dir)
.output()
.unwrap();
assert!(ok.status.success(), "git {args:?}: {}", String::from_utf8_lossy(&ok.stderr));
};
run(&["init", "-q"]);
std::fs::write(dir.join("a.txt"), "a\n").unwrap();
run(&["add", "a.txt"]);
run(&["commit", "-qm", "base"]);
}
#[tokio::test]
async fn tampered_gitfile_worktree_is_recreated() {
let d = tempfile::tempdir().unwrap();
let clone = d.path().join("clone");
std::fs::create_dir(&clone).unwrap();
repo(&clone);
let url = clone.to_string_lossy().to_string();
let wt = d.path().join("wt");
let (_, gitfile) = prepare_worktree(&clone, &wt, &url, "refs/heads/main", "nightporter/pr-1").await.unwrap();
assert!(gitfile_points_into(&clone, &wt).await);
let evil = wt.join("evil-git");
std::fs::create_dir_all(&evil).unwrap();
std::fs::write(wt.join(".git"), format!("gitdir: {}\n", evil.display())).unwrap();
assert!(!gitfile_points_into(&clone, &wt).await);
assert!(verify_gitfile(&wt, &gitfile).is_err());
prepare_worktree(&clone, &wt, &url, "refs/heads/main", "nightporter/pr-1").await.unwrap();
assert!(gitfile_points_into(&clone, &wt).await);
assert!(!evil.exists());
}
#[test]
fn redacts() {
assert_eq!(redact("push https://x-token:ghp_abc@github.com/o/r"), "push https://***@github.com/o/r");
assert_eq!(redact("git@github.com:o/r"), "git@github.com:o/r");
}
#[test]
fn schemes() {
assert_eq!(same_scheme("git@github.com:o/r", "git@x:f/r", "https://x/f/r"), "git@x:f/r");
assert_eq!(same_scheme("https://github.com/o/r", "git@x:f/r", "https://x/f/r"), "https://x/f/r");
}
#[test]
fn df_works() {
assert!(free_gb(Path::new("/nonexistent/deep/path")).is_ok());
}
}