From e3705f35c642e578625ce4574d189fa0b0869403 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Sat, 1 Jun 2024 19:59:43 +0200 Subject: feat: Add ref support Now log and tree can be loaded using a given reference. --- pkg/git/git.go | 82 +++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 52 insertions(+), 30 deletions(-) (limited to 'pkg/git/git.go') 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 +} -- cgit v1.2.3