aboutsummaryrefslogtreecommitdiff
path: root/pkg/handler/git/handler.go
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/handler/git/handler.go')
-rw-r--r--pkg/handler/git/handler.go81
1 files changed, 78 insertions, 3 deletions
diff --git a/pkg/handler/git/handler.go b/pkg/handler/git/handler.go
index e2f4042..f3e74c7 100644
--- a/pkg/handler/git/handler.go
+++ b/pkg/handler/git/handler.go
@@ -1,11 +1,17 @@
package git
import (
+ "bytes"
"log/slog"
"net/http"
+ "path/filepath"
"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"
)
@@ -19,6 +25,8 @@ type (
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)
}
@@ -107,10 +115,64 @@ func (g *GitHandler) Refs(w http.ResponseWriter, r *http.Request) {
func (g *GitHandler) Tree(w http.ResponseWriter, r *http.Request) {
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{},
+ 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) {
+ 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)
}
@@ -134,3 +196,16 @@ func (g *GitHandler) Log(w http.ResponseWriter, r *http.Request) {
}
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
+}