From 095041be61c50477befa108759465fdd06cdd088 Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Sun, 14 Apr 2019 16:15:10 -0700 Subject: [PATCH] WTF-400 Git extracted to new config format --- main.go | 3 ++- modules/git/git_repo.go | 12 ++++-------- modules/git/settings.go | 30 ++++++++++++++++++++++++++++++ modules/git/widget.go | 37 +++++++++++++++++++++++++++---------- 4 files changed, 63 insertions(+), 19 deletions(-) create mode 100644 modules/git/settings.go diff --git a/main.go b/main.go index 14f73445..7433cd6c 100644 --- a/main.go +++ b/main.go @@ -214,7 +214,8 @@ func makeWidget(app *tview.Application, pages *tview.Pages, widgetName string) w cfg := gerrit.NewSettingsFromYAML(wtf.Config) widget = gerrit.NewWidget(app, pages, cfg) case "git": - widget = git.NewWidget(app, pages) + cfg := git.NewSettingsFromYAML(wtf.Config) + widget = git.NewWidget(app, pages, cfg) case "github": widget = github.NewWidget(app, pages) case "gitlab": diff --git a/modules/git/git_repo.go b/modules/git/git_repo.go index a402d988..b53e6ed4 100644 --- a/modules/git/git_repo.go +++ b/modules/git/git_repo.go @@ -16,12 +16,12 @@ type GitRepo struct { Path string } -func NewGitRepo(repoPath string) *GitRepo { +func NewGitRepo(repoPath string, commitCount int, commitFormat, dateFormat string) *GitRepo { repo := GitRepo{Path: repoPath} repo.Branch = repo.branch() repo.ChangedFiles = repo.changedFiles() - repo.Commits = repo.commits() + repo.Commits = repo.commits(commitCount, commitFormat, dateFormat) repo.Repository = strings.TrimSpace(repo.repository()) return &repo @@ -49,13 +49,9 @@ func (repo *GitRepo) changedFiles() []string { return data } -func (repo *GitRepo) commits() []string { - numStr := fmt.Sprintf("-n %d", wtf.Config.UInt("wtf.mods.git.commitCount", 10)) - - dateFormat := wtf.Config.UString("wtf.mods.git.dateFormat", "%b %d, %Y") +func (repo *GitRepo) commits(commitCount int, commitFormat, dateFormat string) []string { dateStr := fmt.Sprintf("--date=format:\"%s\"", dateFormat) - - commitFormat := wtf.Config.UString("wtf.mods.git.commitFormat", "[forestgreen]%h [white]%s [grey]%an on %cd[white]") + numStr := fmt.Sprintf("-n %d", commitCount) commitStr := fmt.Sprintf("--pretty=format:\"%s\"", commitFormat) arg := []string{repo.gitDir(), repo.workTree(), "log", dateStr, numStr, commitStr} diff --git a/modules/git/settings.go b/modules/git/settings.go new file mode 100644 index 00000000..d54a69a9 --- /dev/null +++ b/modules/git/settings.go @@ -0,0 +1,30 @@ +package git + +import ( + "github.com/olebedev/config" + "github.com/wtfutil/wtf/cfg" +) + +type Settings struct { + common *cfg.Common + + commitCount int + commitFormat string + dateFormat string + repositories []interface{} +} + +func NewSettingsFromYAML(ymlConfig *config.Config) *Settings { + localConfig, _ := ymlConfig.Get("wtf.mods.git") + + settings := Settings{ + common: cfg.NewCommonSettingsFromYAML(ymlConfig), + + commitCount: localConfig.UInt("commitCount", 10), + commitFormat: localConfig.UString("commitFormat", "[forestgreen]%h [white]%s [grey]%an on %cd[white]"), + dateFormat: localConfig.UString("dateFormat", "%b %d, %Y"), + repositories: localConfig.UList("repositories"), + } + + return &settings +} diff --git a/modules/git/widget.go b/modules/git/widget.go index b94550e1..9231ff55 100644 --- a/modules/git/widget.go +++ b/modules/git/widget.go @@ -32,19 +32,22 @@ type Widget struct { wtf.MultiSourceWidget wtf.TextWidget - app *tview.Application GitRepos []*GitRepo + + app *tview.Application pages *tview.Pages + settings *Settings } -func NewWidget(app *tview.Application, pages *tview.Pages) *Widget { +func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget { widget := Widget{ HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText), MultiSourceWidget: wtf.NewMultiSourceWidget("git", "repository", "repositories"), TextWidget: wtf.NewTextWidget(app, "Git", "git", true), - app: app, - pages: pages, + app: app, + pages: pages, + settings: settings, } widget.LoadSources() @@ -83,7 +86,7 @@ func (widget *Widget) Pull() { } func (widget *Widget) Refresh() { - repoPaths := wtf.ToStrs(wtf.Config.UList("wtf.mods.git.repositories")) + repoPaths := wtf.ToStrs(widget.settings.repositories) widget.GitRepos = widget.gitRepos(repoPaths) sort.Slice(widget.GitRepos, func(i, j int) bool { @@ -164,10 +167,16 @@ func (widget *Widget) gitRepos(repoPaths []string) []*GitRepo { for _, repoPath := range repoPaths { if strings.HasSuffix(repoPath, "/") { - repos = append(repos, findGitRepositories(make([]*GitRepo, 0), repoPath)...) + repos = append(repos, widget.findGitRepositories(make([]*GitRepo, 0), repoPath)...) } else { - repo := NewGitRepo(repoPath) + repo := NewGitRepo( + repoPath, + widget.settings.commitCount, + widget.settings.commitFormat, + widget.settings.dateFormat, + ) + repos = append(repos, repo) } } @@ -175,7 +184,7 @@ func (widget *Widget) gitRepos(repoPaths []string) []*GitRepo { return repos } -func findGitRepositories(repositories []*GitRepo, directory string) []*GitRepo { +func (widget *Widget) findGitRepositories(repositories []*GitRepo, directory string) []*GitRepo { directory = strings.TrimSuffix(directory, "/") files, err := ioutil.ReadDir(directory) @@ -188,16 +197,24 @@ func findGitRepositories(repositories []*GitRepo, directory string) []*GitRepo { for _, file := range files { if file.IsDir() { path = directory + "/" + file.Name() + if file.Name() == ".git" { path = strings.TrimSuffix(path, "/.git") - repo := NewGitRepo(path) + + repo := NewGitRepo( + path, + widget.settings.commitCount, + widget.settings.commitFormat, + widget.settings.dateFormat, + ) + repositories = append(repositories, repo) continue } if file.Name() == "vendor" || file.Name() == "node_modules" { continue } - repositories = findGitRepositories(repositories, path) + repositories = widget.findGitRepositories(repositories, path) } }