Brian's Waste of Time

Mon, 14 Mar 2005

More Fun Little Scripts

What the heck, another useful little script. This one is called svnrepo which does the simple thing of providing convenient svn repository munging:

#!/usr/bin/env ruby

entries = File.open("#{ENV['PWD']}/.svn/entries")
begin
  escape = callcc do |escape|
    entries.each_line do |line|
      if line =~ /\s+url="(.+)"/
        repo = $1
        if ARGV.length > 0
          arg = ARGV[0]
          arg = arg[1, arg.length].to_i
          repo = repo.reverse
          arg.times do |i| 
            repo = repo[repo.index("/") + 1 , repo.length]
          end
          puts repo.reverse
        else
          puts repo
        end
        escape.call
      end
    end
  end
rescue
  puts "You backed up too far into the repo string"
  exit -1
ensure
  entries.close
end

The reason for this instead of just svn info | grep URL | cut -f 2 -d ' ' is backing up directories in the svn repo (svnrepo -1 or svnrepo -2 to back up one directory, or two, respectively):

brianm@kite:~/src/jdbi$ svnrepo
svn+ssh://jdbi.codehaus.org/home/projects/jdbi/scm/trunk
brianm@kite:~/src/jdbi$ svnrepo -2
svn+ssh://jdbi.codehaus.org/home/projects/jdbi
brianm@kite:~/src/jdbi$ 

so you can do niceties like the tag-release.sh script:

svn cp $(svnrepo) $(svnrepo -1)/tags/release-$(date +%Y-%m-%d)

It also has a fun gratuitous use of a continuation as a premature optimization ;-)

1 writebacks [/src/ruby] permanent link