Skip to content

Commit

Permalink
Merge pull request #12 from masonkmeyer/update-go-git
Browse files Browse the repository at this point in the history
use exec to checkout branch
  • Loading branch information
masonkmeyer authored Nov 22, 2024
2 parents b2d75c0 + 9f1813e commit 4b138d3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 12 additions & 8 deletions jet/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package jet

import (
"fmt"
"os/exec"
"sort"
"strings"
"time"

"github.com/go-git/go-git/v5"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

0 comments on commit 4b138d3

Please sign in to comment.