gitpb/checkout.go

120 lines
2.8 KiB
Go
Raw Normal View History

package gitpb
2025-01-19 08:48:43 -06:00
import (
"fmt"
"path/filepath"
"go.wit.com/log"
)
func (repo *Repo) CheckoutMaster() bool {
bName := repo.GetMasterBranchName()
if repo.checkoutBranch(bName) {
return true
}
return false
}
func (repo *Repo) CheckoutDevel() bool {
bName := repo.GetDevelBranchName()
if repo.checkoutBranch(bName) {
2025-01-08 10:11:49 -06:00
repo.DevelBranchName = bName
return true
// switch ok
}
return false
}
2025-01-19 08:48:43 -06:00
func (repo *Repo) CheckoutUser() error {
bName := repo.GetUserBranchName()
2025-01-19 08:48:43 -06:00
// log.Info("attempting checkout user", repo.GetGoPath(), bName)
err := repo.checkoutBranchNew(bName)
if err != nil {
log.Info("attempting checkout user error", repo.GetGoPath(), bName, err)
}
2025-01-19 08:48:43 -06:00
return err
}
func (repo *Repo) BranchExists(bName string) bool {
// fixme after move to protobuf
return true
}
func (repo *Repo) checkoutBranch(bName string) bool {
if !repo.BranchExists(bName) {
return false
}
if bName == "" {
return false
}
if repo.CheckDirty() {
2025-01-08 03:13:09 -06:00
log.Log(INFO, repo.GetFullPath(), "is dirty")
return false
}
cmd := []string{"git", "checkout", bName}
r := repo.Run(cmd)
if r.Error != nil {
2025-01-08 03:13:09 -06:00
log.Log(INFO, "git checkout error:", r.Error)
}
realname := repo.GetCurrentBranchName()
realversion := repo.GetCurrentBranchVersion()
2025-01-08 03:13:09 -06:00
log.Log(INFO, repo.GetFullPath(), "realname =", realname, "realversion =", realversion)
if realname != bName {
2025-01-08 03:13:09 -06:00
log.Log(INFO, "git checkout failed", repo.GetFullPath(), bName, "!=", realname)
return false
}
return true
}
2025-01-19 08:48:43 -06:00
func (repo *Repo) checkoutBranchNew(branch string) error {
if branch == "" || branch == "uerr" {
log.Info("forge.gitpb logic err. branch name was:", branch)
return nil
}
if repo.IsDirty() {
// never change repos on dirty branches
return nil
}
// log.Info("forge.gitpb look for branch name was:", branch, repo.GetGoPath())
if repo.Exists(filepath.Join(".git/refs/heads", branch)) {
var err error
// there is already a local user branch
cmd := []string{"git", "checkout", branch}
if _, err = repo.RunVerboseOnError(cmd); err == nil {
return nil
}
log.Log(INFO, "git checkout error:", err)
}
if repo.Exists(filepath.Join(".git/refs/remote/origin", branch)) {
var err error
// there is a remote user branch
// todo: check other remotes
cmd := []string{"git", "checkout", branch}
if _, err = repo.RunVerboseOnError(cmd); err == nil {
return nil
}
log.Log(INFO, "git checkout error:", err)
}
if repo.GetCurrentBranchName() != repo.GetDevelBranchName() {
return fmt.Errorf("repo must be on devel branch %s", repo.GetGoPath())
}
// log.Info("forge.gitpb try to create", branch, repo.GetGoPath())
// create the branch from devel
cmd := []string{"git", "branch", branch}
if _, err := repo.RunVerboseOnError(cmd); err != nil {
return err
}
cmd = []string{"git", "checkout", branch}
if _, err := repo.RunVerboseOnError(cmd); err != nil {
return err
}
return nil
}