Browse Source

RewriteGenerator: avoid adding null parent

Prevent adding a null parent to a commit's parent array. Doing so
can cause NPEs later on.

Bug: 552160
Change-Id: Ib24b7b9b7b08e0b6f246006b4a4cade7eeb830b9
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
next
Thomas Wolf 5 years ago committed by Matthias Sohn
parent
commit
1daf6f13aa
  1. 36
      org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/FirstParentRevWalkTest.java
  2. 10
      org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RewriteGenerator.java

36
org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/FirstParentRevWalkTest.java

@ -49,6 +49,7 @@ import static org.junit.Assert.assertNull;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.revwalk.filter.MessageRevFilter;
import org.eclipse.jgit.revwalk.filter.RevFilter;
import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
import org.junit.Test;
public class FirstParentRevWalkTest extends RevWalkTestCase {
@ -425,4 +426,39 @@ public class FirstParentRevWalkTest extends RevWalkTestCase {
markStart(a);
assertNull(rw.next());
}
@Test
public void testWithTopoSortAndTreeFilter() throws Exception {
RevCommit a = commit();
RevCommit b = commit(tree(file("0", blob("b"))), a);
RevCommit c = commit(tree(file("0", blob("c"))), b, a);
RevCommit d = commit(tree(file("0", blob("d"))), c);
rw.reset();
rw.setFirstParent(true);
rw.sort(RevSort.TOPO, true);
rw.setTreeFilter(PathFilterGroup.createFromStrings("0"));
markStart(d);
assertCommit(d, rw.next());
assertCommit(c, rw.next());
assertCommit(b, rw.next());
assertNull(rw.next());
}
@Test
public void testWithTopoSortAndTreeFilter2() throws Exception {
RevCommit a = commit();
RevCommit b = commit(tree(file("0", blob("b"))), a);
RevCommit c = commit(tree(file("0", blob("c"))), a, b);
RevCommit d = commit(tree(file("0", blob("d"))), c);
rw.reset();
rw.setFirstParent(true);
rw.sort(RevSort.TOPO, true);
rw.setTreeFilter(PathFilterGroup.createFromStrings("0"));
markStart(d);
assertCommit(d, rw.next());
assertCommit(c, rw.next());
assertNull(rw.next());
}
}

10
org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RewriteGenerator.java

@ -103,11 +103,15 @@ class RewriteGenerator extends Generator {
final int nParents = pList.length;
for (int i = 0; i < nParents; i++) {
final RevCommit oldp = pList[i];
if (firstParent && i > 0) {
c.parents = new RevCommit[] { rewrite(oldp) };
final RevCommit newp = rewrite(oldp);
if (firstParent) {
if (newp == null) {
c.parents = RevCommit.NO_PARENTS;
} else {
c.parents = new RevCommit[] { newp };
}
return c;
}
final RevCommit newp = rewrite(oldp);
if (oldp != newp) {
pList[i] = newp;
rewrote = true;

Loading…
Cancel
Save