summaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit
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
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')
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/api/CheckoutCommand.java71
1 files changed, 63 insertions, 8 deletions
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 8dfd211a08..ee37bbfb8c 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