SVN

Ruby 1.9.1: Hash

In ruby 1.9.1 has many changes for Hash some useful changes are below:

RUBY_VERSION => 1.8.6

RUBY_VERSION => 1.9.1

>> {'name', "Akhil"}
=> syntax error, unexpected ',', expecting tASSOC

>> {name: "Akhil"}
=> {:name=>"Akhil"}

Now Hash preserves order:

RUBY_VERSION => 1.8.6

>> hash = {:a=> 'A', :b=>'B', :c=>'C', :d=>'D'}
=> {:b=>"B", :c=>"C", :d=>"D", :a=>"A"}
>> hash.to_a
=> [[:b, "B"], [:c, "C"], [:d, "D"], [:a, "A"]]
>> hash.keys
=> [:b, :c, :d, :a]
>> hash.values
=> ["B", "C", "D", "A"]

RUBY_VERSION => 1.9.1

>> hash = {:a=> 'A', :b=>'B', :c=>'C', :d=>'D'}
=> {:a=>"A", :b=>"B", :c=>"C", :d=>"D"}
>> hash.to_a
=> [[:a, "A"], [:b, "B"], [:c, "C"], [:d, "D"]]
>> hash.keys
=> [:a, :b, :c, :d]
>> hash.values
=> ["A", "B", "C", "D"]

Better to_s method(similar to hash.inspect).

RUBY_VERSION => 1.8.6

>> hash = {:a=> 1, :b=>2, :c=>3, :d=>4}
=> {:b=>2, :c=>3, :d=>4, :a=>1}
>> hash.to_s
=> "b2c3d4a1"

RUBY_VERSION => 1.9.1

>> hash = {:a=> 1, :b=>2, :c=>3, :d=>4}
=> {:a=>1, :b=>2, :c=>3, :d=>4}
>> hash.to_s
=> "{:a=>1, :b=>2, :c=>3, :d=>4}"

hash.each and hash.each_pair

RUBY_VERSION => 1.8.6

>> hash = {:a=> 1, :b=>2, :c=>3, :d=>4}
=> {:b=>2, :c=>3, :d=>4, :a=>1}
>> hash.each{|x| p x}
[:b, 2]
[:c, 3]
[:d, 4]
[:a, 1]
=> {:b=>2, :c=>3, :d=>4, :a=>1}
>> hash.each_pair{|x| p x}
(irb):48: warning: multiple values for a block parameter (2 for 1)
        from (irb):48
[:b, 2]
(irb):48: warning: multiple values for a block parameter (2 for 1)
        from (irb):48
[:c, 3]
(irb):48: warning: multiple values for a block parameter (2 for 1)
        from (irb):48
[:d, 4]
(irb):48: warning: multiple values for a block parameter (2 for 1)
        from (irb):48
[:a, 1]
=> {:b=>2, :c=>3, :d=>4, :a=>1}

RUBY_VERSION => 1.9.1

>> hash = {:a=> 1, :b=>2, :c=>3, :d=>4}
=> {:a=>1, :b=>2, :c=>3, :d=>4}
>> hash.each{|x| p x}
[:a, 1]
[:b, 2]
[:c, 3]
[:d, 4]
=> {:a=>1, :b=>2, :c=>3, :d=>4}
>> hash.each_pair{|x| p x}
[:a, 1]
[:b, 2]
[:c, 3]
[:d, 4]
=> {:a=>1, :b=>2, :c=>3, :d=>4}

hash.select now returns hash instead of array

RUBY_VERSION => 1.8.6

>> hash = {:a=> 1, :b=>2, :c=>3, :d=>4}
=> {:b=>2, :c=>3, :d=>4, :a=>1}
>> hash.select{|k,v| k == :c }
=> [[:c, 3]]

RUBY_VERSION => 1.9.1

>> hash = {:a=> 1, :b=>2, :c=>3, :d=>4}
=> {:a=>1, :b=>2, :c=>3, :d=>4}
>>  hash.select{|k,v| k == :c }
=> {:c=>3}

Ruby Script for SVN commit notification with log message, list of updated files and readable colored SVN Diff

Some days ago I wrote a post about “SVN commit notification” which uses a perl script for sending commit notification with svn diff by mail. In this mail you can find svn diff from the last committed revision. I used to love this mail, soon I realized that it is a bit ugly and difficult to read. Also there were some important information missing. Like the name of user committing the code, the log message etc…

And then I started writing my own ruby script for same purpose but with some addition and modification. Commit Notification Script is that script, you can download and configure it with your SVN post commit hook as follows.

Add following line at the bottom of your post-commit file:

/usr/bin/ruby /var/www/repositories/project/hooks/commit-email.rb "$1"  "$2"

* Please remember to change the path of you commit-email ruby script.

Now open commit-email ruby file and modify the following section according to your requirement:

# You can edit below

# Subject prefix
sub = "[test_project@vinsol]"  # A project 'test_project' is maitained at vinsol

#list of users who will recieve commit notification
recipients = ["bansalakhil30.10@gmail.com", "akhil@vinsol.com"]

# email which will appear in from email field
from_user = "svn-notify@somedomain.com"

# your smtp settings here
ActionMailer::Base.smtp_settings = { :address => 'localhost', :port => 25, :domain => 'domain.com'}

# Do not edit below this line

You are done with that, now onwards whenever someone commits the code, you’ll get the commit notification mail like:

Commit Email Preview

Get svn commit notification right into your inbox by using svn hook post-commit

If you wish you can get notification right into your inbox when a team member commits the code. This mail will contain list of files added/deleted along with the svn diff of files that are modified. This needs some extra efforts to setup, but once it is setup it is very helpful.

This can be done by using svn hooks. Yes, svn has inbuilt hook functionality like pre-commit, post-commit, post-lock, post-unlock, pre-lock, pre-unlock, start-commit. We’ll use post-commit hook to get notifications.

First of all get a perl script from here http://svn.collab.net/repos/svn/trunk/tools/hook-scripts/commit-email.pl.in and copy it to hooks directory inside your repository directory, lets say /var/www/repos/repository_name/hooks.
Make sure to rename it as commit-email.pl, ofcourse you need perl installed on your server. Now cd to hooks directory inside you repository directory. Rename post-commit.tmpl to post-commit, and chmod 755 to post-commit and commit-email.pl . Now open post-commit in you favorite editor, and put “/usr/bin/perl /var/www/repos/commit-email.pl –from svn-notify@example.com -s ‘SVN commit notification’ ‘$REPOS’ ‘$REV’ user_whom_to_notify@example.com, another_user_to_notify@example.com” at the bottom of that file. Now open commit-email.pl and make changes in configuration section according to your requirement and server, specially svnlook path.

Congrats!, You are done ;-) .

Now your server will send email notification to user_whom_to_notify@example.com, another_user_to_notify@example.com whenever someone commits.


**For security reasons, the Subversion 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, a lot of administrators are baffled when their hook script runs fine by hand,but doesn’t work when run by Subversion. Be sure to explicitly set environment variables in your hook and/or use absolute paths to programs.

It worked for me, Please let me know if it works for you too.

Is SVN really atomic?

I am just wondering if svn is really atomic. Actually some days ago I freezed rails and tried to commit but due to some network problem the commit process failed. And then I found some of those files under version control, which should not be there if snv is atomic. I tried many times to commit after svn delete those files, but stuck with the same problem. May be I misunderstood some thing about svn. Did somebody also faced same problem?

Ruby script for creating new rails project and initial SVN import (with ignoring/removing log/other files)

Some days go I wrote a bash script for creating new rails project and initial SVN import (with ignoring/removing log/other files). And I received many comments if I could write a ruby script for this stuff. Hence here is the ruby script for creating new rails project and initial SVN import (with ignoring/removing log/other files). Copy this script to any of your directory as “create_rails_with_subversion.rb” and execute by issuing “ruby create_rails_with_subversion.rb “
You may need command line subversion for windows.

I have tested it at my windows and linux machine, if it not work for you please let me know.

Also I love to have your comments…. :)

Bash script for creating new rails project and initial SVN import (with ignoring/removing log/other files)

Many times we need to create a new rails project and import it to SVN repository. Each time we have to repeat same commands for creating new rails project, Initial import to SVN, Ignoring *.log files, move database.yml to database.example, ignoring tmp/sessions etc.. etc..

So I have written a bash script that do all this stuff, all you need to do is copy this Bash script (for creating new rails project and initial SVN import with ignoring/removing log/other files) to any of your directory with name “create_rails_with_subversion” and execute this script by issuing ./create_rails_with_subversion & follow screen instructions.

This will:

  • create a new rails project
  • initial import to SVN
  • remove and ignore all log files from svn
  • remove and ignore sessions, cache sockets files from tmp dir
  • move database.yml to database.example
  • ignore database.yml

I also love to have your feedbacks…

UPDATE: I have modified this script, now it doesn’t remove tmp dir, it just ignore content in tmp/sessions, tmp/sockets and tmp/caches :)

UPDATE2: Script again modified , now it also freeze rails (you have to specify version).

Installing subversion from pre-compiled packages or source code

What is subversion(svn):

Subversion is an open source version control system used for keep track of change to source code. Subversion manages files and directories over time an increments revision made to the file system. A tree of file is placed into a central repository. The repository is much like an ordinary file server, except that it remembers every change ever made to your files and directories. This allows you to recover older version of your date or examine the history of how your data changed.

Subversion can access its repository across networks, which allows it to be used by people on different computers. At some level, the ability for various people to modify and manage the same set of data from their respective locations fosters collaboration.

Installation:

There are two types of installation:

  1. Installation from pre-compiled packages
  2. Installation from source code

Installation from packages:

For RedHat Linux(or other Linux distributions which supports RPM) download subversion RPM package and install. See manual of rpm utility for installation.

For Debian Linux issue apt-get install subversion and by confirming the installation you are done. I also recommend to install subversion-tools by issuing apt-get install subversion-tools.

Installation from source:

Installation from source code is slightly tricky. First of all download latest version of subversion code from http://subversion.tigris.org.

Now extract source to your preferred directory (lets assume /home/testuser/svncode/), and issue the following command in sequence after switching to svncode directory:

  • ./configure
  • make
  • make install

./configure will create Makefile
make makes Subversion form the just-created Makefile
make install install Subversion if make is successfullyAnd you are done….

Issue svnadmin create /home/testuser/repository to create a new repository, this should create a directory structure like:

drwxrwxrwx   7 root root 4096 2006-11-07 17:18 .
drwxrwxrwx  14 root root 4096 2006-11-07 17:18 ..
drwxrwxrwx   2 root root 4096 2006-11-07 17:18 con
drwxrwxrwx   2 root root 4096 2006-11-07 17:18 dav
drwxrwxrwx   2 root root 4096 2006-11-07 17:18 db
-rwxrwxrwx   1 root root    2 2006-11-07 17:18 format
drwxrwxrwx   2 root root 4096 2006-11-07 17:18 hooks
drwxrwxrwx   2 root root 4096 2006-11-07 17:18 locks
-rwxrwxrwx   1 root root  379 2006-11-07 17:18 README.txt

Now we need to allow remote access to it so that users can import their files. By editing conf/svnserve.conf we can set user accounts, permissions.

Initially svnserve.conf looks like:

### This file controls the configuration of the svnserve daemon, if you
### use it to allow access to this repository.  (If you only allow
### access through http: and/or file: URLs, then this file is
### irrelevant.)
### Visit http://subversion.tigris.org/ for more information.
### [general]
### These options control access to the repository for unauthenticated
### and authenticated users.  Valid values are "write", "read",
### and "none".  The sample settings below are the defaults.
### anon-access = write
### auth-access = write
### The password-db option controls the location of the password
### database file.  Unless you specify a path starting with a /,
### the file's location is relative to the conf directory.
### The format of the password database is similar to this file.
### It contains one section labelled [users]. The name and
### password for each user follow, one account per line. The
### format is
### USERNAME = PASSWORD
### Please note that both the user name and password are case
### sensitive. There is no default for the password file.
### password-db = passwd
### This option specifies the authentication realm of the repository.
### If two repositories have the same authentication realm, they should
### have the same password database, and vice versa.  The default realm
### is repository's uuid.
### realm = My First Repository

Uncomment line containing [general], anon-access, auth-access and password-db with suitable permissions. Also add file named passwd parallel to svnserve.conf containing

[users]SVNUSERNAME = SVNUSERPASSOWRD

You can add as many users you want. Now you are done with user accounts and permissions.

Next you have to start the svnserver by issuing svnserve -d -r /home/testuser/repository.

Now you are ready with your subversion server, you can access your repository from svn://yourdomain.com/repository/

On some systems (like Debian GNU/Linux) this requires to write an init script so that the server starts up every time the system is rebooted. This could be done by creating a file called svnserve in the /etc/init.d directory, and then editing it to look like the following example file:

#! /bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/svnserve NAME=svnserve DESC="SVN Repository Server Daemon"
test -x $DAEMON || exit 0
OPTIONS="-d -r /cvs/src"
# Get lsb functions
#. /lib/lsb/init-functions
. /etc/default/rcS start() {        echo "Starting $DESC... "
#       echo "Starting $DESC: "
if ! start-stop-daemon --start --quiet --oknodo --exec $DAEMON -- $OPTIONS
>/dev/null 2>&1; then
status=$?
echo $status
return $status
fi        log_end_msg 0
return 0
}
case "$1" in
start)
start
;;
stop)
echo "Stopping $DESC: "
start-stop-daemon --stop --quiet --oknodo --exec $DAEMON
echo $?
;;
restart|force-reload)
$0 stop
sleep 1
start
#echo "$NAME."
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart|force-reload}" >&2
exit 1
;;
esac exit 0

This script also allows to manually start and stop the svnserver service whenever needed. Note: be sure and change the OPTIONS line to reflect the location of your repository; in our previous example this location was /home/testuser/repository. Once this file is created and edited to look like the previous example, you need to chmod the script to be executable by issuing chmod +x /etc/init.d/svnserve and also set it to startup at boot time by issuing update-rc.d svnserve defaults

Now simply start/stop the service by issuing:

/etc/init.d/svnserve start/stop

Please refer Subversion book for more help and usage.