surshar / claude-code
Claude Code is an agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster by executing routine tasks, explaining complex code, and handling git workflows - all through natural language commands.
Built by Anthropic · github.com/anthropics
| 1 | import Argv from '../../argv' |
| 2 | import type Types from '../../types' |
| 3 | import { isAbsolutePath } from '../is-absolute-path' |
| 4 | import { REPOSITORY_LINES } from '../repository-lines' |
| 5 | |
| 6 | /** |
| 7 | * The repository the session's directory is in, read with one `rev-parse`: |
| 8 | * working tree top, git directory, common directory, all absolute. |
| 9 | * |
| 10 | * Exactly three absolute lines or nothing: a git too old for |
| 11 | * `--path-format` echoes the flag as a line of its own, and such an answer |
| 12 | * pins no repository rather than a wrong one. |
| 13 | * |
| 14 | * @param run runs git in the session's directory, nothing pinned yet |
| 15 | * @returns the three absolute paths, or null outside a working tree |
| 16 | */ |
| 17 | export async function repositoryOf( |
| 18 | run: Types.GitRun, |
| 19 | ): Promise<Types.Repository | null> { |
| 20 | const { exitCode, stdout } = await run([ |
| 21 | Argv.NO_OPTIONAL_LOCKS, |
| 22 | 'rev-parse', |
| 23 | '--path-format=absolute', |
| 24 | '--show-toplevel', |
| 25 | '--git-dir', |
| 26 | '--git-common-dir', |
| 27 | ]) |
| 28 | |
| 29 | const lines = stdout.split('\n').filter(line => line !== '') |
| 30 | const [toplevel = '', gitDir = '', commonDir = ''] = lines |
| 31 | |
| 32 | const isResolved = |
| 33 | exitCode === 0 && |
| 34 | lines.length === REPOSITORY_LINES && |
| 35 | lines.every(isAbsolutePath) |
| 36 | |
| 37 | return isResolved ? { toplevel, gitDir, commonDir } : null |
| 38 | } |