forked from JeremySkinner/posh-hg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHgTabExpansion.ps1
227 lines (196 loc) · 5.1 KB
/
HgTabExpansion.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
$script:hgCommands = @()
function HgTabExpansion($lastBlock) {
switch -regex ($lastBlock) {
#handles hgtk help <cmd>
#handles hgtk <cmd>
'thg (help )?(\S*)$' {
thgCommands($matches[2]);
}
#handles hg update <branch name>
#handles hg merge <branch name>
'hg (up|update|merge|co|checkout) (\S*)$' {
findBranchOrBookmark($matches[2])
}
#Handles hg pull -B <bookmark>
'hg pull (-\S* )*-(B) (\S*)$' {
hgRemoteBookmarks($matches[3])
hgLocalBookmarks($matches[3])
}
#Handles hg push -B <bookmark>
'hg push (-\S* )*-(B) (\S*)$' {
hgLocalBookmarks($matches[3])
}
#Handles hg bookmark <bookmark>
'hg (book|bookmark) (\S*)$' {
hgLocalBookmarks($matches[2])
}
#Handles hg push <path>
#Handles hg pull <path>
'hg (push|pull) (-\S* )*(\S*)$' {
hgRemotes($matches[3])
}
#handles hg help <cmd>
#handles hg <cmd>
'hg (help )?(\S*)$' {
hgCommands($matches[2]);
}
#handles hg <cmd> --<option>
'hg (\S+) (-\S* )*--(\S*)$' {
hgOptions $matches[1] $matches[3];
}
#handles hg revert <path>
'hg revert (\S*)$' {
hgFiles $matches[1] 'M|A|R|!'
}
#handles hg add <path>
'hg add (\S*)$' {
hgFiles $matches[1] '\?'
}
# handles hg diff <path>
'hg diff (\S*)$' {
hgFiles $matches[1] 'M'
}
# handles hg commit -(I|X) <path>
'hg commit (\S* )*-(I|X) (\S*)$' {
hgFiles $matches[3] 'M|A|R|!'
}
}
}
function hgFiles($filter, $pattern) {
hg status |
foreach {
if($_ -match "($pattern){1} (.*)") {
$matches[2]
}
} |
where { $_ -like "*$filter*" } |
foreach { if($_ -like '* *') { "'$_'" } else { $_ } }
}
function hgRemotes($filter) {
hg paths | foreach {
$path = $_.Split("=")[0].Trim();
if($filter -and $path.StartsWith($filter)) {
$path
} elseif(-not $filter) {
$path
}
}
}
# By default the hg command list is populated the first time hgCommands is invoked.
# Invoke PopulateHgCommands in your profile if you don't want the initial hit.
function hgCommands($filter) {
if($script:hgCommands.Length -eq 0) {
populateHgCommands
}
if($filter) {
$hgCommands | ? { $_.StartsWith($filter) } | % { $_.Trim() } | sort
}
else {
$hgCommands | % { $_.Trim() } | sort
}
}
# By default the hg command list is populated the first time hgCommands is invoked.
# Invoke PopulateHgCommands in your profile if you don't want the initial hit.
function PopulateHgCommands() {
$hgCommands = foreach($cmd in (hg help)) {
# Stop once we reach the "Enabled Extensions" section of help.
# Not sure if there's a better way to do this...
if($cmd -eq "enabled extensions:") {
break
}
if($cmd -match '^ (\S+) (.*)') {
$matches[1]
}
}
if($global:PoshHgSettings.ShowPatches) {
# MQ integration must be explicitly enabled as the user may not have the extension
$hgCommands += (hg help mq) | % {
if($_ -match '^ (\S+) (.*)') {
$matches[1]
}
}
}
$script:hgCommands = $hgCommands
}
function findBranchOrBookmark($filter){
hgLocalBranches($filter)
hgLocalBookmarks($filter)
}
function hgLocalBranches($filter) {
hg branches -a | foreach {
if($_ -match "(\S+) .*") {
if($filter -and $matches[1].StartsWith($filter)) {
$matches[1]
}
elseif(-not $filter) {
$matches[1]
}
}
}
}
function bookmarkName($bookmark) {
$split = $bookmark.Split(" ");
if($bookmark.StartsWith("*")) {
$split[1]
}
else{
$split[0]
}
}
function hgLocalBookmarks($filter) {
hg bookmarks | foreach {
if($_ -match "(\S+) .*") {
$bookmark = bookmarkName($matches[0])
if($filter -and $bookmark.StartsWith($filter)) {
$bookmark
}
elseif(-not $filter) {
$bookmark
}
}
}
}
function hgRemoteBookmarks($filter) {
hg incoming -B | foreach {
if($_ -match "(\S+) .*") {
if($filter -and $matches[1].StartsWith($filter)) {
$matches[1]
}
elseif(-not $filter) {
$matches[1]
}
}
}
}
function hgOptions($cmd, $filter) {
$optList = @()
$output = hg help $cmd
foreach($line in $output) {
if($line -match '^ ((-\S)| ) --(\S+) .*$') {
$opt = $matches[3]
if($filter -and $opt.StartsWith($filter)) {
$optList += '--' + $opt.Trim()
}
elseif(-not $filter) {
$optList += '--' + $opt.Trim()
}
}
}
$optList | sort
}
function thgCommands($filter) {
$cmdList = @()
$output = thg help
foreach($line in $output) {
if($line -match '^ (\S+) (.*)') {
$cmd = $matches[1]
if($filter -and $cmd.StartsWith($filter)) {
$cmdList += $cmd.Trim()
}
elseif(-not $filter) {
$cmdList += $cmd.Trim()
}
}
}
$cmdList | sort
}