11
votes

I would like to know how to get the list of files that are being committed on the pre-commit hook.

If that list doesn't contain a specific file in a specific path, then I want to reject the commit.

2

2 Answers

13
votes

Use svnlook in a pre-commit. svnlook changed gives the changed paths of the commit. Compare this against your list. And reject it if path is found/not found. One simple example for pre-commit could be.

#!/bin/sh

REPOS="$1"
TXN="$2"
SPATH="specific/path"
FOUND=$(svnlook changed -t "$TXN" "$REPOS" | tr -d '\n' | grep -E ".*$SPATH.*")

if [ "$FOUND" != "" ]
then
    echo "Reject commit!" 1>&2 && exit 1
else 
    exit 0
fi

Here I removed newlines and grep for the interested path. If path is not found, recject the commit exit 1. User will see what you echo there.

14
votes

The hook scripts should use the svnlook command and not svn. The svnlook command can take the transaction number of the commit (if this is a pre-commit hook, you need to use the transaction number. If this is a post-commit hook, you need the revision number).

Do a svnlook -h to see all of the subcommands. Here's a list of them:

  • author - Retrieve the user ID of the committer
  • cat - Prints out a file specified
  • changed - Prints out the files and directories changed
  • date - Prints out the timestamp of the commit
  • diff - Prints out the diff of all the files
  • dirs-changed - Prints out the directories changed (
  • filesize - Prints out the filesize in bytes`
  • history - Prints out the history (more like svn log)
  • info - Prints out the information of a file
  • lock - Prints out the lock information
  • propget - Gets a particular property.
  • proplist - Lists all properties.
  • tree - Prints out the directory structure
  • uuid - Prints out the UUID of the repository
  • youngest - Prints out the last revision number.

Looks like svnlook changed is what you want.

Two very important things about svnlook:

  1. The svnlook command cannot change any data, just display it. Some people look to see how you can change a property value with svnlook. Answer, you can't.
  2. The svnlook takes the Repository Directory Location as an argument, and not the URL of the repository. This means svnlook can only run on the server itself.