Files
openchamber/packages/ui/src/components/views/diffPatchUtils.ts
T
Bohdan Triapitsyn d8f0ef074b fix: compute first changed diff line from patch hunks
Uses hunk contents to find the first modified line instead of the hunk start
Handles added, removed, and binary-only patches more accurately
Adds tests for patch parsing edge cases
2026-07-06 01:26:56 +03:00

37 lines
801 B
TypeScript

export const getFirstChangedModifiedLineFromPatch = (patch: string): number | null => {
if (!patch) {
return null;
}
const lines = patch.split('\n');
let modifiedLine: number | null = null;
for (const line of lines) {
const hunkMatch = line.match(/^@@\s*-\d+(?:,\d+)?\s+\+(\d+)(?:,\d+)?\s*@@/);
if (hunkMatch) {
const parsed = Number.parseInt(hunkMatch[1] ?? '', 10);
modifiedLine = Number.isFinite(parsed) && parsed >= 1 ? parsed : null;
continue;
}
if (modifiedLine === null) {
continue;
}
if (line.startsWith(' ')) {
modifiedLine += 1;
continue;
}
if (line.startsWith('+')) {
return modifiedLine;
}
if (line.startsWith('-')) {
return Math.max(1, modifiedLine);
}
}
return null;
};