mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
WTF-400 Git extracted to new config format
This commit is contained in:
parent
0790493c53
commit
095041be61
3
main.go
3
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":
|
||||
|
@ -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}
|
||||
|
30
modules/git/settings.go
Normal file
30
modules/git/settings.go
Normal file
@ -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
|
||||
}
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user