Skip to content

Commit

Permalink
Merge pull request #1 from luizperes/Code-Hex-develop
Browse files Browse the repository at this point in the history
Fixing Conflicts for accepting filhodanuvem#14 and filhodanuvem#38
  • Loading branch information
Code-Hex authored Dec 19, 2016
2 parents 7f4dc13 + cfcd5f5 commit 351e366
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 88 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
language: go
go:
- 1.0
- 1.1
- 1.2
- 1.5
- 1.6
- 1.7
before_script:
- export GOPATH="$HOME/gopath"
- go get -d -v
Expand Down
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ build:
@echo "Building..."
@go build
@echo "Ready to go!"

install:
@cp ./libgit2/install/lib/lib* /usr/local/lib/
@ldconfig /usr/local/lib >/dev/null 2>&1 || echo "ldconfig not found">/dev/null
@cp ./gitql /usr/local/bin/gitql
@ln -s -f /usr/local/bin/gitql /usr/local/bin/git-ql
@echo "Git is in /usr/local/bin/gitql"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ See more [here](https://asciinema.org/a/8863)
- `cd $GOPATH/src/github.com/cloudson/gitql`
- `make`
- `sudo make install`
- `export LD_LIBRARY_PATH=$PWD/libgit2/install/lib` on linux or `export DYLD_LIBRARY_PATH=$PWD/libgit2/install/lib`on Mac OS.


## Examples
Expand Down Expand Up @@ -59,3 +58,4 @@ Notes:
* The limit default is 10 rows
* It's inspired by [textql](https://github.com/dinedal/textql)
* But, why gitql is a compiler/interpreter instead of just read a sqlite database with all commits, tags and etc? Answer: Because we would need to sync the tables every time before run sql and we would have sqlite bases for each repository. :neutral_face:

27 changes: 17 additions & 10 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ import (
"github.com/pkg/errors"
)

const Version = "Gitql 1.1.1"
const Version = "Gitql 1.2.1"

type Gitql struct {
Path string `short:"p" default:"."`
Version bool `short:"v"`
Isinteractive bool `short:"i"`
ShowTables bool `long:"show-tables"`
TypeFormat string `long:"type" default:"table"`
Query string
}

Expand All @@ -45,13 +46,13 @@ func (cmd Gitql) execute() error {
}

if cmd.Isinteractive {
return runPrompt(folder)
return runPrompt(folder, cmd.TypeFormat)
}

return runQuery(cmd.Query, folder)
return runQuery(cmd.Query, folder, cmd.TypeFormat)
}

func runPrompt(folder string) error {
func runPrompt(folder, typeFormat string) error {

term, err := readline.New("gitql> ")
if err != nil {
Expand All @@ -76,7 +77,7 @@ func runPrompt(folder string) error {
break
}

if err := runQuery(query, folder); err != nil {
if err := runQuery(query, folder, typeFormat); err != nil {
fmt.Println("Error: " + err.Error())
continue
}
Expand All @@ -85,7 +86,7 @@ func runPrompt(folder string) error {
return nil
}

func runQuery(query, folder string) error {
func runQuery(query, folder, typeFormat string) error {
parser.New(query)
ast, err := parser.AST()
if err != nil {
Expand All @@ -97,7 +98,7 @@ func runQuery(query, folder string) error {
return err
}

runtime.Run(ast)
runtime.Run(ast, &typeFormat)

return nil
}
Expand All @@ -122,7 +123,11 @@ func (cmd *Gitql) parse(argv []string) error {
p := flags.NewParser(cmd, flags.PrintErrors)
args, err := p.ParseArgs(argv)

if (!cmd.Isinteractive && len(args) == 0) || err != nil {
if err != nil {
return err
}

if (!cmd.Isinteractive && !cmd.Version && !cmd.ShowTables && len(args) == 0){
os.Stderr.Write(cmd.usage())
return errors.New("invalid command line options")
}
Expand All @@ -137,14 +142,16 @@ func (cmd Gitql) usage() []byte {
fmt.Fprintf(&buf, `Gitql - Git query language
Usage: gitql [flags] [args]
Flags:
Flags:
-i Enter to interactive mode
-p string
The (optional) path to run gitql (default ".")
--show-tables
Show all tables
--type string
The output type format {table|json} (default "table")
-v The version of gitql
Arguments:
Arguments:
sql: A query to run
`)

Expand Down
38 changes: 21 additions & 17 deletions runtime/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (
"github.com/cloudson/gitql/parser"
)

func walkCommits(n *parser.NodeProgram, visitor *RuntimeVisitor) {
builder.walk, _ = repo.Walk()
builder.walk.PushHead()
builder.walk.Sorting(git.SortTime)

func walkCommits(n *parser.NodeProgram, visitor *RuntimeVisitor) *TableData{
builder.walk, _ = repo.Walk()
builder.walk.PushHead()
builder.walk.Sorting(git.SortTime)

s := n.Child.(*parser.NodeSelect)
where := s.Where
Expand Down Expand Up @@ -46,19 +47,22 @@ func walkCommits(n *parser.NodeProgram, visitor *RuntimeVisitor) {
return true
}

err := builder.walk.Iterate(fn)
if err != nil {
fmt.Printf(err.Error())
}
rowsSliced := rows[len(rows)-counter+1:]
rowsSliced = orderTable(rowsSliced, s.Order)
if usingOrder {
if counter > s.Limit {
counter = s.Limit
}
rowsSliced = rowsSliced[0:counter]
}
printTable(rowsSliced, fields)
err := builder.walk.Iterate(fn)
if err != nil {
fmt.Printf(err.Error())
}
rowsSliced := rows[len(rows)-counter+1:]
rowsSliced = orderTable(rowsSliced, s.Order)
if usingOrder {
if counter > s.Limit {
counter = s.Limit
}
rowsSliced = rowsSliced[0:counter]
}
tableData := new(TableData)
tableData.rows = rowsSliced
tableData.fields = fields
return tableData
}

func metadataCommit(identifier string, object *git.Commit) string {
Expand Down
65 changes: 34 additions & 31 deletions runtime/reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"github.com/cloudson/gitql/parser"
)

func walkReferences(n *parser.NodeProgram, visitor *RuntimeVisitor) {
s := n.Child.(*parser.NodeSelect)
where := s.Where
func walkReferences(n *parser.NodeProgram, visitor *RuntimeVisitor) *TableData{
s := n.Child.(*parser.NodeSelect)
where := s.Where

// @TODO make PR with Repository.WalkReference()
iterator, err := builder.repo.NewReferenceIterator()
Expand All @@ -28,34 +28,37 @@ func walkReferences(n *parser.NodeProgram, visitor *RuntimeVisitor) {
}
for object, inTheEnd := iterator.Next(); inTheEnd == nil; object, inTheEnd = iterator.Next() {

builder.setReference(object)
boolRegister = true
visitor.VisitExpr(where)
if boolRegister {
fields := s.Fields
if s.WildCard {
fields = builder.possibleTables[s.Tables[0]]
}
newRow := make(tableRow)
for _, f := range fields {
newRow[f] = metadataReference(f, object)
}
rows = append(rows, newRow)
counter = counter + 1
if !usingOrder && counter > s.Limit {
break
}
}
}
rowsSliced := rows[len(rows)-counter+1:]
rowsSliced = orderTable(rowsSliced, s.Order)
if usingOrder {
if counter > s.Limit {
counter = s.Limit
}
rowsSliced = rowsSliced[0:counter]
}
printTable(rowsSliced, fields)
builder.setReference(object)
boolRegister = true
visitor.VisitExpr(where)
if boolRegister {
fields := s.Fields
if s.WildCard {
fields = builder.possibleTables[s.Tables[0]]
}
newRow := make(tableRow)
for _, f := range fields {
newRow[f] = metadataReference(f, object)
}
rows = append(rows, newRow)
counter = counter + 1
if !usingOrder && counter > s.Limit {
break
}
}
}
rowsSliced := rows[len(rows)-counter+1:]
rowsSliced = orderTable(rowsSliced, s.Order)
if usingOrder {
if counter > s.Limit {
counter = s.Limit
}
rowsSliced = rowsSliced[0:counter]
}
tableData := new(TableData)
tableData.rows = rowsSliced
tableData.fields = fields
return tableData
}

func metadataReference(identifier string, object *git.Reference) string {
Expand Down
39 changes: 21 additions & 18 deletions runtime/remotes.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"github.com/cloudson/gitql/parser"
)

func walkRemotes(n *parser.NodeProgram, visitor *RuntimeVisitor) {
s := n.Child.(*parser.NodeSelect)
where := s.Where
func walkRemotes(n *parser.NodeProgram, visitor *RuntimeVisitor) *TableData{
s := n.Child.(*parser.NodeSelect)
where := s.Where

remoteNames, err := builder.repo.ListRemotes()
if err != nil {
Expand Down Expand Up @@ -43,21 +43,24 @@ func walkRemotes(n *parser.NodeProgram, visitor *RuntimeVisitor) {
}
rows = append(rows, newRow)

counter = counter + 1
if !usingOrder && counter > s.Limit {
break
}
}
}
rowsSliced := rows[len(rows)-counter+1:]
rowsSliced = orderTable(rowsSliced, s.Order)
if usingOrder {
if counter > s.Limit {
counter = s.Limit
}
rowsSliced = rowsSliced[0:counter]
}
printTable(rowsSliced, fields)
counter = counter + 1
if !usingOrder && counter > s.Limit {
break
}
}
}
rowsSliced := rows[len(rows)-counter+1:]
rowsSliced = orderTable(rowsSliced, s.Order)
if usingOrder {
if counter > s.Limit {
counter = s.Limit
}
rowsSliced = rowsSliced[0:counter]
}
tableData := new(TableData)
tableData.rows = rowsSliced
tableData.fields = fields
return tableData
}

func metadataRemote(identifier string, object *git.Remote) string {
Expand Down
39 changes: 32 additions & 7 deletions runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/cloudson/gitql/parser"
"github.com/cloudson/gitql/semantical"
"github.com/crackcomm/go-clitable"
"encoding/json"
)

const (
Expand Down Expand Up @@ -53,6 +54,11 @@ type RuntimeVisitor struct {
semantical.Visitor
}

type TableData struct {
rows []tableRow
fields []string
}

// =========================== Error

func (e *RuntimeError) Error() string {
Expand All @@ -68,24 +74,32 @@ func throwRuntimeError(message string, code uint8) *RuntimeError {
}

// =========================== Runtime
func Run(n *parser.NodeProgram) {
func Run(n *parser.NodeProgram, typeFormat *string) {
builder = GetGitBuilder(n.Path)
visitor := new(RuntimeVisitor)
err := visitor.Visit(n)
if err != nil {
log.Fatalln(err)
}
var tableData *TableData

switch findWalkType(n) {
case WALK_COMMITS:
walkCommits(n, visitor)
tableData = walkCommits(n, visitor)
break
case WALK_REFERENCES:
walkReferences(n, visitor)
tableData = walkReferences(n, visitor)
break
case WALK_REMOTES:
walkRemotes(n, visitor)
tableData = walkRemotes(n, visitor)
break
}

if *typeFormat == "json" {
printJson(tableData)
} else {
printTable(tableData)
}
}

func findWalkType(n *parser.NodeProgram) uint8 {
Expand All @@ -102,14 +116,25 @@ func findWalkType(n *parser.NodeProgram) uint8 {
return builder.currentWalkType
}

func printTable(rows []tableRow, fields []string) {
table := clitable.New(fields)
for _, r := range rows {
func printTable(tableData *TableData) {
table := clitable.New(tableData.fields)
for _, r := range tableData.rows {
table.AddRow(r)
}
table.Print()
}

func printJson(tableData *TableData) error {
res, err := json.Marshal(tableData.rows)
if err != nil {
log.Fatalln(err)
return throwRuntimeError(fmt.Sprintf("Json error:'%s'", err), 0)
} else {
fmt.Println(string(res))
}
return nil
}

func orderTable(rows []tableRow, order *parser.NodeOrder) []tableRow {
if order == nil {
return rows
Expand Down
Loading

0 comments on commit 351e366

Please sign in to comment.