mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-28 20:26:19 +01:00
Lint git_diff.go
Semi-automatic linting (don't really document things)
This commit is contained in:
parent
6ed7f269f1
commit
6e644726d0
1 changed files with 26 additions and 6 deletions
|
@ -28,8 +28,10 @@ import (
|
||||||
"golang.org/x/text/transform"
|
"golang.org/x/text/transform"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// DiffLineType ...
|
||||||
type DiffLineType uint8
|
type DiffLineType uint8
|
||||||
|
|
||||||
|
// DiffLineType possible values.
|
||||||
const (
|
const (
|
||||||
DiffLinePlain DiffLineType = iota + 1
|
DiffLinePlain DiffLineType = iota + 1
|
||||||
DiffLineAdd
|
DiffLineAdd
|
||||||
|
@ -37,8 +39,10 @@ const (
|
||||||
DiffLineSection
|
DiffLineSection
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// DiffFileType ...
|
||||||
type DiffFileType uint8
|
type DiffFileType uint8
|
||||||
|
|
||||||
|
// DiffFileType possible values.
|
||||||
const (
|
const (
|
||||||
DiffFileAdd DiffFileType = iota + 1
|
DiffFileAdd DiffFileType = iota + 1
|
||||||
DiffFileChange
|
DiffFileChange
|
||||||
|
@ -46,6 +50,7 @@ const (
|
||||||
DiffFileRename
|
DiffFileRename
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// DiffLine ...
|
||||||
type DiffLine struct {
|
type DiffLine struct {
|
||||||
LeftIdx int
|
LeftIdx int
|
||||||
RightIdx int
|
RightIdx int
|
||||||
|
@ -53,10 +58,12 @@ type DiffLine struct {
|
||||||
Content string
|
Content string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetType ...
|
||||||
func (d *DiffLine) GetType() int {
|
func (d *DiffLine) GetType() int {
|
||||||
return int(d.Type)
|
return int(d.Type)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DiffSection ...
|
||||||
type DiffSection struct {
|
type DiffSection struct {
|
||||||
Name string
|
Name string
|
||||||
Lines []*DiffLine
|
Lines []*DiffLine
|
||||||
|
@ -68,6 +75,7 @@ var (
|
||||||
codeTagSuffix = []byte("</span>")
|
codeTagSuffix = []byte("</span>")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// diffToHTML ...
|
||||||
func diffToHTML(diffs []diffmatchpatch.Diff, lineType DiffLineType) template.HTML {
|
func diffToHTML(diffs []diffmatchpatch.Diff, lineType DiffLineType) template.HTML {
|
||||||
buf := bytes.NewBuffer(nil)
|
buf := bytes.NewBuffer(nil)
|
||||||
|
|
||||||
|
@ -97,7 +105,7 @@ func diffToHTML(diffs []diffmatchpatch.Diff, lineType DiffLineType) template.HTM
|
||||||
return template.HTML(buf.Bytes())
|
return template.HTML(buf.Bytes())
|
||||||
}
|
}
|
||||||
|
|
||||||
// get an specific line by type (add or del) and file line number
|
// GetLine gets a specific line by type (add or del) and file line number
|
||||||
func (diffSection *DiffSection) GetLine(lineType DiffLineType, idx int) *DiffLine {
|
func (diffSection *DiffSection) GetLine(lineType DiffLineType, idx int) *DiffLine {
|
||||||
var (
|
var (
|
||||||
difference = 0
|
difference = 0
|
||||||
|
@ -142,11 +150,12 @@ LOOP:
|
||||||
|
|
||||||
var diffMatchPatch = diffmatchpatch.New()
|
var diffMatchPatch = diffmatchpatch.New()
|
||||||
|
|
||||||
|
// init ...
|
||||||
func init() {
|
func init() {
|
||||||
diffMatchPatch.DiffEditCost = 100
|
diffMatchPatch.DiffEditCost = 100
|
||||||
}
|
}
|
||||||
|
|
||||||
// computes inline diff for the given line
|
// GetComputedInlineDiffFor computes inline diff for the given line.
|
||||||
func (diffSection *DiffSection) GetComputedInlineDiffFor(diffLine *DiffLine) template.HTML {
|
func (diffSection *DiffSection) GetComputedInlineDiffFor(diffLine *DiffLine) template.HTML {
|
||||||
if setting.Git.DisableDiffHighlight {
|
if setting.Git.DisableDiffHighlight {
|
||||||
return template.HTML(html.EscapeString(diffLine.Content[1:]))
|
return template.HTML(html.EscapeString(diffLine.Content[1:]))
|
||||||
|
@ -183,6 +192,7 @@ func (diffSection *DiffSection) GetComputedInlineDiffFor(diffLine *DiffLine) tem
|
||||||
return diffToHTML(diffRecord, diffLine.Type)
|
return diffToHTML(diffRecord, diffLine.Type)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DiffFile ...
|
||||||
type DiffFile struct {
|
type DiffFile struct {
|
||||||
Name string
|
Name string
|
||||||
OldName string
|
OldName string
|
||||||
|
@ -198,26 +208,31 @@ type DiffFile struct {
|
||||||
IsIncomplete bool
|
IsIncomplete bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetType returns type of diff file.
|
||||||
func (diffFile *DiffFile) GetType() int {
|
func (diffFile *DiffFile) GetType() int {
|
||||||
return int(diffFile.Type)
|
return int(diffFile.Type)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetHighlightClass ...
|
||||||
func (diffFile *DiffFile) GetHighlightClass() string {
|
func (diffFile *DiffFile) GetHighlightClass() string {
|
||||||
return highlight.FileNameToHighlightClass(diffFile.Name)
|
return highlight.FileNameToHighlightClass(diffFile.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Diff ...
|
||||||
type Diff struct {
|
type Diff struct {
|
||||||
TotalAddition, TotalDeletion int
|
TotalAddition, TotalDeletion int
|
||||||
Files []*DiffFile
|
Files []*DiffFile
|
||||||
IsIncomplete bool
|
IsIncomplete bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NumFiles ...
|
||||||
func (diff *Diff) NumFiles() int {
|
func (diff *Diff) NumFiles() int {
|
||||||
return len(diff.Files)
|
return len(diff.Files)
|
||||||
}
|
}
|
||||||
|
|
||||||
const DIFF_HEAD = "diff --git "
|
const cmdDiffHead = "diff --git "
|
||||||
|
|
||||||
|
// ParsePatch ...
|
||||||
// TODO: move this function to gogits/git-module
|
// TODO: move this function to gogits/git-module
|
||||||
func ParsePatch(maxLines, maxLineCharacteres, maxFiles int, reader io.Reader) (*Diff, error) {
|
func ParsePatch(maxLines, maxLineCharacteres, maxFiles int, reader io.Reader) (*Diff, error) {
|
||||||
var (
|
var (
|
||||||
|
@ -307,19 +322,19 @@ func ParsePatch(maxLines, maxLineCharacteres, maxFiles int, reader io.Reader) (*
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get new file.
|
// Get new file.
|
||||||
if strings.HasPrefix(line, DIFF_HEAD) {
|
if strings.HasPrefix(line, cmdDiffHead) {
|
||||||
middle := -1
|
middle := -1
|
||||||
|
|
||||||
// Note: In case file name is surrounded by double quotes (it happens only in git-shell).
|
// Note: In case file name is surrounded by double quotes (it happens only in git-shell).
|
||||||
// e.g. diff --git "a/xxx" "b/xxx"
|
// e.g. diff --git "a/xxx" "b/xxx"
|
||||||
hasQuote := line[len(DIFF_HEAD)] == '"'
|
hasQuote := line[len(cmdDiffHead)] == '"'
|
||||||
if hasQuote {
|
if hasQuote {
|
||||||
middle = strings.Index(line, ` "b/`)
|
middle = strings.Index(line, ` "b/`)
|
||||||
} else {
|
} else {
|
||||||
middle = strings.Index(line, " b/")
|
middle = strings.Index(line, " b/")
|
||||||
}
|
}
|
||||||
|
|
||||||
beg := len(DIFF_HEAD)
|
beg := len(cmdDiffHead)
|
||||||
a := line[beg+2 : middle]
|
a := line[beg+2 : middle]
|
||||||
b := line[middle+3:]
|
b := line[middle+3:]
|
||||||
if hasQuote {
|
if hasQuote {
|
||||||
|
@ -405,6 +420,7 @@ func ParsePatch(maxLines, maxLineCharacteres, maxFiles int, reader io.Reader) (*
|
||||||
return diff, nil
|
return diff, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetDiffRange ...
|
||||||
func GetDiffRange(repoPath, beforeCommitID, afterCommitID string, maxLines, maxLineCharacteres, maxFiles int) (*Diff, error) {
|
func GetDiffRange(repoPath, beforeCommitID, afterCommitID string, maxLines, maxLineCharacteres, maxFiles int) (*Diff, error) {
|
||||||
gitRepo, err := git.OpenRepository(repoPath)
|
gitRepo, err := git.OpenRepository(repoPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -456,8 +472,10 @@ func GetDiffRange(repoPath, beforeCommitID, afterCommitID string, maxLines, maxL
|
||||||
return diff, nil
|
return diff, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RawDiffType ...
|
||||||
type RawDiffType string
|
type RawDiffType string
|
||||||
|
|
||||||
|
// RawDiffType possible values.
|
||||||
const (
|
const (
|
||||||
RawDiffNormal RawDiffType = "diff"
|
RawDiffNormal RawDiffType = "diff"
|
||||||
RawDiffPatch RawDiffType = "patch"
|
RawDiffPatch RawDiffType = "patch"
|
||||||
|
@ -465,6 +483,7 @@ const (
|
||||||
|
|
||||||
// GetRawDiff dumps diff results of repository in given commit ID to io.Writer.
|
// GetRawDiff dumps diff results of repository in given commit ID to io.Writer.
|
||||||
// TODO: move this function to gogits/git-module
|
// TODO: move this function to gogits/git-module
|
||||||
|
// GetRawDiff ...
|
||||||
func GetRawDiff(repoPath, commitID string, diffType RawDiffType, writer io.Writer) error {
|
func GetRawDiff(repoPath, commitID string, diffType RawDiffType, writer io.Writer) error {
|
||||||
repo, err := git.OpenRepository(repoPath)
|
repo, err := git.OpenRepository(repoPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -509,6 +528,7 @@ func GetRawDiff(repoPath, commitID string, diffType RawDiffType, writer io.Write
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetDiffCommit ...
|
||||||
func GetDiffCommit(repoPath, commitID string, maxLines, maxLineCharacteres, maxFiles int) (*Diff, error) {
|
func GetDiffCommit(repoPath, commitID string, maxLines, maxLineCharacteres, maxFiles int) (*Diff, error) {
|
||||||
return GetDiffRange(repoPath, "", commitID, maxLines, maxLineCharacteres, maxFiles)
|
return GetDiffRange(repoPath, "", commitID, maxLines, maxLineCharacteres, maxFiles)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue