Skip to content

Commit 71dee6b

Browse files
Bwkobkcsoft
authored andcommitted
Improve the way how branches are deleted
Delete branch from HeadRepo instead of BaseRepo Prevent the deletion of a master branch Show a yes/no overlay when you press the delete branch button
1 parent 7163445 commit 71dee6b

File tree

4 files changed

+73
-16
lines changed

4 files changed

+73
-16
lines changed

options/locale/locale_en-US.ini

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,6 @@ pulls.cannot_auto_merge_desc = This pull request can't be merged automatically b
591591
pulls.cannot_auto_merge_helper = Please merge manually in order to resolve the conflicts.
592592
pulls.merge_pull_request = Merge Pull Request
593593
pulls.open_unmerged_pull_exists = `You can't perform reopen operation because there is already an open pull request (#%d) from same repository with same merge information and is waiting for merging.`
594-
pulls.delete_branch = Delete Branch
595594
596595
milestones.new = New Milestone
597596
milestones.open_tab = %d Open
@@ -815,6 +814,14 @@ release.tag_name_already_exist = Release with this tag name already exists.
815814
release.tag_name_invalid = Tag name is not valid.
816815
release.downloads = Downloads
817816

817+
branch.delete = Delete Branch %s
818+
branch.delete_desc = Once you delete a branch, there is no going back. Please be certain.
819+
branch.delete_notices_1 = - This operation <strong>CANNOT</strong> be undone.
820+
branch.delete_notices_2 = - This operation will permanently delete everything of branch %s.
821+
branch.deletion_success = %s has been deleted successfully!
822+
branch.deletion_failed = Failed to delete branch %s.
823+
branch.delete_branch_has_new_commits = %s cannot be deleted because it has new commits after mergence.
824+
818825
[org]
819826
org_name_holder = Organization Name
820827
org_full_name_holder = Organization Full Name

routers/repo/branch.go

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"code.gitea.io/git"
99
"code.gitea.io/gitea/modules/base"
1010
"code.gitea.io/gitea/modules/context"
11+
"code.gitea.io/gitea/modules/log"
1112
)
1213

1314
const (
@@ -35,17 +36,46 @@ func Branches(ctx *context.Context) {
3536
// DeleteBranchPost responses for delete merged branch
3637
func DeleteBranchPost(ctx *context.Context) {
3738
branchName := ctx.Params(":name")
39+
commitID := ctx.Query("commit")
40+
41+
defer func() {
42+
redirectTo := ctx.Query("redirect_to")
43+
if len(redirectTo) == 0 {
44+
redirectTo = ctx.Repo.RepoLink
45+
}
46+
47+
ctx.JSON(200, map[string]interface{}{
48+
"redirect": redirectTo,
49+
})
50+
}()
51+
52+
fullBranchName := ctx.Repo.Owner.Name + "/" + branchName
53+
54+
if !ctx.Repo.GitRepo.IsBranchExist(branchName) || branchName == "master" {
55+
ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
56+
return
57+
}
58+
59+
if len(commitID) > 0 {
60+
branchCommitID, err := ctx.Repo.GitRepo.GetBranchCommitID(branchName)
61+
if err != nil {
62+
log.Error(4, "GetBranchCommitID: %v", err)
63+
return
64+
}
65+
66+
if branchCommitID != commitID {
67+
ctx.Flash.Error(ctx.Tr("repo.branch.delete_branch_has_new_commits", fullBranchName))
68+
return
69+
}
70+
}
3871

3972
if err := ctx.Repo.GitRepo.DeleteBranch(branchName, git.DeleteBranchOptions{
4073
Force: false,
4174
}); err != nil {
42-
ctx.Handle(500, "DeleteBranch", err)
75+
log.Error(4, "DeleteBranch: %v", err)
76+
ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
4377
return
4478
}
4579

46-
redirectTo := ctx.Query("redirect_to")
47-
if len(redirectTo) == 0 {
48-
redirectTo = ctx.Repo.RepoLink
49-
}
50-
ctx.Redirect(redirectTo)
80+
ctx.Flash.Success(ctx.Tr("repo.branch.deletion_success", fullBranchName))
5181
}

routers/repo/issue.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/Unknwon/com"
1818
"github.com/Unknwon/paginater"
1919

20+
"code.gitea.io/git"
2021
"code.gitea.io/gitea/models"
2122
"code.gitea.io/gitea/modules/auth"
2223
"code.gitea.io/gitea/modules/base"
@@ -182,7 +183,6 @@ func Issues(ctx *context.Context) {
182183
pager := paginater.New(total, setting.UI.IssuePagingNum, page, 5)
183184
ctx.Data["Page"] = pager
184185

185-
186186
var issues []*models.Issue
187187
if forceEmpty {
188188
issues = []*models.Issue{}
@@ -663,11 +663,21 @@ func ViewIssue(ctx *context.Context) {
663663

664664
if issue.IsPull {
665665
pull := issue.PullRequest
666-
ctx.Data["IsPullBranchDeletable"] = ctx.Repo.IsWriter() && ctx.Repo.GitRepo.IsBranchExist(pull.HeadBranch)
666+
canDelete := false
667+
668+
if ctx.IsSigned && pull.HeadBranch != "master" {
669+
670+
if err := pull.GetHeadRepo(); err != nil {
671+
log.Error(4, "GetHeadRepo: %v", err)
672+
} else if ctx.User.IsWriterOfRepo(pull.HeadRepo) {
673+
canDelete = true
674+
deleteBranchURL := pull.HeadRepo.Link() + "/branches/" + pull.HeadBranch + "/delete"
675+
ctx.Data["DeleteBranchLink"] = fmt.Sprintf("%s?commit=%s&redirect_to=%s", deleteBranchURL, pull.MergedCommitID, ctx.Data["Link"])
676+
677+
}
678+
}
667679

668-
deleteBranchURL := ctx.Repo.RepoLink + "/branches/" + pull.HeadBranch + "/delete"
669-
queryParams := "?redirect_to=" + ctx.Data["Link"].(string)
670-
ctx.Data["DeleteBranchLink"] = deleteBranchURL + queryParams
680+
ctx.Data["IsPullBranchDeletable"] = canDelete && git.IsBranchExist(pull.HeadRepo.RepoPath(), pull.HeadBranch)
671681
}
672682

673683
ctx.Data["Participants"] = participants

templates/repo/issue/view_content.tmpl

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,7 @@
166166
{{if .IsPullBranchDeletable}}
167167
<div class="ui divider"></div>
168168
<div>
169-
<form class="ui form" action="{{.DeleteBranchLink}}" method="post">
170-
{{.CsrfTokenHtml}}
171-
<button class="ui red button">{{$.i18n.Tr "repo.pulls.delete_branch"}}</button>
172-
</form>
169+
<a class="delete-button ui red button" href="" data-url="{{.DeleteBranchLink}}">{{$.i18n.Tr "repo.branch.delete" .HeadTarget}}</a>
173170
</div>
174171
{{end}}
175172
{{else if .Issue.IsClosed}}
@@ -380,3 +377,16 @@
380377
<div class="hide" id="no-content">
381378
<span class="no-content">{{.i18n.Tr "repo.issues.no_content"}}</span>
382379
</div>
380+
381+
<div class="ui small basic delete modal">
382+
<div class="ui icon header">
383+
<i class="trash icon"></i>
384+
{{.i18n.Tr "repo.branch.delete" .HeadTarget | Safe}}
385+
</div>
386+
<div class="content">
387+
<p>{{.i18n.Tr "repo.branch.delete_desc" | Safe}}</p>
388+
{{.i18n.Tr "repo.branch.delete_notices_1" | Safe}}<br>
389+
{{.i18n.Tr "repo.branch.delete_notices_2" .HeadTarget | Safe}}<br>
390+
</div>
391+
{{template "base/delete_modal_actions" .}}
392+
</div>

0 commit comments

Comments
 (0)