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 / parse / parse-file-diff.ts
45 lines · 1.3 KBRaw
1import Limits from '../../limits'
2import { cutHunks } from '../cut-hunks'
3import { hunkOf } from '../hunk-of'
4import { LARGE_FILE_HUNKS } from '../large-file-hunks'
5import type Types from '../types'
6import { bodyLineOf } from './body-line-of'
7import { HUNK_HEADER } from './hunk-header'
8import { isBodyLine } from './is-body-line'
9
10/**
11 * One file's unified `git diff` output as hunks, the body cut at
12 * MAX_LINES_PER_FILE lines in all, as the built-in panel parses one file.
13 *
14 * Past MAX_DIFF_BYTES there are no hunks and `isLarge` is set; everything
15 * before the first hunk header is the file's header.
16 *
17 * @param stdout the command's standard output for that one path
18 * @returns the parsed body
19 */
20export function parseFileDiff(stdout: string): Types.FileHunks {
21 if (stdout.length > Limits.MAX_DIFF_BYTES) {
22 return LARGE_FILE_HUNKS
23 }
24
25 const rows = stdout.split('\n')
26
27 const headerRows = rows.flatMap((row, at) =>
28 HUNK_HEADER.test(row) ? [at] : [],
29 )
30
31 return {
32 ...cutHunks(
33 headerRows.map((at, ordinal) =>
34 hunkOf(
35 HUNK_HEADER.exec(rows[at] ?? '')?.groups,
36 rows
37 .slice(at + 1, headerRows[ordinal + 1])
38 .filter(isBodyLine)
39 .map(bodyLineOf),
40 ),
41 ),
42 ),
43 isLarge: false,
44 }
45}