internal/vcs/repository.go (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | package vcs
import (
"io/fs"
"os"
"go.alanpearce.eu/website/internal/config"
"go.alanpearce.eu/x/log"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"gitlab.com/tozd/go/errors"
)
type Options struct {
LocalPath string
RemoteURL config.URL
Branch string
}
type Repository struct {
repo *git.Repository
log *log.Logger
}
func CloneOrOpen(cfg *Options, log *log.Logger) (repo *Repository, err error) {
stat, err := os.Stat(cfg.LocalPath)
if err != nil && errors.Is(err, fs.ErrNotExist) {
return nil, err
}
repo = &Repository{
log: log,
}
if stat == nil {
repo.repo, err = git.PlainClone(cfg.LocalPath, false, &git.CloneOptions{
URL: cfg.RemoteURL.String(),
Progress: os.Stdout,
})
} else {
repo.repo, err = git.PlainOpen(cfg.LocalPath)
}
return
}
func (r *Repository) Update() (bool, error) {
r.log.Info("updating repository")
head, err := r.repo.Head()
if err != nil {
return false, err
}
r.log.Info("updating from", "rev", head.Hash().String())
err = r.repo.Fetch(&git.FetchOptions{
Prune: true,
})
if err != nil {
if errors.Is(err, git.NoErrAlreadyUpToDate) {
r.log.Info("already up-to-date")
return true, nil
}
return false, err
}
rem, err := r.repo.Remote("origin")
if err != nil {
return false, err
}
refs, err := rem.List(&git.ListOptions{
Timeout: 5,
})
if err != nil {
return false, err
}
var hash plumbing.Hash
for _, ref := range refs {
if ref.Name() == plumbing.Main {
hash = ref.Hash()
break
}
}
wt, err := r.repo.Worktree()
if err != nil {
return false, err
}
err = wt.Checkout(&git.CheckoutOptions{
Hash: hash,
Force: true,
})
if err != nil {
return false, err
}
r.log.Info("updated to", "rev", hash)
return true, r.Clean(wt)
}
func (r *Repository) Clean(wt *git.Worktree) error {
st, err := wt.Status()
if err != nil {
return err
}
if !st.IsClean() {
err = wt.Clean(&git.CleanOptions{
Dir: true,
})
if err != nil {
return err
}
}
return nil
}
|