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 / fetch-file-hunks / fetch-file-hunks.ts
41 lines · 1.2 KBRaw
1import Argv from '../argv'
2import { EMPTY_FILE_HUNKS } from '../empty-file-hunks'
3import Parse from '../parse'
4import type Types from '../types'
5
6/**
7 * One file's hunks against the base its row was read against, so body and
8 * counts agree, by literal pathspec, as the built-in panel reads hunks.
9 *
10 * A rename passes both its paths so git pairs them; an untracked or binary
11 * row, or on an unborn HEAD a file edited after staging, has no honest
12 * body and answers no hunks.
13 *
14 * @param run runs git against the pinned repository
15 * @param data the fetch the row belongs to
16 * @param file the row
17 * @returns the parsed body, or null when git failed
18 */
19export async function fetchFileHunks(
20 run: Types.GitRun,
21 data: Types.DiffData,
22 file: Types.FileStat,
23): Promise<Types.FileHunks | null> {
24 const hasNoBody =
25 file.isUntracked || file.isBinary || data.stalePaths.includes(file.path)
26
27 if (hasNoBody) {
28 return EMPTY_FILE_HUNKS
29 }
30
31 const { exitCode, stdout } = await run([
32 '--literal-pathspecs',
33 ...Argv.DIFF_LEADING_ARGS,
34 data.baseRef,
35 '--',
36 ...(file.renamedFrom === null ? [] : [file.renamedFrom]),
37 file.path,
38 ])
39
40 return exitCode === 0 ? Parse.parseFileDiff(stdout) : null
41}