From ba3846b5f2fc4bdc0588e777ec1ddd1f28d0666d Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Thu, 28 Aug 2025 19:16:06 -0500 Subject: [PATCH] common funcs between forge & forged --- patchset.config.go | 99 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/patchset.config.go b/patchset.config.go index d931299..2788d88 100644 --- a/patchset.config.go +++ b/patchset.config.go @@ -1,11 +1,13 @@ package forgepb import ( + "fmt" "os" "path/filepath" "regexp" "strings" + "github.com/google/uuid" "go.wit.com/log" "google.golang.org/protobuf/proto" ) @@ -80,3 +82,100 @@ func (pb *Patchset) ShowPatchsets() error { */ 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 +}