2014-03-24 18:25:15 +08:00
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
2018-11-28 19:26:14 +08:00
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2022-11-28 02:20:29 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2014-03-24 18:25:15 +08:00
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
2021-06-07 22:52:59 +08:00
|
|
|
"errors"
|
2020-06-12 07:49:47 +08:00
|
|
|
"fmt"
|
2021-04-05 23:30:52 +08:00
|
|
|
"net/http"
|
2023-03-14 13:11:38 +08:00
|
|
|
"net/url"
|
2017-10-26 08:49:16 +08:00
|
|
|
"strings"
|
|
|
|
|
2017-10-16 03:59:24 +08:00
|
|
|
"code.gitea.io/gitea/models"
|
2022-06-12 23:51:54 +08:00
|
|
|
git_model "code.gitea.io/gitea/models/git"
|
2021-12-10 09:27:50 +08:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-10 03:57:58 +08:00
|
|
|
"code.gitea.io/gitea/models/unit"
|
2016-11-11 00:24:48 +08:00
|
|
|
"code.gitea.io/gitea/modules/base"
|
2019-03-27 17:33:00 +08:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
2017-10-26 08:49:16 +08:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2024-03-02 23:42:31 +08:00
|
|
|
"code.gitea.io/gitea/modules/optional"
|
2020-01-14 11:38:04 +08:00
|
|
|
repo_module "code.gitea.io/gitea/modules/repository"
|
2021-06-26 19:28:55 +08:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-03-27 03:59:48 +08:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2021-01-26 23:36:53 +08:00
|
|
|
"code.gitea.io/gitea/modules/web"
|
2020-03-28 12:13:18 +08:00
|
|
|
"code.gitea.io/gitea/routers/utils"
|
2024-02-27 15:12:22 +08:00
|
|
|
"code.gitea.io/gitea/services/context"
|
2021-04-07 03:44:05 +08:00
|
|
|
"code.gitea.io/gitea/services/forms"
|
2021-03-01 03:57:45 +08:00
|
|
|
release_service "code.gitea.io/gitea/services/release"
|
2020-09-11 22:14:48 +08:00
|
|
|
repo_service "code.gitea.io/gitea/services/repository"
|
2014-03-24 18:25:15 +08:00
|
|
|
)
|
|
|
|
|
2014-06-23 11:11:12 +08:00
|
|
|
const (
|
2017-10-26 08:49:16 +08:00
|
|
|
tplBranch base.TplName = "repo/branch/list"
|
2014-06-23 11:11:12 +08:00
|
|
|
)
|
|
|
|
|
2016-11-22 16:32:00 +08:00
|
|
|
// Branches render repository branch page
|
2016-03-12 00:56:52 +08:00
|
|
|
func Branches(ctx *context.Context) {
|
2014-05-26 08:11:25 +08:00
|
|
|
ctx.Data["Title"] = "Branches"
|
|
|
|
ctx.Data["IsRepoToolbarBranches"] = true
|
2023-10-11 12:24:07 +08:00
|
|
|
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 08:49:16 +08:00
|
|
|
ctx.Data["IsMirror"] = ctx.Repo.Repository.IsMirror
|
2021-11-22 23:21:55 +08:00
|
|
|
ctx.Data["CanPull"] = ctx.Repo.CanWrite(unit.TypeCode) ||
|
2023-09-15 01:09:32 +08:00
|
|
|
(ctx.IsSigned && repo_model.HasForkedRepo(ctx, ctx.Doer.ID, ctx.Repo.Repository.ID))
|
2017-10-26 08:49:16 +08:00
|
|
|
ctx.Data["PageIsViewCode"] = true
|
|
|
|
ctx.Data["PageIsBranches"] = true
|
|
|
|
|
2021-07-29 09:42:15 +08:00
|
|
|
page := ctx.FormInt("page")
|
2021-01-19 12:07:38 +08:00
|
|
|
if page <= 1 {
|
|
|
|
page = 1
|
|
|
|
}
|
2023-03-14 13:11:38 +08:00
|
|
|
pageSize := setting.Git.BranchesRangeSize
|
2021-01-19 12:07:38 +08:00
|
|
|
|
2023-09-17 16:24:40 +08:00
|
|
|
kw := ctx.FormString("q")
|
|
|
|
|
2024-03-02 23:42:31 +08:00
|
|
|
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
|
|
|
|
}
|
2024-07-28 23:11:40 +08:00
|
|
|
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
|
|
|
|
pager := context.NewPagination(int(branchesCount), pageSize, page, 5)
|
2021-01-19 12:07:38 +08:00
|
|
|
pager.SetDefaultParams(ctx)
|
|
|
|
ctx.Data["Page"] = pager
|
2024-10-02 03:25:08 +08:00
|
|
|
ctx.Data["LicenseFileName"] = repo_service.LicenseFileName
|
2021-04-05 23:30:52 +08:00
|
|
|
ctx.HTML(http.StatusOK, tplBranch)
|
2017-10-26 08:49:16 +08:00
|
|
|
}
|
2014-05-26 08:11:25 +08:00
|
|
|
|
2017-10-26 08:49:16 +08:00
|
|
|
// DeleteBranchPost responses for delete merged branch
|
|
|
|
func DeleteBranchPost(ctx *context.Context) {
|
|
|
|
defer redirect(ctx)
|
2021-08-11 08:31:13 +08:00
|
|
|
branchName := ctx.FormString("name")
|
2014-03-24 18:25:15 +08:00
|
|
|
|
2023-03-01 06:17:51 +08:00
|
|
|
if err := repo_service.DeleteBranch(ctx, ctx.Doer, ctx.Repo.Repository, ctx.Repo.GitRepo, branchName); err != nil {
|
2021-06-07 22:52:59 +08:00
|
|
|
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):
|
2021-06-07 22:52:59 +08:00
|
|
|
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 08:49:16 +08:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.branch.deletion_success", branchName))
|
|
|
|
}
|
|
|
|
|
|
|
|
// RestoreBranchPost responses for delete merged branch
|
|
|
|
func RestoreBranchPost(ctx *context.Context) {
|
|
|
|
defer redirect(ctx)
|
|
|
|
|
2021-07-29 09:42:15 +08:00
|
|
|
branchID := ctx.FormInt64("branch_id")
|
2021-08-11 08:31:13 +08:00
|
|
|
branchName := ctx.FormString("name")
|
2017-10-26 08:49:16 +08:00
|
|
|
|
2023-01-09 11:50:54 +08:00
|
|
|
deletedBranch, err := git_model.GetDeletedBranchByID(ctx, ctx.Repo.Repository.ID, branchID)
|
2017-10-26 08:49:16 +08:00
|
|
|
if err != nil {
|
2019-04-02 15:48:31 +08:00
|
|
|
log.Error("GetDeletedBranchByID: %v", err)
|
2017-10-26 08:49:16 +08:00
|
|
|
ctx.Flash.Error(ctx.Tr("repo.branch.restore_failed", branchName))
|
|
|
|
return
|
2022-11-26 04:58:20 +08:00
|
|
|
} 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 08:49:16 +08:00
|
|
|
}
|
|
|
|
|
2021-12-01 04:06:32 +08:00
|
|
|
if err := git.Push(ctx, ctx.Repo.Repository.RepoPath(), git.PushOptions{
|
2020-06-12 07:49:47 +08:00
|
|
|
Remote: ctx.Repo.Repository.RepoPath(),
|
2023-06-29 18:03:20 +08:00
|
|
|
Branch: fmt.Sprintf("%s:%s%s", deletedBranch.CommitID, git.BranchPrefix, deletedBranch.Name),
|
2022-05-09 00:46:32 +08:00
|
|
|
Env: repo_module.PushingEnvironment(ctx.Doer, ctx.Repo.Repository),
|
2020-06-12 07:49:47 +08:00
|
|
|
}); err != nil {
|
2017-10-26 08:49:16 +08:00
|
|
|
if strings.Contains(err.Error(), "already exists") {
|
2021-02-04 03:06:13 +08:00
|
|
|
log.Debug("RestoreBranch: Can't restore branch '%s', since one with same name already exist", deletedBranch.Name)
|
2017-10-26 08:49:16 +08:00
|
|
|
ctx.Flash.Error(ctx.Tr("repo.branch.already_exists", deletedBranch.Name))
|
|
|
|
return
|
|
|
|
}
|
2021-02-04 03:06:13 +08:00
|
|
|
log.Error("RestoreBranch: CreateBranch: %v", err)
|
2017-10-26 08:49:16 +08:00
|
|
|
ctx.Flash.Error(ctx.Tr("repo.branch.restore_failed", deletedBranch.Name))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-11 05:30:36 +08:00
|
|
|
objectFormat := git.ObjectFormatFromName(ctx.Repo.Repository.ObjectFormatName)
|
2023-12-14 05:02:00 +08:00
|
|
|
|
2019-12-26 21:17:31 +08:00
|
|
|
// Don't return error below this
|
2020-09-11 22:14:48 +08:00
|
|
|
if err := repo_service.PushUpdate(
|
2020-10-31 05:59:02 +08:00
|
|
|
&repo_module.PushUpdateOptions{
|
2023-05-26 09:04:48 +08:00
|
|
|
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 15:03:22 +08:00
|
|
|
PusherID: ctx.Doer.ID,
|
|
|
|
PusherName: ctx.Doer.Name,
|
2019-12-26 21:17:31 +08:00
|
|
|
RepoUserName: ctx.Repo.Owner.Name,
|
|
|
|
RepoName: ctx.Repo.Repository.Name,
|
|
|
|
}); err != nil {
|
2021-02-04 03:06:13 +08:00
|
|
|
log.Error("RestoreBranch: Update: %v", err)
|
2019-12-26 21:17:31 +08:00
|
|
|
}
|
|
|
|
|
2017-10-26 08:49:16 +08:00
|
|
|
ctx.Flash.Success(ctx.Tr("repo.branch.restore_success", deletedBranch.Name))
|
|
|
|
}
|
|
|
|
|
|
|
|
func redirect(ctx *context.Context) {
|
2023-07-26 14:04:01 +08:00
|
|
|
ctx.JSONRedirect(ctx.Repo.RepoLink + "/branches?page=" + url.QueryEscape(ctx.FormString("page")))
|
2017-10-26 08:49:16 +08:00
|
|
|
}
|
|
|
|
|
2017-10-16 03:59:24 +08:00
|
|
|
// CreateBranch creates new branch in repository
|
2021-01-26 23:36:53 +08:00
|
|
|
func CreateBranch(ctx *context.Context) {
|
2021-04-07 03:44:05 +08:00
|
|
|
form := web.GetForm(ctx).(*forms.NewBranchForm)
|
2017-10-16 03:59:24 +08:00
|
|
|
if !ctx.Repo.CanCreateBranch() {
|
2018-01-11 05:34:17 +08:00
|
|
|
ctx.NotFound("CreateBranch", nil)
|
2017-10-16 03:59:24 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
|
|
|
ctx.Flash.Error(ctx.GetErrMsg())
|
2017-10-30 10:04:25 +08:00
|
|
|
ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL())
|
2017-10-16 03:59:24 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2021-03-01 03:57:45 +08:00
|
|
|
|
|
|
|
if form.CreateTag {
|
2021-11-18 07:50:17 +08:00
|
|
|
target := ctx.Repo.CommitID
|
|
|
|
if ctx.Repo.IsViewBranch {
|
|
|
|
target = ctx.Repo.BranchName
|
2021-03-01 03:57:45 +08:00
|
|
|
}
|
2022-03-22 15:03:22 +08:00
|
|
|
err = release_service.CreateNewTag(ctx, ctx.Doer, ctx.Repo.Repository, target, form.NewBranchName, "")
|
2021-03-01 03:57:45 +08:00
|
|
|
} else if ctx.Repo.IsViewBranch {
|
Also sync DB branches on push if necessary (#28361)
Fix #28056
This PR will check whether the repo has zero branch when pushing a
branch. If that, it means this repository hasn't been synced.
The reason caused that is after user upgrade from v1.20 -> v1.21, he
just push branches without visit the repository user interface. Because
all repositories routers will check whether a branches sync is necessary
but push has not such check.
For every repository, it has two states, synced or not synced. If there
is zero branch for a repository, then it will be assumed as non-sync
state. Otherwise, it's synced state. So if we think it's synced, we just
need to update branch/insert new branch. Otherwise do a full sync. So
that, for every push, there will be almost no extra load added. It's
high performance than yours.
For the implementation, we in fact will try to update the branch first,
if updated success with affect records > 0, then all are done. Because
that means the branch has been in the database. If no record is
affected, that means the branch does not exist in database. So there are
two possibilities. One is this is a new branch, then we just need to
insert the record. Another is the branches haven't been synced, then we
need to sync all the branches into database.
2023-12-09 21:30:56 +08:00
|
|
|
err = repo_service.CreateNewBranch(ctx, ctx.Doer, ctx.Repo.Repository, ctx.Repo.GitRepo, ctx.Repo.BranchName, form.NewBranchName)
|
2017-10-16 03:59:24 +08:00
|
|
|
} else {
|
Also sync DB branches on push if necessary (#28361)
Fix #28056
This PR will check whether the repo has zero branch when pushing a
branch. If that, it means this repository hasn't been synced.
The reason caused that is after user upgrade from v1.20 -> v1.21, he
just push branches without visit the repository user interface. Because
all repositories routers will check whether a branches sync is necessary
but push has not such check.
For every repository, it has two states, synced or not synced. If there
is zero branch for a repository, then it will be assumed as non-sync
state. Otherwise, it's synced state. So if we think it's synced, we just
need to update branch/insert new branch. Otherwise do a full sync. So
that, for every push, there will be almost no extra load added. It's
high performance than yours.
For the implementation, we in fact will try to update the branch first,
if updated success with affect records > 0, then all are done. Because
that means the branch has been in the database. If no record is
affected, that means the branch does not exist in database. So there are
two possibilities. One is this is a new branch, then we just need to
insert the record. Another is the branches haven't been synced, then we
need to sync all the branches into database.
2023-12-09 21:30:56 +08:00
|
|
|
err = repo_service.CreateNewBranchFromCommit(ctx, ctx.Doer, ctx.Repo.Repository, ctx.Repo.GitRepo, ctx.Repo.CommitID, form.NewBranchName)
|
2017-10-16 03:59:24 +08:00
|
|
|
}
|
|
|
|
if err != nil {
|
2022-06-17 04:03:03 +08:00
|
|
|
if models.IsErrProtectedTagName(err) {
|
|
|
|
ctx.Flash.Error(ctx.Tr("repo.release.tag_name_protected"))
|
|
|
|
ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-16 03:59:24 +08:00
|
|
|
if models.IsErrTagAlreadyExists(err) {
|
|
|
|
e := err.(models.ErrTagAlreadyExists)
|
|
|
|
ctx.Flash.Error(ctx.Tr("repo.branch.tag_collision", e.TagName))
|
2017-10-30 10:04:25 +08:00
|
|
|
ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL())
|
2017-10-16 03:59:24 +08:00
|
|
|
return
|
|
|
|
}
|
2023-06-29 18:03:20 +08:00
|
|
|
if git_model.IsErrBranchAlreadyExists(err) || git.IsErrPushOutOfDate(err) {
|
2020-03-28 12:13:18 +08:00
|
|
|
ctx.Flash.Error(ctx.Tr("repo.branch.branch_already_exists", form.NewBranchName))
|
2017-10-30 10:04:25 +08:00
|
|
|
ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL())
|
2017-10-16 03:59:24 +08:00
|
|
|
return
|
|
|
|
}
|
2023-06-29 18:03:20 +08:00
|
|
|
if git_model.IsErrBranchNameConflict(err) {
|
|
|
|
e := err.(git_model.ErrBranchNameConflict)
|
2017-10-16 03:59:24 +08:00
|
|
|
ctx.Flash.Error(ctx.Tr("repo.branch.branch_name_conflict", form.NewBranchName, e.BranchName))
|
2017-10-30 10:04:25 +08:00
|
|
|
ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL())
|
2017-10-16 03:59:24 +08:00
|
|
|
return
|
|
|
|
}
|
2020-03-28 12:13:18 +08:00
|
|
|
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 {
|
2024-03-02 23:05:07 +08:00
|
|
|
flashError, err := ctx.RenderToHTML(tplAlertDetails, map[string]any{
|
2020-10-21 07:50:10 +08:00
|
|
|
"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)
|
2020-03-28 12:13:18 +08:00
|
|
|
}
|
|
|
|
ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL())
|
|
|
|
return
|
|
|
|
}
|
2017-10-16 03:59:24 +08:00
|
|
|
|
2018-01-11 05:34:17 +08:00
|
|
|
ctx.ServerError("CreateNewBranch", err)
|
2017-10-16 03:59:24 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-10-16 03:59:24 +08:00
|
|
|
ctx.Flash.Success(ctx.Tr("repo.branch.create_success", form.NewBranchName))
|
2022-09-15 21:25:16 +08:00
|
|
|
ctx.Redirect(ctx.Repo.RepoLink + "/src/branch/" + util.PathEscapeSegments(form.NewBranchName) + "/" + util.PathEscapeSegments(form.CurrentPath))
|
2017-10-16 03:59:24 +08:00
|
|
|
}
|