Skip to content

Commit 949fd49

Browse files
committed
Merge all deletebranch as one method
1 parent 3d4215f commit 949fd49

File tree

4 files changed

+39
-164
lines changed

4 files changed

+39
-164
lines changed

options/locale/locale_en-US.ini

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1967,7 +1967,6 @@ branch.delete_html = Delete Branch
19671967
branch.delete_desc = Deleting a branch is permanent. It <strong>CANNOT</strong> be undone. Continue?
19681968
branch.deletion_success = Branch '%s' has been deleted.
19691969
branch.deletion_failed = Failed to delete branch '%s'.
1970-
branch.delete_branch_close_prs_failed = Failed to close related pull requests when delete branch '%s'.
19711970
branch.delete_branch_has_new_commits = Branch '%s' cannot be deleted because new commits have been added after merging.
19721971
branch.create_branch = Create branch <strong>%s</strong>
19731972
branch.create_from = from '%s'

routers/api/v1/repo/branch.go

Lines changed: 10 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
package repo
77

88
import (
9+
"errors"
910
"fmt"
1011
"net/http"
1112

1213
"code.gitea.io/gitea/models"
1314
"code.gitea.io/gitea/modules/context"
1415
"code.gitea.io/gitea/modules/convert"
1516
"code.gitea.io/gitea/modules/git"
16-
"code.gitea.io/gitea/modules/log"
1717
repo_module "code.gitea.io/gitea/modules/repository"
1818
api "code.gitea.io/gitea/modules/structs"
1919
"code.gitea.io/gitea/modules/web"
@@ -117,67 +117,20 @@ func DeleteBranch(ctx *context.APIContext) {
117117

118118
branchName := ctx.Params("*")
119119

120-
if ctx.Repo.Repository.DefaultBranch == branchName {
121-
ctx.Error(http.StatusForbidden, "DefaultBranch", fmt.Errorf("can not delete default branch"))
122-
return
123-
}
124-
125-
isProtected, err := ctx.Repo.Repository.IsProtectedBranch(branchName, ctx.User)
126-
if err != nil {
127-
ctx.InternalServerError(err)
128-
return
129-
}
130-
if isProtected {
131-
ctx.Error(http.StatusForbidden, "IsProtectedBranch", fmt.Errorf("branch protected"))
132-
return
133-
}
134-
135-
branch, err := repo_module.GetBranch(ctx.Repo.Repository, branchName)
136-
if err != nil {
137-
if git.IsErrBranchNotExist(err) {
120+
if err := repo_service.DeleteBranch(ctx.User, ctx.Repo.Repository, ctx.Repo.GitRepo, branchName); err != nil {
121+
switch {
122+
case git.IsErrBranchNotExist(err):
138123
ctx.NotFound(err)
139-
} else {
140-
ctx.Error(http.StatusInternalServerError, "GetBranch", err)
124+
case errors.Is(err, repo_service.ErrBranchIsDefault):
125+
ctx.Error(http.StatusForbidden, "DefaultBranch", fmt.Errorf("can not delete default branch"))
126+
case errors.Is(err, repo_service.ErrBranchIsProtected):
127+
ctx.Error(http.StatusForbidden, "IsProtectedBranch", fmt.Errorf("branch protected"))
128+
default:
129+
ctx.Error(http.StatusInternalServerError, "DeleteBranch", err)
141130
}
142131
return
143132
}
144133

145-
c, err := branch.GetCommit()
146-
if err != nil {
147-
ctx.Error(http.StatusInternalServerError, "GetCommit", err)
148-
return
149-
}
150-
151-
if err := ctx.Repo.GitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{
152-
Force: true,
153-
}); err != nil {
154-
ctx.Error(http.StatusInternalServerError, "DeleteBranch", err)
155-
return
156-
}
157-
158-
if err := pull_service.CloseBranchPulls(ctx.User, ctx.Repo.Repository.ID, branchName); err != nil {
159-
log.Error("CloseBranchPulls: %v", err)
160-
return
161-
}
162-
163-
// Don't return error below this
164-
if err := repo_service.PushUpdate(
165-
&repo_module.PushUpdateOptions{
166-
RefFullName: git.BranchPrefix + branchName,
167-
OldCommitID: c.ID.String(),
168-
NewCommitID: git.EmptySHA,
169-
PusherID: ctx.User.ID,
170-
PusherName: ctx.User.Name,
171-
RepoUserName: ctx.Repo.Owner.Name,
172-
RepoName: ctx.Repo.Repository.Name,
173-
}); err != nil {
174-
log.Error("Update: %v", err)
175-
}
176-
177-
if err := ctx.Repo.Repository.AddDeletedBranch(branchName, c.ID.String(), ctx.User.ID); err != nil {
178-
log.Warn("AddDeletedBranch: %v", err)
179-
}
180-
181134
ctx.Status(http.StatusNoContent)
182135
}
183136

routers/repo/branch.go

Lines changed: 16 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package repo
77

88
import (
9+
"errors"
910
"fmt"
1011
"net/http"
1112
"strings"
@@ -21,7 +22,6 @@ import (
2122
"code.gitea.io/gitea/modules/web"
2223
"code.gitea.io/gitea/routers/utils"
2324
"code.gitea.io/gitea/services/forms"
24-
pull_service "code.gitea.io/gitea/services/pull"
2525
release_service "code.gitea.io/gitea/services/release"
2626
repo_service "code.gitea.io/gitea/services/repository"
2727
)
@@ -84,34 +84,23 @@ func Branches(ctx *context.Context) {
8484
func DeleteBranchPost(ctx *context.Context) {
8585
defer redirect(ctx)
8686
branchName := ctx.Query("name")
87-
if branchName == ctx.Repo.Repository.DefaultBranch {
88-
log.Debug("DeleteBranch: Can't delete default branch '%s'", branchName)
89-
ctx.Flash.Error(ctx.Tr("repo.branch.default_deletion_failed", branchName))
90-
return
91-
}
92-
93-
isProtected, err := ctx.Repo.Repository.IsProtectedBranch(branchName, ctx.User)
94-
if err != nil {
95-
log.Error("DeleteBranch: %v", err)
96-
ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", branchName))
97-
return
98-
}
99-
100-
if isProtected {
101-
log.Debug("DeleteBranch: Can't delete protected branch '%s'", branchName)
102-
ctx.Flash.Error(ctx.Tr("repo.branch.protected_deletion_failed", branchName))
103-
return
104-
}
10587

106-
if !ctx.Repo.GitRepo.IsBranchExist(branchName) {
107-
log.Debug("DeleteBranch: Can't delete non existing branch '%s'", branchName)
108-
ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", branchName))
109-
return
110-
}
88+
if err := repo_service.DeleteBranch(ctx.User, ctx.Repo.Repository, ctx.Repo.GitRepo, branchName); err != nil {
89+
switch {
90+
case git.IsErrBranchNotExist(err):
91+
log.Debug("DeleteBranch: Can't delete non existing branch '%s'", branchName)
92+
ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", branchName))
93+
case errors.Is(err, repo_service.ErrBranchIsDefault):
94+
log.Debug("DeleteBranch: Can't delete default branch '%s'", branchName)
95+
ctx.Flash.Error(ctx.Tr("repo.branch.default_deletion_failed", branchName))
96+
case errors.Is(err, repo_service.ErrBranchIsProtected):
97+
log.Debug("DeleteBranch: Can't delete protected branch '%s'", branchName)
98+
ctx.Flash.Error(ctx.Tr("repo.branch.protected_deletion_failed", branchName))
99+
default:
100+
log.Error("DeleteBranch: %v", err)
101+
ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", branchName))
102+
}
111103

112-
if err := deleteBranch(ctx, branchName); err != nil {
113-
log.Error("DeleteBranch: %v", err)
114-
ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", branchName))
115104
return
116105
}
117106

@@ -170,46 +159,6 @@ func redirect(ctx *context.Context) {
170159
})
171160
}
172161

173-
func deleteBranch(ctx *context.Context, branchName string) error {
174-
commit, err := ctx.Repo.GitRepo.GetBranchCommit(branchName)
175-
if err != nil {
176-
log.Error("GetBranchCommit: %v", err)
177-
return err
178-
}
179-
180-
if err := ctx.Repo.GitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{
181-
Force: true,
182-
}); err != nil {
183-
log.Error("DeleteBranch: %v", err)
184-
return err
185-
}
186-
187-
if err := pull_service.CloseBranchPulls(ctx.User, ctx.Repo.Repository.ID, branchName); err != nil {
188-
log.Error("CloseBranchPulls: %v", err)
189-
return err
190-
}
191-
192-
// Don't return error below this
193-
if err := repo_service.PushUpdate(
194-
&repo_module.PushUpdateOptions{
195-
RefFullName: git.BranchPrefix + branchName,
196-
OldCommitID: commit.ID.String(),
197-
NewCommitID: git.EmptySHA,
198-
PusherID: ctx.User.ID,
199-
PusherName: ctx.User.Name,
200-
RepoUserName: ctx.Repo.Owner.Name,
201-
RepoName: ctx.Repo.Repository.Name,
202-
}); err != nil {
203-
log.Error("Update: %v", err)
204-
}
205-
206-
if err := ctx.Repo.Repository.AddDeletedBranch(branchName, commit.ID.String(), ctx.User.ID); err != nil {
207-
log.Warn("AddDeletedBranch: %v", err)
208-
}
209-
210-
return nil
211-
}
212-
213162
// loadBranches loads branches from the repository limited by page & pageSize.
214163
// NOTE: May write to context on error.
215164
func loadBranches(ctx *context.Context, skip, limit int) ([]*Branch, int) {

routers/repo/pull.go

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package repo
99
import (
1010
"container/list"
1111
"crypto/subtle"
12+
"errors"
1213
"fmt"
1314
"net/http"
1415
"path"
@@ -21,7 +22,6 @@ import (
2122
"code.gitea.io/gitea/modules/git"
2223
"code.gitea.io/gitea/modules/log"
2324
"code.gitea.io/gitea/modules/notification"
24-
repo_module "code.gitea.io/gitea/modules/repository"
2525
"code.gitea.io/gitea/modules/setting"
2626
"code.gitea.io/gitea/modules/structs"
2727
"code.gitea.io/gitea/modules/upload"
@@ -1186,20 +1186,6 @@ func CleanUpPullRequest(ctx *context.Context) {
11861186
})
11871187
}()
11881188

1189-
if pr.HeadBranch == pr.HeadRepo.DefaultBranch || !gitRepo.IsBranchExist(pr.HeadBranch) {
1190-
ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
1191-
return
1192-
}
1193-
1194-
// Check if branch is not protected
1195-
if protected, err := pr.HeadRepo.IsProtectedBranch(pr.HeadBranch, ctx.User); err != nil || protected {
1196-
if err != nil {
1197-
log.Error("HeadRepo.IsProtectedBranch: %v", err)
1198-
}
1199-
ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
1200-
return
1201-
}
1202-
12031189
// Check if branch has no new commits
12041190
headCommitID, err := gitBaseRepo.GetRefCommitID(pr.GetGitRefName())
12051191
if err != nil {
@@ -1218,33 +1204,21 @@ func CleanUpPullRequest(ctx *context.Context) {
12181204
return
12191205
}
12201206

1221-
if err := gitRepo.DeleteBranch(pr.HeadBranch, git.DeleteBranchOptions{
1222-
Force: true,
1223-
}); err != nil {
1224-
log.Error("DeleteBranch: %v", err)
1225-
ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
1226-
return
1227-
}
1228-
1229-
if err := pull_service.CloseBranchPulls(ctx.User, pr.HeadRepo.ID, pr.HeadBranch); err != nil {
1230-
log.Error("CloseBranchPulls: %v", err)
1231-
ctx.Flash.Error(ctx.Tr("repo.branch.delete_branch_close_prs_failed", fullBranchName))
1207+
if err := repo_service.DeleteBranch(ctx.User, pr.HeadRepo, gitRepo, pr.HeadBranch); err != nil {
1208+
switch {
1209+
case git.IsErrBranchNotExist(err):
1210+
ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
1211+
case errors.Is(err, repo_service.ErrBranchIsDefault):
1212+
ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
1213+
case errors.Is(err, repo_service.ErrBranchIsProtected):
1214+
ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
1215+
default:
1216+
log.Error("DeleteBranch: %v", err)
1217+
ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
1218+
}
12321219
return
12331220
}
12341221

1235-
if err := repo_service.PushUpdate(
1236-
&repo_module.PushUpdateOptions{
1237-
RefFullName: git.BranchPrefix + pr.HeadBranch,
1238-
OldCommitID: branchCommitID,
1239-
NewCommitID: git.EmptySHA,
1240-
PusherID: ctx.User.ID,
1241-
PusherName: ctx.User.Name,
1242-
RepoUserName: pr.HeadRepo.Owner.Name,
1243-
RepoName: pr.HeadRepo.Name,
1244-
}); err != nil {
1245-
log.Error("Update: %v", err)
1246-
}
1247-
12481222
if err := models.AddDeletePRBranchComment(ctx.User, pr.BaseRepo, issue.ID, pr.HeadBranch); err != nil {
12491223
// Do not fail here as branch has already been deleted
12501224
log.Error("DeleteBranch: %v", err)

0 commit comments

Comments
 (0)