Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

closed as off topic by manatwork, rush, slm, Renan, jasonwryan May 7 '13 at 18:15

Questions on Unix & Linux Stack Exchange are expected to relate to Unix or Linux within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

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

1 Answer 1

up vote 2 down vote accepted

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
share|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. –  Sukminder 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. –  Sukminder May 8 '13 at 16:07

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