Skip to content

Commit

Permalink
Add per-SCM default exclusion patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
reagent committed Oct 20, 2014
1 parent 4c19355 commit 43c06d4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 16 deletions.
22 changes: 13 additions & 9 deletions lib/capistrano/recipes/deploy/strategy/rsync_with_remote_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ class RsyncWithRemoteCache < Remote

class InvalidCacheError < Exception; end

CONFIG = {
:subversion => {:url_command => "svn info . | sed -n \'s/URL: //p\'", :exclusions => '.svn*'},
:git => {:url_command => "git config remote.origin.url", :exclusions => '.git*'},
:mercurial => {:url_command => "hg showconfig paths.default", :exclusions => '.hg*'},
:bzr => {:url_command => "bzr info | grep parent | sed \'s/^.*parent branch: //\'", :exclusions => '.bzr*'}
}

def self.default_attribute(attribute, default_value)
define_method(attribute) { configuration[attribute] || default_value }
end

INFO_COMMANDS = {
:subversion => "svn info . | sed -n \'s/URL: //p\'",
:git => "git config remote.origin.url",
:mercurial => "hg showconfig paths.default",
:bzr => "bzr info | grep parent | sed \'s/^.*parent branch: //\'"
}

default_attribute :rsync_options, '-az --delete-excluded'
default_attribute :local_cache, '.rsync_cache'
default_attribute :repository_cache, 'cached-copy'
Expand Down Expand Up @@ -51,6 +51,10 @@ def mark_local_cache
File.open(File.join(local_cache_path, 'REVISION'), 'w') {|f| f << revision }
end

def default_exclusions
Array(CONFIG[configuration[:scm]].fetch(:exclusions, []))
end

def exclusion_options
copy_exclude.map {|f| "--exclude='#{f}'" }.join(' ')
end
Expand All @@ -73,7 +77,7 @@ def repository_cache_path
end

def repository_url
`cd #{local_cache_path} && #{INFO_COMMANDS[configuration[:scm]]}`.strip
`cd #{local_cache_path} && #{CONFIG[configuration[:scm]][:url_command]}`.strip
end

def repository_url_changed?
Expand Down Expand Up @@ -125,7 +129,7 @@ def command
private

def copy_exclude
Array(configuration.fetch(:copy_exclude, []))
default_exclusions + Array(configuration.fetch(:copy_exclude, []))
end

def run_rsync(*args)
Expand Down
42 changes: 35 additions & 7 deletions spec/capistrano_rsync_with_remote_cache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,43 @@
end
end

describe "#default_exclusions" do
it "knows the value when using Subversion" do
allow(subject).to receive(:configuration).with(no_args).and_return(:scm => :subversion)
expect(subject.default_exclusions).to eq(['.svn*'])
end

it "knows the value when using Git" do
allow(subject).to receive(:configuration).with(no_args).and_return(:scm => :git)
expect(subject.default_exclusions).to eq(['.git*'])
end

it "knows the value when using Mercurial" do
allow(subject).to receive(:configuration).with(no_args).and_return(:scm => :mercurial)
expect(subject.default_exclusions).to eq(['.hg*'])
end

it "knows the value when using Bazaar" do
allow(subject).to receive(:configuration).with(no_args).and_return(:scm => :bzr)
expect(subject.default_exclusions).to eq(['.bzr*'])
end
end

describe "#exclusion_options" do
it "is blank by default" do
expect(subject.exclusion_options).to eq('')
before { allow(subject).to receive(:default_exclusions).with(no_args).and_return(['.git*']) }

it "includes the SCM-specific list by default" do
expect(subject.exclusion_options).to eq("--exclude='.git*'")
end

it "uses the value specified in the `:copy_exclude` configuration variable" do
allow(subject).to receive(:configuration).with(no_args).and_return({:copy_exclude => '.git'})
expect(subject.exclusion_options).to eq("--exclude='.git'")
allow(subject).to receive(:configuration).with(no_args).and_return({:copy_exclude => '.jenkins'})
expect(subject.exclusion_options).to eq("--exclude='.git*' --exclude='.jenkins'")
end

it "allows multiple exclusions" do
allow(subject).to receive(:configuration).with(no_args).and_return({:copy_exclude => ['.git', '.hg']})
expect(subject.exclusion_options).to eq("--exclude='.git' --exclude='.hg'")
allow(subject).to receive(:configuration).with(no_args).and_return({:copy_exclude => ['.jenkins', 'test']})
expect(subject.exclusion_options).to eq("--exclude='.git*' --exclude='.jenkins' --exclude='test'")
end
end

Expand Down Expand Up @@ -331,6 +355,7 @@
allow(subject).to receive(:rsync_host).with(server).and_return('rsync_host')

allow(subject).to receive_messages({
:default_exclusions => [],
:rsync_options => 'rsync_options',
:ssh_port => 'ssh_port',
:local_cache_path => 'local_cache_path',
Expand Down Expand Up @@ -370,7 +395,10 @@

describe "#copy_remote_cache" do
before do
allow(subject).to receive(:repository_cache_path).with(no_args).and_return('repository_cache_path')
allow(subject).to receive_messages({
:default_exclusions => [],
:repository_cache_path => 'repository_cache_path'
})
end

it "runs the appropriate rsync command" do
Expand Down

0 comments on commit 43c06d4

Please sign in to comment.