MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / parseGitDiff

Function parseGitDiff

source code/utils/gitDiff.ts:202–300  ·  view source on GitHub ↗
(
  stdout: string,
)

Source from the content-addressed store, hash-verified

200 * - Files ≤1MB: parsed but limited to MAX_LINES_PER_FILE lines
201 */
202export function parseGitDiff(
203 stdout: string,
204): Map<string, StructuredPatchHunk[]> {
205 const result = new Map<string, StructuredPatchHunk[]>()
206 if (!stdout.trim()) return result
207
208 // Split by file diffs
209 const fileDiffs = stdout.split(/^diff --git /m).filter(Boolean)
210
211 for (const fileDiff of fileDiffs) {
212 // Stop after MAX_FILES
213 if (result.size >= MAX_FILES) break
214
215 // Skip files larger than 1MB
216 if (fileDiff.length > MAX_DIFF_SIZE_BYTES) {
217 continue
218 }
219
220 const lines = fileDiff.split('\n')
221
222 // Extract filename from first line: "a/path/to/file b/path/to/file"
223 const headerMatch = lines[0]?.match(/^a\/(.+?) b\/(.+)$/)
224 if (!headerMatch) continue
225 const filePath = headerMatch[2] ?? headerMatch[1] ?? ''
226
227 // Find and parse hunks
228 const fileHunks: StructuredPatchHunk[] = []
229 let currentHunk: StructuredPatchHunk | null = null
230 let lineCount = 0
231
232 for (let i = 1; i < lines.length; i++) {
233 const line = lines[i] ?? ''
234
235 // StructuredPatchHunk header: @@ -oldStart,oldLines +newStart,newLines @@
236 const hunkMatch = line.match(
237 /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/,
238 )
239 if (hunkMatch) {
240 if (currentHunk) {
241 fileHunks.push(currentHunk)
242 }
243 currentHunk = {
244 oldStart: parseInt(hunkMatch[1] ?? '0', 10),
245 oldLines: parseInt(hunkMatch[2] ?? '1', 10),
246 newStart: parseInt(hunkMatch[3] ?? '0', 10),
247 newLines: parseInt(hunkMatch[4] ?? '1', 10),
248 lines: [],
249 }
250 continue
251 }
252
253 // Skip binary file markers and other metadata
254 if (
255 line.startsWith('index ') ||
256 line.startsWith('---') ||
257 line.startsWith('+++') ||
258 line.startsWith('new file') ||
259 line.startsWith('deleted file') ||

Callers 1

fetchGitDiffHunksFunction · 0.85

Calls 1

setMethod · 0.80

Tested by

no test coverage detected