Search code examples
svnpowershellhookvisualsvn-serverpost-commit

How to export revision on commit with a post-commit hook?


It may be handy to export the specific Subversion repository branch after a commit, using a post-commit hook.

E.g. to update a website after a commit or to update a development branch for testing.

Is there any instruction or sample of such a hook?


Solution

  • The best choice for writing a hook script for Subversion in Windows environment (e.g. VisualSVN Server) is to use Windows PowerShell scripting language or good ol' Windows batch command-line.

    Here is the sample code of post-commit.bat and post-commit.ps1 that should be used together to export a committed revision to C:\Test. Put them into your repository 'hooks' folder, e.g. C:\Repositories\repository\hooks\

    post-commit.ps1

    # Store hook arguments into variables with mnemonic names
    $repos = $args[0]
    $rev   = $args[1]
    
    # Build path to svn.exe
    $svn = "$env:VISUALSVN_SERVER\bin\svn.exe"
    
    # Build url to repository
    $urepos = $repos -replace "\\", "/"
    $url = "file:///$urepos/"
    
    # Export repository revision $rev to the C:\test folder
    &"$svn" export -r $rev --force "$url" c:\test
    

    post-commit.bat

    @echo off
    
    set PWSH=%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe
    %PWSH% -command $input ^| %1\hooks\post-commit.ps1 %1 %2  
    if errorlevel 1 exit %errorlevel%