diff options
| -rw-r--r-- | checkout.go | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/checkout.go b/checkout.go index b71fd16..491644e 100644 --- a/checkout.go +++ b/checkout.go @@ -1,6 +1,7 @@ package gitpb import ( + "errors" "fmt" "os/user" "path/filepath" @@ -16,6 +17,42 @@ func (repo *Repo) CheckoutMasterError() error { return nil } +// if you manage to leave yourself on a branch +// that has been deleted, git porcelain will report +// the repo as having changes (aka 'dirty') +// then this code won't let you switch branches +// this bypasses those checks and moves you +// to a working branch. 1st 'devel', then 'master' +func (r *Repo) CheckoutForce() error { + brName := r.GetDevelBranchName() + if r.IsLocalBranch(brName) { + if err := r.checkoutBranchErrorUnsafe(brName); err == nil { + return nil + } + } + if r.IsRemoteBranch(brName) { + // ya, it could run this twice. whatever + if err := r.checkoutBranchErrorUnsafe(brName); err == nil { + return nil + } + } + brName = r.GetMasterBranchName() + if r.IsLocalBranch(brName) { + if err := r.checkoutBranchErrorUnsafe(brName); err == nil { + return nil + } + } + if r.IsRemoteBranch(brName) { + // things are jacked + if err := r.checkoutBranchErrorUnsafe(brName); err == nil { + return nil + } else { + return err + } + } + return errors.New("no working devel or master branches todo: find one here") +} + // deprecate this func (repo *Repo) CheckoutMaster() bool { bName := repo.GetMasterBranchName() @@ -101,6 +138,20 @@ func (repo *Repo) checkoutBranchError(bName string) error { return nil } +func (repo *Repo) checkoutBranchErrorUnsafe(brName string) error { + cmd := []string{"git", "checkout", brName} + r := repo.Run(cmd) + if r.Exit != 0 { + log.Info("git checkout exit not zero", r.Exit) + return errors.New("git checkout returned -1") + } + if r.Error != nil { + log.Info("git checkout error:", r.Error) + return r.Error + } + return nil +} + func (repo *Repo) checkoutBranch(bName string) bool { if !repo.BranchExists(bName) { return false |
