summaryrefslogtreecommitdiffstats
path: root/services
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2021-06-07 22:52:59 +0800
committerGitHub <noreply@github.com>2021-06-07 22:52:59 +0800
commit0909695204a73901b9164b58930cc2e3efe0fe48 (patch)
treeda740cd8bc9769a714984759744fd3cabb3ebd18 /services
parent3607f79d7869046d919fed05a21b55b6e61df1fa (diff)
downloadgitea-0909695204a73901b9164b58930cc2e3efe0fe48.tar.gz
gitea-0909695204a73901b9164b58930cc2e3efe0fe48.zip
Merge all deleteBranch as one function and also fix bug when delete branch don't close related PRs (#16067)
* Fix bug when delete branch don't close related PRs * Merge all deletebranch as one method * Add missed branch.go * fix comment Co-authored-by: Lauris BH <lauris@nix.lv>
Diffstat (limited to 'services')
-rw-r--r--services/repository/branch.go72
1 files changed, 72 insertions, 0 deletions
diff --git a/services/repository/branch.go b/services/repository/branch.go
new file mode 100644
index 0000000000..df07030be3
--- /dev/null
+++ b/services/repository/branch.go
@@ -0,0 +1,72 @@
+// Copyright 2021 The Gitea Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package repository
+
+import (
+ "errors"
+
+ "code.gitea.io/gitea/models"
+ "code.gitea.io/gitea/modules/git"
+ "code.gitea.io/gitea/modules/log"
+ repo_module "code.gitea.io/gitea/modules/repository"
+ pull_service "code.gitea.io/gitea/services/pull"
+)
+
+// enmuerates all branch related errors
+var (
+ ErrBranchIsDefault = errors.New("branch is default")
+ ErrBranchIsProtected = errors.New("branch is protected")
+)
+
+// DeleteBranch delete branch
+func DeleteBranch(doer *models.User, repo *models.Repository, gitRepo *git.Repository, branchName string) error {
+ if branchName == repo.DefaultBranch {
+ return ErrBranchIsDefault
+ }
+
+ isProtected, err := repo.IsProtectedBranch(branchName, doer)
+ if err != nil {
+ return err
+ }
+
+ if isProtected {
+ return ErrBranchIsProtected
+ }
+
+ commit, err := gitRepo.GetBranchCommit(branchName)
+ if err != nil {
+ return err
+ }
+
+ if err := gitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{
+ Force: true,
+ }); err != nil {
+ return err
+ }
+
+ if err := pull_service.CloseBranchPulls(doer, repo.ID, branchName); err != nil {
+ return err
+ }
+
+ // Don't return error below this
+ if err := PushUpdate(
+ &repo_module.PushUpdateOptions{
+ RefFullName: git.BranchPrefix + branchName,
+ OldCommitID: commit.ID.String(),
+ NewCommitID: git.EmptySHA,
+ PusherID: doer.ID,
+ PusherName: doer.Name,
+ RepoUserName: repo.OwnerName,
+ RepoName: repo.Name,
+ }); err != nil {
+ log.Error("Update: %v", err)
+ }
+
+ if err := repo.AddDeletedBranch(branchName, commit.ID.String(), doer.ID); err != nil {
+ log.Warn("AddDeletedBranch: %v", err)
+ }
+
+ return nil
+}