admin管理员组

文章数量:1427294

I have the following pre-mit hook to use JavaScript Lint for checking JavaScript files before mitting:

#!/bin/env bash

REPOS="$1"
TXN="$2"

ECHO=/bin/echo
GREP=/bin/grep
SED=/bin/sed

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-mit /my/svn/repo/location 6781 # the number is the transaction number

BUT it doesn't error correctly on svn mit.

I have already accounted for:

  • There being no $PATH, I explicitly set all mand paths.
  • I am catching the correct error code from the jsl mand for exit.
  • I am pushing STDOUT to STDERR for the jsl mand so it will be displayed in the mit fail.

What am I missing?

Yours,
Trevor

I have the following pre-mit hook to use JavaScript Lint for checking JavaScript files before mitting:

#!/bin/env bash

REPOS="$1"
TXN="$2"

ECHO=/bin/echo
GREP=/bin/grep
SED=/bin/sed

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-mit /my/svn/repo/location 6781 # the number is the transaction number

BUT it doesn't error correctly on svn mit.

I have already accounted for:

  • There being no $PATH, I explicitly set all mand paths.
  • I am catching the correct error code from the jsl mand for exit.
  • I am pushing STDOUT to STDERR for the jsl mand so it will be displayed in the mit fail.

What am I missing?

Yours,
Trevor

Share Improve this question asked Dec 2, 2010 at 21:37 user285879user285879
Add a ment  | 

3 Answers 3

Reset to default 3

It 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 mands, but in a pre-mit hook you must use -t, not -r. The plete 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 ing 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 ]

本文标签: javascriptWhy would my SVN precommit hook work locallybut not on commitStack Overflow