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