Showing posts with label svn. Show all posts
Showing posts with label svn. Show all posts

Saturday, March 15, 2008

SVN hooks on Windows with Ruby

I'd been meaning to write a pre-commit hook to catch empty log messages for a while but hadn't got around to it for one reason or another. I had a spare 5 minutes so I got stuck in.

The SVN book mentions that on Windows the hook files extension basically has to be .exe, .com or .bat. Even though .rb is executable on my SVN server it was not being called. So, instead I just called the ruby script from the bat file as such:

pre-commit.bat
1 SET DIR=%1/hooks
2 set REPOS="%1"
3 set TXN="%2"
4
5 c:\ruby\bin\ruby.exe d:\svn\hooks\pre-commit.rb %REPOS% %TXN% %DIR%

It also mentions that for security reasons, the repository executes hook scripts with an empty environment—that is, no environment variables are set at all, not even $PATH or %PATH%. Because of this, it is necessary to specify absolute paths.

Setting DIR above is a bit of a trick for debugging purposes so you can write a log file into the hooks directory to check things out. The ruby script is as follows:

pre-commit.rb
 1 REPOS, TXN, DIR = ARGV[0], ARGV[1], ARGV[2]
 2 SVN_LOOK = "C:/Program Files/Subversion/bin/svnlook.exe"
 3
 4 log_msg = %x["#{SVN_LOOK}" log -t #{TXN} #{REPOS}].chomp.strip
 5
 6 if (log_msg.nil? || log_msg.size < 5)
 7   err = <<-EOE
 8     This commit has failed due to the absence of a meaningful log message.
 9     Please provide a message describing why you changed what you did and
10     then try committing again. Ta -- Dan
11   EOE
12
13   STDERR.puts err
14   exit(1)
15 end

Yeah, I'm a bit of a bastard setting the minimum length of the log message to 5 chars. The only tricky bit of the entire process was realizing that I needed to chomp a new line off the log message. That's where using DIR and writing to a log file helped out.

Tally, ho!

Sunday, December 9, 2007

Sventon for my homies

I'm a big fan of sventon, the guys have created one of the kickingest svn repository viewers around.

Much to my dismay after an upgrade to 1.3.0, stuff stopped working. I didn't have time to look at it until recently but I dived into the tomcat logs and pulled up this.

org.springframework.beans.MethodInvocationException: Property 'archiveFileCharset' threw exception; nested exception is java.nio.charset.UnsupportedCharsetException: cp437


Not so cool.

cp437 is the old dos-us character set which sure isn't installed on our svn box, hence the error.

So if this saves anyone else some time, go into the WEB-INF directory of sventon and edit sventon-servlet.xml. There is a property called archiveFileCharset. Change the value to whatever is appropriate for you. In my case it was cp1252 which is en-au amongst others, but should be good enough for most engrish speakers.

I thought Unicode had pretty much made Windows Code Pages obsolete, oh well.

Friday, August 17, 2007

Automating SVN stuff with Ruby

Ahoy hoy.

The other day at work I was looking at upgrading an SVN installation at an off-site and came across a good 70 odd repositories dumped to backup. Rather than take the typical windows muppet route and unzip each one, create an svn directory and import them manually, I scripted the whole thing.

Ruby lends itself to this sort of work. I've used it in the past to automate daily and weekly backups of another repository so I knew it would be pretty easy to sort this out. The best part is it didn't take more than 15 minutes from go to whoah. Here it is in it's entirety, it's not a shining example of best practice, nor does it profess to be, but it does what it's supposed to do.



 1 ################################################################################
 2 #
 3 # SVN importer
 4 #
 5 # rubyzip must be installed: gem install rubyzip
 6 #
 7 ################################################################################
 8
 9 require 'zip/zipfilesystem'
10
11 SVN_REPOS_URL = "http://paleale:8090/svn-repos/"
12 SVN_REPOS_PATH = "C:/svn-repos"
13 BACKUP_DIR = "//sparkling/SVNbackups/"
14 UNZIP_DIR = "C:/Documents and Settings/dan/My Documents/"
15
16 # unzip all the dump files into the UNZIP_DIR
17 # we make the assumption that the dump file inside the zip shares the same name
18 # i.e. web_indicator.dump.zip has web.indicator.dump inside it
19 Dir["#{BACKUP_DIR}*/*.zip"].each do |zipFile|
20   dumpFile = zipFile.gsub(/^.*\//,'').gsub(/\.zip/,'')
21   Zip::ZipFile.open(zipFile) do |unZip|  
22     unZip.extract(dumpFile, UNZIP_DIR + dumpFile)
23   end 
24 end  
25
26 # create svn directories for each dumpFile, load into SVN then delete the file
27 Dir.glob("#{UNZIP_DIR}*.dump").each do |dumpFile|
28   projName = dumpFile.gsub(/^.*\//,'').gsub(/\.dump/,'')
29   cmd = %{svn mkdir -m "automated: creating project structure from import" } 
30   cmd << SVN_REPOS_URL << projName
31   %x"#{cmd}"
32   cmd = "svnadmin load --parent-dir #{projName} #{SVN_REPOS_PATH}"
33   cmd << %{ < "#{dumpFile}"}
34   %x"#{cmd}"  
35   File.delete(dumpFile)
36 end