1

I'm trying to get a pre-commit hook script to work on our old SVN box. It's very old, running Ubuntu Server 8.04.

This script: @echo off :: :: Stops commits that have empty log messages. ::

@echo off

setlocal

rem Subversion sends through the path to the repository and transaction id
set REPOS=%1
set TXN=%2

rem check for an empty log message
svnlook log %REPOS% -t %TXN% | findstr . > nul
if %errorlevel% gtr 0 (goto err) else exit 0

:err
echo. 1>&2
echo Your commit has been blocked because you didn't give any log message 1>&2
echo Please write a log message describing the purpose of your changes and 1>&2
echo then try committing again. -- Thank you 1>&2
exit 1

I think it's not working because the command findstr does not exist. What does work is this:

if [[ -n "" ]] ; then echo "yes"; else echo "no"; fi

So I changed the script to:

@echo off
::
:: Stops commits that have empty log messages.
::

@echo off

setlocal

rem Subversion sends through the path to the repository and transaction id
set REPOS=%1
set TXN=%2

rem check for an empty log message
::svnlook log %REPOS% -t %TXN% | findstr . > nul
::if %errorlevel% gtr 0 (goto err) else (goto exitgood)

::svnlook log %REPOS% -t %TXN% | findstr . > ""
::if %errorlevel% gtr 0 (goto err) else (goto exitgood)

SET LOG=`svnlook log %REPOS% -t %TXN%`

if [[ -n %LOG%  ]]; then
        (goto exitgood)
else
        (goto err)
fi

:err
echo. 1>&2
echo Your commit has been blocked because you didn't give any log message 1>&2
echo Please write a log message describing the purpose of your changes and 1>&2
echo then try committing again. -- Thank you 1>&2
exit 1

:exitgood
exit 0

But that doesn't work either, they both exit with code 255. Can somebody tell me what I'm doing wrong?

  • 4
    Eh? These are cmd.exe batch scripts. What are they doing on your Linux box? – Mat May 7 '13 at 9:52
  • Good point. I forgot SVN runs on windows as well... – RoboJ1M May 7 '13 at 16:00
3

Those are batch scripts – as in MS Windows Batch. FINDSTR was introduced in the Windows NT 4 Resource Kit.

:: and rem are comments. (Or :: is actually a label with invalid name).

You could probably run them under wine cmd, but would be better to port them to some native script (perl, python, bash, etc.).

Simple example:

#!/bin/bash

# Function to print usage and exit
usage()
{
    printf "Usage: %s [repository] [transaction_id]\n" $(basename "$1") >&2
    exit 1
}

# Check that we have at least 2 arguments
[ $# -ge 2 ] || usage

repo="$2"
trans_id="$2"

# Check that command succeed, and set variable "msg" to output
if ! msg="$(svnlook log "$repo" -t "$trans_id")"; then
    printf "Check path and id.\n" >&2
    usage
fi

# If msg is empty
if [ "$msg" = "" ]; then
    printf \
"Your commit has been blocked because you didn't give any log message
Please write a log message describing the purpose of your changes and
then try committing again. -- Thank you.\n" >&2
     exit 2    
fi

# Else default exit AKA 0
| improve this answer | |
  • Oh yeah, I though they looked familiar. I just associate SVN with Linux so it never occurred to me. – RoboJ1M May 7 '13 at 15:59
  • It happens. We learn trough repetition :). If you haven't used batch scripting (enough to recognize it) it isn't that big of a leap to miss. – Runium May 7 '13 at 18:39
  • Actually I've done fairly epic amounts of Windows batch scripting, it's Linux scripting I've done almost none of, beyond putting 1 or 2 commands in a text file and setting it to executable. Thanks for the example! Once upon a time I new how to add something that caused each line of the script to be dumped to stdout when executed. Something to so with adding X to the first line. Any idea what that is? – RoboJ1M May 8 '13 at 9:57
  • @RoboJ1M: You could add -x at prompt or in script for debug mode. For whole script e.g. #!/bin/bash -x, or set -x, for dump use -v. More here. – Runium May 8 '13 at 16:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.