submit-patchset/doCheckout.go

265 lines
6.4 KiB
Go

package main
import (
"fmt"
"time"
"go.wit.com/lib/gui/shell"
"go.wit.com/lib/protobuf/gitpb"
"go.wit.com/log"
)
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() (int, int, int, error) {
var total int
var count int
var nope 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.GetMasterBranchName() == repo.GetCurrentBranchName() {
count += 1
} else {
nope += 1
}
}
if total != count {
// log.Info(ErrorNotAllReposOnMaster)
return total, count, nope, ErrorNotAllReposOnMaster
}
return total, count, nope, nil
}
func IsEverythingOnDevel() (int, int, int, error) {
var total int
var count int
var nope 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.GetDevelBranchName() == repo.GetCurrentBranchName() {
count += 1
} else {
nope += 1
}
}
if total != count {
return total, count, nope, ErrorNotAllReposOnDevel
}
return total, count, nope, nil
}
func IsEverythingOnUser() (int, int, int, error) {
var total int
var count int
var nope 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.GetCurrentBranchName() == repo.GetUserBranchName() {
count += 1
} else {
nope += 1
}
}
if total != count {
return total, count, nope, ErrorNotAllReposOnUser
}
return total, count, nope, nil
}
func doGitReset() {
all := me.found.SortByFullPath()
for all.Scan() {
repo := all.Next()
if me.forge.Config.IsReadOnly(repo.GetGoPath()) {
// log.Info("is readonly", repo.GetGoPath())
if repo.CheckDirty() {
log.Info("is readonly and dirty", repo.GetGoPath())
cmd := []string{"git", "reset", "--hard"}
repo.RunRealtime(cmd)
}
} else {
// log.Info("is not readonly", repo.GetGoPath())
}
}
}
func rillCheckoutUser(repo *gitpb.Repo) error {
if repo.IsDirty() {
// never do dirty repos
return nil
}
if repo.GetCurrentBranchName() == repo.GetUserBranchName() {
// repo is already on user branch
return nil
}
if err := repo.CheckoutUser(); err != nil {
log.Info(repo.GetFullPath(), err)
return err
}
return nil
}
// trys to figure out if there is still something to update
func doAllCheckoutUser() error {
now := time.Now()
me.forge.RillFuncError(rillCheckoutUser)
count := me.forge.RillReload()
if count != 0 {
me.forge.ConfigSave()
}
total, count, nope, err := IsEverythingOnUser()
log.Printf("User branch check. %d total repos. (%d ok) (%d not on user branch) (%s)\n", total, count, nope, shell.FormatDuration(time.Since(now)))
if err != nil {
// display all repos not on user
me.found = new(gitpb.Repos)
all := me.forge.Repos.SortByFullPath()
for all.Scan() {
repo := all.Next()
if repo.GetCurrentBranchName() != repo.GetUserBranchName() {
me.found.AppendByGoPath(repo)
}
}
me.forge.PrintHumanTable(me.found)
log.Printf("There are %d repos that are NOT on the user branch\n", me.found.Len())
return err
}
return nil
}
func rillCheckoutDevel(repo *gitpb.Repo) error {
if repo.IsDirty() {
// never do dirty repos
return nil
}
if repo.GetCurrentBranchName() == repo.GetDevelBranchName() {
// repo is already on devel branch
return nil
}
repo.CheckoutDevel()
return nil
}
// is every repo on the devel branch?
func doAllCheckoutDevel() error {
now := time.Now()
me.forge.RillFuncError(rillCheckoutDevel)
count := me.forge.RillReload()
if count != 0 {
me.forge.ConfigSave()
}
total, count, nope, err := IsEverythingOnDevel()
log.Printf("Devel branch check. %d total repos. (%d ok) (%d not on user branch) (%s)\n", total, count, nope, shell.FormatDuration(time.Since(now)))
if err != nil {
// display all repos not on user
me.found = new(gitpb.Repos)
all := me.forge.Repos.SortByFullPath()
for all.Scan() {
repo := all.Next()
if repo.GetCurrentBranchName() != repo.GetDevelBranchName() {
me.found.AppendByGoPath(repo)
}
}
me.forge.PrintHumanTable(me.found)
log.Printf("There are %d repos that are NOT on the devel branch\n", me.found.Len())
return err
}
return nil
}
func rillCheckoutMaster(repo *gitpb.Repo) error {
if repo.IsDirty() {
// never do dirty repos
return nil
}
if repo.GetCurrentBranchName() == repo.GetMasterBranchName() {
// repo is already on master
return nil
}
if repo.GetUserVersion() == "uerr" {
repo.CheckoutMaster()
return nil
}
if me.forge.Config.IsReadOnly(repo.GetGoPath()) {
// skip other checks for readonly repos
repo.CheckoutMaster()
return nil
}
/*
if repo.GetUserVersion() != repo.GetDevelVersion() {
// don't switch branches if the user branch has uncommitted patches
return nil
}
if repo.GetDevelVersion() != repo.GetMasterVersion() {
// don't switch braches if the devel branch does not match master (needs merge)
return nil
}
*/
repo.CheckoutMaster()
return nil
}
// trys to figure out if there is still something to update
func doAllCheckoutMaster() error {
now := time.Now()
me.forge.RillFuncError(rillCheckoutMaster)
count := me.forge.RillReload()
if count != 0 {
me.forge.ConfigSave()
}
total, count, nope, err := IsEverythingOnMaster()
log.Printf("Master branch check. %d total repos. (%d ok) (%d not on master branch) (%s)\n", total, count, nope, shell.FormatDuration(time.Since(now)))
if 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 {
doAllCheckoutUser()
okExit("")
}
if argv.Checkout.Devel != nil {
doAllCheckoutDevel()
okExit("")
}
if argv.Checkout.Master != nil {
doAllCheckoutMaster()
okExit("")
}
return nil
}