提交 fc0e05c4 编写于 作者: M Mislav Marohnić

`hub create <name>` to create GitHub repository with custom name

Closes #91
上级 d84e8266
......@@ -170,7 +170,7 @@ superpowers:
### git fork
$ git fork
... hardcore forking action ...
[ repo forked on GitHub ]
> git remote add -f YOUR_USER git@github.com:YOUR_USER/CURRENT_REPO.git
Forks the original repo on GitHub and adds the new remote under your
......@@ -180,9 +180,20 @@ login" below for details.
### git create
$ git create
... hardcore creating action ...
[ repo created on GitHub ]
> git remote add origin git@github.com:YOUR_USER/CURRENT_REPO.git
# with description:
$ git create -d 'It shall be mine, all mine!'
$ git create recipes
[ repo created on GitHub ]
> git remote add origin git@github.com:YOUR_USER/recipes.git
$ git create sinatra/recipes
[ repo created in GitHub organization ]
> git remote add origin git@github.com:sinatra/recipes.git
Creates a new public github repository and adds the remote `origin` at
"git@github.com:<USER>/<REPOSITORY>.git"
......
......@@ -296,10 +296,11 @@ module Hub
if !is_repo?
puts "'create' must be run from inside a git repository"
args.skip!
elsif github_user && github_token
elsif owner = github_user and github_token
args.shift
options = {}
options[:private] = true if args.delete('-p')
new_repo_name = nil
until args.empty?
case arg = args.shift
......@@ -308,20 +309,26 @@ module Hub
when '-h'
options[:homepage] = args.shift
else
puts "unexpected argument: #{arg}"
return
if arg =~ /^[^-]/ and new_repo_name.nil?
new_repo_name = arg
owner, new_repo_name = new_repo_name.split('/', 2) if new_repo_name.index('/')
else
abort "invalid argument: #{arg}"
end
end
end
new_repo_name ||= repo_name
repo_with_owner = "#{owner}/#{new_repo_name}"
if repo_exists?(github_user)
puts "#{github_user}/#{repo_name} already exists on GitHub"
if repo_exists?(owner, new_repo_name)
puts "#{repo_with_owner} already exists on GitHub"
action = "set remote origin"
else
action = "created repository"
create_repo(options)
create_repo(repo_with_owner, options)
end
url = github_url(:private => true)
url = github_url(:repo => new_repo_name, :user => owner, :private => true)
if remotes.first != 'origin'
args.replace %W"remote add -f origin #{url}"
......@@ -329,7 +336,7 @@ module Hub
args.replace %W"remote -v"
end
args.after { puts "#{action}: #{github_user}/#{repo_name}" }
args.after { puts "#{action}: #{repo_with_owner}" }
end
rescue HTTPExceptions
display_http_exception("creating repository", $!.response)
......@@ -698,9 +705,9 @@ help
end
# Determines whether a user has a fork of the current repo on GitHub.
def repo_exists?(user)
def repo_exists?(user, repo = repo_name)
load_net_http
url = API_REPO % [user, repo_name]
url = API_REPO % [user, repo]
Net::HTTPSuccess === Net::HTTP.get_response(URI(url))
end
......@@ -716,8 +723,8 @@ help
# Creates a new repo using the GitHub API.
#
# Returns nothing.
def create_repo(options = {})
params = {'name' => repo_name}
def create_repo(name, options = {})
params = {'name' => name.sub(/^#{github_user}\//, '')}
params['public'] = '0' if options[:private]
params['description'] = options[:description] if options[:description]
params['homepage'] = options[:homepage] if options[:homepage]
......
......@@ -16,7 +16,7 @@
\fBgit init \-g\fR \fIOPTIONS\fR
.
.br
\fBgit create\fR [\fB\-p\fR] [\fB\-d <DESCRIPTION>\fR] [\fB\-h <HOMEPAGE>\fR]
\fBgit create\fR [\fINAME\fR] [\fB\-p\fR] [\fB\-d\fR \fIDESCRIPTION\fR] [\fB\-h\fR \fIHOMEPAGE\fR]
.
.br
\fBgit clone\fR [\fB\-p\fR] \fIOPTIONS\fR [\fIUSER\fR/]\fIREPOSITORY\fR \fIDIRECTORY\fR
......@@ -64,10 +64,10 @@
\fBgit init\fR \fB\-g\fR \fIOPTIONS\fR: Create a git repository as with git\-init(1) and add remote \fBorigin\fR at "git@github\.com:\fIUSER\fR/\fIREPOSITORY\fR\.git"; \fIUSER\fR is your GitHub username and \fIREPOSITORY\fR is the current working directory\'s basename\.
.
.IP "\(bu" 4
\fBgit create\fR [\fB\-p\fR] [\fB\-d <DESCRIPTION>\fR] [\fB\-h <HOMEPAGE>\fR]:
\fBgit create\fR [\fINAME\fR] [\fB\-p\fR] [\fB\-d\fR \fIDESCRIPTION\fR] [\fB\-h\fR \fIHOMEPAGE\fR]:
.
.br
Create a new public github repository from the current git repository and add remote \fBorigin\fR at "git@github\.com:\fIUSER\fR/\fIREPOSITORY\fR\.git"; \fIUSER\fR is your GitHub username and \fIREPOSITORY\fR is the current working directory\'s basename\. With \fB\-p\fR, create a private repository\. \fB\-d\fR and \fB\-h\fR set the repository\'s description and homepage, respectively\.
Create a new public GitHub repository from the current git repository and add remote \fBorigin\fR at "git@github\.com:\fIUSER\fR/\fIREPOSITORY\fR\.git"; \fIUSER\fR is your GitHub username and \fIREPOSITORY\fR is the current working directory name\. To explicitly name the new repository, pass in \fINAME\fR, optionally in \fIORGANIZATION\fR/\fINAME\fR form to create under an organization you\'re a member of\. With \fB\-p\fR, create a private repository, and with \fB\-d\fR and \fB\-h\fR set the repository\'s description and homepage URL, respectively\.
.
.IP "\(bu" 4
\fBgit clone\fR [\fB\-p\fR] \fIOPTIONS\fR [\fIUSER\fR\fB/\fR]\fIREPOSITORY\fR \fIDIRECTORY\fR:
......@@ -260,7 +260,7 @@ $ git am \-\-ignore\-whitespace https://github\.com/davidbalbert/hub/commit/fdb9
.nf
$ git fork
\.\.\. hardcore forking action \.\.\.
[ repo forked on GitHub ]
> git remote add YOUR_USER git@github\.com:YOUR_USER/CURRENT_REPO\.git
.
.fi
......@@ -280,8 +280,19 @@ $ git init \-g
.nf
$ git create
\.\.\. hardcore creating action \.\.\.
[ repo created on GitHub ]
> git remote add origin git@github\.com:YOUR_USER/CURRENT_REPO\.git
# with description:
$ git create \-d \'It shall be mine, all mine!\'
$ git create recipes
[ repo created on GitHub ]
> git remote add origin git@github\.com:YOUR_USER/recipes\.git
$ git create sinatra/recipes
[ repo created in GitHub organization ]
> git remote add origin git@github\.com:sinatra/recipes\.git
.
.fi
.
......
......@@ -80,7 +80,7 @@
<code>hub alias</code> [<code>-s</code>] <var>SHELL</var></p>
<p><code>git init -g</code> <var>OPTIONS</var><br />
<code>git create</code> [<code>-p</code>] [<code>-d &lt;DESCRIPTION></code>] [<code>-h &lt;HOMEPAGE></code>]<br />
<code>git create</code> [<var>NAME</var>] [<code>-p</code>] [<code>-d</code> <var>DESCRIPTION</var>] [<code>-h</code> <var>HOMEPAGE</var>]<br />
<code>git clone</code> [<code>-p</code>] <var>OPTIONS</var> [<var>USER</var>/]<var>REPOSITORY</var> <var>DIRECTORY</var><br />
<code>git remote add</code> [<code>-p</code>] <var>OPTIONS</var> <var>USER</var>[/<var>REPOSITORY</var>]<br />
<code>git remote set-url</code> [<code>-p</code>] <var>OPTIONS</var> <var>REMOTE-NAME</var> <var>USER</var>[/<var>REPOSITORY</var>]<br />
......@@ -108,13 +108,15 @@ this command can be evaluated directly within the shell:<br />
Create a git repository as with <span class="man-ref">git-init<span class="s">(1)</span></span> and add remote <code>origin</code> at
"git@github.com:<var>USER</var>/<var>REPOSITORY</var>.git"; <var>USER</var> is your GitHub username and
<var>REPOSITORY</var> is the current working directory's basename.</p></li>
<li><p><code>git create</code> [<code>-p</code>] [<code>-d &lt;DESCRIPTION></code>] [<code>-h &lt;HOMEPAGE></code>]:<br />
Create a new public github repository from the current git
<li><p><code>git create</code> [<var>NAME</var>] [<code>-p</code>] [<code>-d</code> <var>DESCRIPTION</var>] [<code>-h</code> <var>HOMEPAGE</var>]:<br />
Create a new public GitHub repository from the current git
repository and add remote <code>origin</code> at
"git@github.com:<var>USER</var>/<var>REPOSITORY</var>.git"; <var>USER</var> is your GitHub
username and <var>REPOSITORY</var> is the current working directory's
basename. With <code>-p</code>, create a private repository. <code>-d</code> and <code>-h</code>
set the repository's description and homepage, respectively.</p></li>
username and <var>REPOSITORY</var> is the current working directory name.
To explicitly name the new repository, pass in <var>NAME</var>, optionally in
<var>ORGANIZATION</var>/<var>NAME</var> form to create under an organization you're a
member of. With <code>-p</code>, create a private repository, and with <code>-d</code> and <code>-h</code>
set the repository's description and homepage URL, respectively.</p></li>
<li><p><code>git clone</code> [<code>-p</code>] <var>OPTIONS</var> [<var>USER</var><code>/</code>]<var>REPOSITORY</var> <var>DIRECTORY</var>:<br />
Clone repository "git://github.com/<var>USER</var>/<var>REPOSITORY</var>.git" into
<var>DIRECTORY</var> as with <span class="man-ref">git-clone<span class="s">(1)</span></span>. When <var>USER</var>/ is omitted, assumes
......@@ -278,7 +280,7 @@ $ git am --ignore-whitespace https://github.com/davidbalbert/hub/commit/fdb9921
<h3 id="git-fork">git fork</h3>
<pre><code>$ git fork
... hardcore forking action ...
[ repo forked on GitHub ]
&gt; git remote add YOUR_USER git@github.com:YOUR_USER/CURRENT_REPO.git
</code></pre>
......@@ -292,8 +294,19 @@ $ git am --ignore-whitespace https://github.com/davidbalbert/hub/commit/fdb9921
<h3 id="git-create">git create</h3>
<pre><code>$ git create
... hardcore creating action ...
[ repo created on GitHub ]
&gt; git remote add origin git@github.com:YOUR_USER/CURRENT_REPO.git
# with description:
$ git create -d 'It shall be mine, all mine!'
$ git create recipes
[ repo created on GitHub ]
&gt; git remote add origin git@github.com:YOUR_USER/recipes.git
$ git create sinatra/recipes
[ repo created in GitHub organization ]
&gt; git remote add origin git@github.com:sinatra/recipes.git
</code></pre>
<h3 id="git-push">git push</h3>
......
......@@ -7,7 +7,7 @@ hub(1) -- git + hub = github
`hub alias` [`-s`] <SHELL>
`git init -g` <OPTIONS>
`git create` [`-p`] [`-d <DESCRIPTION>`] [`-h <HOMEPAGE>`]
`git create` [<NAME>] [`-p`] [`-d` <DESCRIPTION>] [`-h` <HOMEPAGE>]
`git clone` [`-p`] <OPTIONS> [<USER>/]<REPOSITORY> <DIRECTORY>
`git remote add` [`-p`] <OPTIONS> <USER>[/<REPOSITORY>]
`git remote set-url` [`-p`] <OPTIONS> <REMOTE-NAME> <USER>[/<REPOSITORY>]
......@@ -36,13 +36,15 @@ alias command displays information on configuring your environment:
"git@github.com:<USER>/<REPOSITORY>.git"; <USER> is your GitHub username and
<REPOSITORY> is the current working directory's basename.
* `git create` [`-p`] [`-d <DESCRIPTION>`] [`-h <HOMEPAGE>`]:
Create a new public github repository from the current git
* `git create` [<NAME>] [`-p`] [`-d` <DESCRIPTION>] [`-h` <HOMEPAGE>]:
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's
basename. With `-p`, create a private repository. `-d` and `-h`
set the repository's description and homepage, respectively.
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.
* `git clone` [`-p`] <OPTIONS> [<USER>`/`]<REPOSITORY> <DIRECTORY>:
Clone repository "git://github.com/<USER>/<REPOSITORY>.git" into
......@@ -201,7 +203,7 @@ authentication?
### git fork
$ git fork
... hardcore forking action ...
[ repo forked on GitHub ]
> git remote add YOUR_USER git@github.com:YOUR_USER/CURRENT_REPO.git
### git init
......@@ -213,9 +215,20 @@ authentication?
### git create
$ git create
... hardcore creating action ...
[ repo created on GitHub ]
> git remote add origin git@github.com:YOUR_USER/CURRENT_REPO.git
# with description:
$ git create -d 'It shall be mine, all mine!'
$ git create recipes
[ repo created on GitHub ]
> git remote add origin git@github.com:YOUR_USER/recipes.git
$ git create sinatra/recipes
[ repo created in GitHub organization ]
> git remote add origin git@github.com:sinatra/recipes.git
### git push
$ git push origin,staging,qa bert_timeout
......
......@@ -432,6 +432,28 @@ class HubTest < Test::Unit::TestCase
assert_equal expected, hub("create") { ENV['GIT'] = 'echo' }
end
def test_create_custom_name
stub_no_remotes
stub_nonexisting_fork('tpw', 'hubbub')
stub_request(:post, "https://#{auth}github.com/api/v2/yaml/repos/create").
with(:body => { 'name' => 'hubbub' })
expected = "remote add -f origin git@github.com:tpw/hubbub.git\n"
expected << "created repository: tpw/hubbub\n"
assert_equal expected, hub("create hubbub") { ENV['GIT'] = 'echo' }
end
def test_create_in_organization
stub_no_remotes
stub_nonexisting_fork('acme', 'hubbub')
stub_request(:post, "https://#{auth}github.com/api/v2/yaml/repos/create").
with(:body => { 'name' => 'acme/hubbub' })
expected = "remote add -f origin git@github.com:acme/hubbub.git\n"
expected << "created repository: acme/hubbub\n"
assert_equal expected, hub("create acme/hubbub") { ENV['GIT'] = 'echo' }
end
def test_create_no_openssl
stub_no_remotes
stub_nonexisting_fork('tpw')
......@@ -503,6 +525,11 @@ class HubTest < Test::Unit::TestCase
assert_equal expected, hub("create -d toyproject -h http://example.com") { ENV['GIT'] = 'echo' }
end
def test_create_with_invalid_arguments
assert_equal "invalid argument: -a\n", hub("create -a blah") { ENV['GIT'] = 'echo' }
assert_equal "invalid argument: bleh\n", hub("create blah bleh") { ENV['GIT'] = 'echo' }
end
def test_create_with_existing_repository
stub_no_remotes
stub_existing_fork('tpw')
......@@ -871,16 +898,16 @@ config
@git["config alias.#{name}"] = value
end
def stub_existing_fork(user)
stub_fork(user, 200)
def stub_existing_fork(user, repo = 'hub')
stub_fork(user, repo, 200)
end
def stub_nonexisting_fork(user)
stub_fork(user, 404)
def stub_nonexisting_fork(user, repo = 'hub')
stub_fork(user, repo, 404)
end
def stub_fork(user, status)
stub_request(:get, "github.com/api/v2/yaml/repos/show/#{user}/hub").
def stub_fork(user, repo, status)
stub_request(:get, "github.com/api/v2/yaml/repos/show/#{user}/#{repo}").
to_return(:status => status)
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册