in org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Branch.java [153:245]
protected void run() {
try {
if (delete != null || deleteForce != null) {
if (delete != null) {
delete(delete, false);
}
if (deleteForce != null) {
delete(deleteForce, true);
}
return;
}
if (rename) {
String src, dst;
if (otherBranch == null) {
final Ref head = db.exactRef(Constants.HEAD);
if (head != null && head.isSymbolic()) {
src = head.getLeaf().getName();
} else {
throw die(CLIText.get().cannotRenameDetachedHEAD);
}
dst = branch;
} else {
src = branch;
final Ref old = db.findRef(src);
if (old == null) {
throw die(MessageFormat.format(CLIText.get().doesNotExist, src));
}
if (!old.getName().startsWith(Constants.R_HEADS)) {
throw die(MessageFormat.format(CLIText.get().notABranch, src));
}
src = old.getName();
dst = otherBranch;
}
if (!dst.startsWith(Constants.R_HEADS)) {
dst = Constants.R_HEADS + dst;
}
if (!Repository.isValidRefName(dst)) {
throw die(MessageFormat.format(CLIText.get().notAValidRefName, dst));
}
RefRename r = db.renameRef(src, dst);
if (r.rename() != Result.RENAMED) {
throw die(MessageFormat.format(CLIText.get().cannotBeRenamed, src));
}
} else if (createForce || branch != null) {
String newHead = branch;
String startBranch;
if (createForce) {
startBranch = otherBranch;
} else {
startBranch = Constants.HEAD;
}
Ref startRef = db.findRef(startBranch);
ObjectId startAt = db.resolve(startBranch + "^0"); //$NON-NLS-1$
if (startRef != null) {
startBranch = startRef.getName();
} else if (startAt != null) {
startBranch = startAt.name();
} else {
throw die(MessageFormat.format(
CLIText.get().notAValidCommitName, startBranch));
}
startBranch = Repository.shortenRefName(startBranch);
String newRefName = newHead;
if (!newRefName.startsWith(Constants.R_HEADS)) {
newRefName = Constants.R_HEADS + newRefName;
}
if (!Repository.isValidRefName(newRefName)) {
throw die(MessageFormat.format(CLIText.get().notAValidRefName, newRefName));
}
if (!createForce && db.resolve(newRefName) != null) {
throw die(MessageFormat.format(CLIText.get().branchAlreadyExists, newHead));
}
RefUpdate updateRef = db.updateRef(newRefName);
updateRef.setNewObjectId(startAt);
updateRef.setForceUpdate(createForce);
updateRef.setRefLogMessage(MessageFormat.format(CLIText.get().branchCreatedFrom, startBranch), false);
Result update = updateRef.update();
if (update == Result.REJECTED) {
throw die(MessageFormat.format(CLIText.get().couldNotCreateBranch, newHead, update.toString()));
}
} else {
if (verbose) {
rw = new RevWalk(db);
}
list();
}
} catch (IOException | GitAPIException e) {
throw die(e.getMessage(), e);
}
}