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 dcf67d019..813c6f76a 100644 Binary files a/profile.example.ps1 and b/profile.example.ps1 differ