aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit/src/org/eclipse/jgit/revwalk
diff options
context:
space:
mode:
authorRonald Bhuleskar <funronald@google.com>2022-08-02 17:15:08 -0700
committerRonald Bhuleskar <funronald@google.com>2022-08-02 20:25:58 -0400
commitceb51a5e0e9db166e572ea7cd362795b4662b0cd (patch)
tree01891adc5e445805f43630155f5fcc3ed4d11843 /org.eclipse.jgit/src/org/eclipse/jgit/revwalk
parent61b4d105e4924bf15c07f5bc6f1ab0d23b50bff1 (diff)
downloadjgit-ceb51a5e0e9db166e572ea7cd362795b4662b0cd.tar.gz
jgit-ceb51a5e0e9db166e572ea7cd362795b4662b0cd.zip
Adds FilteredRevCommit that can overwrites its parents in the DAG.
Change-Id: I2df9843dde0f589f5fea6cedaaff52e313eea6de
Diffstat (limited to 'org.eclipse.jgit/src/org/eclipse/jgit/revwalk')
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/revwalk/FilteredRevCommit.java115
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RewriteGenerator.java6
2 files changed, 118 insertions, 3 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/FilteredRevCommit.java b/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/FilteredRevCommit.java
new file mode 100644
index 0000000000..be6e57fd5f
--- /dev/null
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/FilteredRevCommit.java
@@ -0,0 +1,115 @@
+/*
+ * Copyright (C) 2022, Google LLC.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Distribution License v. 1.0 which is available at
+ * https://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+package org.eclipse.jgit.revwalk;
+
+import org.eclipse.jgit.lib.ObjectId;
+
+/** A filtered commit reference that overrides its parent in the DAG. */
+public class FilteredRevCommit extends RevCommit {
+ private RevCommit[] overriddenParents;
+
+ /**
+ * Create a new commit reference for the given id.
+ *
+ * @param id
+ * object name for the commit.
+ */
+ public FilteredRevCommit(ObjectId id) {
+ this(id, NO_PARENTS);
+ }
+
+ /**
+ * Create a new commit reference wrapping an underlying commit reference.
+ *
+ * @param commit
+ * commit that is being wrapped
+ */
+ public FilteredRevCommit(RevCommit commit) {
+ this(commit, NO_PARENTS);
+ }
+
+ /**
+ * Create a new commit reference wrapping an underlying commit reference.
+ *
+ * @param commit
+ * commit that is being wrapped
+ * @param parents
+ * overridden parents for the commit
+ */
+ public FilteredRevCommit(RevCommit commit, RevCommit... parents) {
+ this(commit.getId(), parents);
+ }
+
+ /**
+ * Create a new commit reference wrapping an underlying commit reference.
+ *
+ * @param id
+ * object name for the commit.
+ * @param parents
+ * overridden parents for the commit
+ */
+ public FilteredRevCommit(ObjectId id, RevCommit... parents) {
+ super(id);
+ this.overriddenParents = parents;
+ }
+
+ /**
+ * Update parents on the commit
+ *
+ * @param overriddenParents
+ * parents to be overwritten
+ */
+ public void setParents(RevCommit... overriddenParents) {
+ this.overriddenParents = overriddenParents;
+ }
+
+ /**
+ * Get the number of parent commits listed in this commit.
+ *
+ * @return number of parents; always a positive value but can be 0 if it has
+ * no parents.
+ */
+ @Override
+ public int getParentCount() {
+ return overriddenParents.length;
+ }
+
+ /**
+ * Get the nth parent from this commit's parent list.
+ *
+ * @param nth
+ * parent index to obtain. Must be in the range 0 through
+ * {@link #getParentCount()}-1.
+ * @return the specified parent.
+ * @throws java.lang.ArrayIndexOutOfBoundsException
+ * an invalid parent index was specified.
+ */
+ @Override
+ public RevCommit getParent(int nth) {
+ return overriddenParents[nth];
+ }
+
+ /**
+ * Obtain an array of all parents (<b>NOTE - THIS IS NOT A COPY</b>).
+ *
+ * <p>
+ * This method is exposed only to provide very fast, efficient access to
+ * this commit's parent list. Applications relying on this list should be
+ * very careful to ensure they do not modify its contents during their use
+ * of it.
+ *
+ * @return the array of parents.
+ */
+ @Override
+ public RevCommit[] getParents() {
+ return overriddenParents;
+ }
+}
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 2c88bb872e..3318a97a3f 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RewriteGenerator.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/revwalk/RewriteGenerator.java
@@ -79,9 +79,9 @@ class RewriteGenerator extends Generator {
final RevCommit newp = rewrite(oldp);
if (firstParent) {
if (newp == null) {
- c.parents = RevCommit.NO_PARENTS;
+ c = new FilteredRevCommit(c.getId());
} else {
- c.parents = new RevCommit[] { newp };
+ c = new FilteredRevCommit(c.getId(), newp);
}
return c;
}
@@ -91,7 +91,7 @@ class RewriteGenerator extends Generator {
}
}
if (rewrote) {
- c.parents = cleanup(pList);
+ c = new FilteredRevCommit(c.getId(), cleanup(pList));
}
return c;
}