From 6e644726d0fb064edc1ba5031bd447799338bf9c Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Tue, 22 Nov 2016 12:08:23 +0100 Subject: [PATCH 1/2] Lint git_diff.go Semi-automatic linting (don't really document things) --- models/git_diff.go | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/models/git_diff.go b/models/git_diff.go index 7d4f5db7f5..8bdce68221 100644 --- a/models/git_diff.go +++ b/models/git_diff.go @@ -28,8 +28,10 @@ import ( "golang.org/x/text/transform" ) +// DiffLineType ... type DiffLineType uint8 +// DiffLineType possible values. const ( DiffLinePlain DiffLineType = iota + 1 DiffLineAdd @@ -37,8 +39,10 @@ const ( DiffLineSection ) +// DiffFileType ... type DiffFileType uint8 +// DiffFileType possible values. const ( DiffFileAdd DiffFileType = iota + 1 DiffFileChange @@ -46,6 +50,7 @@ const ( DiffFileRename ) +// DiffLine ... type DiffLine struct { LeftIdx int RightIdx int @@ -53,10 +58,12 @@ type DiffLine struct { Content string } +// GetType ... func (d *DiffLine) GetType() int { return int(d.Type) } +// DiffSection ... type DiffSection struct { Name string Lines []*DiffLine @@ -68,6 +75,7 @@ var ( codeTagSuffix = []byte("") ) +// diffToHTML ... func diffToHTML(diffs []diffmatchpatch.Diff, lineType DiffLineType) template.HTML { buf := bytes.NewBuffer(nil) @@ -97,7 +105,7 @@ func diffToHTML(diffs []diffmatchpatch.Diff, lineType DiffLineType) template.HTM 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 { var ( difference = 0 @@ -142,11 +150,12 @@ LOOP: var diffMatchPatch = diffmatchpatch.New() +// init ... func init() { 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 { if setting.Git.DisableDiffHighlight { return template.HTML(html.EscapeString(diffLine.Content[1:])) @@ -183,6 +192,7 @@ func (diffSection *DiffSection) GetComputedInlineDiffFor(diffLine *DiffLine) tem return diffToHTML(diffRecord, diffLine.Type) } +// DiffFile ... type DiffFile struct { Name string OldName string @@ -198,26 +208,31 @@ type DiffFile struct { IsIncomplete bool } +// GetType returns type of diff file. func (diffFile *DiffFile) GetType() int { return int(diffFile.Type) } +// GetHighlightClass ... func (diffFile *DiffFile) GetHighlightClass() string { return highlight.FileNameToHighlightClass(diffFile.Name) } +// Diff ... type Diff struct { TotalAddition, TotalDeletion int Files []*DiffFile IsIncomplete bool } +// NumFiles ... func (diff *Diff) NumFiles() int { return len(diff.Files) } -const DIFF_HEAD = "diff --git " +const cmdDiffHead = "diff --git " +// ParsePatch ... // TODO: move this function to gogits/git-module func ParsePatch(maxLines, maxLineCharacteres, maxFiles int, reader io.Reader) (*Diff, error) { var ( @@ -307,19 +322,19 @@ func ParsePatch(maxLines, maxLineCharacteres, maxFiles int, reader io.Reader) (* } // Get new file. - if strings.HasPrefix(line, DIFF_HEAD) { + if strings.HasPrefix(line, cmdDiffHead) { middle := -1 // Note: In case file name is surrounded by double quotes (it happens only in git-shell). // e.g. diff --git "a/xxx" "b/xxx" - hasQuote := line[len(DIFF_HEAD)] == '"' + hasQuote := line[len(cmdDiffHead)] == '"' if hasQuote { middle = strings.Index(line, ` "b/`) } else { middle = strings.Index(line, " b/") } - beg := len(DIFF_HEAD) + beg := len(cmdDiffHead) a := line[beg+2 : middle] b := line[middle+3:] if hasQuote { @@ -405,6 +420,7 @@ func ParsePatch(maxLines, maxLineCharacteres, maxFiles int, reader io.Reader) (* return diff, nil } +// GetDiffRange ... func GetDiffRange(repoPath, beforeCommitID, afterCommitID string, maxLines, maxLineCharacteres, maxFiles int) (*Diff, error) { gitRepo, err := git.OpenRepository(repoPath) if err != nil { @@ -456,8 +472,10 @@ func GetDiffRange(repoPath, beforeCommitID, afterCommitID string, maxLines, maxL return diff, nil } +// RawDiffType ... type RawDiffType string +// RawDiffType possible values. const ( RawDiffNormal RawDiffType = "diff" RawDiffPatch RawDiffType = "patch" @@ -465,6 +483,7 @@ const ( // GetRawDiff dumps diff results of repository in given commit ID to io.Writer. // TODO: move this function to gogits/git-module +// GetRawDiff ... func GetRawDiff(repoPath, commitID string, diffType RawDiffType, writer io.Writer) error { repo, err := git.OpenRepository(repoPath) if err != nil { @@ -509,6 +528,7 @@ func GetRawDiff(repoPath, commitID string, diffType RawDiffType, writer io.Write return nil } +// GetDiffCommit ... func GetDiffCommit(repoPath, commitID string, maxLines, maxLineCharacteres, maxFiles int) (*Diff, error) { return GetDiffRange(repoPath, "", commitID, maxLines, maxLineCharacteres, maxFiles) } From 3fba29c571573ed1488d299647a4a81080ce486d Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Thu, 24 Nov 2016 09:30:08 +0100 Subject: [PATCH 2/2] Expand documentations --- models/git_diff.go | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/models/git_diff.go b/models/git_diff.go index 8bdce68221..b8f9cacec9 100644 --- a/models/git_diff.go +++ b/models/git_diff.go @@ -28,7 +28,7 @@ import ( "golang.org/x/text/transform" ) -// DiffLineType ... +// DiffLineType represents the type of a DiffLine. type DiffLineType uint8 // DiffLineType possible values. @@ -39,7 +39,7 @@ const ( DiffLineSection ) -// DiffFileType ... +// DiffFileType represents the type of a DiffFile. type DiffFileType uint8 // DiffFileType possible values. @@ -50,7 +50,7 @@ const ( DiffFileRename ) -// DiffLine ... +// DiffLine represents a line difference in a DiffSection. type DiffLine struct { LeftIdx int RightIdx int @@ -58,12 +58,12 @@ type DiffLine struct { Content string } -// GetType ... +// GetType returns the type of a DiffLine. func (d *DiffLine) GetType() int { return int(d.Type) } -// DiffSection ... +// DiffSection represents a section of a DiffFile. type DiffSection struct { Name string Lines []*DiffLine @@ -75,7 +75,6 @@ var ( codeTagSuffix = []byte("") ) -// diffToHTML ... func diffToHTML(diffs []diffmatchpatch.Diff, lineType DiffLineType) template.HTML { buf := bytes.NewBuffer(nil) @@ -150,7 +149,6 @@ LOOP: var diffMatchPatch = diffmatchpatch.New() -// init ... func init() { diffMatchPatch.DiffEditCost = 100 } @@ -192,7 +190,7 @@ func (diffSection *DiffSection) GetComputedInlineDiffFor(diffLine *DiffLine) tem return diffToHTML(diffRecord, diffLine.Type) } -// DiffFile ... +// DiffFile represents a file diff. type DiffFile struct { Name string OldName string @@ -213,26 +211,27 @@ func (diffFile *DiffFile) GetType() int { return int(diffFile.Type) } -// GetHighlightClass ... +// GetHighlightClass returns highlight class for a filename. func (diffFile *DiffFile) GetHighlightClass() string { return highlight.FileNameToHighlightClass(diffFile.Name) } -// Diff ... +// Diff represents a difference between two git trees. type Diff struct { TotalAddition, TotalDeletion int Files []*DiffFile IsIncomplete bool } -// NumFiles ... +// NumFiles returns number of files changes in a diff. func (diff *Diff) NumFiles() int { return len(diff.Files) } const cmdDiffHead = "diff --git " -// ParsePatch ... +// ParsePatch builds a Diff object from a io.Reader and some +// parameters. // TODO: move this function to gogits/git-module func ParsePatch(maxLines, maxLineCharacteres, maxFiles int, reader io.Reader) (*Diff, error) { var ( @@ -420,7 +419,9 @@ func ParsePatch(maxLines, maxLineCharacteres, maxFiles int, reader io.Reader) (* return diff, nil } -// GetDiffRange ... +// 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) { gitRepo, err := git.OpenRepository(repoPath) if err != nil { @@ -472,7 +473,7 @@ func GetDiffRange(repoPath, beforeCommitID, afterCommitID string, maxLines, maxL return diff, nil } -// RawDiffType ... +// RawDiffType type of a raw diff. type RawDiffType string // RawDiffType possible values. @@ -483,7 +484,6 @@ const ( // GetRawDiff dumps diff results of repository in given commit ID to io.Writer. // TODO: move this function to gogits/git-module -// GetRawDiff ... func GetRawDiff(repoPath, commitID string, diffType RawDiffType, writer io.Writer) error { repo, err := git.OpenRepository(repoPath) if err != nil { @@ -528,7 +528,7 @@ func GetRawDiff(repoPath, commitID string, diffType RawDiffType, writer io.Write return nil } -// GetDiffCommit ... +// GetDiffCommit builds a Diff representing the given commitID. func GetDiffCommit(repoPath, commitID string, maxLines, maxLineCharacteres, maxFiles int) (*Diff, error) { return GetDiffRange(repoPath, "", commitID, maxLines, maxLineCharacteres, maxFiles) }