Subversion and Ruby on Rails

When using Subversion with Ruby on Rails there are a couple of things to take note of.

Firstly, there are files you want to exclude from the Subversion repository, the log files, temporary files and database file. The log files and tmp files can get very large, and the database file might be different on different machines.

To exclude them you can use the script from railsonwave, also attached below. Run this when you set the project up, or at any time afterwards.

svn remove log/*
svn commit -m"removing log files"
svn propset svn:ignore "*.log" log/
svn update log/
svn commit -m 'Ignoring all files in /log/ ending in .log'
svn move config/database.yml config/database.example
svn commit -m 'Moving database.yml to database.example to provide a template for anyone who checks out the code'
svn propset svn:ignore "database.yml" config/
svn update config/
svn commit -m 'Ignoring database.yml'
svn remove tmp/*
svn propset svn:ignore "*" tmp/
svn update tmp/
svn commit -m "ignore tmp/ content from now"
svn propset svn:ignore ".htaccess" config/
svn update config/
svn commit -m 'Ignoring .htaccess'
svn propset svn:ignore "dispatch.fcgi" config/
svn update config/
svn commit -m 'Ignoring dispatch.fcgi'

Random mutterings has taken this a step further with this script which will run all this for you when you create a new project.

Secondly, you will want to automatically add new files created by the generate script when you commit. If you use the Subversion command

svn --force add .

To do this, you will end up adding all those files you excluded. Instead try this tip from the Rails Wiki

svn status | grep “^\?” | awk “{print $2}” | xargs svn add

Run that just before a commit and you can be sure all files, old and new have been updated.

An even better solution from the Rails Wiki also is this script which you can save in your apps folder under script as rails-commit.

#!/usr/bin/env ruby

# my take on a easy commit script for rails...
# it is far from prefect, so:
# please feed back the modifications you made to it!
# Fixed bugs on empty to_ arrays and spelling mistakes
# by Vesa-Pekka Palmu
# orginal by Cies Breijs -- cies.breijsATgmailDOTcom
# based on a bash script by Anonymous Gentleman
# found here: <a href=""></a>

to_add = []
to_remove = []
to_checkin = []

`svn status`.each_line do |l|
  action_char, path = l.split(' ', 2)
  case action_char
    when '?'
      to_add << path
    when '!'
      to_remove << path
    when 'M'
      to_checkin << path

puts "\nyou are about to..." 

def print_list(array, str)
  puts "\n#{str}:"
  array.each { |i| puts "\t"+i }
  puts "\t<nothing>" if array.length == 0

print_list(to_add, 'add')
print_list(to_remove, 'remove')
print_list(to_checkin, 'checkin')

puts "\nplease write something for the commit log and hit enter..."
puts "(hitting enter on an empty line will cancel this commit)\n\n" 

log = gets.strip

if log.empty?
  puts "commit cancelled!\n"

puts "\ncontacting repository...\n" 

`svn add #{to_add.join(' ')}` unless to_add.empty?
`svn remove #{to_remove.join(' ')}` unless to_remove.empty?
puts "\n" + `svn commit -m "#{log.gsub('"', '\"')}"` + "\n" 

puts "running 'svn update' to be sure we are up-to-date..."
puts `svn update`

puts "\nfinished.\n"

Once added, in command line do

ruby script/rails-commit

and the script will run, telling you what will be added, and prompt you to enter a message. Perfect!

  • Ed

    Mark – Great post and script, thank you. It took me about 10 minutes to copy and paste and clean up all the HTML characters, ticks, and ellipsis to get this script to work right on my Mac. Here is a clean up ready to go version

    thanks again!