From f1643ffcbd543cacfe9ab5e46eafdd0c39cd47fd Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Sat, 1 Jun 2024 18:24:22 +0200 Subject: feat: Add refs page UI now it is a broken state. I'll all pages working first so I can better style it later. --- pkg/git/git.go | 48 +++++++++++++++ pkg/handler/git/handler.go | 23 ++++++- pkg/service/git.go | 17 ++++++ scss/main.scss | 15 ++--- templates/gititemlog.qtpl | 8 +-- templates/gititemlog.qtpl.go | 10 +-- templates/gititemrefs.qtpl | 38 +++++++++++- templates/gititemrefs.qtpl.go | 137 ++++++++++++++++++++++++++++++++---------- 8 files changed, 241 insertions(+), 55 deletions(-) diff --git a/pkg/git/git.go b/pkg/git/git.go index 80c0e46..7ef23f7 100644 --- a/pkg/git/git.go +++ b/pkg/git/git.go @@ -6,6 +6,7 @@ import ( "io" "github.com/go-git/go-git/v5" + "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/object" ) @@ -54,6 +55,7 @@ func (g *GitRepository) Commits() ([]*object.Commit, error) { if err != nil { return nil, err } + ref, err := repo.Head() if err != nil { return nil, errors.Join(MissingHeadErr, err) @@ -81,3 +83,49 @@ func (g *GitRepository) Commits() ([]*object.Commit, error) { return commits, nil } + +func (g *GitRepository) Tags() ([]*object.Tag, error) { + repo, err := git.PlainOpen(g.path) + if err != nil { + return nil, err + } + + ti, err := repo.TagObjects() + if err != nil { + return nil, err + } + + tags := []*object.Tag{} + err = ti.ForEach(func(t *object.Tag) error { + tags = append(tags, t) + return nil + }) + if err != nil { + return nil, err + } + + return tags, nil +} + +func (g *GitRepository) Branches() ([]*plumbing.Reference, error) { + repo, err := git.PlainOpen(g.path) + if err != nil { + return nil, err + } + + bs, err := repo.Branches() + if err != nil { + return nil, err + } + + branches := []*plumbing.Reference{} + err = bs.ForEach(func(ref *plumbing.Reference) error { + branches = append(branches, ref) + return nil + }) + if err != nil { + return nil, err + } + + return branches, nil +} diff --git a/pkg/handler/git/handler.go b/pkg/handler/git/handler.go index ebfb37f..d090f22 100644 --- a/pkg/handler/git/handler.go +++ b/pkg/handler/git/handler.go @@ -6,6 +6,7 @@ import ( "git.gabrielgio.me/cerrado/pkg/service" "git.gabrielgio.me/cerrado/templates" + "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/object" "github.com/gorilla/mux" ) @@ -18,6 +19,8 @@ type ( gitService interface { ListRepositories() ([]*service.Repository, error) ListCommits(string) ([]*object.Commit, error) + ListTags(string) ([]*object.Tag, error) + ListBranches(string) ([]*plumbing.Reference, error) } ) @@ -58,9 +61,25 @@ func (g *GitHandler) About(w http.ResponseWriter, r *http.Request) { func (g *GitHandler) Refs(w http.ResponseWriter, r *http.Request) { name := mux.Vars(r)["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 + } + gitList := &templates.GitItemPage{ - Name: name, - GitItemBase: &templates.GitItemRefsPage{}, + Name: name, + GitItemBase: &templates.GitItemRefsPage{ + Tags: tags, + Branches: branches, + }, } templates.WritePageTemplate(w, gitList) } diff --git a/pkg/service/git.go b/pkg/service/git.go index 614770f..57b9b6e 100644 --- a/pkg/service/git.go +++ b/pkg/service/git.go @@ -5,6 +5,7 @@ import ( "git.gabrielgio.me/cerrado/pkg/config" "git.gabrielgio.me/cerrado/pkg/git" + "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/object" ) @@ -65,3 +66,19 @@ func (g *GitService) ListCommits(name string) ([]*object.Commit, error) { repo := git.NewGitRepository(r.Path) return repo.Commits() } + +func (g *GitService) ListTags(name string) ([]*object.Tag, error) { + // TODO: handle nil + r := g.configRepo.GetByName(name) + + repo := git.NewGitRepository(r.Path) + return repo.Tags() +} + +func (g *GitService) ListBranches(name string) ([]*plumbing.Reference, error) { + // TODO: handle nil + r := g.configRepo.GetByName(name) + + repo := git.NewGitRepository(r.Path) + return repo.Branches() +} diff --git a/scss/main.scss b/scss/main.scss index 9f17dad..bb7d7f0 100644 --- a/scss/main.scss +++ b/scss/main.scss @@ -80,15 +80,10 @@ body { .logs pre::first-line { font-weight: bold; } - -@include media-breakpoint-down(sm) { - // add extra spacing then list is seen on vertical - .logs>div>div:first-child { - margin-bottom: 15px; - } - .logs>div>div:last-child { - margin-top: 15px; - } +.logs>div>div:first-child { + margin-bottom: 15px; +} +.logs>div>div:last-child { + margin-top: 15px; } - diff --git a/templates/gititemlog.qtpl b/templates/gititemlog.qtpl index a39fb77..436c1d2 100644 --- a/templates/gititemlog.qtpl +++ b/templates/gititemlog.qtpl @@ -12,14 +12,14 @@ type GitItemLogPage struct {
{% for _, c := range g.Commits %}
-
+
{%s TimeFormat(c.Committer.When) %}
-
+
{%s c.Message %}
-
- {%s c.Committer.Name %} +
+ {%s c.Committer.Name %}
{% endfor %} diff --git a/templates/gititemlog.qtpl.go b/templates/gititemlog.qtpl.go index cc5652d..e63c871 100644 --- a/templates/gititemlog.qtpl.go +++ b/templates/gititemlog.qtpl.go @@ -69,26 +69,26 @@ func (g *GitItemLogPage) StreamGitContent(qw422016 *qt422016.Writer) { //line gititemlog.qtpl:13 qw422016.N().S(`
-
+
`) //line gititemlog.qtpl:16 qw422016.E().S(TimeFormat(c.Committer.When)) //line gititemlog.qtpl:16 qw422016.N().S(`
-
+
`)
 //line gititemlog.qtpl:19
 		qw422016.E().S(c.Message)
 //line gititemlog.qtpl:19
 		qw422016.N().S(`
-
- `) +
+ `) //line gititemlog.qtpl:22 qw422016.E().S(c.Committer.Name) //line gititemlog.qtpl:22 - qw422016.N().S(` + qw422016.N().S(`
`) diff --git a/templates/gititemrefs.qtpl b/templates/gititemrefs.qtpl index 5fed393..9c58863 100644 --- a/templates/gititemrefs.qtpl +++ b/templates/gititemrefs.qtpl @@ -1,10 +1,46 @@ +{% import "github.com/go-git/go-git/v5/plumbing" %} +{% import "github.com/go-git/go-git/v5/plumbing/object" %} + {% code type GitItemRefsPage struct { + Tags []*object.Tag + Branches []*plumbing.Reference } %} {% func (g *GitItemRefsPage) Nav(name string) %}{%= GitItemNav(name ,Refs) %}{% endfunc %} {% func (g *GitItemRefsPage) GitContent() %} -

Refs

+
+
+

Tags

+
+ {% for _, t := range g.Tags %} +
+
+ {%s TimeFormat(t.Tagger.When) %} +
+
+
{%s t.Message %}
+
+
+ {%s t.Tagger.Name %} +
+
+ {% endfor %} +
+
+
+

Branches

+
+ {% for _, b := range g.Branches %} +
+
+ {%s b.String() %} +
+
+ {% endfor %} +
+
+
{% endfunc %} diff --git a/templates/gititemrefs.qtpl.go b/templates/gititemrefs.qtpl.go index 5294bf9..f2d2b6f 100644 --- a/templates/gititemrefs.qtpl.go +++ b/templates/gititemrefs.qtpl.go @@ -5,86 +5,157 @@ package templates //line gititemrefs.qtpl:1 +import "github.com/go-git/go-git/v5/plumbing" + +//line gititemrefs.qtpl:2 +import "github.com/go-git/go-git/v5/plumbing/object" + +//line gititemrefs.qtpl:4 import ( qtio422016 "io" qt422016 "github.com/valyala/quicktemplate" ) -//line gititemrefs.qtpl:1 +//line gititemrefs.qtpl:4 var ( _ = qtio422016.Copy _ = qt422016.AcquireByteBuffer ) -//line gititemrefs.qtpl:2 +//line gititemrefs.qtpl:5 type GitItemRefsPage struct { + Tags []*object.Tag + Branches []*plumbing.Reference } -//line gititemrefs.qtpl:6 +//line gititemrefs.qtpl:11 func (g *GitItemRefsPage) StreamNav(qw422016 *qt422016.Writer, name string) { -//line gititemrefs.qtpl:6 +//line gititemrefs.qtpl:11 StreamGitItemNav(qw422016, name, Refs) -//line gititemrefs.qtpl:6 +//line gititemrefs.qtpl:11 } -//line gititemrefs.qtpl:6 +//line gititemrefs.qtpl:11 func (g *GitItemRefsPage) WriteNav(qq422016 qtio422016.Writer, name string) { -//line gititemrefs.qtpl:6 +//line gititemrefs.qtpl:11 qw422016 := qt422016.AcquireWriter(qq422016) -//line gititemrefs.qtpl:6 +//line gititemrefs.qtpl:11 g.StreamNav(qw422016, name) -//line gititemrefs.qtpl:6 +//line gititemrefs.qtpl:11 qt422016.ReleaseWriter(qw422016) -//line gititemrefs.qtpl:6 +//line gititemrefs.qtpl:11 } -//line gititemrefs.qtpl:6 +//line gititemrefs.qtpl:11 func (g *GitItemRefsPage) Nav(name string) string { -//line gititemrefs.qtpl:6 +//line gititemrefs.qtpl:11 qb422016 := qt422016.AcquireByteBuffer() -//line gititemrefs.qtpl:6 +//line gititemrefs.qtpl:11 g.WriteNav(qb422016, name) -//line gititemrefs.qtpl:6 +//line gititemrefs.qtpl:11 qs422016 := string(qb422016.B) -//line gititemrefs.qtpl:6 +//line gititemrefs.qtpl:11 qt422016.ReleaseByteBuffer(qb422016) -//line gititemrefs.qtpl:6 +//line gititemrefs.qtpl:11 return qs422016 -//line gititemrefs.qtpl:6 +//line gititemrefs.qtpl:11 } -//line gititemrefs.qtpl:8 +//line gititemrefs.qtpl:13 func (g *GitItemRefsPage) StreamGitContent(qw422016 *qt422016.Writer) { -//line gititemrefs.qtpl:8 +//line gititemrefs.qtpl:13 + qw422016.N().S(` +
+
+

Tags

+
+ `) +//line gititemrefs.qtpl:18 + for _, t := range g.Tags { +//line gititemrefs.qtpl:18 + qw422016.N().S(` +
+
+ `) +//line gititemrefs.qtpl:21 + qw422016.E().S(TimeFormat(t.Tagger.When)) +//line gititemrefs.qtpl:21 + qw422016.N().S(` +
+
+
`)
+//line gititemrefs.qtpl:24
+		qw422016.E().S(t.Message)
+//line gititemrefs.qtpl:24
+		qw422016.N().S(`
+
+
+ `) +//line gititemrefs.qtpl:27 + qw422016.E().S(t.Tagger.Name) +//line gititemrefs.qtpl:27 + qw422016.N().S(` +
+
+ `) +//line gititemrefs.qtpl:30 + } +//line gititemrefs.qtpl:30 + qw422016.N().S(` +
+
+
+

Branches

+
+ `) +//line gititemrefs.qtpl:36 + for _, b := range g.Branches { +//line gititemrefs.qtpl:36 + qw422016.N().S(` +
+
+ `) +//line gititemrefs.qtpl:39 + qw422016.E().S(b.String()) +//line gititemrefs.qtpl:39 + qw422016.N().S(` +
+
+ `) +//line gititemrefs.qtpl:42 + } +//line gititemrefs.qtpl:42 qw422016.N().S(` -

Refs

+
+
+
`) -//line gititemrefs.qtpl:10 +//line gititemrefs.qtpl:46 } -//line gititemrefs.qtpl:10 +//line gititemrefs.qtpl:46 func (g *GitItemRefsPage) WriteGitContent(qq422016 qtio422016.Writer) { -//line gititemrefs.qtpl:10 +//line gititemrefs.qtpl:46 qw422016 := qt422016.AcquireWriter(qq422016) -//line gititemrefs.qtpl:10 +//line gititemrefs.qtpl:46 g.StreamGitContent(qw422016) -//line gititemrefs.qtpl:10 +//line gititemrefs.qtpl:46 qt422016.ReleaseWriter(qw422016) -//line gititemrefs.qtpl:10 +//line gititemrefs.qtpl:46 } -//line gititemrefs.qtpl:10 +//line gititemrefs.qtpl:46 func (g *GitItemRefsPage) GitContent() string { -//line gititemrefs.qtpl:10 +//line gititemrefs.qtpl:46 qb422016 := qt422016.AcquireByteBuffer() -//line gititemrefs.qtpl:10 +//line gititemrefs.qtpl:46 g.WriteGitContent(qb422016) -//line gititemrefs.qtpl:10 +//line gititemrefs.qtpl:46 qs422016 := string(qb422016.B) -//line gititemrefs.qtpl:10 +//line gititemrefs.qtpl:46 qt422016.ReleaseByteBuffer(qb422016) -//line gititemrefs.qtpl:10 +//line gititemrefs.qtpl:46 return qs422016 -//line gititemrefs.qtpl:10 +//line gititemrefs.qtpl:46 } -- cgit v1.2.3