Skip to content

Commit b1ee4ef

Browse files
committed
getPullRequestEditedDiffRanges: compute diff ranges
1 parent bab5cb0 commit b1ee4ef

File tree

1 file changed

+80
-2
lines changed

1 file changed

+80
-2
lines changed

src/analyze.ts

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,19 @@ async function getPullRequestEditedDiffRanges(
294294
headLabel: string,
295295
logger: Logger,
296296
): Promise<DiffThunkRange[] | undefined> {
297-
await getFileDiffsWithBasehead(baseRef, headLabel, logger);
298-
return undefined;
297+
const filediffs = await getFileDiffsWithBasehead(baseRef, headLabel, logger);
298+
if (filediffs === undefined) {
299+
return undefined;
300+
}
301+
const results: DiffThunkRange[] = [];
302+
for (const filediff of filediffs) {
303+
const diffRanges = getDiffRanges(filediff, logger);
304+
if (diffRanges === undefined) {
305+
return undefined;
306+
}
307+
results.push(...diffRanges);
308+
}
309+
return results;
299310
}
300311

301312
/**
@@ -346,6 +357,73 @@ async function getFileDiffsWithBasehead(
346357
}
347358
}
348359

360+
function getDiffRanges(
361+
fileDiff: FileDiff,
362+
logger: Logger,
363+
): DiffThunkRange[] | undefined {
364+
if (fileDiff.patch === undefined) {
365+
return undefined;
366+
}
367+
368+
// Diff-informed queries expect the file path to be absolute.
369+
const filename = path.join(
370+
actionsUtil.getRequiredInput("checkout_path"),
371+
fileDiff.filename,
372+
);
373+
374+
let currentLine = 0;
375+
let additionRangeStartLine: number | undefined = undefined;
376+
const diffRanges: DiffThunkRange[] = [];
377+
378+
const lines = fileDiff.patch.split("\n");
379+
// Adding a fake context line at the end ensures that the following loop will
380+
// always terminate the last range of added lines.
381+
lines.push(" ");
382+
383+
for (const line of lines) {
384+
if (line.startsWith("-")) {
385+
// Ignore deletions completely -- we do not even want to consider them when
386+
// calculating consecutive ranges of added lines.
387+
continue;
388+
}
389+
if (line.startsWith("+")) {
390+
if (additionRangeStartLine === undefined) {
391+
additionRangeStartLine = currentLine;
392+
}
393+
currentLine++;
394+
continue;
395+
}
396+
if (additionRangeStartLine !== undefined) {
397+
// Any line that does not start with a "+" or "-" terminates the current
398+
// range of added lines.
399+
diffRanges.push({
400+
path: filename,
401+
startLine: additionRangeStartLine,
402+
endLine: currentLine - 1,
403+
});
404+
additionRangeStartLine = undefined;
405+
}
406+
if (line.startsWith("@@ ")) {
407+
// A new hunk header line resets the current line number.
408+
const match = line.match(/^@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@/);
409+
if (match === null) {
410+
logger.warning(
411+
`Cannot parse diff hunk header for ${fileDiff.filename}: ${line}`,
412+
);
413+
return undefined;
414+
}
415+
currentLine = parseInt(match[1], 10);
416+
continue;
417+
}
418+
if (line.startsWith(" ")) {
419+
// An unchanged context line advances the current line number.
420+
currentLine++;
421+
continue;
422+
}
423+
}
424+
return diffRanges;
425+
}
426+
349427
/**
350428
* Create an extension pack in the temporary directory that contains the file
351429
* line ranges that were added or modified in the pull request.

0 commit comments

Comments
 (0)