Public/Invoke-Git.ps1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
Function Invoke-Git { <# .SYNOPSIS Wrapper to invoke git and return streams .FUNCTIONALITY CI/CD .DESCRIPTION Wrapper to invoke git and return streams .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. .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) .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, $NoWindow = $true, $RedirectStandardError = $true, $RedirectStandardOutput = $true, $UseShellExecute = $false, $Path = $PWD.Path, $Quiet, $Split = "`n", $Raw, [validatescript({ if(-not (Get-Command $_)) { throw "Could not find command at GitPath [$_]" } $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)[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) { $stdout = $stdout -split "`n" | Where {$_} } $stdout = foreach($item in @($stdout)){ $item.trim() } } if($stderr = $p.StandardError.ReadToEnd()) { if($split) { $stderr = $stderr -split "`n" | Where {$_} } $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() } } } } |