submit-patchset/submitPatches.go

296 lines
7.4 KiB
Go

package main
import (
"path/filepath"
"strconv"
"strings"
"go.wit.com/gui"
"go.wit.com/lib/gadgets"
"go.wit.com/lib/gui/gowit"
"go.wit.com/lib/gui/repostatus"
"go.wit.com/lib/gui/shell"
"go.wit.com/log"
)
type patch struct {
ref string
giturl string
comment string
rs *repostatus.RepoStatus
}
type patchSummary struct {
grid *gui.Node
updateB *gui.Node
docsB *gui.Node
gitPushB *gui.Node
gitPullB *gui.Node
checkB *gui.Node
totalOL *gadgets.OneLiner
dirtyOL *gadgets.OneLiner
readonlyOL *gadgets.OneLiner
totalPatchesOL *gadgets.OneLiner
unknownOL *gadgets.OneLiner
unknownSubmitB *gui.Node
reason *gadgets.BasicEntry
submitB *gui.Node
allp []*patch
}
func submitPatchesBox(box *gui.Node) *patchSummary {
s := new(patchSummary)
group1 := box.NewGroup("Submit Patches Summary")
s.grid = group1.RawGrid()
s.grid.NewButton("List Patches", func() {
for i, p := range s.allp {
log.Info(i, p.ref, p.rs.String())
}
// update the stats
s.Update()
})
s.gitPullB = s.grid.NewButton("git pull", func() {
me.Disable()
defer me.Enable()
for _, repo := range me.allrepos {
// gitcmd := []string{"git", "fetch", "origin"}
gitcmd := []string{"git", "pull"}
err, output := repo.status.RunCmd(gitcmd)
log.Info("output =", output)
if err == nil {
log.Info("git fetch worked", repo.String())
} else {
log.Info("git fetch failed", repo.String())
return
}
}
s.gitPullB.SetText("GOOD")
// update the stats
s.Update()
})
s.gitPushB = s.grid.NewButton("git push", func() {
me.Disable()
defer me.Enable()
for _, repo := range me.allrepos {
gitcmd := []string{"git", "push"}
err, output := repo.status.RunCmd(gitcmd)
log.Info("output =", output)
if err == nil {
log.Info("git push worked", repo.String())
} else {
log.Info("git push failed", repo.String())
return
}
}
s.gitPushB.SetText("GOOD")
// update the stats
s.Update()
})
s.checkB = s.grid.NewButton("Check repos are working", func() {
me.Disable()
defer me.Enable()
for _, repo := range me.allrepos {
if repo.giturl != "" {
log.Info("repo already checked. do they match?", repo.String())
log.Info("go.wit.com =", repo.giturl)
log.Info("localurl =", repo.status.GitURL())
} else {
ok, giturl := gowit.CheckRegistered(repo.status)
if ok {
log.Info("is url correct?", repo.String(), "vs", giturl)
repo.giturl = giturl
if giturl != repo.status.GitURL() {
log.Info("repo check failed", repo.String())
s.unknownOL.SetText(repo.String())
s.unknownOL.Show()
s.unknownSubmitB.Show()
return
}
} else {
log.Info("repo check failed", repo.String())
repo.giturl = "look in .git/config"
s.unknownOL.SetText(repo.String())
s.unknownOL.Show()
s.unknownSubmitB.Show()
return
}
}
}
s.checkB.SetText("GOOD")
})
s.grid.NextRow()
s.unknownOL = gadgets.NewOneLiner(s.grid, "Unknown Repo:")
s.unknownSubmitB = s.grid.NewButton("Register Repo", func() {
log.Info("Submit repo:", s.unknownOL.String())
repo, ok := me.allrepos[s.unknownOL.String()]
if ok {
log.Info("found repo:", repo.String(), "with giturl", repo.giturl)
localurl := repo.status.GitURL()
if localurl == "" {
log.Info("local repo check failed. repo is not uploaded?")
} else {
log.Info("local repo has", localurl)
// attempts to register the unknown repo
if gowit.Register(repo.String(), localurl) {
s.unknownOL.Hide()
s.unknownSubmitB.Hide()
}
}
} else {
log.Info("what is this?", s.unknownOL.String())
}
})
s.unknownOL.Hide()
s.unknownSubmitB.Hide()
s.grid.NextRow()
s.totalOL = gadgets.NewOneLiner(s.grid, "Total")
s.grid.NextRow()
s.dirtyOL = gadgets.NewOneLiner(s.grid, "dirty")
s.grid.NextRow()
s.readonlyOL = gadgets.NewOneLiner(s.grid, "read-only")
s.grid.NextRow()
s.totalPatchesOL = gadgets.NewOneLiner(s.grid, "total commits")
s.grid.NextRow()
s.reason = gadgets.NewBasicEntry(s.grid, "patch name:")
s.reason.Custom = func() {
if s.reason.String() != "" {
s.submitB.Enable()
} else {
s.submitB.Disable()
}
}
s.submitB = s.grid.NewButton("Submit Patches", func() {
patchdir := filepath.Join(me.userHomePwd.String(), "autotypist.patchset")
if shell.Exists(patchdir) {
log.Info("patchset dir already exists", patchdir)
return
} else {
shell.Mkdir(patchdir)
}
if !shell.Exists(patchdir) {
log.Info("something went wrong making", patchdir)
return
}
if makePatchset(patchdir) {
}
})
// disable these until there are not dirty repos
s.reason.Disable()
s.submitB.Disable()
return s
}
func (s *patchSummary) Update() {
var total, dirty, readonly int
for _, repo := range me.allrepos {
total += 1
if repo.status.CheckDirty() {
if repo.String() == "go.wit.com/apps/autotypist" {
// log.Info("ignoring dirty autotypist for now")
dirty += 1
} else {
dirty += 1
}
}
if repo.status.ReadOnly() {
readonly += 1
}
}
s.totalOL.SetText(strconv.Itoa(total) + " repos")
s.dirtyOL.SetText(strconv.Itoa(dirty) + " repos")
s.readonlyOL.SetText(strconv.Itoa(readonly) + " repos")
p, allp := s.GetPatches()
if s.allp == nil {
s.allp = make([]*patch, 0, 0)
s.allp = append(s.allp, allp...)
}
if dirty == 0 {
s.totalPatchesOL.SetText(strconv.Itoa(p) + " patches")
s.reason.Enable()
// force the user to submit a reason to enable the submit button
// s.submitB.Enable()
} else {
s.totalPatchesOL.SetText(strconv.Itoa(p) + " patches + ? dirty")
}
}
func (s *patchSummary) GetPatches() (int, []*patch) {
var patchcount int
patches := make([]*patch, 0, 0)
for _, repo := range me.allrepos {
// git log --oneline devel..jcarr
userv := repo.status.GetUserVersion()
develv := repo.status.GetDevelVersion()
usern := repo.status.GetUserBranchName()
develn := repo.status.GetDevelBranchName()
if userv == develv {
// log.Info("skipping unchanged repo", repo.String())
} else {
// log.Info("repo userv, develv", userv, develv)
gitcmd := []string{"git", "log", "--oneline", develn + ".." + usern}
// log.Info("Run:", gitcmd)
err, output := repo.status.RunCmd(gitcmd)
if err == nil {
// patches := strings.Split(output, "\n")
for _, line := range strings.Split(output, "\n") {
parts := strings.Split(line, " ")
newp := new(patch)
newp.rs = repo.status
newp.ref = parts[0]
newp.comment = strings.Join(parts[1:], " ")
log.Info("patch:", line, newp.rs.String())
patchcount += 1
patches = append(patches, newp)
}
} else {
log.Info("git failed err=", err)
}
}
}
return patchcount, patches
}
func makePatchset(setdir string) bool {
for _, repo := range me.allrepos {
userv := repo.status.GetUserVersion()
develv := repo.status.GetDevelVersion()
usern := repo.status.GetUserBranchName()
develn := repo.status.GetDevelBranchName()
if userv == develv {
// this repo is unchanged
continue
}
repodir := filepath.Join(setdir, repo.String())
shell.Mkdir(repodir)
// git format-patch branch1..branch2
gitcmd := []string{"git", "format-patch", "-o", repodir, develn + ".." + usern}
log.Info("Run:", gitcmd)
err, output := repo.status.RunCmd(gitcmd)
log.Info("output =", output)
if err == nil {
log.Info("patches made okay for:", repo.String())
continue
}
log.Info("patches failed for:", repo.String())
return false
}
return true
}