Skip to content

Commit ddb160e

Browse files
Handle setting default branch via API
1 parent 4b3fc74 commit ddb160e

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

routers/api/v1/api.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ func RegisterRoutes(m *macaron.Macaron) {
606606
m.Group("/:username/:reponame", func() {
607607
m.Combo("").Get(reqAnyRepoReader(), repo.Get).
608608
Delete(reqToken(), reqOwner(), repo.Delete).
609-
Patch(reqToken(), reqAdmin(), bind(api.EditRepoOption{}), repo.Edit)
609+
Patch(reqToken(), reqAdmin(), bind(api.EditRepoOption{}), context.RepoRef(), repo.Edit)
610610
m.Group("/hooks", func() {
611611
m.Combo("").Get(repo.ListHooks).
612612
Post(bind(api.CreateHookOption{}), repo.CreateHook)

routers/api/v1/repo/repo.go

+12
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"code.gitea.io/gitea/modules/auth"
1818
"code.gitea.io/gitea/modules/context"
1919
"code.gitea.io/gitea/modules/convert"
20+
"code.gitea.io/gitea/modules/git"
2021
"code.gitea.io/gitea/modules/log"
2122
"code.gitea.io/gitea/modules/migrations"
2223
"code.gitea.io/gitea/modules/notification"
@@ -697,6 +698,17 @@ func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) err
697698
repo.IsTemplate = *opts.Template
698699
}
699700

701+
// Default branch only updated if changed and exist
702+
if opts.DefaultBranch != nil && repo.DefaultBranch != *opts.DefaultBranch && ctx.Repo.GitRepo.IsBranchExist(*opts.DefaultBranch) {
703+
if err := ctx.Repo.GitRepo.SetDefaultBranch(*opts.DefaultBranch); err != nil {
704+
if !git.IsErrUnsupportedVersion(err) {
705+
ctx.Error(http.StatusInternalServerError, "SetDefaultBranch", err)
706+
return err
707+
}
708+
}
709+
repo.DefaultBranch = *opts.DefaultBranch
710+
}
711+
700712
if err := models.UpdateRepository(repo, visibilityChanged); err != nil {
701713
ctx.Error(http.StatusInternalServerError, "UpdateRepository", err)
702714
return err

0 commit comments

Comments
 (0)