|
6 | 6 | package repo
|
7 | 7 |
|
8 | 8 | import (
|
| 9 | + "errors" |
9 | 10 | "fmt"
|
10 | 11 | "strings"
|
11 | 12 |
|
@@ -82,34 +83,23 @@ func Branches(ctx *context.Context) {
|
82 | 83 | func DeleteBranchPost(ctx *context.Context) {
|
83 | 84 | defer redirect(ctx)
|
84 | 85 | branchName := ctx.Query("name")
|
85 |
| - if branchName == ctx.Repo.Repository.DefaultBranch { |
86 |
| - log.Debug("DeleteBranch: Can't delete default branch '%s'", branchName) |
87 |
| - ctx.Flash.Error(ctx.Tr("repo.branch.default_deletion_failed", branchName)) |
88 |
| - return |
89 |
| - } |
90 |
| - |
91 |
| - isProtected, err := ctx.Repo.Repository.IsProtectedBranch(branchName, ctx.User) |
92 |
| - if err != nil { |
93 |
| - log.Error("DeleteBranch: %v", err) |
94 |
| - ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", branchName)) |
95 |
| - return |
96 |
| - } |
97 |
| - |
98 |
| - if isProtected { |
99 |
| - log.Debug("DeleteBranch: Can't delete protected branch '%s'", branchName) |
100 |
| - ctx.Flash.Error(ctx.Tr("repo.branch.protected_deletion_failed", branchName)) |
101 |
| - return |
102 |
| - } |
103 | 86 |
|
104 |
| - if !ctx.Repo.GitRepo.IsBranchExist(branchName) { |
105 |
| - log.Debug("DeleteBranch: Can't delete non existing branch '%s'", branchName) |
106 |
| - ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", branchName)) |
107 |
| - return |
108 |
| - } |
| 87 | + if err := repo_service.DeleteBranch(ctx.User, ctx.Repo.Repository, ctx.Repo.GitRepo, branchName); err != nil { |
| 88 | + switch { |
| 89 | + case git.IsErrBranchNotExist(err): |
| 90 | + log.Debug("DeleteBranch: Can't delete non existing branch '%s'", branchName) |
| 91 | + ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", branchName)) |
| 92 | + case errors.Is(err, repo_service.ErrBranchIsDefault): |
| 93 | + log.Debug("DeleteBranch: Can't delete default branch '%s'", branchName) |
| 94 | + ctx.Flash.Error(ctx.Tr("repo.branch.default_deletion_failed", branchName)) |
| 95 | + case errors.Is(err, repo_service.ErrBranchIsProtected): |
| 96 | + log.Debug("DeleteBranch: Can't delete protected branch '%s'", branchName) |
| 97 | + ctx.Flash.Error(ctx.Tr("repo.branch.protected_deletion_failed", branchName)) |
| 98 | + default: |
| 99 | + log.Error("DeleteBranch: %v", err) |
| 100 | + ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", branchName)) |
| 101 | + } |
109 | 102 |
|
110 |
| - if err := deleteBranch(ctx, branchName); err != nil { |
111 |
| - log.Error("DeleteBranch: %v", err) |
112 |
| - ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", branchName)) |
113 | 103 | return
|
114 | 104 | }
|
115 | 105 |
|
@@ -168,41 +158,6 @@ func redirect(ctx *context.Context) {
|
168 | 158 | })
|
169 | 159 | }
|
170 | 160 |
|
171 |
| -func deleteBranch(ctx *context.Context, branchName string) error { |
172 |
| - commit, err := ctx.Repo.GitRepo.GetBranchCommit(branchName) |
173 |
| - if err != nil { |
174 |
| - log.Error("GetBranchCommit: %v", err) |
175 |
| - return err |
176 |
| - } |
177 |
| - |
178 |
| - if err := ctx.Repo.GitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{ |
179 |
| - Force: true, |
180 |
| - }); err != nil { |
181 |
| - log.Error("DeleteBranch: %v", err) |
182 |
| - return err |
183 |
| - } |
184 |
| - |
185 |
| - // Don't return error below this |
186 |
| - if err := repo_service.PushUpdate( |
187 |
| - &repo_module.PushUpdateOptions{ |
188 |
| - RefFullName: git.BranchPrefix + branchName, |
189 |
| - OldCommitID: commit.ID.String(), |
190 |
| - NewCommitID: git.EmptySHA, |
191 |
| - PusherID: ctx.User.ID, |
192 |
| - PusherName: ctx.User.Name, |
193 |
| - RepoUserName: ctx.Repo.Owner.Name, |
194 |
| - RepoName: ctx.Repo.Repository.Name, |
195 |
| - }); err != nil { |
196 |
| - log.Error("Update: %v", err) |
197 |
| - } |
198 |
| - |
199 |
| - if err := ctx.Repo.Repository.AddDeletedBranch(branchName, commit.ID.String(), ctx.User.ID); err != nil { |
200 |
| - log.Warn("AddDeletedBranch: %v", err) |
201 |
| - } |
202 |
| - |
203 |
| - return nil |
204 |
| -} |
205 |
| - |
206 | 161 | // loadBranches loads branches from the repository limited by page & pageSize.
|
207 | 162 | // NOTE: May write to context on error.
|
208 | 163 | func loadBranches(ctx *context.Context, skip, limit int) ([]*Branch, int) {
|
|
0 commit comments