1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
use super::super::*;
impl CliRunner {
pub(crate) async fn show_logs(
&self,
follow: bool,
agent: Option<&str>,
lines: usize,
) -> Result<()> {
use std::fs;
use std::io::{BufRead, BufReader};
let logs_dir = self.repo_path.join(".ccswarm/logs");
// Check if logs directory exists
if !logs_dir.exists() {
if self.json_output {
println!(
"{}",
serde_json::to_string_pretty(&serde_json::json!({
"status": "success",
"message": "No logs directory found",
"lines": 0,
}))?
);
} else {
println!("📝 Logs");
println!("======");
println!("No logs directory found at {}", logs_dir.display());
println!("Run 'ccswarm pipeline --task \"...\"' to create logs");
}
return Ok(());
}
// Read log files
let mut log_entries = Vec::new();
for entry in fs::read_dir(&logs_dir)? {
let entry = entry?;
let path = entry.path();
if !path.is_file() {
continue;
}
// Filter by agent if specified
if let Some(agent_filter) = agent
&& let Some(filename) = path.file_name().and_then(|n| n.to_str())
&& !filename.contains(agent_filter)
{
continue;
}
// Read log file
let file = fs::File::open(&path)?;
let reader = BufReader::new(file);
for line_content in reader.lines().map_while(Result::ok) {
log_entries.push((
path.file_name()
.and_then(|n| n.to_str())
.unwrap_or("unknown")
.to_string(),
line_content,
));
}
}
// Get last N lines
let start_idx = if log_entries.len() > lines {
log_entries.len() - lines
} else {
0
};
let displayed_logs: Vec<_> = log_entries[start_idx..].to_vec();
if self.json_output {
println!(
"{}",
serde_json::to_string_pretty(&serde_json::json!({
"status": "success",
"message": "Logs displayed",
"lines": displayed_logs.len(),
"total_lines": log_entries.len(),
"logs": displayed_logs.iter().map(|(file, content)| {
serde_json::json!({
"file": file,
"content": content,
})
}).collect::<Vec<_>>(),
}))?
);
} else {
println!("📝 Logs");
println!("======");
if let Some(agent_filter) = agent {
println!("Agent filter: {}", agent_filter);
}
println!("Showing last {} lines", lines);
println!();
for (file, content) in displayed_logs {
println!("[{}] {}", file.bright_blue(), content);
}
if log_entries.is_empty() {
println!("No logs available yet");
}
if follow {
println!();
println!("⚠️ Follow mode requires the TUI");
println!(" Run: ccswarm tui --follow-logs");
}
}
Ok(())
}
pub(crate) async fn list_agents(&self, _all: bool) -> Result<()> {
if self.json_output {
println!("{}", serde_json::to_string_pretty(&self.config.agents)?);
} else {
println!("🤖 Configured Agents");
println!("==================");
for (name, config) in &self.config.agents {
println!("Agent: {}", name);
println!(" Specialization: {}", config.specialization);
println!(" Worktree: {}", config.worktree);
println!(" Branch: {}", config.branch);
println!(" Model: {}", config.claude_config.model);
println!(" Output Format: {:?}", config.claude_config.output_format);
println!();
}
}
Ok(())
}
pub(crate) async fn handle_worktree(&self, action: &WorktreeAction) -> Result<()> {
let manager = crate::git::shell::ShellWorktreeManager::new(self.repo_path.clone())?;
match action {
WorktreeAction::List => {
let worktrees = manager.list_worktrees().await?;
if self.json_output {
println!("{}", serde_json::to_string_pretty(&worktrees)?);
} else {
println!("🌳 Git Worktrees");
println!("===============");
for wt in &worktrees {
println!("Path: {}", wt.path.display());
println!(" Branch: {}", wt.branch);
println!(" Head: {}", wt.head_commit);
println!(" Locked: {}", wt.is_locked);
println!();
}
}
}
WorktreeAction::Create {
path,
branch,
new_branch,
} => {
let info = if *new_branch {
manager.create_worktree_full(path, branch, true).await?
} else {
manager.create_worktree(path, branch).await?
};
if self.json_output {
println!("{}", serde_json::to_string_pretty(&info)?);
} else {
println!("✅ Worktree created");
println!(" Path: {}", info.path.display());
println!(" Branch: {}", info.branch);
}
}
WorktreeAction::Remove { path, force } => {
if *force {
manager.remove_worktree_full(path, true).await?
} else {
manager.remove_worktree(path).await?
};
if self.json_output {
println!(
"{}",
serde_json::to_string_pretty(&serde_json::json!({
"status": "success",
"message": "Worktree removed",
"path": path,
}))?
);
} else {
println!("✅ Worktree removed: {}", path.display());
}
}
WorktreeAction::Prune => {
manager.prune_worktrees().await?;
if self.json_output {
println!(
"{}",
serde_json::to_string_pretty(&serde_json::json!({
"status": "success",
"message": "Worktrees pruned",
}))?
);
} else {
println!("✅ Stale worktrees pruned");
}
}
WorktreeAction::Clean { force } => {
use std::io::{self, Write};
// Find all ccswarm-related worktrees
let worktrees = manager.list_worktrees().await?;
let ccswarm_worktrees: Vec<_> = worktrees
.iter()
.filter(|w| w.branch.contains("agent") || w.branch.contains("feature/"))
.collect();
if ccswarm_worktrees.is_empty() {
if self.json_output {
println!(
"{}",
serde_json::to_string_pretty(&serde_json::json!({
"status": "success",
"message": "No ccswarm worktrees found",
}))?
);
} else {
println!("✅ No ccswarm worktrees to clean");
}
return Ok(());
}
// Ask for confirmation unless forced
if !force {
println!("⚠️ Found {} ccswarm worktrees:", ccswarm_worktrees.len());
for w in &ccswarm_worktrees {
println!(" - {} ({})", w.path.display(), w.branch);
}
print!("\nAre you sure you want to remove all these worktrees? [y/N] ");
io::stdout().flush()?;
let mut response = String::new();
io::stdin().read_line(&mut response)?;
if !response.trim().eq_ignore_ascii_case("y") {
println!("❌ Cleanup cancelled");
return Ok(());
}
}
// Remove all ccswarm worktrees
let mut removed_count = 0;
for worktree in ccswarm_worktrees {
match manager.remove_worktree(&worktree.path).await {
Ok(_) => {
removed_count += 1;
if !self.json_output {
println!(" ✓ Removed {}", worktree.path.display());
}
}
Err(e) => {
if !self.json_output {
println!(
" ✗ Failed to remove {}: {}",
worktree.path.display(),
e
);
}
}
}
}
// Also clean up branches
let output = tokio::process::Command::new("git")
.args(["branch", "--list", "*agent*", "feature/*"])
.output()
.await?;
if output.status.success() {
let branches = String::from_utf8_lossy(&output.stdout);
let branch_count = branches.lines().count();
if branch_count > 0 {
tokio::process::Command::new("git")
.args(&["branch", "-D"])
.args(branches.lines().map(|b| b.trim().trim_start_matches("* ")))
.output()
.await?;
}
}
if self.json_output {
println!(
"{}",
serde_json::to_string_pretty(&serde_json::json!({
"status": "success",
"message": "Cleanup completed",
"worktrees_removed": removed_count,
}))?
);
} else {
println!(
"\n✅ Cleanup completed: {} worktrees removed",
removed_count
);
}
}
}
Ok(())
}
}