Skip to content

Commit

Permalink
Merge pull request #208 from StartAutomating/ugit-the-blame
Browse files Browse the repository at this point in the history
ugit 0.4.2
  • Loading branch information
StartAutomating authored Nov 4, 2023
2 parents 11ca250 + b88553c commit dd8be6d
Show file tree
Hide file tree
Showing 17 changed files with 346 additions and 30 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 0.4.2:

* git blame support (#192, #193, #199, #201)
* Use-Git will write to Verbose, not warning, when a directory is not a repository (#198, #204)
* ugit PSA improvements (#189, #205, #206, #207)

---

## 0.4.1:

* New Git Command Support:
Expand Down
50 changes: 50 additions & 0 deletions Extensions/Git.Blame.Input.ugit.extension.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<#
.SYNOPSIS
Extends git blame's parameters
.DESCRIPTION
Extends the parameters for git blame.
#>
[ValidatePattern('^git blame')]
[Management.Automation.Cmdlet("Use","Git")]
[CmdletBinding(PositionalBinding=$false)]
param(
# The line number (and relative offset)
[Parameter(ValueFromPipelineByPropertyName)]
[int[]]
$LineNumber,

# The blame pattern to look for.
[Parameter(ValueFromPipelineByPropertyName)]
[string[]]
$Pattern
)

process {

# All git of these git blame parameters need to be after the input object:
foreach ($gitArgToBe in @(
if ($LineNumber) { # If a -LineNumber was provided
# turn each pair into a range
for ($LineNumberNumber = 0; $LineNumberNumber -lt $LineNumber.Length; $LineNumberNumber++) {
"-L" # (this will be specified by git blame's real parameter, '-L')
"$(
if ($LineNumberNumber -lt ($LineNumber.Length - 1)) {
"$($lineNumber[$LineNumberNumber]),$($lineNumber[$LineNumberNumber + 1])"
} else {
# if there was only one of a pair, only grab that line.
"$($lineNumber[$LineNumberNumber]),1"
}
)"
}
}

if ($Pattern) { # If a -Pattern was provided
foreach ($linePattern in $pattern) {
"-L" # this also becomes '-L' in git blame
"$linePattern"
}
}
)) {
$gitArgToBe | Add-Member NoteProperty AfterInput $true -Force -PassThru
}
}
61 changes: 61 additions & 0 deletions Extensions/Git.Blame.UGit.extension.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<#
.SYNOPSIS
Parses git blame output
.DESCRIPTION
Parses the output of git blame.
.EXAMPLE
git blame ugit.psd1
#>
[Management.Automation.Cmdlet("Out","Git")] # It's an extension for Out-Git
[ValidatePattern("^u?git blame",Options='IgnoreCase')] # when the pattern is "git blame"
param()

begin {
$gitBlameOutput = @()
$blameHeaderPattern = @(
'(?<CommitHash>[0-9a-f]{8})\s'

# If your filename contains parenthesis, this may not work, and you'll only have yourself to blame.
'(?:(?<FileName>[\S-[\(]]+)\s)?'

'\((?<WhoWhenWhat>.+?)\)'
) -join ''
$blameRevision = @($gitCommand -split '\s' -notmatch '^--')[-1]
}

process {
$gitBlameOutput += $gitout
}

end {
$blameObjects = @(
foreach ($gitBlameLine in $gitBlameOutput) {
if ($gitBlameLine -notmatch $blameHeaderPattern) { continue }
$lineContent = $gitBlameLine -replace $blameHeaderPattern
$commitMatch = [Ordered]@{} + $matches
$whoWhenWhat = $commitMatch.WhoWhenWhat -split '\s+'

[PSCustomObject][Ordered]@{
PSTypeName = 'git.blame'
CommitHash = $commitMatch.CommitHash
CommitDate = ($whoWhenWhat[-4..-2] -join ' ') -as [DateTime]
Line = $whoWhenWhat[-1]
File = $matches.File
Revision = $blameRevision
Content = $lineContent
GitRoot = $GitRoot
Author = $whoWhenWhat[0..$(
($whoWhenWhat.Length - 5)
)] -join ' '
GitOutputLines = $gitBlameLine
}
}
)

if ($blameObjects) {
$blameObjects
} else {
$gitBlameOutput
}
}

13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ Get-UGitExtension is built using [Piecemeal](https://github.com/StartAutomating/
ugit comes packed with many examples.
You might want to try giving some of these a try.

### Git.Blame Example 1


~~~PowerShell
git blame ugit.psd1
~~~

### Git.Branch Example 1


Expand Down Expand Up @@ -392,6 +399,9 @@ You might want to try giving some of these a try.
Most extensions handle output from a single git command.


* [Git Blame](docs/Git.Blame-Extension.md)


* [Git Branch](docs/Git.Branch-Extension.md)


Expand Down Expand Up @@ -472,6 +482,9 @@ It will attempt to locate any output specified by -o and return it as a file or
ugit also allows you to extend the input for git.


* [Git Blame Input](docs/Git.Blame.Input-Extension.md)


* [Git Clone Input](docs/Git.Clone.Input-Extension.md)


Expand Down
1 change: 1 addition & 0 deletions Types/git.blame/PSTypeName.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
git.blame
4 changes: 4 additions & 0 deletions Types/git.blame/get_Log.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

Push-Location $this.GitRoot
git log $this.CommitHash -NumberOfCommits 1
Pop-Location
4 changes: 4 additions & 0 deletions Types/git.blame/git.blame.format.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Write-FormatView -TypeName git.blame -GroupByProperty GitRoot -Property CommitHash, Author, CommitDate, Line, Content -AlignProperty @{
'Line' = 'Right'
'Content' = 'Left'
}
2 changes: 1 addition & 1 deletion Use-Git.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@
if (-not $script:RepoRoots[$dir] -and # If we did not have a repo root
-not ($gitArgument -match "(?>$($RepoNotRequired -join '|'))") # and we are not doing an operation that does not require one
) {
Write-Warning "'$($dir)' is not a git repository" # warn that there is no repo (#21)
Write-Verbose "'$($dir)' is not a git repository" # write that there is no repo to verbose (#21 , #198, #204)
Pop-Location # pop back out of the directory
continue nextDirectory # and continue to the next directory.
}
Expand Down
8 changes: 8 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 0.4.2:

* git blame support (#192, #193, #199, #201)
* Use-Git will write to Verbose, not warning, when a directory is not a repository (#198, #204)
* ugit PSA improvements (#189, #205, #206, #207)

---

## 0.4.1:

* New Git Command Support:
Expand Down
38 changes: 38 additions & 0 deletions docs/Git.Blame-Extension.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
Extensions/Git.Blame.UGit.extension.ps1
---------------------------------------




### Synopsis
Parses git blame output



---


### Description

Parses the output of git blame.



---


### Examples
> EXAMPLE 1
```PowerShell
git blame ugit.psd1
```


---


### Syntax
```PowerShell
Extensions/Git.Blame.UGit.extension.ps1 [<CommonParameters>]
```
63 changes: 63 additions & 0 deletions docs/Git.Blame.Input-Extension.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
Extensions/Git.Blame.Input.ugit.extension.ps1
---------------------------------------------




### Synopsis
Extends git blame's parameters



---


### Description

Extends the parameters for git blame.



---


### Parameters
#### **LineNumber**

The line number (and relative offset)






|Type |Required|Position|PipelineInput |
|-----------|--------|--------|---------------------|
|`[Int32[]]`|false |named |true (ByPropertyName)|



#### **Pattern**

The blame pattern to look for.






|Type |Required|Position|PipelineInput |
|------------|--------|--------|---------------------|
|`[String[]]`|false |named |true (ByPropertyName)|





---


### Syntax
```PowerShell
Extensions/Git.Blame.Input.ugit.extension.ps1 [-LineNumber <Int32[]>] [-Pattern <String[]>] [<CommonParameters>]
```
13 changes: 13 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ Get-UGitExtension is built using [Piecemeal](https://github.com/StartAutomating/
ugit comes packed with many examples.
You might want to try giving some of these a try.

### Git.Blame Example 1


~~~PowerShell
git blame ugit.psd1
~~~

### Git.Branch Example 1


Expand Down Expand Up @@ -392,6 +399,9 @@ You might want to try giving some of these a try.
Most extensions handle output from a single git command.


* [Git Blame](Git.Blame-Extension.md)


* [Git Branch](Git.Branch-Extension.md)


Expand Down Expand Up @@ -472,6 +482,9 @@ It will attempt to locate any output specified by -o and return it as a file or
ugit also allows you to extend the input for git.


* [Git Blame Input](Git.Blame.Input-Extension.md)


* [Git Clone Input](Git.Clone.Input-Extension.md)


Expand Down
2 changes: 1 addition & 1 deletion ugit.ezout.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Push-Location $myRoot
$formatting = @(
# Add your own Write-FormatView here,
# or put them in a Formatting or Views directory
foreach ($potentialDirectory in 'Formatting','Views') {
foreach ($potentialDirectory in 'Formatting','Views','Types') {
Join-Path $myRoot $potentialDirectory |
Get-ChildItem -ea ignore |
Import-FormatView -FilePath {$_.Fullname}
Expand Down
48 changes: 47 additions & 1 deletion ugit.format.ps1xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-16"?>
<!-- Generated with EZOut 2.0.1: Install-Module EZOut or https://github.com/StartAutomating/EZOut -->
<!-- Generated with EZOut 2.0.2: Install-Module EZOut or https://github.com/StartAutomating/EZOut -->
<Configuration>
<Controls>
<Control>
Expand Down Expand Up @@ -3036,5 +3036,51 @@ $BackgroundColor
</TableRowEntries>
</TableControl>
</View>
<View>
<Name>git.blame</Name>
<ViewSelectedBy>
<TypeName>git.blame</TypeName>
</ViewSelectedBy>
<GroupBy>
<PropertyName>GitRoot</PropertyName>
</GroupBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
</TableColumnHeader>
<TableColumnHeader>
</TableColumnHeader>
<TableColumnHeader>
</TableColumnHeader>
<TableColumnHeader>
<Alignment>Right</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Alignment>Left</Alignment>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>CommitHash</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Author</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>CommitDate</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Line</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Content</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>
Loading

0 comments on commit dd8be6d

Please sign in to comment.