move common patches code to forgepb

This commit is contained in:
Jeff Carr 2025-08-28 18:56:27 -05:00
parent 250fa7de4a
commit 22f823bebe
5 changed files with 22 additions and 104 deletions

View File

@ -1,57 +0,0 @@
package main
import (
"os"
"path/filepath"
"go.wit.com/lib/protobuf/forgepb"
"go.wit.com/log"
"google.golang.org/protobuf/proto"
)
func LoadPatchsets() error {
me.all = forgepb.NewPatchsets()
filename := filepath.Join(LIBDIR, "all-patches.pb")
data, err := os.ReadFile(filename)
if err != nil {
return err
}
err = me.all.Unmarshal(data)
if err != nil {
log.Infof("LoadPatchsets() proto.Marshal() error %v\n", err)
return err
}
log.Infof("LoadPatchsets() worked ok %d\n", me.all.Len())
return nil
}
func SavePatchsets() error {
filename := filepath.Join(LIBDIR, "all-patches.pb")
regfile, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
log.Info("SavePatchsets() filename open error:", filename, err)
// fmt.Fprintln(w, "filename open error:", filename, err)
return err
}
defer regfile.Close()
newpb := proto.Clone(me.all).(*forgepb.Patchsets)
if newpb == nil {
for pset := range me.all.IterAll() {
showPatchsets(pset)
}
return log.Errorf("SavePatchsets() Clone failed!")
}
data, err := newpb.Marshal()
if err != nil {
log.Infof("SavePatchset() proto.Marshal() error %v\n", err)
return err
}
log.Infof("SavePatchset() worked (%d) bytes\n", len(data))
regfile.Write(data)
return nil
}

View File

@ -1,9 +1,6 @@
package main package main
import ( import (
"regexp"
"strings"
"go.wit.com/lib/protobuf/forgepb" "go.wit.com/lib/protobuf/forgepb"
"go.wit.com/log" "go.wit.com/log"
) )
@ -11,56 +8,32 @@ import (
func doList() error { func doList() error {
log.Info("do list here") log.Info("do list here")
if err := LoadPatchsets(); err != nil { if err := me.forge.LoadPatchsets(); err != nil {
badExit(err) badExit(err)
} }
// first show the general patchset protobuf // first show the general patchset protobuf
for pb := range me.all.IterAll() { for pb := range me.forge.Patchsets.IterAll() {
if pb.Name == "forge auto commit" { if pb.Name == "forge auto commit" {
showPatchsets(pb) pb.ShowPatchsets()
} }
} }
// show all the patchsets with Names // show all the patchsets with Names
for pset := range me.all.IterAll() { for pset := range me.forge.Patchsets.IterAll() {
if pset.Name == "forge auto commit" { if pset.Name == "forge auto commit" {
continue continue
} }
showPatchsets(pset) pset.ShowPatchsets()
} }
return nil return nil
} }
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 showPatchsets(pb *forgepb.Patchset) error {
author := "Author: " + pb.GitAuthorName
author += " <" + pb.GitAuthorEmail + ">"
log.Printf("%-16s %s %s %s\n", string(pb.Uuid)[0:8], pb.Name, pb.Comment, author)
for _, patch := range pb.Patches.Patches {
comment := cleanSubject(patch.Comment)
log.Printf("\t%-8s %-50s %-50s\n", string(patch.CommitHash)[0:8], patch.Namespace, comment)
}
/*
for patch := range pb.IterAll() {
comment := cleanSubject(patch.Comment)
log.Info("\tnew patch:", patch.NewHash, "commithash:", patch.CommitHash, patch.Namespace, comment)
}
*/
return nil
}
// returns true if the patch already exists in the protobuf // returns true if the patch already exists in the protobuf
func findPatch(newpatch *forgepb.Patch) bool { func findPatch(newpatch *forgepb.Patch) bool {
// log.Info("\tlook for patch:", newpatch.CommitHash, newpatch.Namespace) // log.Info("\tlook for patch:", newpatch.CommitHash, newpatch.Namespace)
for pset := range me.all.IterAll() { for pset := range me.forge.Patchsets.IterAll() {
for _, patch := range pset.Patches.Patches { for _, patch := range pset.Patches.Patches {
if patch.CommitHash == newpatch.CommitHash { if patch.CommitHash == newpatch.CommitHash {
// log.Info("\tfound pset!!!!!!", pset.Uuid, patch.Namespace) // log.Info("\tfound pset!!!!!!", pset.Uuid, patch.Namespace)

View File

@ -14,7 +14,7 @@ import (
func doMerge() error { func doMerge() error {
mergePatchsets() mergePatchsets()
if err := SavePatchsets(); err != nil { if err := me.forge.SavePatchsets(); err != nil {
log.Warn("savePatchsets() failed", err) log.Warn("savePatchsets() failed", err)
return err return err
} }
@ -22,7 +22,7 @@ func doMerge() error {
} }
func findAutoPatchset() *forgepb.Patchset { func findAutoPatchset() *forgepb.Patchset {
for pset := range me.all.IterAll() { for pset := range me.forge.Patchsets.IterAll() {
if pset.Name == "forge auto commit" { if pset.Name == "forge auto commit" {
return pset return pset
break break
@ -36,7 +36,7 @@ func findAutoPatchset() *forgepb.Patchset {
fauto.Name = "forge auto commit" fauto.Name = "forge auto commit"
fauto.Patches = forgepb.NewPatches() fauto.Patches = forgepb.NewPatches()
fauto.Uuid = uuid.New().String() fauto.Uuid = uuid.New().String()
me.all.Patchsets = append(me.all.Patchsets, fauto) me.forge.Patchsets.Patchsets = append(me.forge.Patchsets.Patchsets, fauto)
} }
return fauto return fauto
} }
@ -88,18 +88,18 @@ func addPatchset(filename string, pb *forgepb.Patchset) {
// if you got here, this patchset was submitted with a name // if you got here, this patchset was submitted with a name
// Has this patchset already been submitted? // Has this patchset already been submitted?
for pset := range me.all.IterAll() { for pset := range me.forge.Patchsets.IterAll() {
if pset.Uuid == pb.Uuid { if pset.Uuid == pb.Uuid {
log.Info("ALREADY ADDED", pset.Uuid, pset.Name) log.Info("ALREADY ADDED", pset.Uuid, pset.Name)
return return
} }
} }
// Clone() this protobuf into me.all // Clone() this protobuf into me.forge.Patchsets
var newpb *forgepb.Patchset var newpb *forgepb.Patchset
newpb = proto.Clone(pb).(*forgepb.Patchset) newpb = proto.Clone(pb).(*forgepb.Patchset)
if newpb != nil { if newpb != nil {
me.all.Patchsets = append(me.all.Patchsets, newpb) me.forge.Patchsets.Patchsets = append(me.forge.Patchsets.Patchsets, newpb)
} }
} }

14
main.go
View File

@ -48,12 +48,14 @@ func main() {
os.Setenv("FORGE_GOSRC", "/home/forge") os.Setenv("FORGE_GOSRC", "/home/forge")
} }
if err := LoadPatchsets(); err != nil { if os.Getenv("FORGE_PATCHDIR") == "" {
if argv.Force == true { os.Setenv("FORGE_PATCHDIR", "/var/lib/forged")
me.all = forgepb.NewPatchsets() }
} else {
badExit(err) me.forge = forgepb.RawInitPB()
}
if err := me.forge.LoadPatchsets(); err != nil {
badExit(err)
} }
if argv.List != nil { if argv.List != nil {

View File

@ -12,7 +12,7 @@ var me *mainType
// this app's variables // this app's variables
type mainType struct { type mainType struct {
pp *arg.Parser // for parsing the command line args. Yay to alexf lint! pp *arg.Parser // for parsing the command line args. Yay to alexf lint!
forge *forgepb.Forge // for holding the forge protobuf files
// myGui *gui.Node // the gui toolkit handle // myGui *gui.Node // the gui toolkit handle
all *forgepb.Patchsets
} }