SVN pre-commit hook for avoiding changes to tags subdirectories

前端 未结 11 711
陌清茗
陌清茗 2020-11-28 04:25

Is there anybody who has clear instructions on how to add a pre-commit hook that avoids changes to tags subdirectories?

I already searched the internet quite a bit.

11条回答
  •  鱼传尺愫
    2020-11-28 05:11

    Here is my windows batch file pre-commit hook. If the user is an administrator the other checks will be skipped. It checks if the commit message is empty, and if the commit is to a tag. Note: findstr is a nerfed alternative to grep on other platforms.

    The way it checks if the commit is to a tag, it first checks if svnlook changed contains "tags/". It then checks if svnlook changed matches "^A.tags/[^/]/$", which means that it will check if you are adding a new folder under tags/.

    Users are allowed to create new projects. The pre-commit hook allows a user to create the folders trunk/ tags/ and branches/. Users are not allowed to delete the folders trunk/ tags/ and branches/. This will work for a single or multi-project repository.

     @echo off
     rem This pre-commit hook will block commits with no log messages and blocks commits on tags.
     rem Users may create tags, but not modify them.
     rem If the user is an Administrator the commit will succeed.
    
     rem Specify the username of the repository administrator
     rem commits by this user are not checked for comments or tags
     rem Recommended to change the Administrator only when an admin commit is neccessary
     rem then reset the Administrator after the admin commit is complete
     rem this way the admin user is only an administrator when neccessary
     set Administrator=Administrator
    
     setlocal
    
     rem Subversion sends through the path to the repository and transaction id.
     set REPOS=%1%
     set TXN=%2%
    
     :Main
     rem check if the user is an Administrator
     svnlook author %REPOS% -t %TXN% | findstr /r "^%Administrator%$" >nul
     if %errorlevel%==0 (exit 0)
    
     rem Check if the commit has an empty log message
     svnlook log %REPOS% -t %TXN% | findstr . > nul
     if %errorlevel% gtr 0 (goto CommentError)
    
     rem Block deletion of branches and trunk
     svnlook changed %REPOS% -t %TXN% | findstr /r "^D.*trunk/$ ^D.*branches/$" >nul
     if %errorlevel%==0 (goto DeleteBranchTrunkError)
    
     rem Check if the commit is to a tag
     svnlook changed %REPOS% -t %TXN% | findstr /r "^.*tags/" >nul
     if %errorlevel%==0 (goto TagCommit)
     exit 0
    
     :DeleteBranchTrunkError
     echo. 1>&2
     echo Trunk/Branch Delete Error: 1>&2
     echo     Only an Administrator may delete the branches or the trunk. 1>&2
     echo Commit details: 1>&2
     svnlook changed %REPOS% -t %TXN% 1>&2
     exit 1
    
     :TagCommit
     rem Check if the commit is creating a subdirectory under tags/ (tags/v1.0.0.1)
     svnlook changed %REPOS% -t %TXN% | findstr /r "^A.*tags/[^/]*/$" >nul
     if %errorlevel% gtr 0 (goto CheckCreatingTags)
     exit 0
    
     :CheckCreatingTags
     rem Check if the commit is creating a tags/ directory
     svnlook changed %REPOS% -t %TXN% | findstr /r "^A.*tags/$" >nul
     if %errorlevel% == 0 (exit 0)
     goto TagsCommitError
    
     :CommentError
     echo. 1>&2
     echo Comment Error: 1>&2
     echo     Your commit has been blocked because you didn't enter a comment. 1>&2
     echo     Write a log message describing your changes and try again. 1>&2
     exit 1
    
     :TagsCommitError
     echo. 1>&2
     echo %cd% 1>&2
     echo Tags Commit Error: 1>&2
     echo     Your commit to a tag has been blocked. 1>&2
     echo     You are only allowed to create tags. 1>&2
     echo     Tags may only be modified by an Administrator. 1>&2
     echo Commit details: 1>&2
     svnlook changed %REPOS% -t %TXN% 1>&2
     exit 1
    

提交回复
热议问题