{"record":{"id":"4e6f78129a3bca98","repo":"theonedev/onedev","slug":"branch-request-getbranchname-already-exists","errorCode":null,"errorMessage":"Branch '${request.getBranchName()}' already exists","messagePattern":"Branch '(.+?)' already exists","errorType":"http","errorClass":"NotAcceptableException","httpStatus":406,"severity":"error","filePath":"server-core/src/main/java/io/onedev/server/rest/resource/RepositoryResource.java","lineNumber":158,"sourceCode":"\n\t\tRefResponse response = new RefResponse();\n\t\t\n\t\tresponse.refName = ref.getName();\n\t\tresponse.commitHash = project.getRevCommit(ref.getObjectId(), true).getName();\n\t\t\n\t\treturn response;\n\t}\n\n\t@Api(order=30, description=\"Create a new branch\")\n\t@Path(\"/{projectId}/branches\")\n\t@POST\n\tpublic Response createBranch(@PathParam(\"projectId\") Long projectId, @NotNull CreateBranchRequest request) {\n\t\tProject project = projectService.load(projectId);\n\t\tUser user = SecurityUtils.getUser();\n\t\tif (!SecurityUtils.canWriteCode(project)) \n\t\t\tthrow new UnauthorizedException();\n\t\telse if (project.getBranchRef(request.getBranchName()) != null) \n\t\t\tthrow new NotAcceptableException(\"Branch '\" + request.getBranchName() + \"' already exists\");\n\t\telse if (project.getBranchProtection(request.getBranchName(), user).isPreventCreation()) \n\t\t\tthrow new ExplicitException(\"Branch creation prohibited by branch protection rule\");\n\t\t\n\t\tif (!project.isCommitSignatureRequirementSatisfied(\n\t\t\t\tuser, request.getBranchName(), \n\t\t\t\tproject.getRevCommit(request.getRevision(), true))) {\n\t\t\tthrow new ExplicitException(\"Cannot create this branch as branch protection setting \"\n\t\t\t\t\t+ \"requires valid signature on head commit\");\n\t\t}\n\t\t\n\t\tgitService.createBranch(project, request.getBranchName(), request.getRevision());\n\n\t\treturn Response.ok().build();\n\t}\n\n\t@Api(order=40, description=\"Delete specified branch\")\n\t@Path(\"/{projectId}/branches/{branch:.*}\")\n\t@DELETE","sourceCodeStart":140,"sourceCodeEnd":176,"githubUrl":"https://github.com/theonedev/onedev/blob/d44925c47c37992c828ea673a5f9620539bc3ff2/server-core/src/main/java/io/onedev/server/rest/resource/RepositoryResource.java#L140-L176","documentation":"POST /{projectId}/branches throws NotAcceptableException when the requested branch name already exists as a ref in the repository. OneDev checks project.getBranchRef(branchName) before creating and refuses duplicate branch creation.","triggerScenarios":"Calling POST /~api/{projectId}/branches with a branchName that resolves to an existing git ref (e.g. 'refs/heads/main').","commonSituations":"Rerunning an idempotency-unguarded CI script that creates a branch; branch created concurrently by another user or automation; case-sensitivity confusion with existing branch names.","solutions":["Choose a different, unique branch name","Delete or rename the existing branch first if it is obsolete","Check GET /~api/{projectId}/branches (or branch existence) before POST and skip creation if present","Use a timestamped or run-id suffixed branch name in automation"],"exampleFix":"// before\nPOST /~api/1/branches {\"branchName\":\"release\",\"revision\":\"main\"}\n// after\nPOST /~api/1/branches {\"branchName\":\"release-2026-09-06\",\"revision\":\"main\"}","handlingStrategy":"validation","validationCode":"const exists = (await api.get(`/projects/${id}/branches`)).data.includes(name);\nif (exists) throw new Error(`Branch ${name} already exists`);","typeGuard":null,"tryCatchPattern":"try {\n  createBranch(projectId, req);\n} catch (NotAcceptableException e) {\n  // treat as idempotent success or pick a new branch name\n}","preventionTips":["Check branch existence before creating","Include run-id/timestamp in generated branch names","Make CI scripts idempotent on re-run"],"tags":["rest","git","branch","conflict"],"backgroundTag":"file-already-exists","analyzedSha":"d44925c47c37992c828ea673a5f9620539bc3ff2","analyzedAt":"2026-09-06T07:18:27.995Z","contentChangedAt":"2026-09-06T07:18:27.995Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}