summaryrefslogtreecommitdiffstats
path: root/routers/repo
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 /routers/repo
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 'routers/repo')
-rw-r--r--routers/repo/branch.go77
-rw-r--r--routers/repo/pull.go46
2 files changed, 29 insertions, 94 deletions
diff --git a/routers/repo/branch.go b/routers/repo/branch.go
index eecaa88821..4625b1a272 100644
--- a/routers/repo/branch.go
+++ b/routers/repo/branch.go
@@ -6,6 +6,7 @@
package repo
import (
+ "errors"
"fmt"
"net/http"
"strings"
@@ -83,34 +84,23 @@ func Branches(ctx *context.Context) {
func DeleteBranchPost(ctx *context.Context) {
defer redirect(ctx)
branchName := ctx.Query("name")
- if branchName == ctx.Repo.Repository.DefaultBranch {
- log.Debug("DeleteBranch: Can't delete default branch '%s'", branchName)
- ctx.Flash.Error(ctx.Tr("repo.branch.default_deletion_failed", branchName))
- return
- }
-
- isProtected, err := ctx.Repo.Repository.IsProtectedBranch(branchName, ctx.User)
- if err != nil {
- log.Error("DeleteBranch: %v", err)
- ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", branchName))
- return
- }
-
- if isProtected {
- log.Debug("DeleteBranch: Can't delete protected branch '%s'", branchName)
- ctx.Flash.Error(ctx.Tr("repo.branch.protected_deletion_failed", branchName))
- return
- }
- if !ctx.Repo.GitRepo.IsBranchExist(branchName) {
- log.Debug("DeleteBranch: Can't delete non existing branch '%s'", branchName)
- ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", branchName))
- return
- }
+ if err := repo_service.DeleteBranch(ctx.User, 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))
+ 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))
+ }
- if err := deleteBranch(ctx, branchName); err != nil {
- log.Error("DeleteBranch: %v", err)
- ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", branchName))
return
}
@@ -169,41 +159,6 @@ func redirect(ctx *context.Context) {
})
}
-func deleteBranch(ctx *context.Context, branchName string) error {
- commit, err := ctx.Repo.GitRepo.GetBranchCommit(branchName)
- if err != nil {
- log.Error("GetBranchCommit: %v", err)
- return err
- }
-
- if err := ctx.Repo.GitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{
- Force: true,
- }); err != nil {
- log.Error("DeleteBranch: %v", err)
- return err
- }
-
- // Don't return error below this
- if err := repo_service.PushUpdate(
- &repo_module.PushUpdateOptions{
- RefFullName: git.BranchPrefix + branchName,
- OldCommitID: commit.ID.String(),
- NewCommitID: git.EmptySHA,
- PusherID: ctx.User.ID,
- PusherName: ctx.User.Name,
- RepoUserName: ctx.Repo.Owner.Name,
- RepoName: ctx.Repo.Repository.Name,
- }); err != nil {
- log.Error("Update: %v", err)
- }
-
- if err := ctx.Repo.Repository.AddDeletedBranch(branchName, commit.ID.String(), ctx.User.ID); err != nil {
- log.Warn("AddDeletedBranch: %v", err)
- }
-
- return nil
-}
-
// loadBranches loads branches from the repository limited by page & pageSize.
// NOTE: May write to context on error.
func loadBranches(ctx *context.Context, skip, limit int) ([]*Branch, int) {
diff --git a/routers/repo/pull.go b/routers/repo/pull.go
index bb166c68a6..28f94c8417 100644
--- a/routers/repo/pull.go
+++ b/routers/repo/pull.go
@@ -9,6 +9,7 @@ package repo
import (
"container/list"
"crypto/subtle"
+ "errors"
"fmt"
"net/http"
"path"
@@ -21,7 +22,6 @@ import (
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/notification"
- repo_module "code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/upload"
@@ -1186,20 +1186,6 @@ func CleanUpPullRequest(ctx *context.Context) {
})
}()
- if pr.HeadBranch == pr.HeadRepo.DefaultBranch || !gitRepo.IsBranchExist(pr.HeadBranch) {
- ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
- return
- }
-
- // Check if branch is not protected
- if protected, err := pr.HeadRepo.IsProtectedBranch(pr.HeadBranch, ctx.User); err != nil || protected {
- if err != nil {
- log.Error("HeadRepo.IsProtectedBranch: %v", err)
- }
- ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
- return
- }
-
// Check if branch has no new commits
headCommitID, err := gitBaseRepo.GetRefCommitID(pr.GetGitRefName())
if err != nil {
@@ -1218,27 +1204,21 @@ func CleanUpPullRequest(ctx *context.Context) {
return
}
- if err := gitRepo.DeleteBranch(pr.HeadBranch, git.DeleteBranchOptions{
- Force: true,
- }); err != nil {
- log.Error("DeleteBranch: %v", err)
- ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
+ if err := repo_service.DeleteBranch(ctx.User, pr.HeadRepo, gitRepo, pr.HeadBranch); err != nil {
+ switch {
+ case git.IsErrBranchNotExist(err):
+ ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
+ case errors.Is(err, repo_service.ErrBranchIsDefault):
+ ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
+ case errors.Is(err, repo_service.ErrBranchIsProtected):
+ ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
+ default:
+ log.Error("DeleteBranch: %v", err)
+ ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
+ }
return
}
- if err := repo_service.PushUpdate(
- &repo_module.PushUpdateOptions{
- RefFullName: git.BranchPrefix + pr.HeadBranch,
- OldCommitID: branchCommitID,
- NewCommitID: git.EmptySHA,
- PusherID: ctx.User.ID,
- PusherName: ctx.User.Name,
- RepoUserName: pr.HeadRepo.Owner.Name,
- RepoName: pr.HeadRepo.Name,
- }); err != nil {
- log.Error("Update: %v", err)
- }
-
if err := models.AddDeletePRBranchComment(ctx.User, pr.BaseRepo, issue.ID, pr.HeadBranch); err != nil {
// Do not fail here as branch has already been deleted
log.Error("DeleteBranch: %v", err)