-
-
Notifications
You must be signed in to change notification settings - Fork 809
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
Use GitStatusCache when it's installed. #208
Changes from 26 commits
c6f759c
f970bd6
e032b91
92a5562
2d3cf04
cf1b9bc
6bafdd4
909b3bd
0551a36
caed8f3
002df86
008836e
49bb147
0febf54
2f13488
b90f7df
675299a
03131e1
3e13ee8
6c53ac1
600a390
4735df7
b07bce5
25a4c61
8e1aada
2b202c0
05015b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -120,6 +120,8 @@ function GetUniquePaths($pathCollections) { | |
$hash.Keys | ||
} | ||
|
||
$castStringSeq = [Linq.Enumerable].GetMethod("Cast").MakeGenericMethod([string]) | ||
|
||
function Get-GitStatus($gitDir = (Get-GitDirectory)) { | ||
$settings = $Global:GitPromptSettings | ||
$enabled = (-not $settings) -or $settings.EnablePromptStatus | ||
|
@@ -146,60 +148,93 @@ function Get-GitStatus($gitDir = (Get-GitDirectory)) { | |
$stashCount = 0 | ||
|
||
if($settings.EnableFileStatus -and !$(InDisabledRepository)) { | ||
dbg 'Getting status' $sw | ||
$status = Invoke-Utf8ConsoleCommand { git -c color.status=false status --short --branch 2>$null } | ||
if($settings.EnableStashStatus) { | ||
dbg 'Getting stash count' $sw | ||
$stashCount = $null | git stash list 2>$null | measure-object | Select-Object -expand Count | ||
if ($settings.EnableFileStatusFromCache -eq $null) { | ||
$settings.EnableFileStatusFromCache = (Get-Module GitStatusCachePoshClient) -ne $null | ||
} | ||
} | ||
else { | ||
$status = @() | ||
} | ||
|
||
dbg 'Parsing status' $sw | ||
switch -regex ($status) { | ||
'^(?<index>[^#])(?<working>.) (?<path1>.*?)(?: -> (?<path2>.*))?$' { | ||
if ($sw) { dbg "Status: $_" $sw } | ||
|
||
switch ($matches['index']) { | ||
'A' { $null = $indexAdded.Add($matches['path1']); break } | ||
'M' { $null = $indexModified.Add($matches['path1']); break } | ||
'R' { $null = $indexModified.Add($matches['path1']); break } | ||
'C' { $null = $indexModified.Add($matches['path1']); break } | ||
'D' { $null = $indexDeleted.Add($matches['path1']); break } | ||
'U' { $null = $indexUnmerged.Add($matches['path1']); break } | ||
if ($settings.EnableFileStatusFromCache) { | ||
dbg 'Getting status from cache' $sw | ||
$cacheResponse = Get-GitStatusFromCache | ||
dbg 'Parsing status' $sw | ||
|
||
$indexAdded.AddRange($castStringSeq.Invoke($null, (,@($cacheResponse.IndexAdded)))) | ||
$indexModified.AddRange($castStringSeq.Invoke($null, (,@($cacheResponse.IndexModified)))) | ||
$indexRenamedOld = $cacheResponse.IndexRenamed.Old | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just learned that you could do this today; what is this trick called (for search purposes)? It returns either null (zero paths), a string (one path), or an object[] (many paths). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Member enumeration but it is only available in PS v3 and higher. To make that work on PS v2, use: $indexRenamedOld = foreach ($obj in $cacheResponse.IndexRenamed) { $obj.Old } The above is a bit faster than both Select-Object and Foreach-Object and shouldn't mess with the type. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Drat. I'm reverting back to the initial foreach and add version for the renamed files as I don't see a way to use the AddRange version without doing more enumerations (extracting the property then casting from objects to strings). (Recall the earlier version with Select-Object didn't work due to Select-Object's return type.) Please feel free to make additional tweaks if there's a faster approach. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm content with this. |
||
if ($indexRenamedOld) { | ||
$indexModified.AddRange($castStringSeq.Invoke($null, (,@($indexRenamedOld)))) | ||
} | ||
switch ($matches['working']) { | ||
'?' { $null = $filesAdded.Add($matches['path1']); break } | ||
'A' { $null = $filesAdded.Add($matches['path1']); break } | ||
'M' { $null = $filesModified.Add($matches['path1']); break } | ||
'D' { $null = $filesDeleted.Add($matches['path1']); break } | ||
'U' { $null = $filesUnmerged.Add($matches['path1']); break } | ||
$indexDeleted.AddRange($castStringSeq.Invoke($null, (,@($cacheResponse.IndexDeleted)))) | ||
$indexUnmerged.AddRange($castStringSeq.Invoke($null, (,@($cacheResponse.Conflicted)))) | ||
|
||
$filesAdded.AddRange($castStringSeq.Invoke($null, (,@($cacheResponse.WorkingAdded)))) | ||
$filesModified.AddRange($castStringSeq.Invoke($null, (,@($cacheResponse.WorkingModified)))) | ||
$workingRenamedOld = $cacheResponse.WorkingRenamed.Old | ||
if ($workingRenamedOld) { | ||
$filesModified.AddRange($castStringSeq.Invoke($null, (,@($workingRenamedOld)))) | ||
} | ||
$filesDeleted.AddRange($castStringSeq.Invoke($null, (,@($cacheResponse.WorkingDeleted)))) | ||
$filesUnmerged.AddRange($castStringSeq.Invoke($null, (,@($cacheResponse.Conflicted)))) | ||
|
||
$branch = $cacheResponse.Branch | ||
$upstream = $cacheResponse.Upstream | ||
$aheadBy = $cacheResponse.AheadBy | ||
$behindBy = $cacheResponse.BehindBy | ||
|
||
if ($cacheResponse.Stashes) { $stashCount = $cacheResponse.Stashes.Length } | ||
if ($cacheResponse.State) { $branch += "|" + $cacheResponse.State } | ||
} else { | ||
dbg 'Getting status' $sw | ||
$status = Invoke-Utf8ConsoleCommand { git -c color.status=false status --short --branch 2>$null } | ||
if($settings.EnableStashStatus) { | ||
dbg 'Getting stash count' $sw | ||
$stashCount = $null | git stash list 2>$null | measure-object | Select-Object -expand Count | ||
} | ||
continue | ||
} | ||
|
||
'^## (?<branch>\S+?)(?:\.\.\.(?<upstream>\S+))?(?: \[(?:ahead (?<ahead>\d+))?(?:, )?(?:behind (?<behind>\d+))?(?<gone>gone)?\])?$' { | ||
if ($sw) { dbg "Status: $_" $sw } | ||
dbg 'Parsing status' $sw | ||
switch -regex ($status) { | ||
'^(?<index>[^#])(?<working>.) (?<path1>.*?)(?: -> (?<path2>.*))?$' { | ||
if ($sw) { dbg "Status: $_" $sw } | ||
|
||
switch ($matches['index']) { | ||
'A' { $null = $indexAdded.Add($matches['path1']); break } | ||
'M' { $null = $indexModified.Add($matches['path1']); break } | ||
'R' { $null = $indexModified.Add($matches['path1']); break } | ||
'C' { $null = $indexModified.Add($matches['path1']); break } | ||
'D' { $null = $indexDeleted.Add($matches['path1']); break } | ||
'U' { $null = $indexUnmerged.Add($matches['path1']); break } | ||
} | ||
switch ($matches['working']) { | ||
'?' { $null = $filesAdded.Add($matches['path1']); break } | ||
'A' { $null = $filesAdded.Add($matches['path1']); break } | ||
'M' { $null = $filesModified.Add($matches['path1']); break } | ||
'D' { $null = $filesDeleted.Add($matches['path1']); break } | ||
'U' { $null = $filesUnmerged.Add($matches['path1']); break } | ||
} | ||
continue | ||
} | ||
|
||
$branch = $matches['branch'] | ||
$upstream = $matches['upstream'] | ||
$aheadBy = [int]$matches['ahead'] | ||
$behindBy = [int]$matches['behind'] | ||
$gone = [string]$matches['gone'] -eq 'gone' | ||
continue | ||
} | ||
'^## (?<branch>\S+?)(?:\.\.\.(?<upstream>\S+))?(?: \[(?:ahead (?<ahead>\d+))?(?:, )?(?:behind (?<behind>\d+))?(?<gone>gone)?\])?$' { | ||
if ($sw) { dbg "Status: $_" $sw } | ||
|
||
'^## Initial commit on (?<branch>\S+)$' { | ||
if ($sw) { dbg "Status: $_" $sw } | ||
$branch = $matches['branch'] | ||
$upstream = $matches['upstream'] | ||
$aheadBy = [int]$matches['ahead'] | ||
$behindBy = [int]$matches['behind'] | ||
$gone = [string]$matches['gone'] -eq 'gone' | ||
continue | ||
} | ||
|
||
$branch = $matches['branch'] | ||
continue | ||
} | ||
'^## Initial commit on (?<branch>\S+)$' { | ||
if ($sw) { dbg "Status: $_" $sw } | ||
|
||
default { if ($sw) { dbg "Status: $_" $sw } } | ||
$branch = $matches['branch'] | ||
continue | ||
} | ||
|
||
default { if ($sw) { dbg "Status: $_" $sw } } | ||
|
||
} | ||
} | ||
} | ||
|
||
if(!$branch) { $branch = Get-GitBranch $gitDir $sw } | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we sure we want to do this? It makes it harder to "turn off" this feature if something is misbehaving (or you want to test with it off). Right now, you'd have to unload (remove-module GitStatusCachePoshClient) and then be careful not to execute a command that would auto-load it again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It auto-selects based on the module's presence the first time through (when set to the default null value), but if you manually set it to false that'll stick on subsequent calls since it's no longer null.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it.