Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SVN (server - pre-commit hook): Know the list of files that are being committed

Tags:

svn

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.

like image 761
Sudhakar Chavali Avatar asked Jul 22 '13 14:07

Sudhakar Chavali


People also ask

What is pre-commit hook in SVN?

A pre-commit hook is a feature available in the Subversion version control system that allows code to be validated before it is committed to the repository. The PHP_CodeSniffer pre-commit hook allows you to check code for coding standard errors and stop the commit process if errors are found.

How do you run pre-commit hook on all files?

pre-commit will now run on every commit. Every time you clone a project using pre-commit running pre-commit install should always be the first thing you do. If you want to manually run all pre-commit hooks on a repository, run pre-commit run --all-files . To run individual hooks use pre-commit run <hook_id> .

What does pre-commit hook do?

The pre-commit hook is run first, before you even type in a commit message. It's used to inspect the snapshot that's about to be committed, to see if you've forgotten something, to make sure tests run, or to examine whatever you need to inspect in the code.


2 Answers

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.
like image 148
David W. Avatar answered Sep 30 '22 19:09

David W.


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.

like image 42
swo Avatar answered Sep 30 '22 19:09

swo