From 61b4d105e4924bf15c07f5bc6f1ab0d23b50bff1 Mon Sep 17 00:00:00 2001 From: Ronald Bhuleskar Date: Wed, 15 Jun 2022 14:37:21 -0700 Subject: [PATCH] Add the ability to override parents on RevCommit. This makes RevCommit extensible to allow having different structure of child-parent relationship. This change is a pre-requsite for having a FilteredRevCommit that overrides parents from the RevCommit. That then provides a cheaper way to walk over a subset of RevCommits instead of an expensive way that applies filters while walking over selected commits. Useful with Blame which works on a single file and that can be made performant, if we know all the commits needed by the Blame algorithm. So Blame algorithm can avoid walking over finding what commits to blame on. This change makes parents field on RevCommit private and exposes it thrrough overrideable methods such as getParents, getParent at index, getParentCount and setParents. All other files other than RevCommit are updating the usages of accessing RevCommits parents. Change-Id: I2d13b001c599cc4ebc92d1ab6e07b07acb3b7fe5 --- .../jgit/revwalk/RevCommitParseTest.java | 123 ++++++++----- .../RevCommitWithOverriddenParentTest.java | 170 ++++++++++++++++++ .../eclipse/jgit/revwalk/RevWalkCullTest.java | 2 +- .../jgit/revwalk/AbstractRevQueue.java | 2 +- .../jgit/revwalk/BoundaryGenerator.java | 5 +- .../eclipse/jgit/revwalk/DepthGenerator.java | 5 +- .../jgit/revwalk/MergeBaseGenerator.java | 4 +- .../jgit/revwalk/PendingGenerator.java | 5 +- .../org/eclipse/jgit/revwalk/RevCommit.java | 33 ++-- .../src/org/eclipse/jgit/revwalk/RevWalk.java | 79 ++++---- .../jgit/revwalk/RewriteGenerator.java | 12 +- .../revwalk/TopoNonIntermixSortGenerator.java | 4 +- .../jgit/revwalk/TopoSortGenerator.java | 4 +- .../eclipse/jgit/revwalk/TreeRevFilter.java | 14 +- 14 files changed, 333 insertions(+), 129 deletions(-) create mode 100644 org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevCommitWithOverriddenParentTest.java diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevCommitParseTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevCommitParseTest.java index 0e4c05d792..82af34ded2 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevCommitParseTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevCommitParseTest.java @@ -78,7 +78,7 @@ public class RevCommitParseTest extends RepositoryTestCase { c = new RevCommit(id("9473095c4cb2f12aefe1db8a355fe3fafba42f67")); assertNull(c.getTree()); - assertNull(c.parents); + assertNull(c.getParents()); try (RevWalk rw = new RevWalk(db)) { c.parseCanonical(rw, body.toString().getBytes(UTF_8)); @@ -86,23 +86,26 @@ public class RevCommitParseTest extends RepositoryTestCase { assertEquals(treeId, c.getTree().getId()); assertSame(rw.lookupTree(treeId), c.getTree()); } - assertNotNull(c.parents); - assertEquals(0, c.parents.length); + assertNotNull(c.getParents()); + assertEquals(0, c.getParentCount()); assertEquals("", c.getFullMessage()); final PersonIdent cAuthor = c.getAuthorIdent(); assertNotNull(cAuthor); assertEquals(authorName, cAuthor.getName()); assertEquals(authorEmail, cAuthor.getEmailAddress()); - assertEquals((long)authorTime * 1000, cAuthor.getWhen().getTime()); - assertEquals(TimeZone.getTimeZone("GMT" + authorTimeZone), cAuthor.getTimeZone()); + assertEquals((long) authorTime * 1000, cAuthor.getWhen().getTime()); + assertEquals(TimeZone.getTimeZone("GMT" + authorTimeZone), + cAuthor.getTimeZone()); final PersonIdent cCommitter = c.getCommitterIdent(); assertNotNull(cCommitter); assertEquals(committerName, cCommitter.getName()); assertEquals(committerEmail, cCommitter.getEmailAddress()); - assertEquals((long)committerTime * 1000, cCommitter.getWhen().getTime()); - assertEquals(TimeZone.getTimeZone("GMT" + committerTimeZone), cCommitter.getTimeZone()); + assertEquals((long) committerTime * 1000, + cCommitter.getWhen().getTime()); + assertEquals(TimeZone.getTimeZone("GMT" + committerTimeZone), + cCommitter.getTimeZone()); } private RevCommit create(String msg) throws Exception { @@ -149,16 +152,22 @@ public class RevCommitParseTest extends RepositoryTestCase { try (RevWalk rw = new RevWalk(db)) { c.parseCanonical(rw, b.toString().getBytes(UTF_8)); } - assertEquals(new PersonIdent("", "a_u_thor@example.com", 1218123387000l, 7), c.getAuthorIdent()); - assertEquals(new PersonIdent("", "", 1218123390000l, -5), c.getCommitterIdent()); + assertEquals( + new PersonIdent("", "a_u_thor@example.com", 1218123387000l, 7), + c.getAuthorIdent()); + assertEquals(new PersonIdent("", "", 1218123390000l, -5), + c.getCommitterIdent()); } @Test public void testParse_implicit_UTF8_encoded() throws Exception { final ByteArrayOutputStream b = new ByteArrayOutputStream(); - b.write("tree 9788669ad918b6fcce64af8882fc9a81cb6aba67\n".getBytes(UTF_8)); - b.write("author F\u00f6r fattare 1218123387 +0700\n".getBytes(UTF_8)); - b.write("committer C O. Miter 1218123390 -0500\n".getBytes(UTF_8)); + b.write("tree 9788669ad918b6fcce64af8882fc9a81cb6aba67\n" + .getBytes(UTF_8)); + b.write("author F\u00f6r fattare 1218123387 +0700\n" + .getBytes(UTF_8)); + b.write("committer C O. Miter 1218123390 -0500\n" + .getBytes(UTF_8)); b.write("\n".getBytes(UTF_8)); b.write("Sm\u00f6rg\u00e5sbord\n".getBytes(UTF_8)); b.write("\n".getBytes(UTF_8)); @@ -171,15 +180,19 @@ public class RevCommitParseTest extends RepositoryTestCase { assertSame(UTF_8, c.getEncoding()); assertEquals("F\u00f6r fattare", c.getAuthorIdent().getName()); assertEquals("Sm\u00f6rg\u00e5sbord", c.getShortMessage()); - assertEquals("Sm\u00f6rg\u00e5sbord\n\n\u304d\u308c\u3044\n", c.getFullMessage()); + assertEquals("Sm\u00f6rg\u00e5sbord\n\n\u304d\u308c\u3044\n", + c.getFullMessage()); } @Test public void testParse_implicit_mixed_encoded() throws Exception { final ByteArrayOutputStream b = new ByteArrayOutputStream(); - b.write("tree 9788669ad918b6fcce64af8882fc9a81cb6aba67\n".getBytes(UTF_8)); - b.write("author F\u00f6r fattare 1218123387 +0700\n".getBytes(ISO_8859_1)); - b.write("committer C O. Miter 1218123390 -0500\n".getBytes(UTF_8)); + b.write("tree 9788669ad918b6fcce64af8882fc9a81cb6aba67\n" + .getBytes(UTF_8)); + b.write("author F\u00f6r fattare 1218123387 +0700\n" + .getBytes(ISO_8859_1)); + b.write("committer C O. Miter 1218123390 -0500\n" + .getBytes(UTF_8)); b.write("\n".getBytes(UTF_8)); b.write("Sm\u00f6rg\u00e5sbord\n".getBytes(UTF_8)); b.write("\n".getBytes(UTF_8)); @@ -192,7 +205,8 @@ public class RevCommitParseTest extends RepositoryTestCase { assertSame(UTF_8, c.getEncoding()); assertEquals("F\u00f6r fattare", c.getAuthorIdent().getName()); assertEquals("Sm\u00f6rg\u00e5sbord", c.getShortMessage()); - assertEquals("Sm\u00f6rg\u00e5sbord\n\n\u304d\u308c\u3044\n", c.getFullMessage()); + assertEquals("Sm\u00f6rg\u00e5sbord\n\n\u304d\u308c\u3044\n", + c.getFullMessage()); } /** @@ -203,9 +217,12 @@ public class RevCommitParseTest extends RepositoryTestCase { @Test public void testParse_explicit_encoded() throws Exception { final ByteArrayOutputStream b = new ByteArrayOutputStream(); - b.write("tree 9788669ad918b6fcce64af8882fc9a81cb6aba67\n".getBytes("EUC-JP")); - b.write("author F\u00f6r fattare 1218123387 +0700\n".getBytes("EUC-JP")); - b.write("committer C O. Miter 1218123390 -0500\n".getBytes("EUC-JP")); + b.write("tree 9788669ad918b6fcce64af8882fc9a81cb6aba67\n" + .getBytes("EUC-JP")); + b.write("author F\u00f6r fattare 1218123387 +0700\n" + .getBytes("EUC-JP")); + b.write("committer C O. Miter 1218123390 -0500\n" + .getBytes("EUC-JP")); b.write("encoding euc_JP\n".getBytes("EUC-JP")); b.write("\n".getBytes("EUC-JP")); b.write("\u304d\u308c\u3044\n".getBytes("EUC-JP")); @@ -235,9 +252,12 @@ public class RevCommitParseTest extends RepositoryTestCase { @Test public void testParse_explicit_bad_encoded() throws Exception { final ByteArrayOutputStream b = new ByteArrayOutputStream(); - b.write("tree 9788669ad918b6fcce64af8882fc9a81cb6aba67\n".getBytes(UTF_8)); - b.write("author F\u00f6r fattare 1218123387 +0700\n".getBytes(ISO_8859_1)); - b.write("committer C O. Miter 1218123390 -0500\n".getBytes(UTF_8)); + b.write("tree 9788669ad918b6fcce64af8882fc9a81cb6aba67\n" + .getBytes(UTF_8)); + b.write("author F\u00f6r fattare 1218123387 +0700\n" + .getBytes(ISO_8859_1)); + b.write("committer C O. Miter 1218123390 -0500\n" + .getBytes(UTF_8)); b.write("encoding EUC-JP\n".getBytes(UTF_8)); b.write("\n".getBytes(UTF_8)); b.write("\u304d\u308c\u3044\n".getBytes(UTF_8)); @@ -256,21 +276,25 @@ public class RevCommitParseTest extends RepositoryTestCase { } /** - * This is a twisted case too, but show what we expect here. We can revise the - * expectations provided this case is updated. + * This is a twisted case too, but show what we expect here. We can revise + * the expectations provided this case is updated. * * What happens here is that an encoding us given, but data is not encoded - * that way (and we can detect it), so we try other encodings. Here data could - * actually be decoded in the stated encoding, but we override using UTF-8. + * that way (and we can detect it), so we try other encodings. Here data + * could actually be decoded in the stated encoding, but we override using + * UTF-8. * * @throws Exception */ @Test public void testParse_explicit_bad_encoded2() throws Exception { final ByteArrayOutputStream b = new ByteArrayOutputStream(); - b.write("tree 9788669ad918b6fcce64af8882fc9a81cb6aba67\n".getBytes(UTF_8)); - b.write("author F\u00f6r fattare 1218123387 +0700\n".getBytes(UTF_8)); - b.write("committer C O. Miter 1218123390 -0500\n".getBytes(UTF_8)); + b.write("tree 9788669ad918b6fcce64af8882fc9a81cb6aba67\n" + .getBytes(UTF_8)); + b.write("author F\u00f6r fattare 1218123387 +0700\n" + .getBytes(UTF_8)); + b.write("committer C O. Miter 1218123390 -0500\n" + .getBytes(UTF_8)); b.write("encoding ISO-8859-1\n".getBytes(UTF_8)); b.write("\n".getBytes(UTF_8)); b.write("\u304d\u308c\u3044\n".getBytes(UTF_8)); @@ -319,9 +343,11 @@ public class RevCommitParseTest extends RepositoryTestCase { @Test public void testParse_illegalEncoding() throws Exception { ByteArrayOutputStream b = new ByteArrayOutputStream(); - b.write("tree 9788669ad918b6fcce64af8882fc9a81cb6aba67\n".getBytes(UTF_8)); + b.write("tree 9788669ad918b6fcce64af8882fc9a81cb6aba67\n" + .getBytes(UTF_8)); b.write("author au 1218123387 +0700\n".getBytes(UTF_8)); - b.write("committer co 1218123390 -0500\n".getBytes(UTF_8)); + b.write("committer co 1218123390 -0500\n" + .getBytes(UTF_8)); b.write("encoding utf-8logoutputencoding=gbk\n".getBytes(UTF_8)); b.write("\n".getBytes(UTF_8)); b.write("message\n".getBytes(UTF_8)); @@ -348,9 +374,11 @@ public class RevCommitParseTest extends RepositoryTestCase { @Test public void testParse_unsupportedEncoding() throws Exception { ByteArrayOutputStream b = new ByteArrayOutputStream(); - b.write("tree 9788669ad918b6fcce64af8882fc9a81cb6aba67\n".getBytes(UTF_8)); + b.write("tree 9788669ad918b6fcce64af8882fc9a81cb6aba67\n" + .getBytes(UTF_8)); b.write("author au 1218123387 +0700\n".getBytes(UTF_8)); - b.write("committer co 1218123390 -0500\n".getBytes(UTF_8)); + b.write("committer co 1218123390 -0500\n" + .getBytes(UTF_8)); b.write("encoding it_IT.UTF8\n".getBytes(UTF_8)); b.write("\n".getBytes(UTF_8)); b.write("message\n".getBytes(UTF_8)); @@ -474,21 +502,18 @@ public class RevCommitParseTest extends RepositoryTestCase { @Test public void testParse_gpgSig() throws Exception { - String commit = "tree e3a1035abd2b319bb01e57d69b0ba6cab289297e\n" + - "parent 54e895b87c0768d2317a2b17062e3ad9f76a8105\n" + - "committer A U Thor tr; + + private RevWalk rw; + + @Before + public void setUp() throws Exception { + tr = new TestRepository<>( + new InMemoryRepository(new DfsRepositoryDescription("test"))); + rw = tr.getRevWalk(); + } + + @Test + public void testParseBody() throws Exception { + RevCommit a = tr.commit().add("a", "foo").create(); + RevCommit b = tr.commit().parent(a).add("b", "bar").create(); + RevCommit c = tr.commit().parent(b).message("commit3").add("a", "foo'") + .create(); + + RevCommit cBar = new RevCommit(c.getId()) { + @Override + public int getParentCount() { + return 1; + } + + @Override + public RevCommit getParent(int nth) { + return a; + } + + @Override + public RevCommit[] getParents() { + return new RevCommit[] { a }; + } + }; + + rw.parseBody(cBar); + assertEquals(a, cBar.getParents()[0]); + assertEquals("commit3", cBar.getFullMessage()); + assertEquals("foo'", blobAsString(cBar, "a")); + } + + @Test + public void testParseHeader() throws Exception { + RevCommit a = tr.commit().add("a", "foo").create(); + RevCommit b = tr.commit().parent(a).add("b", "bar").create(); + RevCommit c = tr.commit().parent(b).message("commit3").add("a", "foo'") + .create(); + + RevCommit cBar = new RevCommit(c.getId()) { + @Override + public int getParentCount() { + return 1; + } + + @Override + public RevCommit getParent(int nth) { + return a; + } + + @Override + public RevCommit[] getParents() { + return new RevCommit[] { a }; + } + }; + + RevCommit parsed = rw.parseCommit(cBar.getId()); + rw.parseHeaders(cBar); + + assertEquals(c.getId(), parsed.getId()); + assertEquals(parsed.getTree(), cBar.getTree()); + assertEquals(parsed.getCommitTime(), cBar.getCommitTime()); + assertEquals(parsed.getAuthorIdent(), cBar.getAuthorIdent()); + } + + @Test + public void testFilter() throws Exception { + RevCommit a = tr.commit().add("a", "foo").create(); + RevCommit b = tr.commit().parent(a).add("b", "bar").create(); + RevCommit c = tr.commit().parent(b).message("commit3").add("a", "foo'") + .create(); + + RevCommit cBar = new RevCommit(c.getId()) { + @Override + public int getParentCount() { + return 1; + } + + @Override + public RevCommit getParent(int nth) { + return a; + } + + @Override + public RevCommit[] getParents() { + return new RevCommit[] { a }; + } + }; + + rw.setRevFilter(RevFilter.ALL); + rw.markStart(cBar); + assertSame(cBar, rw.next()); + assertSame(a, rw.next()); + assertNull(rw.next()); + } + + @Test + public void testFlag() throws Exception { + RevCommit root = tr.commit().add("todelete", "to be deleted").create(); + RevCommit orig = tr.commit().parent(root).rm("todelete") + .add("foo", "foo contents").add("bar", "bar contents") + .add("dir/baz", "baz contents").create(); + + RevCommit commitOrigBar = new RevCommit(orig.getId()) { + @Override + public int getParentCount() { + return 1; + } + + @Override + public RevCommit getParent(int nth) { + return root; + } + + @Override + public RevCommit[] getParents() { + return new RevCommit[] { root }; + } + }; + + assertEquals(RevObject.PARSED, orig.flags); + assertEquals(0, commitOrigBar.flags); + commitOrigBar.parseBody(rw); + assertEquals(RevObject.PARSED, commitOrigBar.flags); + } + + private String blobAsString(AnyObjectId treeish, String path) + throws Exception { + RevObject obj = tr.get(rw.parseTree(treeish), path); + assertSame(RevBlob.class, obj.getClass()); + ObjectLoader loader = rw.getObjectReader().open(obj); + return new String(loader.getCachedBytes(), UTF_8); + } +} diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevWalkCullTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevWalkCullTest.java index 722b2d2927..7b0e2b2267 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevWalkCullTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevWalkCullTest.java @@ -75,6 +75,6 @@ public class RevWalkCullTest extends RevWalkTestCase { // We should have aborted before we got back so far that "a" // would be parsed. Thus, its parents shouldn't be allocated. // - assertNull(a2.parents); + assertNull(a2.getParents()); } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/AbstractRevQueue.java b/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/AbstractRevQueue.java index e0c7bdd396..dda108bc69 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/AbstractRevQueue.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/AbstractRevQueue.java @@ -66,7 +66,7 @@ abstract class AbstractRevQueue extends Generator { * flag that controls admission to the queue. */ public final void addParents(RevCommit c, RevFlag queueControl) { - final RevCommit[] pList = c.parents; + final RevCommit[] pList = c.getParents(); if (pList == null) { return; } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/BoundaryGenerator.java b/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/BoundaryGenerator.java index 8b78d062b5..bdf63ff10b 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/BoundaryGenerator.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/BoundaryGenerator.java @@ -76,11 +76,12 @@ class BoundaryGenerator extends Generator { IncorrectObjectTypeException, IOException { RevCommit c = source.next(); if (c != null) { - for (int i = 0; i < c.parents.length; i++) { + int n = c.getParentCount(); + for (int i = 0; i < n; i++) { if (firstParent && i > 0) { break; } - RevCommit p = c.parents[i]; + RevCommit p = c.getParent(i); if ((p.flags & UNINTERESTING) != 0) { held.add(p); } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/DepthGenerator.java b/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/DepthGenerator.java index 3a2cb8b0f9..ec0824cb0b 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/DepthGenerator.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/DepthGenerator.java @@ -164,11 +164,12 @@ class DepthGenerator extends Generator { int newDepth = c.depth + 1; - for (int i = 0; i < c.parents.length; i++) { + int n = c.getParentCount(); + for (int i = 0; i < n; i++) { if (firstParent && i > 0) { break; } - RevCommit p = c.parents[i]; + RevCommit p = c.getParent(i); DepthWalk.Commit dp = (DepthWalk.Commit) p; // If no depth has been assigned to this commit, assign diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/MergeBaseGenerator.java b/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/MergeBaseGenerator.java index c0fea75777..a213dd47c6 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/MergeBaseGenerator.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/MergeBaseGenerator.java @@ -114,7 +114,7 @@ class MergeBaseGenerator extends Generator { return null; } - for (RevCommit p : c.parents) { + for (RevCommit p : c.getParents()) { if ((p.flags & IN_PENDING) != 0) continue; if ((p.flags & PARSED) == 0) @@ -180,7 +180,7 @@ class MergeBaseGenerator extends Generator { private void carryOntoHistoryInnerLoop(RevCommit c, int carry) { for (;;) { - RevCommit[] parents = c.parents; + RevCommit[] parents = c.getParents(); if (parents == null || parents.length == 0) { break; } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/PendingGenerator.java b/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/PendingGenerator.java index add387de03..a49f787316 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/PendingGenerator.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/PendingGenerator.java @@ -108,8 +108,9 @@ class PendingGenerator extends Generator { produce = filter.include(walker, c); } - for (int i = 0; i < c.parents.length; i++) { - RevCommit p = c.parents[i]; + int parentCount = c.getParentCount(); + for (int i = 0; i < parentCount; i++) { + RevCommit p = c.getParent(i); // If the commit is uninteresting, don't try to prune // parents because we want the maximal uninteresting set. if (firstParent && i > 0 && (c.flags & UNINTERESTING) == 0) { diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevCommit.java b/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevCommit.java index 82725f3db9..7a74e314f6 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevCommit.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevCommit.java @@ -102,7 +102,12 @@ public class RevCommit extends RevObject { private RevTree tree; - RevCommit[] parents; + /** + * Avoid accessing this field directly. Use method + * {@link RevCommit#getParents()} instead. RevCommit does not allow parents + * to be overridden and altering parent(s) is not supported. + */ + protected RevCommit[] parents; int commitTime; // An int here for performance, overflows in 2038 @@ -146,7 +151,7 @@ public class RevCommit extends RevObject { tree = walk.lookupTree(idBuffer); int ptr = 46; - if (parents == null) { + if (getParents() == null) { RevCommit[] pList = new RevCommit[1]; int nParents = 0; for (;;) { @@ -210,8 +215,8 @@ public class RevCommit extends RevObject { } private static FIFORevQueue carryFlags1(RevCommit c, int carry, int depth) { - for(;;) { - RevCommit[] pList = c.parents; + for (;;) { + RevCommit[] pList = c.getParents(); if (pList == null || pList.length == 0) return null; if (pList.length != 1) { @@ -259,7 +264,7 @@ public class RevCommit extends RevObject { // Commits in q have non-null parent arrays and have set all // flags in carry. This loop finishes copying over the graph. for (RevCommit c; (c = q.next()) != null;) { - for (RevCommit p : c.parents) + for (RevCommit p : c.getParents()) carryOneStep(q, carry, p); } } @@ -267,7 +272,7 @@ public class RevCommit extends RevObject { private static void carryOneStep(FIFORevQueue q, int carry, RevCommit c) { if ((c.flags & carry) != carry) { c.flags |= carry; - if (c.parents != null) + if (c.getParents() != null) q.add(c); } } @@ -313,8 +318,8 @@ public class RevCommit extends RevObject { * * @return number of parents; always a positive value but can be 0. */ - public final int getParentCount() { - return parents.length; + public int getParentCount() { + return parents == null ? 0 : parents.length; } /** @@ -327,7 +332,7 @@ public class RevCommit extends RevObject { * @throws java.lang.ArrayIndexOutOfBoundsException * an invalid parent index was specified. */ - public final RevCommit getParent(int nth) { + public RevCommit getParent(int nth) { return parents[nth]; } @@ -341,7 +346,7 @@ public class RevCommit extends RevObject { * * @return the array of parents. */ - public final RevCommit[] getParents() { + public RevCommit[] getParents() { return parents; } @@ -353,9 +358,9 @@ public class RevCommit extends RevObject { * this buffer should be very careful to ensure they do not modify its * contents during their use of it. * - * @return the raw unparsed commit body. This is NOT A COPY. - * Altering the contents of this buffer may alter the walker's - * knowledge of this commit, and the results it produces. + * @return the raw unparsed commit body. This is NOT A COPY. Altering + * the contents of this buffer may alter the walker's knowledge of + * this commit, and the results it produces. */ public final byte[] getRawBuffer() { return buffer; @@ -380,7 +385,7 @@ public class RevCommit extends RevObject { */ public final byte[] getRawGpgSignature() { final byte[] raw = buffer; - final byte[] header = {'g', 'p', 'g', 's', 'i', 'g'}; + final byte[] header = { 'g', 'p', 'g', 's', 'i', 'g' }; final int start = RawParseUtils.headerStart(header, raw, 0); if (start < 0) { return null; diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevWalk.java b/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevWalk.java index a25948e50b..8da36c5243 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevWalk.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RevWalk.java @@ -154,7 +154,9 @@ public class RevWalk implements Iterable, AutoCloseable { */ static final int TREE_REV_FILTER_APPLIED = 1 << 7; - /** Number of flag bits we keep internal for our own use. See above flags. */ + /** + * Number of flag bits we keep internal for our own use. See above flags. + */ static final int RESERVED_FLAGS = 8; private static final int APP_FLAGS = -1 & ~((1 << RESERVED_FLAGS) - 1); @@ -196,9 +198,7 @@ public class RevWalk implements Iterable, AutoCloseable { boolean shallowCommitsInitialized; private enum GetMergedIntoStrategy { - RETURN_ON_FIRST_FOUND, - RETURN_ON_FIRST_NOT_FOUND, - EVALUATE_ALL + RETURN_ON_FIRST_FOUND, RETURN_ON_FIRST_NOT_FOUND, EVALUATE_ALL } /** @@ -219,8 +219,8 @@ public class RevWalk implements Iterable, AutoCloseable { * * @param or * the reader the walker will obtain data from. The reader is not - * closed when the walker is closed (but is closed by {@link - * #dispose()}. + * closed when the walker is closed (but is closed by + * {@link #dispose()}. */ public RevWalk(ObjectReader or) { this(or, false); @@ -381,9 +381,8 @@ public class RevWalk implements Iterable, AutoCloseable { * @throws java.io.IOException * a pack file or loose object could not be read. */ - public void markUninteresting(RevCommit c) - throws MissingObjectException, IncorrectObjectTypeException, - IOException { + public void markUninteresting(RevCommit c) throws MissingObjectException, + IncorrectObjectTypeException, IOException { c.flags |= UNINTERESTING; carryFlagsImpl(c); markStart(c); @@ -392,8 +391,8 @@ public class RevWalk implements Iterable, AutoCloseable { /** * Determine if a commit is reachable from another commit. *

- * A commit base is an ancestor of tip if we - * can find a path of commits that leads from tip and ends at + * A commit base is an ancestor of tip if we can + * find a path of commits that leads from tip and ends at * base. *

* This utility function resets the walker, inserts the two supplied @@ -462,7 +461,7 @@ public class RevWalk implements Iterable, AutoCloseable { * @since 5.12 */ public List getMergedInto(RevCommit commit, Collection refs) - throws IOException{ + throws IOException { return getMergedInto(commit, refs, NullProgressMonitor.INSTANCE); } @@ -486,9 +485,8 @@ public class RevWalk implements Iterable, AutoCloseable { * @since 5.12 */ public List getMergedInto(RevCommit commit, Collection refs, - ProgressMonitor monitor) throws IOException{ - return getMergedInto(commit, refs, - GetMergedIntoStrategy.EVALUATE_ALL, + ProgressMonitor monitor) throws IOException { + return getMergedInto(commit, refs, GetMergedIntoStrategy.EVALUATE_ALL, monitor); } @@ -531,12 +529,11 @@ public class RevWalk implements Iterable, AutoCloseable { throws IOException { return getMergedInto(commit, refs, GetMergedIntoStrategy.RETURN_ON_FIRST_NOT_FOUND, - NullProgressMonitor.INSTANCE).size() - == refs.size(); + NullProgressMonitor.INSTANCE).size() == refs.size(); } private List getMergedInto(RevCommit needle, Collection haystacks, - Enum returnStrategy, ProgressMonitor monitor) throws IOException { + Enum returnStrategy, ProgressMonitor monitor) throws IOException { List result = new ArrayList<>(); List uninteresting = new ArrayList<>(); List marked = new ArrayList<>(); @@ -547,7 +544,7 @@ public class RevWalk implements Iterable, AutoCloseable { reset(~freeFlags & APP_FLAGS); filter = RevFilter.ALL; treeFilter = TreeFilter.ALL; - for (Ref r: haystacks) { + for (Ref r : haystacks) { if (monitor.isCancelled()) { return result; } @@ -574,7 +571,7 @@ public class RevWalk implements Iterable, AutoCloseable { break; } } - if(!commitFound){ + if (!commitFound) { markUninteresting(c); uninteresting.add(c); if (returnStrategy == GetMergedIntoStrategy.RETURN_ON_FIRST_NOT_FOUND) { @@ -990,9 +987,8 @@ public class RevWalk implements Iterable, AutoCloseable { * a pack file or loose object could not be read. */ @NonNull - public RevCommit parseCommit(AnyObjectId id) - throws MissingObjectException, IncorrectObjectTypeException, - IOException { + public RevCommit parseCommit(AnyObjectId id) throws MissingObjectException, + IncorrectObjectTypeException, IOException { RevObject c = peel(parseAny(id)); if (!(c instanceof RevCommit)) throw new IncorrectObjectTypeException(id.toObjectId(), @@ -1018,9 +1014,8 @@ public class RevWalk implements Iterable, AutoCloseable { * a pack file or loose object could not be read. */ @NonNull - public RevTree parseTree(AnyObjectId id) - throws MissingObjectException, IncorrectObjectTypeException, - IOException { + public RevTree parseTree(AnyObjectId id) throws MissingObjectException, + IncorrectObjectTypeException, IOException { RevObject c = peel(parseAny(id)); final RevTree t; @@ -1274,8 +1269,8 @@ public class RevWalk implements Iterable, AutoCloseable { * @throws java.io.IOException * a pack file or loose object could not be read. */ - public RevObject peel(RevObject obj) throws MissingObjectException, - IOException { + public RevObject peel(RevObject obj) + throws MissingObjectException, IOException { while (obj instanceof RevTag) { parseHeaders(obj); obj = ((RevTag) obj).getObject(); @@ -1304,9 +1299,9 @@ public class RevWalk implements Iterable, AutoCloseable { int allocFlag() { if (freeFlags == 0) - throw new IllegalArgumentException(MessageFormat.format( - JGitText.get().flagsAlreadyCreated, - Integer.valueOf(32 - RESERVED_FLAGS))); + throw new IllegalArgumentException( + MessageFormat.format(JGitText.get().flagsAlreadyCreated, + Integer.valueOf(32 - RESERVED_FLAGS))); final int m = Integer.lowestOneBit(freeFlags); freeFlags &= ~m; return m; @@ -1323,9 +1318,11 @@ public class RevWalk implements Iterable, AutoCloseable { */ public void carry(RevFlag flag) { if ((freeFlags & flag.mask) != 0) - throw new IllegalArgumentException(MessageFormat.format(JGitText.get().flagIsDisposed, flag.name)); + throw new IllegalArgumentException(MessageFormat + .format(JGitText.get().flagIsDisposed, flag.name)); if (flag.walker != this) - throw new IllegalArgumentException(MessageFormat.format(JGitText.get().flagNotFromThis, flag.name)); + throw new IllegalArgumentException(MessageFormat + .format(JGitText.get().flagNotFromThis, flag.name)); carryFlags |= flag.mask; } @@ -1359,9 +1356,11 @@ public class RevWalk implements Iterable, AutoCloseable { */ public final void retainOnReset(RevFlag flag) { if ((freeFlags & flag.mask) != 0) - throw new IllegalArgumentException(MessageFormat.format(JGitText.get().flagIsDisposed, flag.name)); + throw new IllegalArgumentException(MessageFormat + .format(JGitText.get().flagIsDisposed, flag.name)); if (flag.walker != this) - throw new IllegalArgumentException(MessageFormat.format(JGitText.get().flagNotFromThis, flag.name)); + throw new IllegalArgumentException(MessageFormat + .format(JGitText.get().flagNotFromThis, flag.name)); retainOnReset |= flag.mask; } @@ -1496,9 +1495,9 @@ public class RevWalk implements Iterable, AutoCloseable { final RevCommit c = q.next(); if (c == null) break; - if (c.parents == null) + if (c.getParents() == null) continue; - for (RevCommit p : c.parents) { + for (RevCommit p : c.getParents()) { if ((p.flags & clearFlags) == 0) continue; p.flags &= retainFlags; @@ -1538,7 +1537,8 @@ public class RevWalk implements Iterable, AutoCloseable { * Like {@link #next()}, but if a checked exception is thrown during the * walk it is rethrown as a {@link RevWalkException}. * - * @throws RevWalkException if an {@link IOException} was thrown. + * @throws RevWalkException + * if an {@link IOException} was thrown. * @return next most recent commit; null if traversal is over. */ @Nullable @@ -1598,7 +1598,8 @@ public class RevWalk implements Iterable, AutoCloseable { protected void assertNotStarted() { if (isNotStarted()) return; - throw new IllegalStateException(JGitText.get().outputHasAlreadyBeenStarted); + throw new IllegalStateException( + JGitText.get().outputHasAlreadyBeenStarted); } /** diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RewriteGenerator.java b/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RewriteGenerator.java index 1adef07ad9..2c88bb872e 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RewriteGenerator.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RewriteGenerator.java @@ -72,7 +72,7 @@ class RewriteGenerator extends Generator { applyFilterToParents(c); boolean rewrote = false; - final RevCommit[] pList = c.parents; + final RevCommit[] pList = c.getParents(); final int nParents = pList.length; for (int i = 0; i < nParents; i++) { final RevCommit oldp = pList[i]; @@ -108,7 +108,7 @@ class RewriteGenerator extends Generator { private void applyFilterToParents(RevCommit c) throws MissingObjectException, IncorrectObjectTypeException, IOException { - for (RevCommit parent : c.parents) { + for (RevCommit parent : c.getParents()) { while ((parent.flags & RevWalk.TREE_REV_FILTER_APPLIED) == 0) { RevCommit n = source.next(); @@ -130,7 +130,7 @@ class RewriteGenerator extends Generator { IncorrectObjectTypeException, IOException { for (;;) { - if (p.parents.length > 1) { + if (p.getParentCount() > 1) { // This parent is a merge, so keep it. // return p; @@ -150,15 +150,15 @@ class RewriteGenerator extends Generator { return p; } - if (p.parents.length == 0) { + if (p.getParentCount() == 0) { // We can't go back any further, other than to // just delete the parent entirely. // return null; } - applyFilterToParents(p.parents[0]); - p = p.parents[0]; + applyFilterToParents(p.getParent(0)); + p = p.getParent(0); } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/TopoNonIntermixSortGenerator.java b/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/TopoNonIntermixSortGenerator.java index 4f6d417ed1..452545a04e 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/TopoNonIntermixSortGenerator.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/TopoNonIntermixSortGenerator.java @@ -48,7 +48,7 @@ class TopoNonIntermixSortGenerator extends Generator { break; } if ((c.flags & TOPO_QUEUED) == 0) { - for (RevCommit p : c.parents) { + for (RevCommit p : c.getParents()) { p.inDegree++; if (firstParent) { @@ -94,7 +94,7 @@ class TopoNonIntermixSortGenerator extends Generator { continue; } - for (RevCommit p : c.parents) { + for (RevCommit p : c.getParents()) { if (--p.inDegree == 0 && (p.flags & TOPO_QUEUED) != 0) { // The parent has no unproduced interesting children. unpop // the parent so it goes right behind this child. This means diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/TopoSortGenerator.java b/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/TopoSortGenerator.java index 7a5db43a7a..4739f78fc0 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/TopoSortGenerator.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/TopoSortGenerator.java @@ -47,7 +47,7 @@ class TopoSortGenerator extends Generator { if (c == null) { break; } - for (RevCommit p : c.parents) { + for (RevCommit p : c.getParents()) { p.inDegree++; if (firstParent) { break; @@ -86,7 +86,7 @@ class TopoSortGenerator extends Generator { // All of our children have already produced, // so it is OK for us to produce now as well. // - for (RevCommit p : c.parents) { + for (RevCommit p : c.getParents()) { if (--p.inDegree == 0 && (p.flags & TOPO_DELAY) != 0) { // This parent tried to come before us, but we are // his last child. unpop the parent so it goes right 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 92d72268d1..f921449ffa 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/TreeRevFilter.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/TreeRevFilter.java @@ -44,6 +44,7 @@ public class TreeRevFilter extends RevFilter { private static final int FILTER_APPLIED = RevWalk.TREE_REV_FILTER_APPLIED; private final int rewriteFlag; + private final TreeWalk pathFilter; /** @@ -62,10 +63,9 @@ public class TreeRevFilter extends RevFilter { this(walker, t, 0); } - /** - * Create a filter for the first phase of a parent-rewriting limited revision - * walk. + * Create a filter for the first phase of a parent-rewriting limited + * revision walk. *

* This filter is ANDed to evaluate before all other filters and ties the * configured {@link TreeFilter} into the revision walking process. @@ -79,8 +79,8 @@ public class TreeRevFilter extends RevFilter { * @param walker * walker used for reading trees. * @param t - * filter to compare against any changed paths in each commit. If a - * {@link FollowFilter}, will be replaced with a new filter + * filter to compare against any changed paths in each commit. If + * a {@link FollowFilter}, will be replaced with a new filter * following new paths after a rename. * @param rewriteFlag * flag to color commits to be removed from the simplified DAT. @@ -106,12 +106,12 @@ public class TreeRevFilter extends RevFilter { c.flags |= FILTER_APPLIED; // Reset the tree filter to scan this commit and parents. // - RevCommit[] pList = c.parents; + RevCommit[] pList = c.getParents(); int nParents = pList.length; TreeWalk tw = pathFilter; ObjectId[] trees = new ObjectId[nParents + 1]; for (int i = 0; i < nParents; i++) { - RevCommit p = c.parents[i]; + RevCommit p = c.getParent(i); if ((p.flags & PARSED) == 0) { p.parseHeaders(walker); } -- 2.39.5