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
68 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'
6import { fetchFileHunks } from './fetch-file-hunks'
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 *
16 * Each child runs with `--git-dir`, `--work-tree` and cwd the top, under
17 * the C locale and the fetch's timeout (the built-in's execPinnedGit).
18 *
19 * @param host the bound host's runner and probes
20 * @returns the backend, or null outside a git working tree
21 */
22export async function gitBackendOf(
23 host: Backend.BackendHost,
24): Promise<Backend.Backend | null> {
25 const runOf =
26 (pinned: Types.Repository | null): Types.GitRun =>
27 async argv => {
28 try {
29 return await host.run(
30 ['git', ...(pinned ? Argv.pinnedLeadOf(pinned) : []), ...argv],
31 {
32 timeoutMs: Limits.GIT_TIMEOUT_MS,
33 env: Argv.GIT_CHILD_ENV,
34 ...(pinned && { cwd: pinned.toplevel }),
35 },
36 )
37 } catch {
38 return FAILED_RUN
39 }
40 }
41
42 const repository = await Probes.repositoryOf(runOf(null))
43
44 if (!repository) {
45 return null
46 }
47
48 const run = runOf(repository)
49
50 const depsOf = (): Types.GitDeps => ({
51 run,
52 repository,
53 mtimeOf: host.mtimeOf,
54 entryKindsOf: host.entryKindsOf,
55 sessionStartMs: host.sessionStartMsOf(),
56 onBranchBase: host.onBranchBase,
57 })
58
59 return {
60 repository,
61 baseModes: GIT_BASE_MODES,
62 words: GIT_WORDS,
63 fetchDiff: mode => fetchDiff(depsOf(), mode),
64 fetchFileHunks: (data, file) => fetchFileHunks(run, data, file),
65 headKeyOf: () =>
66 Probes.headKeyOf({ ...depsOf(), readFile: host.readFile }, repository),
67 }
68}