|
| 1 | +// Copyright 2021 The Gitea Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// Package private includes all internal routes. The package name internal is ideal but Golang is not allowed, so we use private as package name instead. |
| 6 | +package private |
| 7 | + |
| 8 | +import ( |
| 9 | + "fmt" |
| 10 | + "net/http" |
| 11 | + |
| 12 | + "code.gitea.io/gitea/models" |
| 13 | + gitea_context "code.gitea.io/gitea/modules/context" |
| 14 | + "code.gitea.io/gitea/modules/git" |
| 15 | + "code.gitea.io/gitea/modules/log" |
| 16 | + "code.gitea.io/gitea/modules/private" |
| 17 | +) |
| 18 | + |
| 19 | +// ________ _____ .__ __ |
| 20 | +// \______ \ _____/ ____\____ __ __| |_/ |_ |
| 21 | +// | | \_/ __ \ __\\__ \ | | \ |\ __\ |
| 22 | +// | ` \ ___/| | / __ \| | / |_| | |
| 23 | +// /_______ /\___ >__| (____ /____/|____/__| |
| 24 | +// \/ \/ \/ |
| 25 | +// __________ .__ |
| 26 | +// \______ \____________ ____ ____ | |__ |
| 27 | +// | | _/\_ __ \__ \ / \_/ ___\| | \ |
| 28 | +// | | \ | | \// __ \| | \ \___| Y \ |
| 29 | +// |______ / |__| (____ /___| /\___ >___| / |
| 30 | +// \/ \/ \/ \/ \/ |
| 31 | + |
| 32 | +// SetDefaultBranch updates the default branch |
| 33 | +func SetDefaultBranch(ctx *gitea_context.PrivateContext) { |
| 34 | + ownerName := ctx.Params(":owner") |
| 35 | + repoName := ctx.Params(":repo") |
| 36 | + branch := ctx.Params(":branch") |
| 37 | + repo, err := models.GetRepositoryByOwnerAndName(ownerName, repoName) |
| 38 | + if err != nil { |
| 39 | + log.Error("Failed to get repository: %s/%s Error: %v", ownerName, repoName, err) |
| 40 | + ctx.JSON(http.StatusInternalServerError, private.Response{ |
| 41 | + Err: fmt.Sprintf("Failed to get repository: %s/%s Error: %v", ownerName, repoName, err), |
| 42 | + }) |
| 43 | + return |
| 44 | + } |
| 45 | + if repo.OwnerName == "" { |
| 46 | + repo.OwnerName = ownerName |
| 47 | + } |
| 48 | + |
| 49 | + repo.DefaultBranch = branch |
| 50 | + gitRepo, err := git.OpenRepository(repo.RepoPath()) |
| 51 | + if err != nil { |
| 52 | + ctx.JSON(http.StatusInternalServerError, private.Response{ |
| 53 | + Err: fmt.Sprintf("Failed to get git repository: %s/%s Error: %v", ownerName, repoName, err), |
| 54 | + }) |
| 55 | + return |
| 56 | + } |
| 57 | + if err := gitRepo.SetDefaultBranch(repo.DefaultBranch); err != nil { |
| 58 | + if !git.IsErrUnsupportedVersion(err) { |
| 59 | + gitRepo.Close() |
| 60 | + ctx.JSON(http.StatusInternalServerError, private.Response{ |
| 61 | + Err: fmt.Sprintf("Unable to set default branch on repository: %s/%s Error: %v", ownerName, repoName, err), |
| 62 | + }) |
| 63 | + return |
| 64 | + } |
| 65 | + } |
| 66 | + gitRepo.Close() |
| 67 | + |
| 68 | + if err := repo.UpdateDefaultBranch(); err != nil { |
| 69 | + ctx.JSON(http.StatusInternalServerError, private.Response{ |
| 70 | + Err: fmt.Sprintf("Unable to set default branch on repository: %s/%s Error: %v", ownerName, repoName, err), |
| 71 | + }) |
| 72 | + return |
| 73 | + } |
| 74 | + ctx.PlainText(http.StatusOK, []byte("success")) |
| 75 | +} |
0 commit comments