Users validation in SVN pre-commit hook

2019-09-08 06:57发布

Found one of the useful SVN pre-commit hook in SVN pre-commit hook for avoiding changes to tags subdirectories by mcdon.

I want to add the validation check on the user before committing. Can I do something like below?

@echo off
REM  user1, user2, user3 are example
set VALID_USERS=user1,user2,user3

set SVNROOT="C:\Program Files\CollabNet Subversion Server\svnlook.exe"
set REPOS=%1%
set TXN=%2%

%SVNROOT% author %REPOS% -t %TXN% | findstr /r "^%VALID_USERS%$" >nul
if %errorlevel% EQU 0 (
   echo This is an invalid user 1>&2
   exit 1
) else (
   echo This is valid user 1>&2
   exit 0
)

The above pre-commit script failed as all users can commit their files. Also, the 'echo' command not working as I don't see any echo statement above. Can anyone help?

2条回答
Rolldiameter
2楼-- · 2019-09-08 07:11

I don't understand why you should want a pre-commit hook for that, so I give you a sketch what we are using when working on tags.

  1. We use the Subversion path-based authorization, and have rules like that:

    [/tags]
    * = r
    @R_SVN_ADMINS = rw
    

    so only admins are allowed to create and modify tags.

  2. If necessary, we add for each tag a rule to avoid modification:

    [/tags/r1.0]
    @R_SVN_ADMINS = r
    
  3. An alternative solution is to modify the rules in the first point after creation, so that tags are only read-only.

This works for us because tags are created seldomly, and we don't have much of them. We don't need any hooks to forbid something ...

查看更多
冷血范
3楼-- · 2019-09-08 07:23

Found the following solution working:

REM     Block deletion of folder/file in trunk
%SVNLOOK% changed %REPOS% -t %TXN% | findstr /r "^D.*trunk/.*$" >nul
if %errorlevel%==0 (goto DeleteInTrunkError)

REM     DeleteInTrunkError
REM ------------------------
:DeleteTrunkTagsError
echo. 1>&2
echo Trunk Delete Error: 1>&2
echo     Only Administrator can delete in the trunk folder. 1>&2
echo Commit details: 1>&2
%SVNROOT% changed %REPOS% -t %TXN% 1>&2
exit 1
查看更多
登录 后发表回答