@@ -113,7 +113,12 @@ func CreateCodeComment(ctx context.Context, doer *user_model.User, gitRepo *git.
113113 defer closer .Close ()
114114 }
115115
116- prCommitIDs , err := git .CommitIDsBetween (ctx , gitRepo .Path , beforeCommitID , afterCommitID )
116+ headCommitID , err := gitRepo .GetRefCommitID (issue .PullRequest .GetGitHeadRefName ())
117+ if err != nil {
118+ return nil , fmt .Errorf ("GetRefCommitID[%s]: %w" , issue .PullRequest .GetGitHeadRefName (), err )
119+ }
120+
121+ prCommitIDs , err := git .CommitIDsBetween (ctx , gitRepo .Path , issue .PullRequest .MergeBase , headCommitID )
117122 if err != nil {
118123 return nil , fmt .Errorf ("CommitIDsBetween[%s, %s]: %w" , beforeCommitID , afterCommitID , err )
119124 }
@@ -138,10 +143,7 @@ func CreateCodeComment(ctx context.Context, doer *user_model.User, gitRepo *git.
138143 beforeCommitID = beforeCommit .ID .String ()
139144 }
140145 if afterCommitID == "" {
141- afterCommitID , err = gitRepo .GetRefCommitID (issue .PullRequest .GetGitHeadRefName ())
142- if err != nil {
143- return nil , fmt .Errorf ("GetRefCommitID[%s]: %w" , issue .PullRequest .GetGitHeadRefName (), err )
144- }
146+ afterCommitID = headCommitID
145147 } else if ! slices .Contains (prCommitIDs , afterCommitID ) { // afterCommitID must be one of the pull request commits
146148 return nil , fmt .Errorf ("afterCommitID[%s] is not a valid pull request commit" , afterCommitID )
147149 }
@@ -520,7 +522,7 @@ func ReCalculateLineNumber(hunks []*git.HunkInfo, leftLine int64) int64 {
520522}
521523
522524// FetchCodeCommentsByLine fetches the code comments for a given commit, treePath and line number of a pull request.
523- func FetchCodeCommentsByLine (ctx context.Context , repo * repo_model.Repository , issueID int64 , currentUser * user_model.User , startCommitID , endCommitID , treePath string , line int64 , showOutdatedComments bool ) (issues_model.CommentList , error ) {
525+ func FetchCodeCommentsByLine (ctx context.Context , gitRepo * git. Repository , repo * repo_model.Repository , issueID int64 , currentUser * user_model.User , startCommitID , endCommitID , treePath string , line int64 , showOutdatedComments bool ) (issues_model.CommentList , error ) {
524526 opts := issues_model.FindCommentsOptions {
525527 Type : issues_model .CommentTypeCode ,
526528 IssueID : issueID ,
@@ -538,7 +540,24 @@ func FetchCodeCommentsByLine(ctx context.Context, repo *repo_model.Repository, i
538540 n := 0
539541 hunksCache := make (map [string ][]* git.HunkInfo )
540542 for _ , comment := range comments {
541- if comment .CommitSHA == endCommitID {
543+ // Code comment should always have a commit SHA, if not, we need to set it based on the line number
544+ if comment .CommitSHA == "" {
545+ if comment .Line > 0 {
546+ comment .CommitSHA = endCommitID
547+ } else if comment .Line < 0 {
548+ comment .CommitSHA = startCommitID
549+ } else {
550+ // If the comment has no line number, we cannot display it in the diff view
551+ continue
552+ }
553+ }
554+
555+ dstCommitID := startCommitID
556+ if comment .Line > 0 {
557+ dstCommitID = endCommitID
558+ }
559+
560+ if comment .CommitSHA == dstCommitID {
542561 if comment .Line == line {
543562 comments [n ] = comment
544563 n ++
@@ -547,18 +566,27 @@ func FetchCodeCommentsByLine(ctx context.Context, repo *repo_model.Repository, i
547566 }
548567
549568 // If the comment is not for the current commit, we need to recalculate the line number
550- hunks , ok := hunksCache [comment .CommitSHA ]
569+ hunks , ok := hunksCache [comment .CommitSHA + ".." + dstCommitID ]
551570 if ! ok {
552- hunks , err = git .GetAffectedHunksForTwoCommitsSpecialFile (ctx , repo .RepoPath (), comment .CommitSHA , endCommitID , treePath )
571+ hunks , err = git .GetAffectedHunksForTwoCommitsSpecialFile (ctx , repo .RepoPath (), comment .CommitSHA , dstCommitID , treePath )
553572 if err != nil {
554- return nil , fmt .Errorf ("GetAffectedHunksForTwoCommitsSpecialFile[%s, %s, %s]: %w" , repo .FullName (), comment .CommitSHA , endCommitID , err )
573+ return nil , fmt .Errorf ("GetAffectedHunksForTwoCommitsSpecialFile[%s, %s, %s]: %w" , repo .FullName (), comment .CommitSHA , dstCommitID , err )
555574 }
556- hunksCache [comment .CommitSHA ] = hunks
575+ hunksCache [comment .CommitSHA + ".." + dstCommitID ] = hunks
557576 }
558577
559578 comment .Line = ReCalculateLineNumber (hunks , comment .Line )
560- comments [n ] = comment
561- n ++
579+ if comment .Line != 0 {
580+ dstCommit , err := gitRepo .GetCommit (dstCommitID )
581+ if err != nil {
582+ return nil , fmt .Errorf ("GetCommit[%s]: %w" , dstCommitID , err )
583+ }
584+ // If the comment is not the first one or the comment created before the current commit
585+ if n > 0 || comment .CreatedUnix .AsTime ().Before (dstCommit .Committer .When ) {
586+ comments [n ] = comment
587+ n ++
588+ }
589+ }
562590 }
563591 return comments [:n ], nil
564592}
0 commit comments