targetTreeId = Optional.of(commit.getTreeId());
targetCommitId = Optional.of(commit.getId());
targetRef = Optional.absent();
} else {
Ref branch = command(BranchCreateOp.class)
.setName(targetRef.get().localName())
.setSource(commitId.toString()).call();
command(ConfigOp.class).setAction(ConfigAction.CONFIG_SET)
.setScope(ConfigScope.LOCAL)
.setName("branches." + branch.localName() + ".remote")
.setValue(remoteName).call();
command(ConfigOp.class).setAction(ConfigAction.CONFIG_SET)
.setScope(ConfigScope.LOCAL)
.setName("branches." + branch.localName() + ".merge")
.setValue(targetRef.get().getName()).call();
targetRef = Optional.of(branch);
result.setResult(CheckoutResult.Results.CHECKOUT_REMOTE_BRANCH);
result.setRemoteName(remoteName);
}
}
if (commitId.isNull()) {
targetTreeId = Optional.of(ObjectId.NULL);
targetCommitId = Optional.of(ObjectId.NULL);
} else {
Optional<RevCommit> parsed = command(RevObjectParse.class)
.setObjectId(commitId).call(RevCommit.class);
checkState(parsed.isPresent());
checkState(parsed.get() instanceof RevCommit);
RevCommit commit = parsed.get();
targetCommitId = Optional.of(commit.getId());
targetTreeId = Optional.of(commit.getTreeId());
}
} else {
final Optional<ObjectId> addressed = command(RevParse.class).setRefSpec(
branchOrCommit).call();
checkArgument(addressed.isPresent(), "source '" + branchOrCommit
+ "' not found in repository");
RevCommit commit = command(RevObjectParse.class).setObjectId(addressed.get())
.call(RevCommit.class).get();
targetTreeId = Optional.of(commit.getTreeId());
targetCommitId = Optional.of(commit.getId());
}
if (targetTreeId.isPresent()) {
if (!force) {
if (!index().isClean() || !workingTree().isClean()) {
throw new CheckoutException(StatusCode.LOCAL_CHANGES_NOT_COMMITTED);
}
}
// update work tree
ObjectId treeId = targetTreeId.get();
workingTree().updateWorkHead(treeId);
index().updateStageHead(treeId);
result.setNewTree(treeId);
if (targetRef.isPresent()) {
// update HEAD
Ref target = targetRef.get();
String refName;
if (target instanceof SymRef) {// beware of cyclic refs, peel symrefs
refName = ((SymRef) target).getTarget();
} else {
refName = target.getName();
}
command(UpdateSymRef.class).setName(Ref.HEAD).setNewValue(refName).call();
result.setNewRef(targetRef.get());
result.setOid(targetCommitId.get());
result.setResult(CheckoutResult.Results.CHECKOUT_LOCAL_BRANCH);