Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use --list for branch and tag tab expansion #864

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions src/GitTabExpansion.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,19 @@ filter quoteStringWithSpecialChars {
}
}

function script:getGitFilter($filter) {
# There is a difference between POSIX and Windows on how
# `git --list "*"` behaves. On Windows, it lists everything.
# On POSIX-systems, it lists nothing.
# Both system types agree that `git --list ""` should return
# everything, so this takes care of both.
#
# Only use this function when the $filter stands alone,
# i.e. do not use here: `git branch --remotes --list "$remote/$filter"`

if ($filter) { "$filter*" } else { "" }
}

function script:gitCommands($filter, $includeAliases) {
$cmdList = @()
if (-not $global:GitTabSettings.AllCommands) {
Expand Down Expand Up @@ -136,8 +149,10 @@ function script:gitBranches($filter, $includeHEAD = $false, $prefix = '') {
$filter = $matches['to']
}

$branches = @(git branch --no-color | ForEach-Object { if (($_ -notmatch "^\* \(HEAD detached .+\)$") -and ($_ -match "^[\*\+]?\s*(?<ref>\S+)(?: -> .+)?")) { $matches['ref'] } }) +
@(git branch --no-color -r | ForEach-Object { if ($_ -match "^ (?<ref>\S+)(?: -> .+)?") { $matches['ref'] } }) +
$gitFilter = getGitFilter $filter

$branches = @(git branch --no-color --list $gitFilter | ForEach-Object { if (($_ -notmatch "^\* \(HEAD detached .+\)$") -and ($_ -match "^[\*\+]?\s*(?<ref>\S+)(?: -> .+)?")) { $matches['ref'] } }) +
@(git branch --no-color --remotes --list $gitFilter | ForEach-Object { if ($_ -match "^ (?<ref>\S+)(?: -> .+)?") { $matches['ref'] } }) +
@(if ($includeHEAD) { 'HEAD','FETCH_HEAD','ORIG_HEAD','MERGE_HEAD' })

$branches |
Expand All @@ -147,7 +162,9 @@ function script:gitBranches($filter, $includeHEAD = $false, $prefix = '') {
}

function script:gitRemoteUniqueBranches($filter) {
git branch --no-color -r |
$gitFilter = getGitFilter $filter

git branch --no-color --remotes --list $gitFilter |
ForEach-Object { if ($_ -match "^ (?<remote>[^/]+)/(?<branch>\S+)(?! -> .+)?$") { $matches['branch'] } } |
Group-Object -NoElement |
Where-Object { $_.Count -eq 1 } |
Expand All @@ -169,24 +186,26 @@ function script:gitConfigKeys($section, $filter, $defaultOptions = '') {
}

function script:gitTags($filter, $prefix = '') {
git tag |
Where-Object { $_ -like "$filter*" } |
$gitFilter = getGitFilter $filter

git tag --list $gitFilter |
ForEach-Object { $prefix + $_ } |
quoteStringWithSpecialChars
}

function script:gitFeatures($filter, $command) {
$gitFilter = getGitFilter $filter

$featurePrefix = git config --local --get "gitflow.prefix.$command"
$branches = @(git branch --no-color | ForEach-Object { if ($_ -match "^\*?\s*$featurePrefix(?<ref>.*)") { $matches['ref'] } })
$branches = @(git branch --no-color --list $gitFilter | ForEach-Object { if ($_ -match "^\*?\s*$featurePrefix(?<ref>.*)") { $matches['ref'] } })
$branches |
Where-Object { $_ -ne '(no branch)' -and $_ -like "$filter*" } |
ForEach-Object { $featurePrefix + $_ } |
quoteStringWithSpecialChars
}

function script:gitRemoteBranches($remote, $ref, $filter, $prefix = '') {
git branch --no-color -r |
Where-Object { $_ -like " $remote/$filter*" } |
git branch --no-color --remotes --list "$remote/$filter*" |
ForEach-Object { $prefix + $ref + ($_ -replace " $remote/","") } |
quoteStringWithSpecialChars
}
Expand Down
188 changes: 94 additions & 94 deletions test/GitParamTabExpansion.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,85 +6,85 @@ Describe 'ParamsTabExpansion Tests' {
Context 'Push Parameters TabExpansion Tests' {
It 'Tab completes all long push parameters' {
$result = & $module GitTabExpansionInternal 'git push --'
$result -contains '--all' | Should -Be $true
$result -contains '--delete' | Should -Be $true
$result -contains '--dry-run' | Should -Be $true
$result -contains '--exec=' | Should -Be $true
$result -contains '--follow-tags' | Should -Be $true
$result -contains '--force' | Should -Be $true
$result -contains '--force-with-lease' | Should -Be $true
$result -contains '--mirror' | Should -Be $true
$result -contains '--no-force-with-lease' | Should -Be $true
$result -contains '--no-thin' | Should -Be $true
$result -contains '--no-verify' | Should -Be $true
$result -contains '--porcelain' | Should -Be $true
$result -contains '--progress' | Should -Be $true
$result -contains '--prune' | Should -Be $true
$result -contains '--quiet' | Should -Be $true
$result -contains '--receive-pack=' | Should -Be $true
$result -contains '--recurse-submodules=' | Should -Be $true
$result -contains '--repo=' | Should -Be $true
$result -contains '--set-upstream' | Should -Be $true
$result -contains '--tags' | Should -Be $true
$result -contains '--thin' | Should -Be $true
$result -contains '--verbose' | Should -Be $true
$result -contains '--verify' | Should -Be $true
$result | Should -Contain '--all'
$result | Should -Contain '--delete'
$result | Should -Contain '--dry-run'
$result | Should -Contain '--exec='
$result | Should -Contain '--follow-tags'
$result | Should -Contain '--force'
$result | Should -Contain '--force-with-lease'
$result | Should -Contain '--mirror'
$result | Should -Contain '--no-force-with-lease'
$result | Should -Contain '--no-thin'
$result | Should -Contain '--no-verify'
$result | Should -Contain '--porcelain'
$result | Should -Contain '--progress'
$result | Should -Contain '--prune'
$result | Should -Contain '--quiet'
$result | Should -Contain '--receive-pack='
$result | Should -Contain '--recurse-submodules='
$result | Should -Contain '--repo='
$result | Should -Contain '--set-upstream'
$result | Should -Contain '--tags'
$result | Should -Contain '--thin'
$result | Should -Contain '--verbose'
$result | Should -Contain '--verify'
}
It 'Tab completes all short push parameters' {
$result = & $module GitTabExpansionInternal 'git push -'
$result -contains '-f' | Should -Be $true
$result -contains '-n' | Should -Be $true
$result -contains '-q' | Should -Be $true
$result -contains '-u' | Should -Be $true
$result -contains '-v' | Should -Be $true
$result | Should -Contain '-f'
$result | Should -Contain '-n'
$result | Should -Contain '-q'
$result | Should -Contain '-u'
$result | Should -Contain '-v'
}
It 'Tab completes push parameters values' {
$result = & $module GitTabExpansionInternal 'git push --recurse-submodules='
$result -contains '--recurse-submodules=check' | Should -Be $true
$result -contains '--recurse-submodules=on-demand' | Should -Be $true
$result | Should -Contain '--recurse-submodules=check'
$result | Should -Contain '--recurse-submodules=on-demand'
}
}

Context 'Pretty/Format TabCompletion Tests - No Custom Formats' {
It 'Tab completes default formats for log --pretty' {
$result = & $module GitTabExpansionInternal 'git log --pretty='
$result -contains '--pretty=oneline' | Should -Be $true
$result -contains '--pretty=short' | Should -Be $true
$result -contains '--pretty=medium' | Should -Be $true
$result -contains '--pretty=full' | Should -Be $true
$result -contains '--pretty=fuller' | Should -Be $true
$result -contains '--pretty=email' | Should -Be $true
$result -contains '--pretty=raw' | Should -Be $true
$result | Should -Contain '--pretty=oneline'
$result | Should -Contain '--pretty=short'
$result | Should -Contain '--pretty=medium'
$result | Should -Contain '--pretty=full'
$result | Should -Contain '--pretty=fuller'
$result | Should -Contain '--pretty=email'
$result | Should -Contain '--pretty=raw'
}
It 'Tab completes default formats for log --format' {
$result = & $module GitTabExpansionInternal 'git log --format='
$result -contains '--format=oneline' | Should -Be $true
$result -contains '--format=short' | Should -Be $true
$result -contains '--format=medium' | Should -Be $true
$result -contains '--format=full' | Should -Be $true
$result -contains '--format=fuller' | Should -Be $true
$result -contains '--format=email' | Should -Be $true
$result -contains '--format=raw' | Should -Be $true
$result | Should -Contain '--format=oneline'
$result | Should -Contain '--format=short'
$result | Should -Contain '--format=medium'
$result | Should -Contain '--format=full'
$result | Should -Contain '--format=fuller'
$result | Should -Contain '--format=email'
$result | Should -Contain '--format=raw'
}
It 'Tab completes default formats for show --pretty' {
$result = & $module GitTabExpansionInternal 'git show --pretty='
$result -contains '--pretty=oneline' | Should -Be $true
$result -contains '--pretty=short' | Should -Be $true
$result -contains '--pretty=medium' | Should -Be $true
$result -contains '--pretty=full' | Should -Be $true
$result -contains '--pretty=fuller' | Should -Be $true
$result -contains '--pretty=email' | Should -Be $true
$result -contains '--pretty=raw' | Should -Be $true
$result | Should -Contain '--pretty=oneline'
$result | Should -Contain '--pretty=short'
$result | Should -Contain '--pretty=medium'
$result | Should -Contain '--pretty=full'
$result | Should -Contain '--pretty=fuller'
$result | Should -Contain '--pretty=email'
$result | Should -Contain '--pretty=raw'
}
It 'Tab completes default formats for show --format' {
$result = & $module GitTabExpansionInternal 'git show --format='
$result -contains '--format=oneline' | Should -Be $true
$result -contains '--format=short' | Should -Be $true
$result -contains '--format=medium' | Should -Be $true
$result -contains '--format=full' | Should -Be $true
$result -contains '--format=fuller' | Should -Be $true
$result -contains '--format=email' | Should -Be $true
$result -contains '--format=raw' | Should -Be $true
$result | Should -Contain '--format=oneline'
$result | Should -Contain '--format=short'
$result | Should -Contain '--format=medium'
$result | Should -Contain '--format=full'
$result | Should -Contain '--format=fuller'
$result | Should -Contain '--format=email'
$result | Should -Contain '--format=raw'
}
}

Expand All @@ -103,51 +103,51 @@ Describe 'ParamsTabExpansion Tests' {

It 'Tab completes default and custom formats for log --pretty' {
$result = & $module GitTabExpansionInternal 'git log --pretty='
$result -contains '--pretty=oneline' | Should -Be $true
$result -contains '--pretty=short' | Should -Be $true
$result -contains '--pretty=medium' | Should -Be $true
$result -contains '--pretty=full' | Should -Be $true
$result -contains '--pretty=fuller' | Should -Be $true
$result -contains '--pretty=email' | Should -Be $true
$result -contains '--pretty=raw' | Should -Be $true
$result -contains '--pretty=birdseye' | Should -Be $true
$result -contains '--pretty=test2' | Should -Be $true
$result | Should -Contain '--pretty=oneline'
$result | Should -Contain '--pretty=short'
$result | Should -Contain '--pretty=medium'
$result | Should -Contain '--pretty=full'
$result | Should -Contain '--pretty=fuller'
$result | Should -Contain '--pretty=email'
$result | Should -Contain '--pretty=raw'
$result | Should -Contain '--pretty=birdseye'
$result | Should -Contain '--pretty=test2'
}
It 'Tab completes default and custom formats for log --format' {
$result = & $module GitTabExpansionInternal 'git log --format='
$result -contains '--format=oneline' | Should -Be $true
$result -contains '--format=short' | Should -Be $true
$result -contains '--format=medium' | Should -Be $true
$result -contains '--format=full' | Should -Be $true
$result -contains '--format=fuller' | Should -Be $true
$result -contains '--format=email' | Should -Be $true
$result -contains '--format=raw' | Should -Be $true
$result -contains '--format=birdseye' | Should -Be $true
$result -contains '--format=test2' | Should -Be $true
$result | Should -Contain '--format=oneline'
$result | Should -Contain '--format=short'
$result | Should -Contain '--format=medium'
$result | Should -Contain '--format=full'
$result | Should -Contain '--format=fuller'
$result | Should -Contain '--format=email'
$result | Should -Contain '--format=raw'
$result | Should -Contain '--format=birdseye'
$result | Should -Contain '--format=test2'
}
It 'Tab completes default and custom formats for show --pretty' {
$result = & $module GitTabExpansionInternal 'git show --pretty='
$result -contains '--pretty=oneline' | Should -Be $true
$result -contains '--pretty=short' | Should -Be $true
$result -contains '--pretty=medium' | Should -Be $true
$result -contains '--pretty=full' | Should -Be $true
$result -contains '--pretty=fuller' | Should -Be $true
$result -contains '--pretty=email' | Should -Be $true
$result -contains '--pretty=raw' | Should -Be $true
$result -contains '--pretty=birdseye' | Should -Be $true
$result -contains '--pretty=test2' | Should -Be $true
$result | Should -Contain '--pretty=oneline'
$result | Should -Contain '--pretty=short'
$result | Should -Contain '--pretty=medium'
$result | Should -Contain '--pretty=full'
$result | Should -Contain '--pretty=fuller'
$result | Should -Contain '--pretty=email'
$result | Should -Contain '--pretty=raw'
$result | Should -Contain '--pretty=birdseye'
$result | Should -Contain '--pretty=test2'
}
It 'Tab completes default and custom formats for show --format' {
$result = & $module GitTabExpansionInternal 'git show --format='
$result -contains '--format=oneline' | Should -Be $true
$result -contains '--format=short' | Should -Be $true
$result -contains '--format=medium' | Should -Be $true
$result -contains '--format=full' | Should -Be $true
$result -contains '--format=fuller' | Should -Be $true
$result -contains '--format=email' | Should -Be $true
$result -contains '--format=raw' | Should -Be $true
$result -contains '--format=birdseye' | Should -Be $true
$result -contains '--format=test2' | Should -Be $true
$result | Should -Contain '--format=oneline'
$result | Should -Contain '--format=short'
$result | Should -Contain '--format=medium'
$result | Should -Contain '--format=full'
$result | Should -Contain '--format=fuller'
$result | Should -Contain '--format=email'
$result | Should -Contain '--format=raw'
$result | Should -Contain '--format=birdseye'
$result | Should -Contain '--format=test2'
}
}
}
Expand Down
30 changes: 15 additions & 15 deletions test/GitParamTabExpansionVsts.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,32 @@ Describe 'ParamsTabExpansion VSTS Tests' {

It 'Tab completes git pr create parameters values' {
$result = & $module GitTabExpansionInternal 'git test-vsts-pr create --'
$result -contains '--auto-complete' | Should -Be $true
$result | Should -Contain '--auto-complete'
}
It 'Tab completes git pr create auto-complete parameters values' {
$result = & $module GitTabExpansionInternal 'git test-vsts-pr create --auto-complete --'
$result -contains '--delete-source-branch' | Should -Be $true
$result | Should -Contain '--delete-source-branch'
}

It 'Tab completes git pr show all parameters values' {
$result = & $module GitTabExpansionInternal 'git test-vsts-pr show --'
$result -contains '--' | Should -Be $false
$result -contains '--debug' | Should -Be $true
$result -contains '--help' | Should -Be $true
$result -contains '--output' | Should -Be $true
$result -contains '--query' | Should -Be $true
$result -contains '--verbose' | Should -Be $true
$result | Should -Not -Contain '--'
$result | Should -Contain '--debug'
$result | Should -Contain '--help'
$result | Should -Contain '--output'
$result | Should -Contain '--query'
$result | Should -Contain '--verbose'
}

It 'Tab completes git pr create all short push parameters' {
$result = & $module GitTabExpansionInternal 'git test-vsts-pr create -'
$result -contains '-d' | Should -Be $true
$result -contains '-i' | Should -Be $true
$result -contains '-p' | Should -Be $true
$result -contains '-r' | Should -Be $true
$result -contains '-s' | Should -Be $true
$result -contains '-h' | Should -Be $true
$result -contains '-o' | Should -Be $true
$result | Should -Contain '-d'
$result | Should -Contain '-i'
$result | Should -Contain '-p'
$result | Should -Contain '-r'
$result | Should -Contain '-s'
$result | Should -Contain '-h'
$result | Should -Contain '-o'
}
}
}
Loading