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 / tests / fixtures / git-in.ts
39 lines · 1.3 KBRaw
1import type { ProcessRunResult } from 'claude-code'
2
3import { NOT_A_REPOSITORY } from './not-a-repository.js'
4import { REPOSITORY } from './repository.js'
5import Scripted from './scripted'
6
7/**
8 * What git answers from a script of outputs by command-line key, REPOSITORY
9 * when none is given.
10 *
11 * A hunks read (`--raw`) is answered for the paths it names from the bodies
12 * the script holds under `-- <path>`; an invocation the script does not
13 * know fails as git does outside a repository.
14 *
15 * @param argv the invocation, program first
16 * @param script git's output for each invocation whose line holds the key
17 * @returns git's exit code and output
18 */
19export function gitIn(
20 argv: readonly string[],
21 script: Readonly<Record<string, string>> = REPOSITORY,
22): ProcessRunResult {
23 const line = argv.join(' ')
24 const found = Object.entries(script).find(([key]) => line.includes(key))
25 const isHunksRead = argv.includes('--raw')
26
27 const stdout = isHunksRead
28 ? Scripted.hunksAnswerOf(
29 argv
30 .slice(argv.indexOf('--') + 1)
31 .filter(path => Object.hasOwn(script, `-- ${path}`))
32 .map(path => [path, script[`-- ${path}`] ?? '']),
33 )
34 : found?.[1]
35
36 const isKnown = stdout !== undefined
37
38 return isKnown ? { exitCode: 0, stdout, stderr: '' } : NOT_A_REPOSITORY
39}