diff --git a/search.go b/search.go index 936cd66..10ecd3b 100644 --- a/search.go +++ b/search.go @@ -6,8 +6,6 @@ import ( "os/exec" "path/filepath" "regexp" - - tea "github.com/charmbracelet/bubbletea" ) type repo struct { @@ -17,44 +15,43 @@ type repo struct { var excludeDirs = regexp.MustCompile(`.+/(\..+|node_modules)`) // Skip hidden directories (incl. .git) and node_modules -func getRepos(path string, sub chan repo) tea.Cmd { - return func() tea.Msg { - path, err := filepath.Abs(path) +func collectDirtyRepos(path string, sub chan repo) error { + path, err := filepath.Abs(path) + if err != nil { + return err + } + + err = filepath.WalkDir(path, func(path string, d fs.DirEntry, err error) error { if err != nil { return err } + if d.IsDir() && excludeDirs.MatchString(path) { + if filepath.Base(path) == ".git" { + repopath := filepath.Dir(path) + + var buf = new(bytes.Buffer) + cmd := exec.Command("git", "status", "--porcelain=v2", "--show-stash", "--branch") + cmd.Stdout = buf + cmd.Dir = repopath + err = cmd.Run() + if err != nil { + return err + } - err = filepath.WalkDir(path, func(path string, d fs.DirEntry, err error) error { - if err != nil { - return err - } - if d.IsDir() && excludeDirs.MatchString(path) { - if filepath.Base(path) == ".git" { - repopath := filepath.Dir(path) - - var buf = new(bytes.Buffer) - cmd := exec.Command("git", "status", "--porcelain=v2", "--show-stash", "--branch") - cmd.Stdout = buf - cmd.Dir = repopath - err = cmd.Run() - if err != nil { - return err - } - - gi := GitInfo{} - gi.Parse(buf) - - if !gi.IsClean() { - sub <- repo{path: repopath, status: gi.Summary()} - } + gi := GitInfo{} + gi.Parse(buf) + + if !gi.IsClean() { + sub <- repo{path: repopath, status: gi.Summary()} } - return fs.SkipDir } - return nil - }) - if err != nil { - return err + return fs.SkipDir } - return doneMsg{} + return nil + }) + if err != nil { + return err } + + return nil } diff --git a/search_test.go b/search_test.go index eef1a04..ebaf8b1 100644 --- a/search_test.go +++ b/search_test.go @@ -28,7 +28,7 @@ func TestDetectGitRepo(t *testing.T) { output := make(chan repo) go func() { - getRepos(d, output)() + _ = collectDirtyRepos(d, output) defer close(output) }() diff --git a/tui.go b/tui.go index fa44fa2..9a02dd3 100644 --- a/tui.go +++ b/tui.go @@ -43,7 +43,7 @@ type model struct { func (m model) Init() tea.Cmd { return tea.Batch( m.list.StartSpinner(), - getRepos(m.path, m.sub), + startRepoSearch(m.path, m.sub), waitForRepoStatus(m.sub), ) } @@ -110,6 +110,16 @@ func additionalShortHelpKeys() []key.Binding { } } +func startRepoSearch(path string, sub chan repo) tea.Cmd { + return func() tea.Msg { + err := collectDirtyRepos(path, sub) + if err != nil { + return err + } + return doneMsg{} + } +} + func waitForRepoStatus(sub chan repo) tea.Cmd { return func() tea.Msg { repo := <-sub