Last active
August 6, 2019 15:16
-
-
Save ap0llo/89f968508ccf1d9993393ffa0ce37f88 to your computer and use it in GitHub Desktop.
This file contains 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
# https://gist.github.com/ap0llo/89f968508ccf1d9993393ffa0ce37f88 | |
function Open-GitHub([string]$Path) { | |
if (($null -eq $Path) -or ($Path -eq "")) { | |
$Path = (Get-Location).Path | |
} | |
$Path = (Resolve-Path $Path -ErrorAction Stop).Path | |
Push-Location $dir | |
$command = "git remote get-url origin" | |
$output = Invoke-Expression $command | |
if($LASTEXITCODE -ne 0) { | |
throw "Failed to get remote 'origin' for directory '$PATH': Command '$command' completed with exit code '$LASTEXITCODE'" | |
} | |
Pop-Location | |
$uri = ([System.Uri]$output) | |
$hostName = $uri.Host | |
if ($uri.Host -notlike "github.com") { | |
throw "Unsupported host $hostName" | |
} | |
if ($uri.Segments.Length -ne 3) { | |
throw "Unsupported uri $uri" | |
} | |
$user = $uri.Segments[1].Trim("/") | |
# get repo name and remove the .git suffix | |
$repo = $uri.Segments[2].Substring(0, $uri.Segments[2].Length - 4).Trim("/") | |
Start-Process "https://www.$hostName/$user/$repo" | |
} | |
Set-Alias github Open-GitHub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment