Skip to content

Commit c1b2316

Browse files
authored
improve commit stats (#864)
improve commit stats
2 parents 5e1da86 + e5ca74a commit c1b2316

File tree

4 files changed

+62
-12
lines changed

4 files changed

+62
-12
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
### Added
10+
11+
- Added documentation about `commit_stats`.
12+
13+
### Changed
14+
15+
- internal/function: gracefully handle errors in commit_stats.
16+
917
### Fixed
1018

1119
- internal/function: take into account if repository is resolved in commit_stats ([#863](https://github.com/src-d/gitbase/pull/863))

docs/using-gitbase/functions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ 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|
910
|`is_remote(reference_name)bool`| check if the given reference name is from a remote one |
1011
|`is_tag(reference_name)bool`| check if the given reference name is a tag |
1112
|`is_vendor(file_path)bool`| check if the given file name is a vendored file |

internal/function/commit_stats.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package function
33
import (
44
"fmt"
55

6+
"github.com/sirupsen/logrus"
67
"github.com/src-d/gitbase"
78
"github.com/src-d/gitbase/internal/commitstats"
89

@@ -81,7 +82,7 @@ func (f *CommitStats) Children() []sql.Expression {
8182

8283
// IsNullable implements the Expression interface.
8384
func (*CommitStats) IsNullable() bool {
84-
return false
85+
return true
8586
}
8687

8788
// Resolved implements the Expression interface.
@@ -98,20 +99,39 @@ func (f *CommitStats) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
9899

99100
r, err := f.resolveRepo(ctx, row)
100101
if err != nil {
101-
return nil, err
102+
ctx.Warn(0, "commit_stats: unable to resolve repository")
103+
logrus.WithField("err", err).Error("commit_stats: unable to resolve repository")
104+
return nil, nil
102105
}
103106

107+
log := logrus.WithField("repository", r)
108+
104109
to, err := f.resolveCommit(ctx, r, row, f.To)
105110
if err != nil {
106-
return nil, err
111+
ctx.Warn(0, "commit_stats: unable to resolve 'to' commit of repository: %v", r)
112+
log.WithField("err", err).Error("commit_stats: unable to resolve 'to' commit")
113+
return nil, nil
107114
}
108115

109116
from, err := f.resolveCommit(ctx, r, row, f.From)
110117
if err != nil {
111-
return nil, err
118+
ctx.Warn(0, "commit_stats: unable to resolve 'from' commit of repository: %v", r)
119+
log.WithField("err", err).Error("commit_stats: unable to resolve from commit")
120+
return nil, nil
121+
}
122+
123+
result, err := commitstats.Calculate(r.Repository, from, to)
124+
if err != nil {
125+
ctx.Warn(0, "commit_stats: unable to calculate for repository: %v, from: %v, to: %v", r, from, to)
126+
log.WithFields(logrus.Fields{
127+
"err": err,
128+
"from": from,
129+
"to": to,
130+
}).Error("commit_stats: unable to calculate")
131+
return nil, nil
112132
}
113133

114-
return commitstats.Calculate(r.Repository, from, to)
134+
return result, nil
115135
}
116136

117137
func (f *CommitStats) resolveRepo(ctx *sql.Context, r sql.Row) (*gitbase.Repository, error) {

internal/function/commit_stats_test.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import (
88
"github.com/src-d/gitbase/internal/commitstats"
99
"github.com/stretchr/testify/require"
1010

11-
"gopkg.in/src-d/go-git-fixtures.v3"
12-
"gopkg.in/src-d/go-git.v4/plumbing/cache"
1311
"github.com/src-d/go-mysql-server/sql"
1412
"github.com/src-d/go-mysql-server/sql/expression"
13+
fixtures "gopkg.in/src-d/go-git-fixtures.v3"
14+
"gopkg.in/src-d/go-git.v4/plumbing/cache"
1515
)
1616

1717
func TestCommitStatsEval(t *testing.T) {
@@ -34,7 +34,7 @@ func TestCommitStatsEval(t *testing.T) {
3434
from sql.Expression
3535
to sql.Expression
3636
row sql.Row
37-
expected *commitstats.CommitStats
37+
expected interface{}
3838
}{
3939
{
4040
name: "init commit",
@@ -48,6 +48,30 @@ func TestCommitStatsEval(t *testing.T) {
4848
Total: commitstats.KindStats{Additions: 22, Deletions: 0},
4949
},
5050
},
51+
{
52+
name: "invalid repository id",
53+
repo: expression.NewGetField(0, sql.Text, "repository_id", false),
54+
from: nil,
55+
to: expression.NewGetField(1, sql.Text, "commit_hash", false),
56+
row: sql.NewRow("foobar", "b029517f6300c2da0f4b651b8642506cd6aaf45d"),
57+
expected: nil,
58+
},
59+
{
60+
name: "invalid to",
61+
repo: expression.NewGetField(0, sql.Text, "repository_id", false),
62+
from: nil,
63+
to: expression.NewGetField(1, sql.Text, "commit_hash", false),
64+
row: sql.NewRow("worktree", "foobar"),
65+
expected: nil,
66+
},
67+
{
68+
name: "invalid from",
69+
repo: expression.NewGetField(0, sql.Text, "repository_id", false),
70+
from: expression.NewGetField(2, sql.Text, "commit_hash", false),
71+
to: expression.NewGetField(1, sql.Text, "commit_hash", false),
72+
row: sql.NewRow("worktree", "b029517f6300c2da0f4b651b8642506cd6aaf45d", "foobar"),
73+
expected: nil,
74+
},
5175
}
5276

5377
for _, tc := range testCases {
@@ -58,10 +82,7 @@ func TestCommitStatsEval(t *testing.T) {
5882
result, err := diff.Eval(ctx, tc.row)
5983
require.NoError(t, err)
6084

61-
stats, ok := result.(*commitstats.CommitStats)
62-
require.True(t, ok)
63-
64-
require.EqualValues(t, tc.expected, stats)
85+
require.EqualValues(t, tc.expected, result)
6586
})
6687
}
6788
}

0 commit comments

Comments
 (0)