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