forgepb/patchset.config.go

182 lines
4.8 KiB
Go

package forgepb
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/google/uuid"
"go.wit.com/log"
"google.golang.org/protobuf/proto"
)
func (f *Forge) LoadPatchsets() error {
f.Patchsets = NewPatchsets()
filename := filepath.Join(f.patchDir, "all-patches.pb")
data, err := os.ReadFile(filename)
if err != nil {
return err
}
err = f.Patchsets.Unmarshal(data)
if err != nil {
log.Infof("LoadPatchsets() proto.Marshal() error %v\n", err)
return err
}
log.Infof("LoadPatchsets() worked ok %d\n", f.Patchsets.Len())
return nil
}
func (f *Forge) SavePatchsets() error {
filename := filepath.Join(f.patchDir, "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(f.Patchsets).(*Patchsets)
if newpb == nil {
for pset := range f.Patchsets.IterAll() {
pset.ShowPatchsets()
}
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
}
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 (pb *Patchset) ShowPatchsets() 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
}
// adds a patchset or just the patches
func (f *Forge) AddPatchset(pb *Patchset) {
// if the name of the patchset is "forge auto commit"
// then just add all the patches
if pb.Name == "forge auto commit" {
author := "Author: " + pb.GitAuthorName
author += " <" + pb.GitAuthorEmail + ">"
// author := "Author: " + os.Getenv("GIT_AUTHOR_NAME")
// author += " <" + os.Getenv("GIT_AUTHOR_EMAIL") + ">"
fmt.Println(pb.Name, pb.Comment, author)
for _, patch := range pb.Patches.Patches {
// log.Info("\tnew patch:", i, patch.CommitHash, patch.Namespace)
if f.findPatch(patch) {
// log.Info("\talready found!!!!!!!", pset.Uuid, patch.Namespace)
} else {
log.Info("\tnew patch:", pb.Name, pb.Comment, author)
f.addRandomPatch(patch)
}
}
return
}
// if you got here, this patchset was submitted with a name
// Has this patchset already been submitted?
for pset := range f.Patchsets.IterAll() {
if pset.Uuid == pb.Uuid {
log.Info("ALREADY ADDED", pset.Uuid, pset.Name)
return
}
}
// Clone() this protobuf into me.forge.Patchsets
var newpb *Patchset
newpb = proto.Clone(pb).(*Patchset)
if newpb != nil {
f.Patchsets.Patchsets = append(f.Patchsets.Patchsets, newpb)
}
}
func (f *Forge) findAutoPatchset() *Patchset {
for pset := range f.Patchsets.IterAll() {
if pset.Name == "forge auto commit" {
return pset
}
}
var fauto *Patchset
log.Warn("findAutoPatchset() had to create 'forge auto commit'")
if fauto == nil {
fauto = new(Patchset)
fauto.Name = "forge auto commit"
fauto.Patches = NewPatches()
fauto.Uuid = uuid.New().String()
f.Patchsets.Patchsets = append(f.Patchsets.Patchsets, fauto)
}
return fauto
}
// adds submitted patches not specifically assigned to a patchset
// to the generic patchset called "forge auto commit"
func (f *Forge) addRandomPatch(patch *Patch) error {
// ignore patch if it's already here
if f.findPatch(patch) {
log.Info("already found patch", patch.CommitHash, patch.Namespace)
return nil
}
fauto := f.findAutoPatchset()
if fauto == nil {
return log.Errorf("no default place yet")
}
newpb := proto.Clone(patch).(*Patch)
if newpb == nil {
return log.Errorf("proto.Clone returned nil")
}
fauto.Patches.Patches = append(fauto.Patches.Patches, newpb)
return nil
}
// returns true if the patch already exists in the protobuf
func (f *Forge) findPatch(newpatch *Patch) bool {
// log.Info("\tlook for patch:", newpatch.CommitHash, newpatch.Namespace)
for pset := range f.Patchsets.IterAll() {
for _, patch := range pset.Patches.Patches {
if patch.CommitHash == newpatch.CommitHash {
// log.Info("\tfound pset!!!!!!", pset.Uuid, patch.Namespace)
return true
}
}
}
return false
}