views:

2332

answers:

5

In my svn Pre commit hooks I use findstr to block certain file types beign committed. I now want to extend this to directories, in the first instance \obj\ directories however I am having problems with the Regular expression and escaping the \ of the dir

Currently I have

"C:\Program Files\VisualSVN Server\bin\svnlook.exe" changed -t %2 %1 | FindStr /R ".obj\\\"
IF %ERRORLEVEL% EQU 1 GOTO OK
echo "obj directories cannot be committed" >&2
exit 1
:OK
exit 0

i have tried with just \ at the end but that seems to escape the double quotes as well?

Any Ideas?

+1  A: 

In a regexp, backslash should be double escaped to be correctly interpreted in a String regex:

FindStr /R "\\.obj\\\\"

But in your case, since your regex should match both .obj files and "obj" directories, I would suggest:

FindStr /R "\\.?obj\\\\?"

because your orignal regex (".obj\\") would only have detected ".obj" directory, not "obj". Hence the '?'

Since '.' means any character, you also need "\\" before it to change its interpretation.

VonC
Due to weird quoting rules, your examples won't work -- you need to double the number of backslashes before any double-quote character. Also, you should put a backslash before the "." to indicate it should be treated literally, rather than as a wildcard.
j_random_hacker
True, I have updated the answer
VonC
You didn't test this did you. The backslash before the "." must be a *single* backslash, and the 4 backslashes at the end become 2 if you add a "?" before the final double-quote. The quoting rules are pretty strange I grant you.
j_random_hacker
I did not test indeed ;) That is why I upvoted *your* answer
VonC
+3  A: 

Empirically, either of the following commands does what you want:

... | findstr /R \.obj\\

... | findstr /R "\.obj\\\\"

Since you specified /R, you also need a backslash before the . because it will otherwise be interpreted as a wildcard character.

Side note: it appears from my tests that findstr.exe uses the somewhat strange quoting rules used by MS's C library, described on Microsoft's website. In this particular case the relevant rule is the one mentioning that a double-quote character preceded by an even number of backslashes is interpreted to be half as many backslashes. (Yes, it's weird, and it gets weirder when you realise that cmd.exe treats double-quote characters specially too... Quoting things properly on Windows is a world of pain, frankly.)

j_random_hacker
More precise regexp (I have update my own answer). +1
VonC
+1  A: 

What's the error you're getting?

This may be a red herring, but SVN uses / as the path seperator, which causes some problems under Windows. I had to put the following in all my hook scripts to change / to \:

SET REPOS=%1

:: Transform forward-slashes to back-slashes for Windows
SET REPOS=%REPOS:/=^\%
Patrick Cuff
+1  A: 

I solved this using the following.

:CHECKOBJDIRWITHFILES
"C:\Program Files\VisualSVN Server\bin\svnlook.exe" changed -t %2 %1  > "C:\Repositories\SoftwareRepository\hooks\out.txt"
"C:\Program Files\VisualSVN Server\bin\svnlook.exe" changed -t %2 %1 | FindStr /R ./obj/.
echo %ERRORLEVEL% > "C:\Repositories\SoftwareRepository\hooks\error.txt"
IF %ERRORLEVEL% EQU 1 GOTO CHECKOBJDIRWITHOUTFILES
echo "obj directories and their files cannot be committed" >&2
exit 1
:CHECKOBJDIRWITHOUTFILES
"C:\Program Files\VisualSVN Server\bin\svnlook.exe" changed -t %2 %1  > "C:\Repositories\SoftwareRepository\hooks\out.txt"
"C:\Program Files\VisualSVN Server\bin\svnlook.exe" changed -t %2 %1 | FindStr /R ./obj
echo %ERRORLEVEL% > "C:\Repositories\SoftwareRepository\hooks\error.txt"
IF %ERRORLEVEL% EQU 1 GOTO OK
echo "obj directories cannot be committed" >&2
exit 1
:OK
echo %ERRORLEVEL%  >&2
exit 0
Dean
no quotes! (around the regex of FINDSTR) Brilliant ;-) +1
VonC
A: 

Do you actually need a regular expression in this case? If you're just searching for the substring "\obj\" you can use /C instead of /R to treat the text as a literal match string:

{command} | findstr /C:\obj\
bobbymcr