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 / git-backend-of.ts
66 lines · 1.9 KBRaw
1import type Backend from '../backend'
2import Limits from '../limits'
3import Argv from './argv'
4import { FAILED_RUN } from './failed-run'
5import { fetchDiff } from './fetch-diff.js'
6import { fetchFileHunks } from './fetch-file-hunks.js'
7import { GIT_BASE_MODES } from './git-base-modes'
8import { GIT_WORDS } from './git-words'
9import Probes from './probes'
10import type Types from './types'
11
12/**
13 * The git backend over the session's directory: the repository read with
14 * one `rev-parse` in that directory, then every child pinned to it
15 * (`--git-dir`, `--work-tree`, cwd the top) under the C locale and the
16 * fetch's timeout (gitDiff.ts execPinnedGit).
17 *
18 * @param host the bound host's runner and probes
19 * @returns the backend, or null outside a git working tree
20 */
21export async function gitBackendOf(
22 host: Backend.BackendHost,
23): Promise<Backend.Backend | null> {
24 const runOf =
25 (pinned: Types.Repository | null): Types.GitRun =>
26 async argv => {
27 try {
28 return await host.run(
29 ['git', ...(pinned ? Argv.pinnedLeadOf(pinned) : []), ...argv],
30 {
31 timeoutMs: Limits.GIT_TIMEOUT_MS,
32 env: Argv.GIT_CHILD_ENV,
33 ...(pinned && { cwd: pinned.toplevel }),
34 },
35 )
36 } catch {
37 return FAILED_RUN
38 }
39 }
40
41 const repository = await Probes.repositoryOf(runOf(null))
42
43 if (!repository) {
44 return null
45 }
46
47 const run = runOf(repository)
48 const depsOf = (): Types.GitDeps => ({
49 run,
50 repository,
51 mtimeOf: host.mtimeOf,
52 entryKindsOf: host.entryKindsOf,
53 sessionStartMs: host.sessionStartMsOf(),
54 onBranchBase: host.onBranchBase,
55 })
56
57 return {
58 repository,
59 baseModes: GIT_BASE_MODES,
60 words: GIT_WORDS,
61 fetchDiff: mode => fetchDiff(depsOf(), mode),
62 fetchFileHunks: (data, file) => fetchFileHunks(run, data, file),
63 headKeyOf: () =>
64 Probes.headKeyOf({ ...depsOf(), readFile: host.readFile }, repository),
65 }
66}