Wednesday, December 28, 2011

Subversion Hook script

Hook Script is a program which is triggered by specified repository even. There are two type of hook scripts server side hook script and the client side hook script the server side hook script is more powerfull then the client side hook script as it has the capability of altering the other repository present on the server.


What a hook script can do:

  1. Can send out the mail notification for the changes performed on the repository (post commit)
  2. Can impose some pre-condition before committing some changes like length of the svn comment
  3. Can impose file size restriction so that one should not be able to checkin files bigger then the limited size

Where the hook script is located:

When a repository is created a hook sub-directory is created under the repository folder and there by default following hook script template will be there in the hooks folder:



























Type of hook scripts:

  1. Start-commit: This script controls the action which should be taken before the commit start its generally used for checking whether a person who is doing the commit has proper privileged/authorization of doing the commit so its kind of checking access control before doing the commit.
  2. Pre-Commit:  The pre-commit transaction is just executed before  a commit gets completed and the commit is while getting prompted to new revision like checking proper bug id has been supplied in log message or the log message should not be empty. if it returns a non zero value the commit is aborted.
  3. Post-Commit: The post commit transaction is completed when the commit operation is completed and a new revision is created its generally for email notification,build trigger,bug tracker update
  4. Pre-Lock: The Pre-lock event occurs when some one tried to lock the subversion repository path 
  5. Post-Lock: Notification of path lock
  6. Pre-unlock — Notification of a path unlock attempt.
  7. Post-Unlock - Notification is sent when path unlock is successful



Example of pre-commit to check the log message length 


Pre-Commit: 
Below is the example of pre-commit script which will check that the committer is entering something in log message and will also check its length.



create a file name pre-commit.bat and past the below content in the pre-commit.bat 


@echo off
 :: Stops commits that don't include a log message of at least 6 characters.
 @echo off  


 setlocal  


 rem Subversion sends through the repository path and transaction id
 set REPOS=%1
 set TXN=%2           


 svnlook log %REPOS% -t %TXN% | findstr .......... > nul
 if %errorlevel% gtr 0 (goto err) else exit 0  


 :err
 echo --------------------------------------------------------------------------- 1>&2
 echo Your commit has been blocked because it didn't include a log message or its less than 10 character. 1>&2
 echo Do the commit again, this time with a log message that describes your changes. 1>&2
 echo --------------------------------------------------------------------------- 1>&2
 exit 1



and save it under the hooks folder next time if the committer doesn't enter log message or enter error message less than 10 characters it should throw the below log message.















Send Email Notifications with a Post-Commit Hook (linux)

You can use Subversion’s post-commit hook to execute a script after a complete commit has taken place. In this example, you can send a notification email after each commit to your Subversion repository.
To create a post-commit hook to send an email notification:
  1. Create the following script as a file named post-commit in the ~/webapps/svn_app/hooksdirectory such that
    • from_addr is the address from which the notifications will be sent;
    • dest_addrN are the destination addresses for the notification email;
    • mailbox_name is the name of the WebFaction mailbox which will be used to send the email;
    • mailbox_password is the password for the WebFaction mailbox which will be used to send the email.
    #!/usr/local/bin/python2.5
    
    # Settings
    mail_server = 'smtp.webfaction.com'
    from_email = 'from_addr'
    to_email = ['dest_addr1', 'dest_addr2']
    mailbox_name = 'mailbox_name'
    mailbox_password = 'mailbox_password'
    
    import os
    import smtplib
    import sys
    
    server = smtplib.SMTP(mail_server)
    server.login(mailbox_name, mailbox_password)
    
    repository, revision = sys.argv[1:]
    
    # Get the diff from svnlook
    cmd = "svnlook diff %(repository)s -r %(revision)s" % locals()
    diff = os.popen(cmd).read()
    
    msg = (
    "To: %(to_email)s\r\n"
    "From: %(from_email)s\r\n"
    "Subject: Subversion post-commit: r%(revision)s\r\n"
    "Content-type: text/plain\r\n"
    "\r\n"
    "Repository: %(repository)s\r\n"
    "Revision: %(revision)s\r\n"
    "Diff:\r\n"
    "%(diff)s\r\n"
    )
    
    msg = msg % locals()
    
    server.sendmail(from_email, to_email, msg)
    server.quit()
    
  2. Open an SSH session.
  3. Switch to the ~/webapps/svn_app/hooks directory.
  4. Enter chmod ogu+x post-commit and press Enter.
Now, every time a new change is committed to the repository, an email will be sent with a diff of the changes committed to the list of recipients.