summaryrefslogtreecommitdiff
path: root/windowPatchsets.go
blob: 9c005feffcc155e4a3a224c00c3c7e1c37d227cc (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
389
390
391
392
393
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0

package main

import (
	"bytes"
	"fmt"
	"os"
	"os/exec"
	"regexp"
	"strings"
	"sync"

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

type stdPatchsetTableWin struct {
	sync.Mutex
	win    *gadgets.GenericWindow  // the machines gui window
	box    *gui.Node               // the machines gui parent box widget
	TB     *forgepb.PatchsetsTable // the gui table buffer
	update bool                    // if the window should be updated
}

func (w *stdPatchsetTableWin) Toggle() {
	if w == nil {
		return
	}
	if w.win == nil {
		return
	}
	w.win.Toggle()
}

func loadUpstreamPatchsets() {
	psets, err := me.forge.GetPatchesets()
	if err != nil {
		log.Info("Get Patchsets failed", err)
		return
	}

	var foundnew bool
	all := psets.All()
	for all.Scan() {
		pset := all.Next()
		found := me.psets.FindByUuid(pset.Uuid)
		if found == nil {
			log.Info("new patchset", pset.Name, pset.Uuid)
			pset.State = "new"
			foundnew = true
		} else {
			log.Info("patchset already on disk", found.Name, found.State)
			pset.State = found.State
			if pset.State == "" {
				pset.State = "new"
			}
		}
	}
	if foundnew {
		log.Info("should save these here")
		me.psets = psets
		savePatchsets()
	}
}

func makePatchsetsWin() *stdPatchsetTableWin {
	dwin := new(stdPatchsetTableWin)
	dwin.win = gadgets.NewGenericWindow("forge current patchsets", "patchset options")
	dwin.win.Custom = func() {
		log.Info("test delete window here")
	}
	grid := dwin.win.Group.RawGrid()

	grid.NewButton("ondisk", func() {
		openPatchsets()
		if me.psets == nil {
			log.Info("No Patchsets loaded")
			return
		}
		dwin.doPatchsetsTable(me.psets)
	})

	grid.NewButton("upstream", func() {
		loadUpstreamPatchsets()
		dwin.doPatchsetsTable(me.psets)
	})

	grid.NewButton("save", func() {
		if me.psets == nil {
			log.Info("No Patchsets loaded")
			return
		}
		savePatchsets()
	})

	grid.NewButton("set patchset state", func() {
		if me.psets == nil {
			log.Info("No Patchsets loaded")
			return
		}
		all := me.psets.All()
		for all.Scan() {
			pset := all.Next()
			if pset.State == "" {
				log.Info("What is up with?", pset.Name)
				setPatchsetState(pset)
			} else {
				log.Info("patchset already had state", pset.Name, pset.State)
			}
		}
		savePatchsets()
	})

	grid.NewButton("find commit hashes", func() {
		if me.psets == nil {
			log.Info("No Patchsets loaded")
			return
		}
		all := me.psets.All()
		for all.Scan() {
			pset := all.Next()
			if pset.State != "new" {
				log.Info("patchset already had state", pset.Name, pset.State)
				continue
			}
			if setNewCommitHash(pset) {
				// everything in this patchset is applied
				pset.State = "APPLIED"
			}
		}
		savePatchsets()
	})

	grid.NewButton("show pending patches", func() {
		if me.psets == nil {
			log.Info("No Patchsets loaded")
			return
		}
		notdone := new(forgepb.Patches)

		all := me.psets.All()
		for all.Scan() {
			pset := all.Next()
			AddNotDonePatches(notdone, pset, false)
		}

		for patch := range notdone.IterAll() {
			comment := cleanSubject(patch.Comment)
			log.Info("new patch:", patch.NewHash, "commithash:", patch.CommitHash, patch.RepoNamespace, comment)
		}
		// savePatchsets()
		makePatchesWin(notdone)
	})

	// make a box at the bottom of the window for the protobuf table
	dwin.box = dwin.win.Bottom.Box().SetProgName("TBOX")

	// load and show the current patch sets
	openPatchsets()
	if me.psets == nil {
		log.Info("Open Patchsets failed")
		return dwin
	}
	dwin.doPatchsetsTable(me.psets)

	return dwin
}

func (dwin *stdPatchsetTableWin) doPatchsetsTable(currentPatchsets *forgepb.Patchsets) {
	dwin.Lock()
	defer dwin.Unlock()
	if dwin.TB != nil {
		dwin.TB.Delete()
		dwin.TB = nil
	}

	// display the protobuf
	dwin.TB = AddPatchsetsPB(dwin.box, currentPatchsets)
	f := func(pset *forgepb.Patchset) {
		log.Info("Triggered. do something here", pset.Name)
		/*
			win := makePatchWindow(pset)
			win.Show()
		*/
	}
	dwin.TB.Custom(f)
}

func AddPatchsetsPB(tbox *gui.Node, pb *forgepb.Patchsets) *forgepb.PatchsetsTable {
	t := pb.NewTable("PatchsetsPB")
	t.NewUuid()
	t.SetParent(tbox)

	t.AddStringFunc("#", func(p *forgepb.Patchset) string {
		return fmt.Sprintf("%d", p.Patches.Len())
	})

	vp := t.AddButtonFunc("View Patchset", func(p *forgepb.Patchset) string {
		return p.Name
	})
	vp.Custom = func(pset *forgepb.Patchset) {
		log.Info("show patches here", pset.Name)
		makePatchesWin(pset.Patches)
		// patchwin := makePatchesWin()
		// patchwin.doPatchesTable(pset.Patches)
		/*
			win := makePatchWindow(pset)
			win.Show()
		*/
	}

	t.AddComment()
	t.AddState()
	t.AddHostname()

	ctimef := func(p *forgepb.Patchset) string {
		ctime := p.Ctime.AsTime()
		return ctime.Format("2006/01/02 15:04")
	}
	t.AddStringFunc("ctime", ctimef)

	/*
		etimef := func(e *forgepb.Patchset) string {
			etime := e.Etime.AsTime()
			s := etime.Format("2006/01/02 15:04")
			if strings.HasPrefix(s, "1970/") {
				// just show a blank if it's not set
				return ""
			}
			return s
		}
		t.AddStringFunc("etime", etimef)
	*/

	t.AddStringFunc("Author", func(p *forgepb.Patchset) string {
		return fmt.Sprintf("%s <%s>", p.GitAuthorName, p.GitAuthorEmail)
	})

	t.AddUuid()

	newCommit := t.AddButtonFunc("new hash", func(p *forgepb.Patchset) string {
		return "find"
	})
	newCommit.Custom = func(pset *forgepb.Patchset) {
		log.Info("find new commits here", pset.Name)
		// makePatchesWin(pset.Patches)
		setNewCommitHash(pset)
	}

	t.ShowTable()
	return t
}

func setPatchsetState(p *forgepb.Patchset) {
	var bad bool
	var good bool
	var done bool = true

	all := p.Patches.All()
	for all.Scan() {
		patch := all.Next()
		// log.Info("patch:", patch.StartHash, patch.CommitHash, patch.RepoNamespace, patch.Filename)
		repo := me.forge.FindByGoPath(patch.RepoNamespace)
		if repo == nil {
			log.Info("could not find repo", patch.RepoNamespace)
			bad = true
			continue
		}
		if _, err := repo.GetHashName(patch.CommitHash); err == nil {
			// this patch has been applied
			patch.Applied = true
			done = true
			continue
		}
		if name, err := repo.GetHashName(patch.StartHash); err == nil {
			// it might be possible to apply this patch
			log.Info("patch may be good:", patch.RepoNamespace, name, patch.CommitHash, patch.Filename)
			good = true
		} else {
			// probably screwed up git trees
			log.Info("patch with unknown origin:", patch.RepoNamespace, name, err, patch.CommitHash, patch.Filename)
			bad = true
		}
	}
	if bad {
		p.State = "BAD"
		return
	}
	if good {
		p.State = "TRY"
		return
	}
	if done {
		p.State = "DONE"
		return
	}
}

func cleanSubject(line string) string {
	// Regular expression to remove "Subject:" and "[PATCH...]" patterns
	re := regexp.MustCompile(`(?i)^Subject:\s*(\[\s*PATCH[^\]]*\]\s*)?`)
	cleaned := re.ReplaceAllString(line, "")
	return strings.TrimSpace(cleaned)
}

func findCommitBySubject(subject string) (string, error) {
	cmd := exec.Command("git", "log", "--pretty=format:%H %s", "--grep="+subject, "-i")
	var out bytes.Buffer
	cmd.Stdout = &out
	err := cmd.Run()
	if err != nil {
		return "", err
	}

	lines := strings.Split(out.String(), "\n")
	for _, line := range lines {
		if strings.Contains(strings.ToLower(line), strings.ToLower(subject)) {
			return strings.Fields(line)[0], nil // return the commit hash
		}
	}
	return "", fmt.Errorf("no commit found for subject: %s", subject)
}

func setNewCommitHash(p *forgepb.Patchset) bool {
	var done bool = true
	for patch := range p.Patches.IterAll() {
		// parts := strings.Fields(patch.Comment)

		repo := me.forge.FindByGoPath(patch.RepoNamespace)
		if repo == nil {
			log.Info("could not find repo", patch.RepoNamespace)
			continue
		}

		comment := cleanSubject(patch.Comment)

		if patch.NewHash != "na" {
			log.Info("patch: newhash:", patch.NewHash, "commithash:", patch.CommitHash, patch.RepoNamespace, comment)
			continue
		}
		done = false
		os.Chdir(repo.GetFullPath())
		newhash, err := findCommitBySubject(comment)
		if err != nil {
			log.Info("patch: not found hash:", patch.CommitHash, patch.RepoNamespace, comment, newhash, err)
			continue
		}
		patch.NewHash = newhash
		log.Info("patch: found hash:", patch.CommitHash, newhash, patch.RepoNamespace, comment)
	}

	return done
}

func AddNotDonePatches(notdone *forgepb.Patches, pset *forgepb.Patchset, full bool) {
	for patch := range pset.Patches.IterAll() {
		comment := cleanSubject(patch.Comment)

		if found := notdone.FindByCommitHash(patch.CommitHash); found != nil {
			log.Info("duplicate notdone patch:", patch.NewHash, "commithash:", patch.CommitHash, patch.RepoNamespace, comment)
			continue
		}

		repo := me.forge.FindByGoPath(patch.RepoNamespace)
		if repo == nil {
			log.Info("could not find repo", patch.RepoNamespace)
			if full {
				notdone.AppendByCommitHash(patch) // double check to ensure the commit hash isn't added twice
			}
			continue
		}

		if patch.NewHash != "na" {
			log.Info("already applied patch: newhash:", patch.NewHash, "commithash:", patch.CommitHash, patch.RepoNamespace, comment)
			continue
		}
		os.Chdir(repo.GetFullPath())
		newhash, err := findCommitBySubject(comment)
		if err == nil {
			patch.NewHash = newhash
			log.Info("patch: found hash:", patch.CommitHash, newhash, patch.RepoNamespace, comment)
			continue
		}

		// this patch has not been applied yet
		log.Info("patch: not found hash:", patch.CommitHash, patch.RepoNamespace, comment, newhash, err)
		notdone.AppendByCommitHash(patch) // double check to ensure the commit hash isn't added twice
	}
}