Skip to content

Commit

Permalink
internal/function: implement commit_file_stats
Browse files Browse the repository at this point in the history
Signed-off-by: Miguel Molina <miguel@erizocosmi.co>
  • Loading branch information
erizocosmico committed Jun 14, 2019
1 parent e5ca74a commit 758ea6a
Show file tree
Hide file tree
Showing 13 changed files with 968 additions and 444 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Added `commit_file_stats` function.
- Added documentation about `commit_stats`.

### Changed
Expand All @@ -17,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- internal/function: take into account if repository is resolved in commit_stats ([#863](https://github.com/src-d/gitbase/pull/863))
- internal/function: `Files` field in `commit_stats` contains now proper results.

### Changed

Expand Down
3 changes: 2 additions & 1 deletion docs/using-gitbase/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ To make some common tasks easier for the user, there are some functions to inter

| Name | Description |
|:-------------|:-------------------------------------------------------------------------------------------------------------------------------|
|`commit_stats(repository_id, [from_commit_hash], to_commit_hash)`|returns the stats between two commits for a repository. If from is empty, it will compare the given `to_commit_hash` with its parent commit|
|`commit_stats(repository_id, [from_commit_hash], to_commit_hash)`|returns the stats between two commits for a repository. If from is empty, it will compare the given `to_commit_hash` with its parent commit. Vendored files stats are not included in the result of this function.|
|`commit_file_stats(repository_id, [from_commit_hash], to_commit_hash)`|returns an array with the stats of each file in `to_commit_hash` since the given `from_commit_hash`. If from is not given, the parent commit will be used. Vendored files stats are not included in the result of this function.|
|`is_remote(reference_name)bool`| check if the given reference name is from a remote one |
|`is_tag(reference_name)bool`| check if the given reference name is a tag |
|`is_vendor(file_path)bool`| check if the given file name is a vendored file |
Expand Down
60 changes: 60 additions & 0 deletions internal/commitstats/commit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package commitstats

import (
"fmt"

"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing/object"
)

// CommitStats represents the stats for a commit.
type CommitStats struct {
// Files add/modified/removed by this commit.
Files int
// Code stats of the code lines.
Code KindStats
// Comment stats of the comment lines.
Comment KindStats
// Blank stats of the blank lines.
Blank KindStats
// Other stats of files that are not from a recognized or format language.
Other KindStats
// Total the sum of the previous stats.
Total KindStats
}

func (s *CommitStats) String() string {
return fmt.Sprintf("Code (+%d/-%d)\nComment (+%d/-%d)\nBlank (+%d/-%d)\nOther (+%d/-%d)\nTotal (+%d/-%d)\nFiles (%d)\n",
s.Code.Additions, s.Code.Deletions,
s.Comment.Additions, s.Comment.Deletions,
s.Blank.Additions, s.Blank.Deletions,
s.Other.Additions, s.Other.Deletions,
s.Total.Additions, s.Total.Deletions,
s.Files,
)
}

// Calculate calculates the CommitStats for from commit to another.
// if from is nil the first parent is used, if the commit is orphan the stats
// are compared against a empty commit.
func Calculate(r *git.Repository, from, to *object.Commit) (*CommitStats, error) {
fs, err := CalculateByFile(r, from, to)
if err != nil {
return nil, err
}

return commitStatsFromCommitFileStats(fs), nil
}

func commitStatsFromCommitFileStats(fs []CommitFileStats) *CommitStats {
var s CommitStats
for _, f := range fs {
s.Blank.Add(f.Blank)
s.Comment.Add(f.Comment)
s.Code.Add(f.Code)
s.Other.Add(f.Other)
s.Total.Add(f.Total)
s.Files++
}
return &s
}
Loading

0 comments on commit 758ea6a

Please sign in to comment.