From 4a02d5848c40469e4cb04cd10e4182f9a50f61c3 Mon Sep 17 00:00:00 2001 From: Anand Sudhir Prayaga Date: Thu, 15 Nov 2018 15:18:15 +0100 Subject: [PATCH] Add support for recursive search for git repositories in git module Fix #126 --- git/display.go | 2 +- git/widget.go | 85 +++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 75 insertions(+), 12 deletions(-) diff --git a/git/display.go b/git/display.go index aedbf36c..34b143c2 100644 --- a/git/display.go +++ b/git/display.go @@ -18,7 +18,7 @@ func (widget *Widget) display() { title := fmt.Sprintf("%s - [green]%s[white]", widget.Name, repoData.Repository) widget.View.SetTitle(widget.ContextualTitle(title)) - str := wtf.SigilStr(len(widget.Data), widget.Idx, widget.View) + "\n" + str := wtf.SigilStr(len(widget.GitRepos), widget.Idx, widget.View) + "\n" str = str + " [red]Branch[white]\n" str = str + fmt.Sprintf(" %s", repoData.Branch) str = str + "\n" diff --git a/git/widget.go b/git/widget.go index 672c9754..934f2f01 100644 --- a/git/widget.go +++ b/git/widget.go @@ -4,6 +4,10 @@ import ( "github.com/gdamore/tcell" "github.com/rivo/tview" "github.com/senorprogrammer/wtf/wtf" + "io/ioutil" + "log" + "sort" + "strings" ) const HelpText = ` @@ -28,9 +32,9 @@ type Widget struct { wtf.MultiSourceWidget wtf.TextWidget - app *tview.Application - Data []*GitRepo - pages *tview.Pages + app *tview.Application + GitRepos []*GitRepo + pages *tview.Pages } func NewWidget(app *tview.Application, pages *tview.Pages) *Widget { @@ -59,7 +63,7 @@ func (widget *Widget) Checkout() { checkoutFctn := func() { text := form.GetFormItem(0).(*tview.InputField).GetText() - repoToCheckout := widget.Data[widget.Idx] + repoToCheckout := widget.GitRepos[widget.Idx] repoToCheckout.checkout(text) widget.pages.RemovePage("modal") widget.app.SetFocus(widget.View) @@ -72,7 +76,7 @@ func (widget *Widget) Checkout() { } func (widget *Widget) Pull() { - repoToPull := widget.Data[widget.Idx] + repoToPull := widget.GitRepos[widget.Idx] repoToPull.pull() widget.Refresh() @@ -81,7 +85,10 @@ func (widget *Widget) Pull() { func (widget *Widget) Refresh() { repoPaths := wtf.ToStrs(wtf.Config.UList("wtf.mods.git.repositories")) - widget.Data = widget.gitRepos(repoPaths) + widget.GitRepos = widget.gitRepos(repoPaths) + sort.Slice(widget.GitRepos, func(i, j int) bool { + return widget.GitRepos[i].Path < widget.GitRepos[j].Path + }) widget.display() } @@ -141,28 +148,84 @@ func (widget *Widget) modalFrame(form *tview.Form) *tview.Frame { } func (widget *Widget) currentData() *GitRepo { - if len(widget.Data) == 0 { + if len(widget.GitRepos) == 0 { return nil } - if widget.Idx < 0 || widget.Idx >= len(widget.Data) { + if widget.Idx < 0 || widget.Idx >= len(widget.GitRepos) { return nil } - return widget.Data[widget.Idx] + return widget.GitRepos[widget.Idx] } func (widget *Widget) gitRepos(repoPaths []string) []*GitRepo { repos := []*GitRepo{} for _, repoPath := range repoPaths { - repo := NewGitRepo(repoPath) - repos = append(repos, repo) + if strings.HasSuffix(repoPath, "/") { + repos = append(repos, findGitRepositories(make([]*GitRepo, 0), repoPath)...) + + } else { + repo := NewGitRepo(repoPath) + repos = append(repos, repo) + } } return repos } +func findGitRepositories(repositories []*GitRepo, directory string) []*GitRepo { + directory = strings.TrimSuffix(directory, "/") + + files, err := ioutil.ReadDir(directory) + if err != nil { + log.Fatal(err) + } + + var path string + + for _, file := range files { + if file.IsDir() { + path = directory + "/" + file.Name() + if file.Name() == ".git" { + path = strings.TrimSuffix(path, "/.git") + repo := NewGitRepo(path) + repositories = append(repositories, repo) + continue + } + if file.Name() == "vendor" || file.Name() == "node_modules" { + continue + } + repositories = findGitRepositories(repositories, path) + } + } + + return repositories +} + +func (widget *Widget) Next() { + widget.Idx = widget.Idx + 1 + if widget.Idx == len(widget.GitRepos) { + widget.Idx = 0 + } + + if widget.DisplayFunction != nil { + widget.DisplayFunction() + } +} + +func (widget *Widget) Prev() { + widget.Idx = widget.Idx - 1 + if widget.Idx < 0 { + widget.Idx = len(widget.GitRepos) - 1 + } + + if widget.DisplayFunction != nil { + widget.DisplayFunction() + } +} + func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey { switch string(event.Rune()) { case "/":