diff --git a/models/issues/comment_code.go b/models/issues/comment_code.go index 67a77ceb13..fdba7dca25 100644 --- a/models/issues/comment_code.go +++ b/models/issues/comment_code.go @@ -18,11 +18,11 @@ import ( type CodeComments map[string]map[int64][]*Comment // FetchCodeComments will return a 2d-map: ["Path"]["Line"] = Comments at line -func FetchCodeComments(ctx context.Context, issue *Issue, currentUser *user_model.User, showOutdatedComments bool) (CodeComments, error) { - return fetchCodeCommentsByReview(ctx, issue, currentUser, nil, showOutdatedComments) +func FetchCodeComments(ctx context.Context, issue *Issue, currentUser *user_model.User, showOutdatedComments bool, filePath *string) (CodeComments, error) { + return fetchCodeCommentsByReview(ctx, issue, currentUser, nil, showOutdatedComments, filePath) } -func fetchCodeCommentsByReview(ctx context.Context, issue *Issue, currentUser *user_model.User, review *Review, showOutdatedComments bool) (CodeComments, error) { +func fetchCodeCommentsByReview(ctx context.Context, issue *Issue, currentUser *user_model.User, review *Review, showOutdatedComments bool, filePath *string) (CodeComments, error) { pathToLineToComment := make(CodeComments) if review == nil { review = &Review{ID: 0} @@ -33,6 +33,15 @@ func fetchCodeCommentsByReview(ctx context.Context, issue *Issue, currentUser *u ReviewID: review.ID, } + if filePath != nil { + opts = FindCommentsOptions{ + Type: CommentTypeCode, + IssueID: issue.ID, + ReviewID: review.ID, + TreePath: *filePath, + } + } + comments, err := findCodeComments(ctx, opts, issue, currentUser, review, showOutdatedComments) if err != nil { return nil, err diff --git a/models/issues/comment_test.go b/models/issues/comment_test.go index c5bbfdedc2..10a4558639 100644 --- a/models/issues/comment_test.go +++ b/models/issues/comment_test.go @@ -50,7 +50,7 @@ func TestFetchCodeComments(t *testing.T) { issue := unittest.AssertExistsAndLoadBean(t, &issues_model.Issue{ID: 2}) user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) - res, err := issues_model.FetchCodeComments(db.DefaultContext, issue, user, false) + res, err := issues_model.FetchCodeComments(db.DefaultContext, issue, user, false, nil) assert.NoError(t, err) assert.Contains(t, res, "README.md") assert.Contains(t, res["README.md"], int64(4)) @@ -58,7 +58,7 @@ func TestFetchCodeComments(t *testing.T) { assert.Equal(t, int64(4), res["README.md"][4][0].ID) user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) - res, err = issues_model.FetchCodeComments(db.DefaultContext, issue, user2, false) + res, err = issues_model.FetchCodeComments(db.DefaultContext, issue, user2, false, nil) assert.NoError(t, err) assert.Len(t, res, 1) } diff --git a/models/issues/review.go b/models/issues/review.go index 8b345e5fd8..1242517261 100644 --- a/models/issues/review.go +++ b/models/issues/review.go @@ -158,7 +158,7 @@ func (r *Review) LoadCodeComments(ctx context.Context) (err error) { if err = r.LoadIssue(ctx); err != nil { return err } - r.CodeComments, err = fetchCodeCommentsByReview(ctx, r.Issue, nil, r, false) + r.CodeComments, err = fetchCodeCommentsByReview(ctx, r.Issue, nil, r, false, nil) return err } diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index 8112886683..4cd2f1006a 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -924,16 +924,14 @@ func ExcerptBlob(ctx *context.Context) { Type: gitdiff.DiffLineSection, Content: lineText, SectionInfo: &gitdiff.DiffLineSectionInfo{ - Path: filePath, - LastLeftIdx: lastLeft, - LastRightIdx: lastRight, - LeftIdx: idxLeft, - RightIdx: idxRight, - LeftHunkSize: leftHunkSize, - RightHunkSize: rightHunkSize, - HasComments: false, - LastRightCommentIdx: 0, - RightCommentIdx: 0, + Path: filePath, + LastLeftIdx: lastLeft, + LastRightIdx: lastRight, + LeftIdx: idxLeft, + RightIdx: idxRight, + LeftHunkSize: leftHunkSize, + RightHunkSize: rightHunkSize, + HasComments: false, }, Comments: nil, } @@ -946,11 +944,11 @@ func ExcerptBlob(ctx *context.Context) { issueIndex := ctx.FormInt64("issue_index") if ctx.FormBool("pull") && issueIndex > 0 { issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, issueIndex) - if issue == nil { + if err != nil { ctx.ServerError("GetIssueByIndex", err) return } - allComments, err := issues_model.FetchCodeComments(ctx, issue, ctx.Doer, false) + allComments, err := issues_model.FetchCodeComments(ctx, issue, ctx.Doer, false, &filePath) if err != nil { ctx.ServerError("FetchCodeComments", err) return diff --git a/routers/web/web.go b/routers/web/web.go index 6a1fb58264..85e0fdc41e 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -1520,10 +1520,6 @@ func registerRoutes(m *web.Router) { m.Get("/{sha}", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.ExcerptBlob) }, func(ctx *context.Context) gocontext.CancelFunc { // FIXME: refactor this function, use separate routes for wiki/code - if ctx.FormBool("pull") { - ctx.Data["PageIsPullFiles"] = true - } - if ctx.FormBool("wiki") { ctx.Data["PageIsWiki"] = true repo.MustEnableWiki(ctx) diff --git a/services/gitdiff/gitdiff.go b/services/gitdiff/gitdiff.go index c9e338c080..9cc1cc15a3 100644 --- a/services/gitdiff/gitdiff.go +++ b/services/gitdiff/gitdiff.go @@ -86,16 +86,14 @@ type DiffLine struct { // DiffLineSectionInfo represents diff line section meta data type DiffLineSectionInfo struct { - Path string - LastLeftIdx int - LastRightIdx int - LeftIdx int - RightIdx int - LeftHunkSize int - RightHunkSize int - HasComments bool - LastRightCommentIdx int - RightCommentIdx int + Path string + LastLeftIdx int + LastRightIdx int + LeftIdx int + RightIdx int + LeftHunkSize int + RightHunkSize int + HasComments bool } // BlobExcerptChunkSize represent max lines of excerpt @@ -146,12 +144,10 @@ func (d *DiffLine) GetBlobExcerptQuery() string { "last_left=%d&last_right=%d&"+ "left=%d&right=%d&"+ "left_hunk_size=%d&right_hunk_size=%d&"+ - "last_rightt_comment_idx=%d&right_comment_idx=%d&"+ "path=%s", d.SectionInfo.LastLeftIdx, d.SectionInfo.LastRightIdx, d.SectionInfo.LeftIdx, d.SectionInfo.RightIdx, d.SectionInfo.LeftHunkSize, d.SectionInfo.RightHunkSize, - d.SectionInfo.LastRightCommentIdx, d.SectionInfo.RightCommentIdx, url.QueryEscape(d.SectionInfo.Path)) return query } @@ -175,16 +171,14 @@ func getDiffLineSectionInfo(treePath, line string, lastLeftIdx, lastRightIdx int leftLine, leftHunk, rightLine, righHunk := git.ParseDiffHunkString(line) return &DiffLineSectionInfo{ - Path: treePath, - LastLeftIdx: lastLeftIdx, - LastRightIdx: lastRightIdx, - LeftIdx: leftLine, - RightIdx: rightLine, - LeftHunkSize: leftHunk, - RightHunkSize: righHunk, - HasComments: false, - LastRightCommentIdx: 0, - RightCommentIdx: 0, + Path: treePath, + LastLeftIdx: lastLeftIdx, + LastRightIdx: lastRightIdx, + LeftIdx: leftLine, + RightIdx: rightLine, + LeftHunkSize: leftHunk, + RightHunkSize: righHunk, + HasComments: false, } } @@ -404,14 +398,12 @@ func (diffFile *DiffFile) GetTailSection(gitRepo *git.Repository, leftCommit, ri Type: DiffLineSection, Content: " ", SectionInfo: &DiffLineSectionInfo{ - Path: diffFile.Name, - LastLeftIdx: lastLine.LeftIdx, - LastRightIdx: lastLine.RightIdx, - LeftIdx: leftLineCount, - RightIdx: rightLineCount, - HasComments: false, - LastRightCommentIdx: 0, - RightCommentIdx: 0, + Path: diffFile.Name, + LastLeftIdx: lastLine.LeftIdx, + LastRightIdx: lastLine.RightIdx, + LeftIdx: leftLineCount, + RightIdx: rightLineCount, + HasComments: false, }, } tailSection := &DiffSection{FileName: diffFile.Name, Lines: []*DiffLine{tailDiffLine}} @@ -469,11 +461,9 @@ type Diff struct { NumViewedFiles int // user-specific } -// function (section *DiffSection) GetType() int { - // LoadComments loads comments into each line func (diff *Diff) LoadComments(ctx context.Context, issue *issues_model.Issue, currentUser *user_model.User, showOutdatedComments bool) error { - allComments, err := issues_model.FetchCodeComments(ctx, issue, currentUser, showOutdatedComments) + allComments, err := issues_model.FetchCodeComments(ctx, issue, currentUser, showOutdatedComments, nil) if err != nil { return err } diff --git a/services/repository/files/diff_test.go b/services/repository/files/diff_test.go index 7b98886358..c698c83892 100644 --- a/services/repository/files/diff_test.go +++ b/services/repository/files/diff_test.go @@ -59,16 +59,14 @@ func TestGetDiffPreview(t *testing.T) { Content: "@@ -1,3 +1,4 @@", Comments: nil, SectionInfo: &gitdiff.DiffLineSectionInfo{ - Path: "README.md", - LastLeftIdx: 0, - LastRightIdx: 0, - LeftIdx: 1, - RightIdx: 1, - LeftHunkSize: 3, - RightHunkSize: 4, - HasComments: false, - LastRightCommentIdx: 0, - RightCommentIdx: 0, + Path: "README.md", + LastLeftIdx: 0, + LastRightIdx: 0, + LeftIdx: 1, + RightIdx: 1, + LeftHunkSize: 3, + RightHunkSize: 4, + HasComments: false, }, }, {