use std::path::Path;
use anyhow::{bail, Context, Result};
pub(super) fn git_companion_mount(base: &Path) -> String {
let git = base.join(".git");
format!("{}:{}", git.display(), git.display())
}
pub(super) fn create_worktree(
base: &Path,
worktree: &Path,
branch: &str,
base_ref: Option<&str>,
) -> Result<()> {
if let Some(parent) = worktree.parent() {
std::fs::create_dir_all(parent)
.with_context(|| format!("cannot create {}", parent.display()))?;
}
let start = worktree_start_point(base, base_ref)?;
let head = std::process::Command::new("git")
.args([
"-C",
&base.to_string_lossy(),
"rev-parse",
"--verify",
&format!("{start}^{{commit}}"),
])
.output()
.context("failed to run git (is it installed?)")?;
if !head.status.success() {
if let Some(r) = base_ref {
bail!("worktree base `{r}` is not a commit in `{}`", base.display());
}
bail!(
"base repo `{}` has no commits; cannot create a worktree",
base.display()
);
}
let exists = std::process::Command::new("git")
.args([
"-C",
&base.to_string_lossy(),
"show-ref",
"--verify",
"--quiet",
&format!("refs/heads/{branch}"),
])
.status()
.context("failed to run git (is it installed?)")?;
if exists.success() {
bail!(
"branch `{branch}` already exists; a worktree needs a unique branch \
(include `${{instance}}` in `worktree-branch` or pass a distinct `--branch`)"
);
}
let status = std::process::Command::new("git")
.args([
"-C",
&base.to_string_lossy(),
"worktree",
"add",
&worktree.to_string_lossy(),
"-b",
branch,
"--no-track",
&start,
])
.status()
.context("failed to run git (is it installed?)")?;
if !status.success() {
bail!("git worktree add failed for `{}`", worktree.display());
}
if !worktree.join(".git").exists() {
bail!(
"git worktree add produced an empty worktree at `{}`",
worktree.display()
);
}
Ok(())
}
fn git_query(base: &Path, args: &[&str]) -> Option<String> {
let out = std::process::Command::new("git")
.arg("-C")
.arg(base)
.args(args)
.output()
.ok()?;
out.status
.success()
.then(|| String::from_utf8_lossy(&out.stdout).trim().to_string())
}
const FETCH_FRESH_FOR: std::time::Duration = std::time::Duration::from_secs(120);
fn fetch_is_fresh(modified: std::time::SystemTime, now: std::time::SystemTime) -> bool {
now.duration_since(modified).map_or(true, |age| age < FETCH_FRESH_FOR)
}
fn fetch_origin(base: &Path) -> Result<()> {
let fresh = git_query(base, &["rev-parse", "--git-path", "FETCH_HEAD"])
.and_then(|p| std::fs::metadata(base.join(p)).ok())
.and_then(|m| m.modified().ok())
.is_some_and(|t| fetch_is_fresh(t, std::time::SystemTime::now()));
if fresh {
return Ok(());
}
let fetch = std::process::Command::new("git")
.arg("-C")
.arg(base)
.args(["fetch", "--quiet", "origin"])
.output()
.context("failed to run git (is it installed?)")?;
if !fetch.status.success() {
eprintln!(
"warning: `git fetch origin` failed in `{}`; worktree may start from a stale ref: {}",
base.display(),
String::from_utf8_lossy(&fetch.stderr).trim()
);
}
Ok(())
}
fn worktree_start_point(base: &Path, explicit: Option<&str>) -> Result<String> {
let fallback = || explicit.unwrap_or("HEAD").to_string();
if git_query(base, &["remote", "get-url", "origin"]).is_none() {
return Ok(fallback());
}
fetch_origin(base)?;
if let Some(r) = explicit {
return Ok(r.to_string());
}
if let Some(r) = git_query(base, &["symbolic-ref", "--quiet", "--short", "refs/remotes/origin/HEAD"]) {
return Ok(r);
}
for r in ["origin/main", "origin/master"] {
let full = format!("refs/remotes/{r}");
if git_query(base, &["show-ref", "--verify", "--quiet", &full]).is_some() {
return Ok(r.to_string());
}
}
Ok("HEAD".to_string())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn git_companion_mount_uses_identical_paths() {
let mount = git_companion_mount(Path::new("/home/u/repo"));
assert_eq!(mount, "/home/u/repo/.git:/home/u/repo/.git");
}
fn git(dir: &Path, args: &[&str]) -> String {
let out = std::process::Command::new("git")
.arg("-C")
.arg(dir)
.args(["-c", "user.name=t", "-c", "user.email=t@t", "-c", "commit.gpgsign=false"])
.args(args)
.output()
.unwrap();
assert!(out.status.success(), "git {args:?}: {}", String::from_utf8_lossy(&out.stderr));
String::from_utf8_lossy(&out.stdout).trim().to_string()
}
#[test]
fn fetch_freshness_window() {
use std::time::{Duration, SystemTime};
let now = SystemTime::now();
assert!(fetch_is_fresh(now - Duration::from_secs(60), now));
assert!(!fetch_is_fresh(now - Duration::from_secs(120), now));
assert!(!fetch_is_fresh(now - Duration::from_secs(3600), now));
assert!(fetch_is_fresh(now + Duration::from_secs(5), now));
}
#[test]
fn worktree_starts_from_fetched_origin_default_untracked() {
let root = std::env::temp_dir().join(format!("devsandbox-wt-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&root);
let (origin, base, wt) = (root.join("origin"), root.join("base"), root.join("wt"));
std::fs::create_dir_all(&origin).unwrap();
git(&origin, &["init", "-q", "-b", "main"]);
git(&origin, &["commit", "-q", "--allow-empty", "-m", "one"]);
git(&root, &["clone", "-q", &origin.to_string_lossy(), "base"]);
git(&base, &["checkout", "-q", "-b", "feature"]);
git(&base, &["commit", "-q", "--allow-empty", "-m", "wip"]);
let feature_tip = git(&base, &["rev-parse", "HEAD"]);
git(&origin, &["commit", "-q", "--allow-empty", "-m", "two"]);
let main_tip = git(&origin, &["rev-parse", "HEAD"]);
create_worktree(&base, &wt, "sandbox/x", None).unwrap();
assert_eq!(git(&wt, &["rev-parse", "HEAD"]), main_tip);
let upstream = std::process::Command::new("git")
.arg("-C")
.arg(&wt)
.args(["rev-parse", "--abbrev-ref", "@{upstream}"])
.output()
.unwrap();
assert!(!upstream.status.success(), "branch must not track origin");
assert_eq!(git(&base, &["rev-parse", "HEAD"]), feature_tip);
assert_eq!(git(&base, &["branch", "--show-current"]), "feature");
let wt2 = root.join("wt2");
create_worktree(&base, &wt2, "sandbox/y", Some("feature")).unwrap();
assert_eq!(git(&wt2, &["rev-parse", "HEAD"]), feature_tip);
let err = create_worktree(&base, &root.join("wt3"), "sandbox/z", Some("nope"))
.unwrap_err()
.to_string();
assert!(err.contains("worktree base `nope`"), "{err}");
std::fs::remove_dir_all(&root).unwrap();
}
}