Get current working copy version in subversion/git

Hi,

How would I report the current revision, of the subversion/git repo

that is holding a ruby app?

This is actually going to be used in rails, but it seems generally

applicable to any ruby programming…

I keep my ruby and rails projects in subversion (or git, or in one

case both…) and it would be nice to be able to show in my output, the
current working copy revision number.

I'd like to put this in my app/views/layouts/application.rb in rails

for example, so that the people using my dev server can always see the
revision when they are forming bug reports, and also include the
information in the “email me a bug report” link I have on the front
page… I’d also like to include it in the about box of a wxRuby app

Thanks

Anthony

On Thu, 1 Oct 2009, Anthony M. wrote:

Hi,

How would I report the current revision, of the subversion/git repo
that is holding a ruby app?

I use this in a Rails initializer, it works under Windows as well.

http://gist.github.com/198268

Call +git+ and obtain the current commit SHA1 and commit date. Store

these values

in +APP_VERSION_COMMIT_ID+ and +APP_VERSION_COMMIT_DATE+. Then define

+APP_VERSION+ to be a string of the two.

git_cmd = ‘git show --pretty=format:“%H|%ci” --quiet’
commit_id = nil
commit_date = nil

begin
unless RAILS_ENV==‘test’
commit_id, commit_date = IO.popen(git_cmd) {|f|
log_entry = f.gets
log_entry.split(“|”) unless log_entry.nil?
}
else
commit_id, commit_date = ‘’,‘’
end
rescue Errno::ENOENT # git not installed
commit_id = ‘Unknown’
commit_date = ‘’
end

unless defined?(::APP_VERSION)
APP_VERSION_COMMIT_ID = commit_id
APP_VERSION_COMMIT_DATE = commit_date
APP_VERSION = “#{commit_id} #{commit_date}”
end

Matt Haley wrote:

I use this in a Rails initializer, it works under Windows as well.

Hi Matt,

Thanks for this, it's taken me a while to look at it though.

If I include the code below in my wxRuby app,  I get the errors

below. This is simply including the code in a method, in a class, and
including the source file. No calls to it or anything yet…

Which file to I need to include this in in a rails app?

Thanks

Anthony

/usr/lib/ruby19/gems/1.9.1/gems/activesupport-2.3.3/lib/active_support/dependencies.rb:156:in
require': /home/anthony/Personal/Projects/wxRuby/wxRCG/lib/controllers/dm_screen_controller.rb:30: dynamic constant assignment (SyntaxError) APP_VERSION_COMMIT_ID = commit_id ^ /home/anthony/Personal/Projects/wxRuby/wxRCG/lib/controllers/dm_screen_controller.rb:31: dynamic constant assignment APP_VERSION_COMMIT_DATE = commit_date ^ /home/anthony/Personal/Projects/wxRuby/wxRCG/lib/controllers/dm_screen_controller.rb:32: dynamic constant assignment APP_VERSION = "#{commit_id} #{commit_date}" ^ from /usr/lib/ruby19/gems/1.9.1/gems/activesupport-2.3.3/lib/active_support/dependencies.rb:156:inblock in require’
from
/usr/lib/ruby19/gems/1.9.1/gems/activesupport-2.3.3/lib/active_support/dependencies.rb:521:in
new_constants_in' from /usr/lib/ruby19/gems/1.9.1/gems/activesupport-2.3.3/lib/active_support/dependencies.rb:156:inrequire’
from internal:prelude:34:in require_relative' from dm_screen.rb:9:in

Ryan D. wrote:

On Sep 30, 2009, at 08:32 , Anthony M. wrote:

I keep my ruby and rails projects in subversion (or git, or in one
case both…) and it would be nice to be able to show in my output, the
current working copy revision number.

for subversion it is: svnversion ..chomp

Cool!

I take it the ‘`’ mean "run this command and return the results as a
string?

Anthony

On 10/4/09, Anthony M. [email protected] wrote:

Ryan D. wrote:

for subversion it is: svnversion ..chomp

Cool!

I take it the ‘`’ mean "run this command and return the results as a
string?

That may have been the problem you had with the other version, when I
copied and pasted from the email it came out with ':

git_cmd = ‘git show --pretty=format:“%H|%ci” --quiet’

instead of `:

[email protected] wrote:

git_cmd = git show --pretty=format:"%H|%ci" --quiet

No, it looks like the last three assignments…

dynamic constant assignment (SyntaxError)
APP_VERSION_COMMIT_ID = commit_id

On Sep 30, 2009, at 08:32 , Anthony M. wrote:

I keep my ruby and rails projects in subversion (or git, or in one
case both…) and it would be nice to be able to show in my output,
the
current working copy revision number.

for subversion it is: svnversion ..chomp

On Oct 4, 5:48 am, [email protected] wrote:

That may have been the problem you had with the other version, when I
copied and pasted from the email it came out with ':

git_cmd = ‘git show --pretty=format:"%H|%ci" --quiet’

instead of `:

git_cmd = git show --pretty=format:"%H|%ci" --quiet

There is no need for IO.popen:

commit_id, commit_date = git show --pretty=format:"%H|%ci" -- quiet.split(’|’)

Jean-Michel

On 10/4/09, Anthony M. [email protected] wrote:

Matt Haley wrote:

I use this in a Rails initializer, it works under Windows as well.

Thanks for this, it's taken me a while to look at it though.

If I include the code below in my wxRuby app,  I get the errors

below. This is simply including the code in a method, in a class, and
including the source file. No calls to it or anything yet…

On Sun, Oct 4, 2009 at 9:53 AM, Anthony M.
[email protected] wrote:

No, it looks like the last three assignments…

dynamic constant assignment (SyntaxError)
APP_VERSION_COMMIT_ID = commit_id

Oh, right, I hadn’t really looked at your error message. That error
is from sticking Matt’s example inside a method:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/264536

On Tue, 6 Oct 2009, Jean-Michel wrote:

There is no need for IO.popen:

commit_id, commit_date = git show --pretty=format:"%H|%ci" -- quiet.split(’|’)

IIRC, I was having trouble using the backticks under Windows and it
worked well with IO.popen. I may be mistaken though, it’s been a while
since I initially wrote that snippet.

[email protected] wrote:

is from sticking Matt’s example inside a method:

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/264536

Ahh, thank you. Next obvious question…Where should this code be then?

Anthony