]> source.dussan.org Git - gitea.git/commitdiff
Handle setting default branch via API (#9030)
authorDavid Svantesson <davidsvantesson@gmail.com>
Sun, 17 Nov 2019 06:30:01 +0000 (07:30 +0100)
committertechknowlogick <techknowlogick@gitea.io>
Sun, 17 Nov 2019 06:30:00 +0000 (01:30 -0500)
routers/api/v1/api.go
routers/api/v1/repo/repo.go

index 6fdd0d074c90295561ad3cad441ed4cad9d8c1de..ec6ec191d74eb806df75ecf35780380bb1d7b939 100644 (file)
@@ -606,7 +606,7 @@ func RegisterRoutes(m *macaron.Macaron) {
                        m.Group("/:username/:reponame", func() {
                                m.Combo("").Get(reqAnyRepoReader(), repo.Get).
                                        Delete(reqToken(), reqOwner(), repo.Delete).
-                                       Patch(reqToken(), reqAdmin(), bind(api.EditRepoOption{}), repo.Edit)
+                                       Patch(reqToken(), reqAdmin(), bind(api.EditRepoOption{}), context.RepoRef(), repo.Edit)
                                m.Group("/hooks", func() {
                                        m.Combo("").Get(repo.ListHooks).
                                                Post(bind(api.CreateHookOption{}), repo.CreateHook)
index 151475a8e949584126e7b3d88aa3ce574fdfca72..05ab9cb38b174f480bf6c722a7d25d735d957058 100644 (file)
@@ -17,6 +17,7 @@ import (
        "code.gitea.io/gitea/modules/auth"
        "code.gitea.io/gitea/modules/context"
        "code.gitea.io/gitea/modules/convert"
+       "code.gitea.io/gitea/modules/git"
        "code.gitea.io/gitea/modules/log"
        "code.gitea.io/gitea/modules/migrations"
        "code.gitea.io/gitea/modules/notification"
@@ -687,6 +688,17 @@ func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) err
                repo.IsTemplate = *opts.Template
        }
 
+       // Default branch only updated if changed and exist
+       if opts.DefaultBranch != nil && repo.DefaultBranch != *opts.DefaultBranch && ctx.Repo.GitRepo.IsBranchExist(*opts.DefaultBranch) {
+               if err := ctx.Repo.GitRepo.SetDefaultBranch(*opts.DefaultBranch); err != nil {
+                       if !git.IsErrUnsupportedVersion(err) {
+                               ctx.Error(http.StatusInternalServerError, "SetDefaultBranch", err)
+                               return err
+                       }
+               }
+               repo.DefaultBranch = *opts.DefaultBranch
+       }
+
        if err := models.UpdateRepository(repo, visibilityChanged); err != nil {
                ctx.Error(http.StatusInternalServerError, "UpdateRepository", err)
                return err