Skip to content

Instantly share code, notes, and snippets.

@steviecoaster
Created January 28, 2025 16:09
Show Gist options
  • Save steviecoaster/1b5af4266d69e00440c7f37f21e8ee54 to your computer and use it in GitHub Desktop.
Save steviecoaster/1b5af4266d69e00440c7f37f21e8ee54 to your computer and use it in GitHub Desktop.
Convert the output of 'git remote -v' to a PowerShell object and open one of the remotes in a browser window
function Convert-GitRemote {
<#
.SYNOPSIS
Converts the output of 'git remote -v' to a PowerShell object
.EXAMPLE
Convert-GitRemote
#>
[CmdletBinding()]
Param()
end {
$matcher = '^(?<Name>\S+)\s+(?<Url>\S+)\s+\((?<Type>\S+)\)$'
$output = git remote -v
foreach ($line in $output) {
$null = $line -match $matcher
[PSCustomobject]@{
Name = $Matches.Name
Type = $Matches.Type
Url = $Matches.Url
}
}
}
}
function Open-GitRemote {
<#
.SYNOPSIS
Opens a given remote in a web browser
.DESCRIPTION
Long description
.PARAMETER Name
The remote name
.PARAMETER Type
The remote type
.EXAMPLE
Open-GitRemote
.EXAMPLE
Open-GitRemote -Name upstream
.EXAMPLE
ogr
.EXAMPLE
ogr gep13fork
#>
[Alias('ogr')]
[CmdletBinding()]
Param(
[Parameter()]
[String]
$Name = 'origin',
[Parameter()]
[ValidateSet('fetch', 'push')]
[String]
$Type = 'fetch'
)
end {
$remotes = Convert-GitRemote
$url = ($remotes | Where-Object { ($_.Name -eq $Name) -and ($_.Type -eq $Type) }).url
if ($url -match ':(?<RepositoryPath>[^:]+?)\.git') {
$url = 'https://github.com/{0}' -f $Matches.RepositoryPath
}
Start-Process $url
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment