Skip to content

Commit

Permalink
Merge pull request #422 from dahlbyk/rkeithhill/is421-fix-tabexp-dupe…
Browse files Browse the repository at this point in the history
…-aliases

Fix issue with tab expansion of duplicate aliases.
  • Loading branch information
dahlbyk authored Feb 18, 2017
2 parents ffbe9c1 + 1722202 commit c66402d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 9 deletions.
7 changes: 4 additions & 3 deletions src/GitTabExpansion.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,13 @@ function script:gitAliases($filter) {
$alias
}
}
} | Sort-Object
} | Sort-Object -Unique
}

function script:expandGitAlias($cmd, $rest) {
if ((git config --get-regexp "^alias\.$cmd`$") -match "^alias\.$cmd (?<cmd>[^!].*)`$") {
return "git $($Matches['cmd'])$rest"
$alias = git config "alias.$cmd"
if ($alias) {
return "git $alias$rest"
}
else {
return "git $cmd$rest"
Expand Down
9 changes: 8 additions & 1 deletion test/Shared.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,19 @@ function MakeGitPath([string]$Path) {
$Path -replace '\\', '/'
}

function NewGitTempRepo {
function NewGitTempRepo([switch]$MakeInitialCommit) {
Push-Location
$temp = [System.IO.Path]::GetTempPath()
$repoPath = Join-Path $temp ([IO.Path]::GetRandomFileName())
git.exe init $repoPath *>$null
Set-Location $repoPath

if ($MakeInitialCommit) {
'readme' | Out-File .\README.md -Encoding ascii
git.exe add .\README.md *>$null
git.exe commit -m "initial commit." *>$null
}

$repoPath
}

Expand Down
56 changes: 51 additions & 5 deletions test/TabExpansion.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,60 @@ Describe 'TabExpansion Tests' {
}
}

Context 'PowerShell Special Chars Tests' {
Context 'Alias TabExpansion Tests' {
$addedAliases = @()
function Add-GlobalTestAlias($Name, $Value) {
if (!(git config --global "alias.$Name")) {
git.exe config --global "alias.$Name" $Value
$addedAliases += $Name
}
}
BeforeAll {
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')]
$repoPath = NewGitTempRepo
$repoPath = NewGitTempRepo -MakeInitialCommit
}
AfterAll {
$addedAliases | Where-Object { $_ } | ForEach-Object {
git.exe config --global --unset "alias.$_" 2>$null
}

RemoveGitTempRepo $repoPath
}
It 'Command completion includes unique list of aliases' {
$alias = "test-$(New-Guid)"

Add-GlobalTestAlias $alias config
git.exe config alias.$alias help
(git.exe config --get-all alias.$alias).Count | Should Be 2

$result = @(& $module GitTabExpansionInternal "git $alias")
$result.Count | Should Be 1
$result[0] | Should BeExactly $alias
}
It 'Tab completes when there is one alias of a given name' {
$alias = "test-$(New-Guid)"

'readme' | Out-File .\README.md -Encoding ascii
git.exe add .\README.md
git.exe commit -m "initial commit."
git.exe config alias.$alias checkout
(git.exe config --get-all alias.$alias).Count | Should Be 1

$result = & $module GitTabExpansionInternal "git $alias ma"
$result | Should BeExactly 'master'
}
It 'Tab completes when there are multiple aliases of the same name' {
Add-GlobalTestAlias co checkout

git.exe config alias.co checkout
(git.exe config --get-all alias.co).Count | Should BeGreaterThan 1

$result = & $module GitTabExpansionInternal 'git co ma'
$result | Should BeExactly 'master'
}
}

Context 'PowerShell Special Chars Tests' {
BeforeAll {
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')]
$repoPath = NewGitTempRepo -MakeInitialCommit
}
AfterAll {
RemoveGitTempRepo $repoPath
Expand Down

0 comments on commit c66402d

Please sign in to comment.