code.gitea.io/gitea@v1.22.3/routers/private/default_branch.go (about)

     1  // Copyright 2021 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package private
     5  
     6  import (
     7  	"fmt"
     8  	"net/http"
     9  
    10  	repo_model "code.gitea.io/gitea/models/repo"
    11  	"code.gitea.io/gitea/modules/git"
    12  	"code.gitea.io/gitea/modules/gitrepo"
    13  	"code.gitea.io/gitea/modules/private"
    14  	gitea_context "code.gitea.io/gitea/services/context"
    15  )
    16  
    17  // SetDefaultBranch updates the default branch
    18  func SetDefaultBranch(ctx *gitea_context.PrivateContext) {
    19  	ownerName := ctx.Params(":owner")
    20  	repoName := ctx.Params(":repo")
    21  	branch := ctx.Params(":branch")
    22  
    23  	ctx.Repo.Repository.DefaultBranch = branch
    24  	if err := gitrepo.SetDefaultBranch(ctx, ctx.Repo.Repository, ctx.Repo.Repository.DefaultBranch); err != nil {
    25  		if !git.IsErrUnsupportedVersion(err) {
    26  			ctx.JSON(http.StatusInternalServerError, private.Response{
    27  				Err: fmt.Sprintf("Unable to set default branch on repository: %s/%s Error: %v", ownerName, repoName, err),
    28  			})
    29  			return
    30  		}
    31  	}
    32  
    33  	if err := repo_model.UpdateDefaultBranch(ctx, ctx.Repo.Repository); err != nil {
    34  		ctx.JSON(http.StatusInternalServerError, private.Response{
    35  			Err: fmt.Sprintf("Unable to set default branch on repository: %s/%s Error: %v", ownerName, repoName, err),
    36  		})
    37  		return
    38  	}
    39  	ctx.PlainText(http.StatusOK, "success")
    40  }