From 44bc8e4078a09857ad86691a83e7ba7d4e3a69c4 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Sun, 7 Jul 2024 20:28:42 +0200 Subject: ref: Simplify path builder code --- pkg/git/git.go | 48 +++++++++++++++++++++++++++++++------ pkg/handler/git/handler.go | 19 ++------------- pkg/service/git.go | 2 +- pkg/u/file.go | 44 +++++++++++++++++++++++++--------- pkg/u/file_test.go | 59 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 136 insertions(+), 36 deletions(-) create mode 100644 pkg/u/file_test.go (limited to 'pkg') 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) + } + + }) + } +} -- cgit v1.2.3