Skip to content

Commit

Permalink
Improve tab expansion to handle params for push.
Browse files Browse the repository at this point in the history
Fix #234  BTW if we decide this is an OK fix (need another set of eyes on my $gitParams regex), I'd like to review the fact that all these switch regex case statements fallthrough to the next (observed while debugging tab expansion).  I don't believe that is necessary.  I'm thinking that most (all) of these should have a break statement.

Started a minimum set of Pester tests for tab completion.  Would be good to flesh these out over time.
  • Loading branch information
rkeithhill committed Jan 28, 2017
1 parent 5ada37f commit 84154b9
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 18 deletions.
14 changes: 13 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "PowerShell",
"request": "launch",
"name": "PowerShell Interactive Session"
},
{
"type": "PowerShell",
"request": "attach",
"name": "PowerShell Attach to Host Process",
"processId": "${command.PickPSHostProcess}",
"runspaceId": 1
},
{
"type": "PowerShell",
"request": "launch",
Expand All @@ -10,4 +22,4 @@
"cwd": "${file}"
}
]
}
}
35 changes: 19 additions & 16 deletions src/GitTabExpansion.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ $gitflowsubcommands = @{
}

function script:gitCmdOperations($commands, $command, $filter) {
$commands.$command -split ' ' |
Where-Object { $_ -like "$filter*" }
$commands.$command -split ' ' | Where-Object { $_ -like "$filter*" }
}


Expand All @@ -40,9 +39,9 @@ $script:someCommands = @('add','am','annotate','archive','bisect','blame','branc
'notes','prune','pull','push','rebase','reflog','remote','rerere','reset','revert','rm',
'shortlog','show','stash','status','submodule','svn','tag','whatchanged', 'worktree')
try {
if ($null -ne (git help -a 2>&1 | Select-String flow)) {
$script:someCommands += 'flow'
}
if ($null -ne (git help -a 2>&1 | Select-String flow)) {
$script:someCommands += 'flow'
}
}
catch {
Write-Debug "Search for 'flow' in 'git help' output failed with error: $_"
Expand All @@ -52,7 +51,8 @@ function script:gitCommands($filter, $includeAliases) {
$cmdList = @()
if (-not $global:GitTabSettings.AllCommands) {
$cmdList += $someCommands -like "$filter*"
} else {
}
else {
$cmdList += git help --all |
Where-Object { $_ -match '^ \S.*' } |
ForEach-Object { $_.Split(' ', [StringSplitOptions]::RemoveEmptyEntries) } |
Expand All @@ -62,12 +62,12 @@ function script:gitCommands($filter, $includeAliases) {
if ($includeAliases) {
$cmdList += gitAliases $filter
}

$cmdList | Sort-Object
}

function script:gitRemotes($filter) {
git remote |
Where-Object { $_ -like "$filter*" }
git remote | Where-Object { $_ -like "$filter*" }
}

function script:gitBranches($filter, $includeHEAD = $false, $prefix = '') {
Expand Down Expand Up @@ -180,12 +180,12 @@ function script:expandGitAlias($cmd, $rest) {
}

function GitTabExpansion($lastBlock) {
Invoke-Utf8ConsoleCommand {
GitTabExpansionInternal $lastBlock
}
$res = Invoke-Utf8ConsoleCommand { GitTabExpansionInternal $lastBlock }
$res
}

function GitTabExpansionInternal($lastBlock) {
$gitParams = '(?<params>\s+-(?:[aA-zZ0-9]+|-[aA-zZ0-9][aA-zZ0-9-]*)(?:=\S+)?)*'

if ($lastBlock -match "^$(Get-AliasPattern git) (?<cmd>\S+)(?<args> .*)$") {
$lastBlock = expandGitAlias $Matches['cmd'] $Matches['args']
Expand All @@ -209,7 +209,6 @@ function GitTabExpansionInternal($lastBlock) {
gitCmdOperations $subcommands $matches['cmd'] $matches['op']
}


# Handles git flow <cmd> <op>
"^flow (?<cmd>$($gitflowsubcommands.Keys -join '|'))\s+(?<op>\S*)$" {
gitCmdOperations $gitflowsubcommands $matches['cmd'] $matches['op']
Expand Down Expand Up @@ -258,22 +257,22 @@ function GitTabExpansionInternal($lastBlock) {

# Handles git push remote <ref>:<branch>
# Handles git push remote +<ref>:<branch>
"^push.* (?<remote>\S+) (?<force>\+?)(?<ref>[^\s\:]*\:)(?<branch>\S*)$" {
"^push.* ${gitParams}(?<remote>\S+) (?<force>\+?)(?<ref>[^\s\:]*\:)(?<branch>\S*)$" {
gitRemoteBranches $matches['remote'] $matches['ref'] $matches['branch'] -prefix $matches['force']
}

# Handles git push remote <ref>
# Handles git push remote +<ref>
# Handles git pull remote <ref>
"^(?:push|pull).* (?:\S+) (?<force>\+?)(?<ref>[^\s\:]*)$" {
"^(?:push|pull).* ${gitParams}(?<remote>[^\s-]\S*) (?<force>\+?)(?<ref>[^\s\:]*)$" {
gitBranches $matches['ref'] -prefix $matches['force']
gitTags $matches['ref'] -prefix $matches['force']
}

# Handles git pull <remote>
# Handles git push <remote>
# Handles git fetch <remote>
"^(?:push|pull|fetch).* (?<remote>\S*)$" {
"^(?:push|pull|fetch).* ${gitParams}(?<remote>\S*)$" {
gitRemotes $matches['remote']
}

Expand Down Expand Up @@ -360,6 +359,10 @@ function TabExpansion($line, $lastWord) {
"^$(Get-AliasPattern gitk) (.*)" { GitTabExpansion $lastBlock }

# Fall back on existing tab expansion
default { if (Test-Path Function:\TabExpansionBackup) { TabExpansionBackup $line $lastWord } }
default {
if (Test-Path Function:\TabExpansionBackup) {
TabExpansionBackup $line $lastWord
}
}
}
}
22 changes: 22 additions & 0 deletions test/TabExpansion.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
. $PSScriptRoot\Shared.ps1

Describe 'TabExpansion Tests' {
Context 'Git Push TabExpansion Tests' {
It 'Tab completes remote' {
$result = & $module GitTabExpansionInternal 'git push or'
$result | Should BeExactly 'origin'
}
It 'Tab completes remote and branch' {
$result = & $module GitTabExpansionInternal 'git push origin ma'
$result | Should BeExactly 'master'
}
It 'Tab completes remote with preceding parameters' {
$result = & $module GitTabExpansionInternal 'git push --follow-tags -u or'
$result | Should BeExactly 'origin'
}
It 'Tab completes remote and branch with preceding parameters' {
$result = & $module GitTabExpansionInternal 'git push --follow-tags -u origin ma'
$result | Should BeExactly 'master'
}
}
}
2 changes: 1 addition & 1 deletion test/testDebugHarness.ps1
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@

Invoke-Pester $PSScriptRoot\Utils.Tests.ps1
Invoke-Pester $PSScriptRoot

0 comments on commit 84154b9

Please sign in to comment.