public Ref call() throws GitAPIException, RefNotFoundException, InvalidRefNameException,
RefAlreadyExistsException, DetachedHeadException {
checkCallable();
if (newName == null)
throw new InvalidRefNameException(MessageFormat.format(JGitText
.get().branchNameInvalid, "<null>"));
try {
String fullOldName;
String fullNewName;
if (repo.getRef(newName) != null)
throw new RefAlreadyExistsException(MessageFormat.format(
JGitText.get().refAlreadyExists1, newName));
if (oldName != null) {
Ref ref = repo.getRef(oldName);
if (ref == null)
throw new RefNotFoundException(MessageFormat.format(
JGitText.get().refNotResolved, oldName));
if (ref.getName().startsWith(Constants.R_TAGS))
throw new RefNotFoundException(MessageFormat.format(
JGitText.get().renameBranchFailedBecauseTag,
oldName));
fullOldName = ref.getName();
} else {
fullOldName = repo.getFullBranch();
if (ObjectId.isId(fullOldName))
throw new DetachedHeadException();
}
if (fullOldName.startsWith(Constants.R_REMOTES))
fullNewName = Constants.R_REMOTES + newName;
else {
fullNewName = Constants.R_HEADS + newName;
}
if (!Repository.isValidRefName(fullNewName))
throw new InvalidRefNameException(MessageFormat.format(JGitText
.get().branchNameInvalid, fullNewName));
RefRename rename = repo.renameRef(fullOldName, fullNewName);
Result renameResult = rename.rename();