diff --git a/commands/checkout.go b/commands/checkout.go index a2e77bfb..3713ff15 100644 --- a/commands/checkout.go +++ b/commands/checkout.go @@ -28,7 +28,7 @@ set with BRANCH. func checkout(command *Command, args *Args) { if !args.IsParamsEmpty() { err := transformCheckoutArgs(args) - utils.Fatal(err) + utils.Check(err) } } diff --git a/commands/clone.go b/commands/clone.go index 9238cd04..5e3634bb 100644 --- a/commands/clone.go +++ b/commands/clone.go @@ -9,7 +9,7 @@ import ( var cmdClone = &Command{ Run: clone, GitExtension: true, - Usage: "Clone [-p] OPTIONS [USER/]REPOSITORY DIRECTORY", + Usage: "clone [-p] OPTIONS [USER/]REPOSITORY DIRECTORY", Short: "Clone a remote repository into a new directory", Long: `Clone repository "git://github.com/USER/REPOSITORY.git" into DIRECTORY as with git-clone(1). When USER/ is omitted, assumes diff --git a/commands/commands.go b/commands/commands.go index e0fa14b4..c01cec8e 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -30,7 +30,7 @@ func (c *Command) PrintUsage() { utils.Check(err) } else { if c.Runnable() { - fmt.Printf("Usage: gh %s\n\n", c.Usage) + fmt.Printf("Usage: git %s\n\n", c.Usage) } fmt.Println(strings.Trim(c.Long, "\n")) @@ -54,6 +54,10 @@ func (c *Command) List() bool { return c.Short != "" } +var Basic = []*Command{ + cmdInit, +} + var Branching = []*Command{ cmdCheckout, cmdMerge, @@ -74,6 +78,7 @@ var GitHub = []*Command{ func All() []*Command { all := make([]*Command, 0) + all = append(all, Basic...) all = append(all, Branching...) all = append(all, Remote...) all = append(all, GitHub...) diff --git a/commands/help.go b/commands/help.go index d94d6c33..bc6023f3 100644 --- a/commands/help.go +++ b/commands/help.go @@ -40,6 +40,9 @@ func runHelp(cmd *Command, args *Args) { var usageTemplate = template.Must(template.New("usage").Parse(`Usage: gh [command] [options] [arguments] +Branching Commands:{{range .BasicCommands}}{{if .Runnable}}{{if .List}} + {{.Name | printf "%-16s"}} {{.Short}}{{end}}{{end}}{{end}} + Branching Commands:{{range .BranchingCommands}}{{if .Runnable}}{{if .List}} {{.Name | printf "%-16s"}} {{.Short}}{{end}}{{end}}{{end}} @@ -54,10 +57,12 @@ See 'gh help [command]' for more information about a command. func printUsage() { usageTemplate.Execute(os.Stdout, struct { + BasicCommands []*Command BranchingCommands []*Command RemoteCommands []*Command GitHubCommands []*Command }{ + Basic, Branching, Remote, GitHub, diff --git a/commands/init.go b/commands/init.go new file mode 100644 index 00000000..8222a7c9 --- /dev/null +++ b/commands/init.go @@ -0,0 +1,59 @@ +package commands + +import ( + "github.com/jingweno/gh/github" + "github.com/jingweno/gh/utils" + "os" + "path/filepath" +) + +var cmdInit = &Command{ + Run: gitInit, + GitExtension: true, + Usage: "init -g", + Short: "Create an empty git repository or reinitialize an existing one", + Long: `Create a git repository as with git-init(1) and add remote origin at +"git@github.com:USER/REPOSITORY.git"; USER is your GitHub username and +REPOSITORY is the current working directory's basename. +`, +} + +/* + $ gh init -g + > git init + > git remote add origin git@github.com:USER/REPO.git +*/ +func gitInit(command *Command, args *Args) { + if !args.IsParamsEmpty() { + err := transformInitArgs(args) + utils.Check(err) + } +} + +func transformInitArgs(args *Args) error { + if !parseInitFlag(args) { + return nil + } + + dir, err := os.Getwd() + if err != nil { + return err + } + + name := filepath.Base(dir) + owner := github.CurrentConfig().FetchUser() + project := github.Project{Owner: owner, Name: name} + url := project.GitURL(name, owner, true) + args.After("git", "remote", "add", "origin", url) + + return nil +} + +func parseInitFlag(args *Args) bool { + if i := args.IndexOfParam("-g"); i != -1 { + args.RemoveParam(i) + return true + } + + return false +} diff --git a/commands/init_test.go b/commands/init_test.go new file mode 100644 index 00000000..6a2e3d43 --- /dev/null +++ b/commands/init_test.go @@ -0,0 +1,35 @@ +package commands + +import ( + "github.com/bmizerany/assert" + "github.com/jingweno/gh/github" + "os" + "path/filepath" + "regexp" + "testing" +) + +func TestTransformInitArgs(t *testing.T) { + github.DefaultConfigFile = "./test_support/gh" + config := github.Config{User: "jingweno", Token: "123"} + github.SaveConfig(&config) + defer os.RemoveAll(filepath.Dir(github.DefaultConfigFile)) + + args := NewArgs([]string{"init"}) + err := transformInitArgs(args) + + assert.Equal(t, nil, err) + assert.Equal(t, true, args.IsParamsEmpty()) + + args = NewArgs([]string{"init", "-g"}) + err = transformInitArgs(args) + + assert.Equal(t, nil, err) + assert.Equal(t, true, args.IsParamsEmpty()) + + commands := args.Commands() + assert.Equal(t, 2, len(commands)) + assert.Equal(t, "git init", commands[0].String()) + reg := regexp.MustCompile("git remote add origin git@github.com:jingweno/.+\\.git") + assert.T(t, reg.MatchString(commands[1].String())) +} diff --git a/commands/merge.go b/commands/merge.go index 6923fe79..afb63795 100644 --- a/commands/merge.go +++ b/commands/merge.go @@ -24,7 +24,7 @@ ID and title, similar to the GitHub Merge Button. func merge(command *Command, args *Args) { if !args.IsParamsEmpty() { err := transformMergeArgs(args) - utils.Fatal(err) + utils.Check(err) } } diff --git a/utils/utils.go b/utils/utils.go index d565fbd4..174d9666 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -9,12 +9,6 @@ import ( "strings" ) -func Fatal(err error) { - if err != nil { - log.Fatal(err) - } -} - func Check(err error) { if err != nil { log.Fatalf("fatal: %v", err)