Last active
March 4, 2021 02:11
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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