summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorVladimir Vissoultchev <wqweto@gmail.com>2015-07-29 17:55:01 +0300
committerVladimir Vissoultchev <wqweto@gmail.com>2015-07-29 17:55:01 +0300
commit4917d29c121af221c3768871efabc5f99b88c4b8 (patch)
tree5ce3e28ad0c09d6f3129fb2da005b172355301de /models
parent2bb1fb8f44bcd39c1f54b33377212636ae8c9558 (diff)
downloadgitea-4917d29c121af221c3768871efabc5f99b88c4b8.tar.gz
gitea-4917d29c121af221c3768871efabc5f99b88c4b8.zip
Partial impl of git diff encoding
Diffstat (limited to 'models')
-rw-r--r--models/git_diff.go43
1 files 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
}