@@ -396,11 +396,11 @@ func ListBranches(ctx *context.APIContext) {
396
396
ctx .JSON (http .StatusOK , apiBranches )
397
397
}
398
398
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
402
402
// ---
403
- // summary: Rename a branch
403
+ // summary: Update a branch
404
404
// consumes:
405
405
// - application/json
406
406
// produces:
@@ -416,12 +416,16 @@ func RenameBranch(ctx *context.APIContext) {
416
416
// description: name of the repo
417
417
// type: string
418
418
// required: true
419
+ // - name: branch
420
+ // in: path
421
+ // description: name of the branch
422
+ // type: string
419
423
// - name: body
420
424
// in: body
421
425
// schema:
422
- // "$ref": "#/definitions/RenameBranchRepoOption "
426
+ // "$ref": "#/definitions/UpdateBranchRepoOption "
423
427
// responses:
424
- // "201 ":
428
+ // "200 ":
425
429
// "$ref": "#/responses/Branch"
426
430
// "403":
427
431
// "$ref": "#/responses/forbidden"
@@ -430,7 +434,9 @@ func RenameBranch(ctx *context.APIContext) {
430
434
// "422":
431
435
// "$ref": "#/responses/validationError"
432
436
433
- opt := web .GetForm (ctx ).(* api.RenameBranchRepoOption )
437
+ opt := web .GetForm (ctx ).(* api.UpdateBranchRepoOption )
438
+
439
+ oldName := ctx .PathParam ("*" )
434
440
repo := ctx .Repo .Repository
435
441
436
442
if repo .IsEmpty {
@@ -443,17 +449,26 @@ func RenameBranch(ctx *context.APIContext) {
443
449
return
444
450
}
445
451
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
454
469
}
455
470
456
- branch , err := ctx .Repo .GitRepo .GetBranch (opt . NewName )
471
+ branch , err := ctx .Repo .GitRepo .GetBranch (branchName )
457
472
if err != nil {
458
473
ctx .Error (http .StatusInternalServerError , "GetBranch" , err )
459
474
return
@@ -471,13 +486,13 @@ func RenameBranch(ctx *context.APIContext) {
471
486
return
472
487
}
473
488
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 ())
475
490
if err != nil {
476
491
ctx .Error (http .StatusInternalServerError , "convert.ToBranch" , err )
477
492
return
478
493
}
479
494
480
- ctx .JSON (http .StatusCreated , br )
495
+ ctx .JSON (http .StatusOK , br )
481
496
}
482
497
483
498
// GetBranchProtection gets a branch protection
0 commit comments