@in-the-loop-labs/pair-review 5.3.4 → 5.3.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -1
- package/bin/git-diff-lines +2 -1
- package/package.json +1 -1
- package/plugin/.claude-plugin/plugin.json +1 -1
- package/plugin-code-critic/.claude-plugin/plugin.json +1 -1
- package/plugin-code-critic/skills/analyze/scripts/git-diff-lines +104 -25
- package/plugin-pair-loop/.claude-plugin/plugin.json +1 -1
- package/public/css/pr.css +162 -46
- package/public/css/styles.css +0 -12
- package/public/js/components/AIPanel.js +3 -2
- package/public/js/components/AnalysisConfigModal.js +4 -1
- package/public/js/components/ChatPanel.js +15 -8
- package/public/js/components/PanelGroup.js +115 -21
- package/public/js/components/SuggestionNavigator.js +16 -3
- package/public/js/local.js +7 -12
- package/public/js/modules/diff-renderer.js +13 -0
- package/public/js/modules/file-comment-manager.js +19 -10
- package/public/js/modules/hunk-parser.js +39 -2
- package/public/js/modules/panel-resizer.js +41 -3
- package/public/js/modules/pierre-bridge.js +11 -2
- package/public/js/modules/suggestion-manager.js +128 -33
- package/public/js/pr.js +132 -29
- package/public/js/repo-settings.js +35 -19
- package/public/js/utils/git-paths.js +140 -0
- package/public/js/utils/smart-truncate.js +88 -0
- package/public/local.html +6 -0
- package/public/pr.html +6 -0
- package/src/ai/analyzer.js +15 -13
- package/src/ai/antigravity-provider.js +44 -24
- package/src/ai/cursor-agent-provider.js +4 -4
- package/src/ai/muse-provider.js +68 -3
- package/src/ai/summary-generator.js +8 -5
- package/src/git/diff-flags.js +26 -12
- package/src/git/worktree.js +21 -43
- package/src/github/client.js +0 -88
- package/src/github/impl/graphql/pending-review-comments.js +5 -1
- package/src/github/submit-pr-context.js +56 -0
- package/src/local-review.js +34 -17
- package/src/main.js +80 -106
- package/src/routes/executable-analysis.js +14 -15
- package/src/routes/local.js +3 -5
- package/src/routes/pr.js +18 -105
- package/src/setup/pr-setup.js +1 -1
- package/src/setup/stack-setup.js +1 -1
- package/src/utils/diff-annotator.js +175 -25
- package/src/utils/diff-file-content.js +4 -26
- package/src/utils/diff-file-list.js +178 -42
- package/src/utils/git-paths.js +77 -0
- package/src/utils/paths.js +1 -25
|
@@ -3,6 +3,9 @@
|
|
|
3
3
|
* Diff annotation utility for adding explicit line numbers to unified diffs
|
|
4
4
|
* Provides two-column format showing both OLD (base) and NEW (head) line numbers
|
|
5
5
|
*/
|
|
6
|
+
const { parseDiffGitPaths, decodeDiffLinePath } = require('./git-paths');
|
|
7
|
+
|
|
8
|
+
const logger = require('./logger');
|
|
6
9
|
|
|
7
10
|
/**
|
|
8
11
|
* Parse a hunk header to extract line number information
|
|
@@ -12,8 +15,12 @@
|
|
|
12
15
|
*/
|
|
13
16
|
function parseHunkHeader(header) {
|
|
14
17
|
// Match: @@ -oldStart,oldCount +newStart,newCount @@ optional context
|
|
15
|
-
// Count can be omitted, defaulting to 1
|
|
16
|
-
|
|
18
|
+
// Count can be omitted, defaulting to 1. The `s` (dotAll) flag lets the
|
|
19
|
+
// trailing context span a carriage return: git trims trailing whitespace
|
|
20
|
+
// from the function-context line but keeps a lone `\r` inside it, and
|
|
21
|
+
// without dotAll `.` stops at that `\r`, `$` cannot match, and the whole
|
|
22
|
+
// header is rejected, so its hunk body goes uncounted.
|
|
23
|
+
const match = header.match(/^@@\s+-(\d+)(?:,(\d+))?\s+\+(\d+)(?:,(\d+))?\s+@@(.*)$/s);
|
|
17
24
|
|
|
18
25
|
if (!match) {
|
|
19
26
|
return null;
|
|
@@ -73,6 +80,13 @@ function getLineContent(line) {
|
|
|
73
80
|
|
|
74
81
|
/**
|
|
75
82
|
* Parse file header lines to extract old and new file paths
|
|
83
|
+
*
|
|
84
|
+
* CALLER CONTRACT: only call this on lines that are OUTSIDE a hunk body. It
|
|
85
|
+
* returns `true` for anything merely starting with `---`, `+++`, `index ` or
|
|
86
|
+
* `diff --git`, and those prefixes are ordinary CONTENT inside a hunk (a
|
|
87
|
+
* deleted `-- x` is emitted as `--- x`, an added `++ x` as `+++ x`). Both
|
|
88
|
+
* loops in this file enforce that with an `!inHunk` guard.
|
|
89
|
+
*
|
|
76
90
|
* @param {string} line - A diff line that might be a file header
|
|
77
91
|
* @param {Object} currentFile - Current file info to update
|
|
78
92
|
* @returns {boolean} Whether this was a file header line
|
|
@@ -80,28 +94,30 @@ function getLineContent(line) {
|
|
|
80
94
|
function parseFileHeader(line, currentFile) {
|
|
81
95
|
// Handle diff --git header
|
|
82
96
|
if (line.startsWith('diff --git')) {
|
|
83
|
-
const
|
|
84
|
-
if (
|
|
85
|
-
currentFile.oldPath =
|
|
86
|
-
currentFile.newPath =
|
|
97
|
+
const paths = parseDiffGitPaths(line);
|
|
98
|
+
if (paths) {
|
|
99
|
+
currentFile.oldPath = paths.oldPath;
|
|
100
|
+
currentFile.newPath = paths.newPath;
|
|
87
101
|
}
|
|
88
102
|
return true;
|
|
89
103
|
}
|
|
90
104
|
|
|
91
105
|
// Handle --- header
|
|
92
106
|
if (line.startsWith('---')) {
|
|
93
|
-
const match = line.match(/^---\s+(
|
|
94
|
-
|
|
95
|
-
|
|
107
|
+
const match = line.match(/^---\s+(.+)$/);
|
|
108
|
+
const oldPath = match ? decodeDiffLinePath(match[1], 'a/') : null;
|
|
109
|
+
if (oldPath !== null) {
|
|
110
|
+
currentFile.oldPath = oldPath;
|
|
96
111
|
}
|
|
97
112
|
return true;
|
|
98
113
|
}
|
|
99
114
|
|
|
100
115
|
// Handle +++ header
|
|
101
116
|
if (line.startsWith('+++')) {
|
|
102
|
-
const match = line.match(/^\+\+\+\s+(
|
|
103
|
-
|
|
104
|
-
|
|
117
|
+
const match = line.match(/^\+\+\+\s+(.+)$/);
|
|
118
|
+
const newPath = match ? decodeDiffLinePath(match[1], 'b/') : null;
|
|
119
|
+
if (newPath !== null) {
|
|
120
|
+
currentFile.newPath = newPath;
|
|
105
121
|
}
|
|
106
122
|
return true;
|
|
107
123
|
}
|
|
@@ -137,7 +153,7 @@ function parseFileHeader(line, currentFile) {
|
|
|
137
153
|
if (line.startsWith('rename from')) {
|
|
138
154
|
const match = line.match(/^rename from (.+)$/);
|
|
139
155
|
if (match) {
|
|
140
|
-
currentFile.renamedFrom = match[1];
|
|
156
|
+
currentFile.renamedFrom = decodeDiffLinePath(match[1]);
|
|
141
157
|
}
|
|
142
158
|
return true;
|
|
143
159
|
}
|
|
@@ -145,7 +161,7 @@ function parseFileHeader(line, currentFile) {
|
|
|
145
161
|
if (line.startsWith('rename to')) {
|
|
146
162
|
const match = line.match(/^rename to (.+)$/);
|
|
147
163
|
if (match) {
|
|
148
|
-
currentFile.renamedTo = match[1];
|
|
164
|
+
currentFile.renamedTo = decodeDiffLinePath(match[1]);
|
|
149
165
|
}
|
|
150
166
|
return true;
|
|
151
167
|
}
|
|
@@ -218,8 +234,13 @@ function annotateDiff(rawDiff) {
|
|
|
218
234
|
continue;
|
|
219
235
|
}
|
|
220
236
|
|
|
221
|
-
// Parse file header lines
|
|
222
|
-
|
|
237
|
+
// Parse file header lines. Guarded on `!inHunk`: inside a hunk body these
|
|
238
|
+
// prefixes are content, and letting `parseFileHeader` claim them both
|
|
239
|
+
// corrupts the file path (an added `++ x` line arrives as `+++ x`) and
|
|
240
|
+
// skips the counter-advancing branches below, shifting every later anchor
|
|
241
|
+
// in the file. `git diff` opens every file with `diff --git`, which resets
|
|
242
|
+
// `inHunk` above before that file's real headers arrive.
|
|
243
|
+
if (!inHunk && parseFileHeader(line, currentFile)) {
|
|
223
244
|
// Check if we just detected a binary file that needs output
|
|
224
245
|
if (currentFile.isBinary && fileStarted && !fileHeaderOutput) {
|
|
225
246
|
let filePath = currentFile.newPath || currentFile.oldPath || 'unknown';
|
|
@@ -410,15 +431,35 @@ function parseAnnotatedDiff(annotatedDiff) {
|
|
|
410
431
|
* Used to determine whether a comment targets a line GitHub can render inline
|
|
411
432
|
* (inside a hunk) vs. one that must be submitted as file-level.
|
|
412
433
|
*
|
|
434
|
+
* File headers are only headers OUTSIDE a hunk body. `parseFileHeader`
|
|
435
|
+
* returns `true` for anything starting with `---`, `+++`, `index ` or
|
|
436
|
+
* `diff --git`, and inside a hunk those are content: a deleted `-- x` line is
|
|
437
|
+
* emitted as `--- x`, an added `++ x` as `+++ x`, and an unindented `++i;` as
|
|
438
|
+
* `+++i;`. Letting the header parser claim them overwrote the section's path
|
|
439
|
+
* with a fragment of source code AND skipped the counter-advancing branch,
|
|
440
|
+
* shifting every later RIGHT anchor in the file by one. The `!inHunk` guard is
|
|
441
|
+
* what makes those lines fall through to the ordinary `+`/`-` accounting where
|
|
442
|
+
* they belong.
|
|
443
|
+
*
|
|
444
|
+
* Each line also records the hunk it belongs to, so a caller can tell
|
|
445
|
+
* whether two lines share a hunk: GitHub only accepts a multi-line comment
|
|
446
|
+
* whose two ends are in the same hunk.
|
|
447
|
+
*
|
|
413
448
|
* @param {string} rawDiff - Raw unified diff from git
|
|
414
|
-
* @returns {{
|
|
449
|
+
* @returns {{
|
|
450
|
+
* isLineInDiff: (file: string, line: number, side?: string) => boolean,
|
|
451
|
+
* hunkOf: (file: string, line: number, side?: string) => number|null
|
|
452
|
+
* }} `hunkOf` returns an index that is unique per hunk across the whole
|
|
453
|
+
* diff, or null for a line outside every hunk.
|
|
415
454
|
*/
|
|
416
455
|
function buildDiffLineSet(rawDiff) {
|
|
417
456
|
if (!rawDiff || rawDiff.trim() === '') {
|
|
418
|
-
return { isLineInDiff: () => false };
|
|
457
|
+
return { isLineInDiff: () => false, hunkOf: () => null };
|
|
419
458
|
}
|
|
420
459
|
|
|
421
|
-
|
|
460
|
+
// `${file}:${side}:${line}` -> index of the hunk that contains the line
|
|
461
|
+
const entries = new Map();
|
|
462
|
+
let hunkIndex = -1;
|
|
422
463
|
const diffLines = rawDiff.split('\n');
|
|
423
464
|
// Trim trailing empty element produced by split('\n') on newline-terminated input.
|
|
424
465
|
// Without this, the empty string matches the context-line branch and adds phantom
|
|
@@ -437,7 +478,9 @@ function buildDiffLineSet(rawDiff) {
|
|
|
437
478
|
continue;
|
|
438
479
|
}
|
|
439
480
|
|
|
440
|
-
|
|
481
|
+
// Header lines are headers only outside a hunk body — see the docblock.
|
|
482
|
+
// Inside one they fall through to the +/- accounting below.
|
|
483
|
+
if (!inHunk && parseFileHeader(line, currentFile)) {
|
|
441
484
|
continue;
|
|
442
485
|
}
|
|
443
486
|
|
|
@@ -446,6 +489,7 @@ function buildDiffLineSet(rawDiff) {
|
|
|
446
489
|
oldLineNum = hunkInfo.oldStart;
|
|
447
490
|
newLineNum = hunkInfo.newStart;
|
|
448
491
|
inHunk = true;
|
|
492
|
+
hunkIndex++;
|
|
449
493
|
continue;
|
|
450
494
|
}
|
|
451
495
|
|
|
@@ -457,14 +501,14 @@ function buildDiffLineSet(rawDiff) {
|
|
|
457
501
|
if (!filePath) continue;
|
|
458
502
|
|
|
459
503
|
if (line.startsWith('+')) {
|
|
460
|
-
entries.
|
|
504
|
+
entries.set(`${filePath}:RIGHT:${newLineNum}`, hunkIndex);
|
|
461
505
|
newLineNum++;
|
|
462
506
|
} else if (line.startsWith('-')) {
|
|
463
|
-
entries.
|
|
507
|
+
entries.set(`${filePath}:LEFT:${oldLineNum}`, hunkIndex);
|
|
464
508
|
oldLineNum++;
|
|
465
509
|
} else if (line.startsWith(' ') || line === '') {
|
|
466
|
-
entries.
|
|
467
|
-
entries.
|
|
510
|
+
entries.set(`${filePath}:LEFT:${oldLineNum}`, hunkIndex);
|
|
511
|
+
entries.set(`${filePath}:RIGHT:${newLineNum}`, hunkIndex);
|
|
468
512
|
oldLineNum++;
|
|
469
513
|
newLineNum++;
|
|
470
514
|
}
|
|
@@ -473,10 +517,115 @@ function buildDiffLineSet(rawDiff) {
|
|
|
473
517
|
return {
|
|
474
518
|
isLineInDiff(file, lineNum, side = 'RIGHT') {
|
|
475
519
|
return entries.has(`${file}:${side}:${lineNum}`);
|
|
520
|
+
},
|
|
521
|
+
hunkOf(file, lineNum, side = 'RIGHT') {
|
|
522
|
+
const hunk = entries.get(`${file}:${side}:${lineNum}`);
|
|
523
|
+
return hunk === undefined ? null : hunk;
|
|
476
524
|
}
|
|
477
525
|
};
|
|
478
526
|
}
|
|
479
527
|
|
|
528
|
+
/**
|
|
529
|
+
* Map stored comment rows to the GitHub review comments that a review
|
|
530
|
+
* submission sends. The one mapping for both submit paths: the web submit
|
|
531
|
+
* route (src/routes/pr.js) and headless --ai-draft / --ai-review
|
|
532
|
+
* (src/main.js). Both GitHub transports (GraphQL and the host REST
|
|
533
|
+
* extension) accept the result.
|
|
534
|
+
*
|
|
535
|
+
* - `is_file_level = 1` rows become file-level comments.
|
|
536
|
+
* - A range written backwards (`line_end < line_start`) is the same span,
|
|
537
|
+
* so its ends are put in ascending order first.
|
|
538
|
+
* - A range becomes a multi-line comment (`start_line`, `start_side`, `line`)
|
|
539
|
+
* only when both ends are inside the diff, on the comment side, in the
|
|
540
|
+
* SAME hunk. GitHub rejects a range that crosses hunks, and one rejected
|
|
541
|
+
* comment discards the whole pending review.
|
|
542
|
+
* - Otherwise, a single line or a range whose `line_start` is inside a hunk
|
|
543
|
+
* becomes a single-line comment on `line_start` (the line the finding is
|
|
544
|
+
* anchored on).
|
|
545
|
+
* - Anything else (e.g. expanded context) cannot be inline, so it becomes a
|
|
546
|
+
* file-level comment whose body starts with `(Ref Line N)` or
|
|
547
|
+
* `(Ref Lines N-M)`.
|
|
548
|
+
*
|
|
549
|
+
* @param {Array<Object>|null} rows - Comment rows: file, line_start,
|
|
550
|
+
* line_end, side, body, is_file_level
|
|
551
|
+
* @param {string} diffContent - Raw unified diff used for the hunk check
|
|
552
|
+
* @param {Object} [options]
|
|
553
|
+
* @param {(row: Object) => string} [options.formatBody] - Comment text for a
|
|
554
|
+
* row, before any line reference prefix. Defaults to `row.body`.
|
|
555
|
+
* @returns {Array<{path: string, body: string, isFileLevel: boolean,
|
|
556
|
+
* line?: number, side?: string, start_line?: number, start_side?: string}>}
|
|
557
|
+
*/
|
|
558
|
+
function buildReviewComments(rows, diffContent, { formatBody = row => row.body } = {}) {
|
|
559
|
+
const diffLineSet = buildDiffLineSet(diffContent);
|
|
560
|
+
|
|
561
|
+
return (rows || []).map(comment => {
|
|
562
|
+
const side = comment.side || 'RIGHT';
|
|
563
|
+
const body = formatBody(comment);
|
|
564
|
+
|
|
565
|
+
// Explicit file-level comment: about the whole file, not tied to lines
|
|
566
|
+
if (comment.is_file_level === 1) {
|
|
567
|
+
logger.debug(`Formatting file-level comment: ${comment.file}`);
|
|
568
|
+
return {
|
|
569
|
+
path: comment.file,
|
|
570
|
+
body,
|
|
571
|
+
isFileLevel: true
|
|
572
|
+
};
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
// A backwards range is the same span; order it so start < end.
|
|
576
|
+
const isRange = comment.line_start != null && Boolean(comment.line_end) && comment.line_end !== comment.line_start;
|
|
577
|
+
const rangeStart = isRange ? Math.min(comment.line_start, comment.line_end) : comment.line_start;
|
|
578
|
+
const rangeEnd = isRange ? Math.max(comment.line_start, comment.line_end) : comment.line_start;
|
|
579
|
+
|
|
580
|
+
// Check whether the target lines actually appear in a diff hunk rather
|
|
581
|
+
// than relying on diff_position, which may be absent (e.g. comments
|
|
582
|
+
// created by the chat agent or headless AI findings).
|
|
583
|
+
if (isRange) {
|
|
584
|
+
const startHunk = diffLineSet.hunkOf(comment.file, rangeStart, side);
|
|
585
|
+
const endHunk = diffLineSet.hunkOf(comment.file, rangeEnd, side);
|
|
586
|
+
if (startHunk !== null && startHunk === endHunk) {
|
|
587
|
+
logger.debug(`Formatting range comment: ${comment.file}:${rangeStart}-${rangeEnd} side=${side}`);
|
|
588
|
+
return {
|
|
589
|
+
path: comment.file,
|
|
590
|
+
line: rangeEnd,
|
|
591
|
+
body,
|
|
592
|
+
side,
|
|
593
|
+
isFileLevel: false,
|
|
594
|
+
start_line: rangeStart,
|
|
595
|
+
start_side: side
|
|
596
|
+
};
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
// A single line, or a range GitHub cannot render as one thread: anchor
|
|
601
|
+
// on line_start when it is inside a hunk.
|
|
602
|
+
if (diffLineSet.isLineInDiff(comment.file, comment.line_start, side)) {
|
|
603
|
+
if (isRange) {
|
|
604
|
+
logger.debug(`Range ${comment.file}:${rangeStart}-${rangeEnd} side=${side} does not fit one hunk; anchoring on line ${comment.line_start}`);
|
|
605
|
+
} else {
|
|
606
|
+
logger.debug(`Formatting line comment: ${comment.file}:${comment.line_start} side=${side}`);
|
|
607
|
+
}
|
|
608
|
+
return {
|
|
609
|
+
path: comment.file,
|
|
610
|
+
line: comment.line_start,
|
|
611
|
+
body,
|
|
612
|
+
side,
|
|
613
|
+
isFileLevel: false
|
|
614
|
+
};
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
const lineRef = isRange
|
|
618
|
+
? `(Ref Lines ${rangeStart}-${rangeEnd})`
|
|
619
|
+
: `(Ref Line ${comment.line_start})`;
|
|
620
|
+
logger.debug(`Formatting file-level comment (expanded context): ${comment.file} ${lineRef}`);
|
|
621
|
+
return {
|
|
622
|
+
path: comment.file,
|
|
623
|
+
body: `${lineRef} ${body}`,
|
|
624
|
+
isFileLevel: true
|
|
625
|
+
};
|
|
626
|
+
});
|
|
627
|
+
}
|
|
628
|
+
|
|
480
629
|
module.exports = {
|
|
481
630
|
annotateDiff,
|
|
482
631
|
parseAnnotatedDiff,
|
|
@@ -484,5 +633,6 @@ module.exports = {
|
|
|
484
633
|
formatLineNum,
|
|
485
634
|
getLineMarker,
|
|
486
635
|
getLineContent,
|
|
487
|
-
buildDiffLineSet
|
|
636
|
+
buildDiffLineSet,
|
|
637
|
+
buildReviewComments
|
|
488
638
|
};
|
|
@@ -7,30 +7,7 @@
|
|
|
7
7
|
* rendered patch, even if cached PR metadata drifts.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
if (!headerLine.startsWith('diff --git ')) {
|
|
12
|
-
return null;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
const rest = headerLine.slice('diff --git '.length);
|
|
16
|
-
const quotedMatch = rest.match(/^"a\/(.+)" "b\/(.+)"$/);
|
|
17
|
-
if (quotedMatch) {
|
|
18
|
-
return {
|
|
19
|
-
oldPath: quotedMatch[1].replace(/\\"/g, '"'),
|
|
20
|
-
newPath: quotedMatch[2].replace(/\\"/g, '"')
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const plainMatch = rest.match(/^a\/(.+?) b\/(.+)$/);
|
|
25
|
-
if (!plainMatch) {
|
|
26
|
-
return null;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
return {
|
|
30
|
-
oldPath: plainMatch[1],
|
|
31
|
-
newPath: plainMatch[2]
|
|
32
|
-
};
|
|
33
|
-
}
|
|
10
|
+
const { parseDiffGitPaths, parsePatchPaths } = require('./git-paths');
|
|
34
11
|
|
|
35
12
|
function findFileBlobInfoInDiff(diffText, fileName) {
|
|
36
13
|
if (!diffText || !fileName) {
|
|
@@ -40,9 +17,10 @@ function findFileBlobInfoInDiff(diffText, fileName) {
|
|
|
40
17
|
const lines = diffText.split('\n');
|
|
41
18
|
let matchedPaths = null;
|
|
42
19
|
|
|
43
|
-
for (
|
|
20
|
+
for (let i = 0; i < lines.length; i++) {
|
|
21
|
+
const line = lines[i];
|
|
44
22
|
if (line.startsWith('diff --git ')) {
|
|
45
|
-
const paths =
|
|
23
|
+
const paths = parsePatchPaths(lines, i);
|
|
46
24
|
matchedPaths = paths && (paths.oldPath === fileName || paths.newPath === fileName)
|
|
47
25
|
? paths
|
|
48
26
|
: null;
|
|
@@ -2,68 +2,219 @@
|
|
|
2
2
|
const { promisify } = require('util');
|
|
3
3
|
const { exec } = require('child_process');
|
|
4
4
|
const { queryOne } = require('../database');
|
|
5
|
-
const { GIT_DIFF_FLAGS } = require('../git/diff-flags');
|
|
5
|
+
const { GIT_DIFF_FLAGS, GIT_PATH_CONFIG } = require('../git/diff-flags');
|
|
6
6
|
const { normalizePath, resolveRenamedFile } = require('./paths');
|
|
7
|
+
const { parseHunkHeader } = require('./diff-annotator');
|
|
8
|
+
const { decodeGitPath, parsePatchPaths, splitNulPaths } = require('./git-paths');
|
|
7
9
|
|
|
8
10
|
const execPromise = promisify(exec);
|
|
9
11
|
|
|
10
12
|
/**
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
+
* Split a unified diff into its per-file patches, in order, keyed by the
|
|
14
|
+
* decoded new path: the `rename to` / `copy to` line when there is one,
|
|
15
|
+
* otherwise the `diff --git` header's "b/" side (see parsePatchPaths in
|
|
16
|
+
* public/js/utils/git-paths.js).
|
|
17
|
+
*
|
|
18
|
+
* A path can appear more than once: git prints a file <-> symlink type change
|
|
19
|
+
* as a deletion block followed by an addition block for the same path. Every
|
|
20
|
+
* block is kept, so callers that need the whole change can combine them.
|
|
13
21
|
*
|
|
14
22
|
* @param {string} diff - Full unified diff
|
|
15
|
-
* @returns {
|
|
23
|
+
* @returns {Array<[string, string]>} [file path, per-file patch] pairs
|
|
16
24
|
*/
|
|
17
|
-
function
|
|
18
|
-
const
|
|
19
|
-
if (!diff) return
|
|
25
|
+
function splitUnifiedDiffPatches(diff) {
|
|
26
|
+
const patches = [];
|
|
27
|
+
if (!diff) return patches;
|
|
20
28
|
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
for (const part of parts) {
|
|
29
|
+
for (const part of diff.split(/(?=^diff --git )/m)) {
|
|
24
30
|
if (!part.trim()) continue;
|
|
25
31
|
|
|
26
|
-
const
|
|
27
|
-
if (
|
|
28
|
-
|
|
32
|
+
const paths = parsePatchPaths(part);
|
|
33
|
+
if (paths) {
|
|
34
|
+
patches.push([paths.newPath, part]);
|
|
29
35
|
}
|
|
30
36
|
}
|
|
31
37
|
|
|
32
|
-
return
|
|
38
|
+
return patches;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Parse a unified diff into a map of file path -> per-file patch, keyed like
|
|
43
|
+
* splitUnifiedDiffPatches. When a path has more than one block (a type
|
|
44
|
+
* change), the map holds the last one, matching the browser's
|
|
45
|
+
* parseUnifiedDiff.
|
|
46
|
+
*
|
|
47
|
+
* @param {string} diff - Full unified diff
|
|
48
|
+
* @returns {Map<string, string>} Map of file paths to full patch text
|
|
49
|
+
*/
|
|
50
|
+
function parseUnifiedDiffPatches(diff) {
|
|
51
|
+
return new Map(splitUnifiedDiffPatches(diff));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The extended header of a per-file patch: everything before the first `@@`
|
|
56
|
+
* hunk header, where git prints the rename and binary markers.
|
|
57
|
+
*
|
|
58
|
+
* @param {string} patch - Per-file patch text
|
|
59
|
+
* @returns {string} Header text (the whole patch when it has no hunk)
|
|
60
|
+
*/
|
|
61
|
+
function patchHeader(patch) {
|
|
62
|
+
const hunkStart = patch.search(/^@@/m);
|
|
63
|
+
return hunkStart === -1 ? patch : patch.slice(0, hunkStart);
|
|
33
64
|
}
|
|
34
65
|
|
|
35
66
|
/**
|
|
36
67
|
* Count additions and deletions inside a single patch body.
|
|
37
68
|
*
|
|
69
|
+
* Only lines inside a hunk count: after an `@@` header and within the line
|
|
70
|
+
* counts it declares. A prefix test cannot tell headers from content — a
|
|
71
|
+
* deleted `-- x` line is emitted as `--- x` and an added `++ x` as `+++ x` —
|
|
72
|
+
* but hunk state can: `---`/`+++` are headers only outside a hunk (the same
|
|
73
|
+
* rule `buildDiffLineSet` in diff-annotator.js follows). Mirrored in the
|
|
74
|
+
* browser by `HunkParser.countPatchStats`; both suites share
|
|
75
|
+
* tests/utils/diff-stat-fixtures.js.
|
|
76
|
+
*
|
|
38
77
|
* @param {string} patch - Per-file patch text
|
|
39
78
|
* @returns {{ insertions: number, deletions: number }}
|
|
40
79
|
*/
|
|
41
80
|
function countPatchStats(patch) {
|
|
42
81
|
let insertions = 0;
|
|
43
82
|
let deletions = 0;
|
|
83
|
+
let oldRemaining = 0;
|
|
84
|
+
let newRemaining = 0;
|
|
44
85
|
|
|
45
|
-
for (const line of patch.split('\n')) {
|
|
46
|
-
|
|
86
|
+
for (const line of (patch || '').split('\n')) {
|
|
87
|
+
const hunk = parseHunkHeader(line);
|
|
88
|
+
if (hunk) {
|
|
89
|
+
oldRemaining = hunk.oldCount;
|
|
90
|
+
newRemaining = hunk.newCount;
|
|
91
|
+
} else if (line.startsWith('+') && newRemaining > 0) {
|
|
47
92
|
insertions++;
|
|
48
|
-
|
|
93
|
+
newRemaining--;
|
|
94
|
+
} else if (line.startsWith('-') && oldRemaining > 0) {
|
|
49
95
|
deletions++;
|
|
96
|
+
oldRemaining--;
|
|
97
|
+
} else if (line.startsWith(' ')) {
|
|
98
|
+
oldRemaining--;
|
|
99
|
+
newRemaining--;
|
|
50
100
|
}
|
|
51
101
|
}
|
|
52
102
|
|
|
53
103
|
return { insertions, deletions };
|
|
54
104
|
}
|
|
55
105
|
|
|
106
|
+
/**
|
|
107
|
+
* Describe one file of a unified diff as a `changed_files` entry.
|
|
108
|
+
*
|
|
109
|
+
* Renames come from the explicit `rename from` / `rename to` lines, so a
|
|
110
|
+
* rename is recorded whatever the two paths have in common, and a file
|
|
111
|
+
* literally named `a => b.md` is never mistaken for one.
|
|
112
|
+
*
|
|
113
|
+
* @param {string} filePath - Decoded new path (the parseUnifiedDiffPatches key)
|
|
114
|
+
* @param {string} patch - Per-file patch text
|
|
115
|
+
* @returns {{ file: string, insertions: number, deletions: number, changes: number,
|
|
116
|
+
* binary: boolean, renamed?: true, renamedFrom?: string }}
|
|
117
|
+
*/
|
|
118
|
+
function buildChangedFileEntry(filePath, patch) {
|
|
119
|
+
const { insertions, deletions } = countPatchStats(patch);
|
|
120
|
+
const header = patchHeader(patch);
|
|
121
|
+
const entry = {
|
|
122
|
+
file: filePath,
|
|
123
|
+
insertions,
|
|
124
|
+
deletions,
|
|
125
|
+
changes: insertions + deletions,
|
|
126
|
+
binary: /^Binary files .* differ$/m.test(header) || /^GIT binary patch$/m.test(header)
|
|
127
|
+
};
|
|
128
|
+
if (/^rename from /m.test(header) && /^rename to /m.test(header)) {
|
|
129
|
+
entry.renamed = true;
|
|
130
|
+
entry.renamedFrom = parsePatchPaths(patch).oldPath;
|
|
131
|
+
}
|
|
132
|
+
return entry;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Describe every file of a unified diff as a `changed_files` entry, one per
|
|
137
|
+
* path. A path with more than one block (git prints a file <-> symlink type
|
|
138
|
+
* change as a deletion followed by an addition) gets one entry whose counts
|
|
139
|
+
* are the sum of its blocks, so it reads +1/-1 like `--numstat`, not as a
|
|
140
|
+
* new file.
|
|
141
|
+
*
|
|
142
|
+
* @param {Array<[string, string]>} patches - From splitUnifiedDiffPatches
|
|
143
|
+
* @returns {Map<string, object>} File path -> entry, in diff order
|
|
144
|
+
*/
|
|
145
|
+
function buildMergedChangedFileEntries(patches) {
|
|
146
|
+
const entries = new Map();
|
|
147
|
+
for (const [filePath, patch] of patches) {
|
|
148
|
+
const entry = buildChangedFileEntry(filePath, patch);
|
|
149
|
+
const previous = entries.get(filePath);
|
|
150
|
+
if (!previous) {
|
|
151
|
+
entries.set(filePath, entry);
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
previous.insertions += entry.insertions;
|
|
155
|
+
previous.deletions += entry.deletions;
|
|
156
|
+
previous.changes += entry.changes;
|
|
157
|
+
previous.binary = previous.binary || entry.binary;
|
|
158
|
+
if (entry.renamed && !previous.renamed) {
|
|
159
|
+
previous.renamed = true;
|
|
160
|
+
previous.renamedFrom = entry.renamedFrom;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return entries;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Build PR `changed_files` from the unified diff: one entry per file, with
|
|
168
|
+
* path, rename, additions/deletions and binary flag read from the diff text
|
|
169
|
+
* and `generated` from .gitattributes.
|
|
170
|
+
*
|
|
171
|
+
* This is the only producer of PR changed_files, so every PR path (setup,
|
|
172
|
+
* refresh, headless, `?w=1`) spells each file exactly as the diff headers do.
|
|
173
|
+
*
|
|
174
|
+
* @param {string} diff - Full unified diff
|
|
175
|
+
* @param {{ isGenerated: (file: string) => boolean }|null} [gitattributes] - Generated-file matcher
|
|
176
|
+
* @returns {Array<object>} changed_files entries
|
|
177
|
+
*/
|
|
178
|
+
function buildChangedFileEntries(diff, gitattributes = null) {
|
|
179
|
+
return [...buildMergedChangedFileEntries(splitUnifiedDiffPatches(diff)).values()].map(entry => ({
|
|
180
|
+
...entry,
|
|
181
|
+
generated: gitattributes ? gitattributes.isGenerated(entry.file) : false
|
|
182
|
+
}));
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Re-spell a changed_files entry cached by a version that read paths from
|
|
187
|
+
* git's default C-quoted output (`"caf\303\251.js"`), so it matches the
|
|
188
|
+
* decoded diff headers until the review is refreshed. The decoded spelling is
|
|
189
|
+
* adopted only when the diff confirms it, so a real name that merely looks
|
|
190
|
+
* quoted is never mangled.
|
|
191
|
+
*
|
|
192
|
+
* @param {object|string} entry - changed_files entry
|
|
193
|
+
* @param {Map<string, string>} patchMap - From parseUnifiedDiffPatches
|
|
194
|
+
* @returns {object|string} Entry spelled like the diff headers
|
|
195
|
+
*/
|
|
196
|
+
function respellCachedEntry(entry, patchMap) {
|
|
197
|
+
const filePath = typeof entry === 'string' ? entry : entry.file;
|
|
198
|
+
if (patchMap.has(filePath)) return entry;
|
|
199
|
+
const decoded = decodeGitPath(filePath);
|
|
200
|
+
if (decoded === filePath || !patchMap.has(decoded)) return entry;
|
|
201
|
+
return typeof entry === 'string' ? decoded : { ...entry, file: decoded };
|
|
202
|
+
}
|
|
203
|
+
|
|
56
204
|
/**
|
|
57
205
|
* Merge changed_files metadata with the authoritative file list from the diff.
|
|
58
206
|
* This recovers files when cached changed_files were derived from abbreviated
|
|
59
|
-
* diff --stat output and no longer match the full patch headers
|
|
207
|
+
* diff --stat output and no longer match the full patch headers, and
|
|
208
|
+
* re-spells entries cached in git's quoted form so they match the decoded
|
|
209
|
+
* diff headers until the review is refreshed.
|
|
60
210
|
*
|
|
61
211
|
* @param {Array<object|string>} changedFiles - Existing changed_files array
|
|
62
212
|
* @param {string} diff - Full unified diff
|
|
63
213
|
* @returns {Array<object|string>} Merged changed_files array
|
|
64
214
|
*/
|
|
65
215
|
function mergeChangedFilesWithDiff(changedFiles, diff) {
|
|
66
|
-
const
|
|
216
|
+
const patches = splitUnifiedDiffPatches(diff);
|
|
217
|
+
const patchMap = new Map(patches);
|
|
67
218
|
if (patchMap.size === 0) {
|
|
68
219
|
return Array.isArray(changedFiles) ? changedFiles : [];
|
|
69
220
|
}
|
|
@@ -73,7 +224,7 @@ function mergeChangedFilesWithDiff(changedFiles, diff) {
|
|
|
73
224
|
const existing = (Array.isArray(changedFiles) ? changedFiles : []).filter(entry => {
|
|
74
225
|
const filePath = typeof entry === 'string' ? entry : entry?.file;
|
|
75
226
|
return filePath && !filePath.includes('...');
|
|
76
|
-
});
|
|
227
|
+
}).map(entry => respellCachedEntry(entry, patchMap));
|
|
77
228
|
|
|
78
229
|
const normalizedExisting = new Set(existing.map(file => {
|
|
79
230
|
const filePath = typeof file === 'string' ? file : file?.file;
|
|
@@ -82,26 +233,13 @@ function mergeChangedFilesWithDiff(changedFiles, diff) {
|
|
|
82
233
|
|
|
83
234
|
const merged = [...existing];
|
|
84
235
|
|
|
85
|
-
for (const [filePath,
|
|
236
|
+
for (const [filePath, entry] of buildMergedChangedFileEntries(patches)) {
|
|
86
237
|
const normalizedPatchPath = normalizePath(resolveRenamedFile(filePath));
|
|
87
238
|
if (normalizedExisting.has(normalizedPatchPath)) {
|
|
88
239
|
continue;
|
|
89
240
|
}
|
|
90
241
|
|
|
91
|
-
|
|
92
|
-
const renameFrom = patch.match(/^rename from (.+)$/m)?.[1] || null;
|
|
93
|
-
const renameTo = patch.match(/^rename to (.+)$/m)?.[1] || null;
|
|
94
|
-
const binary = /^Binary files .* differ$/m.test(patch) || /^GIT binary patch$/m.test(patch);
|
|
95
|
-
|
|
96
|
-
merged.push({
|
|
97
|
-
file: filePath,
|
|
98
|
-
insertions,
|
|
99
|
-
deletions,
|
|
100
|
-
changes: insertions + deletions,
|
|
101
|
-
binary,
|
|
102
|
-
renamed: Boolean(renameFrom && renameTo),
|
|
103
|
-
renamedFrom: renameFrom
|
|
104
|
-
});
|
|
242
|
+
merged.push(entry);
|
|
105
243
|
normalizedExisting.add(normalizedPatchPath);
|
|
106
244
|
}
|
|
107
245
|
|
|
@@ -142,14 +280,10 @@ async function getDiffFileList(db, review) {
|
|
|
142
280
|
try {
|
|
143
281
|
const opts = { cwd: review.local_path };
|
|
144
282
|
const [{ stdout: unstaged }, { stdout: untracked }] = await Promise.all([
|
|
145
|
-
execPromise(`git diff ${GIT_DIFF_FLAGS} --name-only`, opts),
|
|
146
|
-
execPromise(
|
|
283
|
+
execPromise(`git ${GIT_PATH_CONFIG} diff ${GIT_DIFF_FLAGS} --name-only -z`, opts),
|
|
284
|
+
execPromise(`git ${GIT_PATH_CONFIG} ls-files -z --others --exclude-standard`, opts),
|
|
147
285
|
]);
|
|
148
|
-
|
|
149
|
-
.split('\n')
|
|
150
|
-
.map(l => l.trim())
|
|
151
|
-
.filter(Boolean);
|
|
152
|
-
return [...new Set(combined)];
|
|
286
|
+
return [...new Set([...splitNulPaths(unstaged), ...splitNulPaths(untracked)])];
|
|
153
287
|
} catch {
|
|
154
288
|
// git error – fall through to empty list
|
|
155
289
|
}
|
|
@@ -161,7 +295,9 @@ async function getDiffFileList(db, review) {
|
|
|
161
295
|
|
|
162
296
|
module.exports = {
|
|
163
297
|
getDiffFileList,
|
|
298
|
+
splitUnifiedDiffPatches,
|
|
164
299
|
parseUnifiedDiffPatches,
|
|
165
300
|
countPatchStats,
|
|
301
|
+
buildChangedFileEntries,
|
|
166
302
|
mergeChangedFilesWithDiff
|
|
167
303
|
};
|