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

package main

import (
	"os"
	"path/filepath"
	"sync"

	"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 patchWindow struct {
	once  sync.Once            // only init() the window once
	win   *gadgets.BasicWindow // the patches window
	stack *gui.Node            // the top box set as vertical
	shelf *gui.Node            // the first box in the stack, set as horizontal
	grid  *gui.Node            // the list of available patches
	// summary *patchSummary        // summary of current patches
	setgrid *gui.Node         // the list of each patchset
	pset    *forgepb.Patchset // the patchset in question
}

// todo: autogenerate these or make them standared 'gui' package functions
// make this an go interface somehow

// is the window hidden right now?
func (w *patchWindow) Hidden() bool {
	return w.win.Hidden()
}

// switches between the window being visable or hidden on the desktop
func (w *patchWindow) Toggle() {
	if w.Hidden() {
		w.Show()
	} else {
		w.Hide()
	}
}

// hides the window completely
func (w *patchWindow) Show() {
	w.win.Show()
}

func (w *patchWindow) Hide() {
	w.win.Hide()
}

// should be the first box/widget in the window
// greys out the window to the user
func (w *patchWindow) Disable() {
	w.stack.Disable()
}

func (w *patchWindow) Enable() {
	w.stack.Enable()
}

// you can only have one of these
func makePatchWindow(pset *forgepb.Patchset) *patchWindow {
	pw := new(patchWindow)

	// sync.Once()
	pw.win = gadgets.RawBasicWindow("Patcheset for " + pset.Name + " (" + pset.Comment + ")")
	pw.win.Make()

	pw.stack = pw.win.Box().NewBox("bw vbox", false)
	// me.reposwin.Draw()
	pw.win.Custom = func() {
		// sets the hidden flag to false so Toggle() works
		pw.win.Hide()
	}

	grid := pw.stack.NewGrid("", 0, 0)

	grid.NewLabel("Patchset Details:")
	grid.NewLabel(pset.GitAuthorName)
	grid.NewLabel(pset.GitAuthorEmail)
	grid.NewLabel("start branch: " + pset.StartBranchName)
	grid.NewLabel(pset.StartBranchHash)
	grid.NewLabel("end branch: " + pset.EndBranchName)
	grid.NewLabel(pset.EndBranchHash)
	grid.NewButton("Apply All", func() {
		var count int
		all := pset.Patches.SortByFilename()
		for all.Scan() {
			p := all.Next()
			rn := p.RepoNamespace
			repo := me.forge.FindByGoPath(rn)
			if repo == nil {
				log.Info("Could not figure out repo path", rn)
				return
			}
			filename, err := savePatch(p)
			if err != nil {
				log.Info("savePatch() failed", err)
				return
			}
			count += 1
			if err := applyPatch(repo, filename); err != nil {
				log.Info("warn user of git am error", err)
				return
			}
		}
		log.Info("ALL PATCHES WORKED! count =", count)
	})
	grid.NextRow()

	g := pw.stack.NewGroup("PatchSet List")

	// make a grid to put the list of git repos that have patches
	// in this particular patchset
	grid = g.NewGrid("", 0, 0)
	grid.NewLabel("repo")
	grid.NewLabel("patch name")
	grid.NewLabel("Applied in current branch?")
	grid.NewLabel("start hash")
	grid.NewLabel("")
	/*
		grid.NewButton("Extract", func() {
			if err := savePatchset(pset); err != nil {
				log.Info("Save err:", err)
				return
			}
		})
			grid.NewButton("Apply with git am", func() {
				if _, _, _, err := IsEverythingOnUser(); err != nil {
					log.Info("You can only apply patches to the user branch")
					return
				}
				if IsAnythingDirty() {
					log.Info("You can't apply patches when repos are dirty")
					me.forge.PrintHumanTable(me.found)
					return
				}
				if argv.Force {
					applyPatchset(pset)
				}
			})
	*/
	grid.NextRow()

	// add the patches to the grid
	pw.addPatchset(grid, pset)
	return pw
}

func (r *patchWindow) addPatchset(grid *gui.Node, pset *forgepb.Patchset) {
	/*
		repomap := make(map[*gitpb.Repo][]*forgepb.Patch)
		repohash := make(map[*gitpb.Repo]string)

		// sort patches by repo namespace
		all := pset.Patches.SortByFilename()
		for all.Scan() {
			p := all.Next()
			s := p.RepoNamespace
			repo := me.forge.FindByGoPath(s)
			if repo == nil {
				log.Info("Could not figure out repo path", s)
				continue
			}
			repomap[repo] = append(repomap[repo], p)
			repohash[repo] = p.StartHash
		}
	*/

	all := pset.Patches.SortByFilename()
	for all.Scan() {
		p := all.Next()
		// for repo, patches := range repomap {
		rn := p.RepoNamespace
		repo := me.forge.FindByGoPath(rn)
		if repo == nil {
			log.Info("Could not figure out repo path", rn)
			rn += " bad repo"
		}
		log.Info("Adding patches for", rn)
		grid.NewLabel(rn)

		/*
			for i, p := range patches {
				log.Info(i, p.Filename)
				grid.NewLabel(p.Comment)
				grid.NewLabel("in current branch?")
				break
			}
		*/
		// hash := repohash[repo]
		grid.NewLabel(p.StartHash)
		grid.NewLabel(p.Filename)

		if repo == nil {
			continue
		}
		// var win *repoPatchWindow
		grid.NewButton("apply", func() {
			filename, _ := savePatch(p)
			if err := applyPatch(repo, filename); err != nil {
				log.Info("warn user of git am error", err)
			}
			// win = makeRepoPatchWindow(repo, p)
			// win.Show()
		})
		grid.NewCheckbox("").SetChecked(true)
		grid.NewCheckbox("").SetChecked(true)
		grid.NewButton("save patch to /tmp", func() {
			savePatch(p)
			// for _, pat := range patches {
			// }
		})
		/*
			grid.NewButton("view hash", func() {
				cmd := []string{"git", "whatchanged", hash}
				log.Info(repo.GetFullPath(), cmd)
			})
			grid.NewButton("git am", func() {
				cmd := []string{"git", "whatchanged", hash}
				log.Info(repo.GetFullPath(), cmd)
			})
		*/
		grid.NextRow()
	}
}

func applyPatch(repo *gitpb.Repo, filename string) error {
	cmd := []string{"git", "am", filename}
	err := repo.RunVerbose(cmd)
	return err
}

func savePatch(p *forgepb.Patch) (string, error) {
	_, filen := filepath.Split(p.Filename)
	tmpname := filepath.Join("/tmp", filen)
	log.Info("saving as", tmpname, p.Filename)
	raw, err := os.OpenFile(tmpname, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
	if err != nil {
		return "", err
	}
	raw.Write(p.Data)
	raw.Close()

	return tmpname, nil
}

// saves the patches in ~/.config/forge/currentpatches/
func savePatchset(pset *forgepb.Patchset) error {
	log.Info("savePatches() NAME", pset.Name)
	log.Info("savePatches() COMMENT", pset.Comment)
	log.Info("savePatches() GIT_AUTHOR_NAME", pset.GetGitAuthorName())
	log.Info("savePatches() GIT_AUTHOR_EMAIL", pset.GetGitAuthorEmail())
	log.Info("savePatches() Branch Name", pset.GetStartBranchName())
	log.Info("savePatches() Start Hash", pset.GetStartBranchHash())

	var count int
	var bad int
	var lasterr error
	all := pset.Patches.SortByFilename()
	for all.Scan() {
		p := all.Next()
		basedir := filepath.Join(os.Getenv("FORGE_CONFIG"), "currentpatches")
		if fullname, err := savePatchFile(p, basedir); err != nil {
			log.Info(fullname, "save failed", err)
			bad += 1
			lasterr = err
		}
		count += 1
	}
	log.Info("pset has", count, "total patches, ", bad, "bad save patches")
	if bad == 0 {
		return lasterr
	}
	return nil
}