From e20dc64d0d68108a8a899a4744d4d1dd0aff055e Mon Sep 17 00:00:00 2001 From: Mya Pitzeruse Date: Wed, 29 Jun 2022 22:46:40 -0500 Subject: [PATCH] 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 --- internal/commands/host.go | 8 ++++++-- internal/git/billy_http.go | 4 ++++ internal/git/service.go | 26 ++++++++++++++++++-------- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/internal/commands/host.go b/internal/commands/host.go index 6b54966..d52c1ff 100644 --- a/internal/commands/host.go +++ b/internal/commands/host.go @@ -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 } diff --git a/internal/git/billy_http.go b/internal/git/billy_http.go index cacf045..a4487d1 100644 --- a/internal/git/billy_http.go +++ b/internal/git/billy_http.go @@ -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 } diff --git a/internal/git/service.go b/internal/git/service.go index 745b131..1d24a52 100644 --- a/internal/git/service.go +++ b/internal/git/service.go @@ -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 }