@@ -59,7 +59,7 @@ function Get-GitDirectory {
5959 }
6060}
6161
62- function Get-GitBranch ($gitDir = $ (Get-GitDirectory ), [Diagnostics.Stopwatch ]$sw ) {
62+ function Get-GitBranch ($branch = $null , $ gitDir = $ (Get-GitDirectory ), [ switch ] $isDotGitOrBare , [Diagnostics.Stopwatch ]$sw ) {
6363 if (! $gitDir ) { return }
6464
6565 Invoke-Utf8ConsoleCommand {
@@ -114,7 +114,13 @@ function Get-GitBranch($gitDir = $(Get-GitDirectory), [Diagnostics.Stopwatch]$sw
114114 $r = ' |BISECTING'
115115 }
116116
117+ if ($step -and $total ) {
118+ $r += " $step /$total "
119+ }
120+
117121 $b = Invoke-NullCoalescing `
122+ $b `
123+ $branch `
118124 { dbg ' Trying symbolic-ref' $sw ; git -- no- optional- locks symbolic- ref HEAD - q 2> $null } `
119125 { ' ({0})' -f (Invoke-NullCoalescing `
120126 {
@@ -143,7 +149,7 @@ function Get-GitBranch($gitDir = $(Get-GitDirectory), [Diagnostics.Stopwatch]$sw
143149 return $Matches [' ref' ]
144150 }
145151 elseif ($ref -and $ref.Length -ge 7 ) {
146- return $ref.Substring (0 , 7 ) + ' ...'
152+ return $ref.Substring (0 , 7 ) + ' ...'
147153 }
148154 else {
149155 return ' unknown'
@@ -152,23 +158,21 @@ function Get-GitBranch($gitDir = $(Get-GitDirectory), [Diagnostics.Stopwatch]$sw
152158 ) }
153159 }
154160
155- dbg ' Inside git directory?' $sw
156- $revParseOut = git -- no- optional- locks rev- parse - -is - inside- git- dir 2> $null
157- if (' true' -eq $revParseOut ) {
158- dbg ' Inside git directory' $sw
159- $revParseOut = git -- no- optional- locks rev- parse - -is - bare- repository 2> $null
161+ if ($isDotGitOrBare -or ! $b ) {
162+ dbg ' Inside git directory?' $sw
163+ $revParseOut = git -- no- optional- locks rev- parse - -is - inside- git- dir 2> $null
160164 if (' true' -eq $revParseOut ) {
161- $c = ' BARE:'
162- }
163- else {
164- $b = ' GIT_DIR!'
165+ dbg ' Inside git directory' $sw
166+ $revParseOut = git -- no- optional- locks rev- parse - -is - bare- repository 2> $null
167+ if (' true' -eq $revParseOut ) {
168+ $c = ' BARE:'
169+ }
170+ else {
171+ $b = ' GIT_DIR!'
172+ }
165173 }
166174 }
167175
168- if ($step -and $total ) {
169- $r += " $step /$total "
170- }
171-
172176 " $c $ ( $b -replace ' refs/heads/' , ' ' ) $r "
173177 }
174178}
@@ -228,7 +232,7 @@ function Get-GitStatus {
228232 param (
229233 # The path of a directory within a Git repository that you want to get
230234 # the Git status.
231- [Parameter (Position = 0 )]
235+ [Parameter (Position = 0 )]
232236 $GitDir = (Get-GitDirectory ),
233237
234238 # If specified, overrides $GitPromptSettings.EnableFileStatus and
@@ -266,7 +270,8 @@ function Get-GitStatus {
266270 $stashCount = 0
267271
268272 $fileStatusEnabled = $Force -or $settings.EnableFileStatus
269- if ($fileStatusEnabled -and ! $ (InDotGitOrBareRepoDir $GitDir ) -and ! $ (InDisabledRepository)) {
273+ # Optimization: short-circuit to avoid InDotGitOrBareRepoDir and InDisabledRepository if !$fileStatusEnabled
274+ if ($fileStatusEnabled -and ! $ ($isDotGitOrBare = InDotGitOrBareRepoDir $GitDir ) -and ! $ (InDisabledRepository)) {
270275 if ($null -eq $settings.EnableFileStatusFromCache ) {
271276 $settings.EnableFileStatusFromCache = $null -ne (Get-Module GitStatusCachePoshClient)
272277 }
@@ -285,21 +290,21 @@ function Get-GitStatus {
285290 else {
286291 dbg ' Parsing status' $sw
287292
288- $indexAdded.AddRange ($castStringSeq.Invoke ($null , (, @ ($cacheResponse.IndexAdded ))))
289- $indexModified.AddRange ($castStringSeq.Invoke ($null , (, @ ($cacheResponse.IndexModified ))))
293+ $indexAdded.AddRange ($castStringSeq.Invoke ($null , (, @ ($cacheResponse.IndexAdded ))))
294+ $indexModified.AddRange ($castStringSeq.Invoke ($null , (, @ ($cacheResponse.IndexModified ))))
290295 foreach ($indexRenamed in $cacheResponse.IndexRenamed ) {
291296 $indexModified.Add ($indexRenamed.Old )
292297 }
293- $indexDeleted.AddRange ($castStringSeq.Invoke ($null , (, @ ($cacheResponse.IndexDeleted ))))
294- $indexUnmerged.AddRange ($castStringSeq.Invoke ($null , (, @ ($cacheResponse.Conflicted ))))
298+ $indexDeleted.AddRange ($castStringSeq.Invoke ($null , (, @ ($cacheResponse.IndexDeleted ))))
299+ $indexUnmerged.AddRange ($castStringSeq.Invoke ($null , (, @ ($cacheResponse.Conflicted ))))
295300
296- $filesAdded.AddRange ($castStringSeq.Invoke ($null , (, @ ($cacheResponse.WorkingAdded ))))
297- $filesModified.AddRange ($castStringSeq.Invoke ($null , (, @ ($cacheResponse.WorkingModified ))))
301+ $filesAdded.AddRange ($castStringSeq.Invoke ($null , (, @ ($cacheResponse.WorkingAdded ))))
302+ $filesModified.AddRange ($castStringSeq.Invoke ($null , (, @ ($cacheResponse.WorkingModified ))))
298303 foreach ($workingRenamed in $cacheResponse.WorkingRenamed ) {
299304 $filesModified.Add ($workingRenamed.Old )
300305 }
301- $filesDeleted.AddRange ($castStringSeq.Invoke ($null , (, @ ($cacheResponse.WorkingDeleted ))))
302- $filesUnmerged.AddRange ($castStringSeq.Invoke ($null , (, @ ($cacheResponse.Conflicted ))))
306+ $filesDeleted.AddRange ($castStringSeq.Invoke ($null , (, @ ($cacheResponse.WorkingDeleted ))))
307+ $filesUnmerged.AddRange ($castStringSeq.Invoke ($null , (, @ ($cacheResponse.Conflicted ))))
303308
304309 $branch = $cacheResponse.Branch
305310 $upstream = $cacheResponse.Upstream
@@ -319,9 +324,9 @@ function Get-GitStatus {
319324 else {
320325 dbg ' Getting status' $sw
321326 switch ($settings.UntrackedFilesMode ) {
322- " No" { $untrackedFilesOption = " -uno" }
323- " All" { $untrackedFilesOption = " -uall" }
324- default { $untrackedFilesOption = " -unormal" }
327+ " No" { $untrackedFilesOption = " -uno" }
328+ " All" { $untrackedFilesOption = " -uall" }
329+ default { $untrackedFilesOption = " -unormal" }
325330 }
326331 $status = Invoke-Utf8ConsoleCommand { git -- no- optional- locks - c core.quotepath= false - c color.status= false status $untrackedFilesOption -- short -- branch 2> $null }
327332 if ($settings.EnableStashStatus ) {
@@ -384,41 +389,41 @@ function Get-GitStatus {
384389 }
385390 }
386391
387- if ( ! $branch ) { $branch = Get-GitBranch $ GitDir $sw }
392+ $branch = Get-GitBranch - Branch $branch - GitDir $GitDir - IsDotGitOrBare: $isDotGitOrBare - sw $sw
388393
389394 dbg ' Building status object' $sw
390395
391396 # This collection is used twice, so create the array just once
392397 $filesAdded = $filesAdded.ToArray ()
393398
394- $indexPaths = @ (GetUniquePaths $indexAdded , $indexModified , $indexDeleted , $indexUnmerged )
395- $workingPaths = @ (GetUniquePaths $filesAdded , $filesModified , $filesDeleted , $filesUnmerged )
396- $index = (, $indexPaths ) |
399+ $indexPaths = @ (GetUniquePaths $indexAdded , $indexModified , $indexDeleted , $indexUnmerged )
400+ $workingPaths = @ (GetUniquePaths $filesAdded , $filesModified , $filesDeleted , $filesUnmerged )
401+ $index = (, $indexPaths ) |
397402 Add-Member - Force - PassThru NoteProperty Added $indexAdded.ToArray () |
398403 Add-Member - Force - PassThru NoteProperty Modified $indexModified.ToArray () |
399404 Add-Member - Force - PassThru NoteProperty Deleted $indexDeleted.ToArray () |
400405 Add-Member - Force - PassThru NoteProperty Unmerged $indexUnmerged.ToArray ()
401406
402- $working = (, $workingPaths ) |
407+ $working = (, $workingPaths ) |
403408 Add-Member - Force - PassThru NoteProperty Added $filesAdded |
404409 Add-Member - Force - PassThru NoteProperty Modified $filesModified.ToArray () |
405410 Add-Member - Force - PassThru NoteProperty Deleted $filesDeleted.ToArray () |
406411 Add-Member - Force - PassThru NoteProperty Unmerged $filesUnmerged.ToArray ()
407412
408413 $result = New-Object PSObject - Property @ {
409- GitDir = $GitDir
410- RepoName = Split-Path (Split-Path $GitDir - Parent) - Leaf
411- Branch = $branch
412- AheadBy = $aheadBy
413- BehindBy = $behindBy
414- UpstreamGone = $gone
415- Upstream = $upstream
416- HasIndex = [bool ]$index
417- Index = $index
418- HasWorking = [bool ]$working
419- Working = $working
420- HasUntracked = [bool ]$filesAdded
421- StashCount = $stashCount
414+ GitDir = $GitDir
415+ RepoName = Split-Path (Split-Path $GitDir - Parent) - Leaf
416+ Branch = $branch
417+ AheadBy = $aheadBy
418+ BehindBy = $behindBy
419+ UpstreamGone = $gone
420+ Upstream = $upstream
421+ HasIndex = [bool ]$index
422+ Index = $index
423+ HasWorking = [bool ]$working
424+ Working = $working
425+ HasUntracked = [bool ]$filesAdded
426+ StashCount = $stashCount
422427 }
423428
424429 dbg ' Finished' $sw
@@ -453,7 +458,7 @@ function InDotGitOrBareRepoDir([string][ValidateNotNullOrEmpty()]$GitDir) {
453458
454459function Get-AliasPattern ($cmd ) {
455460 $aliases = @ ($cmd ) + @ (Get-Alias | Where-Object { $_.Definition -match " ^$cmd (\.exe)?$" } | Foreach-Object Name)
456- " ($ ( $aliases -join ' |' ) )"
461+ " ($ ( $aliases -join ' |' ) )"
457462}
458463
459464<#
@@ -531,16 +536,16 @@ function Get-AliasPattern($cmd) {
531536 Shows the branches, both merged and unmerged, that match the specified wildcard that would be deleted without actually deleting them. Once you've verified the list of branches looks correct, remove the WhatIf parameter to actually delete the branches.
532537#>
533538function Remove-GitBranch {
534- [CmdletBinding (DefaultParameterSetName = " Wildcard" , SupportsShouldProcess , ConfirmImpact = " Medium" )]
539+ [CmdletBinding (DefaultParameterSetName = " Wildcard" , SupportsShouldProcess , ConfirmImpact = " Medium" )]
535540 param (
536541 # Specifies a regular expression pattern for the branches that will be deleted. Certain branches are always excluded from deletion e.g. the current branch as well as the develop and master branches. See the ExcludePattern parameter to modify that pattern.
537- [Parameter (Position = 0 , Mandatory , ParameterSetName = " Wildcard" )]
542+ [Parameter (Position = 0 , Mandatory , ParameterSetName = " Wildcard" )]
538543 [ValidateNotNullOrEmpty ()]
539544 [string ]
540545 $Name ,
541546
542547 # Specifies a regular expression for the branches that will be deleted. Certain branches are always excluded from deletion e.g. the current branch as well as the develop and master branches. See the ExcludePattern parameter to modify that pattern.
543- [Parameter (Position = 0 , Mandatory , ParameterSetName = " Pattern" )]
548+ [Parameter (Position = 0 , Mandatory , ParameterSetName = " Pattern" )]
544549 [ValidateNotNull ()]
545550 [string ]
546551 $Pattern ,
@@ -579,7 +584,7 @@ function Remove-GitBranch {
579584 $branches = git branch -- merged $Commit
580585 }
581586
582- $filteredBranches = $branches | Where-Object {$_ -notmatch $ExcludePattern }
587+ $filteredBranches = $branches | Where-Object { $_ -notmatch $ExcludePattern }
583588
584589 if ($PSCmdlet.ParameterSetName -eq " Wildcard" ) {
585590 $branchesToDelete = $filteredBranches | Where-Object { $_.Trim () -like $Name }
@@ -588,15 +593,16 @@ function Remove-GitBranch {
588593 $branchesToDelete = $filteredBranches | Where-Object { $_ -match $Pattern }
589594 }
590595
591- $action = if ($DeleteForce ) { " delete with force" } else { " delete" }
596+ $action = if ($DeleteForce ) { " delete with force" } else { " delete" }
592597 $yesToAll = $noToAll = $false
593598
594599 foreach ($branch in $branchesToDelete ) {
595600 $targetBranch = $branch.Trim ()
596601 if ($PSCmdlet.ShouldProcess ($targetBranch , $action )) {
597602 if ($Force -or $yesToAll -or
598- $PSCmdlet.ShouldContinue (" Are you REALLY sure you want to $action `" $targetBranch `" ?" ,
599- " Confirm branch deletion" , [ref ]$yesToAll , [ref ]$noToAll )) {
603+ $PSCmdlet.ShouldContinue (
604+ " Are you REALLY sure you want to $action `" $targetBranch `" ?" ,
605+ " Confirm branch deletion" , [ref ]$yesToAll , [ref ]$noToAll )) {
600606
601607 if ($noToAll ) { return }
602608
0 commit comments