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
15 changed files with 164 additions and 111 deletions
|
@ -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,
|
||||
}}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue