use disk storage for fetching git repos as an alternative to in-memory store

This commit is contained in:
Serge Zaitsev
2018-09-25 16:03:04 +02:00
parent b786cfca1b
commit 374bc078c5
4 changed files with 29 additions and 11 deletions

View File

@@ -8,6 +8,7 @@ import (
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
@@ -26,14 +27,15 @@ const remoteName = "origin"
type gitVCS struct {
log logger
dir string
module string
auth Auth
}
// NewGit return a go-git VCS client implementation that provides information
// about the specific module using the pgiven authentication mechanism.
func NewGit(l logger, module string, auth Auth) VCS {
return &gitVCS{log: l, module: module, auth: auth}
func NewGit(l logger, dir string, module string, auth Auth) VCS {
return &gitVCS{log: l, dir: dir, module: module, auth: auth}
}
func (g *gitVCS) List(ctx context.Context) ([]Version, error) {
@@ -142,8 +144,18 @@ func (g *gitVCS) Zip(ctx context.Context, version Version) (io.ReadCloser, error
return ioutil.NopCloser(bytes.NewBuffer(b.Bytes())), nil
}
func (g *gitVCS) repo(ctx context.Context) (*git.Repository, error) {
repo, err := git.Init(memory.NewStorage(), nil)
func (g *gitVCS) repo(ctx context.Context) (repo *git.Repository, err error) {
if g.dir != "" {
dir := filepath.Join(g.dir, g.module)
if _, err := os.Stat(dir); os.IsNotExist(err) {
os.MkdirAll(dir, 0755)
repo, err = git.PlainInit(dir, true)
} else {
return git.PlainOpen(dir)
}
} else {
repo, err = git.Init(memory.NewStorage(), nil)
}
if err != nil {
return nil, err
}
@@ -175,7 +187,7 @@ func (g *gitVCS) commit(ctx context.Context, version Version) (*object.Commit, e
RemoteName: remoteName,
Auth: auth,
})
if err != nil {
if err != nil && err != git.NoErrAlreadyUpToDate {
return nil, err
}

View File

@@ -66,7 +66,7 @@ func TestGit(t *testing.T) {
auth := NoAuth()
if test.Tag != "" {
t.Run(test.Module+"/List", func(t *testing.T) {
git := NewGit(t.Log, test.Module, auth)
git := NewGit(t.Log, "", test.Module, auth)
list, err := git.List(context.Background())
if err != nil {
t.Fatal(err)
@@ -81,7 +81,7 @@ func TestGit(t *testing.T) {
}
if test.Timestamp != "" {
t.Run(test.Module+"/Timestamp", func(t *testing.T) {
git := NewGit(t.Log, test.Module, auth)
git := NewGit(t.Log, "", test.Module, auth)
timestamp, err := git.Timestamp(context.Background(), Version(test.Tag))
if err != nil {
t.Fatal(err)
@@ -93,7 +93,7 @@ func TestGit(t *testing.T) {
}
if test.Checksum != "" {
t.Run(test.Module+"/ZIP", func(t *testing.T) {
git := NewGit(t.Log, test.Module, auth)
git := NewGit(t.Log, "", test.Module, auth)
r, err := git.Zip(context.Background(), Version(test.Tag))
if err != nil {
t.Fatal(err)