diff options
author | Gabriel A. Giovanini <mail@gabrielgio.me> | 2024-06-01 19:59:43 +0200 |
---|---|---|
committer | Gabriel A. Giovanini <mail@gabrielgio.me> | 2024-06-01 19:59:43 +0200 |
commit | e3705f35c642e578625ce4574d189fa0b0869403 (patch) | |
tree | f63d779d4eb817ad6b5abed30f50ad3b85ffcab1 /pkg/service | |
parent | f1643ffcbd543cacfe9ab5e46eafdd0c39cd47fd (diff) | |
download | cerrado-e3705f35c642e578625ce4574d189fa0b0869403.tar.gz cerrado-e3705f35c642e578625ce4574d189fa0b0869403.tar.bz2 cerrado-e3705f35c642e578625ce4574d189fa0b0869403.zip |
feat: Add ref support
Now log and tree can be loaded using a given reference.
Diffstat (limited to 'pkg/service')
-rw-r--r-- | pkg/service/git.go | 50 |
1 files changed, 45 insertions, 5 deletions
diff --git a/pkg/service/git.go b/pkg/service/git.go index 57b9b6e..9bf11f4 100644 --- a/pkg/service/git.go +++ b/pkg/service/git.go @@ -15,6 +15,7 @@ type ( Title string LastCommitMessage string LastCommitDate string + Ref string } GitService struct { @@ -41,29 +42,50 @@ func (g *GitService) ListRepositories() ([]*Repository, error) { repos := make([]*Repository, len(rs)) for i, r := range rs { - repo := git.NewGitRepository(r.Path) + repo, err := git.OpenRepository(r.Path) + if err != nil { + return nil, err + } + if err != nil { + return nil, err + } + obj, err := repo.LastCommit() if err != nil { return nil, err } + head, err := repo.Head() + if err != nil { + return nil, err + } + baseName := path.Base(r.Path) repos[i] = &Repository{ Name: baseName, Title: baseName, LastCommitMessage: obj.Message, LastCommitDate: obj.Author.When.Format(timeFormat), + Ref: head.Name().Short(), } } return repos, nil } -func (g *GitService) ListCommits(name string) ([]*object.Commit, error) { +func (g *GitService) ListCommits(name, ref string) ([]*object.Commit, error) { // TODO: handle nil r := g.configRepo.GetByName(name) - repo := git.NewGitRepository(r.Path) + repo, err := git.OpenRepository(r.Path) + if err != nil { + return nil, err + } + + err = repo.SetRef(ref) + if err != nil { + return nil, err + } return repo.Commits() } @@ -71,7 +93,10 @@ func (g *GitService) ListTags(name string) ([]*object.Tag, error) { // TODO: handle nil r := g.configRepo.GetByName(name) - repo := git.NewGitRepository(r.Path) + repo, err := git.OpenRepository(r.Path) + if err != nil { + return nil, err + } return repo.Tags() } @@ -79,6 +104,21 @@ func (g *GitService) ListBranches(name string) ([]*plumbing.Reference, error) { // TODO: handle nil r := g.configRepo.GetByName(name) - repo := git.NewGitRepository(r.Path) + repo, err := git.OpenRepository(r.Path) + if err != nil { + return nil, err + } return repo.Branches() } + +func (g *GitService) GetHead(name string) (*plumbing.Reference, error) { + // TODO: handle nil + r := g.configRepo.GetByName(name) + + repo, err := git.OpenRepository(r.Path) + if err != nil { + return nil, err + } + + return repo.Head() +} |