From ecc3163d282193a34569f3c1151c4db304a62476 Mon Sep 17 00:00:00 2001 From: Tai Groot Date: Sat, 18 Feb 2023 17:56:49 -0800 Subject: [PATCH] refactor Author type --- commits/chancommits.go | 45 ++++++++++++++++++++++++++++++++++++++++-- commits/commits.go | 7 +++++-- types/helpers.go | 4 ++-- types/types.go | 6 +++++- 4 files changed, 55 insertions(+), 7 deletions(-) diff --git a/commits/chancommits.go b/commits/chancommits.go index 2311b71..abddea1 100644 --- a/commits/chancommits.go +++ b/commits/chancommits.go @@ -2,6 +2,7 @@ package commits import ( "regexp" + "sort" "sync" git "github.com/go-git/go-git/v5" @@ -10,6 +11,43 @@ import ( "github.com/taigrr/gico/types" ) +func (paths RepoSet) GetRepoAuthors() ([]string, error) { + outChan := make(chan types.Commit, 10) + var wg sync.WaitGroup + for _, p := range paths { + wg.Add(1) + go func(path string) { + defer wg.Done() + repo, err := OpenRepo(path) + if err != nil { + return + } + cc, err := repo.GetCommitChan() + if err != nil { + return + } + for c := range cc { + outChan <- c + } + }(p) + } + go func() { + wg.Wait() + close(outChan) + }() + authors := make(map[string]bool) + for commit := range outChan { + authors[commit.Author.Email] = true + authors[commit.Author.Name] = true + } + a := []string{} + for k := range authors { + a = append(a, k) + } + sort.Strings(a) + return a, nil +} + func (paths RepoSet) GetRepoCommits(year int, authors []string) ([][]types.Commit, error) { yearLength := 365 if year%4 == 0 { @@ -132,7 +170,10 @@ func (repo Repo) GetCommitChan() (chan types.Commit, error) { cIter.ForEach(func(c *object.Commit) error { ts := c.Author.When commit := types.Commit{ - Author: c.Author.Name, + Author: types.Author{ + Name: c.Author.Name, + Email: c.Author.Email, + }, Message: c.Message, TimeStamp: ts, Hash: c.Hash.String(), Repo: repo.Path, FilesChanged: 0, Added: 0, Deleted: 0, @@ -197,7 +238,7 @@ func FilterCChanByAuthor(in chan types.Commit, authors []string) (chan types.Com for commit := range in { regset: for _, r := range regSet { - if r.MatchString(commit.Author) { + if r.MatchString(commit.Author.Email) || r.MatchString(commit.Author.Name) { out <- commit break regset } diff --git a/commits/commits.go b/commits/commits.go index b99327e..81d3c58 100644 --- a/commits/commits.go +++ b/commits/commits.go @@ -89,7 +89,10 @@ func (repo Repo) GetCommitSet() (CommitSet, error) { cIter.ForEach(func(c *object.Commit) error { ts := c.Author.When commit := types.Commit{ - Author: c.Author.Name, + Author: types.Author{ + Name: c.Author.Name, + Email: c.Author.Email, + }, Message: c.Message, TimeStamp: ts, Hash: c.Hash.String(), Repo: repo.Path, FilesChanged: 0, Added: 0, Deleted: 0, @@ -137,7 +140,7 @@ func (cs CommitSet) FilterByAuthorRegex(authors []string) (CommitSet, error) { for _, commit := range cs.Commits { regset: for _, r := range regSet { - if r.MatchString(commit.Author) { + if r.MatchString(commit.Author.Name) || r.MatchString(commit.Author.Email) { newCS.Commits = append(newCS.Commits, commit) break regset } diff --git a/types/helpers.go b/types/helpers.go index ccece49..dc4558a 100644 --- a/types/helpers.go +++ b/types/helpers.go @@ -10,10 +10,10 @@ func NewDataSet() DataSet { return make(DataSet) } -func NewCommit(Author, Message, Repo, Path string, Added, Deleted, FilesChanged int) Commit { +func NewCommit(AuthorName, AuthorEmail, Message, Repo, Path string, Added, Deleted, FilesChanged int) Commit { ci := Commit{ Message: Message, Added: Added, Deleted: Deleted, - Author: Author, FilesChanged: FilesChanged, TimeStamp: time.Now(), + Author: Author{Name: AuthorName, Email: AuthorEmail}, FilesChanged: FilesChanged, TimeStamp: time.Now(), Repo: Repo, Path: Path, } return ci diff --git a/types/types.go b/types/types.go index 88c1298..cc2853d 100644 --- a/types/types.go +++ b/types/types.go @@ -8,6 +8,10 @@ import ( type ( Month string + Author struct { + Name string `json:"name,omitempty"` + Email string `json:"email,omitempty"` + } Commit struct { Deleted int `json:"deleted,omitempty"` Added int `json:"added,omitempty"` @@ -15,7 +19,7 @@ type ( Message string `json:"message,omitempty"` Hash string `json:"hash,omitempty"` TimeStamp time.Time `json:"ts,omitempty"` - Author string `json:"author,omitempty"` + Author Author `json:"author,omitempty"` Repo string `json:"repo,omitempty"` Path string `json:"path,omitempty"` }