package main import ( "fmt" "os" "path/filepath" "github.com/google/uuid" "go.wit.com/lib/protobuf/forgepb" "go.wit.com/log" "google.golang.org/protobuf/proto" ) func doMerge() error { mergePatchsets() if err := me.forge.SavePatchsets(); err != nil { log.Warn("savePatchsets() failed", err) return err } return nil } func findAutoPatchset() *forgepb.Patchset { for pset := range me.forge.Patchsets.IterAll() { if pset.Name == "forge auto commit" { return pset break } } var fauto *forgepb.Patchset log.Warn("findAutoPatchset() had to create 'forge auto commit'") if fauto == nil { fauto = new(forgepb.Patchset) fauto.Name = "forge auto commit" fauto.Patches = forgepb.NewPatches() fauto.Uuid = uuid.New().String() me.forge.Patchsets.Patchsets = append(me.forge.Patchsets.Patchsets, fauto) } return fauto } // adds submitted patches not specifically assigned to a patchset // to the generic patchset called "forge auto commit" func addRandomPatch(patch *forgepb.Patch) error { // ignore patch if it's already here if findPatch(patch) { log.Info("already found patch", patch.CommitHash, patch.Namespace) return nil } fauto := findAutoPatchset() if fauto == nil { return log.Errorf("no default place yet") } newpb := proto.Clone(patch).(*forgepb.Patch) if newpb == nil { return log.Errorf("proto.Clone returned nil") } fauto.Patches.Patches = append(fauto.Patches.Patches, newpb) return nil } // adds a patchset or just the patches func addPatchset(filename string, pb *forgepb.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(filename, pb.Name, pb.Comment, author) for _, patch := range pb.Patches.Patches { // log.Info("\tnew patch:", i, patch.CommitHash, patch.Namespace) if findPatch(patch) { // log.Info("\talready found!!!!!!!", pset.Uuid, patch.Namespace) } else { log.Info("\tnew patch:", filename, pb.Name, pb.Comment, author) addRandomPatch(patch) } } return } // if you got here, this patchset was submitted with a name // Has this patchset already been submitted? for pset := range me.forge.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 *forgepb.Patchset newpb = proto.Clone(pb).(*forgepb.Patchset) if newpb != nil { me.forge.Patchsets.Patchsets = append(me.forge.Patchsets.Patchsets, newpb) } } func mergePatchsets() { dirname := filepath.Join(LIBDIR, "patchset/") // Open the directory entries, err := os.ReadDir(dirname) if err != nil { fmt.Printf("Error reading directory: %v\n", err) return } // Iterate through the directory entries for _, entry := range entries { // Check if the entry is a file and matches the *.pb pattern if !entry.IsDir() && filepath.Ext(entry.Name()) == ".pb" { bytes, err := os.ReadFile(filepath.Join(dirname, entry.Name())) if err != nil { fmt.Println(entry.Name(), err) continue } var p *forgepb.Patchset p = new(forgepb.Patchset) err = p.Unmarshal(bytes) if err != nil { fmt.Println(entry.Name(), err) continue } addPatchset(entry.Name(), p) } } }