Skip to content

Commit ef8bc48

Browse files
authored
Feature/commit file stats (#868)
Feature/commit file stats
2 parents c1b2316 + bc26937 commit ef8bc48

13 files changed

+969
-448
lines changed

CHANGELOG.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
### Added
1010

11+
- Added `commit_file_stats` function.
1112
- Added documentation about `commit_stats`.
1213

1314
### Changed
1415

1516
- internal/function: gracefully handle errors in commit_stats.
17+
- Removed vendor folder.
1618

1719
### Fixed
1820

1921
- internal/function: take into account if repository is resolved in commit_stats ([#863](https://github.com/src-d/gitbase/pull/863))
20-
21-
### Changed
22-
23-
- Removed vendor folder.
22+
- internal/function: `Files` field in `commit_stats` contains now proper results.
2423

2524
## [0.21.0-beta1] - 2019-06-12
2625

docs/using-gitbase/functions.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ To make some common tasks easier for the user, there are some functions to inter
66

77
| Name | Description |
88
|:-------------|:-------------------------------------------------------------------------------------------------------------------------------|
9-
|`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|
9+
|`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.|
10+
|`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.|
1011
|`is_remote(reference_name)bool`| check if the given reference name is from a remote one |
1112
|`is_tag(reference_name)bool`| check if the given reference name is a tag |
1213
|`is_vendor(file_path)bool`| check if the given file name is a vendored file |

internal/commitstats/commit.go

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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

Comments
 (0)