Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix(prune): wrap multi-line git errors with context to silence debug_assert (#2567)
Maximilian Roos0e20e7bac147
diff --git a/src/commands/step_commands.rs b/src/commands/step_commands.rsindex 4e836b162..acebc537f 100644--- a/src/commands/step_commands.rs+++ b/src/commands/step_commands.rs@@ -1547,7 +1547,7 @@ pub fn step_prune(// Capture once at command entry. Reused for every per-branch// `integration_reason` probe later in this function.- let snapshot = repo.capture_refs()?;+ let snapshot = repo.capture_refs().context("capturing repository refs")?;// Pass the local default branch (e.g. "main") directly — `integration_reason`// ORs over local + upstream internally, so a branch merged into either side@@ -1556,8 +1556,12 @@ pub fn step_prune(.default_branch().context("cannot determine default branch")?;- let worktrees = repo.list_worktrees()?;- let current_root = repo.current_worktree().root()?.to_path_buf();+ let worktrees = repo.list_worktrees().context("listing worktrees")?;+ let current_root = repo+ .current_worktree()+ .root()+ .context("resolving current worktree root")?+ .to_path_buf();let current_root = dunce::canonicalize(¤t_root).unwrap_or(current_root);let now_secs = worktrunk::utils::epoch_now();@@ -1760,7 +1764,10 @@ pub fn step_prune(// Skip main worktree (non-linked); in bare repos all are linked,// so the default-branch check above is the primary guard.let wt_tree = repo.worktree_at(&wt.path);- if !wt_tree.is_linked()? {+ if !wt_tree+ .is_linked()+ .context("checking whether worktree is linked")?+ {continue;}@@ -1775,7 +1782,7 @@ pub fn step_prune(});}- for branch in repo.all_branches()? {+ for branch in repo.all_branches().context("listing branches")? {if seen_branches.contains(&branch) {continue;}@@ -1833,7 +1840,7 @@ pub fn step_prune(// Process results as they arrive from the channel.for (idx, result) in rx {- let (effective_target, reason) = result?;+ let (effective_target, reason) = result.context("checking branch integration")?;let Some(reason) = reason else {continue;};@@ -1853,7 +1860,7 @@ pub fn step_prune(// they were just created from the default branchif min_age_duration > Duration::ZERO {let wt_tree = repo.worktree_at(&wt.path);- let git_dir = wt_tree.git_dir()?;+ let git_dir = wt_tree.git_dir().context("resolving worktree git dir")?;let metadata = fs::metadata(&git_dir).context("Failed to read worktree git dir")?;let created = metadata.created().or_else(|_| {fs::metadata(git_dir.join("commondir")).and_then(|m| m.modified())@@ -1905,7 +1912,9 @@ pub fn step_prune(run_hooks,worktrees,&snapshot_arc,- )? {+ )+ .with_context(|| format!("removing worktree for {}", candidate.label))?+ {removed.push(candidate);}continue;@@ -1964,7 +1973,9 @@ pub fn step_prune(run_hooks,worktrees,&snapshot_arc,- )? {+ )+ .with_context(|| format!("removing worktree for {}", candidate.label))?+ {removed.push(candidate);}}@@ -2043,7 +2054,8 @@ pub fn step_prune(run_hooks,worktrees,&snapshot_arc,- )?+ )+ .with_context(|| format!("removing worktree for {}", current.label))?{removed.push(current);}diff --git a/src/main.rs b/src/main.rsindex ffda8eb8f..6560f636f 100644--- a/src/main.rs+++ b/src/main.rs@@ -1418,3 +1418,26 @@ fn main() {Err(error) => handle_command_failure(error, verbose, &command_line),}}++#[cfg(test)]+mod tests {+ use super::*;++ /// Regression: an anyhow error whose top-level message has the multi-line+ /// stderr from a failing git command — but a non-empty chain — must take+ /// the chain branch, not the `debug_assert!` branch. Real-world example:+ /// step_prune callers wrap `repo.run_command(...)` failures with+ /// `.context("listing worktrees")?`, so the multi-line git stderr ends up+ /// in the chain rather than as the top-level message.+ #[test]+ fn print_command_error_handles_multiline_with_context() {+ let inner = anyhow::anyhow!(+ "warning: unable to access '.git/config': Permission denied\nfatal: unknown error occurred while reading the configuration files"+ );+ let err: anyhow::Error = Err::<(), _>(inner)+ .context("listing worktrees")+ .unwrap_err();+ // Must not panic the debug_assert. The chain branch handles this.+ print_command_error(&err);+ }+}