in org.eclipse.jgit/src/org/eclipse/jgit/api/RenameBranchCommand.java [60:181]
public Ref call() throws GitAPIException, RefNotFoundException, InvalidRefNameException,
RefAlreadyExistsException, DetachedHeadException {
checkCallable();
if (newName == null) {
throw new InvalidRefNameException(MessageFormat.format(JGitText
.get().branchNameInvalid, "<null>")); //$NON-NLS-1$
}
try {
String fullOldName;
String fullNewName;
if (oldName != null) {
// Don't just rely on findRef -- if there are local and remote
// branches with the same name, and oldName is a short name, it
// does not uniquely identify the ref and we might end up
// renaming the wrong branch or finding a tag instead even
// if a unique branch for the name exists!
//
// OldName may be a either a short or a full name.
Ref ref = repo.exactRef(oldName);
if (ref == null) {
ref = repo.exactRef(Constants.R_HEADS + oldName);
Ref ref2 = repo.exactRef(Constants.R_REMOTES + oldName);
if (ref != null && ref2 != null) {
throw new RefNotFoundException(MessageFormat.format(
JGitText.get().renameBranchFailedAmbiguous,
oldName, ref.getName(), ref2.getName()));
} else if (ref == null) {
if (ref2 != null) {
ref = ref2;
} else {
throw new RefNotFoundException(MessageFormat.format(
JGitText.get().refNotResolved, oldName));
}
}
}
fullOldName = ref.getName();
} else {
fullOldName = repo.getFullBranch();
if (fullOldName == null) {
throw new NoHeadException(
JGitText.get().invalidRepositoryStateNoHead);
}
if (ObjectId.isId(fullOldName))
throw new DetachedHeadException();
}
if (fullOldName.startsWith(Constants.R_REMOTES)) {
fullNewName = Constants.R_REMOTES + newName;
} else if (fullOldName.startsWith(Constants.R_HEADS)) {
fullNewName = Constants.R_HEADS + newName;
} else {
throw new RefNotFoundException(MessageFormat.format(
JGitText.get().renameBranchFailedNotABranch,
fullOldName));
}
if (!Repository.isValidRefName(fullNewName)) {
throw new InvalidRefNameException(MessageFormat.format(JGitText
.get().branchNameInvalid, fullNewName));
}
if (repo.exactRef(fullNewName) != null) {
throw new RefAlreadyExistsException(MessageFormat
.format(JGitText.get().refAlreadyExists1, fullNewName));
}
RefRename rename = repo.renameRef(fullOldName, fullNewName);
Result renameResult = rename.rename();
setCallable(false);
if (Result.RENAMED != renameResult) {
throw new JGitInternalException(MessageFormat.format(JGitText
.get().renameBranchUnexpectedResult, renameResult
.name()));
}
if (fullNewName.startsWith(Constants.R_HEADS)) {
String shortOldName = fullOldName.substring(Constants.R_HEADS
.length());
final StoredConfig repoConfig = repo.getConfig();
// Copy all configuration values over to the new branch
for (String name : repoConfig.getNames(
ConfigConstants.CONFIG_BRANCH_SECTION, shortOldName)) {
String[] values = repoConfig.getStringList(
ConfigConstants.CONFIG_BRANCH_SECTION,
shortOldName, name);
if (values.length == 0) {
continue;
}
// Keep any existing values already configured for the
// new branch name
String[] existing = repoConfig.getStringList(
ConfigConstants.CONFIG_BRANCH_SECTION, newName,
name);
if (existing.length > 0) {
String[] newValues = new String[values.length
+ existing.length];
System.arraycopy(existing, 0, newValues, 0,
existing.length);
System.arraycopy(values, 0, newValues, existing.length,
values.length);
values = newValues;
}
repoConfig.setStringList(
ConfigConstants.CONFIG_BRANCH_SECTION, newName,
name, Arrays.asList(values));
}
repoConfig.unsetSection(ConfigConstants.CONFIG_BRANCH_SECTION,
shortOldName);
repoConfig.save();
}
Ref resultRef = repo.exactRef(fullNewName);
if (resultRef == null) {
throw new JGitInternalException(
JGitText.get().renameBranchFailedUnknownReason);
}
return resultRef;
} catch (IOException ioe) {
throw new JGitInternalException(ioe.getMessage(), ioe);
}
}