RevTree newRoot = treeBuilder.build();
stagingDatabase().put(newRoot);
workingTree().updateWorkHead(newRoot.getId());
} else {
ObjectId metadataId = ObjectId.NULL;
Optional<NodeRef> parentNode = command(FindTreeChild.class)
.setParent(workingTree().getTree())
.setChildPath(node.get().getParentPath()).setIndex(true).call();
RevTreeBuilder treeBuilder = null;
if (parentNode.isPresent()) {
metadataId = parentNode.get().getMetadataId();
Optional<RevTree> parsed = command(RevObjectParse.class).setObjectId(
parentNode.get().getNode().getObjectId()).call(RevTree.class);
checkState(parsed.isPresent(),
"Parent tree couldn't be found in the repository.");
treeBuilder = new RevTreeBuilder(stagingDatabase(), parsed.get());
treeBuilder.remove(node.get().getNode().getName());
} else {
treeBuilder = new RevTreeBuilder(stagingDatabase());
}
treeBuilder.put(node.get().getNode());
ObjectId newTreeId = command(WriteBack.class)
.setAncestor(
workingTree().getTree().builder(stagingDatabase()))
.setChildPath(node.get().getParentPath()).setToIndex(true)
.setTree(treeBuilder.build()).setMetadataId(metadataId).call();
workingTree().updateWorkHead(newTreeId);
}
}
}
} else {
if (!conflicts.isEmpty()) {
if (!(force)) {
StringBuilder msg = new StringBuilder();
for (Conflict conflict : conflicts) {
msg.append("error: " + conflict.getPath() + " needs merge.\n");
}
msg.append("You need to resolve your index first.\n");
throw new CheckoutException(msg.toString(), StatusCode.UNMERGED_PATHS);
}
}
Optional<Ref> targetRef = Optional.absent();
Optional<ObjectId> targetCommitId = Optional.absent();
Optional<ObjectId> targetTreeId = Optional.absent();
targetRef = command(RefParse.class).setName(branchOrCommit).call();
if (targetRef.isPresent()) {
ObjectId commitId = targetRef.get().getObjectId();
if (targetRef.get().getName().startsWith(Ref.REMOTES_PREFIX)) {
String remoteName = targetRef.get().getName();
remoteName = remoteName.substring(Ref.REMOTES_PREFIX.length(), targetRef.get()
.getName().lastIndexOf("/"));
if (branchOrCommit.contains(remoteName + '/')) {
RevCommit commit = command(RevObjectParse.class).setObjectId(commitId)
.call(RevCommit.class).get();
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);
} else {
// set HEAD to a dettached state
ObjectId commitId = targetCommitId.get();
command(UpdateRef.class).setName(Ref.HEAD).setNewValue(commitId).call();
result.setOid(commitId);
result.setResult(CheckoutResult.Results.DETACHED_HEAD);
}
Optional<Ref> ref = command(RefParse.class).setName(Ref.MERGE_HEAD).call();