(directory: string)
| 14 | * @returns true if the directory is in a git repository, false otherwise |
| 15 | */ |
| 16 | export function isGitRepository(directory: string): boolean { |
| 17 | try { |
| 18 | let currentDir = path.resolve(directory); |
| 19 | |
| 20 | while (true) { |
| 21 | const gitDir = path.join(currentDir, '.git'); |
| 22 | |
| 23 | // Check if .git exists (either as directory or file for worktrees) |
| 24 | if (fs.existsSync(gitDir)) { |
| 25 | return true; |
| 26 | } |
| 27 | |
| 28 | const parentDir = path.dirname(currentDir); |
| 29 | |
| 30 | // If we've reached the root directory, stop searching |
| 31 | if (parentDir === currentDir) { |
| 32 | break; |
| 33 | } |
| 34 | |
| 35 | currentDir = parentDir; |
| 36 | } |
| 37 | |
| 38 | return false; |
| 39 | } catch (_error) { |
| 40 | // If any filesystem error occurs, assume not a git repo |
| 41 | return false; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Finds the root directory of a git repository |
no outgoing calls
no test coverage detected