mirror of
https://github.com/taigrr/gico.git
synced 2026-04-02 03:09:07 -07:00
add repo caching for non-chan
This commit is contained in:
@@ -4,7 +4,6 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/taigrr/gico/types"
|
"github.com/taigrr/gico/types"
|
||||||
"github.com/taigrr/gico/ui"
|
"github.com/taigrr/gico/ui"
|
||||||
@@ -43,15 +42,6 @@ func NewDataSet() types.DataSet {
|
|||||||
return make(types.DataSet)
|
return make(types.DataSet)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCommit(Author, Message, Repo, Path string, LOC int) types.Commit {
|
|
||||||
ci := types.Commit{
|
|
||||||
Message: Message,
|
|
||||||
Author: Author, LOC: LOC, TimeStamp: time.Now(),
|
|
||||||
Repo: Repo, Path: Path,
|
|
||||||
}
|
|
||||||
return ci
|
|
||||||
}
|
|
||||||
|
|
||||||
func loadRepo() {
|
func loadRepo() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -26,6 +26,23 @@ func init() {
|
|||||||
repoCache = make(map[string][]types.Commit)
|
repoCache = make(map[string][]types.Commit)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CacheRepo(path string, commits []types.Commit) {
|
||||||
|
mapTex.Lock()
|
||||||
|
defer mapTex.Unlock()
|
||||||
|
repoCache[path] = commits
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetCachedRepo(path string, head string) ([]types.Commit, bool) {
|
||||||
|
mapTex.RLock()
|
||||||
|
defer mapTex.RUnlock()
|
||||||
|
if commits, ok := repoCache[path]; !ok {
|
||||||
|
return []types.Commit{}, false
|
||||||
|
} else if len(commits) > 0 && commits[0].Hash == head {
|
||||||
|
return commits, true
|
||||||
|
}
|
||||||
|
return []types.Commit{}, false
|
||||||
|
}
|
||||||
|
|
||||||
func IsRepoCached(path string, head string) bool {
|
func IsRepoCached(path string, head string) bool {
|
||||||
mapTex.RLock()
|
mapTex.RLock()
|
||||||
defer mapTex.RUnlock()
|
defer mapTex.RUnlock()
|
||||||
@@ -36,12 +53,6 @@ func IsRepoCached(path string, head string) bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func CacheRepo(path string, commits []types.Commit) {
|
|
||||||
mapTex.Lock()
|
|
||||||
defer mapTex.Unlock()
|
|
||||||
repoCache[path] = commits
|
|
||||||
}
|
|
||||||
|
|
||||||
func hashSlice(in []string) string {
|
func hashSlice(in []string) string {
|
||||||
sort.Strings(in)
|
sort.Strings(in)
|
||||||
sb := strings.Builder{}
|
sb := strings.Builder{}
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ func YearFreqFromChan(cc chan types.Commit, year int) types.Freq {
|
|||||||
|
|
||||||
func (repo Repo) GetCommitChan() (chan types.Commit, error) {
|
func (repo Repo) GetCommitChan() (chan types.Commit, error) {
|
||||||
cc := make(chan types.Commit, 30)
|
cc := make(chan types.Commit, 30)
|
||||||
r := git.Repository(repo)
|
r := git.Repository(repo.Repo)
|
||||||
ref, err := r.Head()
|
ref, err := r.Head()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cc, err
|
return cc, err
|
||||||
|
|||||||
@@ -61,14 +61,26 @@ func (paths RepoSet) Frequency(year int, authors []string) (types.Freq, error) {
|
|||||||
return gfreq, nil
|
return gfreq, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (repo Repo) GetHead() (string, error) {
|
||||||
|
r := git.Repository(repo.Repo)
|
||||||
|
ref, err := r.Head()
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return ref.String(), nil
|
||||||
|
}
|
||||||
|
|
||||||
func (repo Repo) GetCommitSet() (CommitSet, error) {
|
func (repo Repo) GetCommitSet() (CommitSet, error) {
|
||||||
cs := CommitSet{}
|
cs := CommitSet{}
|
||||||
commits := []types.Commit{}
|
commits := []types.Commit{}
|
||||||
r := git.Repository(repo)
|
r := git.Repository(repo.Repo)
|
||||||
ref, err := r.Head()
|
ref, err := r.Head()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cs, err
|
return cs, err
|
||||||
}
|
}
|
||||||
|
if cachedRepo, ok := GetCachedRepo(repo.Path, ref.String()); ok {
|
||||||
|
return CommitSet{Commits: cachedRepo}, nil
|
||||||
|
}
|
||||||
cIter, err := r.Log(&git.LogOptions{From: ref.Hash()})
|
cIter, err := r.Log(&git.LogOptions{From: ref.Hash()})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return cs, err
|
return cs, err
|
||||||
@@ -80,6 +92,7 @@ func (repo Repo) GetCommitSet() (CommitSet, error) {
|
|||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
cs.Commits = commits
|
cs.Commits = commits
|
||||||
|
CacheRepo(repo.Path, cs.Commits)
|
||||||
return cs, nil
|
return cs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
Repo git.Repository
|
Repo struct {
|
||||||
|
Repo git.Repository
|
||||||
|
Path string
|
||||||
|
}
|
||||||
CommitSet struct {
|
CommitSet struct {
|
||||||
Commits []types.Commit
|
Commits []types.Commit
|
||||||
Year int
|
Year int
|
||||||
@@ -28,7 +31,7 @@ func OpenRepo(directory string) (Repo, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
r, err := git.PlainOpenWithOptions(directory, &(git.PlainOpenOptions{DetectDotGit: true}))
|
r, err := git.PlainOpenWithOptions(directory, &(git.PlainOpenOptions{DetectDotGit: true}))
|
||||||
return Repo(*r), err
|
return Repo{Repo: *r, Path: directory}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetMRRepos() (RepoSet, error) {
|
func GetMRRepos() (RepoSet, error) {
|
||||||
|
|||||||
@@ -10,12 +10,13 @@ func NewDataSet() DataSet {
|
|||||||
return make(DataSet)
|
return make(DataSet)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCommit(Author, Message, Repo, Path string, LOC int) Commit {
|
func NewCommit(Author, Message, Repo, Path string, Added, Deleted, FilesChanged int) Commit {
|
||||||
return Commit{
|
ci := Commit{
|
||||||
Message: Message,
|
Message: Message, Added: Added, Deleted: Deleted,
|
||||||
Author: Author, LOC: LOC, TimeStamp: time.Now(),
|
Author: Author, FilesChanged: FilesChanged, TimeStamp: time.Now(),
|
||||||
Repo: Repo, Path: Path,
|
Repo: Repo, Path: Path,
|
||||||
}
|
}
|
||||||
|
return ci
|
||||||
}
|
}
|
||||||
|
|
||||||
func (yf Freq) String() string {
|
func (yf Freq) String() string {
|
||||||
|
|||||||
Reference in New Issue
Block a user