|
|
|
@ -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; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -516,6 +552,25 @@ public class CheckoutCommand extends GitCommand<Ref> {
|
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 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. |
|
|
|
|
* |
|
|
|
|