aboutsummaryrefslogtreecommitdiff
path: root/pkg
diff options
context:
space:
mode:
authorGabriel A. Giovanini <mail@gabrielgio.me>2025-01-26 14:53:14 +0100
committerGabriel A. Giovanini <mail@gabrielgio.me>2025-01-26 14:54:52 +0100
commitcb9036aab96895ddf03cab68f75d3356f5227968 (patch)
treebc97024b0e947a0624579552955ce7c1ea47c572 /pkg
parentf128f0a029430cb5ab4f89733d8297666a6b2fdf (diff)
downloadcerrado-cb9036aab96895ddf03cab68f75d3356f5227968.tar.gz
cerrado-cb9036aab96895ddf03cab68f75d3356f5227968.tar.bz2
cerrado-cb9036aab96895ddf03cab68f75d3356f5227968.zip
feat: Add log paginationHEADv0.0.19master
Now it is possible to navigate through all commit from a given repository. It implements a simple navigation which will plenty for now.
Diffstat (limited to 'pkg')
-rw-r--r--pkg/git/git.go37
-rw-r--r--pkg/handler/git/handler.go6
-rw-r--r--pkg/service/git.go10
3 files changed, 34 insertions, 19 deletions
diff --git a/pkg/git/git.go b/pkg/git/git.go
index 6221e33..b33afa7 100644
--- a/pkg/git/git.go
+++ b/pkg/git/git.go
@@ -16,8 +16,6 @@ import (
"github.com/go-git/go-git/v5/plumbing/object"
)
-var ()
-
var (
MissingRefErr = errors.New("Reference not found")
TreeForFileErr = errors.New("Trying to get tree of a file")
@@ -96,30 +94,46 @@ func (g *GitRepository) LastCommit() (*object.Commit, error) {
return c, nil
}
-func (g *GitRepository) Commits(count int) ([]*object.Commit, error) {
+func (g *GitRepository) Commits(count int, from string) ([]*object.Commit, *object.Commit, error) {
err := g.validateRef()
if err != nil {
- return nil, err
+ return nil, nil, err
+ }
+
+ opts := &git.LogOptions{Order: git.LogOrderCommitterTime}
+
+ if from != "" {
+ hash, err := g.repository.ResolveRevision(plumbing.Revision(from))
+ if err != nil {
+ return nil, nil, errors.Join(MissingRefErr, err)
+ }
+ opts.From = *hash
}
- ci, err := g.repository.Log(&git.LogOptions{From: g.ref})
+ ci, err := g.repository.Log(opts)
if err != nil {
- return nil, fmt.Errorf("commits from ref: %w", err)
+ return nil, nil, fmt.Errorf("commits from ref: %w", err)
}
commits := []*object.Commit{}
- // TODO: for now only load first 1000
- for x := 0; x < count; x++ {
+ var next *object.Commit
+
+ // iterate one more item so we can fetch the next commit
+ for x := 0; x < (count + 1); x++ {
c, err := ci.Next()
if err != nil && errors.Is(err, io.EOF) {
break
} else if err != nil {
- return nil, err
+ return nil, nil, err
+ }
+ if x == count {
+ next = c
+ } else {
+ commits = append(commits, c)
}
- commits = append(commits, c)
}
- return commits, nil
+ return commits, next, nil
}
func (g *GitRepository) Head() (*plumbing.Reference, error) {
@@ -438,7 +452,6 @@ func (t *TagReference) Message() string {
return t.tag.Message
}
return ""
-
}
func (self *tagList) Len() int {
diff --git a/pkg/handler/git/handler.go b/pkg/handler/git/handler.go
index 6225b1a..436d364 100644
--- a/pkg/handler/git/handler.go
+++ b/pkg/handler/git/handler.go
@@ -130,7 +130,7 @@ func (g *GitHandler) Summary(w http.ResponseWriter, r *http.Request) error {
return err
}
- commits, err := g.gitService.ListCommits(name, "", 10)
+ commits, _, err := g.gitService.ListCommits(name, "", "", 10)
if err != nil {
return err
}
@@ -324,8 +324,9 @@ func (g *GitHandler) Log(w http.ResponseWriter, r *http.Request) error {
ext.SetHTML(w)
name := r.PathValue("name")
ref := r.PathValue("ref")
+ from := r.URL.Query().Get("from")
- commits, err := g.gitService.ListCommits(name, ref, 1000)
+ commits, next, err := g.gitService.ListCommits(name, ref, from, 100)
if err != nil {
return err
}
@@ -335,6 +336,7 @@ func (g *GitHandler) Log(w http.ResponseWriter, r *http.Request) error {
Ref: ref,
GitItemBase: &templates.GitItemLogPage{
Commits: commits,
+ Next: next,
},
}
templates.WritePageTemplate(w, gitList, r.Context())
diff --git a/pkg/service/git.go b/pkg/service/git.go
index 12d238e..2d00715 100644
--- a/pkg/service/git.go
+++ b/pkg/service/git.go
@@ -81,22 +81,22 @@ func (g *GitService) ListRepositories() ([]*Repository, error) {
return repos, nil
}
-func (g *GitService) ListCommits(name, ref string, count int) ([]*object.Commit, error) {
+func (g *GitService) ListCommits(name, ref, from string, count int) ([]*object.Commit, *object.Commit, error) {
r := g.configRepo.GetByName(name)
if r == nil {
- return nil, ErrRepositoryNotFound
+ return nil, nil, ErrRepositoryNotFound
}
repo, err := git.OpenRepository(r.Path)
if err != nil {
- return nil, err
+ return nil, nil, err
}
err = repo.SetRef(ref)
if err != nil {
- return nil, err
+ return nil, nil, err
}
- return repo.Commits(count)
+ return repo.Commits(count, from)
}
func (g *GitService) LastCommit(name, ref string) (*object.Commit, error) {