spacehub

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

claude-code / mods / diff / hooks / git / probes / repository-of / repository-of.ts
36 lines · 1.2 KBRaw
1import Argv from '../../argv'
2import type Types from '../../types'
3import { isAbsolutePath } from '../is-absolute-path'
4import { 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 */
17export 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 const lines = stdout.split('\n').filter(line => line !== '')
29 const [toplevel = '', gitDir = '', commonDir = ''] = lines
30 const isResolved =
31 exitCode === 0 &&
32 lines.length === REPOSITORY_LINES &&
33 lines.every(isAbsolutePath)
34
35 return isResolved ? { toplevel, gitDir, commonDir } : null
36}