-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgit-browse.ps1
More file actions
46 lines (36 loc) · 1.35 KB
/
git-browse.ps1
File metadata and controls
46 lines (36 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
Function BrowseTo-GitRepository {
[CmdletBinding()]
[Alias("origin")]
param(
[string]$remote = $(git config --get remote.origin.url)
)
if (!$remote) {
Write-Output "No Git URL found"
break
}
$matchings = @{
"github-ssh"=@("^git@(ssh\.)?github.com:(?<user>.+)\/(?<git>.*)\.git$", "https://github.com/%user%/%git%")
"github-https"=@("^https://github.com/(?<user>.+)/(?<git>.*)$", "https://github.com/%user%/%git%")
"azure-devops-https"=@("https://[^@]+@dev.azure.com/(?<tenant>.+)/(?<project>.+)/_git/(?<git>.*)$", "https://dev.azure.com/%tenant%/%project%/_git/%git%")
"azure-devops-ssh"=@("^git@ssh.dev.azure.com:v3/(?<tenant>.+)/(?<project>.+)/(?<git>.*)$", "https://dev.azure.com/%tenant%/%project%/_git/%git%")
}
$matchings.Keys |% {
$name = $_
$pattern = $matchings[$name][0]
$url = $matchings[$name][1]
Write-Verbose "Checking $name repository: $pattern"
if ($remote -match $pattern) {
$address = $url
$kv = $matches
$kv.Keys |? { $_ -match "^[A-Za-z_]" } |% {
$placeholder = $_
$replacing = $kv[$placeholder]
Write-Verbose "$($placeholder): $replacing"
$replaced = "%$($placeholder)%"
$address = $address.Replace($replaced, $replacing)
}
Write-Verbose "Navigating to $address"
Invoke-Expression "start $address"
}
}
}