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

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

# <http://gist.github.com/198268&gt;

···

On Thu, 1 Oct 2009, Anthony Metcalf wrote:

Hi,

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

#
# 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

There is no need for IO.popen:

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

Jean-Michel

···

On Oct 4, 5:48 am, brab...@gmail.com wrote:

On 10/4/09, Anthony Metcalf <anthony.metc...@anferny.me.uk> wrote:

> Ryan Davis 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 `:

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

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 10/4/09, Anthony Metcalf <anthony.metcalf@anferny.me.uk> 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 Metcalf <anthony.metcalf@anferny.me.uk> wrote:

No, it looks like the last three assignments...

dynamic constant assignment (SyntaxError)
APP_VERSION_COMMIT_ID = commit_id

brabuhr@gmail.com wrote:

···

On Sun, Oct 4, 2009 at 9:53 AM, Anthony Metcalf > <anthony.metcalf@anferny.me.uk> 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

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

Anthony

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.

···

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('|')