Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed paginated results of git commands #91

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 8 additions & 8 deletions BuildHelpers/Public/Get-BuildVariable.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -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
}
Expand All @@ -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
}
}

Expand Down
6 changes: 3 additions & 3 deletions BuildHelpers/Public/Get-GitChangedFile.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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}
Expand Down
109 changes: 21 additions & 88 deletions BuildHelpers/Public/Invoke-Git.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Function Invoke-Git {
<#
<#
.SYNOPSIS
Wrapper to invoke git and return streams

Expand All @@ -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)

Expand All @@ -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))
{
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion Tests/BuildHelpers.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

Expand Down