SiKaNrOnG.com

Agile Developer Extraordinaire

Blog

Subversion Checkout Hooks in RUBY!

September - 04 - 2007
Well, I promised to share these kinds of things with the community when i wrote this blog code, so here we go.. Basically, the problem is this: Subversion is great version control software. You can even configure it with hooks so that when someone commits to a repo you can write the file:
/repo_parent_path/repo/hooks/pre-commit.tmpl
and the script will be executed (like the tell-tale filename says) pre-commit. So, the hooks folder by default looks like this:
~/Desktop/repo/hooks $ ls
post-commit.tmpl                pre-lock.tmpl
post-lock.tmpl                  pre-revprop-change.tmpl
post-revprop-change.tmpl        pre-unlock.tmpl
post-unlock.tmpl                start-commit.tmpl
pre-commit.tmpl
Woah woah WOAH! What's missing up there? How about like HALF the BATTLE!! Where did pre-checkout and post-checkout go? Where did pre/post-udate go? NOWHERE! The never existed (cruelty!!) So now you're like, crap - what do i do with my poor ruby app, do i go back to my boss and tell him it can't be done? Nay, friends. It can be done.. Basically, you're going to take advantage of the apache2 CustomLog directive, located in mod_log_config apache2 module. Anyway, here's the snippet from my default site apache conf file. I'm using OS X, with a fink install of apache2, so the file I'm about to show you the excerpt from might be in a different location on your machine:
# excerpt from /sw/etc/apache2/sites-available/default 

CustomLog "|/sw/var/www/svnhook_dispatch.rb" "%{SVN-ACTION}e\t%U" env=SVN-ACTION
    
      DAV svn
      
      SVNParentPath /Users/alexanderpilafian/localhost/adhearsion.com/repos/
      SVNListParentPath on
      
      AuthType Basic
      AuthName "Subversion repository"
      AuthUserFile /Users/alexanderpilafian/localhost/adhearsion.com/repos/.svnuser.passwd
      AuthzSVNAccessFile /Users/alexanderpilafian/localhost/adhearsion.com/repos/.svnuser_authz.passwd

      # try anonymous access first, resort to real 
      # authentication if necessary.
	   Satisfy Any
	   Require valid-user
  
Notice that I've put the CustomLog directive OUTSIDE the <Location> block. Also notice the pipe in the CustomLog line. This means (in roughtly translated english) pipe each new value from env SVN-ACTION to /sw/var/www/svnhook_dispatch.rb as it comes in. So, what does /sw/var/www/svnhook_dispatch.rb have in it? Good question:
#!/opt/local/bin/ruby

#########################################################
## This is my solution to the stupid lack of checkout  ##
## hooks in subversion. I'm to lazy too submit a patch.##
## Someone should. Until then, enjoy.                  ##
#########################################################

# Yeah. It only works for SVNParentPath. Sorry :) repos_dir 
# is equivalent to that dir. Dispatch dir is the dir of this file.
# equally, you could do `pwd`. But, i didn't.
repos_dir = "/Library/WebServer/localhost/adhearsion.com/repos/"
dispatch_dir = "/sw/var/www/"


while ln = gets

 #Cleans it up a bit by splitting off some unnecessary spaces and data
 params = ln.strip
 params_ar = params.split("\t")

 @svn_action = params_ar[0].split[0] #take off useless checkout dir
 @svn_unix_name = params_ar[1].split('/')[2] #assign unix name
 
 # Records in dispatch logfile, actually records a high-level log of
 # Your subversion stuff in dispatch_log.txt
 `echo #{params} >> #{dispatch_dir}dispatch_log.txt;`

 #Path to the appropriate hook file
 @hook_location = "#{repos_dir}#{@svn_unix_name}/hooks/pre-#{@svn_action}.tmpl"
 
 # Call the script!
 #script_out = `/Users/alexanderpilafian/localhost/adhearsion.com/repos/sublime/hooks/test_me.rb`
 script_out = `#{@hook_location}`

 #################################################################
 ## uncomment these lines to output some test debugging info    ##
 ## to debug.out (You gotta touch debug.out first)              ##
 #################################################################

 #outstr = "\nAction: #{@svn_action}\n" +
 #"Repo: #{@svn_unix_name}\n" +
 #"File: #{@hook_location}\n" +
 #"Script Out: \r\n#{script_out}\n"
 #`echo '#{outstr}' >> #{dispatch_dir}debug.out;`
end
So, most of this is comments, but basically it gets the new log entry, which looks plenty like
update / /svn/sickofthis/!svn/vcc/default
And it gets the action and name of the repo. Then it calls the command
/path_to_repo/hooks/pre-{action_name}.tmpl
where action_name will either be "update" or "checkout-or-export". Basically, once you get apache all configured and running right with it, it'll be just like SUBVERSION WAS WRITTEN RIGHT ALL ALONG! Also, please note that although all I've included in my log is the action and repo location, you can get a lot more information out of it. If you need it, look at the tutorial that does this with php at http://www.ryandesign.com/svnhookdispatcher/



fmpzlbgh (Posted at: 13:52 09/09/09) (Flag this Comment)  

cjphfrpd xljngbtu http://cwqjaliw.com iqygrkso ymqfuiyk [URL=http://qxveodac.com]splkysun[/URL]

Authentic NFL Jerseys (Posted at: 03:42 19/08/10) (Flag this Comment)  

get apache all configured and running right with it, it'll be just like SUBVERSION WAS WRITTEN RIGHT ALL ALONG! Also, please note that although all I've included in my log is the action and repo location, you can get a lot more information out of it. If you need it, look at the tutorial that does this with php at

jerseys (Posted at: 09:33 01/09/10) (Flag this Comment)  

wholesale jerseys,wholesale jerseys

,Authentic NFL Jerseys,Authentic Jerseys

,Authentic Jerseys,jersey

,Authentic NFL Jerseys

pump shoes (Posted at: 02:47 02/09/10) (Flag this Comment)  

Woah woah WOAH! What's missing up there? How about like HALF the BATTLE!! Where did pre-checkout and post-checkout go? Where did pre/post-udate go? NOWHERE! The never existed (cruelty!!) So now you're like, crap - what do i do with my poor ruby app,



Leave a Comment:

Name:
Website (optional):
Comment:
Back To Entry List