From ae51a583fd62c1b990bbc9219455ca4654af3641 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Pfeuffer?= Date: Wed, 25 Mar 2020 11:39:14 +0100 Subject: [PATCH] POC for rename detection in diffs --- .../java/sonia/scm/repository/spi/Differ.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/Differ.java b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/Differ.java index 6bba6a331e..aa67268ac2 100644 --- a/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/Differ.java +++ b/scm-plugins/scm-git-plugin/src/main/java/sonia/scm/repository/spi/Differ.java @@ -21,17 +21,19 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ - + package sonia.scm.repository.spi; import com.google.common.base.Strings; import org.eclipse.jgit.diff.DiffEntry; +import org.eclipse.jgit.diff.DiffFormatter; import org.eclipse.jgit.errors.MissingObjectException; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevTree; import org.eclipse.jgit.revwalk.RevWalk; +import org.eclipse.jgit.treewalk.AbstractTreeIterator; import org.eclipse.jgit.treewalk.EmptyTreeIterator; import org.eclipse.jgit.treewalk.TreeWalk; import org.eclipse.jgit.treewalk.filter.PathFilter; @@ -57,7 +59,7 @@ final class Differ implements AutoCloseable { static Diff diff(Repository repository, DiffCommandRequest request) throws IOException { try (Differ differ = create(repository, request)) { - return differ.diff(); + return differ.diff(repository); } } @@ -108,8 +110,13 @@ final class Differ implements AutoCloseable { return new Differ(commit, walk, treeWalk); } - private Diff diff() throws IOException { - List entries = DiffEntry.scan(treeWalk); + private Diff diff(Repository repository) throws IOException { + DiffFormatter diffFormatter = new DiffFormatter(null); + diffFormatter.setRepository(repository); + diffFormatter.setDetectRenames(true); + List entries = diffFormatter.scan( + treeWalk.getTree(0, AbstractTreeIterator.class), + treeWalk.getTree(1, AbstractTreeIterator.class)); return new Diff(commit, entries); }