Merge "Identify a commit that generates a diffEntry on a rename Event."

This commit is contained in:
Jonathan Tan 2023-07-25 12:09:40 -04:00 committed by Gerrit Code Review @ Eclipse.org
commit c77fb93478
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 { private static class DiffCollector extends RenameCallback {
List<DiffEntry> diffs = new ArrayList<>(); List<DiffEntry> diffs = new ArrayList<>();
List<RevCommit> commits = new ArrayList<>();
@Override @Override
public void renamed(DiffEntry diff) { public void renamed(DiffEntry diff) {
throw new UnsupportedOperationException("unimplemented");
}
@Override
public void renamed(DiffEntry diff, RevCommit commit) {
diffs.add(diff); diffs.add(diff);
commits.add(commit);
} }
} }
@ -77,6 +85,7 @@ public void testSingleRename() throws Exception {
assertNull(rw.next()); assertNull(rw.next());
assertRenames("a->b"); assertRenames("a->b");
assertRenameCommits(renameCommit);
} }
@Test @Test
@ -108,6 +117,7 @@ public void testMultiRename() throws Exception {
assertNull(rw.next()); assertNull(rw.next());
assertRenames("c->a", "b->c", "a->b"); 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() { protected void assertNoRenames() {
Assert.assertEquals("Found unexpected rename/copy diff", 0, Assert.assertEquals("Found unexpected rename/copy diff", 0,
diffCollector.diffs.size()); 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 * Called whenever a diff was found that is actually a rename or copy of a
* file. * file.
* *
* <p>Subclass of this class have to override this to receive diffEntry for
* the rename.
*
* @param entry * @param entry
* the entry representing the rename/copy * the entry representing the rename/copy
*/ */
public abstract void renamed(DiffEntry entry); 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 // commit. We need to update our filter to its older
// name, if we can discover it. Find out what that is. // 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; return true;
} else if (nParents == 0) { } else if (nParents == 0) {
@ -313,7 +314,8 @@ public long getChangedPathFilterNegative() {
return changedPathFilterNegative; return changedPathFilterNegative;
} }
private void updateFollowFilter(ObjectId[] trees, DiffConfig cfg) private void updateFollowFilter(ObjectId[] trees, DiffConfig cfg,
RevCommit commit)
throws MissingObjectException, IncorrectObjectTypeException, throws MissingObjectException, IncorrectObjectTypeException,
CorruptObjectException, IOException { CorruptObjectException, IOException {
TreeWalk tw = pathFilter; TreeWalk tw = pathFilter;
@ -332,7 +334,7 @@ private void updateFollowFilter(ObjectId[] trees, DiffConfig cfg)
newFilter = FollowFilter.create(ent.getOldPath(), cfg); newFilter = FollowFilter.create(ent.getOldPath(), cfg);
RenameCallback callback = oldFilter.getRenameCallback(); RenameCallback callback = oldFilter.getRenameCallback();
if (callback != null) { if (callback != null) {
callback.renamed(ent); callback.renamed(ent, commit);
// forward the callback to the new follow filter // forward the callback to the new follow filter
((FollowFilter) newFilter).setRenameCallback(callback); ((FollowFilter) newFilter).setRenameCallback(callback);
} }