Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

support "ssh://" protocol remote urls

Fixes #28
  • Loading branch information...
commit fb1e9343e6fc8029ac16f37165a79df425fd6129 1 parent 26e24d0
@mislav authored
View
38 lib/git_deploy/configuration.rb
@@ -1,30 +1,23 @@
+require 'uri'
+require 'cgi'
+require 'forwardable'
+
class GitDeploy
module Configuration
private
- def host
- extract_host_and_user unless defined? @host
- @host
- end
+ extend Forwardable
+ def_delegator :remote_url, :host
+ def_delegator :remote_url, :port, :remote_port
+ def_delegator :remote_url, :path, :deploy_to
def remote_user
- extract_host_and_user unless defined? @user
- @user
- end
-
- def extract_host_and_user
- info = remote_url.split(':').first.split('@')
- if info.size < 2
- @user, @host = `whoami`.chomp, info.first
- else
- @user, @host = *info
+ @user ||= begin
+ user = remote_url.user
+ user ? CGI.unescape(user) : `whoami`.chomp
end
end
- def deploy_to
- @deploy_to ||= remote_url.split(':').last
- end
-
def branch
'master'
end
@@ -50,8 +43,15 @@ def remote_url(remote = options[:remote])
url = remote_urls(remote).first
if url.nil?
abort "Error: Remote url not found for remote #{remote.inspect}"
- elsif url =~ /\bgithub\.com\b/
+ elsif url =~ /(^|@)github\.com\b/
abort "Error: Remote url for #{remote.inspect} points to GitHub. Can't deploy there!"
+ else
+ url = 'ssh://' + url.sub(%r{:/?}, '/') unless url =~ %r{^[\w-]+://}
+ begin
+ url = URI.parse url
+ rescue
+ abort "Error parsing remote url #{url}"
+ end
end
url
end
View
2  lib/git_deploy/ssh_methods.rb
@@ -90,7 +90,7 @@ def scp_upload(files)
def ssh_connection
@ssh ||= begin
- ssh = Net::SSH.start(host, remote_user)
+ ssh = Net::SSH.start(host, remote_user, :port => remote_port)
at_exit { ssh.close }
ssh
end
View
48 spec/configuration_spec.rb
@@ -0,0 +1,48 @@
+require 'rspec'
+require 'git_deploy/configuration'
+
+describe GitDeploy::Configuration do
+
+ subject {
+ mod = described_class
+ obj = Object.new
+ opt = options
+ (class << obj; self; end).class_eval do
+ include mod
+ mod.private_instance_methods.each {|m| public m }
+ define_method(:options) { opt }
+ end
+ obj
+ }
+
+ let(:options) { {:remote => 'production'} }
+
+ def stub_git_config(cmd, value)
+ subject.git_config[cmd] = value
+ end
+
+ def stub_remote_url(url, remote = options[:remote])
+ stub_git_config("config --get-all remote.#{remote}.url", url)
+ end
+
+ describe "extracting user/host from remote url" do
+ context "ssh url" do
+ before { stub_remote_url 'ssh://jon%20doe@example.com:88/path/to/app' }
+
+ its(:host) { should eq('example.com') }
+ its(:remote_port) { should eq(88) }
+ its(:remote_user) { should eq('jon doe') }
+ its(:deploy_to) { should eq('/path/to/app') }
+ end
+
+ context "scp-style" do
+ before { stub_remote_url 'git@example.com:/path/to/app' }
+
+ its(:host) { should eq('example.com') }
+ its(:remote_port) { should be_nil }
+ its(:remote_user) { should eq('git') }
+ its(:deploy_to) { should eq('/path/to/app') }
+ end
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.