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

package main

import (
	"path/filepath"

	"go.wit.com/lib/fhelp"
	"go.wit.com/lib/protobuf/forgepb"
	"go.wit.com/log"
)

func doPatchInit() {
	if me.forge.Patchsets != nil {
		if me.forge.Patchsets.Len() == 0 {
			// log.Info("IGNORE: patches are empty")
		} else {
			log.Info("IGNORE: patches already initalized len =", me.forge.Patchsets.Len())
		}
	}
	if err := me.forge.LoadPatchsets(); err != nil {
		log.Info("patches failed to open", err)
		if err := me.forge.SavePatchsets(); err != nil {
			log.Warn("savePatchsets() failed", err)
		}
	}
}

func isPatchingSafe() bool {
	if me.forge.Config.Mode == forgepb.ForgeMode_NORMAL {
		return true
	}
	log.Info("This patch command is not safe to run now")
	log.Info("you must reset the state of your git repositories. Run:")
	log.Info("")
	log.Info("forge normal")
	log.Info("")
	return false
}

// submit's current working patches
func doPatchSubmit() error {
	pset, err := me.forge.MakeDevelPatchSet("testing")
	if err != nil {
		return err
	}
	if pset.Patches == nil {
		log.Info("pset.Patches == nil")
		return err
	}
	if pset.Patches.Len() == 0 {
		log.Info("did not find any patches")
		return nil
	}
	pset.PrintTable()
	_, _, err = pset.HttpPost(myServer(), "new")
	return err
}

func doPatch() error {
	if argv.Patch.Repos != nil {
		showWorkRepos()
		return nil
	}

	if argv.Patch.Submit != nil {
		return doPatchSubmit()
	}

	if !isPatchingSafe() {
		return log.Errorf("not safe to work on patches")
	}

	if argv.Patch.Get != nil {
		psets := forgepb.NewSets()
		newpb, _, _ := psets.HttpPostVerbose(myServer(), "get")
		newpb.PrintTable()
		me.forge.Patchsets = newpb
		me.forge.SavePatchsets()
		return nil
	}

	if argv.Patch.Check != nil {
		/*
			old := findExpired()
			// old.PrintTable()
			for p := range old.IterAll() {
				log.Info("patch", p.Filename, p.Namespace)
			}
			newpb, err := old.HttpPostVerbose(myServer(), "check")
			if err != nil {
				return err
			}
			newpb.PrintTable()
		*/
		log.Info("do something here to find patches merged to devel")
		doMergeReport()
		return nil
	}

	if argv.Patch.List != nil {
		return doPatchList()
	}

	// list patches by default
	return doPatchList()
}

func doPatchList() error {
	// if nothing selected, list the patches
	var changed bool
	newpatches := new(forgepb.Set)
	newpatches.Patches = forgepb.NewPatches()
	for pset := range me.forge.Patchsets.IterAll() {
		pset.PrintTable()
		if pset.Patches.Len() == 0 {
			log.Info("pset is empty")
			me.forge.Patchsets.Delete(pset)
			changed = true
			continue
		}
		for patch := range pset.Patches.IterAll() {
			changed = true
			if err := isPatchIdApplied(patch); err == nil {
				log.Infof("%s patchId already applied %s\n", patch.PatchId, patch.Filename)
				pset.Patches.Delete(patch)
				changed = true
				continue
			}

			if patch.NewHash == "" || patch.NewHash == "na" {
				if newpatches.Patches.AppendByPatchId(patch) {
					log.Infof("%s patchId added here\n", patch.PatchId)
				} else {
					log.Infof("%s patchId already here\n", patch.PatchId)
					pset.Patches.Delete(patch)
					changed = true
				}
			} else {
				if err := setNewCommitHash(patch); err != nil {
					log.Infof("%s bad check on patch failure %v\n", patch.Filename, err)
					return err
				}
				log.Infof("%s %s newhash set already here\n", patch.PatchId, patch.NewHash)
			}
		}
	}
	log.Info("NEW PATCHES TABLE")
	newpatches.PrintTable()
	for patch := range newpatches.Patches.IterAll() {
		if err := setNewCommitHash(patch); err == nil {
			log.Info("newhash set already here", patch.PatchId, patch.NewHash)
			changed = true
			continue
		}
		log.Infof("%s is new\n", patch.Filename)
		repo := me.forge.FindByGoPath(patch.Namespace)
		if repo == nil {
			log.Info("\tCould not find namespace:", patch.Namespace)
			continue
		}
		if fhelp.QuestionUser("apply this patch?") {
			newhash, err := applyAndTrackPatch(repo, patch)
			log.Info("apply results:", newhash, err)
		}
	}
	if changed {
		if err := me.forge.SavePatchsets(); err != nil {
			log.Warn("savePatchsets() failed", err)
		}
	}
	return nil
	/*
		if newpatches.Len() != 0 {
			for patch := range newpatches.IterAll() {
				log.Info("new patch:", patch.CommitHash, patch.NewHash, patch.Filename)
				repo := me.forge.FindByGoPath(patch.Namespace)
				if repo == nil {
					log.Info("\tCould not find namespace:", patch.Namespace)
					continue
				}
			}
			return log.Errorf("patches need to be applied")
		}

		// return doPatchList()
		applied := findApplied()
		if applied == nil || applied.Len() == 0 {
			log.Info("no patches have been appled to the devel branch yet")
			return nil
		}
		// for patch := range applied.IterAll() {
		// 	log.Info("SEND APPLIED: newhash:", patch.NewHash, "commithash:", patch.CommitHash, "patch", patch.Namespace)
		// }
		newpb, _, err := applied.HttpPostVerbose(myServer(), "applied")
		if err != nil {
			return err
		}
		newpb.PrintTable()
		return nil
	*/
}

// Shows repos that are:
//   - git dirty repos
//   - repos with 'user' branch patches not in 'devel' branch
//   - repos with awaiting master branch verions
//
// return true if any are found
func showWorkRepos() bool {
	// always run dirty first
	me.forge.CheckDirtyQuiet()

	// if no option is given to patch, list out the
	// repos that have patches ready in them
	found := findReposWithPatches()
	if found.Len() == 0 {
		log.Info("you currently have no repos with patches")
		return false
	} else {
		me.forge.PrintHumanTable(found)
	}
	return true
}

// returns bad if patches can not be applied
// logic is not great here but it was a first pass
func showPatchset(pset *forgepb.Set) bool {
	// don't even bother to continue if we already know it's broken
	if pset.State == "BROKEN" {
		log.Printf("Patchset Name: %-24s Author: %s <%s> IS BAD\n", pset.Name, pset.GetGitAuthorName(), pset.GetGitAuthorEmail())
		return false
	} else {
		log.Printf("Patchset Name: %-24s Author: %s <%s> IS GOOD\n", pset.Name, pset.GetGitAuthorName(), pset.GetGitAuthorEmail())
	}

	var count int
	var bad int
	all := pset.Patches.SortByFilename()
	for all.Scan() {
		p := all.Next()
		if IsValidPatch(p) {
			// ok
		} else {
			pset.State = "BROKEN"
			bad += 1
		}
		count += 1
	}
	log.Info("pset has", count, "total patches, ", bad, "bad patches")
	if bad == 0 {
		return true
	}
	return false
}

func IsValidPatch(p *forgepb.Patch) bool {
	basepath, filename := filepath.Split(p.Filename)
	repo := me.forge.FindByGoPath(basepath)
	if argv.Verbose {
		log.Info("start:", p.StartHash, "end:", p.CommitHash, "file:", basepath, filename, "devel version", repo.GetDevelVersion())
	}
	if repo == nil {
		log.Info("can not apply patch! repo not found", basepath, filename)
		return false
	}
	if repo.ActualDevelHash() != p.StartHash {
		log.Info("can not apply patch! devel hash mismatch", basepath, filename)
		return false
	}
	if repo.ActualDevelHash() == p.StartHash {
		log.Info("local devel hash:", repo.ActualDevelHash(), "matches patch hash", p.StartHash, "and can be applied")
	}
	log.Info("start:", p.StartHash, "end:", p.CommitHash, "file:", basepath, filename, "devel version", repo.GetDevelVersion())
	for _, line := range p.Files {
		log.Info("\t", line)
	}
	return true
}