@@ -128,13 +128,78 @@ function setup_proxy() {
128128 }
129129}
130130
131- function git_cmd {
131+ function Invoke-Git {
132+ [CmdletBinding ()]
133+ [OutputType ([String ])]
134+ param (
135+ [Parameter (Mandatory = $false , Position = 0 )]
136+ [Alias (' PSPath' , ' Path' )]
137+ [ValidateNotNullOrEmpty ()]
138+ [String ]
139+ $WorkingDirectory ,
140+ [Parameter (Mandatory = $true , Position = 1 )]
141+ [Alias (' Args' )]
142+ [String []]
143+ $ArgumentList
144+ )
145+
132146 $proxy = get_config PROXY
133- $cmd = " git $ ( $args | ForEach-Object { " $_ " }) "
134- if ($proxy -and $proxy -ne ' none' ) {
135- $cmd = " SET HTTPS_PROXY=$proxy &&SET HTTP_PROXY=$proxy &&$cmd "
147+ $git = Get-HelperPath - Helper Git
148+ $arguments = $ArgumentList -join ' '
149+ $cmd = " `" $git `" $arguments "
150+
151+ if ($WorkingDirectory ) {
152+ $cmd = " `" $git `" -C `" $WorkingDirectory `" $arguments "
153+ }
154+ $sb = [scriptblock ]::Create(" & $cmd " )
155+
156+ if ([String ]::IsNullOrEmpty($proxy ) -or $proxy -eq ' none' ) {
157+ return Invoke-Command $sb
158+ }
159+
160+ if ($arguments -Match ' \b(clone|checkout|pull|fetch|ls-remote)\b' ) {
161+ $old_https = $env: HTTPS_PROXY
162+ $old_http = $env: HTTP_PROXY
163+ try {
164+ # convert proxy setting for git
165+ if ($proxy.StartsWith (' currentuser@' )) {
166+ $proxy = $proxy.Replace (' currentuser@' , ' :@' )
167+ }
168+ $env: HTTPS_PROXY = $proxy
169+ $env: HTTP_PROXY = $proxy
170+ return Invoke-Command $sb
171+ }
172+ catch {
173+ error $_
174+ return
175+ }
176+ finally {
177+ $env: HTTPS_PROXY = $old_https
178+ $env: HTTP_PROXY = $old_http
179+ }
180+ }
181+
182+ return Invoke-Command $sb
183+ }
184+
185+ function Invoke-GitLog {
186+ [CmdletBinding ()]
187+ Param (
188+ [Parameter (Mandatory , ValueFromPipeline )]
189+ [String ]$Path ,
190+ [Parameter (Mandatory , ValueFromPipeline )]
191+ [String ]$CommitHash ,
192+ [String ]$Name = ' '
193+ )
194+ Process {
195+ if ($Name ) {
196+ if ($Name.Length -gt 12 ) {
197+ $Name = " $ ( $Name.Substring (0 , 10 )) .."
198+ }
199+ $Name = " %Cgreen$ ( $Name.PadRight (12 , ' ' ).Substring(0 , 12 )) %Creset "
200+ }
201+ Invoke-Git - Path $Path - ArgumentList @ (' --no-pager' , ' log' , ' --color' , ' --no-decorate' , " --grep='^(chore)'" , ' --invert-grep' , ' --abbrev=12' , " --format='tformat: * %C(yellow)%h%Creset %<|(72,trunc)%s $Name %C(cyan)%cr%Creset'" , " $CommitHash ..HEAD" )
136202 }
137- cmd.exe / d / c $cmd
138203}
139204
140205# helper functions
@@ -293,12 +358,16 @@ Function Test-CommandAvailable {
293358 Return [Boolean ](Get-Command $Name - ErrorAction Ignore)
294359}
295360
361+ Function Test-GitAvailable {
362+ Return [Boolean ](Test-Path (Get-HelperPath - Helper Git) - ErrorAction Ignore)
363+ }
364+
296365function Get-HelperPath {
297366 [CmdletBinding ()]
298367 [OutputType ([String ])]
299368 param (
300369 [Parameter (Mandatory = $true , Position = 0 , ValueFromPipeline = $true )]
301- [ValidateSet (' 7zip' , ' Lessmsi' , ' Innounp' , ' Dark' , ' Aria2' , ' Zstd' )]
370+ [ValidateSet (' Git ' , ' 7zip' , ' Lessmsi' , ' Innounp' , ' Dark' , ' Aria2' , ' Zstd' )]
302371 [String ]
303372 $Helper
304373 )
@@ -307,6 +376,14 @@ function Get-HelperPath {
307376 }
308377 process {
309378 switch ($Helper ) {
379+ ' Git' {
380+ $internalgit = " $ ( versiondir ' git' ' current' ) \mingw64\bin\git.exe"
381+ if (Test-Path $internalgit ) {
382+ $HelperPath = $internalgit
383+ } else {
384+ $HelperPath = (Get-Command git - ErrorAction Ignore).Source
385+ }
386+ }
310387 ' 7zip' {
311388 $HelperPath = Get-AppFilePath ' 7zip' ' 7z.exe'
312389 if ([String ]::IsNullOrEmpty($HelperPath )) {
0 commit comments