Skip to main content

apm/cmd/
worktrees.rs

1use anyhow::Result;
2use apm_core::{config::Config, worktree};
3use std::path::Path;
4
5pub fn run(root: &Path, remove_id: Option<&str>) -> Result<()> {
6    let config = Config::load(root)?;
7
8    if let Some(id_arg) = remove_id {
9        return remove(root, &config, id_arg);
10    }
11
12    list(root, &config)
13}
14
15fn list(root: &Path, config: &Config) -> Result<()> {
16    let wt_tickets = worktree::list_worktrees_with_tickets(root, &config.tickets.dir)?;
17    if wt_tickets.is_empty() {
18        println!("No ticket worktrees provisioned.");
19        return Ok(());
20    }
21
22    for (wt_path, branch, t) in &wt_tickets {
23        let wt_name = wt_path
24            .file_name()
25            .and_then(|n| n.to_str())
26            .unwrap_or(branch.as_str());
27
28        match t {
29            Some(t) => println!(
30                "{}  {}",
31                wt_name,
32                t.frontmatter.state,
33            ),
34            None => println!("{}  (ticket not found)", wt_name),
35        }
36    }
37    Ok(())
38}
39
40fn remove(root: &Path, _config: &Config, id_arg: &str) -> Result<()> {
41    let (wt_path, _id) = crate::util::worktree_for_ticket(root, id_arg)?;
42    worktree::remove_worktree(root, &wt_path, false)?;
43    println!("Removed worktree: {}", wt_path.display());
44    Ok(())
45}