refactor to make gitfetch compile

This commit is contained in:
2023-01-26 23:55:26 -08:00
parent 0e40bbab31
commit 0cdc55f430
4 changed files with 22 additions and 37 deletions

View File

@@ -9,8 +9,6 @@ import (
"github.com/taigrr/gico/ui" "github.com/taigrr/gico/ui"
) )
type Month string
var days [366]int var days [366]int
func init() { func init() {
@@ -26,6 +24,7 @@ func main() {
os.Exit(0) os.Exit(0)
} }
switch args[0] { switch args[0] {
// TODO use cobra-cli instead of switch case
case "inc", "increment", "add": case "inc", "increment", "add":
increment() increment()
case "graph": case "graph":
@@ -39,29 +38,12 @@ func main() {
} }
} }
type Commit struct { func NewDataSet() types.DataSet {
LOC int `json:"loc,omitempty"` return make(types.DataSet)
Message string `json:"message,omitempty"`
TimeStamp time.Time `json:"ts,omitempty"`
Author string `json:"author,omitempty"`
Repo string `json:"repo,omitempty"`
Path string `json:"path,omitempty"`
} }
type DataSet map[time.Time]WorkDay func NewCommit(Author, Message, Repo, Path string, LOC int) types.Commit {
ci := types.Commit{
func NewDataSet() DataSet {
return make(DataSet)
}
type WorkDay struct {
Day time.Time `json:"day"`
Count int `json:"count"`
Commits []Commit `json:"commits,omitempty"`
}
func NewCommit(Author, Message, Repo, Path string, LOC int) Commit {
ci := Commit{
Message: Message, Message: Message,
Author: Author, LOC: LOC, TimeStamp: time.Now(), Author: Author, LOC: LOC, TimeStamp: time.Now(),
Repo: Repo, Path: Path, Repo: Repo, Path: Path,
@@ -72,8 +54,8 @@ func NewCommit(Author, Message, Repo, Path string, LOC int) Commit {
func loadRepo() { func loadRepo() {
} }
func readCommitDB() DataSet { func readCommitDB() types.DataSet {
ds := DataSet{} ds := types.DataSet{}
return ds return ds
} }

View File

@@ -9,7 +9,7 @@ import (
"github.com/go-git/go-git/v5/plumbing/object" "github.com/go-git/go-git/v5/plumbing/object"
gterm "github.com/taigrr/gitgraph/term" gterm "github.com/taigrr/gitgraph/term"
"github.com/taigrr/gico" "github.com/taigrr/gico/types"
) )
func main() { func main() {
@@ -17,24 +17,30 @@ func main() {
} }
func GetYear() { func GetYear() {
year := time.Now().Year()
yearLength := 365
if year%4 == 0 {
yearLength++
}
data := types.NewDataSet()
r, err := git.PlainOpenWithOptions(".", &(git.PlainOpenOptions{DetectDotGit: true})) r, err := git.PlainOpenWithOptions(".", &(git.PlainOpenOptions{DetectDotGit: true}))
if err != nil { if err != nil {
fmt.Printf("gitfetch error: Could not find a git repository to open!\n") fmt.Printf("gitfetch error: Could not find a git repository to open!\n")
os.Exit(1) os.Exit(1)
} }
ref, err := r.Head() ref, err := r.Head()
// TODO handle this error
cIter, err := r.Log(&git.LogOptions{From: ref.Hash()}) cIter, err := r.Log(&git.LogOptions{From: ref.Hash()})
year := time.Now().Year()
data := gico.NewDataSet()
err = cIter.ForEach(func(c *object.Commit) error { err = cIter.ForEach(func(c *object.Commit) error {
ts := c.Author.When ts := c.Author.When
commit := gico.Commit{Author: c.Author.Name, Message: c.Message, TimeStamp: ts} commit := types.Commit{Author: c.Author.Name, Message: c.Message, TimeStamp: ts}
roundedTS := ts.Round(time.Hour * 24) roundedTS := ts.Round(time.Hour * 24)
wd, ok := data[roundedTS] wd, ok := data[roundedTS]
if !ok { if !ok {
wd = gico.WorkDay{} wd = types.WorkDay{}
wd.Commits = []gico.Commit{} wd.Commits = []types.Commit{}
} }
wd.Commits = append(wd.Commits, commit) wd.Commits = append(wd.Commits, commit)
wd.Count++ wd.Count++
@@ -42,10 +48,7 @@ func GetYear() {
data[roundedTS] = wd data[roundedTS] = wd
return nil return nil
}) })
yearLength := 365
if year%4 == 0 {
yearLength++
}
freq := make([]int, yearLength) freq := make([]int, yearLength)
for k, v := range data { for k, v := range data {
if k.Year() != year { if k.Year() != year {

View File

@@ -1,4 +1,4 @@
package main package types
import ( import (
"time" "time"

View File

@@ -1,4 +1,4 @@
package main package types
import ( import (
"time" "time"