thor-wt 0.2.1

Worktree workflow commands for Thor
Documentation
//! Thor worktree workflow commands.
//!
//! High-level worktree operations that compose thor-core primitives
//! with subprocess git calls for push/pull/merge/rebase.

mod clean;
mod done;
mod exec;
mod go;
mod pull;
mod rebase;

pub use clean::{clean, CleanResult};
pub use done::{done, DoneOpts, DoneResult};
pub use exec::{exec, ExecResult};
pub use go::{go, save_last_worktree, GoQuery, GoResult};
pub use pull::{pull, PullResult, PullStatus};
pub use rebase::{rebase, RebaseResult};

use thor_core::{find_repo, list_worktrees, Worktree};
use std::path::PathBuf;

/// List all worktrees.
pub async fn list() -> anyhow::Result<Vec<Worktree>> {
    let repo = find_repo()?;
    Ok(list_worktrees(&repo).await?)
}

/// Remove a worktree by branch name.
pub async fn remove(branch: &str, force: bool) -> anyhow::Result<()> {
    let repo = find_repo()?;
    Ok(thor_core::remove_worktree(&repo, branch, force).await?)
}

/// Result of creating a new worktree.
pub struct NewResult {
    pub path: PathBuf,
    pub branch: String,
}

/// Create a new worktree, optionally tracking a remote branch.
pub async fn new(branch: &str, base: Option<&str>, track: bool) -> anyhow::Result<NewResult> {
    let repo = find_repo()?;

    // If tracking, fetch the remote branch first
    if track {
        let fetch_ref = base.unwrap_or("main");
        let repo_root = thor_core::repo_root(&repo)?;
        let status = std::process::Command::new("git")
            .args(["fetch", "origin", fetch_ref])
            .current_dir(&repo_root)
            .status()?;
        if !status.success() {
            anyhow::bail!("Failed to fetch origin/{}", fetch_ref);
        }
    }

    let wt = thor_core::create_worktree(&repo, branch, base, ".worktrees").await?;
    Ok(NewResult {
        path: wt.path,
        branch: branch.to_string(),
    })
}