diff options
author | lunnyxiao <xiaolunwen@gmail.com> | 2014-09-17 12:03:03 +0800 |
---|---|---|
committer | lunnyxiao <xiaolunwen@gmail.com> | 2014-09-17 12:03:03 +0800 |
commit | ed84adb679f3de70b2bffaead20a87711c38ee3a (patch) | |
tree | ff303000a735b237c8f45bbcc8018da185beb114 /models/git_diff.go | |
parent | efb68a0a96574a334a3ec791c0a7f2bc6b96d006 (diff) | |
download | gitea-ed84adb679f3de70b2bffaead20a87711c38ee3a.tar.gz gitea-ed84adb679f3de70b2bffaead20a87711c38ee3a.zip |
toutf8 improved & add max git diff lines
Diffstat (limited to 'models/git_diff.go')
-rw-r--r-- | models/git_diff.go | 34 |
1 files changed, 22 insertions, 12 deletions
diff --git a/models/git_diff.go b/models/git_diff.go index bf7a9cd575..e093e7ab1b 100644 --- a/models/git_diff.go +++ b/models/git_diff.go @@ -70,7 +70,7 @@ func (diff *Diff) NumFiles() int { const DIFF_HEAD = "diff --git " -func ParsePatch(pid int64, cmd *exec.Cmd, reader io.Reader) (*Diff, error) { +func ParsePatch(pid int64, maxlines int, cmd *exec.Cmd, reader io.Reader) (*Diff, error) { scanner := bufio.NewScanner(reader) var ( curFile *DiffFile @@ -79,6 +79,7 @@ func ParsePatch(pid int64, cmd *exec.Cmd, reader io.Reader) (*Diff, error) { } leftLine, rightLine int + isTooLong bool ) diff := &Diff{Files: make([]*DiffFile, 0)} @@ -90,16 +91,17 @@ func ParsePatch(pid int64, cmd *exec.Cmd, reader io.Reader) (*Diff, error) { continue } + if line == "" { + continue + } + i = i + 1 - // Diff data too large. - if i == 5000 { + // Diff data too large, we only show the first about maxlines lines + if i == maxlines { + isTooLong = true log.Warn("Diff data too large") - return &Diff{}, nil - } - - if line == "" { - continue + //return &Diff{}, nil } switch { @@ -110,6 +112,10 @@ func ParsePatch(pid int64, cmd *exec.Cmd, reader io.Reader) (*Diff, error) { curSection.Lines = append(curSection.Lines, diffLine) continue case line[0] == '@': + if isTooLong { + return diff, nil + } + curSection = &DiffSection{} curFile.Sections = append(curFile.Sections, curSection) ss := strings.Split(line, "@@") @@ -143,6 +149,10 @@ func ParsePatch(pid int64, cmd *exec.Cmd, reader io.Reader) (*Diff, error) { // Get new file. if strings.HasPrefix(line, DIFF_HEAD) { + if isTooLong { + return diff, nil + } + fs := strings.Split(line[len(DIFF_HEAD):], " ") a := fs[0] @@ -174,7 +184,7 @@ func ParsePatch(pid int64, cmd *exec.Cmd, reader io.Reader) (*Diff, error) { return diff, nil } -func GetDiffRange(repoPath, beforeCommitId string, afterCommitId string) (*Diff, error) { +func GetDiffRange(repoPath, beforeCommitId string, afterCommitId string, maxlines int) (*Diff, error) { repo, err := git.OpenRepository(repoPath) if err != nil { return nil, err @@ -228,9 +238,9 @@ func GetDiffRange(repoPath, beforeCommitId string, afterCommitId string) (*Diff, } }() - return ParsePatch(pid, cmd, rd) + return ParsePatch(pid, maxlines, cmd, rd) } -func GetDiffCommit(repoPath, commitId string) (*Diff, error) { - return GetDiffRange(repoPath, "", commitId) +func GetDiffCommit(repoPath, commitId string, maxlines int) (*Diff, error) { + return GetDiffRange(repoPath, "", commitId, maxlines) } |