forge/doCheckout.go

262 lines
6.3 KiB
Go
Raw Normal View History

package main
import (
2025-01-18 23:25:55 -06:00
"fmt"
2025-01-20 01:39:59 -06:00
"time"
2025-01-18 23:25:55 -06:00
2025-01-20 01:39:59 -06:00
"go.wit.com/lib/gui/shell"
"go.wit.com/lib/protobuf/gitpb"
"go.wit.com/log"
)
2025-01-18 23:25:55 -06:00
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")
2025-01-20 01:39:59 -06:00
func IsEverythingOnMaster() (int, int, int, error) {
2025-01-18 23:25:55 -06:00
var total int
var count int
2025-01-19 07:05:45 -06:00
var nope int
2025-01-18 23:25:55 -06:00
// 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
2025-01-19 07:05:45 -06:00
} else {
nope += 1
2025-01-18 23:25:55 -06:00
}
}
if total != count {
// log.Info(ErrorNotAllReposOnMaster)
2025-01-20 01:39:59 -06:00
return total, count, nope, ErrorNotAllReposOnMaster
2025-01-18 23:25:55 -06:00
}
2025-01-20 01:39:59 -06:00
return total, count, nope, nil
2025-01-18 23:25:55 -06:00
}
2025-01-20 07:58:13 -06:00
func IsEverythingOnDevel() (int, int, int, error) {
2025-01-18 23:25:55 -06:00
var total int
var count int
2025-01-20 07:58:13 -06:00
var nope int
2025-01-18 23:25:55 -06:00
// first make sure every repo is on the master branch
all := me.forge.Repos.All()
for all.Scan() {
repo := all.Next()
2025-01-18 23:25:55 -06:00
total += 1
if repo.GetDevelBranchName() == repo.GetCurrentBranchName() {
count += 1
2025-01-20 07:58:13 -06:00
} else {
nope += 1
}
}
2025-01-18 23:25:55 -06:00
if total != count {
2025-01-20 07:58:13 -06:00
return total, count, nope, ErrorNotAllReposOnDevel
}
2025-01-20 07:58:13 -06:00
return total, count, nope, nil
}
2025-01-20 07:58:13 -06:00
func IsEverythingOnUser() (int, int, int, error) {
2025-01-18 23:25:55 -06:00
var total int
var count int
2025-01-20 07:58:13 -06:00
var nope int
2025-01-18 23:25:55 -06:00
// first make sure every repo is on the master branch
all := me.forge.Repos.All()
for all.Scan() {
repo := all.Next()
2025-01-18 23:25:55 -06:00
total += 1
2025-01-19 04:31:38 -06:00
if repo.GetCurrentBranchName() == repo.GetUserBranchName() {
2025-01-18 23:25:55 -06:00
count += 1
2025-01-20 07:58:13 -06:00
} else {
nope += 1
}
}
2025-01-18 23:25:55 -06:00
if total != count {
2025-01-20 07:58:13 -06:00
return total, count, nope, ErrorNotAllReposOnUser
}
2025-01-20 07:58:13 -06:00
return total, count, nope, nil
}
2024-12-13 17:13:07 -06:00
func doGitReset() {
2024-12-17 06:36:00 -06:00
all := me.found.SortByFullPath()
2024-12-13 17:13:07 -06:00
for all.Scan() {
repo := all.Next()
2024-12-17 06:36:00 -06:00
if me.forge.Config.IsReadOnly(repo.GetGoPath()) {
// log.Info("is readonly", repo.GetGoPath())
2024-12-13 17:13:07 -06:00
if repo.CheckDirty() {
2024-12-17 06:36:00 -06:00
log.Info("is readonly and dirty", repo.GetGoPath())
2024-12-13 17:13:07 -06:00
cmd := []string{"git", "reset", "--hard"}
repo.RunRealtime(cmd)
}
} else {
2024-12-17 06:36:00 -06:00
// log.Info("is not readonly", repo.GetGoPath())
2024-12-13 17:13:07 -06:00
}
}
}
2025-01-19 04:31:38 -06:00
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
}
2025-01-19 08:48:17 -06:00
if err := repo.CheckoutUser(); err != nil {
return err
}
2025-01-19 04:31:38 -06:00
return nil
}
2025-01-20 07:58:13 -06:00
// trys to figure out if there is still something to update
2025-01-18 23:25:55 -06:00
func doAllCheckoutUser() error {
2025-01-20 07:58:13 -06:00
now := time.Now()
2025-01-19 04:31:38 -06:00
me.forge.RillFuncError(rillCheckoutUser)
2025-01-18 23:25:55 -06:00
count := me.forge.RillReload()
if count != 0 {
me.forge.ConfigSave()
}
2025-01-20 07:58:13 -06:00
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 {
2025-01-19 04:31:38 -06:00
// 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())
2025-01-18 23:25:55 -06:00
return err
}
return nil
}
2025-01-19 07:05:45 -06:00
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
}
2025-01-20 07:58:13 -06:00
// is every repo on the devel branch?
2025-01-18 23:25:55 -06:00
func doAllCheckoutDevel() error {
2025-01-20 07:58:13 -06:00
now := time.Now()
2025-01-19 07:05:45 -06:00
me.forge.RillFuncError(rillCheckoutDevel)
2025-01-18 23:25:55 -06:00
count := me.forge.RillReload()
if count != 0 {
me.forge.ConfigSave()
}
2025-01-20 07:58:13 -06:00
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 {
2025-01-19 07:05:45 -06:00
// 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())
2025-01-18 23:25:55 -06:00
return err
}
return nil
}
func rillCheckoutMaster(repo *gitpb.Repo) error {
2025-01-19 03:45:10 -06:00
if repo.IsDirty() {
// never do dirty repos
return nil
}
if repo.GetCurrentBranchName() == repo.GetMasterBranchName() {
// repo is already on master
return nil
}
2025-01-19 07:24:18 -06:00
if repo.GetUserVersion() == "uerr" {
repo.CheckoutMaster()
return nil
}
2025-01-19 03:45:10 -06:00
if me.forge.Config.IsReadOnly(repo.GetGoPath()) {
// skip other checks for readonly repos
repo.CheckoutMaster()
2025-01-19 03:39:35 -06:00
return nil
}
2025-01-19 16:11:13 -06:00
if repo.GetUserVersion() != repo.GetDevelVersion() {
// don't switch branches if the user branch has uncommitted patches
return nil
}
2025-01-19 03:45:10 -06:00
if repo.GetDevelVersion() != repo.GetMasterVersion() {
// don't switch braches if the devel branch does not match master (needs merge)
2025-01-18 23:25:55 -06:00
return nil
}
repo.CheckoutMaster()
return nil
}
2025-01-18 23:25:55 -06:00
// trys to figure out if there is still something to update
func doAllCheckoutMaster() error {
2025-01-20 01:39:59 -06:00
now := time.Now()
2025-01-18 23:25:55 -06:00
me.forge.RillFuncError(rillCheckoutMaster)
count := me.forge.RillReload()
if count != 0 {
me.forge.ConfigSave()
}
2025-01-20 03:30:58 -06:00
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 {
2025-01-18 23:25:55 -06:00
// 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)
2025-01-18 23:25:55 -06:00
log.Printf("There are %d repos that are NOT on the master branch\n", me.found.Len())
return err
}
2025-01-18 23:25:55 -06:00
return nil
}
2025-01-18 07:47:34 -06:00
2025-01-18 23:25:55 -06:00
// 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 {
2025-01-19 04:31:38 -06:00
doAllCheckoutUser()
2025-01-18 23:25:55 -06:00
okExit("")
}
if argv.Checkout.Devel != nil {
2025-01-19 07:05:45 -06:00
doAllCheckoutDevel()
2025-01-18 23:25:55 -06:00
okExit("")
}
if argv.Checkout.Master != nil {
doAllCheckoutMaster()
2025-01-19 04:31:38 -06:00
okExit("")
2025-01-18 23:25:55 -06:00
}
return nil
2025-01-18 07:47:34 -06:00
}