implemented parsing of git diff hunks

This commit is contained in:
Sebastian Sdorra
2019-07-29 16:42:49 +02:00
parent 01379caa08
commit 07068880bb
7 changed files with 474 additions and 19 deletions

View File

@@ -43,6 +43,7 @@ import org.eclipse.jgit.api.FetchCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.diff.DiffFormatter;
import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
@@ -441,7 +442,7 @@ public final class GitUtil
*
* @return
*/
public static String getId(ObjectId objectId)
public static String getId(AnyObjectId objectId)
{
String id = Util.EMPTY_STRING;

View File

@@ -0,0 +1,20 @@
package sonia.scm.repository.spi;
public class FileRange {
private final int start;
private final int lineCount;
public FileRange(int start, int lineCount) {
this.start = start;
this.lineCount = lineCount;
}
public int getStart() {
return start;
}
public int getLineCount() {
return lineCount;
}
}

View File

@@ -1,12 +1,15 @@
package sonia.scm.repository.spi;
import com.google.common.base.Throwables;
import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.diff.DiffFormatter;
import sonia.scm.repository.GitUtil;
import sonia.scm.repository.Repository;
import sonia.scm.repository.api.DiffFile;
import sonia.scm.repository.api.DiffResult;
import sonia.scm.repository.api.Hunk;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.stream.Collectors;
@@ -18,8 +21,9 @@ public class GitDiffResultCommand extends AbstractGitCommand implements DiffResu
}
public DiffResult getDiffResult(DiffCommandRequest diffCommandRequest) throws IOException {
try (Differ differ = Differ.create(open(), diffCommandRequest)) {
GitDiffResult result = new GitDiffResult();
org.eclipse.jgit.lib.Repository repository = open();
try (Differ differ = Differ.create(repository, diffCommandRequest)) {
GitDiffResult result = new GitDiffResult(repository);
differ.process(result::process);
return result;
}
@@ -27,8 +31,13 @@ public class GitDiffResultCommand extends AbstractGitCommand implements DiffResu
private class GitDiffResult implements DiffResult {
private final org.eclipse.jgit.lib.Repository repository;
private Differ.Diff diff;
private GitDiffResult(org.eclipse.jgit.lib.Repository repository) {
this.repository = repository;
}
void process(Differ.Diff diff) {
this.diff = diff;
}
@@ -45,26 +54,28 @@ public class GitDiffResultCommand extends AbstractGitCommand implements DiffResu
@Override
public Iterator<DiffFile> iterator() {
return diff.getEntries().stream().map(GitDiffFile::new).collect(Collectors.<DiffFile>toList()).iterator();
return diff.getEntries().stream().map(diffEntry -> new GitDiffFile(repository, diffEntry)).collect(Collectors.<DiffFile>toList()).iterator();
}
}
private static class GitDiffFile implements DiffFile {
private class GitDiffFile implements DiffFile {
private final org.eclipse.jgit.lib.Repository repository;
private final DiffEntry diffEntry;
private GitDiffFile(DiffEntry diffEntry) {
private GitDiffFile(org.eclipse.jgit.lib.Repository repository, DiffEntry diffEntry) {
this.repository = repository;
this.diffEntry = diffEntry;
}
@Override
public String getOldRevision() {
return null;
return GitUtil.getId(diffEntry.getOldId().toObjectId());
}
@Override
public String getNewRevision() {
return null;
return GitUtil.getId(diffEntry.getNewId().toObjectId());
}
@Override
@@ -79,7 +90,22 @@ public class GitDiffResultCommand extends AbstractGitCommand implements DiffResu
@Override
public Iterator<Hunk> iterator() {
return null;
String content = format(repository, diffEntry);
GitHunkParser parser = new GitHunkParser();
return parser.parse(content).iterator();
}
private String format(org.eclipse.jgit.lib.Repository repository, DiffEntry entry) {
try(ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
DiffFormatter formatter = new DiffFormatter(baos);
formatter.setRepository(repository);
formatter.format(entry);
return baos.toString();
} catch (IOException ex) {
throw Throwables.propagate(ex);
}
}
}
}

View File

@@ -0,0 +1,48 @@
package sonia.scm.repository.spi;
import sonia.scm.repository.api.DiffLine;
import sonia.scm.repository.api.Hunk;
import java.util.Iterator;
import java.util.List;
public class GitHunk implements Hunk {
private final FileRange oldFileRange;
private final FileRange newFileRange;
private List<DiffLine> lines;
public GitHunk(FileRange oldFileRange, FileRange newFileRange) {
this.oldFileRange = oldFileRange;
this.newFileRange = newFileRange;
}
@Override
public int getOldStart() {
return oldFileRange.getStart();
}
@Override
public int getOldLineCount() {
return oldFileRange.getLineCount();
}
@Override
public int getNewStart() {
return newFileRange.getStart();
}
@Override
public int getNewLineCount() {
return newFileRange.getLineCount();
}
@Override
public Iterator<DiffLine> iterator() {
return lines.iterator();
}
void setLines(List<DiffLine> lines) {
this.lines = lines;
}
}

View File

@@ -0,0 +1,175 @@
package sonia.scm.repository.spi;
import sonia.scm.repository.api.DiffLine;
import sonia.scm.repository.api.Hunk;
import java.util.ArrayList;
import java.util.List;
import java.util.OptionalInt;
import java.util.Scanner;
import static java.util.OptionalInt.of;
final class GitHunkParser {
private static final int HEADER_PREFIX_LENGTH = "@@ -".length();
private static final int HEADER_SUFFIX_LENGTH = " @@".length();
private GitHunk currentGitHunk = null;
private List<DiffLine> collectedLines = null;
private int oldLineCounter = 0;
private int newLineCounter = 0;
GitHunkParser() {
}
public List<Hunk> parse(String content) {
List<Hunk> hunks = new ArrayList<>();
try (Scanner scanner = new Scanner(content)) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (line.startsWith("@@")) {
parseHeader(hunks, line);
} else if (currentGitHunk != null) {
parseDiffLine(line);
}
}
}
if (currentGitHunk != null) {
currentGitHunk.setLines(collectedLines);
}
return hunks;
}
private void parseHeader(List<Hunk> hunks, String line) {
if (currentGitHunk != null) {
currentGitHunk.setLines(collectedLines);
}
String hunkHeader = line.substring(HEADER_PREFIX_LENGTH, line.length() - HEADER_SUFFIX_LENGTH);
String[] split = hunkHeader.split("\\s");
FileRange oldFileRange = createFileRange(split[0]);
// TODO merge contains two two block which starts with "-" e.g. -1,3 -2,4 +3,6
FileRange newFileRange = createFileRange(split[1]);
currentGitHunk = new GitHunk(oldFileRange, newFileRange);
hunks.add(currentGitHunk);
collectedLines = new ArrayList<>();
oldLineCounter = currentGitHunk.getOldStart();
newLineCounter = currentGitHunk.getNewStart();
}
private void parseDiffLine(String line) {
String content = line.substring(1);
switch (line.charAt(0)) {
case ' ':
collectedLines.add(new UnchangedGitDiffLine(newLineCounter, oldLineCounter, content));
++newLineCounter;
++oldLineCounter;
break;
case '+':
collectedLines.add(new AddedGitDiffLine(newLineCounter, content));
++newLineCounter;
break;
case '-':
collectedLines.add(new RemovedGitDiffLine(oldLineCounter, content));
++oldLineCounter;
break;
default:
throw new IllegalStateException("cannot handle diff line: " + line);
}
}
private static class AddedGitDiffLine implements DiffLine {
private final int newLineNumber;
private final String content;
private AddedGitDiffLine(int newLineNumber, String content) {
this.newLineNumber = newLineNumber;
this.content = content;
}
@Override
public OptionalInt getOldLineNumber() {
return OptionalInt.empty();
}
@Override
public OptionalInt getNewLineNumber() {
return of(newLineNumber);
}
@Override
public String getContent() {
return content;
}
}
private static class RemovedGitDiffLine implements DiffLine {
private final int oldLineNumber;
private final String content;
private RemovedGitDiffLine(int oldLineNumber, String content) {
this.oldLineNumber = oldLineNumber;
this.content = content;
}
@Override
public OptionalInt getOldLineNumber() {
return of(oldLineNumber);
}
@Override
public OptionalInt getNewLineNumber() {
return OptionalInt.empty();
}
@Override
public String getContent() {
return content;
}
}
private static class UnchangedGitDiffLine implements DiffLine {
private final int newLineNumber;
private final int oldLineNumber;
private final String content;
public UnchangedGitDiffLine(int newLineNumber, int oldLineNumber, String content) {
this.newLineNumber = newLineNumber;
this.oldLineNumber = oldLineNumber;
this.content = content;
}
@Override
public OptionalInt getOldLineNumber() {
return of(oldLineNumber);
}
@Override
public OptionalInt getNewLineNumber() {
return of(newLineNumber);
}
@Override
public String getContent() {
return content;
}
}
private static FileRange createFileRange(String fileRangeString) {
int start;
int lineCount = 1;
int commaIndex = fileRangeString.indexOf(',');
if (commaIndex > 0) {
start = Integer.parseInt(fileRangeString.substring(0, commaIndex));
lineCount = Integer.parseInt(fileRangeString.substring(commaIndex + 1));
} else {
start = Integer.parseInt(fileRangeString);
}
return new FileRange(start, lineCount);
}
}