Why would my SVN pre-commit hook work locally, but not on commit?
I have the following pre-commit hook to use JavaScript Lint for checking JavaScript files before committing:
#!/bin/env bash
REPOS="$1"
TXN="$2"
ECHO=/bin/echo
GREP=/bin/grep
SED=/bin/sed开发者_JAVA百科
SVN=/usr/bin/svn
SVNLOOK=/usr/bin/svnlook
FILES_CHANGED=`$SVNLOOK changed -r$TXN $REPOS | $SED -e "s/^....//g"`
JSL=/usr/local/bin/jsl
JSL_CONF=/usr/local/etc/jsl.conf
for FILE in $FILES_CHANGED
do
if $ECHO $FILE | $GREP "\.js$"
then
$SVN cat -r$TXN file://$REPOS/$FILE | $JSL -conf $JSL_CONF -stdin 1>&2
JSL_ERROR_CODE=$?
if [ $JSL_ERROR_CODE != 0 ]
then
exit $JSL_ERROR_CODE
fi
fi
done
# If we got here, nothing is wrong.
exit 0
This code works locally as follows: ./pre-commit /my/svn/repo/location 6781 # the number is the transaction number
BUT it doesn't error correctly on svn commit.
I have already accounted for:
- There being no $PATH, I explicitly set all command paths.
- I am catching the correct error code from the jsl command for exit.
- I am pushing STDOUT to STDERR for the jsl command so it will be displayed in the commit fail.
What am I missing?
Yours,
TrevorIt is possible that one of the programs you're running expect some environment variables to be set.
From Repository Creation and Configuration:
For security reasons, the Subversion repository executes hook scripts with an empty environment—that is, no environment variables are set at all, not even $PATH or %PATH%. Because of this, a lot of administrators are baffled when their hook script runs fine by hand, but doesn't work when run by Subversion. Be sure to explicitly set environment variables in your hook and/or use absolute paths to programs.
Try to execute them locally without any environment variables set and see if that works.
I usually end up importing all my environment in the first line of my hook scripts:
source /home/username/.bash_profile
I discovered the answer after a long and winding road. Basically, in my script above I am using -r in my svn commands, but in a pre-commit hook you must use -t, not -r. The complete script is below:
#!/bin/sh
REPOS="$1"
TXN="$2"
ECHO=/bin/echo
GREP=/bin/grep
SED=/bin/sed
SVNLOOK=/usr/bin/svnlook
FILES_CHANGED=`$SVNLOOK changed -t$TXN $REPOS | $SED -e "s/^....//g"`
JSL=/usr/local/bin/jsl
JSL_CONF=/usr/local/etc/jsl.default.conf
for FILE in $FILES_CHANGED
do
if $ECHO $FILE | $GREP "\.js$"
then
$SVNLOOK cat -t$TXN $REPOS $FILE | $JSL -conf $JSL_CONF -stdin -nologo 1>&2
JSL_ERROR_CODE=$?
if [ $JSL_ERROR_CODE != 0 ]
then
exit $JSL_ERROR_CODE
fi
fi
done
# If we got here, nothing is wrong.
exit 0
If the error you're looking for is coming from svn
in the pipeline rather that jsl
, then $?
isn't going to contain the return code. Use ${PIPESTATUS[@]}
instead. It's an array that contains the return codes of each member of the pipeline. A quick way to check for any unspecified failure would be:
$SVN cat -r$TXN file://$REPOS/$FILE | $JSL -conf $JSL_CONF -stdin 1>&2
[[ ! ${PIPESTATUS[@]} =~ 1 ]]
JSL_ERROR_CODE=$?
if [ $JSL_ERROR_CODE != 0 ]
or
$SVN cat -r$TXN file://$REPOS/$FILE | $JSL -conf $JSL_CONF -stdin 1>&2
[[ ${PIPESTATUS[@]} != *1* ]]
JSL_ERROR_CODE=$?
if [ $JSL_ERROR_CODE != 0 ]
精彩评论