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