diff options
author | Unknwon <u@gogs.io> | 2019-10-24 01:51:46 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-10-24 01:51:46 -0700 |
commit | 01c8df01ec0608f1f25b2f1444adabb98fa5ee8a (patch) | |
tree | f8a7e5dd8d2a8c51e1ce2cabb9d33571a93314dd /models/git_diff.go | |
parent | 613139e7bef81d3573e7988a47eb6765f3de347a (diff) |
internal: move packages under this directory (#5836)
* Rename pkg -> internal
* Rename routes -> route
* Move route -> internal/route
* Rename models -> db
* Move db -> internal/db
* Fix route2 -> route
* Move cmd -> internal/cmd
* Bump version
Diffstat (limited to 'models/git_diff.go')
-rw-r--r-- | models/git_diff.go | 194 |
1 files changed, 0 insertions, 194 deletions
diff --git a/models/git_diff.go b/models/git_diff.go deleted file mode 100644 index f6bdea2d..00000000 --- a/models/git_diff.go +++ /dev/null @@ -1,194 +0,0 @@ -// Copyright 2014 The Gogs 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 - -import ( - "bytes" - "fmt" - "html" - "html/template" - "io" - - "github.com/sergi/go-diff/diffmatchpatch" - "golang.org/x/net/html/charset" - "golang.org/x/text/transform" - - "github.com/gogs/git-module" - - "gogs.io/gogs/pkg/setting" - "gogs.io/gogs/pkg/template/highlight" - "gogs.io/gogs/pkg/tool" -) - -type DiffSection struct { - *git.DiffSection -} - -var ( - addedCodePrefix = []byte("<span class=\"added-code\">") - removedCodePrefix = []byte("<span class=\"removed-code\">") - codeTagSuffix = []byte("</span>") -) - -func diffToHTML(diffs []diffmatchpatch.Diff, lineType git.DiffLineType) template.HTML { - buf := bytes.NewBuffer(nil) - - // Reproduce signs which are cutted for inline diff before. - switch lineType { - case git.DIFF_LINE_ADD: - buf.WriteByte('+') - case git.DIFF_LINE_DEL: - buf.WriteByte('-') - } - - for i := range diffs { - switch { - case diffs[i].Type == diffmatchpatch.DiffInsert && lineType == git.DIFF_LINE_ADD: - buf.Write(addedCodePrefix) - buf.WriteString(html.EscapeString(diffs[i].Text)) - buf.Write(codeTagSuffix) - case diffs[i].Type == diffmatchpatch.DiffDelete && lineType == git.DIFF_LINE_DEL: - buf.Write(removedCodePrefix) - buf.WriteString(html.EscapeString(diffs[i].Text)) - buf.Write(codeTagSuffix) - case diffs[i].Type == diffmatchpatch.DiffEqual: - buf.WriteString(html.EscapeString(diffs[i].Text)) - } - } - - return template.HTML(buf.Bytes()) -} - -var diffMatchPatch = diffmatchpatch.New() - -func init() { - diffMatchPatch.DiffEditCost = 100 -} - -// ComputedInlineDiffFor computes inline diff for the given line. -func (diffSection *DiffSection) ComputedInlineDiffFor(diffLine *git.DiffLine) template.HTML { - if setting.Git.DisableDiffHighlight { - return template.HTML(html.EscapeString(diffLine.Content[1:])) - } - var ( - compareDiffLine *git.DiffLine - diff1 string - diff2 string - ) - - // try to find equivalent diff line. ignore, otherwise - switch diffLine.Type { - case git.DIFF_LINE_ADD: - compareDiffLine = diffSection.Line(git.DIFF_LINE_DEL, diffLine.RightIdx) - if compareDiffLine == nil { - return template.HTML(html.EscapeString(diffLine.Content)) - } - diff1 = compareDiffLine.Content - diff2 = diffLine.Content - case git.DIFF_LINE_DEL: - compareDiffLine = diffSection.Line(git.DIFF_LINE_ADD, diffLine.LeftIdx) - if compareDiffLine == nil { - return template.HTML(html.EscapeString(diffLine.Content)) - } - diff1 = diffLine.Content - diff2 = compareDiffLine.Content - default: - return template.HTML(html.EscapeString(diffLine.Content)) - } - - diffRecord := diffMatchPatch.DiffMain(diff1[1:], diff2[1:], true) - diffRecord = diffMatchPatch.DiffCleanupEfficiency(diffRecord) - - return diffToHTML(diffRecord, diffLine.Type) -} - -type DiffFile struct { - *git.DiffFile - Sections []*DiffSection -} - -func (diffFile *DiffFile) HighlightClass() string { - return highlight.FileNameToHighlightClass(diffFile.Name) -} - -type Diff struct { - *git.Diff - Files []*DiffFile -} - -func NewDiff(gitDiff *git.Diff) *Diff { - diff := &Diff{ - Diff: gitDiff, - Files: make([]*DiffFile, gitDiff.NumFiles()), - } - - // FIXME: detect encoding while parsing. - var buf bytes.Buffer - for i := range gitDiff.Files { - buf.Reset() - - diff.Files[i] = &DiffFile{ - DiffFile: gitDiff.Files[i], - Sections: make([]*DiffSection, gitDiff.Files[i].NumSections()), - } - - for j := range gitDiff.Files[i].Sections { - diff.Files[i].Sections[j] = &DiffSection{ - DiffSection: gitDiff.Files[i].Sections[j], - } - - for k := range diff.Files[i].Sections[j].Lines { - buf.WriteString(diff.Files[i].Sections[j].Lines[k].Content) - buf.WriteString("\n") - } - } - - charsetLabel, err := tool.DetectEncoding(buf.Bytes()) - if charsetLabel != "UTF-8" && err == nil { - encoding, _ := charset.Lookup(charsetLabel) - if encoding != nil { - d := encoding.NewDecoder() - for j := range diff.Files[i].Sections { - for k := range diff.Files[i].Sections[j].Lines { - if c, _, err := transform.String(d, diff.Files[i].Sections[j].Lines[k].Content); err == nil { - diff.Files[i].Sections[j].Lines[k].Content = c - } - } - } - } - } - } - - return diff -} - -func ParsePatch(maxLines, maxLineCharacteres, maxFiles int, reader io.Reader) (*Diff, error) { - done := make(chan error) - var gitDiff *git.Diff - go func() { - gitDiff = git.ParsePatch(done, maxLines, maxLineCharacteres, maxFiles, reader) - }() - - if err := <-done; err != nil { - return nil, fmt.Errorf("ParsePatch: %v", err) - } - return NewDiff(gitDiff), nil -} - -func GetDiffRange(repoPath, beforeCommitID, afterCommitID string, maxLines, maxLineCharacteres, maxFiles int) (*Diff, error) { - gitDiff, err := git.GetDiffRange(repoPath, beforeCommitID, afterCommitID, maxLines, maxLineCharacteres, maxFiles) - if err != nil { - return nil, fmt.Errorf("GetDiffRange: %v", err) - } - return NewDiff(gitDiff), nil -} - -func GetDiffCommit(repoPath, commitID string, maxLines, maxLineCharacteres, maxFiles int) (*Diff, error) { - gitDiff, err := git.GetDiffCommit(repoPath, commitID, maxLines, maxLineCharacteres, maxFiles) - if err != nil { - return nil, fmt.Errorf("GetDiffCommit: %v", err) - } - return NewDiff(gitDiff), nil -} |