<commit_before>package runtime

import (
    "fmt"
    "reflect"
    "strings"
    "github.com/cloudson/gitql/parser"
    "github.com/libgit2/git2go"
)

var repo *git.Repository
var builder *GitBuilder
var boolRegister bool

type GitBuilder struct {
    tables map[string]string 
    possibleTables map[string][]string
    walk *git.RevWalk
    object *git.Commit
}

type RuntimeError struct {
    code uint8 
    message string
}

type RuntimeVisitor struct {

}

// =========================== Runtime
func Run(n *parser.NodeProgram) {
    builder = GetGitBuilder(n.Path)
    visitor := new(RuntimeVisitor)
    err := visitor.Visit(n)
    if err != nil {
        panic(err)
    }

    s := n.Child.(*parser.NodeSelect)
    where := s.Where
    // valuesTable := make([]string, len(s.Fields))
    counter := 1
    fmt.Println()
    fn := func (object *git.Commit) bool {
        builder.setObject(object)
        boolRegister = true
        visitor.VisitExpr(where)
        if boolRegister {
            fields := s.Fields
            if s.WildCard {
                fields = builder.possibleTables[s.Tables[0]]
            }            
            for _, f := range fields {
                fmt.Printf("%s | ", discoverLvalue(f, object))    
            }
            fmt.Println()

            
            counter = counter + 1
        }
        if counter > s.Limit {
            return false
        }
        return true
    }

    err = builder.walk.Iterate(fn)
    if err != nil {
        fmt.Printf(err.Error())
    }

}

func discoverLvalue(identifier string, object *git.Commit) string {
    key := "" 
    for key, _ = range builder.tables {
        break
    }
    table := key
    err := builder.UseFieldFromTable(identifier, table)
    if err != nil {
        panic(err)
    }
    switch identifier {
        case "hash" : 
            return object.Id().String()<commit_msg>Isolate commit walk to support others walkers<commit_after>package runtime

import (
    "fmt"
    "reflect"
    "strings"
    "github.com/cloudson/gitql/parser"
    "github.com/libgit2/git2go"
)

const (
    WALK_COMMITS = 1
    WALK_TRESS = 2
)

var repo *git.Repository
var builder *GitBuilder
var boolRegister bool

type GitBuilder struct {
    tables map[string]string 
    possibleTables map[string][]string
    walk *git.RevWalk
    object *git.Commit
}

type RuntimeError struct {
    code uint8 
    message string
}

type RuntimeVisitor struct {

}

// =========================== Runtime
func Run(n *parser.NodeProgram) {
    builder = GetGitBuilder(n.Path)
    visitor := new(RuntimeVisitor)
    err := visitor.Visit(n)
    if err != nil {
        panic(err)
    }
    switch findWalkType(n) {
        case WALK_COMMITS: 
            walkCommits(n, visitor)
            break
    }
}

func findWalkType(n *parser.NodeProgram) uint8 {
    s := n.Child.(*parser.NodeSelect)

    switch s.Tables[0] {
        case "commits" :
            return WALK_COMMITS
    }
    panic(fmt.Sprintf("Table '%s' yet not supported", s.Tables[0]))
}

func walkCommits(n *parser.NodeProgram, visitor *RuntimeVisitor) {
    s := n.Child.(*parser.NodeSelect)
    where := s.Where
    
    counter := 1
    fmt.Println()
    fn := func (object *git.Commit) bool {
        builder.setObject(object)
        boolRegister = true
        visitor.VisitExpr(where)
        if boolRegister {
            fields := s.Fields
            if s.WildCard {
                fields = builder.possibleTables[s.Tables[0]]
            }            
            for _, f := range fields {
                fmt.Printf("%s | ", discoverLvalue(f, object))    
            }
            fmt.Println()

            
            counter = counter + 1
        }
        if counter > s.Limit {
            return false
        }
        return true
    }

    err := builder.walk.Iterate(fn)
    if err != nil {
        fmt.Printf(err.Error())
    }
}

func discoverLvalue(identifier string, object *git.Commit) string {
    key := "" 
    for key, _ = range builder.tables {
        break
    }
    table := key
    err := builder.UseFieldFromTable(identifier, table)
    if err != nil {
        panic(err)
    }
    switch identifier {
        case "hash" : 
            return object.Id().String()
{'fraction_non_alphanumeric': 0.11968022572301905, 'fraction_numerical': 0.003997178462261932, 'mean_word_length': 1.9136986301369863, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '612531', 'n_tokens_mistral': 1298, 'n_tokens_neox': 1270, 'n_words': 297}