Skip to content
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

Merged
merged 27 commits into from
Jan 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
c6f759c
Use git-status-cache-posh-client to retrieve git informaiton when mod…
cmarcusreid Jul 27, 2015
f970bd6
Read conflicted files from cache response.
cmarcusreid Aug 7, 2015
e032b91
Add dbg calls for timing.
cmarcusreid Aug 10, 2015
92a5562
Update parsing for renamed files.
cmarcusreid Aug 11, 2015
2d3cf04
Delay computing EnableFileStatusFromCache setting until first request…
cmarcusreid Aug 19, 2015
cf1b9bc
Fix whitespace.
cmarcusreid Aug 19, 2015
6bafdd4
Fix incorrect brace style.
cmarcusreid Aug 25, 2015
909b3bd
Merge branch 'master' into useGitStatusCache
cmarcusreid Aug 25, 2015
0551a36
Merge branch 'master' into useGitStatusCache
cmarcusreid Aug 28, 2015
caed8f3
Merge branch 'master' into useGitStatusCache
cmarcusreid Aug 30, 2015
002df86
Merge branch 'master' into useGitStatusCache
cmarcusreid Sep 6, 2015
008836e
Read stash count from cache.
cmarcusreid Sep 6, 2015
49bb147
Merge branch 'master' into useGitStatusCache
cmarcusreid Sep 25, 2015
0febf54
Merge branch 'master' into useGitStatusCache
cmarcusreid Jan 10, 2016
2f13488
Merge branch 'master' into useGitStatusCache
cmarcusreid Mar 12, 2016
b90f7df
Reformat
dahlbyk Jan 2, 2017
675299a
Merge branch 'master' into git-status-cache
dahlbyk Jan 2, 2017
03131e1
Update cache response parsing to add to existing lists rather than ov…
cmarcusreid Jan 2, 2017
3e13ee8
Add cache responses to existing lists
dahlbyk Jan 2, 2017
6c53ac1
Revert accidental whitepsace add.
cmarcusreid Jan 2, 2017
600a390
Merge remote-tracking branch 'remotes/upstream/git-status-cache' into…
cmarcusreid Jan 2, 2017
4735df7
Merge branch 'master' into git-status-cache
dahlbyk Jan 4, 2017
b07bce5
Use efficient ConvertTo-StringSeq
dahlbyk Jan 4, 2017
25a4c61
Inline ConvertTo-StringSeq
dahlbyk Jan 7, 2017
8e1aada
Merge branch 'master' into git-status-cache
dahlbyk Jan 7, 2017
2b202c0
Fix cast break on PSObjects returned by Select-Object.
cmarcusreid Jan 14, 2017
05015b9
Remove member enumeration to support earlier versions of PowerShell.
cmarcusreid Jan 15, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions GitPrompt.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ $global:GitPromptSettings = New-Object PSObject -Property @{

EnablePromptStatus = !$Global:GitMissing
EnableFileStatus = $true
EnableFileStatusFromCache = $null
RepositoriesInWhichToDisableFileStatus = @( ) # Array of repository paths
DescribeStyle = ''

Expand Down
121 changes: 77 additions & 44 deletions GitUtils.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -146,60 +148,91 @@ 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
Copy link
Collaborator

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.

Copy link
Collaborator Author

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.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it.

}
}
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))))
foreach ($indexRenamed in $cacheResponse.IndexRenamed) {
$indexModified.Add($indexRenamed.Old)
}
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))))
foreach ($workingRenamed in $cacheResponse.WorkingRenamed) {
$filesModified.Add($workingRenamed.Old)
}
continue
}
$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
}

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>\S+?)(?:\.\.\.(?<upstream>\S+))?(?: \[(?:ahead (?<ahead>\d+))?(?:, )?(?:behind (?<behind>\d+))?(?<gone>gone)?\])?$' {
if ($sw) { dbg "Status: $_" $sw }
'^## (?<branch>\S+?)(?:\.\.\.(?<upstream>\S+))?(?: \[(?:ahead (?<ahead>\d+))?(?:, )?(?:behind (?<behind>\d+))?(?<gone>gone)?\])?$' {
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']
$upstream = $matches['upstream']
$aheadBy = [int]$matches['ahead']
$behindBy = [int]$matches['behind']
$gone = [string]$matches['gone'] -eq 'gone'
continue
}

'^## Initial commit on (?<branch>\S+)$' {
if ($sw) { dbg "Status: $_" $sw }
'^## Initial commit on (?<branch>\S+)$' {
if ($sw) { dbg "Status: $_" $sw }

$branch = $matches['branch']
continue
}
$branch = $matches['branch']
continue
}

default { if ($sw) { dbg "Status: $_" $sw } }
default { if ($sw) { dbg "Status: $_" $sw } }

}
}
}

if(!$branch) { $branch = Get-GitBranch $gitDir $sw }
Expand Down