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 Limits from '../../limits' |
| 2 | import { cutHunks } from '../cut-hunks' |
| 3 | import { hunkOf } from '../hunk-of' |
| 4 | import { LARGE_FILE_HUNKS } from '../large-file-hunks' |
| 5 | import type Types from '../types' |
| 6 | import { HUNK_HEADER } from './hunk-header' |
| 7 | import { 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 | */ |
| 19 | export 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 | const headerRows = rows.flatMap((row, at) => |
| 26 | HUNK_HEADER.test(row) ? [at] : [], |
| 27 | ) |
| 28 | |
| 29 | return { |
| 30 | ...cutHunks( |
| 31 | headerRows.map((at, ordinal) => |
| 32 | hunkOf( |
| 33 | HUNK_HEADER.exec(rows[at] ?? '')?.groups, |
| 34 | rows.slice(at + 1, headerRows[ordinal + 1]).filter(isBodyLine), |
| 35 | ), |
| 36 | ), |
| 37 | ), |
| 38 | isLarge: false, |
| 39 | } |
| 40 | } |