Skip to content

Commit

Permalink
some restructuring to move the libgit2 modules into their own packa…
Browse files Browse the repository at this point in the history
…ge `native`
  • Loading branch information
patrickdevivo committed Jul 20, 2021
1 parent 89b40e0 commit d7eeeb8
Show file tree
Hide file tree
Showing 15 changed files with 68 additions and 16 deletions.
2 changes: 1 addition & 1 deletion tables/internal/git/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (cur *gitLogCursor) Filter(_ int, s string, values ...sqlite.Value) (err er
var repo *git.Repository
{ // open the git repository
if path == "" {
path, err = getDefaultRepoFromCtx(cur.ctx)
path, err = GetDefaultRepoFromCtx(cur.ctx)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package git
package native

import (
"context"
"fmt"
"io"
"os"

"github.com/askgitdev/askgit/tables/internal/git"
"github.com/askgitdev/askgit/tables/services"
"github.com/augmentable-dev/vtab"
"github.com/go-git/go-git/v5/storage/filesystem"
Expand Down Expand Up @@ -47,7 +48,7 @@ func NewBlameModule(locator services.RepoLocator, ctx services.Context) sqlite.M

if repoPath == "" {
var err error
repoPath, err = getDefaultRepoFromCtx(ctx)
repoPath, err = git.GetDefaultRepoFromCtx(ctx)
if err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package git_test
package native_test

import (
"encoding/csv"
Expand Down
5 changes: 5 additions & 0 deletions tables/internal/git/native/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Package native provides virtual table implementations for git tables using libgit2
// via the git2go bindings (https://github.com/libgit2/git2go).
// Some operations are more performant using libgit2 vs go-git, namely, what's involved in
// the `stats`, `files` and `blame` tables, which are implemented in this package.
package native
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package git
package native

import (
"context"
Expand All @@ -7,6 +7,7 @@ import (
"os"
"path"

"github.com/askgitdev/askgit/tables/internal/git"
"github.com/askgitdev/askgit/tables/services"
"github.com/augmentable-dev/vtab"
"github.com/go-git/go-git/v5/storage/filesystem"
Expand Down Expand Up @@ -40,7 +41,7 @@ func NewFilesModule(locator services.RepoLocator, ctx services.Context) sqlite.M

if repoPath == "" {
var err error
repoPath, err = getDefaultRepoFromCtx(ctx)
repoPath, err = git.GetDefaultRepoFromCtx(ctx)
if err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package git_test
package native_test

import (
"fmt"
Expand Down
43 changes: 43 additions & 0 deletions tables/internal/git/native/native_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package native_test

import (
"database/sql"
"os"
"testing"

"github.com/askgitdev/askgit/pkg/locator"
_ "github.com/askgitdev/askgit/pkg/sqlite"
"github.com/askgitdev/askgit/tables"
_ "github.com/mattn/go-sqlite3"
"go.riyazali.net/sqlite"
)

func init() {
// register sqlite extension when this package is loaded
sqlite.Register(tables.RegisterFn(
tables.WithExtraFunctions(), tables.WithRepoLocator(locator.CachedLocator(locator.MultiLocator())),
))
}

// tests' entrypoint that registers the extension
// automatically with all loaded database connections
func TestMain(m *testing.M) { os.Exit(m.Run()) }

// Memory represents a uri to an in-memory database
const Memory = "file:testing.db?mode=memory"

// Connect opens a connection with the sqlite3 database using
// the given data source address and pings it to check liveliness.
func Connect(t *testing.T, dataSourceName string) *sql.DB {
t.Helper()
db, err := sql.Open("sqlite3", dataSourceName)
if err != nil {
t.Fatalf("failed to open connection: %v", err.Error())
}

if err = db.Ping(); err != nil {
t.Fatalf("failed to open connection: %v", err.Error())
}

return db
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package git
package native

import (
"context"
"fmt"
"io"

"github.com/askgitdev/askgit/tables/internal/git"
"github.com/askgitdev/askgit/tables/services"
"github.com/augmentable-dev/vtab"
"github.com/go-git/go-git/v5/storage/filesystem"
Expand Down Expand Up @@ -41,7 +42,7 @@ func NewStatsModule(locator services.RepoLocator, ctx services.Context) sqlite.M

if repoPath == "" {
var err error
repoPath, err = getDefaultRepoFromCtx(ctx)
repoPath, err = git.GetDefaultRepoFromCtx(ctx)
if err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package git_test
package native_test

import (
"testing"
Expand Down
2 changes: 1 addition & 1 deletion tables/internal/git/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (cur *gitRefCursor) Filter(_ int, s string, values ...sqlite.Value) (err er
var repo *git.Repository
{ // open the git repository
if path == "" {
path, err = getDefaultRepoFromCtx(cur.Context)
path, err = GetDefaultRepoFromCtx(cur.Context)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions tables/internal/git/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ func isRemoteBranch(ref plumbing.ReferenceName) bool {
plumbing.ReferenceName(strings.Replace(ref.String(), "remotes", "heads", 1)).IsBranch()
}

// getDefaultRepoFromCtx looks up the defaultRepoPath key in the supplied context and returns it if set,
// GetDefaultRepoFromCtx looks up the defaultRepoPath key in the supplied context and returns it if set,
// otherwise it returns the current working directory
func getDefaultRepoFromCtx(ctx services.Context) (repoPath string, err error) {
func GetDefaultRepoFromCtx(ctx services.Context) (repoPath string, err error) {
var ok bool
if repoPath, ok = ctx["defaultRepoPath"]; !ok || repoPath == "" {
if wd, err := os.Getwd(); err != nil {
Expand Down
7 changes: 4 additions & 3 deletions tables/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package tables
import (
"github.com/askgitdev/askgit/tables/internal/funcs"
"github.com/askgitdev/askgit/tables/internal/git"
"github.com/askgitdev/askgit/tables/internal/git/native"
"github.com/pkg/errors"
"go.riyazali.net/sqlite"
)
Expand All @@ -22,9 +23,9 @@ func RegisterFn(fns ...OptionFn) func(ext *sqlite.ExtensionApi) (_ sqlite.ErrorC
var modules = map[string]sqlite.Module{
"commits": &git.LogModule{Locator: opt.Locator, Context: opt.Context},
"refs": &git.RefModule{Locator: opt.Locator, Context: opt.Context},
"stats": git.NewStatsModule(opt.Locator, opt.Context),
"files": git.NewFilesModule(opt.Locator, opt.Context),
"blame": git.NewBlameModule(opt.Locator, opt.Context),
"stats": native.NewStatsModule(opt.Locator, opt.Context),
"files": native.NewFilesModule(opt.Locator, opt.Context),
"blame": native.NewBlameModule(opt.Locator, opt.Context),
}

for name, mod := range modules {
Expand Down

0 comments on commit d7eeeb8

Please sign in to comment.