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(string) ([]*object.Commit, error)
22 ListTags(string) ([]*object.Tag, error)
23 ListBranches(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 := mux.Vars(r)["name"]
46 gitList := &templates.GitItemPage{
47 Name: name,
48 GitItemBase: &templates.GitItemSummaryPage{},
49 }
50 templates.WritePageTemplate(w, gitList)
51}
52
53func (g *GitHandler) About(w http.ResponseWriter, r *http.Request) {
54 name := mux.Vars(r)["name"]
55 gitList := &templates.GitItemPage{
56 Name: name,
57 GitItemBase: &templates.GitItemAboutPage{},
58 }
59 templates.WritePageTemplate(w, gitList)
60}
61
62func (g *GitHandler) Refs(w http.ResponseWriter, r *http.Request) {
63 name := mux.Vars(r)["name"]
64
65 tags, err := g.gitService.ListTags(name)
66 if err != nil {
67 slog.Error("Error loading tags", "error", err)
68 return
69 }
70
71 branches, err := g.gitService.ListBranches(name)
72 if err != nil {
73 slog.Error("Error loading branches", "error", err)
74 return
75 }
76
77 gitList := &templates.GitItemPage{
78 Name: name,
79 GitItemBase: &templates.GitItemRefsPage{
80 Tags: tags,
81 Branches: branches,
82 },
83 }
84 templates.WritePageTemplate(w, gitList)
85}
86
87func (g *GitHandler) Tree(w http.ResponseWriter, r *http.Request) {
88 name := mux.Vars(r)["name"]
89 gitList := &templates.GitItemPage{
90 Name: name,
91 GitItemBase: &templates.GitItemTreePage{},
92 }
93 templates.WritePageTemplate(w, gitList)
94}
95
96func (g *GitHandler) Log(w http.ResponseWriter, r *http.Request) {
97 name := mux.Vars(r)["name"]
98
99 commits, err := g.gitService.ListCommits(name)
100 if err != nil {
101 slog.Error("Error loading commits", "error", err)
102 return
103 }
104
105 gitList := &templates.GitItemPage{
106 Name: name,
107 GitItemBase: &templates.GitItemLogPage{
108 Commits: commits,
109 },
110 }
111 templates.WritePageTemplate(w, gitList)
112}