@@ -386,11 +386,11 @@ func ListBranches(ctx *context.APIContext) {
386
386
ctx .JSON (http .StatusOK , apiBranches )
387
387
}
388
388
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
392
392
// ---
393
- // summary: Rename a branch
393
+ // summary: Update a branch
394
394
// consumes:
395
395
// - application/json
396
396
// produces:
@@ -406,12 +406,16 @@ func RenameBranch(ctx *context.APIContext) {
406
406
// description: name of the repo
407
407
// type: string
408
408
// required: true
409
+ // - name: branch
410
+ // in: path
411
+ // description: name of the branch
412
+ // type: string
409
413
// - name: body
410
414
// in: body
411
415
// schema:
412
- // "$ref": "#/definitions/RenameBranchRepoOption "
416
+ // "$ref": "#/definitions/UpdateBranchRepoOption "
413
417
// responses:
414
- // "201 ":
418
+ // "200 ":
415
419
// "$ref": "#/responses/Branch"
416
420
// "403":
417
421
// "$ref": "#/responses/forbidden"
@@ -420,7 +424,9 @@ func RenameBranch(ctx *context.APIContext) {
420
424
// "422":
421
425
// "$ref": "#/responses/validationError"
422
426
423
- opt := web .GetForm (ctx ).(* api.RenameBranchRepoOption )
427
+ opt := web .GetForm (ctx ).(* api.UpdateBranchRepoOption )
428
+
429
+ oldName := ctx .PathParam ("*" )
424
430
repo := ctx .Repo .Repository
425
431
426
432
if repo .IsEmpty {
@@ -433,17 +439,26 @@ func RenameBranch(ctx *context.APIContext) {
433
439
return
434
440
}
435
441
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
444
459
}
445
460
446
- branch , err := ctx .Repo .GitRepo .GetBranch (opt . NewName )
461
+ branch , err := ctx .Repo .GitRepo .GetBranch (branchName )
447
462
if err != nil {
448
463
ctx .Error (http .StatusInternalServerError , "GetBranch" , err )
449
464
return
@@ -461,13 +476,13 @@ func RenameBranch(ctx *context.APIContext) {
461
476
return
462
477
}
463
478
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 ())
465
480
if err != nil {
466
481
ctx .Error (http .StatusInternalServerError , "convert.ToBranch" , err )
467
482
return
468
483
}
469
484
470
- ctx .JSON (http .StatusCreated , br )
485
+ ctx .JSON (http .StatusOK , br )
471
486
}
472
487
473
488
// GetBranchProtection gets a branch protection
0 commit comments