From 680069d4ca65c75ce1d7d462d98ce7e9e4c8bfdc Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Thu, 4 Sep 2025 08:28:09 -0500 Subject: a breath of fresh air. finally can remove all the old code. --- doGui.go | 56 +++---- doPatch.go | 64 -------- helperPatches.go | 225 ++++++++++++++++++++++++++++ structs.go | 13 +- windowForgePatchsets.go | 164 -------------------- windowFound.go | 100 ------------- windowModeMaster.go | 102 ------------- windowPatches.go | 32 ++-- windowPatchsets.go | 386 ------------------------------------------------ 9 files changed, 266 insertions(+), 876 deletions(-) create mode 100644 helperPatches.go delete mode 100644 windowForgePatchsets.go delete mode 100644 windowFound.go delete mode 100644 windowModeMaster.go delete mode 100644 windowPatchsets.go diff --git a/doGui.go b/doGui.go index e42eb79..b121b93 100644 --- a/doGui.go +++ b/doGui.go @@ -13,7 +13,6 @@ import ( "go.wit.com/gui" "go.wit.com/lib/gadgets" "go.wit.com/lib/gui/shell" - "go.wit.com/lib/protobuf/forgepb" "go.wit.com/lib/protobuf/gitpb" "go.wit.com/log" ) @@ -159,24 +158,26 @@ func drawWindow(win *gadgets.GenericWindow) { } // me.forge.GetPatches() // loadUpstreamPatchsets() - if me.psets == nil { - log.Info("failed to download current patchsets") - return - } - notdone := new(forgepb.Patches) + /* + if me.psets == nil { + log.Info("failed to download current patchsets") + return + } + notdone := new(forgepb.Patches) - all := me.psets.All() - for all.Scan() { - pset := all.Next() - AddNotDonePatches(notdone, pset, false) - } + all := me.psets.All() + for all.Scan() { + pset := all.Next() + AddNotDonePatches(notdone, pset, false) + } - for patch := range notdone.IterAll() { - comment := cleanSubject(patch.Comment) - log.Info("new patch:", patch.NewHash, "commithash:", patch.CommitHash, patch.Namespace, comment) - } - // savePatchsets() - patchesWin = makePatchesWin(notdone) + for patch := range notdone.IterAll() { + comment := cleanSubject(patch.Comment) + log.Info("new patch:", patch.NewHash, "commithash:", patch.CommitHash, patch.Namespace, comment) + } + // savePatchsets() + patchesWin = makePatchesWin(notdone) + */ }) var pubWin *gadgets.GenericWindow @@ -407,27 +408,6 @@ func makeOldStuff() *gadgets.GenericWindow { grid := oldWin.Group.RawGrid() - var releaseWin *gadgets.GenericWindow - grid.NewButton("Release Window", func() { - log.Info("todo: move releaser here") - log.Info("for now, run guireleaser") - if releaseWin != nil { - releaseWin.Toggle() - return - } - releaseWin = makeModeMasterWin() - }) - - var patches *stdPatchsetTableWin - grid.NewButton("Patch Window", func() { - if patches != nil { - patches.Toggle() - return - } - patches = makePatchsetsWin() - }) - grid.NextRow() - // var reposWin *gadgets.GenericWindow var reposWin *stdReposTableWin grid.NewButton("Fix Repos", func() { diff --git a/doPatch.go b/doPatch.go index badf5f5..efbc8ec 100644 --- a/doPatch.go +++ b/doPatch.go @@ -4,8 +4,6 @@ package main import ( - "fmt" - "os" "path/filepath" "go.wit.com/lib/protobuf/forgepb" @@ -69,64 +67,9 @@ func doPatch() error { return nil } -func doPatchList() error { - openPatchsets() - if me.psets == nil { - return fmt.Errorf("Open Patchsets failed") - } - log.Info("got psets len", len(me.psets.Patchsets)) - all := me.psets.SortByName() - for all.Scan() { - pset := all.Next() - // log.Info("pset name =", pset.Name) - dumpPatchset(pset) - } - return nil -} - -func savePatchsets() error { - if me.psets == nil { - return fmt.Errorf("savePatchesets() can't save nil") - } - log.Info("savePatchsets() len =", me.psets.Len()) - data, err := me.psets.Marshal() - if err != nil { - log.Info("protobuf.Marshal() failed:", err) - return err - } - fullpath := filepath.Join(me.forge.GetConfigDir(), "patchsets.pb") - var pfile *os.File - pfile, err = os.OpenFile(fullpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) - if err != nil { - log.Info("Patchsets save failed:", err, fullpath) - return err - } - pfile.Write(data) - pfile.Close() - return nil -} - -func openPatchsets() { - fullpath := filepath.Join(me.forge.GetConfigDir(), "patchsets.pb") - data, err := os.ReadFile(fullpath) - if err != nil { - log.Info("Patchsets open failed:", err, fullpath) - return - } - - psets := new(forgepb.Patchsets) - err = psets.Unmarshal(data) - if err != nil { - log.Info("Unmarshal patchsets failed", err) - return - } - me.psets = psets -} - // returns bad if patches can not be applied // logic is not great here but it was a first pass func dumpPatchset(pset *forgepb.Patchset) bool { - // don't even bother to continue if we already know it's broken if pset.State == "BROKEN" { log.Printf("Patchset Name: %-24s Author: %s <%s> IS BAD\n", pset.Name, pset.GetGitAuthorName(), pset.GetGitAuthorEmail()) @@ -135,13 +78,6 @@ func dumpPatchset(pset *forgepb.Patchset) bool { log.Printf("Patchset Name: %-24s Author: %s <%s> IS GOOD\n", pset.Name, pset.GetGitAuthorName(), pset.GetGitAuthorEmail()) } - /* - log.Info("applyPatches() State", pset.State) - log.Info("applyPatches() COMMENT", pset.Comment) - log.Info("applyPatches() Branch Name", pset.GetStartBranchName()) - log.Info("applyPatches() Start Hash", pset.GetStartBranchHash()) - */ - var count int var bad int all := pset.Patches.SortByFilename() diff --git a/helperPatches.go b/helperPatches.go new file mode 100644 index 0000000..3450452 --- /dev/null +++ b/helperPatches.go @@ -0,0 +1,225 @@ +// Copyright 2017-2025 WIT.COM Inc. All rights reserved. +// Use of this source code is governed by the GPL 3.0 + +package main + +import ( + "bytes" + "fmt" + "os" + "os/exec" + "regexp" + "strings" + + "go.wit.com/lib/protobuf/forgepb" + "go.wit.com/log" +) + +/* +type stdPatchsetTableWin struct { + sync.Mutex + win *gadgets.GenericWindow // the machines gui window + box *gui.Node // the machines gui parent box widget + TB *forgepb.PatchsetsTable // the gui table buffer + update bool // if the window should be updated +} + +func (w *stdPatchsetTableWin) Toggle() { + if w == nil { + return + } + if w.win == nil { + return + } + w.win.Toggle() +} +*/ + +/* + etimef := func(e *forgepb.Patchset) string { + etime := e.Etime.AsTime() + s := etime.Format("2006/01/02 15:04") + if strings.HasPrefix(s, "1970/") { + // just show a blank if it's not set + return "" + } + return s + } + t.AddStringFunc("etime", etimef) +*/ + +/* + ctimef := func(p *forgepb.Patchset) string { + ctime := p.Ctime.AsTime() + return ctime.Format("2006/01/02 15:04") + } +} +*/ + +func setPatchsetState(p *forgepb.Patchset) { + var bad bool + var good bool + var done bool = true + + all := p.Patches.All() + for all.Scan() { + patch := all.Next() + // log.Info("patch:", patch.StartHash, patch.CommitHash, patch.Namespace, patch.Filename) + repo := me.forge.FindByGoPath(patch.Namespace) + if repo == nil { + log.Info("could not find repo", patch.Namespace) + bad = true + continue + } + if _, err := repo.GetHashName(patch.CommitHash); err == nil { + // this patch has been applied + patch.Applied = true + done = true + continue + } + if name, err := repo.GetHashName(patch.StartHash); err == nil { + // it might be possible to apply this patch + log.Info("patch may be good:", patch.Namespace, name, patch.CommitHash, patch.Filename) + good = true + } else { + // probably screwed up git trees + log.Info("patch with unknown origin:", patch.Namespace, name, err, patch.CommitHash, patch.Filename) + bad = true + } + } + if bad { + p.State = "BAD" + return + } + if good { + p.State = "TRY" + return + } + if done { + p.State = "DONE" + return + } +} + +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 findCommitByHash(hash string, subject string) (string, error) { + cmd := exec.Command("git", "log", "--pretty=format:%H %s") + var out bytes.Buffer + cmd.Stdout = &out + err := cmd.Run() + if err != nil { + return "", err + } + + lines := strings.Split(out.String(), "\n") + for _, line := range lines { + if strings.Contains(strings.ToLower(line), strings.ToLower(subject)) { + return strings.Fields(line)[0], nil // return the commit hash + } + if strings.Fields(line)[0] == hash { + return "", fmt.Errorf("start commit found: %s", hash) + } + } + return "", fmt.Errorf("no commit found for subject: %s", subject) +} + +func findCommitBySubject(subject string) (string, error) { + cmd := exec.Command("git", "log", "--pretty=format:%H %s", "--grep="+subject, "-i") + var out bytes.Buffer + cmd.Stdout = &out + err := cmd.Run() + if err != nil { + return "", err + } + + lines := strings.Split(out.String(), "\n") + for _, line := range lines { + if strings.Contains(strings.ToLower(line), strings.ToLower(subject)) { + return strings.Fields(line)[0], nil // return the commit hash + } + } + return "", fmt.Errorf("no commit found for subject: %s", subject) +} + +func setNewCommitHash(p *forgepb.Patchset) bool { + var done bool = true + for patch := range p.Patches.IterAll() { + // parts := strings.Fields(patch.Comment) + + repo := me.forge.FindByGoPath(patch.Namespace) + if repo == nil { + log.Info("could not find repo", patch.Namespace) + continue + } + + comment := cleanSubject(patch.Comment) + + if patch.NewHash != "na" { + log.Info("patch: newhash:", patch.NewHash, "commithash:", patch.CommitHash, patch.Namespace, comment) + continue + } + done = false + os.Chdir(repo.GetFullPath()) + newhash, err := findCommitBySubject(comment) + if err != nil { + log.Info("patch: not found hash:", patch.CommitHash, patch.Namespace, comment, newhash, err) + continue + } + patch.NewHash = newhash + log.Info("patch: found hash:", patch.CommitHash, newhash, patch.Namespace, comment) + } + + return done +} + +/* +func AddNotDonePatches(notdone *forgepb.Patches, pset *forgepb.Patchset, full bool) { + for patch := range pset.Patches.IterAll() { + comment := cleanSubject(patch.Comment) + + if found := notdone.FindByCommitHash(patch.CommitHash); found != nil { + log.Info("duplicate notdone", patch.Namespace, "patch:", patch.NewHash, "commithash:", patch.CommitHash, comment) + continue + } + + repo := me.forge.FindByGoPath(patch.Namespace) + if repo == nil { + log.Info("could not find repo", patch.Namespace) + if full { + notdone.AppendByCommitHash(patch) // double check to ensure the commit hash isn't added twice + } + continue + } + + if patch.NewHash != "na" { + log.Info("already applied patch", patch.Namespace, ": newhash:", patch.NewHash, "commithash:", patch.CommitHash, comment) + continue + } + os.Chdir(repo.GetFullPath()) + newhash, err := findCommitByHash(patch.StartHash, comment) + if err != nil { + // this patch has not been applied yet + log.Info("patch: not found hash:", patch.Namespace, patch.CommitHash, comment, err) + notdone.AppendByCommitHash(patch) // double check to ensure the commit hash isn't added twice + continue + } + + newhash, err = findCommitBySubject(comment) + if err == nil { + patch.NewHash = newhash + log.Info("patch: found hash:", patch.Namespace, "commit patch", patch.CommitHash, "new hash", newhash, "start hash", patch.StartHash, comment) + continue + } + + // this patch has not been applied yet + log.Info("patch: not found hash:", patch.Namespace, patch.CommitHash, comment, newhash, err) + notdone.AppendByCommitHash(patch) // double check to ensure the commit hash isn't added twice + } +} +*/ diff --git a/structs.go b/structs.go index 098142d..1d4a1f4 100644 --- a/structs.go +++ b/structs.go @@ -22,13 +22,12 @@ func (b *mainType) Enable() { // this app's variables type mainType struct { - pp *arg.Parser // for parsing the command line args. Yay to alexf lint! - forge *forgepb.Forge // for holding the forge protobuf files - myGui *gui.Node // the gui toolkit handle - psets *forgepb.Patchsets // the locally stored on disk patchsets - foundPaths []string // stores gopaths to act on (when doing go-clone) - configSave bool // if the config file should be saved after finishing - urlbase string // base URL + pp *arg.Parser // for parsing the command line args. Yay to alexf lint! + forge *forgepb.Forge // for holding the forge protobuf files + myGui *gui.Node // the gui toolkit handle + foundPaths []string // stores gopaths to act on (when doing go-clone) + configSave bool // if the config file should be saved after finishing + urlbase string // base URL mainWindow *gadgets.BasicWindow mainbox *gui.Node // the main box. enable/disable this diff --git a/windowForgePatchsets.go b/windowForgePatchsets.go deleted file mode 100644 index c71f384..0000000 --- a/windowForgePatchsets.go +++ /dev/null @@ -1,164 +0,0 @@ -// Copyright 2017-2025 WIT.COM Inc. All rights reserved. -// Use of this source code is governed by the GPL 3.0 - -package main - -// this is the "main" patch window. The first one -// then you can dig down and examine the patchsets and the files - -import ( - "fmt" - "strconv" - - "go.wit.com/lib/gadgets" - "go.wit.com/log" - - "go.wit.com/gui" -) - -type patchesWindow struct { - win *gadgets.BasicWindow // the patches window - stack *gui.Node // the top box set as vertical - grid *gui.Node // the list of available patches - reason *gadgets.BasicEntry // the name of the patchset - submitB *gui.Node // the submit patchet button - psetgrid *gui.Node // the list of each patchset - totalOL *gadgets.OneLiner - dirtyOL *gadgets.OneLiner - readonlyOL *gadgets.OneLiner - rw *gadgets.OneLiner - // checkB *gui.Node -} - -func (r *patchesWindow) Hidden() bool { - return r.win.Hidden() -} - -func (r *patchesWindow) Toggle() { - if r.Hidden() { - r.Show() - } else { - r.Hide() - } -} - -func (r *patchesWindow) Show() { - r.win.Show() -} - -func (r *patchesWindow) Hide() { - r.win.Hide() -} - -// you can only have one of these -func (r *patchesWindow) initWindow() { - r.win = gadgets.RawBasicWindow("Forge Patchesets") - r.win.Make() - - r.stack = r.win.Box().NewBox("bw vbox", false) - // me.reposwin.Draw() - r.win.Custom = func() { - log.Warn("Patchset Window close. setting hidden=true") - // sets the hidden flag to false so Toggle() works - r.win.Hide() - } - - r.grid = r.stack.RawGrid() - r.submitPatchesBox() - - // update the stats about the repos and patches - r.Update() -} - -func (r *patchesWindow) submitPatchesBox() { - // s := new(patchSummary) - group1 := r.stack.NewGroup("Repo Summary") - grid := group1.RawGrid() - - // make the header table for repo stats - r.totalOL = gadgets.NewOneLiner(grid, "Total") - - grid.NextRow() - r.dirtyOL = gadgets.NewOneLiner(grid, "dirty") - grid.NextRow() - r.readonlyOL = gadgets.NewOneLiner(grid, "read-only") - grid.NextRow() - r.rw = gadgets.NewOneLiner(grid, "r/w") - grid.NextRow() - - // now, make the 'widget group' and the buttons at the bottom of the window - group1 = r.stack.NewGroup("Patchset Create") - grid = group1.RawGrid() - - grid.NewButton("show current patches", func() { - r.Update() - pset, err := me.forge.MakeDevelPatchSet("current patches") - if err != nil { - log.Info("patchset creation failed", err) - return - } - if pset == nil { - log.Info("you have no current patches") - return - } - /* - win := makePatchWindow(pset) - win.Show() - */ - }) - - r.reason = gadgets.NewBasicEntry(grid, "Patchset name:") - r.reason.Custom = func() { - if r.reason.String() != "" { - log.Info("Forge: enable submit") - r.submitB.Enable() - } else { - log.Info("Forge: disable submit") - r.submitB.Disable() - } - } - r.submitB = grid.NewButton("Submit", func() { - if r.submitB.IsEnabled() { - log.Info("submit button is enabled") - } else { - log.Info("submit button is disabled. BAD GUI TOOLKIT ERROR") - return - } - // pset, err := me.forge.SubmitDevelPatchSet(r.reason.String()) - // if err != nil { - // log.Info(err) - // return - // } - // r.addPatchsetNew(pset) - }) - - // disables the submit button until the user enters a name - r.submitB.Disable() - grid.NextRow() -} - -// will update this from the current state of the protobuf -func (r *patchesWindow) Update() { - var total, dirty, readonly, rw int - - // figure out the totals - all := me.forge.Repos.SortByFullPath() - for all.Scan() { - repo := all.Next() - total += 1 - if repo.IsDirty() { - dirty += 1 - } - if me.forge.Config.IsReadOnly(repo.GetGoPath()) { - readonly += 1 - } else { - rw += 1 - } - } - - // send the values to the GUI toolkit - r.totalOL.SetText(strconv.Itoa(total) + " repos") - r.dirtyOL.SetText(strconv.Itoa(dirty) + " repos") - r.readonlyOL.SetText(strconv.Itoa(readonly) + " repos") - r.rw.SetText(fmt.Sprintf("%d repos", rw)) -} diff --git a/windowFound.go b/windowFound.go deleted file mode 100644 index 06c530f..0000000 --- a/windowFound.go +++ /dev/null @@ -1,100 +0,0 @@ -// Copyright 2017-2025 WIT.COM Inc. All rights reserved. -// Use of this source code is governed by the GPL 3.0 - -package main - -// shows a window of the 'found' repos - -import ( - "go.wit.com/lib/gadgets" - "go.wit.com/lib/protobuf/gitpb" - "go.wit.com/log" - - "go.wit.com/gui" -) - -type foundWindow struct { - win *gadgets.BasicWindow // the patches window - stack *gui.Node // the top box set as vertical - grid *gui.Node // the list of available patches - reason *gadgets.BasicEntry // the name of the patchset - submitB *gui.Node // the submit patchet button - psetgrid *gui.Node // the list of each patchset - totalOL *gadgets.OneLiner - dirtyOL *gadgets.OneLiner - readonlyOL *gadgets.OneLiner - rw *gadgets.OneLiner - found *gitpb.Repos -} - -func (r *foundWindow) Hidden() bool { - return r.win.Hidden() -} - -func (r *foundWindow) Toggle() { - if r.Hidden() { - r.Show() - } else { - r.Hide() - } -} - -func (r *foundWindow) Show() { - r.win.Show() -} - -func (r *foundWindow) Hide() { - r.win.Hide() -} - -// you can only have one of these -func (r *foundWindow) initWindow() { - r.win = gadgets.RawBasicWindow("Found Repos") - r.win.Make() - - r.stack = r.win.Box().NewBox("bw vbox", false) - // me.reposwin.Draw() - r.win.Custom = func() { - log.Warn("Found Window close. setting hidden=true") - // sets the hidden flag to false so Toggle() works - r.win.Hide() - } - group1 := r.stack.NewGroup("Repo Summary") - group1.NewButton("dirty", func() { - log.Info("find dirty here") - found := me.forge.FindDirty() - me.forge.PrintHumanTable(found) - }) - group1.NewButton("all", func() { - log.Info("find all here") - found := findAll() - me.forge.PrintHumanTable(found) - }) - - r.grid = r.stack.RawGrid() - - group1.NewButton("show", func() { - r.listRepos() - }) -} - -func (r *foundWindow) listRepos() { - for repo := range r.found.IterAll() { - r.addRepo(repo) - } -} - -func (r *foundWindow) addRepo(repo *gitpb.Repo) { - r.grid.NewButton("View", func() { - }) - r.grid.NewLabel(repo.GetGoPath()) - r.grid.NewLabel(repo.GetMasterVersion()) - r.grid.NewLabel(repo.GetDevelVersion()) - r.grid.NewLabel(repo.GetUserVersion()) - r.grid.NewLabel(repo.GetCurrentBranchName()) - r.grid.NextRow() -} - -// will update this from the current state of the protobuf -func (r *foundWindow) Update() { -} diff --git a/windowModeMaster.go b/windowModeMaster.go deleted file mode 100644 index a48875d..0000000 --- a/windowModeMaster.go +++ /dev/null @@ -1,102 +0,0 @@ -// Copyright 2017-2025 WIT.COM Inc. All rights reserved. -// Use of this source code is governed by the GPL 3.0 - -package main - -import ( - "go.wit.com/lib/gadgets" - "go.wit.com/lib/protobuf/gitpb" - "go.wit.com/log" -) - -// An app to submit patches for the 30 GO GUI repos - -func makeModeMasterWin() *gadgets.GenericWindow { - win := gadgets.NewGenericWindow("Release", "tools") - grid := win.Group.RawGrid() - - grid.NewButton("git checkout master", func() { - win.Disable() - defer win.Enable() - }) - grid.NewButton("git pull", func() { - win.Disable() - defer win.Enable() - }) - grid.NextRow() - - grid.NewButton("Clean branches", func() { - win.Disable() - defer win.Enable() - doClean() - }) - - grid.NextRow() - - grid.NewButton("check repo state", func() { - win.Disable() - defer win.Enable() - }) - - grid.NewButton("reset user branches (?)", func() { - resetUserBranchesWindow() - }) - return win -} - -func resetUserBranchesWindow() { - found := gitpb.NewRepos() - all := me.forge.Repos.SortByFullPath() - for all.Scan() { - repo := all.Next() - uname := repo.GetUserBranchName() - dname := repo.GetDevelBranchName() - if repo.GetCurrentBranchName() == uname { - log.Info("Repo is on the user branch. Can't delete it.", repo.GetGoPath()) - continue - } - b1 := repo.CountDiffObjects(uname, dname) - b2 := repo.CountDiffObjects(dname, uname) - log.Info("user vs devel count", b1, b2) - if b1 == 0 && b2 == 0 { - cmd := []string{"git", "branch", "-D", uname} - log.Info(repo.GetGoPath(), cmd) - repo.RunVerbose(cmd) - repo.Reload() - continue - } - found.Append(repo) - - } - - win := gadgets.RawBasicWindow("reset user branches") - win.Make() - win.Show() - win.Custom = func() { - // sets the hidden flag to false so Toggle() works - win.Hide() - } - box := win.Box().NewBox("bw vbox", false) - - group := box.NewGroup("test buttons") - hbox := group.Box().Horizontal() - hbox.NewButton("force delete user branch", func() { - win.Disable() - defer win.Enable() - all := found.SortByFullPath() - for all.Scan() { - repo := all.Next() - brname := repo.GetUserBranchName() - cmd := []string{"git", "branch", "-D", brname} - log.Info(repo.GetGoPath(), cmd) - repo.RunVerbose(cmd) - repo.Reload() - } - me.forge.SetConfigSave(true) - me.forge.ConfigSave() - }) - - t := makeStandardReposGrid(found) - t.SetParent(box) - t.ShowTable() -} diff --git a/windowPatches.go b/windowPatches.go index d68d246..f70ea63 100644 --- a/windowPatches.go +++ b/windowPatches.go @@ -58,23 +58,25 @@ func makePatchesWin(patches *forgepb.Patches) *stdPatchTableWin { grid.NextRow() grid.NewButton("show all", func() { - if me.psets == nil { - log.Info("No Patchsets loaded") - return - } - notdone := new(forgepb.Patches) + /* + if me.psets == nil { + log.Info("No Patchsets loaded") + return + } + notdone := new(forgepb.Patches) - all := me.psets.All() - for all.Scan() { - pset := all.Next() - AddNotDonePatches(notdone, pset, true) - } + all := me.psets.All() + for all.Scan() { + pset := all.Next() + AddNotDonePatches(notdone, pset, true) + } - for patch := range notdone.IterAll() { - comment := cleanSubject(patch.Comment) - log.Info("new patch:", patch.NewHash, "commithash:", patch.CommitHash, patch.Namespace, comment) - } - dwin.doPatchesTable(notdone) + for patch := range notdone.IterAll() { + comment := cleanSubject(patch.Comment) + log.Info("new patch:", patch.NewHash, "commithash:", patch.CommitHash, patch.Namespace, comment) + } + dwin.doPatchesTable(notdone) + */ }) grid.NewButton("Update", func() { diff --git a/windowPatchsets.go b/windowPatchsets.go deleted file mode 100644 index 3756ec2..0000000 --- a/windowPatchsets.go +++ /dev/null @@ -1,386 +0,0 @@ -// Copyright 2017-2025 WIT.COM Inc. All rights reserved. -// Use of this source code is governed by the GPL 3.0 - -package main - -import ( - "bytes" - "fmt" - "os" - "os/exec" - "regexp" - "strings" - "sync" - - "go.wit.com/gui" - "go.wit.com/lib/gadgets" - "go.wit.com/lib/protobuf/forgepb" - "go.wit.com/log" -) - -type stdPatchsetTableWin struct { - sync.Mutex - win *gadgets.GenericWindow // the machines gui window - box *gui.Node // the machines gui parent box widget - TB *forgepb.PatchsetsTable // the gui table buffer - update bool // if the window should be updated -} - -func (w *stdPatchsetTableWin) Toggle() { - if w == nil { - return - } - if w.win == nil { - return - } - w.win.Toggle() -} - -func makePatchsetsWin() *stdPatchsetTableWin { - dwin := new(stdPatchsetTableWin) - dwin.win = gadgets.NewGenericWindow("forge current patchsets", "patchset options") - dwin.win.Custom = func() { - log.Info("test delete window here") - } - grid := dwin.win.Group.RawGrid() - - grid.NewButton("ondisk", func() { - openPatchsets() - if me.psets == nil { - log.Info("No Patchsets loaded") - return - } - dwin.doPatchsetsTable(me.psets) - }) - - grid.NewButton("save", func() { - if me.psets == nil { - log.Info("No Patchsets loaded") - return - } - savePatchsets() - }) - - grid.NewButton("set patchset state", func() { - if me.psets == nil { - log.Info("No Patchsets loaded") - return - } - all := me.psets.All() - for all.Scan() { - pset := all.Next() - if pset.State == "" { - log.Info("What is up with?", pset.Name) - setPatchsetState(pset) - } else { - log.Info("patchset already had state", pset.Name, pset.State) - } - } - savePatchsets() - }) - - grid.NewButton("find commit hashes", func() { - if me.psets == nil { - log.Info("No Patchsets loaded") - return - } - all := me.psets.All() - for all.Scan() { - pset := all.Next() - if pset.State != "new" { - log.Info("patchset already had state", pset.Name, pset.State) - continue - } - if setNewCommitHash(pset) { - // everything in this patchset is applied - pset.State = "APPLIED" - } - } - savePatchsets() - }) - - grid.NewButton("show pending patches", func() { - if me.psets == nil { - log.Info("No Patchsets loaded") - return - } - notdone := new(forgepb.Patches) - - all := me.psets.All() - for all.Scan() { - pset := all.Next() - AddNotDonePatches(notdone, pset, false) - } - - for patch := range notdone.IterAll() { - comment := cleanSubject(patch.Comment) - log.Info("new patch:", patch.NewHash, "commithash:", patch.CommitHash, patch.Namespace, comment) - } - // savePatchsets() - makePatchesWin(notdone) - }) - - // make a box at the bottom of the window for the protobuf table - dwin.box = dwin.win.Bottom.Box().SetProgName("TBOX") - - // load and show the current patch sets - openPatchsets() - if me.psets == nil { - log.Info("Open Patchsets failed") - return dwin - } - dwin.doPatchsetsTable(me.psets) - - return dwin -} - -func (dwin *stdPatchsetTableWin) doPatchsetsTable(currentPatchsets *forgepb.Patchsets) { - dwin.Lock() - defer dwin.Unlock() - if dwin.TB != nil { - dwin.TB.Delete() - dwin.TB = nil - } - - // display the protobuf - dwin.TB = AddPatchsetsPB(dwin.box, currentPatchsets) - f := func(pset *forgepb.Patchset) { - log.Info("Triggered. do something here", pset.Name) - /* - win := makePatchWindow(pset) - win.Show() - */ - } - dwin.TB.Custom(f) -} - -func AddPatchsetsPB(tbox *gui.Node, pb *forgepb.Patchsets) *forgepb.PatchsetsTable { - t := pb.NewTable("PatchsetsPB") - t.NewUuid() - t.SetParent(tbox) - - t.AddStringFunc("#", func(p *forgepb.Patchset) string { - return fmt.Sprintf("%d", p.Patches.Len()) - }) - - vp := t.AddButtonFunc("View Patchset", func(p *forgepb.Patchset) string { - return p.Name - }) - vp.Custom = func(pset *forgepb.Patchset) { - log.Info("show patches here", pset.Name) - makePatchesWin(pset.Patches) - // patchwin := makePatchesWin() - // patchwin.doPatchesTable(pset.Patches) - /* - win := makePatchWindow(pset) - win.Show() - */ - } - - t.AddComment() - t.AddState() - t.AddHostname() - - ctimef := func(p *forgepb.Patchset) string { - ctime := p.Ctime.AsTime() - return ctime.Format("2006/01/02 15:04") - } - t.AddStringFunc("ctime", ctimef) - - /* - etimef := func(e *forgepb.Patchset) string { - etime := e.Etime.AsTime() - s := etime.Format("2006/01/02 15:04") - if strings.HasPrefix(s, "1970/") { - // just show a blank if it's not set - return "" - } - return s - } - t.AddStringFunc("etime", etimef) - */ - - t.AddStringFunc("Author", func(p *forgepb.Patchset) string { - return fmt.Sprintf("%s <%s>", p.GitAuthorName, p.GitAuthorEmail) - }) - - t.AddUuid() - - newCommit := t.AddButtonFunc("new hash", func(p *forgepb.Patchset) string { - return "find" - }) - newCommit.Custom = func(pset *forgepb.Patchset) { - log.Info("find new commits here", pset.Name) - // makePatchesWin(pset.Patches) - setNewCommitHash(pset) - } - - t.ShowTable() - return t -} - -func setPatchsetState(p *forgepb.Patchset) { - var bad bool - var good bool - var done bool = true - - all := p.Patches.All() - for all.Scan() { - patch := all.Next() - // log.Info("patch:", patch.StartHash, patch.CommitHash, patch.Namespace, patch.Filename) - repo := me.forge.FindByGoPath(patch.Namespace) - if repo == nil { - log.Info("could not find repo", patch.Namespace) - bad = true - continue - } - if _, err := repo.GetHashName(patch.CommitHash); err == nil { - // this patch has been applied - patch.Applied = true - done = true - continue - } - if name, err := repo.GetHashName(patch.StartHash); err == nil { - // it might be possible to apply this patch - log.Info("patch may be good:", patch.Namespace, name, patch.CommitHash, patch.Filename) - good = true - } else { - // probably screwed up git trees - log.Info("patch with unknown origin:", patch.Namespace, name, err, patch.CommitHash, patch.Filename) - bad = true - } - } - if bad { - p.State = "BAD" - return - } - if good { - p.State = "TRY" - return - } - if done { - p.State = "DONE" - return - } -} - -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 findCommitByHash(hash string, subject string) (string, error) { - cmd := exec.Command("git", "log", "--pretty=format:%H %s") - var out bytes.Buffer - cmd.Stdout = &out - err := cmd.Run() - if err != nil { - return "", err - } - - lines := strings.Split(out.String(), "\n") - for _, line := range lines { - if strings.Contains(strings.ToLower(line), strings.ToLower(subject)) { - return strings.Fields(line)[0], nil // return the commit hash - } - if strings.Fields(line)[0] == hash { - return "", fmt.Errorf("start commit found: %s", hash) - } - } - return "", fmt.Errorf("no commit found for subject: %s", subject) -} - -func findCommitBySubject(subject string) (string, error) { - cmd := exec.Command("git", "log", "--pretty=format:%H %s", "--grep="+subject, "-i") - var out bytes.Buffer - cmd.Stdout = &out - err := cmd.Run() - if err != nil { - return "", err - } - - lines := strings.Split(out.String(), "\n") - for _, line := range lines { - if strings.Contains(strings.ToLower(line), strings.ToLower(subject)) { - return strings.Fields(line)[0], nil // return the commit hash - } - } - return "", fmt.Errorf("no commit found for subject: %s", subject) -} - -func setNewCommitHash(p *forgepb.Patchset) bool { - var done bool = true - for patch := range p.Patches.IterAll() { - // parts := strings.Fields(patch.Comment) - - repo := me.forge.FindByGoPath(patch.Namespace) - if repo == nil { - log.Info("could not find repo", patch.Namespace) - continue - } - - comment := cleanSubject(patch.Comment) - - if patch.NewHash != "na" { - log.Info("patch: newhash:", patch.NewHash, "commithash:", patch.CommitHash, patch.Namespace, comment) - continue - } - done = false - os.Chdir(repo.GetFullPath()) - newhash, err := findCommitBySubject(comment) - if err != nil { - log.Info("patch: not found hash:", patch.CommitHash, patch.Namespace, comment, newhash, err) - continue - } - patch.NewHash = newhash - log.Info("patch: found hash:", patch.CommitHash, newhash, patch.Namespace, comment) - } - - return done -} - -func AddNotDonePatches(notdone *forgepb.Patches, pset *forgepb.Patchset, full bool) { - for patch := range pset.Patches.IterAll() { - comment := cleanSubject(patch.Comment) - - if found := notdone.FindByCommitHash(patch.CommitHash); found != nil { - log.Info("duplicate notdone", patch.Namespace, "patch:", patch.NewHash, "commithash:", patch.CommitHash, comment) - continue - } - - repo := me.forge.FindByGoPath(patch.Namespace) - if repo == nil { - log.Info("could not find repo", patch.Namespace) - if full { - notdone.AppendByCommitHash(patch) // double check to ensure the commit hash isn't added twice - } - continue - } - - if patch.NewHash != "na" { - log.Info("already applied patch", patch.Namespace, ": newhash:", patch.NewHash, "commithash:", patch.CommitHash, comment) - continue - } - os.Chdir(repo.GetFullPath()) - newhash, err := findCommitByHash(patch.StartHash, comment) - if err != nil { - // this patch has not been applied yet - log.Info("patch: not found hash:", patch.Namespace, patch.CommitHash, comment, err) - notdone.AppendByCommitHash(patch) // double check to ensure the commit hash isn't added twice - continue - } - - newhash, err = findCommitBySubject(comment) - if err == nil { - patch.NewHash = newhash - log.Info("patch: found hash:", patch.Namespace, "commit patch", patch.CommitHash, "new hash", newhash, "start hash", patch.StartHash, comment) - continue - } - - // this patch has not been applied yet - log.Info("patch: not found hash:", patch.Namespace, patch.CommitHash, comment, newhash, err) - notdone.AppendByCommitHash(patch) // double check to ensure the commit hash isn't added twice - } -} -- cgit v1.2.3