Ravi Ram
Ravi Ram

Reputation: 24488

VISUALSVN Post-Commit Hooks - Get repo name

How do I get the repo name from a VISUALSVN Post-Commit Hook?

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

I would like to replace the string 'demo' with the repo name.

Something like the following $reponame

@echo off
$reponame = SOME CODE TO GET REPO NAME
set PWSH=%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe
%PWSH% -command $input ^| C:\temp\post-commit.ps1 %1 %2 '$reponame'
if errorlevel 1 exit %errorlevel%

Upvotes: 1

Views: 490

Answers (2)

Ravi Ram
Ravi Ram

Reputation: 24488

I posted this question in 2014 and in 2017 I have a working copy.

The below code is used with VisualSVN Server Post Commit Hook

Working Code

# PATH TO SVN.EXE
$svn = "C:\Program Files\VisualSVN Server\bin\svn.exe"
$pathtowebistesWP = "c:\websites-wp\"

# STORE HOOK ARGUMENTS INTO FRIENDLY NAMES
$serverpathwithrep = $args[0]
$revision   = $args[1]

# GET DIR NAME ONLY FROM REPO-PATH STRING 
# EXAMPLE: C:\REPOSITORIES\DEVHOOKTEST
# RETURNS 'DEVHOOKTEST'
$dirname = ($serverpathwithrep -split '\\')[-1]

# Combine ServerPath with Dir name
$exportpath = -join($pathtowebistesWP, $dirname);

# BUILD URL TO REPOSITORY
$urepos = $serverpathwithrep -replace "\\", "/"
$url = "file:///$urepos/"


# --------------------------------
# SOME TESTING SCRIPTS
# --------------------------------

# STRING BUILDER PATH + DIRNAME
$name = -join($pathtowebistesWP, "testscript.txt");
# CREATE FILE ON SERVER
New-Item $name -ItemType file
# APPEND TEXT TO FILE
Add-Content $name $pathtowebistesWP
Add-Content $name $exportpath

# --------------------------------


# DO EXPORT REPOSITORY REVISION $REVISION TO THE C:\TEST FOLDER
&"$svn" export -r $revision --force "$url" $exportpath

Upvotes: 2

Ivan Jovović
Ivan Jovović

Reputation: 5298

As %1 contains repo path, which has repo name in it behind the last slash (eg C:/Repos/testRepo), and you are passing %1 into your PowerShell as first argument, just extract that value in your PS script:

 $repoPath = $args[0]
 $index = $repoPath.LastIndexOf("/")
 $repoName =  $repoPath.Substring($index + 1, $string.Length - $index - 1)

Upvotes: 2

Related Questions