mirror of
https://github.com/taigrr/gico.git
synced 2026-04-01 18:58:59 -07:00
update gitfetch to use builtins, add config option for author
This commit is contained in:
@@ -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))
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user