hyprstream_core/cli/
worktree_handlers.rs1#![allow(clippy::print_stdout, clippy::print_stderr)]
4
5use crate::cli::git_handlers::apply_policy_template_to_model;
6use anyhow::Result;
7use hyprstream_rpc_std::registry_client::RegistryClient;
8use hyprstream_rpc_std::registry_client::{CreateWorktreeRequest, RemoveWorktreeRequest};
9use std::io::{self, Write};
10use tracing::info;
11
12pub async fn handle_worktree_add(
14 registry: &RegistryClient,
15 model: &str,
16 branch: &str,
17 policy_template: Option<String>,
18) -> Result<()> {
19 info!("Creating worktree {}/{}", model, branch);
20
21 let tracked = registry
22 .get_by_name(model)
23 .await
24 .map_err(|e| anyhow::anyhow!("Failed to get repository client: {}", e))?;
25 let repo_client = registry.repo(&tracked.id);
26
27 let worktrees = repo_client
29 .list_worktrees()
30 .await
31 .map_err(|e| anyhow::anyhow!("Failed to list worktrees: {}", e))?;
32
33 if worktrees.iter().any(|wt| wt.branch_name == branch) {
34 anyhow::bail!(
35 "Worktree already exists for branch '{}' in model {}",
36 branch,
37 model,
38 );
39 }
40
41 let branches = repo_client
43 .list_branches()
44 .await
45 .map_err(|e| anyhow::anyhow!("Failed to list branches: {}", e))?;
46
47 if !branches.contains(&branch.to_owned()) {
48 let branch_list = if branches.is_empty() {
49 " (no branches found)".to_owned()
50 } else {
51 branches
52 .iter()
53 .map(|b| format!(" - {b}"))
54 .collect::<Vec<_>>()
55 .join("\n")
56 };
57 anyhow::bail!(
58 "Branch '{}' does not exist in model {}.\n\nAvailable branches:\n{}",
59 branch,
60 model,
61 branch_list
62 );
63 }
64
65 repo_client
67 .create_worktree(&CreateWorktreeRequest {
68 branch: branch.to_owned(),
69 })
70 .await
71 .map_err(|e| anyhow::anyhow!("Failed to create worktree: {}", e))?;
72
73 println!("Created worktree {model}/{branch}:");
74 println!(" Branch: {branch}");
75
76 if let Some(ref template_name) = policy_template {
78 apply_policy_template_to_model(registry, model, template_name).await?;
79 }
80
81 Ok(())
82}
83
84pub async fn handle_worktree_list(
86 registry: &RegistryClient,
87 model: &str,
88 show_all: bool,
89) -> Result<()> {
90 info!("Listing worktrees for model {}", model);
91
92 let tracked = registry
93 .get_by_name(model)
94 .await
95 .map_err(|e| anyhow::anyhow!("Failed to get repository client: {}", e))?;
96 let repo_client = registry.repo(&tracked.id);
97 let worktrees = repo_client
98 .list_worktrees()
99 .await
100 .map_err(|e| anyhow::anyhow!("Failed to list worktrees: {}", e))?;
101
102 let has_worktree =
104 |branch: &str| -> bool { worktrees.iter().any(|wt| wt.branch_name == branch) };
105
106 if show_all {
107 let all_branches = repo_client
109 .list_branches()
110 .await
111 .map_err(|e| anyhow::anyhow!("Failed to list branches: {}", e))?;
112
113 if all_branches.is_empty() {
114 println!("No branches found for model {model}");
115 return Ok(());
116 }
117
118 println!("Branches for {model}:\n");
119 for branch in &all_branches {
120 let status = if has_worktree(branch) {
121 "[active]"
122 } else {
123 "[no worktree]"
124 };
125 println!(" {branch} {status}");
126 }
127
128 let inactive_count = all_branches.iter().filter(|b| !has_worktree(b)).count();
129 if inactive_count > 0 {
130 println!("\nCreate a worktree with:");
131 println!(" hyprstream worktree add {model} <branch>");
132 }
133 } else {
134 if worktrees.is_empty() {
136 println!("No worktrees found for model {model}");
137 println!("\nCreate a worktree with:");
138 println!(" hyprstream worktree add {model} <branch>");
139 println!("\nList all branches with:");
140 println!(" hyprstream worktree list {model} --all");
141 return Ok(());
142 }
143
144 println!("Worktrees for {model}:\n");
145
146 for wt in &worktrees {
147 let branch_name = &wt.branch_name;
148 let dirty_marker = if wt.is_dirty { " [dirty]" } else { "" };
149 println!(" {branch_name} ({model}){dirty_marker}");
150 println!();
151 }
152 }
153
154 Ok(())
155}
156
157pub async fn handle_worktree_info(
159 registry: &RegistryClient,
160 model: &str,
161 branch: &str,
162) -> Result<()> {
163 info!("Getting info for worktree {}/{}", model, branch);
164
165 let tracked = registry
166 .get_by_name(model)
167 .await
168 .map_err(|e| anyhow::anyhow!("Failed to get repository client: {}", e))?;
169 let repo_client = registry.repo(&tracked.id);
170
171 let worktrees = repo_client
173 .list_worktrees()
174 .await
175 .map_err(|e| anyhow::anyhow!("Failed to list worktrees: {}", e))?;
176
177 let wt = worktrees
178 .iter()
179 .find(|wt| wt.branch_name == branch)
180 .ok_or_else(|| anyhow::anyhow!("Worktree {} does not exist for model {}", branch, model))?;
181
182 println!("Worktree: {model}/{branch}\n");
183 println!("Status: Active");
184 println!("Branch: {branch}");
185 if wt.is_dirty {
186 println!("Dirty: yes (uncommitted changes)");
187 }
188
189 Ok(())
190}
191
192pub async fn handle_worktree_remove(
194 registry: &RegistryClient,
195 model: &str,
196 branch: &str,
197 force: bool,
198) -> Result<()> {
199 info!("Removing worktree {}/{}", model, branch);
200
201 let tracked = registry
202 .get_by_name(model)
203 .await
204 .map_err(|e| anyhow::anyhow!("Failed to get repository client: {}", e))?;
205 let repo_client = registry.repo(&tracked.id);
206
207 let worktrees = repo_client
209 .list_worktrees()
210 .await
211 .map_err(|e| anyhow::anyhow!("Failed to list worktrees: {}", e))?;
212
213 if !worktrees.iter().any(|wt| wt.branch_name == branch) {
214 anyhow::bail!("Worktree {} does not exist for model {}", branch, model);
215 }
216
217 if !force {
219 println!("Warning: This will remove the worktree for branch '{branch}'");
220 println!(" Any uncommitted changes will be lost!");
221 print!("\n Continue? [y/N] ");
222 io::stdout().flush()?;
223
224 let mut input = String::new();
225 io::stdin().read_line(&mut input)?;
226
227 if !input.trim().eq_ignore_ascii_case("y") {
228 println!("Aborted.");
229 return Ok(());
230 }
231 }
232
233 repo_client
235 .remove_worktree(&RemoveWorktreeRequest {
236 branch: branch.to_owned(),
237 force: false,
238 })
239 .await
240 .map_err(|e| anyhow::anyhow!("Failed to remove worktree: {}", e))?;
241
242 println!("Removed worktree {model}/{branch}");
243
244 Ok(())
245}