]> source.dussan.org Git - jgit.git/commitdiff
Identify a commit that generates a diffEntry on a rename Event. 57/203257/13
authorRonald Bhuleskar <funronald@google.com>
Wed, 19 Jul 2023 21:25:46 +0000 (14:25 -0700)
committerRonald Bhuleskar <funronald@google.com>
Mon, 24 Jul 2023 23:42:51 +0000 (19:42 -0400)
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

org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevWalkFollowFilterTest.java
org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RenameCallback.java
org.eclipse.jgit/src/org/eclipse/jgit/revwalk/TreeRevFilter.java

index c62136e64d958e248fda0e3c263a68592c37d50e..5203e3fbea5fbeb55d4df2d13e0818825b03678f 100644 (file)
@@ -27,9 +27,17 @@ public class RevWalkFollowFilterTest extends RevWalkTestCase {
        private static class DiffCollector extends RenameCallback {
                List<DiffEntry> diffs = new ArrayList<>();
 
+               List<RevCommit> 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());
index ba3399ce9f34e95600805d8099a3679b466d43b3..9856f2c252f0b6fabab43f4e4ba9d81fa5dd64b9 100644 (file)
@@ -23,8 +23,30 @@ public abstract class RenameCallback {
         * Called whenever a diff was found that is actually a rename or copy of a
         * file.
         *
+        * <p>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.
+        *
+        * <p>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);
+       }
 }
index 017a825ac60fde8c9bf9ac9b6742efcf2ce2a8b5..410c36b16a44d863b335e02944c68ab56eadb3fa 100644 (file)
@@ -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);
                                }