Skip to content

Commit c03d75f

Browse files
lunnytechknowlogick
authored andcommitted
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
1 parent b660a73 commit c03d75f

File tree

15 files changed

+164
-111
lines changed

15 files changed

+164
-111
lines changed

models/issue_comment.go

-81
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@
77
package models
88

99
import (
10-
"bytes"
1110
"fmt"
1211
"strings"
1312

1413
"code.gitea.io/gitea/modules/git"
1514
"code.gitea.io/gitea/modules/log"
1615
"code.gitea.io/gitea/modules/markup"
1716
"code.gitea.io/gitea/modules/markup/markdown"
18-
"code.gitea.io/gitea/modules/setting"
1917
api "code.gitea.io/gitea/modules/structs"
2018
"code.gitea.io/gitea/modules/timeutil"
2119

@@ -488,32 +486,6 @@ func (c *Comment) UnsignedLine() uint64 {
488486
return uint64(c.Line)
489487
}
490488

491-
// AsDiff returns c.Patch as *Diff
492-
func (c *Comment) AsDiff() (*Diff, error) {
493-
diff, err := ParsePatch(setting.Git.MaxGitDiffLines,
494-
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, strings.NewReader(c.Patch))
495-
if err != nil {
496-
return nil, err
497-
}
498-
if len(diff.Files) == 0 {
499-
return nil, fmt.Errorf("no file found for comment ID: %d", c.ID)
500-
}
501-
secs := diff.Files[0].Sections
502-
if len(secs) == 0 {
503-
return nil, fmt.Errorf("no sections found for comment ID: %d", c.ID)
504-
}
505-
return diff, nil
506-
}
507-
508-
// MustAsDiff executes AsDiff and logs the error instead of returning
509-
func (c *Comment) MustAsDiff() *Diff {
510-
diff, err := c.AsDiff()
511-
if err != nil {
512-
log.Warn("MustAsDiff: %v", err)
513-
}
514-
return diff
515-
}
516-
517489
// CodeCommentURL returns the url to a comment in code
518490
func (c *Comment) CodeCommentURL() string {
519491
err := c.LoadIssue()
@@ -873,59 +845,6 @@ func CreateIssueComment(doer *User, repo *Repository, issue *Issue, content stri
873845
return comment, nil
874846
}
875847

876-
// CreateCodeComment creates a plain code comment at the specified line / path
877-
func CreateCodeComment(doer *User, repo *Repository, issue *Issue, content, treePath string, line, reviewID int64) (*Comment, error) {
878-
var commitID, patch string
879-
pr, err := GetPullRequestByIssueID(issue.ID)
880-
if err != nil {
881-
return nil, fmt.Errorf("GetPullRequestByIssueID: %v", err)
882-
}
883-
if err := pr.GetBaseRepo(); err != nil {
884-
return nil, fmt.Errorf("GetHeadRepo: %v", err)
885-
}
886-
gitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath())
887-
if err != nil {
888-
return nil, fmt.Errorf("OpenRepository: %v", err)
889-
}
890-
891-
// FIXME validate treePath
892-
// Get latest commit referencing the commented line
893-
// No need for get commit for base branch changes
894-
if line > 0 {
895-
commit, err := gitRepo.LineBlame(pr.GetGitRefName(), gitRepo.Path, treePath, uint(line))
896-
if err == nil {
897-
commitID = commit.ID.String()
898-
} else if !strings.Contains(err.Error(), "exit status 128 - fatal: no such path") {
899-
return nil, fmt.Errorf("LineBlame[%s, %s, %s, %d]: %v", pr.GetGitRefName(), gitRepo.Path, treePath, line, err)
900-
}
901-
}
902-
903-
// Only fetch diff if comment is review comment
904-
if reviewID != 0 {
905-
headCommitID, err := gitRepo.GetRefCommitID(pr.GetGitRefName())
906-
if err != nil {
907-
return nil, fmt.Errorf("GetRefCommitID[%s]: %v", pr.GetGitRefName(), err)
908-
}
909-
patchBuf := new(bytes.Buffer)
910-
if err := GetRawDiffForFile(gitRepo.Path, pr.MergeBase, headCommitID, RawDiffNormal, treePath, patchBuf); err != nil {
911-
return nil, fmt.Errorf("GetRawDiffForLine[%s, %s, %s, %s]: %v", err, gitRepo.Path, pr.MergeBase, headCommitID, treePath)
912-
}
913-
patch = CutDiffAroundLine(patchBuf, int64((&Comment{Line: line}).UnsignedLine()), line < 0, setting.UI.CodeCommentLines)
914-
}
915-
return CreateComment(&CreateCommentOptions{
916-
Type: CommentTypeCode,
917-
Doer: doer,
918-
Repo: repo,
919-
Issue: issue,
920-
Content: content,
921-
LineNum: line,
922-
TreePath: treePath,
923-
CommitSHA: commitID,
924-
ReviewID: reviewID,
925-
Patch: patch,
926-
})
927-
}
928-
929848
// CreateRefComment creates a commit reference comment to issue.
930849
func CreateRefComment(doer *User, repo *Repository, issue *Issue, content, commitSHA string) error {
931850
if len(commitSHA) == 0 {

models/models.go

+5
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,8 @@ func MaxBatchInsertSize(bean interface{}) int {
250250
t := x.TableInfo(bean)
251251
return 999 / len(t.ColumnsSeq())
252252
}
253+
254+
// Count returns records number according struct's fields as database query conditions
255+
func Count(bean interface{}) (int64, error) {
256+
return x.Count(bean)
257+
}

modules/repofiles/diff.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ import (
88
"strings"
99

1010
"code.gitea.io/gitea/models"
11+
"code.gitea.io/gitea/services/gitdiff"
1112
)
1213

1314
// GetDiffPreview produces and returns diff result of a file which is not yet committed.
14-
func GetDiffPreview(repo *models.Repository, branch, treePath, content string) (*models.Diff, error) {
15+
func GetDiffPreview(repo *models.Repository, branch, treePath, content string) (*gitdiff.Diff, error) {
1516
if branch == "" {
1617
branch = repo.DefaultBranch
1718
}

modules/repofiles/diff_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"code.gitea.io/gitea/models"
1111
"code.gitea.io/gitea/modules/test"
12+
"code.gitea.io/gitea/services/gitdiff"
1213

1314
"github.com/stretchr/testify/assert"
1415
)
@@ -25,10 +26,10 @@ func TestGetDiffPreview(t *testing.T) {
2526
treePath := "README.md"
2627
content := "# repo1\n\nDescription for repo1\nthis is a new line"
2728

28-
expectedDiff := &models.Diff{
29+
expectedDiff := &gitdiff.Diff{
2930
TotalAddition: 2,
3031
TotalDeletion: 1,
31-
Files: []*models.DiffFile{
32+
Files: []*gitdiff.DiffFile{
3233
{
3334
Name: "README.md",
3435
OldName: "README.md",
@@ -42,10 +43,10 @@ func TestGetDiffPreview(t *testing.T) {
4243
IsLFSFile: false,
4344
IsRenamed: false,
4445
IsSubmodule: false,
45-
Sections: []*models.DiffSection{
46+
Sections: []*gitdiff.DiffSection{
4647
{
4748
Name: "",
48-
Lines: []*models.DiffLine{
49+
Lines: []*gitdiff.DiffLine{
4950
{
5051
LeftIdx: 0,
5152
RightIdx: 0,

modules/repofiles/temp_repo.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"code.gitea.io/gitea/modules/log"
2121
"code.gitea.io/gitea/modules/process"
2222
"code.gitea.io/gitea/modules/setting"
23+
"code.gitea.io/gitea/services/gitdiff"
2324
)
2425

2526
// 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
290291
}
291292

292293
// DiffIndex returns a Diff of the current index to the head
293-
func (t *TemporaryUploadRepository) DiffIndex() (diff *models.Diff, err error) {
294+
func (t *TemporaryUploadRepository) DiffIndex() (diff *gitdiff.Diff, err error) {
294295
timeout := 5 * time.Minute
295296
ctx, cancel := context.WithTimeout(context.Background(), timeout)
296297
defer cancel()
@@ -313,7 +314,7 @@ func (t *TemporaryUploadRepository) DiffIndex() (diff *models.Diff, err error) {
313314
pid := process.GetManager().Add(fmt.Sprintf("diffIndex [repo_path: %s]", t.repo.RepoPath()), cmd)
314315
defer process.GetManager().Remove(pid)
315316

316-
diff, err = models.ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, stdout)
317+
diff, err = gitdiff.ParsePatch(setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles, stdout)
317318
if err != nil {
318319
return nil, fmt.Errorf("ParsePatch: %v", err)
319320
}

modules/templates/helper.go

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"code.gitea.io/gitea/modules/setting"
2828
"code.gitea.io/gitea/modules/timeutil"
2929
"code.gitea.io/gitea/modules/util"
30+
"code.gitea.io/gitea/services/gitdiff"
3031

3132
"gopkg.in/editorconfig/editorconfig-core-go.v1"
3233
)
@@ -230,6 +231,7 @@ func NewFuncMap() []template.FuncMap {
230231
}
231232
return float32(n) * 100 / float32(sum)
232233
},
234+
"CommentMustAsDiff": gitdiff.CommentMustAsDiff,
233235
}}
234236
}
235237

routers/repo/commit.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"code.gitea.io/gitea/modules/git"
1717
"code.gitea.io/gitea/modules/log"
1818
"code.gitea.io/gitea/modules/setting"
19+
"code.gitea.io/gitea/services/gitdiff"
1920
)
2021

2122
const (
@@ -217,7 +218,7 @@ func Diff(ctx *context.Context) {
217218

218219
ctx.Data["CommitStatus"] = models.CalcCommitStatus(statuses)
219220

220-
diff, err := models.GetDiffCommit(models.RepoPath(userName, repoName),
221+
diff, err := gitdiff.GetDiffCommit(models.RepoPath(userName, repoName),
221222
commitID, setting.Git.MaxGitDiffLines,
222223
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
223224
if err != nil {
@@ -269,10 +270,10 @@ func Diff(ctx *context.Context) {
269270

270271
// RawDiff dumps diff results of repository in given commit ID to io.Writer
271272
func RawDiff(ctx *context.Context) {
272-
if err := models.GetRawDiff(
273+
if err := gitdiff.GetRawDiff(
273274
models.RepoPath(ctx.Repo.Owner.Name, ctx.Repo.Repository.Name),
274275
ctx.Params(":sha"),
275-
models.RawDiffType(ctx.Params(":ext")),
276+
gitdiff.RawDiffType(ctx.Params(":ext")),
276277
ctx.Resp,
277278
); err != nil {
278279
ctx.ServerError("GetRawDiff", err)

routers/repo/compare.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"code.gitea.io/gitea/modules/git"
1515
"code.gitea.io/gitea/modules/log"
1616
"code.gitea.io/gitea/modules/setting"
17+
"code.gitea.io/gitea/services/gitdiff"
1718
)
1819

1920
const (
@@ -230,7 +231,7 @@ func PrepareCompareDiff(
230231
return true
231232
}
232233

233-
diff, err := models.GetDiffRange(models.RepoPath(headUser.Name, headRepo.Name),
234+
diff, err := gitdiff.GetDiffRange(models.RepoPath(headUser.Name, headRepo.Name),
234235
compareInfo.MergeBase, headCommitID, setting.Git.MaxGitDiffLines,
235236
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
236237
if err != nil {

routers/repo/pull.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"code.gitea.io/gitea/modules/pull"
2525
"code.gitea.io/gitea/modules/setting"
2626
"code.gitea.io/gitea/modules/util"
27+
"code.gitea.io/gitea/services/gitdiff"
2728

2829
"github.com/unknwon/com"
2930
)
@@ -517,7 +518,7 @@ func ViewPullFiles(ctx *context.Context) {
517518
ctx.Data["Reponame"] = pull.HeadRepo.Name
518519
}
519520

520-
diff, err := models.GetDiffRangeWithWhitespaceBehavior(diffRepoPath,
521+
diff, err := gitdiff.GetDiffRangeWithWhitespaceBehavior(diffRepoPath,
521522
startCommitID, endCommitID, setting.Git.MaxGitDiffLines,
522523
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles,
523524
whitespaceFlags[ctx.Data["WhitespaceBehavior"].(string)])

routers/repo/pull_review.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"code.gitea.io/gitea/modules/log"
1414
"code.gitea.io/gitea/modules/notification"
1515
pull_service "code.gitea.io/gitea/modules/pull"
16+
comment_service "code.gitea.io/gitea/services/comments"
1617
)
1718

1819
// CreateCodeComment will create a code comment including an pending review if required
@@ -69,7 +70,7 @@ func CreateCodeComment(ctx *context.Context, form auth.CodeCommentForm) {
6970
review.ID = form.Reply
7071
}
7172
//FIXME check if line, commit and treepath exist
72-
comment, err := models.CreateCodeComment(
73+
comment, err := comment_service.CreateCodeComment(
7374
ctx.User,
7475
issue.Repo,
7576
issue,

services/comments/comments.go

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2019 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package comments
6+
7+
import (
8+
"bytes"
9+
"fmt"
10+
"strings"
11+
12+
"code.gitea.io/gitea/models"
13+
"code.gitea.io/gitea/modules/git"
14+
"code.gitea.io/gitea/modules/setting"
15+
"code.gitea.io/gitea/services/gitdiff"
16+
)
17+
18+
// CreateCodeComment creates a plain code comment at the specified line / path
19+
func CreateCodeComment(doer *models.User, repo *models.Repository, issue *models.Issue, content, treePath string, line, reviewID int64) (*models.Comment, error) {
20+
var commitID, patch string
21+
pr, err := models.GetPullRequestByIssueID(issue.ID)
22+
if err != nil {
23+
return nil, fmt.Errorf("GetPullRequestByIssueID: %v", err)
24+
}
25+
if err := pr.GetBaseRepo(); err != nil {
26+
return nil, fmt.Errorf("GetHeadRepo: %v", err)
27+
}
28+
gitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath())
29+
if err != nil {
30+
return nil, fmt.Errorf("OpenRepository: %v", err)
31+
}
32+
33+
// FIXME validate treePath
34+
// Get latest commit referencing the commented line
35+
// No need for get commit for base branch changes
36+
if line > 0 {
37+
commit, err := gitRepo.LineBlame(pr.GetGitRefName(), gitRepo.Path, treePath, uint(line))
38+
if err == nil {
39+
commitID = commit.ID.String()
40+
} else if !strings.Contains(err.Error(), "exit status 128 - fatal: no such path") {
41+
return nil, fmt.Errorf("LineBlame[%s, %s, %s, %d]: %v", pr.GetGitRefName(), gitRepo.Path, treePath, line, err)
42+
}
43+
}
44+
45+
// Only fetch diff if comment is review comment
46+
if reviewID != 0 {
47+
headCommitID, err := gitRepo.GetRefCommitID(pr.GetGitRefName())
48+
if err != nil {
49+
return nil, fmt.Errorf("GetRefCommitID[%s]: %v", pr.GetGitRefName(), err)
50+
}
51+
patchBuf := new(bytes.Buffer)
52+
if err := gitdiff.GetRawDiffForFile(gitRepo.Path, pr.MergeBase, headCommitID, gitdiff.RawDiffNormal, treePath, patchBuf); err != nil {
53+
return nil, fmt.Errorf("GetRawDiffForLine[%s, %s, %s, %s]: %v", err, gitRepo.Path, pr.MergeBase, headCommitID, treePath)
54+
}
55+
patch = gitdiff.CutDiffAroundLine(patchBuf, int64((&models.Comment{Line: line}).UnsignedLine()), line < 0, setting.UI.CodeCommentLines)
56+
}
57+
return models.CreateComment(&models.CreateCommentOptions{
58+
Type: models.CommentTypeCode,
59+
Doer: doer,
60+
Repo: repo,
61+
Issue: issue,
62+
Content: content,
63+
LineNum: line,
64+
TreePath: treePath,
65+
CommitSHA: commitID,
66+
ReviewID: reviewID,
67+
Patch: patch,
68+
})
69+
}

0 commit comments

Comments
 (0)