Skip to content

Instantly share code, notes, and snippets.

@Cologler
Last active July 4, 2020 12:16
Show Gist options
  • Save Cologler/6d6a2f639ed20b041ba7d7fc5678d869 to your computer and use it in GitHub Desktop.
Save Cologler/6d6a2f639ed20b041ba7d7fc5678d869 to your computer and use it in GitHub Desktop.
# git for fork.
param (
[string] $RepoUrl,
[string] $Dest
)
function Help {
Write-Host 'Git command for clone repo which not belong to you.'
Write-Host ' -> create dir with name ' -NoNewline
Write-Host '$host#$user#$repo' -ForegroundColor Green
Write-Host ''
Write-Host '// use ' -NoNewline
Write-Host 'Get-Help gift' -ForegroundColor Yellow -NoNewline
Write-Host ' to get parameter info'
}
if (!$RepoUrl) {
Help
return
}
function ParseRepoUrl([string] $url) {
$schemaPrefixPattern = '(?:https://)'
$hostPattern = '(?<host>.+)'
$userPattern = '(?<user>[^/]+)'
$projPattern = '(?<proj>[^/]+)'
function Perpare([hashtable] $table) {
if (!$table.ContainsKey('GitUrl')) {
$gitHost = $table.Host
$user = $table.User
$proj = $table.Proj
$table['GitUrl'] = "https://$gitHost/$user/$proj.git"
}
return $table
}
if ($url -match "^$schemaPrefixPattern$hostPattern/$userPattern/$projPattern\.git$") {
return Perpare(@{
Host = $Matches.host
User = $Matches.user
Proj = $Matches.proj
GitUrl = $url
})
}
if ($url -match "^$schemaPrefixPattern$hostPattern/$userPattern/$projPattern$") {
return Perpare(@{
Host = $Matches.host
User = $Matches.user
Proj = $Matches.proj
GitUrl = "$url.git"
})
}
if ($url -match "^$userPattern/$projPattern$") {
return Perpare(@{
Host = 'github.com'
User = $Matches.user
Proj = $Matches.proj
})
}
}
$repoInfo = ParseRepoUrl $RepoUrl
if ($repoInfo) {
if (!$Dest) {
$Dest = (Resolve-Path .).Path
}
$name = "$($repoInfo.Host)#$($repoInfo.User)#$($repoInfo.Proj)"
$gitUrl = $repoInfo.GitUrl
$command = "git clone --depth 1 $gitUrl $Dest\$name"
Write-Host "Running '$command' ..." -ForegroundColor Green
Invoke-Expression $command
} else {
Write-Error "Unable parse git repo from url: $RepoUrl"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment