Skip to content

Instantly share code, notes, and snippets.

@kinosang
Last active March 4, 2021 02:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kinosang/3a2720a20fa8c35555e9ef6bec18a501 to your computer and use it in GitHub Desktop.
Save kinosang/3a2720a20fa8c35555e9ef6bec18a501 to your computer and use it in GitHub Desktop.
A PowerShell function to "clone" or "checkout" repositories from remote Version Control Systems
function co (
[String]
[Parameter(Mandatory = $true)]
$url,
[String]
$path,
[Switch]
$git
) {
if ( $url.StartsWith('git://') -Or $url.EndsWith('.git') ) {
git clone --recurse-submodules $url $path
return
}
if ( $url.StartsWith('svn://') ) {
if ( $git ) {
git svn clone $url $path
return
}
svn checkout $url
return
}
if ( $url.StartsWith('http://') -Or $url.StartsWith('https://') ) {
$response = Invoke-WebRequest -Method Options -Body '<?xml version="1.0" encoding="utf-8"?><D:options xmlns:D="DAV:"><D:activity-collection-set></D:activity-collection-set></D:options>' -Uri $url
if ( $response.Headers.ContainsKey('SVN-Youngest-Rev') ) {
if ( $git ) {
git svn clone $url $path
return
}
svn checkout $url
return
}
$response = Invoke-WebRequest -Method HEAD -Uri "${url}?cmd=heads"
if ( $response.Headers.'Content-Type' -eq 'application/mercurial-0.1' ) {
if ( $git ) {
git clone "hg::${url}"
return
}
hg clone $url $path
return
}
}
throw "Source Path Not Supported."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment