@@ -14,6 +14,7 @@ import (
1414 "net/http"
1515 "net/url"
1616 "path/filepath"
17+ "sort"
1718 "strings"
1819
1920 "code.gitea.io/gitea/models/db"
@@ -39,6 +40,7 @@ import (
3940 "code.gitea.io/gitea/services/context"
4041 "code.gitea.io/gitea/services/context/upload"
4142 "code.gitea.io/gitea/services/gitdiff"
43+ user_service "code.gitea.io/gitea/services/user"
4244)
4345
4446const (
@@ -864,6 +866,14 @@ func ExcerptBlob(ctx *context.Context) {
864866 direction := ctx .FormString ("direction" )
865867 filePath := ctx .FormString ("path" )
866868 gitRepo := ctx .Repo .GitRepo
869+ lastRightCommentIdx := ctx .FormInt ("last_left_comment_idx" )
870+ rightCommentIdx := ctx .FormInt ("left_comment_idx" )
871+ fileName := ctx .FormString ("file_name" )
872+
873+ if ctx .FormBool ("pull" ) {
874+ ctx .Data ["PageIsPullFiles" ] = true
875+ }
876+
867877 if ctx .FormBool ("wiki" ) {
868878 var err error
869879 gitRepo , err = gitrepo .OpenWikiRepository (ctx , ctx .Repo .Repository )
@@ -873,6 +883,17 @@ func ExcerptBlob(ctx *context.Context) {
873883 }
874884 defer gitRepo .Close ()
875885 }
886+
887+ issue , err := issues_model .GetIssueByIndex (ctx , ctx .Repo .Repository .ID , int64 (2 ))
888+
889+ if err != nil {
890+ ctx .ServerError ("GetIssueByIndex" , err )
891+ return
892+ }
893+
894+ allComments , err := issues_model .FetchCodeComments (ctx , issue , ctx .Doer , false )
895+ lineCommits := allComments [fileName ]
896+
876897 chunkSize := gitdiff .BlobExcerptChunkSize
877898 commit , err := gitRepo .GetCommit (commitID )
878899 if err != nil {
@@ -882,23 +903,24 @@ func ExcerptBlob(ctx *context.Context) {
882903 section := & gitdiff.DiffSection {
883904 FileName : filePath ,
884905 Name : filePath ,
906+ Lines : []* gitdiff.DiffLine {},
885907 }
886908 if direction == "up" && (idxLeft - lastLeft ) > chunkSize {
887909 idxLeft -= chunkSize
888910 idxRight -= chunkSize
889911 leftHunkSize += chunkSize
890912 rightHunkSize += chunkSize
891- section .Lines , err = getExcerptLines (commit , filePath , idxLeft - 1 , idxRight - 1 , chunkSize )
913+ section .Lines , err = getExcerptLines (commit , filePath , idxLeft - 1 , idxRight - 1 , chunkSize , lastRightCommentIdx , rightCommentIdx )
892914 } else if direction == "down" && (idxLeft - lastLeft ) > chunkSize {
893- section .Lines , err = getExcerptLines (commit , filePath , lastLeft , lastRight , chunkSize )
915+ section .Lines , err = getExcerptLines (commit , filePath , lastLeft , lastRight , chunkSize , lastRightCommentIdx , rightCommentIdx )
894916 lastLeft += chunkSize
895917 lastRight += chunkSize
896918 } else {
897919 offset := - 1
898920 if direction == "down" {
899921 offset = 0
900922 }
901- section .Lines , err = getExcerptLines (commit , filePath , lastLeft , lastRight , idxRight - lastRight + offset )
923+ section .Lines , err = getExcerptLines (commit , filePath , lastLeft , lastRight , idxRight - lastRight + offset , lastRightCommentIdx , rightCommentIdx )
902924 leftHunkSize = 0
903925 rightHunkSize = 0
904926 idxLeft = lastLeft
@@ -918,29 +940,93 @@ func ExcerptBlob(ctx *context.Context) {
918940 Type : gitdiff .DiffLineSection ,
919941 Content : lineText ,
920942 SectionInfo : & gitdiff.DiffLineSectionInfo {
921- Path : filePath ,
922- LastLeftIdx : lastLeft ,
923- LastRightIdx : lastRight ,
924- LeftIdx : idxLeft ,
925- RightIdx : idxRight ,
926- LeftHunkSize : leftHunkSize ,
927- RightHunkSize : rightHunkSize ,
943+ Path : filePath ,
944+ LastLeftIdx : lastLeft ,
945+ LastRightIdx : lastRight ,
946+ LeftIdx : idxLeft ,
947+ RightIdx : idxRight ,
948+ LeftHunkSize : leftHunkSize ,
949+ RightHunkSize : rightHunkSize ,
950+ HasComments : false ,
951+ LastRightCommentIdx : 0 ,
952+ RightCommentIdx : 0 ,
928953 },
954+ Comments : nil ,
929955 }
930956 if direction == "up" {
931957 section .Lines = append ([]* gitdiff.DiffLine {lineSection }, section .Lines ... )
932958 } else if direction == "down" {
933959 section .Lines = append (section .Lines , lineSection )
934960 }
935961 }
962+
963+ for _ , line := range section .Lines {
964+ if line .SectionInfo != nil {
965+ //for now considerign only right side.
966+ start := int64 (line .SectionInfo .LastRightIdx + 1 )
967+ end := int64 (line .SectionInfo .RightIdx - 1 )
968+
969+ //to check section has comments or not.
970+ //1. we can use binary search
971+ //2. we can LastRightCommentIdx, RightCommentIdx, LastLeftCommentIdx, LeftCommentIdx(little complex but fast)
972+ //3. for demo using linear search
973+ for start <= end {
974+ if _ , ok := lineCommits [start ]; ok {
975+ if ! line .SectionInfo .HasComments {
976+ // line.SectionInfo.LastRightCommentIdx = int(start)
977+ // line.SectionInfo.RightCommentIdx = int(start)
978+ line .SectionInfo .HasComments = true
979+ break
980+ }
981+
982+ }
983+ start += 1
984+ }
985+
986+ }
987+ if comments , ok := lineCommits [int64 (line .LeftIdx * - 1 )]; ok {
988+ line .Comments = append (line .Comments , comments ... )
989+ }
990+ if comments , ok := lineCommits [int64 (line .RightIdx )]; ok {
991+ line .Comments = append (line .Comments , comments ... )
992+ }
993+
994+ sort .SliceStable (line .Comments , func (i , j int ) bool {
995+ return line .Comments [i ].CreatedUnix < line .Comments [j ].CreatedUnix
996+ })
997+ }
998+
999+ for _ , line := range section .Lines {
1000+ for _ , comment := range line .Comments {
1001+ if err := comment .LoadAttachments (ctx ); err != nil {
1002+ ctx .ServerError ("LoadAttachments" , err )
1003+ return
1004+ }
1005+ }
1006+ }
1007+
9361008 ctx .Data ["section" ] = section
9371009 ctx .Data ["FileNameHash" ] = git .HashFilePathForWebUI (filePath )
9381010 ctx .Data ["AfterCommitID" ] = commitID
9391011 ctx .Data ["Anchor" ] = anchor
1012+ ctx .Data ["Issue" ] = issue
1013+ ctx .Data ["issue" ] = issue .Index
1014+ ctx .Data ["SignedUserID" ] = ctx .Data ["SignedUserID" ]
1015+ ctx .Data ["CanBlockUser" ] = func (blocker , blockee * user_model.User ) bool {
1016+ return user_service .CanBlockUser (ctx , ctx .Doer , blocker , blockee )
1017+ }
1018+
1019+ if ctx .Data ["SignedUserID" ] == nil {
1020+ ctx .Data ["SignedUserID" ] = ctx .Doer .ID
1021+ }
1022+ ctx .Data ["SignedUser" ] = ctx .Doer
1023+ ctx .Data ["IsSigned" ] = ctx .Doer != nil
1024+ ctx .Data ["Repository" ] = ctx .Repo .Repository
1025+ ctx .Data ["Permission" ] = & ctx .Repo .Permission
9401026 ctx .HTML (http .StatusOK , tplBlobExcerpt )
9411027}
9421028
943- func getExcerptLines (commit * git.Commit , filePath string , idxLeft , idxRight , chunkSize int ) ([]* gitdiff.DiffLine , error ) {
1029+ func getExcerptLines (commit * git.Commit , filePath string , idxLeft , idxRight , chunkSize , lastRightCommentIdx , rightCommentIdx int ) ([]* gitdiff.DiffLine , error ) {
9441030 blob , err := commit .Tree .GetBlobByPath (filePath )
9451031 if err != nil {
9461032 return nil , err
@@ -965,7 +1051,12 @@ func getExcerptLines(commit *git.Commit, filePath string, idxLeft, idxRight, chu
9651051 RightIdx : line + 1 ,
9661052 Type : gitdiff .DiffLinePlain ,
9671053 Content : " " + lineText ,
1054+ Comments : []* issues_model.Comment {},
9681055 }
1056+ // if diffLine.SectionInfo != nil {
1057+ // diffLine.SectionInfo.LastRightCommentIdx = lastRightCommentIdx
1058+ // diffLine.SectionInfo.RightCommentIdx = rightCommentIdx
1059+ // }
9691060 diffLines = append (diffLines , diffLine )
9701061 }
9711062 if err = scanner .Err (); err != nil {
0 commit comments