Skip to content

Instantly share code, notes, and snippets.

@ninmonkey
Last active August 24, 2024 22:32
Show Gist options
  • Save ninmonkey/7be44ed72de3b62c33de060163865183 to your computer and use it in GitHub Desktop.
Save ninmonkey/7be44ed72de3b62c33de060163865183 to your computer and use it in GitHub Desktop.
using `[List[Object]]` to build native `git` command arguments

Examples

GetLogs 4 -OneLine
GetLogs -Since ([datetime]::Now.AddDays(-7)) -OneLine -Limit 3
GetLogs -Since ([datetime]::Now.AddDays(-7)) -OneLine
GetLogs -Since ([datetime]::Now.AddDays(-7))
using namespace System.Collections.Generic
function GetLogs {
# BinArgs is built conditionally based on what parameters are used
param(
[int] $Limit,
[datetime] $Since,
[switch] $OneLine,
# preview the command line that's generated without running git
[switch] $WhatIf
)
[List[Object]] $binArgs = @( 'log' )
if( $Limit -gt 0 ) {
$binArgs.AddRange(@(
'-n', $Limit ))
}
if( $OneLine ) { $binArgs.Add( '--pretty=oneline' ) }
if( $Since ) {
$binArgs.AddRange(@(
('--since={0}' -f $Since.ToString('u') )
))
}
$binArgs -join ' ' | Write-verbose -verbose
if( $WhatIf ) { return }
& git @binArgs
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment