This commit is contained in:
Jingwen Owen Ou 2013-07-04 16:29:24 -07:00
Родитель 2801e65e2a 5f956792aa
Коммит f01ebe4f04
8 изменённых файлов: 108 добавлений и 10 удалений

Просмотреть файл

@ -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)
}
}

Просмотреть файл

@ -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

Просмотреть файл

@ -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...)

Просмотреть файл

@ -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,

59
commands/init.go Normal file
Просмотреть файл

@ -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
}

35
commands/init_test.go Normal file
Просмотреть файл

@ -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()))
}

Просмотреть файл

@ -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)
}
}

Просмотреть файл

@ -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)