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

package main

// this is the "main" patch window. The first one
// then you can dig down and examine the patchsets and the files

import (
	"fmt"
	"strconv"

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

	"go.wit.com/gui"
)

type patchesWindow struct {
	win        *gadgets.BasicWindow // the patches window
	stack      *gui.Node            // the top box set as vertical
	grid       *gui.Node            // the list of available patches
	reason     *gadgets.BasicEntry  // the name of the patchset
	submitB    *gui.Node            // the submit patchet button
	psetgrid   *gui.Node            // the list of each patchset
	totalOL    *gadgets.OneLiner
	dirtyOL    *gadgets.OneLiner
	readonlyOL *gadgets.OneLiner
	rw         *gadgets.OneLiner
	// checkB     *gui.Node
}

func (r *patchesWindow) Hidden() bool {
	return r.win.Hidden()
}

func (r *patchesWindow) Toggle() {
	if r.Hidden() {
		r.Show()
	} else {
		r.Hide()
	}
}

func (r *patchesWindow) Show() {
	r.win.Show()
}

func (r *patchesWindow) Hide() {
	r.win.Hide()
}

// you can only have one of these
func (r *patchesWindow) initWindow() {
	r.win = gadgets.RawBasicWindow("Forge Patchesets")
	r.win.Make()

	r.stack = r.win.Box().NewBox("bw vbox", false)
	// me.reposwin.Draw()
	r.win.Custom = func() {
		log.Warn("Patchset Window close. setting hidden=true")
		// sets the hidden flag to false so Toggle() works
		r.win.Hide()
	}

	r.grid = r.stack.RawGrid()
	r.submitPatchesBox()

	// update the stats about the repos and patches
	r.Update()
}

func (r *patchesWindow) submitPatchesBox() {
	// s := new(patchSummary)
	group1 := r.stack.NewGroup("Repo Summary")
	grid := group1.RawGrid()

	// make the header table for repo stats
	r.totalOL = gadgets.NewOneLiner(grid, "Total")
	grid.NewButton("reset user branches", func() {
		resetUserBranchesWindow()
	})

	grid.NextRow()
	r.dirtyOL = gadgets.NewOneLiner(grid, "dirty")
	grid.NextRow()
	r.readonlyOL = gadgets.NewOneLiner(grid, "read-only")
	grid.NextRow()
	r.rw = gadgets.NewOneLiner(grid, "r/w")
	grid.NextRow()

	// now, make the 'widget group' and the buttons at the bottom of the window
	group1 = r.stack.NewGroup("Patchset Create")
	grid = group1.RawGrid()

	grid.NewButton("show current patches", func() {
		r.Update()
		pset, err := me.forge.MakeDevelPatchSet("current patches")
		if err != nil {
			log.Info("patchset creation failed", err)
			return
		}
		if pset == nil {
			log.Info("you have no current patches")
			return
		}
		win := makePatchWindow(pset)
		win.Show()
	})

	r.reason = gadgets.NewBasicEntry(grid, "Patchset name:")
	r.reason.Custom = func() {
		if r.reason.String() != "" {
			log.Info("Forge: enable submit")
			r.submitB.Enable()
		} else {
			log.Info("Forge: disable submit")
			r.submitB.Disable()
		}
	}
	r.submitB = grid.NewButton("Submit", func() {
		if r.submitB.IsEnabled() {
			log.Info("submit button is enabled")
		} else {
			log.Info("submit button is disabled. BAD GUI TOOLKIT ERROR")
			return
		}
		pset, err := me.forge.SubmitDevelPatchSet(r.reason.String())
		if err != nil {
			log.Info(err)
			return
		}
		r.addPatchsetNew(pset)
	})

	psets, err := openPatchsets()
	if err != nil {
		log.Info("Open Patchsets failed", err)
	}

	grid.NewButton("Update Patchset List", func() {
		psets, err = me.forge.GetPatchesets()
		if err != nil {
			log.Info("Get Patchsets failed", err)
			return
		}
		savePatchsets(psets)

		log.Info("got psets len", len(psets.Patchsets))
		all := psets.SortByName()
		for all.Scan() {
			pset := all.Next()
			r.addPatchsetNew(pset)
		}
	})

	// disables the submit button until the user enters a name
	r.submitB.Disable()
	grid.NextRow()

	g := r.stack.NewGroup("Patchset List")
	// add the grid
	r.psetgrid = g.RawGrid()

	// will look in ~/.config/forge for an existing patchset file
	// attempt to read in patchsets saved on disk

	if psets != nil {
		log.Info("got psets len", len(psets.Patchsets))
		all := psets.SortByName()
		for all.Scan() {
			pset := all.Next()
			// log.Info("pset name =", pset.Name)
			r.addPatchsetNew(pset)
		}
	}
}

func (r *patchesWindow) addPatchsetNew(pset *forgepb.Patchset) {
	var win *patchWindow
	r.psetgrid.NewButton("View", func() {
		// has the window already been created?
		if win != nil {
			win.Toggle()
			return
		}

		win = makePatchWindow(pset)
		win.Show()
	})
	r.psetgrid.NewLabel(pset.Name)
	r.psetgrid.NewLabel(pset.Comment)
	r.psetgrid.NewLabel(pset.GitAuthorName)
	r.psetgrid.NewLabel(pset.Uuid)

	ctime := pset.Ctime.AsTime()
	stime := ctime.UTC().Format("2006-01-02_15:04:05_UTC")
	r.psetgrid.NewLabel(stime)
	if pset.State == "BROKEN" {
		r.psetgrid.NewLabel("Bad")
	} else {
		r.psetgrid.NewLabel("Good")
	}

	r.psetgrid.NextRow()
}

// will update this from the current state of the protobuf
func (r *patchesWindow) Update() {
	var total, dirty, readonly, rw int

	// figure out the totals
	all := me.forge.Repos.SortByFullPath()
	for all.Scan() {
		repo := all.Next()
		total += 1
		if repo.IsDirty() {
			dirty += 1
		}
		if me.forge.Config.IsReadOnly(repo.GetGoPath()) {
			readonly += 1
		} else {
			rw += 1
		}
	}

	// send the values to the GUI toolkit
	r.totalOL.SetText(strconv.Itoa(total) + " repos")
	r.dirtyOL.SetText(strconv.Itoa(dirty) + " repos")
	r.readonlyOL.SetText(strconv.Itoa(readonly) + " repos")
	r.rw.SetText(fmt.Sprintf("%d repos", rw))
}

func resetUserBranchesWindow() {
	found := gitpb.NewRepos()
	all := me.forge.Repos.SortByFullPath()
	for all.Scan() {
		repo := all.Next()
		uname := repo.GetUserBranchName()
		dname := repo.GetDevelBranchName()
		if repo.GetCurrentBranchName() == uname {
			log.Info("Repo is on the user branch. Can't delete it.", repo.GetGoPath())
			continue
		}
		b1 := repo.CountDiffObjects(uname, dname)
		b2 := repo.CountDiffObjects(dname, uname)
		log.Info("user vs devel count", b1, b2)
		if b1 == 0 && b2 == 0 {
			cmd := []string{"git", "branch", "-D", uname}
			log.Info(repo.GetGoPath(), cmd)
			repo.RunVerbose(cmd)
			repo.Reload()
			continue
		}
		found.Append(repo)

	}

	win := gadgets.RawBasicWindow("reset user branches")
	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)

	group := box.NewGroup("test buttons")
	hbox := group.Box().Horizontal()
	hbox.NewButton("force delete user branch", func() {
		win.Disable()
		defer win.Enable()
		all := found.SortByFullPath()
		for all.Scan() {
			repo := all.Next()
			brname := repo.GetUserBranchName()
			cmd := []string{"git", "branch", "-D", brname}
			log.Info(repo.GetGoPath(), cmd)
			repo.RunVerbose(cmd)
			repo.Reload()
		}
		me.forge.SetConfigSave(true)
		me.forge.ConfigSave()
	})

	t := makeStandardReposGrid(found)
	t.SetParent(box)
	t.ShowTable()
}