mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-08 14:35:45 +01:00
simplify Differ api and use the new api in GitDiffCommand
This commit is contained in:
@@ -15,7 +15,6 @@ import sonia.scm.util.Util;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.function.Consumer;
|
|
||||||
|
|
||||||
final class Differ implements AutoCloseable {
|
final class Differ implements AutoCloseable {
|
||||||
|
|
||||||
@@ -29,7 +28,13 @@ final class Differ implements AutoCloseable {
|
|||||||
this.treeWalk = treeWalk;
|
this.treeWalk = treeWalk;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Differ create(Repository repository, DiffCommandRequest request) throws IOException {
|
static Diff diff(Repository repository, DiffCommandRequest request) throws IOException {
|
||||||
|
try (Differ differ = create(repository, request)) {
|
||||||
|
return differ.diff();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Differ create(Repository repository, DiffCommandRequest request) throws IOException {
|
||||||
RevWalk walk = new RevWalk(repository);
|
RevWalk walk = new RevWalk(repository);
|
||||||
|
|
||||||
ObjectId revision = repository.resolve(request.getRevision());
|
ObjectId revision = repository.resolve(request.getRevision());
|
||||||
@@ -81,9 +86,9 @@ final class Differ implements AutoCloseable {
|
|||||||
return GitUtil.computeCommonAncestor(repository, revision1, revision2);
|
return GitUtil.computeCommonAncestor(repository, revision1, revision2);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void process(Consumer<Diff> diffConsumer) throws IOException {
|
private Diff diff() throws IOException {
|
||||||
List<DiffEntry> entries = DiffEntry.scan(treeWalk);
|
List<DiffEntry> entries = DiffEntry.scan(treeWalk);
|
||||||
diffConsumer.accept(new Diff(commit, entries));
|
return new Diff(commit, entries);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -34,26 +34,15 @@ package sonia.scm.repository.spi;
|
|||||||
|
|
||||||
//~--- non-JDK imports --------------------------------------------------------
|
//~--- non-JDK imports --------------------------------------------------------
|
||||||
|
|
||||||
import com.google.common.base.Strings;
|
|
||||||
import org.eclipse.jgit.diff.DiffEntry;
|
import org.eclipse.jgit.diff.DiffEntry;
|
||||||
import org.eclipse.jgit.diff.DiffFormatter;
|
import org.eclipse.jgit.diff.DiffFormatter;
|
||||||
import org.eclipse.jgit.lib.ObjectId;
|
|
||||||
import org.eclipse.jgit.revwalk.RevCommit;
|
|
||||||
import org.eclipse.jgit.revwalk.RevTree;
|
|
||||||
import org.eclipse.jgit.revwalk.RevWalk;
|
|
||||||
import org.eclipse.jgit.treewalk.EmptyTreeIterator;
|
|
||||||
import org.eclipse.jgit.treewalk.TreeWalk;
|
|
||||||
import org.eclipse.jgit.treewalk.filter.PathFilter;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import sonia.scm.repository.GitUtil;
|
import sonia.scm.repository.GitUtil;
|
||||||
import sonia.scm.repository.Repository;
|
import sonia.scm.repository.Repository;
|
||||||
import sonia.scm.util.Util;
|
|
||||||
|
|
||||||
import java.io.BufferedOutputStream;
|
import java.io.BufferedOutputStream;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@@ -78,7 +67,7 @@ public class GitDiffCommand extends AbstractGitCommand implements DiffCommand
|
|||||||
* @param context
|
* @param context
|
||||||
* @param repository
|
* @param repository
|
||||||
*/
|
*/
|
||||||
public GitDiffCommand(GitContext context, Repository repository)
|
GitDiffCommand(GitContext context, Repository repository)
|
||||||
{
|
{
|
||||||
super(context, repository);
|
super(context, repository);
|
||||||
}
|
}
|
||||||
@@ -95,63 +84,18 @@ public class GitDiffCommand extends AbstractGitCommand implements DiffCommand
|
|||||||
@Override
|
@Override
|
||||||
public void getDiffResult(DiffCommandRequest request, OutputStream output)
|
public void getDiffResult(DiffCommandRequest request, OutputStream output)
|
||||||
{
|
{
|
||||||
RevWalk walk = null;
|
|
||||||
TreeWalk treeWalk = null;
|
|
||||||
DiffFormatter formatter = null;
|
DiffFormatter formatter = null;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
org.eclipse.jgit.lib.Repository gr = open();
|
org.eclipse.jgit.lib.Repository repository = open();
|
||||||
|
|
||||||
walk = new RevWalk(gr);
|
|
||||||
|
|
||||||
ObjectId revision = gr.resolve(request.getRevision());
|
|
||||||
RevCommit commit = walk.parseCommit(revision);
|
|
||||||
|
|
||||||
walk.markStart(commit);
|
|
||||||
commit = walk.next();
|
|
||||||
treeWalk = new TreeWalk(gr);
|
|
||||||
treeWalk.reset();
|
|
||||||
treeWalk.setRecursive(true);
|
|
||||||
|
|
||||||
if (Util.isNotEmpty(request.getPath()))
|
|
||||||
{
|
|
||||||
treeWalk.setFilter(PathFilter.create(request.getPath()));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if (!Strings.isNullOrEmpty(request.getAncestorChangeset()))
|
|
||||||
{
|
|
||||||
ObjectId otherRevision = gr.resolve(request.getAncestorChangeset());
|
|
||||||
ObjectId ancestorId = computeCommonAncestor(gr, revision, otherRevision);
|
|
||||||
RevTree tree = walk.parseCommit(ancestorId).getTree();
|
|
||||||
treeWalk.addTree(tree);
|
|
||||||
}
|
|
||||||
else if (commit.getParentCount() > 0)
|
|
||||||
{
|
|
||||||
RevTree tree = commit.getParent(0).getTree();
|
|
||||||
|
|
||||||
if (tree != null)
|
|
||||||
{
|
|
||||||
treeWalk.addTree(tree);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
treeWalk.addTree(new EmptyTreeIterator());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
treeWalk.addTree(new EmptyTreeIterator());
|
|
||||||
}
|
|
||||||
|
|
||||||
treeWalk.addTree(commit.getTree());
|
|
||||||
formatter = new DiffFormatter(new BufferedOutputStream(output));
|
formatter = new DiffFormatter(new BufferedOutputStream(output));
|
||||||
formatter.setRepository(gr);
|
formatter.setRepository(repository);
|
||||||
|
|
||||||
List<DiffEntry> entries = DiffEntry.scan(treeWalk);
|
Differ.Diff diff = Differ.diff(repository, request);
|
||||||
|
|
||||||
for (DiffEntry e : entries)
|
for (DiffEntry e : diff.getEntries())
|
||||||
{
|
{
|
||||||
if (!e.getOldId().equals(e.getNewId()))
|
if (!e.getOldId().equals(e.getNewId()))
|
||||||
{
|
{
|
||||||
@@ -168,14 +112,8 @@ public class GitDiffCommand extends AbstractGitCommand implements DiffCommand
|
|||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
GitUtil.release(walk);
|
|
||||||
GitUtil.release(treeWalk);
|
|
||||||
GitUtil.release(formatter);
|
GitUtil.release(formatter);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private ObjectId computeCommonAncestor(org.eclipse.jgit.lib.Repository repository, ObjectId revision1, ObjectId revision2) throws IOException {
|
|
||||||
return GitUtil.computeCommonAncestor(repository, revision1, revision2);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,23 +22,16 @@ public class GitDiffResultCommand extends AbstractGitCommand implements DiffResu
|
|||||||
|
|
||||||
public DiffResult getDiffResult(DiffCommandRequest diffCommandRequest) throws IOException {
|
public DiffResult getDiffResult(DiffCommandRequest diffCommandRequest) throws IOException {
|
||||||
org.eclipse.jgit.lib.Repository repository = open();
|
org.eclipse.jgit.lib.Repository repository = open();
|
||||||
try (Differ differ = Differ.create(repository, diffCommandRequest)) {
|
return new GitDiffResult(repository, Differ.diff(repository, diffCommandRequest));
|
||||||
GitDiffResult result = new GitDiffResult(repository);
|
|
||||||
differ.process(result::process);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private class GitDiffResult implements DiffResult {
|
private class GitDiffResult implements DiffResult {
|
||||||
|
|
||||||
private final org.eclipse.jgit.lib.Repository repository;
|
private final org.eclipse.jgit.lib.Repository repository;
|
||||||
private Differ.Diff diff;
|
private final Differ.Diff diff;
|
||||||
|
|
||||||
private GitDiffResult(org.eclipse.jgit.lib.Repository repository) {
|
private GitDiffResult(org.eclipse.jgit.lib.Repository repository, Differ.Diff diff) {
|
||||||
this.repository = repository;
|
this.repository = repository;
|
||||||
}
|
|
||||||
|
|
||||||
void process(Differ.Diff diff) {
|
|
||||||
this.diff = diff;
|
this.diff = diff;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,7 +89,7 @@ public class GitDiffResultCommand extends AbstractGitCommand implements DiffResu
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String format(org.eclipse.jgit.lib.Repository repository, DiffEntry entry) {
|
private String format(org.eclipse.jgit.lib.Repository repository, DiffEntry entry) {
|
||||||
try(ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
|
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
|
||||||
DiffFormatter formatter = new DiffFormatter(baos);
|
DiffFormatter formatter = new DiffFormatter(baos);
|
||||||
formatter.setRepository(repository);
|
formatter.setRepository(repository);
|
||||||
formatter.format(entry);
|
formatter.format(entry);
|
||||||
|
|||||||
@@ -51,6 +51,7 @@ final class GitHunkParser {
|
|||||||
|
|
||||||
FileRange oldFileRange = createFileRange(split[0]);
|
FileRange oldFileRange = createFileRange(split[0]);
|
||||||
// TODO merge contains two two block which starts with "-" e.g. -1,3 -2,4 +3,6
|
// TODO merge contains two two block which starts with "-" e.g. -1,3 -2,4 +3,6
|
||||||
|
// check if it is relevant for our use case
|
||||||
FileRange newFileRange = createFileRange(split[1]);
|
FileRange newFileRange = createFileRange(split[1]);
|
||||||
|
|
||||||
currentGitHunk = new GitHunk(oldFileRange, newFileRange);
|
currentGitHunk = new GitHunk(oldFileRange, newFileRange);
|
||||||
@@ -137,7 +138,7 @@ final class GitHunkParser {
|
|||||||
private final int oldLineNumber;
|
private final int oldLineNumber;
|
||||||
private final String content;
|
private final String content;
|
||||||
|
|
||||||
public UnchangedGitDiffLine(int newLineNumber, int oldLineNumber, String content) {
|
private UnchangedGitDiffLine(int newLineNumber, int oldLineNumber, String content) {
|
||||||
this.newLineNumber = newLineNumber;
|
this.newLineNumber = newLineNumber;
|
||||||
this.oldLineNumber = oldLineNumber;
|
this.oldLineNumber = oldLineNumber;
|
||||||
this.content = content;
|
this.content = content;
|
||||||
|
|||||||
Reference in New Issue
Block a user