1package git
2
3import (
4 "errors"
5
6 "github.com/go-git/go-git/v5"
7 "github.com/go-git/go-git/v5/plumbing/object"
8)
9
10var ()
11
12var (
13 MissingHeadErr = errors.New("Head not found")
14)
15
16type (
17 GitRepository struct {
18 path string
19 }
20)
21
22func NewGitRepository(dir string) *GitRepository {
23 return &GitRepository{
24 path: dir,
25 }
26}
27
28func (g *GitRepository) Path() string {
29 return g.path
30}
31
32func (g *GitRepository) LastCommit() (*object.Commit, error) {
33 repo, err := git.PlainOpen(g.path)
34 if err != nil {
35 return nil, err
36 }
37
38 ref, err := repo.Head()
39 if err != nil {
40 return nil, errors.Join(MissingHeadErr, err)
41 }
42
43 c, err := repo.CommitObject(ref.Hash())
44 if err != nil {
45 return nil, err
46 }
47 return c, nil
48}