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

asynchronous git prompt updates #51

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions GitPrompt.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,6 @@ function Global:Write-VcsStatus { $Global:VcsPromptStatuses | foreach { & $_ } }

# Add scriptblock that will execute for Write-VcsStatus
$Global:VcsPromptStatuses += {
$Global:GitStatus = Get-GitStatus
$Global:GitStatus = Get-GitPromptStatus
Write-GitStatus $GitStatus
}
}
131 changes: 131 additions & 0 deletions GitPromptAsync.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
function Start-GitPrompt([switch] $Asynchronous) {
if (-not $GitPromptState) {
if ($Asynchronous) {
$timer = New-Object Timers.Timer -Property @{
Interval = 250
AutoReset = $true
Enabled = $true
}
Set-Variable GitPromptState -Scope Global -Value (New-Object PSObject -Property @{
Asynchronous = $true
Repositories = @{ }
Timer = $timer
Elapsed = Register-ObjectEvent $timer Elapsed -Action { Update-GitPromptRepositories }
})
}
else {
Set-Variable GitPromptState -Scope Global -Value (New-Object PSObject -Property @{
Asynchronous = $false
Repositories = @{ }
})
}
}
}

function Update-GitPromptRepositories {
if ($GitPromptState) {
$repositories = $GitPromptState.Repositories
foreach($key in $repositories.Keys) {
$repository = $repositories[$key]
if ($repository.LastStatusUpdate -lt $repository.LastUpdate) {
if ($repository.LastStatusUpdate.AddMilliseconds($repository.StatusUpdateIntervalBuffer) -lt [DateTime]::Now) {
Push-Location $repository.Path
try { $repository.Status = Get-GitStatus $repository.Path }
finally { Pop-Location }
}
$repository.LastStatusUpdate = [DateTime]::Now
}
}
}
}

function Stop-GitPrompt {
if ($GitPromptState) {
if ($GitPromptState.Asynchronous) {
$GitPromptState.Timer.Enabled = $false
Unregister-Event $GitPromptState.Elapsed.Id
}
$GitPromptState.Repositories.Keys | ForEach-Object { Stop-GitPromptRepository $_ }
Remove-Variable GitPromptState -Scope Global
}
}

function Start-GitPromptRepository($Path) {
if ($GitPromptState -and (-not $GitPromptState.Repositories.ContainsKey($Path))) {
$repositories = $GitPromptState.Repositories.Clone()
$watcher = New-Object IO.FileSystemWatcher $Path, '*.*' -Property @{
IncludeSubdirectories = $true
EnableRaisingEvents = $true
}
$watcherAction = [scriptblock]::Create( { Publish-GitPromptRepositoryUpdated $Path $eventArgs.Name }.ToString().Replace('$Path', $Path) )

$repositories[$Path] = New-Object PSObject -Property @{
Path = $Path
Status = (Get-GitStatus $Path)
StatusUpdateIntervalBuffer = 500
LastStatusUpdate = ([DateTime]::Now)
LastUpdate = ([DateTime]::Now)
Watcher = $watcher
Changed = Register-ObjectEvent $watcher Changed -Action $watcherAction
Created = Register-ObjectEvent $watcher Created -Action $watcherAction
Deleted = Register-ObjectEvent $watcher Deleted -Action $watcherAction
Renamed = Register-ObjectEvent $watcher Renamed -Action $watcherAction
Ignore = @('^\.git$', '^\.git\\index\.lock$', '^\.git\\objects\\')
}
$GitPromptState.Repositories = $repositories
}
}

function Publish-GitPromptRepositoryUpdated($Path, $File) {
if ($GitPromptState -and $GitPromptState.Repositories.ContainsKey($Path)) {
$repository = $GitPromptState.Repositories.Get_Item($Path)
if ($repository.Ignore | Where-Object { $File -match $_ }) { return }
$repository.LastUpdate = [DateTime]::Now
}
}

function Stop-GitPromptRepository($Path) {
if ($GitPromptState -and $GitPromptState.Repositories.ContainsKey($Path)) {
$repository = $GitPromptState.Repositories[$Path]
$repository.Watcher.EnableRaisingEvents = $false
Unregister-Event $repository.Changed.Id
Unregister-Event $repository.Created.Id
Unregister-Event $repository.Deleted.Id
Unregister-Event $repository.Renamed.Id

$repositories = $GitPromptState.Repositories.Clone()
$repositories.Remove($Path)
$GitPromptState.Repositories = $repositories
}
}

function Update-GitPromptRepository($Path) {
if ($GitPromptState -and $GitPromptState.Repositories.ContainsKey($Path) -and (-not $GitPromptState.Asynchronous)) {
$repository = $GitPromptState.Repositories[$Path]
if ($repository.LastStatusUpdate -lt $repository.LastUpdate) {
Push-Location $repository.Path
try { $repository.Status = Get-GitStatus $repository.Path }
finally { Pop-Location }
$repository.LastStatusUpdate = [DateTime]::Now
}
}
}

function Get-GitPromptStatus {
$Path = Get-GitDirectory
if ($Path) {
$repositoryPath = Split-Path -parent $Path
if ($GitPromptState) {
if ($GitPromptState.Repositories.ContainsKey($repositoryPath)) {
Update-GitPromptRepository $repositoryPath
}
else {
Start-GitPromptRepository $repositoryPath
}
$GitPromptState.Repositories[$repositoryPath].Status
}
else {
Get-GitStatus $Path
}
}
}
9 changes: 7 additions & 2 deletions posh-git.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Push-Location $psScriptRoot
. ./Utils.ps1
. ./GitUtils.ps1
. ./GitPrompt.ps1
. ./GitPromptAsync.ps1
. ./GitTabExpansion.ps1
. ./TortoiseGit.ps1
Pop-Location
Expand All @@ -15,7 +16,12 @@ if (!$Env:HOME) { $Env:HOME = "$Env:USERPROFILE" }

Export-ModuleMember -Function @(
'Write-GitStatus',
'Get-GitStatus',
'Get-GitStatus',
'Start-GitPrompt',
'Update-GitPromptRepositories',
'Publish-GitPromptRepositoryUpdated',
'Stop-GitPrompt',
'Get-GitPromptStatus',
'Enable-GitColors',
'Get-GitDirectory',
'TabExpansion',
Expand All @@ -24,4 +30,3 @@ Export-ModuleMember -Function @(
'Stop-SshAgent',
'Add-SshKey',
'tgit')