Skip to content

Commit dd8be6d

Browse files
Merge pull request #208 from StartAutomating/ugit-the-blame
ugit 0.4.2
2 parents 11ca250 + b88553c commit dd8be6d

17 files changed

+346
-30
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
## 0.4.2:
2+
3+
* git blame support (#192, #193, #199, #201)
4+
* Use-Git will write to Verbose, not warning, when a directory is not a repository (#198, #204)
5+
* ugit PSA improvements (#189, #205, #206, #207)
6+
7+
---
8+
19
## 0.4.1:
210

311
* New Git Command Support:
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<#
2+
.SYNOPSIS
3+
Extends git blame's parameters
4+
.DESCRIPTION
5+
Extends the parameters for git blame.
6+
#>
7+
[ValidatePattern('^git blame')]
8+
[Management.Automation.Cmdlet("Use","Git")]
9+
[CmdletBinding(PositionalBinding=$false)]
10+
param(
11+
# The line number (and relative offset)
12+
[Parameter(ValueFromPipelineByPropertyName)]
13+
[int[]]
14+
$LineNumber,
15+
16+
# The blame pattern to look for.
17+
[Parameter(ValueFromPipelineByPropertyName)]
18+
[string[]]
19+
$Pattern
20+
)
21+
22+
process {
23+
24+
# All git of these git blame parameters need to be after the input object:
25+
foreach ($gitArgToBe in @(
26+
if ($LineNumber) { # If a -LineNumber was provided
27+
# turn each pair into a range
28+
for ($LineNumberNumber = 0; $LineNumberNumber -lt $LineNumber.Length; $LineNumberNumber++) {
29+
"-L" # (this will be specified by git blame's real parameter, '-L')
30+
"$(
31+
if ($LineNumberNumber -lt ($LineNumber.Length - 1)) {
32+
"$($lineNumber[$LineNumberNumber]),$($lineNumber[$LineNumberNumber + 1])"
33+
} else {
34+
# if there was only one of a pair, only grab that line.
35+
"$($lineNumber[$LineNumberNumber]),1"
36+
}
37+
)"
38+
}
39+
}
40+
41+
if ($Pattern) { # If a -Pattern was provided
42+
foreach ($linePattern in $pattern) {
43+
"-L" # this also becomes '-L' in git blame
44+
"$linePattern"
45+
}
46+
}
47+
)) {
48+
$gitArgToBe | Add-Member NoteProperty AfterInput $true -Force -PassThru
49+
}
50+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<#
2+
.SYNOPSIS
3+
Parses git blame output
4+
.DESCRIPTION
5+
Parses the output of git blame.
6+
.EXAMPLE
7+
git blame ugit.psd1
8+
#>
9+
[Management.Automation.Cmdlet("Out","Git")] # It's an extension for Out-Git
10+
[ValidatePattern("^u?git blame",Options='IgnoreCase')] # when the pattern is "git blame"
11+
param()
12+
13+
begin {
14+
$gitBlameOutput = @()
15+
$blameHeaderPattern = @(
16+
'(?<CommitHash>[0-9a-f]{8})\s'
17+
18+
# If your filename contains parenthesis, this may not work, and you'll only have yourself to blame.
19+
'(?:(?<FileName>[\S-[\(]]+)\s)?'
20+
21+
'\((?<WhoWhenWhat>.+?)\)'
22+
) -join ''
23+
$blameRevision = @($gitCommand -split '\s' -notmatch '^--')[-1]
24+
}
25+
26+
process {
27+
$gitBlameOutput += $gitout
28+
}
29+
30+
end {
31+
$blameObjects = @(
32+
foreach ($gitBlameLine in $gitBlameOutput) {
33+
if ($gitBlameLine -notmatch $blameHeaderPattern) { continue }
34+
$lineContent = $gitBlameLine -replace $blameHeaderPattern
35+
$commitMatch = [Ordered]@{} + $matches
36+
$whoWhenWhat = $commitMatch.WhoWhenWhat -split '\s+'
37+
38+
[PSCustomObject][Ordered]@{
39+
PSTypeName = 'git.blame'
40+
CommitHash = $commitMatch.CommitHash
41+
CommitDate = ($whoWhenWhat[-4..-2] -join ' ') -as [DateTime]
42+
Line = $whoWhenWhat[-1]
43+
File = $matches.File
44+
Revision = $blameRevision
45+
Content = $lineContent
46+
GitRoot = $GitRoot
47+
Author = $whoWhenWhat[0..$(
48+
($whoWhenWhat.Length - 5)
49+
)] -join ' '
50+
GitOutputLines = $gitBlameLine
51+
}
52+
}
53+
)
54+
55+
if ($blameObjects) {
56+
$blameObjects
57+
} else {
58+
$gitBlameOutput
59+
}
60+
}
61+

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ Get-UGitExtension is built using [Piecemeal](https://github.com/StartAutomating/
7373
ugit comes packed with many examples.
7474
You might want to try giving some of these a try.
7575

76+
### Git.Blame Example 1
77+
78+
79+
~~~PowerShell
80+
git blame ugit.psd1
81+
~~~
82+
7683
### Git.Branch Example 1
7784

7885

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

394401

402+
* [Git Blame](docs/Git.Blame-Extension.md)
403+
404+
395405
* [Git Branch](docs/Git.Branch-Extension.md)
396406

397407

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

474484

485+
* [Git Blame Input](docs/Git.Blame.Input-Extension.md)
486+
487+
475488
* [Git Clone Input](docs/Git.Clone.Input-Extension.md)
476489

477490

Types/git.blame/PSTypeName.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
git.blame

Types/git.blame/get_Log.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
Push-Location $this.GitRoot
3+
git log $this.CommitHash -NumberOfCommits 1
4+
Pop-Location
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Write-FormatView -TypeName git.blame -GroupByProperty GitRoot -Property CommitHash, Author, CommitDate, Line, Content -AlignProperty @{
2+
'Line' = 'Right'
3+
'Content' = 'Left'
4+
}

Use-Git.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@
319319
if (-not $script:RepoRoots[$dir] -and # If we did not have a repo root
320320
-not ($gitArgument -match "(?>$($RepoNotRequired -join '|'))") # and we are not doing an operation that does not require one
321321
) {
322-
Write-Warning "'$($dir)' is not a git repository" # warn that there is no repo (#21)
322+
Write-Verbose "'$($dir)' is not a git repository" # write that there is no repo to verbose (#21 , #198, #204)
323323
Pop-Location # pop back out of the directory
324324
continue nextDirectory # and continue to the next directory.
325325
}

docs/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
## 0.4.2:
2+
3+
* git blame support (#192, #193, #199, #201)
4+
* Use-Git will write to Verbose, not warning, when a directory is not a repository (#198, #204)
5+
* ugit PSA improvements (#189, #205, #206, #207)
6+
7+
---
8+
19
## 0.4.1:
210

311
* New Git Command Support:

docs/Git.Blame-Extension.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
Extensions/Git.Blame.UGit.extension.ps1
2+
---------------------------------------
3+
4+
5+
6+
7+
### Synopsis
8+
Parses git blame output
9+
10+
11+
12+
---
13+
14+
15+
### Description
16+
17+
Parses the output of git blame.
18+
19+
20+
21+
---
22+
23+
24+
### Examples
25+
> EXAMPLE 1
26+
27+
```PowerShell
28+
git blame ugit.psd1
29+
```
30+
31+
32+
---
33+
34+
35+
### Syntax
36+
```PowerShell
37+
Extensions/Git.Blame.UGit.extension.ps1 [<CommonParameters>]
38+
```

0 commit comments

Comments
 (0)