diff options
author | Dave Borowitz <dborowitz@google.com> | 2015-03-11 12:04:10 -0700 |
---|---|---|
committer | Dave Borowitz <dborowitz@google.com> | 2015-03-12 12:46:24 -0700 |
commit | da85ca73ff8cf79da3f13542a93794a1929a8f44 (patch) | |
tree | b27444f6a7b817aad1b19737220efb42e8caafad /org.eclipse.jgit.test/tst/org/eclipse/jgit/junit/TestRepositoryTest.java | |
parent | d79cadb3cf4bb26c01bc5d4795b05858bb25ef42 (diff) | |
download | jgit-da85ca73ff8cf79da3f13542a93794a1929a8f44.tar.gz jgit-da85ca73ff8cf79da3f13542a93794a1929a8f44.zip |
TestRepository: Add methods to amend commits or refs
Change-Id: I47082416f6e281262b160ba15272258f9109abd1
Diffstat (limited to 'org.eclipse.jgit.test/tst/org/eclipse/jgit/junit/TestRepositoryTest.java')
-rw-r--r-- | org.eclipse.jgit.test/tst/org/eclipse/jgit/junit/TestRepositoryTest.java | 106 |
1 files changed, 105 insertions, 1 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/junit/TestRepositoryTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/junit/TestRepositoryTest.java index c14b32e6cc..cc8ed504d0 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/junit/TestRepositoryTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/junit/TestRepositoryTest.java @@ -43,19 +43,29 @@ package org.eclipse.jgit.junit; +import static java.nio.charset.StandardCharsets.UTF_8; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; +import java.util.Date; import java.util.regex.Pattern; import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription; import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository; import org.eclipse.jgit.lib.ObjectId; +import org.eclipse.jgit.lib.AnyObjectId; +import org.eclipse.jgit.lib.Constants; +import org.eclipse.jgit.lib.ObjectLoader; +import org.eclipse.jgit.lib.PersonIdent; import org.eclipse.jgit.lib.Ref; +import org.eclipse.jgit.revwalk.RevBlob; import org.eclipse.jgit.revwalk.RevCommit; +import org.eclipse.jgit.revwalk.RevObject; import org.eclipse.jgit.revwalk.RevWalk; +import org.eclipse.jgit.treewalk.TreeWalk; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -76,7 +86,7 @@ public class TestRepositoryTest { @After public void tearDown() { rw.close(); - tr.getRepository().close(); + repo.close(); } @Test @@ -168,4 +178,98 @@ public class TestRepositoryTest { assertEquals(detached, head.getObjectId()); assertFalse(head.isSymbolic()); } + + @Test + public void amendRef() 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(); + rw.parseBody(orig); + tr.branch("master").update(orig); + assertEquals("foo contents", blobAsString(orig, "foo")); + assertEquals("bar contents", blobAsString(orig, "bar")); + assertEquals("baz contents", blobAsString(orig, "dir/baz")); + + RevCommit amended = tr.amendRef("master") + .tick(3) + .add("bar", "fixed bar contents") + .create(); + assertEquals(amended, repo.getRef("refs/heads/master").getObjectId()); + rw.parseBody(amended); + + assertEquals(1, amended.getParentCount()); + assertEquals(root, amended.getParent(0)); + assertEquals(orig.getFullMessage(), amended.getFullMessage()); + assertEquals(orig.getAuthorIdent(), amended.getAuthorIdent()); + + // Committer name/email is the same, but time was incremented. + assertEquals(new PersonIdent(orig.getCommitterIdent(), new Date(0)), + new PersonIdent(amended.getCommitterIdent(), new Date(0))); + assertTrue(orig.getCommitTime() < amended.getCommitTime()); + + assertEquals("foo contents", blobAsString(amended, "foo")); + assertEquals("fixed bar contents", blobAsString(amended, "bar")); + assertEquals("baz contents", blobAsString(amended, "dir/baz")); + assertNull(TreeWalk.forPath(repo, "todelete", amended.getTree())); + } + + @Test + public void amendHead() throws Exception { + repo.updateRef("HEAD").link("refs/heads/master"); + RevCommit root = tr.commit() + .add("foo", "foo contents") + .create(); + RevCommit orig = tr.commit().parent(root) + .message("original message") + .add("bar", "bar contents") + .create(); + tr.branch("master").update(orig); + + RevCommit amended = tr.amendRef("HEAD") + .add("foo", "fixed foo contents") + .create(); + + Ref head = repo.getRef(Constants.HEAD); + assertEquals(amended, head.getObjectId()); + assertTrue(head.isSymbolic()); + assertEquals("refs/heads/master", head.getTarget().getName()); + + rw.parseBody(amended); + assertEquals("original message", amended.getFullMessage()); + assertEquals("fixed foo contents", blobAsString(amended, "foo")); + assertEquals("bar contents", blobAsString(amended, "bar")); + } + + @Test + public void amendCommit() throws Exception { + RevCommit root = tr.commit() + .add("foo", "foo contents") + .create(); + RevCommit orig = tr.commit().parent(root) + .message("original message") + .add("bar", "bar contents") + .create(); + RevCommit amended = tr.amend(orig.copy()) + .add("foo", "fixed foo contents") + .create(); + + rw.parseBody(amended); + assertEquals("original message", amended.getFullMessage()); + assertEquals("fixed foo contents", blobAsString(amended, "foo")); + assertEquals("bar contents", blobAsString(amended, "bar")); + } + + 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); + } } |