@@ -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
@@ -203,6 +206,62 @@ func CreateBranch(ctx *context.APIContext) {
203206 // "423":
204207 // "$ref": "#/responses/repoArchivedError"
205208
209+ optCreate := web .GetForm (ctx ).(* api.CreateBranchRepoOption )
210+ opt := api.UpdateBranchRepoOption {}
211+ err := copier .Copy (& opt , optCreate )
212+ if err != nil {
213+ ctx .APIError (http .StatusInternalServerError , fmt .Sprintf ("Error processing request %s." , err ))
214+ return
215+ }
216+
217+ CreateUpdateRepoBranch (ctx , & opt )
218+ }
219+
220+ // UpdateBranch update (reset) a branch in a user's repository
221+ func UpdateBranch (ctx * context.APIContext ) {
222+ // swagger:operation PUT /repos/{owner}/{repo}/branches repository repoUpdateBranch
223+ // ---
224+ // summary: Update a branch
225+ // consumes:
226+ // - application/json
227+ // produces:
228+ // - application/json
229+ // parameters:
230+ // - name: owner
231+ // in: path
232+ // description: owner of the repo
233+ // type: string
234+ // required: true
235+ // - name: repo
236+ // in: path
237+ // description: name of the repo
238+ // type: string
239+ // required: true
240+ // - name: body
241+ // in: body
242+ // schema:
243+ // "$ref": "#/definitions/UpdateBranchRepoOption"
244+ // responses:
245+ // "201":
246+ // "$ref": "#/responses/Branch"
247+ // "403":
248+ // description: The branch is archived or a mirror.
249+ // "404":
250+ // description: The branch does not exist.
251+ // "409":
252+ // description: The branch SHA does not match.
253+ // "423":
254+ // "$ref": "#/responses/repoArchivedError"
255+
256+ opt := web .GetForm (ctx ).(* api.UpdateBranchRepoOption )
257+
258+ CreateUpdateRepoBranch (ctx , opt )
259+ }
260+
261+ func CreateUpdateRepoBranch (ctx * context.APIContext , opt * api.UpdateBranchRepoOption ) {
262+ var oldCommit * git.Commit
263+ var err error
264+
206265 if ctx .Repo .Repository .IsEmpty {
207266 ctx .APIError (http .StatusNotFound , "Git Repository is empty." )
208267 return
@@ -213,11 +272,6 @@ func CreateBranch(ctx *context.APIContext) {
213272 return
214273 }
215274
216- opt := web .GetForm (ctx ).(* api.CreateBranchRepoOption )
217-
218- var oldCommit * git.Commit
219- var err error
220-
221275 if len (opt .OldRefName ) > 0 {
222276 oldCommit , err = ctx .Repo .GitRepo .GetCommit (opt .OldRefName )
223277 if err != nil {
@@ -243,14 +297,16 @@ func CreateBranch(ctx *context.APIContext) {
243297 }
244298 }
245299
246- err = repo_service .CreateNewBranchFromCommit (ctx , ctx .Doer , ctx .Repo .Repository , oldCommit .ID .String (), opt .BranchName )
300+ err = repo_service .CreateUpdateBranchFromCommit (ctx , ctx .Doer , ctx .Repo .Repository , oldCommit .ID .String (), opt .BranchName , opt . SHA )
247301 if err != nil {
248302 if git_model .IsErrBranchNotExist (err ) {
249303 ctx .APIError (http .StatusNotFound , "The old branch does not exist" )
250304 } else if release_service .IsErrTagAlreadyExists (err ) {
251305 ctx .APIError (http .StatusConflict , "The branch with the same tag already exists." )
252- } else if git_model .IsErrBranchAlreadyExists (err ) || git . IsErrPushOutOfDate ( err ) {
306+ } else if git_model .IsErrBranchAlreadyExists (err ) {
253307 ctx .APIError (http .StatusConflict , "The branch already exists." )
308+ } else if git .IsErrPushOutOfDate (err ) || git .IsErrPushRejected (err ) {
309+ ctx .APIError (http .StatusConflict , "The branch SHA does not match." )
254310 } else if git_model .IsErrBranchNameConflict (err ) {
255311 ctx .APIError (http .StatusConflict , "The branch with the same name already exists." )
256312 } else {
0 commit comments