From 4bc7060538e11103c6e01209501d66373e340386 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Fri, 10 Oct 2025 04:38:24 -0500 Subject: something to recover from nonexistant branches --- checkout.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) 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 -- cgit v1.2.3