From 39f72d20344c5bbe05b83a13eee9c0d7b54f7e78 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sat, 18 Jan 2025 23:25:55 -0600 Subject: [PATCH] better git checkout --- argv.go | 1 + doCheckout.go | 196 ++++++++++++++++++++++++++++++++++++----------- doClean.go | 26 ++++++- doDirty.go | 2 +- doExamine.go | 6 +- exit.go | 6 ++ find.go | 15 ++++ main.go | 35 ++------- windowMain.go | 22 ++---- windowPatches.go | 2 +- 10 files changed, 219 insertions(+), 92 deletions(-) diff --git a/argv.go b/argv.go index 0bd593e..6856a6e 100644 --- a/argv.go +++ b/argv.go @@ -92,6 +92,7 @@ type FindCmd struct { Favorites bool `arg:"--favorites" help:"your repos configured as favorites"` Private bool `arg:"--private" help:"your private repos from your .config/forge/"` Dirty bool `arg:"--dirty" help:"only use dirty git repos"` + User bool `arg:"--user" help:"show repos on the user branch"` // ReadOnly bool `arg:"--readonly" help:"include read-only repos"` } diff --git a/doCheckout.go b/doCheckout.go index a314d25..4f9aeda 100644 --- a/doCheckout.go +++ b/doCheckout.go @@ -1,41 +1,76 @@ package main import ( + "fmt" + "go.wit.com/lib/protobuf/forgepb" "go.wit.com/lib/protobuf/gitpb" "go.wit.com/log" ) -func IsEverythingOnDevel() bool { - me.found = new(gitpb.Repos) - all := me.forge.Repos.SortByFullPath() +var ErrorNotAllReposOnMaster error = fmt.Errorf("not all repos on are on the master branch") +var ErrorNotAllReposOnDevel error = fmt.Errorf("not all repos on are on the devel branch") +var ErrorNotAllReposOnUser error = fmt.Errorf("not all repos on are on the user branch") + +func IsEverythingOnMaster() error { + var total int + var count int + + // first make sure every repo is on the master branch + all := me.forge.Repos.All() for all.Scan() { repo := all.Next() - if repo.GetCurrentBranchName() != repo.GetDevelBranchName() { - // log.Info(repo.GetFullPath(), repo.GetCurrentBranchName(), repo.GetDevelBranchName()) - // add this to the list of "found" repos - me.found.AppendByGoPath(repo) + total += 1 + if repo.GetMasterBranchName() == repo.GetCurrentBranchName() { + count += 1 } } - if len(me.found.Repos) == 0 { - return true + log.Printf("Master branch check. %d total repos. %d repos on the master branch\n", total, count) + if total != count { + // log.Info(ErrorNotAllReposOnMaster) + return ErrorNotAllReposOnMaster } - return false + return nil } -func IsEverythingOnUser() bool { - me.found = new(gitpb.Repos) - all := me.forge.Repos.SortByFullPath() +func IsEverythingOnDevel() error { + var total int + var count int + + // first make sure every repo is on the master branch + all := me.forge.Repos.All() for all.Scan() { repo := all.Next() - if repo.GetCurrentBranchName() != repo.GetUserBranchName() { - me.found.AppendByGoPath(repo) + total += 1 + if repo.GetDevelBranchName() == repo.GetCurrentBranchName() { + count += 1 } } - if len(me.found.Repos) == 0 { - return true + log.Printf("Devel branch check. %d total repos. %d repos on the devel branch\n", total, count) + if total != count { + return ErrorNotAllReposOnDevel } - return false + return nil +} + +func IsEverythingOnUser() error { + var total int + var count int + + // first make sure every repo is on the master branch + all := me.forge.Repos.All() + for all.Scan() { + repo := all.Next() + total += 1 + if repo.GetUserBranchName() == repo.GetUserBranchName() { + count += 1 + } + } + log.Printf("User branch check. %d total repos. %d repos on the user branch\n", total, count) + if total != count { + return ErrorNotAllReposOnUser + } + return nil } func doGitReset() { @@ -55,6 +90,7 @@ func doGitReset() { } } +/* func checkoutBranches(repo *gitpb.Repo) error { dname := repo.GetDevelBranchName() if dname == "" { @@ -73,34 +109,108 @@ func checkoutBranches(repo *gitpb.Repo) error { } return nil } +*/ -func doAllCheckoutDevel() bool { - me.forge.CheckoutDevel() - me.forge = forgepb.Init() - if !IsEverythingOnDevel() { - log.Info("switching to devel branch failed") - me.forge.PrintHumanTable(me.found) - badExit(nil) - return false - } - return true -} - -func doAllCheckoutUser() bool { +func doAllCheckoutUser() error { me.forge.CheckoutUser() - me.forge = forgepb.Init() - if !IsEverythingOnUser() { - log.Info("switching to user branch failed") - me.forge.PrintHumanTable(me.found) - return false + // me.forge = forgepb.Init() + count := me.forge.RillReload() + log.Info("CHECKOUT USER COUNT", count) + if count != 0 { + me.forge.ConfigSave() } - return true + if err := IsEverythingOnUser(); err != nil { + return err + } + return nil } -func doCheckoutMaster() { - me.forge.CheckoutMaster() - me.forge = forgepb.Init() - me.found = new(gitpb.Repos) - argv.Checkout.Master.findRepos() - me.forge.PrintHumanTable(me.found) +func doAllCheckoutDevel() error { + me.forge.CheckoutDevel() + // me.forge = forgepb.Init() + count := me.forge.RillReload() + log.Info("CHECKOUT DEVEL COUNT", count) + if count != 0 { + me.forge.ConfigSave() + } + if err := IsEverythingOnDevel(); err != nil { + return err + } + return nil +} + +func rillCheckoutMaster(repo *gitpb.Repo) error { + if repo.GetUserVersion() != repo.GetDevelVersion() { + // don't switch braches if the user branch has uncommitted patches + return nil + } + if repo.GetCurrentBranchName() == repo.GetMasterBranchName() { + // repo is already on master + return nil + } + repo.CheckoutMaster() + return nil +} + +// trys to figure out if there is still something to update +// todo: redo this logic as it is terrible + +func doAllCheckoutMaster() error { + me.forge.RillFuncError(rillCheckoutMaster) + count := me.forge.RillReload() + log.Info("CHECKOUT MASTER COUNT", count) + if count != 0 { + me.forge.ConfigSave() + } + + if err := IsEverythingOnMaster(); err != nil { + // display all repos not on master + me.found = new(gitpb.Repos) + all := me.forge.Repos.SortByFullPath() + for all.Scan() { + repo := all.Next() + if repo.GetCurrentBranchName() != repo.GetMasterBranchName() { + me.found.AppendByGoPath(repo) + } + } + me.forge.PrintHumanTable(me.found) + log.Printf("There are %d repos that are NOT on the master branch\n", me.found.Len()) + return err + } + return nil +} + +// trys to figure out if there is still something to update +// todo: redo this logic as it is terrible + +func doCheckout() error { + if argv.Checkout.User != nil { + if argv.Force { + me.forge.CheckoutUserForce() + } else { + me.forge.CheckoutUser() + } + me.forge = forgepb.Init() + me.found = new(gitpb.Repos) + argv.Checkout.User.findRepos() + me.forge.PrintHumanTable(me.found) + if err := IsEverythingOnUser(); err != nil { + badExit(err) + } + okExit("") + } + + if argv.Checkout.Devel != nil { + me.forge.CheckoutDevel() + me.forge = forgepb.Init() + me.found = new(gitpb.Repos) + argv.Checkout.Devel.findRepos() + me.forge.PrintHumanTable(me.found) + okExit("") + } + + if argv.Checkout.Master != nil { + doAllCheckoutMaster() + } + return nil } diff --git a/doClean.go b/doClean.go index 11e4b7e..bc665a6 100644 --- a/doClean.go +++ b/doClean.go @@ -7,18 +7,31 @@ import ( "go.wit.com/log" ) +var ErrorReposHasLocalBranches error = fmt.Errorf("repo still has local branches") + func doClean() error { + if err := IsEverythingOnMaster(); err != nil { + log.Info("Not all repos are on the master branch") + // return err + } + all := me.forge.Repos.SortByFullPath() for all.Scan() { repo := all.Next() + if repo.GetCurrentBranchName() != repo.GetMasterBranchName() { + continue + } if err := doCleanRepo(repo); err != nil { - badExit(err) + badRepoExit(repo, err) } } + log.Info("All repos on the master branch are clean") return nil } +// removes all local branches func doCleanRepo(repo *gitpb.Repo) error { + var hasLocal bool log.Info("Cleaning:", repo.GetGoPath()) if repo.GitConfig == nil { return fmt.Errorf("GitConfig == nil") @@ -30,6 +43,17 @@ func doCleanRepo(repo *gitpb.Repo) error { for name, b := range repo.GitConfig.Branches { log.Info("\tlocal branch name:", name, b.Merge, b.Remote) + if name == repo.GetMasterBranchName() { + continue + } + hasLocal = true + } + if hasLocal { + return ErrorReposHasLocalBranches } return nil } + +func verifyLocalBranchIsMerged(repo *gitpb.Repo, branch *gitpb.GitBranch) error { + return nil +} diff --git a/doDirty.go b/doDirty.go index f81d176..296d852 100644 --- a/doDirty.go +++ b/doDirty.go @@ -49,7 +49,7 @@ func doCheckDirtyAndConfigSave() { */ // this might work? now := time.Now() - me.forge.RillFuncError(20, 10, doCheckDirty) + me.forge.RillFuncError(doCheckDirty) end := straightCheckDirty() log.Printf("dirty check (%d dirty repos) (%d total repos) took:%s\n", end, me.found.Len(), shell.FormatDuration(time.Since(now))) diff --git a/doExamine.go b/doExamine.go index 8cac9d8..8a4ca75 100644 --- a/doExamine.go +++ b/doExamine.go @@ -12,7 +12,7 @@ import ( "go.wit.com/log" ) -func doExamine() bool { +func doExamine() error { me.found = new(gitpb.Repos) all := me.forge.Repos.SortByFullPath() for all.Scan() { @@ -28,7 +28,7 @@ func doExamine() bool { } } if len(me.found.Repos) == 0 { - return true + return nil } // slices.Reverse(me.found.Repos) slices.SortFunc(me.found.Repos, func(a, b *gitpb.Repo) int { @@ -63,7 +63,7 @@ func doExamine() bool { me.found.AppendByGoPath(repo) } me.forge.PrintHumanTableDirty(me.found) - return false + return nil } func isNormal(repo *gitpb.Repo) bool { diff --git a/exit.go b/exit.go index e80df8a..fac574b 100644 --- a/exit.go +++ b/exit.go @@ -3,6 +3,7 @@ package main import ( "os" + "go.wit.com/lib/protobuf/gitpb" "go.wit.com/log" ) @@ -21,3 +22,8 @@ func badExit(err error) { log.Info("forge failed: ", err, me.forge.GetGoSrc()) os.Exit(-1) } + +func badRepoExit(repo *gitpb.Repo, err error) { + log.Printf("forge failed on %s with %v", repo.GetGoPath(), err) + os.Exit(-1) +} diff --git a/find.go b/find.go index 905176b..2c21e85 100644 --- a/find.go +++ b/find.go @@ -42,6 +42,11 @@ func (f *FindCmd) findRepos() { return } + if f.User { + findUser() + return + } + findAll() } @@ -98,3 +103,13 @@ func findAll() { me.found.AppendByGoPath(repo) } } + +func findUser() { + all := me.forge.Repos.SortByFullPath() + for all.Scan() { + repo := all.Next() + if repo.GetCurrentBranchName() == repo.GetUserBranchName() { + me.found.AppendByGoPath(repo) + } + } +} diff --git a/main.go b/main.go index 395a318..53a96a7 100644 --- a/main.go +++ b/main.go @@ -88,37 +88,16 @@ func main() { } if argv.Checkout != nil { - if argv.Checkout.User != nil { - if argv.Force { - me.forge.CheckoutUserForce() - } else { - me.forge.CheckoutUser() - } - me.forge = forgepb.Init() - me.found = new(gitpb.Repos) - argv.Checkout.User.findRepos() - me.forge.PrintHumanTable(me.found) - okExit("") + if err := doCheckout(); err != nil { + badExit(err) } - - if argv.Checkout.Devel != nil { - me.forge.CheckoutDevel() - me.forge = forgepb.Init() - me.found = new(gitpb.Repos) - argv.Checkout.Devel.findRepos() - me.forge.PrintHumanTable(me.found) - okExit("") - } - - if argv.Checkout.Master != nil { - doCheckoutMaster() - } - log.Info("make 'user' the default here?") okExit("") } if argv.Clean != nil { - doClean() + if err := doClean(); err != nil { + badExit(err) + } okExit("") } @@ -128,7 +107,9 @@ func main() { } if argv.Examine != nil { - doExamine() + if err := doExamine(); err != nil { + badExit(err) + } okExit("") } diff --git a/windowMain.go b/windowMain.go index c17f766..814287a 100644 --- a/windowMain.go +++ b/windowMain.go @@ -85,31 +85,21 @@ func globalBuildOptions(vbox *gui.Node) { targetName := me.newBranch.String() log.Warn("setting all branches to", targetName) if targetName == "devel" { - if !doAllCheckoutDevel() { - okExit("") + if err := doAllCheckoutDevel(); err != nil { + log.Info("switching to devel branches failed") } return } if targetName == "master" { - log.Info("Don't know how to set", targetName, "yet") + if err := doAllCheckoutMaster(); err != nil { + log.Info("switching to master branches failed") + } return } // just assume user - if targetName == "jcarr" { - if doAllCheckoutUser() { - return - } + if err := doAllCheckoutUser(); err != nil { log.Info("switching to user branches failed") - return } - /* - loop := me.repos.View.ReposSortByName() - for loop.Scan() { - repo := loop.Repo() - repo.Status.CheckoutBranch(targetName) - repo.Scan() - } - */ }) me.newBranch = grid.NewCombobox() me.newBranch.AddText("master") diff --git a/windowPatches.go b/windowPatches.go index b137454..916ab66 100644 --- a/windowPatches.go +++ b/windowPatches.go @@ -104,7 +104,7 @@ func submitPatchesBox(box *gui.Node) *patchSummary { if err != nil { return } - if !IsEverythingOnDevel() { + if err := IsEverythingOnDevel(); err != nil { log.Info("You can only apply patches to the devel branch") return }