aboutsummaryrefslogtreecommitdiff
path: root/pkg/service/git.go
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/service/git.go')
-rw-r--r--pkg/service/git.go87
1 files changed, 70 insertions, 17 deletions
diff --git a/pkg/service/git.go b/pkg/service/git.go
index f03ba42..6aa5cd6 100644
--- a/pkg/service/git.go
+++ b/pkg/service/git.go
@@ -2,22 +2,25 @@ package service
import (
"compress/gzip"
+ "context"
"errors"
"io"
"log/slog"
"git.gabrielgio.me/cerrado/pkg/config"
"git.gabrielgio.me/cerrado/pkg/git"
+ gogit "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
)
type (
Repository struct {
- Name string
- Description string
- LastCommitDate string
- Ref string
+ Name string
+ Description string
+ Public bool
+ LastCommit *git.CommitReference
+ Ref string
}
GitService struct {
@@ -30,9 +33,7 @@ type (
}
)
-var (
- ErrRepositoryNotFound = errors.New("Repository not found")
-)
+var ErrRepositoryNotFound = errors.New("Repository not found")
// TODO: make it configurable
const timeFormat = "2006.01.02 15:04:05"
@@ -50,6 +51,10 @@ func (g *GitService) ListRepositories() ([]*Repository, error) {
for _, r := range rs {
repo, err := git.OpenRepository(r.Path)
if err != nil {
+ if errors.Is(err, gogit.ErrRepositoryNotExists) {
+ slog.Info("Path does not contain a repository", "path", r.Path)
+ continue
+ }
return nil, err
}
@@ -66,35 +71,36 @@ func (g *GitService) ListRepositories() ([]*Repository, error) {
}
repos = append(repos, &Repository{
- Name: r.Name,
- Description: r.Description,
- LastCommitDate: obj.Author.When.Format(timeFormat),
- Ref: head.Name().Short(),
+ Name: r.Name,
+ Description: r.Description,
+ Public: r.Public,
+ LastCommit: obj,
+ Ref: head.Name().Short(),
})
}
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) ([]*git.CommitReference, *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) {
+func (g *GitService) LastCommit(name, ref string) (*git.CommitReference, error) {
r := g.configRepo.GetByName(name)
if r == nil {
return nil, ErrRepositoryNotFound
@@ -236,6 +242,25 @@ func (g *GitService) GetAbout(name string) ([]byte, error) {
return file, nil
}
+func (g *GitService) GetTag(ref, name string) (*object.Commit, *git.TagReference, error) {
+ r := g.configRepo.GetByName(name)
+ if r == nil {
+ return nil, nil, ErrRepositoryNotFound
+ }
+
+ repo, err := git.OpenRepository(r.Path)
+ if err != nil {
+ return nil, nil, err
+ }
+
+ err = repo.SetRef(ref)
+ if err != nil {
+ return nil, nil, err
+ }
+
+ return repo.Tag()
+}
+
func (g *GitService) ListTags(name string) ([]*git.TagReference, error) {
r := g.configRepo.GetByName(name)
if r == nil {
@@ -275,3 +300,31 @@ func (g *GitService) GetHead(name string) (*plumbing.Reference, error) {
return repo.Head()
}
+
+func (g *GitService) WriteInfoRefs(ctx context.Context, name string, w io.Writer) error {
+ r := g.configRepo.GetByName(name)
+ if r == nil {
+ return ErrRepositoryNotFound
+ }
+
+ repo, err := git.OpenRepository(r.Path)
+ if err != nil {
+ return err
+ }
+
+ return repo.WriteInfoRefs(ctx, w)
+}
+
+func (g *GitService) WriteUploadPack(ctx context.Context, name string, re io.Reader, w io.Writer) error {
+ r := g.configRepo.GetByName(name)
+ if r == nil {
+ return ErrRepositoryNotFound
+ }
+
+ repo, err := git.OpenRepository(r.Path)
+ if err != nil {
+ return err
+ }
+
+ return repo.WriteUploadPack(ctx, re, w)
+}