|
| 1 | +package commitstats |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "gopkg.in/src-d/go-git.v4" |
| 7 | + "gopkg.in/src-d/go-git.v4/plumbing/object" |
| 8 | +) |
| 9 | + |
| 10 | +// CommitStats represents the stats for a commit. |
| 11 | +type CommitStats struct { |
| 12 | + // Files add/modified/removed by this commit. |
| 13 | + Files int |
| 14 | + // Code stats of the code lines. |
| 15 | + Code KindStats |
| 16 | + // Comment stats of the comment lines. |
| 17 | + Comment KindStats |
| 18 | + // Blank stats of the blank lines. |
| 19 | + Blank KindStats |
| 20 | + // Other stats of files that are not from a recognized or format language. |
| 21 | + Other KindStats |
| 22 | + // Total the sum of the previous stats. |
| 23 | + Total KindStats |
| 24 | +} |
| 25 | + |
| 26 | +func (s *CommitStats) String() string { |
| 27 | + return fmt.Sprintf("Code (+%d/-%d)\nComment (+%d/-%d)\nBlank (+%d/-%d)\nOther (+%d/-%d)\nTotal (+%d/-%d)\nFiles (%d)\n", |
| 28 | + s.Code.Additions, s.Code.Deletions, |
| 29 | + s.Comment.Additions, s.Comment.Deletions, |
| 30 | + s.Blank.Additions, s.Blank.Deletions, |
| 31 | + s.Other.Additions, s.Other.Deletions, |
| 32 | + s.Total.Additions, s.Total.Deletions, |
| 33 | + s.Files, |
| 34 | + ) |
| 35 | +} |
| 36 | + |
| 37 | +// Calculate calculates the CommitStats for from commit to another. |
| 38 | +// if from is nil the first parent is used, if the commit is orphan the stats |
| 39 | +// are compared against a empty commit. |
| 40 | +func Calculate(r *git.Repository, from, to *object.Commit) (*CommitStats, error) { |
| 41 | + fs, err := CalculateByFile(r, from, to) |
| 42 | + if err != nil { |
| 43 | + return nil, err |
| 44 | + } |
| 45 | + |
| 46 | + return commitStatsFromCommitFileStats(fs), nil |
| 47 | +} |
| 48 | + |
| 49 | +func commitStatsFromCommitFileStats(fs []CommitFileStats) *CommitStats { |
| 50 | + var s CommitStats |
| 51 | + for _, f := range fs { |
| 52 | + s.Blank.Add(f.Blank) |
| 53 | + s.Comment.Add(f.Comment) |
| 54 | + s.Code.Add(f.Code) |
| 55 | + s.Other.Add(f.Other) |
| 56 | + s.Total.Add(f.Total) |
| 57 | + s.Files++ |
| 58 | + } |
| 59 | + return &s |
| 60 | +} |
0 commit comments