From fe9abe49b3b9e9c325d8cb73728fdde119e8c79b Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 6 Dec 2014 18:16:28 -0500 Subject: git-checkout-history: Passing an arg checks out a branch If you pass in the index of a branch in the checkout-history branch list, the branch at that index in the history will be checked out. TODO: Store the newly checked out branch in the branch history --- git-checkout-history/main.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/git-checkout-history/main.go b/git-checkout-history/main.go index fa9747c..8bf6e24 100644 --- a/git-checkout-history/main.go +++ b/git-checkout-history/main.go @@ -4,7 +4,10 @@ import ( "fmt" "io/ioutil" "log" + "os" + "os/exec" "os/user" + "strconv" "gopkg.in/yaml.v2" ) @@ -37,7 +40,19 @@ func main() { log.Fatal(err) } - for i := 1; i < len(branchList.Branches); i++ { - fmt.Printf("[%d] %s\n", i, branchList.Branches[i]) + args := os.Args[1:] + + if len(args) > 0 { + branchIndex, _ := strconv.Atoi(args[0]) + cmd := exec.Command("git", "checkout", branchList.Branches[branchIndex]) + err := cmd.Run() + if err != nil { + fmt.Fprintf(os.Stderr, err.Error()) + } + } else { + // List branches in history + for i := 1; i < len(branchList.Branches); i++ { + fmt.Printf("[%d] %s\n", i, branchList.Branches[i]) + } } } -- cgit v1.2.3