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
|
// 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"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"go.wit.com/lib/fhelp"
"go.wit.com/lib/protobuf/forgepb"
"go.wit.com/lib/protobuf/gitpb"
"go.wit.com/log"
)
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 (or use --force)")
log.Info("")
if argv.Force {
return true
}
return false
}
func doPatch() error {
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")
doPatchGet(newpb)
return nil
}
if argv.Patch.List != nil {
err := doPatchList()
return err
}
err := doPatchList()
return err
}
// 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 doPatchList() error {
curpatches := forgepb.NewPatches()
curpatches.Filename = "/tmp/curpatches.pb"
if err := curpatches.Load(); err != nil {
return err
}
curpatches.PrintTable()
for patch := range curpatches.IterAll() {
repo := me.forge.Repos.FindByNamespace(patch.Namespace)
if repo == nil {
log.Info("no namespace", patch.PatchId, patch.Namespace, patch.Comment)
continue
}
newId, newHash, err := isPatchIdApplied(repo, patch)
if errors.Is(err, ErrorGitPullOnDirty) {
log.Info("a patch with that comment couldn't be found in the repo")
} else if err != nil {
log.Info("err", patch.PatchId, patch.Namespace, patch.Comment, err)
return patch.Error(err)
}
if (newId == patch.PatchId) && (newHash == patch.CommitHash) {
log.Info(patch.PatchId, "patch made here", patch.Comment)
continue
}
if newId == patch.PatchId {
log.Info(patch.PatchId, "patch already applied", patch.Comment)
continue
}
if newId != patch.PatchId {
log.Info(patch.PatchId, "probably duplicate subject", patch.Comment)
}
log.Info("new patch", patch.PatchId, patch.Comment)
if !argv.Fix {
log.Info("use --fix to attempt to apply new patches")
} else {
if fhelp.QuestionUser("apply this patch?") {
newhash, err := applyPatch(repo, patch)
log.Info("apply results:", newhash, err)
if err != nil {
return err
}
}
}
}
return nil
}
func applyPatch(repo *gitpb.Repo, 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()
cmd := []string{"git", "am", tmpname}
err = repo.RunVerbose(cmd)
if err != nil {
log.Info("git am failed. run 'git am --abort' here")
return "", log.Errorf("git am failed")
}
log.Info("Try to find hash value now")
return p.NewHash, log.Errorf("did not lookup new hash")
}
func doPatchGet(newpb *forgepb.Sets) {
// var changed bool
curpatches := forgepb.NewPatches()
curpatches.Filename = "/tmp/curpatches.pb"
if err := curpatches.Load(); err != nil {
curpatches.Save()
curpatches.Save()
log.Info(err)
panic("no file")
// return
// // THIS IS NEEDED? NOTSURE
curpatches = forgepb.NewPatches()
curpatches.Filename = "/tmp/curpatches.pb"
curpatches.Save()
}
// newpb.PrintTable()
for pset := range newpb.IterAll() {
if pset.Patches.Len() == 0 {
log.Info("pset is empty", pset.Name)
continue
}
for patch := range pset.Patches.IterAll() {
if len(patch.Data) == 0 {
continue
}
patchid, hash, err := gitpb.FindPatchIdFromGitAm(patch.Data)
if err != nil {
log.Info("git patchid exec err", err)
continue
}
if hash != patch.CommitHash {
log.Info("ERROR: patch commit hashes's didn't match", hash, patch.CommitHash)
continue
}
if patchid != patch.PatchId {
log.Info("ERROR: patchid's didn't match", patchid, patch.PatchId)
continue
}
found := curpatches.FindByPatchId(patch.PatchId)
if found != nil {
// already have this patch
continue
}
// gitpb.FindPatchIdFromGitAmBroken(patch.Data) // doesn't os.Exec()
log.Info("adding new patch", patch.CommitHash, patch.PatchId, patch.Filename)
curpatches.AppendByPatchId(patch)
}
}
curpatches.Save()
curpatches.PrintTable()
// me.forge.Patchsets = newpb
// me.forge.Patchsets.Save()
}
var ErrorGitPullOnDirty error = errors.New("git comment is not there")
func isPatchIdApplied(repo *gitpb.Repo, patch *forgepb.Patch) (string, string, error) {
comment := cleanSubject(patch.Comment)
os.Chdir(repo.GetFullPath())
newhash, err := findCommitBySubject(comment)
if err != nil {
return "", "", ErrorGitPullOnDirty
}
patchId, err := repo.FindPatchIdByHash(newhash)
if err != nil {
return "", "", err
}
// log.Infof("%s %s found hash by comment %s \n", patchId, newhash, patch.Comment)
return patchId, newhash, 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()
found.SortNamespace()
if found.Len() == 0 {
log.Info("you currently have no repos with patches")
return false
} else {
footer := me.forge.PrintDefaultTB(found)
log.Info("repos with patches or unsaved changes:", footer)
}
return true
}
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)
}
// jcarr@framebook:~/go/src/go.wit.com/lib/protobuf/forgepb$ git branch --contains 4a27e7702b9b975b066ec9d2ee7ac932d86552e3
// * jcarr
// jcarr@framebook:~/go/src/go.wit.com/lib/protobuf/forgepb$ git merge-base --is-ancestor "4a27e7702b9b975b066ec9d2ee7ac932d86552e3" "devel" ; echo $?
// 1
// jcarr@framebook:~/go/src/go.wit.com/lib/protobuf/forgepb$ git merge-base --is-ancestor "4a27e7702b9b975b066ec9d2ee7ac932d86552e3" "jcarr" ; echo $?
// 0
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)
}
|