(
repo_path: &Path,
branch: Option<&str>,
target_path: Option<&str>,
)
| 97 | } |
| 98 | |
| 99 | pub fn create_worktree( |
| 100 | repo_path: &Path, |
| 101 | branch: Option<&str>, |
| 102 | target_path: Option<&str>, |
| 103 | ) -> Result<WorktreeInfo, WorktreeError> { |
| 104 | if !is_git_repo(repo_path) { |
| 105 | return Err(WorktreeError::NotGitRepo); |
| 106 | } |
| 107 | |
| 108 | let default_branch_name = format!("worktree-{}", chrono::Utc::now().format("%Y%m%d-%H%M%S")); |
| 109 | let branch_name = branch.unwrap_or(&default_branch_name); |
| 110 | |
| 111 | let repo_name = repo_path |
| 112 | .file_name() |
| 113 | .map(|n| n.to_string_lossy().to_string()) |
| 114 | .unwrap_or_else(|| "repo".to_string()); |
| 115 | let default_worktree_path = format!("{}-{}", repo_name, branch_name); |
| 116 | |
| 117 | let worktree_path = target_path |
| 118 | .map(|s| s.to_string()) |
| 119 | .unwrap_or(default_worktree_path); |
| 120 | |
| 121 | let full_path = if Path::new(&worktree_path).is_absolute() { |
| 122 | worktree_path.clone() |
| 123 | } else { |
| 124 | repo_path |
| 125 | .parent() |
| 126 | .map(|p| p.join(&worktree_path)) |
| 127 | .unwrap_or_else(|| repo_path.join(&worktree_path)) |
| 128 | .to_string_lossy() |
| 129 | .to_string() |
| 130 | }; |
| 131 | |
| 132 | let branch_exists = run_git(&["branch", "--list", branch_name], repo_path) |
| 133 | .map(|s| !s.is_empty()) |
| 134 | .unwrap_or(false); |
| 135 | |
| 136 | if branch_exists { |
| 137 | run_git( |
| 138 | &[ |
| 139 | "worktree", |
| 140 | "add", |
| 141 | "-b", |
| 142 | branch_name, |
| 143 | &full_path, |
| 144 | branch_name, |
| 145 | ], |
| 146 | repo_path, |
| 147 | )?; |
| 148 | } else { |
| 149 | let default_branch = run_git(&["symbolic-ref", "--short", "HEAD"], repo_path) |
| 150 | .unwrap_or_else(|_| "main".to_string()); |
| 151 | |
| 152 | run_git( |
| 153 | &[ |
| 154 | "worktree", |
| 155 | "add", |
| 156 | "-b", |
nothing calls this directly
no test coverage detected