1use tokio::process::Command;
7
8use crate::Result;
9
10pub async fn delete_local_branch(branch: &str, force: bool) -> Result<()> {
23 let flag = if force { "-D" } else { "-d" };
24 let output = Command::new("git")
25 .args(["branch", flag, branch])
26 .output()
27 .await
28 .map_err(|e| crate::CoreError::App(format!("Failed to execute git branch: {e}")))?;
29
30 if !output.status.success() {
31 let stderr = String::from_utf8_lossy(&output.stderr);
32 return Err(crate::CoreError::App(format!(
33 "Failed to delete local branch '{branch}': {stderr}"
34 )));
35 }
36
37 Ok(())
38}
39
40pub async fn delete_remote_branch(branch: &str) -> Result<()> {
51 let output = Command::new("git")
52 .args(["push", "origin", "--delete", branch])
53 .output()
54 .await
55 .map_err(|e| crate::CoreError::App(format!("Failed to execute git push: {e}")))?;
56
57 if !output.status.success() {
58 let stderr = String::from_utf8_lossy(&output.stderr);
59 return Err(crate::CoreError::App(format!(
60 "Failed to delete remote branch '{branch}': {stderr}"
61 )));
62 }
63
64 Ok(())
65}
66
67#[allow(
77 clippy::unused_async,
78 reason = "Consistent async API with other git_ops functions"
79)]
80pub async fn is_in_worktree() -> Result<bool> {
81 let git_path = std::path::Path::new(".git");
82 if !git_path.exists() {
83 return Err(crate::CoreError::App("Not in a git repository".to_string()));
84 }
85 Ok(git_path.is_file())
86}
87
88pub async fn get_main_repo_path() -> Result<std::path::PathBuf> {
99 let output = Command::new("git")
100 .args(["rev-parse", "--git-common-dir"])
101 .output()
102 .await
103 .map_err(|e| crate::CoreError::App(format!("Failed to execute git: {e}")))?;
104
105 if !output.status.success() {
106 return Err(crate::CoreError::App(
107 "Failed to get main repo path".to_string(),
108 ));
109 }
110
111 let git_dir = String::from_utf8_lossy(&output.stdout).trim().to_string();
112 let git_path = std::path::PathBuf::from(&git_dir);
113
114 git_path
116 .parent()
117 .map(std::path::Path::to_path_buf)
118 .ok_or_else(|| {
119 crate::CoreError::App("Cannot determine parent of git directory".to_string())
120 })
121}
122
123pub async fn get_current_worktree_path() -> Result<std::path::PathBuf> {
133 let output = Command::new("git")
134 .args(["rev-parse", "--show-toplevel"])
135 .output()
136 .await
137 .map_err(|e| crate::CoreError::App(format!("Failed to execute git: {e}")))?;
138
139 if !output.status.success() {
140 return Err(crate::CoreError::App(
141 "Failed to get current worktree path".to_string(),
142 ));
143 }
144
145 let path = String::from_utf8_lossy(&output.stdout).trim().to_string();
146 Ok(std::path::PathBuf::from(path))
147}
148
149pub async fn remove_worktree(path: &str) -> Result<()> {
160 let output = Command::new("git")
161 .args(["worktree", "remove", path])
162 .output()
163 .await
164 .map_err(|e| {
165 crate::CoreError::App(format!("Failed to execute git worktree remove: {e}"))
166 })?;
167
168 if !output.status.success() {
169 let stderr = String::from_utf8_lossy(&output.stderr);
170 return Err(crate::CoreError::App(format!(
171 "Failed to remove worktree '{path}': {stderr}"
172 )));
173 }
174
175 Ok(())
176}
177
178pub async fn exit_worktree() -> Result<std::path::PathBuf> {
189 if !is_in_worktree().await? {
190 return Err(crate::CoreError::App("Not in a worktree".to_string()));
191 }
192
193 let main_repo = get_main_repo_path().await?;
194 std::env::set_current_dir(&main_repo).map_err(|e| {
195 crate::CoreError::App(format!(
196 "Failed to change directory to {}: {e}",
197 main_repo.display()
198 ))
199 })?;
200
201 Ok(main_repo)
202}
203
204#[cfg(test)]
205mod tests {
206 }