Skip to content

Commit

Permalink
better, smarter hub alias command
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Mar 20, 2012
1 parent 28b64ba commit a0f5504
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 99 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
@@ -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
Expand Down
17 changes: 4 additions & 13 deletions README.md
Expand Up @@ -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)"
~~~


Expand Down
59 changes: 27 additions & 32 deletions lib/hub/commands.rb
Expand Up @@ -625,43 +625,38 @@ def hub(args)
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'
}

silent = args.delete('-s')
shells = %w[bash zsh sh ksh csh fish]

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`"
script = !!args.delete('-s')
shell = args[1] || ENV['SHELL']
abort "hub alias: unknown shell" if shell.nil? or shell.empty?
shell = File.basename shell

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
Expand Down
9 changes: 3 additions & 6 deletions man/hub.1
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions man/hub.1.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions man/hub.1.ronn
Expand Up @@ -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:

Expand Down Expand Up @@ -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
Expand Down
61 changes: 40 additions & 21 deletions test/alias_test.rb
@@ -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
16 changes: 0 additions & 16 deletions test/helper.rb
Expand Up @@ -80,22 +80,6 @@ def assert_forwarded(input)
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),
Expand Down

0 comments on commit a0f5504

Please sign in to comment.