getting svn version info from inside rails application


who has an example of how to retrieve svn-version information from
inside a rails application.

i'd like to display this information to the user, so that he can
report a bug relating to the version number.

retrieving should be quick as it is displayed on every view.

many thanks in advance,

You could parse the results of `svn info` then store it in a cached
class variable in your ApplicationHelper. In production mode, `svn
info` would only be called once.

  def svn_revision
    (@@svn_revision rescue false) or
      @@svn_revision = `svn info`.scan(/Revision: (\d+)/)[0][0]

Hacky, but...

If you're deploying with capistrano and have a revisions.log, you can use something like this:

     <div id='footer'>
       <div id='revision'><%= application_revision %></div>
       <div id='time'><%= %></div>

   def application_revision
     " ver. #{RevisionNumber.from_log}" rescue ''

From lib/revision_number.rb: Note that this uses the file-tail gem to simplify getting the last line of the log file:

# revision_number.rb

You might use a metadata file, accessible to the app, which includes
the svn revision, and then have that revision managed by svn's
'Revision' keyword substitution:


Hope you still need this, I'm a few days behind on reading the forums.

Here's what I use, assuming you have svnversion in your path:

class ApplicationController < ActionController::Base
   def svn_revision
     @svn_revision ||= `svnversion .`.gsub(/^.*\:/, '').gsub(/[MS]/, '').to_i
   helper_method :svn_revision

That caches the revision number for the life of the Ruby process, so it should be fast.


A variation:

In environment.rb

If you're deploying with capistrano and have a revisions.log, you can

revisions.log is phased out in Capistrano 2.x