Files

276 lines
9.5 KiB
Go
Raw Permalink Normal View History

2014-03-24 18:25:15 +08:00
// Copyright 2014 The Gogs Authors. All rights reserved.
// Copyright 2018 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
2014-03-24 18:25:15 +08:00
package repo
import (
"errors"
"fmt"
"net/http"
"net/url"
2017-10-26 02:49:16 +02:00
"strings"
2022-06-12 23:51:54 +08:00
git_model "code.gitea.io/gitea/models/git"
repo_model "code.gitea.io/gitea/models/repo"
2021-11-10 03:57:58 +08:00
"code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/gitrepo"
2017-10-26 02:49:16 +02:00
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/optional"
repo_module "code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/templates"
2019-03-26 15:59:48 -04:00
"code.gitea.io/gitea/modules/util"
2021-01-26 23:36:53 +08:00
"code.gitea.io/gitea/modules/web"
"code.gitea.io/gitea/routers/utils"
"code.gitea.io/gitea/services/context"
"code.gitea.io/gitea/services/forms"
pull_service "code.gitea.io/gitea/services/pull"
2021-03-01 03:57:45 +08:00
release_service "code.gitea.io/gitea/services/release"
repo_service "code.gitea.io/gitea/services/repository"
2014-03-24 18:25:15 +08:00
)
2014-06-22 23:11:12 -04:00
const (
tplBranch templates.TplName = "repo/branch/list"
2014-06-22 23:11:12 -04:00
)
// Branches render repository branch page
2016-03-11 11:56:52 -05:00
func Branches(ctx *context.Context) {
2014-05-25 20:11:25 -04:00
ctx.Data["Title"] = "Branches"
ctx.Data["AllowsPulls"] = ctx.Repo.Repository.AllowsPulls(ctx)
2021-11-10 03:57:58 +08:00
ctx.Data["IsWriter"] = ctx.Repo.CanWrite(unit.TypeCode)
2017-10-26 02:49:16 +02:00
ctx.Data["IsMirror"] = ctx.Repo.Repository.IsMirror
// TODO: Can be replaced by ctx.Repo.PullRequestCtx.CanCreateNewPull()
ctx.Data["CanPull"] = ctx.Repo.CanWrite(unit.TypeCode) ||
2023-09-14 19:09:32 +02:00
(ctx.IsSigned && repo_model.HasForkedRepo(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID))
2017-10-26 02:49:16 +02:00
ctx.Data["PageIsViewCode"] = true
ctx.Data["PageIsBranches"] = true
2025-06-18 03:48:09 +02:00
page := max(ctx.FormInt("page"), 1)
pageSize := setting.Git.BranchesRangeSize
2021-01-19 12:07:38 +08:00
2023-09-17 16:24:40 +08:00
kw := ctx.FormString("q")
defaultBranch, branches, branchesCount, err := repo_service.LoadBranches(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, optional.None[bool](), kw, page, pageSize)
2023-06-29 18:03:20 +08:00
if err != nil {
ctx.ServerError("LoadBranches", err)
2021-01-19 12:07:38 +08:00
return
}
2023-06-29 18:03:20 +08:00
2023-07-03 11:32:21 +08:00
commitIDs := []string{defaultBranch.DBBranch.CommitID}
for _, branch := range branches {
commitIDs = append(commitIDs, branch.DBBranch.CommitID)
}
commitStatuses, err := git_model.GetLatestCommitStatusForRepoCommitIDs(ctx, ctx.Repo.Repository.ID, commitIDs)
if err != nil {
ctx.ServerError("LoadBranches", err)
return
}
if !ctx.Repo.CanRead(unit.TypeActions) {
for key := range commitStatuses {
git_model.CommitStatusesHideActionsURL(ctx, commitStatuses[key])
}
}
2023-07-03 11:32:21 +08:00
commitStatus := make(map[string]*git_model.CommitStatus)
for commitID, cs := range commitStatuses {
commitStatus[commitID] = git_model.CalcCommitStatus(cs)
}
2023-09-17 16:24:40 +08:00
ctx.Data["Keyword"] = kw
2021-01-19 12:07:38 +08:00
ctx.Data["Branches"] = branches
2023-07-03 11:32:21 +08:00
ctx.Data["CommitStatus"] = commitStatus
ctx.Data["CommitStatuses"] = commitStatuses
2023-06-29 18:03:20 +08:00
ctx.Data["DefaultBranchBranch"] = defaultBranch
2026-03-08 15:35:50 +01:00
pager := context.NewPagination(branchesCount, pageSize, page, 5)
2024-12-30 09:57:38 +08:00
pager.AddParamFromRequest(ctx.Req)
2021-01-19 12:07:38 +08:00
ctx.Data["Page"] = pager
ctx.HTML(http.StatusOK, tplBranch)
2017-10-26 02:49:16 +02:00
}
2014-05-25 20:11:25 -04:00
2017-10-26 02:49:16 +02:00
// DeleteBranchPost responses for delete merged branch
func DeleteBranchPost(ctx *context.Context) {
2025-01-31 04:12:14 +08:00
defer jsonRedirectBranches(ctx)
branchName := ctx.FormString("name")
2014-03-24 18:25:15 +08:00
if err := repo_service.DeleteBranch(ctx, ctx.Doer, ctx.Repo.Repository, ctx.Repo.GitRepo, branchName); err != nil {
switch {
case git.IsErrBranchNotExist(err):
log.Debug("DeleteBranch: Can't delete non existing branch '%s'", branchName)
ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", branchName))
case errors.Is(err, repo_service.ErrBranchIsDefault):
log.Debug("DeleteBranch: Can't delete default branch '%s'", branchName)
ctx.Flash.Error(ctx.Tr("repo.branch.default_deletion_failed", branchName))
2023-01-16 16:00:22 +08:00
case errors.Is(err, git_model.ErrBranchIsProtected):
log.Debug("DeleteBranch: Can't delete protected branch '%s'", branchName)
ctx.Flash.Error(ctx.Tr("repo.branch.protected_deletion_failed", branchName))
default:
log.Error("DeleteBranch: %v", err)
ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", branchName))
}
2017-10-26 02:49:16 +02:00
return
}
ctx.Flash.Success(ctx.Tr("repo.branch.deletion_success", branchName))
}
// RestoreBranchPost responses for delete merged branch
func RestoreBranchPost(ctx *context.Context) {
2025-01-31 04:12:14 +08:00
defer jsonRedirectBranches(ctx)
2017-10-26 02:49:16 +02:00
branchID := ctx.FormInt64("branch_id")
branchName := ctx.FormString("name")
2017-10-26 02:49:16 +02:00
deletedBranch, err := git_model.GetDeletedBranchByID(ctx, ctx.Repo.Repository.ID, branchID)
2017-10-26 02:49:16 +02:00
if err != nil {
2019-04-02 08:48:31 +01:00
log.Error("GetDeletedBranchByID: %v", err)
2017-10-26 02:49:16 +02:00
ctx.Flash.Error(ctx.Tr("repo.branch.restore_failed", branchName))
return
} else if deletedBranch == nil {
log.Debug("RestoreBranch: Can't restore branch[%d] '%s', as it does not exist", branchID, branchName)
ctx.Flash.Error(ctx.Tr("repo.branch.restore_failed", branchName))
return
2017-10-26 02:49:16 +02:00
}
if err := gitrepo.Push(ctx, ctx.Repo.Repository, ctx.Repo.Repository, git.PushOptions{
2023-06-29 18:03:20 +08:00
Branch: fmt.Sprintf("%s:%s%s", deletedBranch.CommitID, git.BranchPrefix, deletedBranch.Name),
Env: repo_module.PushingEnvironment(ctx.Doer, ctx.Repo.Repository),
}); err != nil {
2017-10-26 02:49:16 +02:00
if strings.Contains(err.Error(), "already exists") {
log.Debug("RestoreBranch: Can't restore branch '%s', since one with same name already exist", deletedBranch.Name)
2017-10-26 02:49:16 +02:00
ctx.Flash.Error(ctx.Tr("repo.branch.already_exists", deletedBranch.Name))
return
}
log.Error("RestoreBranch: CreateBranch: %v", err)
2017-10-26 02:49:16 +02:00
ctx.Flash.Error(ctx.Tr("repo.branch.restore_failed", deletedBranch.Name))
return
}
objectFormat := git.ObjectFormatFromName(ctx.Repo.Repository.ObjectFormatName)
2023-12-13 21:02:00 +00:00
// Don't return error below this
if err := repo_service.PushUpdate(
2020-10-31 05:59:02 +08:00
&repo_module.PushUpdateOptions{
RefFullName: git.RefNameFromBranch(deletedBranch.Name),
2023-12-17 19:56:08 +08:00
OldCommitID: objectFormat.EmptyObjectID().String(),
2023-06-29 18:03:20 +08:00
NewCommitID: deletedBranch.CommitID,
2022-03-22 08:03:22 +01:00
PusherID: ctx.Doer.ID,
PusherName: ctx.Doer.Name,
RepoUserName: ctx.Repo.Owner.Name,
RepoName: ctx.Repo.Repository.Name,
}); err != nil {
log.Error("RestoreBranch: Update: %v", err)
}
2017-10-26 02:49:16 +02:00
ctx.Flash.Success(ctx.Tr("repo.branch.restore_success", deletedBranch.Name))
}
2025-01-31 04:12:14 +08:00
func jsonRedirectBranches(ctx *context.Context) {
ctx.JSONRedirect(ctx.Repo.RepoLink + "/branches?page=" + url.QueryEscape(ctx.FormString("page")))
2017-10-26 02:49:16 +02:00
}
// CreateBranch creates new branch in repository
2021-01-26 23:36:53 +08:00
func CreateBranch(ctx *context.Context) {
form := web.GetForm(ctx).(*forms.NewBranchForm)
if !ctx.Repo.CanCreateBranch() {
2025-02-17 14:13:17 +08:00
ctx.NotFound(nil)
return
}
if ctx.HasError() {
ctx.Flash.Error(ctx.GetErrMsg())
2025-01-12 11:39:46 +08:00
ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.RefTypeNameSubURL())
return
}
var err error
2021-03-01 03:57:45 +08:00
if form.CreateTag {
2021-11-18 00:50:17 +01:00
target := ctx.Repo.CommitID
2025-01-15 11:15:47 +08:00
if ctx.Repo.RefFullName.IsBranch() {
2021-11-18 00:50:17 +01:00
target = ctx.Repo.BranchName
2021-03-01 03:57:45 +08:00
}
2022-03-22 08:03:22 +01:00
err = release_service.CreateNewTag(ctx, ctx.Doer, ctx.Repo.Repository, target, form.NewBranchName, "")
2025-01-15 11:15:47 +08:00
} else if ctx.Repo.RefFullName.IsBranch() {
err = repo_service.CreateNewBranch(ctx, ctx.Doer, ctx.Repo.Repository, ctx.Repo.BranchName, form.NewBranchName)
} else {
err = repo_service.CreateNewBranchFromCommit(ctx, ctx.Doer, ctx.Repo.Repository, ctx.Repo.CommitID, form.NewBranchName)
}
if err != nil {
if release_service.IsErrProtectedTagName(err) {
ctx.Flash.Error(ctx.Tr("repo.release.tag_name_protected"))
2025-01-12 11:39:46 +08:00
ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.RefTypeNameSubURL())
return
}
if release_service.IsErrTagAlreadyExists(err) {
e := err.(release_service.ErrTagAlreadyExists)
ctx.Flash.Error(ctx.Tr("repo.branch.tag_collision", e.TagName))
2025-01-12 11:39:46 +08:00
ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.RefTypeNameSubURL())
return
}
2023-06-29 18:03:20 +08:00
if git_model.IsErrBranchAlreadyExists(err) || git.IsErrPushOutOfDate(err) {
ctx.Flash.Error(ctx.Tr("repo.branch.branch_already_exists", form.NewBranchName))
2025-01-12 11:39:46 +08:00
ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.RefTypeNameSubURL())
return
}
2023-06-29 18:03:20 +08:00
if git_model.IsErrBranchNameConflict(err) {
e := err.(git_model.ErrBranchNameConflict)
ctx.Flash.Error(ctx.Tr("repo.branch.branch_name_conflict", form.NewBranchName, e.BranchName))
2025-01-12 11:39:46 +08:00
ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.RefTypeNameSubURL())
return
}
if git.IsErrPushRejected(err) {
e := err.(*git.ErrPushRejected)
if len(e.Message) == 0 {
ctx.Flash.Error(ctx.Tr("repo.editor.push_rejected_no_message"))
} else {
flashError, err := ctx.RenderToHTML(tplAlertDetails, map[string]any{
"Message": ctx.Tr("repo.editor.push_rejected"),
"Summary": ctx.Tr("repo.editor.push_rejected_summary"),
"Details": utils.SanitizeFlashErrorString(e.Message),
})
if err != nil {
ctx.ServerError("UpdatePullRequest.HTMLString", err)
return
}
ctx.Flash.Error(flashError)
}
2025-01-12 11:39:46 +08:00
ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.RefTypeNameSubURL())
return
}
2018-01-10 22:34:17 +01:00
ctx.ServerError("CreateNewBranch", err)
return
}
2021-03-01 03:57:45 +08:00
if form.CreateTag {
ctx.Flash.Success(ctx.Tr("repo.tag.create_success", form.NewBranchName))
ctx.Redirect(ctx.Repo.RepoLink + "/src/tag/" + util.PathEscapeSegments(form.NewBranchName))
return
}
ctx.Flash.Success(ctx.Tr("repo.branch.create_success", form.NewBranchName))
ctx.Redirect(ctx.Repo.RepoLink + "/src/branch/" + util.PathEscapeSegments(form.NewBranchName) + "/" + util.PathEscapeSegments(form.CurrentPath))
}
func MergeUpstream(ctx *context.Context) {
branchName := ctx.FormString("branch")
_, err := repo_service.MergeUpstream(ctx, ctx.Doer, ctx.Repo.Repository, branchName, false)
if err != nil {
if errors.Is(err, util.ErrNotExist) {
2025-05-14 03:18:13 +08:00
ctx.JSONErrorNotFound()
return
} else if pull_service.IsErrMergeConflicts(err) {
ctx.JSONError(ctx.Tr("repo.pulls.merge_conflict"))
return
}
ctx.ServerError("MergeUpstream", err)
return
}
ctx.JSONRedirect("")
}