2014-03-24 15:55:15 +05:30
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
2018-11-28 16:56:14 +05:30
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2014-03-24 15:55:15 +05:30
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
2021-06-07 20:22:59 +05:30
|
|
|
"errors"
|
2020-06-12 05:19:47 +05:30
|
|
|
"fmt"
|
2021-04-05 21:00:52 +05:30
|
|
|
"net/http"
|
2017-10-26 06:19:16 +05:30
|
|
|
"strings"
|
|
|
|
|
2017-10-16 01:29:24 +05:30
|
|
|
"code.gitea.io/gitea/models"
|
2022-06-12 21:21:54 +05:30
|
|
|
git_model "code.gitea.io/gitea/models/git"
|
2022-06-13 15:07:59 +05:30
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2021-12-10 06:57:50 +05:30
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-10 01:27:58 +05:30
|
|
|
"code.gitea.io/gitea/models/unit"
|
2016-11-10 21:54:48 +05:30
|
|
|
"code.gitea.io/gitea/modules/base"
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
2019-03-27 15:03:00 +05:30
|
|
|
"code.gitea.io/gitea/modules/git"
|
2017-10-26 06:19:16 +05:30
|
|
|
"code.gitea.io/gitea/modules/log"
|
2020-01-14 09:08:04 +05:30
|
|
|
repo_module "code.gitea.io/gitea/modules/repository"
|
2021-06-26 16:58:55 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-03-27 01:29:48 +05:30
|
|
|
"code.gitea.io/gitea/modules/util"
|
2021-01-26 21:06:53 +05:30
|
|
|
"code.gitea.io/gitea/modules/web"
|
2020-03-28 09:43:18 +05:30
|
|
|
"code.gitea.io/gitea/routers/utils"
|
2021-04-07 01:14:05 +05:30
|
|
|
"code.gitea.io/gitea/services/forms"
|
2021-03-01 01:27:45 +05:30
|
|
|
release_service "code.gitea.io/gitea/services/release"
|
2020-09-11 19:44:48 +05:30
|
|
|
repo_service "code.gitea.io/gitea/services/repository"
|
2021-11-24 13:26:24 +05:30
|
|
|
files_service "code.gitea.io/gitea/services/repository/files"
|
2014-03-24 15:55:15 +05:30
|
|
|
)
|
|
|
|
|
2014-06-23 08:41:12 +05:30
|
|
|
const (
|
2017-10-26 06:19:16 +05:30
|
|
|
tplBranch base.TplName = "repo/branch/list"
|
2014-06-23 08:41:12 +05:30
|
|
|
)
|
|
|
|
|
2017-10-26 06:19:16 +05:30
|
|
|
// Branch contains the branch information
|
|
|
|
type Branch struct {
|
2019-06-27 19:45:30 +05:30
|
|
|
Name string
|
|
|
|
Commit *git.Commit
|
|
|
|
IsProtected bool
|
|
|
|
IsDeleted bool
|
2019-10-15 04:10:17 +05:30
|
|
|
IsIncluded bool
|
2022-06-12 21:21:54 +05:30
|
|
|
DeletedBranch *git_model.DeletedBranch
|
2019-06-27 19:45:30 +05:30
|
|
|
CommitsAhead int
|
|
|
|
CommitsBehind int
|
2022-06-13 15:07:59 +05:30
|
|
|
LatestPullRequest *issues_model.PullRequest
|
2020-01-07 22:36:14 +05:30
|
|
|
MergeMovedOn bool
|
2017-10-26 06:19:16 +05:30
|
|
|
}
|
|
|
|
|
2016-11-22 14:02:00 +05:30
|
|
|
// Branches render repository branch page
|
2016-03-11 22:26:52 +05:30
|
|
|
func Branches(ctx *context.Context) {
|
2014-05-26 05:41:25 +05:30
|
|
|
ctx.Data["Title"] = "Branches"
|
|
|
|
ctx.Data["IsRepoToolbarBranches"] = true
|
2017-10-26 06:19:16 +05:30
|
|
|
ctx.Data["DefaultBranch"] = ctx.Repo.Repository.DefaultBranch
|
2019-07-12 01:51:16 +05:30
|
|
|
ctx.Data["AllowsPulls"] = ctx.Repo.Repository.AllowsPulls()
|
2021-11-10 01:27:58 +05:30
|
|
|
ctx.Data["IsWriter"] = ctx.Repo.CanWrite(unit.TypeCode)
|
2017-10-26 06:19:16 +05:30
|
|
|
ctx.Data["IsMirror"] = ctx.Repo.Repository.IsMirror
|
2021-11-22 20:51:55 +05:30
|
|
|
ctx.Data["CanPull"] = ctx.Repo.CanWrite(unit.TypeCode) ||
|
2022-03-22 12:33:22 +05:30
|
|
|
(ctx.IsSigned && repo_model.HasForkedRepo(ctx.Doer.ID, ctx.Repo.Repository.ID))
|
2017-10-26 06:19:16 +05:30
|
|
|
ctx.Data["PageIsViewCode"] = true
|
|
|
|
ctx.Data["PageIsBranches"] = true
|
|
|
|
|
2021-07-29 07:12:15 +05:30
|
|
|
page := ctx.FormInt("page")
|
2021-01-19 09:37:38 +05:30
|
|
|
if page <= 1 {
|
|
|
|
page = 1
|
|
|
|
}
|
|
|
|
|
2021-07-29 07:12:15 +05:30
|
|
|
limit := ctx.FormInt("limit")
|
2021-06-26 16:58:55 +05:30
|
|
|
if limit <= 0 || limit > setting.Git.BranchesRangeSize {
|
|
|
|
limit = setting.Git.BranchesRangeSize
|
2021-01-19 09:37:38 +05:30
|
|
|
}
|
|
|
|
|
2021-02-04 00:36:13 +05:30
|
|
|
skip := (page - 1) * limit
|
|
|
|
log.Debug("Branches: skip: %d limit: %d", skip, limit)
|
2022-01-16 20:29:16 +05:30
|
|
|
defaultBranchBranch, branches, branchesCount := loadBranches(ctx, skip, limit)
|
2021-01-19 09:37:38 +05:30
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["Branches"] = branches
|
2022-01-16 20:29:16 +05:30
|
|
|
ctx.Data["DefaultBranchBranch"] = defaultBranchBranch
|
2022-06-20 15:32:49 +05:30
|
|
|
pager := context.NewPagination(branchesCount, setting.Git.BranchesRangeSize, page, 5)
|
2021-01-19 09:37:38 +05:30
|
|
|
pager.SetDefaultParams(ctx)
|
|
|
|
ctx.Data["Page"] = pager
|
|
|
|
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.HTML(http.StatusOK, tplBranch)
|
2017-10-26 06:19:16 +05:30
|
|
|
}
|
2014-05-26 05:41:25 +05:30
|
|
|
|
2017-10-26 06:19:16 +05:30
|
|
|
// DeleteBranchPost responses for delete merged branch
|
|
|
|
func DeleteBranchPost(ctx *context.Context) {
|
|
|
|
defer redirect(ctx)
|
2021-08-11 06:01:13 +05:30
|
|
|
branchName := ctx.FormString("name")
|
2014-03-24 15:55:15 +05:30
|
|
|
|
2022-03-22 12:33:22 +05:30
|
|
|
if err := repo_service.DeleteBranch(ctx.Doer, ctx.Repo.Repository, ctx.Repo.GitRepo, branchName); err != nil {
|
2021-06-07 20:22:59 +05:30
|
|
|
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))
|
|
|
|
case errors.Is(err, repo_service.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 06:19:16 +05:30
|
|
|
|
|
|
|
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 07:12:15 +05:30
|
|
|
branchID := ctx.FormInt64("branch_id")
|
2021-08-11 06:01:13 +05:30
|
|
|
branchName := ctx.FormString("name")
|
2017-10-26 06:19:16 +05:30
|
|
|
|
2022-06-12 21:21:54 +05:30
|
|
|
deletedBranch, err := git_model.GetDeletedBranchByID(ctx.Repo.Repository.ID, branchID)
|
2017-10-26 06:19:16 +05:30
|
|
|
if err != nil {
|
2019-04-02 13:18:31 +05:30
|
|
|
log.Error("GetDeletedBranchByID: %v", err)
|
2017-10-26 06:19:16 +05:30
|
|
|
ctx.Flash.Error(ctx.Tr("repo.branch.restore_failed", branchName))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-01 01:36:32 +05:30
|
|
|
if err := git.Push(ctx, ctx.Repo.Repository.RepoPath(), git.PushOptions{
|
2020-06-12 05:19:47 +05:30
|
|
|
Remote: ctx.Repo.Repository.RepoPath(),
|
|
|
|
Branch: fmt.Sprintf("%s:%s%s", deletedBranch.Commit, git.BranchPrefix, deletedBranch.Name),
|
2022-05-08 22:16:32 +05:30
|
|
|
Env: repo_module.PushingEnvironment(ctx.Doer, ctx.Repo.Repository),
|
2020-06-12 05:19:47 +05:30
|
|
|
}); err != nil {
|
2017-10-26 06:19:16 +05:30
|
|
|
if strings.Contains(err.Error(), "already exists") {
|
2021-02-04 00:36:13 +05:30
|
|
|
log.Debug("RestoreBranch: Can't restore branch '%s', since one with same name already exist", deletedBranch.Name)
|
2017-10-26 06:19:16 +05:30
|
|
|
ctx.Flash.Error(ctx.Tr("repo.branch.already_exists", deletedBranch.Name))
|
|
|
|
return
|
|
|
|
}
|
2021-02-04 00:36:13 +05:30
|
|
|
log.Error("RestoreBranch: CreateBranch: %v", err)
|
2017-10-26 06:19:16 +05:30
|
|
|
ctx.Flash.Error(ctx.Tr("repo.branch.restore_failed", deletedBranch.Name))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-26 18:47:31 +05:30
|
|
|
// Don't return error below this
|
2020-09-11 19:44:48 +05:30
|
|
|
if err := repo_service.PushUpdate(
|
2020-10-31 03:29:02 +05:30
|
|
|
&repo_module.PushUpdateOptions{
|
2019-12-26 18:47:31 +05:30
|
|
|
RefFullName: git.BranchPrefix + deletedBranch.Name,
|
|
|
|
OldCommitID: git.EmptySHA,
|
|
|
|
NewCommitID: deletedBranch.Commit,
|
2022-03-22 12:33:22 +05:30
|
|
|
PusherID: ctx.Doer.ID,
|
|
|
|
PusherName: ctx.Doer.Name,
|
2019-12-26 18:47:31 +05:30
|
|
|
RepoUserName: ctx.Repo.Owner.Name,
|
|
|
|
RepoName: ctx.Repo.Repository.Name,
|
|
|
|
}); err != nil {
|
2021-02-04 00:36:13 +05:30
|
|
|
log.Error("RestoreBranch: Update: %v", err)
|
2019-12-26 18:47:31 +05:30
|
|
|
}
|
|
|
|
|
2017-10-26 06:19:16 +05:30
|
|
|
ctx.Flash.Success(ctx.Tr("repo.branch.restore_success", deletedBranch.Name))
|
|
|
|
}
|
|
|
|
|
|
|
|
func redirect(ctx *context.Context) {
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.JSON(http.StatusOK, map[string]interface{}{
|
2017-10-26 06:19:16 +05:30
|
|
|
"redirect": ctx.Repo.RepoLink + "/branches",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-01-19 09:37:38 +05:30
|
|
|
// loadBranches loads branches from the repository limited by page & pageSize.
|
2021-02-04 00:36:13 +05:30
|
|
|
// NOTE: May write to context on error.
|
2022-01-16 20:29:16 +05:30
|
|
|
func loadBranches(ctx *context.Context, skip, limit int) (*Branch, []*Branch, int) {
|
2021-12-09 00:38:16 +05:30
|
|
|
defaultBranch, err := ctx.Repo.GitRepo.GetBranch(ctx.Repo.Repository.DefaultBranch)
|
2021-01-19 09:37:38 +05:30
|
|
|
if err != nil {
|
2022-01-16 20:29:16 +05:30
|
|
|
if !git.IsErrBranchNotExist(err) {
|
|
|
|
log.Error("loadBranches: get default branch: %v", err)
|
|
|
|
ctx.ServerError("GetDefaultBranch", err)
|
|
|
|
return nil, nil, 0
|
|
|
|
}
|
|
|
|
log.Warn("loadBranches: missing default branch %s for %-v", ctx.Repo.Repository.DefaultBranch, ctx.Repo.Repository)
|
2021-01-19 09:37:38 +05:30
|
|
|
}
|
|
|
|
|
2021-12-09 00:38:16 +05:30
|
|
|
rawBranches, totalNumOfBranches, err := ctx.Repo.GitRepo.GetBranches(skip, limit)
|
2017-10-26 06:19:16 +05:30
|
|
|
if err != nil {
|
2021-02-04 00:36:13 +05:30
|
|
|
log.Error("GetBranches: %v", err)
|
2018-01-11 03:04:17 +05:30
|
|
|
ctx.ServerError("GetBranches", err)
|
2022-01-16 20:29:16 +05:30
|
|
|
return nil, nil, 0
|
2017-10-26 06:19:16 +05:30
|
|
|
}
|
|
|
|
|
2022-06-12 21:21:54 +05:30
|
|
|
protectedBranches, err := git_model.GetProtectedBranches(ctx.Repo.Repository.ID)
|
2019-08-01 20:20:06 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetProtectedBranches", err)
|
2022-01-16 20:29:16 +05:30
|
|
|
return nil, nil, 0
|
2019-08-01 20:20:06 +05:30
|
|
|
}
|
|
|
|
|
2021-12-10 06:57:50 +05:30
|
|
|
repoIDToRepo := map[int64]*repo_model.Repository{}
|
2020-01-07 22:36:14 +05:30
|
|
|
repoIDToRepo[ctx.Repo.Repository.ID] = ctx.Repo.Repository
|
|
|
|
|
|
|
|
repoIDToGitRepo := map[int64]*git.Repository{}
|
|
|
|
repoIDToGitRepo[ctx.Repo.Repository.ID] = ctx.Repo.GitRepo
|
|
|
|
|
2021-01-19 09:37:38 +05:30
|
|
|
var branches []*Branch
|
2021-02-04 00:36:13 +05:30
|
|
|
for i := range rawBranches {
|
2022-01-16 20:29:16 +05:30
|
|
|
if defaultBranch != nil && rawBranches[i].Name == defaultBranch.Name {
|
2021-02-04 00:36:13 +05:30
|
|
|
// Skip default branch
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-01-20 23:16:10 +05:30
|
|
|
branch := loadOneBranch(ctx, rawBranches[i], defaultBranch, protectedBranches, repoIDToRepo, repoIDToGitRepo)
|
2021-01-19 09:37:38 +05:30
|
|
|
if branch == nil {
|
2022-01-16 20:29:16 +05:30
|
|
|
return nil, nil, 0
|
2021-01-19 09:37:38 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
branches = append(branches, branch)
|
|
|
|
}
|
|
|
|
|
2022-01-16 20:29:16 +05:30
|
|
|
var defaultBranchBranch *Branch
|
|
|
|
if defaultBranch != nil {
|
|
|
|
// Always add the default branch
|
|
|
|
log.Debug("loadOneBranch: load default: '%s'", defaultBranch.Name)
|
|
|
|
defaultBranchBranch = loadOneBranch(ctx, defaultBranch, defaultBranch, protectedBranches, repoIDToRepo, repoIDToGitRepo)
|
|
|
|
branches = append(branches, defaultBranchBranch)
|
|
|
|
}
|
2021-01-19 09:37:38 +05:30
|
|
|
|
2021-11-10 01:27:58 +05:30
|
|
|
if ctx.Repo.CanWrite(unit.TypeCode) {
|
2021-01-19 09:37:38 +05:30
|
|
|
deletedBranches, err := getDeletedBranches(ctx)
|
2017-10-26 06:19:16 +05:30
|
|
|
if err != nil {
|
2021-01-19 09:37:38 +05:30
|
|
|
ctx.ServerError("getDeletedBranches", err)
|
2022-01-16 20:29:16 +05:30
|
|
|
return nil, nil, 0
|
2017-10-26 06:19:16 +05:30
|
|
|
}
|
2021-01-19 09:37:38 +05:30
|
|
|
branches = append(branches, deletedBranches...)
|
|
|
|
}
|
2017-10-26 06:19:16 +05:30
|
|
|
|
2022-01-16 20:29:16 +05:30
|
|
|
return defaultBranchBranch, branches, totalNumOfBranches
|
2021-01-19 09:37:38 +05:30
|
|
|
}
|
|
|
|
|
2022-06-12 21:21:54 +05:30
|
|
|
func loadOneBranch(ctx *context.Context, rawBranch, defaultBranch *git.Branch, protectedBranches []*git_model.ProtectedBranch,
|
2021-12-10 06:57:50 +05:30
|
|
|
repoIDToRepo map[int64]*repo_model.Repository,
|
2022-02-24 01:46:07 +05:30
|
|
|
repoIDToGitRepo map[int64]*git.Repository,
|
|
|
|
) *Branch {
|
2021-02-04 00:36:13 +05:30
|
|
|
log.Trace("loadOneBranch: '%s'", rawBranch.Name)
|
2021-01-19 09:37:38 +05:30
|
|
|
|
|
|
|
commit, err := rawBranch.GetCommit()
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetCommit", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
branchName := rawBranch.Name
|
|
|
|
var isProtected bool
|
|
|
|
for _, b := range protectedBranches {
|
|
|
|
if b.BranchName == branchName {
|
|
|
|
isProtected = true
|
|
|
|
break
|
2017-10-26 06:19:16 +05:30
|
|
|
}
|
2021-01-19 09:37:38 +05:30
|
|
|
}
|
|
|
|
|
2022-01-16 20:29:16 +05:30
|
|
|
divergence := &git.DivergeObject{
|
|
|
|
Ahead: -1,
|
|
|
|
Behind: -1,
|
|
|
|
}
|
|
|
|
if defaultBranch != nil {
|
2022-01-20 04:56:57 +05:30
|
|
|
divergence, err = files_service.CountDivergingCommits(ctx, ctx.Repo.Repository, git.BranchPrefix+branchName)
|
2022-01-16 20:29:16 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Error("CountDivergingCommits", err)
|
|
|
|
}
|
2021-01-19 09:37:38 +05:30
|
|
|
}
|
2017-10-26 06:19:16 +05:30
|
|
|
|
2022-06-13 15:07:59 +05:30
|
|
|
pr, err := issues_model.GetLatestPullRequestByHeadInfo(ctx.Repo.Repository.ID, branchName)
|
2021-01-19 09:37:38 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("GetLatestPullRequestByHeadInfo", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
headCommit := commit.ID.String()
|
|
|
|
|
|
|
|
mergeMovedOn := false
|
|
|
|
if pr != nil {
|
|
|
|
pr.HeadRepo = ctx.Repo.Repository
|
|
|
|
if err := pr.LoadIssue(); err != nil {
|
|
|
|
ctx.ServerError("pr.LoadIssue", err)
|
2019-05-05 21:55:25 +05:30
|
|
|
return nil
|
|
|
|
}
|
2021-01-19 09:37:38 +05:30
|
|
|
if repo, ok := repoIDToRepo[pr.BaseRepoID]; ok {
|
|
|
|
pr.BaseRepo = repo
|
2022-04-28 17:18:48 +05:30
|
|
|
} else if err := pr.LoadBaseRepoCtx(ctx); err != nil {
|
2021-01-19 09:37:38 +05:30
|
|
|
ctx.ServerError("pr.LoadBaseRepo", err)
|
2019-06-27 19:45:30 +05:30
|
|
|
return nil
|
2021-01-19 09:37:38 +05:30
|
|
|
} else {
|
|
|
|
repoIDToRepo[pr.BaseRepoID] = pr.BaseRepo
|
2019-06-27 19:45:30 +05:30
|
|
|
}
|
2021-01-19 09:37:38 +05:30
|
|
|
pr.Issue.Repo = pr.BaseRepo
|
|
|
|
|
|
|
|
if pr.HasMerged {
|
|
|
|
baseGitRepo, ok := repoIDToGitRepo[pr.BaseRepoID]
|
|
|
|
if !ok {
|
2022-03-30 00:43:41 +05:30
|
|
|
baseGitRepo, err = git.OpenRepository(ctx, pr.BaseRepo.RepoPath())
|
2021-01-19 09:37:38 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("OpenRepository", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
defer baseGitRepo.Close()
|
|
|
|
repoIDToGitRepo[pr.BaseRepoID] = baseGitRepo
|
2019-06-27 19:45:30 +05:30
|
|
|
}
|
2021-01-19 09:37:38 +05:30
|
|
|
pullCommit, err := baseGitRepo.GetRefCommitID(pr.GetGitRefName())
|
|
|
|
if err != nil && !git.IsErrNotExist(err) {
|
|
|
|
ctx.ServerError("GetBranchCommitID", err)
|
2020-01-07 22:36:14 +05:30
|
|
|
return nil
|
|
|
|
}
|
2021-01-19 09:37:38 +05:30
|
|
|
if err == nil && headCommit != pullCommit {
|
|
|
|
// the head has moved on from the merge - we shouldn't delete
|
|
|
|
mergeMovedOn = true
|
2020-01-07 22:36:14 +05:30
|
|
|
}
|
2019-06-27 19:45:30 +05:30
|
|
|
}
|
2017-10-26 06:19:16 +05:30
|
|
|
}
|
|
|
|
|
2021-01-19 09:37:38 +05:30
|
|
|
isIncluded := divergence.Ahead == 0 && ctx.Repo.Repository.DefaultBranch != branchName
|
|
|
|
return &Branch{
|
|
|
|
Name: branchName,
|
|
|
|
Commit: commit,
|
|
|
|
IsProtected: isProtected,
|
|
|
|
IsIncluded: isIncluded,
|
|
|
|
CommitsAhead: divergence.Ahead,
|
|
|
|
CommitsBehind: divergence.Behind,
|
|
|
|
LatestPullRequest: pr,
|
|
|
|
MergeMovedOn: mergeMovedOn,
|
2017-10-26 06:19:16 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getDeletedBranches(ctx *context.Context) ([]*Branch, error) {
|
|
|
|
branches := []*Branch{}
|
|
|
|
|
2022-06-12 21:21:54 +05:30
|
|
|
deletedBranches, err := git_model.GetDeletedBranches(ctx.Repo.Repository.ID)
|
2017-10-26 06:19:16 +05:30
|
|
|
if err != nil {
|
|
|
|
return branches, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range deletedBranches {
|
|
|
|
deletedBranches[i].LoadUser()
|
|
|
|
branches = append(branches, &Branch{
|
|
|
|
Name: deletedBranches[i].Name,
|
|
|
|
IsDeleted: true,
|
|
|
|
DeletedBranch: deletedBranches[i],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return branches, nil
|
2014-03-24 15:55:15 +05:30
|
|
|
}
|
2017-10-16 01:29:24 +05:30
|
|
|
|
|
|
|
// CreateBranch creates new branch in repository
|
2021-01-26 21:06:53 +05:30
|
|
|
func CreateBranch(ctx *context.Context) {
|
2021-04-07 01:14:05 +05:30
|
|
|
form := web.GetForm(ctx).(*forms.NewBranchForm)
|
2017-10-16 01:29:24 +05:30
|
|
|
if !ctx.Repo.CanCreateBranch() {
|
2018-01-11 03:04:17 +05:30
|
|
|
ctx.NotFound("CreateBranch", nil)
|
2017-10-16 01:29:24 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
|
|
|
ctx.Flash.Error(ctx.GetErrMsg())
|
2017-10-30 07:34:25 +05:30
|
|
|
ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL())
|
2017-10-16 01:29:24 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2021-03-01 01:27:45 +05:30
|
|
|
|
|
|
|
if form.CreateTag {
|
2021-11-18 05:20:17 +05:30
|
|
|
target := ctx.Repo.CommitID
|
|
|
|
if ctx.Repo.IsViewBranch {
|
|
|
|
target = ctx.Repo.BranchName
|
2021-03-01 01:27:45 +05:30
|
|
|
}
|
2022-03-22 12:33:22 +05:30
|
|
|
err = release_service.CreateNewTag(ctx, ctx.Doer, ctx.Repo.Repository, target, form.NewBranchName, "")
|
2021-03-01 01:27:45 +05:30
|
|
|
} else if ctx.Repo.IsViewBranch {
|
2022-03-22 12:33:22 +05:30
|
|
|
err = repo_service.CreateNewBranch(ctx, ctx.Doer, ctx.Repo.Repository, ctx.Repo.BranchName, form.NewBranchName)
|
2017-10-16 01:29:24 +05:30
|
|
|
} else {
|
2022-03-22 12:33:22 +05:30
|
|
|
err = repo_service.CreateNewBranchFromCommit(ctx, ctx.Doer, ctx.Repo.Repository, ctx.Repo.CommitID, form.NewBranchName)
|
2017-10-16 01:29:24 +05:30
|
|
|
}
|
|
|
|
if err != nil {
|
2022-06-17 01:33:03 +05:30
|
|
|
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 01:29:24 +05:30
|
|
|
if models.IsErrTagAlreadyExists(err) {
|
|
|
|
e := err.(models.ErrTagAlreadyExists)
|
|
|
|
ctx.Flash.Error(ctx.Tr("repo.branch.tag_collision", e.TagName))
|
2017-10-30 07:34:25 +05:30
|
|
|
ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL())
|
2017-10-16 01:29:24 +05:30
|
|
|
return
|
|
|
|
}
|
2020-03-28 09:43:18 +05:30
|
|
|
if models.IsErrBranchAlreadyExists(err) || git.IsErrPushOutOfDate(err) {
|
|
|
|
ctx.Flash.Error(ctx.Tr("repo.branch.branch_already_exists", form.NewBranchName))
|
2017-10-30 07:34:25 +05:30
|
|
|
ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL())
|
2017-10-16 01:29:24 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
if models.IsErrBranchNameConflict(err) {
|
|
|
|
e := err.(models.ErrBranchNameConflict)
|
|
|
|
ctx.Flash.Error(ctx.Tr("repo.branch.branch_name_conflict", form.NewBranchName, e.BranchName))
|
2017-10-30 07:34:25 +05:30
|
|
|
ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL())
|
2017-10-16 01:29:24 +05:30
|
|
|
return
|
|
|
|
}
|
2020-03-28 09:43:18 +05:30
|
|
|
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 {
|
2021-12-15 12:29:57 +05:30
|
|
|
flashError, err := ctx.RenderToString(tplAlertDetails, map[string]interface{}{
|
2020-10-21 05:20:10 +05:30
|
|
|
"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 09:43:18 +05:30
|
|
|
}
|
|
|
|
ctx.Redirect(ctx.Repo.RepoLink + "/src/" + ctx.Repo.BranchNameSubURL())
|
|
|
|
return
|
|
|
|
}
|
2017-10-16 01:29:24 +05:30
|
|
|
|
2018-01-11 03:04:17 +05:30
|
|
|
ctx.ServerError("CreateNewBranch", err)
|
2017-10-16 01:29:24 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-01 01:27:45 +05:30
|
|
|
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 01:29:24 +05:30
|
|
|
ctx.Flash.Success(ctx.Tr("repo.branch.create_success", form.NewBranchName))
|
2019-03-27 01:29:48 +05:30
|
|
|
ctx.Redirect(ctx.Repo.RepoLink + "/src/branch/" + util.PathEscapeSegments(form.NewBranchName))
|
2017-10-16 01:29:24 +05:30
|
|
|
}
|