forgepb/patchset.config.go

83 lines
2.1 KiB
Go

package forgepb
import (
"os"
"path/filepath"
"regexp"
"strings"
"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
}