Files
java-ai-agents/pr-reviewer/src/main/java/com/ankurm/prreviewer/UnifiedDiff.java
T

105 lines
4.3 KiB
Java

package com.ankurm.prreviewer;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* A small parser for the unified diff Gitea serves at {@code /pulls/{n}.diff}.
*
* <p>The one rule that matters: inside a hunk, count lines using the numbers in the {@code @@} header.
* Do not recognise file headers by their text, because an added line {@code ++i;} appears in the diff
* as {@code +++i;} and a removed SQL comment {@code -- note} appears as {@code --- note}.
*/
public final class UnifiedDiff {
public enum Type { ADDED, REMOVED, CONTEXT }
/** {@code newLine} is 0 for removed lines, {@code oldLine} is 0 for added lines. */
public record Line(Type type, int oldLine, int newLine, String text) {}
public record Hunk(String header, List<Line> lines) {}
public record FileDiff(String path, boolean deleted, boolean binary, List<Hunk> hunks) {}
private static final Pattern HUNK = Pattern.compile("^@@ -(\\d+)(?:,(\\d+))? \\+(\\d+)(?:,(\\d+))? @@.*");
private UnifiedDiff() {}
public static List<FileDiff> parse(String diff) {
List<FileDiff> files = new ArrayList<>();
String path = null;
boolean deleted = false;
boolean binary = false;
List<Hunk> hunks = new ArrayList<>();
List<Line> current = null;
int oldNo = 0, newNo = 0, oldLeft = 0, newLeft = 0;
for (String raw : diff.split("\n", -1)) {
if (oldLeft > 0 || newLeft > 0) {
if (raw.startsWith("\\")) {
continue; // "\ No newline at end of file"
}
char marker = raw.isEmpty() ? ' ' : raw.charAt(0);
String text = raw.isEmpty() ? "" : raw.substring(1);
switch (marker) {
case '+' -> { current.add(new Line(Type.ADDED, 0, newNo++, text)); newLeft--; }
case '-' -> { current.add(new Line(Type.REMOVED, oldNo++, 0, text)); oldLeft--; }
case ' ' -> { current.add(new Line(Type.CONTEXT, oldNo++, newNo++, text)); oldLeft--; newLeft--; }
default -> throw new IllegalArgumentException("unexpected line inside a hunk: " + raw);
}
continue;
}
if (raw.startsWith("diff --git ")) {
if (path != null) {
files.add(new FileDiff(path, deleted, binary, hunks));
}
path = pathFromGitHeader(raw);
deleted = false;
binary = false;
hunks = new ArrayList<>();
} else if (raw.startsWith("+++ ") && !raw.equals("+++ /dev/null")) {
path = clean(raw.substring(4));
} else if (raw.startsWith("rename to ")) {
path = clean("b/" + raw.substring(10));
} else if (raw.startsWith("deleted file mode")) {
deleted = true;
} else if (raw.startsWith("Binary files ") || raw.startsWith("GIT binary patch")) {
binary = true;
} else {
Matcher m = HUNK.matcher(raw);
if (m.matches()) {
oldNo = Integer.parseInt(m.group(1));
oldLeft = m.group(2) == null ? 1 : Integer.parseInt(m.group(2));
newNo = Integer.parseInt(m.group(3));
newLeft = m.group(4) == null ? 1 : Integer.parseInt(m.group(4));
current = new ArrayList<>();
hunks.add(new Hunk(raw, current));
}
}
}
if (path != null) {
files.add(new FileDiff(path, deleted, binary, hunks));
}
return files;
}
private static String pathFromGitHeader(String header) {
int b = header.lastIndexOf(" b/");
return b >= 0 ? clean(header.substring(b + 1)) : header.substring("diff --git ".length());
}
/** Strips the {@code b/} prefix and the tab git appends after a path that contains spaces. */
private static String clean(String p) {
int tab = p.indexOf('\t');
if (tab >= 0) {
p = p.substring(0, tab);
}
if (p.length() > 1 && p.startsWith("\"") && p.endsWith("\"")) {
p = p.substring(1, p.length() - 1);
}
return p.startsWith("b/") ? p.substring(2) : p;
}
}