forked from dahlbyk/posh-git
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitPromptAsync.ps1
More file actions
131 lines (122 loc) · 5.19 KB
/
GitPromptAsync.ps1
File metadata and controls
131 lines (122 loc) · 5.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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
}
}
}