better, smarter `hub alias` command

This commit is contained in:
Mislav Marohnić 2012-03-20 19:04:01 +01:00
Родитель 28b64ba934
Коммит a0f5504103
8 изменённых файлов: 84 добавлений и 99 удалений

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

@ -1,5 +1,6 @@
## master
* improve `hub alias` command
* change `git fork` so it fails when repo already exists under user
* teach custom commands to respect `-h` & `--help` flags
* `pull-request`: better error message for invalid remotes/URLs

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

@ -100,22 +100,13 @@ Aliasing
Using hub feels best when it's aliased as `git`. This is not dangerous; your
_normal git commands will all work_. hub merely adds some sugar.
`hub alias <shell>` displays alias instructions for the given shell. `hub alias`
alone will show the known shells.
`hub alias` displays instructions for the current shell. With the `-s` flag, it
outputs a script suitable for `eval`.
~~~
$ hub alias bash
Run this in your shell to start using `hub` as `git`:
alias git=hub
~~~
You should place this command in your `.bash_profile` or other startup
script to ensure runs on login.
The alias command can also be eval'd directly using the `-s` flag:
You should place this command in your `.bash_profile` or other startup script:
~~~ sh
$ eval `hub alias -s bash`
eval "$(hub alias -s)"
~~~

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

@ -625,43 +625,38 @@ module Hub
end
def alias(args)
shells = {
'sh' => 'alias git=hub',
'bash' => 'alias git=hub',
'zsh' => 'function git(){hub "$@"}',
'csh' => 'alias git hub',
'fish' => 'alias git hub'
}
shells = %w[bash zsh sh ksh csh fish]
silent = args.delete('-s')
script = !!args.delete('-s')
shell = args[1] || ENV['SHELL']
abort "hub alias: unknown shell" if shell.nil? or shell.empty?
shell = File.basename shell
if shell = args[1]
if silent.nil?
puts "Run this in your shell to start using `hub` as `git`:"
print " "
end
else
puts "usage: hub alias [-s] SHELL", ""
puts "You already have hub installed and available in your PATH,"
puts "but to get the full experience you'll want to alias it to"
puts "`git`.", ""
puts "To see how to accomplish this for your shell, run the alias"
puts "command again with the name of your shell.", ""
puts "Known shells:"
shells.map { |key, _| key }.sort.each do |key|
puts " " + key
end
puts "", "Options:"
puts " -s Silent. Useful when using the output with eval, e.g."
puts " $ eval `hub alias -s bash`"
exit
unless shells.include? shell
$stderr.puts "hub alias: unsupported shell"
warn "supported shells: #{shells.join(' ')}"
abort
end
if shells[shell]
puts shells[shell]
if script
puts "alias git=hub"
if 'zsh' == shell
puts "if type compdef >/dev/null; then"
puts " compdef hub=git"
puts "fi"
end
else
abort "fatal: never heard of `#{shell}'"
profile = case shell
when 'bash' then '~/.bash_profile'
when 'zsh' then '~/.zshrc'
when 'ksh' then '~/.profile'
else
'your profile'
end
puts "# Wrap git automatically by adding the following to #{profile}:"
puts
puts 'eval "$(hub alias -s)"'
end
exit

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

@ -10,7 +10,7 @@
\fBhub\fR [\fB\-\-noop\fR] \fICOMMAND\fR \fIOPTIONS\fR
.
.br
\fBhub alias\fR [\fB\-s\fR] \fISHELL\fR
\fBhub alias\fR [\fB\-s\fR] [\fISHELL\fR]
.
.SS "Expanded git commands:"
\fBgit init \-g\fR \fIOPTIONS\fR
@ -68,11 +68,8 @@ hub enhances various git commands to ease most common workflows with GitHub\.
Shows which command(s) would be run as a result of the current command\. Doesn\'t perform anything\.
.
.TP
\fBhub alias\fR [\fB\-s\fR] \fISHELL\fR
Writes shell aliasing code for \fISHELL\fR (\fBbash\fR, \fBsh\fR, \fBzsh\fR, \fBcsh\fR) to standard output\. With the \fB\-s\fR option, the output of this command can be evaluated directly within the shell:
.
.br
\fBeval $(hub alias \-s bash)\fR
\fBhub alias\fR [\fB\-s\fR] [\fISHELL\fR]
Shows shell instructions for wrapping git\. If given, \fISHELL\fR specifies the type of shell; otherwise defaults to the value of SHELL environment variable\. With \fB\-s\fR, outputs shell script suitable for \fBeval\fR\.
.
.TP
\fBgit init\fR \fB\-g\fR \fIOPTIONS\fR

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

@ -77,7 +77,7 @@
<h2 id="SYNOPSIS">SYNOPSIS</h2>
<p><code>hub</code> [<code>--noop</code>] <var>COMMAND</var> <var>OPTIONS</var><br />
<code>hub alias</code> [<code>-s</code>] <var>SHELL</var></p>
<code>hub alias</code> [<code>-s</code>] [<var>SHELL</var>]</p>
<h3 id="Expanded-git-commands-">Expanded git commands:</h3>
@ -108,10 +108,9 @@
<dl>
<dt><code>hub --noop</code> <var>COMMAND</var></dt><dd><p>Shows which command(s) would be run as a result of the current command.
Doesn't perform anything.</p></dd>
<dt><code>hub alias</code> [<code>-s</code>] <var>SHELL</var></dt><dd><p>Writes shell aliasing code for <var>SHELL</var> (<code>bash</code>, <code>sh</code>, <code>zsh</code>,
<code>csh</code>) to standard output. With the <code>-s</code> option, the output of
this command can be evaluated directly within the shell:<br />
<code>eval $(hub alias -s bash)</code></p></dd>
<dt><code>hub alias</code> [<code>-s</code>] [<var>SHELL</var>]</dt><dd><p>Shows shell instructions for wrapping git. If given, <var>SHELL</var> specifies the
type of shell; otherwise defaults to the value of SHELL environment
variable. With <code>-s</code>, outputs shell script suitable for <code>eval</code>.</p></dd>
<dt><code>git init</code> <code>-g</code> <var>OPTIONS</var></dt><dd><p>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></dd>

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

@ -4,7 +4,7 @@ hub(1) -- git + hub = github
## SYNOPSIS
`hub` [`--noop`] <COMMAND> <OPTIONS>
`hub alias` [`-s`] <SHELL>
`hub alias` [`-s`] [<SHELL>]
### Expanded git commands:
@ -36,11 +36,10 @@ hub enhances various git commands to ease most common workflows with GitHub.
Shows which command(s) would be run as a result of the current command.
Doesn't perform anything.
* `hub alias` [`-s`] <SHELL>:
Writes shell aliasing code for <SHELL> (`bash`, `sh`, `zsh`,
`csh`) to standard output. With the `-s` option, the output of
this command can be evaluated directly within the shell:
`eval $(hub alias -s bash)`
* `hub alias` [`-s`] [<SHELL>]:
Shows shell instructions for wrapping git. If given, <SHELL> specifies the
type of shell; otherwise defaults to the value of SHELL environment
variable. With `-s`, outputs shell script suitable for `eval`.
* `git init` `-g` <OPTIONS>:
Create a git repository as with git-init(1) and add remote `origin` at

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

@ -1,40 +1,59 @@
require 'helper'
class AliasTest < Test::Unit::TestCase
def test_alias
instructions = hub("alias")
assert_includes "bash", instructions
assert_includes "sh", instructions
assert_includes "csh", instructions
assert_includes "zsh", instructions
assert_includes "fish", instructions
def test_alias_instructions
expected = "# Wrap git automatically by adding the following to your profile:\n"
expected << "\n"
expected << 'eval "$(hub alias -s)"' << "\n"
assert_equal expected, hub("alias sh")
end
def test_alias_silent
assert_equal "alias git=hub\n", hub("alias -s bash")
def test_alias_instructions_bash
with_shell('bash') do
assert_includes '~/.bash_profile', hub("alias")
end
end
def test_alias_bash
assert_alias_command "bash", "alias git=hub"
def test_alias_instructions_zsh
with_shell('zsh') do
assert_includes '~/.zshrc', hub("alias")
end
end
def test_alias_sh
assert_alias_command "sh", "alias git=hub"
def test_alias_script_bash
with_shell('bash') do
assert_equal "alias git=hub\n", hub("alias -s")
end
end
def test_alias_zsh
assert_alias_command "zsh", 'function git(){hub "$@"}'
def test_alias_script_zsh
with_shell('zsh') do
script = hub("alias -s")
assert_includes "alias git=hub\n", script
assert_includes "compdef hub=git\n", script
end
end
def test_alias_csh
assert_alias_command "csh", "alias git hub"
def test_unknown_shell
with_shell(nil) do
assert_equal "hub alias: unknown shell\n", hub("alias -s")
end
end
def test_alias_fish
assert_alias_command "fish", "alias git hub"
def test_unsupported_shell
with_shell('foosh') do
expected = "hub alias: unsupported shell\n"
expected << "supported shells: bash zsh sh ksh csh fish\n"
assert_equal expected, hub("alias -s")
end
end
def test_alias_blah
assert_alias_command "blah", "fatal: never heard of `blah'"
private
def with_shell(shell)
old_shell, ENV['SHELL'] = ENV['SHELL'], shell
yield
ensure
ENV['SHELL'] = old_shell
end
end

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

@ -80,22 +80,6 @@ class Test::Unit::TestCase
assert !cmd.args.changed?, "arguments were not supposed to change: #{cmd.args.inspect}"
end
# Asserts that `hub` will show a specific alias command for a
# specific shell.
#
# e.g.
# assert_alias_command "sh", "alias git=hub"
#
# Here we are saying that this:
# $ hub alias sh
# Should display this:
# Run this in your shell to start using `hub` as `git`:
# alias git=hub
def assert_alias_command(shell, command)
expected = "Run this in your shell to start using `hub` as `git`:\n %s\n"
assert_equal(expected % command, hub("alias #{shell}"))
end
# Asserts that `haystack` includes `needle`.
def assert_includes(needle, haystack)
assert haystack.include?(needle),