diff options
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() +} |