forked from TSYS-Merchant/posh-svn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SvnUtils.ps1
262 lines (231 loc) · 8.22 KB
/
SvnUtils.ps1
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
function Get-SvnDirectory() {
$pathInfo = Microsoft.PowerShell.Management\Get-Location
if (!$pathInfo -or ($pathInfo.Provider.Name -ne 'FileSystem')) {
return $null
}
else {
$currentDir = Get-Item $pathInfo -Force
while ($currentDir) {
$svnPath = Join-Path $currentDir.FullName .svn
if (Test-Path -LiteralPath $svnPath -PathType Container) {
# return the original path, NOT the .svn path
return $currentDir.FullName
}
# .svn can't be a file, so nothing else to try
$currentDir = $currentDir.Parent
}
}
}
function Get-SvnInfo {
[OutputType([Hashtable])]
param (
[ValidateNotNullOrEmpty()]
[string] $Path = '.'
)
$result = @{}
try {
& $svn info $Path 2> $null |
Where-Object { $_ } | # eat blank lines
ForEach-Object {
if ($_ -imatch '^(?<Name>[^:]*?)\s*:\s*(?<Value>.*?)\s*$') {
$name = $Matches.Name;
$value = switch ($name) {
'URL' { [Uri] $Matches.Value }
default { $Matches.Value }
}
$result.Add($name, $value)
}
else {
throw "line did not match expected pattern: '$_'"
}
}
return $result
}
catch {
throw "argh: $_"
}
}
function Get-SvnStatus($svnDir = (Get-SvnDirectory)) {
$settings = $Global:SvnPromptSettings
$enabled = (-not $settings) -or $settings.EnablePromptStatus
if ($enabled -and $svnDir) {
$untracked = 0
$added = 0
$ignored = 0
$modified = 0
$replaced = 0
$deleted = 0
$missing = 0
$conflicted = 0
$external = 0
$obstructed = 0
$incoming = 0
$incomingRevision = $null
$info = Get-SvnInfo $svnDir
$branch = Get-SvnBranchName $info
$hostName = ([System.Uri]$info['URL']).Host #URL: http://svnserver/trunk/test
$statusArgs = @()
# EnableRemoteStatus: defaults to true
$showRemote = (-not $settings) -or $settings.EnableRemoteStatus
if ($showRemote -and (Test-Connection -computername $hostName -Quiet -Count 1 -BufferSize 1)) {
$statusArgs += '--show-updates'
}
# EnableExternalFileStatus: defaults to false
$showExternalFiles = $settings -and $settings.EnableExternalFileStatus
if (!$showExternalFiles) {
$statusArgs += '--ignore-externals'
}
& $svn status $statusArgs | ForEach-Object {
if ($_ -eq "") {
# blank line between externals
}
elseif ($_.StartsWith("Status against revision:")) {
if ($incomingRevision -eq $null) {
$incomingRevision = [Int]$_.Replace("Status against revision:", "")
}
}
elseif ($_.StartsWith("Performing status on external item at")) {
# External
# ignore for now.
}
else {
switch($_[0]) {
'A' { $added++; break; }
'C' { $conflicted++; break; }
'D' { $deleted++; break; }
'I' { $ignored++; break; }
'M' { $modified++; break; }
'R' { $replaced++; break; }
'X' { $external++; break; }
'?' { $untracked++; break; }
'!' { $missing++; break; }
'~' { $obstructed++; break; }
}
switch($_[1]) {
'A' { $added++; break; }
'C' { $conflicted++; break; }
'D' { $deleted++; break; }
'I' { $ignored++; break; }
'M' { $modified++; break; }
'R' { $replaced++; break; }
'X' { $external++; break; }
'?' { $untracked++; break; }
'!' { $missing++; break; }
'~' { $obstructed++; break; }
}
switch($_[4]) {
'X' { $external++; break; }
}
switch($_[6]) {
'C' { $conflicted++; break; }
}
switch($_[8]) {
'*' { $incoming++; break; }
}
}
}
$title = ''
if ($settings.EnableWindowTitle) {
$repoName = Split-Path -Leaf (Split-Path $svnDir)
$prefix = if ($settings.EnableWindowTitle -is [string]) { $settings.EnableWindowTitle } else { '' }
$title = "${prefix}${repoName} [$($branch)]"
}
return New-Object PSObject -Property @{
SvnDir = $svnDir
Title = $title;
Added = $added;
Modified = $modified + $replaced;
Deleted = $deleted;
HasIndex = [bool]($added -or $modified -or $replaced -or $deleted)
Untracked = $untracked;
Missing = $missing;
Obstructed = $obstructed;
Conflicted = $conflicted;
HasWorking = [bool]($untracked -or $missing -or $obstructed -or $conflicted)
External = $external;
Incoming = $incoming
Url = $info.Url
Branch = $branch;
Revision = $info.Revision;
IncomingRevision = $incomingRevision;
}
}
}
function Get-SvnBranchName($info) {
if (!$info -or !$info.Url) { return }
$pathBits = $info.Url.AbsolutePath.Split("/", [StringSplitOptions]::RemoveEmptyEntries)
for ($i = 0; $i -lt $pathBits.length; $i++) {
switch -regex ($pathBits[$i]) {
"trunk" {
return $pathBits[$i]
}
"branches|tags" {
$next = $i + 1
if ($next -lt $pathBits.Length) {
return $pathBits[$next]
}
}
}
}
# Just return the relative URL for the root path.
# (In practice we just do current directory so don't bother seeing if our path is the root)
$rootInfo = Get-SvnInfo $info['Working Copy Root Path']
return $rootInfo['Relative URL']
}
function tsvn {
if ($args) {
if ($args[0] -eq "help") {
#I don't like the built in help behaviour!
$tsvnCommands.keys | Sort-Object | ForEach-Object { write-host $_ }
return
}
$newArgs = @()
$newArgs += "/command:" + $args[0]
$cmd = $tsvnCommands[$args[0]]
if ($cmd -and $cmd.useCurrentDirectory) {
$newArgs += "/path:."
}
if ($args.length -gt 1) {
$args[1..$args.length] | % { $newArgs += $_ }
}
tortoiseproc $newArgs
}
}
function Find-SvnCommand([object[]] $ArgumentList) {
return $ArgumentList |
Where-Object { $_ -and ($_ -notlike '-*') } |
Select-Object -First 1
}
# Paginate svn commands that should have it.
# svn doesn't have this built in (as of 1.9.7) so we have to do it ourselves.
$pagerCommands = @('diff', 'help', 'log')
function Invoke-Svn {
if ($Env:PAGER) {
# Set local output encoding to CONSOLE output to make sure BOMs don't show up in the UI if the user set their encoding to UTF8/UTF16
$OutputEncoding = [System.Console]::OutputEncoding
$command = Find-SvnCommand -ArgumentList $args
if ($pagerCommands -contains $command) {
return & $svn $args | & $Env:PAGER
}
}
& $svn $args
}
New-Alias -Name 'svn' -Value 'Invoke-Svn'
function Get-AliasPattern($exe) {
$aliases = @($exe) + @(Get-Alias | Where-Object { $_.Definition -eq $exe } | Select-Object -Exp Name)
"($($aliases -join '|'))"
}
#
# Console colors
#
if (!(Get-Command Test-ConsoleColor -ErrorAction Ignore)) {
<# PRIVATE Tests that the value is a valid ConsoleColor #>
function Test-ConsoleColor {
[OutputType([bool])]
param (
[Parameter(Position = 0)]
[Nullable[ConsoleColor]] $Color
)
return $Color -ge 0
}
}