Skip to content

Commit

Permalink
Merge pull request #15 from StartAutomating/ugit-improvements
Browse files Browse the repository at this point in the history
Ugit improvements
  • Loading branch information
StartAutomating authored Mar 25, 2022
2 parents 4bdb215 + 3ad78f9 commit 3d9f545
Show file tree
Hide file tree
Showing 16 changed files with 243 additions and 48 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 0.1.3:
* Updating git.log extension: Adding .Merged (#16)
* Updating git push extension: Support for first push (#14)
* Adding .output to automatic typenames (Fixing #11)
* Adding .ToString to git.branch and git.branch.detail (#9)
* Updating git branch extension: Fixing --delete behavior (#13)
* Use-Git: Support for -d/-D/-v/-V (#12). -Verbose implies --verbose (#10)
---
## 0.1.2
* Support for git push (#7)
* Adding .Amend/.UpdateMessage to git.commit.info (#6)
Expand Down
2 changes: 1 addition & 1 deletion Extensions/Git.Branch.UGit.Extension.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ begin {
}
process {
# If -d or -delete was passed
if ($gitCommand -match '\s-(?>d|delete)') {
if ($gitCommand -match '\s-(?>d|-delete)') {
# and we have a confirmation of the branch being deleted
if ("$gitout" -match '^Deleted branch (?<BranchName>\S+) \(was (?<BranchHash>[0-9a-f]+)\)') {
# Output a 'git.branch.deleted' object
Expand Down
10 changes: 10 additions & 0 deletions Extensions/Git.Log.UGit.Extension.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
Log Extension
.Description
Outputs git log entries as objects
.Example
git log | Group-Object GitUserEmail -NoElement
.EXAMPLE
git log | Where-Object -Not Merged
#>
# It's an extension for Out-Git
[Management.Automation.Cmdlet("Out","Git")]
Expand All @@ -12,6 +16,7 @@ param(
)

begin {
$script:LogChangesMerged = $false
$Git_Log = [Regex]::new(@'
(?m)^commit # Commits start with 'commit'
\s+(?<CommitHash>(?<HexDigits>
Expand Down Expand Up @@ -61,6 +66,10 @@ begin {
if ($gitLogOut.CommitMessage) {
$gitLogOut.CommitMessage = $gitLogOut.CommitMessage.Trim()
}
if ($gitLogOut.MergeHash) {
$script:LogChangesMerged = $true
}
$gitLogOut.Merged = $script:LogChangesMerged
$gitLogOut.GitRoot = $GitRoot
[PSCustomObject]$gitLogOut
}
Expand All @@ -79,5 +88,6 @@ process {

end {
OutGitLog $lines
$ExecutionContext.SessionState.PSVariable.Remove('script:LogChangesMerged')
}

62 changes: 43 additions & 19 deletions Extensions/Git.Push.UGit.Extension.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ begin {
$pushLines = @() # Create a list for all of the lines from a git push.
# This regular expression looks for 3 spaces and two sets of hex digits, separated by two periods.
# We will use this to see if the push was succesful.
$pushCommitHashRegex = '\s{3}(?<o>[a-f0-9]+)\.\.(?<n>[a-f0-9]+)\s{0,}'
$pushCommitHashRegex = '\s{3}(?<o>[a-f0-9]+)\.\.(?<n>[a-f0-9]+)\s{0,}'
$pushFirstCommitRegex = "branch '(?<src>[^']+)' set up to track '(?<dest>[^']+)'"
}

process {
Expand All @@ -24,29 +25,52 @@ process {
}

end {
if (-not ($pushLines -match $pushCommitHashRegex)) { # If the push has no lines with a commit hash
$IsFirst = ($pushLines -match $pushFirstCommitRegex) -as [bool]
$IsUpdate = ($pushLines -match $pushCommitHashRegex) -as [bool]
# If the push has no lines with a commit hash and is not an update
if (-not ($IsFirst -or $IsUpdate)) {
$pushLines # output directly.
return
}

# Create a hashtable to store output
$pushOutput = [Ordered]@{PSTypeName='git.push.info'}

foreach ($pl in $pushLines) {
if ($pl -match '^To http') {
$to, $GitUrl = $pl -split ' '
# Pick out the url from the line starting with To http
$pushOutput.GitUrl = $GitUrl -join ' '
}
if ($IsUpdate) {
# Create a hashtable to store output
$pushOutput = [Ordered]@{PSTypeName='git.push.info'}

if ($pl -match $pushCommitHashRegex) {
# The line with the commit hash has the prior hash and the current has
$pushOutput.LastCommitHash = $matches.o
$pushOutput.CommitHash = $matches.n
# Followed by the source and destination branch, separated by ->
$pushOutput.SourceBranch, $pushOutput.DestinationBranch = $pl -replace $pushCommitHashRegex -split '\s+->\s+'
foreach ($pl in $pushLines) {
if ($pl -match '^To http') {
$to, $GitUrl = $pl -split ' '
# Pick out the url from the line starting with To http
$pushOutput.GitUrl = $GitUrl -join ' '
}

if ($pl -match $pushCommitHashRegex) {
# The line with the commit hash has the prior hash and the current has
$pushOutput.LastCommitHash = $matches.o
$pushOutput.CommitHash = $matches.n
# Followed by the source and destination branch, separated by ->
$pushOutput.SourceBranch, $pushOutput.DestinationBranch = $pl -replace $pushCommitHashRegex -split '\s+->\s+'
}
}
# Output our hashtable as a property bag.
[PSCustomObject]$pushOutput
}
elseif ($IsFirst) {
$pushOutput = [Ordered]@{PSTypeName='git.push.upstream'}
foreach ($pl in $pushLines) {
if ($pl -like 'remote:*' -and $pl -like '*/pull/*') {
$pushOutput.CreatePullRequestUrl = $pl -replace 'remote:\s+' -replace '\s$' -as [uri]
}
elseif ($pl -match '^To http') {
$to, $GitUrl = $pl -split ' '
# Pick out the url from the line starting with To http
$pushOutput.GitUrl = $GitUrl -join ' '
}
elseif ($pl -match $pushFirstCommitRegex) {
$pushOutput.SourceBranch, $pushOutput.DestinationBranch = $matches.src, $matches.dest
}
}
# Output our hashtable as a property bag.
[PSCustomObject]$pushOutput
}
# Output our hashtable as a property bag.
[PSCustomObject]$pushOutput
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Write-FormatView -TypeName git -Action {
Write-FormatView -TypeName git.output -Action {
Write-FormatViewExpression -Property GitOutput
}
17 changes: 17 additions & 0 deletions Formatting/Git.Push.Upstream.format.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Write-FormatView -TypeName Git.Push.Upstream -Action {
Write-FormatViewExpression -ScriptBlock {
"To " + $_.GitUrl + [Environment]::NewLine
}
Write-FormatViewExpression -ScriptBlock {
" * [new branch] " + $_.SourceBranch + " -> " + $_.DestinationBranch + [Environment]::NewLine
} -ForegroundColor Verbose

Write-FormatViewExpression -ScriptBlock {
" Create a pull request for $($_.SourceBranch) on $($_.CreatePullRequestUrl.host) by visiting:" + [Environment]::NewLine
}

Write-FormatViewExpression -ScriptBlock {
" " + $_.CreatePullRequestUrl
}
}

6 changes: 3 additions & 3 deletions Out-Git.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@
# Followed by each smaller set of arguments, separated by periods
# Followed by a PSTypeName of 'git'
# Thus, for example, git clone $repo
# Would have the typenames of :"git.clone.$repo", "git.clone","git"
# Would have the typenames of :"git.clone.$repo.output", "git.clone.output","git.output"
$gitOut.pstypenames.clear()
for ($n = $GitArgument.Length - 1 ; $n -ge 0; $n--) {
$gitOut.pstypenames.add(@('git') + $GitArgument[0..$n] -join '.')
$gitOut.pstypenames.add(@('git') + $GitArgument[0..$n] + @('output') -join '.')
}
$gitOut.pstypenames.add('git')
$gitOut.pstypenames.add('git.output')

# All gitOutput should attach the original output line, as well as the command that produced that line.
$gitOut.psobject.properties.add([PSNoteProperty]::new('GitOutput',"$out"))
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Get-UGitExtension is built using [Piecemeal](https://github.com/StartAutomating/
* git commit
* git diff
* git log
* git push
* git status


Expand Down
3 changes: 3 additions & 0 deletions Types/git.branch.detail/Delete.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Push-Location $this.GitRoot
git branch '-d' $this.BranchName @args
Pop-Location
1 change: 1 addition & 0 deletions Types/git.branch.detail/ToString.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$this.BranchName
1 change: 1 addition & 0 deletions Types/git.branch/ToString.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$this.BranchName
37 changes: 32 additions & 5 deletions Use-Git.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
.NOTES
For almost everything git does, calling Use-Git is the same as calling git directly.
The exceptions are short, single-character parameters, like -v and -d.
These get mapped to PowerShell parameters, like -Verbose and -Debug.
To ensure a parameter is passed to git, instead of interpreted as as PowerShell parameter, simply enclose it in quotes.
If you have any difficulties passing parameters to git, try enclosing them in quotes.
.LINK
Out-Git
.Example
Expand Down Expand Up @@ -63,8 +61,37 @@
if (-not $script:RepoRoots) { # If we have not yet created a cache of repo roots
$script:RepoRoots = @{} # do so now.
}
}

$myInv = $MyInvocation
$callstackPeek = @(Get-PSCallStack)[1]
$callingContext =
if ($callstackPeek.InvocationInfo.MyCommand.ScriptBlock) {
@($callstackPeek.InvocationInfo.MyCommand.ScriptBlock.Ast.FindAll({
param($ast)
$ast.Extent.StartLineNumber -eq $myInv.ScriptLineNumber -and
$ast.Extent.StartColumnNumber -eq $myInv.OffsetInLine -and
$ast -is [Management.Automation.Language.CommandAst]
},$true))[0]
}

$argumentNumber = 0
foreach ($commandElement in $callingContext.CommandElements) {
if ($commandElement.parameterName -in 'd', 'v') {
# If they passed -d/-D or -v/-V, they probably don't mean -Debug/-Verbose
$beforeArgs = @(if ($argumentNumber) { $GitArgument[0..$argumentNumber]})
$afterArgs = @(if ($argumentNumber + 1 -le $gitArgument.Length) {
$GitArgument[($argumentNumber + 1)..($GitArgument.Length - 1)]
})
$GitArgument = @($beforeArgs) + @("$($commandElement.Extent)") + @($afterArgs)
}
$argumentNumber++
}

if ($VerbosePreference -ne 'silentlyContinue' -and -not ($GitArgument -eq '-v'))
{
$GitArgument += '--verbose' # they probably want --verbose (and enough git commands support it to try).
}
}
process {
# First, we need to take any input and figure out what directories we are going into.
$directories = @()
Expand Down Expand Up @@ -98,7 +125,6 @@
$directories = @(foreach ($dir in $directories) { $dir.Fullname })
}


# For each directory we know of, we
foreach ($dir in $directories) {
$AllGitArgs = @(@($GitArgument) + $InputObject) # collect the combined arguments
Expand All @@ -109,6 +135,7 @@
@("$(& $script:CachedGitCmd rev-parse --show-toplevel *>&1)") -like "*/*" -replace '/', [io.path]::DirectorySeparatorChar
}
$OutGitParams.GitRoot = "$($script:RepoRoots[$dir])"
Write-Verbose "Calling git with $AllGitArgs"
& $script:CachedGitCmd @AllGitArgs *>&1 | # Then we run git, combining all streams into output.
Tee-Object -Variable global:lastGitOutput | # We store that output in $global:lastGitOutput, using Tee-Object
# then pipe to Out-Git, which will
Expand Down
5 changes: 4 additions & 1 deletion en-us/About_ugit.help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,16 @@ Get-UGitExtension is built using [Piecemeal](https://github.com/StartAutomating/
## Git Commands Extended

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


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

* git.fileoutput extension.
* 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.
99 changes: 82 additions & 17 deletions ugit.format.ps1xml
Original file line number Diff line number Diff line change
Expand Up @@ -582,23 +582,6 @@ $($ParentNode.CustomControl.CustomEntries.CustomEntry.CustomItem.ExpressionBindi
</CustomEntries>
</CustomControl>
</View>
<View>
<Name>git</Name>
<ViewSelectedBy>
<TypeName>git</TypeName>
</ViewSelectedBy>
<CustomControl>
<CustomEntries>
<CustomEntry>
<CustomItem>
<ExpressionBinding>
<PropertyName>GitOutput</PropertyName>
</ExpressionBinding>
</CustomItem>
</CustomEntry>
</CustomEntries>
</CustomControl>
</View>
<View>
<Name>Git.Log</Name>
<ViewSelectedBy>
Expand Down Expand Up @@ -705,6 +688,23 @@ $($ParentNode.CustomControl.CustomEntries.CustomEntry.CustomItem.ExpressionBindi
</TableRowEntries>
</TableControl>
</View>
<View>
<Name>git.output</Name>
<ViewSelectedBy>
<TypeName>git.output</TypeName>
</ViewSelectedBy>
<CustomControl>
<CustomEntries>
<CustomEntry>
<CustomItem>
<ExpressionBinding>
<PropertyName>GitOutput</PropertyName>
</ExpressionBinding>
</CustomItem>
</CustomEntry>
</CustomEntries>
</CustomControl>
</View>
<View>
<Name>Git.Push.Info</Name>
<ViewSelectedBy>
Expand Down Expand Up @@ -760,6 +760,71 @@ $($ParentNode.CustomControl.CustomEntries.CustomEntry.CustomItem.ExpressionBindi
</CustomEntries>
</CustomControl>
</View>
<View>
<Name>Git.Push.Upstream</Name>
<ViewSelectedBy>
<TypeName>Git.Push.Upstream</TypeName>
</ViewSelectedBy>
<CustomControl>
<CustomEntries>
<CustomEntry>
<CustomItem>
<ExpressionBinding>
<ScriptBlock>
"To " + $_.GitUrl + [Environment]::NewLine
</ScriptBlock>
</ExpressionBinding>
<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>
" * [new branch] " + $_.SourceBranch + " -&gt; " + $_.DestinationBranch + [Environment]::NewLine
</ScriptBlock>
</ExpressionBinding>
<ExpressionBinding>
<ScriptBlock>. ${ugit_ClearOutputStyle}</ScriptBlock>
</ExpressionBinding>
<ExpressionBinding>
<ScriptBlock>
" Create a pull request for $($_.SourceBranch) on $($_.CreatePullRequestUrl.host) by visiting:" + [Environment]::NewLine
</ScriptBlock>
</ExpressionBinding>
<ExpressionBinding>
<ScriptBlock>
" " + $_.CreatePullRequestUrl
</ScriptBlock>
</ExpressionBinding>
</CustomItem>
</CustomEntry>
</CustomEntries>
</CustomControl>
</View>
<View>
<Name>Git.Status</Name>
<ViewSelectedBy>
Expand Down
Loading

0 comments on commit 3d9f545

Please sign in to comment.