Skip to content

Commit

Permalink
refactor: git Repo interface naming
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Feb 17, 2022
1 parent 286dedb commit 4392caa
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 37 deletions.
4 changes: 2 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
50 changes: 24 additions & 26 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}

Expand All @@ -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.
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
Expand All @@ -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,
})
Expand Down Expand Up @@ -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
}
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions internal/tui/bubbles/git/bubble.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"})
Expand Down
4 changes: 2 additions & 2 deletions internal/tui/bubbles/git/types/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
7 changes: 4 additions & 3 deletions internal/tui/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions server/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand All @@ -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)
Expand Down

0 comments on commit 4392caa

Please sign in to comment.