From 3ee7aeecb5f8deb89d4a08cebfa5995f3328e141 Mon Sep 17 00:00:00 2001 From: Jingwen Owen Ou Date: Fri, 21 Jun 2013 13:17:52 -0700 Subject: [PATCH] Add git.Checkout --- commands/checkout.go | 22 ++++++++++++++++++++++ commands/commands.go | 2 +- commands/remote.go | 2 +- git/git.go | 35 +++++++++++++++++++++++------------ 4 files changed, 47 insertions(+), 14 deletions(-) diff --git a/commands/checkout.go b/commands/checkout.go index 3329884f..e7672e59 100644 --- a/commands/checkout.go +++ b/commands/checkout.go @@ -1,5 +1,10 @@ package commands +import ( + "github.com/jingweno/gh/git" + "github.com/jingweno/gh/utils" +) + var cmdCheckout = &Command{ Run: checkout, GitExtension: true, @@ -7,5 +12,22 @@ var cmdCheckout = &Command{ Short: "Switch the active branch to another branch", } +/** + $ gh checkout https://github.com/jingweno/gh/pull/73 + # > git remote add -f -t feature git://github:com/foo/gh.git + # > git checkout --track -B foo-feature foo/feature + + $ gh checkout https://github.com/jingweno/gh/pull/73 custom-branch-name +**/ func checkout(command *Command, args []string) { + if len(args) > 0 { + args = transformCheckoutArgs(args) + } + + err := git.ExecCheckout(args) + utils.Check(err) +} + +func transformCheckoutArgs(args []string) []string { + return nil } diff --git a/commands/commands.go b/commands/commands.go index 5d79ce8c..48a7fe2f 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -20,7 +20,7 @@ type Command struct { func (c *Command) PrintUsage() { if c.GitExtension { - err := git.Help(c.Name()) + err := git.ExecHelp(c.Name()) utils.Check(err) } else { if c.Runnable() { diff --git a/commands/remote.go b/commands/remote.go index a9fd169c..e12c5558 100644 --- a/commands/remote.go +++ b/commands/remote.go @@ -29,7 +29,7 @@ func remote(command *Command, args []string) { args = transformRemoteArgs(args) } - err := git.ExecRemote(args) + err := git.ExecRemote(args...) utils.Check(err) } diff --git a/git/git.go b/git/git.go index 7c00a649..17a287d1 100644 --- a/git/git.go +++ b/git/git.go @@ -109,14 +109,12 @@ func Remote() (string, error) { return "", errors.New("Can't find git remote (push)") } -func ExecRemote(args []string) error { - cmd := cmd.New("git") - cmd.WithArg("remote") - for _, i := range args { - cmd.WithArg(i) - } +func ExecRemote(args ...string) error { + cmdArgs := make([]string, 0) + cmdArgs = append(cmdArgs, "checkout") + cmdArgs = append(cmdArgs, args...) - return cmd.SysExec() + return sysExec(cmdArgs...) } func AddRemote(name, url string) error { @@ -125,12 +123,16 @@ func AddRemote(name, url string) error { return err } -func Help(command string) error { - cmd := cmd.New("git") - cmd.WithArg(command) - cmd.WithArg("--help") +func ExecCheckout(args []string) error { + cmdArgs := make([]string, 0) + cmdArgs = append(cmdArgs, "checkout") + cmdArgs = append(cmdArgs, args...) - return cmd.SysExec() + return sysExec(cmdArgs...) +} + +func ExecHelp(command string) error { + return sysExec("help", command, "--help") } func Log(sha1, sha2 string) (string, error) { @@ -149,6 +151,15 @@ func Log(sha1, sha2 string) (string, error) { return outputs, nil } +func sysExec(args ...string) error { + cmd := cmd.New("git") + for _, a := range args { + cmd.WithArg(a) + } + + return cmd.SysExec() +} + func execGitCmd(input []string) (outputs []string, err error) { cmd := cmd.New("git") for _, i := range input { -- GitLab