Skip to content

Commit 93364b9

Browse files
committed
Don't list as merged branches not at mergecommitid
Once a branch has been merged if the commit ID no longer equals that of the merge commit id don't offer to delete the branch on the pull screen and don't list it as merged on branches. Fix go-gitea#9201
1 parent 133ae18 commit 93364b9

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

routers/repo/issue.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
comment_service "code.gitea.io/gitea/services/comments"
3030
issue_service "code.gitea.io/gitea/services/issue"
3131
pull_service "code.gitea.io/gitea/services/pull"
32+
"gopkg.in/src-d/go-git.v4/plumbing"
3233

3334
"github.com/unknwon/com"
3435
)
@@ -966,6 +967,24 @@ func ViewIssue(ctx *context.Context) {
966967
ctx.Data["IsBlockedByRejection"] = pull.ProtectedBranch.MergeBlockedByRejectedReview(pull)
967968
ctx.Data["GrantedApprovals"] = cnt
968969
}
970+
if pull.HasMerged && pull.HeadRepo != nil {
971+
// && git.IsBranchExist(pull.HeadRepo.RepoPath(), pull.HeadBranch) {
972+
headRepo, err := git.OpenRepository(pull.HeadRepo.RepoPath())
973+
if err != nil {
974+
ctx.ServerError("OpenRepository", err)
975+
return
976+
}
977+
defer headRepo.Close()
978+
commit, err := headRepo.GetBranchCommitID(pull.HeadBranch)
979+
if err != nil && err != plumbing.ErrReferenceNotFound {
980+
ctx.ServerError("GetBranchCommitID", err)
981+
return
982+
} else if err == nil && commit != pull.MergedCommitID {
983+
// the head has moved on from the merge - we shouldn't delete
984+
canDelete = false
985+
}
986+
987+
}
969988
ctx.Data["IsPullBranchDeletable"] = canDelete && pull.HeadRepo != nil && git.IsBranchExist(pull.HeadRepo.RepoPath(), pull.HeadBranch)
970989

971990
ctx.Data["PullReviewers"], err = models.GetReviewersByIssueID(issue.ID)

templates/repo/branch/list.tmpl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@
8484
<button id="new-pull-request" class="ui compact basic button">{{$.i18n.Tr "repo.pulls.compare_changes"}}</button>
8585
</a>
8686
{{end}}
87+
{{else if and .LatestPullRequest.HasMerged (ne .LatestPullRequest.MergedCommitID .Commit.ID.String)}}
88+
{{if and (not .IsDeleted) $.AllowsPulls (gt .CommitsAhead 0)}}
89+
<a href="{{$.RepoLink}}/compare/{{$.DefaultBranch | EscapePound}}...{{if ne $.Repository.Owner.Name $.Owner.Name}}{{$.Owner.Name}}:{{end}}{{.Name | EscapePound}}">
90+
<button id="new-pull-request" class="ui compact basic button">{{$.i18n.Tr "repo.pulls.compare_changes"}}</button>
91+
</a>
92+
{{end}}
8793
{{else}}
8894
<a href="{{$.RepoLink}}/pulls/{{.LatestPullRequest.Issue.Index}}">#{{.LatestPullRequest.Issue.Index}}</a>
8995
{{if .LatestPullRequest.HasMerged}}

0 commit comments

Comments
 (0)