cerrado @ d0e0c1eb99303e1000140d4b98c610077278dc42

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