Merge pull request #233 from strk/git_diff-lint

Lint git_diff.go
This commit is contained in:
Thomas Boerger 2016-11-24 11:29:51 +01:00 committed by GitHub
commit e512411863

View file

@ -28,8 +28,10 @@ import (
"golang.org/x/text/transform" "golang.org/x/text/transform"
) )
// DiffLineType represents the type of a DiffLine.
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 represents the type of a DiffFile.
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 represents a line difference in a DiffSection.
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 returns the type of a DiffLine.
func (d *DiffLine) GetType() int { func (d *DiffLine) GetType() int {
return int(d.Type) return int(d.Type)
} }
// DiffSection represents a section of a DiffFile.
type DiffSection struct { type DiffSection struct {
Name string Name string
Lines []*DiffLine Lines []*DiffLine
@ -97,7 +104,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
@ -146,7 +153,7 @@ 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 +190,7 @@ func (diffSection *DiffSection) GetComputedInlineDiffFor(diffLine *DiffLine) tem
return diffToHTML(diffRecord, diffLine.Type) return diffToHTML(diffRecord, diffLine.Type)
} }
// DiffFile represents a file diff.
type DiffFile struct { type DiffFile struct {
Name string Name string
OldName string OldName string
@ -198,26 +206,32 @@ 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 returns highlight class for a filename.
func (diffFile *DiffFile) GetHighlightClass() string { func (diffFile *DiffFile) GetHighlightClass() string {
return highlight.FileNameToHighlightClass(diffFile.Name) return highlight.FileNameToHighlightClass(diffFile.Name)
} }
// Diff represents a difference between two git trees.
type Diff struct { type Diff struct {
TotalAddition, TotalDeletion int TotalAddition, TotalDeletion int
Files []*DiffFile Files []*DiffFile
IsIncomplete bool IsIncomplete bool
} }
// NumFiles returns number of files changes in a diff.
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 builds a Diff object from a io.Reader and some
// parameters.
// 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 +321,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 +419,9 @@ func ParsePatch(maxLines, maxLineCharacteres, maxFiles int, reader io.Reader) (*
return diff, nil return diff, nil
} }
// GetDiffRange builds a Diff between two commits of a repository.
// passing the empty string as beforeCommitID returns a diff from the
// parent commit.
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 +473,10 @@ func GetDiffRange(repoPath, beforeCommitID, afterCommitID string, maxLines, maxL
return diff, nil return diff, nil
} }
// RawDiffType type of a raw diff.
type RawDiffType string type RawDiffType string
// RawDiffType possible values.
const ( const (
RawDiffNormal RawDiffType = "diff" RawDiffNormal RawDiffType = "diff"
RawDiffPatch RawDiffType = "patch" RawDiffPatch RawDiffType = "patch"
@ -509,6 +528,7 @@ func GetRawDiff(repoPath, commitID string, diffType RawDiffType, writer io.Write
return nil return nil
} }
// GetDiffCommit builds a Diff representing the given commitID.
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)
} }