mirror of
https://github.com/go-gitea/gitea
synced 2024-12-22 16:44:26 +00:00
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:
parent
b660a732ae
commit
c03d75fbd5
@ -7,7 +7,6 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
@ -15,7 +14,6 @@ import (
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/markup"
|
||||
"code.gitea.io/gitea/modules/markup/markdown"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
|
||||
@ -488,32 +486,6 @@ func (c *Comment) UnsignedLine() uint64 {
|
||||
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
|
||||
func (c *Comment) CodeCommentURL() string {
|
||||
err := c.LoadIssue()
|
||||
@ -873,59 +845,6 @@ func CreateIssueComment(doer *User, repo *Repository, issue *Issue, content stri
|
||||
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.
|
||||
func CreateRefComment(doer *User, repo *Repository, issue *Issue, content, commitSHA string) error {
|
||||
if len(commitSHA) == 0 {
|
||||
|
@ -250,3 +250,8 @@ func MaxBatchInsertSize(bean interface{}) int {
|
||||
t := x.TableInfo(bean)
|
||||
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)
|
||||
}
|
||||
|
@ -8,10 +8,11 @@ import (
|
||||
"strings"
|
||||
|
||||
"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.
|
||||
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 == "" {
|
||||
branch = repo.DefaultBranch
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ import (
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/modules/test"
|
||||
"code.gitea.io/gitea/services/gitdiff"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
@ -25,10 +26,10 @@ func TestGetDiffPreview(t *testing.T) {
|
||||
treePath := "README.md"
|
||||
content := "# repo1\n\nDescription for repo1\nthis is a new line"
|
||||
|
||||
expectedDiff := &models.Diff{
|
||||
expectedDiff := &gitdiff.Diff{
|
||||
TotalAddition: 2,
|
||||
TotalDeletion: 1,
|
||||
Files: []*models.DiffFile{
|
||||
Files: []*gitdiff.DiffFile{
|
||||
{
|
||||
Name: "README.md",
|
||||
OldName: "README.md",
|
||||
@ -42,10 +43,10 @@ func TestGetDiffPreview(t *testing.T) {
|
||||
IsLFSFile: false,
|
||||
IsRenamed: false,
|
||||
IsSubmodule: false,
|
||||
Sections: []*models.DiffSection{
|
||||
Sections: []*gitdiff.DiffSection{
|
||||
{
|
||||
Name: "",
|
||||
Lines: []*models.DiffLine{
|
||||
Lines: []*gitdiff.DiffLine{
|
||||
{
|
||||
LeftIdx: 0,
|
||||
RightIdx: 0,
|
||||
|
@ -20,6 +20,7 @@ import (
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/process"
|
||||
"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
|
||||
@ -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
|
||||
func (t *TemporaryUploadRepository) DiffIndex() (diff *models.Diff, err error) {
|
||||
func (t *TemporaryUploadRepository) DiffIndex() (diff *gitdiff.Diff, err error) {
|
||||
timeout := 5 * time.Minute
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
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)
|
||||
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 {
|
||||
return nil, fmt.Errorf("ParsePatch: %v", err)
|
||||
}
|
||||
|
@ -27,6 +27,7 @@ import (
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/services/gitdiff"
|
||||
|
||||
"gopkg.in/editorconfig/editorconfig-core-go.v1"
|
||||
)
|
||||
@ -230,6 +231,7 @@ func NewFuncMap() []template.FuncMap {
|
||||
}
|
||||
return float32(n) * 100 / float32(sum)
|
||||
},
|
||||
"CommentMustAsDiff": gitdiff.CommentMustAsDiff,
|
||||
}}
|
||||
}
|
||||
|
||||
|
@ -16,6 +16,7 @@ import (
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/services/gitdiff"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -217,7 +218,7 @@ func Diff(ctx *context.Context) {
|
||||
|
||||
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,
|
||||
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
|
||||
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
|
||||
func RawDiff(ctx *context.Context) {
|
||||
if err := models.GetRawDiff(
|
||||
if err := gitdiff.GetRawDiff(
|
||||
models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name),
|
||||
ctx.Params(":sha"),
|
||||
models.RawDiffType(ctx.Params(":ext")),
|
||||
gitdiff.RawDiffType(ctx.Params(":ext")),
|
||||
ctx.Resp,
|
||||
); err != nil {
|
||||
ctx.ServerError("GetRawDiff", err)
|
||||
|
@ -14,6 +14,7 @@ import (
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/services/gitdiff"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -230,7 +231,7 @@ func PrepareCompareDiff(
|
||||
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,
|
||||
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
|
||||
if err != nil {
|
||||
|
@ -24,6 +24,7 @@ import (
|
||||
"code.gitea.io/gitea/modules/pull"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/services/gitdiff"
|
||||
|
||||
"github.com/unknwon/com"
|
||||
)
|
||||
@ -517,7 +518,7 @@ func ViewPullFiles(ctx *context.Context) {
|
||||
ctx.Data["Reponame"] = pull.HeadRepo.Name
|
||||
}
|
||||
|
||||
diff, err := models.GetDiffRangeWithWhitespaceBehavior(diffRepoPath,
|
||||
diff, err := gitdiff.GetDiffRangeWithWhitespaceBehavior(diffRepoPath,
|
||||
startCommitID, endCommitID, setting.Git.MaxGitDiffLines,
|
||||
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles,
|
||||
whitespaceFlags[ctx.Data["WhitespaceBehavior"].(string)])
|
||||
|
@ -13,6 +13,7 @@ import (
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/notification"
|
||||
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
|
||||
@ -69,7 +70,7 @@ func CreateCodeComment(ctx *context.Context, form auth.CodeCommentForm) {
|
||||
review.ID = form.Reply
|
||||
}
|
||||
//FIXME check if line, commit and treepath exist
|
||||
comment, err := models.CreateCodeComment(
|
||||
comment, err := comment_service.CreateCodeComment(
|
||||
ctx.User,
|
||||
issue.Repo,
|
||||
issue,
|
||||
|
69
services/comments/comments.go
Normal file
69
services/comments/comments.go
Normal 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,
|
||||
})
|
||||
}
|
@ -1,8 +1,9 @@
|
||||
// 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 models
|
||||
package gitdiff
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
@ -19,6 +20,7 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/modules/charset"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/highlight"
|
||||
@ -60,7 +62,7 @@ type DiffLine struct {
|
||||
RightIdx int
|
||||
Type DiffLineType
|
||||
Content string
|
||||
Comments []*Comment
|
||||
Comments []*models.Comment
|
||||
}
|
||||
|
||||
// GetType returns the type of a DiffLine.
|
||||
@ -254,8 +256,8 @@ type Diff struct {
|
||||
}
|
||||
|
||||
// LoadComments loads comments into each line
|
||||
func (diff *Diff) LoadComments(issue *Issue, currentUser *User) error {
|
||||
allComments, err := FetchCodeComments(issue, currentUser)
|
||||
func (diff *Diff) LoadComments(issue *models.Issue, currentUser *models.User) error {
|
||||
allComments, err := models.FetchCodeComments(issue, currentUser)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -472,16 +474,16 @@ func ParsePatch(maxLines, maxLineCharacters, maxFiles int, reader io.Reader) (*D
|
||||
|
||||
trimLine := strings.Trim(line, "+- ")
|
||||
|
||||
if trimLine == LFSMetaFileIdentifier {
|
||||
if trimLine == models.LFSMetaFileIdentifier {
|
||||
curFileLFSPrefix = true
|
||||
}
|
||||
|
||||
if curFileLFSPrefix && strings.HasPrefix(trimLine, LFSMetaFileOidPrefix) {
|
||||
oid := strings.TrimPrefix(trimLine, LFSMetaFileOidPrefix)
|
||||
if curFileLFSPrefix && strings.HasPrefix(trimLine, models.LFSMetaFileOidPrefix) {
|
||||
oid := strings.TrimPrefix(trimLine, models.LFSMetaFileOidPrefix)
|
||||
|
||||
if len(oid) == 64 {
|
||||
m := &LFSMetaObject{Oid: oid}
|
||||
count, err := x.Count(m)
|
||||
m := &models.LFSMetaObject{Oid: oid}
|
||||
count, err := models.Count(m)
|
||||
|
||||
if err == nil && count > 0 {
|
||||
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) {
|
||||
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
|
||||
}
|
@ -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 (
|
||||
"html/template"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"code.gitea.io/gitea/models"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
|
||||
dmp "github.com/sergi/go-diff/diffmatchpatch"
|
||||
@ -168,23 +174,24 @@ func setupDefaultDiff() *Diff {
|
||||
}
|
||||
}
|
||||
func TestDiff_LoadComments(t *testing.T) {
|
||||
issue := AssertExistsAndLoadBean(t, &Issue{ID: 2}).(*Issue)
|
||||
user := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
|
||||
assert.NoError(t, models.PrepareTestDatabase())
|
||||
|
||||
issue := models.AssertExistsAndLoadBean(t, &models.Issue{ID: 2}).(*models.Issue)
|
||||
user := models.AssertExistsAndLoadBean(t, &models.User{ID: 1}).(*models.User)
|
||||
diff := setupDefaultDiff()
|
||||
assert.NoError(t, PrepareTestDatabase())
|
||||
assert.NoError(t, diff.LoadComments(issue, user))
|
||||
assert.Len(t, diff.Files[0].Sections[0].Lines[0].Comments, 2)
|
||||
}
|
||||
|
||||
func TestDiffLine_CanComment(t *testing.T) {
|
||||
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: DiffLineDel}).CanComment())
|
||||
assert.True(t, (&DiffLine{Type: DiffLinePlain}).CanComment())
|
||||
}
|
||||
|
||||
func TestDiffLine_GetCommentSide(t *testing.T) {
|
||||
assert.Equal(t, "previous", (&DiffLine{Comments: []*Comment{{Line: -3}}}).GetCommentSide())
|
||||
assert.Equal(t, "proposed", (&DiffLine{Comments: []*Comment{{Line: 3}}}).GetCommentSide())
|
||||
assert.Equal(t, "previous", (&DiffLine{Comments: []*models.Comment{{Line: -3}}}).GetCommentSide())
|
||||
assert.Equal(t, "proposed", (&DiffLine{Comments: []*models.Comment{{Line: 3}}}).GetCommentSide())
|
||||
}
|
16
services/gitdiff/main_test.go
Normal file
16
services/gitdiff/main_test.go
Normal 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("..", ".."))
|
||||
}
|
@ -319,7 +319,7 @@
|
||||
{{end}}
|
||||
<a href="{{(index $comms 0).CodeCommentURL}}" class="file-comment">{{$filename}}</a>
|
||||
</div>
|
||||
{{$diff := ((index $comms 0).MustAsDiff)}}
|
||||
{{$diff := (CommentMustAsDiff (index $comms 0))}}
|
||||
{{if $diff}}
|
||||
{{$file := (index $diff.Files 0)}}
|
||||
<div id="code-preview-{{(index $comms 0).ID}}" class="ui table segment{{if $invalid}} hide{{end}}">
|
||||
|
Loading…
Reference in New Issue
Block a user