Add handling when duplicated branch part cannot be created (#1692)

Add handling when duplicated branch cannot be created because a part of the name already exists as a branch
This commit is contained in:
Florian Scholdei
2021-06-09 14:58:59 +02:00
committed by GitHub
parent 35fe536170
commit f274b7f4b2
8 changed files with 112 additions and 9 deletions

View File

@@ -62,7 +62,7 @@ import java.io.IOException;
import java.net.URI;
import java.util.Optional;
import static sonia.scm.AlreadyExistsException.alreadyExists;
import static sonia.scm.BranchAlreadyExistsException.branchAlreadyExists;
import static sonia.scm.ContextEntry.ContextBuilder.entity;
import static sonia.scm.NotFoundException.notFound;
@@ -254,7 +254,7 @@ public class BranchRootResource {
String parentName = branchRequest.getParent();
try (RepositoryService repositoryService = serviceFactory.create(namespaceAndName)) {
if (branchExists(branchName, repositoryService)) {
throw alreadyExists(entity(Branch.class, branchName).in(Repository.class, namespace + "/" + name));
throw branchAlreadyExists(entity(Branch.class, branchName).in(Repository.class, namespace + "/" + name));
}
Repository repository = repositoryService.getRepository();
RepositoryPermissions.push(repository).check();
@@ -275,7 +275,16 @@ public class BranchRootResource {
.getBranches()
.getBranches()
.stream()
.anyMatch(branch -> branchName.equals(branch.getName()));
.anyMatch(branch -> branchName.equals(branch.getName())
|| branchNamespaceAlreadyExistsInGitRepo(branchName, branch.getName(), repositoryService));
}
private boolean branchNamespaceAlreadyExistsInGitRepo(String branchName, String branchName2, RepositoryService repositoryService) {
if (repositoryService.getRepository().getType().equals("hg")) {
return false;
}
return (branchName.contains("/") && branchName2.equals(branchName.substring(0, branchName.indexOf("/"))))
|| (branchName2.contains("/") && branchName.equals(branchName2.substring(0, branchName2.indexOf("/"))));
}
/**