diff options
Diffstat (limited to 'pkg')
| -rw-r--r-- | pkg/git/git.go | 48 | ||||
| -rw-r--r-- | pkg/handler/git/handler.go | 23 | ||||
| -rw-r--r-- | pkg/service/git.go | 17 | 
3 files changed, 86 insertions, 2 deletions
| diff --git a/pkg/git/git.go b/pkg/git/git.go index 80c0e46..7ef23f7 100644 --- a/pkg/git/git.go +++ b/pkg/git/git.go @@ -6,6 +6,7 @@ import (  	"io"  	"github.com/go-git/go-git/v5" +	"github.com/go-git/go-git/v5/plumbing"  	"github.com/go-git/go-git/v5/plumbing/object"  ) @@ -54,6 +55,7 @@ func (g *GitRepository) Commits() ([]*object.Commit, error) {  	if err != nil {  		return nil, err  	} +  	ref, err := repo.Head()  	if err != nil {  		return nil, errors.Join(MissingHeadErr, err) @@ -81,3 +83,49 @@ 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 +	} + +	ti, err := repo.TagObjects() +	if err != nil { +		return nil, err +	} + +	tags := []*object.Tag{} +	err = ti.ForEach(func(t *object.Tag) error { +		tags = append(tags, t) +		return nil +	}) +	if err != nil { +		return nil, err +	} + +	return tags, nil +} + +func (g *GitRepository) Branches() ([]*plumbing.Reference, error) { +	repo, err := git.PlainOpen(g.path) +	if err != nil { +		return nil, err +	} + +	bs, err := repo.Branches() +	if err != nil { +		return nil, err +	} + +	branches := []*plumbing.Reference{} +	err = bs.ForEach(func(ref *plumbing.Reference) error { +		branches = append(branches, ref) +		return nil +	}) +	if err != nil { +		return nil, err +	} + +	return branches, nil +} diff --git a/pkg/handler/git/handler.go b/pkg/handler/git/handler.go index ebfb37f..d090f22 100644 --- a/pkg/handler/git/handler.go +++ b/pkg/handler/git/handler.go @@ -6,6 +6,7 @@ import (  	"git.gabrielgio.me/cerrado/pkg/service"  	"git.gabrielgio.me/cerrado/templates" +	"github.com/go-git/go-git/v5/plumbing"  	"github.com/go-git/go-git/v5/plumbing/object"  	"github.com/gorilla/mux"  ) @@ -18,6 +19,8 @@ type (  	gitService interface {  		ListRepositories() ([]*service.Repository, error)  		ListCommits(string) ([]*object.Commit, error) +		ListTags(string) ([]*object.Tag, error) +		ListBranches(string) ([]*plumbing.Reference, error)  	}  ) @@ -58,9 +61,25 @@ func (g *GitHandler) About(w http.ResponseWriter, r *http.Request) {  func (g *GitHandler) Refs(w http.ResponseWriter, r *http.Request) {  	name := mux.Vars(r)["name"] + +	tags, err := g.gitService.ListTags(name) +	if err != nil { +		slog.Error("Error loading tags", "error", err) +		return +	} + +	branches, err := g.gitService.ListBranches(name) +	if err != nil { +		slog.Error("Error loading branches", "error", err) +		return +	} +  	gitList := &templates.GitItemPage{ -		Name:        name, -		GitItemBase: &templates.GitItemRefsPage{}, +		Name: name, +		GitItemBase: &templates.GitItemRefsPage{ +			Tags:     tags, +			Branches: branches, +		},  	}  	templates.WritePageTemplate(w, gitList)  } diff --git a/pkg/service/git.go b/pkg/service/git.go index 614770f..57b9b6e 100644 --- a/pkg/service/git.go +++ b/pkg/service/git.go @@ -5,6 +5,7 @@ import (  	"git.gabrielgio.me/cerrado/pkg/config"  	"git.gabrielgio.me/cerrado/pkg/git" +	"github.com/go-git/go-git/v5/plumbing"  	"github.com/go-git/go-git/v5/plumbing/object"  ) @@ -65,3 +66,19 @@ func (g *GitService) ListCommits(name string) ([]*object.Commit, error) {  	repo := git.NewGitRepository(r.Path)  	return repo.Commits()  } + +func (g *GitService) ListTags(name string) ([]*object.Tag, error) { +	// TODO: handle nil +	r := g.configRepo.GetByName(name) + +	repo := git.NewGitRepository(r.Path) +	return repo.Tags() +} + +func (g *GitService) ListBranches(name string) ([]*plumbing.Reference, error) { +	// TODO: handle nil +	r := g.configRepo.GetByName(name) + +	repo := git.NewGitRepository(r.Path) +	return repo.Branches() +} | 
