From 4392caaeceef1ca3cd204321271ad3672af13727 Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Thu, 17 Feb 2022 13:48:06 -0500 Subject: [PATCH] refactor: git Repo interface naming --- internal/config/config.go | 4 +-- internal/git/git.go | 50 +++++++++++++-------------- internal/tui/bubbles/git/bubble.go | 4 +-- internal/tui/bubbles/git/types/git.go | 4 +-- internal/tui/commands.go | 7 ++-- server/middleware.go | 4 +-- 6 files changed, 36 insertions(+), 37 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index 2b1ad55c3..eb6d3bcc9 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -152,7 +152,7 @@ func (cfg *Config) createDefaultConfigRepo(yaml string) error { if err != nil { return err } - wt, err := cr.Repository.Worktree() + wt, err := cr.Repository().Worktree() if err != nil { return err } @@ -190,7 +190,7 @@ func (cfg *Config) createDefaultConfigRepo(yaml string) error { if err != nil { return err } - err = cr.Repository.Push(&gg.PushOptions{}) + err = cr.Repository().Push(&gg.PushOptions{}) if err != nil { return err } diff --git a/internal/git/git.go b/internal/git/git.go index 02b9ac6ef..fe21bc782 100644 --- a/internal/git/git.go +++ b/internal/git/git.go @@ -7,7 +7,6 @@ import ( "path/filepath" "sort" "sync" - "time" gitypes "github.com/charmbracelet/soft-serve/internal/tui/bubbles/git/types" "github.com/go-git/go-billy/v5/memfs" @@ -23,28 +22,27 @@ var ErrMissingRepo = errors.New("missing repo") // Repo represents a Git repository. type Repo struct { - Name string - Repository *git.Repository - Readme string - LastUpdated *time.Time - refCommits map[plumbing.Hash]gitypes.Commits - ref *plumbing.Reference - refs []*plumbing.Reference + name string + repository *git.Repository + Readme string + refCommits map[plumbing.Hash]gitypes.Commits + head *plumbing.Reference + refs []*plumbing.Reference } // GetName returns the name of the repository. -func (r *Repo) GetName() string { - return r.Name +func (r *Repo) Name() string { + return r.name } // GetHEAD returns the reference for a repository. func (r *Repo) GetHEAD() *plumbing.Reference { - return r.ref + return r.head } // SetHEAD sets the repository head reference. func (r *Repo) SetHEAD(ref *plumbing.Reference) error { - r.ref = ref + r.head = ref return nil } @@ -53,8 +51,8 @@ func (r *Repo) GetReferences() []*plumbing.Reference { } // GetRepository returns the underlying go-git repository object. -func (r *Repo) GetRepository() *git.Repository { - return r.Repository +func (r *Repo) Repository() *git.Repository { + return r.repository } // Tree returns the git tree for a given path. @@ -64,7 +62,7 @@ func (r *Repo) Tree(ref *plumbing.Reference, path string) (*object.Tree, error) if err != nil { return nil, err } - c, err := r.Repository.CommitObject(hash) + c, err := r.repository.CommitObject(hash) if err != nil { return nil, err } @@ -89,9 +87,9 @@ func (r *Repo) GetCommits(ref *plumbing.Reference) (gitypes.Commits, error) { if ok { return commits, nil } - log.Printf("caching commits for %s/%s: %s", r.Name, ref.Name(), ref.Hash()) + log.Printf("caching commits for %s/%s: %s", r.name, ref.Name(), ref.Hash()) commits = gitypes.Commits{} - co, err := r.Repository.CommitObject(hash) + co, err := r.repository.CommitObject(hash) if err != nil { return nil, err } @@ -124,7 +122,7 @@ func (r *Repo) targetHash(ref *plumbing.Reference) (plumbing.Hash, error) { return plumbing.ZeroHash, plumbing.ErrInvalidType } if ref.Name().IsTag() { - to, err := r.Repository.TagObject(hash) + to, err := r.repository.TagObject(hash) switch err { case nil: // annotated tag (object has a target hash) @@ -145,7 +143,7 @@ func (r *Repo) loadCommits(ref *plumbing.Reference) (gitypes.Commits, error) { if err != nil { return nil, err } - l, err := r.Repository.Log(&git.LogOptions{ + l, err := r.repository.Log(&git.LogOptions{ Order: git.LogOrderCommitterTime, From: hash, }) @@ -204,7 +202,7 @@ func (rs *RepoSource) GetRepo(name string) (*Repo, error) { rs.mtx.Lock() defer rs.mtx.Unlock() for _, r := range rs.repos { - if r.Name == name { + if r.name == name { return r, nil } } @@ -231,8 +229,8 @@ func (rs *RepoSource) InitRepo(name string, bare bool) (*Repo, error) { rg = ar } r := &Repo{ - Name: name, - Repository: rg, + name: name, + repository: rg, } rs.repos = append(rs.repos, r) return r, nil @@ -264,15 +262,15 @@ func (rs *RepoSource) LoadRepos() error { func (rs *RepoSource) loadRepo(name string, rg *git.Repository) (*Repo, error) { r := &Repo{ - Name: name, - Repository: rg, + name: name, + repository: rg, } r.refCommits = make(map[plumbing.Hash]gitypes.Commits) ref, err := rg.Head() if err != nil { return nil, err } - r.ref = ref + r.head = ref rm, err := r.LatestFile("README.md") if err != nil { return nil, err @@ -293,7 +291,7 @@ func (rs *RepoSource) loadRepo(name string, rg *git.Repository) (*Repo, error) { // LatestFile returns the latest file at the specified path in the repository. func (r *Repo) LatestFile(path string) (string, error) { - lg, err := r.Repository.Log(&git.LogOptions{ + lg, err := r.repository.Log(&git.LogOptions{ From: r.GetHEAD().Hash(), }) if err != nil { diff --git a/internal/tui/bubbles/git/bubble.go b/internal/tui/bubbles/git/bubble.go index d9258b701..a6ed04a1d 100644 --- a/internal/tui/bubbles/git/bubble.go +++ b/internal/tui/bubbles/git/bubble.go @@ -65,7 +65,7 @@ func (b *Bubble) Update(msg tea.Msg) (tea.Model, tea.Cmd) { cmds := make([]tea.Cmd, 0) switch msg := msg.(type) { case tea.KeyMsg: - if b.repo.GetName() != "config" { + if b.repo.Name() != "config" { switch msg.String() { case "R": b.state = aboutPage @@ -109,7 +109,7 @@ func (b *Bubble) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (b *Bubble) Help() []types.HelpEntry { h := []types.HelpEntry{} h = append(h, b.boxes[b.state].(types.BubbleHelper).Help()...) - if b.repo.GetName() != "config" { + if b.repo.Name() != "config" { h = append(h, types.HelpEntry{"R", "readme"}) h = append(h, types.HelpEntry{"F", "files"}) h = append(h, types.HelpEntry{"C", "commits"}) diff --git a/internal/tui/bubbles/git/types/git.go b/internal/tui/bubbles/git/types/git.go index a4b78b91f..de6bfd1aa 100644 --- a/internal/tui/bubbles/git/types/git.go +++ b/internal/tui/bubbles/git/types/git.go @@ -7,13 +7,13 @@ import ( ) type Repo interface { - GetName() string + Name() string GetHEAD() *plumbing.Reference SetHEAD(*plumbing.Reference) error GetReferences() []*plumbing.Reference GetReadme() string GetCommits(*plumbing.Reference) (Commits, error) - GetRepository() *git.Repository + Repository() *git.Repository Tree(*plumbing.Reference, string) (*object.Tree, error) } diff --git a/internal/tui/commands.go b/internal/tui/commands.go index 4a6f34507..7eef46ba6 100644 --- a/internal/tui/commands.go +++ b/internal/tui/commands.go @@ -75,17 +75,18 @@ func (b *Bubble) menuEntriesFromSource() ([]MenuEntry, error) { } for _, r := range b.config.Source.AllRepos() { var found bool + rn := r.Name() for _, me := range mes { - if me.Repo == r.Name { + if me.Repo == rn { found = true } } if !found { - acc := b.config.AuthRepo(r.Name, b.session.PublicKey()) + acc := b.config.AuthRepo(rn, b.session.PublicKey()) if acc == gm.NoAccess { continue } - me, err := b.newMenuEntry(r.Name, r.Name) + me, err := b.newMenuEntry(rn, rn) if err != nil { return nil, err } diff --git a/server/middleware.go b/server/middleware.go index faa995969..1ef875941 100644 --- a/server/middleware.go +++ b/server/middleware.go @@ -36,7 +36,7 @@ func softServeMiddleware(ac *appCfg.Config) wish.Middleware { } repoExists := false for _, rp := range ac.Source.AllRepos() { - if rp.Name == repo { + if rp.Name() == repo { repoExists = true } } @@ -62,7 +62,7 @@ func softServeMiddleware(ac *appCfg.Config) wish.Middleware { _ = s.Exit(1) return } - fc, err := readFile(rs.Repository, strings.Join(ps[1:], "/")) + fc, err := readFile(rs.Repository(), strings.Join(ps[1:], "/")) if err != nil { _, _ = s.Write([]byte(err.Error())) _ = s.Exit(1)