Skip to content

Commit 274ed54

Browse files
feat: phase 1 changes: add comments feat to unchanged lines in the pull request.
1 parent 0f397ae commit 274ed54

File tree

8 files changed

+292
-82
lines changed

8 files changed

+292
-82
lines changed

routers/web/repo/compare.go

Lines changed: 102 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

4446
const (
@@ -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 {

routers/web/web.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,6 +1511,11 @@ func registerRoutes(m *web.Router) {
15111511
m.Get("/{sha}", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.ExcerptBlob)
15121512
}, func(ctx *context.Context) gocontext.CancelFunc {
15131513
// FIXME: refactor this function, use separate routes for wiki/code
1514+
if ctx.FormBool("pull") {
1515+
ctx.Data["PageIsPullFiles"] = true
1516+
1517+
}
1518+
15141519
if ctx.FormBool("wiki") {
15151520
ctx.Data["PageIsWiki"] = true
15161521
repo.MustEnableWiki(ctx)

services/gitdiff/gitdiff.go

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,16 @@ type DiffLine struct {
8686

8787
// DiffLineSectionInfo represents diff line section meta data
8888
type DiffLineSectionInfo struct {
89-
Path string
90-
LastLeftIdx int
91-
LastRightIdx int
92-
LeftIdx int
93-
RightIdx int
94-
LeftHunkSize int
95-
RightHunkSize int
89+
Path string
90+
LastLeftIdx int
91+
LastRightIdx int
92+
LeftIdx int
93+
RightIdx int
94+
LeftHunkSize int
95+
RightHunkSize int
96+
HasComments bool
97+
LastRightCommentIdx int
98+
RightCommentIdx int
9699
}
97100

98101
// BlobExcerptChunkSize represent max lines of excerpt
@@ -118,7 +121,7 @@ func (d *DiffLine) GetHTMLDiffLineType() string {
118121

119122
// CanComment returns whether a line can get commented
120123
func (d *DiffLine) CanComment() bool {
121-
return len(d.Comments) == 0 && d.Type != DiffLineSection
124+
return len(d.Comments) == 0
122125
}
123126

124127
// GetCommentSide returns the comment side of the first comment, if not set returns empty string
@@ -143,10 +146,12 @@ func (d *DiffLine) GetBlobExcerptQuery() string {
143146
"last_left=%d&last_right=%d&"+
144147
"left=%d&right=%d&"+
145148
"left_hunk_size=%d&right_hunk_size=%d&"+
149+
"last_rightt_comment_idx=%d&right_comment_idx=%d&"+
146150
"path=%s",
147151
d.SectionInfo.LastLeftIdx, d.SectionInfo.LastRightIdx,
148152
d.SectionInfo.LeftIdx, d.SectionInfo.RightIdx,
149153
d.SectionInfo.LeftHunkSize, d.SectionInfo.RightHunkSize,
154+
d.SectionInfo.LastRightCommentIdx, d.SectionInfo.RightCommentIdx,
150155
url.QueryEscape(d.SectionInfo.Path))
151156
return query
152157
}
@@ -170,13 +175,16 @@ func getDiffLineSectionInfo(treePath, line string, lastLeftIdx, lastRightIdx int
170175
leftLine, leftHunk, rightLine, righHunk := git.ParseDiffHunkString(line)
171176

172177
return &DiffLineSectionInfo{
173-
Path: treePath,
174-
LastLeftIdx: lastLeftIdx,
175-
LastRightIdx: lastRightIdx,
176-
LeftIdx: leftLine,
177-
RightIdx: rightLine,
178-
LeftHunkSize: leftHunk,
179-
RightHunkSize: righHunk,
178+
Path: treePath,
179+
LastLeftIdx: lastLeftIdx,
180+
LastRightIdx: lastRightIdx,
181+
LeftIdx: leftLine,
182+
RightIdx: rightLine,
183+
LeftHunkSize: leftHunk,
184+
RightHunkSize: righHunk,
185+
HasComments: false,
186+
LastRightCommentIdx: 0,
187+
RightCommentIdx: 0,
180188
}
181189
}
182190

@@ -396,11 +404,14 @@ func (diffFile *DiffFile) GetTailSection(gitRepo *git.Repository, leftCommit, ri
396404
Type: DiffLineSection,
397405
Content: " ",
398406
SectionInfo: &DiffLineSectionInfo{
399-
Path: diffFile.Name,
400-
LastLeftIdx: lastLine.LeftIdx,
401-
LastRightIdx: lastLine.RightIdx,
402-
LeftIdx: leftLineCount,
403-
RightIdx: rightLineCount,
407+
Path: diffFile.Name,
408+
LastLeftIdx: lastLine.LeftIdx,
409+
LastRightIdx: lastLine.RightIdx,
410+
LeftIdx: leftLineCount,
411+
RightIdx: rightLineCount,
412+
HasComments: false,
413+
LastRightCommentIdx: 0,
414+
RightCommentIdx: 0,
404415
},
405416
}
406417
tailSection := &DiffSection{FileName: diffFile.Name, Lines: []*DiffLine{tailDiffLine}}
@@ -458,16 +469,38 @@ type Diff struct {
458469
NumViewedFiles int // user-specific
459470
}
460471

472+
// function (section *DiffSection) GetType() int {
473+
461474
// LoadComments loads comments into each line
462475
func (diff *Diff) LoadComments(ctx context.Context, issue *issues_model.Issue, currentUser *user_model.User, showOutdatedComments bool) error {
463476
allComments, err := issues_model.FetchCodeComments(ctx, issue, currentUser, showOutdatedComments)
464477
if err != nil {
465478
return err
466479
}
480+
467481
for _, file := range diff.Files {
468482
if lineCommits, ok := allComments[file.Name]; ok {
469483
for _, section := range file.Sections {
470484
for _, line := range section.Lines {
485+
if line.SectionInfo != nil {
486+
start := int64(line.SectionInfo.LastRightIdx + 1)
487+
end := int64(line.SectionInfo.RightIdx - 1)
488+
489+
for start <= end {
490+
if _, ok := lineCommits[start]; ok {
491+
if line.SectionInfo.LastRightCommentIdx == 0 {
492+
// line.SectionInfo.LastRightCommentIdx = int(start)
493+
// line.SectionInfo.RightCommentIdx = int(start)
494+
line.SectionInfo.HasComments = true
495+
496+
break
497+
}
498+
499+
}
500+
start += 1
501+
502+
}
503+
}
471504
if comments, ok := lineCommits[int64(line.LeftIdx*-1)]; ok {
472505
line.Comments = append(line.Comments, comments...)
473506
}

services/repository/files/diff_test.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,16 @@ func TestGetDiffPreview(t *testing.T) {
5959
Content: "@@ -1,3 +1,4 @@",
6060
Comments: nil,
6161
SectionInfo: &gitdiff.DiffLineSectionInfo{
62-
Path: "README.md",
63-
LastLeftIdx: 0,
64-
LastRightIdx: 0,
65-
LeftIdx: 1,
66-
RightIdx: 1,
67-
LeftHunkSize: 3,
68-
RightHunkSize: 4,
62+
Path: "README.md",
63+
LastLeftIdx: 0,
64+
LastRightIdx: 0,
65+
LeftIdx: 1,
66+
RightIdx: 1,
67+
LeftHunkSize: 3,
68+
RightHunkSize: 4,
69+
HasComments: false,
70+
LastRightCommentIdx: 0,
71+
RightCommentIdx: 0,
6972
},
7073
},
7174
{

0 commit comments

Comments
 (0)