From 907c13566c775ac1210bc66de99f7273e7c8a507 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Tue, 25 Jun 2024 20:37:40 +0200 Subject: feat: Add commit page --- pkg/handler/git/handler.go | 22 ++++++++++++++++++++++ pkg/handler/router.go | 1 + pkg/service/git.go | 19 +++++++++++++++++++ 3 files changed, 42 insertions(+) (limited to 'pkg') diff --git a/pkg/handler/git/handler.go b/pkg/handler/git/handler.go index 0963033..fd62e44 100644 --- a/pkg/handler/git/handler.go +++ b/pkg/handler/git/handler.go @@ -34,6 +34,7 @@ type ( gitService interface { ListRepositories() ([]*service.Repository, error) ListCommits(name string, ref string, count int) ([]*object.Commit, error) + LastCommit(name string, ref string) (*object.Commit, error) GetHead(name string) (*plumbing.Reference, error) GetTree(name, ref, path string) (*object.Tree, error) IsBinary(name, ref, path string) (bool, error) @@ -331,6 +332,27 @@ func (g *GitHandler) Log(w http.ResponseWriter, r *http.Request) error { return nil } +func (g *GitHandler) Commit(w http.ResponseWriter, r *http.Request) error { + ext.SetHTML(w) + name := r.PathValue("name") + ref := r.PathValue("ref") + + commit, err := g.gitService.LastCommit(name, ref) + if err != nil { + return err + } + + gitList := &templates.GitItemPage{ + Name: name, + Ref: ref, + GitItemBase: &templates.GitItemCommitPage{ + Commit: commit, + }, + } + templates.WritePageTemplate(w, gitList) + return nil +} + func GetLexers(filename string) chroma.Lexer { if filename == "APKBUILD" { return lexers.Get("sh") diff --git a/pkg/handler/router.go b/pkg/handler/router.go index 6ee7ba3..f464ac2 100644 --- a/pkg/handler/router.go +++ b/pkg/handler/router.go @@ -41,6 +41,7 @@ func MountHandler( mux.HandleFunc("/{name}/tree/{ref}/{rest...}", gitHandler.Tree) mux.HandleFunc("/{name}/blob/{ref}/{rest...}", gitHandler.Blob) mux.HandleFunc("/{name}/log/{ref}/", gitHandler.Log) + mux.HandleFunc("/{name}/commit/{ref}/", gitHandler.Commit) mux.HandleFunc("/{name}/archive/{file}", gitHandler.Archive) mux.HandleFunc("/config", configHandler) mux.HandleFunc("/about", aboutHandler.About) diff --git a/pkg/service/git.go b/pkg/service/git.go index 1d21204..df4e3aa 100644 --- a/pkg/service/git.go +++ b/pkg/service/git.go @@ -94,6 +94,25 @@ func (g *GitService) ListCommits(name, ref string, count int) ([]*object.Commit, return repo.Commits(count) } +func (g *GitService) LastCommit(name, ref string) (*object.Commit, error) { + r := g.configRepo.GetByName(name) + if r == nil { + return nil, ErrRepositoryNotFound + } + + repo, err := git.OpenRepository(r.Path) + if err != nil { + return nil, err + } + + err = repo.SetRef(ref) + if err != nil { + return nil, err + } + + return repo.LastCommit() +} + func (g *GitService) WriteTarGZip(w io.Writer, name, ref string, prefix string) error { r := g.configRepo.GetByName(name) if r == nil { -- cgit v1.2.3