From 9f1813e207d4b9e43421e6881a8a892060eb4f7f Mon Sep 17 00:00:00 2001 From: Mason Meyer Date: Thu, 21 Nov 2024 19:18:51 -0700 Subject: [PATCH] use exec to checkout branch --- go.mod | 2 +- jet/git.go | 20 ++++++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/go.mod b/go.mod index 1fe8af6..f6d19cd 100644 --- a/go.mod +++ b/go.mod @@ -51,7 +51,7 @@ require ( require ( github.com/fatih/color v1.18.0 - github.com/go-git/go-git/v5 v5.5.2 + github.com/go-git/go-git/v5 v5.12.0 github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/jroimartin/gocui v0.5.0 github.com/spf13/pflag v1.0.5 // indirect diff --git a/jet/git.go b/jet/git.go index 8c698c7..c8124ad 100644 --- a/jet/git.go +++ b/jet/git.go @@ -2,7 +2,9 @@ package jet import ( "fmt" + "os/exec" "sort" + "strings" "time" "github.com/go-git/go-git/v5" @@ -62,18 +64,13 @@ func (g Git) IsClean() bool { // Checkout executes a git checkout command with the branch name // and returns the output and the command that was executed func (g Git) Checkout(branchName string) error { - w, err := g.repo.Worktree() - + result, _, err := g.exec("checkout", branchName) if err != nil { - return err + return fmt.Errorf("error checking out branch: %s", result) } - branchRefName := plumbing.NewBranchReferenceName(branchName) - err = w.Checkout(&git.CheckoutOptions{ - Branch: plumbing.ReferenceName(branchRefName), - }) + return nil - return err } // ListBranches executes a git branch command with --list and any other provided args @@ -130,3 +127,10 @@ func (g Git) Logs(branchName string, n int) []Commit { return results } + +// exec executes a git command with the provided args +// it returns the output of the command and the command that was executed +func (g Git) exec(args ...string) (string, string, error) { + out, err := exec.Command("git", args...).CombinedOutput() + return string(out), fmt.Sprintf("git %s", strings.Join(args, " ")), err +}