From 4917d29c121af221c3768871efabc5f99b88c4b8 Mon Sep 17 00:00:00 2001 From: Vladimir Vissoultchev Date: Wed, 29 Jul 2015 17:55:01 +0300 Subject: [PATCH] Partial impl of git diff encoding --- models/git_diff.go | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/models/git_diff.go b/models/git_diff.go index 0ff2c3a728..9681b3f465 100644 --- a/models/git_diff.go +++ b/models/git_diff.go @@ -87,7 +87,7 @@ func ParsePatch(pid int64, maxlines int, cmd *exec.Cmd, reader io.Reader) (*Diff leftLine, rightLine int isTooLong bool - // FIXME: use first 30 lines to detect file encoding. Should use cache in the future. + // FIXME: Should use cache in the future. buf bytes.Buffer ) @@ -106,16 +106,10 @@ func ParsePatch(pid int64, maxlines int, cmd *exec.Cmd, reader io.Reader) (*Diff i = i + 1 - // FIXME: use first 30 lines to detect file encoding. - if i <= 30 { - buf.WriteString(line) - } - // 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 } switch { @@ -127,7 +121,7 @@ func ParsePatch(pid int64, maxlines int, cmd *exec.Cmd, reader io.Reader) (*Diff continue case line[0] == '@': if isTooLong { - return diff, nil + break } curSection = &DiffSection{} @@ -137,9 +131,14 @@ func ParsePatch(pid int64, maxlines int, cmd *exec.Cmd, reader io.Reader) (*Diff curSection.Lines = append(curSection.Lines, diffLine) // Parse line number. - ranges := strings.Split(ss[len(ss)-2][1:], " ") + ranges := strings.Split(ss[1][1:], " ") leftLine, _ = com.StrTo(strings.Split(ranges[0], ",")[0][1:]).Int() - rightLine, _ = com.StrTo(strings.Split(ranges[1], ",")[0]).Int() + if len(ranges) > 1 { + rightLine, _ = com.StrTo(strings.Split(ranges[1], ",")[0]).Int() + } else { + log.Warn("Parse line number failed: %v", line) + rightLine = leftLine + } continue case line[0] == '+': curFile.Addition++ @@ -164,7 +163,7 @@ func ParsePatch(pid int64, maxlines int, cmd *exec.Cmd, reader io.Reader) (*Diff // Get new file. if strings.HasPrefix(line, DIFF_HEAD) { if isTooLong { - return diff, nil + break } beg := len(DIFF_HEAD) @@ -201,14 +200,19 @@ func ParsePatch(pid int64, maxlines int, cmd *exec.Cmd, reader io.Reader) (*Diff } } - // FIXME: use first 30 lines to detect file encoding. - charsetLabel, err := base.DetectEncoding(buf.Bytes()) - if charsetLabel != "utf8" && err == nil { - encoding, _ := charset.Lookup(charsetLabel) - - if encoding != nil { - d := encoding.NewDecoder() - for _, f := range diff.Files { + for _, f := range diff.Files { + buf.Reset() + for _, sec := range f.Sections { + for _, l := range sec.Lines { + buf.WriteString(l.Content) + buf.WriteString("\n") + } + } + charsetLabel, err := base.DetectEncoding(buf.Bytes()) + if charsetLabel != "UTF-8" && err == nil { + encoding, _ := charset.Lookup(charsetLabel) + if encoding != nil { + d := encoding.NewDecoder() for _, sec := range f.Sections { for _, l := range sec.Lines { if c, _, err := transform.String(d, l.Content); err == nil { @@ -219,7 +223,6 @@ func ParsePatch(pid int64, maxlines int, cmd *exec.Cmd, reader io.Reader) (*Diff } } } - return diff, nil }