Create a branch from HEAD # Errors Returns an error if: - The repository cannot be opened - HEAD cannot be resolved (e.g., empty repository with no commits) - The branch already exists - The working directory cannot be updated
(repo_path: &Path, branch_name: &str)
| 19 | /// - The branch already exists |
| 20 | /// - The working directory cannot be updated |
| 21 | pub async fn create_branch(repo_path: &Path, branch_name: &str) -> Result<(), String> { |
| 22 | tokio::task::spawn_blocking({ |
| 23 | let repo_path = repo_path.to_owned(); |
| 24 | let branch_name = branch_name.to_owned(); |
| 25 | move || -> Result<(), String> { |
| 26 | let repo = Repository::open(&repo_path) |
| 27 | .map_err(|e| format!("Failed to open repository: {e}"))?; |
| 28 | |
| 29 | let head = repo |
| 30 | .head() |
| 31 | .map_err(|e| format!("Failed to get HEAD: {e}"))?; |
| 32 | |
| 33 | let commit = head |
| 34 | .peel_to_commit() |
| 35 | .map_err(|e| format!("Failed to get commit from HEAD: {e}"))?; |
| 36 | |
| 37 | repo.branch(&branch_name, &commit, false) |
| 38 | .map_err(|e| format!("Failed to create branch: {e}"))?; |
| 39 | |
| 40 | repo.set_head(&format!("refs/heads/{branch_name}")) |
| 41 | .map_err(|e| format!("Failed to checkout branch: {e}"))?; |
| 42 | |
| 43 | repo.checkout_head(None) |
| 44 | .map_err(|e| format!("Failed to update working directory: {e}"))?; |
| 45 | |
| 46 | Ok(()) |
| 47 | } |
| 48 | }) |
| 49 | .await |
| 50 | .map_err(|e| format!("Task join error: {e}"))? |
| 51 | } |
| 52 | |
| 53 | pub async fn get_status(repo_path: &Path) -> Result<String, String> { |
| 54 | tokio::task::spawn_blocking({ |