Rails

Stories from the trenches of my day job building Web applications.

4

JAN
2006

Add to Subversion

I love Rails generators and I love Subversion, but I'm not so sure they love each other.

Little is worse than generating some scaffold files or a login system, tediously typing svn add ... a bunch of times, and later learning that you missed a file. Oops.

Given that, here's my hack to get the Rails generators and Subversion to kiss and make up:

desc "Add generated files to Subversion"
task :add_to_svn do
  sh %Q{svn status | ruby -nae 'system "svn add \\#{$F[1]}" if $F[0] == "?"'}
end

I put that in "lib/tasks/add_to_svn.rake" for all my Rails projects. You can then access it whenever you need with rake add_to_svn.

I've seen similar tasks posted, but they've always been Unix specific and I work with some Windows programmers. This is my attempt to help both on platforms.

How does it work?

You're probably not use to seeing ugly Ruby code like that, but Ruby supports a lot of command-line shortcuts that it inherited from Perl. Here's a breakdown of the options used:

-e The code to execute directly follows this option.
-n Wrap my code in a standard Unix filter style processing loop.
-a Auto-split each line of input.

Together they mean that Ruby sees the above input as something close to:

ARGF.each_line do |line|
  $F = line.split

  # my custom code is inserted here...
  system "svn add #{$F[1]}" if $F[0] == "?"
end

That just asks Ruby to call svn add ... for each file svn status flagged with a "?". Those will be all the files Subversion doesn't yet recognize.

WARNING: Make sure you've told Subversion to ignore files you don't want it messing with (like Rails's log files), or they will be added!

Comments (2)
  1. Marcel Molina Jr.
    Marcel Molina Jr. February 13th, 2006 Reply Link

    It so happens that this already exists as part of the generator framework.

    % ruby script/generate | grep -- -c
    -c, --svn          Modify files with subversion. (Note: svn must be in path)
    
    1. Reply (using GitHub Flavored Markdown)

      Comments on this blog are moderated. Spam is removed, formatting is fixed, and there's a zero tolerance policy on intolerance.

      Ajax loader
    2. James Edward Gray II
      James Edward Gray II February 13th, 2006 Reply Link

      That's great to know, as I certainly wasn't aware of it.

      Quick question: does the generator solution work for files it didn't add to the application?

      1. Reply (using GitHub Flavored Markdown)

        Comments on this blog are moderated. Spam is removed, formatting is fixed, and there's a zero tolerance policy on intolerance.

        Ajax loader
Leave a Comment (using GitHub Flavored Markdown)

Comments on this blog are moderated. Spam is removed, formatting is fixed, and there's a zero tolerance policy on intolerance.

Ajax loader