Skip to content

Instantly share code, notes, and snippets.

@kagminjeong
Created September 13, 2011 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kagminjeong/1215416 to your computer and use it in GitHub Desktop.
Save kagminjeong/1215416 to your computer and use it in GitHub Desktop.
Capistrano subversion branch switching patch
require "capistrano/recipes/deploy/scm/subversion"
# Overlay patch for https://capistrano.lighthouseapp.com/projects/8716-capistrano/tickets/46.
# New ticket: https://github.com/capistrano/capistrano/issues/40
# Use class_eval instead of class definition to ensure that we are overriding
# existing sync method.
Capistrano::Deploy::SCM::Subversion.class_eval do
# Returns the command that will do an "svn switch" to the given
# revision, for the working copy at the given destination.
define_method(:sync) do |revision, destination|
# Subversion supports revision pegging in switch starting with 1.5.0.
# Before that we can only hope that the path being synced to exists
# in the currently checked out version.
scm_min_version = scm_min_version = variable(:scm_min_version)
if scm_min_version && scm_min_version >= '1.5.0'
scm :switch, arguments, verbose, authentication, "-r#{revision}", "#{repository}@#{revision}", destination
else
check = <<-CMD
remote_repository=$(
cd #{destination} &&
svn info |grep URL: |awk '{print $2}'
) &&
if test "$remote_repository" != "#{repository}"; then
{
echo "Cannot switch branches while updating on subversion less than 1.5.0";
echo "If remote subversion is 1.5.0 or better, set :scm_min_version variable appropriately";
echo "Remote repository URL: $remote_repository";
echo "Repository URL being deployed: #{repository}";
} 1>&2;
exit 4;
fi
CMD
switch = scm :switch, arguments, verbose, authentication, "-r#{revision}", repository, destination
"#{check} && #{switch}"
end
end
end
def run_svn_version_check
scm_min_version = fetch(:scm_min_version, nil)
if scm_min_version
run <<-EOT
svn --version | grep 'svn, version' |awk '{print $3}' |
ruby -e "
actual_text = STDIN.read.strip;
actual = actual_text.split('.');
required = '#{scm_min_version}'.split('.');
required.each_with_index do |required_piece, index|
actual_piece = actual[index];
if required_piece.to_i > actual_piece.to_i then
STDERR.puts \\\"Remote svn version too low: needed #{scm_min_version}, found \#{actual_text}\\\";
exit 1;
end
end
"
EOT
end
end
# Usage:
#
# before 'deploy:update_code', 'deploy:svn_version_check'
#
namespace :deploy do
task :svn_version_check, :except => {:no_release => true} do
run_svn_version_check
end
end
# In your deploy.rb:
require 'lib/capistrano_subversion_sync_patch'
# Specify minimum version of scm in use. This should be set to the smallest
# of versions installed on the machine running capistrano commands and the
# machines which are deployment targets.
set :scm_min_version, '1.6.0'
# To check remote svn version before deployment:
load 'lib/capistrano_subversion_sync_patch_tasks'
before 'deploy:update_code', 'deploy:svn_version_check'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment