From 7d080ff1534aae5ca53622036702113cbdb74852 Mon Sep 17 00:00:00 2001 From: Tai Groot Date: Tue, 7 Feb 2023 15:16:42 -0800 Subject: [PATCH] update gitfetch to use builtins, add config option for author --- cmd/gitfetch/gitfetch.go | 79 ++++++---------------------------------- commits/common.go | 20 +++++++++- graph/common/common.go | 2 - 3 files changed, 30 insertions(+), 71 deletions(-) diff --git a/cmd/gitfetch/gitfetch.go b/cmd/gitfetch/gitfetch.go index 67d6389..aa6a734 100644 --- a/cmd/gitfetch/gitfetch.go +++ b/cmd/gitfetch/gitfetch.go @@ -1,87 +1,30 @@ package main import ( - "errors" "fmt" "os" "time" - git "github.com/go-git/go-git/v5" - "github.com/go-git/go-git/v5/plumbing/object" + "github.com/taigrr/gico/commits" gterm "github.com/taigrr/gico/graph/term" - - "github.com/taigrr/gico/types" ) func main() { year := time.Now().Year() - repo, err := OpenRepo(".") + dir, err := os.Getwd() if err != nil { panic(err) } - str, err := repo.GetYear(year) + r, err := commits.OpenRepo(dir) + if err != nil { + fmt.Println("Error opening current directory as a git repo") + os.Exit(1) + } + cs, err := r.GetCommitSet() if err != nil { panic(err) } - fmt.Print(str) -} - -type Repo git.Repository - -func OpenRepo(directory string) (Repo, error) { - if s, err := os.Stat(directory); err != nil { - return Repo{}, err - } else { - if !s.IsDir() { - return Repo{}, errors.New("received path to non-directory for git repo") - } - } - - r, err := git.PlainOpenWithOptions(directory, &(git.PlainOpenOptions{DetectDotGit: true})) - return Repo(*r), err -} - -func (repo Repo) GetYear(year int) (string, error) { - yearLength := 365 - if year%4 == 0 { - yearLength++ - } - data := types.NewDataSet() - r := git.Repository(repo) - ref, err := r.Head() - if err != nil { - return "", err - } - cIter, err := r.Log(&git.LogOptions{From: ref.Hash()}) - if err != nil { - return "", err - } - err = cIter.ForEach(func(c *object.Commit) error { - ts := c.Author.When - commit := types.Commit{Author: c.Author.Name, Message: c.Message, TimeStamp: ts} - roundedTS := ts.Round(time.Hour * 24) - wd, ok := data[roundedTS] - if !ok { - wd = types.WorkDay{} - wd.Commits = []types.Commit{} - } - wd.Commits = append(wd.Commits, commit) - wd.Count++ - wd.Day = roundedTS - data[roundedTS] = wd - return nil - }) - - freq := make([]int, yearLength) - for k, v := range data { - if k.Year() != year { - continue - } - // this is equivalent to adding len(commits) to the freq total, but - // it's a stub for later when we do more here - for range v.Commits { - freq[k.YearDay()-1]++ - } - } - return gterm.GetYearUnicode(freq), nil + cs = cs.FilterByYear(year) + freq := cs.ToYearFreq() + fmt.Print(gterm.GetYearUnicode(freq)) } diff --git a/commits/common.go b/commits/common.go index 415d8d9..374df5b 100644 --- a/commits/common.go +++ b/commits/common.go @@ -5,9 +5,11 @@ import ( "os" git "github.com/go-git/go-git/v5" + "github.com/go-git/go-git/v5/config" + + "github.com/taigrr/mg/parse" "github.com/taigrr/gico/types" - "github.com/taigrr/mg/parse" ) type ( @@ -42,3 +44,19 @@ func GetMRRepos() (RepoSet, error) { paths := mrconf.GetRepoPaths() return RepoSet(paths), nil } + +func GetAuthorName() (string, error) { + conf, err := config.LoadConfig(config.GlobalScope) + if err != nil { + return "", err + } + return conf.Author.Name, nil +} + +func GetAuthorEmail() (string, error) { + conf, err := config.LoadConfig(config.GlobalScope) + if err != nil { + return "", err + } + return conf.Author.Email, nil +} diff --git a/graph/common/common.go b/graph/common/common.go index 0129126..13a3d4f 100644 --- a/graph/common/common.go +++ b/graph/common/common.go @@ -2,7 +2,6 @@ package common import ( "bytes" - "fmt" "image/color" "sync" @@ -35,7 +34,6 @@ func SetColorScheme(c []color.Color) { } func ColorForFrequency(freq, min, max int) sc.SimpleColor { - fmt.Printf("f: %d, min: %d, max: %d\n", freq, min, max) if freq == 0 { return sc.SimpleColor(0) }