cerrado @ e3705f35c642e578625ce4574d189fa0b0869403

  1package git
  2
  3import (
  4	"log/slog"
  5	"net/http"
  6
  7	"git.gabrielgio.me/cerrado/pkg/service"
  8	"git.gabrielgio.me/cerrado/templates"
  9	"github.com/go-git/go-git/v5/plumbing"
 10	"github.com/go-git/go-git/v5/plumbing/object"
 11	"github.com/gorilla/mux"
 12)
 13
 14type (
 15	GitHandler struct {
 16		gitService gitService
 17	}
 18
 19	gitService interface {
 20		ListRepositories() ([]*service.Repository, error)
 21		ListCommits(name string, ref string) ([]*object.Commit, error)
 22		GetHead(name string) (*plumbing.Reference, error)
 23		ListTags(name string) ([]*object.Tag, error)
 24		ListBranches(name string) ([]*plumbing.Reference, error)
 25	}
 26)
 27
 28func NewGitHandler(gitService gitService) *GitHandler {
 29	return &GitHandler{
 30		gitService: gitService,
 31	}
 32}
 33
 34func (g *GitHandler) List(w http.ResponseWriter, _ *http.Request) {
 35	repos, err := g.gitService.ListRepositories()
 36	if err != nil {
 37		slog.Error("Error listing repo", "error", err)
 38		return
 39	}
 40
 41	gitList := &templates.GitListPage{repos}
 42	templates.WritePageTemplate(w, gitList)
 43}
 44
 45func (g *GitHandler) Summary(w http.ResponseWriter, r *http.Request) {
 46	name := mux.Vars(r)["name"]
 47	ref, err := g.gitService.GetHead(name)
 48	if err != nil {
 49		slog.Error("Error loading head", "error", err)
 50		return
 51	}
 52
 53	gitList := &templates.GitItemPage{
 54		Name:        name,
 55		Ref:         ref.Name().Short(),
 56		GitItemBase: &templates.GitItemSummaryPage{},
 57	}
 58	templates.WritePageTemplate(w, gitList)
 59}
 60
 61func (g *GitHandler) About(w http.ResponseWriter, r *http.Request) {
 62	name := mux.Vars(r)["name"]
 63	ref, err := g.gitService.GetHead(name)
 64	if err != nil {
 65		slog.Error("Error loading head", "error", err)
 66		return
 67	}
 68	gitList := &templates.GitItemPage{
 69		Name:        name,
 70		Ref:         ref.Name().Short(),
 71		GitItemBase: &templates.GitItemAboutPage{},
 72	}
 73	templates.WritePageTemplate(w, gitList)
 74}
 75
 76func (g *GitHandler) Refs(w http.ResponseWriter, r *http.Request) {
 77	name := mux.Vars(r)["name"]
 78
 79	tags, err := g.gitService.ListTags(name)
 80	if err != nil {
 81		slog.Error("Error loading tags", "error", err)
 82		return
 83	}
 84
 85	branches, err := g.gitService.ListBranches(name)
 86	if err != nil {
 87		slog.Error("Error loading branches", "error", err)
 88		return
 89	}
 90
 91	ref, err := g.gitService.GetHead(name)
 92	if err != nil {
 93		slog.Error("Error loading head", "error", err)
 94		return
 95	}
 96
 97	gitList := &templates.GitItemPage{
 98		Name: name,
 99		Ref:  ref.Name().Short(),
100		GitItemBase: &templates.GitItemRefsPage{
101			Tags:     tags,
102			Branches: branches,
103		},
104	}
105	templates.WritePageTemplate(w, gitList)
106}
107
108func (g *GitHandler) Tree(w http.ResponseWriter, r *http.Request) {
109	name := mux.Vars(r)["name"]
110	ref := mux.Vars(r)["ref"]
111	gitList := &templates.GitItemPage{
112		Name:        name,
113		Ref:         ref,
114		GitItemBase: &templates.GitItemTreePage{},
115	}
116	templates.WritePageTemplate(w, gitList)
117}
118
119func (g *GitHandler) Log(w http.ResponseWriter, r *http.Request) {
120	name := mux.Vars(r)["name"]
121	ref := mux.Vars(r)["ref"]
122
123	commits, err := g.gitService.ListCommits(name, ref)
124	if err != nil {
125		slog.Error("Error loading commits", "error", err)
126		return
127	}
128
129	gitList := &templates.GitItemPage{
130		Name: name,
131		Ref:  ref,
132		GitItemBase: &templates.GitItemLogPage{
133			Commits: commits,
134		},
135	}
136	templates.WritePageTemplate(w, gitList)
137}