Skip to content

Commit 23806ef

Browse files
author
Juanjo Alvarez
committed
Rebased, changelog, update mysql, added doc, more tests
Signed-off-by: Juanjo Alvarez <[email protected]>
1 parent 2f8c37c commit 23806ef

File tree

5 files changed

+165
-17
lines changed

5 files changed

+165
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
263263
- COUNT expression now returns an int64 number instead of int32 https://github.com/src-d/go-mysql-server/issues/642
264264
- Dockerfile: include zero-config MySQL client https://github.com/src-d/gitbase/pull/737
265265
- uast_extract function now returns a JSON for `pos` instead of a custom format https://github.com/src-d/gitbase/pull/715
266+
- Added BLAME function.
266267

267268
#### Documentation
268269
- docs: expand optimization guide on early filtering (#837)

Gopkg.toml

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
[[constraint]]
2+
name = "gopkg.in/src-d/go-mysql-server.v0"
3+
revision = "0966c4b25d0fa2bc6560feb977ff19cc3fcf1d18"
4+
5+
[[constraint]]
6+
name = "github.com/jessevdk/go-flags"
7+
version = "1.3.0"
8+
9+
[[constraint]]
10+
name = "github.com/hashicorp/golang-lru"
11+
version = "0.5.0"
12+
13+
[[constraint]]
14+
name = "github.com/stretchr/testify"
15+
version = "1.1.4"
16+
17+
[[constraint]]
18+
name = "gopkg.in/src-d/go-git.v4"
19+
source = "github.com/src-d/go-git"
20+
revision = "0106daba16494c33d330168e38c8a610b7a9b32c"
21+
22+
[[constraint]]
23+
name = "gopkg.in/src-d/go-git-fixtures.v3"
24+
version = "3.0.0"
25+
26+
[[override]]
27+
name = "golang.org/x/sys"
28+
source = "github.com/golang/sys"
29+
revision = "f0d5e33068cb57c22a181f5df0ffda885309eb5a"
30+
31+
[[override]]
32+
name = "golang.org/x/crypto"
33+
source = "github.com/golang/crypto"
34+
35+
[[override]]
36+
name = "golang.org/x/text"
37+
source = "github.com/golang/text"
38+
39+
[[override]]
40+
name = "golang.org/x/net"
41+
source = "github.com/golang/net"
42+
43+
[[override]]
44+
name = "google.golang.org/grpc"
45+
version = "1.14.0"
46+
47+
[[override]]
48+
name = "github.com/satori/go.uuid"
49+
version = "1.2.0"
50+
51+
[[constraint]]
52+
name = "gopkg.in/src-d/enry.v1"
53+
version = "1.7.2"
54+
55+
[[constraint]]
56+
name = "gopkg.in/bblfsh/client-go.v3"
57+
version = "3.2.0"
58+
59+
[[override]]
60+
name = "gopkg.in/bblfsh/sdk.v1"
61+
version = "1.16.1"
62+
63+
[[constraint]]
64+
name = "gopkg.in/bblfsh/sdk.v2"
65+
version = "2.14.2"
66+
67+
[[constraint]]
68+
name = "github.com/uber/jaeger-client-go"
69+
version = "^2.7.0"
70+
71+
[[override]]
72+
name = "github.com/pilosa/pilosa"
73+
version = "1.3.0"
74+
75+
[[override]]
76+
name = "github.com/pilosa/go-pilosa"
77+
version = "1.3.0"
78+
79+
[[override]]
80+
name = "github.com/src-d/go-oniguruma"
81+
version = "1.0.0"
82+
83+
[[constraint]]
84+
name = "gopkg.in/src-d/go-vitess.v1"
85+
version = "1.6.0"
86+
87+
[[constraint]]
88+
name = "github.com/hhatto/gocloc"
89+
revision = "c5379f7427f9a9a5bec11accdf8a2ef7fa414d3e"
90+
91+
[prune]
92+
[[prune.project]]
93+
name = "gopkg.in/src-d/go-mysql-server.v0"
94+
go-tests = true
95+
unused-packages = true
96+
[[prune.project]]
97+
name = "gopkg.in/src-d/go-git.v4"
98+
go-tests = true
99+
non-go = true
100+
unused-packages = true
101+
[[prune.project]]
102+
name = "gopkg.in/src-d/enry.v1"
103+
go-tests = true
104+
non-go = true
105+
unused-packages = true
106+
[[prune.project]]
107+
name = "gopkg.in/bblfsh/sdk.v2"
108+
go-tests = true
109+
non-go = true
110+
unused-packages = true

docs/using-gitbase/functions.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ To make some common tasks easier for the user, there are some functions to inter
1919
|`uast_children(blob) blob`| returns a flattened array of the children UAST nodes from each one of the UAST nodes in the given array. |
2020
|`loc(path, blob) json`| returns a JSON map, containing the lines of code of a file, separated in three categories: Code, Blank and Comment lines. |
2121
|`version() text`| returns the gitbase version in the following format `8.0.11-{GITBASE_VERSION}` for compatibility with MySQL versioning. |
22+
||||||| merged common ancestors
23+
|`is_remote(reference_name)bool`| check if the given reference name is from a remote one |
24+
|`is_tag(reference_name)bool`| check if the given reference name is a tag |
25+
|`language(path, [blob])text`| gets the language of a file given its path and the optional content of the file |
26+
|`uast(blob, [lang, [xpath]]) blob`| returns a node array of UAST nodes in semantic mode |
27+
|`uast_mode(mode, blob, lang) blob`| returns a node array of UAST nodes specifying its language and mode (semantic, annotated or native) |
28+
|`uast_xpath(blob, xpath) blob`| performs an XPath query over the given UAST nodes |
29+
|`uast_extract(blob, key) text array`| extracts information identified by the given key from the uast nodes |
30+
|`uast_children(blob) blob`| returns a flattened array of the children UAST nodes from each one of the UAST nodes in the given array |
31+
|`loc(path, blob) json`| returns a JSON map, containing the lines of code of a file, separated in three categories: Code, Blank and Comment lines |
32+
2233
## Standard functions
2334

2435
These are all functions that are available because they are implemented in `go-mysql-server`, used by gitbase.
@@ -28,6 +39,7 @@ These are all functions that are available because they are implemented in `go-m
2839
|:-------------|:-------------------------------------------------------------------------------------------------------------------------------|
2940
|`ARRAY_LENGTH(json)`|if the json representation is an array, this function returns its size.|
3041
|`AVG(expr)`| returns the average value of expr in all rows.|
42+
|`BLAME(expr)`|Returns a list, as a json array, of lines changes and authorship.|
3143
|`CEIL(number)`| returns the smallest integer value that is greater than or equal to `number`.|
3244
|`CEILING(number)`| returns the smallest integer value that is greater than or equal to `number`.|
3345
|`CHAR_LENGTH(str)`| returns the length of the string in characters.|

internal/function/blame_test.go

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@ package function
22

33
import (
44
"context"
5-
"fmt"
6-
"gopkg.in/src-d/go-mysql-server.v0/sql/expression"
7-
85
"github.com/src-d/gitbase"
96
"github.com/stretchr/testify/require"
107
fixtures "gopkg.in/src-d/go-git-fixtures.v3"
118
"gopkg.in/src-d/go-git.v4/plumbing/cache"
129
"gopkg.in/src-d/go-mysql-server.v0/sql"
10+
"gopkg.in/src-d/go-mysql-server.v0/sql/expression"
1311
"testing"
1412
)
1513

@@ -21,44 +19,70 @@ func TestBlameEval(t *testing.T) {
2119
}()
2220

2321
path := fixtures.ByTag("worktree").One().Worktree().Root()
24-
2522
pool := gitbase.NewRepositoryPool(cache.DefaultMaxSize)
2623
require.NoError(t, pool.AddGitWithID("worktree", path))
2724

2825
session := gitbase.NewSession(pool)
2926
ctx := sql.NewContext(context.TODO(), sql.WithSession(session))
3027

3128
testCases := []struct {
32-
name string
33-
repo sql.Expression
34-
commit sql.Expression
35-
row sql.Row
36-
expected BlameLine
29+
name string
30+
repo sql.Expression
31+
commit sql.Expression
32+
row sql.Row
33+
expected BlameLine
34+
testedLine int
35+
lineCount int
3736
}{
3837
{
39-
name: "init commit",
40-
repo: expression.NewGetField(0, sql.Text, "repository_id", false),
41-
commit: expression.NewGetField(1, sql.Text, "commit_hash", false),
42-
row: sql.NewRow("worktree", "b029517f6300c2da0f4b651b8642506cd6aaf45d"),
38+
name: "init commit",
39+
repo: expression.NewGetField(0, sql.Text, "repository_id", false),
40+
commit: expression.NewGetField(1, sql.Text, "commit_hash", false),
41+
row: sql.NewRow("worktree", "b029517f6300c2da0f4b651b8642506cd6aaf45d"),
42+
testedLine: 0,
43+
lineCount: 12,
4344
expected: BlameLine{
4445
"b029517f6300c2da0f4b651b8642506cd6aaf45d",
4546
".gitignore",
4647
0,
4748
4849
"*.class",
4950
},
50-
}}
51+
},
52+
{
53+
name: "changelog",
54+
repo: expression.NewGetField(0, sql.Text, "repository_id", false),
55+
commit: expression.NewGetField(1, sql.Text, "commit_hash", false),
56+
row: sql.NewRow("worktree", "b8e471f58bcbca63b07bda20e428190409c2db47"),
57+
testedLine: 0,
58+
lineCount: 1,
59+
expected: BlameLine{
60+
"b8e471f58bcbca63b07bda20e428190409c2db47",
61+
"CHANGELOG",
62+
0,
63+
64+
"Creating changelog",
65+
},
66+
},
67+
}
5168

5269
for _, tc := range testCases {
5370
t.Run(tc.name, func(t *testing.T) {
5471
blame := NewBlame(tc.repo, tc.commit)
5572
results, err := blame.Eval(ctx, tc.row)
5673
require.NoError(t, err)
57-
for _, r := range results.([]BlameLine) {
58-
fmt.Println(r)
74+
lineCount := 0
75+
for i, r := range results.([]BlameLine) {
76+
if r.File != tc.expected.File {
77+
continue
78+
}
79+
lineCount++
80+
if i != tc.testedLine {
81+
continue
82+
}
5983
require.EqualValues(t, tc.expected, r)
60-
break
6184
}
85+
require.Equal(t, tc.lineCount, lineCount)
6286
})
6387
}
6488
}

internal/function/registry.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ var Functions = []sql.Function{
1717
sql.Function1{Name: "uast_children", Fn: NewUASTChildren},
1818
sql.Function1{Name: "uast_imports", Fn: NewUASTImports},
1919
sql.Function1{Name: "is_vendor", Fn: NewIsVendor},
20+
sql.Function2{Name: "blame", Fn: NewBlame},
2021
}

0 commit comments

Comments
 (0)