Skip to content

Instantly share code, notes, and snippets.

@bryanstearns
Created April 15, 2010 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bryanstearns/367653 to your computer and use it in GitHub Desktop.
Save bryanstearns/367653 to your computer and use it in GitHub Desktop.
config/initializers/ascertain_revision.rb
# "Ascertain" the revision we're running (so named so it'll come before
# the cache initializer, which uses this info).
def determine_revision
# Note the revision number we're running, and a
# more-informative string containing it.
revision_path = File.dirname(__FILE__) + "/../../REVISION"
begin
digits = 8
if File.exist? revision_path
mod_date = File.mtime(revision_path)
number = File.read(revision_path).strip[0...digits]
extra = mod_date.strftime("%H:%M %a, %b %d %Y").gsub(" 0"," ")
else
if File.exist?(".git")
full_number = `git log -1`.split(" ")[1]
number = full_number[0...digits]
extra = `git branch`.split("\n").grep(/^\*/)[0].split(' ')[-1]
else
# We don't use --xml anymore, because CentOS's svn doesn't have it.
number = `svn info`.grep(%r"^Revision: ")[0].split(" ")[1]
extra = ''
end
end
rescue
number = '???'
extra = ''
end
details = "#{Rails.env} #{number} #{extra}"
return full_number, number, details
end
FULL_SOURCE_REVISION_NUMBER, SOURCE_REVISION_NUMBER, SOURCE_REVISION_DETAILS = determine_revision
# Add this to the environment too, so it'll be visible in Hoptoad
ENV["SOURCE_REVISION_DETAILS"] = SOURCE_REVISION_DETAILS
@danielpietzsch
Copy link

Thanks for sharing.
This helped me put together my solution: http://gist.github.com/405717

@lidaobing
Copy link

you forget to initialize full_number

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment