Skip to content

Commit ca4f5c3

Browse files
authored
Fix branch deletion for squash or rebase merged pull requests (#3425)
1 parent 5911f98 commit ca4f5c3

File tree

1 file changed

+15
-31
lines changed

1 file changed

+15
-31
lines changed

routers/repo/pull.go

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -957,37 +957,21 @@ func CleanUpPullRequest(ctx *context.Context) {
957957
}
958958

959959
// Check if branch has no new commits
960-
if len(pr.MergedCommitID) > 0 {
961-
branchCommitID, err := gitRepo.GetBranchCommitID(pr.HeadBranch)
962-
if err != nil {
963-
log.Error(4, "GetBranchCommitID: %v", err)
964-
ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
965-
return
966-
}
967-
968-
commit, err := gitBaseRepo.GetCommit(pr.MergedCommitID)
969-
if err != nil {
970-
log.Error(4, "GetCommit: %v", err)
971-
ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
972-
return
973-
}
974-
975-
isParent := false
976-
for i := 0; i < commit.ParentCount(); i++ {
977-
if parent, err := commit.Parent(i); err != nil {
978-
log.Error(4, "Parent: %v", err)
979-
ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
980-
return
981-
} else if parent.ID.String() == branchCommitID {
982-
isParent = true
983-
break
984-
}
985-
}
986-
987-
if !isParent {
988-
ctx.Flash.Error(ctx.Tr("repo.branch.delete_branch_has_new_commits", fullBranchName))
989-
return
990-
}
960+
headCommitID, err := gitBaseRepo.GetRefCommitID(pr.GetGitRefName())
961+
if err != nil {
962+
log.Error(4, "GetRefCommitID: %v", err)
963+
ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
964+
return
965+
}
966+
branchCommitID, err := gitRepo.GetBranchCommitID(pr.HeadBranch)
967+
if err != nil {
968+
log.Error(4, "GetBranchCommitID: %v", err)
969+
ctx.Flash.Error(ctx.Tr("repo.branch.deletion_failed", fullBranchName))
970+
return
971+
}
972+
if headCommitID != branchCommitID {
973+
ctx.Flash.Error(ctx.Tr("repo.branch.delete_branch_has_new_commits", fullBranchName))
974+
return
991975
}
992976

993977
if err := gitRepo.DeleteBranch(pr.HeadBranch, git.DeleteBranchOptions{

0 commit comments

Comments
 (0)