diff options
| author | Jeff Carr <[email protected]> | 2025-08-28 18:56:47 -0500 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-08-28 19:30:59 -0500 |
| commit | c4146f9fbe96332ede413f8ba3f3546f8346dfc0 (patch) | |
| tree | 33efbbb851af19d842bba8ae774c2b3642a24571 /patchset.config.go | |
| parent | 0539e2feee23adc0be8a88e1c6dc214cc7d98de5 (diff) | |
read in patches in Init()
Diffstat (limited to 'patchset.config.go')
| -rw-r--r-- | patchset.config.go | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/patchset.config.go b/patchset.config.go new file mode 100644 index 0000000..d931299 --- /dev/null +++ b/patchset.config.go @@ -0,0 +1,82 @@ +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 +} |
