mirror of
https://github.com/stablyai/orca.git
synced 2026-10-27 16:02:42 +00:00
46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
const HUNK_HEADER_RE = /^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@/
|
|
|
|
export function getPRReviewCommentLineNumbersFromPatch(patch: string | undefined): number[] {
|
|
if (!patch) {
|
|
return []
|
|
}
|
|
|
|
const lineNumbers: number[] = []
|
|
let nextModifiedLine: number | null = null
|
|
|
|
for (const line of patch.split('\n')) {
|
|
const hunk = HUNK_HEADER_RE.exec(line)
|
|
if (hunk) {
|
|
const start = Number(hunk[1])
|
|
const count = hunk[2] === undefined ? 1 : Number(hunk[2])
|
|
nextModifiedLine = Number.isInteger(start) && count > 0 ? start : null
|
|
continue
|
|
}
|
|
|
|
if (nextModifiedLine === null || line.startsWith('\\')) {
|
|
continue
|
|
}
|
|
|
|
// Inside a hunk every `+` line is added content. GitHub's per-file patch keeps the
|
|
// `+++ b/file` header before the first @@, so a `+++…` line here is `++…` content,
|
|
// not a header, and must stay comment-eligible.
|
|
if (line.startsWith('+')) {
|
|
lineNumbers.push(nextModifiedLine)
|
|
nextModifiedLine++
|
|
continue
|
|
}
|
|
|
|
if (line.startsWith(' ')) {
|
|
lineNumbers.push(nextModifiedLine)
|
|
nextModifiedLine++
|
|
continue
|
|
}
|
|
|
|
if (!line.startsWith('-')) {
|
|
nextModifiedLine++
|
|
}
|
|
}
|
|
|
|
return lineNumbers
|
|
}
|