aboutsummaryrefslogtreecommitdiffstats
path: root/models/git_diff.go
diff options
context:
space:
mode:
authorUnknwon <joe2010xtmf@163.com>2014-12-09 02:18:25 -0500
committerUnknwon <joe2010xtmf@163.com>2014-12-09 02:18:25 -0500
commit9a1fe801e526ae1bc0a211d8a218aabcf985376d (patch)
treee40f92f1b34f81c60ffef084761b7d6cd4336aa2 /models/git_diff.go
parent6f71632e3ef6c0c818b3224e501fa66583108806 (diff)
downloadgitea-9a1fe801e526ae1bc0a211d8a218aabcf985376d.tar.gz
gitea-9a1fe801e526ae1bc0a211d8a218aabcf985376d.zip
fix #711
Diffstat (limited to 'models/git_diff.go')
-rw-r--r--models/git_diff.go25
1 files changed, 25 insertions, 0 deletions
diff --git a/models/git_diff.go b/models/git_diff.go
index e093e7ab1b..4bbe3c0e8f 100644
--- a/models/git_diff.go
+++ b/models/git_diff.go
@@ -6,6 +6,7 @@ package models
import (
"bufio"
+ "bytes"
"fmt"
"io"
"os"
@@ -15,8 +16,10 @@ import (
"github.com/Unknwon/com"
+ "github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/git"
"github.com/gogits/gogs/modules/log"
+ "github.com/gogits/gogs/modules/mahonia"
"github.com/gogits/gogs/modules/process"
)
@@ -80,6 +83,8 @@ 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.
+ buf bytes.Buffer
)
diff := &Diff{Files: make([]*DiffFile, 0)}
@@ -97,6 +102,11 @@ 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
@@ -181,6 +191,21 @@ func ParsePatch(pid int64, maxlines int, cmd *exec.Cmd, reader io.Reader) (*Diff
}
}
+ // FIXME: use first 30 lines to detect file encoding.
+ charset, err := base.DetectEncoding(buf.Bytes())
+ if charset != "utf8" && err == nil {
+ decoder := mahonia.NewDecoder(charset)
+ if decoder != nil {
+ for _, f := range diff.Files {
+ for _, sec := range f.Sections {
+ for _, l := range sec.Lines {
+ l.Content = decoder.ConvertString(l.Content)
+ }
+ }
+ }
+ }
+ }
+
return diff, nil
}