aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSATO taichi2014-01-17 00:57:39 (EST)
committerSATO taichi2014-01-17 01:37:39 (EST)
commit2f425cf30c79917a93eeeac0ee3fcfc7af77d203 (patch)
tree85d51c2cb2c77900a1e2c4707949b7dda6854126
parent3db6e05e52b24e16fbe93376d3fd8935e5f4fc9b (diff)
downloadjgit-2f425cf30c79917a93eeeac0ee3fcfc7af77d203.zip
jgit-2f425cf30c79917a93eeeac0ee3fcfc7af77d203.tar.gz
jgit-2f425cf30c79917a93eeeac0ee3fcfc7af77d203.tar.bz2
Add git checkout --orphan implementationrefs/changes/95/20095/5
Change-Id: I7bb583674641efed210d3cd5b86af27d7bb48e97 Signed-off-by: SATO taichi <ryushi@gmail.com>
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CheckoutCommandTest.java96
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/api/CheckoutCommand.java71
2 files changed, 159 insertions, 8 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 4087fb0..614cdd0 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
+ }
+ }
+
}
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/CheckoutCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/CheckoutCommand.java
index 8dfd211..ee37bbf 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/api/CheckoutCommand.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/CheckoutCommand.java
@@ -47,6 +47,7 @@ import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
+import java.util.EnumSet;
import java.util.LinkedList;
import java.util.List;
@@ -158,6 +159,8 @@ public class CheckoutCommand extends GitCommand<Ref> {
private boolean createBranch = false;
+ private boolean orphan = false;
+
private CreateBranchCommand.SetupUpstreamMode upstreamMode;
private String startPoint = null;
@@ -197,8 +200,8 @@ public class CheckoutCommand extends GitCommand<Ref> {
RefNotFoundException, InvalidRefNameException,
CheckoutConflictException {
checkCallable();
- processOptions();
try {
+ processOptions();
if (checkoutAllPaths || !paths.isEmpty()) {
checkoutPaths();
status = new CheckoutResult(Status.OK, paths);
@@ -219,10 +222,25 @@ public class CheckoutCommand extends GitCommand<Ref> {
Ref headRef = repo.getRef(Constants.HEAD);
String shortHeadRef = getShortBranchName(headRef);
String refLogMessage = "checkout: moving from " + shortHeadRef; //$NON-NLS-1$
- ObjectId branch = repo.resolve(name);
- if (branch == null)
- throw new RefNotFoundException(MessageFormat.format(JGitText
- .get().refNotResolved, name));
+ ObjectId branch;
+ if (orphan) {
+ if (startPoint == null && startCommit == null) {
+ Result r = repo.updateRef(Constants.HEAD).link(
+ getBranchName());
+ if (!EnumSet.of(Result.NEW, Result.FORCED).contains(r))
+ throw new JGitInternalException(MessageFormat.format(
+ JGitText.get().checkoutUnexpectedResult,
+ r.name()));
+ this.status = CheckoutResult.NOT_TRIED_RESULT;
+ return repo.getRef(Constants.HEAD);
+ }
+ branch = getStartPoint();
+ } else {
+ branch = repo.resolve(name);
+ if (branch == null)
+ throw new RefNotFoundException(MessageFormat.format(
+ JGitText.get().refNotResolved, name));
+ }
RevWalk revWalk = new RevWalk(repo);
AnyObjectId headId = headRef.getObjectId();
@@ -256,7 +274,10 @@ public class CheckoutCommand extends GitCommand<Ref> {
Result updateResult;
if (ref != null)
updateResult = refUpdate.link(ref.getName());
- else {
+ else if (orphan) {
+ updateResult = refUpdate.link(getBranchName());
+ ref = repo.getRef(Constants.HEAD);
+ } else {
refUpdate.setNewObjectId(newCommit);
updateResult = refUpdate.forceUpdate();
}
@@ -465,12 +486,27 @@ public class CheckoutCommand extends GitCommand<Ref> {
return result;
}
- private void processOptions() throws InvalidRefNameException {
- if ((!checkoutAllPaths && paths.isEmpty())
+ private void processOptions() throws InvalidRefNameException,
+ RefAlreadyExistsException, IOException {
+ if (((!checkoutAllPaths && paths.isEmpty()) || orphan)
&& (name == null || !Repository
.isValidRefName(Constants.R_HEADS + name)))
throw new InvalidRefNameException(MessageFormat.format(JGitText
.get().branchNameInvalid, name == null ? "<null>" : name)); //$NON-NLS-1$
+
+ if (orphan) {
+ Ref refToCheck = repo.getRef(getBranchName());
+ if (refToCheck != null)
+ throw new RefAlreadyExistsException(MessageFormat.format(
+ JGitText.get().refAlreadyExists, name));
+ }
+ }
+
+ private String getBranchName() {
+ if (name.startsWith(Constants.R_REFS))
+ return name;
+
+ return Constants.R_HEADS + name;
}
/**
@@ -517,6 +553,25 @@ public class CheckoutCommand extends GitCommand<Ref> {
}
/**
+ * Specify whether to create a new orphan branch.
+ * <p>
+ * If <code>true</code> is used, the name of the new orphan branch must be
+ * set using {@link #setName(String)}. The commit at which to start the new
+ * orphan branch can be set using {@link #setStartPoint(String)} or
+ * {@link #setStartPoint(RevCommit)}; if not specified, HEAD is used.
+ *
+ * @param orphan
+ * if <code>true</code> a orphan branch will be created as part
+ * of the checkout to the specified start point
+ * @return this instance
+ */
+ public CheckoutCommand setOrphan(boolean orphan) {
+ checkCallable();
+ this.orphan = orphan;
+ return this;
+ }
+
+ /**
* Specify to force the ref update in case of a branch switch.
*
* @param force