From ec3d919aa5ae891edc3d5e7eafe09cf272801878 Mon Sep 17 00:00:00 2001 From: Ronald Bhuleskar Date: Wed, 19 Jul 2023 14:25:46 -0700 Subject: [PATCH] Identify a commit that generates a diffEntry on a rename Event. When using FollowFilter's rename callback, a callback is generated with the diff. The caller that is interested in the renames knows what the diff's are but have no idea what commit generated that diff. This will allow FollowFilter's rename callback to track diffEntry for a given commit. Change-Id: If1e63ccd19fdcb9c58c59137110fe24e0ce023d2 --- .../jgit/revwalk/RevWalkFollowFilterTest.java | 24 +++++++++++++++++++ .../eclipse/jgit/revwalk/RenameCallback.java | 22 +++++++++++++++++ .../eclipse/jgit/revwalk/TreeRevFilter.java | 8 ++++--- 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevWalkFollowFilterTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevWalkFollowFilterTest.java index c62136e64d..5203e3fbea 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevWalkFollowFilterTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevWalkFollowFilterTest.java @@ -27,9 +27,17 @@ public class RevWalkFollowFilterTest extends RevWalkTestCase { private static class DiffCollector extends RenameCallback { List diffs = new ArrayList<>(); + List commits = new ArrayList<>(); + @Override public void renamed(DiffEntry diff) { + throw new UnsupportedOperationException("unimplemented"); + } + + @Override + public void renamed(DiffEntry diff, RevCommit commit) { diffs.add(diff); + commits.add(commit); } } @@ -77,6 +85,7 @@ public class RevWalkFollowFilterTest extends RevWalkTestCase { assertNull(rw.next()); assertRenames("a->b"); + assertRenameCommits(renameCommit); } @Test @@ -108,6 +117,7 @@ public class RevWalkFollowFilterTest extends RevWalkTestCase { assertNull(rw.next()); assertRenames("c->a", "b->c", "a->b"); + assertRenameCommits(renameCommit3, renameCommit2, renameCommit1); } /** @@ -136,6 +146,20 @@ public class RevWalkFollowFilterTest extends RevWalkTestCase { } } + protected void assertRenameCommits(RevCommit... expectedCommits) { + Assert.assertEquals( + "Unexpected number of rename commits. Expected: " + + expectedCommits.length + ", actual: " + + diffCollector.diffs.size(), + expectedCommits.length, diffCollector.diffs.size()); + + for (int i = 0; i < expectedCommits.length; i++) { + RevCommit renameCommit = diffCollector.commits.get(i); + Assert.assertNotNull(renameCommit); + Assert.assertEquals(expectedCommits[i], renameCommit); + } + } + protected void assertNoRenames() { Assert.assertEquals("Found unexpected rename/copy diff", 0, diffCollector.diffs.size()); diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RenameCallback.java b/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RenameCallback.java index ba3399ce9f..9856f2c252 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RenameCallback.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RenameCallback.java @@ -23,8 +23,30 @@ public abstract class RenameCallback { * Called whenever a diff was found that is actually a rename or copy of a * file. * + *

Subclass of this class have to override this to receive diffEntry for + * the rename. + * * @param entry * the entry representing the rename/copy */ public abstract void renamed(DiffEntry entry); + + /** + * Called whenever a diff was found that is actually a rename or copy of a + * file along with the commit that caused it. + * + *

Subclass of this class have an option to override this if it wants to + * know what commit generated the diffEntry. Otherwise defaults to the + * {@link RenameCallback#renamed(DiffEntry)} function. + * + * @param entry + * the entry representing the rename/copy + * @param commit + * commit at which callback occurred + * + * @since 6.7 + */ + public void renamed(DiffEntry entry, RevCommit commit) { + renamed(entry); + } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/TreeRevFilter.java b/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/TreeRevFilter.java index 017a825ac6..410c36b16a 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/TreeRevFilter.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/TreeRevFilter.java @@ -185,7 +185,8 @@ public class TreeRevFilter extends RevFilter { // commit. We need to update our filter to its older // name, if we can discover it. Find out what that is. // - updateFollowFilter(trees, ((FollowFilter) tw.getFilter()).cfg); + updateFollowFilter(trees, ((FollowFilter) tw.getFilter()).cfg, + c); } return true; } else if (nParents == 0) { @@ -313,7 +314,8 @@ public class TreeRevFilter extends RevFilter { return changedPathFilterNegative; } - private void updateFollowFilter(ObjectId[] trees, DiffConfig cfg) + private void updateFollowFilter(ObjectId[] trees, DiffConfig cfg, + RevCommit commit) throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException, IOException { TreeWalk tw = pathFilter; @@ -332,7 +334,7 @@ public class TreeRevFilter extends RevFilter { newFilter = FollowFilter.create(ent.getOldPath(), cfg); RenameCallback callback = oldFilter.getRenameCallback(); if (callback != null) { - callback.renamed(ent); + callback.renamed(ent, commit); // forward the callback to the new follow filter ((FollowFilter) newFilter).setRenameCallback(callback); } -- 2.39.5