Skip to content

Commit

Permalink
add checkout command
Browse files Browse the repository at this point in the history
  • Loading branch information
patinthehat committed Nov 15, 2024
1 parent 83cbf11 commit e2ccaa4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package main

var Version = "0.4.1-dev.6"
var Version = "0.4.1-dev.10"
44 changes: 44 additions & 0 deletions cmd/checkout.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cmd

import (
"fmt"

"github.com/permafrost-dev/git-ninja/app/helpers"
"github.com/spf13/cobra"
)

func init() {
var flagAutoPull bool = false

var checkoutCmd = &cobra.Command{
Use: "checkout",
Aliases: []string{"co"},
Short: "Checks out the specified branch",
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
fmt.Println("error: branch name required")
return
}

if err := helpers.RunCommandOnStdout("git", "checkout", args[0]); err != nil {
return
}

if flagAutoPull {
currentBranch, _ := helpers.GetCurrentBranchName()

if currentBranch != args[0] {
fmt.Println("error: failed to switch branches")
return
}

if err := helpers.RunCommandOnStdout("git", "pull", "origin", args[0], "--rebase"); err != nil {
return
}
}
},
}

rootCmd.AddCommand(checkoutCmd)
checkoutCmd.Flags().BoolVarP(&flagAutoPull, "pull", "p", false, "Automatically pull origin after checkout")
}

0 comments on commit e2ccaa4

Please sign in to comment.