NewDiff returns a new wrapper of given git.Diff.
(oldDiff *git.Diff)
| 124 | |
| 125 | // NewDiff returns a new wrapper of given git.Diff. |
| 126 | func NewDiff(oldDiff *git.Diff) *Diff { |
| 127 | newDiff := &Diff{ |
| 128 | Diff: oldDiff, |
| 129 | Files: make([]*DiffFile, oldDiff.NumFiles()), |
| 130 | } |
| 131 | |
| 132 | // FIXME: detect encoding while parsing. |
| 133 | var buf bytes.Buffer |
| 134 | for i := range oldDiff.Files { |
| 135 | buf.Reset() |
| 136 | |
| 137 | newDiff.Files[i] = &DiffFile{ |
| 138 | DiffFile: oldDiff.Files[i], |
| 139 | Sections: make([]*DiffSection, oldDiff.Files[i].NumSections()), |
| 140 | } |
| 141 | |
| 142 | for j := range oldDiff.Files[i].Sections { |
| 143 | newDiff.Files[i].Sections[j] = &DiffSection{ |
| 144 | DiffSection: oldDiff.Files[i].Sections[j], |
| 145 | } |
| 146 | |
| 147 | for k := range newDiff.Files[i].Sections[j].Lines { |
| 148 | buf.WriteString(newDiff.Files[i].Sections[j].Lines[k].Content) |
| 149 | buf.WriteString("\n") |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | charsetLabel, err := tool.DetectEncoding(buf.Bytes()) |
| 154 | if charsetLabel != "UTF-8" && err == nil { |
| 155 | encoding, _ := charset.Lookup(charsetLabel) |
| 156 | if encoding != nil { |
| 157 | d := encoding.NewDecoder() |
| 158 | for j := range newDiff.Files[i].Sections { |
| 159 | for k := range newDiff.Files[i].Sections[j].Lines { |
| 160 | if c, _, err := transform.String(d, newDiff.Files[i].Sections[j].Lines[k].Content); err == nil { |
| 161 | newDiff.Files[i].Sections[j].Lines[k].Content = c |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | return newDiff |
| 170 | } |
| 171 | |
| 172 | // ParseDiff parses the diff from given io.Reader. |
| 173 | func ParseDiff(r io.Reader, maxFiles, maxFileLines, maxLineChars int) (*Diff, error) { |
no test coverage detected