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

package main

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

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

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

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

func makePatchesWin(patches *forgepb.Patches) *stdPatchTableWin {
	dwin := new(stdPatchTableWin)
	dwin.win = gadgets.NewGenericWindow("current patches", "patching options")
	dwin.win.Custom = func() {
		log.Info("test delete window here")
		// dwin = nil
	}
	grid := dwin.win.Group.RawGrid()

	grid.NewLabel(fmt.Sprintf("%d", patches.Len()))
	grid.NewLabel(fmt.Sprintf("total patches"))
	grid.NextRow()

	repomap := make(map[string]int)
	all := patches.All()
	for all.Scan() {
		patch := all.Next()
		repomap[patch.RepoNamespace] += 1
	}
	grid.NewLabel(fmt.Sprintf("%d", len(repomap)))
	grid.NewLabel(fmt.Sprintf("total repos"))
	grid.NextRow()

	grid.NewButton("reload", func() {
	})
	grid.NewButton("Apply All", func() {
		var count int
		all := 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)
	})

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

	if patches != nil {
		dwin.doPatchesTable(patches)
	}

	return dwin
}

func applyPatchNew(p *forgepb.Patch) error {
	rn := p.RepoNamespace
	repo := me.forge.FindByGoPath(rn)
	if repo == nil {
		return fmt.Errorf("Could not figure out repo path %s", rn)
	}
	filename, err := savePatch(p)
	if err != nil {
		log.Info("savePatch() failed", err)
		return err
	}
	if err := applyPatch(repo, filename); err != nil {
		log.Info("warn user of git am error", err)
		return err
	}
	return nil
}

func (dwin *stdPatchTableWin) doPatchesTable(currentPatches *forgepb.Patches) {
	dwin.Lock()
	defer dwin.Unlock()
	if dwin.TB != nil {
		dwin.TB.Delete()
		dwin.TB = nil
	}

	// display the protobuf
	dwin.TB = AddPatchesPB(dwin.box, currentPatches)
	f := func(p *forgepb.Patch) {
		log.Info("do something with patch", p.Filename)
	}
	dwin.TB.Custom(f)
}

// define what rows to have in the protobuf table
func AddPatchesPB(tbox *gui.Node, pb *forgepb.Patches) *forgepb.PatchesTable {
	t := pb.NewTable("PatchesPB")
	t.NewUuid()
	t.SetParent(tbox)

	gitam := t.AddButtonFunc("apply", func(p *forgepb.Patch) string {
		return "git am"
	})
	gitam.Custom = func(p *forgepb.Patch) {
		if err := applyPatchNew(p); err != nil {
			log.Info("git am failed on file", p.Filename, "with error", err)
			return
		}
		log.Info("ran: git am", p.Filename, "ok")
	}

	t.AddCommitHash()
	t.AddRepoNamespace()
	// t.AddFilename()
	t.AddStringFunc("file", func(p *forgepb.Patch) string {
		_, fname := filepath.Split(p.Filename)
		return fname
	})
	t.AddCommitHash()

	t.ShowTable()
	return t
}

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
}