-
Notifications
You must be signed in to change notification settings - Fork 2
/
Enable-GitTabExpansion.ps1
84 lines (73 loc) · 1.99 KB
/
Enable-GitTabExpansion.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
# git tab expansion by Jeremy Skinner
# http://www.jeremyskinner.co.uk/2010/03/07/using-git-with-windows-powershell/
function TabExpansion($line, $lastWord) {
$LineBlocks = [regex]::Split($line, '[|;]')
$lastBlock = $LineBlocks[-1]
switch -regex ($lastBlock) {
'git (.*)' { gitTabExpansion($lastBlock) }
}
}
function gitTabExpansion($lastBlock) {
switch -regex ($lastBlock) {
#Handles git branch -x -y -z <branch name>
'git branch -(d|D) (\S*)$' {
gitLocalBranches($matches[2])
}
#handles git checkout <branch name>
#handles git merge <brancj name>
'git (checkout|merge) (\S*)$' {
gitLocalBranches($matches[2])
}
#handles git <cmd>
#handles git help <cmd>
'git (help )?(\S*)$' {
gitCommands($matches[2])
}
#handles git push remote <branch>
#handles git pull remote <branch>
'git (push|pull) (\S+) (\S*)$' {
gitLocalBranches($matches[3])
}
#handles git pull <remote>
#handles git push <remote>
'git (push|pull) (\S*)$' {
gitRemotes($matches[2])
}
}
}
function gitCommands($filter) {
$cmdList = @()
$output = git 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
}
function gitRemotes($filter) {
if($filter) {
git remote | where { $_.StartsWith($filter) }
}
else {
git remote
}
}
function gitLocalBranches($filter) {
git branch | foreach {
if($_ -match "^\*?\s*(.*)") {
if($filter -and $matches[1].StartsWith($filter)) {
$matches[1]
}
elseif(-not $filter) {
$matches[1]
}
}
}
}