summaryrefslogtreecommitdiff
path: root/humanTable.go
blob: ea2ce59a83d2c99b91c4851f006e1a1b48255088 (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
// Copyright 2025 WIT.COM Inc Licensed GPL 3.0

package forgepb

import (
	"path/filepath"

	"go.wit.com/lib/cobol"
	"go.wit.com/lib/gui/shell"
	"go.wit.com/lib/protobuf/gitpb"
	"go.wit.com/log"
)

// All this does is output human readable text formatted to be viewable on
// a console with a fixed with font. AKA: a typerwriter

func (f *Forge) PrintHumanTable(allr *gitpb.Repos) {
	log.DaemonMode(true)

	t := new(tally)

	// print the header
	args := []string{"Namespace", "branch", "age", "user", "devel", "master", "", "lasttag", "next", "repo type"}
	sizes := []int{35, 9, 4, 13, 13, 13, 1, 12, 12, 8}
	log.Info(cobol.TerminalChomp(cobol.StandardTableSize10(sizes, args)))

	all := allr.SortByFullPath()
	for all.Scan() {
		repo := all.Next()
		f.printRepoToTable(repo, sizes, false)
		tallyBranchTotals(t, repo)
	}
	log.Infof("Total repositories: %d (%d master) (%d devel) (%d user) (%d unknown)\n", t.total, t.master, t.devel, t.user, t.unknown)
}

func (f *Forge) PrintForgedTable(allr *gitpb.Repos) {
	log.DaemonMode(true)

	t := new(tally)

	// print the header
	args := []string{"Namespace", "branch", "age", "master", "devel", "last tag", "", "", "", ""}
	sizes := []int{35, 9, 4, 13, 13, 13, 13, 12, 12, 8}
	log.Info(cobol.TerminalChomp(cobol.StandardTableSize10(sizes, args)))

	all := allr.SortByFullPath()
	for all.Scan() {
		repo := all.Next()
		f.printForgedToTable(repo, sizes)
		tallyBranchTotals(t, repo)
	}
	log.Infof("Total repositories: %d (%d user) (%d devel) (%d master) (%d unknown)\n", t.total, t.user, t.devel, t.master, t.unknown)
}

func (f *Forge) PrintHumanTableFull(allr *gitpb.Repos) {
	log.DaemonMode(true)

	t := new(tally)

	// print the header
	args := []string{"branch", "age", "user", "devel", "master", "", "lasttag", "next", "repo type", "Namespace"}
	sizes := []int{9, 4, 13, 13, 13, 1, 12, 12, 8, 0}
	log.Info(cobol.TerminalChomp(cobol.StandardTableSize10(sizes, args)))

	all := allr.SortByFullPath()
	for all.Scan() {
		repo := all.Next()
		f.printRepoToTable(repo, sizes, true)
		tallyBranchTotals(t, repo)
	}
	log.Infof("Total repositories: %d (%d user) (%d devel) (%d master) (%d unknown)\n", t.total, t.user, t.devel, t.master, t.unknown)
}

// also shows which files are dirty
func (f *Forge) PrintHumanTableDirty(allr *gitpb.Repos) {
	log.DaemonMode(true)

	t := new(tally)

	// print the header
	args := []string{"Namespace", "branch", "age", "user", "devel", "master", "", "lasttag", "next", "repo type"}
	sizes := []int{35, 9, 4, 13, 13, 13, 1, 12, 12, 8}
	log.Info(cobol.TerminalChomp(cobol.StandardTableSize10(sizes, args)))

	for repo := range allr.IterAll() {
		f.printRepoToTable(repo, sizes, false)
		if len(repo.DirtyList) != 0 {
			for _, line := range repo.DirtyList {
				log.Info(cobol.TerminalChomp("\t" + line))
			}
		}
		var mver string = repo.GetMasterVersion()
		repo.Tags.GetAge(mver)

		tallyBranchTotals(t, repo)
	}
	log.Infof("Total repositories: %d (%d user) (%d devel) (%d master) (%d unknown)\n", t.total, t.user, t.devel, t.master, t.unknown)
}

// used to count which repos are on which branches (master/main, devel, user)
type tally struct {
	total   int
	master  int
	devel   int
	user    int
	unknown int
}

func tallyBranchTotals(t *tally, repo *gitpb.Repo) {
	t.total += 1

	if repo.GetCurrentBranchName() == repo.GetMasterBranchName() {
		t.master += 1
		return
	}
	if repo.GetCurrentBranchName() == repo.GetDevelBranchName() {
		t.devel += 1
		return
	}
	if repo.GetCurrentBranchName() == repo.GetUserBranchName() {
		t.user += 1
		return
	}
	log.Printf("unknown curr=%s user=%s devel=%s master=%s\n", repo.GetCurrentBranchName(), repo.GetUserBranchName(), repo.GetDevelBranchName(), repo.GetMasterBranchName())
	t.unknown += 1
}

func (f *Forge) printRepoToTable(repo *gitpb.Repo, sizes []int, full bool) {
	var end string

	// shortened version numbers
	var mhort string = repo.GetMasterVersion()
	var dhort string = repo.GetDevelVersion()
	var uver string = repo.GetUserVersion()
	if uver == "uerr" {
		// blank these out
		uver = ""
	}
	var lasttag string = repo.GetLastTag()
	var thort string = repo.GetTargetVersion()
	var chort string = "" // repo.GetCurrentBranchVersion()
	var cname string = repo.GetCurrentBranchName()

	var gopath string = repo.GetNamespace()
	if gopath == "" {
		gopath = repo.GetFullPath()
	}
	var rtype string = repo.GetRepoType()
	switch rtype {
	case "binary":
		rtype = "GO bin"
	case "library":
		rtype = "GO lib"
	case "protobuf":
		rtype = "GO pb"
	}
	if f.IsPrivate(repo) {
		rtype = "priv"
	}

	// ctime := repo.Tags.GetAge(mhort)
	// age := shell.FormatDuration(time.Since(ctime))
	age := shell.FormatDuration(repo.BranchAge(cname))

	if f.Config.IsReadOnly(repo.GetGoPath()) {
		// end += "(readonly) "
	} else {
		end += "(rw) "
	}

	if repo.IsDirty() {
		age = ""
		uver = "* " + uver
		end += "(dirty) "
	}

	var args []string
	if full {
		args = []string{cname, age, uver, dhort, mhort, chort, lasttag, thort, rtype, gopath}
	} else {
		args = []string{gopath, cname, age, uver, dhort, mhort, chort, lasttag, thort, rtype}
	}
	start := cobol.StandardTableSize10(sizes, args)

	if rtype == "protobuf" {
		if repo.GoInfo.GoBinary {
			end += "(binary) "
		}
	}

	switch repo.GetState() {
	case "PERFECT":
	case "unchanged":
	case "dirty":
	case "unknown branches":
		if repo.CurrentTag == nil {
			end += "(" + repo.GetState() + ") "
		} else {
			end += "(unknown branch " + repo.CurrentTag.Refname + ") "
		}
		// end += "(invalid tag) "
	default:
		end += "(" + repo.GetState() + ") "
	}

	if repo.GetMasterBranchName() != "master" && repo.GetMasterBranchName() != "main" {
		end += "(m:" + repo.GetMasterBranchName() + ") "
	}

	if repo.GetDevelBranchName() != "devel" {
		end += "(d:" + repo.GetDevelBranchName() + ") "
	}

	if repo.GetUserBranchName() != f.Config.Username {
		end += "(u:" + repo.GetUserBranchName() + ") "
	}

	debname := f.Config.DebName(repo.GetGoPath())
	if debname != filepath.Base(gopath) {
		end += "(deb:" + debname + ") "
	}

	log.Info(cobol.TerminalChomp(start + " " + end))
}

func (f *Forge) printForgedToTable(repo *gitpb.Repo, sizes []int) {
	var end string

	// shortened version numbers
	var mhort string = repo.GetMasterVersion()
	var dhort string = repo.GetDevelVersion()

	var lasttag string = repo.GetLastTag()
	var cname string = repo.GetCurrentBranchName()

	var ns string = repo.GetNamespace()

	age := shell.FormatDuration(repo.BranchAge(cname))

	var args []string
	args = []string{ns, cname, age, mhort, dhort, lasttag, "", "", "", ""}
	start := cobol.StandardTableSize10(sizes, args)

	end += "todo"

	log.Info(cobol.TerminalChomp(start + " " + end))
}

func (psets *Patchsets) PrintTable() {
	if psets == nil {
		return
	}
	log.DaemonMode(true)

	// print the header
	args := []string{"commit hash", "new hash", "", "", "name", "Repo Namespace", "", "", "", "", ""}
	sizes := []int{12, 12, 3, 3, 40, 80, 2, 2, 2, 2}
	log.Info(cobol.TerminalChomp(cobol.StandardTableSize10(sizes, args)))

	var countCONTENTS int
	var countPARTS int
	for x, pset := range psets.GetPatchsets() {
		log.Info(pset.Uuid, pset.Name, pset.State)
		if pset.State == "DONE" {
			// old patchset
			continue
		}
		cId := log.Sprintf("%d", x)
		countCONTENTS += 1
		for i, p := range pset.Patches.GetPatches() {
			var args []string
			partId := log.Sprintf("%d", i)

			_, fname := filepath.Split(p.GetFilename())
			args = []string{p.CommitHash, p.NewHash, cId, partId, fname, p.GetNamespace(), "", "", "", "", ""}

			start := cobol.StandardTableSize10(sizes, args)
			log.Info(cobol.TerminalChomp(start))
			countPARTS += 1
		}
	}
	log.Infof("Total Contents (%d) Parts (%d)\n", countCONTENTS, countPARTS)
}

func (patches *Patches) PrintTable() {
	if patches == nil {
		return
	}
	log.DaemonMode(true)

	// print the header
	args := []string{"commit hash", "new hash", "", "", "name", "Repo Namespace", "", "", "", "", ""}
	sizes := []int{12, 12, 3, 3, 40, 80, 2, 2, 2, 2}
	log.Info(cobol.TerminalChomp(cobol.StandardTableSize10(sizes, args)))

	var countPARTS int
	for x, p := range patches.GetPatches() {
		var args []string
		partId := log.Sprintf("%d", x)

		_, fname := filepath.Split(p.GetFilename())
		args = []string{p.CommitHash, p.NewHash, partId, fname, p.GetNamespace(), "", "", "", "", ""}

		start := cobol.StandardTableSize10(sizes, args)
		log.Info(cobol.TerminalChomp(start))
		countPARTS += 1
	}
	log.Infof("Total Patches (%d)\n", countPARTS)
}