use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::atomic::{AtomicU64, Ordering};
pub const PREFIX: &str = "agent86-wt-";
const KEPT_SUFFIX: &str = ".kept";
pub const MAX_DIFF_CHARS: usize = 60_000;
static COUNTER: AtomicU64 = AtomicU64::new(0);
#[derive(Debug)]
pub struct Worktree {
pub path: PathBuf,
pub root: PathBuf,
pub branch: String,
pub base: String,
pub dirty: bool,
common: PathBuf,
git_dir: PathBuf,
}
fn empty_hooks() -> Result<PathBuf, String> {
let dir = std::env::temp_dir().join("agent86-wt-hooks-empty");
std::fs::create_dir_all(&dir).map_err(|error| format!("could not prepare git: {error}"))?;
Ok(dir)
}
fn git(hooks: &Path) -> Command {
let mut command = Command::new("git");
command
.arg("-c")
.arg(format!(
"core.hooksPath={}",
crate::tools::plain_path(hooks).display()
))
.args(["-c", "core.fsmonitor=false", "-c", "color.ui=false"])
.env_remove("GIT_DIR")
.env_remove("GIT_WORK_TREE")
.env_remove("GIT_INDEX_FILE")
.env_remove("GIT_COMMON_DIR")
.env_remove("GIT_OBJECT_DIRECTORY")
.env("GIT_TERMINAL_PROMPT", "0");
command
}
fn output(mut command: Command) -> Result<String, String> {
let out = command
.output()
.map_err(|error| format!("git could not be run: {error}"))?;
if out.status.success() {
Ok(String::from_utf8_lossy(&out.stdout).trim_end().to_owned())
} else {
let stderr = String::from_utf8_lossy(&out.stderr);
Err(stderr
.trim()
.lines()
.last()
.unwrap_or("git failed")
.to_owned())
}
}
fn canonical(path: &Path) -> PathBuf {
crate::tools::plain_path(&std::fs::canonicalize(path).unwrap_or_else(|_| path.to_owned()))
}
impl Worktree {
pub fn create(workspace: &Path) -> Result<Self, String> {
let hooks = empty_hooks()?;
let workspace = canonical(workspace);
let in_workspace = |args: &[&str]| {
let mut command = git(&hooks);
command.arg("-C").arg(&workspace).args(args);
output(command)
};
let not_a_repo = |_| {
"Isolation needs a git repository: the workspace is not inside one, so the \
specialty was not run (rather than run in the shared workspace)."
.to_owned()
};
let top = canonical(Path::new(
&in_workspace(&["rev-parse", "--show-toplevel"]).map_err(not_a_repo)?,
));
let common = canonical(Path::new(
&in_workspace(&["rev-parse", "--path-format=absolute", "--git-common-dir"])
.map_err(not_a_repo)?,
));
let base = in_workspace(&["rev-parse", "--verify", "HEAD"]).map_err(|_| {
"Isolation needs a commit to start from, and this repository has none yet.".to_owned()
})?;
let dirty = in_workspace(&["status", "--porcelain"])
.map(|status| !status.is_empty())
.unwrap_or(false);
let nanos = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map_or(0, |elapsed| elapsed.subsec_nanos());
let name = format!(
"{PREFIX}{}-{}-{nanos}",
std::process::id(),
COUNTER.fetch_add(1, Ordering::Relaxed)
);
let path = std::env::temp_dir().join(&name);
let branch = format!("agent86/{name}");
let mut add = git(&hooks);
add.arg("--git-dir")
.arg(&common)
.args(["worktree", "add", "--quiet", "-b", &branch])
.arg(crate::tools::plain_path(&path))
.arg(&base);
output(add).map_err(|error| format!("The worktree could not be made: {error}"))?;
let path = canonical(&path);
let relative = workspace
.strip_prefix(&top)
.map(Path::to_path_buf)
.unwrap_or_default();
let root = path.join(relative);
if !root.is_dir() {
let _ = std::fs::create_dir_all(&root);
}
Ok(Self {
git_dir: common.join("worktrees").join(&name),
path,
root,
branch,
base,
dirty,
common,
})
}
fn command(&self) -> Result<Command, String> {
let mut command = git(&empty_hooks()?);
command
.arg("--git-dir")
.arg(&self.git_dir)
.arg("--work-tree")
.arg(&self.path)
.arg("-C")
.arg(&self.path);
Ok(command)
}
pub fn diff(&self) -> Result<String, String> {
let mut add = self.command()?;
add.args(["add", "--intent-to-add", "--all", "."]);
output(add)?;
let mut diff = self.command()?;
diff.args([
"diff",
"--no-ext-diff",
"--no-textconv",
"--no-color",
&self.base,
]);
output(diff)
}
pub fn remove(&self) {
if let Ok(hooks) = empty_hooks() {
let mut remove = git(&hooks);
remove
.arg("--git-dir")
.arg(&self.common)
.args(["worktree", "remove", "--force"])
.arg(&self.path);
let _ = output(remove);
let mut branch = git(&hooks);
branch
.arg("--git-dir")
.arg(&self.common)
.args(["branch", "-D", &self.branch]);
let _ = output(branch);
}
let _ = std::fs::remove_dir_all(&self.path);
}
pub fn keep(&self) {
let mut marker = self.path.clone().into_os_string();
marker.push(KEPT_SUFFIX);
let _ = std::fs::write(marker, &self.branch);
}
pub fn finish(&self, redact: impl Fn(&str) -> String) -> String {
let short: String = self.base.chars().take(12).collect();
let mut text = format!(
"\n\n[worktree] This ran in a git worktree of HEAD ({short}), not in your workspace."
);
if self.dirty {
text.push_str(
" The workspace had uncommitted changes, which the worktree did not have.",
);
}
match self.diff() {
Ok(diff) if diff.trim().is_empty() => {
text.push_str(" It changed nothing, and the worktree was removed.");
self.remove();
}
Ok(diff) => {
self.keep();
let diff = redact(&diff);
let total = diff.chars().count();
let shown: String = diff.chars().take(MAX_DIFF_CHARS).collect();
text.push_str(&format!(
" Nothing was applied: the worktree is kept at {} on branch {}. Its changes:\n```diff\n{shown}\n```",
crate::tools::display_path(&self.path),
self.branch
));
if total > MAX_DIFF_CHARS {
text.push_str(&format!(
"\n[diff cut at {MAX_DIFF_CHARS} of {total} characters; the rest is in the worktree]"
));
}
}
Err(error) => {
self.keep();
text.push_str(&format!(
" Its diff could not be made ({error}); the worktree is kept at {} on branch {}.",
crate::tools::display_path(&self.path),
self.branch
));
}
}
text
}
}
fn alive(pid: u32) -> bool {
if pid == std::process::id() {
return true;
}
let result = if cfg!(windows) {
Command::new("tasklist")
.args(["/FI", &format!("PID eq {pid}"), "/FO", "CSV", "/NH"])
.output()
.map(|out| String::from_utf8_lossy(&out.stdout).contains(&format!("\"{pid}\"")))
} else {
Command::new("kill")
.args(["-0", &pid.to_string()])
.output()
.map(|out| out.status.success())
};
result.unwrap_or(true)
}
pub fn sweep_in(dir: &Path) {
let Ok(entries) = std::fs::read_dir(dir) else {
return;
};
for entry in entries.filter_map(Result::ok) {
let name = entry.file_name().to_string_lossy().into_owned();
let Some(rest) = name.strip_prefix(PREFIX) else {
continue;
};
let Some(pid) = rest
.split('-')
.next()
.and_then(|pid| pid.parse::<u32>().ok())
else {
continue;
};
let path = entry.path();
if !path.is_dir() || dir.join(format!("{name}{KEPT_SUFFIX}")).exists() || alive(pid) {
continue;
}
let gitdir = std::fs::read_to_string(path.join(".git"))
.ok()
.and_then(|text| {
text.strip_prefix("gitdir:")
.map(|g| PathBuf::from(g.trim()))
});
if let (Some(gitdir), Ok(hooks)) = (gitdir, empty_hooks()) {
if let Some(common) = gitdir.parent().and_then(Path::parent) {
let mut remove = git(&hooks);
remove
.arg("--git-dir")
.arg(common)
.args(["worktree", "remove", "--force"])
.arg(&path);
let _ = output(remove);
let mut branch = git(&hooks);
branch.arg("--git-dir").arg(common).args([
"branch",
"-D",
&format!("agent86/{name}"),
]);
let _ = output(branch);
}
}
let _ = std::fs::remove_dir_all(&path);
}
}
pub fn sweep() {
sweep_in(&std::env::temp_dir());
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_sweep_spares_live_and_kept_worktrees() {
let dir = std::env::temp_dir().join(format!("agent86-sweep-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let dead = dir.join(format!("{PREFIX}4294967290-0-1"));
let mine = dir.join(format!("{PREFIX}{}-0-1", std::process::id()));
let kept = dir.join(format!("{PREFIX}4294967291-0-1"));
let other = dir.join("unrelated");
for path in [&dead, &mine, &kept, &other] {
std::fs::create_dir_all(path).unwrap();
}
std::fs::write(
dir.join(format!("{PREFIX}4294967291-0-1{KEPT_SUFFIX}")),
"b",
)
.unwrap();
sweep_in(&dir);
assert!(!dead.exists());
assert!(mine.exists() && kept.exists() && other.exists());
let _ = std::fs::remove_dir_all(&dir);
}
}