siegi
April 23, 2007, 9:27am
1
hi,
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,
siegi
siegi
April 23, 2007, 2:11pm
2
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]
end
Hacky, but…
siegi
April 23, 2007, 3:15pm
3
On Apr 23, 2007, at 8:10 AM, eden li wrote:
many thanks in advance,
siegi
If you’re deploying with capistrano and have a revisions.log, you can
use something like this:
Layout:
<%= application_revision %>
<%= Time.now %>
Helper:
def application_revision
" ver. #{RevisionNumber.from_log}" rescue ‘’
end
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
Copyright (c) 2007 Rob B.
Rob_Biedenharn [at] alum.mit.edu
Permission is hereby granted, free of charge, to any person
obtaining a copy
of this software and associated documentation files (the
“Software”), to
deal in the Software without restriction, including without
limitation the
rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or
sell copies of the Software, and to permit persons to whom the
Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be
included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE
SOFTWARE.
require ‘rubygems’
require ‘file/tail’
require ‘logger’
class RevisionNumber
@@logger = Logger.new(STDERR)
@@logger.level = ENV[‘RAILS_ENV’] == ‘development’ ?
Logger::DEBUG : Logger::INFO
@@logger.debug “Loading #{self}”
@@revision = {}
def self.from_log name=‘revisions.log’, forced=false
return @@revision [name] if @@revision.has_key ?(name) && ! forced
starting_dir = Dir.pwd
begin
@@logger.debug "Starting in #{Dir.pwd} to look for #{name}"
this_dir = Dir.pwd
until File.exists?(name) || this_dir == '/'
Dir.chdir '..'
@@logger.debug "Not found; moving up to #{Dir.pwd}"
break if this_dir == Dir.pwd # I.e., Windows "c:/.." => "c:/"
this_dir = Dir.pwd
end
begin
@@logger.debug "In #{Dir.pwd}, ready to open #{name}..."
rev = File.open(name) do |f|
f.extend(File::Tail)
f.backward(1).gets.match(/(\d+) \d{14}$/)[1]
end
rescue
rev = 'indeterminant'
else
rev = 'unknown' if rev.nil?
end
ensure
Dir.chdir(starting_dir)
end
@@revision[name] = rev
end
end
siegi
April 25, 2007, 3:58am
4
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:
http://svnbook.red-bean.com/nightly/en/
svn.advanced.props.special.keywords.html
-faisal
siegi
April 28, 2007, 2:43pm
5
A variation:
In environment.rb
APP_REVISION = IO.popen(“svn info”).readlines[4] rescue “not found”
In your views/layout :
I place that line at the very bottom of layouts => I can quickly check
what version has been deployed withouth showing to the users who don’t
care.
Alain
siegi
April 28, 2007, 2:46pm
6
If you’re deploying with capistrano and have a revisions.log, you can
revisions.log is phased out in Capistrano 2.x
see:
http://www.capify.org/upgrade
Alain
siegi
April 27, 2007, 11:52pm
7
On Apr 23, 2007, at 2:27 AM, siegi wrote:
hi,
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.
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
end
helper_method :svn_revision
end
That caches the revision number for the life of the Ruby process, so
it should be fast.
Brad