SVN Tip: Uniform Hook Script Wrappers

If you have a collection of SVN repositories with common hook scripts, it makes sense to have a single copy of them in a separate directory (say _common_/) alongside the repository directories.

The hook files can then just be ‘wrappers’ that call out to the real scripts. To make things really easy, there’s no reason why those wrappers couldn’t be identical.

For Windows, here’s such a sample batch wrapper script, assuming the real script is written in Perl. You will need to adjust it to work with your preferred layout and the locations of your tools.

@echo off
setlocal
cd "%~0/../../../_common_/hooks" 2>nul || ( echo ERROR: can't cd to ^<../../_common_/hooks/^> 1>&2 & exit /b 1 )
call "subroutine/prepare_environment.bat" 2>nul || ( echo ERROR: can't call ^ 1>&2 & exit /b 1 )
if not exist "%~n0.pl" ( echo ERROR: can't do perl ^<%~n0.pl^> 1>&2 & exit /b 1 )
perl "%~n0.pl" "%~1" "%~2" "%~3" "%~4" "%~5" "%~6" "%~7" "%~8" "%~9"
if errorlevel 1 exit 1
exit 0

Note that in the cd command, one of the .. terms is to step up past the filename in %0. An alternative to “%~0/..” would be “%~dp0”.

For Linux, here’s a corresponding Bash wrapper script.

#!/bin/bash
selfbase=${0%.*}; selfbase=${selfbase##*/}
cd "%~0/../../../_common_/hooks" 2> /dev/null || { echo "ERROR: can't cd to <../../_common_/hooks/>" 1>&2 ; exit 1 ; }
. "subroutine/prepare_environment.sh" 2> /dev/null || { echo "ERROR: can't source " 1>&2 ; exit 1 ; }
if [ ! -e "${selfbase}.pl" ]
then
  echo "ERROR: can't do perl <${selfbase}.pl>" 1>&2
  exit 1
fi
perl "${selfbase}.pl" "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" || { echo "ERROR: can't perl <${selfbase}.pl>" 1>&2 ; exit 1 ; }

The prepare_environment scripts would need to ensure that the locations of Perl and any other tools used are on the PATH.

These wrappers pass more parameters than the hooks take at present; there’s no harm in doing so, and the wrappers need no adjustment if the SVN hooks are extended a little in future.

Advertisements

2 Responses to “SVN Tip: Uniform Hook Script Wrappers”

  1. Sales Ultimately Profitshow Says:

    Howdy just wanted to give you a quick heads up. The text in your article seem to be running off the screen in Firefox.
    I’m not sure if this is a formatting issue or something to do with browser compatibility but I thought I’d post to let you know.
    The design look great though! Hope you get the issue resolved soon.

    Many thanks

    • Rob Says:

      Thanks for the note.

      I just use the default free WordPress setup.
      The text does look a bit daft, but it can be copy-and-pasted.

      It’s possible WordPress supports horizontally-scrolling text boxes, but I’ve not investigated that yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: