| 287 | } |
| 288 | |
| 289 | func getFileStatsFromFilePatches(filePatches []fdiff.FilePatch) FileStats { |
| 290 | var fileStats FileStats |
| 291 | |
| 292 | for _, fp := range filePatches { |
| 293 | // ignore empty patches (binary files, submodule refs updates) |
| 294 | if len(fp.Chunks()) == 0 { |
| 295 | continue |
| 296 | } |
| 297 | |
| 298 | cs := FileStat{} |
| 299 | from, to := fp.Files() |
| 300 | if from == nil { |
| 301 | // New File is created. |
| 302 | cs.Name = to.Path() |
| 303 | } else if to == nil { |
| 304 | // File is deleted. |
| 305 | cs.Name = from.Path() |
| 306 | } else if from.Path() != to.Path() { |
| 307 | // File is renamed. |
| 308 | cs.Name = fmt.Sprintf("%s => %s", from.Path(), to.Path()) |
| 309 | } else { |
| 310 | cs.Name = from.Path() |
| 311 | } |
| 312 | |
| 313 | for _, chunk := range fp.Chunks() { |
| 314 | s := chunk.Content() |
| 315 | if len(s) == 0 { |
| 316 | continue |
| 317 | } |
| 318 | |
| 319 | switch chunk.Type() { |
| 320 | case fdiff.Add: |
| 321 | cs.Addition += strings.Count(s, "\n") |
| 322 | if s[len(s)-1] != '\n' { |
| 323 | cs.Addition++ |
| 324 | } |
| 325 | case fdiff.Delete: |
| 326 | cs.Deletion += strings.Count(s, "\n") |
| 327 | if s[len(s)-1] != '\n' { |
| 328 | cs.Deletion++ |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | fileStats = append(fileStats, cs) |
| 334 | } |
| 335 | |
| 336 | return fileStats |
| 337 | } |