@@ -6,6 +6,7 @@ package repo
66
77import (
88 "errors"
9+ "fmt"
910 "net/http"
1011
1112 "code.gitea.io/gitea/models/db"
@@ -26,6 +27,8 @@ import (
2627 pull_service "code.gitea.io/gitea/services/pull"
2728 release_service "code.gitea.io/gitea/services/release"
2829 repo_service "code.gitea.io/gitea/services/repository"
30+
31+ "github.com/jinzhu/copier"
2932)
3033
3134// GetBranch get a branch of a repository
@@ -213,8 +216,69 @@ func CreateBranch(ctx *context.APIContext) {
213216 return
214217 }
215218
216- opt := web .GetForm (ctx ).(* api.CreateBranchRepoOption )
219+ optCreate := web .GetForm (ctx ).(* api.CreateBranchRepoOption )
220+ opt := api.UpdateBranchRepoOption {}
221+ err := copier .Copy (& opt , optCreate )
222+ if err != nil {
223+ ctx .APIError (http .StatusInternalServerError , fmt .Sprintf ("Error processing request %s." , err ))
224+ return
225+ }
226+
227+ CreateUpdateRepoBranch (ctx , & opt )
228+ }
229+
230+ // UpdateBranch update (reset) a branch in a user's repository
231+ func UpdateBranch (ctx * context.APIContext ) {
232+ // swagger:operation PUT /repos/{owner}/{repo}/branches repository repoUpdateBranch
233+ // ---
234+ // summary: Update a branch
235+ // consumes:
236+ // - application/json
237+ // produces:
238+ // - application/json
239+ // parameters:
240+ // - name: owner
241+ // in: path
242+ // description: owner of the repo
243+ // type: string
244+ // required: true
245+ // - name: repo
246+ // in: path
247+ // description: name of the repo
248+ // type: string
249+ // required: true
250+ // - name: body
251+ // in: body
252+ // schema:
253+ // "$ref": "#/definitions/UpdateBranchRepoOption"
254+ // responses:
255+ // "201":
256+ // "$ref": "#/responses/Branch"
257+ // "403":
258+ // description: The branch is archived or a mirror.
259+ // "404":
260+ // description: The branch does not exist.
261+ // "409":
262+ // description: The branch SHA does not match.
263+ // "423":
264+ // "$ref": "#/responses/repoArchivedError"
265+
266+ if ctx .Repo .Repository .IsEmpty {
267+ ctx .APIError (http .StatusNotFound , "Git Repository is empty." )
268+ return
269+ }
270+
271+ if ctx .Repo .Repository .IsMirror {
272+ ctx .APIError (http .StatusForbidden , "Git Repository is a mirror." )
273+ return
274+ }
275+
276+ opt := web .GetForm (ctx ).(* api.UpdateBranchRepoOption )
277+
278+ CreateUpdateRepoBranch (ctx , opt )
279+ }
217280
281+ func CreateUpdateRepoBranch (ctx * context.APIContext , opt * api.UpdateBranchRepoOption ) {
218282 var oldCommit * git.Commit
219283 var err error
220284
@@ -243,14 +307,16 @@ func CreateBranch(ctx *context.APIContext) {
243307 }
244308 }
245309
246- err = repo_service .CreateNewBranchFromCommit (ctx , ctx .Doer , ctx .Repo .Repository , oldCommit .ID .String (), opt .BranchName )
310+ err = repo_service .CreateUpdateBranchFromCommit (ctx , ctx .Doer , ctx .Repo .Repository , oldCommit .ID .String (), opt .BranchName , opt . SHA )
247311 if err != nil {
248312 if git_model .IsErrBranchNotExist (err ) {
249313 ctx .APIError (http .StatusNotFound , "The old branch does not exist" )
250314 } else if release_service .IsErrTagAlreadyExists (err ) {
251315 ctx .APIError (http .StatusConflict , "The branch with the same tag already exists." )
252- } else if git_model .IsErrBranchAlreadyExists (err ) || git . IsErrPushOutOfDate ( err ) {
316+ } else if git_model .IsErrBranchAlreadyExists (err ) {
253317 ctx .APIError (http .StatusConflict , "The branch already exists." )
318+ } else if git .IsErrPushOutOfDate (err ) || git .IsErrPushRejected (err ) {
319+ ctx .APIError (http .StatusConflict , "The branch SHA does not match." )
254320 } else if git_model .IsErrBranchNameConflict (err ) {
255321 ctx .APIError (http .StatusConflict , "The branch with the same name already exists." )
256322 } else {
0 commit comments