use std::collections::HashSet;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::{Duration, SystemTime};
use crate::deny::{DenyPolicy, path_is_denied_glob};
#[derive(Debug, Clone)]
pub struct GcConfig {
pub leftover_dir: PathBuf,
pub saved_ref_prefix: String,
pub max_age: Duration,
pub dry_run: bool,
pub now: SystemTime,
}
impl GcConfig {
pub fn new(cwd: &Path, max_age: Duration) -> Self {
Self {
leftover_dir: cwd.join(".workpen-worktrees"),
saved_ref_prefix: "refs/workpen/reclaimed".into(),
max_age,
dry_run: false,
now: SystemTime::now(),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum KeepReason {
DirtyWork,
UniqueUntracked,
TooNew,
UntrackedWorktree,
NotAGitDir,
StatusUnreadable,
LiveCwd,
Locked,
}
impl KeepReason {
pub fn as_str(&self) -> &'static str {
match self {
Self::DirtyWork => "unique uncommitted work",
Self::UniqueUntracked => "unique untracked files",
Self::TooNew => "newer than --max-age",
Self::UntrackedWorktree => "untracked leftover (not a registered worktree)",
Self::NotAGitDir => "not a git worktree",
Self::StatusUnreadable => "git status unreadable",
Self::LiveCwd => "live process cwd",
Self::Locked => "locked",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum GcDecision {
Keep { reason: KeepReason },
Reclaim { saved_refs: Vec<String> },
}
#[derive(Debug, thiserror::Error)]
pub enum GcError {
#[error("invalid duration: {0}")]
InvalidDuration(String),
#[error("worktree registry unreadable: {0}")]
RegistryUnreadable(String),
#[error("{0} is not a git repository; gc reclaims leftover worktrees of a git repo")]
NotARepo(String),
#[error("git {op} failed: {detail}")]
Git { op: String, detail: String },
#[error("refuse to treat home as a git workspace: {0}")]
Home(String),
}
pub fn parse_max_age(raw: &str) -> Result<Duration, GcError> {
let s = raw.trim();
if s.chars().count() < 2 {
return Err(GcError::InvalidDuration(format!(
"{s} (use e.g. 30s, 60m, 24h, 7d)"
)));
}
let unit_start = s.char_indices().next_back().map(|(i, _)| i).unwrap_or(0);
let (num, unit) = s.split_at(unit_start);
let n: u64 = num
.trim()
.parse()
.map_err(|_| GcError::InvalidDuration(format!("{s} (use s, m, h, d)")))?;
if n == 0 {
return Err(GcError::InvalidDuration(format!(
"duration must be greater than zero: {s}"
)));
}
let dur = match unit.to_ascii_lowercase().as_str() {
"s" => Duration::from_secs(n),
"m" => Duration::from_secs(n.saturating_mul(60)),
"h" => Duration::from_secs(n.saturating_mul(3600)),
"d" => Duration::from_secs(n.saturating_mul(86400)),
_ => {
return Err(GcError::InvalidDuration(format!("{s} (use s, m, h, d)")));
}
};
if std::time::Instant::now().checked_add(dur).is_none() {
return Err(GcError::InvalidDuration(format!(
"duration does not fit on the clock: {s}"
)));
}
Ok(dur)
}
pub fn classify_for_age_gc(
path: &Path,
untracked: bool,
max_age: Duration,
now: SystemTime,
) -> GcDecision {
classify_for_age_gc_with_policy(path, untracked, max_age, now, &DenyPolicy::default())
}
pub fn classify_for_age_gc_with_policy(
path: &Path,
untracked: bool,
max_age: Duration,
now: SystemTime,
policy: &DenyPolicy,
) -> GcDecision {
if untracked {
return GcDecision::Keep {
reason: KeepReason::UntrackedWorktree,
};
}
if newest_tree_mtime(path).is_err() {
return GcDecision::Keep {
reason: KeepReason::StatusUnreadable,
};
}
let last_used = worktree_last_used(path).unwrap_or(now);
if now.duration_since(last_used).unwrap_or_default() < max_age {
return GcDecision::Keep {
reason: KeepReason::TooNew,
};
}
classify_worktree_with_policy(path, false, policy)
}
pub fn remove_explicit(
path: &Path,
saved_ref_prefix: &str,
force: bool,
) -> Result<GcDecision, GcError> {
remove_explicit_with_policy(path, saved_ref_prefix, force, &DenyPolicy::default())
}
pub fn remove_explicit_with_policy(
path: &Path,
saved_ref_prefix: &str,
force: bool,
policy: &DenyPolicy,
) -> Result<GcDecision, GcError> {
if !path.is_dir() || !is_git_repo(path) {
return Ok(GcDecision::Keep {
reason: KeepReason::NotAGitDir,
});
}
let common = git_common_dir(path).map_err(registry_unreadable)?;
let repo = repo_cwd_from_common_dir(&common);
let registered = registered_worktrees(&repo)?;
let entry = registered.iter().find(|w| paths_eq(&w.path, path));
let locked = entry.is_some_and(|w| w.locked);
if locked && !force {
return Ok(GcDecision::Keep {
reason: KeepReason::Locked,
});
}
if worktree_has_live_cwd(path) {
return Ok(GcDecision::Keep {
reason: KeepReason::LiveCwd,
});
}
match classify_worktree_with_policy(path, false, policy) {
keep @ GcDecision::Keep { .. } => Ok(keep),
GcDecision::Reclaim { .. } => {
let saved = match save_unique_commits(path, saved_ref_prefix) {
Ok(saved) => saved,
Err(_) => {
return Ok(GcDecision::Keep {
reason: KeepReason::StatusUnreadable,
});
}
};
if entry.is_some() {
remove_worktree(&repo, path, locked && force)?;
} else {
std::fs::remove_dir_all(path).map_err(|e| GcError::Git {
op: "leftover rm".into(),
detail: e.to_string(),
})?;
}
Ok(GcDecision::Reclaim { saved_refs: saved })
}
}
}
pub fn classify_worktree(path: &Path, untracked: bool) -> GcDecision {
classify_worktree_with_policy(path, untracked, &DenyPolicy::default())
}
pub fn classify_worktree_with_policy(
path: &Path,
untracked: bool,
policy: &DenyPolicy,
) -> GcDecision {
if untracked {
return GcDecision::Keep {
reason: KeepReason::UntrackedWorktree,
};
}
if !path.is_dir() {
return GcDecision::Keep {
reason: KeepReason::NotAGitDir,
};
}
if !is_git_repo(path) {
return GcDecision::Keep {
reason: KeepReason::NotAGitDir,
};
}
match unique_work_reason(path, policy) {
Some(reason) => GcDecision::Keep { reason },
None => GcDecision::Reclaim {
saved_refs: Vec::new(),
},
}
}
pub fn run_gc(cwd: &Path, cfg: &GcConfig) -> Result<Vec<(PathBuf, GcDecision)>, GcError> {
run_gc_with_policy(cwd, cfg, &DenyPolicy::default())
}
pub fn run_gc_with_policy(
cwd: &Path,
cfg: &GcConfig,
policy: &DenyPolicy,
) -> Result<Vec<(PathBuf, GcDecision)>, GcError> {
refuse_home(cwd)?;
let registered = registered_worktrees(cwd)?;
let registered_paths: Vec<PathBuf> = registered.iter().map(|w| w.path.clone()).collect();
let primary = registered
.first()
.map(|w| w.path.clone())
.or_else(|| repo_root(cwd).ok());
let mut rows = Vec::new();
for wt in ®istered {
if primary.as_ref().is_some_and(|p| paths_eq(p, &wt.path)) {
continue;
}
if wt.locked {
rows.push((
wt.path.clone(),
GcDecision::Keep {
reason: KeepReason::Locked,
},
));
continue;
}
if worktree_has_live_cwd(&wt.path) {
rows.push((
wt.path.clone(),
GcDecision::Keep {
reason: KeepReason::LiveCwd,
},
));
continue;
}
let mut decision =
classify_for_age_gc_with_policy(&wt.path, false, cfg.max_age, cfg.now, policy);
if let GcDecision::Reclaim { .. } = &decision
&& !cfg.dry_run
{
match save_unique_commits(&wt.path, &cfg.saved_ref_prefix) {
Ok(saved) => {
remove_worktree(cwd, &wt.path, false)?;
decision = GcDecision::Reclaim { saved_refs: saved };
}
Err(_) => {
decision = GcDecision::Keep {
reason: KeepReason::StatusUnreadable,
};
}
}
}
rows.push((wt.path.clone(), decision));
}
for path in untracked_leftovers(&cfg.leftover_dir, ®istered_paths) {
rows.push((
path,
GcDecision::Keep {
reason: KeepReason::UntrackedWorktree,
},
));
}
if !cfg.dry_run {
git(cwd, &["worktree", "prune"]).map_err(|e| match e {
GcError::Git { detail, .. } => GcError::Git {
op: "worktree prune".into(),
detail,
},
other => other,
})?;
}
Ok(rows)
}
const CACHE_DIR_NAMES: &[&str] = &["target", "node_modules", ".venv", "dist", "__pycache__"];
const LAST_USED_WALK_LIMIT: usize = 4096;
struct Registered {
path: PathBuf,
locked: bool,
}
fn refuse_home(repo: &Path) -> Result<(), GcError> {
let top = match git(repo, &["rev-parse", "--show-toplevel"]) {
Ok(s) => PathBuf::from(s.trim()),
Err(_) => return Ok(()),
};
if is_home(&top) {
return Err(GcError::Home(top.display().to_string()));
}
Ok(())
}
fn is_home(path: &Path) -> bool {
crate::guard::is_user_home_dir(path)
}
fn repo_root(cwd: &Path) -> Result<PathBuf, GcError> {
let top = git(cwd, &["rev-parse", "--show-toplevel"])?;
Ok(PathBuf::from(top.trim()))
}
fn is_git_repo(cwd: &Path) -> bool {
if !cwd.join(".git").exists() {
return false;
}
git(cwd, &["rev-parse", "--is-inside-work-tree"])
.map(|s| s.trim() == "true")
.unwrap_or(false)
}
fn registered_worktrees(cwd: &Path) -> Result<Vec<Registered>, GcError> {
let out = git(cwd, &["worktree", "list", "--porcelain"]).map_err(|e| match e {
GcError::Git { detail, .. } if detail.contains("not a git repository") => {
GcError::NotARepo(cwd.display().to_string())
}
GcError::Git { detail, .. } => GcError::RegistryUnreadable(detail),
other => other,
})?;
Ok(parse_worktree_list(&out))
}
fn parse_worktree_list(text: &str) -> Vec<Registered> {
let mut trees = Vec::new();
let mut path: Option<PathBuf> = None;
let mut locked = false;
let flush = |trees: &mut Vec<Registered>, path: &mut Option<PathBuf>, locked: &mut bool| {
if let Some(p) = path.take() {
trees.push(Registered {
path: p,
locked: *locked,
});
*locked = false;
}
};
for line in text.lines() {
if let Some(rest) = line.strip_prefix("worktree ") {
flush(&mut trees, &mut path, &mut locked);
path = Some(PathBuf::from(rest));
} else if line == "locked" || line.starts_with("locked ") {
locked = true;
}
}
flush(&mut trees, &mut path, &mut locked);
trees
}
fn untracked_leftovers(leftover_dir: &Path, registered: &[PathBuf]) -> Vec<PathBuf> {
let Ok(rd) = std::fs::read_dir(leftover_dir) else {
return Vec::new();
};
let mut out = Vec::new();
for entry in rd.flatten() {
let p = entry.path();
if !p.is_dir() {
continue;
}
if registered.iter().any(|r| paths_eq(r, &p)) {
continue;
}
out.push(p);
}
out
}
fn paths_eq(a: &Path, b: &Path) -> bool {
if a == b {
return true;
}
match (std::fs::canonicalize(a), std::fs::canonicalize(b)) {
(Ok(ca), Ok(cb)) => ca == cb,
_ => a == b,
}
}
fn path_is_same_or_parent(root: &Path, inner: &Path) -> bool {
if paths_eq(root, inner) {
return true;
}
match (std::fs::canonicalize(root), std::fs::canonicalize(inner)) {
(Ok(r), Ok(i)) => i.starts_with(&r),
_ => inner.starts_with(root),
}
}
fn worktree_has_live_cwd(path: &Path) -> bool {
let current = std::env::current_dir().ok();
let other = other_process_cwds();
worktree_has_live_cwd_from(
path,
current.as_deref(),
match &other {
Ok(cwds) => Ok(cwds.as_slice()),
Err(()) => Err(()),
},
)
}
fn worktree_has_live_cwd_from(
path: &Path,
current: Option<&Path>,
other: Result<&[PathBuf], ()>,
) -> bool {
if let Some(cwd) = current
&& path_is_same_or_parent(path, cwd)
{
return true;
}
match other {
Err(()) => true,
Ok(cwds) => cwds.iter().any(|cwd| path_is_same_or_parent(path, cwd)),
}
}
fn other_process_cwds() -> Result<Vec<PathBuf>, ()> {
#[cfg(target_os = "linux")]
{
if let Ok(cwds) = linux_process_cwds() {
return Ok(cwds);
}
}
#[cfg(unix)]
{
lsof_process_cwds()
}
#[cfg(not(unix))]
{
Ok(Vec::new())
}
}
#[cfg(target_os = "linux")]
fn linux_process_cwds() -> Result<Vec<PathBuf>, ()> {
let rd = std::fs::read_dir("/proc").map_err(|_| ())?;
let mut out = Vec::new();
for entry in rd.flatten() {
let name = entry.file_name();
if !name
.to_str()
.is_some_and(|s| !s.is_empty() && s.bytes().all(|b| b.is_ascii_digit()))
{
continue;
}
let cwd_link = entry.path().join("cwd");
if let Ok(cwd) = std::fs::read_link(cwd_link) {
out.push(cwd);
}
}
Ok(out)
}
#[cfg(unix)]
fn lsof_process_cwds() -> Result<Vec<PathBuf>, ()> {
let out = Command::new("lsof")
.args(["-a", "-d", "cwd", "-Fn"])
.output()
.map_err(|_| ())?;
let paths: Vec<PathBuf> = String::from_utf8_lossy(&out.stdout)
.lines()
.filter_map(|line| line.strip_prefix('n'))
.filter(|p| !p.is_empty())
.map(PathBuf::from)
.collect();
if !out.status.success() && paths.is_empty() {
return Err(());
}
Ok(paths)
}
pub fn worktree_last_used(path: &Path) -> Option<SystemTime> {
let mut latest = head_commit_time(path);
if let Some(t) = index_mtime(path) {
latest = Some(latest.map_or(t, |n| n.max(t)));
}
if let Ok(Some(t)) = newest_tree_mtime(path) {
latest = Some(latest.map_or(t, |n| n.max(t)));
}
latest
}
fn head_commit_time(path: &Path) -> Option<SystemTime> {
let out = git(path, &["log", "-1", "--format=%ct"]).ok()?;
let secs: u64 = out.trim().parse().ok()?;
Some(SystemTime::UNIX_EPOCH + Duration::from_secs(secs))
}
fn index_file(path: &Path) -> Option<PathBuf> {
let raw = git(path, &["rev-parse", "--git-path", "index"]).ok()?;
let index = PathBuf::from(raw.trim());
Some(if index.is_absolute() {
index
} else {
path.join(index)
})
}
fn index_mtime(path: &Path) -> Option<SystemTime> {
std::fs::metadata(index_file(path)?)
.and_then(|m| m.modified())
.ok()
}
fn restore_index_mtime(path: &Path, prev: SystemTime) {
let Some(index) = index_file(path) else {
return;
};
if let Ok(file) = std::fs::OpenOptions::new().write(true).open(index) {
let _ = file.set_modified(prev);
}
}
fn newest_tree_mtime(root: &Path) -> Result<Option<SystemTime>, ()> {
let mut newest: Option<SystemTime> = None;
let mut remaining = LAST_USED_WALK_LIMIT;
let mut queue = std::collections::VecDeque::new();
queue.push_back(root.to_path_buf());
while remaining > 0 {
let Some(dir) = queue.pop_front() else {
return Ok(newest);
};
let Ok(rd) = std::fs::read_dir(&dir) else {
return Err(());
};
for entry in rd.flatten() {
if remaining == 0 {
return Err(());
}
remaining -= 1;
let p = entry.path();
let name = entry.file_name();
let name = name.to_string_lossy();
if name == ".git" || is_age_cache_dir_name(&name) {
continue;
}
let Ok(meta) = entry.metadata() else {
continue;
};
if meta.is_dir() {
queue.push_back(p);
continue;
}
if let Ok(t) = meta.modified() {
newest = Some(newest.map_or(t, |n| n.max(t)));
}
}
}
if !queue.is_empty() {
return Err(());
}
Ok(newest)
}
fn is_age_cache_dir_name(name: &str) -> bool {
CACHE_DIR_NAMES.iter().any(|n| name.eq_ignore_ascii_case(n))
}
fn cache_tree_has_denied_name(root: &Path, rel: &Path, policy: &DenyPolicy) -> bool {
if cache_path_denied_glob(policy, rel) {
return true;
}
let Some(std::path::Component::Normal(name)) = rel.components().next() else {
return false;
};
let start = root.join(name);
let mut remaining = LAST_USED_WALK_LIMIT;
let mut queue = std::collections::VecDeque::new();
queue.push_back(start);
while remaining > 0 {
let Some(dir) = queue.pop_front() else {
return false;
};
let rd = match std::fs::read_dir(&dir) {
Ok(rd) => rd,
Err(_) => return true,
};
for entry in rd.flatten() {
if remaining == 0 {
return true;
}
remaining -= 1;
let p = entry.path();
if cache_path_denied_glob(policy, &p) {
return true;
}
let is_real_dir = match entry.file_type() {
Ok(ft) => ft.is_dir(),
Err(_) => match std::fs::symlink_metadata(&p) {
Ok(meta) => meta.file_type().is_dir(),
Err(_) => return true,
},
};
if is_real_dir {
queue.push_back(p);
}
}
}
!queue.is_empty()
}
fn cache_path_denied_glob(policy: &DenyPolicy, path: &Path) -> bool {
let full = path.to_string_lossy().replace('\\', "/");
if path_is_denied_glob(policy.globs(), &full) {
return true;
}
path.file_name()
.is_some_and(|base| path_is_denied_glob(policy.globs(), &base.to_string_lossy()))
}
fn is_under_known_cache(root: &Path, file: &Path) -> bool {
let Ok(rel) = file.strip_prefix(root) else {
return false;
};
let Some(std::path::Component::Normal(name)) = rel.components().next() else {
return false;
};
is_age_cache_dir_name(&name.to_string_lossy())
}
fn unique_work_reason(path: &Path, policy: &DenyPolicy) -> Option<KeepReason> {
let index_before = index_mtime(path);
let out = match git(
path,
&[
"-c",
"status.showUntrackedFiles=all",
"status",
"--porcelain=v1",
"-uall",
"--ignored",
],
) {
Ok(s) => s,
Err(_) => return Some(KeepReason::StatusUnreadable),
};
if let Some(prev) = index_before {
restore_index_mtime(path, prev);
}
let mut has_unique = false;
for line in out.lines() {
if line.is_empty() {
continue;
}
let rel = porcelain_path(line);
if line.starts_with("??") || line.starts_with("!!") {
if is_under_known_cache(path, &path.join(&rel)) {
if cache_tree_has_denied_name(path, &rel, policy) {
has_unique = true;
}
continue;
}
has_unique = true;
} else {
return Some(KeepReason::DirtyWork);
}
}
if !has_unique {
for name in CACHE_DIR_NAMES {
if !path.join(name).is_dir() {
continue;
}
if cache_tree_has_denied_name(path, Path::new(name), policy) {
has_unique = true;
break;
}
}
}
if has_unique {
Some(KeepReason::UniqueUntracked)
} else {
None
}
}
fn porcelain_path(line: &str) -> PathBuf {
let rest = if line.len() >= 3 { &line[3..] } else { line };
let rest = rest.trim();
if let Some((_, dest)) = rest.split_once(" -> ") {
PathBuf::from(dest.trim_matches('"'))
} else {
PathBuf::from(rest.trim_matches('"'))
}
}
fn save_unique_commits(path: &Path, prefix: &str) -> Result<Vec<String>, GcError> {
let reflog = git(path, &["reflog", "--format=%H"]).map_err(|e| match e {
GcError::Git { detail, .. } => GcError::Git {
op: "reflog".into(),
detail,
},
other => other,
})?;
let name = path
.file_name()
.map(|s| s.to_string_lossy().into_owned())
.unwrap_or_else(|| "worktree".into());
let name = sanitize_ref_component(&name);
let prefix = prefix.trim_end_matches('/');
let mut saved = Vec::new();
let mut seen = HashSet::new();
for sha in reflog.lines() {
let sha = sha.trim();
if sha.len() < 7 || !seen.insert(sha.to_owned()) {
continue;
}
if !commit_is_dangling(path, sha)? {
continue;
}
let refname = format!("{prefix}/{name}/{sha}");
git(path, &["update-ref", &refname, sha]).map_err(|e| match e {
GcError::Git { detail, .. } => GcError::Git {
op: "update-ref".into(),
detail,
},
other => other,
})?;
saved.push(refname);
}
Ok(saved)
}
fn commit_is_dangling(path: &Path, sha: &str) -> Result<bool, GcError> {
let contains = git(path, &["branch", "-a", "--contains", sha])?;
Ok(!contains.lines().any(|l| !l.trim().is_empty()))
}
fn sanitize_ref_component(raw: &str) -> String {
let s: String = raw
.chars()
.map(|c| {
if c.is_ascii_alphanumeric() || c == '-' || c == '_' {
c
} else {
'-'
}
})
.collect();
let s = s.trim_matches('-').to_owned();
if s.is_empty() { "worktree".into() } else { s }
}
fn git_common_dir(path: &Path) -> Result<PathBuf, GcError> {
let raw = git(path, &["rev-parse", "--git-common-dir"])?;
let p = PathBuf::from(raw.trim());
if p.is_absolute() {
Ok(p)
} else {
Ok(path.join(p))
}
}
fn repo_cwd_from_common_dir(common: &Path) -> PathBuf {
match common.file_name() {
Some(name) if name == ".git" => common.parent().unwrap_or(common).to_path_buf(),
_ => common.to_path_buf(),
}
}
fn registry_unreadable(err: GcError) -> GcError {
match err {
GcError::Git { detail, .. } => GcError::RegistryUnreadable(detail),
other => other,
}
}
fn remove_worktree(repo: &Path, tree: &Path, unlock: bool) -> Result<(), GcError> {
let path_s = tree
.to_str()
.ok_or_else(|| GcError::Git {
op: "worktree remove".into(),
detail: "non-utf8 path".into(),
})?
.to_owned();
let args: Vec<&str> = if unlock {
vec!["worktree", "remove", "--force", "--force", &path_s]
} else {
vec!["worktree", "remove", "--force", &path_s]
};
match git(repo, &args) {
Ok(_) => Ok(()),
Err(_) => {
git(tree, &args)?;
Ok(())
}
}
}
const GIT_FORWARD_ENV: &[&str] = &[
"PATH",
"HOME",
"USER",
"LOGNAME",
"LANG",
"LC_ALL",
"LC_CTYPE",
"TZ",
"TMPDIR",
"TEMP",
"TMP",
"SYSTEMROOT",
"SYSTEMDRIVE",
"WINDIR",
"USERPROFILE",
"COMSPEC",
"PATHEXT",
"HOMEDRIVE",
"HOMEPATH",
];
fn git(cwd: &Path, args: &[&str]) -> Result<String, GcError> {
let mut cmd = Command::new("git");
cmd.env_clear();
for key in GIT_FORWARD_ENV {
if let Some(v) = std::env::var_os(key) {
cmd.env(key, v);
}
}
let out = cmd
.env("GIT_TERMINAL_PROMPT", "0")
.env("GIT_CONFIG_NOSYSTEM", "1")
.args(args)
.current_dir(cwd)
.output()
.map_err(|e| GcError::Git {
op: args.join(" "),
detail: e.to_string(),
})?;
if !out.status.success() {
return Err(GcError::Git {
op: args.join(" "),
detail: String::from_utf8_lossy(&out.stderr).trim().into(),
});
}
Ok(String::from_utf8_lossy(&out.stdout).into_owned())
}
#[cfg(test)]
mod parse_tests {
use super::*;
#[test]
fn parse_primary_and_locked() {
let text = "worktree /repo\nHEAD abc\nbranch refs/heads/main\n\nworktree /repo/.wt\nHEAD def\nlocked\n";
let trees = parse_worktree_list(text);
assert_eq!(trees.len(), 2);
assert!(!trees[0].locked);
assert!(trees[1].locked);
}
#[test]
fn parse_max_age_rejects_a_duration_the_clock_cannot_hold() {
match parse_max_age("999999999999999d") {
Err(GcError::InvalidDuration(msg)) => {
assert!(
msg.contains("does not fit on the clock"),
"huge duration must name the clock: {msg}"
);
}
other => panic!("huge duration must stay InvalidDuration, got {other:?}"),
}
}
#[test]
fn parse_max_age_rejects_bare_number() {
match parse_max_age("7") {
Err(GcError::InvalidDuration(msg)) => {
assert!(msg.contains("30s"), "bare number must hint 30s: {msg}");
}
other => panic!("bare number must stay InvalidDuration, got {other:?}"),
}
}
#[test]
fn commit_is_dangling_git_failure_is_not_dangling() {
let dir = tempfile::tempdir().expect("tmp");
match commit_is_dangling(dir.path(), "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") {
Err(GcError::Git { op, .. }) => {
assert!(
op.contains("branch"),
"a failed contains check must stay a git error: {op}"
);
}
other => {
panic!("git failure must be Err so the sha is not skipped as reachable: {other:?}")
}
}
}
#[test]
fn probe_failure_is_live_when_current_dir_is_elsewhere() {
let tree = tempfile::tempdir().expect("tree");
let elsewhere = tempfile::tempdir().expect("elsewhere");
assert!(
worktree_has_live_cwd_from(tree.path(), Some(elsewhere.path()), Err(())),
"probe failure must keep the tree"
);
assert!(
!worktree_has_live_cwd_from(tree.path(), Some(elsewhere.path()), Ok(&[])),
"successful empty scan is not live"
);
}
#[test]
fn git_forward_env_has_no_git_keys() {
for key in GIT_FORWARD_ENV {
assert!(
!key.starts_with("GIT_"),
"forward list must not include {key}"
);
}
assert!(GIT_FORWARD_ENV.contains(&"PATH"));
assert!(GIT_FORWARD_ENV.contains(&"HOME") || GIT_FORWARD_ENV.contains(&"USERPROFILE"));
}
}