aboutsummaryrefslogtreecommitdiff
path: root/pkg
diff options
context:
space:
mode:
authorGabriel A. Giovanini <mail@gabrielgio.me>2024-07-07 20:28:42 +0200
committerGabriel A. Giovanini <mail@gabrielgio.me>2024-07-07 21:44:29 +0200
commit44bc8e4078a09857ad86691a83e7ba7d4e3a69c4 (patch)
tree6788cafeaac286d381cc69e1917dab6ae58d1f85 /pkg
parent8dff852753a1c4a708fd87e3cbb0f4844803aa95 (diff)
downloadcerrado-44bc8e4078a09857ad86691a83e7ba7d4e3a69c4.tar.gz
cerrado-44bc8e4078a09857ad86691a83e7ba7d4e3a69c4.tar.bz2
cerrado-44bc8e4078a09857ad86691a83e7ba7d4e3a69c4.zip
ref: Simplify path builder code
Diffstat (limited to 'pkg')
-rw-r--r--pkg/git/git.go48
-rw-r--r--pkg/handler/git/handler.go19
-rw-r--r--pkg/service/git.go2
-rw-r--r--pkg/u/file.go44
-rw-r--r--pkg/u/file_test.go59
5 files changed, 136 insertions, 36 deletions
diff --git a/pkg/git/git.go b/pkg/git/git.go
index 6b58d35..7341c1b 100644
--- a/pkg/git/git.go
+++ b/pkg/git/git.go
@@ -38,6 +38,11 @@ type (
modTime time.Time
isDir bool
}
+
+ TagReference struct {
+ ref *plumbing.Reference
+ tag *object.Tag
+ }
)
func OpenRepository(dir string) (*GitRepository, error) {
@@ -119,18 +124,31 @@ func (g *GitRepository) Head() (*plumbing.Reference, error) {
return g.repository.Head()
}
-func (g *GitRepository) Tags() ([]*plumbing.Reference, error) {
- ti, err := g.repository.Tags()
+func (g *GitRepository) Tags() ([]*TagReference, error) {
+ iter, err := g.repository.Tags()
if err != nil {
return nil, err
}
- tags := []*plumbing.Reference{}
- err = ti.ForEach(func(t *plumbing.Reference) error {
- tags = append(tags, t)
+ tags := make([]*TagReference, 0)
+
+ if err := iter.ForEach(func(ref *plumbing.Reference) error {
+ obj, err := g.repository.TagObject(ref.Hash())
+ switch err {
+ case nil:
+ tags = append(tags, &TagReference{
+ ref: ref,
+ tag: obj,
+ })
+ case plumbing.ErrObjectNotFound:
+ tags = append(tags, &TagReference{
+ ref: ref,
+ })
+ default:
+ return err
+ }
return nil
- })
- if err != nil {
+ }); err != nil {
return nil, err
}
@@ -361,3 +379,19 @@ func (i *infoWrapper) IsDir() bool {
func (i *infoWrapper) Sys() any {
return nil
}
+
+func (t *TagReference) HashString() string {
+ return t.ref.Hash().String()
+}
+
+func (t *TagReference) ShortName() string {
+ return t.ref.Name().Short()
+}
+
+func (t *TagReference) Message() string {
+ if t.tag != nil {
+ return t.tag.Message
+ }
+ return ""
+
+}
diff --git a/pkg/handler/git/handler.go b/pkg/handler/git/handler.go
index 5e50122..9549f0e 100644
--- a/pkg/handler/git/handler.go
+++ b/pkg/handler/git/handler.go
@@ -18,7 +18,6 @@ import (
"github.com/alecthomas/chroma/v2/formatters/html"
"github.com/alecthomas/chroma/v2/lexers"
"github.com/alecthomas/chroma/v2/styles"
- "github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/gomarkdown/markdown"
markdownhtml "github.com/gomarkdown/markdown/html"
@@ -27,30 +26,16 @@ import (
type (
GitHandler struct {
- gitService gitService
+ gitService *service.GitService
readmePath string
}
- gitService interface {
- ListRepositories() ([]*service.Repository, error)
- ListCommits(name string, ref string, count int) ([]*object.Commit, error)
- LastCommit(name string, ref string) (*object.Commit, error)
- GetHead(name string) (*plumbing.Reference, error)
- GetTree(name, ref, path string) (*object.Tree, error)
- IsBinary(name, ref, path string) (bool, error)
- GetFileContent(name, ref, path string) ([]byte, error)
- GetAbout(name string) ([]byte, error)
- ListTags(name string) ([]*plumbing.Reference, error)
- ListBranches(name string) ([]*plumbing.Reference, error)
- WriteTarGZip(w io.Writer, name, ref, prefix string) error
- }
-
configurationRepository interface {
GetRootReadme() string
}
)
-func NewGitHandler(gitService gitService, confRepo configurationRepository) *GitHandler {
+func NewGitHandler(gitService *service.GitService, confRepo configurationRepository) *GitHandler {
return &GitHandler{
gitService: gitService,
readmePath: confRepo.GetRootReadme(),
diff --git a/pkg/service/git.go b/pkg/service/git.go
index df4e3aa..b368f0c 100644
--- a/pkg/service/git.go
+++ b/pkg/service/git.go
@@ -217,7 +217,7 @@ func (g *GitService) GetAbout(name string) ([]byte, error) {
return file, nil
}
-func (g *GitService) ListTags(name string) ([]*plumbing.Reference, error) {
+func (g *GitService) ListTags(name string) ([]*git.TagReference, error) {
r := g.configRepo.GetByName(name)
if r == nil {
return nil, ErrRepositoryNotFound
diff --git a/pkg/u/file.go b/pkg/u/file.go
index fafe0fb..5010b3e 100644
--- a/pkg/u/file.go
+++ b/pkg/u/file.go
@@ -4,7 +4,7 @@ import (
"errors"
"log/slog"
"os"
- "path/filepath"
+ "strings"
)
func FileExist(filename string) bool {
@@ -22,21 +22,43 @@ func FileExist(filename string) bool {
}
// This is just a slin wraper to make easier to compose path in the template
-type Pathing string
+type Pathing struct {
+ sb strings.Builder
+}
-func Root() Pathing {
- return "/"
+func NewPathing() *Pathing {
+ return &Pathing{}
}
-func (s Pathing) AddPath(p string) Pathing {
- return Pathing(filepath.Join(string(s), p))
+func (s *Pathing) AddPath(p string) *Pathing {
+ if len(p) == 0 {
+ return s
+ }
+
+ // if it has trailing / remove it
+ if p[len(p)-1] == '/' {
+ p = p[:len(p)-1]
+ return s.AddPath(p)
+ }
+
+ // if it does not have it so add
+ if p[0] == '/' {
+ s.sb.WriteString(p)
+ } else {
+ s.sb.WriteString("/" + p)
+ }
+
+ return s
}
-func (s Pathing) AddPaths(p []string) Pathing {
- f := filepath.Join(p...)
- return Pathing(filepath.Join(string(s), f))
+func (s *Pathing) AddPaths(p []string) *Pathing {
+ for _, v := range p {
+ s.AddPath(v)
+ }
+
+ return s
}
-func (s Pathing) Done() string {
- return string(s)
+func (s *Pathing) Done() string {
+ return s.sb.String()
}
diff --git a/pkg/u/file_test.go b/pkg/u/file_test.go
new file mode 100644
index 0000000..b7d6975
--- /dev/null
+++ b/pkg/u/file_test.go
@@ -0,0 +1,59 @@
+// go:build unit
+package u
+
+import "testing"
+
+func TestPathing(t *testing.T) {
+ testCases := []struct {
+ name string
+ in []any
+ out string
+ }{
+ {
+ name: "root",
+ in: []any{},
+ out: "",
+ },
+ {
+ name: "empty",
+ in: []any{
+ "/",
+ []string{"/", "/"},
+ "/",
+ []string{"/"},
+ },
+ out: "",
+ },
+ {
+ name: "empty",
+ in: []any{
+ "usr",
+ []string{"/share/", "lib"},
+ "/demo",
+ []string{"/out//"},
+ },
+ out: "/usr/share/lib/demo/out",
+ },
+ }
+
+ for _, tc := range testCases {
+ t.Run(tc.name, func(t *testing.T) {
+ r := NewPathing()
+
+ for _, v := range tc.in {
+ switch s := v.(type) {
+ case string:
+ r = r.AddPath(s)
+ case []string:
+ r = r.AddPaths(s)
+ }
+ }
+
+ path := r.Done()
+ if tc.out != path {
+ t.Errorf("String mismatch: wanted %s got %s", tc.out, path)
+ }
+
+ })
+ }
+}