diff options
author | Gabriel A. Giovanini <mail@gabrielgio.me> | 2025-02-15 17:33:24 +0100 |
---|---|---|
committer | Gabriel A. Giovanini <mail@gabrielgio.me> | 2025-02-15 17:33:24 +0100 |
commit | 27400b0fce5d4ef3b7fd5ef4d25bac8f00754e33 (patch) | |
tree | b1e4df482d7c78e1d7fc56e862b7c266ba2b8e8b /pkg | |
parent | 4708ad8ffd96f3b457ff9d3a9660e7d0aa59a4b1 (diff) | |
download | cerrado-27400b0fce5d4ef3b7fd5ef4d25bac8f00754e33.tar.gz cerrado-27400b0fce5d4ef3b7fd5ef4d25bac8f00754e33.tar.bz2 cerrado-27400b0fce5d4ef3b7fd5ef4d25bac8f00754e33.zip |
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.
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/git/git.go | 55 | ||||
-rw-r--r-- | pkg/handler/git/handler.go | 6 | ||||
-rw-r--r-- | pkg/service/git.go | 6 | ||||
-rw-r--r-- | pkg/u/list.go | 8 |
4 files changed, 63 insertions, 12 deletions
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 |