Skip to content

Commit

Permalink
fix: cache repo references
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Feb 17, 2022
1 parent 3308f43 commit 286dedb
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 27 deletions.
25 changes: 20 additions & 5 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,29 @@ type Repo struct {
LastUpdated *time.Time
refCommits map[plumbing.Hash]gitypes.Commits
ref *plumbing.Reference
refs []*plumbing.Reference
}

// GetName returns the name of the repository.
func (r *Repo) GetName() string {
return r.Name
}

// GetReference returns the reference for a repository.
func (r *Repo) GetReference() *plumbing.Reference {
// GetHEAD returns the reference for a repository.
func (r *Repo) GetHEAD() *plumbing.Reference {
return r.ref
}

// SetReference sets the repository head reference.
func (r *Repo) SetReference(ref *plumbing.Reference) error {
// SetHEAD sets the repository head reference.
func (r *Repo) SetHEAD(ref *plumbing.Reference) error {
r.ref = ref
return nil
}

func (r *Repo) GetReferences() []*plumbing.Reference {
return r.refs
}

// GetRepository returns the underlying go-git repository object.
func (r *Repo) GetRepository() *git.Repository {
return r.Repository
Expand Down Expand Up @@ -273,13 +278,23 @@ func (rs *RepoSource) loadRepo(name string, rg *git.Repository) (*Repo, error) {
return nil, err
}
r.Readme = rm
refs := make([]*plumbing.Reference, 0)
ri, err := rg.References()
if err != nil {
return nil, err
}
ri.ForEach(func(r *plumbing.Reference) error {
refs = append(refs, r)
return nil
})
r.refs = refs
return r, nil
}

// 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{
From: r.GetReference().Hash(),
From: r.GetHEAD().Hash(),
})
if err != nil {
return "", err
Expand Down
2 changes: 1 addition & 1 deletion internal/tui/bubbles/git/about/bubble.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewBubble(repo types.Repo, styles *style.Styles, width, wm, height, hm int)
styles: styles,
widthMargin: wm,
heightMargin: hm,
ref: repo.GetReference(),
ref: repo.GetHEAD(),
}
b.SetSize(width, height)
return b
Expand Down
2 changes: 1 addition & 1 deletion internal/tui/bubbles/git/bubble.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func NewBubble(repo types.Repo, styles *style.Styles, width, wm, height, hm int)
heightMargin: hm,
style: styles,
boxes: make([]tea.Model, 4),
ref: repo.GetReference(),
ref: repo.GetHEAD(),
}
heightMargin := hm + lipgloss.Height(b.headerView())
b.boxes[aboutPage] = about.NewBubble(repo, b.style, b.width, wm, b.height, heightMargin)
Expand Down
2 changes: 1 addition & 1 deletion internal/tui/bubbles/git/log/bubble.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func NewBubble(repo types.Repo, styles *style.Styles, width, widthMargin, height
height: height,
heightMargin: heightMargin,
list: l,
ref: repo.GetReference(),
ref: repo.GetHEAD(),
}
b.SetSize(width, height)
return b
Expand Down
24 changes: 9 additions & 15 deletions internal/tui/bubbles/git/refs/bubble.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func NewBubble(repo types.Repo, styles *style.Styles, width, widthMargin, height
widthMargin: widthMargin,
heightMargin: heightMargin,
list: l,
ref: repo.GetReference(),
ref: repo.GetHEAD(),
}
b.SetSize(width, height)
return b
Expand Down Expand Up @@ -123,21 +123,15 @@ func (b *Bubble) Help() []types.HelpEntry {
func (b *Bubble) updateItems() tea.Cmd {
its := make(items, 0)
tags := make(items, 0)
ri, err := b.repo.GetRepository().References()
if err != nil {
return nil
}
if err = ri.ForEach(func(r *plumbing.Reference) error {
if r.Type() == plumbing.HashReference {
if r.Name().IsTag() {
tags = append(tags, item{r})
} else {
its = append(its, item{r})
}
for _, r := range b.repo.GetReferences() {
if r.Type() != plumbing.HashReference {
continue
}
if r.Name().IsTag() {
tags = append(tags, item{r})
} else {
its = append(its, item{r})
}
return nil
}); err != nil {
return nil
}
sort.Sort(its)
sort.Sort(tags)
Expand Down
4 changes: 2 additions & 2 deletions internal/tui/bubbles/git/tree/bubble.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func NewBubble(repo types.Repo, styles *style.Styles, width, widthMargin, height
heightMargin: heightMargin,
list: l,
state: treeState,
ref: repo.GetReference(),
ref: repo.GetHEAD(),
}
b.SetSize(width, height)
return b
Expand Down Expand Up @@ -222,7 +222,7 @@ func (b *Bubble) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

case tea.KeyMsg:
if b.state == errorState {
ref := b.repo.GetReference()
ref := b.repo.GetHEAD()
b.ref = ref
return b, tea.Batch(b.reset(), func() tea.Msg {
return ref
Expand Down
5 changes: 3 additions & 2 deletions internal/tui/bubbles/git/types/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (

type Repo interface {
GetName() string
GetReference() *plumbing.Reference
SetReference(*plumbing.Reference) error
GetHEAD() *plumbing.Reference
SetHEAD(*plumbing.Reference) error
GetReferences() []*plumbing.Reference
GetReadme() string
GetCommits(*plumbing.Reference) (Commits, error)
GetRepository() *git.Repository
Expand Down

0 comments on commit 286dedb

Please sign in to comment.