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

support git aliases

closes #54
上级 ff40b54e
......@@ -41,6 +41,22 @@ module Hub
API_FORK = 'http://github.com/api/v2/yaml/repos/fork/%s/%s'
API_CREATE = 'http://github.com/api/v2/yaml/repos/create'
def run(args)
# Hack to emulate git-style
args.unshift 'help' if args.grep(/^[^-]|version|exec-path$|html-path/).empty?
cmd = args[0]
expanded_args = expand_alias(cmd)
cmd = expanded_args[0] if expanded_args
# git commands can have dashes
cmd = cmd.sub(/(\w)-/, '\1_')
if method_defined?(cmd) and cmd != 'run'
args[0, 1] = expanded_args if expanded_args
send(cmd, args)
end
end
# $ hub clone rtomayko/tilt
# > git clone git://github.com/rtomayko/tilt.
#
......@@ -687,5 +703,14 @@ help
response.error! unless Net::HTTPSuccess === response
end
def expand_alias(cmd)
if expanded = git_alias_for(cmd)
if expanded.index('!') != 0
require 'shellwords' unless expanded.respond_to? :shellsplit
expanded.shellsplit
end
end
end
end
end
......@@ -106,6 +106,10 @@ module Hub
GIT_CONFIG['config --bool hub.http-clone'] == 'true'
end
def git_alias_for(name)
GIT_CONFIG["config alias.#{name}"]
end
# Core.repositoryformatversion should exist for all git
# repositories, and be blank for all non-git repositories. If
# there's a better config setting to check here, this can be
......
......@@ -9,13 +9,7 @@ module Hub
def initialize(*args)
@args = Args.new(args)
# Hack to emulate git-style
@args.unshift 'help' if @args.grep(/^[^-]|version|exec-path$|html-path/).empty?
# git commands can have dashes
cmd = @args[0].sub(/(\w)-/, '\1_')
Commands.send(cmd, @args) if Commands.method_defined?(cmd)
Commands.run(@args)
end
# Shortcut
......@@ -36,7 +30,8 @@ module Hub
def commands
args.commands.map do |cmd|
if cmd.respond_to?(:join)
cmd.map { |c| c.index(' ') ? "'#{c}'" : c }.join(' ')
# a simplified `Shellwords.join` but it's OK since this is only used to inspect
cmd.map { |c| (c.index(' ') || c.empty?) ? "'#{c}'" : c }.join(' ')
else
cmd.to_s
end
......
......@@ -30,7 +30,9 @@ class HubTest < Test::Unit::TestCase
Hub::Context::REMOTES.clear
@git = Hub::Context::GIT_CONFIG.replace(Hash.new { |h, k|
raise ArgumentError, "`git #{k}` not stubbed"
unless k.index('config alias.') == 0
raise ArgumentError, "`git #{k}` not stubbed"
end
}).update(
'remote' => "mislav\norigin",
'symbolic-ref -q HEAD' => 'refs/heads/master',
......@@ -114,6 +116,25 @@ class HubTest < Test::Unit::TestCase
assert_forwarded "clone ./test"
end
def test_alias_expand
stub_alias 'c', 'clone --bare'
input = "c rtomayko/ronn"
command = "git clone --bare git://github.com/rtomayko/ronn.git"
assert_command input, command
end
def test_alias_expand_advanced
stub_alias 'c', 'clone --template="white space"'
input = "c rtomayko/ronn"
command = "git clone '--template=white space' git://github.com/rtomayko/ronn.git"
assert_command input, command
end
def test_alias_doesnt_expand_for_unknown_commands
stub_alias 'c', 'compute --fast'
assert_forwarded "c rtomayko/ronn"
end
def test_remote_origin
input = "remote add origin"
command = "git remote add origin git://github.com/tpw/hub.git"
......@@ -771,6 +792,10 @@ config
@git.replace({})
end
def stub_alias(name, value)
@git["config alias.#{name}"] = value
end
def stub_existing_fork(user)
stub_fork(user, 200)
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册