Skip to content

Commit

Permalink
Merge pull request #5 from StartAutomating/MoreGitUpdates
Browse files Browse the repository at this point in the history
More git updates
  • Loading branch information
StartAutomating authored Mar 21, 2022
2 parents b35b1a6 + debc0e6 commit 75bb371
Show file tree
Hide file tree
Showing 8 changed files with 333 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
## 0.1.1
* Support for git commit (#4)
---
## 0.1
* Initial Release of ugit
---
74 changes: 74 additions & 0 deletions Extensions/Git.Commit.UGit.Extension.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<#
.SYNOPSIS
git commit extension
.DESCRIPTION
Returns output from succesful git commits as objects.
#>
[Management.Automation.Cmdlet("Out","Git")]
[ValidatePattern("^git commit")]
[OutputType('git.commit.info')]
param()

begin {
$commitLines = @()
}

process {
$commitLines += $gitOut
}

end {
# If it doesn't look like the commit lines had a commit hash, output them directly
if (-not ($commitLines -match '[a-f0-9]+\]')) {
$commitLines
}
else
{
# Otherwise initialize commit information
$commitInfo = [Ordered]@{
FilesChanged = 0
Insertions = 0
Deletions = 0
GitRoot = $GitRoot
PSTypeName = 'git.commit.info'
}
# and walk over each line in the commit output.
for ($cln = 0; $cln -lt $commitLines.Length; $cln++) {
# If the line has the branch name and hash
if ($commitLines[$cln] -match '^\[(?<n>\S+)\s(?<h>[a-f0-9]+)\]') {
$commitInfo.BranchName = $matches.n # set .BranchName,
$commitInfo.CommitHash = $matches.h # set .CommitHash
$commitInfo.CommitMessage = # and set .CommitMessage to the rest of the line.
$commitLines[$cln] -replace '^\[[^\]]+\]\s+'
}
elseif ($commitLines[$cln] -match '^\s\d+') # If the line starts with a space and digits
{
# It's the summary. Split it on commas and remove most of the rest of the text.
foreach ($commitLinePart in $commitLines[$cln] -split ',' -replace '[\s\w\(\)-[\d]]') {

if ($commitLinePart.Contains('+')) {
# If the part contains +, it's insertions.
$commitInfo.Insertions = $commitLinePart -replace '\+' -as [int]
}
elseif ($commitLinePart.Contains('-'))
{
# If the part contains -, it's deletions.
$commitInfo.Deletions = $commitLinePart -replace '\-' -as [int]
}
else
{
# Otherwise, its the file change count.
$commitInfo.FilesChanged = $commitLinePart -as [int]
}
}
}
elseif ($commitInfo.BranchName) # Otherwise, if we already know the branch name
{
# add the line to the commit message.
$commitInfo.CommitMessage += [Environment]::NewLine + $commitLines[$cln]
}
}
# After we have walked thru all lines, output the commit info.
[PSCustomObject]$commitInfo
}
}
20 changes: 20 additions & 0 deletions Formatting/Git.Commit.Info.Format.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Write-FormatView -TypeName Git.Commit.Info -Action {
Write-FormatViewExpression -ScriptBlock {
"[$($_.BranchName) $($_.CommitHash)] "
} -ForegroundColor verbose
Write-FormatViewExpression -Property CommitMessage
Write-FormatViewExpression -Newline
Write-FormatViewExpression -ScriptBlock {
" $($_.FilesChanged) files changed"
} -ForegroundColor verbose
Write-FormatViewExpression -If {
$_.Insertions
} -ScriptBlock {
", $($_.Insertions) insertions(+)"
} -ForegroundColor Success
Write-FormatViewExpression -If {
$_.Deletions
} -ScriptBlock {
", $($_.Deletions) deletions(-)"
} -ForegroundColor Error
}
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,54 @@ git log -n 5
git log -n 5 |
Get-Member
~~~


## How ugit works:

ugit only has a few commands:

### Use-Git

After you've imported ugit, Use-Git is what will be called when you run "git".

This happens because Use-Git is aliased to "git", and aliases are resolved first in PowerShell.

Use-Git assumes all positional parameters are arguments to Git, and passes them on directly.

This works in almost every scenario, except with some single character git options. You can pass these in quotes.

When Use-Git outputs, it sets $global:LastGitOutput and then pipes to Out-Git.

### Out-Git

Out-Git will attempt to take git output and return it as a useful object.

This object can then be extended and formatted by PowerShell's Extended Type System.

Out-Git accomplishes this with several extensions. You can list extensions with Get-UGitExtension:

### Get-UGitExtension

Get-UGitExtension enables any file beneath ugit (or a module that tags ugit) named *.ugit.extension.ps1 to be treated as an extension.

In ugit, extensions signal that they apply to a given git command by adding a ```[ValidatePattern]``` attribute to the command.

If this pattern matches the given git command, the extension will run.

Get-UGitExtension is built using [Piecemeal](https://github.com/StartAutomating/Piecemeal)

## Git Commands Extended

* git branch
* git commit
* git diff
* git log
* git status


### Extensions that may apply to any git command:

* git.fileoutput

This applies to an git command that uses the -o flag.
It will attempt to locate any output specified by -o and return it as a file or directory.
74 changes: 74 additions & 0 deletions en-us/About_ugit.help.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# ugit
Updated Git: A powerful PowerShell wrapper for git that lets you extend git, automate multiple repos, and output git as objects.


## What is ugit?

ugit is a PowerShell module that gives you an updated git. You can use the object pipeline to pipe folders or files into git.
If you're using one of a number of supported commands, ugit will return your git output as objects.
This enables _a lot_ of interesting scenarios, giving you and updated way to work with git.

## Getting started

### Installing ugit
~~~PowerShell
Install-Module ugit -Scope CurrentUser
Import-Module ugit -Force -PassThru
# Once you've imported ugit, just run git commands normally.
# If ugit has an extension for the command, it will output as an object.
# These objects can be formatted by PowerShell
git log -n 5

# To get a sense of what you can do, pipe a given git command into Get-Member.
git log -n 5 |
Get-Member
~~~


## How ugit works:

ugit only has a few commands:

### Use-Git

After you've imported ugit, Use-Git is what will be called when you run "git".

This happens because Use-Git is aliased to "git", and aliases are resolved first in PowerShell.

Use-Git assumes all positional parameters are arguments to Git, and passes them on directly.

This works in almost every scenario, except with some single character git options. You can pass these in quotes.

When Use-Git outputs, it sets $global:LastGitOutput and then pipes to Out-Git.

### Out-Git

Out-Git will attempt to take git output and return it as a useful object.

This object can then be extended and formatted by PowerShell's Extended Type System.

Out-Git accomplishes this with several extensions. You can list extensions with Get-UGitExtension:

### Get-UGitExtension

Get-UGitExtension enables any file beneath ugit (or a module that tags ugit) named *.ugit.extension.ps1 to be treated as an extension.

In ugit, extensions signal that they apply to a given git command by adding a ```[ValidatePattern]``` attribute to the command.

If this pattern matches the given git command, the extension will run.

Get-UGitExtension is built using [Piecemeal](https://github.com/StartAutomating/Piecemeal)

## Git Commands Extended

* git branch
* git diff
* git log
* git status

### Extensions that may apply to any git command:

* git.fileoutput extension.

This applies to an git command that uses the -o flag.
It will attempt to locate any output specified by -o and return it as a file or directory.
97 changes: 97 additions & 0 deletions ugit.format.ps1xml
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,103 @@ $BackgroundColor
</TableRowEntries>
</TableControl>
</View>
<View>
<Name>Git.Commit.Info</Name>
<ViewSelectedBy>
<TypeName>Git.Commit.Info</TypeName>
</ViewSelectedBy>
<CustomControl>
<CustomEntries>
<CustomEntry>
<CustomItem>
<ExpressionBinding>
<ScriptBlock>$moduleName = 'ugit'
do {
$lm = Get-Module -Name $moduleName -ErrorAction Ignore
if (-not $lm) { continue }
if ($lm.FormatPartsLoaded) { break }
$wholeScript = @(foreach ($formatFilePath in $lm.exportedFormatFiles) {
foreach ($partNodeName in Select-Xml -LiteralPath $formatFilePath -XPath "/Configuration/Controls/Control/Name[starts-with(., '$')]") {
$ParentNode = $partNodeName.Node.ParentNode
"$($ParentNode.Name)={
$($ParentNode.CustomControl.CustomEntries.CustomEntry.CustomItem.ExpressionBinding.ScriptBlock)}"
}
}) -join [Environment]::NewLine
New-Module -Name "${ModuleName}.format.ps1xml" -ScriptBlock ([ScriptBlock]::Create(($wholeScript + ';Export-ModuleMember -Variable *'))) |
Import-Module -Global
$onRemove = [ScriptBlock]::Create("Remove-Module '${ModuleName}.format.ps1xml'")

if (-not $lm.OnRemove) {
$lm.OnRemove = $onRemove
} else {
$lm.OnRemove = [ScriptBlock]::Create($onRemove.ToString() + '' + [Environment]::NewLine + $lm.OnRemove)
}
$lm | Add-Member NoteProperty FormatPartsLoaded $true -Force

} while ($false)

. ${ugit_SetOutputStyle} -ForegroundColor 'verbose'</ScriptBlock>
</ExpressionBinding>
<ExpressionBinding>
<ScriptBlock>
"[$($_.BranchName) $($_.CommitHash)] "
</ScriptBlock>
</ExpressionBinding>
<ExpressionBinding>
<ScriptBlock>. ${ugit_ClearOutputStyle}</ScriptBlock>
</ExpressionBinding>
<ExpressionBinding>
<PropertyName>CommitMessage</PropertyName>
</ExpressionBinding>
<NewLine />
<ExpressionBinding>
<ScriptBlock>. ${ugit_SetOutputStyle} -ForegroundColor 'verbose'</ScriptBlock>
</ExpressionBinding>
<ExpressionBinding>
<ScriptBlock>
" $($_.FilesChanged) files changed"
</ScriptBlock>
</ExpressionBinding>
<ExpressionBinding>
<ScriptBlock>. ${ugit_ClearOutputStyle}</ScriptBlock>
</ExpressionBinding>
<ExpressionBinding>
<ScriptBlock>. ${ugit_SetOutputStyle} -ForegroundColor 'Success'</ScriptBlock>
</ExpressionBinding>
<ExpressionBinding>
<ItemSelectionCondition>
<ScriptBlock>
$_.Insertions
</ScriptBlock>
</ItemSelectionCondition>
<ScriptBlock>
", $($_.Insertions) insertions(+)"
</ScriptBlock>
</ExpressionBinding>
<ExpressionBinding>
<ScriptBlock>. ${ugit_ClearOutputStyle}</ScriptBlock>
</ExpressionBinding>
<ExpressionBinding>
<ScriptBlock>. ${ugit_SetOutputStyle} -ForegroundColor 'Error'</ScriptBlock>
</ExpressionBinding>
<ExpressionBinding>
<ItemSelectionCondition>
<ScriptBlock>
$_.Deletions
</ScriptBlock>
</ItemSelectionCondition>
<ScriptBlock>
", $($_.Deletions) deletions(-)"
</ScriptBlock>
</ExpressionBinding>
<ExpressionBinding>
<ScriptBlock>. ${ugit_ClearOutputStyle}</ScriptBlock>
</ExpressionBinding>
</CustomItem>
</CustomEntry>
</CustomEntries>
</CustomControl>
</View>
<View>
<Name>Git.Diff.ChangeSet</Name>
<ViewSelectedBy>
Expand Down
7 changes: 5 additions & 2 deletions ugit.psd1
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
@{
ModuleVersion = '0.1'
ModuleVersion = '0.1.1'
RootModule = 'ugit.psm1'
FormatsToProcess = 'ugit.format.ps1xml'
TypesToProcess = 'ugit.types.ps1xml'
Guid = '32323806-1d4a-485b-a64b-c502b0468847'
Author = 'James Brundage'
Copyright = '2022 Start-Automating'
CompanyName = 'Start-Automating'
Description = 'uGit: Updated Git.
Description = 'ugit: Updated Git.
A powerful PowerShell wrapper for git that lets you extend git, automate multiple repos, and use the object pipeline.'
PrivateData = @{
Expand All @@ -16,6 +16,9 @@ PrivateData = @{
ProjectURI = 'https://github.com/StartAutomating/ugit'
LicenseURI = 'https://github.com/StartAutomating/ugit/blob/main/LICENSE'
ReleaseNotes = @'
## 0.1.1
* Support for git commit (#4)
---
## 0.1
* Initial Release of ugit
---
Expand Down
6 changes: 6 additions & 0 deletions ugit.types.ps1xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ git diff $this.CommitHash @args
Pop-Location
</Script>
</ScriptMethod>
<ScriptProperty>
<Name>ReferenceNumbers</Name>
<GetScriptBlock>
[Regex]::new("#(?&lt;n&gt;\d+)").Matches($this.CommitMessage) -replace '#' -as [int[]]
</GetScriptBlock>
</ScriptProperty>
</Members>
</Type>
<Type>
Expand Down

0 comments on commit 75bb371

Please sign in to comment.