Skip to content

Commit

Permalink
Merge pull request #166 from StartAutomating/ugit-file-diffs
Browse files Browse the repository at this point in the history
ugit 0.3.9
  • Loading branch information
StartAutomating authored Apr 24, 2023
2 parents 06a67e6 + 84dca72 commit 5c99410
Show file tree
Hide file tree
Showing 22 changed files with 668 additions and 35 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/TestAndPublish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,9 @@ jobs:
steps:
- name: Check out repository
uses: actions/checkout@v2
- name: GitLogger
uses: GitLogging/GitLoggerAction@main
id: GitLogger
- name: Use PSSVG Action
uses: StartAutomating/PSSVG@main
id: PSSVG
Expand Down
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
## 0.3.9:

* Adding ugit demo (Fixes #163)
* Use-Git: Improving -Confirm (Fixes #165) and -WhatIf (Fixes #162)
* git.log.input
* Adding -NumberOfCommits (#161/#156)
* Adding -SearchPattern (Fixes #158)
* Adding -SearchString (Fixes #157)
* Adding -Statistics (Fixes #155)
* Adding git.branch IsTracked (Fixes #160)
* Extending FileInfo for better git results (Fixes #153) (thanks @ninmonkey)
* Adding ugit.extension formatting (Fixes #164)
* git log formatting - right-aligning CommitMessage (Fixes #152) (thanks @ninmonkey)
* git log .diff FileChange fix (Fixes #151)
* Git Diff ChangeSet Formatting Fix (Fixes #150)
* Updating workflow (Using GitLogger)

---

## 0.3.8:

* Use-Git can now be extended (#140, #97), letting you add PowerShell parameters to any git command
Expand Down
51 changes: 48 additions & 3 deletions Extensions/Git.Log.Input.UGit.Extension.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
[Management.Automation.Cmdlet("Use","Git")]
[CmdletBinding(PositionalBinding=$false)]
param(
# The number of entries to get.
[Alias('CommitNumber','N','Number')]
[int]
$NumberOfCommits,

# Gets logs after a given date
[DateTime]
[Alias('Since')]
Expand All @@ -43,16 +48,47 @@ $CurrentBranch,
[Parameter(ValueFromPipelineByPropertyName)]
[Alias('ReferenceNumbers','ReferenceNumber','IssueNumbers','WorkItemID','WorkItemIDs')]
[int[]]
$IssueNumber
$IssueNumber,

# If set, will get statistics associated with each change
[Alias('Stat')]
[switch]
$Statistics,

# If provided, will search for specific strings within the change sets of a commit.
# This is especially useful when finding references to or changes to a given function or structure.
[Parameter(ValueFromPipelineByPropertyName)]
[Alias('Search')]
[string]
$SearchString,

# If provided, will search for specific patterns within the change sets of a commit.
# This is especially useful when finding references to or changes to a given function or structure.
[Parameter(ValueFromPipelineByPropertyName)]
[Alias('Pattern')]
[string]
$SearchPattern
)

# If the number of commits was provided, it should come first.
if ($NumberOfCommits) {
'-n'
"$NumberOfCommits"
}

foreach ($dashToDoubleDash in 'after', 'before', 'author') {
if ($PSBoundParameters[$dashToDoubleDash]) {
"--$dashToDoubleDash"
"$($PSBoundParameters[$dashToDoubleDash])"
}
}

foreach ($dashToDoubleDashSwitch in 'Statistics') {
if ($PSBoundParameters[$dashToDoubleDash]) {
"--$dashToDoubleDashSwitch"
}
}

if ($CurrentBranch) {
$headbranch = git remote | git remote show | Select-Object -ExpandProperty HeadBranch
$currentBranchName = git branch | Where-Object IsCurrentBranch
Expand All @@ -72,6 +108,15 @@ if ($IssueNumber) {
} else {
"\#$IssueNum\D"
}
}

}
}

if ($SearchString) {
"-S"
$SearchString
}

if ($SearchPattern) {
"-G"
$SearchPattern
}
12 changes: 4 additions & 8 deletions Formatting/Git.Diff.ChangeSet.Format.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@ Write-FormatView -TypeName Git.Diff.ChangeSet -AsControl -Name Git.Diff.ChangeSe
$changeLines = @($_.Changes -split '(?>\r\n|\n)' -ne '')
@(foreach ($changeLine in $changeLines) {
[Environment]::NewLine
if ($changeLine.StartsWith('+')) {
. $SetOutputStyle -ForegroundColor Success
$changeLine -replace "[\s\r\n]+$"
. $ClearOutputStyle
if ($changeLine.StartsWith('+')) {
Format-RichText -InputObject ($changeLine -replace "[\s\r\n]+$") -ForegroundColor Success
}
elseif ($changeLine.StartsWith('-')) {
. $SetOutputStyle -ForegroundColor Failure
$changeLine -replace "[\s\r\n]+$"
. $ClearOutputStyle
elseif ($changeLine.StartsWith('-')) {
Format-RichText -InputObject ($changeLine -replace "[\s\r\n]+$") -ForegroundColor Error
}
else {
$changeLine
Expand Down
18 changes: 15 additions & 3 deletions Formatting/Git.Log.format.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
Write-FormatView -TypeName Git.Log -Property GitUserName, CommitDate, CommitMessage -Wrap
Write-FormatView -TypeName Git.Log -Property GitUserName, CommitDate, CommitMessage -Wrap -AlignProperty @{
"CommitDate" = "Right"
"CommitMessage" = "Left"
}

Write-FormatView -TypeName Git.Log -Property GitUserName, CommitDate, CommitHash, CommitMessage -Wrap -Name IncludeCommitHash
Write-FormatView -TypeName Git.Log -Property GitUserName, CommitDate, CommitHash, CommitMessage -Wrap -Name IncludeCommitHash -AlignProperty @{
"CommitDate" = "Right"
"CommitHash" = "Left"
"CommitMessage" = "Left"
}

Write-FormatView -TypeName Git.Merge.Log -Property GitUserName, CommitDate, MergeHash, CommitMessage -Wrap
Write-FormatView -TypeName Git.Merge.Log -Property GitUserName, CommitDate, MergeHash, CommitMessage -Wrap -AlignProperty @{
"CommitDate" = "Right"
"CommitHash" = "Left"
"MergeHash" = "Left"
"CommitMessage" = "Left"
}
3 changes: 3 additions & 0 deletions Formatting/ugit.extension.Format.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Write-FormatView -TypeName ugit.extension -Property DisplayName, Synopsis -AutoSize

Write-FormatView -TypeName ugit.extension -Property DisplayName, Pattern -Name Pattern -AutoSize
12 changes: 12 additions & 0 deletions Types/System.IO.FileInfo/Alias.psd1
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
@{
'GitLog' = 'GitLogs'
'GitDifference' = 'GitDiff'
'Diff' = 'GitDiff'
'Logs' = 'GitLogs'
'GitHistory' = 'GitChanges'
'Changes' = 'GitChanges'
'History' = 'GitChanges'
'HasChanged' = 'GitDirty'
'HasChanges' = 'GitDirty'
'IsUnstaged' = 'GitDirty'
}
60 changes: 60 additions & 0 deletions Types/System.IO.FileInfo/GitChanges.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<#
.SYNOPSIS
Get Changes for a given file
.DESCRIPTION
Gets changes from git for a given file. Can provide a timespan, series of numbers, date, or pair of dates.
#>

$byDate = @()
$byNumber = @()
$byTimespan = @()
foreach ($arg in $args) {
if ($arg -as [int] -ne $null) {
$byNumber += $arg -as [int]
}
elseif ($arg -is [object[]]) {
$byNumber += $arg
}
elseif ($arg -as [DateTime]) {
$byDate+= $arg -as [DateTime]
}
elseif (
$arg -as [TimeSpan]
) {
$byTimespan+= $arg -as [TimeSpan]
}
}

Push-Location $this.Directory



if ($byTimespan) {
git log -Since ([DateTime]::Now - $byTimespan[0]) $this.Name
}
elseif ($byDate) {
if ($byDate.Length -gt 1) {
$first, $second = $byDate | Sort-Object
git log -After $second -Before $first $this.Name
} elseif ($byDate.Length -eq 1) {
git log -Since $byDate[0] $this.Name
} else {
throw "Can only list Changes between two dates"
}
}
elseif ($byNumber.Length) {
$maxNumber = $byNumber | Measure-Object -Maximum | Select-Object -ExpandProperty Maximum
$maxNumber = $maxNumber + 1
$anyNegative = @($byNumber -lt 0).Length

if ($anyNegative) {
@(git log $this.Name)[@($byNumber -as [int[]])]
} else {
@(git log -n $maxNumber $this.Name)[@($byNumber -as [int[]])]
}
}
else {
git log $this.Name
}

Pop-Location
3 changes: 3 additions & 0 deletions Types/System.IO.FileInfo/get_GitDiff.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Push-Location $this.Directory
(git log -n 1 $this.Name).Diff()
Pop-Location
3 changes: 3 additions & 0 deletions Types/System.IO.FileInfo/get_GitDirty.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Push-Location $this.Directory
$(git status $this.Name '-s') -as [bool]
Pop-Location
3 changes: 3 additions & 0 deletions Types/System.IO.FileInfo/get_GitLogs.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Push-Location $this.Directory
git log $this.Name
Pop-Location
3 changes: 3 additions & 0 deletions Types/git.branch/Alias.psd1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@{
HasUpstream = 'IsTracked'
}
8 changes: 8 additions & 0 deletions Types/git.branch/get_IsTracked.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Push-Location $this.GitRoot
(
git remote |
git remote show |
Select-Object -ExpandProperty RemoteBranches |
Where-Object BranchName -like "*$($this.BranchName)"
) -as [bool]
Pop-Location
4 changes: 3 additions & 1 deletion Types/git.log/Diff.ps1
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
Push-Location $this.GitRoot
$logPaths = @($this.GitArgument -ne 'log' -notmatch '^\-')
$logPaths = @($this.GitCommand -split '\s' -notmatch '^(?>git|log)$' -notmatch '^\-' -ne '')
Write-Debug "Logging paths: $logPaths"
foreach ($logPath in $logPaths) {
if (Test-Path $logPath) {
$relativeArgs = @("--relative", $logPath)
git diff $this.CommitHash @relativeArgs @args
}
}
if (-not $logPaths) {
Write-Debug "Getting diff of commit hash: $($this.CommitHash)"
git diff $this.CommitHash @args
}
Pop-Location
5 changes: 5 additions & 0 deletions Types/ugit.extension/get_Pattern.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
foreach ($attr in $this.ScriptBlock.Attributes) {
if ($attr -is [Management.Automation.ValidatePatternAttribute]) {
$attr.RegexPattern
}
}
46 changes: 38 additions & 8 deletions Use-Git.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@

$argumentNumber = 0

$gitArgsArray = [Collections.ArrayList]::new($GitArgument)
$gitArgsArray = [Collections.ArrayList]::new()
if ($GitArgument.Length) {
$gitArgsArray.AddRange($GitArgument)
}

foreach ($commandElement in $callingContext.CommandElements) {
if ($commandElement.parameterName -in 'd', 'v', 'c') {
Expand All @@ -122,7 +125,9 @@
$commandElement.Extent.ToString()
)
if ($commandElement.parameterName -in 'd', 'c', 'v') {
$ConfirmPreference = 'none' # so set confirm impact to none
if ($commandElement.parameterName -eq 'c') {
$ConfirmPreference = 'none' # so set confirm preference to none
}
$VerbosePreference = 'silentlyContinue'
$DebugPreference = 'silentlyContinue'
}
Expand Down Expand Up @@ -269,6 +274,8 @@

$GitCommand = "git $AllGitArgs"

$validInputExtensions = Get-UGitExtension -CommandName Use-Git -ValidateInput $GitCommand

# Get any arguments from extensions
$extensionOutputs = @(
Get-UGitExtension -CommandName Use-Git -Run -Parameter $paramCopy -Stream -ValidateInput $GitCommand
Expand All @@ -286,20 +293,39 @@
$extensionArgs += $extensionOutput
} else {
# However, if we have non-string arguments
$extensionOutput
$RunGit = $false
$extensionOutput # output them directly
$RunGit = $false # and do not run git.
}
}

# If we don't want to run git, continue.
if (-not $RunGit) { continue }

if ($inObject -isnot [string] -and
$inObject.ToString -isnot [Management.Automation.PSScriptMethod]) {
Write-Verbose "Input was not a string or ugit object"
Write-Verbose "InputObject was not a string or customized object, not passing down to git."
$inObject = $null
}

$AllGitArgs = @(@($GitArgument) + $extensionArgs + $inObject) # Then we collect the combined arguments
# Then we collect the combined arguments
$AllGitArgs = @(
$GitArgument[0]
foreach ($xa in $extensionArgs) {
if (-not $xa.AfterInput) {
$xa
}
}
if ($GitArgument.Length -gt 1) {
$GitArgument[1..($gitArgument.Length - 1)]
}
$inObject
foreach ($xa in $extensionArgs) {
if ($xa.AfterInput) {
$xa
}
}
)

$AllGitArgs = @($AllGitArgs -ne '') # (skipping any empty arguments)
$OutGitParams = @{GitArgument=$AllGitArgs} # and prepare a splat (to save precious space when reporting errors).

Expand All @@ -318,8 +344,12 @@
Write-Progress -PercentComplete $percentageComplete -Status "git $allGitArgs " -Activity "$($dir) " -Id $progId
}

if ($WhatIfPreference) {
"git $AllGitArgs"
}

# If we have indicated we do not care about -Confirmation, don't prompt
if (($ConfirmPreference -eq 'None' -and (-not $paramCopy.Confirm)) -or
elseif (($ConfirmPreference -eq 'None' -and (-not $paramCopy.Confirm)) -or
$PSCmdlet.ShouldProcess("$pwd : git $allGitArgs") # otherwise, as for confirmation to run.
) {
$eventSourceIds = @("Use-Git","Use-Git $allGitArgs")
Expand Down
19 changes: 19 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
## 0.3.9:

* Adding ugit demo (Fixes #163)
* Use-Git: Improving -Confirm (Fixes #165) and -WhatIf (Fixes #162)
* git.log.input
* Adding -NumberOfCommits (#161/#156)
* Adding -SearchPattern (Fixes #158)
* Adding -SearchString (Fixes #157)
* Adding -Statistics (Fixes #155)
* Adding git.branch IsTracked (Fixes #160)
* Extending FileInfo for better git results (Fixes #153) (thanks @ninmonkey)
* Adding ugit.extension formatting (Fixes #164)
* git log formatting - right-aligning CommitMessage (Fixes #152) (thanks @ninmonkey)
* git log .diff FileChange fix (Fixes #151)
* Git Diff ChangeSet Formatting Fix (Fixes #150)
* Updating workflow (Using GitLogger)

---

## 0.3.8:

* Use-Git can now be extended (#140, #97), letting you add PowerShell parameters to any git command
Expand Down
Loading

0 comments on commit 5c99410

Please sign in to comment.