@@ -396,11 +396,11 @@ func ListBranches(ctx *context.APIContext) {
396396	ctx .JSON (http .StatusOK , apiBranches )
397397}
398398
399- // RenameBranch  renames a repository's branch. 
400- func  RenameBranch (ctx  * context.APIContext ) {
401- 	// swagger:operation POST  /repos/{owner}/{repo}/branches/rename  repository repoRenameBranch  
399+ // UpdateBranch  renames a repository's branch. 
400+ func  UpdateBranch (ctx  * context.APIContext ) {
401+ 	// swagger:operation PATCH  /repos/{owner}/{repo}/branches/{branch}  repository repoUpdateBranch  
402402	// --- 
403- 	// summary: Rename  a branch 
403+ 	// summary: Update  a branch 
404404	// consumes: 
405405	// - application/json 
406406	// produces: 
@@ -416,12 +416,16 @@ func RenameBranch(ctx *context.APIContext) {
416416	//   description: name of the repo 
417417	//   type: string 
418418	//   required: true 
419+ 	// - name: branch 
420+ 	//   in: path 
421+ 	//   description: name of the branch 
422+ 	//   type: string 
419423	// - name: body 
420424	//   in: body 
421425	//   schema: 
422- 	//     "$ref": "#/definitions/RenameBranchRepoOption " 
426+ 	//     "$ref": "#/definitions/UpdateBranchRepoOption " 
423427	// responses: 
424- 	//   "201 ": 
428+ 	//   "200 ": 
425429	//     "$ref": "#/responses/Branch" 
426430	//   "403": 
427431	//     "$ref": "#/responses/forbidden" 
@@ -430,7 +434,9 @@ func RenameBranch(ctx *context.APIContext) {
430434	//   "422": 
431435	//     "$ref": "#/responses/validationError" 
432436
433- 	opt  :=  web .GetForm (ctx ).(* api.RenameBranchRepoOption )
437+ 	opt  :=  web .GetForm (ctx ).(* api.UpdateBranchRepoOption )
438+ 
439+ 	oldName  :=  ctx .PathParam ("*" )
434440	repo  :=  ctx .Repo .Repository 
435441
436442	if  repo .IsEmpty  {
@@ -443,17 +449,26 @@ func RenameBranch(ctx *context.APIContext) {
443449		return 
444450	}
445451
446- 	msg , err  :=  repo_service .RenameBranch (ctx , repo , ctx .Doer , ctx .Repo .GitRepo , opt .OldName , opt .NewName )
447- 	if  err  !=  nil  {
448- 		ctx .Error (http .StatusInternalServerError , "RenameBranch" , err )
449- 		return 
450- 	}
451- 	if  msg  !=  ""  {
452- 		ctx .Error (http .StatusUnprocessableEntity , "" , msg )
453- 		return 
452+ 	branchName  :=  opt .Name 
453+ 	if  branchName  !=  ""  {
454+ 		msg , err  :=  repo_service .RenameBranch (ctx , repo , ctx .Doer , ctx .Repo .GitRepo , oldName , branchName )
455+ 		if  err  !=  nil  {
456+ 			ctx .Error (http .StatusInternalServerError , "RenameBranch" , err )
457+ 			return 
458+ 		}
459+ 		if  msg  ==  "target_exist"  {
460+ 			ctx .Error (http .StatusUnprocessableEntity , "" , "Cannot rename a branch using the same name or rename to a branch that already exists." )
461+ 			return 
462+ 		}
463+ 		if  msg  ==  "from_not_exist"  {
464+ 			ctx .Error (http .StatusUnprocessableEntity , "" , "Branch doesn't exist." )
465+ 			return 
466+ 		}
467+ 	} else  {
468+ 		branchName  =  oldName 
454469	}
455470
456- 	branch , err  :=  ctx .Repo .GitRepo .GetBranch (opt . NewName )
471+ 	branch , err  :=  ctx .Repo .GitRepo .GetBranch (branchName )
457472	if  err  !=  nil  {
458473		ctx .Error (http .StatusInternalServerError , "GetBranch" , err )
459474		return 
@@ -471,13 +486,13 @@ func RenameBranch(ctx *context.APIContext) {
471486		return 
472487	}
473488
474- 	br , err  :=  convert .ToBranch (ctx , repo , opt . NewName , commit , pb , ctx .Doer , ctx .Repo .IsAdmin ())
489+ 	br , err  :=  convert .ToBranch (ctx , repo , branch . Name , commit , pb , ctx .Doer , ctx .Repo .IsAdmin ())
475490	if  err  !=  nil  {
476491		ctx .Error (http .StatusInternalServerError , "convert.ToBranch" , err )
477492		return 
478493	}
479494
480- 	ctx .JSON (http .StatusCreated , br )
495+ 	ctx .JSON (http .StatusOK , br )
481496}
482497
483498// GetBranchProtection gets a branch protection 
0 commit comments