[svn.haxx.se] · SVN Dev · SVN Users · SVN Org · TSVN Dev · TSVN Users · Subclipse Dev · Subclipse Users · this month's index

Re: Copying hooks automatically upon repository creation

From: Martin J. Stumpf <mjs_at_jhu.edu>
Date: Mon, 04 Oct 2010 15:21:43 -0400

  On 10/4/2010 2:42 PM, Ryan Schmidt wrote:
> On Oct 4, 2010, at 12:51, Tech Geek wrote:
>
>> We use one repository per project. All repository lives in /var/lib/svn/.
>>
>> Also we use the hooks post-commit and pre-commit for every repository. Right now we have to manually copy these two hooks whenever a new repository is created.
>> For example:
>> cd /var/lib/svn/
>> cp /path-to-my-hooks/* projectA/hooks
>> cp /path-to-my-hooks/* projectB/hooks
>> cp /path-to-my-hooks/* projectC/hooks
>>
>> Is there any way to automate this process?
> As Andy said, you can write a script that creates the repository and then copies the scripts, then make sure you call that script instead of calling "svnadmin create" directly whenever you want a new repo. My version of this script creates the repository as the Apache user and symlinks in my common hooks and conf directories. I symlink instead of copying because I want all my repositories to always have the same hooks and conf files, and I want changes I make in them to be reflected in existing repositories. Here's my script:
>
>
> #!/bin/bash
>
> REPO="/path/to/subversion/repositories/$1"
> USER="www"
>
> if [ -e "$REPO" ]; then
> echo "Repository \"$1\" already exists." 1>&2
> exit 1
> fi
>
> sudo -u "$USER" svnadmin create "$REPO" || exit $?
> sudo -u "$USER" rm -rf "$REPO"/{conf,hooks} || exit $?
> sudo -u "$USER" ln -s ../../conf "$REPO" || exit $?
> sudo -u "$USER" ln -s ../../hooks "$REPO" || exit $?
>
>
>
I also use a bash script replacement for svnadmin create and use a common hooks directory.
This script will set the file permissions and make the hook symlinks even for a repository that was created by the svnadmin create command. We use one project/one repo and this makes repo creation a snap.

HTH,

-Martin

#!/bin/bash
#
# svncreate - Create an svn repository and set it up with the correct permissions
# The -n switch is used to change the permissions of an existing
# repository in the event that we did not create it.
#
# $Id: svncreate 60 2009-11-19 21:00:02Z mjs $
#

# Defaults

REPOS=/mip/svn # This is the parent path to the project repositories.
HOOKS=/mip/svn/.hooks # The pre, post commit hook scripts.
TYPE=fsfs # The type of the repo, bdb or fsfs.

NOCREATE=

function usage()
{
         echo "Usage: svncreate [-n] [-r REPOPATH] <repo name> [repo name] ..."
         echo "Where: -n means don't create the repo, just change permissions and add hooks."
         echo "Where: -r REPOPATH specifies the repository parent path. (Default=/mip/svn)"
}

function set_permissions()
{
         if [ $EUID -eq 0 ];then
                         chown -R root ${REPOS}/${PROJECT} || return $?
         fi
         mkdir ${REPOS}/${PROJECT}/dav
         chgrp -R svn ${REPOS}/${PROJECT} || return $?
         chmod 6770 ${REPOS}/${PROJECT} || return $?
         dirs=$(find ${REPOS}/${PROJECT} -type d)
         for d in $dirs; do
                 chmod 6770 $d || return $?
         done
         files=$(find ${REPOS}/${PROJECT} -type f)
         for f in $files; do
                 chmod 660 $f || return $?
         done
}

function setup_hooks()
{
         ln -s ${HOOKS}/pre-commit ${REPOS}/${PROJECT}/hooks 2>/dev/null
         ln -s ${HOOKS}/post-commit ${REPOS}/${PROJECT}/hooks 2>/dev/null
         ln -s ${HOOKS}/pre-revprop-change ${REPOS}/${PROJECT}/hooks 2>/dev/null
}

# Get the command line args

while getopts "nr:" opt; do
         case $opt in
                 n)NOCREATE=1;;
                 r)REPOS="$OPTARG";;
                 \?)usage;exit;;
         esac
done

shift $(($OPTIND - 1))

if [ $# -lt 1 ]; then
         usage
         exit
fi

for PROJECT in "$@"; do
# We need write access to the REPOS directory
         [ -d "$REPOS" -a -w "$REPOS" ] || { echo "No write permission to $REPOS"; exit; }

         if [ -z $NOCREATE ]; then
                 if svnadmin create --fs-type ${TYPE} ${REPOS}/${PROJECT} && set_permissions; then
                         setup_hooks
                         echo ""
                         echo "The Project repository: ${REPOS}/${PROJECT} was created."
                         echo ""
                         echo "Use svn import -m "Initial Import" https://svnserver/<path to repo> /path/to/files to populate it."
                         echo ""
                         echo "Note: If you browse the repo and don't like what you imported"
                         echo "then just rm -rf the project ${REPOS}/${PROJECT} and start over."
                 else
                         echo "Failed to create project repository: ${REPOS}/${PROJECT}"
                         break
                 fi
         else
                 if set_permissions; then
                         setup_hooks
                         echo ""
                         echo "Permissions on the project repository: ${REPOS}/${PROJECT} were changed."
                         echo ""
                 else
                         echo "Failed to change permissions on project repository: ${REPOS}/${PROJECT}"
                         break
                 fi
         fi
done
Received on 2010-10-04 21:21:38 CEST

This is an archived mail posted to the Subversion Users mailing list.

This site is subject to the Apache Privacy Policy and the Apache Public Forum Archive Policy.