Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use database command #110

Merged
merged 27 commits into from
Oct 19, 2020
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a1d8b1d
lexical data for "use"
srafi1 Oct 18, 2020
e136eec
parse use command
srafi1 Oct 18, 2020
63bc407
use command implementation
srafi1 Oct 18, 2020
1586a54
continue lexing ID after seeing '-'
srafi1 Oct 18, 2020
506d436
checkout using refname
srafi1 Oct 18, 2020
70185fe
try fetching branch from origin
srafi1 Oct 18, 2020
f6fa14c
don't checkout if worktree is not clean
srafi1 Oct 18, 2020
b71f9c0
store checkout options
srafi1 Oct 18, 2020
8dc65af
use command tests
srafi1 Oct 18, 2020
2239fdc
message after switching branches
srafi1 Oct 18, 2020
a1f4b11
fix "Error: Error: ..." messages
srafi1 Oct 19, 2020
e001135
bats tests for use command
srafi1 Oct 19, 2020
300b1f4
switch back to previous branch
srafi1 Oct 19, 2020
62eca46
fix indentation
srafi1 Oct 19, 2020
0a63e31
compatibility with older versions of git
srafi1 Oct 19, 2020
005f52c
redirect teardown to stderr instead of /dev/null
srafi1 Oct 19, 2020
b366ef6
check if git is available
srafi1 Oct 19, 2020
dd629b5
Revert "check if git is available"
srafi1 Oct 19, 2020
f011d13
use current branch instead of master
srafi1 Oct 19, 2020
d951ef0
Revert "redirect teardown to stderr instead of /dev/null"
srafi1 Oct 19, 2020
9b971db
fix test so branch var can be used
srafi1 Oct 19, 2020
4207397
no more need for teardown
srafi1 Oct 19, 2020
e34e558
get status in setup
srafi1 Oct 19, 2020
da8662f
get output from run
srafi1 Oct 19, 2020
30c45c3
check remote branch
srafi1 Oct 19, 2020
2a3fa5a
check status
srafi1 Oct 19, 2020
51fa1c7
store current branch if head is detached
srafi1 Oct 19, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lexical/lexemes.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ const L_COUNT = "count"
const L_SHOW = "show"
const L_TABLES = "tables"
const L_DATABASES = "databases"
const L_USE = "use"
4 changes: 3 additions & 1 deletion lexical/lexical.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func Token() (uint8, *TokenError) {
}
break
case S_ID:
for unicode.IsLetter(char) || unicode.IsNumber(char) || string(char) == "_" {
for unicode.IsLetter(char) || unicode.IsNumber(char) || string(char) == "_" || string(char) == "-" {
lexeme = lexeme + string(char)
char = nextChar()
}
Expand Down Expand Up @@ -240,6 +240,8 @@ func lexemeToToken(lexeme string) uint8 {
return T_TABLES
case L_DATABASES:
return T_DATABASES
case L_USE:
return T_USE
}
return T_ID
}
Expand Down
2 changes: 2 additions & 0 deletions lexical/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const T_FUCK = 66
const T_SHOW = 31
const T_TABLES = 32
const T_DATABASES = 33
const T_USE = 34

var tokenNameMap map[uint8]string

Expand Down Expand Up @@ -70,6 +71,7 @@ func allocMapTokenNames() {
T_SHOW: "T_SHOW",
T_TABLES: "T_TABLES",
T_DATABASES: "T_DATABASES",
T_USE: "T_USE",
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ func main() {
Hidden: true,
},
&cli.BoolFlag{
Name: "show-tables",
Name: "show-tables",
Aliases: []string{"s"},
Hidden: true,
Hidden: true,
},
},
Commands: []*cli.Command{
Expand Down Expand Up @@ -171,6 +171,9 @@ func runQuery(query, folder, typeFormat string) error {
case lexical.T_SHOW:
err = runtime.RunShow(ast)
break
case lexical.T_USE:
err = runtime.RunUse(ast)
break
}

return err
Expand Down
8 changes: 8 additions & 0 deletions parser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ type NodeShow struct {
Databases bool
}

type NodeUse struct {
Branch string
}

type NodeExpr interface {
Assertion(lvalue, rvalue string) bool
Operator() uint8
Expand Down Expand Up @@ -137,6 +141,10 @@ func (s *NodeShow) Run() {
return
}

func (u *NodeUse) Run() {
return
}

func (e *NodeEmpty) Run() {
return
}
Expand Down
20 changes: 20 additions & 0 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ func gProgram() (NodeMain, error) {
case lexical.T_SHOW:
node, err = gShow()
break
case lexical.T_USE:
node, err = gUse()
break
default:
err = fmt.Errorf("Error: invalid command")
}
Expand Down Expand Up @@ -162,6 +165,23 @@ func gShow() (*NodeShow, error) {
return node, nil
}

func gUse() (*NodeUse, error) {
var err *lexical.TokenError
look_ahead, err = lexical.Token()
if err != nil {
return nil, err
}

node := new(NodeUse)
if look_ahead != lexical.T_ID {
return nil, throwSyntaxError(lexical.T_ID, look_ahead)
}

node.Branch = lexical.CurrentLexeme
look_ahead, _ = lexical.Token()
return node, nil
}

func gTableNames() ([]string, error) {
if look_ahead != lexical.T_FROM {
return nil, throwSyntaxError(lexical.T_FROM, look_ahead)
Expand Down
35 changes: 35 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -710,3 +710,38 @@ func TestInvalidShow(t *testing.T) {
t.Fail()
}
}

func TestUse(t *testing.T) {
New("use master")
ast, err := AST()
node := ast.Child.(*NodeUse)

if err != nil {
t.Errorf("Error parsing 'use master': %v", err)
}

if node.Branch != "master" {
t.Errorf("NodeUse.Branch should be 'master', is '%s'", node.Branch)
}
}

func TestInvalidUse(t *testing.T) {
cases := []string{
"use",
"use master extra-param",
}

fail := false
for _, c := range cases {
New(c)
_, err := AST()
if err == nil {
t.Logf("Input '%v' should fail", c)
fail = true
}
}

if fail {
t.Fail()
}
}
50 changes: 50 additions & 0 deletions runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/cloudson/gitql/parser"
"github.com/cloudson/gitql/semantical"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/olekukonko/tablewriter"
Expand Down Expand Up @@ -143,6 +144,55 @@ func RunShow(node *parser.NodeProgram) error {
return nil
}

func RunUse(node *parser.NodeProgram) error {
builder = GetGitBuilder(node.Path)
u := node.Child.(*parser.NodeUse)

w, err := repo.Worktree()
if err != nil {
return err
}

s, err := w.Status()
if err != nil {
return err
}

if !s.IsClean() {
return fmt.Errorf("worktree is not clean")
}

refName := plumbing.ReferenceName(fmt.Sprintf("refs/heads/%s", u.Branch))
cOp := &git.CheckoutOptions{
Branch: refName,
Create: false,
}
err = w.Checkout(cOp)
if err != nil {
// Try fetching branch from origin and then switching to it.
// If it doesn't work, return the original error.
remote, remoteErr := repo.Remote("origin")
if remoteErr != nil {
return err
}
remoteErr = remote.Fetch(&git.FetchOptions{
RefSpecs: []config.RefSpec{
config.RefSpec(fmt.Sprintf("%s:%s", refName, refName)),
},
})
if remoteErr != nil {
return err
}
err = w.Checkout(cOp)
}

if err == nil {
fmt.Println("switched to database", u.Branch)
}

return err
}

func findWalkType(n *parser.NodeProgram) uint8 {
s := n.Child.(*parser.NodeSelect)
switch s.Tables[0] {
Expand Down