cerrado @ 18aa098f50e2a2c7db01dd4d04dde460fd40f5d5

  1package service
  2
  3import (
  4	"path"
  5
  6	"git.gabrielgio.me/cerrado/pkg/config"
  7	"git.gabrielgio.me/cerrado/pkg/git"
  8	"github.com/go-git/go-git/v5/plumbing"
  9	"github.com/go-git/go-git/v5/plumbing/object"
 10)
 11
 12type (
 13	Repository struct {
 14		Name              string
 15		Title             string
 16		LastCommitMessage string
 17		LastCommitDate    string
 18		Ref               string
 19	}
 20
 21	GitService struct {
 22		configRepo configurationRepository
 23	}
 24
 25	configurationRepository interface {
 26		List() []*config.GitRepositoryConfiguration
 27		GetByName(name string) *config.GitRepositoryConfiguration
 28	}
 29)
 30
 31// TODO: make it configurable
 32const timeFormat = "2006.01.02 15:04:05"
 33
 34func NewGitService(configRepo configurationRepository) *GitService {
 35	return &GitService{
 36		configRepo: configRepo,
 37	}
 38}
 39
 40func (g *GitService) ListRepositories() ([]*Repository, error) {
 41	rs := g.configRepo.List()
 42
 43	repos := make([]*Repository, len(rs))
 44	for i, r := range rs {
 45		repo, err := git.OpenRepository(r.Path)
 46		if err != nil {
 47			return nil, err
 48		}
 49		if err != nil {
 50			return nil, err
 51		}
 52
 53		obj, err := repo.LastCommit()
 54		if err != nil {
 55			return nil, err
 56		}
 57
 58		head, err := repo.Head()
 59		if err != nil {
 60			return nil, err
 61		}
 62
 63		baseName := path.Base(r.Path)
 64		repos[i] = &Repository{
 65			Name:              baseName,
 66			Title:             baseName,
 67			LastCommitMessage: obj.Message,
 68			LastCommitDate:    obj.Author.When.Format(timeFormat),
 69			Ref:               head.Name().Short(),
 70		}
 71	}
 72
 73	return repos, nil
 74}
 75
 76func (g *GitService) ListCommits(name, ref string) ([]*object.Commit, error) {
 77	// TODO: handle nil
 78	r := g.configRepo.GetByName(name)
 79
 80	repo, err := git.OpenRepository(r.Path)
 81	if err != nil {
 82		return nil, err
 83	}
 84
 85	err = repo.SetRef(ref)
 86	if err != nil {
 87		return nil, err
 88	}
 89	return repo.Commits()
 90}
 91
 92func (g *GitService) GetTree(name, ref, path string) (*object.Tree, error) {
 93	// TODO: handle nil
 94	r := g.configRepo.GetByName(name)
 95
 96	repo, err := git.OpenRepository(r.Path)
 97	if err != nil {
 98		return nil, err
 99	}
100	err = repo.SetRef(ref)
101	if err != nil {
102		return nil, err
103	}
104
105	return repo.Tree(path)
106}
107
108func (g *GitService) GetFileContent(name, ref, path string) (string, error) {
109	// TODO: handle nil
110	r := g.configRepo.GetByName(name)
111
112	repo, err := git.OpenRepository(r.Path)
113	if err != nil {
114		return "", err
115	}
116	err = repo.SetRef(ref)
117	if err != nil {
118		return "", err
119	}
120
121	return repo.FileContent(path)
122}
123
124func (g *GitService) ListTags(name string) ([]*object.Tag, error) {
125	// TODO: handle nil
126	r := g.configRepo.GetByName(name)
127
128	repo, err := git.OpenRepository(r.Path)
129	if err != nil {
130		return nil, err
131	}
132	return repo.Tags()
133}
134
135func (g *GitService) ListBranches(name string) ([]*plumbing.Reference, error) {
136	// TODO: handle nil
137	r := g.configRepo.GetByName(name)
138
139	repo, err := git.OpenRepository(r.Path)
140	if err != nil {
141		return nil, err
142	}
143	return repo.Branches()
144}
145
146func (g *GitService) GetHead(name string) (*plumbing.Reference, error) {
147	// TODO: handle nil
148	r := g.configRepo.GetByName(name)
149
150	repo, err := git.OpenRepository(r.Path)
151	if err != nil {
152		return nil, err
153	}
154
155	return repo.Head()
156}