Skip to main content

worktree_io/git/
prune.rs

1use anyhow::{bail, Context, Result};
2use std::path::Path;
3
4/// Prune stale worktree administrative files from `repo`.
5///
6/// This removes git's internal bookkeeping for any worktree whose working
7/// directory no longer exists on disk, allowing a fresh `git worktree add`
8/// to succeed for the same branch.
9///
10/// # Errors
11///
12/// Returns an error if the git command fails to spawn or exits non-zero.
13pub fn git_worktree_prune(repo: &Path) -> Result<()> {
14    let status = super::git_cmd()
15        .args(["-C"])
16        .arg(repo)
17        .args(["worktree", "prune"])
18        .status()
19        .context("Failed to run `git worktree prune`")?;
20
21    if !status.success() {
22        bail!("git worktree prune failed"); // LLVM_COV_EXCL_LINE
23    }
24    Ok(())
25}