Skip to content

Commit a5444f8

Browse files
committed
Remove unecessary commit comments when force push
1 parent 5cc2686 commit a5444f8

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

models/action.go

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,7 @@ func CommitRepoAction(opts CommitRepoActionOptions) error {
573573
}
574574
}
575575

576+
var commentCommits = opts.Commits.Commits
576577
if len(opts.Commits.Commits) > setting.UI.FeedMaxCommitNum {
577578
opts.Commits.Commits = opts.Commits.Commits[:setting.UI.FeedMaxCommitNum]
578579
}
@@ -653,7 +654,7 @@ func CommitRepoAction(opts CommitRepoActionOptions) error {
653654
}
654655

655656
issues := make([]*Issue, 0, len(prs))
656-
lastCommitID := opts.Commits.Commits[len(opts.Commits.Commits)-1].Sha1
657+
lastCommitID := commentCommits[len(commentCommits)-1].Sha1
657658
for _, pr := range prs {
658659
pr.LastCommitID = lastCommitID
659660
if err := pr.UpdateCols("last_commit_id"); err != nil {
@@ -668,15 +669,43 @@ func CommitRepoAction(opts CommitRepoActionOptions) error {
668669
return fmt.Errorf("IssueList.LoadRepositories: %v", err)
669670
}
670671

672+
var commitIDCache = make(map[string]string)
673+
var ok bool
671674
for _, issue := range issues {
675+
var commitID string
672676
if isForcePush {
673677
if err := ClearPullPushComment(issue); err != nil {
674678
return fmt.Errorf("ClearPullPushComent: %v", err)
675679
}
680+
681+
var key = fmt.Sprintf("%s/%s", issue.Repo.RepoPath(), issue.PullRequest.BaseBranch)
682+
if commitID, ok = commitIDCache[key]; !ok {
683+
gitRepo, err := git.OpenRepository(issue.Repo.RepoPath())
684+
if err != nil {
685+
return fmt.Errorf("OpenRepository: %v", err)
686+
}
687+
commit, err := gitRepo.GetBranchCommit(issue.PullRequest.BaseBranch)
688+
if err != nil {
689+
return fmt.Errorf("GetBranchCommit: %v", err)
690+
}
691+
commitID = commit.ID.String()
692+
commitIDCache[key] = commitID
693+
}
676694
}
677695

678-
for i := len(opts.Commits.Commits) - 1; i >= 0; i-- {
679-
if err := CreatePullPushComment(pusher, issue.Repo, issue, opts.Commits.Commits[i]); err != nil {
696+
var findStart = false
697+
for i := len(commentCommits) - 1; i >= 0; i-- {
698+
if isForcePush {
699+
if commitID == commentCommits[i].Sha1 {
700+
findStart = true
701+
continue
702+
}
703+
if !findStart {
704+
continue
705+
}
706+
}
707+
708+
if err := CreatePullPushComment(pusher, issue.Repo, issue, commentCommits[i]); err != nil {
680709
return fmt.Errorf("CreatePullPushComment: %v", err)
681710
}
682711
}

0 commit comments

Comments
 (0)