Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
github
hub
提交
244495f9
H
hub
项目概览
github
/
hub
11 个月 前同步成功
通知
3
Star
22523
Fork
2406
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
H
hub
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
244495f9
编写于
4月 16, 2012
作者:
M
Mislav Marohnić
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
extract SshConfig to a separate file
上级
0a0757f2
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
94 addition
and
88 deletion
+94
-88
lib/hub.rb
lib/hub.rb
+1
-0
lib/hub/context.rb
lib/hub/context.rb
+0
-86
lib/hub/ssh_config.rb
lib/hub/ssh_config.rb
+91
-0
test/hub_test.rb
test/hub_test.rb
+2
-2
未找到文件。
lib/hub.rb
浏览文件 @
244495f9
require
'hub/version'
require
'hub/args'
require
'hub/context'
require
'hub/ssh_config'
require
'hub/json'
require
'hub/commands'
require
'hub/runner'
lib/hub/context.rb
浏览文件 @
244495f9
...
...
@@ -513,91 +513,5 @@ module Hub
def
command?
(
name
)
!
which
(
name
).
nil?
end
class
SshConfig
CONFIG_FILES
=
%w(~/.ssh/config /etc/ssh_config /etc/ssh/ssh_config)
def
initialize
files
=
nil
@settings
=
Hash
.
new
{
|
h
,
k
|
h
[
k
]
=
{}
}
Array
(
files
||
CONFIG_FILES
).
each
do
|
path
|
file
=
File
.
expand_path
path
parse_file
file
if
File
.
exist?
file
end
end
# yields if not found
def
get_value
hostname
,
key
key
=
key
.
to_s
.
downcase
@settings
.
each
do
|
pattern
,
settings
|
if
pattern
.
match?
hostname
and
found
=
settings
[
key
]
return
found
end
end
yield
end
class
HostPattern
def
initialize
pattern
@pattern
=
pattern
.
to_s
.
downcase
end
def
to_s
()
@pattern
end
def
==
(
other
)
other
.
to_s
==
self
.
to_s
end
def
matcher
@matcher
||=
if
'*'
==
@pattern
Proc
.
new
{
true
}
elsif
@pattern
!~
/[?*]/
lambda
{
|
hostname
|
hostname
.
to_s
.
downcase
==
@pattern
}
else
re
=
self
.
class
.
pattern_to_regexp
@pattern
lambda
{
|
hostname
|
re
=~
hostname
}
end
end
def
match?
hostname
matcher
.
call
hostname
end
def
self
.
pattern_to_regexp
pattern
escaped
=
Regexp
.
escape
(
pattern
)
escaped
.
gsub!
(
'\*'
,
'.*'
)
escaped
.
gsub!
(
'\?'
,
'.'
)
/^
#{
escaped
}
$/i
end
end
def
parse_file
file
host_patterns
=
[
HostPattern
.
new
(
'*'
)]
IO
.
foreach
(
file
)
do
|
line
|
case
line
when
/^\s*(#|$)/
then
next
when
/^\s*(\S+)\s*=/
key
,
value
=
$1
,
$'
else
key
,
value
=
line
.
strip
.
split
(
/\s+/
,
2
)
end
next
if
value
.
nil?
key
.
downcase!
value
=
$1
if
value
=~
/^"(.*)"$/
value
.
chomp!
if
'host'
==
key
host_patterns
=
value
.
split
(
/\s+/
).
map
{
|
p
|
HostPattern
.
new
p
}
else
record_setting
key
,
value
,
host_patterns
end
end
end
def
record_setting
key
,
value
,
patterns
patterns
.
each
do
|
pattern
|
@settings
[
pattern
][
key
]
||=
value
end
end
end
end
end
lib/hub/ssh_config.rb
0 → 100644
浏览文件 @
244495f9
module
Hub
# Reads ssh configuration files and records each setting under its host
# pattern so it can be looked up by hostname.
class
SshConfig
CONFIG_FILES
=
%w(~/.ssh/config /etc/ssh_config /etc/ssh/ssh_config)
def
initialize
files
=
nil
@settings
=
Hash
.
new
{
|
h
,
k
|
h
[
k
]
=
{}
}
Array
(
files
||
CONFIG_FILES
).
each
do
|
path
|
file
=
File
.
expand_path
path
parse_file
file
if
File
.
exist?
file
end
end
# Public: Get a setting as it would apply to a specific hostname.
#
# Yields if not found.
def
get_value
hostname
,
key
key
=
key
.
to_s
.
downcase
@settings
.
each
do
|
pattern
,
settings
|
if
pattern
.
match?
hostname
and
found
=
settings
[
key
]
return
found
end
end
yield
end
class
HostPattern
def
initialize
pattern
@pattern
=
pattern
.
to_s
.
downcase
end
def
to_s
()
@pattern
end
def
==
(
other
)
other
.
to_s
==
self
.
to_s
end
def
matcher
@matcher
||=
if
'*'
==
@pattern
Proc
.
new
{
true
}
elsif
@pattern
!~
/[?*]/
lambda
{
|
hostname
|
hostname
.
to_s
.
downcase
==
@pattern
}
else
re
=
self
.
class
.
pattern_to_regexp
@pattern
lambda
{
|
hostname
|
re
=~
hostname
}
end
end
def
match?
hostname
matcher
.
call
hostname
end
def
self
.
pattern_to_regexp
pattern
escaped
=
Regexp
.
escape
(
pattern
)
escaped
.
gsub!
(
'\*'
,
'.*'
)
escaped
.
gsub!
(
'\?'
,
'.'
)
/^
#{
escaped
}
$/i
end
end
def
parse_file
file
host_patterns
=
[
HostPattern
.
new
(
'*'
)]
IO
.
foreach
(
file
)
do
|
line
|
case
line
when
/^\s*(#|$)/
then
next
when
/^\s*(\S+)\s*=/
key
,
value
=
$1
,
$'
else
key
,
value
=
line
.
strip
.
split
(
/\s+/
,
2
)
end
next
if
value
.
nil?
key
.
downcase!
value
=
$1
if
value
=~
/^"(.*)"$/
value
.
chomp!
if
'host'
==
key
host_patterns
=
value
.
split
(
/\s+/
).
map
{
|
p
|
HostPattern
.
new
p
}
else
record_setting
key
,
value
,
host_patterns
end
end
end
def
record_setting
key
,
value
,
patterns
patterns
.
each
do
|
pattern
|
@settings
[
pattern
][
key
]
||=
value
end
end
end
end
test/hub_test.rb
浏览文件 @
244495f9
...
...
@@ -36,7 +36,7 @@ class HubTest < Test::Unit::TestCase
super
COMMANDS
.
replace
%w[open groff]
Hub
::
Context
::
PWD
.
replace
'/path/to/hub'
Hub
::
Context
::
SshConfig
::
CONFIG_FILES
.
replace
[]
Hub
::
SshConfig
::
CONFIG_FILES
.
replace
[]
@git_reader
=
Hub
::
Context
::
GitReader
.
new
'git'
do
|
cache
,
cmd
|
unless
cmd
.
index
(
'config --get alias.'
)
==
0
...
...
@@ -1444,7 +1444,7 @@ config
def
with_ssh_config
config_file
=
File
.
expand_path
'../ssh_config'
,
__FILE__
Hub
::
Context
::
SshConfig
::
CONFIG_FILES
.
replace
[
config_file
]
Hub
::
SshConfig
::
CONFIG_FILES
.
replace
[
config_file
]
yield
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录