summaryrefslogtreecommitdiff
path: root/gitTag.byAge.go
blob: aac234d4073939c7fe518df12562c3a141856d64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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() *GitTagScanner {
	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() *GitTagScanner {
	packs := all.selectAllGitTags()

	sort.Sort(GitTagAge(packs))

	iterator := newGitTagScanner(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) NewestTime() time.Time {
	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 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)
}