aboutsummaryrefslogtreecommitdiff
path: root/pkg/git/git.go
blob: b9ab235f7226301c7f57ca245c5b1660fa6230b9 (plain)
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
package git

import (
	"errors"

	"github.com/go-git/go-git/v5"
	"github.com/go-git/go-git/v5/plumbing/object"
)

var ()

var (
	MissingHeadErr = errors.New("Head not found")
)

type (
	GitRepository struct {
		path string
	}
)

func NewGitRepository(dir string) *GitRepository {
	return &GitRepository{
		path: dir,
	}
}

func (g *GitRepository) Path() string {
	return g.path
}

func (g *GitRepository) LastCommit() (*object.Commit, error) {
	repo, err := git.PlainOpen(g.path)
	if err != nil {
		return nil, err
	}

	ref, err := repo.Head()
	if err != nil {
		return nil, errors.Join(MissingHeadErr, err)
	}

	c, err := repo.CommitObject(ref.Hash())
	if err != nil {
		return nil, err
	}
	return c, nil
}