提交 2a3268cc 编写于 作者: J Jingwen Owen Ou

Merge branch 'create'

......@@ -71,6 +71,7 @@ var Remote = []*Command{
var GitHub = []*Command{
cmdPullRequest,
cmdFork,
cmdCreate,
cmdCiStatus,
cmdBrowse,
cmdCompare,
......
package commands
import (
"fmt"
"github.com/jingweno/gh/git"
"github.com/jingweno/gh/github"
"github.com/jingweno/gh/utils"
"strings"
)
var cmdCreate = &Command{
Run: create,
Usage: "create [-p] [-d DESCRIPTION] [-h HOMEPAGE] [NAME]",
Short: "Create this repository on GitHub and add GitHub as origin",
Long: `Create a new public GitHub repository from the current git
repository and add remote origin at "git@github.com:USER/REPOSITORY.git";
USER is your GitHub username and REPOSITORY is the current working
directory name. To explicitly name the new repository, pass in NAME,
optionally in ORGANIZATION/NAME form to create under an organization
you're a member of. With -p, create a private repository, and with
-d and -h set the repository's description and homepage URL, respectively.
`,
}
var (
flagCreatePrivate bool
flagCreateDescription, flagCreateHomepage string
)
func init() {
cmdCreate.Flag.BoolVar(&flagCreatePrivate, "p", false, "PRIVATE")
cmdCreate.Flag.StringVar(&flagCreateDescription, "d", "", "DESCRIPTION")
cmdCreate.Flag.StringVar(&flagCreateHomepage, "h", "", "HOMEPAGE")
}
/*
$ gh create
... create repo on github ...
> git remote add -f origin git@github.com:YOUR_USER/CURRENT_REPO.git
# with description:
$ gh create -d 'It shall be mine, all mine!'
$ gh create recipes
[ repo created on GitHub ]
> git remote add origin git@github.com:YOUR_USER/recipes.git
$ gh create sinatra/recipes
[ repo created in GitHub organization ]
> git remote add origin git@github.com:sinatra/recipes.git
*/
func create(command *Command, args *Args) {
gh := github.NewWithoutProject()
var nameWithOwner string
if args.IsParamsEmpty() {
name, err := repoName()
utils.Check(err)
nameWithOwner = fmt.Sprintf("%s/%s", gh.Config.FetchUser(), name)
} else {
nameWithOwner = args.FirstParam()
}
owner, name := parseCreateOwnerAndName(nameWithOwner)
if owner == "" {
owner = gh.Config.FetchUser()
}
project := github.Project{Name: name, Owner: owner}
var msg string
if gh.IsRepositoryExist(project) {
fmt.Printf("%s already exists on %s\n", project, github.GitHubHost)
msg = "set remmote origin"
} else {
msg = "created repository"
if !args.Noop {
repo, err := gh.CreateRepository(project, flagCreateDescription, flagCreateHomepage, flagCreatePrivate)
utils.Check(err)
owner, name = parseCreateOwnerAndName(repo.FullName)
project = github.Project{Name: name, Owner: owner}
}
}
remote, _ := git.OriginRemote()
if remote == nil {
url := project.GitURL("", "", true)
args.Replace("git", "remote", "add", "-f", "origin", url)
} else {
args.Replace("git", "remote", "-v")
}
args.After("echo", fmt.Sprintf("%s:", msg), project.String())
}
func parseCreateOwnerAndName(nameWithOwner string) (owner, name string) {
if strings.Contains(nameWithOwner, "/") {
result := strings.SplitN(nameWithOwner, "/", 2)
owner = result[0]
name = result[1]
} else {
name = nameWithOwner
}
return
}
package commands
import (
"github.com/bmizerany/assert"
"testing"
)
func TestParseCreateOwnerAndName(t *testing.T) {
owner, name := parseCreateOwnerAndName("jingweno/gh")
assert.Equal(t, "jingweno", owner)
assert.Equal(t, "gh", name)
owner, name = parseCreateOwnerAndName("gh")
assert.Equal(t, "", owner)
assert.Equal(t, "gh", name)
owner, name = parseCreateOwnerAndName("jingweno/gh/foo")
assert.Equal(t, "jingweno", owner)
assert.Equal(t, "gh/foo", name)
}
......@@ -3,8 +3,6 @@ package commands
import (
"github.com/jingweno/gh/github"
"github.com/jingweno/gh/utils"
"os"
"path/filepath"
)
var cmdInit = &Command{
......@@ -35,12 +33,11 @@ func transformInitArgs(args *Args) error {
return nil
}
dir, err := os.Getwd()
name, err := repoName()
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)
......
package commands
import (
"fmt"
"github.com/jingweno/gh/github"
"github.com/jingweno/gh/utils"
"os"
"path/filepath"
"regexp"
)
var cmdRemote = &Command{
......@@ -45,10 +41,10 @@ func transformRemoteArgs(args *Args) {
return
}
isPriavte := parseRemotePrivateFlag(args)
var err error
if name == "" {
dir, err := os.Getwd()
name, err = repoName()
utils.Check(err)
name = filepath.Base(dir)
}
if owner == "origin" {
......@@ -61,23 +57,6 @@ func transformRemoteArgs(args *Args) {
args.AppendParams(url)
}
func parseRepoNameOwner(nameWithOwner string) (string, string, bool) {
ownerRe := fmt.Sprintf("^(%s)$", OwnerRe)
ownerRegexp := regexp.MustCompile(ownerRe)
if ownerRegexp.MatchString(nameWithOwner) {
return ownerRegexp.FindStringSubmatch(nameWithOwner)[1], "", true
}
nameWithOwnerRe := fmt.Sprintf("^(%s)\\/(%s)$", OwnerRe, NameRe)
nameWithOwnerRegexp := regexp.MustCompile(nameWithOwnerRe)
if nameWithOwnerRegexp.MatchString(nameWithOwner) {
match := nameWithOwnerRegexp.FindStringSubmatch(nameWithOwner)
return match[1], match[2], true
}
return "", "", false
}
func parseRemotePrivateFlag(args *Args) bool {
if i := args.IndexOfParam("-p"); i != -1 {
args.RemoveParam(i)
......
......@@ -9,20 +9,6 @@ import (
"testing"
)
func TestParseRepoNameOwner(t *testing.T) {
owner, repo, match := parseRepoNameOwner("jingweno")
assert.T(t, match)
assert.Equal(t, "jingweno", owner)
assert.Equal(t, "", repo)
owner, repo, match = parseRepoNameOwner("jingweno/gh")
assert.T(t, match)
assert.Equal(t, "jingweno", owner)
assert.Equal(t, "gh", repo)
}
func TestTransformRemoteArgs(t *testing.T) {
args := NewArgs([]string{"remote", "add", "jingweno"})
transformRemoteArgs(args)
......
......@@ -5,9 +5,19 @@ import (
"github.com/jingweno/gh/github"
"github.com/jingweno/octokat"
"os"
"path/filepath"
"regexp"
)
func repoName() (string, error) {
dir, err := os.Getwd()
if err != nil {
return "", err
}
return filepath.Base(dir), nil
}
func isDir(file string) bool {
f, err := os.Open(file)
if err != nil {
......@@ -59,3 +69,24 @@ func convertToGitURL(pullRequest *octokat.PullRequest) (string, error) {
return project.GitURL("", user, isSSH), nil
}
func parseRepoNameOwner(nameWithOwner string) (owner, repo string, match bool) {
ownerRe := fmt.Sprintf("^(%s)$", OwnerRe)
ownerRegexp := regexp.MustCompile(ownerRe)
if ownerRegexp.MatchString(nameWithOwner) {
owner = ownerRegexp.FindStringSubmatch(nameWithOwner)[1]
match = true
return
}
nameWithOwnerRe := fmt.Sprintf("^(%s)\\/(%s)$", OwnerRe, NameRe)
nameWithOwnerRegexp := regexp.MustCompile(nameWithOwnerRe)
if nameWithOwnerRegexp.MatchString(nameWithOwner) {
result := nameWithOwnerRegexp.FindStringSubmatch(nameWithOwner)
owner = result[1]
repo = result[2]
match = true
}
return
}
......@@ -12,3 +12,17 @@ func TestParsePullRequestId(t *testing.T) {
url = "https://github.com/jingweno/gh/pull/"
assert.Equal(t, "", parsePullRequestId(url))
}
func TestParseRepoNameOwner(t *testing.T) {
owner, repo, match := parseRepoNameOwner("jingweno")
assert.T(t, match)
assert.Equal(t, "jingweno", owner)
assert.Equal(t, "", repo)
owner, repo, match = parseRepoNameOwner("jingweno/gh")
assert.T(t, match)
assert.Equal(t, "jingweno", owner)
assert.Equal(t, "gh", repo)
}
......@@ -33,6 +33,25 @@ func (gh *GitHub) CreatePullRequest(base, head, title, body string) (string, err
return pullRequest.HTMLURL, nil
}
// TODO: detach GitHub from Project
func (gh *GitHub) IsRepositoryExist(project Project) bool {
client := gh.client()
repo, err := client.Repository(octokat.Repo{project.Name, project.Owner})
return err == nil && repo != nil
}
func (gh *GitHub) CreateRepository(project Project, description, homepage string, isPrivate bool) (*octokat.Repository, error) {
params := octokat.Params{"description": description, "homepage": homepage, "private": isPrivate}
if project.Owner != gh.Config.FetchUser() {
params.Put("organization", project.Owner)
}
client := gh.client()
return client.CreateRepository(project.Name, &params)
}
func (gh *GitHub) CreatePullRequestForIssue(base, head, issue string) (string, error) {
client := gh.client()
params := octokat.PullRequestForIssueParams{base, head, issue}
......@@ -134,3 +153,11 @@ func New() *GitHub {
return &GitHub{project, c}
}
// TODO: detach project from GitHub
func NewWithoutProject() *GitHub {
c := CurrentConfig()
c.FetchUser()
return &GitHub{nil, c}
}
......@@ -15,6 +15,10 @@ type Project struct {
Owner string
}
func (p Project) String() string {
return fmt.Sprintf("%s/%s", p.Owner, p.Name)
}
func (p *Project) WebURL(name, owner, path string) string {
if owner == "" {
owner = p.Owner
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册