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
| 1 | import Argv from '../argv' |
| 2 | import { EMPTY_FILE_HUNKS } from '../empty-file-hunks' |
| 3 | import Parse from '../parse' |
| 4 | import type Types from '../types' |
| 5 | import { isLastOfDiff } from './is-last-of-diff' |
| 6 | import { 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 | */ |
| 21 | export 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 | } |