Thursday, July 13, 2006

Pre-Commit Rake Task

Every time I'm going to commit code I try to follow the same steps:
  • Add unadded files to Subversion (svn add)
  • Delete missing files from Subversion (svn rm)
  • Update my local files from Subversion (svn up)
  • Run the tests (rake)
  • Display the status of the files (svn st)
Since I do this often, it makes sense to pull this into some kind of process:
namespace :svn do
task :st do
puts %x[svn st]
end

task :up do
puts %x[svn up]
end

task :add do
%x[svn st].split(/\n/).each do |line|
trimmed_line = line.delete('?').lstrip
if line[0,1] =~ /\?/
%x[svn add #{trimmed_line}]
puts %[added #{trimmed_line}]
end
end
end

task :delete do
%x[svn st].split(/\n/).each do |line|
trimmed_line = line.delete('!').lstrip
if line[0,1] =~ /\!/
%x[svn rm #{trimmed_line}]
puts %[removed #{trimmed_line}]
end
end
end
end

desc "Run before checking in"
task :pc => ['svn:add', 'svn:delete', 'svn:up', :default, 'svn:st']
If you add the above code to a .rake file in lib/tasks you can simply run rake pc when you are ready to commit. If everything executes successfully, you are ready to commit.

Obviously, this only works for Subversion, but you are using Subversion anyway, right?

1 comment:

  1. Jay,

    I'm working on a svn rake library - It's getting there slowly but surely, with the holy grail to be able to bootstrap a new Rails app into the repository with all the proper svn:ignores etc.

    Here's a link: Subversion Rake Tools

    ReplyDelete

Note: Only a member of this blog may post a comment.