1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
package git
import (
"bytes"
"log/slog"
"net/http"
"path/filepath"
"git.gabrielgio.me/cerrado/pkg/ext"
"git.gabrielgio.me/cerrado/pkg/service"
"git.gabrielgio.me/cerrado/templates"
"github.com/alecthomas/chroma/v2"
"github.com/alecthomas/chroma/v2/formatters/html"
"github.com/alecthomas/chroma/v2/lexers"
"github.com/alecthomas/chroma/v2/styles"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
)
type (
GitHandler struct {
gitService gitService
}
gitService interface {
ListRepositories() ([]*service.Repository, error)
ListCommits(name string, ref string) ([]*object.Commit, error)
GetHead(name string) (*plumbing.Reference, error)
GetTree(name, ref, path string) (*object.Tree, error)
GetFileContent(name, ref, path string) (string, error)
ListTags(name string) ([]*object.Tag, error)
ListBranches(name string) ([]*plumbing.Reference, error)
}
)
func NewGitHandler(gitService gitService) *GitHandler {
return &GitHandler{
gitService: gitService,
}
}
func (g *GitHandler) List(w http.ResponseWriter, _ *http.Request) {
repos, err := g.gitService.ListRepositories()
if err != nil {
slog.Error("Error listing repo", "error", err)
return
}
gitList := &templates.GitListPage{repos}
templates.WritePageTemplate(w, gitList)
}
func (g *GitHandler) Summary(w http.ResponseWriter, r *http.Request) {
ext.SetHTML(w)
name := r.PathValue("name")
ref, err := g.gitService.GetHead(name)
if err != nil {
slog.Error("Error loading head", "error", err)
return
}
gitList := &templates.GitItemPage{
Name: name,
Ref: ref.Name().Short(),
GitItemBase: &templates.GitItemSummaryPage{},
}
templates.WritePageTemplate(w, gitList)
}
func (g *GitHandler) About(w http.ResponseWriter, r *http.Request) {
ext.SetHTML(w)
name := r.PathValue("name")
ref, err := g.gitService.GetHead(name)
if err != nil {
slog.Error("Error loading head", "error", err)
return
}
gitList := &templates.GitItemPage{
Name: name,
Ref: ref.Name().Short(),
GitItemBase: &templates.GitItemAboutPage{},
}
templates.WritePageTemplate(w, gitList)
}
func (g *GitHandler) Refs(w http.ResponseWriter, r *http.Request) {
ext.SetHTML(w)
name := r.PathValue("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
}
ref, err := g.gitService.GetHead(name)
if err != nil {
slog.Error("Error loading head", "error", err)
return
}
gitList := &templates.GitItemPage{
Name: name,
Ref: ref.Name().Short(),
GitItemBase: &templates.GitItemRefsPage{
Tags: tags,
Branches: branches,
},
}
templates.WritePageTemplate(w, gitList)
}
func (g *GitHandler) Tree(w http.ResponseWriter, r *http.Request) {
ext.SetHTML(w)
name := r.PathValue("name")
ref := r.PathValue("ref")
rest := r.PathValue("rest")
tree, err := g.gitService.GetTree(name, ref, rest)
if err != nil {
slog.Error("Error loading tree", "error", err)
return
}
gitList := &templates.GitItemPage{
Name: name,
Ref: ref,
GitItemBase: &templates.GitItemTreePage{
CurrentPath: rest,
Tree: tree,
Ref: ref,
Name: name,
},
}
templates.WritePageTemplate(w, gitList)
}
func (g *GitHandler) Blob(w http.ResponseWriter, r *http.Request) {
ext.SetHTML(w)
name := r.PathValue("name")
ref := r.PathValue("ref")
rest := r.PathValue("rest")
file, err := g.gitService.GetFileContent(name, ref, rest)
if err != nil {
slog.Error("Error loading blob", "error", err)
return
}
filename := filepath.Base(rest)
lexer := GetLexers(filename)
style := styles.Get("xcode")
formatter := html.New(
html.WithLineNumbers(true),
)
iterator, err := lexer.Tokenise(nil, file)
if err != nil {
slog.Error("Error tokenise", "error", err)
return
}
var code bytes.Buffer
err = formatter.Format(&code, style, iterator)
if err != nil {
slog.Error("Error format", "error", err)
return
}
gitList := &templates.GitItemPage{
Name: name,
Ref: ref,
GitItemBase: &templates.GitItemBlobPage{
File: rest,
Content: code.Bytes(),
},
}
templates.WritePageTemplate(w, gitList)
}
func (g *GitHandler) Log(w http.ResponseWriter, r *http.Request) {
ext.SetHTML(w)
name := r.PathValue("name")
ref := r.PathValue("ref")
commits, err := g.gitService.ListCommits(name, ref)
if err != nil {
slog.Error("Error loading commits", "error", err)
return
}
gitList := &templates.GitItemPage{
Name: name,
Ref: ref,
GitItemBase: &templates.GitItemLogPage{
Commits: commits,
},
}
templates.WritePageTemplate(w, gitList)
}
func GetLexers(filename string) chroma.Lexer {
if filename == "APKBUILD" {
return lexers.Get("sh")
}
lexer := lexers.Get(filename)
if lexer == nil {
lexer = lexers.Get("txt")
}
return lexer
}
|