Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 228 lines (190 sloc) 7.999 kB
3033fa5 @dahlbyk Rough start
dahlbyk authored
1 # Initial implementation by Jeremy Skinner
2 # http://www.jeremyskinner.co.uk/2010/03/07/using-git-with-windows-powershell/
3
6daf5c8 @dahlbyk Add GitTabSettings
dahlbyk authored
4 $global:GitTabSettings = New-Object PSObject -Property @{
5 AllCommands = $false
6 }
7
9ecab15 @dahlbyk Operation completion for remote and stash
dahlbyk authored
8 $global:ops = @{
b1e1cdd @dahlbyk Add tab expansion for `reflog <op>` and `reflog show <branch name>`. …
dahlbyk authored
9 reflog = 'expire','delete','show'
9ecab15 @dahlbyk Operation completion for remote and stash
dahlbyk authored
10 remote = 'add','rename','rm','set-head','show','prune','update'
11 stash = 'list','show','drop','pop','apply','branch','save','clear','create'
cf77202 @dahlbyk Fix whitespace
dahlbyk authored
12 svn = 'init', 'fetch', 'clone', 'rebase', 'dcommit', 'branch', 'tag', 'log', 'blame', 'find-rev', 'set-tree', 'create-ignore', 'show-ignore', 'mkdirs', 'commit-diff', 'info', 'proplist', 'propget', 'show-externals', 'gc', 'reset'
9ecab15 @dahlbyk Operation completion for remote and stash
dahlbyk authored
13 }
14
15 function script:gitCmdOperations($command, $filter) {
16 $ops.$command |
17 where { $_ -like "$filter*" }
18 }
19
8e118a4 @dahlbyk Hard-code list of standard commands to include more than the basics (…
dahlbyk authored
20 $script:someCommands = @('add','am','annotate','archive','bisect','blame','branch','bundle','checkout','cherry','cherry-pick','citool','clean','clone','commit','config','describe','diff','difftool','fetch','format-patch','gc','grep','gui','help','init','instaweb','log','merge','mergetool','mv','notes','prune','pull','push','rebase','reflog','remote','rerere','reset','revert','rm','shortlog','show','stash','status','submodule','svn','tag','whatchanged')
21
6daf5c8 @dahlbyk Add GitTabSettings
dahlbyk authored
22 function script:gitCommands($filter, $includeAliases) {
eeb1aa0 @markembling Added ability to show advanced git commands if required.
markembling authored
23 $cmdList = @()
6daf5c8 @dahlbyk Add GitTabSettings
dahlbyk authored
24 if (-not $global:GitTabSettings.AllCommands) {
8e118a4 @dahlbyk Hard-code list of standard commands to include more than the basics (…
dahlbyk authored
25 $cmdList += $someCommands -like "$filter*"
eeb1aa0 @markembling Added ability to show advanced git commands if required.
markembling authored
26 } else {
4864330 Simplify tab filtering a bit and make case-insensitive
unknown authored
27 $cmdList += git help --all |
28 where { $_ -match '^ \S.*' } |
29 foreach { $_.Split(' ', [StringSplitOptions]::RemoveEmptyEntries) } |
30 where { $_ -like "$filter*" }
3033fa5 @dahlbyk Rough start
dahlbyk authored
31 }
cf77202 @dahlbyk Fix whitespace
dahlbyk authored
32
06798ba @markembling Don't include aliases when completing 'git help ...'
markembling authored
33 if ($includeAliases) {
34 $cmdList += gitAliases $filter
35 }
eeb1aa0 @markembling Added ability to show advanced git commands if required.
markembling authored
36 $cmdList | sort
3033fa5 @dahlbyk Rough start
dahlbyk authored
37 }
38
39 function script:gitRemotes($filter) {
4864330 Simplify tab filtering a bit and make case-insensitive
unknown authored
40 git remote |
41 where { $_ -like "$filter*" }
3033fa5 @dahlbyk Rough start
dahlbyk authored
42 }
cf77202 @dahlbyk Fix whitespace
dahlbyk authored
43
aaae63a @dahlbyk Rename gitLocalBranches to gitBranches, and include HEAD more liberally
dahlbyk authored
44 function script:gitBranches($filter, $includeHEAD = $false) {
00d7c91 @dahlbyk Add expansion for remote branches
dahlbyk authored
45 if ($filter -match "^(?<from>\S*\.{2,3})(?<to>.*)") {
46 $prefix = $matches['from']
47 $filter = $matches['to']
48 }
17d1a8d @dahlbyk Refactor gitBranches a bit and add FETCH_HEAD, ORIG_HEAD AND MERGE_HE…
dahlbyk authored
49 $branches = @(git branch | foreach { if($_ -match "^\*?\s*(?<ref>.*)") { $matches['ref'] } }) +
00d7c91 @dahlbyk Add expansion for remote branches
dahlbyk authored
50 @(git branch -r | foreach { if($_ -match "^ (?<ref>\S+)(?: -> .+)?") { $matches['ref'] } }) +
17d1a8d @dahlbyk Refactor gitBranches a bit and add FETCH_HEAD, ORIG_HEAD AND MERGE_HE…
dahlbyk authored
51 @(if ($includeHEAD) { 'HEAD','FETCH_HEAD','ORIG_HEAD','MERGE_HEAD' })
52 $branches |
00d7c91 @dahlbyk Add expansion for remote branches
dahlbyk authored
53 where { $_ -ne '(no branch)' -and $_ -like "$filter*" } |
54 foreach { $prefix + $_ }
3033fa5 @dahlbyk Rough start
dahlbyk authored
55 }
56
8c526cb Stash completion
unknown authored
57 function script:gitStashes($filter) {
58 (git stash list) -replace ':.*','' |
59 where { $_ -like "$filter*" } |
60 foreach { "'$_'" }
61 }
62
3033fa5 @dahlbyk Rough start
dahlbyk authored
63 function script:gitIndex($filter) {
64 if($GitStatus) {
4864330 Simplify tab filtering a bit and make case-insensitive
unknown authored
65 $GitStatus.Index |
2c3f480 @dahlbyk Fix tab completion for file names with spaces
dahlbyk authored
66 where { $_ -like "$filter*" } |
67 foreach { if($_ -like '* *') { "'$_'" } else { $_ } }
3033fa5 @dahlbyk Rough start
dahlbyk authored
68 }
69 }
70
71 function script:gitFiles($filter) {
72 if($GitStatus) {
4864330 Simplify tab filtering a bit and make case-insensitive
unknown authored
73 $GitStatus.Working |
2c3f480 @dahlbyk Fix tab completion for file names with spaces
dahlbyk authored
74 where { $_ -like "$filter*" } |
75 foreach { if($_ -like '* *') { "'$_'" } else { $_ } }
3033fa5 @dahlbyk Rough start
dahlbyk authored
76 }
77 }
78
223c61a @dahlbyk Complete 'git rm' with files deleted from working tree
dahlbyk authored
79 function script:gitDeleted($filter) {
80 if($GitStatus) {
290c601 @dahlbyk Add tab expansion for 'git rm' during deleted/updated merge conflict
dahlbyk authored
81 @($GitStatus.Working.Deleted) + @($GitStatus.Index.Deleted) |
223c61a @dahlbyk Complete 'git rm' with files deleted from working tree
dahlbyk authored
82 where { $_ -like "$filter*" } |
83 foreach { if($_ -like '* *') { "'$_'" } else { $_ } }
84 }
85 }
86
eeb1aa0 @markembling Added ability to show advanced git commands if required.
markembling authored
87 function script:gitAliases($filter) {
5ed95ee @dahlbyk Refactor gitAliases
dahlbyk authored
88 git config --get-regexp ^alias\. | foreach {
89 if($_ -match "^alias\.(?<alias>\S+) .*") {
90 $alias = $Matches['alias']
91 if($alias -like "$filter*") {
92 $alias
93 }
eeb1aa0 @markembling Added ability to show advanced git commands if required.
markembling authored
94 }
5ed95ee @dahlbyk Refactor gitAliases
dahlbyk authored
95 } | Sort
eeb1aa0 @markembling Added ability to show advanced git commands if required.
markembling authored
96 }
97
ed18e09 @dahlbyk Support tab expansion for commands behind simple git aliases
dahlbyk authored
98 function script:expandGitAlias($cmd, $rest) {
221c069 @dahlbyk Fix simple (single-word) alias expansion (hat tip to Will Green)
dahlbyk authored
99 if((git config --get-regexp "^alias\.$cmd`$") -match "^alias\.$cmd (?<cmd>[^!].*)`$") {
ed18e09 @dahlbyk Support tab expansion for commands behind simple git aliases
dahlbyk authored
100 return "git $($Matches['cmd'])$rest"
101 } else {
102 return "git $cmd$rest"
103 }
104 }
105
133fba1 @dahlbyk Revert "Adjust function scoping"
dahlbyk authored
106 function GitTabExpansion($lastBlock) {
86ec620 @MarkBorcherding Expanding on all aliases of git
MarkBorcherding authored
107
7ac4665 @dahlbyk Generalize Get-GitAliasPattern as Get-AliasPattern
dahlbyk authored
108 if($lastBlock -match "^$(Get-AliasPattern git) (?<cmd>\S+)(?<args> .*)$") {
ed18e09 @dahlbyk Support tab expansion for commands behind simple git aliases
dahlbyk authored
109 $lastBlock = expandGitAlias $Matches['cmd'] $Matches['args']
110 }
111
86ec620 @MarkBorcherding Expanding on all aliases of git
MarkBorcherding authored
112 # Handles tgit <command> (tortoisegit)
caf55a9 @dahlbyk Add tab expansion for TortoiseGit (tgit) aliases
dahlbyk authored
113 if($lastBlock -match "^$(Get-AliasPattern tgit) (?<cmd>\S*)$") {
2eb6bb0 @JeremySkinner Add support for TortoiseGit tab expansion
JeremySkinner authored
114 # Need return statement to prevent fall-through.
4b97213 @dahlbyk Use named capture group for tgit
dahlbyk authored
115 return $tortoiseGitCommands | where { $_ -like "$($matches['cmd'])*" }
86ec620 @MarkBorcherding Expanding on all aliases of git
MarkBorcherding authored
116 }
117
7ac4665 @dahlbyk Generalize Get-GitAliasPattern as Get-AliasPattern
dahlbyk authored
118 switch -regex ($lastBlock -replace "^$(Get-AliasPattern git) ","") {
cf77202 @dahlbyk Fix whitespace
dahlbyk authored
119
b1e1cdd @dahlbyk Add tab expansion for `reflog <op>` and `reflog show <branch name>`. …
dahlbyk authored
120 # Handles git reflog <op>
9ecab15 @dahlbyk Operation completion for remote and stash
dahlbyk authored
121 # Handles git remote <op>
122 # Handles git stash <op>
b1e1cdd @dahlbyk Add tab expansion for `reflog <op>` and `reflog show <branch name>`. …
dahlbyk authored
123 # Handles git svn <op>
124 "^(?<cmd>reflog|remote|stash|svn)\s+(?<op>\S*)$" {
2b3269f @dahlbyk Update tab expansion regex to use named capture
dahlbyk authored
125 gitCmdOperations $matches['cmd'] $matches['op']
9ecab15 @dahlbyk Operation completion for remote and stash
dahlbyk authored
126 }
8c526cb Stash completion
unknown authored
127
3087b5e @dahlbyk Remote name completion for git fetch and relevant git remote operations
dahlbyk authored
128 # Handles git remote (rename|rm|set-head|set-branches|set-url|show|prune) <stash>
86ec620 @MarkBorcherding Expanding on all aliases of git
MarkBorcherding authored
129 "^remote.* (?:rename|rm|set-head|set-branches|set-url|show|prune).* (?<remote>\S*)$" {
3087b5e @dahlbyk Remote name completion for git fetch and relevant git remote operations
dahlbyk authored
130 gitRemotes $matches['remote']
131 }
132
8c526cb Stash completion
unknown authored
133 # Handles git stash (show|apply|drop|pop|branch) <stash>
86ec620 @MarkBorcherding Expanding on all aliases of git
MarkBorcherding authored
134 "^stash (?:show|apply|drop|pop|branch).* (?<stash>\S*)$" {
2b3269f @dahlbyk Update tab expansion regex to use named capture
dahlbyk authored
135 gitStashes $matches['stash']
8c526cb Stash completion
unknown authored
136 }
cf77202 @dahlbyk Fix whitespace
dahlbyk authored
137
d1a35c5 @dahlbyk Tab completion for branch rename
dahlbyk authored
138 # Handles git branch -d|-D|-m|-M <branch name>
223c61a @dahlbyk Complete 'git rm' with files deleted from working tree
dahlbyk authored
139 # Handles git branch <branch name> <start-point>
86ec620 @MarkBorcherding Expanding on all aliases of git
MarkBorcherding authored
140 "^branch.* (?<branch>\S*)$" {
aaae63a @dahlbyk Rename gitLocalBranches to gitBranches, and include HEAD more liberally
dahlbyk authored
141 gitBranches $matches['branch']
3033fa5 @dahlbyk Rough start
dahlbyk authored
142 }
cf77202 @dahlbyk Fix whitespace
dahlbyk authored
143
06798ba @markembling Don't include aliases when completing 'git help ...'
markembling authored
144 # Handles git <cmd> (commands & aliases)
86ec620 @MarkBorcherding Expanding on all aliases of git
MarkBorcherding authored
145 "^(?<cmd>\S*)$" {
2b3269f @dahlbyk Update tab expansion regex to use named capture
dahlbyk authored
146 gitCommands $matches['cmd'] $TRUE
06798ba @markembling Don't include aliases when completing 'git help ...'
markembling authored
147 }
cf77202 @dahlbyk Fix whitespace
dahlbyk authored
148
06798ba @markembling Don't include aliases when completing 'git help ...'
markembling authored
149 # Handles git help <cmd> (commands only)
86ec620 @MarkBorcherding Expanding on all aliases of git
MarkBorcherding authored
150 "^help (?<cmd>\S*)$" {
2b3269f @dahlbyk Update tab expansion regex to use named capture
dahlbyk authored
151 gitCommands $matches['cmd'] $FALSE
3033fa5 @dahlbyk Rough start
dahlbyk authored
152 }
cf77202 @dahlbyk Fix whitespace
dahlbyk authored
153
eeb1aa0 @markembling Added ability to show advanced git commands if required.
markembling authored
154 # Handles git push remote <branch>
155 # Handles git pull remote <branch>
86ec620 @MarkBorcherding Expanding on all aliases of git
MarkBorcherding authored
156 "^(?:push|pull).* (?:\S+) (?<branch>\S*)$" {
aaae63a @dahlbyk Rename gitLocalBranches to gitBranches, and include HEAD more liberally
dahlbyk authored
157 gitBranches $matches['branch']
3033fa5 @dahlbyk Rough start
dahlbyk authored
158 }
cf77202 @dahlbyk Fix whitespace
dahlbyk authored
159
eeb1aa0 @markembling Added ability to show advanced git commands if required.
markembling authored
160 # Handles git pull <remote>
161 # Handles git push <remote>
3087b5e @dahlbyk Remote name completion for git fetch and relevant git remote operations
dahlbyk authored
162 # Handles git fetch <remote>
86ec620 @MarkBorcherding Expanding on all aliases of git
MarkBorcherding authored
163 "^(?:push|pull|fetch).* (?<remote>\S*)$" {
2b3269f @dahlbyk Update tab expansion regex to use named capture
dahlbyk authored
164 gitRemotes $matches['remote']
3033fa5 @dahlbyk Rough start
dahlbyk authored
165 }
166
eeb1aa0 @markembling Added ability to show advanced git commands if required.
markembling authored
167 # Handles git reset HEAD <path>
894c0c4 @dahlbyk Make tab expansion less picky about when it will expand; clean up reg…
dahlbyk authored
168 # Handles git reset HEAD -- <path>
86ec620 @MarkBorcherding Expanding on all aliases of git
MarkBorcherding authored
169 "^reset.* HEAD(?:\s+--)? (?<path>\S*)$" {
2b3269f @dahlbyk Update tab expansion regex to use named capture
dahlbyk authored
170 gitIndex $matches['path']
3033fa5 @dahlbyk Rough start
dahlbyk authored
171 }
172
21d8bf9 @dahlbyk Local branch completion for cherry-pick and log
dahlbyk authored
173 # Handles git cherry-pick <commit>
9343ceb @dahlbyk Add branch expansion for diff and show
dahlbyk authored
174 # Handles git diff <commit>
341e65e @dahlbyk Add branch expansion for difftool
dahlbyk authored
175 # Handles git difftool <commit>
21d8bf9 @dahlbyk Local branch completion for cherry-pick and log
dahlbyk authored
176 # Handles git log <commit>
9343ceb @dahlbyk Add branch expansion for diff and show
dahlbyk authored
177 # Handles git show <commit>
86ec620 @MarkBorcherding Expanding on all aliases of git
MarkBorcherding authored
178 "^(?:cherry-pick|diff|difftool|log|show).* (?<commit>\S*)$" {
aaae63a @dahlbyk Rename gitLocalBranches to gitBranches, and include HEAD more liberally
dahlbyk authored
179 gitBranches $matches['commit'] $true
21d8bf9 @dahlbyk Local branch completion for cherry-pick and log
dahlbyk authored
180 }
181
58bf2a7 @dahlbyk Add expansion for git reset <commit>
dahlbyk authored
182 # Handles git reset <commit>
86ec620 @MarkBorcherding Expanding on all aliases of git
MarkBorcherding authored
183 "^reset.* (?<commit>\S*)$" {
aaae63a @dahlbyk Rename gitLocalBranches to gitBranches, and include HEAD more liberally
dahlbyk authored
184 gitBranches $matches['commit'] $true
58bf2a7 @dahlbyk Add expansion for git reset <commit>
dahlbyk authored
185 }
186
eeb1aa0 @markembling Added ability to show advanced git commands if required.
markembling authored
187 # Handles git add <path>
86ec620 @MarkBorcherding Expanding on all aliases of git
MarkBorcherding authored
188 "^add.* (?<files>\S*)$" {
2b3269f @dahlbyk Update tab expansion regex to use named capture
dahlbyk authored
189 gitFiles $matches['files']
3033fa5 @dahlbyk Rough start
dahlbyk authored
190 }
c7511ea @dahlbyk Path completion for 'git checkout -- <tab>'
dahlbyk authored
191
192 # Handles git checkout -- <path>
86ec620 @MarkBorcherding Expanding on all aliases of git
MarkBorcherding authored
193 "^checkout.* -- (?<files>\S*)$" {
2b3269f @dahlbyk Update tab expansion regex to use named capture
dahlbyk authored
194 gitFiles $matches['files']
c7511ea @dahlbyk Path completion for 'git checkout -- <tab>'
dahlbyk authored
195 }
894c0c4 @dahlbyk Make tab expansion less picky about when it will expand; clean up reg…
dahlbyk authored
196
197 # Handles git rm <path>
86ec620 @MarkBorcherding Expanding on all aliases of git
MarkBorcherding authored
198 "^rm.* (?<index>\S*)$" {
2b3269f @dahlbyk Update tab expansion regex to use named capture
dahlbyk authored
199 gitDeleted $matches['index']
894c0c4 @dahlbyk Make tab expansion less picky about when it will expand; clean up reg…
dahlbyk authored
200 }
201
202 # Handles git checkout <branch name>
203 # Handles git merge <branch name>
204 # handles git rebase <branch name>
b1e1cdd @dahlbyk Add tab expansion for `reflog <op>` and `reflog show <branch name>`. …
dahlbyk authored
205 # Handles git reflog show <branch name>
206 "^(?:checkout|merge|rebase|reflog\s+show).*\s(?<branch>\S*)$" {
aaae63a @dahlbyk Rename gitLocalBranches to gitBranches, and include HEAD more liberally
dahlbyk authored
207 gitBranches $matches['branch'] $true
894c0c4 @dahlbyk Make tab expansion less picky about when it will expand; clean up reg…
dahlbyk authored
208 }
8c526cb Stash completion
unknown authored
209 }
2c3f480 @dahlbyk Fix tab completion for file names with spaces
dahlbyk authored
210 }
36d61ca @dahlbyk Export TabExpansion from module instead of defining in profile
dahlbyk authored
211
212 if (Test-Path Function:\TabExpansion) {
213 Rename-Item Function:\TabExpansion TabExpansionBackup
214 }
215
216 function TabExpansion($line, $lastWord) {
217 $lastBlock = [regex]::Split($line, '[|;]')[-1].TrimStart()
218
219 switch -regex ($lastBlock) {
220 # Execute git tab completion for all git-related commands
221 "^$(Get-AliasPattern git) (.*)" { GitTabExpansion $lastBlock }
222 "^$(Get-AliasPattern tgit) (.*)" { GitTabExpansion $lastBlock }
223
224 # Fall back on existing tab expansion
225 default { if (Test-Path Function:\TabExpansionBackup) { TabExpansionBackup $line $lastWord } }
226 }
227 }
Something went wrong with that request. Please try again.