code.gitea.io/gitea@v1.21.7/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  	gitea_context "code.gitea.io/gitea/modules/context"
    12  	"code.gitea.io/gitea/modules/git"
    13  	"code.gitea.io/gitea/modules/private"
    14  )
    15  
    16  // SetDefaultBranch updates the default branch
    17  func SetDefaultBranch(ctx *gitea_context.PrivateContext) {
    18  	ownerName := ctx.Params(":owner")
    19  	repoName := ctx.Params(":repo")
    20  	branch := ctx.Params(":branch")
    21  
    22  	ctx.Repo.Repository.DefaultBranch = branch
    23  	if err := ctx.Repo.GitRepo.SetDefaultBranch(ctx.Repo.Repository.DefaultBranch); err != nil {
    24  		if !git.IsErrUnsupportedVersion(err) {
    25  			ctx.JSON(http.StatusInternalServerError, private.Response{
    26  				Err: fmt.Sprintf("Unable to set default branch on repository: %s/%s Error: %v", ownerName, repoName, err),
    27  			})
    28  			return
    29  		}
    30  	}
    31  
    32  	if err := repo_model.UpdateDefaultBranch(ctx, ctx.Repo.Repository); err != nil {
    33  		ctx.JSON(http.StatusInternalServerError, private.Response{
    34  			Err: fmt.Sprintf("Unable to set default branch on repository: %s/%s Error: %v", ownerName, repoName, err),
    35  		})
    36  		return
    37  	}
    38  	ctx.PlainText(http.StatusOK, "success")
    39  }