From 4223d0ce79ffc038b14eff7edb8c28e56cb592a5 Mon Sep 17 00:00:00 2001 From: James Brundage <@github.com> Date: Sat, 4 Nov 2023 15:24:26 -0700 Subject: [PATCH] enhancement: git blame input (Fixes #199) --- Extensions/Git.Blame.Input.ugit.extension.ps1 | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 Extensions/Git.Blame.Input.ugit.extension.ps1 diff --git a/Extensions/Git.Blame.Input.ugit.extension.ps1 b/Extensions/Git.Blame.Input.ugit.extension.ps1 new file mode 100644 index 00000000..edda0c03 --- /dev/null +++ b/Extensions/Git.Blame.Input.ugit.extension.ps1 @@ -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 + } +}