From cd5f1d9d0f52196d6ea07684a4307369893911bd Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Thu, 22 Feb 2024 15:29:22 -0600 Subject: [PATCH] add NewestTag() and Tag.Age() --- git.go | 16 +++++++++++++++ gitConfig.go | 3 +++ new.go | 8 ++++++++ tagWindow.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+) diff --git a/git.go b/git.go index 5cfa9eb..c54ab8c 100644 --- a/git.go +++ b/git.go @@ -4,6 +4,7 @@ import ( "errors" "os/user" "strings" + "time" "unicode/utf8" "io/ioutil" @@ -19,6 +20,21 @@ func (rs *RepoStatus) GetCurrentBranchVersion() string { return rs.currentVersion.String() } +func (rs *RepoStatus) Age() time.Duration { + var t *Tag + t = rs.NewestTag() + + if t != nil { + log.Log(REPO, "newest tag:", t.date.String(), t.tag.String(), t.Name()) + return t.Age() + } + + const gitLayout = "Mon Jan 2 15:04:05 2006 -0700" + const madeuptime = "Mon Jun 3 15:04:05 2013 -0700" + tagTime, _ := time.Parse(gitLayout, madeuptime) + return time.Since(tagTime) +} + /* // this isn't right func (rs *RepoStatus) LastTagAge() (time.Time, string) { diff --git a/gitConfig.go b/gitConfig.go index 519d294..633d732 100644 --- a/gitConfig.go +++ b/gitConfig.go @@ -136,6 +136,9 @@ func (rs *RepoStatus) readGitConfig() error { switch currentSection { case "core": rs.gitConfig.core[key] = value + case "pull": + // don't store git config pull settings here + // probably has 'rebase = false' case "remote": test, ok := rs.gitConfig.remotes[currentName] if !ok { diff --git a/new.go b/new.go index ce05dfd..c333e24 100644 --- a/new.go +++ b/new.go @@ -32,6 +32,14 @@ func FindPathOld(path string) *RepoStatus { return windowMap[path] } +// makes a window of the status of the repo +// don't worry, you can think of it like Sierpinski carpet +// it's doesn't need to be displayed so it'll work fine even in an embedded space +func New(path string) (*RepoStatus, error) { + err, r := NewRepoStatusWindow(path) + return r, err +} + func NewRepoStatusWindow(path string) (error, *RepoStatus) { var realpath string var isGoLang bool = false diff --git a/tagWindow.go b/tagWindow.go index 05d9087..c630af7 100644 --- a/tagWindow.go +++ b/tagWindow.go @@ -5,6 +5,8 @@ import ( "regexp" "slices" "strings" + "sync" + "time" "go.wit.com/gui" "go.wit.com/log" @@ -312,3 +314,59 @@ func (rs *RepoStatus) LocalTagExists(findname string) bool { } return false } + +func (t *Tag) Age() time.Duration { + const gitLayout = "Mon Jan 2 15:04:05 2006 -0700" + tagTime, _ := time.Parse(gitLayout, t.date.String()) + return time.Since(tagTime) +} + +func (t *Tag) Name() string { + return t.tag.String() +} + +func (t *Tag) getDate() (time.Time, error) { + const gitLayout = "Mon Jan 2 15:04:05 2006 -0700" + tagTime, err := time.Parse(gitLayout, t.date.String()) + + if err != nil { + log.Log(REPOWARN, "tag date err", t.ref.String(), t.tag.String(), err) + return time.Now(), err + } + return tagTime, nil +} + +func (rs *RepoStatus) NewestTag() *Tag { + var newest *Tag + var newestTime time.Time + var tagTime time.Time + var err error + var allTags []*Tag + var mu sync.Mutex + + allTags = make([]*Tag, 0, 0) + junk := rs.Tags.ListAll() + allTags = append(allTags, junk...) + for _, t := range allTags { + mu.Lock() + if tagTime, err = t.getDate(); err != nil { + mu.Unlock() + continue + } + + // log.Log(REPOWARN, "tag", t.ref.String(), t.date.String(), t.tag.String()) + // if this is the first tag, use it + if newest == nil { + newestTime = tagTime + newest = t + } + + // if the tag date is after the newest date, it's newer so use this tag + if tagTime.After(newestTime) { + newestTime = tagTime + newest = t + } + mu.Unlock() + } + return newest +}