Skip to main content

thor_wt/
lib.rs

1//! Thor worktree workflow commands.
2//!
3//! High-level worktree operations that compose thor-core primitives
4//! with subprocess git calls for push/pull/merge/rebase.
5
6mod clean;
7mod done;
8mod exec;
9mod go;
10mod pull;
11mod rebase;
12
13pub use clean::{clean, CleanResult};
14pub use done::{done, DoneOpts, DoneResult};
15pub use exec::{exec, ExecResult};
16pub use go::{go, save_last_worktree, GoQuery, GoResult};
17pub use pull::{pull, PullResult, PullStatus};
18pub use rebase::{rebase, RebaseResult};
19
20use thor_core::{find_repo, list_worktrees, Worktree};
21use std::path::PathBuf;
22
23/// List all worktrees.
24pub async fn list() -> anyhow::Result<Vec<Worktree>> {
25    let repo = find_repo()?;
26    Ok(list_worktrees(&repo).await?)
27}
28
29/// Remove a worktree by branch name.
30pub async fn remove(branch: &str, force: bool) -> anyhow::Result<()> {
31    let repo = find_repo()?;
32    Ok(thor_core::remove_worktree(&repo, branch, force).await?)
33}
34
35/// Result of creating a new worktree.
36pub struct NewResult {
37    pub path: PathBuf,
38    pub branch: String,
39}
40
41/// Create a new worktree, optionally tracking a remote branch.
42pub async fn new(branch: &str, base: Option<&str>, track: bool) -> anyhow::Result<NewResult> {
43    let repo = find_repo()?;
44
45    // If tracking, fetch the remote branch first
46    if track {
47        let fetch_ref = base.unwrap_or("main");
48        let repo_root = thor_core::repo_root(&repo)?;
49        let status = std::process::Command::new("git")
50            .args(["fetch", "origin", fetch_ref])
51            .current_dir(&repo_root)
52            .status()?;
53        if !status.success() {
54            anyhow::bail!("Failed to fetch origin/{}", fetch_ref);
55        }
56    }
57
58    let wt = thor_core::create_worktree(&repo, branch, base, ".worktrees").await?;
59    Ok(NewResult {
60        path: wt.path,
61        branch: branch.to_string(),
62    })
63}