Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 11-20 of 22 total

Ruby subversion pre-commit hook to prevent conflicting Rails migrations

This is a subversion pre-commit hook that prevents a Ruby on Rails migration being committed that has the same version as an existing migration. To install you place this in a file called pre-commit in the hooks directory of your subversion repository. You can read about hooks here: http://svnbook.red-bean.com/en/1.0/ch05s02.html

#!/usr/bin/env ruby

repo_path = ARGV[0]
transaction = ARGV[1]
svnlook = '/usr/bin/svnlook'

commit_dirs_changed = `#{svnlook} dirs-changed #{repo_path} -t #{transaction}`
commit_changed = `#{svnlook} changed #{repo_path} -t #{transaction}`
#commit_author = `#{svnlook} author #{repo_path} -t #{transaction}`.chop
commit_log = `#{svnlook} log #{repo_path} -t #{transaction}`
#commit_diff = `#{svnlook} diff #{repo_path} -t #{transaction}`
#commit_date = `#{svnlook} date #{repo_path} -t #{transaction}`

# ******* Migration check ********
# if this is a migration then check that there is not already a migration with the same version number in the repository
files = commit_changed.split(/\n/)
current_migrations = nil
for file in files
  if(file =~ /A\s*(.*?\/migrate\/)(\d+)(.*)/)
    migration_path = $1
    migration_version = $2
    
    if(current_migrations == nil)
      current_migrations = {}
      migration_files = `#{svnlook} tree #{repo_path} #{migration_path}`
      for migration in migration_files
        current_migrations[$1] = true if(migration =~ /\s*(\d+)_(.*)/)
      end
    end
    
    if(current_migrations[migration_version])
     STDERR.puts("The is a pre-existing migration with version #{migration_version} in #{migration_path}")
     exit(1)
    end
  end
end


SVN revision from shell

I use this to set the subject of an email that notifies my team of a Capistrano deploy. The part of interest is how it gets the subversion revision number with a few piped shell commands.

echo "Subject: [rake deploy] Deployed revision `cd /home/www/current ; svn info | grep Revision | sed "s/Revision: //"` on `hostname` at `date`" > ~/sendmail_tmp

backup subversion repository over email

#!/bin/bash
#
# $Id: repodiff 3 2006-09-21 18:48:39Z sevkin $
#
# subversion repository incremental backup over e-mail
#
# (c) 2006 Vsevolod Balashov under terms of GNU GPL v.2 or later

SVNROOT=/var/svn
EMAIL=your@email.here
STORE=`mktemp -d`
GPGCRYPT=n

for REPO in `ls $SVNROOT`; do 
	REPOPATH=$SVNROOT/$REPO;
	if [ -r $REPOPATH/youngest ]; then
		LATEST=`cat $REPOPATH/youngest`
		YOUNGEST=`svnlook youngest $REPOPATH`
		if [ $LATEST -lt $YOUNGEST ]; then
			svnadmin dump $REPOPATH --incremental -r $LATEST:$YOUNGEST >$STORE/$REPO 2>/dev/null
		fi
	else
		svnadmin dump $REPOPATH --incremental >$STORE/$REPO 2>/dev/null
	fi 
	echo $YOUNGEST >$REPOPATH/youngest 
done

if [ `ls $STORE | wc -w` -gt 0 ]; then
	BACKUP=repodiff_`date -u +%Y%m%d%H%M%S`.tar.bz2
	ATTACH=$STORE/../$BACKUP
	tar  -C $STORE -cjf $ATTACH .
	if [ $GPGCRYPT = y ]; then
		gpg -e -r $EMAIL $ATTACH
		ATTACH=$ATTACH.gpg
	fi
	echo "." | mutt -c $EMAIL -a $ATTACH -s "repository incremental backup"
	rm -f $ATTACH
fi

rm -rf $STORE

Subversion service for low-load (personal?) sources repository

Create separate user and insert svnserve into inetd.

# useradd -g root -s /bin/false -d /dev/null -c "SubVersion Daemon" svnserve
# mkdir /var/svn
# chown -R svnserve /var/svn
# update-inetd --add 'svn\tstream\ttcp\tnowait\tsvnserve\t/usr/sbin/tcpd\t/usr/bin/svnserve --inetd --root /var/svn'


/var/svn - root of repository.
update-inetd - standart tool in debian and ubuntu linux distros

You must run svnadmin as svnserve user for manage your repository

$ sudo sudo -u svnserve svnadmin <command>


Remove all .svn folders in a directory tree

// You did a checkout when you really wanted to do an export.
// Now there are tons of .svn folders in your project, and you need them to go away.
// Shell scripting to the rescue.

// Credit: Zed Shaw, at the Mongrel mailing list.

find . -name ".svn" -exec rm -rf {} \;

Accessing Subversion repository information in Ruby

require 'yaml'
svn_info = YAML.load(`svn info /my/working/copy`)
puts svn_info['URL']
#=> "svn://some-remote-repository.com/svn"
puts svn_info['Revision']
#=> "133"


Should also work with svk info as well. Credit to htonl in #caboose for the YAML tip.

Application version number and cool codename based on subversion number

// description of your code here

#codename generated from the dictionary
REVISION_NUMBER = `svn info`.split("\n")[4][/\d+/].to_i
APP_CODENAME  = IO.readlines("/usr/share/dict/words")[REVISION_NUMBER]

deploy your rails apps on edge rails

lib/tasks/rails.rake
desc "Checks out rails"
task :init do
  ENV['SHARED_PATH']  = '../../shared' unless ENV['SHARED_PATH']
  ENV['RAILS_PATH'] ||= File.join(ENV['SHARED_PATH'], 'rails')
 
  checkout_path = File.join(ENV['RAILS_PATH'], 'trunk')
  export_path   = "#{ENV['RAILS_PATH']}/rev_#{ENV['REVISION']}"
  symlink_path  = 'vendor/rails'

  # do we need to checkout the file?
  unless File.exists?(checkout_path)
    puts 'setting up rails trunk'    
    get_framework_for checkout_path do |framework|
      system "svn co http://dev.rubyonrails.org/svn/rails/trunk/#{framework}/lib #{checkout_path}/#{framework}/lib --quiet"
    end
  end

  # do we need to export the revision?
  unless File.exists?(export_path)
    puts "setting up rails rev #{ENV['REVISION']}"
    get_framework_for export_path do |framework|
      system "svn up #{checkout_path}/#{framework}/lib -r #{ENV['REVISION']} --quiet"
      system "svn export #{checkout_path}/#{framework}/lib #{export_path}/#{framework}/lib"
    end
  end

  puts 'linking rails'
  rm_rf   symlink_path
  mkdir_p symlink_path

  get_framework_for symlink_path do |framework|
    ln_s File.expand_path("#{export_path}/#{framework}/lib"), "#{symlink_path}/#{framework}/lib"
  end
end

def get_framework_for(*paths)
  %w( railties actionpack activerecord actionmailer activesupport actionwebservice ).each do |framework|
    paths.each { |path| mkdir_p "#{path}/#{framework}" }
    yield framework
  end
end


Switchtower recipe code:
set :rails_version, 3517
desc "Checks out rails rev ##{rails_version}"
task :after_symlink do
  run <<-CMD
    cd #{current_release} &&
    rake init REVISION=#{rails_version} RAILS_PATH=/home/username/projects/rails
  CMD
end

Recursively remove CVS or Subversion files from folders

You could just swap CVS with .svn for subversion etc.

find -d . -name 'CVS' -exec rm -rf '{}' \; -print

Subversion: check in all new files with one command line

$ svn add `svn st | grep "^?" | cut -c8-`
« Newer Snippets
Older Snippets »
Showing 11-20 of 22 total