forked from JeremySkinner/posh-hg
-
Notifications
You must be signed in to change notification settings - Fork 1
/
HgUtils.ps1
252 lines (218 loc) · 5.51 KB
/
HgUtils.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
function isHgDirectory() {
if(test-path ".git") {
return $false; #short circuit if git repo
}
if(test-path ".hg") {
return $true;
}
# Test within parent dirs
$checkIn = (Get-Item .).parent
while ($checkIn -ne $NULL) {
$pathToTest = $checkIn.fullname + '/.hg'
if ((Test-Path $pathToTest) -eq $TRUE) {
return $true
} else {
$checkIn = $checkIn.parent
}
}
return $false
}
function Get-HgStatus($getFileStatus=$true, $getBookmarkStatus=$true) {
if(isHgDirectory) {
$untracked = 0
$added = 0
$modified = 0
$deleted = 0
$missing = 0
$renamed = 0
$tags = @()
$commit = ""
$behind = $false
$multipleHeads = $false
$rev = ""
if ($getFileStatus -eq $false) {
hg parent | foreach {
switch -regex ($_) {
'tag:\s*(.*)' { $tags = $matches[1].Replace("(empty repository)", "").Split(" ", [StringSplitOptions]::RemoveEmptyEntries) }
'changeset:\s*(\S*)' { $commit = $matches[1]}
}
}
$branch = hg branch
$behind = $true
$headCount = 0
hg heads $branch | foreach {
switch -regex ($_) {
'changeset:\s*(\S*)'
{
if ($commit -eq $matches[1]) { $behind=$false }
$headCount++
if ($headCount -gt 1) { $multipleHeads=$true }
}
}
}
}
else
{
hg summary | foreach {
switch -regex ($_) {
'parent: (\S*) ?(.*)' { $commit = $matches[1]; $tags = $matches[2].Replace("(empty repository)", "").Split(" ", [StringSplitOptions]::RemoveEmptyEntries) }
'branch: ([\S ]*)' { $branch = $matches[1] }
'update: (\d+)' { $behind = $true }
'pmerge: (\d+) pending' { $behind = $true }
'commit: (.*)' {
$matches[1].Split(",") | foreach {
switch -regex ($_.Trim()) {
'(\d+) modified' { $modified = $matches[1] }
'(\d+) added' { $added = $matches[1] }
'(\d+) removed' { $deleted = $matches[1] }
'(\d+) deleted' { $missing = $matches[1] }
'(\d+) unknown' { $untracked = $matches[1] }
'(\d+) renamed' { $renamed = $matches[1] }
}
}
}
}
}
}
if ($getBookmarkStatus)
{
$active = ""
hg bookmarks | ?{$_} | foreach {
if($_.Trim().StartsWith("*")) {
$split = $_.Split(" ");
$active= $split[2]
}
}
}
$rev = hg log -r . --template '{rev}:{node|short}'
return @{"Untracked" = $untracked;
"Added" = $added;
"Modified" = $modified;
"Deleted" = $deleted;
"Missing" = $missing;
"Renamed" = $renamed;
"Tags" = $tags;
"Commit" = $commit;
"Behind" = $behind;
"MultipleHeads" = $multipleHeads;
"ActiveBookmark" = $active;
"Branch" = $branch
"Revision" = $rev}
}
}
function Get-MqPatches($filter) {
$applied = @()
$unapplied = @()
hg qseries -v | % {
$bits = $_.Split(" ")
$status = $bits[1]
$name = $bits[2]
if($status -eq "A") {
$applied += $name
} else {
$unapplied += $name
}
}
$all = $unapplied + $applied
if($filter) {
$all = $all | ? { $_.StartsWith($filter) }
}
return @{
"All" = $all;
"Unapplied" = $unapplied;
"Applied" = $applied
}
}
function Get-AliasPattern($exe) {
$aliases = @($exe) + @(Get-Alias | where { $_.Definition -eq $exe } | select -Exp Name)
"($($aliases -join '|'))"
}
function hg {
if($args -eq "prp") {
Hg-Prp
} elseif ($($args[0]) -eq "push") {
If ($args.Count -gt 1) {
$passArguments = $args[1 .. ($args.Count - 1)]
} Else {
$passArguments = @()
}
Hg-Push @passArguments
} elseif ($($args[0]) -eq "closebranch") {
Hg-CloseBranch $($args[1]) $($args[2])
} elseif ($($args[0]) -eq "closemerge") {
Hg-CloseMerge $($args[1]) $($args[2])
} elseif ($args -eq "c") {
Hg-Clean
} elseif($args -eq "pr") {
Hg-PullRebase
} elseif ($($args[0]) -eq "pu") {
Hg-PullUpdate $($args[1])
} elseif ($($args[0]) -eq "pm") {
Hg-PullMerge $($args[1])
} else {
hg.exe $args
if($LastExitCode -ne 0) {
Throw "hg.exe failed with an error ($LastExitCode)"
}
}
}
function Hg-Push {
hg.exe push $args
if($LastExitCode -ne 0 -And $LastExitCode -ne 1) {
Throw "Could not push, error ($LastExitCode)"
}
}
function Hg-PullRebase {
hg.exe pull --rebase
if($LastExitCode -ne 0) {
Throw "Could not pull and rebase, error ($LastExitCode)"
}
}
function Hg-PullUpdate ($branch) {
if ($branch -eq "") {
$branch = hg.exe branch
}
hg.exe pull
if($LastExitCode -ne 0) {
Throw "Could not pull, error ($LastExitCode)"
}
hg.exe update $branch
if($LastExitCode -ne 0) {
Throw "Could not update, error ($LastExitCode)"
}
}
function Hg-PullMerge ($branch) {
Hg-PullUpdate
hg.exe merge -r $branch
if($LastExitCode -ne 0) {
Throw "Could not merge, error ($LastExitCode)"
}
}
function Hg-Prp {
Hg-PullRebase
Hg-Push
}
function Hg-CloseBranch($branch, $comment) {
$currentBranch = hg.exe branch
hg.exe debugsetparent $branch
hg.exe branch $branch
hg.exe commit --close-branch -X * -m "$comment"
hg.exe debugsetparent $currentBranch
hg.exe update $currentBranch -C
Hg-Clean
}
function Hg-Clean {
hg.exe update -C
hg.exe purge
}
function Hg-CloseMerge($branch, $comment) {
if (!$branch) {
Throw "Hg-CloseMerge: no branch given!"
}
if (!$comment) {
Throw "Hg-CloseMerge: no comment given!"
}
hg.exe pull --rebase
Hg-CloseBranch $branch $comment
hg.exe merge $branch
}