3

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?

Chad Birch
  • 73,098
  • 23
  • 151
  • 149
Dean
  • 5,896
  • 12
  • 58
  • 95

6 Answers6

3

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
  • 50,331
  • 10
  • 105
  • 169
1

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
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 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 Feb 26 '09 at 15:09
  • 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 Feb 26 '09 at 15:19
1

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
  • 28,540
  • 12
  • 67
  • 94
1

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
  • 5,896
  • 12
  • 58
  • 95
0

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
  • 23,769
  • 3
  • 56
  • 67
0

In the case of findstr consisting of the two character sequence \" the search string must be \\\\" (four backslashes).

BSalita
  • 8,420
  • 10
  • 51
  • 68