Move git diff codes from models to services/gitdiff (#7889)

* move git diff codes from models to services/gitdiff

* fix template

* fix test

* fix template
This commit is contained in:
Lunny Xiao 2019-09-06 10:20:09 +08:00 committed by techknowlogick
parent b660a732ae
commit c03d75fbd5
15 changed files with 164 additions and 111 deletions

View file

@ -7,7 +7,6 @@
package models package models
import ( import (
"bytes"
"fmt" "fmt"
"strings" "strings"
@ -15,7 +14,6 @@ import (
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/markup/markdown" "code.gitea.io/gitea/modules/markup/markdown"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs" api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/timeutil"
@ -488,32 +486,6 @@ func (c *Comment) UnsignedLine() uint64 {
return uint64(c.Line) return uint64(c.Line)
} }
// AsDiff returns c.Patch as *Diff
func (c *Comment) AsDiff() (*Diff, error) {
diff, err := ParsePatch(setting.Git.MaxGitDiffLines,
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(c.Patch))
if err != nil {
return nil, err
}
if len(diff.Files) == 0 {
return nil, fmt.Errorf("no file found for comment ID: %d", c.ID)
}
secs := diff.Files[0].Sections
if len(secs) == 0 {
return nil, fmt.Errorf("no sections found for comment ID: %d", c.ID)
}
return diff, nil
}
// MustAsDiff executes AsDiff and logs the error instead of returning
func (c *Comment) MustAsDiff() *Diff {
diff, err := c.AsDiff()
if err != nil {
log.Warn("MustAsDiff: %v", err)
}
return diff
}
// CodeCommentURL returns the url to a comment in code // CodeCommentURL returns the url to a comment in code
func (c *Comment) CodeCommentURL() string { func (c *Comment) CodeCommentURL() string {
err := c.LoadIssue() err := c.LoadIssue()
@ -873,59 +845,6 @@ func CreateIssueComment(doer *User, repo *Repository, issue *Issue, content stri
return comment, nil return comment, nil
} }
// CreateCodeComment creates a plain code comment at the specified line / path
func CreateCodeComment(doer *User, repo *Repository, issue *Issue, content, treePath string, line, reviewID int64) (*Comment, error) {
var commitID, patch string
pr, err := GetPullRequestByIssueID(issue.ID)
if err != nil {
return nil, fmt.Errorf("GetPullRequestByIssueID: %v", err)
}
if err := pr.GetBaseRepo(); err != nil {
return nil, fmt.Errorf("GetHeadRepo: %v", err)
}
gitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath())
if err != nil {
return nil, fmt.Errorf("OpenRepository: %v", err)
}
// FIXME validate treePath
// Get latest commit referencing the commented line
// No need for get commit for base branch changes
if line > 0 {
commit, err := gitRepo.LineBlame(pr.GetGitRefName(), gitRepo.Path, treePath, uint(line))
if err == nil {
commitID = commit.ID.String()
} else if !strings.Contains(err.Error(), "exit status 128 - fatal: no such path") {
return nil, fmt.Errorf("LineBlame[%s, %s, %s, %d]: %v", pr.GetGitRefName(), gitRepo.Path, treePath, line, err)
}
}
// Only fetch diff if comment is review comment
if reviewID != 0 {
headCommitID, err := gitRepo.GetRefCommitID(pr.GetGitRefName())
if err != nil {
return nil, fmt.Errorf("GetRefCommitID[%s]: %v", pr.GetGitRefName(), err)
}
patchBuf := new(bytes.Buffer)
if err := GetRawDiffForFile(gitRepo.Path, pr.MergeBase, headCommitID, RawDiffNormal, treePath, patchBuf); err != nil {
return nil, fmt.Errorf("GetRawDiffForLine[%s, %s, %s, %s]: %v", err, gitRepo.Path, pr.MergeBase, headCommitID, treePath)
}
patch = CutDiffAroundLine(patchBuf, int64((&Comment{Line: line}).UnsignedLine()), line < 0, setting.UI.CodeCommentLines)
}
return CreateComment(&CreateCommentOptions{
Type: CommentTypeCode,
Doer: doer,
Repo: repo,
Issue: issue,
Content: content,
LineNum: line,
TreePath: treePath,
CommitSHA: commitID,
ReviewID: reviewID,
Patch: patch,
})
}
// CreateRefComment creates a commit reference comment to issue. // CreateRefComment creates a commit reference comment to issue.
func CreateRefComment(doer *User, repo *Repository, issue *Issue, content, commitSHA string) error { func CreateRefComment(doer *User, repo *Repository, issue *Issue, content, commitSHA string) error {
if len(commitSHA) == 0 { if len(commitSHA) == 0 {

View file

@ -250,3 +250,8 @@ func MaxBatchInsertSize(bean interface{}) int {
t := x.TableInfo(bean) t := x.TableInfo(bean)
return 999 / len(t.ColumnsSeq()) return 999 / len(t.ColumnsSeq())
} }
// Count returns records number according struct's fields as database query conditions
func Count(bean interface{}) (int64, error) {
return x.Count(bean)
}

View file

@ -8,10 +8,11 @@ import (
"strings" "strings"
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/services/gitdiff"
) )
// GetDiffPreview produces and returns diff result of a file which is not yet committed. // GetDiffPreview produces and returns diff result of a file which is not yet committed.
func GetDiffPreview(repo *models.Repository, branch, treePath, content string) (*models.Diff, error) { func GetDiffPreview(repo *models.Repository, branch, treePath, content string) (*gitdiff.Diff, error) {
if branch == "" { if branch == "" {
branch = repo.DefaultBranch branch = repo.DefaultBranch
} }

View file

@ -9,6 +9,7 @@ import (
"code.gitea.io/gitea/models" "code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/test" "code.gitea.io/gitea/modules/test"
"code.gitea.io/gitea/services/gitdiff"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -25,10 +26,10 @@ func TestGetDiffPreview(t *testing.T) {
treePath := "README.md" treePath := "README.md"
content := "# repo1\n\nDescription for repo1\nthis is a new line" content := "# repo1\n\nDescription for repo1\nthis is a new line"
expectedDiff := &models.Diff{ expectedDiff := &gitdiff.Diff{
TotalAddition: 2, TotalAddition: 2,
TotalDeletion: 1, TotalDeletion: 1,
Files: []*models.DiffFile{ Files: []*gitdiff.DiffFile{
{ {
Name: "README.md", Name: "README.md",
OldName: "README.md", OldName: "README.md",
@ -42,10 +43,10 @@ func TestGetDiffPreview(t *testing.T) {
IsLFSFile: false, IsLFSFile: false,
IsRenamed: false, IsRenamed: false,
IsSubmodule: false, IsSubmodule: false,
Sections: []*models.DiffSection{ Sections: []*gitdiff.DiffSection{
{ {
Name: "", Name: "",
Lines: []*models.DiffLine{ Lines: []*gitdiff.DiffLine{
{ {
LeftIdx: 0, LeftIdx: 0,
RightIdx: 0, RightIdx: 0,

View file

@ -20,6 +20,7 @@ import (
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/process" "code.gitea.io/gitea/modules/process"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/services/gitdiff"
) )
// TemporaryUploadRepository is a type to wrap our upload repositories as a shallow clone // TemporaryUploadRepository is a type to wrap our upload repositories as a shallow clone
@ -290,7 +291,7 @@ func (t *TemporaryUploadRepository) Push(doer *models.User, commitHash string, b
} }
// DiffIndex returns a Diff of the current index to the head // DiffIndex returns a Diff of the current index to the head
func (t *TemporaryUploadRepository) DiffIndex() (diff *models.Diff, err error) { func (t *TemporaryUploadRepository) DiffIndex() (diff *gitdiff.Diff, err error) {
timeout := 5 * time.Minute timeout := 5 * time.Minute
ctx, cancel := context.WithTimeout(context.Background(), timeout) ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel() defer cancel()
@ -313,7 +314,7 @@ func (t *TemporaryUploadRepository) DiffIndex() (diff *models.Diff, err error) {
pid := process.GetManager().Add(fmt.Sprintf("diffIndex [repo_path: %s]", t.repo.RepoPath()), cmd) pid := process.GetManager().Add(fmt.Sprintf("diffIndex [repo_path: %s]", t.repo.RepoPath()), cmd)
defer process.GetManager().Remove(pid) defer process.GetManager().Remove(pid)
diff, err = models.ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, stdout) diff, err = gitdiff.ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, stdout)
if err != nil { if err != nil {
return nil, fmt.Errorf("ParsePatch: %v", err) return nil, fmt.Errorf("ParsePatch: %v", err)
} }

View file

@ -27,6 +27,7 @@ import (
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/timeutil" "code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/services/gitdiff"
"gopkg.in/editorconfig/editorconfig-core-go.v1" "gopkg.in/editorconfig/editorconfig-core-go.v1"
) )
@ -230,6 +231,7 @@ func NewFuncMap() []template.FuncMap {
} }
return float32(n) * 100 / float32(sum) return float32(n) * 100 / float32(sum)
}, },
"CommentMustAsDiff": gitdiff.CommentMustAsDiff,
}} }}
} }

View file

@ -16,6 +16,7 @@ import (
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/services/gitdiff"
) )
const ( const (
@ -217,7 +218,7 @@ func Diff(ctx *context.Context) {
ctx.Data["CommitStatus"] = models.CalcCommitStatus(statuses) ctx.Data["CommitStatus"] = models.CalcCommitStatus(statuses)
diff, err := models.GetDiffCommit(models.RepoPath(userName, repoName), diff, err := gitdiff.GetDiffCommit(models.RepoPath(userName, repoName),
commitID, setting.Git.MaxGitDiffLines, commitID, setting.Git.MaxGitDiffLines,
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles) setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
if err != nil { if err != nil {
@ -269,10 +270,10 @@ func Diff(ctx *context.Context) {
// RawDiff dumps diff results of repository in given commit ID to io.Writer // RawDiff dumps diff results of repository in given commit ID to io.Writer
func RawDiff(ctx *context.Context) { func RawDiff(ctx *context.Context) {
if err := models.GetRawDiff( if err := gitdiff.GetRawDiff(
models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name), models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name),
ctx.Params(":sha"), ctx.Params(":sha"),
models.RawDiffType(ctx.Params(":ext")), gitdiff.RawDiffType(ctx.Params(":ext")),
ctx.Resp, ctx.Resp,
); err != nil { ); err != nil {
ctx.ServerError("GetRawDiff", err) ctx.ServerError("GetRawDiff", err)

View file

@ -14,6 +14,7 @@ import (
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/services/gitdiff"
) )
const ( const (
@ -230,7 +231,7 @@ func PrepareCompareDiff(
return true return true
} }
diff, err := models.GetDiffRange(models.RepoPath(headUser.Name, headRepo.Name), diff, err := gitdiff.GetDiffRange(models.RepoPath(headUser.Name, headRepo.Name),
compareInfo.MergeBase, headCommitID, setting.Git.MaxGitDiffLines, compareInfo.MergeBase, headCommitID, setting.Git.MaxGitDiffLines,
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles) setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
if err != nil { if err != nil {

View file

@ -24,6 +24,7 @@ import (
"code.gitea.io/gitea/modules/pull" "code.gitea.io/gitea/modules/pull"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/services/gitdiff"
"github.com/unknwon/com" "github.com/unknwon/com"
) )
@ -517,7 +518,7 @@ func ViewPullFiles(ctx *context.Context) {
ctx.Data["Reponame"] = pull.HeadRepo.Name ctx.Data["Reponame"] = pull.HeadRepo.Name
} }
diff, err := models.GetDiffRangeWithWhitespaceBehavior(diffRepoPath, diff, err := gitdiff.GetDiffRangeWithWhitespaceBehavior(diffRepoPath,
startCommitID, endCommitID, setting.Git.MaxGitDiffLines, startCommitID, endCommitID, setting.Git.MaxGitDiffLines,
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles,
whitespaceFlags[ctx.Data["WhitespaceBehavior"].(string)]) whitespaceFlags[ctx.Data["WhitespaceBehavior"].(string)])

View file

@ -13,6 +13,7 @@ import (
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/notification" "code.gitea.io/gitea/modules/notification"
pull_service "code.gitea.io/gitea/modules/pull" pull_service "code.gitea.io/gitea/modules/pull"
comment_service "code.gitea.io/gitea/services/comments"
) )
// CreateCodeComment will create a code comment including an pending review if required // CreateCodeComment will create a code comment including an pending review if required
@ -69,7 +70,7 @@ func CreateCodeComment(ctx *context.Context, form auth.CodeCommentForm) {
review.ID = form.Reply review.ID = form.Reply
} }
//FIXME check if line, commit and treepath exist //FIXME check if line, commit and treepath exist
comment, err := models.CreateCodeComment( comment, err := comment_service.CreateCodeComment(
ctx.User, ctx.User,
issue.Repo, issue.Repo,
issue, issue,

View file

@ -0,0 +1,69 @@
// Copyright 2019 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package comments
import (
"bytes"
"fmt"
"strings"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/services/gitdiff"
)
// CreateCodeComment creates a plain code comment at the specified line / path
func CreateCodeComment(doer *models.User, repo *models.Repository, issue *models.Issue, content, treePath string, line, reviewID int64) (*models.Comment, error) {
var commitID, patch string
pr, err := models.GetPullRequestByIssueID(issue.ID)
if err != nil {
return nil, fmt.Errorf("GetPullRequestByIssueID: %v", err)
}
if err := pr.GetBaseRepo(); err != nil {
return nil, fmt.Errorf("GetHeadRepo: %v", err)
}
gitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath())
if err != nil {
return nil, fmt.Errorf("OpenRepository: %v", err)
}
// FIXME validate treePath
// Get latest commit referencing the commented line
// No need for get commit for base branch changes
if line > 0 {
commit, err := gitRepo.LineBlame(pr.GetGitRefName(), gitRepo.Path, treePath, uint(line))
if err == nil {
commitID = commit.ID.String()
} else if !strings.Contains(err.Error(), "exit status 128 - fatal: no such path") {
return nil, fmt.Errorf("LineBlame[%s, %s, %s, %d]: %v", pr.GetGitRefName(), gitRepo.Path, treePath, line, err)
}
}
// Only fetch diff if comment is review comment
if reviewID != 0 {
headCommitID, err := gitRepo.GetRefCommitID(pr.GetGitRefName())
if err != nil {
return nil, fmt.Errorf("GetRefCommitID[%s]: %v", pr.GetGitRefName(), err)
}
patchBuf := new(bytes.Buffer)
if err := gitdiff.GetRawDiffForFile(gitRepo.Path, pr.MergeBase, headCommitID, gitdiff.RawDiffNormal, treePath, patchBuf); err != nil {
return nil, fmt.Errorf("GetRawDiffForLine[%s, %s, %s, %s]: %v", err, gitRepo.Path, pr.MergeBase, headCommitID, treePath)
}
patch = gitdiff.CutDiffAroundLine(patchBuf, int64((&models.Comment{Line: line}).UnsignedLine()), line < 0, setting.UI.CodeCommentLines)
}
return models.CreateComment(&models.CreateCommentOptions{
Type: models.CommentTypeCode,
Doer: doer,
Repo: repo,
Issue: issue,
Content: content,
LineNum: line,
TreePath: treePath,
CommitSHA: commitID,
ReviewID: reviewID,
Patch: patch,
})
}

View file

@ -1,8 +1,9 @@
// Copyright 2014 The Gogs Authors. All rights reserved. // Copyright 2014 The Gogs Authors. All rights reserved.
// Copyright 2019 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style // Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file. // license that can be found in the LICENSE file.
package models package gitdiff
import ( import (
"bufio" "bufio"
@ -19,6 +20,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/charset" "code.gitea.io/gitea/modules/charset"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/highlight" "code.gitea.io/gitea/modules/highlight"
@ -60,7 +62,7 @@ type DiffLine struct {
RightIdx int RightIdx int
Type DiffLineType Type DiffLineType
Content string Content string
Comments []*Comment Comments []*models.Comment
} }
// GetType returns the type of a DiffLine. // GetType returns the type of a DiffLine.
@ -254,8 +256,8 @@ type Diff struct {
} }
// LoadComments loads comments into each line // LoadComments loads comments into each line
func (diff *Diff) LoadComments(issue *Issue, currentUser *User) error { func (diff *Diff) LoadComments(issue *models.Issue, currentUser *models.User) error {
allComments, err := FetchCodeComments(issue, currentUser) allComments, err := models.FetchCodeComments(issue, currentUser)
if err != nil { if err != nil {
return err return err
} }
@ -472,16 +474,16 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D
trimLine := strings.Trim(line, "+- ") trimLine := strings.Trim(line, "+- ")
if trimLine == LFSMetaFileIdentifier { if trimLine == models.LFSMetaFileIdentifier {
curFileLFSPrefix = true curFileLFSPrefix = true
} }
if curFileLFSPrefix && strings.HasPrefix(trimLine, LFSMetaFileOidPrefix) { if curFileLFSPrefix && strings.HasPrefix(trimLine, models.LFSMetaFileOidPrefix) {
oid := strings.TrimPrefix(trimLine, LFSMetaFileOidPrefix) oid := strings.TrimPrefix(trimLine, models.LFSMetaFileOidPrefix)
if len(oid) == 64 { if len(oid) == 64 {
m := &LFSMetaObject{Oid: oid} m := &models.LFSMetaObject{Oid: oid}
count, err := x.Count(m) count, err := models.Count(m)
if err == nil && count > 0 { if err == nil && count > 0 {
curFile.IsBin = true curFile.IsBin = true
@ -798,3 +800,29 @@ func GetRawDiffForFile(repoPath, startCommit, endCommit string, diffType RawDiff
func GetDiffCommit(repoPath, commitID string, maxLines, maxLineCharacters, maxFiles int) (*Diff, error) { func GetDiffCommit(repoPath, commitID string, maxLines, maxLineCharacters, maxFiles int) (*Diff, error) {
return GetDiffRange(repoPath, "", commitID, maxLines, maxLineCharacters, maxFiles) return GetDiffRange(repoPath, "", commitID, maxLines, maxLineCharacters, maxFiles)
} }
// CommentAsDiff returns c.Patch as *Diff
func CommentAsDiff(c *models.Comment) (*Diff, error) {
diff, err := ParsePatch(setting.Git.MaxGitDiffLines,
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(c.Patch))
if err != nil {
return nil, err
}
if len(diff.Files) == 0 {
return nil, fmt.Errorf("no file found for comment ID: %d", c.ID)
}
secs := diff.Files[0].Sections
if len(secs) == 0 {
return nil, fmt.Errorf("no sections found for comment ID: %d", c.ID)
}
return diff, nil
}
// CommentMustAsDiff executes AsDiff and logs the error instead of returning
func CommentMustAsDiff(c *models.Comment) *Diff {
diff, err := CommentAsDiff(c)
if err != nil {
log.Warn("CommentMustAsDiff: %v", err)
}
return diff
}

View file

@ -1,10 +1,16 @@
package models // Copyright 2014 The Gogs Authors. All rights reserved.
// Copyright 2019 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitdiff
import ( import (
"html/template" "html/template"
"strings" "strings"
"testing" "testing"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
dmp "github.com/sergi/go-diff/diffmatchpatch" dmp "github.com/sergi/go-diff/diffmatchpatch"
@ -168,23 +174,24 @@ func setupDefaultDiff() *Diff {
} }
} }
func TestDiff_LoadComments(t *testing.T) { func TestDiff_LoadComments(t *testing.T) {
issue := AssertExistsAndLoadBean(t, &Issue{ID: 2}).(*Issue) assert.NoError(t, models.PrepareTestDatabase())
user := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 2}).(*models.Issue)
user := models.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User)
diff := setupDefaultDiff() diff := setupDefaultDiff()
assert.NoError(t, PrepareTestDatabase())
assert.NoError(t, diff.LoadComments(issue, user)) assert.NoError(t, diff.LoadComments(issue, user))
assert.Len(t, diff.Files[0].Sections[0].Lines[0].Comments, 2) assert.Len(t, diff.Files[0].Sections[0].Lines[0].Comments, 2)
} }
func TestDiffLine_CanComment(t *testing.T) { func TestDiffLine_CanComment(t *testing.T) {
assert.False(t, (&DiffLine{Type: DiffLineSection}).CanComment()) assert.False(t, (&DiffLine{Type: DiffLineSection}).CanComment())
assert.False(t, (&DiffLine{Type: DiffLineAdd, Comments: []*Comment{{Content: "bla"}}}).CanComment()) assert.False(t, (&DiffLine{Type: DiffLineAdd, Comments: []*models.Comment{{Content: "bla"}}}).CanComment())
assert.True(t, (&DiffLine{Type: DiffLineAdd}).CanComment()) assert.True(t, (&DiffLine{Type: DiffLineAdd}).CanComment())
assert.True(t, (&DiffLine{Type: DiffLineDel}).CanComment()) assert.True(t, (&DiffLine{Type: DiffLineDel}).CanComment())
assert.True(t, (&DiffLine{Type: DiffLinePlain}).CanComment()) assert.True(t, (&DiffLine{Type: DiffLinePlain}).CanComment())
} }
func TestDiffLine_GetCommentSide(t *testing.T) { func TestDiffLine_GetCommentSide(t *testing.T) {
assert.Equal(t, "previous", (&DiffLine{Comments: []*Comment{{Line: -3}}}).GetCommentSide()) assert.Equal(t, "previous", (&DiffLine{Comments: []*models.Comment{{Line: -3}}}).GetCommentSide())
assert.Equal(t, "proposed", (&DiffLine{Comments: []*Comment{{Line: 3}}}).GetCommentSide()) assert.Equal(t, "proposed", (&DiffLine{Comments: []*models.Comment{{Line: 3}}}).GetCommentSide())
} }

View file

@ -0,0 +1,16 @@
// Copyright 2019 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package gitdiff
import (
"path/filepath"
"testing"
"code.gitea.io/gitea/models"
)
func TestMain(m *testing.M) {
models.MainTest(m, filepath.Join("..", ".."))
}

View file

@ -319,7 +319,7 @@
{{end}} {{end}}
<a href="{{(index $comms 0).CodeCommentURL}}" class="file-comment">{{$filename}}</a> <a href="{{(index $comms 0).CodeCommentURL}}" class="file-comment">{{$filename}}</a>
</div> </div>
{{$diff := ((index $comms 0).MustAsDiff)}} {{$diff := (CommentMustAsDiff (index $comms 0))}}
{{if $diff}} {{if $diff}}
{{$file := (index $diff.Files 0)}} {{$file := (index $diff.Files 0)}}
<div id="code-preview-{{(index $comms 0).ID}}" class="ui table segment{{if $invalid}} hide{{end}}"> <div id="code-preview-{{(index $comms 0).ID}}" class="ui table segment{{if $invalid}} hide{{end}}">