aboutsummaryrefslogtreecommitdiff
path: root/pkg/git/git.go
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/git/git.go')
-rw-r--r--pkg/git/git.go82
1 files changed, 52 insertions, 30 deletions
diff --git a/pkg/git/git.go b/pkg/git/git.go
index 7ef23f7..ce72465 100644
--- a/pkg/git/git.go
+++ b/pkg/git/git.go
@@ -13,19 +13,50 @@ import (
var ()
var (
- MissingHeadErr = errors.New("Head not found")
+ MissingRefErr = errors.New("Reference not found")
)
type (
GitRepository struct {
- path string
+ path string
+ repository *git.Repository
+
+ ref plumbing.Hash
+ // this is setRef when ref is setRef
+ setRef bool
}
)
-func NewGitRepository(dir string) *GitRepository {
- return &GitRepository{
+func OpenRepository(dir string) (*GitRepository, error) {
+ g := &GitRepository{
path: dir,
}
+
+ repo, err := git.PlainOpen(dir)
+ if err != nil {
+ return nil, err
+ }
+ g.repository = repo
+
+ return g, nil
+}
+
+func (g *GitRepository) SetRef(ref string) error {
+ if ref == "" {
+ head, err := g.repository.Head()
+ if err != nil {
+ return errors.Join(MissingRefErr, err)
+ }
+ g.ref = head.Hash()
+ } else {
+ hash, err := g.repository.ResolveRevision(plumbing.Revision(ref))
+ if err != nil {
+ return errors.Join(MissingRefErr, err)
+ }
+ g.ref = *hash
+ }
+ g.setRef = true
+ return nil
}
func (g *GitRepository) Path() string {
@@ -33,17 +64,12 @@ func (g *GitRepository) Path() string {
}
func (g *GitRepository) LastCommit() (*object.Commit, error) {
- repo, err := git.PlainOpen(g.path)
+ err := g.validateRef()
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())
+ c, err := g.repository.CommitObject(g.ref)
if err != nil {
return nil, err
}
@@ -51,17 +77,12 @@ func (g *GitRepository) LastCommit() (*object.Commit, error) {
}
func (g *GitRepository) Commits() ([]*object.Commit, error) {
- repo, err := git.PlainOpen(g.path)
+ err := g.validateRef()
if err != nil {
return nil, err
}
- ref, err := repo.Head()
- if err != nil {
- return nil, errors.Join(MissingHeadErr, err)
- }
-
- ci, err := repo.Log(&git.LogOptions{From: ref.Hash()})
+ ci, err := g.repository.Log(&git.LogOptions{From: g.ref})
if err != nil {
return nil, fmt.Errorf("commits from ref: %w", err)
}
@@ -84,13 +105,12 @@ func (g *GitRepository) Commits() ([]*object.Commit, error) {
return commits, nil
}
-func (g *GitRepository) Tags() ([]*object.Tag, error) {
- repo, err := git.PlainOpen(g.path)
- if err != nil {
- return nil, err
- }
+func (g *GitRepository) Head() (*plumbing.Reference, error) {
+ return g.repository.Head()
+}
- ti, err := repo.TagObjects()
+func (g *GitRepository) Tags() ([]*object.Tag, error) {
+ ti, err := g.repository.TagObjects()
if err != nil {
return nil, err
}
@@ -108,12 +128,7 @@ func (g *GitRepository) Tags() ([]*object.Tag, error) {
}
func (g *GitRepository) Branches() ([]*plumbing.Reference, error) {
- repo, err := git.PlainOpen(g.path)
- if err != nil {
- return nil, err
- }
-
- bs, err := repo.Branches()
+ bs, err := g.repository.Branches()
if err != nil {
return nil, err
}
@@ -129,3 +144,10 @@ func (g *GitRepository) Branches() ([]*plumbing.Reference, error) {
return branches, nil
}
+
+func (g *GitRepository) validateRef() error {
+ if !g.setRef {
+ return g.SetRef("")
+ }
+ return nil
+}