DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Capistrano : Apply Local Patches When Deploying From An External Source Code Repository

12.13.2006
| 5302 views |
  • submit to reddit
        I've submitted patches to a couple rails apps, and want to run off of their SCM's trunk code, but with my local patches applied. These Capistrano tasks will take any files matching <pre>patches/*.diff</pre> in your local directory, and apply them before restarting your app.

task :after_setup do
  patches_setup
end

task :after_update_code do
  send_and_apply_patches
end

task :patches_setup do
  run "mkdir -p #{deploy_to}/#{shared_dir}/patches" 
end

task :send_and_apply_patches do
  Dir[File.join(File.dirname(__FILE__), '../patches/*.diff')].sort.each do |patch|
    puts "sending #{File.basename(patch)}"
    put(File.read(patch),
       "#{deploy_to}/#{shared_dir}/patches/#{File.basename(patch)}",
       :mode => 0777)
    puts "applying #{File.basename(patch)}"
    run "cd #{release_path}; patch -p0 < #{deploy_to}/#{shared_dir}/patches/#{File.basename(patch)}"
  end
end