|
| 1 | +<# |
| 2 | +.Synopsis |
| 3 | + Git Remotes Extension |
| 4 | +.Description |
| 5 | + Outputs git remotes as objects. |
| 6 | +.EXAMPLE |
| 7 | + git remote |
| 8 | +.EXAMPLE |
| 9 | + git remote | git remote get-url |
| 10 | +.EXAMPLE |
| 11 | + git remote | git remote show |
| 12 | +#> |
| 13 | +[Management.Automation.Cmdlet("Out","Git")] # It's an extension for Out-Git |
| 14 | +[ValidatePattern("^git remote")] # that is run when the command starts with git remote. |
| 15 | +[OutputType('git.remote.name','git.remote.uri', 'git.remote')] |
| 16 | +param( ) |
| 17 | + |
| 18 | +begin { |
| 19 | + $remoteLines = @() |
| 20 | +} |
| 21 | + |
| 22 | +process { |
| 23 | + $remoteLines += $_ |
| 24 | +} |
| 25 | + |
| 26 | +end { |
| 27 | + |
| 28 | + if ($gitArgument -match '--(?>n|dry-run)' -or # If the arguments matched --n or --dry-run or |
| 29 | + $remoteLines[0] -like 'usage:*' # the output lines started with usage: |
| 30 | + ) { |
| 31 | + return $remoteLines # return the output directly. |
| 32 | + } |
| 33 | + |
| 34 | + # git remote can do a few different things |
| 35 | + switch -Regex ($gitCommand) { |
| 36 | + 'git remote\s{0,}$' { |
| 37 | + # With no other parameters, it returns the remote name. |
| 38 | + return [PSCustomObject][Ordered]@{ |
| 39 | + PSTypename = 'git.remote.name' |
| 40 | + RemoteName = $remoteLines -join ' ' -replace '\s' |
| 41 | + GitRoot = $gitRoot |
| 42 | + } |
| 43 | + } |
| 44 | + 'git remote get-url (?<RemoteName>\S+)\s{0,}$' { |
| 45 | + # With get-url, it returns the URL |
| 46 | + return [PSCustomObject][Ordered]@{ |
| 47 | + PSTypename = 'git.remote.url' |
| 48 | + RemoteName = $matches.RemoteName |
| 49 | + RemoteUrl = $remoteLines -join ' ' -replace '\s' |
| 50 | + GitOutputLines = $remoteLines |
| 51 | + GitRoot = $gitRoot |
| 52 | + } |
| 53 | + } |
| 54 | + 'git remote show (?<RemoteName>\S+)\s{0,}$' { |
| 55 | + # With show, it returns _a lot_ of stuff. We want to track: |
| 56 | + $remoteName = $matches.RemoteName |
| 57 | + # * Each named URL |
| 58 | + $gitRemoteUrls = [ordered]@{ |
| 59 | + PSTypeName = 'git.remote.urls' |
| 60 | + } |
| 61 | + # * All remote branches |
| 62 | + $remoteBranches = @() |
| 63 | + # * All local branches |
| 64 | + $localBranches = @() |
| 65 | + # * All tracked upstream branches |
| 66 | + $trackedUpstreams = @() |
| 67 | + # * The Head branch |
| 68 | + $headBranch = '' |
| 69 | + $inSection = '' |
| 70 | + # We go thru each line returned by git remote show |
| 71 | + foreach ($line in $remoteLines) { |
| 72 | + if ($line -match '^\*\sremote\s(?<RemoteName>\S+)') { |
| 73 | + # We can ignore the first line (we already know the remote name) |
| 74 | + } |
| 75 | + elseif ($line -match 'URL:') { |
| 76 | + # Lines containing URL: can be split into a purpose and URL. |
| 77 | + $purpose, $remoteUrl = $line -split 'URL:' |
| 78 | + $gitRemoteUrls[$purpose -replace '\s'] = $remoteUrl -replace '\s' |
| 79 | + } |
| 80 | + elseif ($line -match '^\s{1,}HEAD branch:') { |
| 81 | + # The head branch line is helpfully marked. |
| 82 | + $headBranch = $line -replace '^\s{1,}HEAD branch:' -replace '\s' |
| 83 | + } |
| 84 | + elseif ($line -match '^\s{2}Remote Branches:') { |
| 85 | + # as are the names of each section |
| 86 | + $inSection = 'Remote Branches' |
| 87 | + } |
| 88 | + elseif ($line -match "^\s{2}Local branches configured for 'git pull':") { |
| 89 | + $inSection = 'LocalBranches' |
| 90 | + } |
| 91 | + elseif ($line -match "^\s{2}Local refs configured for 'git push':") { |
| 92 | + $inSection = 'LocalRefs' |
| 93 | + } |
| 94 | + elseif ($inSection -and $line -match '^\s{4}') { |
| 95 | + # Within each section, we'll want capture a branch name and status |
| 96 | + if ($inSection -eq 'Remote Branches') { |
| 97 | + $remoteBranch, $status, $null = $line -split '\s{1,}' -ne '' |
| 98 | + $remoteBranches += [PSCustomObject][Ordered]@{ |
| 99 | + PSTypename = 'git.remote.branch' |
| 100 | + BranchName = $remoteBranch |
| 101 | + Status = $status |
| 102 | + } |
| 103 | + } |
| 104 | + elseif ($inSection -eq 'Local Branches') { |
| 105 | + $localBranch, $status = $line -split '\s{1,}' -ne '' |
| 106 | + $status = $status -join ' ' |
| 107 | + $localBranches += [PSCustomObject][Ordered]@{ |
| 108 | + PSTypename = 'git.remote.local.branch' |
| 109 | + BranchName = $localBranch |
| 110 | + Status = $status |
| 111 | + } |
| 112 | + } |
| 113 | + elseif ($inSection -eq 'LocalRefs') { |
| 114 | + $localBranch, $status = $line -split '\s{1,}' -ne '' |
| 115 | + $status = $status -join ' ' |
| 116 | + $trackedUpstreams += [PSCustomObject][Ordered]@{ |
| 117 | + PSTypename = 'git.remote.tracked.upstream' |
| 118 | + BranchName = $localBranch |
| 119 | + Status = $status |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + # Now we can return a custom object with all of the data from git.remote.show, and let the formatter do the work. |
| 126 | + return [PSCustomObject][Ordered]@{ |
| 127 | + PSTypename = 'git.remote.show' |
| 128 | + RemoteName = $remoteName |
| 129 | + HeadBranch = $headBranch |
| 130 | + RemoteURLs = [PSCustomObject]$gitRemoteUrls |
| 131 | + RemoteBranches = $remoteBranches |
| 132 | + LocalBranches = $localBranches |
| 133 | + TrackedUpstreams = $trackedUpstreams |
| 134 | + GitOutputLines = $remoteLines |
| 135 | + GitRoot = $gitRoot |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + default { |
| 140 | + # If it wasn't any scenario we know how to parse, return the lines as-is. |
| 141 | + return $remoteLines |
| 142 | + } |
| 143 | + } |
| 144 | +} |
| 145 | + |
0 commit comments