Skip to content

Commit 9732c68

Browse files
author
Juanjo Alvarez
committed
Feedback from review
- Re-add IsNullable for Blame - Update docs/using-gitbase/functions.md - Sorted and removed duplicated from docs/using-gitbase/functions.md - gofmt + goimports + revert some odd Golang changes to go.mod Signed-off-by: Juanjo Alvarez <[email protected]>
1 parent ea429f5 commit 9732c68

File tree

5 files changed

+72
-33
lines changed

5 files changed

+72
-33
lines changed

docs/using-gitbase/functions.md

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,20 @@ 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) json`|returns the stats between two commits for a repository. If `from_commit_hash` 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. This function is more thoroughly explained later in this document.|
9+
|`blame(repository, commit)`|Returns an array of lines changes and authorship. |
1010
|`commit_file_stats(repository_id, [from_commit_hash], to_commit_hash) json array`|returns an array with the stats of each file in `to_commit_hash` since the given `from_commit_hash`. If `from_commit_hash` is not given, the parent commit will be used. Vendored files stats are not included in the result of this function. This function is more thoroughly explained later in this document.|
11-
|`is_tag(reference_name)bool`| checks if the given reference name is a tag. |
12-
|`is_vendor(file_path)bool`| checks if the given file name is a vendored file. |
13-
|`language(path, [blob])text`| gets the language of a file given its path and the optional content of the file. |
14-
|`uast(blob, [lang, [xpath]]) blob`| returns a node array of UAST nodes in semantic mode. |
15-
|`uast_mode(mode, blob, lang) blob`| returns a node array of UAST nodes specifying its language and mode (semantic, annotated or native). |
16-
|`uast_xpath(blob, xpath) blob`| performs an XPath query over the given UAST nodes. |
17-
|`uast_extract(blob, key) text array`| extracts information identified by the given key from the uast nodes. |
18-
|`uast_children(blob) blob`| returns a flattened array of the children UAST nodes from each one of the UAST nodes in the given array. |
19-
|`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. |
20-
|`version() text`| returns the gitbase version in the following format `8.0.11-{GITBASE_VERSION}` for compatibility with MySQL versioning. |
11+
|`commit_stats(repository_id, [from_commit_hash], to_commit_hash) json`|returns the stats between two commits for a repository. If `from_commit_hash` 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. This function is more thoroughly explained later in this document.|
2112
|`is_remote(reference_name)bool`| checks if the given reference name is from a remote one. |
22-
|`is_tag(reference_name)bool`| check if the given reference name is a tag. |
13+
|`is_tag(reference_name)bool`| checks if the given reference name is a tag. |
14+
|`is_vendor(file_path)bool`| checks if the given file name is a vendored file. |
2315
|`language(path, [blob])text`| gets the language of a file given its path and the optional content of the file. |
16+
|`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. |
2417
|`uast(blob, [lang, [xpath]]) blob`| returns a node array of UAST nodes in semantic mode. |
18+
|`uast_children(blob) blob`| returns a flattened array of the children UAST nodes from each one of the UAST nodes in the given array. |
19+
|`uast_extract(blob, key) text array`| extracts information identified by the given key from the uast nodes. |
2520
|`uast_mode(mode, blob, lang) blob`| returns a node array of UAST nodes specifying its language and mode (semantic, annotated or native). |
2621
|`uast_xpath(blob, xpath) blob`| performs an XPath query over the given UAST nodes. |
27-
|`uast_extract(blob, key) text array`| extracts information identified by the given key from the uast nodes. |
28-
|`uast_children(blob) blob`| returns a flattened array of the children UAST nodes from each one of the UAST nodes in the given array. |
29-
|`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. |
22+
|`version() text`| returns the gitbase version in the following format `8.0.11-{GITBASE_VERSION}` for compatibility with MySQL versioning. |
3023

3124
## Standard functions
3225

@@ -37,7 +30,6 @@ These are all functions that are available because they are implemented in `go-m
3730
|:-------------|:-------------------------------------------------------------------------------------------------------------------------------|
3831
|`ARRAY_LENGTH(json)`|if the json representation is an array, this function returns its size.|
3932
|`AVG(expr)`| returns the average value of expr in all rows.|
40-
|`BLAME(repository, commit)`|Returns an array, of lines changes and authorship.|
4133
|`CEIL(number)`| returns the smallest integer value that is greater than or equal to `number`.|
4234
|`CEILING(number)`| returns the smallest integer value that is greater than or equal to `number`.|
4335
|`CHAR_LENGTH(str)`| returns the length of the string in characters.|
@@ -46,10 +38,10 @@ These are all functions that are available because they are implemented in `go-m
4638
|`CONCAT_WS(sep, ...)`| concatenates any group of fields into a single string. The first argument is the separator for the rest of the arguments. The separator is added between the strings to be concatenated. The separator can be a string, as can the rest of the arguments. If the separator is NULL, the result is NULL.|
4739
|`CONNECTION_ID()`| returns the current connection ID.|
4840
|`COUNT(expr)`| returns a count of the number of non-NULL values of expr in the rows retrieved by a SELECT statement.|
41+
|`DATE(date)`| returns the date part of the given `date`.|
4942
|`DATE_ADD(date, interval)`| adds the interval to the given `date`.|
5043
|`DATE_SUB(date, interval)`| subtracts the interval from the given `date`.|
5144
|`DAY(date)`| is a synonym for DAYOFMONTH().|
52-
|`DATE(date)`| returns the date part of the given `date`.|
5345
|`DAYOFMONTH(date)`| returns the day of the month (0-31).|
5446
|`DAYOFWEEK(date)`| returns the day of the week of the given `date`.|
5547
|`DAYOFYEAR(date)`| returns the day of the year of the given `date`.|

go.mod

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ require (
1616
github.com/prometheus/client_golang v1.0.0
1717
github.com/sirupsen/logrus v1.4.2
1818
github.com/src-d/enry/v2 v2.0.0
19-
github.com/src-d/go-borges v0.1.1
19+
github.com/src-d/go-borges v0.1.3
2020
github.com/src-d/go-git v4.7.0+incompatible
2121
github.com/src-d/go-git-fixtures v3.5.1-0.20190605154830-57f3972b0248+incompatible
2222
github.com/src-d/go-mysql-server v0.4.1-0.20191001122120-b09e8c1c8ab9
@@ -27,10 +27,9 @@ require (
2727
go.uber.org/atomic v1.4.0 // indirect
2828
golang.org/x/net v0.0.0-20190619014844-b5b0513f8c1b // indirect
2929
golang.org/x/sync v0.0.0-20190423024810-112230192c58 // indirect
30-
golang.org/x/sys v0.0.0-20190618155005-516e3c20635f // indirect
3130
google.golang.org/grpc v1.20.1
32-
gopkg.in/src-d/go-billy-siva.v4 v4.5.1
33-
gopkg.in/src-d/go-billy.v4 v4.3.0
31+
gopkg.in/src-d/go-billy-siva.v4 v4.6.0
32+
gopkg.in/src-d/go-billy.v4 v4.3.2
3433
gopkg.in/src-d/go-errors.v1 v1.0.0
3534
gopkg.in/src-d/go-git-fixtures.v3 v3.5.0
3635
gopkg.in/src-d/go-git.v4 v4.12.0

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,13 @@ github.com/containerd/continuity v0.0.0-20180712174259-0377f7d76720/go.mod h1:GL
5151
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
5252
github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
5353
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
54+
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
5455
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5556
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
5657
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5758
github.com/docker/go-connections v0.3.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec=
5859
github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
60+
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
5961
github.com/emirpasic/gods v1.9.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o=
6062
github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg=
6163
github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o=
@@ -147,6 +149,7 @@ github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFB
147149
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
148150
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
149151
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
152+
github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw=
150153
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
151154
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
152155
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
@@ -243,6 +246,7 @@ github.com/src-d/gcfg v1.4.0 h1:xXbNR5AlLSA315x2UO+fTSSAXCDf+Ar38/6oyGbDKQ4=
243246
github.com/src-d/gcfg v1.4.0/go.mod h1:p/UMsR43ujA89BJY9duynAwIpvqEujIH/jFlfL7jWoI=
244247
github.com/src-d/go-borges v0.1.1 h1:URkX6ycDzodt5n6mAF54cdjmmAMSWIyuL12ga/E5/sM=
245248
github.com/src-d/go-borges v0.1.1/go.mod h1:q/ufK0FXUS0ngH1Crd1TyirdtUM4dl/5ky2Oxiaw4YA=
249+
github.com/src-d/go-borges v0.1.3/go.mod h1:2lbHENELjpD+6bTOOnrnGEpVPMmO+wBGtYwzvQdr/Zc=
246250
github.com/src-d/go-git v4.7.0+incompatible h1:IYSSnbAHeKmsfbQFi9ozbid+KNh0bKjlorMfQehQbcE=
247251
github.com/src-d/go-git v4.7.0+incompatible/go.mod h1:1bQciz+hn0jzPQNsYj0hDFZHLJBdV7gXE2mWhC7EkFk=
248252
github.com/src-d/go-git-fixtures v3.5.1-0.20190605154830-57f3972b0248+incompatible h1:A5bKevhs9C//Nh8QV0J+1KphEaIa25cDe1DTs/yPxDI=
@@ -330,6 +334,7 @@ golang.org/x/sys v0.0.0-20190520201301-c432e742b0af/go.mod h1:h1NjWce9XRLGQEsW7w
330334
golang.org/x/sys v0.0.0-20190609082536-301114b31cce/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
331335
golang.org/x/sys v0.0.0-20190618155005-516e3c20635f h1:dHNZYIYdq2QuU6w73vZ/DzesPbVlZVYZTtTZmrnsbQ8=
332336
golang.org/x/sys v0.0.0-20190618155005-516e3c20635f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
337+
golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
333338
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
334339
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
335340
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
@@ -363,9 +368,11 @@ gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMy
363368
gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2/go.mod h1:Xk6kEKp8OKb+X14hQBKWaSkCsqBpgog8nAV2xsGOxlo=
364369
gopkg.in/src-d/go-billy-siva.v4 v4.5.1 h1:+UdpGGmJjANhXwg6TCcTVbACUqsbtX19QvJ9AdeX4ts=
365370
gopkg.in/src-d/go-billy-siva.v4 v4.5.1/go.mod h1:4wKeCzOCSsdyFeM5+58M6ObU6FM+lZT12p7zm7A+9n0=
371+
gopkg.in/src-d/go-billy-siva.v4 v4.6.0/go.mod h1:EcgzPxovlWGD+lZFFriUleL3EVZ/SPs6CH2FOE/eooI=
366372
gopkg.in/src-d/go-billy.v4 v4.2.1/go.mod h1:tm33zBoOwxjYHZIE+OV8bxTWFMJLrconzFMd38aARFk=
367373
gopkg.in/src-d/go-billy.v4 v4.3.0 h1:KtlZ4c1OWbIs4jCv5ZXrTqG8EQocr0g/d4DjNg70aek=
368374
gopkg.in/src-d/go-billy.v4 v4.3.0/go.mod h1:tm33zBoOwxjYHZIE+OV8bxTWFMJLrconzFMd38aARFk=
375+
gopkg.in/src-d/go-billy.v4 v4.3.2/go.mod h1:nDjArDMp+XMs1aFAESLRjfGSgfvoYN0hDfzEk0GjC98=
369376
gopkg.in/src-d/go-errors.v1 v1.0.0 h1:cooGdZnCjYbeS1zb1s6pVAAimTdKceRrpn7aKOnNIfc=
370377
gopkg.in/src-d/go-errors.v1 v1.0.0/go.mod h1:q1cBlomlw2FnDBDNGlnh6X0jPihy+QxZfMMNxPCbdYg=
371378
gopkg.in/src-d/go-git-fixtures.v3 v3.1.1/go.mod h1:dLBcvytrw/TYZsNTWCnkNF2DSIlzWYqTe3rJR56Ac7g=
@@ -377,6 +384,7 @@ gopkg.in/src-d/go-git.v4 v4.12.0/go.mod h1:zjlNnzc1Wjn43v3Mtii7RVxiReNP0fIu9npcX
377384
gopkg.in/src-d/go-log.v1 v1.0.2/go.mod h1:GN34hKP0g305ysm2/hctJ0Y8nWP3zxXXJ8GFabTyABE=
378385
gopkg.in/src-d/go-siva.v1 v1.5.0 h1:WowvbZTlz0SPoV7WNCGktPSi2yRK78HPyXl7wYqDeHE=
379386
gopkg.in/src-d/go-siva.v1 v1.5.0/go.mod h1:tk1jnIXawd/PTlRNWdr5V5lC0PttNJmu1fv7wt7IZlw=
387+
gopkg.in/src-d/go-siva.v1 v1.7.0/go.mod h1:ChxMHSRkICHZ9IbTlG3ihkuG7gc2RZPsIYh7OaXYvic=
380388
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
381389
gopkg.in/toqueteos/substring.v1 v1.0.2 h1:urLqCeMm6x/eTuQa1oZerNw8N1KNOIp5hD5kGL7lFsE=
382390
gopkg.in/toqueteos/substring.v1 v1.0.2/go.mod h1:Eb2Z1UYehlVK8LYW2WBVR2rwbujsz3aX8XDrM1vbNew=

internal/function/blame.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package function
22

33
import (
44
"fmt"
5+
56
"github.com/src-d/gitbase"
67
"github.com/src-d/go-mysql-server/sql"
78
"gopkg.in/src-d/go-git.v4"
@@ -33,6 +34,11 @@ func (g *BlameGenerator) loadNewFile() error {
3334
if err != nil {
3435
return err
3536
}
37+
38+
if len(result.Lines) == 0 {
39+
return g.loadNewFile()
40+
}
41+
3642
g.lines = result.Lines
3743
g.curLine = 0
3844
return nil
@@ -110,8 +116,8 @@ func (b *Blame) Children() []sql.Expression {
110116
}
111117

112118
// IsNullable implements the Expression interface.
113-
func (*Blame) IsNullable() bool {
114-
return false
119+
func (b *Blame) IsNullable() bool {
120+
return b.repo.IsNullable() || (b.commit.IsNullable())
115121
}
116122

117123
// Resolved implements the Expression interface.
@@ -126,12 +132,14 @@ func (b *Blame) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
126132

127133
repo, err := b.resolveRepo(ctx, row)
128134
if err != nil {
129-
return nil, err
135+
ctx.Warn(0, err.Error())
136+
return nil, nil
130137
}
131138

132139
commit, err := b.resolveCommit(ctx, repo, row)
133140
if err != nil {
134-
return nil, err
141+
ctx.Warn(0, err.Error())
142+
return nil, nil
135143
}
136144

137145
fIter, err := commit.Files()

internal/function/blame_test.go

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package function
22

33
import (
44
"context"
5+
"testing"
6+
57
"github.com/src-d/gitbase"
68
"github.com/src-d/go-mysql-server/sql"
79
"github.com/src-d/go-mysql-server/sql/expression"
810
"github.com/stretchr/testify/require"
911
fixtures "gopkg.in/src-d/go-git-fixtures.v3"
10-
"testing"
1112
)
1213

1314
func TestBlameEval(t *testing.T) {
@@ -24,13 +25,14 @@ func TestBlameEval(t *testing.T) {
2425
ctx := sql.NewContext(context.TODO(), sql.WithSession(session))
2526

2627
testCases := []struct {
27-
name string
28-
repo sql.Expression
29-
commit sql.Expression
30-
row sql.Row
31-
expected BlameLine
32-
testedLine int
33-
lineCount int
28+
name string
29+
repo sql.Expression
30+
commit sql.Expression
31+
row sql.Row
32+
expected BlameLine
33+
expectedNil bool
34+
testedLine int
35+
lineCount int
3436
}{
3537
{
3638
name: "init commit",
@@ -46,6 +48,7 @@ func TestBlameEval(t *testing.T) {
4648
4749
"*.class",
4850
},
51+
expectedNil: false,
4952
},
5053
{
5154
name: "changelog",
@@ -61,6 +64,27 @@ func TestBlameEval(t *testing.T) {
6164
6265
"Initial changelog",
6366
},
67+
expectedNil: false,
68+
},
69+
{
70+
name: "no repo",
71+
repo: expression.NewGetField(0, sql.Text, "repository_id", false),
72+
commit: expression.NewGetField(1, sql.Text, "commit_hash", false),
73+
row: sql.NewRow("foo", "bar"),
74+
testedLine: 0,
75+
lineCount: 1,
76+
expected: BlameLine{},
77+
expectedNil: true,
78+
},
79+
{
80+
name: "no commit",
81+
repo: expression.NewGetField(0, sql.Text, "repository_id", false),
82+
commit: expression.NewGetField(1, sql.Text, "commit_hash", false),
83+
row: sql.NewRow("worktree", "foo"),
84+
testedLine: 0,
85+
lineCount: 1,
86+
expected: BlameLine{},
87+
expectedNil: true,
6488
},
6589
}
6690

@@ -69,6 +93,14 @@ func TestBlameEval(t *testing.T) {
6993
blame := NewBlame(tc.repo, tc.commit)
7094
blameGen, err := blame.Eval(ctx, tc.row)
7195
require.NoError(t, err)
96+
97+
if tc.expectedNil {
98+
require.Nil(t, blameGen)
99+
return
100+
} else {
101+
require.NotNil(t, blameGen)
102+
}
103+
72104
bg := blameGen.(*BlameGenerator)
73105
defer bg.Close()
74106

0 commit comments

Comments
 (0)