package gitpb import ( "path/filepath" "sort" "time" "go.wit.com/log" ) /* // todo: probably switch to using slices. new things added in 1.23 // https://pkg.go.dev/slices func (all *GitTags) newSort() *GitTagIterator { slices.SortFunc(all.GitTags, func(a, b *GitTag) int { if n := strings.Compare(a.Name, b.Name); n != 0 { return n } // If names are equal, order by age return cmp.Compare(a.Age, b.Age) }) } */ // all this code below is junk and seamingly wrong func (all *GitTags) GetAge(name string) time.Time { packs := all.selectAllGitTags() var newest time.Time for _, tag := range packs { // log.Info("\t\ttag", i, tag.Refname, tag.Authordate.AsTime()) _, rname := filepath.Split(tag.Refname) if name == rname { // log.Info("\t\tfound tag", i, rbase, rname, tag.Authordate.AsTime()) newest = tag.Authordate.AsTime() return newest } newest = tag.Authordate.AsTime() } return newest } func (all *GitTags) SortByAge() *GitTagIterator { packs := all.selectAllGitTags() sort.Sort(GitTagAge(packs)) iterator := newGitTagIterator(packs) return iterator } type GitTagAge []*GitTag func (a GitTagAge) Len() int { return len(a) } // sorts in ? order func (a GitTagAge) Less(i, j int) bool { if time.Since(a[i].Authordate.AsTime()) < time.Since(a[j].Authordate.AsTime()) { return true } return false } func (a GitTagAge) Swap(i, j int) { a[i], a[j] = a[j], a[i] } // biased code that gives out newer tag dates // even if the code hasn't been patched func (repo *Repo) NewestAge() time.Duration { alltags := repo.Tags.selectAllGitTags() var newest time.Time for _, tag := range alltags { // check the actual age of the patch if newest.Before(tag.Authordate.AsTime()) { newest = tag.Authordate.AsTime() } // check the age of the commit if newest.Before(tag.Creatordate.AsTime()) { newest = tag.Creatordate.AsTime() } } return time.Since(newest) } func (repo *Repo) NewestAgeVerbose() time.Duration { alltags := repo.Tags.selectAllGitTags() var newest time.Time var cur time.Time for i, tag := range alltags { cur = tag.Authordate.AsTime() rbase, rname := filepath.Split(tag.Refname) log.Info("\t\tfound tag", i, rbase, rname, tag.Authordate.AsTime(), tag.Creatordate.AsTime()) if newest.Before(cur) { newest = cur } } return time.Since(newest) } // not really accurate. temprorary until git Config() parsing is better func (repo *Repo) BranchAge(branch string) time.Duration { alltags := repo.Tags.selectAllGitTags() var newest time.Time var cur time.Time var auth time.Time for _, tag := range alltags { cur = tag.Creatordate.AsTime() auth = tag.Authordate.AsTime() if branch == filepath.Base(tag.Refname) { // log.Info("\t\tfound tag", i, branch, tag.Authordate.AsTime(), tag.Creatordate.AsTime()) if cur.Before(auth) { return time.Since(auth) } return time.Since(cur) } // check both dates I guess if newest.Before(auth) { newest = auth } if newest.Before(cur) { newest = cur } } return time.Since(newest) }