Branch operations: list or create.
(&self, args: &serde_json::Value, git: &dyn WorkspaceGit)
| 235 | |
| 236 | /// Branch operations: list or create. |
| 237 | async fn branch(&self, args: &serde_json::Value, git: &dyn WorkspaceGit) -> Result<ToolOutput> { |
| 238 | let name = args.get("name").and_then(|v| v.as_str()); |
| 239 | |
| 240 | if let Some(branch_name) = name { |
| 241 | let base = args.get("base").and_then(|v| v.as_str()).unwrap_or("HEAD"); |
| 242 | |
| 243 | match git |
| 244 | .create_branch(WorkspaceGitCreateBranchRequest { |
| 245 | name: branch_name.to_string(), |
| 246 | base: base.to_string(), |
| 247 | }) |
| 248 | .await |
| 249 | { |
| 250 | Ok(_) => Ok(ToolOutput::success(format!( |
| 251 | "Created branch: {} (based on {})", |
| 252 | branch_name, base |
| 253 | )) |
| 254 | .with_metadata(serde_json::json!({ "branch": branch_name, "base": base }))), |
| 255 | Err(e) => Ok(ToolOutput::error(format!("Failed to create branch: {e}"))), |
| 256 | } |
| 257 | } else { |
| 258 | match git.list_branches().await { |
| 259 | Ok(branches) => { |
| 260 | if branches.is_empty() { |
| 261 | return Ok(ToolOutput::success("No branches found.")); |
| 262 | } |
| 263 | |
| 264 | let entries: Vec<String> = branches |
| 265 | .iter() |
| 266 | .map(|branch| { |
| 267 | let prefix = if branch.is_current { "* " } else { " " }; |
| 268 | format!("{}{}", prefix, branch.name) |
| 269 | }) |
| 270 | .collect(); |
| 271 | |
| 272 | Ok( |
| 273 | ToolOutput::success(format!("Branches:\n{}", entries.join("\n"))) |
| 274 | .with_metadata(serde_json::json!({ "count": branches.len() })), |
| 275 | ) |
| 276 | } |
| 277 | Err(e) => Ok(ToolOutput::error(format!("Failed to list branches: {e}"))), |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | /// Checkout a branch or commit. |
| 283 | async fn checkout( |
no test coverage detected