From d07e22a0a11aee551347866ad6c1f4f167f3ec89 Mon Sep 17 00:00:00 2001 From: Oliver Lipkau Date: Wed, 2 Jan 2019 17:13:38 +0100 Subject: [PATCH 1/2] Fixed paginated results of git commands --- BuildHelpers/Public/Invoke-Git.ps1 | 206 ++++++++++++----------------- 1 file changed, 86 insertions(+), 120 deletions(-) diff --git a/BuildHelpers/Public/Invoke-Git.ps1 b/BuildHelpers/Public/Invoke-Git.ps1 index fe56325..3cd55f2 100644 --- a/BuildHelpers/Public/Invoke-Git.ps1 +++ b/BuildHelpers/Public/Invoke-Git.ps1 @@ -1,153 +1,119 @@ Function Invoke-Git { -<# - .SYNOPSIS - Wrapper to invoke git and return streams + <# + .SYNOPSIS + Wrapper to invoke git and return streams - .FUNCTIONALITY - CI/CD + .FUNCTIONALITY + CI/CD - .DESCRIPTION - Wrapper to invoke git and return streams + .DESCRIPTION + Wrapper to invoke git and return streams - .PARAMETER Arguments - If specified, call git with these arguments. + .PARAMETER Arguments + If specified, call git with these arguments. - This takes a positional argument and accepts all value afterwards for a more natural 'git-esque' use. + This takes a positional argument and accepts all value afterwards for a more natural 'git-esque' use. - .PARAMETER Path - Working directory to launch git within. Defaults to current location + .PARAMETER Path + Working directory to launch git within. Defaults to current location - .PARAMETER RedirectStandardError - Whether to capture standard error. Defaults to $true + .PARAMETER RedirectStandardError + Whether to capture standard error. Defaults to $true - .PARAMETER RedirectStandardOutput - Whether to capture standard output. Defaults to $true + .PARAMETER RedirectStandardOutput + Whether to capture standard output. Defaults to $true - .PARAMETER UseShellExecute - See System.Diagnostics.ProcessStartInfo. Defaults to $false + .PARAMETER UseShellExecute + See System.Diagnostics.ProcessStartInfo. Defaults to $false - .PARAMETER Raw - If specified, return an object with the command, output, and error properties. + .PARAMETER Raw + If specified, return an object with the command, output, and error properties. - Without Raw or Quiet, we return output if there's output, and we write an error if there are errors + Without Raw or Quiet, we return output if there's output, and we write an error if there are errors - .PARAMETER Split - If specified, split output and error on this. Defaults to `n + .PARAMETER Split + If specified, split output and error on this. Defaults to `n - .PARAMETER Quiet - If specified, do not return output + .PARAMETER Quiet + If specified, do not return output - .PARAMETER GitPath - Path to git. Defaults to git (i.e. git is in $ENV:PATH) + .PARAMETER GitPath + Path to git. Defaults to git (i.e. git is in $ENV:PATH) - .EXAMPLE - Invoke-Git rev-parse HEAD + .EXAMPLE + Invoke-Git rev-parse HEAD - # Get the current commit hash for HEAD + # Get the current commit hash for HEAD - .EXAMPLE - Invoke-Git rev-parse HEAD -path C:\sc\PSStackExchange + .EXAMPLE + Invoke-Git rev-parse HEAD -path C:\sc\PSStackExchange - # Get the current commit hash for HEAD for the repo located at C:\sc\PSStackExchange + # Get the current commit hash for HEAD for the repo located at C:\sc\PSStackExchange - .LINK - https://github.com/RamblingCookieMonster/BuildHelpers + .LINK + https://github.com/RamblingCookieMonster/BuildHelpers - .LINK - about_BuildHelpers - #> - [cmdletbinding()] - param( - [parameter(Position = 0, - ValueFromRemainingArguments = $true)] - $Arguments, + .LINK + about_BuildHelpers + #> + [cmdletbinding()] + param( + [parameter(Position = 0, + ValueFromRemainingArguments = $true)] + $Arguments, - $NoWindow = $true, - $RedirectStandardError = $true, - $RedirectStandardOutput = $true, - $UseShellExecute = $false, - $Path = $PWD.Path, - $Quiet, - $Split = "`n", - $Raw, - [validatescript({ - if(-not (Get-Command $_ -ErrorAction SilentlyContinue)) - { - throw "Could not find command at GitPath [$_]" + $Path = $PWD.Path, + + [switch]$Quiet, + + [switch]$Raw, + + [validatescript({ + if(-not (Get-Command $_ -ErrorAction SilentlyContinue)) + { + throw "Could not find command at GitPath [$_]" + } + $true + })] + [string]$GitPath = 'git' + ) + + $Path = (Resolve-Path $Path).Path + if(!$PSBoundParameters.ContainsKey('GitPath')) { + $GitPath = (Get-Command $GitPath -ErrorAction Stop)[0].Path + } + + $result = & $GitPath $Arguments 2>&1 + + if(-not $Quiet) { + $output = [pscustomobject]@{ + Command = "$GitPath $Arguments" + Output = "" + Error = "" } - $true - })] - [string]$GitPath = 'git' - ) - - $Path = (Resolve-Path $Path).Path - # http://stackoverflow.com/questions/8761888/powershell-capturing-standard-out-and-error-with-start-process - $pinfo = New-Object System.Diagnostics.ProcessStartInfo - if(!$PSBoundParameters.ContainsKey('GitPath')) { - $GitPath = (Get-Command $GitPath -ErrorAction Stop)[0].Path - } - $pinfo.FileName = $GitPath - $Command = $GitPath - $pinfo.CreateNoWindow = $NoWindow - $pinfo.RedirectStandardError = $RedirectStandardError - $pinfo.RedirectStandardOutput = $RedirectStandardOutput - $pinfo.UseShellExecute = $UseShellExecute - $pinfo.WorkingDirectory = $Path - if($PSBoundParameters.ContainsKey('Arguments')) - { - $pinfo.Arguments = $Arguments - $Command = "$Command $Arguments" - } - $p = New-Object System.Diagnostics.Process - $p.StartInfo = $pinfo - $null = $p.Start() - $p.WaitForExit() - if($Quiet) - { - return - } - else - { - #there was a newline in output... - if($stdout = $p.StandardOutput.ReadToEnd()) - { - if($split) + if ($result.writeErrorStream) { - $stdout = $stdout -split "`n" | Where-Object {$_} + $output.Error = $result.Exception.Message -join "`n" } - $stdout = foreach($item in @($stdout)){ - $item.trim() - } - } - if($stderr = $p.StandardError.ReadToEnd()) - { - if($split) + else { - $stderr = $stderr -split "`n" | Where-Object {$_} + $output.Output = $result -join "`n" } - $stderr = foreach($item in @($stderr)){ - $item.trim() - } - } - if($Raw) - { - [pscustomobject]@{ - Command = $Command - Output = $stdout - Error = $stderr - } - } - else - { - if($stdout) + if($Raw) { - $stdout + $output } - if($stderr) + else { - Write-Error $stderr.trim() + if ($result.writeErrorStream) + { + $output.Error + } + else + { + $output.Output + } } } } -} From 2ec5276dfbde5d5c0269282eae72edf7f4c57377 Mon Sep 17 00:00:00 2001 From: Oliver Lipkau Date: Wed, 2 Jan 2019 17:54:52 +0100 Subject: [PATCH 2/2] Fixed use of parameters Parameters like `-Raw` can be shorten with `-r` which is a valid parameter for a git command --- BuildHelpers/Public/Get-BuildVariable.ps1 | 16 +- BuildHelpers/Public/Get-GitChangedFile.ps1 | 6 +- BuildHelpers/Public/Invoke-Git.ps1 | 165 +++++++++------------ Tests/BuildHelpers.Tests.ps1 | 2 +- 4 files changed, 78 insertions(+), 111 deletions(-) diff --git a/BuildHelpers/Public/Get-BuildVariable.ps1 b/BuildHelpers/Public/Get-BuildVariable.ps1 index 08357c1..5917c6c 100644 --- a/BuildHelpers/Public/Get-BuildVariable.ps1 +++ b/BuildHelpers/Public/Get-BuildVariable.ps1 @@ -142,7 +142,7 @@ function Get-BuildVariable { { # Using older than 1.6.3 in your build system? Yuck # Thanks to earl: http://stackoverflow.com/a/1418022/3067642 - $BuildBranch = Invoke-Git @IGParams -Arguments "rev-parse --abbrev-ref HEAD" + $BuildBranch = (Invoke-Git @IGParams -Arguments "rev-parse --abbrev-ref HEAD").Output } } @@ -156,42 +156,42 @@ function Get-BuildVariable { 'CI_COMMIT_SHA' { if($WeCanGit) { - Invoke-Git @IGParams -Arguments "log --format=%B -n 1 $( (Get-Item -Path "ENV:$_").Value )" + (Invoke-Git @IGParams -Arguments "log --format=%B -n 1 $( (Get-Item -Path "ENV:$_").Value )").Output break } # Gitlab 9.0+ - thanks to mipadi http://stackoverflow.com/a/3357357/3067642 } 'CI_BUILD_REF' { if($WeCanGit) { - Invoke-Git @IGParams -Arguments "log --format=%B -n 1 $( (Get-Item -Path "ENV:$_").Value )" + (Invoke-Git @IGParams -Arguments "log --format=%B -n 1 $( (Get-Item -Path "ENV:$_").Value )").Output break } # Gitlab 8.x - thanks to mipadi http://stackoverflow.com/a/3357357/3067642 } 'GIT_COMMIT' { if($WeCanGit) { - Invoke-Git @IGParams -Arguments "log --format=%B -n 1 $( (Get-Item -Path "ENV:$_").Value )" + (Invoke-Git @IGParams -Arguments "log --format=%B -n 1 $( (Get-Item -Path "ENV:$_").Value )").Output break } # Jenkins - thanks to mipadi http://stackoverflow.com/a/3357357/3067642 } 'BUILD_SOURCEVERSION' { if($WeCanGit) { - Invoke-Git @IGParams -Arguments "log --format=%B -n 1 $( (Get-Item -Path "ENV:$_").Value )" + (Invoke-Git @IGParams -Arguments "log --format=%B -n 1 $( (Get-Item -Path "ENV:$_").Value )").Output break } # VSTS (https://www.visualstudio.com/en-us/docs/build/define/variables#) } 'BUILD_VCS_NUMBER' { if($WeCanGit) { - Invoke-Git @IGParams -Arguments "log --format=%B -n 1 $( (Get-Item -Path "ENV:$_").Value )" + (Invoke-Git @IGParams -Arguments "log --format=%B -n 1 $( (Get-Item -Path "ENV:$_").Value )").Output break } # Teamcity https://confluence.jetbrains.com/display/TCD10/Predefined+Build+Parameters } 'BAMBOO_REPOSITORY_REVISION_NUMBER' { if($WeCanGit) { - Invoke-Git @IGParams -Arguments "log --format=%B -n 1 $( (Get-Item -Path "ENV:$_").Value )" + (Invoke-Git @IGParams -Arguments "log --format=%B -n 1 $( (Get-Item -Path "ENV:$_").Value )").Output break } # Bamboo https://confluence.atlassian.com/bamboo/bamboo-variables-289277087.html } @@ -204,7 +204,7 @@ function Get-BuildVariable { { if($WeCanGit) { - $CommitMessage = Invoke-Git @IGParams -Arguments "log --format=%B -n 1" + $CommitMessage = (Invoke-Git @IGParams -Arguments "log --format=%B -n 1").Output } } diff --git a/BuildHelpers/Public/Get-GitChangedFile.ps1 b/BuildHelpers/Public/Get-GitChangedFile.ps1 index 49f852c..382bdb4 100644 --- a/BuildHelpers/Public/Get-GitChangedFile.ps1 +++ b/BuildHelpers/Public/Get-GitChangedFile.ps1 @@ -62,7 +62,7 @@ function Get-GitChangedFile { [switch]$Resolve ) $Path = (Resolve-Path $Path).Path - $GitPathRaw = Invoke-Git rev-parse --show-toplevel -Path $Path + $GitPathRaw = (Invoke-Git rev-parse --show-toplevel -Path $Path).Output Write-Verbose "Found git root [$GitPathRaw]" $GitPath = Resolve-Path $GitPathRaw if(Test-Path $GitPath) @@ -76,14 +76,14 @@ function Get-GitChangedFile { if(-not $PSBoundParameters.ContainsKey('Commit')) { - $Commit = Invoke-Git rev-parse HEAD -Path $GitPath + $Commit = (Invoke-Git rev-parse HEAD -Path $GitPath).Output } if(-not $Commit) { return } - [string[]]$Files = Invoke-Git "diff-tree --no-commit-id --name-only -r $Commit" -Path $GitPath + [string[]]$Files = (Invoke-Git diff-tree --no-commit-id --name-only -r $Commit -Path $GitPath).Output if($Files.Count -gt 0) { $Params = @{Collection = $Files} diff --git a/BuildHelpers/Public/Invoke-Git.ps1 b/BuildHelpers/Public/Invoke-Git.ps1 index 3cd55f2..4a72f60 100644 --- a/BuildHelpers/Public/Invoke-Git.ps1 +++ b/BuildHelpers/Public/Invoke-Git.ps1 @@ -1,119 +1,86 @@ Function Invoke-Git { <# - .SYNOPSIS - Wrapper to invoke git and return streams + .SYNOPSIS + Wrapper to invoke git and return streams - .FUNCTIONALITY - CI/CD + .FUNCTIONALITY + CI/CD - .DESCRIPTION - Wrapper to invoke git and return streams + .DESCRIPTION + Wrapper to invoke git and return streams - .PARAMETER Arguments - If specified, call git with these arguments. + .PARAMETER Arguments + If specified, call git with these arguments. - This takes a positional argument and accepts all value afterwards for a more natural 'git-esque' use. + This takes a positional argument and accepts all value afterwards for a more natural 'git-esque' use. - .PARAMETER Path - Working directory to launch git within. Defaults to current location + .PARAMETER Path + Working directory to launch git within. Defaults to current location - .PARAMETER RedirectStandardError - Whether to capture standard error. Defaults to $true + .PARAMETER GitPath + Path to git. Defaults to git (i.e. git is in $ENV:PATH) - .PARAMETER RedirectStandardOutput - Whether to capture standard output. Defaults to $true + .EXAMPLE + Invoke-Git rev-parse HEAD - .PARAMETER UseShellExecute - See System.Diagnostics.ProcessStartInfo. Defaults to $false + # Get the current commit hash for HEAD - .PARAMETER Raw - If specified, return an object with the command, output, and error properties. + .EXAMPLE + Invoke-Git rev-parse HEAD -path C:\sc\PSStackExchange - Without Raw or Quiet, we return output if there's output, and we write an error if there are errors + # Get the current commit hash for HEAD for the repo located at C:\sc\PSStackExchange - .PARAMETER Split - If specified, split output and error on this. Defaults to `n + .LINK + https://github.com/RamblingCookieMonster/BuildHelpers - .PARAMETER Quiet - If specified, do not return output + .LINK + about_BuildHelpers + #> + [cmdletbinding()] + param( + [parameter(Position = 0, + ValueFromRemainingArguments = $true)] + $Arguments, - .PARAMETER GitPath - Path to git. Defaults to git (i.e. git is in $ENV:PATH) + $Path = $PWD.Path, - .EXAMPLE - Invoke-Git rev-parse HEAD - - # Get the current commit hash for HEAD - - .EXAMPLE - Invoke-Git rev-parse HEAD -path C:\sc\PSStackExchange - - # Get the current commit hash for HEAD for the repo located at C:\sc\PSStackExchange - - .LINK - https://github.com/RamblingCookieMonster/BuildHelpers - - .LINK - about_BuildHelpers - #> - [cmdletbinding()] - param( - [parameter(Position = 0, - ValueFromRemainingArguments = $true)] - $Arguments, - - $Path = $PWD.Path, - - [switch]$Quiet, - - [switch]$Raw, - - [validatescript({ - if(-not (Get-Command $_ -ErrorAction SilentlyContinue)) - { - throw "Could not find command at GitPath [$_]" - } - $true - })] - [string]$GitPath = 'git' - ) - - $Path = (Resolve-Path $Path).Path - if(!$PSBoundParameters.ContainsKey('GitPath')) { - $GitPath = (Get-Command $GitPath -ErrorAction Stop)[0].Path - } - - $result = & $GitPath $Arguments 2>&1 - - if(-not $Quiet) { - $output = [pscustomobject]@{ - Command = "$GitPath $Arguments" - Output = "" - Error = "" - } - if ($result.writeErrorStream) + [validatescript({ + if(-not (Get-Command $_ -ErrorAction SilentlyContinue)) { - $output.Error = $result.Exception.Message -join "`n" - } - else - { - $output.Output = $result -join "`n" + throw "Could not find command at GitPath [$_]" } + $true + })] + [string]$GitPath = 'git' + ) + + $Path = (Resolve-Path $Path).Path + if(!$PSBoundParameters.ContainsKey('GitPath')) { + $GitPath = (Get-Command $GitPath -ErrorAction Stop)[0].Path + } - if($Raw) - { - $output - } - else - { - if ($result.writeErrorStream) - { - $output.Error - } - else - { - $output.Output - } - } - } + try + { + Push-Location $Path + $result = & $GitPath $($Arguments -split " ") 2>&1 + } + finally + { + Pop-Location + } + + $output = [pscustomobject]@{ + Command = "$GitPath $Arguments" + Output = "" + Error = "" + } + if ($result.writeErrorStream) + { + $output.Error = $result.Exception.Message + } + else + { + $output.Output = $result } + $output +} diff --git a/Tests/BuildHelpers.Tests.ps1 b/Tests/BuildHelpers.Tests.ps1 index 4138b37..4a53c5a 100644 --- a/Tests/BuildHelpers.Tests.ps1 +++ b/Tests/BuildHelpers.Tests.ps1 @@ -274,7 +274,7 @@ Describe 'Get-GitChangedFile' { Describe 'Invoke-Git' { Context 'This repository' { It 'Should find the root of the BuildHelpers repo' { - Invoke-Git rev-parse --show-toplevel -Path $PSScriptRoot | Should BeLike "*BuildHelpers" + (Invoke-Git rev-parse --show-toplevel -Path $PSScriptRoot).Output | Should BeLike "*BuildHelpers" } }