Skip to content

Commit d9b57ef

Browse files
authored
Merge branch 'master' into patch-1
2 parents 5576380 + da85514 commit d9b57ef

File tree

4 files changed

+62
-44
lines changed

4 files changed

+62
-44
lines changed

docs/using-gitbase/examples.md

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,30 @@ FROM refs
1818
WHERE ref_name = 'HEAD';
1919
```
2020

21-
## First commit on HEAD history for all repositories
21+
## Files in the first commit on HEAD history for all repositories
2222

2323
```sql
2424
SELECT file_path,
2525
ref_commits.repository_id
2626
FROM commit_files
2727
NATURAL JOIN ref_commits
28-
WHERE ref_commits.ref_name = 'HEAD'
29-
AND ref_commits.history_index = 0;
28+
WHERE ref_name = 'HEAD'
29+
AND history_index = 0;
3030
```
3131

3232
## Commits that appear in more than one reference
3333

3434
```sql
35-
SELECT *
36-
FROM
37-
(SELECT COUNT(c.commit_hash) AS num,
38-
c.commit_hash
39-
FROM ref_commits r
40-
NATURAL JOIN commits c
41-
GROUP BY c.commit_hash) t
42-
WHERE num > 1;
35+
SELECT COUNT(commit_hash) AS num, commit_hash
36+
FROM ref_commits
37+
GROUP BY commit_hash
38+
HAVING num > 1;
4339
```
4440

4541
## Get the number of blobs per HEAD commit
4642

4743
```sql
48-
SELECT COUNT(commit_hash),
44+
SELECT COUNT(commit_blob),
4945
commit_hash
5046
FROM ref_commits
5147
NATURAL JOIN commits
@@ -85,8 +81,8 @@ SELECT
8581
SUM(JSON_EXTRACT(LOC(file_path, blob_content), '$.Comments')) as comments,
8682
SUM(JSON_EXTRACT(LOC(file_path, blob_content), '$.Blanks')) as blanks,
8783
COUNT(1) as files
88-
FROM commit_files
89-
NATURAL JOIN refs
84+
FROM refs
85+
NATURAL JOIN commit_files
9086
NATURAL JOIN blobs
9187
WHERE ref_name='HEAD'
9288
GROUP BY lang;
@@ -98,13 +94,13 @@ GROUP BY lang;
9894
SELECT file_path,
9995
repository_id,
10096
blob_content
101-
FROM files
97+
FROM ref_commits
10298
NATURAL JOIN commit_files
103-
NATURAL JOIN ref_commits
99+
NATURAL JOIN files
104100
WHERE ref_name = 'HEAD'
105-
AND ref_commits.history_index BETWEEN 0 AND 5
106-
AND is_binary(blob_content) = FALSE
107-
AND files.file_path NOT REGEXP '^vendor.*'
101+
AND history_index BETWEEN 0 AND 5
102+
AND NOT IS_BINARY(blob_content)
103+
AND NOT IS_VENDOR(file_path)
108104
AND (blob_content REGEXP '(?i)facebook.*[\'\\"][0-9a-f]{32}[\'\\"]'
109105
OR blob_content REGEXP '(?i)twitter.*[\'\\"][0-9a-zA-Z]{35,44}[\'\\"]'
110106
OR blob_content REGEXP '(?i)github.*[\'\\"][0-9a-zA-Z]{35,40}[\'\\"]'
@@ -145,7 +141,7 @@ DROP INDEX files_lang_idx ON files;
145141

146142
This query will report how many lines of actual code (only code, not comments, blank lines or text) changed in the last commit of each repository.
147143

148-
```
144+
```sql
149145
SELECT
150146
repo,
151147
JSON_EXTRACT(stats, '$.Code.Additions') AS code_lines_added,
@@ -175,7 +171,7 @@ The output will be similar to this:
175171
This query will report how many lines of actual code (only code, not comments, blank lines or text) changed in each file of the last commit of each repository. It's similar to the previous example. `COMMIT_STATS` is an aggregation over the result of `COMMIT_FILE_STATS` so to speak.
176172
We will only report those files that whose language has been identified.
177173

178-
```
174+
```sql
179175
SELECT
180176
repo,
181177
JSON_UNQUOTE(JSON_EXTRACT(stats, '$.Path')) AS file_path,
@@ -210,29 +206,15 @@ First of all, you should check out the [bblfsh documentation](https://docs.sourc
210206

211207
Also, you can take a look to all the UDFs and their signatures in the [functions section](/docs/using-gitbase/functions.md)
212208

213-
## Extract all import paths for every *Go* file on *HEAD* reference
214-
215-
```sql
216-
SELECT repository_id,
217-
file_path,
218-
uast_extract(uast(blob_content, LANGUAGE(file_path), '//uast:Import/Path'), "Value") AS imports
219-
FROM commit_files
220-
NATURAL JOIN refs
221-
NATURAL JOIN blobs
222-
WHERE ref_name = 'HEAD'
223-
AND LANGUAGE(file_path) = 'Go'
224-
AND ARRAY_LENGTH(imports) > 0;
225-
```
226-
227209
## Extracting all identifier names
228210

229211
```sql
230212
SELECT file_path,
231213
uast_extract(uast(blob_content, LANGUAGE(file_path), '//uast:Identifier'), "Name") name
232-
FROM commit_files
233-
NATURAL JOIN refs
214+
FROM refs
215+
NATURAL JOIN commit_files
234216
NATURAL JOIN blobs
235-
WHERE ref_name='HEAD' AND LANGUAGE(file_path) = 'Go';
217+
WHERE ref_name = 'HEAD' AND LANGUAGE(file_path) = 'Go';
236218
```
237219

238220
As result, you will get an array showing a list of the retrieved information. Each element in the list matches a node in the given sequence of nodes having a value for that property. It means that the length of the properties list may not be equal to the length of the given sequence of nodes:

docs/using-gitbase/functions.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,26 @@ Also, if you want to retrieve values from a non common property, you can pass it
169169

170170
> uast_extract(nodes_column, 'some-property')
171171
172+
## How to use `loc`
173+
174+
`loc` will return statistics about the lines of code in a file, such as the code lines, comment lines, etc.
175+
176+
It requires a file path and a file content.
177+
178+
> loc(file_path, blob_content)
179+
180+
The result of this function is a JSON document with the following shape:
181+
182+
```
183+
{
184+
"Code": code lines,
185+
"Comment": comment lines,
186+
"Blank": blank lines,
187+
"Name": file name,
188+
"Lang": language
189+
}
190+
```
191+
172192
## How to use `commit_file_stats`
173193

174194
`commit_file_stats` will return statistics about the line changes in all files in the given range of commits classifying them in 4 categories: code, comments, blank lines and other.

internal/function/loc.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ func (f *LOC) WithChildren(children ...sql.Expression) (sql.Expression, error) {
5252
return NewLOC(children...)
5353
}
5454

55+
// LocFile is the result of the LOC function for each file.
56+
type LocFile struct {
57+
Code int32 `json:"Code"`
58+
Comments int32 `json:"Comment"`
59+
Blanks int32 `json:"Blank"`
60+
Name string `json:"Name"`
61+
Lang string `json:"Language"`
62+
}
63+
5564
// Eval implements the Expression interface.
5665
func (f *LOC) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
5766
span, ctx := ctx.Span("gitbase.LOC")
@@ -70,11 +79,19 @@ func (f *LOC) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
7079
return nil, nil
7180
}
7281

73-
return gocloc.AnalyzeReader(
82+
file := gocloc.AnalyzeReader(
7483
path,
7584
languages.Langs[lang],
7685
bytes.NewReader(blob), &gocloc.ClocOptions{},
77-
), nil
86+
)
87+
88+
return LocFile{
89+
Code: file.Code,
90+
Comments: file.Comments,
91+
Blanks: file.Blanks,
92+
Name: file.Name,
93+
Lang: file.Lang,
94+
}, nil
7895
}
7996

8097
func (f *LOC) getInputValues(ctx *sql.Context, row sql.Row) (string, []byte, error) {

internal/function/loc_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ package function
33
import (
44
"testing"
55

6-
"github.com/hhatto/gocloc"
7-
"github.com/stretchr/testify/require"
8-
"gopkg.in/src-d/go-errors.v1"
96
"github.com/src-d/go-mysql-server/sql"
107
"github.com/src-d/go-mysql-server/sql/expression"
8+
"github.com/stretchr/testify/require"
9+
"gopkg.in/src-d/go-errors.v1"
1110
)
1211

1312
func TestLoc(t *testing.T) {
@@ -22,7 +21,7 @@ func TestLoc(t *testing.T) {
2221
{"too few args given", sql.NewRow("foo.foobar"), nil, nil},
2322
{"too many args given", sql.NewRow("foo.rb", "bar", "baz"), nil, sql.ErrInvalidArgumentNumber},
2423
{"invalid blob type given", sql.NewRow("foo", 5), nil, sql.ErrInvalidType},
25-
{"path and blob are given", sql.NewRow("foo", "#!/usr/bin/env python\n\nprint 'foo'"), &gocloc.ClocFile{
24+
{"path and blob are given", sql.NewRow("foo", "#!/usr/bin/env python\n\nprint 'foo'"), LocFile{
2625
Code: 2, Comments: 0, Blanks: 1, Name: "foo", Lang: "Python",
2726
}, nil},
2827
}

0 commit comments

Comments
 (0)