summaryrefslogtreecommitdiff
path: root/doGui.go
blob: db0350874b52ac3753dcabbbbae864e0161d7bd7 (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
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0

package main

// An app to submit patches for the 30 GO GUI repos

import (
	"fmt"
	"os"
	"time"

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

func doGui() {
	win := gadgets.NewGenericWindow("Forge: A federated git development tool by WIT.COM", "Current Settings")
	win.Custom = func() {
		log.Warn("MAIN WINDOW CLOSE")
		okExit("")
	}

	grid := win.Group.RawGrid()
	if me.forge.Config.GetPathLock() {
		me.goSrcPwd = gadgets.NewOneLiner(grid, "Working Directory")
		me.goSrcPwd.SetText(me.forge.Config.ReposDir)
	} else {
		me.goSrcEdit = gadgets.NewBasicEntry(grid, "Working Directory")
		me.goSrcEdit.SetText(me.forge.Config.ReposDir)
		me.goSrcEdit.Custom = func() {
			log.Info("updating text to", me.goSrcEdit.String())
		}
	}
	lockpath := grid.NewCheckbox("Lock").SetChecked(me.forge.Config.PathLock)
	lockpath.Custom = func() {
		if lockpath.IsChecked() {
			log.Info("lock working directory")
			me.forge.Config.PathLock = true
		} else {
			log.Info("unlock working directory")
			me.forge.Config.PathLock = false
		}
		me.forge.Config.ConfigSave()
		okExit("you must restart forge after changing the Path Lock")
	}

	grid.NextRow()

	// use ENV GIT_AUTHOR
	me.gitAuthor = gadgets.NewOneLiner(grid, "Git Author")
	grid.NextRow()

	if os.Getenv("GIT_AUTHOR_NAME") == "" {
		me.gitAuthor.SetText("ENV GIT_AUTHOR_NAME is unset")
	} else {
		author := os.Getenv("GIT_AUTHOR_NAME")
		author += " <" + os.Getenv("GIT_AUTHOR_EMAIL") + ">"
		me.gitAuthor.SetText(author)
	}

	grid.NextRow()

	gridM := win.Stack.RawGrid()

	var insertWin *gadgets.GenericWindow
	s := fmt.Sprintf("Repos (%d)", me.forge.Repos.Len())
	me.reposWinB = gridM.NewButton(s, func() {
		// if the window exists, just toggle it open or closed
		if insertWin != nil {
			insertWin.Toggle()
			return
		}

		insertWin = makeReposWinNew()
	})

	var patchesWin *stdPatchTableWin
	var patchButton *gui.Node
	patchButton = gridM.NewButton("Your patches", func() {
		if patchesWin != nil {
			patchesWin.Toggle()
			return
		}
		if !isPatchingSafe() {
			patchButton.SetLabel("not safe yet")
			return
		}
		// patchesWin = makePatchesWin(me.forge.Patchsets)
		notdone := new(forgepb.Patches)

		all := me.forge.Patchsets.All()
		for all.Scan() {
			pset := all.Next()
			if pset.State == "DONE" {
				// skip old patchsets
				continue
			}
			AddAllPatches(notdone, pset, false)
			// AddNotDonePatches(notdone, pset, false)
		}
		notdone.PrintTable()

		patchesWin = makePatchesWin(notdone)
	})

	var pubWin *gadgets.GenericWindow
	gridM.NewButton("Publish", func() {
		if pubWin != nil {
			pubWin.Toggle()
			return
		}
		pubWin = makePublishWindow()
	})

	var oldWin *gadgets.GenericWindow
	gridM.NewButton("old", func() {
		if oldWin != nil {
			oldWin.Toggle()
			return
		}
		oldWin = makeOldStuff()
	})
}

// this is the magic that generates a window directly from the protocol buffer
func makeStandardReposGrid(pb *gitpb.Repos) *gitpb.ReposTable {
	t := pb.NewTable("testDirty")
	t.NewUuid()
	sf := t.AddStringFunc("repo", func(r *gitpb.Repo) string {
		return r.GetNamespace()
	})
	// t.Custom = func() {
	// 	log.Info("close grid?")
	// }
	sf.Custom = func(r *gitpb.Repo) {
		log.Info("do button click on", r.GetNamespace())
	}
	t.AddTimeFunc("age", func(repo *gitpb.Repo) time.Time {
		return repo.NewestTime()
	})
	t.AddMasterVersion()
	t.AddDevelVersion()
	t.AddUserVersion()
	t.AddCurrentBranchName()
	t.AddState()
	return t
}

// this is the magic that generates a window directly from the protocol buffer
func makeStandardReposWindow(title string, pb *gitpb.Repos) (*gitpb.ReposTable, *gui.Node) {
	win := gadgets.RawBasicWindow(title)
	win.Make()
	win.Show()
	win.Custom = func() {
		// sets the hidden flag to false so Toggle() works
		win.Hide()
	}
	box := win.Box().NewBox("bw vbox", false)

	t := makeStandardReposGrid(pb)
	t.SetParent(box)
	t.ShowTable()
	return t, box
}

func findMergeToDevel() *gitpb.Repos {
	found := gitpb.NewRepos()

	for repo := range me.forge.Repos.IterByFullPath() {
		// this sees if user has patches for devel. If it does, add it to found
		if repo.CountDiffObjects(repo.GetUserBranchName(), repo.GetDevelBranchName()) > 0 {
			found.AppendByFullPath(repo)
		}
	}
	now := time.Now()
	if found.Len() == 0 {
		log.Info("nothing to merge with devel")
		return found
	}
	// me.forge.PrintHumanTable(found)

	// check for merges from devel
	total, count, nope, _ := me.forge.IsEverythingOnDevel()
	log.Printf("devel branch check. %d total repos. (%d ok) (%d not on devel branch) (%s)\n", total, count, nope, shell.FormatDuration(time.Since(now)))
	return found
}

func findMergeToMaster() *gitpb.Repos {
	found := new(gitpb.Repos)

	all := me.forge.Repos.SortByFullPath()
	for all.Scan() {
		repo := all.Next()

		if me.forge.Config.IsReadOnly(repo.GetNamespace()) {
			continue
		}
		/*
			if repo.IsDirty() {
				continue
			}
			if repo.GetMasterVersion() != repo.GetDevelVersion() {
				me.found.AppendByFullPath(repo)
				continue
			}
		*/

		// this sees if devel is behind master. IT SHOULD NOT BE
		if repo.CountDiffObjects(repo.GetMasterBranchName(), repo.GetDevelBranchName()) == 0 {
			// everything is normal
		} else {
			repo.State = "DEVEL < MASTER"
			log.Info("SERIOUS ERROR. DEVEL BRANCH IS BEHIND MASTER", repo.GetNamespace())
		}

		// this sees if devel has patches for master. If it does, add it to me.found
		if repo.CountDiffObjects(repo.GetDevelBranchName(), repo.GetMasterBranchName()) > 0 {
			found.AppendByFullPath(repo)
		}
	}
	now := time.Now()
	if found.Len() == 0 {
		log.Info("nothing to merge with master")
		return found
	}
	me.forge.PrintHumanTable(found)

	// check for merges from devel
	total, count, nope, _ := me.forge.IsEverythingOnMaster()
	log.Printf("Master branch check. %d total repos. (%d ok) (%d not on master branch) (%s)\n", total, count, nope, shell.FormatDuration(time.Since(now)))

	return found
}

func mergeDevelToMaster(doit bool) {
	found := findMergeToMaster()

	if !doit {
		return
	}

	all := found.SortByFullPath()
	for all.Scan() {
		repo := all.Next()
		log.Info("repo:", repo.GetNamespace())
		if result, err := repo.MergeToMaster(); err == nil {
			log.Warn("THINGS SEEM OK", repo.GetFullPath())
			for _, line := range result.Stdout {
				log.Warn("stdout:", line)
			}
			for _, line := range result.Stderr {
				log.Warn("stderr:", line)
			}
		} else {
			log.Warn("THINGS FAILED ", repo.GetFullPath())
			log.Warn("err", err)
			if result == nil {
				break
			}
			for _, line := range result.Stdout {
				log.Warn("stdout:", line)
			}
			for _, line := range result.Stderr {
				log.Warn("stderr:", line)
			}
			log.Warn("THINGS FAILED ", repo.GetFullPath())
			break
		}
		me.forge.SetConfigSave(true)
		// view.Update()
	}
	me.forge.ConfigSave()
}

func mergeUserToDevel(doit bool) {
	found := findMergeToDevel()

	if !doit {
		return
	}

	all := found.SortByFullPath()
	for all.Scan() {
		repo := all.Next()
		bruser := repo.GetUserBranchName()
		brdevel := repo.GetDevelBranchName()

		if repo.GetUserVersion() == "uerr" {
			// no user branch
			return
		}
		log.Info("trying", bruser, repo.GetUserVersion())

		b1 := repo.CountDiffObjects(bruser, brdevel) // should be zero
		if b1 == 0 {
			// log.Info("User is already merged into Devel", repo.GetNamespace(), cmd)
			return
		}
		log.Info("merging user into devel repo:", repo.GetNamespace())
		if result, err := repo.MergeToDevel(); err == nil {
			log.Warn("THINGS SEEM OK", repo.GetFullPath())
			for _, line := range result.Stdout {
				log.Warn("stdout:", line)
			}
			for _, line := range result.Stderr {
				log.Warn("stderr:", line)
			}
		} else {
			log.Warn("THINGS FAILED ", repo.GetFullPath())
			log.Warn("err", err)
			if result == nil {
				break
			}
			for _, line := range result.Stdout {
				log.Warn("stdout:", line)
			}
			for _, line := range result.Stderr {
				log.Warn("stderr:", line)
			}
			break
		}
		me.forge.SetConfigSave(true)
		// view.Update()
	}
	me.forge.ConfigSave()
}

// old things before they are removed, deprecated, fixed, etc
func makeOldStuff() *gadgets.GenericWindow {
	oldWin := gadgets.NewGenericWindow("old code", "old code on it's way out")

	grid := oldWin.Group.RawGrid()

	// var reposWin *gadgets.GenericWindow
	var reposWin *stdReposTableWin
	grid.NewButton("Fix Repos", func() {
		if reposWin != nil {
			reposWin.Toggle()
			return
		}
		reposWin = makeReposWin()
	})

	var howtoWin *gadgets.GenericWindow
	grid.NewButton("Tutorial", func() {
		if howtoWin != nil {
			howtoWin.Toggle()
			return
		}
		howtoWin = makeHowtoWin()
	})

	grid.NextRow()
	grid.NewLabel("")

	return oldWin
}