diff --git a/HISTORY.md b/HISTORY.md index 59436ac0f..5a86e0ed2 100644 --- a/HISTORY.md +++ b/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 diff --git a/README.md b/README.md index e1710ddf5..0599a55a2 100644 --- a/README.md +++ b/README.md @@ -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 ` 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)" ~~~ diff --git a/lib/hub/commands.rb b/lib/hub/commands.rb index 5ad86ba45..6c113f7fc 100644 --- a/lib/hub/commands.rb +++ b/lib/hub/commands.rb @@ -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 diff --git a/man/hub.1 b/man/hub.1 index 3173a4fee..dc37e249d 100644 --- a/man/hub.1 +++ b/man/hub.1 @@ -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 diff --git a/man/hub.1.html b/man/hub.1.html index 549ae6ea1..c5ecf592a 100644 --- a/man/hub.1.html +++ b/man/hub.1.html @@ -77,7 +77,7 @@

NAME

SYNOPSIS

hub [--noop] COMMAND OPTIONS
-hub alias [-s] SHELL

+hub alias [-s] [SHELL]

Expanded git commands:

@@ -108,10 +108,9 @@

DESCRIPTION

hub --noop COMMAND

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 "git@github.com:USER/REPOSITORY.git"; USER is your GitHub username and REPOSITORY is the current working directory's basename.

diff --git a/man/hub.1.ronn b/man/hub.1.ronn index 29ba2361b..e002c8616 100644 --- a/man/hub.1.ronn +++ b/man/hub.1.ronn @@ -4,7 +4,7 @@ hub(1) -- git + hub = github ## SYNOPSIS `hub` [`--noop`] -`hub alias` [`-s`] +`hub alias` [`-s`] [] ### 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`] : - Writes shell aliasing code for (`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`] []: + Shows shell instructions for wrapping git. If given, 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` : Create a git repository as with git-init(1) and add remote `origin` at diff --git a/test/alias_test.rb b/test/alias_test.rb index 1b7f97b83..7c8a93b3c 100644 --- a/test/alias_test.rb +++ b/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 diff --git a/test/helper.rb b/test/helper.rb index 037e0e30b..ae2f1fffe 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -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),