summaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.junit/src/org
diff options
context:
space:
mode:
authorDave Borowitz <dborowitz@google.com>2015-03-11 12:04:10 -0700
committerDave Borowitz <dborowitz@google.com>2015-03-12 12:46:24 -0700
commitda85ca73ff8cf79da3f13542a93794a1929a8f44 (patch)
treeb27444f6a7b817aad1b19737220efb42e8caafad /org.eclipse.jgit.junit/src/org
parentd79cadb3cf4bb26c01bc5d4795b05858bb25ef42 (diff)
downloadjgit-da85ca73ff8cf79da3f13542a93794a1929a8f44.tar.gz
jgit-da85ca73ff8cf79da3f13542a93794a1929a8f44.zip
TestRepository: Add methods to amend commits or refs
Change-Id: I47082416f6e281262b160ba15272258f9109abd1
Diffstat (limited to 'org.eclipse.jgit.junit/src/org')
-rw-r--r--org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java99
1 files changed, 87 insertions, 12 deletions
diff --git a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java
index 479ac7e5cf..6abd34d6f1 100644
--- a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java
+++ b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java
@@ -436,6 +436,72 @@ public class TestRepository<R extends Repository> {
}
/**
+ * Amend an existing ref.
+ *
+ * @param ref
+ * the name of the reference to amend, which must already exist.
+ * If {@code ref} does not start with {@code refs/} and is not the
+ * magic names {@code HEAD} {@code FETCH_HEAD} or {@code
+ * MERGE_HEAD}, then {@code refs/heads/} will be prefixed in front
+ * of the given name, thereby assuming it is a branch.
+ * @return commit builder that amends the branch on commit.
+ * @throws Exception
+ */
+ public CommitBuilder amendRef(String ref) throws Exception {
+ String name = normalizeRef(ref);
+ Ref r = db.getRef(name);
+ if (r == null)
+ throw new IOException("Not a ref: " + ref);
+ return amend(pool.parseCommit(r.getObjectId()), branch(name).commit());
+ }
+
+ /**
+ * Amend an existing commit.
+ *
+ * @param id
+ * the id of the commit to amend.
+ * @return commit builder.
+ * @throws Exception
+ */
+ public CommitBuilder amend(AnyObjectId id) throws Exception {
+ return amend(pool.parseCommit(id), commit());
+ }
+
+ private CommitBuilder amend(RevCommit old, CommitBuilder b) throws Exception {
+ pool.parseBody(old);
+ b.author(old.getAuthorIdent());
+ b.committer(old.getCommitterIdent());
+ b.message(old.getFullMessage());
+ // Use the committer name from the old commit, but update it after ticking
+ // the clock in CommitBuilder#create().
+ b.updateCommitterTime = true;
+
+ // Reset parents to original parents.
+ b.noParents();
+ for (int i = 0; i < old.getParentCount(); i++)
+ b.parent(old.getParent(i));
+
+ // Reset tree to original tree; resetting parents reset tree contents to the
+ // first parent.
+ b.tree.clear();
+ try (TreeWalk tw = new TreeWalk(db)) {
+ tw.reset(old.getTree());
+ tw.setRecursive(true);
+ while (tw.next()) {
+ b.edit(new PathEdit(tw.getPathString()) {
+ @Override
+ public void apply(DirCacheEntry ent) {
+ ent.setFileMode(tw.getFileMode(0));
+ ent.setObjectId(tw.getObjectId(0));
+ }
+ });
+ }
+ }
+
+ return b;
+ }
+
+ /**
* Update a reference to point to an object.
*
* @param <T>
@@ -452,17 +518,7 @@ public class TestRepository<R extends Repository> {
* @throws Exception
*/
public <T extends AnyObjectId> T update(String ref, T obj) throws Exception {
- if (Constants.HEAD.equals(ref)) {
- // nothing
- } else if ("FETCH_HEAD".equals(ref)) {
- // nothing
- } else if ("MERGE_HEAD".equals(ref)) {
- // nothing
- } else if (ref.startsWith(Constants.R_REFS)) {
- // nothing
- } else
- ref = Constants.R_HEADS + ref;
-
+ ref = normalizeRef(ref);
RefUpdate u = db.updateRef(ref);
u.setNewObjectId(obj);
switch (u.forceUpdate()) {
@@ -478,6 +534,20 @@ public class TestRepository<R extends Repository> {
}
}
+ private static String normalizeRef(String ref) {
+ if (Constants.HEAD.equals(ref)) {
+ // nothing
+ } else if ("FETCH_HEAD".equals(ref)) {
+ // nothing
+ } else if ("MERGE_HEAD".equals(ref)) {
+ // nothing
+ } else if (ref.startsWith(Constants.R_REFS)) {
+ // nothing
+ } else
+ ref = Constants.R_HEADS + ref;
+ return ref;
+ }
+
/**
* Soft-reset HEAD to a detached state.
* <p>
@@ -807,6 +877,8 @@ public class TestRepository<R extends Repository> {
private boolean insertChangeId;
+ private boolean updateCommitterTime;
+
CommitBuilder() {
branch = null;
}
@@ -930,8 +1002,11 @@ public class TestRepository<R extends Repository> {
setAuthorAndCommitter(c);
if (author != null)
c.setAuthor(author);
- if (committer != null)
+ if (committer != null) {
+ if (updateCommitterTime)
+ committer = new PersonIdent(committer, new Date(now));
c.setCommitter(committer);
+ }
ObjectId commitId;
try (ObjectInserter ins = inserter) {