diff options
| author | Jeff Carr <[email protected]> | 2025-10-10 04:38:24 -0500 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-10-10 04:38:24 -0500 |
| commit | 4bc7060538e11103c6e01209501d66373e340386 (patch) | |
| tree | 1a04eb519d01447f7573443d62518984ea03be72 | |
| parent | f2fe51ee0ae6c26f5cb3d5fb12001f0db87f7bd1 (diff) | |
something to recover from nonexistant branches
| -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 |
