@@ -386,11 +386,11 @@ func ListBranches(ctx *context.APIContext) {
386386 ctx .JSON (http .StatusOK , apiBranches )
387387}
388388
389- // RenameBranch renames a repository's branch.
390- func RenameBranch (ctx * context.APIContext ) {
391- // swagger:operation POST /repos/{owner}/{repo}/branches/rename repository repoRenameBranch
389+ // UpdateBranch renames a repository's branch.
390+ func UpdateBranch (ctx * context.APIContext ) {
391+ // swagger:operation PATCH /repos/{owner}/{repo}/branches/{branch} repository repoUpdateBranch
392392 // ---
393- // summary: Rename a branch
393+ // summary: Update a branch
394394 // consumes:
395395 // - application/json
396396 // produces:
@@ -406,12 +406,16 @@ func RenameBranch(ctx *context.APIContext) {
406406 // description: name of the repo
407407 // type: string
408408 // required: true
409+ // - name: branch
410+ // in: path
411+ // description: name of the branch
412+ // type: string
409413 // - name: body
410414 // in: body
411415 // schema:
412- // "$ref": "#/definitions/RenameBranchRepoOption "
416+ // "$ref": "#/definitions/UpdateBranchRepoOption "
413417 // responses:
414- // "201 ":
418+ // "200 ":
415419 // "$ref": "#/responses/Branch"
416420 // "403":
417421 // "$ref": "#/responses/forbidden"
@@ -420,7 +424,9 @@ func RenameBranch(ctx *context.APIContext) {
420424 // "422":
421425 // "$ref": "#/responses/validationError"
422426
423- opt := web .GetForm (ctx ).(* api.RenameBranchRepoOption )
427+ opt := web .GetForm (ctx ).(* api.UpdateBranchRepoOption )
428+
429+ oldName := ctx .PathParam ("*" )
424430 repo := ctx .Repo .Repository
425431
426432 if repo .IsEmpty {
@@ -433,17 +439,26 @@ func RenameBranch(ctx *context.APIContext) {
433439 return
434440 }
435441
436- msg , err := repo_service .RenameBranch (ctx , repo , ctx .Doer , ctx .Repo .GitRepo , opt .OldName , opt .NewName )
437- if err != nil {
438- ctx .Error (http .StatusInternalServerError , "RenameBranch" , err )
439- return
440- }
441- if msg != "" {
442- ctx .Error (http .StatusUnprocessableEntity , "" , msg )
443- return
442+ branchName := opt .Name
443+ if branchName != "" {
444+ msg , err := repo_service .RenameBranch (ctx , repo , ctx .Doer , ctx .Repo .GitRepo , oldName , branchName )
445+ if err != nil {
446+ ctx .Error (http .StatusInternalServerError , "RenameBranch" , err )
447+ return
448+ }
449+ if msg == "target_exist" {
450+ ctx .Error (http .StatusUnprocessableEntity , "" , "Cannot rename a branch using the same name or rename to a branch that already exists." )
451+ return
452+ }
453+ if msg == "from_not_exist" {
454+ ctx .Error (http .StatusUnprocessableEntity , "" , "Branch doesn't exist." )
455+ return
456+ }
457+ } else {
458+ branchName = oldName
444459 }
445460
446- branch , err := ctx .Repo .GitRepo .GetBranch (opt . NewName )
461+ branch , err := ctx .Repo .GitRepo .GetBranch (branchName )
447462 if err != nil {
448463 ctx .Error (http .StatusInternalServerError , "GetBranch" , err )
449464 return
@@ -461,13 +476,13 @@ func RenameBranch(ctx *context.APIContext) {
461476 return
462477 }
463478
464- br , err := convert .ToBranch (ctx , repo , opt . NewName , commit , pb , ctx .Doer , ctx .Repo .IsAdmin ())
479+ br , err := convert .ToBranch (ctx , repo , branch . Name , commit , pb , ctx .Doer , ctx .Repo .IsAdmin ())
465480 if err != nil {
466481 ctx .Error (http .StatusInternalServerError , "convert.ToBranch" , err )
467482 return
468483 }
469484
470- ctx .JSON (http .StatusCreated , br )
485+ ctx .JSON (http .StatusOK , br )
471486}
472487
473488// GetBranchProtection gets a branch protection
0 commit comments