2

I have a problem with my pre-revprop-change. I want to allow changes to the log message, but only for the author himself OR a set admin. The code I've edited is this one: https://stackoverflow.com/a/18005347/16739479

I've edited this part, but it doesn't work as it should:

set admin = muellerp
set AUTHOR=
for /f "delims=" %%a in ('svnlook author -r %REV% %REPOS%') do @set AUTHOR=%%a
if /I not '%AUTHOR%'=='%user%' if /I not '%user%'=='%admin%' goto ERROR_WRONGUSER

If I just write

if /I not '%AUTHOR%'=='%user%'goto ERROR_WRONGUSER

everything works perfectly fine, but then the admin can't make changes. I also have tried just to write

if /I not '%user%'=='%admin%' goto ERROR_WRONGUSER

but then it doesn't work as it should, it prints out the message within ERROR_WRONGUSER. I've also tried

if /I not '%user%'=="muellerp" goto ERROR_WRONGUSER

not even this is working, also I've checked the output of %user% is exactly the same as the name, so actually it shouldn't jump to the ERROR_WRONGUSER.

Can somebody help me, I'm pretty confused.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
phil9
  • 33
  • 4
  • 2
    You should use double-quotes on both sides of the comparison since single-quotes (apostrophes) do not have a special meaning at all… – aschipfl Aug 31 '21 at 10:50
  • 2
    And don't mix different quotes, `if /I not '%user%'=="muellerp" goto ERROR_WRONGUSER` can't never be true, because the quotes itself will be compared, too – jeb Aug 31 '21 at 10:56
  • Moreover, quote the repository path in the `svnlook` command line in order to protect potential special characters… – aschipfl Aug 31 '21 at 11:01

1 Answers1

3

set admin = muellerp is not the correct syntax.

It should be set "admin=muellerp", else you create a variable with the name admin<space> and the content <space>muellerp

A list of admins can be used by a small improvement, like:

set "admin_list=,mueller,schmidt,schulte,schmidtmeier,"    
if "!admin_list!" == "!admin_list:,%user%,=,###,!" echo Not an admin

The IF-condition is true, if both sides are equal.
But if the user exists in the list the right part will be different.

Example:
user=schmidt then !admin_list:,schmidt,=,###,! results to ,mueller,###,schulte,schmidtmeier,

Then the complete code looks like

@echo off
setlocal EnableDelayedExpansion
set "admin_list=,muellera,zernacka,"
set "AUTHOR="
for /f "delims=" %%a in ('svnlook author -r %REV% %REPOS%') do set "AUTHOR=%%a"
if /I not "!AUTHOR!" == "!user!" goto :user_accepted
if not "!admin_list!" == "!admin_list:,%user%,=,###,!" goto :user_accepted

echo "!user!" is not accepted
exit /b 1

:user_accepted
jeb
  • 78,592
  • 17
  • 171
  • 225
  • Thank you very much, it worked. Do you know a possibility how I can create a list of admins? – phil9 Aug 31 '21 at 10:20
  • 2
    I would include surrounding commas into the sub-string substitution in the list of admins in order to avoid false positives (e. g., administrators `ed` and `fred`, where the latter name contains the former one); of course the list must be augmented by a leading and a trailing comma then… – aschipfl Aug 31 '21 at 10:59
  • @aschipfl Thanks, very good point! I modified the code – jeb Aug 31 '21 at 11:37
  • So I've tried it with this code: `set check=true set "admin_list=,muellera,zernacka," if "!admin_list!" == "!admin_list:,%user%,=###!" set check=false if /I not "%AUTHOR%"=="%user%" if /I "%check%"=="false" goto ERROR_WRONGUSER` but it never jumps into the ERROR_WRONGUSER, even if my name is muellerp and not muellera as declared in the admin list ?! – phil9 Aug 31 '21 at 11:44
  • I got this error: Revprop change blocked by pre-revprop-change hook (exit code 255) with output: '"set AUTHOR=muellerp"' is not recognized as an internal or external command, operable program or batch file. The syntax of the command is incorrect. – phil9 Aug 31 '21 at 12:52
  • 1
    @phil9 Sorry, was a typo. Fixed now `"set AUTHOR=%%a"` vs `set "AUTHOR=%%a"` – jeb Aug 31 '21 at 13:05
  • You missed a " in the sixth line after AUTHOR! but now everything is working. Thank you very much ! :) I also had to adjust the logic to `if /I not "!AUTHOR!"=="!user!" if "!admin_list!" == "!admin_list:,%user%,=,###,!" goto ERROR_WRONGUSER` so that my requirements are met. – phil9 Sep 01 '21 at 06:03