Skip to content

Commit

Permalink
Merge pull request #134 from StartAutomating/ugit-remotes
Browse files Browse the repository at this point in the history
ugit 0.3.7
  • Loading branch information
StartAutomating committed Mar 9, 2023
2 parents 87ce2f7 + 3e7ac3e commit 8290286
Show file tree
Hide file tree
Showing 22 changed files with 788 additions and 130 deletions.
56 changes: 29 additions & 27 deletions .github/workflows/TestAndPublish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -383,32 +383,34 @@ jobs:
if ($releaseExists) {
"::warning::Release '$($releaseExists.Name )' Already Exists" | Out-Host
return
$releasedIt = $releaseExists
} else {
$releasedIt = Invoke-RestMethod -Uri $releasesURL -Method Post -Body (
[Ordered]@{
owner = '${{github.owner}}'
repo = '${{github.repository}}'
tag_name = $targetVersion
name = $ExecutionContext.InvokeCommand.ExpandString($ReleaseNameFormat)
body =
if ($env:RELEASENOTES) {
$env:RELEASENOTES
} elseif ($imported.PrivateData.PSData.ReleaseNotes) {
$imported.PrivateData.PSData.ReleaseNotes
} else {
"$($imported.Name) $targetVersion"
}
draft = if ($env:RELEASEISDRAFT) { [bool]::Parse($env:RELEASEISDRAFT) } else { $false }
prerelease = if ($env:PRERELEASE) { [bool]::Parse($env:PRERELEASE) } else { $false }
} | ConvertTo-Json
) -Headers @{
"Accept" = "application/vnd.github.v3+json"
"Content-type" = "application/json"
"Authorization" = 'Bearer ${{ secrets.GITHUB_TOKEN }}'
}
}
$releasedIt = Invoke-RestMethod -Uri $releasesURL -Method Post -Body (
[Ordered]@{
owner = '${{github.owner}}'
repo = '${{github.repository}}'
tag_name = $targetVersion
name = $ExecutionContext.InvokeCommand.ExpandString($ReleaseNameFormat)
body =
if ($env:RELEASENOTES) {
$env:RELEASENOTES
} elseif ($imported.PrivateData.PSData.ReleaseNotes) {
$imported.PrivateData.PSData.ReleaseNotes
} else {
"$($imported.Name) $targetVersion"
}
draft = if ($env:RELEASEISDRAFT) { [bool]::Parse($env:RELEASEISDRAFT) } else { $false }
prerelease = if ($env:PRERELEASE) { [bool]::Parse($env:PRERELEASE) } else { $false }
} | ConvertTo-Json
) -Headers @{
"Accept" = "application/vnd.github.v3+json"
"Content-type" = "application/json"
"Authorization" = 'Bearer ${{ secrets.GITHUB_TOKEN }}'
}
if (-not $releasedIt) {
Expand Down Expand Up @@ -443,10 +445,9 @@ jobs:
$fileBytes = [IO.File]::ReadAllBytes($file.FullName)
$releasedFiles[$file.Name] =
Invoke-RestMethod -Uri "${releaseUploadUrl}?name=$($file.Name)" -Headers @{
"Accept" = "application/vnd.github+json"
"ContentType" = "application/octet-stream"
"Accept" = "application/vnd.github+json"
"Authorization" = 'Bearer ${{ secrets.GITHUB_TOKEN }}'
} -Body $fileBytes
} -Body $fileBytes -ContentType Application/octet-stream
$releasedFiles[$file.Name]
}
}
Expand Down Expand Up @@ -584,6 +585,7 @@ jobs:
uses: StartAutomating/Piecemeal@main
- name: UseEZOut
uses: StartAutomating/EZOut@master
- name: UseHelpOut
- name: Run HelpOut
uses: StartAutomating/HelpOut@master
id: HelpOut

17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
## 0.3.7:

* git remote
* Now supporting git remote! (Fixes #129)

~~~PowerShell
git remote | git remote show
~~~

Also, some improvements to the GitHub Action:

* Icon Update (Fixes #132)
* No longer using set-output (Fixes #131)
* Adding -InstallModule to Action (Fixes #132)

---

## 0.3.6:

* git log
Expand Down
12 changes: 12 additions & 0 deletions Extensions/Git.Grep.UGit.Extension.ps1
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
<#
.SYNOPSIS
git grep extension
.DESCRIPTION
Outputs matches from git grep.
When possible, the regular expression will be converted into PowerSEhll so that the .Match populates accurately.
.EXAMPLE
git grep '-i' example # look for all examples in the repository
.LINK
Out-Git
#>
[Management.Automation.Cmdlet("Out","Git")] # It extends Out-Git
[ValidatePattern("^git grep",Options='IgnoreCase')] # when the pattern is "git grep"
[OutputType('Git.Grep.Match')]
Expand Down
145 changes: 145 additions & 0 deletions Extensions/Git.Remote.UGit.Extension.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<#
.Synopsis
Git Remotes Extension
.Description
Outputs git remotes as objects.
.EXAMPLE
git remote
.EXAMPLE
git remote | git remote get-url
.EXAMPLE
git remote | git remote show
#>
[Management.Automation.Cmdlet("Out","Git")] # It's an extension for Out-Git
[ValidatePattern("^git remote")] # that is run when the command starts with git remote.
[OutputType('git.remote.name','git.remote.uri', 'git.remote')]
param( )

begin {
$remoteLines = @()
}

process {
$remoteLines += $_
}

end {

if ($gitArgument -match '--(?>n|dry-run)' -or # If the arguments matched --n or --dry-run or
$remoteLines[0] -like 'usage:*' # the output lines started with usage:
) {
return $remoteLines # return the output directly.
}

# git remote can do a few different things
switch -Regex ($gitCommand) {
'git remote\s{0,}$' {
# With no other parameters, it returns the remote name.
return [PSCustomObject][Ordered]@{
PSTypename = 'git.remote.name'
RemoteName = $remoteLines -join ' ' -replace '\s'
GitRoot = $gitRoot
}
}
'git remote get-url (?<RemoteName>\S+)\s{0,}$' {
# With get-url, it returns the URL
return [PSCustomObject][Ordered]@{
PSTypename = 'git.remote.url'
RemoteName = $matches.RemoteName
RemoteUrl = $remoteLines -join ' ' -replace '\s'
GitOutputLines = $remoteLines
GitRoot = $gitRoot
}
}
'git remote show (?<RemoteName>\S+)\s{0,}$' {
# With show, it returns _a lot_ of stuff. We want to track:
$remoteName = $matches.RemoteName
# * Each named URL
$gitRemoteUrls = [ordered]@{
PSTypeName = 'git.remote.urls'
}
# * All remote branches
$remoteBranches = @()
# * All local branches
$localBranches = @()
# * All tracked upstream branches
$trackedUpstreams = @()
# * The Head branch
$headBranch = ''
$inSection = ''
# We go thru each line returned by git remote show
foreach ($line in $remoteLines) {
if ($line -match '^\*\sremote\s(?<RemoteName>\S+)') {
# We can ignore the first line (we already know the remote name)
}
elseif ($line -match 'URL:') {
# Lines containing URL: can be split into a purpose and URL.
$purpose, $remoteUrl = $line -split 'URL:'
$gitRemoteUrls[$purpose -replace '\s'] = $remoteUrl -replace '\s'
}
elseif ($line -match '^\s{1,}HEAD branch:') {
# The head branch line is helpfully marked.
$headBranch = $line -replace '^\s{1,}HEAD branch:' -replace '\s'
}
elseif ($line -match '^\s{2}Remote Branches:') {
# as are the names of each section
$inSection = 'Remote Branches'
}
elseif ($line -match "^\s{2}Local branches configured for 'git pull':") {
$inSection = 'LocalBranches'
}
elseif ($line -match "^\s{2}Local refs configured for 'git push':") {
$inSection = 'LocalRefs'
}
elseif ($inSection -and $line -match '^\s{4}') {
# Within each section, we'll want capture a branch name and status
if ($inSection -eq 'Remote Branches') {
$remoteBranch, $status, $null = $line -split '\s{1,}' -ne ''
$remoteBranches += [PSCustomObject][Ordered]@{
PSTypename = 'git.remote.branch'
BranchName = $remoteBranch
Status = $status
}
}
elseif ($inSection -eq 'Local Branches') {
$localBranch, $status = $line -split '\s{1,}' -ne ''
$status = $status -join ' '
$localBranches += [PSCustomObject][Ordered]@{
PSTypename = 'git.remote.local.branch'
BranchName = $localBranch
Status = $status
}
}
elseif ($inSection -eq 'LocalRefs') {
$localBranch, $status = $line -split '\s{1,}' -ne ''
$status = $status -join ' '
$trackedUpstreams += [PSCustomObject][Ordered]@{
PSTypename = 'git.remote.tracked.upstream'
BranchName = $localBranch
Status = $status
}
}
}
}

# Now we can return a custom object with all of the data from git.remote.show, and let the formatter do the work.
return [PSCustomObject][Ordered]@{
PSTypename = 'git.remote.show'
RemoteName = $remoteName
HeadBranch = $headBranch
RemoteURLs = [PSCustomObject]$gitRemoteUrls
RemoteBranches = $remoteBranches
LocalBranches = $localBranches
TrackedUpstreams = $trackedUpstreams
GitOutputLines = $remoteLines
GitRoot = $gitRoot
}
}

default {
# If it wasn't any scenario we know how to parse, return the lines as-is.
return $remoteLines
}
}
}

45 changes: 45 additions & 0 deletions Formatting/Git.Remote.format.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Formatting definitions for various outputs from Git.Remote

Write-FormatView -TypeName Git.Remote.Name -Property RemoteName -GroupByProperty GitRoot

Write-FormatView -TypeName Git.Remote.Url -Property RemoteName, RemoteUrl -GroupByProperty GitRoot

Write-FormatView -TypeName Git.Remote.Show -Action {
Write-FormatViewExpression -Text '* remote '
Write-FormatViewExpression -ForegroundColor Verbose -Property RemoteName
Write-FormatViewExpression -Newline
Write-FormatViewExpression -Text ' HEAD branch: '
Write-FormatViewExpression -ForegroundColor Verbose -Property HeadBranch
Write-FormatViewExpression -Newline
Write-FormatViewExpression -Text ' URLS: '
Write-FormatViewExpression -Newline
Write-FormatViewExpression -ScriptBlock {
(' ' * 4) + @(
$_.RemoteUrls | Out-String -Width ($host.UI.RawUI.BufferSize.Width - 4)
) -split [Environment]::NewLine -join (
[Environment]::NewLine + (' ' * 4)
)
}
Write-FormatViewExpression -Newline
Write-FormatViewExpression -Text ' Remote Branches:'
Write-FormatViewExpression -Newline
Write-FormatViewExpression -ControlName GitRemoteBranchList -Property RemoteBranches
Write-FormatViewExpression -Newline
Write-FormatViewExpression -Text ' Local Branches:'
Write-FormatViewExpression -Newline
Write-FormatViewExpression -ControlName GitRemoteBranchList -Property LocalBranches
Write-FormatViewExpression -Text ' Tracked Upstreams:'
Write-FormatViewExpression -Newline
Write-FormatViewExpression -ControlName GitRemoteBranchList -Property TrackedUpstreams
Write-FormatViewExpression -Newline
}

Write-FormatView -TypeName n/a -Name GitRemoteBranchList -AsControl -Action {
(' ' * 4) + @($_ |
Format-Table -Property BranchName, Status |
Out-String -Width ($host.UI.RawUI.BufferSize.Width - 4)
) -split [Environment]::NewLine -join (
[Environment]::NewLine + (' ' * 4)
)
}

30 changes: 26 additions & 4 deletions GitHub/Actions/UGitAction.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ $UGitScript,
[switch]
$SkipUGitPS1,

# A list of modules to be installed from the PowerShell gallery before scripts run.
[string[]]
$InstallModule,

# If provided, will commit any remaining changes made to the workspace with this commit message.
[string]
$CommitMessage,
Expand Down Expand Up @@ -55,6 +59,24 @@ if ($env:GITHUB_ACTION_PATH) {
throw "Action Path not found"
}

#region -InstallModule
if ($InstallModule) {
"::group::Installing Modules" | Out-Host
foreach ($moduleToInstall in $InstallModule) {
$moduleInWorkspace = Get-ChildItem -Path $env:GITHUB_WORKSPACE -Recurse -File |
Where-Object Name -eq "$($moduleToInstall).psd1" |
Where-Object {
$(Get-Content $_.FullName -Raw) -match 'ModuleVersion'
}
if (-not $moduleInWorkspace) {
Install-Module $moduleToInstall -Scope CurrentUser -Force
Import-Module $moduleToInstall -Force -PassThru | Out-Host
}
}
"::endgroup::" | Out-Host
}
#endregion -InstallModule

"::notice title=ModuleLoaded::ugit Loaded from Path - $($ugitModulePath)" | Out-Host

$anyFilesChanged = $false
Expand Down Expand Up @@ -96,7 +118,7 @@ if ($ugitScript) {
Out-Host
}
$ugitScriptTook = [Datetime]::Now - $ugitScriptStart
"::set-output name=ugitScriptRuntime::$($ugitScriptTook.TotalMilliseconds)" | Out-Host
"::notice title=ugitScriptRuntime::$($ugitScriptTook.TotalMilliseconds)" | Out-Host

$ugitPS1Start = [DateTime]::Now
$ugitPS1List = @()
Expand All @@ -115,9 +137,9 @@ if (-not $SkipugitPS1) {
}
$ugitPS1EndStart = [DateTime]::Now
$ugitPS1Took = [Datetime]::Now - $ugitPS1Start
"::set-output name=ugitPS1Count::$($ugitPS1List.Length)" | Out-Host
"::set-output name=ugitPS1Files::$($ugitPS1List -join ';')" | Out-Host
"::set-output name=ugitPS1Runtime::$($ugitPS1Took.TotalMilliseconds)" | Out-Host
"::notice title=ugitPS1Count::$($ugitPS1List.Length)" | Out-Host
"::notice title=ugitPS1Files::$($ugitPS1List -join ';')" | Out-Host
"::notice title=ugitPS1Runtime::$($ugitPS1Took.TotalMilliseconds)" | Out-Host
if ($CommitMessage -or $anyFilesChanged) {
if ($CommitMessage) {
dir $env:GITHUB_WORKSPACE -Recurse |
Expand Down
Loading

0 comments on commit 8290286

Please sign in to comment.