update gitfetch to use builtins, add config option for author

This commit is contained in:
2023-02-07 15:16:42 -08:00
parent 9271b27511
commit 7d080ff153
3 changed files with 30 additions and 71 deletions

View File

@@ -1,87 +1,30 @@
package main package main
import ( import (
"errors"
"fmt" "fmt"
"os" "os"
"time" "time"
git "github.com/go-git/go-git/v5" "github.com/taigrr/gico/commits"
"github.com/go-git/go-git/v5/plumbing/object"
gterm "github.com/taigrr/gico/graph/term" gterm "github.com/taigrr/gico/graph/term"
"github.com/taigrr/gico/types"
) )
func main() { func main() {
year := time.Now().Year() year := time.Now().Year()
repo, err := OpenRepo(".") dir, err := os.Getwd()
if err != nil { if err != nil {
panic(err) 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 { if err != nil {
panic(err) panic(err)
} }
fmt.Print(str) cs = cs.FilterByYear(year)
} freq := cs.ToYearFreq()
fmt.Print(gterm.GetYearUnicode(freq))
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
} }

View File

@@ -5,9 +5,11 @@ import (
"os" "os"
git "github.com/go-git/go-git/v5" 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/gico/types"
"github.com/taigrr/mg/parse"
) )
type ( type (
@@ -42,3 +44,19 @@ func GetMRRepos() (RepoSet, error) {
paths := mrconf.GetRepoPaths() paths := mrconf.GetRepoPaths()
return RepoSet(paths), nil 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
}

View File

@@ -2,7 +2,6 @@ package common
import ( import (
"bytes" "bytes"
"fmt"
"image/color" "image/color"
"sync" "sync"
@@ -35,7 +34,6 @@ func SetColorScheme(c []color.Color) {
} }
func ColorForFrequency(freq, min, max int) sc.SimpleColor { func ColorForFrequency(freq, min, max int) sc.SimpleColor {
fmt.Printf("f: %d, min: %d, max: %d\n", freq, min, max)
if freq == 0 { if freq == 0 {
return sc.SimpleColor(0) return sc.SimpleColor(0)
} }