From eeb1aa01ec1f28ff44938e3960d96fca9e1d3525 Mon Sep 17 00:00:00 2001 From: Mark Embling Date: Thu, 11 Mar 2010 13:08:08 +0000 Subject: [PATCH] Added ability to show advanced git commands if required. Includes any defined aliases in git command completion. Removed tab completion function from GitTabExpansion as it makes it difficult to plug in other tab expansion methods. Updated example profile. --- GitTabExpansion.ps1 | 161 ++++++++++++++++++++++++-------------------- profile.example.ps1 | Bin 1160 -> 1796 bytes 2 files changed, 89 insertions(+), 72 deletions(-) diff --git a/GitTabExpansion.ps1 b/GitTabExpansion.ps1 index 953ebddf1..c53daf3c2 100644 --- a/GitTabExpansion.ps1 +++ b/GitTabExpansion.ps1 @@ -1,48 +1,64 @@ # Initial implementation by Jeremy Skinner # http://www.jeremyskinner.co.uk/2010/03/07/using-git-with-windows-powershell/ -if(-not (Test-Path Function:\DefaultTabExpansion)) { - Rename-Item Function:\TabExpansion DefaultTabExpansion -} - -function script: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() - } +function script:gitCommands($filter, $advanced = $FALSE) { + $cmdList = @() + if (-not $advanced) { + $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() + } + } + } + } else { + $output = git help --all + foreach ($line in $output) { + if ($line -match ' (.+)') { + $lineCmds = $line.Split(' ', [StringSplitOptions]::RemoveEmptyEntries) + foreach ($cmd in $lineCmds) { + if($filter) { + if($filter -and $cmd.StartsWith($filter)) { + $cmdList += $cmd.Trim(); + } + } + else { + $cmdList += $cmd.Trim(); + } + } + } + } } - } - - $cmdList | sort + + $cmdList += gitAliases $filter + $cmdList | sort } function script:gitRemotes($filter) { - if($filter) { - git remote | where { $_.StartsWith($filter) } - } - else { - git remote - } + if($filter) { + git remote | where { $_.StartsWith($filter) } + } + else { + git remote + } } function script:gitLocalBranches($filter) { - git branch | foreach { - if($_ -match "^\*?\s*(.*)") { - if($filter -and $matches[1].StartsWith($filter)) { - $matches[1] - } - elseif(-not $filter) { - $matches[1] + git branch | foreach { + if($_ -match "^\*?\s*(.*)") { + if($filter -and $matches[1].StartsWith($filter)) { + $matches[1] + } + elseif(-not $filter) { + $matches[1] + } } - } - } + } } function script:gitIndex($filter) { @@ -65,57 +81,58 @@ function script:gitFiles($filter) { } } -function gitTabExpansion($line, $lastWord, $lastBlock) { - switch -regex ($lastBlock) { - - #Handles git branch -x -y -z +function script:gitAliases($filter) { + $aliasList = @() + git config --get-regexp alias\..+ | foreach { + $alias = $_.Split(' ', [StringSplitOptions]::RemoveEmptyEntries)[0].Split( + '.', [StringSplitOptions]::RemoveEmptyEntries)[1] + + if($filter -and $alias.StartsWith($filter)) { + $aliasList += $alias.Trim() + } + } + $aliasList | Sort +} + +function GitTabExpansion($lastBlock, $advanced = $FALSE) { + switch -regex ($lastBlock) { + # Handles git branch -d|-D 'git branch -(d|D) (\S*)$' { - gitLocalBranches($matches[2]) + gitLocalBranches $matches[2] } - - #handles git checkout - #handles git merge + + # Handles git checkout + # Handles git merge 'git (checkout|merge) (\S*)$' { - gitLocalBranches($matches[2]) + gitLocalBranches $matches[2] } - - #handles git - #handles git help - 'git (help )?(\S*)$' { - gitCommands($matches[2]) + + # Handles git + # Handles git help + 'git (help )?(\S*)$' { + gitCommands $matches[2] $advanced } - - #handles git push remote - #handles git pull remote + + # Handles git push remote + # Handles git pull remote 'git (push|pull) (\S+) (\S*)$' { - gitLocalBranches($matches[3]) + gitLocalBranches $matches[3] } - - #handles git pull - #handles git push + + # Handles git pull + # Handles git push 'git (push|pull) (\S*)$' { - gitRemotes($matches[2]) + gitRemotes $matches[2] } - #handles git reset HEAD + # Handles git reset HEAD 'git reset HEAD (\S*)$' { - gitIndex($matches[1]) + gitIndex $matches[1] } - #handles git add + # Handles git add 'git add (\S*)$' { - gitFiles($matches[1]) - } - - default { - DefaultTabExpansion $line $lastWord + gitFiles $matches[1] } } -} - -function TabExpansion($line, $lastWord) { - switch -regex ($line) { - '(?:^\s*|[;|]\s*)git (.*)' { gitTabExpansion $line $lastWord $matches[0] } - default { DefaultTabExpansion $line $lastWord } - } -} +} \ No newline at end of file diff --git a/profile.example.ps1 b/profile.example.ps1 index dcf67d019b7417c932274e3c70d2a11d5238ea2f..813c6f76ab24f5e019cd5560c18510317f72ac32 100644 GIT binary patch literal 1796 zcmb`HPjAye5XI+=#CNc0q_l_x^%?{R{s@pjM5GaiibI+>Ey7K$*eM|3s{_Bco2|!* zb3<0*UGLkOdGp@x-#=s9SY=l>uy=N2bDLRhUl^?{vtyfBX$A9|^>J9c;=3RB?7jW4 zx8QGg_7B6#3%*|S%s?z`XfG|dB@tI4YRVKNu31}=>%{Kcz3@9C+v|YLs8f{{RIAk? zD?@v{U7g;o4XL=Y(+e@Uog+@J26#%MSgW=KmS?4zS=|X z<{7iU?C7=UJ0W9D-VMl%xLL@1&8!IZm5#@wfLPF3b@Y;1jh13@x$T-qXMx8WOa($a z#AwmNOM{XsW0#jyvBDLNC))9ThF)Yu)ANbSg}Lo-AD60V&--vWfM>T z)$bPzKUYsx#TveuJ+NotUx?oUr;%8;l|Sr1xk9HYYB-1SW=F+t&+e;v$hT50=_8M6 zHlvF*8sESps@Yb*!8`}8yW}f7)%A0BX{UR@Q*h3&eN=cIs6a85McHU1POE#~k_C$Tvb%g__4Qt+g+7pqa< z6iaHw-RUljCoE-8@BZD%J?55rNDum<;&cWMDdsiu{f`by(bHR8KiEh9Yii&3QDQQq w?pqk>4(R3(_bN`jE!!Lu=XiIej*VJ+HZ}ck?mat$;}li1?MX|yVprn-KX2g{S^xk5 delta 188 zcmZqS>)_nL!8o~qQDL$Ilh$NECMRWH1}+9p24w~X26u)`h7uqhz)-}H&ydSd0OWyX zCm(0>o-D>}!wTW0GOJF0#~dZ9!r%kck;jnA;KYyv)RD}P%}_i!p2d+FsBW@5i@Y{g z6%5e~x(tR4u|S(Z<|#nv$-7xRCr@IPVoaaBmQ{B09Tq9Z+R5y!^&GWeRkaKNN$4b0