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.
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.
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> .
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.
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 committercat
- Prints out a file specifiedchanged
- Prints out the files and directories changeddate
- Prints out the timestamp of the commitdiff
- Prints out the diff of all the filesdirs-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 filelock
- Prints out the lock informationpropget
- Gets a particular property.proplist
- Lists all properties.tree
- Prints out the directory structureuuid
- Prints out the UUID of the repositoryyoungest
- Prints out the last revision number.Looks like svnlook changed
is what you want.
Two very important things about svnlook
:
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.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.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With