diff options
author | Dave Borowitz <dborowitz@google.com> | 2015-03-11 15:21:48 -0700 |
---|---|---|
committer | Dave Borowitz <dborowitz@google.com> | 2015-03-12 12:45:46 -0700 |
commit | d79cadb3cf4bb26c01bc5d4795b05858bb25ef42 (patch) | |
tree | b2f1dc2c3997824401b867933adc9e2aa4a9fa86 /org.eclipse.jgit.test/tst | |
parent | c1d40caa320ff5b22beab97702acd6e39bb02c83 (diff) | |
download | jgit-d79cadb3cf4bb26c01bc5d4795b05858bb25ef42.tar.gz jgit-d79cadb3cf4bb26c01bc5d4795b05858bb25ef42.zip |
TestRepository: Add a reset method to move HEAD around
This flushed out a number of bugs in the way DfsRefUpdate, or at least
the InMemoryRepository implementation, processes symrefs. These have
been fixed, to an extent, in InMemoryRepository, but other
implementations may still suffer from these bugs.
Change-Id: Ifd12115a0060b9ff45a88d305b72f91ca0472f9a
Diffstat (limited to 'org.eclipse.jgit.test/tst')
-rw-r--r-- | org.eclipse.jgit.test/tst/org/eclipse/jgit/junit/TestRepositoryTest.java | 83 |
1 files changed, 83 insertions, 0 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 d2ad0d9a67..c14b32e6cc 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 @@ -44,12 +44,16 @@ package org.eclipse.jgit.junit; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; 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.Ref; import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevWalk; import org.junit.After; @@ -58,12 +62,14 @@ import org.junit.Test; public class TestRepositoryTest { private TestRepository<InMemoryRepository> tr; + private InMemoryRepository repo; private RevWalk rw; @Before public void setUp() throws Exception { tr = new TestRepository<>(new InMemoryRepository( new DfsRepositoryDescription("test"))); + repo = tr.getRepository(); rw = tr.getRevWalk(); } @@ -85,4 +91,81 @@ public class TestRepositoryTest { assertEquals("\n\nChange-Id: I0000000000000000000000000000000000000000\n", c2.getFullMessage()); } + + @Test + public void resetFromSymref() throws Exception { + repo.updateRef("HEAD").link("refs/heads/master"); + Ref head = repo.getRef("HEAD"); + RevCommit master = tr.branch("master").commit().create(); + RevCommit branch = tr.branch("branch").commit().create(); + RevCommit detached = tr.commit().create(); + + assertTrue(head.isSymbolic()); + assertEquals("refs/heads/master", head.getTarget().getName()); + assertEquals(master, repo.getRef("refs/heads/master").getObjectId()); + assertEquals(branch, repo.getRef("refs/heads/branch").getObjectId()); + + // Reset to branches preserves symref. + tr.reset("master"); + head = repo.getRef("HEAD"); + assertEquals(master, head.getObjectId()); + assertTrue(head.isSymbolic()); + assertEquals("refs/heads/master", head.getTarget().getName()); + + tr.reset("branch"); + head = repo.getRef("HEAD"); + assertEquals(branch, head.getObjectId()); + assertTrue(head.isSymbolic()); + assertEquals("refs/heads/master", head.getTarget().getName()); + ObjectId lastHeadBeforeDetach = head.getObjectId().copy(); + + // Reset to a SHA-1 detaches. + tr.reset(detached); + head = repo.getRef("HEAD"); + assertEquals(detached, head.getObjectId()); + assertFalse(head.isSymbolic()); + + tr.reset(detached.name()); + head = repo.getRef("HEAD"); + assertEquals(detached, head.getObjectId()); + assertFalse(head.isSymbolic()); + + // Reset back to a branch remains detached. + tr.reset("master"); + head = repo.getRef("HEAD"); + assertEquals(lastHeadBeforeDetach, head.getObjectId()); + assertFalse(head.isSymbolic()); + } + + @Test + public void resetFromDetachedHead() throws Exception { + Ref head = repo.getRef("HEAD"); + RevCommit master = tr.branch("master").commit().create(); + RevCommit branch = tr.branch("branch").commit().create(); + RevCommit detached = tr.commit().create(); + + assertNull(head); + assertEquals(master, repo.getRef("refs/heads/master").getObjectId()); + assertEquals(branch, repo.getRef("refs/heads/branch").getObjectId()); + + tr.reset("master"); + head = repo.getRef("HEAD"); + assertEquals(master, head.getObjectId()); + assertFalse(head.isSymbolic()); + + tr.reset("branch"); + head = repo.getRef("HEAD"); + assertEquals(branch, head.getObjectId()); + assertFalse(head.isSymbolic()); + + tr.reset(detached); + head = repo.getRef("HEAD"); + assertEquals(detached, head.getObjectId()); + assertFalse(head.isSymbolic()); + + tr.reset(detached.name()); + head = repo.getRef("HEAD"); + assertEquals(detached, head.getObjectId()); + assertFalse(head.isSymbolic()); + } } |