@@ -176,6 +176,54 @@ func redirect(ctx *context.Context) {
176176 ctx .JSONRedirect (ctx .Repo .RepoLink + "/branches?page=" + url .QueryEscape (ctx .FormString ("page" )))
177177}
178178
179+ func handleCreateBranchError (ctx * context.Context , err error , form * forms.NewBranchForm ) {
180+ if models .IsErrProtectedTagName (err ) {
181+ ctx .Flash .Error (ctx .Tr ("repo.release.tag_name_protected" ))
182+ ctx .Redirect (ctx .Repo .RepoLink + "/src/" + ctx .Repo .BranchNameSubURL ())
183+ return
184+ }
185+
186+ if models .IsErrTagAlreadyExists (err ) {
187+ e := err .(models.ErrTagAlreadyExists )
188+ ctx .Flash .Error (ctx .Tr ("repo.branch.tag_collision" , e .TagName ))
189+ ctx .Redirect (ctx .Repo .RepoLink + "/src/" + ctx .Repo .BranchNameSubURL ())
190+ return
191+ }
192+ if git_model .IsErrBranchAlreadyExists (err ) || git .IsErrPushOutOfDate (err ) {
193+ ctx .Flash .Error (ctx .Tr ("repo.branch.branch_already_exists" , form .NewBranchName ))
194+ ctx .Redirect (ctx .Repo .RepoLink + "/src/" + ctx .Repo .BranchNameSubURL ())
195+ return
196+ }
197+ if git_model .IsErrBranchNameConflict (err ) {
198+ e := err .(git_model.ErrBranchNameConflict )
199+ ctx .Flash .Error (ctx .Tr ("repo.branch.branch_name_conflict" , form .NewBranchName , e .BranchName ))
200+ ctx .Redirect (ctx .Repo .RepoLink + "/src/" + ctx .Repo .BranchNameSubURL ())
201+ return
202+ }
203+ if git .IsErrPushRejected (err ) {
204+ e := err .(* git.ErrPushRejected )
205+ if len (e .Message ) == 0 {
206+ ctx .Flash .Error (ctx .Tr ("repo.editor.push_rejected_no_message" ))
207+ } else {
208+ flashError , err := ctx .RenderToHTML (tplAlertDetails , map [string ]any {
209+ "Message" : ctx .Tr ("repo.editor.push_rejected" ),
210+ "Summary" : ctx .Tr ("repo.editor.push_rejected_summary" ),
211+ "Details" : utils .SanitizeFlashErrorString (e .Message ),
212+ })
213+ if err != nil {
214+ ctx .ServerError ("UpdatePullRequest.HTMLString" , err )
215+ return
216+ }
217+ ctx .Flash .Error (flashError )
218+ }
219+ ctx .Redirect (ctx .Repo .RepoLink + "/src/" + ctx .Repo .BranchNameSubURL ())
220+ return
221+ }
222+
223+ ctx .ServerError ("CreateNewBranch" , err )
224+ return
225+ }
226+
179227// CreateBranch creates new branch in repository
180228func CreateBranch (ctx * context.Context ) {
181229 form := web .GetForm (ctx ).(* forms.NewBranchForm )
@@ -204,50 +252,7 @@ func CreateBranch(ctx *context.Context) {
204252 err = repo_service .CreateNewBranchFromCommit (ctx , ctx .Doer , ctx .Repo .Repository , ctx .Repo .GitRepo , ctx .Repo .CommitID , form .NewBranchName )
205253 }
206254 if err != nil {
207- if models .IsErrProtectedTagName (err ) {
208- ctx .Flash .Error (ctx .Tr ("repo.release.tag_name_protected" ))
209- ctx .Redirect (ctx .Repo .RepoLink + "/src/" + ctx .Repo .BranchNameSubURL ())
210- return
211- }
212-
213- if models .IsErrTagAlreadyExists (err ) {
214- e := err .(models.ErrTagAlreadyExists )
215- ctx .Flash .Error (ctx .Tr ("repo.branch.tag_collision" , e .TagName ))
216- ctx .Redirect (ctx .Repo .RepoLink + "/src/" + ctx .Repo .BranchNameSubURL ())
217- return
218- }
219- if git_model .IsErrBranchAlreadyExists (err ) || git .IsErrPushOutOfDate (err ) {
220- ctx .Flash .Error (ctx .Tr ("repo.branch.branch_already_exists" , form .NewBranchName ))
221- ctx .Redirect (ctx .Repo .RepoLink + "/src/" + ctx .Repo .BranchNameSubURL ())
222- return
223- }
224- if git_model .IsErrBranchNameConflict (err ) {
225- e := err .(git_model.ErrBranchNameConflict )
226- ctx .Flash .Error (ctx .Tr ("repo.branch.branch_name_conflict" , form .NewBranchName , e .BranchName ))
227- ctx .Redirect (ctx .Repo .RepoLink + "/src/" + ctx .Repo .BranchNameSubURL ())
228- return
229- }
230- if git .IsErrPushRejected (err ) {
231- e := err .(* git.ErrPushRejected )
232- if len (e .Message ) == 0 {
233- ctx .Flash .Error (ctx .Tr ("repo.editor.push_rejected_no_message" ))
234- } else {
235- flashError , err := ctx .RenderToHTML (tplAlertDetails , map [string ]any {
236- "Message" : ctx .Tr ("repo.editor.push_rejected" ),
237- "Summary" : ctx .Tr ("repo.editor.push_rejected_summary" ),
238- "Details" : utils .SanitizeFlashErrorString (e .Message ),
239- })
240- if err != nil {
241- ctx .ServerError ("UpdatePullRequest.HTMLString" , err )
242- return
243- }
244- ctx .Flash .Error (flashError )
245- }
246- ctx .Redirect (ctx .Repo .RepoLink + "/src/" + ctx .Repo .BranchNameSubURL ())
247- return
248- }
249-
250- ctx .ServerError ("CreateNewBranch" , err )
255+ handleCreateBranchError (ctx , err , form )
251256 return
252257 }
253258
0 commit comments