aboutsummaryrefslogtreecommitdiff
path: root/pkg/handler/git/handler.go
blob: 096303374b4fa36a7a62cf4f97a4bea41cb44c73 (plain)
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package git

import (
	"bytes"
	"errors"
	"fmt"
	"io"
	"log/slog"
	"net/http"
	"os"
	"path/filepath"
	"strings"

	"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"
	"github.com/gomarkdown/markdown"
	markdownhtml "github.com/gomarkdown/markdown/html"
	"github.com/gomarkdown/markdown/parser"
)

type (
	GitHandler struct {
		gitService gitService
		readmePath string
	}

	gitService interface {
		ListRepositories() ([]*service.Repository, error)
		ListCommits(name string, ref string, count int) ([]*object.Commit, error)
		GetHead(name string) (*plumbing.Reference, error)
		GetTree(name, ref, path string) (*object.Tree, error)
		IsBinary(name, ref, path string) (bool, error)
		GetFileContent(name, ref, path string) ([]byte, error)
		GetAbout(name string) ([]byte, error)
		ListTags(name string) ([]*plumbing.Reference, error)
		ListBranches(name string) ([]*plumbing.Reference, error)
		WriteTarGZip(w io.Writer, name, ref, prefix string) error
	}

	configurationRepository interface {
		GetRootReadme() string
	}
)

func NewGitHandler(gitService gitService, confRepo configurationRepository) *GitHandler {
	return &GitHandler{
		gitService: gitService,
		readmePath: confRepo.GetRootReadme(),
	}
}

func (g *GitHandler) List(w http.ResponseWriter, _ *http.Request) error {
	repos, err := g.gitService.ListRepositories()
	if err != nil {
		return err
	}

	f, err := os.Open(g.readmePath)
	if err != nil {
		return err
	}

	bs, err := io.ReadAll(f)
	if err != nil {
		return err
	}

	extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock
	p := parser.NewWithExtensions(extensions)
	doc := p.Parse(bs)

	htmlFlag := markdownhtml.CommonFlags | markdownhtml.HrefTargetBlank
	opts := markdownhtml.RendererOptions{Flags: htmlFlag}
	renderer := markdownhtml.NewRenderer(opts)

	bs = markdown.Render(doc, renderer)

	gitList := &templates.GitListPage{
		Respositories: repos,
		About:         bs,
	}
	templates.WritePageTemplate(w, gitList)
	return nil
}

func (g *GitHandler) Archive(w http.ResponseWriter, r *http.Request) error {
	ext.SetGZip(w)
	name := r.PathValue("name")
	file := r.PathValue("file")
	ref := strings.TrimSuffix(file, ".tar.gz")

	// TODO: remove it once we can support more than gzip
	if !strings.HasSuffix(file, ".tar.gz") {
		ext.NotFound(w)
		return nil
	}

	filename := fmt.Sprintf("%s-%s.tar.gz", name, ref)
	ext.SetFileName(w, filename)

	prefix := fmt.Sprintf("%s-%s", name, ref)
	err := g.gitService.WriteTarGZip(w, name, ref, prefix)
	if err != nil {
		// once we start writing to the body we can't report error anymore
		// so we are only left with printing the error.
		slog.Error("Error generating tar gzip file", "error", err)
	}

	return nil
}

func (g *GitHandler) Summary(w http.ResponseWriter, r *http.Request) error {
	ext.SetHTML(w)
	name := r.PathValue("name")
	ref, err := g.gitService.GetHead(name)
	if err != nil {
		return err
	}

	tags, err := g.gitService.ListTags(name)
	if err != nil {
		return err
	}

	branches, err := g.gitService.ListBranches(name)
	if err != nil {
		return err
	}

	commits, err := g.gitService.ListCommits(name, "", 10)
	if err != nil {
		return err
	}

	gitList := &templates.GitItemPage{
		Name: name,
		Ref:  ref.Name().Short(),
		GitItemBase: &templates.GitItemSummaryPage{
			Tags:     tags,
			Branches: branches,
			Commits:  commits,
		},
	}
	templates.WritePageTemplate(w, gitList)
	return nil
}

func (g *GitHandler) About(w http.ResponseWriter, r *http.Request) error {
	ext.SetHTML(w)
	name := r.PathValue("name")
	ref, err := g.gitService.GetHead(name)
	if err != nil {
		return err
	}

	file, err := g.gitService.GetAbout(name)
	if errors.Is(err, object.ErrFileNotFound) {
		templates.WritePageTemplate(w, &templates.GitItemPage{
			Name: name,
			Ref:  ref.Name().Short(),
			GitItemBase: &templates.GitItemAboutPage{
				About: []byte("About file not configured properly"),
			},
		})
		return nil
	}
	if err != nil {
		return err
	}

	extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock
	p := parser.NewWithExtensions(extensions)
	doc := p.Parse(file)

	htmlFlag := markdownhtml.CommonFlags | markdownhtml.HrefTargetBlank
	opts := markdownhtml.RendererOptions{Flags: htmlFlag}
	renderer := markdownhtml.NewRenderer(opts)

	bs := markdown.Render(doc, renderer)

	gitList := &templates.GitItemPage{
		Name: name,
		Ref:  ref.Name().Short(),
		GitItemBase: &templates.GitItemAboutPage{
			About: bs,
		},
	}
	templates.WritePageTemplate(w, gitList)
	return nil
}

func (g *GitHandler) Refs(w http.ResponseWriter, r *http.Request) error {
	ext.SetHTML(w)
	name := r.PathValue("name")

	tags, err := g.gitService.ListTags(name)
	if err != nil {
		return err
	}

	branches, err := g.gitService.ListBranches(name)
	if err != nil {
		return err
	}

	ref, err := g.gitService.GetHead(name)
	if err != nil {
		return err
	}

	gitList := &templates.GitItemPage{
		Name: name,
		Ref:  ref.Name().Short(),
		GitItemBase: &templates.GitItemRefsPage{
			Tags:     tags,
			Branches: branches,
		},
	}
	templates.WritePageTemplate(w, gitList)
	return nil
}

func (g *GitHandler) Tree(w http.ResponseWriter, r *http.Request) error {
	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 {
		return err
	}

	gitList := &templates.GitItemPage{
		Name: name,
		Ref:  ref,
		GitItemBase: &templates.GitItemTreePage{
			CurrentPath: rest,
			Tree:        tree,
		},
	}
	templates.WritePageTemplate(w, gitList)
	return nil
}

func (g *GitHandler) Blob(w http.ResponseWriter, r *http.Request) error {
	ext.SetHTML(w)
	name := r.PathValue("name")
	ref := r.PathValue("ref")
	rest := r.PathValue("rest")

	isBin, err := g.gitService.IsBinary(name, ref, rest)
	if err != nil {
		return err
	}

	// if it is binary no need to over all the chroma process
	if isBin {
		gitList := &templates.GitItemPage{
			Name: name,
			Ref:  ref,
			GitItemBase: &templates.GitItemBlobPage{
				File:    rest,
				Content: []byte("Binary file"),
			},
		}
		templates.WritePageTemplate(w, gitList)
		return nil
	}

	file, err := g.gitService.GetFileContent(name, ref, rest)
	if err != nil {
		return err
	}

	filename := filepath.Base(rest)
	lexer := GetLexers(filename)
	style := styles.Get("xcode")
	formatter := html.New(
		html.WithLineNumbers(true),
	)

	iterator, err := lexer.Tokenise(nil, string(file))
	if err != nil {
		return err
	}

	var code bytes.Buffer
	err = formatter.Format(&code, style, iterator)
	if err != nil {
		return err
	}

	gitList := &templates.GitItemPage{
		Name: name,
		Ref:  ref,
		GitItemBase: &templates.GitItemBlobPage{
			File:    rest,
			Content: code.Bytes(),
		},
	}
	templates.WritePageTemplate(w, gitList)
	return nil
}

func (g *GitHandler) Log(w http.ResponseWriter, r *http.Request) error {
	ext.SetHTML(w)
	name := r.PathValue("name")
	ref := r.PathValue("ref")

	commits, err := g.gitService.ListCommits(name, ref, 1000)
	if err != nil {
		return err
	}

	gitList := &templates.GitItemPage{
		Name: name,
		Ref:  ref,
		GitItemBase: &templates.GitItemLogPage{
			Commits: commits,
		},
	}
	templates.WritePageTemplate(w, gitList)
	return nil
}

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
}