Skip to content

Instantly share code, notes, and snippets.

@H0R5E
Last active August 11, 2021 11:39
Show Gist options
  • Save H0R5E/204554cb66ef89eca8c7e86182798e14 to your computer and use it in GitHub Desktop.
Save H0R5E/204554cb66ef89eca8c7e86182798e14 to your computer and use it in GitHub Desktop.
PowerShell script which checks out a path in an SVN repository and continuously retries if connection is lost
<#
.Description
Check out a path in an SVN repository and continuously retry if connection is lost.
.PARAMETER DST
Local destination path to store checkout.
.PARAMETER SRC
Source path of SVN repository.
.PARAMETER USR
Username for accessing SVN repository.
.PARAMETER PWD
Password for accessing the SVN repository (prompted if not entered).
.PARAMETER FORCE
Overwrite the destination directory if it already exists
.INPUTS
None.
.OUTPUTS
None.
#>
param ([Parameter(Mandatory=$true, Position=0)]
[String[]]
$DST,
[String[]]
$SRC,
[String[]]
$USR,
[String[]]
$PWD,
[switch]
$FORCE = $false)
$RETRY = $false
# Don't overwrite an existing directory unless explicitly told to
if ((Test-Path -Path $DST) -and ($FORCE)) {
Get-ChildItem -Hidden -Path $DST -Recurse | Remove-Item -force -recurse
}
# Initiate the directory if required
if ((-not (Test-Path -Path $DST)) -or ($FORCE)) {
# Get server path if not given
if ($SRC -eq $null) {
$SRC = Read-Host -Prompt "Please enter the source path"
}
# Get the user if not given
if ($USR -eq $null) {
$secure = Read-Host "Please enter your user name" -AsSecureString
$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secure)
$USR = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($bstr)
}
# Get the password if not given
if ($PWD -eq $null) {
$secure = Read-Host "Please enter your password" -AsSecureString
$bstr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secure)
$PWD = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($bstr)
}
# Check that the repo can be reached
svn list --username "$USR" --password "$PWD" "$SRC" | out-null
if ($LASTEXITCODE -eq 1) {
throw "Could not access SVN repository. Check source path, username and password"
}
# Make the directory and initiate check out
New-Item -ItemType Directory -Force -Path "$DST" | out-null
svn checkout --username "$USR" --password "$PWD" "$SRC" "$DST"
}
else {
$RETRY = $true
}
# Repeatedly update until finished.
$BASE = (Get-Item -Path '.\' -Verbose).FullName
try {
if (($LASTEXITCODE -eq 1) -or ($RETRY)) {
cd "$DST"
svn cleanup
if ($LASTEXITCODE -eq 1) {
throw "Destination path is not an SVN repository. Use -force flag to overwrite"
}
do{
svn cleanup
svn update
} while ($LASTEXITCODE -eq 1)
}
}
finally {
cd "$BASE"
}
<#
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment