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 fe56325..4a72f60 100644 --- a/BuildHelpers/Public/Invoke-Git.ps1 +++ b/BuildHelpers/Public/Invoke-Git.ps1 @@ -1,5 +1,5 @@ Function Invoke-Git { -<# + <# .SYNOPSIS Wrapper to invoke git and return streams @@ -17,26 +17,6 @@ .PARAMETER Path Working directory to launch git within. Defaults to current location - .PARAMETER RedirectStandardError - Whether to capture standard error. Defaults to $true - - .PARAMETER RedirectStandardOutput - Whether to capture standard output. Defaults to $true - - .PARAMETER UseShellExecute - See System.Diagnostics.ProcessStartInfo. Defaults to $false - - .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 - - .PARAMETER Split - If specified, split output and error on this. Defaults to `n - - .PARAMETER Quiet - If specified, do not return output - .PARAMETER GitPath Path to git. Defaults to git (i.e. git is in $ENV:PATH) @@ -59,17 +39,11 @@ [cmdletbinding()] param( [parameter(Position = 0, - ValueFromRemainingArguments = $true)] + 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)) { @@ -81,73 +55,32 @@ ) $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')) + + try + { + Push-Location $Path + $result = & $GitPath $($Arguments -split " ") 2>&1 + } + finally { - $pinfo.Arguments = $Arguments - $Command = "$Command $Arguments" + Pop-Location + } + + $output = [pscustomobject]@{ + Command = "$GitPath $Arguments" + Output = "" + Error = "" } - $p = New-Object System.Diagnostics.Process - $p.StartInfo = $pinfo - $null = $p.Start() - $p.WaitForExit() - if($Quiet) + if ($result.writeErrorStream) { - return + $output.Error = $result.Exception.Message } else { - #there was a newline in output... - if($stdout = $p.StandardOutput.ReadToEnd()) - { - if($split) - { - $stdout = $stdout -split "`n" | Where-Object {$_} - } - $stdout = foreach($item in @($stdout)){ - $item.trim() - } - } - if($stderr = $p.StandardError.ReadToEnd()) - { - if($split) - { - $stderr = $stderr -split "`n" | Where-Object {$_} - } - $stderr = foreach($item in @($stderr)){ - $item.trim() - } - } - - if($Raw) - { - [pscustomobject]@{ - Command = $Command - Output = $stdout - Error = $stderr - } - } - else - { - if($stdout) - { - $stdout - } - if($stderr) - { - Write-Error $stderr.trim() - } - } + $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" } }