aboutsummaryrefslogtreecommitdiff
path: root/pkg/git/git.go
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/git/git.go
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/git/git.go')
-rw-r--r--pkg/git/git.go37
1 files changed, 25 insertions, 12 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 {