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 }