use tokio::process::Command;
use crate::Result;
pub async fn delete_local_branch(branch: &str, force: bool) -> Result<()> {
let flag = if force { "-D" } else { "-d" };
let output = Command::new("git")
.args(["branch", flag, branch])
.output()
.await
.map_err(|e| crate::CoreError::App(format!("Failed to execute git branch: {e}")))?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
return Err(crate::CoreError::App(format!(
"Failed to delete local branch '{branch}': {stderr}"
)));
}
Ok(())
}
pub async fn delete_remote_branch(branch: &str) -> Result<()> {
let output = Command::new("git")
.args(["push", "origin", "--delete", branch])
.output()
.await
.map_err(|e| crate::CoreError::App(format!("Failed to execute git push: {e}")))?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
return Err(crate::CoreError::App(format!(
"Failed to delete remote branch '{branch}': {stderr}"
)));
}
Ok(())
}
#[allow(
clippy::unused_async,
reason = "Consistent async API with other git_ops functions"
)]
pub async fn is_in_worktree() -> Result<bool> {
let git_path = std::path::Path::new(".git");
if !git_path.exists() {
return Err(crate::CoreError::App("Not in a git repository".to_string()));
}
Ok(git_path.is_file())
}
pub async fn get_main_repo_path() -> Result<std::path::PathBuf> {
let output = Command::new("git")
.args(["rev-parse", "--git-common-dir"])
.output()
.await
.map_err(|e| crate::CoreError::App(format!("Failed to execute git: {e}")))?;
if !output.status.success() {
return Err(crate::CoreError::App(
"Failed to get main repo path".to_string(),
));
}
let git_dir = String::from_utf8_lossy(&output.stdout).trim().to_string();
let git_path = std::path::PathBuf::from(&git_dir);
git_path
.parent()
.map(std::path::Path::to_path_buf)
.ok_or_else(|| {
crate::CoreError::App("Cannot determine parent of git directory".to_string())
})
}
pub async fn get_current_worktree_path() -> Result<std::path::PathBuf> {
let output = Command::new("git")
.args(["rev-parse", "--show-toplevel"])
.output()
.await
.map_err(|e| crate::CoreError::App(format!("Failed to execute git: {e}")))?;
if !output.status.success() {
return Err(crate::CoreError::App(
"Failed to get current worktree path".to_string(),
));
}
let path = String::from_utf8_lossy(&output.stdout).trim().to_string();
Ok(std::path::PathBuf::from(path))
}
pub async fn remove_worktree(path: &str) -> Result<()> {
let output = Command::new("git")
.args(["worktree", "remove", path])
.output()
.await
.map_err(|e| {
crate::CoreError::App(format!("Failed to execute git worktree remove: {e}"))
})?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
return Err(crate::CoreError::App(format!(
"Failed to remove worktree '{path}': {stderr}"
)));
}
Ok(())
}
pub async fn exit_worktree() -> Result<std::path::PathBuf> {
if !is_in_worktree().await? {
return Err(crate::CoreError::App("Not in a worktree".to_string()));
}
let main_repo = get_main_repo_path().await?;
std::env::set_current_dir(&main_repo).map_err(|e| {
crate::CoreError::App(format!(
"Failed to change directory to {}: {e}",
main_repo.display()
))
})?;
Ok(main_repo)
}
#[cfg(test)]
mod tests {
}