Skip to content

Helpful rake tasks for using Rails with Subversion

If you’re running Rails 1.0RC or 0.14.1+, drop the following code in a file entitled `svn.rake` in the `lib/tasks` directory of your rails application:

desc "Configure Subversion for Rails"
task :configure_for_svn do
  system "svn remove log/*"
  system "svn commit -m 'removing all log files from subversion'"
  system 'svn propset svn:ignore "*.log" log/'
  system "svn update log/"
  system "svn commit -m 'Ignoring all files in /log/ ending in .log'"
  system 'svn propset svn:ignore "*.db" db/'
  system "svn update db/"
  system "svn commit -m 'Ignoring all files in /db/ ending in .db'"
  system "svn move config/database.yml config/database.example"
  system "svn commit -m 'Moving database.yml to database.example to provide a template for anyone who checks out the code'"
  system 'svn propset svn:ignore "database.yml" config/'
  system "svn update config/"
  system "svn commit -m 'Ignoring database.yml'"
end
   
desc "Add new files to subversion"
task :add_new_files do
   system "svn status | grep '^\?' | sed -e 's/? *//' | sed -e 's/ /\ /g' | xargs svn add"
end

desc "shortcut for adding new files"
task :add => [ :add_new_files ]

###What’s that gonna do?

It eases the process of importing a new Rails project into subversion by ignoring and/or removing files that you probably don’t want under version control: database config files (for security reasons), databases, and log files.

It _will delete your database.yml_, so I don’t recommend running the task on anything but a brand new project.

Follow my lead:

% rails project

Now would be a good time to drop `svn.rake` in the proper spot…

% svn import project_http://your.svn.server.here/project

% svn checkout http://your.svn.server.here/project project-svn

% cd project-svn
% rake configure_for_svn

In those five steps, you’ve created a fresh Rails project, imported it into a subversion repository, checked it back out into a working copy (you really don’t need the original directory after that), and then moved into your working copy and configured it properly for Subversion, according to [this page on the Rails wiki][1].

[1]: http://wiki.rubyonrails.com/rails/pages/HowtoUseRailsWithSubversion

The other two commands make it easier to put new files under version control, useful after using `script/generate` commands.

Just type `rake add` and watch it go!

_Oh, and please… if you’ve never used SVN before, don’t consider this a tutorial. It’s not. [Go read this one][2] to get started, then come back here._

[2]: http://www.germane-software.com/~ser/R_n_R/subversion.html

13 Comments

  1. topfunky wrote:

    Good stuff.

    We need to distribute more tasks like this now that we have a tidy lib/tasks folder to use.

    Posted on 22-Nov-05 at 8:49 am | Permalink
  2. Don’t forget that you can throw the -c flag onto your script/generate commands to automatically run “svn add” on generated files.

    Posted on 30-Dec-05 at 8:23 am | Permalink
  3. Jake wrote:

    Whoa.

    I did not know that.

    Where did you find that??

    Posted on 31-Dec-05 at 9:27 pm | Permalink
  4. Gary wrote:

    I also recommend adding:

    svn propset svn:ignore -R “.DS_Store” .

    to the configure_for_svn rake script if you are in Mac OS X. OS X puts in .DS_Store files in unix directories to remember icon positions and you don’t want to check those in.

    Posted on 02-Jan-06 at 12:12 pm | Permalink
  5. Jon wrote:

    Good stuff. This really ought to be in rails proper.

    Posted on 15-Mar-06 at 7:39 pm | Permalink
  6. Great post! I have extended this a bit, creating a script which you run instead of the rails command–i.e. ./svnrails.rb myproject instead of rails myproject.

    The blog post about it is here and the script itself is here.

    Thanks for the idea!

    Posted on 17-Jun-06 at 2:48 pm | Permalink
  7. Leo wrote:

    Hello,

    Very usefull, THANKS!

    I used the svn st | grep … and I needed to modify it so that file names with spaces and unusual characters would work on my OS X (using Terminal.app). Here’s the modified command if it helps anyone:

    svn st | grep '^\?' | sed -e 's/? *//' | sed -e 's/ /\ /g' | sed 's/[^][^]*/"&"/' | xargs -t svn add

    Posted on 21-Aug-06 at 4:18 pm | Permalink
  8. Leo wrote:

    BTW, what it does is surround the string provided to xargs at the end of the pipe with quotations, so you get svn add “arg”

    Posted on 21-Aug-06 at 4:19 pm | Permalink
  9. thanks for the GREAT post! Very useful…

    Posted on 21-Nov-07 at 5:44 am | Permalink
  10. The contests on online are interesting and easy to participate
    Please suggest sites which conduct online contests .

    Posted on 23-Feb-08 at 11:26 pm | Permalink
  11. The contests on online are interesting and easy to participate
    Please suggest sites which conduct online contests .
    Some of the interesting ways to spend time online is to participate in
    contests conducted online . Click here for more information on contests

    Posted on 23-Feb-08 at 11:40 pm | Permalink
  12. sporotrichum octastylos isatide fishmonger breth herzegovinian unprepare journalizer
    Humane Society of Canada
    http://www.news.cornell.edu/releases/Nov00/energy.davis.deb.html

    Posted on 25-Mar-08 at 2:03 pm | Permalink
  13. sporotrichum octastylos isatide fishmonger breth herzegovinian unprepare journalizer
    Orbiscope
    http://www.ocsys.com

    Posted on 19-Apr-08 at 5:52 pm | Permalink

One Trackback/Pingback

  1. […] found several pages on the internets that discuss how to do a good subversion setup, and they each had their strong […]

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*