package gitpb import ( "fmt" "os" "os/user" "path/filepath" "go.wit.com/log" ) func (repo *Repo) CheckoutMaster() bool { bName := repo.GetMasterBranchName() if bName == "giterr" { cmd := []string{"git", "checkout", "main"} // todo: figure out main repo.RunVerboseOnError(cmd) os.Exit(-1) // TODO: try to fix this if repo.checkoutBranch("main") { repo.MasterBranchName = "main" return true } else { cmd := []string{"git", "checkout", "main"} // todo: figure out main repo.RunVerboseOnError(cmd) return false } } if repo.checkoutBranch(bName) { return true } return false } func (repo *Repo) CheckoutDevel() bool { bName := repo.GetDevelBranchName() if repo.checkoutBranch(bName) { repo.DevelBranchName = bName return true // switch ok } return false } func (repo *Repo) CheckoutUser() error { bName := repo.GetUserBranchName() if bName == "uerr" { usr, _ := user.Current() repo.SetUserBranchName(usr.Username) bName = usr.Username log.Info("gitpb CheckoutUser() somehow got user 'uerr'") } return repo.createUserBranch(bName) /* if err != nil { log.Info("attempting checkout user error", repo.GetGoPath(), bName, err) } 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() { log.Log(INFO, repo.GetFullPath(), "is dirty") return false } cmd := []string{"git", "checkout", bName} r := repo.Run(cmd) if r.Error != nil { log.Log(INFO, "git checkout error:", r.Error) } realname := repo.GetCurrentBranchName() realversion := repo.GetCurrentBranchVersion() log.Log(INFO, repo.GetFullPath(), "realname =", realname, "realversion =", realversion) if realname != bName { log.Log(INFO, "git checkout failed", repo.GetFullPath(), bName, "!=", realname) return false } return true } // actually creates a local user branch func (repo *Repo) createUserBranch(branch string) error { if branch == "" { // get the username here? return fmt.Errorf("gitpb createuserBranch() logic err. git branch name can not be blank") } if repo.IsDirty() { // never change repos on dirty branches return fmt.Errorf("repo is dirty") } 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() { repo.CheckoutDevel() } repo.Reload() if repo.GetCurrentBranchName() != repo.GetDevelBranchName() { log.Info("create user branch will probably fail", repo.GetGoPath()) // TODO: FIX THIS // return fmt.Errorf("repo must be on devel branch %s", 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 }