summaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test
diff options
context:
space:
mode:
authorSATO taichi <ryushi@gmail.com>2014-01-17 14:57:39 +0900
committerSATO taichi <ryushi@gmail.com>2014-01-17 15:37:39 +0900
commit2f425cf30c79917a93eeeac0ee3fcfc7af77d203 (patch)
tree85d51c2cb2c77900a1e2c4707949b7dda6854126 /org.eclipse.jgit.test
parent3db6e05e52b24e16fbe93376d3fd8935e5f4fc9b (diff)
downloadjgit-2f425cf30c79917a93eeeac0ee3fcfc7af77d203.tar.gz
jgit-2f425cf30c79917a93eeeac0ee3fcfc7af77d203.zip
Add git checkout --orphan implementation
Change-Id: I7bb583674641efed210d3cd5b86af27d7bb48e97 Signed-off-by: SATO taichi <ryushi@gmail.com>
Diffstat (limited to 'org.eclipse.jgit.test')
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CheckoutCommandTest.java96
1 files changed, 96 insertions, 0 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CheckoutCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CheckoutCommandTest.java
index 4087fb0dda..614cdd0cef 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CheckoutCommandTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CheckoutCommandTest.java
@@ -59,7 +59,9 @@ import java.io.IOException;
import org.eclipse.jgit.api.CheckoutResult.Status;
import org.eclipse.jgit.api.errors.GitAPIException;
+import org.eclipse.jgit.api.errors.InvalidRefNameException;
import org.eclipse.jgit.api.errors.JGitInternalException;
+import org.eclipse.jgit.api.errors.RefAlreadyExistsException;
import org.eclipse.jgit.api.errors.RefNotFoundException;
import org.eclipse.jgit.dircache.DirCache;
import org.eclipse.jgit.dircache.DirCacheEntry;
@@ -351,4 +353,98 @@ public class CheckoutCommandTest extends RepositoryTestCase {
assertEquals(size, entry.getLength());
assertEquals(mTime, entry.getLastModified());
}
+
+ @Test
+ public void testCheckoutOrphanBranch() throws Exception {
+ CheckoutCommand co = newOrphanBranchCommand();
+ assertCheckoutRef(co.call());
+
+ File HEAD = new File(trash, ".git/HEAD");
+ String headRef = read(HEAD);
+ assertEquals("ref: refs/heads/orphanbranch\n", headRef);
+ assertEquals(2, trash.list().length);
+
+ File heads = new File(trash, ".git/refs/heads");
+ assertEquals(2, heads.listFiles().length);
+
+ this.assertNoHead();
+ this.assertRepositoryCondition(1);
+ assertEquals(CheckoutResult.NOT_TRIED_RESULT, co.getResult());
+ }
+
+ private CheckoutCommand newOrphanBranchCommand() {
+ return git.checkout().setOrphan(true)
+ .setName("orphanbranch");
+ }
+
+ private static void assertCheckoutRef(Ref ref) {
+ assertNotNull(ref);
+ assertEquals("refs/heads/orphanbranch", ref.getTarget().getName());
+ }
+
+ private void assertNoHead() throws IOException {
+ assertNull(db.resolve("HEAD"));
+ }
+
+ private void assertRepositoryCondition(int files) throws GitAPIException {
+ org.eclipse.jgit.api.Status status = this.git.status().call();
+ assertFalse(status.isClean());
+ assertEquals(files, status.getAdded().size());
+ }
+
+ @Test
+ public void testCreateOrphanBranchWithStartCommit() throws Exception {
+ CheckoutCommand co = newOrphanBranchCommand();
+ Ref ref = co.setStartPoint(initialCommit).call();
+ assertCheckoutRef(ref);
+ assertEquals(2, trash.list().length);
+ this.assertNoHead();
+ this.assertRepositoryCondition(1);
+ }
+
+ @Test
+ public void testCreateOrphanBranchWithStartPoint() throws Exception {
+ CheckoutCommand co = newOrphanBranchCommand();
+ Ref ref = co.setStartPoint("HEAD^").call();
+ assertCheckoutRef(ref);
+
+ assertEquals(2, trash.list().length);
+ this.assertNoHead();
+ this.assertRepositoryCondition(1);
+ }
+
+ @Test
+ public void testInvalidRefName() throws Exception {
+ try {
+ git.checkout().setOrphan(true).setName("../invalidname").call();
+ fail("Should have failed");
+ } catch (InvalidRefNameException e) {
+ // except to hit here
+ }
+ }
+
+ @Test
+ public void testNullRefName() throws Exception {
+ try {
+ git.checkout().setOrphan(true).setName(null).call();
+ fail("Should have failed");
+ } catch (InvalidRefNameException e) {
+ // except to hit here
+ }
+ }
+
+ @Test
+ public void testAlreadyExists() throws Exception {
+ this.git.checkout().setCreateBranch(true).setName("orphanbranch")
+ .call();
+ this.git.checkout().setName("master").call();
+
+ try {
+ newOrphanBranchCommand().call();
+ fail("Should have failed");
+ } catch (RefAlreadyExistsException e) {
+ // except to hit here
+ }
+ }
+
}