summaryrefslogtreecommitdiff
path: root/tagWindow.go
blob: 5eb4b8430a7676cd6bcdd84e964a67a5775a2a82 (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package repostatus

import (
	"errors"
	"path/filepath"
	"regexp"
	"strings"
	"sync"
	"time"

	"go.wit.com/gui"
	"go.wit.com/lib/gui/shell"
	"go.wit.com/log"
)

type Tag struct {
	// tracks if the tag is displayed
	hidden bool

	// the tag "v0.1.3"
	tag *gui.Node

	// the .git/ref hash
	ref *gui.Node

	// the .git/ref date
	date     *gui.Node
	duration *gui.Node

	// the tag comment
	subject *gui.Node

	// a button to delete the tag
	deleteB *gui.Node
}

// a GUI box of all the tags in a repo
type GitTagBox struct {
	// the box to list all the tags in
	box   *gui.Node
	group *gui.Node
	grid  *gui.Node

	// all the tags
	tags []*Tag
}

func (rs *RepoStatus) makeTagBox(box *gui.Node) error {
	if rs.Tags != nil {
		log.Log(WARN, "already scanned tags")
		return errors.New("already scanned tags")
	}
	tagB := new(GitTagBox)
	rs.Tags = tagB
	tagB.group = box.NewGroup(".git tags for " + rs.Path())

	//	tagB.group.NewButton("prune tags", func() {
	//		tagB.Prune()
	//	})
	var dups *gui.Node

	dups = tagB.group.NewCheckbox("Show duplicate tags").SetChecked(false)
	dups.Custom = func() {
		if dups.Checked() {
			tagB.Prune()
		} else {
			for _, t := range tagB.tags {
				t.Show()
			}
		}
	}
	tagB.group.NewButton("delete all", func() {
		/*
			for i, t := range tagB.tags {
				if t.hidden {
					// log.Info("tag is hidden", i, t.tag.String())
					continue
				}
				log.Info("tag is shown", i, t.tag.String())
				// rs.DeleteTag(t)
			}
		*/
	})

	grid := tagB.group.NewGrid("tags", 0, 0)
	tagB.grid = grid

	grid.NewLabel("version")
	grid.NewLabel("ref")
	grid.NewLabel("date")
	grid.NewLabel("release subject")
	grid.NextRow() // works like a typerwriter newline

	loop := rs.pb.Tags.SortByAge()
	for loop.Scan() {
		tag := loop.Next()

		rTag := new(Tag)
		rTag.tag = grid.NewLabel(tag.GetRefname())
		rTag.ref = grid.NewEntrybox(tag.GetHash())

		ctime := tag.GetAuthordate().AsTime()
		dur := shell.GetDurationStamp(ctime)
		rTag.date = grid.NewLabel(ctime.Format("YYYY/MM/DD"))
		rTag.duration = grid.NewLabel(dur)

		rTag.subject = grid.NewLabel(tag.GetSubject())
		rTag.deleteB = grid.NewButton("delete", func() {
			/*
				tagversion := tag.GetRefname()
				log.Info("remove tag", tagversion)
				var all [][]string
				all = append(all, []string{"git", "tag", "--delete", tagversion})
				all = append(all, []string{"git", "push", "--delete", "origin", tagversion})

				if rs.DoAll(all) {
					log.Info("TAG DELETED", rs.Path(), tagversion)
				} else {
					log.Info("TAG DELETE FAILED", rs.Path(), tagversion)
				}
			*/
		})

		tagB.tags = append(tagB.tags, rTag)
		// works like a typerwriter
		grid.NextRow()
	}
	return nil
}

func (rtags *GitTagBox) ListAll() []*Tag {
	var tags []*Tag
	for _, t := range rtags.tags {
		tags = append(tags, t)
	}
	return tags
}

func (rtags *GitTagBox) List() []*Tag {
	var tags []*Tag
	for _, t := range rtags.tags {
		if t.hidden {
			// log.Info("tag is hidden", i, t.tag.String())
			continue
		}
		// log.Info("tag is shown", t.tag.String(), rtags.rs.String())
		tags = append(tags, t)
	}
	return tags
}

func (rtags *GitTagBox) Prune() {
	dups := make(map[string]*Tag)
	for i, t := range rtags.tags {
		if t == nil {
			log.Info("tag empty:", i)
			continue
		}
		ref := t.ref.String()
		_, ok := dups[ref]
		if ok {
			log.Info("tag is duplicate:", i, t.tag.String())
		} else {
			log.Info("new tag", i, t.tag.String())
			dups[ref] = t
			t.Hide()
		}

	}
}

// hide tags worth keeping
func (rtags *GitTagBox) PruneSmart() {
	// always keep the first tag
	var first bool = true

	dups := make(map[string]*Tag)
	for i, t := range rtags.tags {
		if t == nil {
			log.Info("tag empty:", i)
			continue
		}

		// check for duplicate tags
		ref := t.ref.String()
		_, ok := dups[ref]
		if ok {
			log.Info("tag is duplicate:", i, t.tag.String())
			continue
		}
		dups[ref] = t

		// dump any tags that don't start with 'v'
		//if !strings.HasPrefix(t.tag.String(), "v") {
		//	log.Info("tag does not start with v", i, t.tag.String())
		//	continue
		//}

		isVersion := regexp.MustCompile("v[0-9]+.[0-9]+.[0-9]+").MatchString
		if isVersion(t.tag.String()) {
			if first {
				log.Info("keep first tag", i, t.tag.String())
				t.Hide()
				first = false
				continue
			}
			log.Info("valid tag", i, t.tag.String())
			t.Hide()
			continue
		}

		if first {
			log.Info("keep first tag", i, t.tag.String())
			t.Hide()
			first = false
			continue
		}

		log.Info("keep tag", i, t.tag.String())
	}
}

/*
// deleting it locally triggers some but when
// the git server was uncontactable (over IPv6 if that matters, probably it doesn't)
// and then the local delete re-added it into the tag
func (rs *RepoStatus) DeleteTag(rt *Tag) {
	cmd := []string{"git", "push", "--delete", "origin", rt.tag.String()}
	log.Info("RUN:", cmd)
	r := rs.Run(cmd)
	output := strings.Join(r.Stdout, "\n")
	if r.Error != nil {
		log.Info("cmd failed", r.Error)
		log.Info("output:", output)
	}
	log.Info("output:", output)

	cmd = []string{"git", "tag", "--delete", rt.tag.String()}
	log.Info("RUN:", cmd)
	r = rs.Run(cmd)
	output = strings.Join(r.Stdout, "\n")
	if r.Error != nil {
		log.Info("cmd failed", r.Error)
		log.Info("output:", output)
	}
	log.Info("output:", output)

}
*/

func (rt *Tag) TagString() string {
	return rt.tag.String()
}

func (rt *Tag) Hide() {
	rt.hidden = true
	rt.tag.Hide()
	rt.ref.Hide()
	rt.date.Hide()
	rt.duration.Hide()
	rt.subject.Hide()
	rt.deleteB.Hide()
}

func (rt *Tag) Show() {
	rt.hidden = false
	rt.tag.Show()
	rt.ref.Show()
	rt.date.Show()
	rt.duration.Show()
	rt.subject.Show()
	rt.deleteB.Show()
}

func (rs *RepoStatus) TagExists(findname string) bool {
	allTags := rs.Tags.ListAll()
	for _, t := range allTags {
		tagname := t.TagString()
		_, filename := filepath.Split(tagname)
		if filename == findname {
			// log.Info("found tag:", path, filename, "from", rs.Path())
			return true
		}
	}
	return false
}

func (rs *RepoStatus) LocalTagExists(findname string) bool {
	allTags := rs.Tags.ListAll()
	for _, t := range allTags {
		tagname := t.TagString()
		if strings.HasPrefix(tagname, "refs/remotes") {
			continue
		}
		path, filename := filepath.Split(tagname)
		log.Log(INFO, "tag:", path, filename, "from", rs.Path())
		if filename == findname {
			log.Log(INFO, "found tag:", path, filename, "from", rs.Path())
			return true
		}
	}
	return false
}

// returns true if 'taggy' is _ONLY_ a local tag
// this means you can not do a git pull or git push on it
func (rs *RepoStatus) IsOnlyLocalTag(taggy string) bool {
	// first make sure the tag is actually even local
	if !rs.LocalTagExists(taggy) {
		// this means it's not even local now.
		return false
	}
	// okay, taggy exists, does it exist in a remote repo?
	for _, t := range rs.Tags.ListAll() {
		tagname := t.TagString()
		if strings.HasPrefix(tagname, "refs/remotes") {
			path, filename := filepath.Split(tagname)
			if filename == taggy {
				log.Log(REPOWARN, "found tag:", path, filename, "from", rs.Path())
				return false
			}
		}
	}
	// we couldn't find the local tag anywhere remote, so it's probably only local
	return true
}

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
}

func (rs *RepoStatus) DumpTags() {
	for _, t := range rs.Tags.ListAll() {
		log.Log(REPOWARN, "tag", t.ref.String(), t.date.String(), t.tag.String())
	}
}