From e2ccaa4b9bc445483541f939dc3e1cd017aaed8b Mon Sep 17 00:00:00 2001 From: Patrick Organ Date: Thu, 14 Nov 2024 22:00:32 -0500 Subject: [PATCH] add checkout command --- app/version.go | 2 +- cmd/checkout.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 cmd/checkout.go diff --git a/app/version.go b/app/version.go index c3ec4ef..1a4aed6 100644 --- a/app/version.go +++ b/app/version.go @@ -1,3 +1,3 @@ package main -var Version = "0.4.1-dev.6" \ No newline at end of file +var Version = "0.4.1-dev.10" \ No newline at end of file diff --git a/cmd/checkout.go b/cmd/checkout.go new file mode 100644 index 0000000..3561207 --- /dev/null +++ b/cmd/checkout.go @@ -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") +}