Subversion Info in Ruby / Rails

I was trying to tag a few of my internal apps with a subversion revision number just for personal reference. Since running the command svn info yields YAML-ish output the ruby YAML library can load it. Sweet. The next step was just wrapping it in a class and creating some instance variables and methods for them.

The default YAML output

This is what you will get if you simply throw the YAML.load result to an array or something. Nice, but I’m not a big fan of “Keys Like This” so…

irb(main):011:0> pp YAML.load(`svn info`)
{"Node Kind"=>"directory",
 "Last Changed Author"=>"rob",
 "URL"=>"http://example.com/share/lib",
 "Schedule"=>"normal",
 "Last Changed Rev"=>441,
 "Repository UUID"=>"0afc494f-e74d-0410-99f2-b94b27995134",
 "Repository Root"=>"http://example.com",
 "Last Changed Date"=>"2008-07-15 15:55:54 -0400 (Tue, 15 Jul 2008)",
 "Revision"=>446,
 "Path"=>"."}
=> nil

A nice class wrapper for the SVN Info

Heres what I ended up with. Nothing fancy, just a simple wrapper around the subversion info dump.

require 'yaml'
class SVNInfo
  def initialize
      YAML.load(`svn info`).each do |k,v|
          key = k.gsub(/\s/, '_').downcase
          instance_variable_set "@#{key}", v
          instance_eval %{ def #{key}; @#{key}; end }
      end
  end
end

Here is the end result:

irb(main):013:0> svn_info = SVNInfo.new
# ... snip ...
irb(main):014:0> pp svn_info
<SVNInfo:0x57f378
 @last_changed_author="rob",
 @last_changed_date="2008-07-15 15:55:54 -0400 (Tue, 15 Jul 2008)",
 @last_changed_rev=441,
 @node_kind="directory",
 @path=".",
 @repository_root="http://example.com",
 @repository_uuid="0afc494f-e74d-0410-99f2-b94b27995134",
 @revision=446,
 @schedule="normal",
 @url="http://example.com/share/lib">
=> nil
irb(main):015:0>

Messing Around

irb(main):018:0> pp svn_info.methods - Object.methods
["last_changed_author",
 "revision",
 "repository_root",
 "last_changed_rev",
 "path",
 "url",
 "node_kind",
 "last_changed_date",
 "repository_uuid",
 "schedule"]
=> nil
irb(main):019:0> svn_info.revision
=> 446
irb(main):020:0> svn_info.last_changed_author
=> "rob"
irb(main):021:0>

Hope that comes in handy :)



Comments

Add Yours

  • Author Avatar

    YOU


Comment Arrow



About Author

Rob Hurring

Ruby, Rails, PHP, bash... oh my!