diff --git a/checkout.go b/checkout.go index 8e489a8..2b0e3f2 100644 --- a/checkout.go +++ b/checkout.go @@ -1,6 +1,11 @@ package gitpb -import "go.wit.com/log" +import ( + "fmt" + "path/filepath" + + "go.wit.com/log" +) func (repo *Repo) CheckoutMaster() bool { bName := repo.GetMasterBranchName() @@ -20,13 +25,14 @@ func (repo *Repo) CheckoutDevel() bool { return false } -func (repo *Repo) CheckoutUser() bool { +func (repo *Repo) CheckoutUser() error { bName := repo.GetUserBranchName() - if repo.checkoutBranch(bName) { - repo.UserBranchName = bName - return true + // 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) } - return false + return err } func (repo *Repo) BranchExists(bName string) bool { @@ -61,3 +67,53 @@ func (repo *Repo) checkoutBranch(bName string) bool { } return true } + +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 +} diff --git a/currentVersions.go b/currentVersions.go index db2ebfa..7f64335 100644 --- a/currentVersions.go +++ b/currentVersions.go @@ -55,6 +55,11 @@ func (repo *Repo) setDevelVersion() { func (repo *Repo) setUserVersion() { bname := repo.GetUserBranchName() + if !repo.Exists(filepath.Join(".git/refs/heads", bname)) { + // the user branch does not exist at this time + repo.UserVersion = "uerr" + return + } v, err := repo.gitVersionByName(bname) if err == nil { repo.UserVersion = v