From 27400b0fce5d4ef3b7fd5ef4d25bac8f00754e33 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Sat, 15 Feb 2025 17:33:24 +0100 Subject: feat: Add wrapper for commit Add a wrapper to the commit log that also contains possible reference. This will be useful once to later display the reference in the commit log. --- Makefile | 2 +- pkg/git/git.go | 55 +++++++++++++++-- pkg/handler/git/handler.go | 6 +- pkg/service/git.go | 6 +- pkg/u/list.go | 8 +++ templates/gititemlog.qtpl | 5 +- templates/gititemlog.qtpl.go | 105 ++++++++++++++++---------------- templates/gititemsummary.qtpl | 5 +- templates/gititemsummary.qtpl.go | 125 +++++++++++++++++++-------------------- templates/gitlist.qtpl | 6 +- templates/gitlist.qtpl.go | 12 ++-- 11 files changed, 193 insertions(+), 142 deletions(-) diff --git a/Makefile b/Makefile index 80aaf2b..d214a85 100644 --- a/Makefile +++ b/Makefile @@ -33,7 +33,7 @@ sass: $(OUTPUT_CSS) $(OUTPUT_CSS): $(SASS_FILES) @mkdir -p $(CSS_DIR) - sassc $(SASS_DIR)/main.scss $(OUTPUT_CSS) + sassc --style compressed $(SASS_DIR)/main.scss $(OUTPUT_CSS) tmpl: $(GO_TEMPLATES_FILES) diff --git a/pkg/git/git.go b/pkg/git/git.go index b33afa7..9e7f9c9 100644 --- a/pkg/git/git.go +++ b/pkg/git/git.go @@ -32,6 +32,10 @@ type ( ref *plumbing.Reference tag *object.Tag } + CommitReference struct { + commit *object.Commit + ref *plumbing.Reference + } infoWrapper struct { name string size int64 @@ -81,7 +85,7 @@ func (g *GitRepository) Path() string { return g.path } -func (g *GitRepository) LastCommit() (*object.Commit, error) { +func (g *GitRepository) LastCommit() (*CommitReference, error) { err := g.validateRef() if err != nil { return nil, err @@ -91,10 +95,27 @@ func (g *GitRepository) LastCommit() (*object.Commit, error) { if err != nil { return nil, err } - return c, nil + + iter, err := g.repository.Tags() + if err != nil { + return nil, err + } + + commitRef := &CommitReference{commit: c} + if err := iter.ForEach(func(ref *plumbing.Reference) error { + if ref.Hash() != c.Hash { + return nil + } + commitRef.ref = ref + return nil + }); err != nil { + return nil, err + } + + return commitRef, nil } -func (g *GitRepository) Commits(count int, from string) ([]*object.Commit, *object.Commit, error) { +func (g *GitRepository) Commits(count int, from string) ([]*CommitReference, *object.Commit, error) { err := g.validateRef() if err != nil { return nil, nil, err @@ -115,7 +136,7 @@ func (g *GitRepository) Commits(count int, from string) ([]*object.Commit, *obje return nil, nil, fmt.Errorf("commits from ref: %w", err) } - commits := []*object.Commit{} + commitRefs := []*CommitReference{} var next *object.Commit // iterate one more item so we can fetch the next commit @@ -129,11 +150,29 @@ func (g *GitRepository) Commits(count int, from string) ([]*object.Commit, *obje if x == count { next = c } else { - commits = append(commits, c) + commitRefs = append(commitRefs, &CommitReference{commit: c}) } } - return commits, next, nil + // new we fetch for possible tags for each commit + iter, err := g.repository.Tags() + if err != nil { + return nil, nil, err + } + + if err := iter.ForEach(func(ref *plumbing.Reference) error { + for _, c := range commitRefs { + if c.commit.Hash != ref.Hash() { + continue + } + c.ref = ref + } + return nil + }); err != nil { + return nil, nil, err + } + + return commitRefs, next, nil } func (g *GitRepository) Head() (*plumbing.Reference, error) { @@ -454,6 +493,10 @@ func (t *TagReference) Message() string { return "" } +func (c *CommitReference) Commit() *object.Commit { + return c.commit +} + func (self *tagList) Len() int { return len(self.refs) } diff --git a/pkg/handler/git/handler.go b/pkg/handler/git/handler.go index 80f7de6..33adc8d 100644 --- a/pkg/handler/git/handler.go +++ b/pkg/handler/git/handler.go @@ -384,7 +384,7 @@ func (g *GitHandler) Commit(w http.ResponseWriter, r *http.Request) error { Name: name, Ref: ref, GitItemBase: &templates.GitItemCommitPage{ - Commit: commit, + Commit: commit.Commit(), Diff: code.Bytes(), }, } @@ -425,11 +425,11 @@ func orderBy(repos []*service.Repository, order config.OrderBy) []*service.Repos }) case config.LastCommitAsc: sort.Slice(repos, func(i, j int) bool { - return repos[i].LastCommit.Committer.When.Before(repos[j].LastCommit.Committer.When) + return repos[i].LastCommit.Commit().Committer.When.Before(repos[j].LastCommit.Commit().Committer.When) }) case config.LastCommitDesc: sort.Slice(repos, func(i, j int) bool { - return repos[i].LastCommit.Committer.When.After(repos[j].LastCommit.Committer.When) + return repos[i].LastCommit.Commit().Committer.When.After(repos[j].LastCommit.Commit().Committer.When) }) } diff --git a/pkg/service/git.go b/pkg/service/git.go index 773d335..8642b5b 100644 --- a/pkg/service/git.go +++ b/pkg/service/git.go @@ -18,7 +18,7 @@ type ( Name string Description string Public bool - LastCommit *object.Commit + LastCommit *git.CommitReference Ref string } @@ -81,7 +81,7 @@ func (g *GitService) ListRepositories() ([]*Repository, error) { return repos, nil } -func (g *GitService) ListCommits(name, ref, from string, count int) ([]*object.Commit, *object.Commit, error) { +func (g *GitService) ListCommits(name, ref, from string, count int) ([]*git.CommitReference, *object.Commit, error) { r := g.configRepo.GetByName(name) if r == nil { return nil, nil, ErrRepositoryNotFound @@ -99,7 +99,7 @@ func (g *GitService) ListCommits(name, ref, from string, count int) ([]*object.C return repo.Commits(count, from) } -func (g *GitService) LastCommit(name, ref string) (*object.Commit, error) { +func (g *GitService) LastCommit(name, ref string) (*git.CommitReference, error) { r := g.configRepo.GetByName(name) if r == nil { return nil, ErrRepositoryNotFound diff --git a/pkg/u/list.go b/pkg/u/list.go index 835ecd2..1cffbd5 100644 --- a/pkg/u/list.go +++ b/pkg/u/list.go @@ -12,6 +12,14 @@ func Filter[T any](v []T, f func(T) bool) []T { return result } +func Map[T any, V any](a []T, f func(T) V) []V { + result := make([]V, len(a)) + for i, v := range a { + result[i] = f(v) + } + return result +} + func First[T any](v []T) (T, bool) { if len(v) == 0 { var zero T diff --git a/templates/gititemlog.qtpl b/templates/gititemlog.qtpl index b0c8cec..93efb24 100644 --- a/templates/gititemlog.qtpl +++ b/templates/gititemlog.qtpl @@ -1,8 +1,9 @@ +{% import "git.gabrielgio.me/cerrado/pkg/git" %} {% import "github.com/go-git/go-git/v5/plumbing/object" %} {% code type GitItemLogPage struct { - Commits []*object.Commit + Commits []*git.CommitReference Next *object.Commit } %} @@ -12,7 +13,7 @@ type GitItemLogPage struct { {% func (g *GitItemLogPage) GitContent(name, ref string) %}
{% for _, c := range g.Commits %} - {%= Commit(name, c, false) %} + {%= Commit(name, c.Commit(), false) %} {% endfor %} {% if g.Next != nil %} Next diff --git a/templates/gititemlog.qtpl.go b/templates/gititemlog.qtpl.go index 719b71f..05950ed 100644 --- a/templates/gititemlog.qtpl.go +++ b/templates/gititemlog.qtpl.go @@ -5,131 +5,134 @@ package templates //line templates/gititemlog.qtpl:1 +import "git.gabrielgio.me/cerrado/pkg/git" + +//line templates/gititemlog.qtpl:2 import "github.com/go-git/go-git/v5/plumbing/object" -//line templates/gititemlog.qtpl:3 +//line templates/gititemlog.qtpl:4 import ( qtio422016 "io" qt422016 "github.com/valyala/quicktemplate" ) -//line templates/gititemlog.qtpl:3 +//line templates/gititemlog.qtpl:4 var ( _ = qtio422016.Copy _ = qt422016.AcquireByteBuffer ) -//line templates/gititemlog.qtpl:4 +//line templates/gititemlog.qtpl:5 type GitItemLogPage struct { - Commits []*object.Commit + Commits []*git.CommitReference Next *object.Commit } -//line templates/gititemlog.qtpl:10 +//line templates/gititemlog.qtpl:11 func (g *GitItemLogPage) StreamNav(qw422016 *qt422016.Writer, name, ref string) { -//line templates/gititemlog.qtpl:10 +//line templates/gititemlog.qtpl:11 StreamGitItemNav(qw422016, name, ref, Log) -//line templates/gititemlog.qtpl:10 +//line templates/gititemlog.qtpl:11 } -//line templates/gititemlog.qtpl:10 +//line templates/gititemlog.qtpl:11 func (g *GitItemLogPage) WriteNav(qq422016 qtio422016.Writer, name, ref string) { -//line templates/gititemlog.qtpl:10 +//line templates/gititemlog.qtpl:11 qw422016 := qt422016.AcquireWriter(qq422016) -//line templates/gititemlog.qtpl:10 +//line templates/gititemlog.qtpl:11 g.StreamNav(qw422016, name, ref) -//line templates/gititemlog.qtpl:10 +//line templates/gititemlog.qtpl:11 qt422016.ReleaseWriter(qw422016) -//line templates/gititemlog.qtpl:10 +//line templates/gititemlog.qtpl:11 } -//line templates/gititemlog.qtpl:10 +//line templates/gititemlog.qtpl:11 func (g *GitItemLogPage) Nav(name, ref string) string { -//line templates/gititemlog.qtpl:10 +//line templates/gititemlog.qtpl:11 qb422016 := qt422016.AcquireByteBuffer() -//line templates/gititemlog.qtpl:10 +//line templates/gititemlog.qtpl:11 g.WriteNav(qb422016, name, ref) -//line templates/gititemlog.qtpl:10 +//line templates/gititemlog.qtpl:11 qs422016 := string(qb422016.B) -//line templates/gititemlog.qtpl:10 +//line templates/gititemlog.qtpl:11 qt422016.ReleaseByteBuffer(qb422016) -//line templates/gititemlog.qtpl:10 +//line templates/gititemlog.qtpl:11 return qs422016 -//line templates/gititemlog.qtpl:10 +//line templates/gititemlog.qtpl:11 } -//line templates/gititemlog.qtpl:12 +//line templates/gititemlog.qtpl:13 func (g *GitItemLogPage) StreamGitContent(qw422016 *qt422016.Writer, name, ref string) { -//line templates/gititemlog.qtpl:12 +//line templates/gititemlog.qtpl:13 qw422016.N().S(`
`) -//line templates/gititemlog.qtpl:14 - for _, c := range g.Commits { -//line templates/gititemlog.qtpl:14 - qw422016.N().S(` - `) //line templates/gititemlog.qtpl:15 - StreamCommit(qw422016, name, c, false) + for _, c := range g.Commits { //line templates/gititemlog.qtpl:15 qw422016.N().S(` `) //line templates/gititemlog.qtpl:16 - } + StreamCommit(qw422016, name, c.Commit(), false) //line templates/gititemlog.qtpl:16 - qw422016.N().S(` + qw422016.N().S(` `) //line templates/gititemlog.qtpl:17 - if g.Next != nil { + } //line templates/gititemlog.qtpl:17 + qw422016.N().S(` + `) +//line templates/gititemlog.qtpl:18 + if g.Next != nil { +//line templates/gititemlog.qtpl:18 qw422016.N().S(` Next `) -//line templates/gititemlog.qtpl:19 +//line templates/gititemlog.qtpl:20 } -//line templates/gititemlog.qtpl:19 +//line templates/gititemlog.qtpl:20 qw422016.N().S(`
`) -//line templates/gititemlog.qtpl:22 +//line templates/gititemlog.qtpl:23 } -//line templates/gititemlog.qtpl:22 +//line templates/gititemlog.qtpl:23 func (g *GitItemLogPage) WriteGitContent(qq422016 qtio422016.Writer, name, ref string) { -//line templates/gititemlog.qtpl:22 +//line templates/gititemlog.qtpl:23 qw422016 := qt422016.AcquireWriter(qq422016) -//line templates/gititemlog.qtpl:22 +//line templates/gititemlog.qtpl:23 g.StreamGitContent(qw422016, name, ref) -//line templates/gititemlog.qtpl:22 +//line templates/gititemlog.qtpl:23 qt422016.ReleaseWriter(qw422016) -//line templates/gititemlog.qtpl:22 +//line templates/gititemlog.qtpl:23 } -//line templates/gititemlog.qtpl:22 +//line templates/gititemlog.qtpl:23 func (g *GitItemLogPage) GitContent(name, ref string) string { -//line templates/gititemlog.qtpl:22 +//line templates/gititemlog.qtpl:23 qb422016 := qt422016.AcquireByteBuffer() -//line templates/gititemlog.qtpl:22 +//line templates/gititemlog.qtpl:23 g.WriteGitContent(qb422016, name, ref) -//line templates/gititemlog.qtpl:22 +//line templates/gititemlog.qtpl:23 qs422016 := string(qb422016.B) -//line templates/gititemlog.qtpl:22 +//line templates/gititemlog.qtpl:23 qt422016.ReleaseByteBuffer(qb422016) -//line templates/gititemlog.qtpl:22 +//line templates/gititemlog.qtpl:23 return qs422016 -//line templates/gititemlog.qtpl:22 +//line templates/gititemlog.qtpl:23 } diff --git a/templates/gititemsummary.qtpl b/templates/gititemsummary.qtpl index f2de5be..5d07680 100644 --- a/templates/gititemsummary.qtpl +++ b/templates/gititemsummary.qtpl @@ -1,12 +1,11 @@ {% import "github.com/go-git/go-git/v5/plumbing" %} -{% import "github.com/go-git/go-git/v5/plumbing/object" %} {% import "git.gabrielgio.me/cerrado/pkg/git" %} {% code type GitItemSummaryPage struct { Tags []*git.TagReference Branches []*plumbing.Reference - Commits []*object.Commit + Commits []*git.CommitReference } %} @@ -39,7 +38,7 @@ type GitItemSummaryPage struct {
{% for _, c := range g.Commits %} - {%= Commit(name, c, false) %} + {%= Commit(name, c.Commit(), false) %} {% endfor %}
diff --git a/templates/gititemsummary.qtpl.go b/templates/gititemsummary.qtpl.go index 41d5b67..e17ec7c 100644 --- a/templates/gititemsummary.qtpl.go +++ b/templates/gititemsummary.qtpl.go @@ -8,127 +8,124 @@ package templates import "github.com/go-git/go-git/v5/plumbing" //line templates/gititemsummary.qtpl:2 -import "github.com/go-git/go-git/v5/plumbing/object" - -//line templates/gititemsummary.qtpl:3 import "git.gabrielgio.me/cerrado/pkg/git" -//line templates/gititemsummary.qtpl:5 +//line templates/gititemsummary.qtpl:4 import ( qtio422016 "io" qt422016 "github.com/valyala/quicktemplate" ) -//line templates/gititemsummary.qtpl:5 +//line templates/gititemsummary.qtpl:4 var ( _ = qtio422016.Copy _ = qt422016.AcquireByteBuffer ) -//line templates/gititemsummary.qtpl:6 +//line templates/gititemsummary.qtpl:5 type GitItemSummaryPage struct { Tags []*git.TagReference Branches []*plumbing.Reference - Commits []*object.Commit + Commits []*git.CommitReference } -//line templates/gititemsummary.qtpl:13 +//line templates/gititemsummary.qtpl:12 func (g *GitItemSummaryPage) StreamNav(qw422016 *qt422016.Writer, name, ref string) { -//line templates/gititemsummary.qtpl:13 +//line templates/gititemsummary.qtpl:12 StreamGitItemNav(qw422016, name, ref, Summary) -//line templates/gititemsummary.qtpl:13 +//line templates/gititemsummary.qtpl:12 } -//line templates/gititemsummary.qtpl:13 +//line templates/gititemsummary.qtpl:12 func (g *GitItemSummaryPage) WriteNav(qq422016 qtio422016.Writer, name, ref string) { -//line templates/gititemsummary.qtpl:13 +//line templates/gititemsummary.qtpl:12 qw422016 := qt422016.AcquireWriter(qq422016) -//line templates/gititemsummary.qtpl:13 +//line templates/gititemsummary.qtpl:12 g.StreamNav(qw422016, name, ref) -//line templates/gititemsummary.qtpl:13 +//line templates/gititemsummary.qtpl:12 qt422016.ReleaseWriter(qw422016) -//line templates/gititemsummary.qtpl:13 +//line templates/gititemsummary.qtpl:12 } -//line templates/gititemsummary.qtpl:13 +//line templates/gititemsummary.qtpl:12 func (g *GitItemSummaryPage) Nav(name, ref string) string { -//line templates/gititemsummary.qtpl:13 +//line templates/gititemsummary.qtpl:12 qb422016 := qt422016.AcquireByteBuffer() -//line templates/gititemsummary.qtpl:13 +//line templates/gititemsummary.qtpl:12 g.WriteNav(qb422016, name, ref) -//line templates/gititemsummary.qtpl:13 +//line templates/gititemsummary.qtpl:12 qs422016 := string(qb422016.B) -//line templates/gititemsummary.qtpl:13 +//line templates/gititemsummary.qtpl:12 qt422016.ReleaseByteBuffer(qb422016) -//line templates/gititemsummary.qtpl:13 +//line templates/gititemsummary.qtpl:12 return qs422016 -//line templates/gititemsummary.qtpl:13 +//line templates/gititemsummary.qtpl:12 } -//line templates/gititemsummary.qtpl:15 +//line templates/gititemsummary.qtpl:14 func (g *GitItemSummaryPage) StreamGitContent(qw422016 *qt422016.Writer, name, ref string) { -//line templates/gititemsummary.qtpl:15 +//line templates/gititemsummary.qtpl:14 qw422016.N().S(`
`) -//line templates/gititemsummary.qtpl:18 +//line templates/gititemsummary.qtpl:17 StreamListTags(qw422016, name, g.Tags) -//line templates/gititemsummary.qtpl:18 +//line templates/gititemsummary.qtpl:17 qw422016.N().S(`
`) -//line templates/gititemsummary.qtpl:22 +//line templates/gititemsummary.qtpl:21 for _, b := range g.Branches { -//line templates/gititemsummary.qtpl:22 +//line templates/gititemsummary.qtpl:21 qw422016.N().S(`
`) -//line templates/gititemsummary.qtpl:25 +//line templates/gititemsummary.qtpl:24 qw422016.E().S(b.Name().Short()) -//line templates/gititemsummary.qtpl:25 +//line templates/gititemsummary.qtpl:24 qw422016.N().S(`
`) -//line templates/gititemsummary.qtpl:35 +//line templates/gititemsummary.qtpl:34 } -//line templates/gititemsummary.qtpl:35 +//line templates/gititemsummary.qtpl:34 qw422016.N().S(`
@@ -136,48 +133,48 @@ func (g *GitItemSummaryPage) StreamGitContent(qw422016 *qt422016.Writer, name, r
`) -//line templates/gititemsummary.qtpl:41 +//line templates/gititemsummary.qtpl:40 for _, c := range g.Commits { -//line templates/gititemsummary.qtpl:41 +//line templates/gititemsummary.qtpl:40 qw422016.N().S(` `) -//line templates/gititemsummary.qtpl:42 - StreamCommit(qw422016, name, c, false) -//line templates/gititemsummary.qtpl:42 +//line templates/gititemsummary.qtpl:41 + StreamCommit(qw422016, name, c.Commit(), false) +//line templates/gititemsummary.qtpl:41 qw422016.N().S(` `) -//line templates/gititemsummary.qtpl:43 +//line templates/gititemsummary.qtpl:42 } -//line templates/gititemsummary.qtpl:43 +//line templates/gititemsummary.qtpl:42 qw422016.N().S(`
`) -//line templates/gititemsummary.qtpl:46 +//line templates/gititemsummary.qtpl:45 } -//line templates/gititemsummary.qtpl:46 +//line templates/gititemsummary.qtpl:45 func (g *GitItemSummaryPage) WriteGitContent(qq422016 qtio422016.Writer, name, ref string) { -//line templates/gititemsummary.qtpl:46 +//line templates/gititemsummary.qtpl:45 qw422016 := qt422016.AcquireWriter(qq422016) -//line templates/gititemsummary.qtpl:46 +//line templates/gititemsummary.qtpl:45 g.StreamGitContent(qw422016, name, ref) -//line templates/gititemsummary.qtpl:46 +//line templates/gititemsummary.qtpl:45 qt422016.ReleaseWriter(qw422016) -//line templates/gititemsummary.qtpl:46 +//line templates/gititemsummary.qtpl:45 } -//line templates/gititemsummary.qtpl:46 +//line templates/gititemsummary.qtpl:45 func (g *GitItemSummaryPage) GitContent(name, ref string) string { -//line templates/gititemsummary.qtpl:46 +//line templates/gititemsummary.qtpl:45 qb422016 := qt422016.AcquireByteBuffer() -//line templates/gititemsummary.qtpl:46 +//line templates/gititemsummary.qtpl:45 g.WriteGitContent(qb422016, name, ref) -//line templates/gititemsummary.qtpl:46 +//line templates/gititemsummary.qtpl:45 qs422016 := string(qb422016.B) -//line templates/gititemsummary.qtpl:46 +//line templates/gititemsummary.qtpl:45 qt422016.ReleaseByteBuffer(qb422016) -//line templates/gititemsummary.qtpl:46 +//line templates/gititemsummary.qtpl:45 return qs422016 -//line templates/gititemsummary.qtpl:46 +//line templates/gititemsummary.qtpl:45 } diff --git a/templates/gitlist.qtpl b/templates/gitlist.qtpl index 3cd133f..e62e68a 100644 --- a/templates/gitlist.qtpl +++ b/templates/gitlist.qtpl @@ -41,9 +41,9 @@ type GitListPage struct {

{%s r.Description %}

log diff --git a/templates/gitlist.qtpl.go b/templates/gitlist.qtpl.go index 1c0fdac..d2b28d9 100644 --- a/templates/gitlist.qtpl.go +++ b/templates/gitlist.qtpl.go @@ -156,7 +156,7 @@ func (p *GitListPage) StreamContent(qw422016 *qt422016.Writer, ctx context.Conte

-- cgit v1.2.3