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
This commit is contained in:
Ronald Bhuleskar 2023-07-19 14:25:46 -07:00
parent 3b77e33ad8
commit ec3d919aa5
3 changed files with 51 additions and 3 deletions

View 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 void testSingleRename() throws Exception {
assertNull(rw.next());
assertRenames("a->b");
assertRenameCommits(renameCommit);
}
@Test
@ -108,6 +117,7 @@ public void testMultiRename() throws Exception {
assertNull(rw.next());
assertRenames("c->a", "b->c", "a->b");
assertRenameCommits(renameCommit3, renameCommit2, renameCommit1);
}
/**
@ -136,6 +146,20 @@ protected void assertRenames(String... expectedRenames) {
}
}
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());

View 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);
}
}

View File

@ -185,7 +185,8 @@ public boolean include(RevWalk walker, RevCommit c)
// 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 long getChangedPathFilterNegative() {
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 @@ private void updateFollowFilter(ObjectId[] trees, DiffConfig cfg)
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);
}