fix(git): back by disk and fix synchronization

Resolves https://github.com/mjpitz/pages/issues/12
Resolves https://github.com/mjpitz/pages/issues/11
Resolves https://github.com/mjpitz/pages/issues/10
This commit is contained in:
Mya 2022-06-29 22:46:40 -05:00
parent f8db96c717
commit e20dc64d0d
No known key found for this signature in database
GPG Key ID: C3ECFA648DAD27FA
3 changed files with 28 additions and 10 deletions

View File

@ -60,8 +60,12 @@ var (
_ = mime.AddExtensionType(".yml", "application/yaml")
_ = mime.AddExtensionType(".json", "application/json")
gitService := git.NewService(hostConfig.Git)
err := gitService.Load(ctx.Context)
gitService, err := git.NewService(hostConfig.Git)
if err != nil {
return err
}
err = gitService.Load(ctx.Context)
if err != nil {
return err
}

View File

@ -58,6 +58,10 @@ type httpFile struct {
file billy.File
}
func (f *httpFile) Seek(offset int64, whence int) (int64, error) {
return f.file.Seek(offset, whence)
}
func (f *httpFile) Stat() (fs.FileInfo, error) {
return f.fileInfo, nil
}

View File

@ -18,9 +18,10 @@ package git
import (
"context"
"os"
"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/memfs"
"github.com/go-git/go-billy/v5/osfs"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/transport/http"
@ -41,11 +42,14 @@ type Config struct {
}
// NewService constructs a Service that manages the underlying git repository.
func NewService(config Config) *Service {
func NewService(config Config) (*Service, error) {
temp, err := os.MkdirTemp(os.TempDir(), "pages-*")
if err != nil {
return nil, err
}
options := &git.CloneOptions{
URL: config.URL,
Depth: 1,
SingleBranch: true,
URL: config.URL,
}
if config.Username != "" && config.Password != "" {
@ -65,8 +69,8 @@ func NewService(config Config) *Service {
return &Service{
options: options,
Store: memory.NewStorage(),
FS: memfs.New(),
}
FS: osfs.New(temp),
}, nil
}
// Service encapsulates operations that can be performed against the target git repository.
@ -98,7 +102,7 @@ func (s *Service) Sync(ctx context.Context) error {
return errors.Wrap(err, "failed to obtain worktree")
}
_ = wt.PullContext(ctx, &git.PullOptions{
err = wt.PullContext(ctx, &git.PullOptions{
ReferenceName: s.options.ReferenceName,
SingleBranch: s.options.SingleBranch,
Depth: s.options.Depth,
@ -106,5 +110,11 @@ func (s *Service) Sync(ctx context.Context) error {
Force: true,
})
switch {
case errors.Is(err, git.NoErrAlreadyUpToDate):
case err != nil:
zaputil.Extract(ctx).Error("failed to pull", zap.Error(err))
}
return nil
}