@@ -268,30 +268,34 @@ func createCodeComment(ctx context.Context, doer *user_model.User, repo *repo_mo
268268 return nil , fmt .Errorf ("GetPatch failed: %w" , err )
269269 }
270270
271- lineCommitID := util .Iif (line < 0 , beforeCommitID , afterCommitID )
272271 return db .WithTx2 (ctx , func (ctx context.Context ) (* issues_model.Comment , error ) {
273272 comment , err := issues_model .CreateComment (ctx , & issues_model.CreateCommentOptions {
274- Type : issues_model .CommentTypeCode ,
275- Doer : doer ,
276- Repo : repo ,
277- Issue : issue ,
278- Content : content ,
279- LineNum : line ,
280- TreePath : treePath ,
281- CommitSHA : lineCommitID ,
282- ReviewID : reviewID ,
283- Patch : patch ,
284- Invalidated : false ,
285- Attachments : attachments ,
273+ Type : issues_model .CommentTypeCode ,
274+ Doer : doer ,
275+ Repo : repo ,
276+ Issue : issue ,
277+ Content : content ,
278+ LineNum : line ,
279+ TreePath : treePath ,
280+ BeforeCommitID : beforeCommitID ,
281+ CommitSHA : afterCommitID ,
282+ ReviewID : reviewID ,
283+ Patch : patch ,
284+ Invalidated : false ,
285+ Attachments : attachments ,
286286 })
287287 if err != nil {
288288 return nil , err
289289 }
290290
291291 // The line commit ID Must be referenced in the git repository, because the branch maybe rebased or force-pushed.
292292 // If the review commit is GC, the position can not be calculated dynamically.
293- if err := git .UpdateRef (ctx , pr .BaseRepo .RepoPath (), issues_model .GetCodeCommentRefName (pr .Index , comment .ID ), lineCommitID ); err != nil {
294- log .Error ("Unable to update ref in base repository for PR[%d] Error: %v" , pr .ID , err )
293+ if err := git .UpdateRef (ctx , pr .BaseRepo .RepoPath (), issues_model .GetCodeCommentRefName (pr .Index , comment .ID , "before" ), beforeCommitID ); err != nil {
294+ log .Error ("Unable to update ref before_commitid in base repository for PR[%d] Error: %v" , pr .ID , err )
295+ return nil , err
296+ }
297+ if err := git .UpdateRef (ctx , pr .BaseRepo .RepoPath (), issues_model .GetCodeCommentRefName (pr .Index , comment .ID , "after" ), afterCommitID ); err != nil {
298+ log .Error ("Unable to update ref after_commitid in base repository for PR[%d] Error: %v" , pr .ID , err )
295299 return nil , err
296300 }
297301
@@ -484,6 +488,7 @@ func DismissReview(ctx context.Context, reviewID, repoID int64, message string,
484488}
485489
486490// ReCalculateLineNumber recalculates the line number based on the hunks of the diff.
491+ // left side is the commit the comment was created on, right side is the commit the comment is displayed on.
487492// If the returned line number is zero, it should not be displayed.
488493func ReCalculateLineNumber (hunks []* git.HunkInfo , leftLine int64 ) int64 {
489494 if len (hunks ) == 0 || leftLine == 0 {
@@ -498,15 +503,16 @@ func ReCalculateLineNumber(hunks []*git.HunkInfo, leftLine int64) int64 {
498503 newLine := absLine
499504
500505 for _ , hunk := range hunks {
501- if hunk .LeftLine + hunk .LeftHunk <= absLine {
502- newLine += hunk .RightHunk - hunk .LeftHunk
503- } else if hunk .LeftLine <= absLine && absLine < hunk .LeftLine + hunk .LeftHunk {
504- // The line has been removed, so it should not be displayed
505- return 0
506- } else if absLine < hunk .LeftLine {
506+ if absLine < hunk .LeftLine {
507507 // The line is before the hunk, so we can ignore it
508508 continue
509+ } else if hunk .LeftLine <= absLine && absLine < hunk .LeftLine + hunk .LeftHunk {
510+ // The line is within the hunk, that means the line is deleted from the current commit
511+ // So that we don't need to display this line
512+ return 0
509513 }
514+ // The line is after the hunk, so we can add the right hunk size
515+ newLine += hunk .RightHunk - hunk .LeftHunk
510516 }
511517 return util .Iif (isLeft , - newLine , newLine )
512518}
@@ -614,27 +620,35 @@ func LoadCodeComments(ctx context.Context, gitRepo *git.Repository, repo *repo_m
614620 }
615621
616622 dstCommitID := beforeCommit .ID .String ()
623+ commentCommitID := comment .BeforeCommitID
617624 if comment .Line > 0 {
618625 dstCommitID = afterCommit .ID .String ()
626+ commentCommitID = comment .CommitSHA
619627 }
620628
621- if comment .CommitSHA == dstCommitID {
622- lineComments [comment .Line ] = append (lineComments [comment .Line ], comment )
623- continue
629+ if commentCommitID != dstCommitID {
630+ // If the comment is not for the current commit, we need to recalculate the line number
631+ hunks , ok := hunksCache [commentCommitID + ".." + dstCommitID ]
632+ if ! ok {
633+ hunks , err = git .GetAffectedHunksForTwoCommitsSpecialFile (ctx , repo .RepoPath (), commentCommitID , dstCommitID , file .Name )
634+ if err != nil {
635+ return fmt .Errorf ("GetAffectedHunksForTwoCommitsSpecialFile[%s, %s, %s]: %w" , repo .FullName (), commentCommitID , dstCommitID , err )
636+ }
637+ hunksCache [commentCommitID + ".." + dstCommitID ] = hunks
638+ }
639+ comment .Line = ReCalculateLineNumber (hunks , comment .Line )
624640 }
625641
626- // If the comment is not for the current commit, we need to recalculate the line number
627- hunks , ok := hunksCache [comment .CommitSHA + ".." + dstCommitID ]
628- if ! ok {
629- hunks , err = git .GetAffectedHunksForTwoCommitsSpecialFile (ctx , repo .RepoPath (), comment .CommitSHA , dstCommitID , file .Name )
642+ if comment .Line != 0 {
643+ commentAfterCommit , err := gitRepo .GetCommit (comment .CommitSHA )
630644 if err != nil {
631- return fmt .Errorf ("GetAffectedHunksForTwoCommitsSpecialFile[%s, %s, %s]: %w" , repo .FullName (), dstCommitID , comment .CommitSHA , err )
645+ return fmt .Errorf ("GetCommit[%s]: %w" , comment .CommitSHA , err )
646+ }
647+ // If the comment is not the first one or the comment created before the current commit
648+ if lineComments [comment .Line ] != nil || comment .CommitSHA == afterCommit .ID .String () ||
649+ commentAfterCommit .Committer .When .Before (afterCommit .Committer .When ) {
650+ lineComments [comment .Line ] = append (lineComments [comment .Line ], comment )
632651 }
633- hunksCache [comment .CommitSHA + ".." + dstCommitID ] = hunks
634- }
635- comment .Line = ReCalculateLineNumber (hunks , comment .Line )
636- if comment .Line != 0 {
637- lineComments [comment .Line ] = append (lineComments [comment .Line ], comment )
638652 }
639653 }
640654
0 commit comments