Skip to content

Commit

Permalink
Load repo by name
Browse files Browse the repository at this point in the history
  • Loading branch information
Toby Padilla committed Aug 7, 2021
1 parent e69529f commit e228d7d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
14 changes: 14 additions & 0 deletions git/git.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package git

import (
"errors"
"log"
"os"
"sort"
Expand All @@ -11,6 +12,8 @@ import (
"github.com/go-git/go-git/v5/plumbing/object"
)

var ErrMissingRepo = errors.New("missing repo")

type Repo struct {
Name string
Repository *git.Repository
Expand Down Expand Up @@ -58,6 +61,17 @@ func (rs *RepoSource) AllRepos() []*Repo {
return rs.repos
}

func (rs *RepoSource) GetRepo(name string) (*Repo, error) {
rs.mtx.Lock()
defer rs.mtx.Unlock()
for _, r := range rs.repos {
if r.Name == name {
return r, nil
}
}
return nil, ErrMissingRepo
}

func (rs *RepoSource) GetCommits(limit int) []RepoCommit {
rs.mtx.Lock()
defer rs.mtx.Unlock()
Expand Down
7 changes: 1 addition & 6 deletions tui/bubble.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,7 @@ func (b *Bubble) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
b.width = msg.Width
b.height = msg.Height
case selection.SelectedMsg:
rmd := b.repos[msg.Index].Readme
b.readmeViewport.Viewport.GotoTop()
b.readmeViewport.Viewport.Height = b.height - verticalPadding - viewportHeightConstant
b.readmeViewport.Viewport.Width = boxLeftWidth - 2
b.readmeViewport.Viewport.SetContent(rmd)
b.boxes[1] = b.readmeViewport
cmds = append(cmds, b.getRepoCmd(b.repos[msg.Index].Name))
}
if b.state == loadedState {
ab, cmd := b.boxes[b.activeBox].Update(msg)
Expand Down
16 changes: 16 additions & 0 deletions tui/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ func (b *Bubble) windowChangesCmd() tea.Msg {
return windowMsg{}
}

func (b *Bubble) getRepoCmd(name string) tea.Cmd {
return func() tea.Msg {
r, err := b.repoSource.GetRepo(name)
if err != nil {
return errMsg{err}
}
b.readmeViewport.Viewport.GotoTop()
b.readmeViewport.Viewport.Height = b.height - verticalPadding - viewportHeightConstant
b.readmeViewport.Viewport.Width = boxLeftWidth - 2
b.readmeViewport.Viewport.SetContent(r.Readme)
b.boxes[1] = b.readmeViewport
b.activeBox = 1
return nil
}
}

func (b *Bubble) loadGitCmd() tea.Msg {
b.repos = b.repoSource.AllRepos()
rs := make([]string, 0)
Expand Down

0 comments on commit e228d7d

Please sign in to comment.