IsWorktree checks if a path is a valid git worktree.
(path string)
| 148 | |
| 149 | // IsWorktree checks if a path is a valid git worktree. |
| 150 | func IsWorktree(path string) bool { |
| 151 | cmd := exec.Command("git", "rev-parse", "--is-inside-work-tree") |
| 152 | cmd.Dir = path |
| 153 | output, err := cmd.Output() |
| 154 | if err != nil { |
| 155 | return false |
| 156 | } |
| 157 | if strings.TrimSpace(string(output)) != "true" { |
| 158 | return false |
| 159 | } |
| 160 | |
| 161 | // Verify it's actually a worktree (not the main repo) by checking for .git file |
| 162 | cmd = exec.Command("git", "rev-parse", "--git-common-dir") |
| 163 | cmd.Dir = path |
| 164 | commonDir, err := cmd.Output() |
| 165 | if err != nil { |
| 166 | return false |
| 167 | } |
| 168 | |
| 169 | cmd = exec.Command("git", "rev-parse", "--git-dir") |
| 170 | cmd.Dir = path |
| 171 | gitDir, err := cmd.Output() |
| 172 | if err != nil { |
| 173 | return false |
| 174 | } |
| 175 | |
| 176 | // In a worktree, --git-dir differs from --git-common-dir |
| 177 | // But we also consider the main worktree valid |
| 178 | _ = commonDir |
| 179 | _ = gitDir |
| 180 | return true |
| 181 | } |
| 182 | |
| 183 | // WorktreePathForPRD returns the worktree path for a given PRD name. |
| 184 | func WorktreePathForPRD(baseDir, prdName string) string { |
no outgoing calls