286dedb633f878a5038f8b834d1d556429864d1d

Author
Ayman Bagabas <ayman.bagabas@gmail.com>
Committer
Ayman Bagabas <ayman.bagabas@gmail.com>
Date

Message

fix: cache repo references

Diff

  1diff --git a/internal/git/git.go b/internal/git/git.go
  2index fbfa102503fbe978c07756f0bb9d7dd35d22ab1b..02b9ac6ef6146a09669aced9885c213f87eb09db 100644
  3--- a/internal/git/git.go
  4+++ b/internal/git/git.go
  5@@ -29,6 +29,7 @@ type Repo struct {
  6 	LastUpdated *time.Time
  7 	refCommits  map[plumbing.Hash]gitypes.Commits
  8 	ref         *plumbing.Reference
  9+	refs        []*plumbing.Reference
 10 }
 11 
 12 // GetName returns the name of the repository.
 13@@ -36,17 +37,21 @@ func (r *Repo) GetName() string {
 14 	return r.Name
 15 }
 16 
 17-// GetReference returns the reference for a repository.
 18-func (r *Repo) GetReference() *plumbing.Reference {
 19+// GetHEAD returns the reference for a repository.
 20+func (r *Repo) GetHEAD() *plumbing.Reference {
 21 	return r.ref
 22 }
 23 
 24-// SetReference sets the repository head reference.
 25-func (r *Repo) SetReference(ref *plumbing.Reference) error {
 26+// SetHEAD sets the repository head reference.
 27+func (r *Repo) SetHEAD(ref *plumbing.Reference) error {
 28 	r.ref = ref
 29 	return nil
 30 }
 31 
 32+func (r *Repo) GetReferences() []*plumbing.Reference {
 33+	return r.refs
 34+}
 35+
 36 // GetRepository returns the underlying go-git repository object.
 37 func (r *Repo) GetRepository() *git.Repository {
 38 	return r.Repository
 39@@ -273,13 +278,23 @@ func (rs *RepoSource) loadRepo(name string, rg *git.Repository) (*Repo, error) {
 40 		return nil, err
 41 	}
 42 	r.Readme = rm
 43+	refs := make([]*plumbing.Reference, 0)
 44+	ri, err := rg.References()
 45+	if err != nil {
 46+		return nil, err
 47+	}
 48+	ri.ForEach(func(r *plumbing.Reference) error {
 49+		refs = append(refs, r)
 50+		return nil
 51+	})
 52+	r.refs = refs
 53 	return r, nil
 54 }
 55 
 56 // LatestFile returns the latest file at the specified path in the repository.
 57 func (r *Repo) LatestFile(path string) (string, error) {
 58 	lg, err := r.Repository.Log(&git.LogOptions{
 59-		From: r.GetReference().Hash(),
 60+		From: r.GetHEAD().Hash(),
 61 	})
 62 	if err != nil {
 63 		return "", err
 64diff --git a/internal/tui/bubbles/git/about/bubble.go b/internal/tui/bubbles/git/about/bubble.go
 65index 17ad0dbdc1903786faad95e8e6761b6d0d9a8798..1bb36e9e071348ef856bb7879811f31f2e25ea0c 100644
 66--- a/internal/tui/bubbles/git/about/bubble.go
 67+++ b/internal/tui/bubbles/git/about/bubble.go
 68@@ -30,7 +30,7 @@ func NewBubble(repo types.Repo, styles *style.Styles, width, wm, height, hm int)
 69 		styles:       styles,
 70 		widthMargin:  wm,
 71 		heightMargin: hm,
 72-		ref:          repo.GetReference(),
 73+		ref:          repo.GetHEAD(),
 74 	}
 75 	b.SetSize(width, height)
 76 	return b
 77diff --git a/internal/tui/bubbles/git/bubble.go b/internal/tui/bubbles/git/bubble.go
 78index 8bec715b8feb344db683de02d1c75494d8088cd0..d9258b70196158d6c1a83d1bb12acb3befbadf7c 100644
 79--- a/internal/tui/bubbles/git/bubble.go
 80+++ b/internal/tui/bubbles/git/bubble.go
 81@@ -47,7 +47,7 @@ func NewBubble(repo types.Repo, styles *style.Styles, width, wm, height, hm int)
 82 		heightMargin: hm,
 83 		style:        styles,
 84 		boxes:        make([]tea.Model, 4),
 85-		ref:          repo.GetReference(),
 86+		ref:          repo.GetHEAD(),
 87 	}
 88 	heightMargin := hm + lipgloss.Height(b.headerView())
 89 	b.boxes[aboutPage] = about.NewBubble(repo, b.style, b.width, wm, b.height, heightMargin)
 90diff --git a/internal/tui/bubbles/git/log/bubble.go b/internal/tui/bubbles/git/log/bubble.go
 91index d6928889d76fcbeea298f066f285e3b8fcbdda21..46118cff6889080e5d0c1f55989227eab426d222 100644
 92--- a/internal/tui/bubbles/git/log/bubble.go
 93+++ b/internal/tui/bubbles/git/log/bubble.go
 94@@ -125,7 +125,7 @@ func NewBubble(repo types.Repo, styles *style.Styles, width, widthMargin, height
 95 		height:       height,
 96 		heightMargin: heightMargin,
 97 		list:         l,
 98-		ref:          repo.GetReference(),
 99+		ref:          repo.GetHEAD(),
100 	}
101 	b.SetSize(width, height)
102 	return b
103diff --git a/internal/tui/bubbles/git/refs/bubble.go b/internal/tui/bubbles/git/refs/bubble.go
104index ac9cf285ec351b2952809dd930c0af00351c446d..c180d128771d1cab56ecc810f102dd3ba09953f5 100644
105--- a/internal/tui/bubbles/git/refs/bubble.go
106+++ b/internal/tui/bubbles/git/refs/bubble.go
107@@ -93,7 +93,7 @@ func NewBubble(repo types.Repo, styles *style.Styles, width, widthMargin, height
108 		widthMargin:  widthMargin,
109 		heightMargin: heightMargin,
110 		list:         l,
111-		ref:          repo.GetReference(),
112+		ref:          repo.GetHEAD(),
113 	}
114 	b.SetSize(width, height)
115 	return b
116@@ -123,21 +123,15 @@ func (b *Bubble) Help() []types.HelpEntry {
117 func (b *Bubble) updateItems() tea.Cmd {
118 	its := make(items, 0)
119 	tags := make(items, 0)
120-	ri, err := b.repo.GetRepository().References()
121-	if err != nil {
122-		return nil
123-	}
124-	if err = ri.ForEach(func(r *plumbing.Reference) error {
125-		if r.Type() == plumbing.HashReference {
126-			if r.Name().IsTag() {
127-				tags = append(tags, item{r})
128-			} else {
129-				its = append(its, item{r})
130-			}
131+	for _, r := range b.repo.GetReferences() {
132+		if r.Type() != plumbing.HashReference {
133+			continue
134+		}
135+		if r.Name().IsTag() {
136+			tags = append(tags, item{r})
137+		} else {
138+			its = append(its, item{r})
139 		}
140-		return nil
141-	}); err != nil {
142-		return nil
143 	}
144 	sort.Sort(its)
145 	sort.Sort(tags)
146diff --git a/internal/tui/bubbles/git/tree/bubble.go b/internal/tui/bubbles/git/tree/bubble.go
147index e8d4e0924eb8276c4046fb5ba3fa0c2a233fcef0..3bbefef5d983db3ff63debe6e7397209c73db321 100644
148--- a/internal/tui/bubbles/git/tree/bubble.go
149+++ b/internal/tui/bubbles/git/tree/bubble.go
150@@ -154,7 +154,7 @@ func NewBubble(repo types.Repo, styles *style.Styles, width, widthMargin, height
151 		heightMargin: heightMargin,
152 		list:         l,
153 		state:        treeState,
154-		ref:          repo.GetReference(),
155+		ref:          repo.GetHEAD(),
156 	}
157 	b.SetSize(width, height)
158 	return b
159@@ -222,7 +222,7 @@ func (b *Bubble) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
160 
161 	case tea.KeyMsg:
162 		if b.state == errorState {
163-			ref := b.repo.GetReference()
164+			ref := b.repo.GetHEAD()
165 			b.ref = ref
166 			return b, tea.Batch(b.reset(), func() tea.Msg {
167 				return ref
168diff --git a/internal/tui/bubbles/git/types/git.go b/internal/tui/bubbles/git/types/git.go
169index 9cff00a5b2ef8a00891b2cdd9126b53dd2f439f1..a4b78b91fe3d7f2467b8e50553d5f771b27ae16f 100644
170--- a/internal/tui/bubbles/git/types/git.go
171+++ b/internal/tui/bubbles/git/types/git.go
172@@ -8,8 +8,9 @@ import (
173 
174 type Repo interface {
175 	GetName() string
176-	GetReference() *plumbing.Reference
177-	SetReference(*plumbing.Reference) error
178+	GetHEAD() *plumbing.Reference
179+	SetHEAD(*plumbing.Reference) error
180+	GetReferences() []*plumbing.Reference
181 	GetReadme() string
182 	GetCommits(*plumbing.Reference) (Commits, error)
183 	GetRepository() *git.Repository