pr-shepherd 0.31.0 → 0.31.1
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.
|
@@ -4,6 +4,49 @@
|
|
|
4
4
|
* The diff uses `--- a/<path>` / `+++ b/<path>` headers so `git apply` and
|
|
5
5
|
* `git apply --check` accept it without a `diff --git` preamble.
|
|
6
6
|
*/
|
|
7
|
+
/**
|
|
8
|
+
* Strip leading/trailing replacement lines that are identical to the adjacent
|
|
9
|
+
* file lines just outside the removed range.
|
|
10
|
+
*
|
|
11
|
+
* GitHub stores a suggestion as a verbatim replacement for the *highlighted*
|
|
12
|
+
* line range, but reviewers often paste surrounding context into the box. Those
|
|
13
|
+
* context-duplicating lines must not appear as additions in the diff — they
|
|
14
|
+
* belong to the surrounding unchanged file content. Stripping them here produces
|
|
15
|
+
* a minimal diff that applies cleanly without duplicating lines.
|
|
16
|
+
*
|
|
17
|
+
* Comparison normalises trailing `\r` from file lines so CRLF files (which
|
|
18
|
+
* carry `\r` on each entry after `split("\n")`) still match suggestion lines
|
|
19
|
+
* delivered as LF-only by the GitHub API.
|
|
20
|
+
*/
|
|
21
|
+
function trimReplacementToContext(fileLines, startLine, endLine, replacementLines) {
|
|
22
|
+
const norm = (s) => (s.endsWith("\r") ? s.slice(0, -1) : s);
|
|
23
|
+
// Leading trim: largest L where replacement[0..L) == fileLines[startLine-1-L..startLine-1)
|
|
24
|
+
const maxL = Math.min(startLine - 1, replacementLines.length);
|
|
25
|
+
let L = 0;
|
|
26
|
+
leading: for (let l = maxL; l >= 1; l--) {
|
|
27
|
+
for (let i = 0; i < l; i++) {
|
|
28
|
+
if (norm(replacementLines[i]) !== norm(fileLines[startLine - 1 - l + i]))
|
|
29
|
+
continue leading;
|
|
30
|
+
}
|
|
31
|
+
L = l;
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
const remainder = replacementLines.slice(L);
|
|
35
|
+
// Trailing trim: largest T where remainder[len-T..len) == fileLines[endLine..endLine+T)
|
|
36
|
+
const maxT = Math.min(fileLines.length - endLine, remainder.length);
|
|
37
|
+
let T = 0;
|
|
38
|
+
trailing: for (let t = maxT; t >= 1; t--) {
|
|
39
|
+
for (let j = 0; j < t; j++) {
|
|
40
|
+
if (norm(remainder[remainder.length - t + j]) !== norm(fileLines[endLine + j]))
|
|
41
|
+
continue trailing;
|
|
42
|
+
}
|
|
43
|
+
T = t;
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
if (L === 0 && T === 0)
|
|
47
|
+
return replacementLines;
|
|
48
|
+
return T === 0 ? remainder : remainder.slice(0, remainder.length - T);
|
|
49
|
+
}
|
|
7
50
|
export function buildUnifiedDiff({ path, originalContent, startLine, endLine, replacementLines, context = 3, }) {
|
|
8
51
|
const endsWithNewline = originalContent.endsWith("\n");
|
|
9
52
|
const body = endsWithNewline ? originalContent.slice(0, -1) : originalContent;
|
|
@@ -13,9 +56,12 @@ export function buildUnifiedDiff({ path, originalContent, startLine, endLine, re
|
|
|
13
56
|
const beforeLines = fileLines.slice(beforeStart, startLine - 1);
|
|
14
57
|
const afterEnd = Math.min(fileLines.length, endLine + context);
|
|
15
58
|
const afterLines = fileLines.slice(endLine, afterEnd);
|
|
59
|
+
// Trim replacement lines that duplicate adjacent file context so the diff is
|
|
60
|
+
// minimal and `git apply`-clean (issue #294).
|
|
61
|
+
const replacement = trimReplacementToContext(fileLines, startLine, endLine, replacementLines);
|
|
16
62
|
const hunkOrigStart = beforeStart + 1;
|
|
17
63
|
const hunkOrigCount = beforeLines.length + removedLines.length + afterLines.length;
|
|
18
|
-
const hunkNewCount = beforeLines.length +
|
|
64
|
+
const hunkNewCount = beforeLines.length + replacement.length + afterLines.length;
|
|
19
65
|
const noNewline = "\\n";
|
|
20
66
|
const isLastOrigLine = (lineIdx) => !endsWithNewline && lineIdx === fileLines.length - 1;
|
|
21
67
|
const out = [
|
|
@@ -37,10 +83,10 @@ export function buildUnifiedDiff({ path, originalContent, startLine, endLine, re
|
|
|
37
83
|
// not just because context is 0 (there may still be unshown lines beyond the hunk).
|
|
38
84
|
const addedEndsFile = !endsWithNewline && endLine >= fileLines.length;
|
|
39
85
|
const hasCr = originalContent.includes("\r\n");
|
|
40
|
-
for (let i = 0; i <
|
|
41
|
-
const line = hasCr ?
|
|
86
|
+
for (let i = 0; i < replacement.length; i++) {
|
|
87
|
+
const line = hasCr ? replacement[i] + "\r" : replacement[i];
|
|
42
88
|
out.push(`+${line}\n`);
|
|
43
|
-
if (addedEndsFile && i ===
|
|
89
|
+
if (addedEndsFile && i === replacement.length - 1)
|
|
44
90
|
out.push(noNewline);
|
|
45
91
|
}
|
|
46
92
|
for (let i = 0; i < afterLines.length; i++) {
|
package/package.json
CHANGED